xref: /titanic_50/usr/src/uts/common/nfs/nfs4.h (revision d338766d6376ee721b7f1778e12bf79a112bd778)
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 2006 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 extern	int		rfs4_check_stateid_seqid(rfs4_state_t *, stateid4 *);
863 extern	int		rfs4_check_lo_stateid_seqid(rfs4_lo_state_t *,
864 					stateid4 *);
865 
866 /* return values for rfs4_check_stateid_seqid() */
867 #define	NFS4_CHECK_STATEID_OKAY	1
868 #define	NFS4_CHECK_STATEID_OLD	2
869 #define	NFS4_CHECK_STATEID_BAD	3
870 #define	NFS4_CHECK_STATEID_EXPIRED	4
871 #define	NFS4_CHECK_STATEID_REPLAY	5
872 #define	NFS4_CHECK_STATEID_CLOSED	6
873 #define	NFS4_CHECK_STATEID_UNCONFIRMED	7
874 
875 /* delay() time that server is willing to briefly wait for a delegreturn */
876 #define	NFS4_DELEGATION_CONFLICT_DELAY	(hz/10)
877 
878 /*
879  * Interfaces for handling of callback's client handle cache and
880  * callback interfaces themselves.
881  */
882 extern	void		rfs4_cbinfo_free(rfs4_cbinfo_t *);
883 extern	void		rfs4_client_setcb(rfs4_client_t *, cb_client4 *,
884 					uint32_t);
885 extern	void		rfs4_deleg_cb_check(rfs4_client_t *);
886 extern	nfsstat4	rfs4_vop_getattr(vnode_t *, vattr_t *, int, cred_t *);
887 
888 /* rfs4_deleg_state_t handling and other delegation interfaces */
889 extern	rfs4_deleg_state_t *rfs4_finddeleg(rfs4_state_t *, bool_t *);
890 extern	rfs4_deleg_state_t *rfs4_finddelegstate(stateid_t *);
891 extern	bool_t		rfs4_check_recall(rfs4_state_t *, uint32_t);
892 extern	void		rfs4_recall_deleg(rfs4_file_t *,
893 				bool_t, rfs4_client_t *);
894 extern	int		rfs4_get_deleg(rfs4_state_t *,  open_delegation_type4,
895 			open_delegation_type4 (*policy)(rfs4_state_t *,
896 				open_delegation_type4 dtype));
897 extern	rfs4_deleg_state_t *rfs4_grant_delegation(delegreq_t, rfs4_state_t *,
898 				int *);
899 extern	void		rfs4_set_deleg_response(rfs4_deleg_state_t *,
900 				open_delegation4 *, nfsace4 *, int);
901 extern	void		rfs4_return_deleg(rfs4_deleg_state_t *, bool_t);
902 extern	bool_t		rfs4_is_deleg(rfs4_state_t *);
903 extern	void		rfs4_deleg_state_rele(rfs4_deleg_state_t *);
904 extern	bool_t		rfs4_check_delegated_byfp(int, rfs4_file_t *,
905 					bool_t, bool_t, bool_t, clientid4 *);
906 extern	void		rfs4_clear_dont_grant(rfs4_file_t *);
907 
908 /*
909  * nfs4 monitored operations.
910  */
911 extern int deleg_rdopen(femarg_t *, int, cred_t *);
912 extern int deleg_wropen(femarg_t *, int, cred_t *);
913 extern int deleg_rd_rwlock(femarg_t *, int, caller_context_t *);
914 extern int deleg_wr_rwlock(femarg_t *, int, caller_context_t *);
915 extern int deleg_read(femarg_t *, uio_t *, int, cred_t *, caller_context_t *);
916 extern int deleg_write(femarg_t *, uio_t *, int, cred_t *, caller_context_t *);
917 extern int deleg_setattr(femarg_t *, vattr_t *, int, cred_t *,
918 		caller_context_t *);
919 extern int deleg_space(femarg_t *, int, flock64_t *, int, offset_t, cred_t *,
920 		caller_context_t *);
921 extern int deleg_setsecattr(femarg_t *, vsecattr_t *, int, cred_t *);
922 extern int deleg_vnevent(femarg_t *, vnevent_t);
923 
924 extern void rfs4_mon_hold(void *);
925 extern void rfs4_mon_rele(void *);
926 
927 extern fem_t	*deleg_rdops;
928 extern fem_t	*deleg_wrops;
929 
930 extern	void		rfs4_unshare(rfs4_state_t *);
931 extern	void		rfs4_set_deleg_policy(srv_deleg_policy_t);
932 #ifdef DEBUG
933 #define	NFS4_DEBUG(var, args) if (var) cmn_err args
934 
935 extern int rfs4_debug;
936 extern int nfs4_client_attr_debug;
937 extern int nfs4_client_state_debug;
938 extern int nfs4_client_shadow_debug;
939 extern int nfs4_client_lock_debug;
940 extern int nfs4_client_lease_debug;
941 extern int nfs4_seqid_sync;
942 extern int nfs4_client_map_debug;
943 extern int nfs4_client_inactive_debug;
944 extern int nfs4_client_recov_debug;
945 extern int nfs4_client_recov_stub_debug;
946 extern int nfs4_client_failover_debug;
947 extern int nfs4_client_call_debug;
948 extern int nfs4_client_foo_debug;
949 extern int nfs4_client_zone_debug;
950 extern int nfs4_lost_rqst_debug;
951 extern int nfs4_open_stream_debug;
952 extern int nfs4_client_open_dg;
953 extern int nfs4_srvmnt_debug;
954 extern int nfs4_utf8_debug;
955 
956 void rfs4_dbe_debug(rfs4_dbe_t *e);
957 
958 #ifdef NFS4_DEBUG_MUTEX
959 void nfs4_debug_mutex_enter(kmutex_t *, char *, int);
960 void nfs4_debug_mutex_exit(kmutex_t *, char *, int);
961 
962 #define	mutex_enter(m) nfs4_debug_mutex_enter((m), __FILE__, __LINE__)
963 #define	mutex_exit(m) nfs4_debug_mutex_exit((m), __FILE__, __LINE__)
964 #endif /* NFS4_DEBUG_MUTEX */
965 
966 #else  /* ! DEBUG */
967 #define	NFS4_DEBUG(var, args)
968 #endif /* DEBUG */
969 
970 /*
971  * XXX - temporary for testing of volatile fh
972  */
973 
974 #ifdef VOLATILE_FH_TEST
975 
976 struct nfs_fh4_fmt {
977 	fhandle4_t	fh4_i;
978 	uint32_t	fh4_flag;
979 	uint32_t	fh4_volatile_id;
980 };
981 
982 #else /* VOLATILE_FH_TEST */
983 
984 struct nfs_fh4_fmt {
985 	fhandle4_t	fh4_i;
986 	uint32_t	fh4_flag;
987 };
988 
989 #endif /* VOLATILE_FH_TEST */
990 
991 #define	FH4_NAMEDATTR	1
992 #define	FH4_ATTRDIR	2
993 
994 #define	fh4_fsid	fh4_i.fhx_fsid
995 #define	fh4_len		fh4_i.fhx_len 	/* fid length */
996 #define	fh4_data	fh4_i.fhx_data 	/* fid bytes */
997 #define	fh4_xlen	fh4_i.fhx_xlen
998 #define	fh4_xdata	fh4_i.fhx_xdata
999 typedef struct nfs_fh4_fmt nfs_fh4_fmt_t;
1000 
1001 #define	fh4_to_fmt4(fh4p) ((nfs_fh4_fmt_t *)(fh4p)->nfs_fh4_val)
1002 #define	get_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) & (flag))
1003 #define	set_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) |= (flag))
1004 #define	clr_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) &= ~(flag))
1005 
1006 #define	NFS_FH4_LEN	sizeof (nfs_fh4_fmt_t)
1007 
1008 
1009 /*
1010  * A few definitions of repeatedly used constructs for nfsv4
1011  */
1012 #define	UTF8STRING_FREE(str)					\
1013 	kmem_free((str).utf8string_val,	(str).utf8string_len);	\
1014 	(str).utf8string_val = NULL;				\
1015 	(str).utf8string_len = 0;
1016 
1017 /*
1018  * NFS4_VOLATILE_FH yields non-zero if the filesystem uses non-persistent
1019  * filehandles.
1020  */
1021 #define	NFS4_VOLATILE_FH(mi)					\
1022 	((mi)->mi_fh_expire_type &				\
1023 	(FH4_VOLATILE_ANY | FH4_VOL_MIGRATION | FH4_VOL_RENAME))
1024 
1025 /*
1026  * NFS_IS_DOTNAME checks if the name given represents a dot or dotdot entry
1027  */
1028 #define	NFS_IS_DOTNAME(name)					\
1029 	(((name)[0] == '.') &&					\
1030 	(((name)[1] == '\0') || (((name)[1] == '.') && ((name)[2] == '\0'))))
1031 
1032 /*
1033  * Define the number of bits in a bitmap word (uint32)
1034  */
1035 #define	NFS4_BITMAP4_BITSPERWORD	(sizeof (uint32_t) * 8)
1036 
1037 /*
1038  * Define the value for the access field of the compound_state structure
1039  * based on the result of nfsauth access checking.
1040  */
1041 #define	CS_ACCESS_OK		0x1
1042 #define	CS_ACCESS_DENIED	0x2
1043 #define	CS_ACCESS_LIMITED	0x4
1044 
1045 /*
1046  * compound state in nfsv4 server
1047  */
1048 struct compound_state {
1049 	struct exportinfo *exi;
1050 	struct exportinfo *saved_exi;	/* export struct for saved_vp */
1051 	cred_t 		*basecr;	/* UNIX cred:  only RPC request */
1052 	caddr_t 	principal;
1053 	int 		nfsflavor;
1054 	cred_t 		*cr;		/* UNIX cred: RPC request and */
1055 					/* target export */
1056 	bool_t  	cont;
1057 	uint_t 		access;		/* access perm on vp per request */
1058 	bool_t 		deleg;		/* TRUE if current fh has */
1059 					/* write delegated */
1060 	vnode_t 	*vp;		/* modified by PUTFH, and by ops that */
1061 					/* input to GETFH */
1062 	bool_t 		mandlock;	/* Is mandatory locking in effect */
1063 					/* for vp */
1064 	vnode_t 	*saved_vp;	/* modified by SAVEFH, copied to */
1065 					/* vp by RESTOREFH */
1066 	nfsstat4 	*statusp;
1067 	nfs_fh4 	fh;		/* ditto. valid only if vp != NULL */
1068 	nfs_fh4 	saved_fh;	/* ditto. valid only if */
1069 					/* 	saved_vp != NULL */
1070 	struct svc_req	*req;
1071 	char 		fhbuf[NFS4_FHSIZE];
1072 };
1073 
1074 /*
1075  * Conversion commands for nfsv4 server attr checking
1076  */
1077 enum nfs4_attr_cmd {
1078 	NFS4ATTR_SUPPORTED = 0,		/* check which attrs supported */
1079 	NFS4ATTR_GETIT = 1,		/* getattr - sys to fattr4 (r) */
1080 	NFS4ATTR_SETIT = 2,		/* setattr - fattr4 to sys (w) */
1081 	NFS4ATTR_VERIT = 3,		/* verify - fattr4 to sys (r) */
1082 	NFS4ATTR_FREEIT = 4		/* free any alloc'd space for attr */
1083 };
1084 
1085 typedef enum nfs4_attr_cmd nfs4_attr_cmd_t;
1086 
1087 struct nfs4_svgetit_arg {
1088 	nfs4_attr_cmd_t op;		/* getit or setit */
1089 	struct compound_state *cs;
1090 	struct statvfs64 *sbp;
1091 	uint_t 		flag;		/* VOP_GETATTR/VOP_SETATTR flag */
1092 	uint_t 		xattr;		/* object is xattr */
1093 	bool_t 		rdattr_error_req; /* if readdir & client wants */
1094 						/* rdattr_error */
1095 	nfsstat4	rdattr_error;	/* used for per-entry status */
1096 					/* (if rdattr_err) */
1097 	bool_t		mntdfid_set;
1098 	fattr4_mounted_on_fileid
1099 			mounted_on_fileid;
1100 					/* readdir op can always return	*/
1101 					/* d_ino from server fs dirent  */
1102 					/* for mounted_on_fileid attr.	*/
1103 					/* This field holds d_ino so	*/
1104 					/* srv attr conv code can avoid */
1105 					/* doing an untraverse.		*/
1106 	vattr_t		vap[1];
1107 };
1108 
1109 struct nfs4_ntov_map {
1110 	bitmap4		fbit; 		/* FATTR4_XXX_MASKY */
1111 	uint_t 		vbit; 		/* AT_XXX */
1112 	bool_t 		vfsstat;
1113 	bool_t 		mandatory; 	/* attribute mandatory to implement? */
1114 	uint_t 		nval;
1115 	int		xdr_size;	/* Size of XDR'd attr */
1116 	xdrproc_t 	xfunc;
1117 	int (*sv_getit)(nfs4_attr_cmd_t, struct nfs4_svgetit_arg *,
1118 		union nfs4_attr_u *);	/* subroutine for getting attr. */
1119 	char 		*prtstr;	/* string attr for printing */
1120 };
1121 
1122 struct nfs4attr_to_vattr {
1123 	vnode_t 	*vp;
1124 	vattr_t 	*vap;
1125 	nfs_fh4   	*fhp;
1126 	nfsstat4	rdattr_error;
1127 	uint32_t	flag;
1128 	fattr4_change	change;
1129 	fattr4_fsid	srv_fsid;
1130 	fattr4_mounted_on_fileid	mntd_fid;
1131 };
1132 
1133 typedef struct nfs4attr_to_vattr ntov4_t;
1134 
1135 /*
1136  * nfs4attr_to_vattr flags
1137  */
1138 #define	NTOV_FHP_VALID			0x01
1139 #define	NTOV_RDATTR_ERROR_VALID		0x02
1140 #define	NTOV_CHANGE_VALID		0x04
1141 #define	NTOV_SUPP_VALID			0x08
1142 #define	NTOV_SRV_FSID_VALID		0x10
1143 #define	NTOV_MOUNTED_ON_FILEID_VALID	0x20
1144 
1145 
1146 #define	FATTR4_MANDATTR_MASK (		\
1147 	FATTR4_SUPPORTED_ATTRS_MASK |	\
1148 	FATTR4_TYPE_MASK |		\
1149 	FATTR4_FH_EXPIRE_TYPE_MASK |	\
1150 	FATTR4_CHANGE_MASK |		\
1151 	FATTR4_SIZE_MASK |		\
1152 	FATTR4_LINK_SUPPORT_MASK |	\
1153 	FATTR4_SYMLINK_SUPPORT_MASK |	\
1154 	FATTR4_NAMED_ATTR_MASK |	\
1155 	FATTR4_FSID_MASK |		\
1156 	FATTR4_UNIQUE_HANDLES_MASK |	\
1157 	FATTR4_LEASE_TIME_MASK |	\
1158 	FATTR4_RDATTR_ERROR_MASK |	\
1159 	FATTR4_FILEHANDLE_MASK)
1160 
1161 
1162 struct nfs4attr_to_osattr {
1163 	void *attrconv_arg;
1164 	uint_t mask;
1165 };
1166 
1167 struct mntinfo4;
1168 
1169 /*
1170  * lkp4_attr_setup lists the different options for attributes when calling
1171  * nfs4lookup_setup - either no attributes (just lookups - e.g., secinfo),
1172  * one component only (normal component lookup), get attributes for the
1173  * last component (e.g., mount), attributes for each component (e.g.,
1174  * failovers later), just the filehandle for the last component (e.g.,
1175  * volatile filehandle recovery), or stuff that needs OPENATTR (e.g.
1176  * looking up a named attribute or it's hidden directory).
1177  */
1178 enum lkp4_attr_setup {
1179 	LKP4_NO_ATTRIBUTES = 0,		/* no attrs or filehandles */
1180 	LKP4_ALL_ATTRIBUTES = 3,	/* multi-comp: attrs for all comps */
1181 	LKP4_LAST_NAMED_ATTR = 5,	/* multi-comp: named attr & attrdir */
1182 	LKP4_LAST_ATTRDIR = 6,		/* multi-comp: just attrdir */
1183 	LKP4_ALL_ATTR_SECINFO = 7	/* multi-comp: attrs for all comp and */
1184 					/*	secinfo for last comp */
1185 };
1186 
1187 /*
1188  * lookup4_param a set of parameters to nfs4lookup_setup -
1189  * used to setup a path lookup compound request.
1190  */
1191 typedef struct lookup4_param {
1192 	enum lkp4_attr_setup l4_getattrs; /* (in) get attrs in the lookup? */
1193 	int 		header_len;	/* (in) num ops before first lookup  */
1194 	int 		trailer_len;	/* (in) num ops after last	*/
1195 					/*	Lookup/Getattr		*/
1196 	bitmap4 	ga_bits;	/* (in) Which attributes for Getattr */
1197 	COMPOUND4args_clnt *argsp;	/* (in/out) args for compound struct */
1198 	COMPOUND4res_clnt  *resp;	/* (in/out) res for compound  struct */
1199 	int 		arglen;		/* (out) argop buffer alloc'd length */
1200 	struct mntinfo4 *mi;
1201 } lookup4_param_t;
1202 
1203 
1204 #define	NFS4_FATTR4_FINISH	-1	/* fattr4 index indicating finish */
1205 
1206 typedef int (*nfs4attr_to_os_t)(int, union nfs4_attr_u *,
1207 		struct nfs4attr_to_osattr *);
1208 
1209 /*
1210  * The nfs4_error_t is the basic structure to return error values
1211  * from rfs4call.  It encapsulates the unix errno
1212  * value, the nfsstat4 value and the rpc status value into a single
1213  * structure.
1214  *
1215  * If error is set, then stat is ignored and rpc_status may be
1216  * set if the error occurred as the result of a CLNT_CALL.  If
1217  * stat is set, then rpc request succeeded, error and
1218  * rpc_status are set to 0 and stat contains the result of
1219  * operation, NFS4_OK or one of the NFS4ERR_* values.
1220  *
1221  * Functions which want to generate errors independently from
1222  * rfs4call should set error to the desired errno value and
1223  * set stat and rpc_status to 0.  nfs4_error_init() is a
1224  * convenient function to do this.
1225  */
1226 typedef struct {
1227 	int		error;
1228 	nfsstat4	stat;
1229 	enum clnt_stat	rpc_status;
1230 } nfs4_error_t;
1231 
1232 /*
1233  * Shared functions
1234  */
1235 extern void	rfs4_op_readdir(nfs_argop4 *, nfs_resop4 *,
1236 			struct svc_req *, struct compound_state *);
1237 extern void	nfs_fh4_copy(nfs_fh4 *, nfs_fh4 *);
1238 
1239 extern void	nfs4_fattr4_free(fattr4 *);
1240 
1241 extern int	nfs4lookup_setup(char *, lookup4_param_t *, int);
1242 extern void	nfs4_getattr_otw_norecovery(vnode_t *,
1243 			nfs4_ga_res_t *, nfs4_error_t *, cred_t *, int);
1244 extern int	nfs4_getattr_otw(vnode_t *, nfs4_ga_res_t *, cred_t *, int);
1245 extern int	nfs4cmpfh(const nfs_fh4 *, const nfs_fh4 *);
1246 extern int	nfs4cmpfhandle(nfs4_fhandle_t *, nfs4_fhandle_t *);
1247 extern int	nfs4getattr(vnode_t *, struct vattr *, cred_t *);
1248 extern int	nfs4_waitfor_purge_complete(vnode_t *);
1249 extern int	nfs4_validate_caches(vnode_t *, cred_t *);
1250 extern int	nfs4init(int, char *);
1251 extern void	nfs4fini(void);
1252 extern int	nfs4_vfsinit(void);
1253 extern void	nfs4_vfsfini(void);
1254 
1255 extern void	nfs4_vnops_init(void);
1256 extern void	nfs4_vnops_fini(void);
1257 extern void	nfs_idmap_init(void);
1258 extern void	nfs_idmap_flush(int);
1259 extern void	nfs_idmap_fini(void);
1260 extern int	nfs4_rnode_init(void);
1261 extern int	nfs4_rnode_fini(void);
1262 extern int	nfs4_shadow_init(void);
1263 extern int	nfs4_shadow_fini(void);
1264 extern int	nfs4_acache_init(void);
1265 extern int	nfs4_acache_fini(void);
1266 extern int	nfs4_subr_init(void);
1267 extern int	nfs4_subr_fini(void);
1268 extern void	nfs4_acl_init(void);
1269 extern void	nfs4_acl_free_cache(vsecattr_t *);
1270 
1271 extern int	geterrno4(nfsstat4);
1272 extern nfsstat4	puterrno4(int);
1273 extern int	nfs4_need_to_bump_seqid(COMPOUND4res_clnt *);
1274 extern int	nfs4tsize(void);
1275 extern int	checkauth4(struct compound_state *, struct svc_req *);
1276 extern nfsstat4 call_checkauth4(struct compound_state *, struct svc_req *);
1277 extern int	is_exported_sec(int, struct exportinfo *);
1278 extern void	nfs4_vmask_to_nmask(uint_t, bitmap4 *);
1279 extern void	nfs4_vmask_to_nmask_set(uint_t, bitmap4 *);
1280 extern int	nfs_idmap_str_uid(utf8string *u8s, uid_t *, bool_t);
1281 extern int	nfs_idmap_str_gid(utf8string *u8s, gid_t *, bool_t);
1282 extern int	nfs_idmap_uid_str(uid_t, utf8string *u8s, bool_t);
1283 extern int	nfs_idmap_gid_str(gid_t gid, utf8string *u8s, bool_t);
1284 extern int	nfs4_time_ntov(nfstime4 *, timestruc_t *);
1285 extern int	nfs4_time_vton(timestruc_t *, nfstime4 *);
1286 extern char	*utf8_to_str(utf8string *, uint_t *, char *);
1287 extern char	*utf8_to_fn(utf8string *, uint_t *, char *);
1288 extern utf8string *str_to_utf8(char *, utf8string *);
1289 extern utf8string *utf8_copy(utf8string *, utf8string *);
1290 extern int	utf8_compare(const utf8string *, const utf8string *);
1291 extern int	utf8_dir_verify(utf8string *);
1292 extern char	*utf8_strchr(utf8string *, const char);
1293 extern int	ln_ace4_cmp(nfsace4 *, nfsace4 *, int);
1294 extern int	vs_aent_to_ace4(vsecattr_t *, vsecattr_t *, int, int);
1295 extern int	vs_ace4_to_aent(vsecattr_t *, vsecattr_t *, uid_t, gid_t,
1296     int, int, int);
1297 extern int	vs_ace4_to_acet(vsecattr_t *, vsecattr_t *, uid_t, gid_t,
1298     int, int);
1299 extern int	vs_acet_to_ace4(vsecattr_t *, vsecattr_t *, int);
1300 extern void	vs_acet_destroy(vsecattr_t *);
1301 extern void	vs_ace4_destroy(vsecattr_t *);
1302 extern void	vs_aent_destroy(vsecattr_t *);
1303 
1304 extern int	stateid4_cmp(stateid4 *, stateid4 *);
1305 
1306 extern vtype_t	nf4_to_vt[];
1307 
1308 extern struct nfs4_ntov_map nfs4_ntov_map[];
1309 extern uint_t nfs4_ntov_map_size;
1310 
1311 extern kstat_named_t	*rfsproccnt_v4_ptr;
1312 extern struct vfsops	*nfs4_vfsops;
1313 extern struct vnodeops	*nfs4_vnodeops;
1314 extern const struct	fs_operation_def nfs4_vnodeops_template[];
1315 
1316 extern uint_t nfs4_tsize(struct knetconfig *);
1317 extern uint_t rfs4_tsize(struct svc_req *);
1318 
1319 extern bool_t	xdr_inline_decode_nfs_fh4(uint32_t *, nfs_fh4_fmt_t *,
1320 			uint32_t);
1321 extern bool_t	xdr_inline_encode_nfs_fh4(uint32_t **, uint32_t *,
1322 			nfs_fh4_fmt_t *);
1323 
1324 #ifdef DEBUG
1325 extern int		rfs4_do_pre_op_attr;
1326 extern int		rfs4_do_post_op_attr;
1327 #endif
1328 
1329 extern stateid4 clnt_special0;
1330 extern stateid4 clnt_special1;
1331 #define	CLNT_ISSPECIAL(id) (stateid4_cmp(id, &clnt_special0) || \
1332 				stateid4_cmp(id, &clnt_special1))
1333 
1334 /*
1335  * The NFS Version 4 service procedures.
1336  */
1337 
1338 extern void	rfs4_compound(COMPOUND4args *, COMPOUND4res *,
1339 			struct exportinfo *, struct svc_req *, cred_t *);
1340 extern void	rfs4_compound_free(COMPOUND4res *);
1341 extern void	rfs4_compound_flagproc(COMPOUND4args *, int *);
1342 
1343 extern int	rfs4_srvrinit(void);
1344 extern void	rfs4_srvrfini(void);
1345 extern void	rfs4_state_init(void);
1346 extern void	rfs4_state_fini(void);
1347 
1348 #endif
1349 #ifdef	__cplusplus
1350 }
1351 #endif
1352 
1353 #endif /* _NFS4_H */
1354