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