1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
  25  * Copyright 2016 Jason King.
  26  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
  27  */
  28 
  29 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  30 /*        All Rights Reserved   */
  31 
  32 #ifndef _NFS_EXPORT_H
  33 #define _NFS_EXPORT_H
  34 
  35 #include <nfs/nfs_sec.h>
  36 #include <nfs/auth.h>
  37 #include <sys/vnode.h>
  38 #include <nfs/nfs4.h>
  39 #include <sys/kiconv.h>
  40 #include <sys/avl.h>
  41 
  42 #ifdef _KERNEL
  43 #include <sys/pkp_hash.h> /* for PKP_HASH_SIZE */
  44 #endif /* _KERNEL */
  45 
  46 #ifdef  __cplusplus
  47 extern "C" {
  48 #endif
  49 
  50 /*
  51  * nfs pseudo flavor number is owned by IANA. Need to make sure the
  52  * Solaris specific NFS_FLAVOR_NOMAP number will not overlap with any
  53  * new IANA defined pseudo flavor numbers. The chance for the overlap
  54  * is very small since the growth of new flavor numbers is expected
  55  * to be limited.
  56  */
  57 #define NFS_FLAVOR_NOMAP        999999  /* no nfs flavor mapping */
  58 
  59 /*
  60  * As duplicate flavors can be passed into exportfs in the arguments, we
  61  * allocate a cleaned up array with non duplicate flavors on the stack.
  62  * So we need to know how much to allocate.
  63  */
  64 #define MAX_FLAVORS             6       /* none, sys, dh, krb5, krb5i krb5p */
  65 
  66 /*
  67  * Note: exported_lock is currently used to ensure the integrity of
  68  * the secinfo fields.
  69  */
  70 struct secinfo {
  71         seconfig_t      s_secinfo;      /* /etc/nfssec.conf entry */
  72         unsigned int    s_flags;        /* flags (see below) */
  73         int32_t         s_refcnt;       /* reference count for tracking */
  74                                         /* how many children (self included) */
  75                                         /* use this flavor. */
  76         int             s_window;       /* window */
  77         uint_t          s_rootid;       /* UID to use for authorized roots */
  78         int             s_rootcnt;      /* count of root names */
  79         caddr_t         *s_rootnames;   /* array of root names */
  80                                         /* they are strings for AUTH_DES and */
  81                                         /* rpc_gss_principal_t for RPCSEC_GSS */
  82 };
  83 
  84 #ifdef _SYSCALL32
  85 struct secinfo32 {
  86         seconfig32_t    s_secinfo;      /* /etc/nfssec.conf entry */
  87         uint32_t        s_flags;        /* flags (see below) */
  88         int32_t         s_refcnt;       /* reference count for tracking */
  89                                         /* how many children (self included) */
  90                                         /* use this flavor. */
  91         int32_t         s_window;       /* window */
  92         uint32_t        s_rootid;       /* UID to use for authorized roots */
  93         int32_t         s_rootcnt;      /* count of root names */
  94         caddr32_t       s_rootnames;    /* array of root names */
  95                                         /* they are strings for AUTH_DES and */
  96                                         /* rpc_gss_principal_t for RPCSEC_GSS */
  97 };
  98 #endif /* _SYSCALL32 */
  99 
 100 /*
 101  * security negotiation related
 102  */
 103 
 104 #define SEC_QUERY       0x01    /* query sec modes */
 105 
 106 struct sec_ol {
 107         int             sec_flags;      /* security nego flags */
 108         uint_t          sec_index;      /* index into sec flavor array */
 109 };
 110 
 111 /*
 112  * Per-mode flags (secinfo.s_flags)
 113  */
 114 #define M_RO            0x01    /* exported ro to all */
 115 #define M_ROL           0x02    /* exported ro to all listed */
 116 #define M_RW            0x04    /* exported rw to all */
 117 #define M_RWL           0x08    /* exported ro to all listed */
 118 #define M_ROOT          0x10    /* root list is defined */
 119 #define M_4SEC_EXPORTED 0x20    /* this is an explicitly shared flavor */
 120 #define M_NONE          0x40    /* none list is defined */
 121 #define M_MAP           0x80    /* uidmap and/or gidmap is defined */
 122 
 123 /* invalid secinfo reference count */
 124 #define SEC_REF_INVALID(p) ((p)->s_refcnt < 1)
 125 
 126 /* last secinfo reference */
 127 #define SEC_REF_LAST(p) ((p)->s_refcnt == 1)
 128 
 129 /* sec flavor explicitly shared for the exported node */
 130 #define SEC_REF_EXPORTED(p) ((p)->s_flags & M_4SEC_EXPORTED)
 131 
 132 /* the only reference count left is for referring itself */
 133 #define SEC_REF_SELF(p) (SEC_REF_LAST(p) && SEC_REF_EXPORTED(p))
 134 
 135 /*
 136  * The export information passed to exportfs() (Version 2)
 137  */
 138 #define EX_CURRENT_VERSION 2    /* current version of exportdata struct */
 139 
 140 struct exportdata {
 141         int             ex_version;     /* structure version */
 142         char            *ex_path;       /* exported path */
 143         size_t          ex_pathlen;     /* path length */
 144         int             ex_flags;       /* flags */
 145         unsigned int    ex_anon;        /* uid for unauthenticated requests */
 146         int             ex_seccnt;      /* count of security modes */
 147         struct secinfo  *ex_secinfo;    /* security mode info */
 148         char            *ex_index;      /* index file for public filesystem */
 149         char            *ex_log_buffer; /* path to logging buffer file */
 150         size_t          ex_log_bufferlen;       /* buffer file path len */
 151         char            *ex_tag;        /* tag used to identify log config */
 152         size_t          ex_taglen;      /* tag length */
 153 };
 154 
 155 #ifdef _SYSCALL32
 156 struct exportdata32 {
 157         int32_t         ex_version;     /* structure version */
 158         caddr32_t       ex_path;        /* exported path */
 159         int32_t         ex_pathlen;     /* path length */
 160         int32_t         ex_flags;       /* flags */
 161         uint32_t        ex_anon;        /* uid for unauthenticated requests */
 162         int32_t         ex_seccnt;      /* count of security modes */
 163         caddr32_t       ex_secinfo;     /* security mode info */
 164         caddr32_t       ex_index;       /* index file for public filesystem */
 165         caddr32_t       ex_log_buffer;  /* path to logging buffer file */
 166         int32_t         ex_log_bufferlen;       /* buffer file path len */
 167         caddr32_t       ex_tag;         /* tag used to identify log config */
 168         int32_t         ex_taglen;      /* tag length */
 169 };
 170 #endif /* _SYSCALL32 */
 171 
 172 /*
 173  * exported vfs flags.
 174  */
 175 
 176 #define EX_NOSUID       0x01    /* exported with unsetable set[ug]ids */
 177 #define EX_ACLOK        0x02    /* exported with maximal access if acl exists */
 178 #define EX_PUBLIC       0x04    /* exported with public filehandle */
 179 #define EX_NOSUB        0x08    /* no nfs_getfh or MCL below export point */
 180 #define EX_INDEX        0x10    /* exported with index file specified */
 181 #define EX_LOG          0x20    /* logging enabled */
 182 #define EX_LOG_ALLOPS   0x40    /* logging of all RPC operations enabled */
 183                                 /* by default only operations which affect */
 184                                 /* transaction logging are enabled */
 185 #define EX_PSEUDO       0x80    /* pseudo filesystem export */
 186 #ifdef VOLATILE_FH_TEST
 187 #define EX_VOLFH        0x100   /* XXX nfsv4 fh may expire anytime */
 188 #define EX_VOLRNM       0x200   /* XXX nfsv4 fh expire at rename */
 189 #define EX_VOLMIG       0x400   /* XXX nfsv4 fh expire at migration */
 190 #define EX_NOEXPOPEN    0x800   /* XXX nfsv4 fh no expire with open */
 191 #endif /* VOLATILE_FH_TEST */
 192 
 193 #define EX_CHARMAP      0x1000  /* NFS may need a character set conversion */
 194 #define EX_NOACLFAB     0x2000  /* If set, NFSv2 and v3 servers won't */
 195                                 /* fabricate an aclent_t ACL on file systems */
 196                                 /* that don't support aclent_t ACLs */
 197 #define EX_NOHIDE       0x4000  /* traversable from exported parent */
 198 
 199 #ifdef  _KERNEL
 200 
 201 #define RPC_IDEMPOTENT  0x1     /* idempotent or not */
 202 /*
 203  * Be very careful about which NFS procedures get the RPC_ALLOWANON bit.
 204  * Right now, if this bit is on, we ignore the results of per NFS request
 205  * access control.
 206  */
 207 #define RPC_ALLOWANON   0x2     /* allow anonymous access */
 208 #define RPC_MAPRESP     0x4     /* use mapped response buffer */
 209 #define RPC_AVOIDWORK   0x8     /* do work avoidance for dups */
 210 #define RPC_PUBLICFH_OK 0x10    /* allow use of public filehandle */
 211 
 212 /*
 213  * RPC_ALL is an or of all above bits to be used with "don't care"
 214  * nfsv4 ops. The flags of an nfsv4 request is the bit-AND of the
 215  * per-op flags.
 216  */
 217 #define RPC_ALL (RPC_IDEMPOTENT|RPC_ALLOWANON|RPC_AVOIDWORK|RPC_PUBLICFH_OK)
 218 
 219 
 220 #ifdef VOLATILE_FH_TEST
 221 struct ex_vol_rename {
 222         nfs_fh4_fmt_t vrn_fh_fmt;
 223         struct ex_vol_rename *vrn_next;
 224 };
 225 #endif /* VOLATILE_FH_TEST */
 226 
 227 /*
 228  * An auth cache client entry.  This is the umbrella structure and contains all
 229  * related auth_cache entries in the authc_tree AVL tree.
 230  */
 231 struct auth_cache_clnt {
 232         avl_node_t              authc_link;
 233         struct netbuf           authc_addr;     /* address of the client */
 234         krwlock_t               authc_lock;     /* protects authc_tree */
 235         avl_tree_t              authc_tree;     /* auth_cache entries */
 236 };
 237 
 238 /*
 239  * An auth cache entry can exist in 6 states.
 240  *
 241  * A NEW entry was recently allocated and added to the cache.  It does not
 242  * contain the valid auth state yet.
 243  *
 244  * A WAITING entry is one which is actively engaging the user land mountd code
 245  * to authenticate or re-authenticate it.  The auth state might not be valid
 246  * yet.  The other threads should wait on auth_cv until the retrieving thread
 247  * finishes the retrieval and changes the auth cache entry to FRESH, or NEW (in
 248  * a case this entry had no valid auth state yet).
 249  *
 250  * A REFRESHING entry is one which is actively engaging the user land mountd
 251  * code to re-authenticate the cache entry.  There is currently no other thread
 252  * waiting for the results of the refresh.
 253  *
 254  * A FRESH entry is one which is valid (it is either newly retrieved or has
 255  * been refreshed at least once).
 256  *
 257  * A STALE entry is one which has been detected to be too old.  The transition
 258  * from FRESH to STALE prevents multiple threads from submitting refresh
 259  * requests.
 260  *
 261  * An INVALID entry is one which was either STALE or REFRESHING and was deleted
 262  * out of the encapsulating exi.  Since we can't delete it yet, we mark it as
 263  * INVALID, which lets the refresh thread know not to work on it and free it
 264  * instead.
 265  *
 266  * Note that the auth state of the entry is valid, even if the entry is STALE.
 267  * Just as you can eat stale bread, you can consume a stale cache entry. The
 268  * only time the contents change could be during the transition from REFRESHING
 269  * or WAITING to FRESH.
 270  *
 271  * Valid state transitions:
 272  *
 273  *          alloc
 274  *            |
 275  *            v
 276  *         +-----+
 277  *    +--->| NEW |------>free
 278  *    |    +-----+
 279  *    |       |
 280  *    |       v
 281  *    |  +---------+
 282  *    +<-| WAITING |
 283  *    ^  +---------+
 284  *    |       |
 285  *    |       v
 286  *    |       +<--------------------------+<---------------+
 287  *    |       |                           ^                |
 288  *    |       v                           |                |
 289  *    |   +-------+    +-------+    +------------+    +---------+
 290  *    +---| FRESH |--->| STALE |--->| REFRESHING |--->| WAITING |
 291  *        +-------+    +-------+    +------------+    +---------+
 292  *            |            |              |
 293  *            |            v              |
 294  *            v       +---------+         |
 295  *          free<-----| INVALID |<--------+
 296  *                    +---------+
 297  */
 298 typedef enum auth_state {
 299         NFS_AUTH_FRESH,
 300         NFS_AUTH_STALE,
 301         NFS_AUTH_REFRESHING,
 302         NFS_AUTH_INVALID,
 303         NFS_AUTH_NEW,
 304         NFS_AUTH_WAITING
 305 } auth_state_t;
 306 
 307 /*
 308  * An authorization cache entry
 309  *
 310  * Either the state in auth_state will protect the
 311  * contents or auth_lock must be held.
 312  */
 313 struct auth_cache {
 314         avl_node_t              auth_link;
 315         struct auth_cache_clnt  *auth_clnt;
 316         int                     auth_flavor;
 317         cred_t                  *auth_clnt_cred;
 318         uid_t                   auth_srv_uid;
 319         gid_t                   auth_srv_gid;
 320         uint_t                  auth_srv_ngids;
 321         gid_t                   *auth_srv_gids;
 322         int                     auth_access;
 323         time_t                  auth_time;
 324         time_t                  auth_freshness;
 325         auth_state_t            auth_state;
 326         kmutex_t                auth_lock;
 327         kcondvar_t              auth_cv;
 328 };
 329 
 330 #define AUTH_TABLESIZE  32
 331 
 332 /*
 333  * Structure containing log file meta-data.
 334  */
 335 struct log_file {
 336         unsigned int    lf_flags;       /* flags (see below) */
 337         int             lf_writers;     /* outstanding writers */
 338         int             lf_refcnt;      /* references to this struct */
 339         caddr_t         lf_path;        /* buffer file location */
 340         vnode_t         *lf_vp;         /* vnode for the buffer file */
 341         kmutex_t        lf_lock;
 342         kcondvar_t      lf_cv_waiters;
 343 };
 344 
 345 /*
 346  * log_file and log_buffer flags.
 347  */
 348 #define L_WAITING       0x01            /* flush of in-core data to stable */
 349                                         /* storage in progress */
 350 #define L_PRINTED       0x02            /* error message printed to console */
 351 #define L_ERROR         0x04            /* error condition detected */
 352 
 353 /*
 354  * The logging buffer information.
 355  * This structure may be shared by multiple exportinfo structures,
 356  * if they share the same buffer file.
 357  * This structure contains the basic information about the buffer, such
 358  * as it's location in the filesystem.
 359  *
 360  * 'lb_lock' protects all the fields in this structure except for 'lb_path',
 361  * and 'lb_next'.
 362  * 'lb_path' is a write-once/read-many field which needs no locking, it is
 363  * set before the structure is linked to any exportinfo structure.
 364  * 'lb_next' is protected by the log_buffer_list_lock.
 365  */
 366 struct log_buffer {
 367         unsigned int    lb_flags;       /* L_ONLIST set? */
 368         int             lb_refcnt;      /* references to this struct */
 369         unsigned int    lb_rec_id;      /* used to generate unique id */
 370         caddr_t         lb_path;        /* buffer file pathname */
 371         struct log_file *lb_logfile;    /* points to log_file structure */
 372         kmutex_t        lb_lock;
 373         struct log_buffer       *lb_next;
 374         kcondvar_t      lb_cv_waiters;
 375         caddr_t         lb_records;     /* linked list of records to write */
 376         int             lb_num_recs;    /* # of records to write */
 377         ssize_t         lb_size_queued; /* number of bytes queued for write */
 378 };
 379 
 380 #define LOG_BUFFER_HOLD(lbp)    { \
 381         mutex_enter(&(lbp)->lb_lock); \
 382         (lbp)->lb_refcnt++; \
 383         mutex_exit(&(lbp)->lb_lock); \
 384 }
 385 
 386 #define LOG_BUFFER_RELE(lbp)    { \
 387         log_buffer_rele(lbp); \
 388 }
 389 
 390 /*
 391  * Structure for character set conversion mapping based on client address.
 392  */
 393 struct charset_cache {
 394         struct charset_cache *next;
 395         kiconv_t        inbound;
 396         kiconv_t        outbound;
 397         struct sockaddr client_addr;
 398 };
 399 
 400 /* Forward declarations */
 401 struct exportinfo;
 402 struct exp_visible;
 403 struct svc_req;
 404 
 405 /*
 406  * Treenodes are used to build tree representing every node which is part
 407  * of nfs server pseudo namespace. They are connected with both exportinfo
 408  * and exp_visible struct. They were introduced to avoid lookup of ".."
 409  * in the underlying file system during unshare, which was failing if the
 410  * file system was forcibly unmounted or if the directory was removed.
 411  * One exp_visible_t can be shared via several treenode_t, i.e.
 412  * different tree_vis can point to the same exp_visible_t.
 413  * This will happen if some directory is on two different shared paths:
 414  * E.g. after share /tmp/a/b1 and share /tmp/a/b2 there will be two treenodes
 415  * corresponding to /tmp/a and both will have same value in tree_vis.
 416  *
 417  *
 418  *
 419  *     NEW DATA STRUCT         ORIGINAL DATA STRUCT
 420  *
 421  * ns_root +---+               +----------+
 422  *         | / |               |PSEUDO EXP|-->+---+   +---+   +---+
 423  *         +---+---------  ----+----------+   | a |-->| k |-->| b |
 424  *          /\                                +---+   +---+   +---+
 425  *         /  \                                .       .       .
 426  *     +---+...\.........  .....................       .       .
 427  *    *| a |    \              +----------+            .       .
 428  *     +---+-----\-------  ----|REAL EXP a|            .       .
 429  *       /        \            +----------+            .       .
 430  *      /        +===+...  .............................       .
 431  *     /        *| k |         +----------+                    .
 432  *    /          +===+---  ----|REAL EXP k|                    .
 433  *   /                         +----------+                    .
 434  *  +===+................  .....................................
 435  * *| b |                      +----------+
 436  *  +===+----------------  ----|REAL EXP b|-->+---+
 437  *     \                       +----------+   | d |
 438  *     +===+.............  ...................+---+
 439  *     | d |                   +----------+
 440  *     +===+-------------  ----|PSEUDO EXP|-->+---+   +---+
 441  *     /                       +----------+   | e |-->| g |
 442  * +---+.................  ...................+---+   +---+
 443  * | e |                                              .
 444  * +---+                                              .
 445  *    \                                               .
 446  *    +---+..............  ............................
 447  *   *| g |                    +----------+
 448  *    +---+--------------  ----|REAL EXP g|
 449  *                             +----------+
 450  *
 451  *
 452  *
 453  * +===+               +---+                    +---+
 454  * | b |..mountpoint   | e |..directory/file   *| a |..node is shared
 455  * +===+  (VROOT)      +---+                    +---+
 456  *
 457  *
 458  * Bi-directional interconnect:
 459  * treenode_t::tree_exi ---------  exportinfo_t::exi_tree
 460  * One-way direction connection:
 461  * treenode_t::tree_vis .........> exp_visible_t
 462  */
 463 /* Access to treenode_t is under protection of exported_lock RW_LOCK */
 464 typedef struct treenode {
 465         /* support for generic n-ary trees */
 466         struct treenode *tree_parent;
 467         struct treenode *tree_child_first;
 468         struct treenode *tree_sibling; /* next sibling */
 469         /* private, nfs specific part */
 470         struct exportinfo  *tree_exi;
 471         struct exp_visible *tree_vis;
 472 } treenode_t;
 473 
 474 /*
 475  * TREE_ROOT checks if the node corresponds to a filesystem root
 476  * TREE_EXPORTED checks if the node is explicitly shared
 477  */
 478 
 479 #define TREE_ROOT(t) \
 480         ((t)->tree_exi && (t)->tree_exi->exi_vp->v_flag & VROOT)
 481 
 482 #define TREE_EXPORTED(t) \
 483         ((t)->tree_exi && !PSEUDO((t)->tree_exi))
 484 
 485 #define EXPTABLESIZE   256
 486 
 487 struct exp_hash {
 488         struct exportinfo       *prev;  /* ptr to the previous exportinfo */
 489         struct exportinfo       *next;  /* ptr to the next exportinfo */
 490         struct exportinfo       **bckt; /* backpointer to the hash bucket */
 491 };
 492 
 493 /*
 494  * A node associated with an export entry on the
 495  * list of exported filesystems.
 496  *
 497  * exi_count+exi_lock protects an individual exportinfo from being freed
 498  * when in use.
 499  *
 500  * You must have the writer lock on exported_lock to add/delete an exportinfo
 501  * structure to/from the list.
 502  *
 503  * exi_volatile_dev maps to VSW_VOLATILEDEV.  It means that the
 504  * underlying fs devno can change on each mount.  When set, the server
 505  * should not use va_fsid for a GETATTR(FATTR4_FSID) reply.  It must
 506  * use exi_fsid because it is guaranteed to be persistent.  This isn't
 507  * in any way related to NFS4 volatile filehandles.
 508  *
 509  * The exi_cache_lock protects the exi_cache AVL trees.
 510  */
 511 struct exportinfo {
 512         struct exportdata       exi_export;
 513         fsid_t                  exi_fsid;
 514         struct fid              exi_fid;
 515         struct exp_hash         fid_hash;
 516         struct exp_hash         path_hash;
 517         struct treenode         *exi_tree;
 518         fhandle_t               exi_fh;
 519         krwlock_t               exi_cache_lock;
 520         kmutex_t                exi_lock;
 521         uint_t                  exi_count;
 522         vnode_t                 *exi_vp;
 523         vnode_t                 *exi_dvp;
 524         avl_tree_t              *exi_cache[AUTH_TABLESIZE];
 525         struct log_buffer       *exi_logbuffer;
 526         struct exp_visible      *exi_visible;
 527         struct charset_cache    *exi_charset;
 528         unsigned                exi_volatile_dev:1;
 529         unsigned                exi_moved:1;
 530         int                     exi_id;
 531         avl_node_t              exi_id_link;
 532         zoneid_t                exi_zoneid;
 533 #ifdef VOLATILE_FH_TEST
 534         uint32_t                exi_volatile_id;
 535         struct ex_vol_rename    *exi_vol_rename;
 536         kmutex_t                exi_vol_rename_lock;
 537 #endif /* VOLATILE_FH_TEST -- keep last! */
 538 };
 539 
 540 typedef struct exportinfo exportinfo_t;
 541 typedef struct exportdata exportdata_t;
 542 typedef struct secinfo secinfo_t;
 543 
 544 /*
 545  * exp_visible is a visible list per filesystem. It is for filesystems
 546  * that may need a limited view of its contents. A pseudo export and
 547  * a real export at the mount point (VROOT) which has a subtree shared
 548  * has a visible list.
 549  *
 550  * The exi_visible field is NULL for normal, non-pseudo filesystems
 551  * which do not have any subtree exported. If the field is non-null,
 552  * it points to a list of visible entries, identified by vis_fid and/or
 553  * vis_ino. The presence of a "visible" list means that if this export
 554  * can only have a limited view, it can only view the entries in the
 555  * exp_visible list. The directories in the fid list comprise paths that
 556  * lead to exported directories.
 557  *
 558  * The vis_count field records the number of paths in this filesystem
 559  * that use this directory. The vis_exported field is non-zero if the
 560  * entry is an exported directory (leaf node).
 561  *
 562  * exp_visible itself is not reference counted. Each exp_visible is
 563  * referenced twice:
 564  * 1) from treenode::tree_vis
 565  * 2) linked from exportinfo::exi_visible
 566  * The 'owner' of exp_visible is the exportinfo structure. exp_visible should
 567  * be always freed only from exportinfo_t, never from treenode::tree_vis.
 568  */
 569 
 570 struct exp_visible {
 571         vnode_t                 *vis_vp;
 572         fid_t                   vis_fid;
 573         u_longlong_t            vis_ino;
 574         int                     vis_count;
 575         int                     vis_exported;
 576         struct exp_visible      *vis_next;
 577         struct secinfo          *vis_secinfo;
 578         int                     vis_seccnt;
 579         timespec_t              vis_change;
 580 };
 581 typedef struct exp_visible exp_visible_t;
 582 
 583 #define PSEUDO(exi)     ((exi)->exi_export.ex_flags & EX_PSEUDO)
 584 #define EXP_LINKED(exi) ((exi)->fid_hash.bckt != NULL)
 585 
 586 #define EQFSID(fsidp1, fsidp2)  \
 587         (((fsidp1)->val[0] == (fsidp2)->val[0]) && \
 588             ((fsidp1)->val[1] == (fsidp2)->val[1]))
 589 
 590 #define EQFID(fidp1, fidp2)     \
 591         ((fidp1)->fid_len == (fidp2)->fid_len && \
 592             bcmp((char *)(fidp1)->fid_data, (char *)(fidp2)->fid_data, \
 593             (uint_t)(fidp1)->fid_len) == 0)
 594 
 595 #define exportmatch(exi, fsid, fid)     \
 596         (EQFSID(&(exi)->exi_fsid, (fsid)) && EQFID(&(exi)->exi_fid, (fid)))
 597 
 598 /*
 599  * Returns true iff exported filesystem is read-only to the given host.
 600  *
 601  * Note:  this macro should be as fast as possible since it's called
 602  * on each NFS modification request.
 603  */
 604 #define rdonly(ro, vp)  ((ro) || vn_is_readonly(vp))
 605 #define rdonly4(req, cs)  \
 606         (vn_is_readonly((cs)->vp) || \
 607             (nfsauth4_access((cs)->exi, (cs)->vp, (req), (cs)->basecr, NULL, \
 608             NULL, NULL, NULL) & (NFSAUTH_RO | NFSAUTH_LIMITED)))
 609 
 610 extern int      nfsauth4_access(struct exportinfo *, vnode_t *,
 611     struct svc_req *, cred_t *, uid_t *, gid_t *, uint_t *, gid_t **);
 612 extern int      nfsauth4_secinfo_access(struct exportinfo *,
 613     struct svc_req *, int, int, cred_t *);
 614 extern int      nfsauth_cache_clnt_compar(const void *, const void *);
 615 extern int      nfs_fhbcmp(char *, char *, int);
 616 extern void     nfs_exportinit(void);
 617 extern void     nfs_exportfini(void);
 618 extern void     nfs_export_zone_init(nfs_globals_t *);
 619 extern void     nfs_export_zone_fini(nfs_globals_t *);
 620 extern void     nfs_export_zone_shutdown(nfs_globals_t *);
 621 extern int      nfs_export_get_rootfh(nfs_globals_t *);
 622 extern int      chk_clnt_sec(struct exportinfo *, struct svc_req *);
 623 extern int      makefh(fhandle_t *, struct vnode *, struct exportinfo *);
 624 extern int      makefh_ol(fhandle_t *, struct exportinfo *, uint_t);
 625 extern int      makefh3(nfs_fh3 *, struct vnode *, struct exportinfo *);
 626 extern int      makefh3_ol(nfs_fh3 *, struct exportinfo *, uint_t);
 627 extern vnode_t *nfs_fhtovp(fhandle_t *, struct exportinfo *);
 628 extern vnode_t *nfs3_fhtovp(nfs_fh3 *, struct exportinfo *);
 629 extern struct   exportinfo *checkexport(fsid_t *, struct fid *);
 630 extern struct   exportinfo *checkexport4(fsid_t *, struct fid *, vnode_t *);
 631 extern void     exi_hold(struct exportinfo *);
 632 extern void     exi_rele(struct exportinfo *);
 633 extern struct exportinfo *nfs_vptoexi(vnode_t *, vnode_t *, cred_t *, int *,
 634     int *, bool_t);
 635 extern int      nfs_check_vpexi(vnode_t *, vnode_t *, cred_t *,
 636                         struct exportinfo **);
 637 extern vnode_t *untraverse(vnode_t *);
 638 extern int      vn_is_nfs_reparse(vnode_t *, cred_t *);
 639 extern int      client_is_downrev(struct svc_req *);
 640 extern char    *build_symlink(vnode_t *, cred_t *, size_t *);
 641 
 642 extern fhandle_t nullfh2;       /* for comparing V2 filehandles */
 643 
 644 typedef struct nfs_export {
 645         /* Root of nfs pseudo namespace */
 646         treenode_t *ns_root;
 647 
 648         nfs_globals_t           *ne_globals;    /* "up" pointer */
 649 
 650         struct exportinfo *exptable_path_hash[PKP_HASH_SIZE];
 651         struct exportinfo *exptable[EXPTABLESIZE];
 652 
 653         /*
 654          * Read/Write lock that protects the exportinfo list.  This lock
 655          * must be held when searching or modifiying the exportinfo list.
 656          */
 657         krwlock_t exported_lock;
 658 
 659         /* "public" and default (root) location for public filehandle */
 660         struct exportinfo *exi_public, *exi_root;
 661         /* For checking default public file handle */
 662         fid_t exi_rootfid;
 663         /* For comparing V2 filehandles */
 664         fhandle_t nullfh2;
 665 
 666         /* The change attribute value of the root of nfs pseudo namespace */
 667         timespec_t ns_root_change;
 668 } nfs_export_t;
 669 
 670 /*
 671  * Functions that handle the NFSv4 server namespace
 672  */
 673 extern exportinfo_t *vis2exi(treenode_t *);
 674 extern int      treeclimb_export(struct exportinfo *);
 675 extern void     treeclimb_unexport(nfs_export_t *, struct exportinfo *);
 676 extern int      nfs_visible(struct exportinfo *, vnode_t *, int *);
 677 extern int      nfs_visible_inode(struct exportinfo *, ino64_t,
 678                     struct exp_visible **);
 679 extern int      has_visible(struct exportinfo *, vnode_t *);
 680 extern void     free_visible(struct exp_visible *);
 681 extern int      nfs_exported(struct exportinfo *, vnode_t *);
 682 extern struct exportinfo *pseudo_exportfs(nfs_export_t *, vnode_t *, fid_t *,
 683                     struct exp_visible *, struct exportdata *);
 684 extern int      vop_fid_pseudo(vnode_t *, fid_t *);
 685 extern int      nfs4_vget_pseudo(struct exportinfo *, vnode_t **, fid_t *);
 686 extern bool_t   nfs_visible_change(struct exportinfo *, vnode_t *,
 687                     timespec_t *);
 688 extern void     tree_update_change(nfs_export_t *, treenode_t *, timespec_t *);
 689 extern void     rfs4_clean_state_exi(nfs_export_t *, struct exportinfo *);
 690 
 691 /*
 692  * Functions that handle the NFSv4 server namespace security flavors
 693  * information.
 694  */
 695 extern void     srv_secinfo_exp2pseu(struct exportdata *, struct exportdata *);
 696 extern void     srv_secinfo_list_free(struct secinfo *, int);
 697 
 698 extern nfs_export_t *nfs_get_export();
 699 extern void     export_link(nfs_export_t *, struct exportinfo *);
 700 extern void     export_unlink(nfs_export_t *, struct exportinfo *);
 701 
 702 /*
 703  * exi_id support
 704  */
 705 extern kmutex_t  nfs_exi_id_lock;
 706 extern avl_tree_t exi_id_tree;
 707 extern int exi_id_get_next(void);
 708 
 709 /*
 710  * Two macros for identifying public filehandles.
 711  * A v2 public filehandle is 32 zero bytes.
 712  * A v3 public filehandle is zero length.
 713  */
 714 #define PUBLIC_FH2(fh) \
 715         ((fh)->fh_fsid.val[1] == 0 && \
 716         bcmp((fh), &nullfh2, sizeof (fhandle_t)) == 0)
 717 
 718 #define PUBLIC_FH3(fh) \
 719         ((fh)->fh3_length == 0)
 720 
 721 extern int      makefh4(nfs_fh4 *, struct vnode *, struct exportinfo *);
 722 extern vnode_t *nfs4_fhtovp(nfs_fh4 *, struct exportinfo *, nfsstat4 *);
 723 
 724 #endif /* _KERNEL */
 725 
 726 #ifdef  __cplusplus
 727 }
 728 #endif
 729 
 730 #endif  /* _NFS_EXPORT_H */