Home | History | Annotate | Download | only in nfs
      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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #ifndef _NFS4_H
     27 #define	_NFS4_H
     28 
     29 #include <sys/types.h>
     30 #include <sys/vnode.h>
     31 #include <sys/fem.h>
     32 #include <rpc/rpc.h>
     33 #include <nfs/nfs.h>
     34 
     35 #ifdef _KERNEL
     36 #include <nfs/nfs4_kprot.h>
     37 #include <sys/nvpair.h>
     38 #else
     39 #include <rpcsvc/nfs4_prot.h>
     40 #endif
     41 #include <nfs/nfs4_attr.h>
     42 #include <sys/acl.h>
     43 #include <sys/list.h>
     44 
     45 #ifdef	__cplusplus
     46 extern "C" {
     47 #endif
     48 
     49 #define	NFS4_MAX_UTF8STRING	65536
     50 #define	NFS4_MAX_PATHNAME4	65536
     51 #define	NFS4_MAX_SECOID4	65536
     52 
     53 #ifdef _KERNEL
     54 
     55 typedef struct nfs4_fhandle {
     56 	int fh_len;
     57 	char fh_buf[NFS4_FHSIZE];
     58 } nfs4_fhandle_t;
     59 
     60 #define	NFS4_MINORVERSION 0
     61 #define	CB4_MINORVERSION 0
     62 
     63 /*
     64  * Set the fattr4_change variable using a time struct. Note that change
     65  * is 64 bits, but timestruc_t is 128 bits in a 64-bit kernel.
     66  */
     67 #define	NFS4_SET_FATTR4_CHANGE(change, ts)			\
     68 {							\
     69 	change = (ts).tv_sec;				\
     70 	change <<= 32;					\
     71 	change |= (uint32_t)((ts).tv_nsec);		\
     72 }
     73 
     74 /*
     75  * Server lease period.  Value is in seconds;  Also used for grace period
     76  */
     77 extern time_t rfs4_lease_time;
     78 
     79 /*
     80  * This set of typedefs and interfaces represent the core or base set
     81  * of functionality that backs the NFSv4 server's state related data
     82  * structures.  Since the NFSv4 server needs inter-RPC state to be
     83  * available that is unrelated to the filesystem (in other words,
     84  * soft-state), this functionality is needed to maintain that and is
     85  * written to be somewhat flexible to adapt to the various types of
     86  * data structures contained within the server.
     87  *
     88  * The basic structure at this level is that the server maintains a
     89  * global "database" which consists of a set of tables.  Each table
     90  * contains a set of like data structures.  Each table is indexed by
     91  * at least one hash function and in most cases two hashes.  Each
     92  * table's characteristics is set when it is created at run-time via
     93  * rfs4_table_create().  All table creation and related functions are
     94  * located in nfs4_state.c.  The generic database functionality is
     95  * located in nfs4_db.c.
     96  */
     97 
     98 typedef struct rfs4_dbe rfs4_dbe_t;		/* basic opaque db entry */
     99 typedef struct rfs4_table rfs4_table_t;		/* basic table type */
    100 typedef struct rfs4_index rfs4_index_t;		/* index */
    101 typedef struct rfs4_database rfs4_database_t;	/* and database */
    102 
    103 typedef struct {		/* opaque entry type for later use */
    104 	rfs4_dbe_t *dbe;
    105 } *rfs4_entry_t;
    106 
    107 extern rfs4_table_t *rfs4_client_tab;
    108 
    109 /* database, table, index creation entry points */
    110 extern rfs4_database_t *rfs4_database_create(uint32_t);
    111 extern void		rfs4_database_shutdown(rfs4_database_t *);
    112 extern void		rfs4_database_destroy(rfs4_database_t *);
    113 
    114 extern void		rfs4_database_destroy(rfs4_database_t *);
    115 
    116 extern rfs4_table_t	*rfs4_table_create(rfs4_database_t *, char *,
    117 				time_t, uint32_t,
    118 				bool_t (*create)(rfs4_entry_t, void *),
    119 				void (*destroy)(rfs4_entry_t),
    120 				bool_t (*expiry)(rfs4_entry_t),
    121 				uint32_t, uint32_t, uint32_t, id_t);
    122 extern void		rfs4_table_destroy(rfs4_database_t *, rfs4_table_t *);
    123 extern rfs4_index_t	*rfs4_index_create(rfs4_table_t *, char *,
    124 				uint32_t (*hash)(void *),
    125 				bool_t (compare)(rfs4_entry_t, void *),
    126 				void *(*mkkey)(rfs4_entry_t), bool_t);
    127 extern void		rfs4_index_destroy(rfs4_index_t *);
    128 
    129 /* Type used to direct rfs4_dbsearch() in what types of records to inspect */
    130 typedef enum {RFS4_DBS_VALID, RFS4_DBS_INVALID} rfs4_dbsearch_type_t;
    131 /* search and db entry manipulation entry points */
    132 extern rfs4_entry_t	rfs4_dbsearch(rfs4_index_t *, void *,
    133 				bool_t *, void *, rfs4_dbsearch_type_t);
    134 extern void		rfs4_dbe_lock(rfs4_dbe_t *);
    135 extern void		rfs4_dbe_unlock(rfs4_dbe_t *);
    136 extern clock_t		rfs4_dbe_twait(rfs4_dbe_t *, clock_t);
    137 extern void		rfs4_dbe_cv_broadcast(rfs4_dbe_t *);
    138 extern void		rfs4_dbe_hold(rfs4_dbe_t *);
    139 extern void		rfs4_dbe_hold_nolock(rfs4_dbe_t *);
    140 extern void		rfs4_dbe_rele_nolock(rfs4_dbe_t *);
    141 extern void		rfs4_dbe_rele(rfs4_dbe_t *);
    142 extern uint32_t	rfs4_dbe_refcnt(rfs4_dbe_t *);
    143 extern id_t		rfs4_dbe_getid(rfs4_dbe_t *);
    144 extern void		rfs4_dbe_invalidate(rfs4_dbe_t *);
    145 extern bool_t		rfs4_dbe_is_invalid(rfs4_dbe_t *);
    146 extern time_t		rfs4_dbe_get_timerele(rfs4_dbe_t *);
    147 extern void		rfs4_dbe_hide(rfs4_dbe_t *);
    148 extern void		rfs4_dbe_unhide(rfs4_dbe_t *);
    149 #ifdef DEBUG
    150 extern bool_t		rfs4_dbe_islocked(rfs4_dbe_t *);
    151 #endif
    152 extern void		rfs4_dbe_walk(rfs4_table_t *,
    153 			void (*callout)(rfs4_entry_t, void *), void *);
    154 
    155 /*
    156  * Minimal server stable storage.
    157  *
    158  * Currently the NFSv4 server will only save the client
    159  * ID (the long version) so that it will be able to
    160  * grant possible reclaim requests during the infamous
    161  * grace_period.
    162  */
    163 
    164 #define	RFS4_SS_DIRSIZE	64 * 1024
    165 #define	NFS4_SS_VERSION 1
    166 
    167 /* handy pathname structure */
    168 typedef struct ss_pn {
    169 	char *leaf;
    170 	char pn[MAXPATHLEN];
    171 } rfs4_ss_pn_t;
    172 
    173 /*
    174  * The server will build this link list on startup. It represents the
    175  * clients that have had valid state on the server in a prior instance.
    176  *
    177  */
    178 typedef struct rfs4_oldstate {
    179 	struct rfs4_oldstate 	*next;
    180 	struct rfs4_oldstate 	*prev;
    181 	rfs4_ss_pn_t		*ss_pn;
    182 	nfs_client_id4		cl_id4;
    183 } rfs4_oldstate_t;
    184 
    185 /*
    186  * This union is used to overlay the server's internal treatment of
    187  * the protocols stateid4 datatype.  Therefore, "bits" must not exceed
    188  * the size of stateid4 and more importantly should match the size of
    189  * stateid4.  The chgseq field must the first entry since it overlays
    190  * stateid4.seqid.
    191  */
    192 typedef union {
    193 	stateid4 stateid;
    194 	struct {
    195 		uint32_t chgseq;	/* State changes / protocol's seqid */
    196 		uint32_t boottime;	/* boot time  */
    197 		uint32_t type:2;	/* stateid_type_t as define below */
    198 		uint32_t clnodeid:8;	/* cluster server nodeid */
    199 		uint32_t ident:22;	/* 2^22-1 openowner x fhs */
    200 		pid_t	 pid;		/* pid of corresponding lock owner */
    201 	} bits;
    202 } stateid_t;
    203 /*
    204  * Note that the way the type field above is defined, this enum must
    205  * not have more than 4 members.
    206  */
    207 typedef enum {OPENID, LOCKID, DELEGID} stateid_type_t;
    208 
    209 
    210 /*
    211  * Set of RPC credentials used for a particular operation.
    212  * Used for operations like SETCLIENTID_CONFIRM where the
    213  * credentials needs to match those used at SETCLIENTID.
    214  */
    215 typedef void *cred_set_t;		/* For now XXX */
    216 
    217 /*
    218  * "wait" struct for use in the open open and lock owner state
    219  * structures to provide serialization between server threads that are
    220  * handling requests for the same open owner or lock stateid.  This
    221  * way only one thread will be updating things like sequence ids,
    222  * replay cache and stateid at a time.
    223  */
    224 typedef struct rfs4_state_wait {
    225 	uint32_t		sw_active;
    226 	uint32_t		sw_wait_count;
    227 	kmutex_t		sw_cv_lock[1];
    228 	kcondvar_t		sw_cv[1];
    229 } rfs4_state_wait_t;
    230 
    231 extern void	rfs4_sw_enter(rfs4_state_wait_t *);
    232 extern void	rfs4_sw_exit(rfs4_state_wait_t *);
    233 
    234 /*
    235  * This enum and the following rfs4_cbinfo_t struct are used to
    236  * maintain information about the callback path used from the server
    237  * to client for operations like CB_GETATTR and CB_RECALL.  The
    238  * rfs4_cbinfo_t struct is meant to be encompassed in the client
    239  * struct and managed within that structure's locking scheme.
    240  *
    241  * The various states of the callback path are used by the server to
    242  * determine if delegations should initially be provided to a client
    243  * and then later on if connectivity has been lost and delegations
    244  * should be revoked.
    245  */
    246 
    247 /*
    248  * CB_NOCHANGE - Special value used for interfaces within the delegation
    249  *		code to signify that "no change" has occurred to the
    250  *		callback path
    251  * CB_UNINIT	- No callback info provided by the client
    252  * CB_NONE	- Callback info provided but CB_NULL call
    253  *		  has yet to be attempted
    254  * CB_OK	- Callback path tested with CB_NULL with success
    255  * CB_INPROG	- Callback path currently being tested with CB_NULL
    256  * CB_FAILED	- Callback path was == CB_OK but has failed
    257  *		  with timeout/rpc error
    258  * CB_BAD	- Callback info provided but CB_NULL failed
    259  */
    260 typedef enum {
    261 	CB_NOCHANGE = 0,
    262 	CB_UNINIT = 1,
    263 	CB_NONE = 2,
    264 	CB_OK = 3,
    265 	CB_INPROG = 4,
    266 	CB_FAILED = 5,
    267 	CB_BAD = 6
    268 } rfs4_cbstate_t;
    269 
    270 #define	RFS4_CBCH_MAX	10	/* size callback client handle cache */
    271 /*
    272  * Callback info for a client.
    273  * Client only provides: cb_client4 and cb_ident
    274  * The rest of the information is used to track callback path status
    275  * and usage.
    276  *
    277  * cb_state - used as comments for the rfs4_cbstate_t enum indicate
    278  * cb_notified_of_cb_path_down - if the callback path was once CB_OK and
    279  *	has hence CB_FAILED, the client needs to be notified via RENEW.
    280  * cb_timefailed - current time when cb_state transitioned from
    281  *	CB_OK -> CB_FAILED.  Meant for observability.  When did that happen?
    282  * cb_chc_free/cb_chc - cache of client handles for the callback path
    283  * cb_ident - SETCLIENTID provided callback_ident value
    284  * callback - SETCLIENTID provided cb_client4 value
    285  * cb_refcnt - current number of users of this structure's content
    286  *	protected by cb_lock
    287  * cb_badbehavior - how many times did a client do something we didn't like?
    288  * cb_lock - lock for contents of cbinfo
    289  * cb_cv - used to allow threads to wait on CB_NULL completion
    290  * cb_nullcaller - is there a thread currently taking care of
    291  *	new callback information?
    292  * cb_cv_nullcaller - used by the thread doing CB_NULL to wait on
    293  *	threads that may be using client handles of the current
    294  *	client handle cache.
    295  * newer - new callback info provided by a client and awaiting
    296  *	CB_NULL testing and move to regular cbinfo.
    297  */
    298 typedef struct {
    299 	rfs4_cbstate_t	cb_state;
    300 	unsigned	cb_notified_of_cb_path_down:1;
    301 	time_t		cb_timefailed;
    302 	int		cb_chc_free;
    303 	CLIENT		*cb_chc[RFS4_CBCH_MAX];
    304 	uint32_t	cb_ident;
    305 	cb_client4	cb_callback;
    306 	uint32_t	cb_refcnt;
    307 	uint32_t	cb_badbehavior;
    308 	kmutex_t	cb_lock[1];
    309 	kcondvar_t	cb_cv[1];
    310 	bool_t		cb_nullcaller;
    311 	kcondvar_t	cb_cv_nullcaller[1];
    312 	struct {
    313 		bool_t		cb_new;
    314 		bool_t		cb_confirmed;
    315 		uint32_t	cb_ident;
    316 		cb_client4	cb_callback;
    317 	} cb_newer;
    318 } rfs4_cbinfo_t;
    319 
    320 /*
    321  * A server instance. We can associate sets of clients - via a pointer in
    322  * rfs4_client_t - with a given server instance, allowing us to treat clients
    323  * in the set differently to clients in other sets.
    324  *
    325  * Currently used only for Sun Cluster HA-NFS support, to group clients
    326  * on NFS resource failover so each set of clients gets its own dedicated
    327  * grace period and distributed stable storage data.
    328  */
    329 typedef struct rfs4_servinst {
    330 	int			dss_npaths;
    331 	krwlock_t		rwlock;
    332 	krwlock_t		oldstate_lock;
    333 	time_t			start_time;
    334 	time_t			grace_period;
    335 	rfs4_oldstate_t		*oldstate;
    336 	struct rfs4_dss_path	**dss_paths;
    337 	struct rfs4_servinst	*next;
    338 	struct rfs4_servinst	*prev;
    339 } rfs4_servinst_t;
    340 
    341 /*
    342  * DSS: distributed stable storage
    343  */
    344 
    345 typedef struct rfs4_dss_path {
    346 	struct rfs4_dss_path	*next; /* for insque/remque */
    347 	struct rfs4_dss_path	*prev; /* for insque/remque */
    348 	char			*path;
    349 	struct rfs4_servinst	*sip;
    350 	unsigned		index; /* offset in servinst's array */
    351 } rfs4_dss_path_t;
    352 
    353 /* array of paths passed-in from nfsd command-line; stored in nvlist */
    354 char		**rfs4_dss_newpaths;
    355 uint_t		rfs4_dss_numnewpaths;
    356 
    357 /*
    358  * Circular doubly-linked list of paths for currently-served RGs.
    359  * No locking required: only changed on warmstart. Managed with insque/remque.
    360  */
    361 rfs4_dss_path_t	*rfs4_dss_pathlist;
    362 
    363 /* nvlists of all DSS paths: current, and before last warmstart */
    364 nvlist_t *rfs4_dss_paths, *rfs4_dss_oldpaths;
    365 
    366 /*
    367  * The server maintains a set of state on a per client basis that
    368  * matches that of the protocol requirements.  A client's state is
    369  * rooted with the rfs4_client_t struct of which there is one per
    370  * client and is created when SETCLIENTID/SETCLIENTID_CONFIRM are
    371  * received.  From there, the server then creates rfs4_openowner_t
    372  * structs for each new open owner from that client and are initiated
    373  * at OPEN/OPEN_CONFIRM (when the open owner is new to the server).
    374  * At OPEN, at least two other structures are created, and potentially a
    375  * third.  rfs4_state_t is created to track the association between an
    376  * open owner and a particular file. An rfs4_file_t struct may be
    377  * created (if the file is not already open) at OPEN as well.  The
    378  * rfs4_file_t struct is the only one that is per server and not per
    379  * client.  The rfs4_deleg_state_t struct is created in the
    380  * instance that the server is going to provide a delegation for the
    381  * file being OPENed.  Finally, the rfs4_lockowner_t is created at the
    382  * first use of a lock owner at the server and is a result of the LOCK
    383  * operation.  The rfs4_lo_state_t struct is then created to represent
    384  * the relation between the lock owner and the file.
    385  *
    386  */
    387 /*
    388  * The following ascii art represents each of these data structs and
    389  * their references to each other.  Note: "<-(x)->" represents the
    390  * doubly link lists defined above.
    391  *
    392  *                          ____________________
    393  *                         |                    |
    394  *                         |    rfs4_client_t   |
    395  *                       ->|         (1),(2)    |<-
    396  *                      /  |____________________|  \
    397  *                     /              ^             \
    398  *                    /               |              \
    399  *  ____________________    ____________________    ____________________
    400  * |                    |  |                    |  |                    |
    401  * |  rfs4_lockowner_t  |  |  rfs4_openowner_t  |  | rfs4_deleg_state_t |
    402  * |                    |  |     (3)    <-(1)-> |  |            <-(2)-> |
    403  * |____________________|  |____________________|  |____________________|
    404  *           ^                        ^                       |
    405  *           |                        |                       V
    406  *  ____________________    ____________________    ____________________
    407  * |                    |  |                    |  |                    |
    408  * |  rfs4_lo_state_t   |->|    rfs4_state_t    |->|     rfs4_file_t    |
    409  * |            <-(4)-> |  |     (4)    <-(3)-> |  |                    |
    410  * |____________________|  |____________________|  |____________________|
    411  */
    412 /*
    413  * Each of these data types are kept in a separate rfs4_table_t and is
    414  * actually encapsulated within a rfs4_dbe_t struct.  The various
    415  * tables and their construction is done in nfs4_state.c but
    416  * documented here to completeness.
    417  *
    418  * Table		Data struct stored	Indexed by
    419  * -----		------------------	----------
    420  * rfs4_client_tab	rfs4_client_t		nfs_client_id4
    421  *						clientid4
    422  *
    423  * rfs4_openowner_tab	rfs4_openowner_t	open_owner4
    424  *
    425  * rfs4_state_tab	rfs4_state_t		open_owner4 | file
    426  *						stateid
    427  *
    428  * rfs4_lo_state_tab	rfs4_lo_state_t		lockowner | stateid
    429  *						lock_stateid
    430  *
    431  * rfs4_lockowner_tab	rfs4_lockowner_t	lockowner
    432  *						pid
    433  *
    434  * rfs4_file_tab	rfs4_file_t		filehandle
    435  *
    436  * rfs4_deleg_state_tab	rfs4_deleg_state_t	clientid4 | file
    437  *						deleg_stateid
    438  */
    439 
    440 /*
    441  * The client struct, it is the root of all state for a particular
    442  * client.  The client is identified by the nfs_client_id4 via
    443  * SETCLIENTID and the server returns the clientid4 as short hand reference
    444  */
    445 /*
    446  * Client struct - as mentioned above it is the root of all state for
    447  * a single client as identified by the client supplied nfs_client_id4
    448  *
    449  * dbe - encapsulation struct
    450  * clientid - server assigned short hand reference to client
    451  * nfs_client - client supplied identifier for itself
    452  * confirm_verf - the value provided to the client for SETCLIENTID_CONFIRM
    453  * need_confirm - does this client need to be SETCLIENTID_CONFIRMed?
    454  *
    455  * unlksys_completed - has an F_UNLKSYS been done for this client which
    456  *		says that the use of cleanlocks() on individual files
    457  *		is not required?
    458  * can_reclaim - indicates if client is allowed to reclaim after server
    459  * 		start-up (client had previous state at server)
    460  * ss_remove - indicates that the rfs4_client_destroy function should
    461  * 		clean up stable storage file.
    462  * forced_expire - set if the sysadmin has used clear_locks for this client.
    463  * deleg_revoked - how many delegations have been revoked for this client?
    464  *
    465  * cp_confirmed - this refers to a confirmed client struct that has
    466  * the same nfs_client_id4 as this client struct.  When/if this client
    467  * struct is confirmed via SETCLINETID_CONFIRM, the previously
    468  * confirmed client struct will be "closed" and hence this reference.
    469  *
    470  * last_access - used to determine if the client has let its lease expire
    471  * cbinfo - struct containing all callback related information
    472  * cr_set - credentials used for the SETCLIENTID/SETCLIENTID_CONFIRM pair
    473  * sysid - the lock manager sysid allocated for this client's file locks
    474  * openownerlist - root of openowners list associated with this client
    475  * ss_pn - Pathname to the stable storage file.
    476  * cl_addr - Clients network address.
    477  * server_instance - pointer to the currently associated server instance
    478  */
    479 typedef struct rfs4_client {
    480 	rfs4_dbe_t		*rc_dbe;
    481 	clientid4		rc_clientid;
    482 	nfs_client_id4		rc_nfs_client;
    483 	verifier4		rc_confirm_verf;
    484 	unsigned		rc_need_confirm:1;
    485 	unsigned		rc_unlksys_completed:1;
    486 	unsigned		rc_can_reclaim:1;
    487 	unsigned 		rc_ss_remove:1;
    488 	unsigned		rc_forced_expire:1;
    489 	uint_t			rc_deleg_revoked;
    490 	struct rfs4_client	*rc_cp_confirmed;
    491 	time_t			rc_last_access;
    492 	rfs4_cbinfo_t		rc_cbinfo;
    493 	cred_set_t		rc_cr_set;
    494 	sysid_t			rc_sysidt;
    495 	list_t			rc_openownerlist;
    496 	rfs4_ss_pn_t		*rc_ss_pn;
    497 	struct sockaddr_storage rc_addr;
    498 	rfs4_servinst_t		*rc_server_instance;
    499 } rfs4_client_t;
    500 
    501 /*
    502  * The openowner contains the client supplied open_owner4 as well as
    503  * the matching sequence id and is used to track the client's usage of
    504  * the open_owner4.  Note that a reply is saved here as well for
    505  * processing of retransmissions.
    506  *
    507  * dbe - encapsulation struct
    508  * client - reference to rfs4_client_t for this openowner
    509  * owner - actual client supplied open_owner4
    510  * need_confirm - does this openowner need to be OPEN_CONFIRMed
    511  * postpone_confirm - set if error received on first use of open_owner
    512  * state2confirm - what stateid4 should be used on the OPEN_CONFIRM
    513  * open_seqid - what is the next open_seqid expected for this openowner
    514  * oo_sw - used to serialize access to the open seqid/reply handling
    515  * cr_set - credential used for the OPEN
    516  * statelist - root of state struct list associated with this openowner
    517  * node - node for client struct list of openowners
    518  * reply_fh - open replay processing needs the filehandle so that it is
    519  *	able to reset the current filehandle for appropriate compound
    520  *	processing and reply.
    521  * reply - last reply sent in relation to this openowner
    522  */
    523 typedef struct rfs4_openowner {
    524 	rfs4_dbe_t		*ro_dbe;
    525 	rfs4_client_t		*ro_client;
    526 	open_owner4		ro_owner;
    527 	unsigned		ro_need_confirm:1;
    528 	unsigned		ro_postpone_confirm:1;
    529 	seqid4			ro_open_seqid;
    530 	rfs4_state_wait_t	ro_sw;
    531 	cred_set_t		ro_cr_set;
    532 	list_t			ro_statelist;
    533 	list_node_t		ro_node;
    534 	nfs_fh4			ro_reply_fh;
    535 	nfs_resop4		ro_reply;
    536 } rfs4_openowner_t;
    537 
    538 /*
    539  * This state struct represents the association between an openowner
    540  * and a file that has been OPENed by that openowner.
    541  *
    542  * dbe - encapsulation struct
    543  * stateid - server provided stateid
    544  * owner - reference back to the openowner for this state
    545  * finfo - reference to the open file for this state
    546  * open_access - how did the openowner OPEN the file (access)
    547  * open_deny - how did the openowner OPEN the file (deny)
    548  * share_access - what share reservation is on the file (access)
    549  * share_deny - what share reservation is on the file (deny)
    550  * closed - has this file been closed?
    551  * lostatelist - root of list of lo_state associated with this state/file
    552  * node - node for state struct list of states
    553  */
    554 typedef struct rfs4_state {
    555 	rfs4_dbe_t		*rs_dbe;
    556 	stateid_t		rs_stateid;
    557 	rfs4_openowner_t	*rs_owner;
    558 	struct rfs4_file	*rs_finfo;
    559 	uint32_t		rs_open_access;
    560 	uint32_t		rs_open_deny;
    561 	uint32_t		rs_share_access;
    562 	uint32_t		rs_share_deny;
    563 	unsigned		rs_closed:1;
    564 	list_t			rs_lostatelist;
    565 	list_node_t		rs_node;
    566 } rfs4_state_t;
    567 
    568 /*
    569  * Lockowner - track the lockowner and its related info
    570  *
    571  * dbe - encapsulation struct
    572  * client - reference to the client
    573  * owner - lockowner supplied by the client
    574  * pid - local identifier used for file locking
    575  */
    576 typedef struct rfs4_lockowner {
    577 	rfs4_dbe_t		*rl_dbe;
    578 	rfs4_client_t		*rl_client;
    579 	lock_owner4		rl_owner;
    580 	pid_t			rl_pid;
    581 } rfs4_lockowner_t;
    582 
    583 /*
    584  * Lockowner_state associated with a state struct and lockowner
    585  *
    586  * dbe - encapsulation struct
    587  * state - reference back to state struct for open file
    588  * lockid - stateid for this lockowner/state
    589  * locker - reference to lockowner
    590  * seqid - sequence id for this lockowner/state
    591  * skip_seqid_check - used on initialization of struct
    592  * locks_cleaned - have all locks been released for this lockowner/file?
    593  * lock_completed - successful LOCK with lockowner/file?
    594  * ls_sw - used to serialize update seqid/reply/stateid handling
    595  * node - node for state struct list of lo_states
    596  * reply - last reply sent in relation to this lockowner/state
    597  */
    598 typedef struct rfs4_lo_state {
    599 	rfs4_dbe_t		*rls_dbe;
    600 	rfs4_state_t		*rls_state;
    601 	stateid_t		rls_lockid;
    602 	rfs4_lockowner_t	*rls_locker;
    603 	seqid4			rls_seqid;
    604 	unsigned		rls_skip_seqid_check:1;
    605 	unsigned		rls_locks_cleaned:1;
    606 	unsigned		rls_lock_completed:1;
    607 	rfs4_state_wait_t	rls_sw;
    608 	list_node_t		rls_node;
    609 	nfs_resop4		rls_reply;
    610 } rfs4_lo_state_t;
    611 
    612 /*
    613  * Delegation state - per client
    614  *
    615  * dbe - encapsulation struct
    616  * dtype - type of delegation (NONE, READ, WRITE)
    617  * delegid - stateid for this delegation
    618  * time_granted - time this delegation was assigned to client
    619  * time_recalled - time when the server started recall process
    620  * time_revoked - if revoked, time that the revoke occurred
    621  * finfo - reference to the file associated with this delegation
    622  * client - reference to client for which this delegation is associated
    623  * node - list of delegations for the file (WRITE == 1, READ == )
    624  */
    625 typedef struct rfs4_deleg_state {
    626 	rfs4_dbe_t		*rds_dbe;
    627 	open_delegation_type4	rds_dtype;
    628 	stateid_t		rds_delegid;
    629 	time_t			rds_time_granted;
    630 	time_t			rds_time_recalled;
    631 	time_t			rds_time_revoked;
    632 	struct rfs4_file	*rds_finfo;
    633 	rfs4_client_t		*rds_client;
    634 	list_node_t		rds_node;
    635 } rfs4_deleg_state_t;
    636 
    637 /*
    638  * Delegation info associated with the file
    639  *
    640  * dtype - type of delegation for file (NONE, READ, WRITE)
    641  * time_returned - time that last delegation was returned for file
    642  * time_recalled - time that recall sequence started
    643  * time_lastgrant - time that last delegation was provided to a client
    644  * time_lastwrite - time of last write to use the delegation stateid
    645  * time_rm_delayed - time of last remove/rename which was DELAYed
    646  * rdgrants - how many read delegations have been provided for this file
    647  * wrgrants - how many write delegations provided (can only be one)
    648  * recall_count - how many recall threads are outstanding
    649  * recall_lock - lock to protect contents of this struct
    650  * recall_cv - condition var for the "parent" thread to wait upon
    651  * deleg_change_grant - value for change attribute at time of write grant
    652  * deleg_change - most recent value of change obtained from client
    653  * deleg_change_ts - time of last deleg_change update
    654  * ever_recalled - has this particular delegation ever been recalled?
    655  * dont_grant - file deletion is impending, don't grant a delegation
    656  * conflicted_client - clientid of the client that caused a CB_RECALL
    657  *	to occur. This is used for delegation policy (should a delegation
    658  *	be granted shortly after it has been returned?)
    659  */
    660 typedef struct rfs4_dinfo {
    661 	open_delegation_type4 rd_dtype;
    662 	time_t		rd_time_returned;
    663 	time_t		rd_time_recalled;
    664 	time_t		rd_time_lastgrant;
    665 	time_t		rd_time_lastwrite;
    666 	time_t		rd_time_rm_delayed;
    667 	uint32_t	rd_rdgrants;
    668 	uint32_t	rd_wrgrants;
    669 	int32_t		rd_recall_count;
    670 	kmutex_t	rd_recall_lock[1];
    671 	kcondvar_t	rd_recall_cv[1];
    672 	bool_t		rd_ever_recalled;
    673 	uint32_t	rd_hold_grant;
    674 	clientid4	rd_conflicted_client;
    675 } rfs4_dinfo_t;
    676 
    677 /*
    678  * File
    679  *
    680  * dbe - encapsulation struct
    681  * vp - vnode for the file that is open or has a delegation
    682  * filehandle - the filehandle generated by the server for this file
    683  * delegstatelist - root of delegation list for this file
    684  * dinfo - see struct definition above
    685  * share_deny - union of all deny modes on file
    686  * share_access - union of all access modes on file
    687  * access_read - count of read access
    688  * access_write - count of write access
    689  * deny_read - count of deny reads
    690  * deny_write - count of deny writes
    691  * file_rwlock - lock for serializing the removal of a file while
    692  *	the state structures are active within the server
    693  *
    694  * 	The only requirement for locking file_rwlock is that the
    695  * 	caller have a reference to the containing rfs4_file.  The dbe
    696  * 	lock may or may not be held for lock/unlock of file_rwlock.
    697  * 	As mentioned above, the file_rwlock is used for serialization
    698  * 	of file removal and more specifically reference to the held
    699  * 	vnode (e.g. vp).
    700  */
    701 typedef struct rfs4_file {
    702 	rfs4_dbe_t	*rf_dbe;
    703 	vnode_t		*rf_vp;
    704 	nfs_fh4		rf_filehandle;
    705 	list_t		rf_delegstatelist;
    706 	rfs4_dinfo_t	rf_dinfo;
    707 	uint32_t	rf_share_deny;
    708 	uint32_t	rf_share_access;
    709 	uint32_t	rf_access_read;
    710 	uint32_t	rf_access_write;
    711 	uint32_t	rf_deny_read;
    712 	uint32_t	rf_deny_write;
    713 	krwlock_t	rf_file_rwlock;
    714 } rfs4_file_t;
    715 
    716 extern int	rfs4_seen_first_compound;	/* set first time we see one */
    717 
    718 extern rfs4_servinst_t	*rfs4_cur_servinst;	/* current server instance */
    719 extern kmutex_t		rfs4_servinst_lock;	/* protects linked list */
    720 extern void		rfs4_servinst_create(int, int, char **);
    721 extern void		rfs4_servinst_destroy_all(void);
    722 extern void		rfs4_servinst_assign(rfs4_client_t *,
    723 			    rfs4_servinst_t *);
    724 extern rfs4_servinst_t	*rfs4_servinst(rfs4_client_t *);
    725 extern int		rfs4_clnt_in_grace(rfs4_client_t *);
    726 extern int		rfs4_servinst_in_grace(rfs4_servinst_t *);
    727 extern int		rfs4_servinst_grace_new(rfs4_servinst_t *);
    728 extern void		rfs4_grace_start(rfs4_servinst_t *);
    729 extern void		rfs4_grace_start_new(void);
    730 extern void		rfs4_grace_reset_all(void);
    731 extern void		rfs4_ss_oldstate(rfs4_oldstate_t *, char *, char *);
    732 extern void		rfs4_dss_readstate(int, char **);
    733 
    734 /*
    735  * rfs4_deleg_policy is used to signify the server's global delegation
    736  * policy.  The default is to NEVER delegate files and the
    737  * administrator must configure the server to enable delegations.
    738  *
    739  * The disable/enable delegation functions are used to eliminate a
    740  * race with exclusive creates.
    741  */
    742 typedef enum {
    743 	SRV_NEVER_DELEGATE = 0,
    744 	SRV_NORMAL_DELEGATE = 1
    745 } srv_deleg_policy_t;
    746 
    747 extern srv_deleg_policy_t rfs4_deleg_policy;
    748 extern kmutex_t rfs4_deleg_lock;
    749 extern void rfs4_disable_delegation(void), rfs4_enable_delegation(void);
    750 
    751 /*
    752  * Request types for delegation. These correspond with
    753  * open_delegation_type4 with the addition of a new value, DELEG_ANY,
    754  * to reqequest any delegation.
    755  */
    756 typedef enum {
    757 	DELEG_NONE = 0,		/* Corresponds to OPEN_DELEG_NONE */
    758 	DELEG_READ = 1,		/* Corresponds to OPEN_DELEG_READ */
    759 	DELEG_WRITE = 2,	/* Corresponds to OPEN_DELEG_WRITE */
    760 	DELEG_ANY = -1		/* New value to request any delegation type */
    761 } delegreq_t;
    762 
    763 #define	NFS4_DELEG4TYPE2REQTYPE(x) (delegreq_t)(x)
    764 
    765 /*
    766  * Various interfaces to manipulate the state structures introduced
    767  * above
    768  */
    769 extern	kmutex_t	rfs4_state_lock;
    770 extern	void		rfs4_clean_state_exi(struct exportinfo *exi);
    771 extern	void		rfs4_free_reply(nfs_resop4 *);
    772 extern	void		rfs4_copy_reply(nfs_resop4 *, nfs_resop4 *);
    773 
    774 /* rfs4_client_t handling */
    775 extern	rfs4_client_t	*rfs4_findclient(nfs_client_id4 *,
    776 					bool_t *, rfs4_client_t *);
    777 extern	rfs4_client_t	*rfs4_findclient_by_id(clientid4, bool_t);
    778 extern	void		rfs4_client_rele(rfs4_client_t *);
    779 extern	void		rfs4_client_close(rfs4_client_t *);
    780 extern	void		rfs4_client_state_remove(rfs4_client_t *);
    781 extern	void		rfs4_client_scv_next(rfs4_client_t *);
    782 extern	void		rfs4_update_lease(rfs4_client_t *);
    783 extern	bool_t		rfs4_lease_expired(rfs4_client_t *);
    784 extern	nfsstat4	rfs4_check_clientid(clientid4 *, int);
    785 
    786 /* rfs4_openowner_t handling */
    787 extern	rfs4_openowner_t *rfs4_findopenowner(open_owner4 *, bool_t *, seqid4);
    788 extern	void		rfs4_update_open_sequence(rfs4_openowner_t *);
    789 extern	void		rfs4_update_open_resp(rfs4_openowner_t *,
    790 					nfs_resop4 *, nfs_fh4 *);
    791 extern	void		rfs4_openowner_rele(rfs4_openowner_t *);
    792 extern	void		rfs4_free_opens(rfs4_openowner_t *, bool_t, bool_t);
    793 
    794 /* rfs4_lockowner_t handling */
    795 extern	rfs4_lockowner_t *rfs4_findlockowner(lock_owner4 *, bool_t *);
    796 extern	rfs4_lockowner_t *rfs4_findlockowner_by_pid(pid_t);
    797 extern	void		rfs4_lockowner_rele(rfs4_lockowner_t *);
    798 
    799 /* rfs4_state_t handling */
    800 extern	rfs4_state_t	*rfs4_findstate_by_owner_file(rfs4_openowner_t *,
    801 					rfs4_file_t *, bool_t *);
    802 extern	void		rfs4_state_rele(rfs4_state_t *);
    803 extern	void		rfs4_state_close(rfs4_state_t *, bool_t,
    804 					bool_t, cred_t *);
    805 extern	void		rfs4_release_share_lock_state(rfs4_state_t *,
    806 					cred_t *, bool_t);
    807 extern	void		rfs4_close_all_state(rfs4_file_t *);
    808 
    809 /* rfs4_lo_state_t handling */
    810 extern	rfs4_lo_state_t *rfs4_findlo_state_by_owner(rfs4_lockowner_t *,
    811 						rfs4_state_t *, bool_t *);
    812 extern	void		rfs4_lo_state_rele(rfs4_lo_state_t *, bool_t);
    813 extern	void		rfs4_update_lock_sequence(rfs4_lo_state_t *);
    814 extern	void		rfs4_update_lock_resp(rfs4_lo_state_t *,
    815 					nfs_resop4 *);
    816 
    817 /* rfs4_file_t handling */
    818 extern	rfs4_file_t	*rfs4_findfile(vnode_t *, nfs_fh4 *, bool_t *);
    819 extern	rfs4_file_t	*rfs4_findfile_withlock(vnode_t *, nfs_fh4 *,
    820 						bool_t *);
    821 extern	void		rfs4_file_rele(rfs4_file_t *);
    822 
    823 /* General collection of "get state" functions */
    824 extern	nfsstat4	rfs4_get_state(stateid4 *, rfs4_state_t **,
    825 					rfs4_dbsearch_type_t);
    826 extern	nfsstat4	rfs4_get_deleg_state(stateid4 *,
    827 					rfs4_deleg_state_t **);
    828 extern	nfsstat4	rfs4_get_lo_state(stateid4 *, rfs4_lo_state_t **,
    829 					bool_t);
    830 extern	nfsstat4	rfs4_check_stateid(int, vnode_t *, stateid4 *,
    831 					bool_t, bool_t *, bool_t,
    832 					caller_context_t *);
    833 extern	int		rfs4_check_stateid_seqid(rfs4_state_t *, stateid4 *);
    834 extern	int		rfs4_check_lo_stateid_seqid(rfs4_lo_state_t *,
    835 					stateid4 *);
    836 
    837 /* return values for rfs4_check_stateid_seqid() */
    838 #define	NFS4_CHECK_STATEID_OKAY	1
    839 #define	NFS4_CHECK_STATEID_OLD	2
    840 #define	NFS4_CHECK_STATEID_BAD	3
    841 #define	NFS4_CHECK_STATEID_EXPIRED	4
    842 #define	NFS4_CHECK_STATEID_REPLAY	5
    843 #define	NFS4_CHECK_STATEID_CLOSED	6
    844 #define	NFS4_CHECK_STATEID_UNCONFIRMED	7
    845 
    846 /* delay() time that server is willing to briefly wait for a delegreturn */
    847 #define	NFS4_DELEGATION_CONFLICT_DELAY	(hz/10)
    848 
    849 /*
    850  * Interfaces for handling of callback's client handle cache and
    851  * callback interfaces themselves.
    852  */
    853 extern	void		rfs4_cbinfo_free(rfs4_cbinfo_t *);
    854 extern	void		rfs4_client_setcb(rfs4_client_t *, cb_client4 *,
    855 					uint32_t);
    856 extern	void		rfs4_deleg_cb_check(rfs4_client_t *);
    857 extern	nfsstat4	rfs4_vop_getattr(vnode_t *, vattr_t *, int, cred_t *);
    858 
    859 /* rfs4_deleg_state_t handling and other delegation interfaces */
    860 extern	rfs4_deleg_state_t *rfs4_finddeleg(rfs4_state_t *, bool_t *);
    861 extern	rfs4_deleg_state_t *rfs4_finddelegstate(stateid_t *);
    862 extern	bool_t		rfs4_check_recall(rfs4_state_t *, uint32_t);
    863 extern	void		rfs4_recall_deleg(rfs4_file_t *,
    864 				bool_t, rfs4_client_t *);
    865 extern	int		rfs4_get_deleg(rfs4_state_t *,  open_delegation_type4,
    866 			open_delegation_type4 (*policy)(rfs4_state_t *,
    867 				open_delegation_type4 dtype));
    868 extern	rfs4_deleg_state_t *rfs4_grant_delegation(delegreq_t, rfs4_state_t *,
    869 				int *);
    870 extern	void		rfs4_set_deleg_response(rfs4_deleg_state_t *,
    871 				open_delegation4 *, nfsace4 *, int);
    872 extern	void		rfs4_return_deleg(rfs4_deleg_state_t *, bool_t);
    873 extern	bool_t		rfs4_is_deleg(rfs4_state_t *);
    874 extern	void		rfs4_deleg_state_rele(rfs4_deleg_state_t *);
    875 extern	bool_t		rfs4_check_delegated_byfp(int, rfs4_file_t *,
    876 					bool_t, bool_t, bool_t, clientid4 *);
    877 extern	void		rfs4_clear_dont_grant(rfs4_file_t *);
    878 
    879 /*
    880  * nfs4 monitored operations.
    881  */
    882 extern int deleg_rd_open(femarg_t *, int, cred_t *, caller_context_t *);
    883 extern int deleg_wr_open(femarg_t *, int, cred_t *, caller_context_t *);
    884 extern int deleg_wr_read(femarg_t *, uio_t *, int, cred_t *,
    885 	    caller_context_t *);
    886 extern int deleg_rd_write(femarg_t *, uio_t *, int, cred_t *,
    887 	    caller_context_t *);
    888 extern int deleg_wr_write(femarg_t *, uio_t *, int, cred_t *,
    889 	    caller_context_t *);
    890 extern int deleg_rd_setattr(femarg_t *, vattr_t *, int, cred_t *,
    891 		caller_context_t *);
    892 extern int deleg_wr_setattr(femarg_t *, vattr_t *, int, cred_t *,
    893 		caller_context_t *);
    894 extern int deleg_rd_rwlock(femarg_t *, int, caller_context_t *);
    895 extern int deleg_wr_rwlock(femarg_t *, int, caller_context_t *);
    896 extern int deleg_rd_space(femarg_t *, int, flock64_t *, int, offset_t, cred_t *,
    897 		caller_context_t *);
    898 extern int deleg_wr_space(femarg_t *, int, flock64_t *, int, offset_t, cred_t *,
    899 		caller_context_t *);
    900 extern int deleg_rd_setsecattr(femarg_t *, vsecattr_t *, int, cred_t *,
    901 		caller_context_t *);
    902 extern int deleg_wr_setsecattr(femarg_t *, vsecattr_t *, int, cred_t *,
    903 		caller_context_t *);
    904 extern int deleg_rd_vnevent(femarg_t *, vnevent_t, vnode_t *, char *,
    905 		caller_context_t *);
    906 extern int deleg_wr_vnevent(femarg_t *, vnevent_t, vnode_t *, char *,
    907 		caller_context_t *);
    908 
    909 extern void rfs4_mon_hold(void *);
    910 extern void rfs4_mon_rele(void *);
    911 
    912 extern fem_t	*deleg_rdops;
    913 extern fem_t	*deleg_wrops;
    914 
    915 extern int rfs4_share(rfs4_state_t *, uint32_t, uint32_t);
    916 extern int rfs4_unshare(rfs4_state_t *);
    917 extern	void		rfs4_set_deleg_policy(srv_deleg_policy_t);
    918 #ifdef DEBUG
    919 #define	NFS4_DEBUG(var, args) if (var) cmn_err args
    920 
    921 extern int rfs4_debug;
    922 extern int nfs4_client_attr_debug;
    923 extern int nfs4_client_state_debug;
    924 extern int nfs4_client_shadow_debug;
    925 extern int nfs4_client_lock_debug;
    926 extern int nfs4_client_lease_debug;
    927 extern int nfs4_seqid_sync;
    928 extern int nfs4_client_map_debug;
    929 extern int nfs4_client_inactive_debug;
    930 extern int nfs4_client_recov_debug;
    931 extern int nfs4_client_failover_debug;
    932 extern int nfs4_client_call_debug;
    933 extern int nfs4_client_foo_debug;
    934 extern int nfs4_client_zone_debug;
    935 extern int nfs4_lost_rqst_debug;
    936 extern int nfs4_open_stream_debug;
    937 extern int nfs4_client_open_dg;
    938 extern int nfs4_srvmnt_debug;
    939 extern int nfs4_utf8_debug;
    940 
    941 void rfs4_dbe_debug(rfs4_dbe_t *e);
    942 
    943 #ifdef NFS4_DEBUG_MUTEX
    944 void nfs4_debug_mutex_enter(kmutex_t *, char *, int);
    945 void nfs4_debug_mutex_exit(kmutex_t *, char *, int);
    946 
    947 #define	mutex_enter(m) nfs4_debug_mutex_enter((m), __FILE__, __LINE__)
    948 #define	mutex_exit(m) nfs4_debug_mutex_exit((m), __FILE__, __LINE__)
    949 #endif /* NFS4_DEBUG_MUTEX */
    950 
    951 #else  /* ! DEBUG */
    952 #define	NFS4_DEBUG(var, args)
    953 #endif /* DEBUG */
    954 
    955 /*
    956  * XXX - temporary for testing of volatile fh
    957  */
    958 
    959 #ifdef VOLATILE_FH_TEST
    960 
    961 struct nfs_fh4_fmt {
    962 	fhandle4_t	fh4_i;
    963 	uint32_t	fh4_flag;
    964 	uint32_t	fh4_volatile_id;
    965 };
    966 
    967 #else /* VOLATILE_FH_TEST */
    968 
    969 struct nfs_fh4_fmt {
    970 	fhandle4_t	fh4_i;
    971 	uint32_t	fh4_flag;
    972 };
    973 
    974 #endif /* VOLATILE_FH_TEST */
    975 
    976 #define	FH4_NAMEDATTR	1
    977 #define	FH4_ATTRDIR	2
    978 
    979 #define	fh4_fsid	fh4_i.fhx_fsid
    980 #define	fh4_len		fh4_i.fhx_len 	/* fid length */
    981 #define	fh4_data	fh4_i.fhx_data 	/* fid bytes */
    982 #define	fh4_xlen	fh4_i.fhx_xlen
    983 #define	fh4_xdata	fh4_i.fhx_xdata
    984 typedef struct nfs_fh4_fmt nfs_fh4_fmt_t;
    985 
    986 #define	fh4_to_fmt4(fh4p) ((nfs_fh4_fmt_t *)(fh4p)->nfs_fh4_val)
    987 #define	get_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) & (flag))
    988 #define	set_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) |= (flag))
    989 #define	clr_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) &= ~(flag))
    990 
    991 #define	NFS_FH4_LEN	sizeof (nfs_fh4_fmt_t)
    992 
    993 /*
    994  * Copy fields from external (fhandle_t) to in-memory (nfs_fh4_fmt_t)
    995  * format to support export info checking.  It does not copy over
    996  * the complete filehandle, just the fsid, xlen and xdata.  It may
    997  * need to be changed to be used in other places.
    998  *
    999  * NOTE: The macro expects the space to be  pre-allocated for
   1000  * the contents of nfs_fh4_fmt_t.
   1001  */
   1002 #define	FH_TO_FMT4(exifh, nfs_fmt) {				\
   1003 	bzero((nfs_fmt), NFS_FH4_LEN);				\
   1004 	(nfs_fmt)->fh4_fsid = (exifh)->fh_fsid;			\
   1005 	(nfs_fmt)->fh4_xlen = (exifh)->fh_xlen;			\
   1006 	bcopy((exifh)->fh_xdata, (nfs_fmt)->fh4_xdata,		\
   1007 	    (exifh)->fh_xlen);					\
   1008 }
   1009 
   1010 /*
   1011  * A few definitions of repeatedly used constructs for nfsv4
   1012  */
   1013 #define	UTF8STRING_FREE(str)					\
   1014 	kmem_free((str).utf8string_val,	(str).utf8string_len);	\
   1015 	(str).utf8string_val = NULL;				\
   1016 	(str).utf8string_len = 0;
   1017 
   1018 /*
   1019  * NFS4_VOLATILE_FH yields non-zero if the filesystem uses non-persistent
   1020  * filehandles.
   1021  */
   1022 #define	NFS4_VOLATILE_FH(mi)					\
   1023 	((mi)->mi_fh_expire_type &				\
   1024 	(FH4_VOLATILE_ANY | FH4_VOL_MIGRATION | FH4_VOL_RENAME))
   1025 
   1026 /*
   1027  * NFS_IS_DOTNAME checks if the name given represents a dot or dotdot entry
   1028  */
   1029 #define	NFS_IS_DOTNAME(name)					\
   1030 	(((name)[0] == '.') &&					\
   1031 	(((name)[1] == '\0') || (((name)[1] == '.') && ((name)[2] == '\0'))))
   1032 
   1033 /*
   1034  * Define the number of bits in a bitmap word (uint32)
   1035  */
   1036 #define	NFS4_BITMAP4_BITSPERWORD	(sizeof (uint32_t) * 8)
   1037 
   1038 /*
   1039  * Define the value for the access field of the compound_state structure
   1040  * based on the result of nfsauth access checking.
   1041  */
   1042 #define	CS_ACCESS_OK		0x1
   1043 #define	CS_ACCESS_DENIED	0x2
   1044 #define	CS_ACCESS_LIMITED	0x4
   1045 
   1046 /*
   1047  * compound state in nfsv4 server
   1048  */
   1049 struct compound_state {
   1050 	struct exportinfo *exi;
   1051 	struct exportinfo *saved_exi;	/* export struct for saved_vp */
   1052 	cred_t 		*basecr;	/* UNIX cred:  only RPC request */
   1053 	caddr_t 	principal;
   1054 	int 		nfsflavor;
   1055 	cred_t 		*cr;		/* UNIX cred: RPC request and */
   1056 					/* target export */
   1057 	bool_t  	cont;
   1058 	uint_t 		access;		/* access perm on vp per request */
   1059 	bool_t 		deleg;		/* TRUE if current fh has */
   1060 					/* write delegated */
   1061 	vnode_t 	*vp;		/* modified by PUTFH, and by ops that */
   1062 					/* input to GETFH */
   1063 	bool_t 		mandlock;	/* Is mandatory locking in effect */
   1064 					/* for vp */
   1065 	vnode_t 	*saved_vp;	/* modified by SAVEFH, copied to */
   1066 					/* vp by RESTOREFH */
   1067 	nfsstat4 	*statusp;
   1068 	nfs_fh4 	fh;		/* ditto. valid only if vp != NULL */
   1069 	nfs_fh4 	saved_fh;	/* ditto. valid only if */
   1070 					/* 	saved_vp != NULL */
   1071 	struct svc_req	*req;
   1072 	char 		fhbuf[NFS4_FHSIZE];
   1073 };
   1074 
   1075 /*
   1076  * Conversion commands for nfsv4 server attr checking
   1077  */
   1078 enum nfs4_attr_cmd {
   1079 	NFS4ATTR_SUPPORTED = 0,		/* check which attrs supported */
   1080 	NFS4ATTR_GETIT = 1,		/* getattr - sys to fattr4 (r) */
   1081 	NFS4ATTR_SETIT = 2,		/* setattr - fattr4 to sys (w) */
   1082 	NFS4ATTR_VERIT = 3,		/* verify - fattr4 to sys (r) */
   1083 	NFS4ATTR_FREEIT = 4		/* free any alloc'd space for attr */
   1084 };
   1085 
   1086 typedef enum nfs4_attr_cmd nfs4_attr_cmd_t;
   1087 
   1088 struct nfs4_svgetit_arg {
   1089 	nfs4_attr_cmd_t op;		/* getit or setit */
   1090 	struct compound_state *cs;
   1091 	struct statvfs64 *sbp;
   1092 	uint_t 		flag;		/* VOP_GETATTR/VOP_SETATTR flag */
   1093 	uint_t 		xattr;		/* object is xattr */
   1094 	bool_t 		rdattr_error_req; /* if readdir & client wants */
   1095 						/* rdattr_error */
   1096 	nfsstat4	rdattr_error;	/* used for per-entry status */
   1097 					/* (if rdattr_err) */
   1098 	bool_t		mntdfid_set;
   1099 	fattr4_mounted_on_fileid
   1100 			mounted_on_fileid;
   1101 					/* readdir op can always return	*/
   1102 					/* d_ino from server fs dirent  */
   1103 					/* for mounted_on_fileid attr.	*/
   1104 					/* This field holds d_ino so	*/
   1105 					/* srv attr conv code can avoid */
   1106 					/* doing an untraverse.		*/
   1107 	vattr_t		vap[1];
   1108 };
   1109 
   1110 struct nfs4_ntov_map {
   1111 	bitmap4		fbit; 		/* FATTR4_XXX_MASKY */
   1112 	uint_t 		vbit; 		/* AT_XXX */
   1113 	bool_t 		vfsstat;
   1114 	bool_t 		mandatory; 	/* attribute mandatory to implement? */
   1115 	uint_t 		nval;
   1116 	int		xdr_size;	/* Size of XDR'd attr */
   1117 	xdrproc_t 	xfunc;
   1118 	int (*sv_getit)(nfs4_attr_cmd_t, struct nfs4_svgetit_arg *,
   1119 		union nfs4_attr_u *);	/* subroutine for getting attr. */
   1120 	char 		*prtstr;	/* string attr for printing */
   1121 };
   1122 
   1123 struct nfs4attr_to_vattr {
   1124 	vnode_t 	*vp;
   1125 	vattr_t 	*vap;
   1126 	nfs_fh4   	*fhp;
   1127 	nfsstat4	rdattr_error;
   1128 	uint32_t	flag;
   1129 	fattr4_change	change;
   1130 	fattr4_fsid	srv_fsid;
   1131 	fattr4_mounted_on_fileid	mntd_fid;
   1132 };
   1133 
   1134 typedef struct nfs4attr_to_vattr ntov4_t;
   1135 
   1136 /*
   1137  * nfs4attr_to_vattr flags
   1138  */
   1139 #define	NTOV_FHP_VALID			0x01
   1140 #define	NTOV_RDATTR_ERROR_VALID		0x02
   1141 #define	NTOV_CHANGE_VALID		0x04
   1142 #define	NTOV_SUPP_VALID			0x08
   1143 #define	NTOV_SRV_FSID_VALID		0x10
   1144 #define	NTOV_MOUNTED_ON_FILEID_VALID	0x20
   1145 
   1146 
   1147 #define	FATTR4_MANDATTR_MASK (		\
   1148 	FATTR4_SUPPORTED_ATTRS_MASK |	\
   1149 	FATTR4_TYPE_MASK |		\
   1150 	FATTR4_FH_EXPIRE_TYPE_MASK |	\
   1151 	FATTR4_CHANGE_MASK |		\
   1152 	FATTR4_SIZE_MASK |		\
   1153 	FATTR4_LINK_SUPPORT_MASK |	\
   1154 	FATTR4_SYMLINK_SUPPORT_MASK |	\
   1155 	FATTR4_NAMED_ATTR_MASK |	\
   1156 	FATTR4_FSID_MASK |		\
   1157 	FATTR4_UNIQUE_HANDLES_MASK |	\
   1158 	FATTR4_LEASE_TIME_MASK |	\
   1159 	FATTR4_RDATTR_ERROR_MASK |	\
   1160 	FATTR4_FILEHANDLE_MASK)
   1161 
   1162 
   1163 struct nfs4attr_to_osattr {
   1164 	void *attrconv_arg;
   1165 	uint_t mask;
   1166 };
   1167 
   1168 struct mntinfo4;
   1169 
   1170 /*
   1171  * lkp4_attr_setup lists the different options for attributes when calling
   1172  * nfs4lookup_setup - either no attributes (just lookups - e.g., secinfo),
   1173  * one component only (normal component lookup), get attributes for the
   1174  * last component (e.g., mount), attributes for each component (e.g.,
   1175  * failovers later), just the filehandle for the last component (e.g.,
   1176  * volatile filehandle recovery), or stuff that needs OPENATTR (e.g.
   1177  * looking up a named attribute or it's hidden directory).
   1178  */
   1179 enum lkp4_attr_setup {
   1180 	LKP4_NO_ATTRIBUTES = 0,		/* no attrs or filehandles */
   1181 	LKP4_ALL_ATTRIBUTES = 3,	/* multi-comp: attrs for all comps */
   1182 	LKP4_LAST_NAMED_ATTR = 5,	/* multi-comp: named attr & attrdir */
   1183 	LKP4_LAST_ATTRDIR = 6,		/* multi-comp: just attrdir */
   1184 	LKP4_ALL_ATTR_SECINFO = 7	/* multi-comp: attrs for all comp and */
   1185 					/*	secinfo for last comp */
   1186 };
   1187 
   1188 /*
   1189  * lookup4_param a set of parameters to nfs4lookup_setup -
   1190  * used to setup a path lookup compound request.
   1191  */
   1192 typedef struct lookup4_param {
   1193 	enum lkp4_attr_setup l4_getattrs; /* (in) get attrs in the lookup? */
   1194 	int 		header_len;	/* (in) num ops before first lookup  */
   1195 	int 		trailer_len;	/* (in) num ops after last	*/
   1196 					/*	Lookup/Getattr		*/
   1197 	bitmap4 	ga_bits;	/* (in) Which attributes for Getattr */
   1198 	COMPOUND4args_clnt *argsp;	/* (in/out) args for compound struct */
   1199 	COMPOUND4res_clnt  *resp;	/* (in/out) res for compound  struct */
   1200 	int 		arglen;		/* (out) argop buffer alloc'd length */
   1201 	struct mntinfo4 *mi;
   1202 } lookup4_param_t;
   1203 
   1204 
   1205 #define	NFS4_FATTR4_FINISH	-1	/* fattr4 index indicating finish */
   1206 
   1207 typedef int (*nfs4attr_to_os_t)(int, union nfs4_attr_u *,
   1208 		struct nfs4attr_to_osattr *);
   1209 
   1210 /*
   1211  * The nfs4_error_t is the basic structure to return error values
   1212  * from rfs4call.  It encapsulates the unix errno
   1213  * value, the nfsstat4 value and the rpc status value into a single
   1214  * structure.
   1215  *
   1216  * If error is set, then stat is ignored and rpc_status may be
   1217  * set if the error occurred as the result of a CLNT_CALL.  If
   1218  * stat is set, then rpc request succeeded, error and
   1219  * rpc_status are set to 0 and stat contains the result of
   1220  * operation, NFS4_OK or one of the NFS4ERR_* values.
   1221  *
   1222  * Functions which want to generate errors independently from
   1223  * rfs4call should set error to the desired errno value and
   1224  * set stat and rpc_status to 0.  nfs4_error_init() is a
   1225  * convenient function to do this.
   1226  */
   1227 typedef struct {
   1228 	int		error;
   1229 	nfsstat4	stat;
   1230 	enum clnt_stat	rpc_status;
   1231 } nfs4_error_t;
   1232 
   1233 /*
   1234  * Shared functions
   1235  */
   1236 extern void	rfs4_op_readdir(nfs_argop4 *, nfs_resop4 *,
   1237 			struct svc_req *, struct compound_state *);
   1238 extern void	nfs_fh4_copy(nfs_fh4 *, nfs_fh4 *);
   1239 
   1240 extern void	nfs4_fattr4_free(fattr4 *);
   1241 
   1242 extern int	nfs4lookup_setup(char *, lookup4_param_t *, int);
   1243 extern void	nfs4_getattr_otw_norecovery(vnode_t *,
   1244 			nfs4_ga_res_t *, nfs4_error_t *, cred_t *, int);
   1245 extern int	nfs4_getattr_otw(vnode_t *, nfs4_ga_res_t *, cred_t *, int);
   1246 extern int	nfs4cmpfh(const nfs_fh4 *, const nfs_fh4 *);
   1247 extern int	nfs4cmpfhandle(nfs4_fhandle_t *, nfs4_fhandle_t *);
   1248 extern int	nfs4getattr(vnode_t *, struct vattr *, cred_t *);
   1249 extern int	nfs4_waitfor_purge_complete(vnode_t *);
   1250 extern int	nfs4_validate_caches(vnode_t *, cred_t *);
   1251 extern int	nfs4init(int, char *);
   1252 extern void	nfs4fini(void);
   1253 extern int	nfs4_vfsinit(void);
   1254 extern void	nfs4_vfsfini(void);
   1255 
   1256 extern void	nfs4_vnops_init(void);
   1257 extern void	nfs4_vnops_fini(void);
   1258 extern void	nfs_idmap_init(void);
   1259 extern void	nfs_idmap_flush(int);
   1260 extern void	nfs_idmap_fini(void);
   1261 extern int	nfs4_rnode_init(void);
   1262 extern int	nfs4_rnode_fini(void);
   1263 extern int	nfs4_shadow_init(void);
   1264 extern int	nfs4_shadow_fini(void);
   1265 extern int	nfs4_acache_init(void);
   1266 extern int	nfs4_acache_fini(void);
   1267 extern int	nfs4_subr_init(void);
   1268 extern int	nfs4_subr_fini(void);
   1269 extern void	nfs4_acl_init(void);
   1270 extern void	nfs4_acl_free_cache(vsecattr_t *);
   1271 
   1272 extern int	geterrno4(nfsstat4);
   1273 extern nfsstat4	puterrno4(int);
   1274 extern int	nfs4_need_to_bump_seqid(COMPOUND4res_clnt *);
   1275 extern int	nfs4tsize(void);
   1276 extern int	checkauth4(struct compound_state *, struct svc_req *);
   1277 extern nfsstat4 call_checkauth4(struct compound_state *, struct svc_req *);
   1278 extern int	is_exported_sec(int, struct exportinfo *);
   1279 extern void	nfs4_vmask_to_nmask(uint_t, bitmap4 *);
   1280 extern void	nfs4_vmask_to_nmask_set(uint_t, bitmap4 *);
   1281 extern int	nfs_idmap_str_uid(utf8string *u8s, uid_t *, bool_t);
   1282 extern int	nfs_idmap_str_gid(utf8string *u8s, gid_t *, bool_t);
   1283 extern int	nfs_idmap_uid_str(uid_t, utf8string *u8s, bool_t);
   1284 extern int	nfs_idmap_gid_str(gid_t gid, utf8string *u8s, bool_t);
   1285 extern int	nfs4_time_ntov(nfstime4 *, timestruc_t *);
   1286 extern int	nfs4_time_vton(timestruc_t *, nfstime4 *);
   1287 extern char	*utf8_to_str(utf8string *, uint_t *, char *);
   1288 extern char	*utf8_to_fn(utf8string *, uint_t *, char *);
   1289 extern utf8string *str_to_utf8(char *, utf8string *);
   1290 extern utf8string *utf8_copy(utf8string *, utf8string *);
   1291 extern int	utf8_compare(const utf8string *, const utf8string *);
   1292 extern int	utf8_dir_verify(utf8string *);
   1293 extern char	*utf8_strchr(utf8string *, const char);
   1294 extern int	ln_ace4_cmp(nfsace4 *, nfsace4 *, int);
   1295 extern int	vs_aent_to_ace4(vsecattr_t *, vsecattr_t *, int, int);
   1296 extern int	vs_ace4_to_aent(vsecattr_t *, vsecattr_t *, uid_t, gid_t,
   1297     int, int, int);
   1298 extern int	vs_ace4_to_acet(vsecattr_t *, vsecattr_t *, uid_t, gid_t,
   1299     int, int);
   1300 extern int	vs_acet_to_ace4(vsecattr_t *, vsecattr_t *, int);
   1301 extern void	vs_acet_destroy(vsecattr_t *);
   1302 extern void	vs_ace4_destroy(vsecattr_t *);
   1303 extern void	vs_aent_destroy(vsecattr_t *);
   1304 
   1305 extern int	stateid4_cmp(stateid4 *, stateid4 *);
   1306 
   1307 extern vtype_t	nf4_to_vt[];
   1308 
   1309 extern struct nfs4_ntov_map nfs4_ntov_map[];
   1310 extern uint_t nfs4_ntov_map_size;
   1311 
   1312 extern kstat_named_t	*rfsproccnt_v4_ptr;
   1313 extern struct vfsops	*nfs4_vfsops;
   1314 extern struct vnodeops	*nfs4_vnodeops;
   1315 extern const struct	fs_operation_def nfs4_vnodeops_template[];
   1316 extern vnodeops_t	*nfs4_trigger_vnodeops;
   1317 extern const struct	fs_operation_def nfs4_trigger_vnodeops_template[];
   1318 
   1319 extern uint_t nfs4_tsize(struct knetconfig *);
   1320 extern uint_t rfs4_tsize(struct svc_req *);
   1321 
   1322 extern bool_t	xdr_inline_decode_nfs_fh4(uint32_t *, nfs_fh4_fmt_t *,
   1323 			uint32_t);
   1324 extern bool_t	xdr_inline_encode_nfs_fh4(uint32_t **, uint32_t *,
   1325 			nfs_fh4_fmt_t *);
   1326 
   1327 #ifdef DEBUG
   1328 extern int		rfs4_do_pre_op_attr;
   1329 extern int		rfs4_do_post_op_attr;
   1330 #endif
   1331 
   1332 extern stateid4 clnt_special0;
   1333 extern stateid4 clnt_special1;
   1334 #define	CLNT_ISSPECIAL(id) (stateid4_cmp(id, &clnt_special0) || \
   1335 				stateid4_cmp(id, &clnt_special1))
   1336 
   1337 /*
   1338  * The NFS Version 4 service procedures.
   1339  */
   1340 
   1341 extern void	rfs4_compound(COMPOUND4args *, COMPOUND4res *,
   1342 			struct exportinfo *, struct svc_req *, cred_t *, int *);
   1343 extern void	rfs4_compound_free(COMPOUND4res *);
   1344 extern void	rfs4_compound_flagproc(COMPOUND4args *, int *);
   1345 
   1346 extern int	rfs4_srvrinit(void);
   1347 extern void	rfs4_srvrfini(void);
   1348 extern void	rfs4_state_init(void);
   1349 extern void	rfs4_state_fini(void);
   1350 
   1351 #endif
   1352 #ifdef	__cplusplus
   1353 }
   1354 #endif
   1355 
   1356 #endif /* _NFS4_H */
   1357