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