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