xref: /titanic_41/usr/src/uts/common/nfs/nfs4.h (revision 75e21072b15921edb21066ef5eaa2e412b163266)
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  * share_access - how did the openowner OPEN the file (access)
547  * share_deny - how did the openowner OPEN the file (deny)
548  * closed - has this file been closed?
549  * lostatelist - root of list of lo_state associated with this state/file
550  * node - node for state struct list of states
551  */
552 typedef struct rfs4_state {
553 	rfs4_dbe_t		*rs_dbe;
554 	stateid_t		rs_stateid;
555 	rfs4_openowner_t	*rs_owner;
556 	struct rfs4_file	*rs_finfo;
557 	uint32_t		rs_share_access;
558 	uint32_t		rs_share_deny;
559 	unsigned		rs_closed:1;
560 	list_t			rs_lostatelist;
561 	list_node_t		rs_node;
562 } rfs4_state_t;
563 
564 /*
565  * Lockowner - track the lockowner and its related info
566  *
567  * dbe - encapsulation struct
568  * client - reference to the client
569  * owner - lockowner supplied by the client
570  * pid - local identifier used for file locking
571  */
572 typedef struct rfs4_lockowner {
573 	rfs4_dbe_t		*rl_dbe;
574 	rfs4_client_t		*rl_client;
575 	lock_owner4		rl_owner;
576 	pid_t			rl_pid;
577 } rfs4_lockowner_t;
578 
579 /*
580  * Lockowner_state associated with a state struct and lockowner
581  *
582  * dbe - encapsulation struct
583  * state - reference back to state struct for open file
584  * lockid - stateid for this lockowner/state
585  * locker - reference to lockowner
586  * seqid - sequence id for this lockowner/state
587  * skip_seqid_check - used on initialization of struct
588  * locks_cleaned - have all locks been released for this lockowner/file?
589  * lock_completed - successful LOCK with lockowner/file?
590  * ls_sw - used to serialize update seqid/reply/stateid handling
591  * node - node for state struct list of lo_states
592  * reply - last reply sent in relation to this lockowner/state
593  */
594 typedef struct rfs4_lo_state {
595 	rfs4_dbe_t		*rls_dbe;
596 	rfs4_state_t		*rls_state;
597 	stateid_t		rls_lockid;
598 	rfs4_lockowner_t	*rls_locker;
599 	seqid4			rls_seqid;
600 	unsigned		rls_skip_seqid_check:1;
601 	unsigned		rls_locks_cleaned:1;
602 	unsigned		rls_lock_completed:1;
603 	rfs4_state_wait_t	rls_sw;
604 	list_node_t		rls_node;
605 	nfs_resop4		rls_reply;
606 } rfs4_lo_state_t;
607 
608 /*
609  * Delegation state - per client
610  *
611  * dbe - encapsulation struct
612  * dtype - type of delegation (NONE, READ, WRITE)
613  * delegid - stateid for this delegation
614  * time_granted - time this delegation was assigned to client
615  * time_recalled - time when the server started recall process
616  * time_revoked - if revoked, time that the revoke occurred
617  * finfo - reference to the file associated with this delegation
618  * client - reference to client for which this delegation is associated
619  * node - list of delegations for the file (WRITE == 1, READ == )
620  */
621 typedef struct rfs4_deleg_state {
622 	rfs4_dbe_t		*rds_dbe;
623 	open_delegation_type4	rds_dtype;
624 	stateid_t		rds_delegid;
625 	time_t			rds_time_granted;
626 	time_t			rds_time_recalled;
627 	time_t			rds_time_revoked;
628 	struct rfs4_file	*rds_finfo;
629 	rfs4_client_t		*rds_client;
630 	list_node_t		rds_node;
631 } rfs4_deleg_state_t;
632 
633 /*
634  * Delegation info associated with the file
635  *
636  * dtype - type of delegation for file (NONE, READ, WRITE)
637  * time_returned - time that last delegation was returned for file
638  * time_recalled - time that recall sequence started
639  * time_lastgrant - time that last delegation was provided to a client
640  * time_lastwrite - time of last write to use the delegation stateid
641  * time_rm_delayed - time of last remove/rename which was DELAYed
642  * rdgrants - how many read delegations have been provided for this file
643  * wrgrants - how many write delegations provided (can only be one)
644  * recall_count - how many recall threads are outstanding
645  * recall_lock - lock to protect contents of this struct
646  * recall_cv - condition var for the "parent" thread to wait upon
647  * deleg_change_grant - value for change attribute at time of write grant
648  * deleg_change - most recent value of change obtained from client
649  * deleg_change_ts - time of last deleg_change update
650  * ever_recalled - has this particular delegation ever been recalled?
651  * dont_grant - file deletion is impending, don't grant a delegation
652  * conflicted_client - clientid of the client that caused a CB_RECALL
653  *	to occur. This is used for delegation policy (should a delegation
654  *	be granted shortly after it has been returned?)
655  */
656 typedef struct rfs4_dinfo {
657 	open_delegation_type4 rd_dtype;
658 	time_t		rd_time_returned;
659 	time_t		rd_time_recalled;
660 	time_t		rd_time_lastgrant;
661 	time_t		rd_time_lastwrite;
662 	time_t		rd_time_rm_delayed;
663 	uint32_t	rd_rdgrants;
664 	uint32_t	rd_wrgrants;
665 	int32_t		rd_recall_count;
666 	kmutex_t	rd_recall_lock[1];
667 	kcondvar_t	rd_recall_cv[1];
668 	bool_t		rd_ever_recalled;
669 	uint32_t	rd_hold_grant;
670 	clientid4	rd_conflicted_client;
671 } rfs4_dinfo_t;
672 
673 /*
674  * File
675  *
676  * dbe - encapsulation struct
677  * vp - vnode for the file that is open or has a delegation
678  * filehandle - the filehandle generated by the server for this file
679  * delegstatelist - root of delegation list for this file
680  * dinfo - see struct definition above
681  * share_deny - union of all deny modes on file
682  * share_access - union of all access modes on file
683  * access_read - count of read access
684  * access_write - count of write access
685  * deny_read - count of deny reads
686  * deny_write - count of deny writes
687  * file_rwlock - lock for serializing the removal of a file while
688  *	the state structures are active within the server
689  *
690  * 	The only requirement for locking file_rwlock is that the
691  * 	caller have a reference to the containing rfs4_file.  The dbe
692  * 	lock may or may not be held for lock/unlock of file_rwlock.
693  * 	As mentioned above, the file_rwlock is used for serialization
694  * 	of file removal and more specifically reference to the held
695  * 	vnode (e.g. vp).
696  */
697 typedef struct rfs4_file {
698 	rfs4_dbe_t	*rf_dbe;
699 	vnode_t		*rf_vp;
700 	nfs_fh4		rf_filehandle;
701 	list_t		rf_delegstatelist;
702 	rfs4_dinfo_t	rf_dinfo;
703 	uint32_t	rf_share_deny;
704 	uint32_t	rf_share_access;
705 	uint32_t	rf_access_read;
706 	uint32_t	rf_access_write;
707 	uint32_t	rf_deny_read;
708 	uint32_t	rf_deny_write;
709 	krwlock_t	rf_file_rwlock;
710 } rfs4_file_t;
711 
712 extern int	rfs4_seen_first_compound;	/* set first time we see one */
713 
714 extern rfs4_servinst_t	*rfs4_cur_servinst;	/* current server instance */
715 extern kmutex_t		rfs4_servinst_lock;	/* protects linked list */
716 extern void		rfs4_servinst_create(int, int, char **);
717 extern void		rfs4_servinst_destroy_all(void);
718 extern void		rfs4_servinst_assign(rfs4_client_t *,
719 			    rfs4_servinst_t *);
720 extern rfs4_servinst_t	*rfs4_servinst(rfs4_client_t *);
721 extern int		rfs4_clnt_in_grace(rfs4_client_t *);
722 extern int		rfs4_servinst_in_grace(rfs4_servinst_t *);
723 extern int		rfs4_servinst_grace_new(rfs4_servinst_t *);
724 extern void		rfs4_grace_start(rfs4_servinst_t *);
725 extern void		rfs4_grace_start_new(void);
726 extern void		rfs4_grace_reset_all(void);
727 extern void		rfs4_ss_oldstate(rfs4_oldstate_t *, char *, char *);
728 extern void		rfs4_dss_readstate(int, char **);
729 
730 /*
731  * rfs4_deleg_policy is used to signify the server's global delegation
732  * policy.  The default is to NEVER delegate files and the
733  * administrator must configure the server to enable delegations.
734  *
735  * The disable/enable delegation functions are used to eliminate a
736  * race with exclusive creates.
737  */
738 typedef enum {
739 	SRV_NEVER_DELEGATE = 0,
740 	SRV_NORMAL_DELEGATE = 1
741 } srv_deleg_policy_t;
742 
743 extern srv_deleg_policy_t rfs4_deleg_policy;
744 extern kmutex_t rfs4_deleg_lock;
745 extern void rfs4_disable_delegation(void), rfs4_enable_delegation(void);
746 
747 /*
748  * Request types for delegation. These correspond with
749  * open_delegation_type4 with the addition of a new value, DELEG_ANY,
750  * to reqequest any delegation.
751  */
752 typedef enum {
753 	DELEG_NONE = 0,		/* Corresponds to OPEN_DELEG_NONE */
754 	DELEG_READ = 1,		/* Corresponds to OPEN_DELEG_READ */
755 	DELEG_WRITE = 2,	/* Corresponds to OPEN_DELEG_WRITE */
756 	DELEG_ANY = -1		/* New value to request any delegation type */
757 } delegreq_t;
758 
759 #define	NFS4_DELEG4TYPE2REQTYPE(x) (delegreq_t)(x)
760 
761 /*
762  * Various interfaces to manipulate the state structures introduced
763  * above
764  */
765 extern	kmutex_t	rfs4_state_lock;
766 extern	void		rfs4_clean_state_exi(struct exportinfo *exi);
767 extern	void		rfs4_free_reply(nfs_resop4 *);
768 extern	void		rfs4_copy_reply(nfs_resop4 *, nfs_resop4 *);
769 
770 /* rfs4_client_t handling */
771 extern	rfs4_client_t	*rfs4_findclient(nfs_client_id4 *,
772 					bool_t *, rfs4_client_t *);
773 extern	rfs4_client_t	*rfs4_findclient_by_id(clientid4, bool_t);
774 extern	void		rfs4_client_rele(rfs4_client_t *);
775 extern	void		rfs4_client_close(rfs4_client_t *);
776 extern	void		rfs4_client_state_remove(rfs4_client_t *);
777 extern	void		rfs4_client_scv_next(rfs4_client_t *);
778 extern	void		rfs4_update_lease(rfs4_client_t *);
779 extern	bool_t		rfs4_lease_expired(rfs4_client_t *);
780 extern	nfsstat4	rfs4_check_clientid(clientid4 *, int);
781 
782 /* rfs4_openowner_t handling */
783 extern	rfs4_openowner_t *rfs4_findopenowner(open_owner4 *, bool_t *, seqid4);
784 extern	void		rfs4_update_open_sequence(rfs4_openowner_t *);
785 extern	void		rfs4_update_open_resp(rfs4_openowner_t *,
786 					nfs_resop4 *, nfs_fh4 *);
787 extern	void		rfs4_openowner_rele(rfs4_openowner_t *);
788 extern	void		rfs4_free_opens(rfs4_openowner_t *, bool_t, bool_t);
789 
790 /* rfs4_lockowner_t handling */
791 extern	rfs4_lockowner_t *rfs4_findlockowner(lock_owner4 *, bool_t *);
792 extern	rfs4_lockowner_t *rfs4_findlockowner_by_pid(pid_t);
793 extern	void		rfs4_lockowner_rele(rfs4_lockowner_t *);
794 
795 /* rfs4_state_t handling */
796 extern	rfs4_state_t	*rfs4_findstate_by_owner_file(rfs4_openowner_t *,
797 					rfs4_file_t *, bool_t *);
798 extern	void		rfs4_state_rele(rfs4_state_t *);
799 extern	void		rfs4_state_close(rfs4_state_t *, bool_t,
800 					bool_t, cred_t *);
801 extern	void		rfs4_release_share_lock_state(rfs4_state_t *,
802 					cred_t *, bool_t);
803 extern	void		rfs4_close_all_state(rfs4_file_t *);
804 
805 /* rfs4_lo_state_t handling */
806 extern	rfs4_lo_state_t *rfs4_findlo_state_by_owner(rfs4_lockowner_t *,
807 						rfs4_state_t *, bool_t *);
808 extern	void		rfs4_lo_state_rele(rfs4_lo_state_t *, bool_t);
809 extern	void		rfs4_update_lock_sequence(rfs4_lo_state_t *);
810 extern	void		rfs4_update_lock_resp(rfs4_lo_state_t *,
811 					nfs_resop4 *);
812 
813 /* rfs4_file_t handling */
814 extern	rfs4_file_t	*rfs4_findfile(vnode_t *, nfs_fh4 *, bool_t *);
815 extern	rfs4_file_t	*rfs4_findfile_withlock(vnode_t *, nfs_fh4 *,
816 						bool_t *);
817 extern	void		rfs4_file_rele(rfs4_file_t *);
818 
819 /* General collection of "get state" functions */
820 extern	nfsstat4	rfs4_get_state(stateid4 *, rfs4_state_t **,
821 					rfs4_dbsearch_type_t);
822 extern	nfsstat4	rfs4_get_deleg_state(stateid4 *,
823 					rfs4_deleg_state_t **);
824 extern	nfsstat4	rfs4_get_lo_state(stateid4 *, rfs4_lo_state_t **,
825 					bool_t);
826 extern	nfsstat4	rfs4_check_stateid(int, vnode_t *, stateid4 *,
827 					bool_t, bool_t *, bool_t,
828 					caller_context_t *);
829 extern	int		rfs4_check_stateid_seqid(rfs4_state_t *, stateid4 *);
830 extern	int		rfs4_check_lo_stateid_seqid(rfs4_lo_state_t *,
831 					stateid4 *);
832 
833 /* return values for rfs4_check_stateid_seqid() */
834 #define	NFS4_CHECK_STATEID_OKAY	1
835 #define	NFS4_CHECK_STATEID_OLD	2
836 #define	NFS4_CHECK_STATEID_BAD	3
837 #define	NFS4_CHECK_STATEID_EXPIRED	4
838 #define	NFS4_CHECK_STATEID_REPLAY	5
839 #define	NFS4_CHECK_STATEID_CLOSED	6
840 #define	NFS4_CHECK_STATEID_UNCONFIRMED	7
841 
842 /* delay() time that server is willing to briefly wait for a delegreturn */
843 #define	NFS4_DELEGATION_CONFLICT_DELAY	(hz/10)
844 
845 /*
846  * Interfaces for handling of callback's client handle cache and
847  * callback interfaces themselves.
848  */
849 extern	void		rfs4_cbinfo_free(rfs4_cbinfo_t *);
850 extern	void		rfs4_client_setcb(rfs4_client_t *, cb_client4 *,
851 					uint32_t);
852 extern	void		rfs4_deleg_cb_check(rfs4_client_t *);
853 extern	nfsstat4	rfs4_vop_getattr(vnode_t *, vattr_t *, int, cred_t *);
854 
855 /* rfs4_deleg_state_t handling and other delegation interfaces */
856 extern	rfs4_deleg_state_t *rfs4_finddeleg(rfs4_state_t *, bool_t *);
857 extern	rfs4_deleg_state_t *rfs4_finddelegstate(stateid_t *);
858 extern	bool_t		rfs4_check_recall(rfs4_state_t *, uint32_t);
859 extern	void		rfs4_recall_deleg(rfs4_file_t *,
860 				bool_t, rfs4_client_t *);
861 extern	int		rfs4_get_deleg(rfs4_state_t *,  open_delegation_type4,
862 			open_delegation_type4 (*policy)(rfs4_state_t *,
863 				open_delegation_type4 dtype));
864 extern	rfs4_deleg_state_t *rfs4_grant_delegation(delegreq_t, rfs4_state_t *,
865 				int *);
866 extern	void		rfs4_set_deleg_response(rfs4_deleg_state_t *,
867 				open_delegation4 *, nfsace4 *, int);
868 extern	void		rfs4_return_deleg(rfs4_deleg_state_t *, bool_t);
869 extern	bool_t		rfs4_is_deleg(rfs4_state_t *);
870 extern	void		rfs4_deleg_state_rele(rfs4_deleg_state_t *);
871 extern	bool_t		rfs4_check_delegated_byfp(int, rfs4_file_t *,
872 					bool_t, bool_t, bool_t, clientid4 *);
873 extern	void		rfs4_clear_dont_grant(rfs4_file_t *);
874 
875 /*
876  * nfs4 monitored operations.
877  */
878 extern int deleg_rd_open(femarg_t *, int, cred_t *, caller_context_t *);
879 extern int deleg_wr_open(femarg_t *, int, cred_t *, caller_context_t *);
880 extern int deleg_wr_read(femarg_t *, uio_t *, int, cred_t *,
881 	    caller_context_t *);
882 extern int deleg_rd_write(femarg_t *, uio_t *, int, cred_t *,
883 	    caller_context_t *);
884 extern int deleg_wr_write(femarg_t *, uio_t *, int, cred_t *,
885 	    caller_context_t *);
886 extern int deleg_rd_setattr(femarg_t *, vattr_t *, int, cred_t *,
887 		caller_context_t *);
888 extern int deleg_wr_setattr(femarg_t *, vattr_t *, int, cred_t *,
889 		caller_context_t *);
890 extern int deleg_rd_rwlock(femarg_t *, int, caller_context_t *);
891 extern int deleg_wr_rwlock(femarg_t *, int, caller_context_t *);
892 extern int deleg_rd_space(femarg_t *, int, flock64_t *, int, offset_t, cred_t *,
893 		caller_context_t *);
894 extern int deleg_wr_space(femarg_t *, int, flock64_t *, int, offset_t, cred_t *,
895 		caller_context_t *);
896 extern int deleg_rd_setsecattr(femarg_t *, vsecattr_t *, int, cred_t *,
897 		caller_context_t *);
898 extern int deleg_wr_setsecattr(femarg_t *, vsecattr_t *, int, cred_t *,
899 		caller_context_t *);
900 extern int deleg_rd_vnevent(femarg_t *, vnevent_t, vnode_t *, char *,
901 		caller_context_t *);
902 extern int deleg_wr_vnevent(femarg_t *, vnevent_t, vnode_t *, char *,
903 		caller_context_t *);
904 
905 extern void rfs4_mon_hold(void *);
906 extern void rfs4_mon_rele(void *);
907 
908 extern fem_t	*deleg_rdops;
909 extern fem_t	*deleg_wrops;
910 
911 extern int rfs4_share(rfs4_state_t *, uint32_t, uint32_t);
912 extern int rfs4_unshare(rfs4_state_t *);
913 extern	void		rfs4_set_deleg_policy(srv_deleg_policy_t);
914 #ifdef DEBUG
915 #define	NFS4_DEBUG(var, args) if (var) cmn_err args
916 
917 extern int rfs4_debug;
918 extern int nfs4_client_attr_debug;
919 extern int nfs4_client_state_debug;
920 extern int nfs4_client_shadow_debug;
921 extern int nfs4_client_lock_debug;
922 extern int nfs4_client_lease_debug;
923 extern int nfs4_seqid_sync;
924 extern int nfs4_client_map_debug;
925 extern int nfs4_client_inactive_debug;
926 extern int nfs4_client_recov_debug;
927 extern int nfs4_client_failover_debug;
928 extern int nfs4_client_call_debug;
929 extern int nfs4_client_foo_debug;
930 extern int nfs4_client_zone_debug;
931 extern int nfs4_lost_rqst_debug;
932 extern int nfs4_open_stream_debug;
933 extern int nfs4_client_open_dg;
934 extern int nfs4_srvmnt_debug;
935 extern int nfs4_utf8_debug;
936 
937 void rfs4_dbe_debug(rfs4_dbe_t *e);
938 
939 #ifdef NFS4_DEBUG_MUTEX
940 void nfs4_debug_mutex_enter(kmutex_t *, char *, int);
941 void nfs4_debug_mutex_exit(kmutex_t *, char *, int);
942 
943 #define	mutex_enter(m) nfs4_debug_mutex_enter((m), __FILE__, __LINE__)
944 #define	mutex_exit(m) nfs4_debug_mutex_exit((m), __FILE__, __LINE__)
945 #endif /* NFS4_DEBUG_MUTEX */
946 
947 #else  /* ! DEBUG */
948 #define	NFS4_DEBUG(var, args)
949 #endif /* DEBUG */
950 
951 /*
952  * XXX - temporary for testing of volatile fh
953  */
954 
955 #ifdef VOLATILE_FH_TEST
956 
957 struct nfs_fh4_fmt {
958 	fhandle4_t	fh4_i;
959 	uint32_t	fh4_flag;
960 	uint32_t	fh4_volatile_id;
961 };
962 
963 #else /* VOLATILE_FH_TEST */
964 
965 struct nfs_fh4_fmt {
966 	fhandle4_t	fh4_i;
967 	uint32_t	fh4_flag;
968 };
969 
970 #endif /* VOLATILE_FH_TEST */
971 
972 #define	FH4_NAMEDATTR	1
973 #define	FH4_ATTRDIR	2
974 
975 #define	fh4_fsid	fh4_i.fhx_fsid
976 #define	fh4_len		fh4_i.fhx_len 	/* fid length */
977 #define	fh4_data	fh4_i.fhx_data 	/* fid bytes */
978 #define	fh4_xlen	fh4_i.fhx_xlen
979 #define	fh4_xdata	fh4_i.fhx_xdata
980 typedef struct nfs_fh4_fmt nfs_fh4_fmt_t;
981 
982 #define	fh4_to_fmt4(fh4p) ((nfs_fh4_fmt_t *)(fh4p)->nfs_fh4_val)
983 #define	get_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) & (flag))
984 #define	set_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) |= (flag))
985 #define	clr_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) &= ~(flag))
986 
987 #define	NFS_FH4_LEN	sizeof (nfs_fh4_fmt_t)
988 
989 /*
990  * Copy fields from external (fhandle_t) to in-memory (nfs_fh4_fmt_t)
991  * format to support export info checking.  It does not copy over
992  * the complete filehandle, just the fsid, xlen and xdata.  It may
993  * need to be changed to be used in other places.
994  *
995  * NOTE: The macro expects the space to be  pre-allocated for
996  * the contents of nfs_fh4_fmt_t.
997  */
998 #define	FH_TO_FMT4(exifh, nfs_fmt) {				\
999 	bzero((nfs_fmt), NFS_FH4_LEN);				\
1000 	(nfs_fmt)->fh4_fsid = (exifh)->fh_fsid;			\
1001 	(nfs_fmt)->fh4_xlen = (exifh)->fh_xlen;			\
1002 	bcopy((exifh)->fh_xdata, (nfs_fmt)->fh4_xdata,		\
1003 	    (exifh)->fh_xlen);					\
1004 }
1005 
1006 /*
1007  * A few definitions of repeatedly used constructs for nfsv4
1008  */
1009 #define	UTF8STRING_FREE(str)					\
1010 	kmem_free((str).utf8string_val,	(str).utf8string_len);	\
1011 	(str).utf8string_val = NULL;				\
1012 	(str).utf8string_len = 0;
1013 
1014 /*
1015  * NFS4_VOLATILE_FH yields non-zero if the filesystem uses non-persistent
1016  * filehandles.
1017  */
1018 #define	NFS4_VOLATILE_FH(mi)					\
1019 	((mi)->mi_fh_expire_type &				\
1020 	(FH4_VOLATILE_ANY | FH4_VOL_MIGRATION | FH4_VOL_RENAME))
1021 
1022 /*
1023  * NFS_IS_DOTNAME checks if the name given represents a dot or dotdot entry
1024  */
1025 #define	NFS_IS_DOTNAME(name)					\
1026 	(((name)[0] == '.') &&					\
1027 	(((name)[1] == '\0') || (((name)[1] == '.') && ((name)[2] == '\0'))))
1028 
1029 /*
1030  * Define the number of bits in a bitmap word (uint32)
1031  */
1032 #define	NFS4_BITMAP4_BITSPERWORD	(sizeof (uint32_t) * 8)
1033 
1034 /*
1035  * Define the value for the access field of the compound_state structure
1036  * based on the result of nfsauth access checking.
1037  */
1038 #define	CS_ACCESS_OK		0x1
1039 #define	CS_ACCESS_DENIED	0x2
1040 #define	CS_ACCESS_LIMITED	0x4
1041 
1042 /*
1043  * compound state in nfsv4 server
1044  */
1045 struct compound_state {
1046 	struct exportinfo *exi;
1047 	struct exportinfo *saved_exi;	/* export struct for saved_vp */
1048 	cred_t 		*basecr;	/* UNIX cred:  only RPC request */
1049 	caddr_t 	principal;
1050 	int 		nfsflavor;
1051 	cred_t 		*cr;		/* UNIX cred: RPC request and */
1052 					/* target export */
1053 	bool_t  	cont;
1054 	uint_t 		access;		/* access perm on vp per request */
1055 	bool_t 		deleg;		/* TRUE if current fh has */
1056 					/* write delegated */
1057 	vnode_t 	*vp;		/* modified by PUTFH, and by ops that */
1058 					/* input to GETFH */
1059 	bool_t 		mandlock;	/* Is mandatory locking in effect */
1060 					/* for vp */
1061 	vnode_t 	*saved_vp;	/* modified by SAVEFH, copied to */
1062 					/* vp by RESTOREFH */
1063 	nfsstat4 	*statusp;
1064 	nfs_fh4 	fh;		/* ditto. valid only if vp != NULL */
1065 	nfs_fh4 	saved_fh;	/* ditto. valid only if */
1066 					/* 	saved_vp != NULL */
1067 	struct svc_req	*req;
1068 	char 		fhbuf[NFS4_FHSIZE];
1069 };
1070 
1071 /*
1072  * Conversion commands for nfsv4 server attr checking
1073  */
1074 enum nfs4_attr_cmd {
1075 	NFS4ATTR_SUPPORTED = 0,		/* check which attrs supported */
1076 	NFS4ATTR_GETIT = 1,		/* getattr - sys to fattr4 (r) */
1077 	NFS4ATTR_SETIT = 2,		/* setattr - fattr4 to sys (w) */
1078 	NFS4ATTR_VERIT = 3,		/* verify - fattr4 to sys (r) */
1079 	NFS4ATTR_FREEIT = 4		/* free any alloc'd space for attr */
1080 };
1081 
1082 typedef enum nfs4_attr_cmd nfs4_attr_cmd_t;
1083 
1084 struct nfs4_svgetit_arg {
1085 	nfs4_attr_cmd_t op;		/* getit or setit */
1086 	struct compound_state *cs;
1087 	struct statvfs64 *sbp;
1088 	uint_t 		flag;		/* VOP_GETATTR/VOP_SETATTR flag */
1089 	uint_t 		xattr;		/* object is xattr */
1090 	bool_t 		rdattr_error_req; /* if readdir & client wants */
1091 						/* rdattr_error */
1092 	nfsstat4	rdattr_error;	/* used for per-entry status */
1093 					/* (if rdattr_err) */
1094 	bool_t		mntdfid_set;
1095 	fattr4_mounted_on_fileid
1096 			mounted_on_fileid;
1097 					/* readdir op can always return	*/
1098 					/* d_ino from server fs dirent  */
1099 					/* for mounted_on_fileid attr.	*/
1100 					/* This field holds d_ino so	*/
1101 					/* srv attr conv code can avoid */
1102 					/* doing an untraverse.		*/
1103 	vattr_t		vap[1];
1104 };
1105 
1106 struct nfs4_ntov_map {
1107 	bitmap4		fbit; 		/* FATTR4_XXX_MASKY */
1108 	uint_t 		vbit; 		/* AT_XXX */
1109 	bool_t 		vfsstat;
1110 	bool_t 		mandatory; 	/* attribute mandatory to implement? */
1111 	uint_t 		nval;
1112 	int		xdr_size;	/* Size of XDR'd attr */
1113 	xdrproc_t 	xfunc;
1114 	int (*sv_getit)(nfs4_attr_cmd_t, struct nfs4_svgetit_arg *,
1115 		union nfs4_attr_u *);	/* subroutine for getting attr. */
1116 	char 		*prtstr;	/* string attr for printing */
1117 };
1118 
1119 struct nfs4attr_to_vattr {
1120 	vnode_t 	*vp;
1121 	vattr_t 	*vap;
1122 	nfs_fh4   	*fhp;
1123 	nfsstat4	rdattr_error;
1124 	uint32_t	flag;
1125 	fattr4_change	change;
1126 	fattr4_fsid	srv_fsid;
1127 	fattr4_mounted_on_fileid	mntd_fid;
1128 };
1129 
1130 typedef struct nfs4attr_to_vattr ntov4_t;
1131 
1132 /*
1133  * nfs4attr_to_vattr flags
1134  */
1135 #define	NTOV_FHP_VALID			0x01
1136 #define	NTOV_RDATTR_ERROR_VALID		0x02
1137 #define	NTOV_CHANGE_VALID		0x04
1138 #define	NTOV_SUPP_VALID			0x08
1139 #define	NTOV_SRV_FSID_VALID		0x10
1140 #define	NTOV_MOUNTED_ON_FILEID_VALID	0x20
1141 
1142 
1143 #define	FATTR4_MANDATTR_MASK (		\
1144 	FATTR4_SUPPORTED_ATTRS_MASK |	\
1145 	FATTR4_TYPE_MASK |		\
1146 	FATTR4_FH_EXPIRE_TYPE_MASK |	\
1147 	FATTR4_CHANGE_MASK |		\
1148 	FATTR4_SIZE_MASK |		\
1149 	FATTR4_LINK_SUPPORT_MASK |	\
1150 	FATTR4_SYMLINK_SUPPORT_MASK |	\
1151 	FATTR4_NAMED_ATTR_MASK |	\
1152 	FATTR4_FSID_MASK |		\
1153 	FATTR4_UNIQUE_HANDLES_MASK |	\
1154 	FATTR4_LEASE_TIME_MASK |	\
1155 	FATTR4_RDATTR_ERROR_MASK |	\
1156 	FATTR4_FILEHANDLE_MASK)
1157 
1158 
1159 struct nfs4attr_to_osattr {
1160 	void *attrconv_arg;
1161 	uint_t mask;
1162 };
1163 
1164 struct mntinfo4;
1165 
1166 /*
1167  * lkp4_attr_setup lists the different options for attributes when calling
1168  * nfs4lookup_setup - either no attributes (just lookups - e.g., secinfo),
1169  * one component only (normal component lookup), get attributes for the
1170  * last component (e.g., mount), attributes for each component (e.g.,
1171  * failovers later), just the filehandle for the last component (e.g.,
1172  * volatile filehandle recovery), or stuff that needs OPENATTR (e.g.
1173  * looking up a named attribute or it's hidden directory).
1174  */
1175 enum lkp4_attr_setup {
1176 	LKP4_NO_ATTRIBUTES = 0,		/* no attrs or filehandles */
1177 	LKP4_ALL_ATTRIBUTES = 3,	/* multi-comp: attrs for all comps */
1178 	LKP4_LAST_NAMED_ATTR = 5,	/* multi-comp: named attr & attrdir */
1179 	LKP4_LAST_ATTRDIR = 6,		/* multi-comp: just attrdir */
1180 	LKP4_ALL_ATTR_SECINFO = 7	/* multi-comp: attrs for all comp and */
1181 					/*	secinfo for last comp */
1182 };
1183 
1184 /*
1185  * lookup4_param a set of parameters to nfs4lookup_setup -
1186  * used to setup a path lookup compound request.
1187  */
1188 typedef struct lookup4_param {
1189 	enum lkp4_attr_setup l4_getattrs; /* (in) get attrs in the lookup? */
1190 	int 		header_len;	/* (in) num ops before first lookup  */
1191 	int 		trailer_len;	/* (in) num ops after last	*/
1192 					/*	Lookup/Getattr		*/
1193 	bitmap4 	ga_bits;	/* (in) Which attributes for Getattr */
1194 	COMPOUND4args_clnt *argsp;	/* (in/out) args for compound struct */
1195 	COMPOUND4res_clnt  *resp;	/* (in/out) res for compound  struct */
1196 	int 		arglen;		/* (out) argop buffer alloc'd length */
1197 	struct mntinfo4 *mi;
1198 } lookup4_param_t;
1199 
1200 
1201 #define	NFS4_FATTR4_FINISH	-1	/* fattr4 index indicating finish */
1202 
1203 typedef int (*nfs4attr_to_os_t)(int, union nfs4_attr_u *,
1204 		struct nfs4attr_to_osattr *);
1205 
1206 /*
1207  * The nfs4_error_t is the basic structure to return error values
1208  * from rfs4call.  It encapsulates the unix errno
1209  * value, the nfsstat4 value and the rpc status value into a single
1210  * structure.
1211  *
1212  * If error is set, then stat is ignored and rpc_status may be
1213  * set if the error occurred as the result of a CLNT_CALL.  If
1214  * stat is set, then rpc request succeeded, error and
1215  * rpc_status are set to 0 and stat contains the result of
1216  * operation, NFS4_OK or one of the NFS4ERR_* values.
1217  *
1218  * Functions which want to generate errors independently from
1219  * rfs4call should set error to the desired errno value and
1220  * set stat and rpc_status to 0.  nfs4_error_init() is a
1221  * convenient function to do this.
1222  */
1223 typedef struct {
1224 	int		error;
1225 	nfsstat4	stat;
1226 	enum clnt_stat	rpc_status;
1227 } nfs4_error_t;
1228 
1229 /*
1230  * Shared functions
1231  */
1232 extern void	rfs4_op_readdir(nfs_argop4 *, nfs_resop4 *,
1233 			struct svc_req *, struct compound_state *);
1234 extern void	nfs_fh4_copy(nfs_fh4 *, nfs_fh4 *);
1235 
1236 extern void	nfs4_fattr4_free(fattr4 *);
1237 
1238 extern int	nfs4lookup_setup(char *, lookup4_param_t *, int);
1239 extern void	nfs4_getattr_otw_norecovery(vnode_t *,
1240 			nfs4_ga_res_t *, nfs4_error_t *, cred_t *, int);
1241 extern int	nfs4_getattr_otw(vnode_t *, nfs4_ga_res_t *, cred_t *, int);
1242 extern int	nfs4cmpfh(const nfs_fh4 *, const nfs_fh4 *);
1243 extern int	nfs4cmpfhandle(nfs4_fhandle_t *, nfs4_fhandle_t *);
1244 extern int	nfs4getattr(vnode_t *, struct vattr *, cred_t *);
1245 extern int	nfs4_waitfor_purge_complete(vnode_t *);
1246 extern int	nfs4_validate_caches(vnode_t *, cred_t *);
1247 extern int	nfs4init(int, char *);
1248 extern void	nfs4fini(void);
1249 extern int	nfs4_vfsinit(void);
1250 extern void	nfs4_vfsfini(void);
1251 
1252 extern void	nfs4_vnops_init(void);
1253 extern void	nfs4_vnops_fini(void);
1254 extern void	nfs_idmap_init(void);
1255 extern void	nfs_idmap_flush(int);
1256 extern void	nfs_idmap_fini(void);
1257 extern int	nfs4_rnode_init(void);
1258 extern int	nfs4_rnode_fini(void);
1259 extern int	nfs4_shadow_init(void);
1260 extern int	nfs4_shadow_fini(void);
1261 extern int	nfs4_acache_init(void);
1262 extern int	nfs4_acache_fini(void);
1263 extern int	nfs4_subr_init(void);
1264 extern int	nfs4_subr_fini(void);
1265 extern void	nfs4_acl_init(void);
1266 extern void	nfs4_acl_free_cache(vsecattr_t *);
1267 
1268 extern int	geterrno4(nfsstat4);
1269 extern nfsstat4	puterrno4(int);
1270 extern int	nfs4_need_to_bump_seqid(COMPOUND4res_clnt *);
1271 extern int	nfs4tsize(void);
1272 extern int	checkauth4(struct compound_state *, struct svc_req *);
1273 extern nfsstat4 call_checkauth4(struct compound_state *, struct svc_req *);
1274 extern int	is_exported_sec(int, struct exportinfo *);
1275 extern void	nfs4_vmask_to_nmask(uint_t, bitmap4 *);
1276 extern void	nfs4_vmask_to_nmask_set(uint_t, bitmap4 *);
1277 extern int	nfs_idmap_str_uid(utf8string *u8s, uid_t *, bool_t);
1278 extern int	nfs_idmap_str_gid(utf8string *u8s, gid_t *, bool_t);
1279 extern int	nfs_idmap_uid_str(uid_t, utf8string *u8s, bool_t);
1280 extern int	nfs_idmap_gid_str(gid_t gid, utf8string *u8s, bool_t);
1281 extern int	nfs4_time_ntov(nfstime4 *, timestruc_t *);
1282 extern int	nfs4_time_vton(timestruc_t *, nfstime4 *);
1283 extern char	*utf8_to_str(utf8string *, uint_t *, char *);
1284 extern char	*utf8_to_fn(utf8string *, uint_t *, char *);
1285 extern utf8string *str_to_utf8(char *, utf8string *);
1286 extern utf8string *utf8_copy(utf8string *, utf8string *);
1287 extern int	utf8_compare(const utf8string *, const utf8string *);
1288 extern int	utf8_dir_verify(utf8string *);
1289 extern char	*utf8_strchr(utf8string *, const char);
1290 extern int	ln_ace4_cmp(nfsace4 *, nfsace4 *, int);
1291 extern int	vs_aent_to_ace4(vsecattr_t *, vsecattr_t *, int, int);
1292 extern int	vs_ace4_to_aent(vsecattr_t *, vsecattr_t *, uid_t, gid_t,
1293     int, int, int);
1294 extern int	vs_ace4_to_acet(vsecattr_t *, vsecattr_t *, uid_t, gid_t,
1295     int, int);
1296 extern int	vs_acet_to_ace4(vsecattr_t *, vsecattr_t *, int);
1297 extern void	vs_acet_destroy(vsecattr_t *);
1298 extern void	vs_ace4_destroy(vsecattr_t *);
1299 extern void	vs_aent_destroy(vsecattr_t *);
1300 
1301 extern int	stateid4_cmp(stateid4 *, stateid4 *);
1302 
1303 extern vtype_t	nf4_to_vt[];
1304 
1305 extern struct nfs4_ntov_map nfs4_ntov_map[];
1306 extern uint_t nfs4_ntov_map_size;
1307 
1308 extern kstat_named_t	*rfsproccnt_v4_ptr;
1309 extern struct vfsops	*nfs4_vfsops;
1310 extern struct vnodeops	*nfs4_vnodeops;
1311 extern const struct	fs_operation_def nfs4_vnodeops_template[];
1312 extern vnodeops_t	*nfs4_trigger_vnodeops;
1313 extern const struct	fs_operation_def nfs4_trigger_vnodeops_template[];
1314 
1315 extern uint_t nfs4_tsize(struct knetconfig *);
1316 extern uint_t rfs4_tsize(struct svc_req *);
1317 
1318 extern bool_t	xdr_inline_decode_nfs_fh4(uint32_t *, nfs_fh4_fmt_t *,
1319 			uint32_t);
1320 extern bool_t	xdr_inline_encode_nfs_fh4(uint32_t **, uint32_t *,
1321 			nfs_fh4_fmt_t *);
1322 
1323 #ifdef DEBUG
1324 extern int		rfs4_do_pre_op_attr;
1325 extern int		rfs4_do_post_op_attr;
1326 #endif
1327 
1328 extern stateid4 clnt_special0;
1329 extern stateid4 clnt_special1;
1330 #define	CLNT_ISSPECIAL(id) (stateid4_cmp(id, &clnt_special0) || \
1331 				stateid4_cmp(id, &clnt_special1))
1332 
1333 /*
1334  * The NFS Version 4 service procedures.
1335  */
1336 
1337 extern void	rfs4_compound(COMPOUND4args *, COMPOUND4res *,
1338 			struct exportinfo *, struct svc_req *, cred_t *, int *);
1339 extern void	rfs4_compound_free(COMPOUND4res *);
1340 extern void	rfs4_compound_flagproc(COMPOUND4args *, int *);
1341 
1342 extern int	rfs4_srvrinit(void);
1343 extern void	rfs4_srvrfini(void);
1344 extern void	rfs4_state_init(void);
1345 extern void	rfs4_state_fini(void);
1346 
1347 #endif
1348 #ifdef	__cplusplus
1349 }
1350 #endif
1351 
1352 #endif /* _NFS4_H */
1353