xref: /illumos-gate/usr/src/uts/common/nfs/nfs4_clnt.h (revision c25309d42d46e04be84e0dbadaf3c9ab3369ad05)
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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /*	All Rights Reserved   */
28 
29 /*
30  * Portions of this source code were derived from Berkeley 4.3 BSD
31  * under license from the Regents of the University of California.
32  */
33 
34 #ifndef _NFS4_CLNT_H
35 #define	_NFS4_CLNT_H
36 
37 #include <sys/errno.h>
38 #include <sys/types.h>
39 #include <sys/kstat.h>
40 #include <sys/time.h>
41 #include <sys/flock.h>
42 #include <vm/page.h>
43 #include <nfs/nfs4_kprot.h>
44 #include <nfs/nfs4.h>
45 #include <nfs/rnode.h>
46 #include <sys/avl.h>
47 #include <sys/list.h>
48 #include <rpc/auth.h>
49 
50 #ifdef	__cplusplus
51 extern "C" {
52 #endif
53 
54 #define	NFS4_SIZE_OK(size)	((size) <= MAXOFFSET_T)
55 
56 /* Four states of nfs4_server's lease_valid */
57 #define	NFS4_LEASE_INVALID		0
58 #define	NFS4_LEASE_VALID		1
59 #define	NFS4_LEASE_UNINITIALIZED	2
60 #define	NFS4_LEASE_NOT_STARTED		3
61 
62 /* flag to tell the renew thread it should exit */
63 #define	NFS4_THREAD_EXIT	1
64 
65 /* Default number of seconds to wait on GRACE and DELAY errors */
66 #define	NFS4ERR_DELAY_TIME	10
67 
68 /* Number of hash buckets for open owners for each nfs4_server */
69 #define	NFS4_NUM_OO_BUCKETS	53
70 
71 /* Number of freed open owners (per mntinfo4_t) to keep around */
72 #define	NFS4_NUM_FREED_OPEN_OWNERS	8
73 
74 /* Number of seconds to wait before retrying a SETCLIENTID(_CONFIRM) op */
75 #define	NFS4_RETRY_SCLID_DELAY	10
76 
77 /* Number of times we should retry a SETCLIENTID(_CONFIRM) op */
78 #define	NFS4_NUM_SCLID_RETRIES	3
79 
80 /* Number of times we should retry on open after getting NFS4ERR_BAD_SEQID */
81 #define	NFS4_NUM_RETRY_BAD_SEQID	3
82 
83 /*
84  * Is the attribute cache valid?  If client holds a delegation, then attrs
85  * are by definition valid.  If not, then check to see if attrs have timed out.
86  */
87 #define	ATTRCACHE4_VALID(vp) (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE || \
88 	gethrtime() < VTOR4(vp)->r_time_attr_inval)
89 
90 /*
91  * Flags to indicate whether to purge the DNLC for non-directory vnodes
92  * in a call to nfs_purge_caches.
93  */
94 #define	NFS4_NOPURGE_DNLC	0
95 #define	NFS4_PURGE_DNLC		1
96 
97 /*
98  * Is cache valid?
99  * Swap is always valid, if no attributes (attrtime == 0) or
100  * if mtime matches cached mtime it is valid
101  * NOTE: mtime is now a timestruc_t.
102  * Caller should be holding the rnode r_statelock mutex.
103  */
104 #define	CACHE4_VALID(rp, mtime, fsize)				\
105 	((RTOV4(rp)->v_flag & VISSWAP) == VISSWAP ||		\
106 	(((mtime).tv_sec == (rp)->r_attr.va_mtime.tv_sec &&	\
107 	(mtime).tv_nsec == (rp)->r_attr.va_mtime.tv_nsec) &&	\
108 	((fsize) == (rp)->r_attr.va_size)))
109 
110 /*
111  * Macro to detect forced unmount or a zone shutdown.
112  */
113 #define	FS_OR_ZONE_GONE4(vfsp) \
114 	(((vfsp)->vfs_flag & VFS_UNMOUNTED) || \
115 	zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN)
116 
117 /*
118  * Macro to help determine whether a request failed because the underlying
119  * filesystem has been forcibly unmounted or because of zone shutdown.
120  */
121 #define	NFS4_FRC_UNMT_ERR(err, vfsp) \
122 	((err) == EIO && FS_OR_ZONE_GONE4((vfsp)))
123 
124 /*
125  * Due to the way the address space callbacks are used to execute a delmap,
126  * we must keep track of how many times the same thread has called
127  * VOP_DELMAP()->nfs4_delmap().  This is done by having a list of
128  * nfs4_delmapcall_t's associated with each rnode4_t.  This list is protected
129  * by the rnode4_t's r_statelock.  The individual elements do not need to be
130  * protected as they will only ever be created, modified and destroyed by
131  * one thread (the call_id).
132  * See nfs4_delmap() for further explanation.
133  */
134 typedef struct nfs4_delmapcall {
135 	kthread_t	*call_id;
136 	int		error;	/* error from delmap */
137 	list_node_t	call_node;
138 } nfs4_delmapcall_t;
139 
140 /*
141  * delmap address space callback args
142  */
143 typedef struct nfs4_delmap_args {
144 	vnode_t			*vp;
145 	offset_t		off;
146 	caddr_t			addr;
147 	size_t			len;
148 	uint_t			prot;
149 	uint_t			maxprot;
150 	uint_t			flags;
151 	cred_t			*cr;
152 	nfs4_delmapcall_t	*caller; /* to retrieve errors from the cb */
153 } nfs4_delmap_args_t;
154 
155 /*
156  * client side statistics
157  */
158 /*
159  * Per-zone counters
160  */
161 struct clstat4 {
162 	kstat_named_t	calls;			/* client requests */
163 	kstat_named_t	badcalls;		/* rpc failures */
164 	kstat_named_t	clgets;			/* client handle gets */
165 	kstat_named_t	cltoomany;		/* client handle cache misses */
166 #ifdef DEBUG
167 	kstat_named_t	clalloc;		/* number of client handles */
168 	kstat_named_t	noresponse;		/* server not responding cnt */
169 	kstat_named_t	failover;		/* server failover count */
170 	kstat_named_t	remap;			/* server remap count */
171 #endif
172 };
173 
174 #ifdef DEBUG
175 /*
176  * The following are statistics that describe the behavior of the system as a
177  * whole and don't correspond to any particular zone.
178  */
179 struct clstat4_debug {
180 	kstat_named_t	nrnode;			/* number of allocated rnodes */
181 	kstat_named_t	access;			/* size of access cache */
182 	kstat_named_t	dirent;			/* size of readdir cache */
183 	kstat_named_t	dirents;		/* size of readdir buf cache */
184 	kstat_named_t	reclaim;		/* number of reclaims */
185 	kstat_named_t	clreclaim;		/* number of cl reclaims */
186 	kstat_named_t	f_reclaim;		/* number of free reclaims */
187 	kstat_named_t	a_reclaim;		/* number of active reclaims */
188 	kstat_named_t	r_reclaim;		/* number of rnode reclaims */
189 	kstat_named_t	rpath;			/* bytes used to store rpaths */
190 };
191 extern struct clstat4_debug clstat4_debug;
192 
193 #endif
194 
195 /*
196  * The NFS specific async_reqs structure.
197  */
198 
199 enum iotype4 {
200 	NFS4_READ_AHEAD,
201 	NFS4_PUTAPAGE,
202 	NFS4_PAGEIO,
203 	NFS4_READDIR,
204 	NFS4_INACTIVE,
205 	NFS4_COMMIT
206 };
207 #define	NFS4_ASYNC_TYPES	(NFS4_COMMIT + 1)
208 
209 struct nfs4_async_read_req {
210 	void (*readahead)();		/* pointer to readahead function */
211 	u_offset_t blkoff;		/* offset in file */
212 	struct seg *seg;		/* segment to do i/o to */
213 	caddr_t addr;			/* address to do i/o to */
214 };
215 
216 struct nfs4_pageio_req {
217 	int (*pageio)();		/* pointer to pageio function */
218 	page_t *pp;			/* page list */
219 	u_offset_t io_off;		/* offset in file */
220 	uint_t io_len;			/* size of request */
221 	int flags;
222 };
223 
224 struct nfs4_readdir_req {
225 	int (*readdir)();		/* pointer to readdir function */
226 	struct rddir4_cache *rdc;	/* pointer to cache entry to fill */
227 };
228 
229 struct nfs4_commit_req {
230 	void (*commit)();		/* pointer to commit function */
231 	page_t *plist;			/* page list */
232 	offset4 offset;			/* starting offset */
233 	count4 count;			/* size of range to be commited */
234 };
235 
236 struct nfs4_async_reqs {
237 	struct nfs4_async_reqs *a_next;	/* pointer to next arg struct */
238 #ifdef DEBUG
239 	kthread_t *a_queuer;		/* thread id of queueing thread */
240 #endif
241 	struct vnode *a_vp;		/* vnode pointer */
242 	struct cred *a_cred;		/* cred pointer */
243 	enum iotype4 a_io;		/* i/o type */
244 	union {
245 		struct nfs4_async_read_req a_read_args;
246 		struct nfs4_pageio_req a_pageio_args;
247 		struct nfs4_readdir_req a_readdir_args;
248 		struct nfs4_commit_req a_commit_args;
249 	} a_args;
250 };
251 
252 #define	a_nfs4_readahead a_args.a_read_args.readahead
253 #define	a_nfs4_blkoff a_args.a_read_args.blkoff
254 #define	a_nfs4_seg a_args.a_read_args.seg
255 #define	a_nfs4_addr a_args.a_read_args.addr
256 
257 #define	a_nfs4_putapage a_args.a_pageio_args.pageio
258 #define	a_nfs4_pageio a_args.a_pageio_args.pageio
259 #define	a_nfs4_pp a_args.a_pageio_args.pp
260 #define	a_nfs4_off a_args.a_pageio_args.io_off
261 #define	a_nfs4_len a_args.a_pageio_args.io_len
262 #define	a_nfs4_flags a_args.a_pageio_args.flags
263 
264 #define	a_nfs4_readdir a_args.a_readdir_args.readdir
265 #define	a_nfs4_rdc a_args.a_readdir_args.rdc
266 
267 #define	a_nfs4_commit a_args.a_commit_args.commit
268 #define	a_nfs4_plist a_args.a_commit_args.plist
269 #define	a_nfs4_offset a_args.a_commit_args.offset
270 #define	a_nfs4_count a_args.a_commit_args.count
271 
272 /*
273  * Security information
274  */
275 typedef struct sv_secinfo {
276 	uint_t		count;	/* how many sdata there are */
277 	uint_t		index;	/* which sdata[index] */
278 	struct sec_data	*sdata;
279 } sv_secinfo_t;
280 
281 /*
282  * Hash bucket for the mi's open owner list (mi_oo_list).
283  */
284 typedef struct nfs4_oo_hash_bucket {
285 	list_t			b_oo_hash_list;
286 	kmutex_t		b_lock;
287 } nfs4_oo_hash_bucket_t;
288 
289 /*
290  * Global array of ctags.
291  */
292 extern ctag_t nfs4_ctags[];
293 
294 typedef enum nfs4_tag_type {
295 	TAG_NONE,
296 	TAG_ACCESS,
297 	TAG_CLOSE,
298 	TAG_CLOSE_LOST,
299 	TAG_CLOSE_UNDO,
300 	TAG_COMMIT,
301 	TAG_DELEGRETURN,
302 	TAG_FSINFO,
303 	TAG_GET_SYMLINK,
304 	TAG_GETATTR,
305 	TAG_INACTIVE,
306 	TAG_LINK,
307 	TAG_LOCK,
308 	TAG_LOCK_RECLAIM,
309 	TAG_LOCK_RESEND,
310 	TAG_LOCK_REINSTATE,
311 	TAG_LOCK_UNKNOWN,
312 	TAG_LOCKT,
313 	TAG_LOCKU,
314 	TAG_LOCKU_RESEND,
315 	TAG_LOCKU_REINSTATE,
316 	TAG_LOOKUP,
317 	TAG_LOOKUP_PARENT,
318 	TAG_LOOKUP_VALID,
319 	TAG_LOOKUP_VPARENT,
320 	TAG_MKDIR,
321 	TAG_MKNOD,
322 	TAG_MOUNT,
323 	TAG_OPEN,
324 	TAG_OPEN_CONFIRM,
325 	TAG_OPEN_CONFIRM_LOST,
326 	TAG_OPEN_DG,
327 	TAG_OPEN_DG_LOST,
328 	TAG_OPEN_LOST,
329 	TAG_OPENATTR,
330 	TAG_PATHCONF,
331 	TAG_PUTROOTFH,
332 	TAG_READ,
333 	TAG_READAHEAD,
334 	TAG_READDIR,
335 	TAG_READLINK,
336 	TAG_RELOCK,
337 	TAG_REMAP_LOOKUP,
338 	TAG_REMAP_LOOKUP_AD,
339 	TAG_REMAP_LOOKUP_NA,
340 	TAG_REMAP_MOUNT,
341 	TAG_RMDIR,
342 	TAG_REMOVE,
343 	TAG_RENAME,
344 	TAG_RENAME_VFH,
345 	TAG_RENEW,
346 	TAG_REOPEN,
347 	TAG_REOPEN_LOST,
348 	TAG_SECINFO,
349 	TAG_SETATTR,
350 	TAG_SETCLIENTID,
351 	TAG_SETCLIENTID_CF,
352 	TAG_SYMLINK,
353 	TAG_WRITE
354 } nfs4_tag_type_t;
355 
356 #define	NFS4_TAG_INITIALIZER	{				\
357 		{TAG_NONE,		"",			\
358 			{0x20202020, 0x20202020, 0x20202020}},	\
359 		{TAG_ACCESS,		"access",		\
360 			{0x61636365, 0x73732020, 0x20202020}},	\
361 		{TAG_CLOSE,		"close",		\
362 			{0x636c6f73, 0x65202020, 0x20202020}},	\
363 		{TAG_CLOSE_LOST,	"lost close",		\
364 			{0x6c6f7374, 0x20636c6f, 0x73652020}},	\
365 		{TAG_CLOSE_UNDO,	"undo close",		\
366 			{0x756e646f, 0x20636c6f, 0x73652020}},	\
367 		{TAG_COMMIT,		"commit",		\
368 			{0x636f6d6d, 0x69742020, 0x20202020}},	\
369 		{TAG_DELEGRETURN,	"delegreturn",		\
370 			{0x64656c65, 0x67726574, 0x75726e20}},	\
371 		{TAG_FSINFO,		"fsinfo",		\
372 			{0x6673696e, 0x666f2020, 0x20202020}},	\
373 		{TAG_GET_SYMLINK,	"get symlink text",	\
374 			{0x67657420, 0x736c6e6b, 0x20747874}},	\
375 		{TAG_GETATTR,		"getattr",		\
376 			{0x67657461, 0x74747220, 0x20202020}},	\
377 		{TAG_INACTIVE,		"inactive",		\
378 			{0x696e6163, 0x74697665, 0x20202020}},	\
379 		{TAG_LINK,		"link",			\
380 			{0x6c696e6b, 0x20202020, 0x20202020}},	\
381 		{TAG_LOCK,		"lock",			\
382 			{0x6c6f636b, 0x20202020, 0x20202020}},	\
383 		{TAG_LOCK_RECLAIM,	"reclaim lock",		\
384 			{0x7265636c, 0x61696d20, 0x6c6f636b}},	\
385 		{TAG_LOCK_RESEND,	"resend lock",		\
386 			{0x72657365, 0x6e64206c, 0x6f636b20}},	\
387 		{TAG_LOCK_REINSTATE,	"reinstate lock",	\
388 			{0x7265696e, 0x7374206c, 0x6f636b20}},	\
389 		{TAG_LOCK_UNKNOWN,	"unknown lock",		\
390 			{0x756e6b6e, 0x6f776e20, 0x6c6f636b}},	\
391 		{TAG_LOCKT,		"lock test",		\
392 			{0x6c6f636b, 0x5f746573, 0x74202020}},	\
393 		{TAG_LOCKU,		"unlock",		\
394 			{0x756e6c6f, 0x636b2020, 0x20202020}},	\
395 		{TAG_LOCKU_RESEND,	"resend locku",		\
396 			{0x72657365, 0x6e64206c, 0x6f636b75}},	\
397 		{TAG_LOCKU_REINSTATE,	"reinstate unlock",	\
398 			{0x7265696e, 0x73742075, 0x6e6c636b}},	\
399 		{TAG_LOOKUP,		"lookup",		\
400 			{0x6c6f6f6b, 0x75702020, 0x20202020}},	\
401 		{TAG_LOOKUP_PARENT,	"lookup parent",	\
402 			{0x6c6f6f6b, 0x75702070, 0x6172656e}},	\
403 		{TAG_LOOKUP_VALID,	"lookup valid",		\
404 			{0x6c6f6f6b, 0x75702076, 0x616c6964}},	\
405 		{TAG_LOOKUP_VPARENT,	"lookup valid parent",	\
406 			{0x6c6f6f6b, 0x766c6420, 0x7061726e}},	\
407 		{TAG_MKDIR,		"mkdir",		\
408 			{0x6d6b6469, 0x72202020, 0x20202020}},	\
409 		{TAG_MKNOD,		"mknod",		\
410 			{0x6d6b6e6f, 0x64202020, 0x20202020}},	\
411 		{TAG_MOUNT,		"mount",		\
412 			{0x6d6f756e, 0x74202020, 0x20202020}},	\
413 		{TAG_OPEN,		"open",			\
414 			{0x6f70656e, 0x20202020, 0x20202020}},	\
415 		{TAG_OPEN_CONFIRM,	"open confirm",		\
416 			{0x6f70656e, 0x5f636f6e, 0x6669726d}},	\
417 		{TAG_OPEN_CONFIRM_LOST,	"lost open confirm",	\
418 			{0x6c6f7374, 0x206f7065, 0x6e5f636f}},	\
419 		{TAG_OPEN_DG,		"open downgrade",	\
420 			{0x6f70656e, 0x20646772, 0x61646520}},	\
421 		{TAG_OPEN_DG_LOST,	"lost open downgrade",	\
422 			{0x6c737420, 0x6f70656e, 0x20646772}},	\
423 		{TAG_OPEN_LOST,		"lost open",		\
424 			{0x6c6f7374, 0x206f7065, 0x6e202020}},	\
425 		{TAG_OPENATTR,		"openattr",		\
426 			{0x6f70656e, 0x61747472, 0x20202020}},	\
427 		{TAG_PATHCONF,		"pathhconf",		\
428 			{0x70617468, 0x636f6e66, 0x20202020}},	\
429 		{TAG_PUTROOTFH,		"putrootfh",		\
430 			{0x70757472, 0x6f6f7466, 0x68202020}},	\
431 		{TAG_READ,		"read",			\
432 			{0x72656164, 0x20202020, 0x20202020}},	\
433 		{TAG_READAHEAD,		"readahead",		\
434 			{0x72656164, 0x61686561, 0x64202020}},	\
435 		{TAG_READDIR,		"readdir",		\
436 			{0x72656164, 0x64697220, 0x20202020}},	\
437 		{TAG_READLINK,		"readlink",		\
438 			{0x72656164, 0x6c696e6b, 0x20202020}},	\
439 		{TAG_RELOCK,		"relock",		\
440 			{0x72656c6f, 0x636b2020, 0x20202020}},	\
441 		{TAG_REMAP_LOOKUP,	"remap lookup",		\
442 			{0x72656d61, 0x70206c6f, 0x6f6b7570}},	\
443 		{TAG_REMAP_LOOKUP_AD,	"remap lookup attr dir",	\
444 			{0x72656d70, 0x206c6b75, 0x70206164}},	\
445 		{TAG_REMAP_LOOKUP_NA,	"remap lookup named attrs",	\
446 			{0x72656d70, 0x206c6b75, 0x70206e61}},	\
447 		{TAG_REMAP_MOUNT,	"remap mount",		\
448 			{0x72656d61, 0x70206d6f, 0x756e7420}},	\
449 		{TAG_RMDIR,		"rmdir",		\
450 			{0x726d6469, 0x72202020, 0x20202020}},	\
451 		{TAG_REMOVE,		"remove",		\
452 			{0x72656d6f, 0x76652020, 0x20202020}},	\
453 		{TAG_RENAME,		"rename",		\
454 			{0x72656e61, 0x6d652020, 0x20202020}},	\
455 		{TAG_RENAME_VFH,	"rename volatile fh",	\
456 			{0x72656e61, 0x6d652028, 0x76666829}},	\
457 		{TAG_RENEW,		"renew",		\
458 			{0x72656e65, 0x77202020, 0x20202020}},	\
459 		{TAG_REOPEN,		"reopen",		\
460 			{0x72656f70, 0x656e2020, 0x20202020}},	\
461 		{TAG_REOPEN_LOST,	"lost reopen",		\
462 			{0x6c6f7374, 0x2072656f, 0x70656e20}},	\
463 		{TAG_SECINFO,		"secinfo",		\
464 			{0x73656369, 0x6e666f20, 0x20202020}},	\
465 		{TAG_SETATTR,		"setattr",		\
466 			{0x73657461, 0x74747220, 0x20202020}},	\
467 		{TAG_SETCLIENTID,	"setclientid",		\
468 			{0x73657463, 0x6c69656e, 0x74696420}},	\
469 		{TAG_SETCLIENTID_CF,	"setclientid_confirm",	\
470 			{0x73636c6e, 0x7469645f, 0x636f6e66}},	\
471 		{TAG_SYMLINK,		"symlink",		\
472 			{0x73796d6c, 0x696e6b20, 0x20202020}},	\
473 		{TAG_WRITE,		"write",		\
474 			{0x77726974, 0x65202020, 0x20202020}}	\
475 	}
476 
477 /*
478  * These flags are for differentiating the search criterian for
479  * find_open_owner().  The comparison is done with the open_owners's
480  * 'oo_just_created' flag.
481  */
482 #define	NFS4_PERM_CREATED	0x0
483 #define	NFS4_JUST_CREATED	0x1
484 
485 /*
486  * Hashed by the cr_uid and cr_ruid of credential 'oo_cred'. 'oo_cred_otw'
487  * is stored upon a successful OPEN.  This is needed when the user's effective
488  * and real uid's don't match.  The 'oo_cred_otw' overrides the credential
489  * passed down by VFS for async read/write, commit, lock, and close operations.
490  *
491  * The oo_ref_count keeps track the number of active references on this
492  * data structure + number of nfs4_open_streams point to this structure.
493  *
494  * 'oo_valid' tells whether this stuct is about to be freed or not.
495  *
496  * 'oo_just_created' tells us whether this struct has just been created but
497  * not been fully finalized (that is created upon an OPEN request and
498  * finalized upon the OPEN success).
499  *
500  * The 'oo_seqid_inuse' is for the open seqid synchronization.  If a thread
501  * is currently using the open owner and it's open_seqid, then it sets the
502  * oo_seqid_inuse to true if it currently is not set.  If it is set then it
503  * does a cv_wait on the oo_cv_seqid_sync condition variable.  When the thread
504  * is done it unsets the oo_seqid_inuse and does a cv_signal to wake a process
505  * waiting on the condition variable.
506  *
507  * 'oo_last_good_seqid' is the last valid seqid this open owner sent OTW,
508  * and 'oo_last_good_op' is the operation that issued the last valid seqid.
509  *
510  * Lock ordering:
511  *	mntinfo4_t::mi_lock > oo_lock (for searching mi_oo_list)
512  *
513  *	oo_seqid_inuse > mntinfo4_t::mi_lock
514  *	oo_seqid_inuse > rnode4_t::r_statelock
515  *	oo_seqid_inuse > rnode4_t::r_statev4_lock
516  *	oo_seqid_inuse > nfs4_open_stream_t::os_sync_lock
517  *
518  * The 'oo_seqid_inuse'/'oo_cv_seqid_sync' protects:
519  *	oo_last_good_op
520  *	oo_last_good_seqid
521  *	oo_name
522  *	oo_seqid
523  *
524  * The 'oo_lock' protects:
525  *	oo_cred
526  *	oo_cred_otw
527  *	oo_foo_node
528  *	oo_hash_node
529  *	oo_just_created
530  *	oo_ref_count
531  *	oo_valid
532  */
533 
534 typedef struct nfs4_open_owner {
535 	cred_t			*oo_cred;
536 	int			oo_ref_count;
537 	int			oo_valid;
538 	int			oo_just_created;
539 	seqid4			oo_seqid;
540 	seqid4			oo_last_good_seqid;
541 	nfs4_tag_type_t		oo_last_good_op;
542 	unsigned		oo_seqid_inuse:1;
543 	cred_t			*oo_cred_otw;
544 	kcondvar_t		oo_cv_seqid_sync;
545 	/*
546 	 * Fix this to always be 8 bytes
547 	 */
548 	uint64_t		oo_name;
549 	list_node_t		oo_hash_node;
550 	list_node_t		oo_foo_node;
551 	kmutex_t		oo_lock;
552 } nfs4_open_owner_t;
553 
554 /*
555  * Static server information.
556  * These fields are read-only once they are initialized:
557  *	sv_addr
558  *	sv_dhsec
559  *	sv_hostname
560  *	sv_hostnamelen
561  *	sv_knconf
562  *	sv_next
563  *	sv_origknconf
564  *
565  * These fields are protected by sv_lock:
566  *	sv_currsec
567  *	sv_fhandle
568  *	sv_flags
569  *	sv_fsid
570  *	sv_path
571  *	sv_pathlen
572  *	sv_pfhandle
573  *	sv_save_secinfo
574  *	sv_savesec
575  *	sv_secdata
576  *	sv_secinfo
577  *	sv_supp_attrs
578  *
579  * Lock ordering:
580  * nfs_rtable4_lock > sv_lock
581  * rnode4_t::r_statelock > sv_lock
582  */
583 typedef struct servinfo4 {
584 	struct knetconfig *sv_knconf;   /* bound TLI fd */
585 	struct knetconfig *sv_origknconf;	/* For RDMA save orig knconf */
586 	struct netbuf	   sv_addr;	/* server's address */
587 	nfs4_fhandle_t	   sv_fhandle;	/* this server's filehandle */
588 	nfs4_fhandle_t	   sv_pfhandle; /* parent dir filehandle */
589 	int		   sv_pathlen;	/* Length of server path */
590 	char		  *sv_path;	/* Path name on server */
591 	uint32_t	   sv_flags;	/* flags for this server */
592 	sec_data_t	  *sv_secdata;	/* client initiated security data */
593 	sv_secinfo_t	  *sv_secinfo;	/* server security information */
594 	sec_data_t	  *sv_currsec;	/* security data currently used; */
595 					/* points to one of the sec_data */
596 					/* entries in sv_secinfo */
597 	sv_secinfo_t	  *sv_save_secinfo; /* saved secinfo */
598 	sec_data_t	  *sv_savesec;	/* saved security data */
599 	sec_data_t	  *sv_dhsec;    /* AUTH_DH data from the user land */
600 	char		  *sv_hostname;	/* server's hostname */
601 	int		   sv_hostnamelen;  /* server's hostname length */
602 	fattr4_fsid		sv_fsid;    /* fsid of shared obj	*/
603 	fattr4_supported_attrs	sv_supp_attrs;
604 	struct servinfo4  *sv_next;	/* next in list */
605 	nfs_rwlock_t	   sv_lock;
606 } servinfo4_t;
607 
608 /* sv_flags fields */
609 #define	SV4_TRYSECINFO		0x001	/* try secinfo data from the server */
610 #define	SV4_TRYSECDEFAULT	0x002	/* try a default flavor */
611 #define	SV4_NOTINUSE		0x004	/* servinfo4_t had fatal errors */
612 #define	SV4_ROOT_STALE		0x008	/* root vnode got ESTALE */
613 
614 /*
615  * Lock call types.  See nfs4frlock().
616  */
617 typedef enum nfs4_lock_call_type {
618 	NFS4_LCK_CTYPE_NORM,
619 	NFS4_LCK_CTYPE_RECLAIM,
620 	NFS4_LCK_CTYPE_RESEND,
621 	NFS4_LCK_CTYPE_REINSTATE
622 } nfs4_lock_call_type_t;
623 
624 /*
625  * This structure holds the information for a lost open/close/open downgrade/
626  * lock/locku request.  It is also used for requests that are queued up so
627  * that the recovery thread can release server state after a forced
628  * unmount.
629  * "lr_op" is 0 if the struct is uninitialized.  Otherwise, it is set to
630  * the proper OP_* nfs_opnum4 number.  The other fields contain information
631  * to reconstruct the call.
632  *
633  * lr_dvp is used for OPENs with CREATE, so that we can do a PUTFH of the
634  * parent directroy without relying on vtodv (since we may not have a vp
635  * for the file we wish to create).
636  *
637  * lr_putfirst means that the request should go to the front of the resend
638  * queue, rather than the end.
639  */
640 typedef struct nfs4_lost_rqst {
641 	list_node_t			lr_node;
642 	nfs_opnum4			lr_op;
643 	vnode_t				*lr_vp;
644 	vnode_t				*lr_dvp;
645 	nfs4_open_owner_t		*lr_oop;
646 	struct nfs4_open_stream		*lr_osp;
647 	struct nfs4_lock_owner		*lr_lop;
648 	cred_t				*lr_cr;
649 	flock64_t			*lr_flk;
650 	bool_t				lr_putfirst;
651 	union {
652 		struct {
653 			nfs4_lock_call_type_t lru_ctype;
654 			nfs_lock_type4	lru_locktype;
655 		} lru_lockargs;		/* LOCK, LOCKU */
656 		struct {
657 			uint32_t		lru_oaccess;
658 			uint32_t		lru_odeny;
659 			enum open_claim_type4	lru_oclaim;
660 			stateid4		lru_ostateid; /* reopen only */
661 			component4		lru_ofile;
662 		} lru_open_args;
663 		struct {
664 			uint32_t	lru_dg_access;
665 			uint32_t	lru_dg_deny;
666 		} lru_open_dg_args;
667 	} nfs4_lr_u;
668 } nfs4_lost_rqst_t;
669 
670 #define	lr_oacc		nfs4_lr_u.lru_open_args.lru_oaccess
671 #define	lr_odeny	nfs4_lr_u.lru_open_args.lru_odeny
672 #define	lr_oclaim	nfs4_lr_u.lru_open_args.lru_oclaim
673 #define	lr_ostateid	nfs4_lr_u.lru_open_args.lru_ostateid
674 #define	lr_ofile	nfs4_lr_u.lru_open_args.lru_ofile
675 #define	lr_dg_acc	nfs4_lr_u.lru_open_dg_args.lru_dg_access
676 #define	lr_dg_deny	nfs4_lr_u.lru_open_dg_args.lru_dg_deny
677 #define	lr_ctype	nfs4_lr_u.lru_lockargs.lru_ctype
678 #define	lr_locktype	nfs4_lr_u.lru_lockargs.lru_locktype
679 
680 /*
681  * Recovery actions.  Some actions can imply further recovery using a
682  * different recovery action (e.g., recovering the clientid leads to
683  * recovering open files and locks).
684  */
685 
686 typedef enum {
687 	NR_UNUSED,
688 	NR_CLIENTID,
689 	NR_OPENFILES,
690 	NR_FHEXPIRED,
691 	NR_FAILOVER,
692 	NR_WRONGSEC,
693 	NR_EXPIRED,
694 	NR_BAD_STATEID,
695 	NR_BADHANDLE,
696 	NR_BAD_SEQID,
697 	NR_OLDSTATEID,
698 	NR_GRACE,
699 	NR_DELAY,
700 	NR_LOST_LOCK,
701 	NR_LOST_STATE_RQST,
702 	NR_STALE
703 } nfs4_recov_t;
704 
705 /*
706  * Administrative and debug message framework.
707  */
708 
709 #define	NFS4_MSG_MAX	100
710 extern int nfs4_msg_max;
711 
712 typedef enum {
713 	RE_BAD_SEQID,
714 	RE_BADHANDLE,
715 	RE_CLIENTID,
716 	RE_DEAD_FILE,
717 	RE_END,
718 	RE_FAIL_RELOCK,
719 	RE_FAIL_REMAP_LEN,
720 	RE_FAIL_REMAP_OP,
721 	RE_FAILOVER,
722 	RE_FILE_DIFF,
723 	RE_LOST_STATE,
724 	RE_OPENS_CHANGED,
725 	RE_SIGLOST,
726 	RE_SIGLOST_NO_DUMP,
727 	RE_START,
728 	RE_UNEXPECTED_ACTION,
729 	RE_UNEXPECTED_ERRNO,
730 	RE_UNEXPECTED_STATUS,
731 	RE_WRONGSEC,
732 	RE_LOST_STATE_BAD_OP
733 } nfs4_event_type_t;
734 
735 typedef enum {
736 	RFS_NO_INSPECT,
737 	RFS_INSPECT
738 } nfs4_fact_status_t;
739 
740 typedef enum {
741 	RF_BADOWNER,
742 	RF_ERR,
743 	RF_RENEW_EXPIRED,
744 	RF_SRV_NOT_RESPOND,
745 	RF_SRV_OK,
746 	RF_SRVS_NOT_RESPOND,
747 	RF_SRVS_OK,
748 	RF_DELMAP_CB_ERR,
749 	RF_SENDQ_FULL
750 } nfs4_fact_type_t;
751 
752 typedef enum {
753 	NFS4_MS_DUMP,
754 	NFS4_MS_NO_DUMP
755 } nfs4_msg_status_t;
756 
757 typedef struct nfs4_rfact {
758 	nfs4_fact_type_t	rf_type;
759 	nfs4_fact_status_t	rf_status;
760 	bool_t			rf_reboot;
761 	nfs4_recov_t		rf_action;
762 	nfs_opnum4		rf_op;
763 	nfsstat4		rf_stat4;
764 	timespec_t		rf_time;
765 	int			rf_error;
766 	struct rnode4		*rf_rp1;
767 	char			*rf_char1;
768 } nfs4_rfact_t;
769 
770 typedef struct nfs4_revent {
771 	nfs4_event_type_t	re_type;
772 	nfsstat4		re_stat4;
773 	uint_t			re_uint;
774 	pid_t			re_pid;
775 	struct mntinfo4		*re_mi;
776 	struct rnode4		*re_rp1;
777 	struct rnode4		*re_rp2;
778 	char			*re_char1;
779 	char			*re_char2;
780 	nfs4_tag_type_t		re_tag1;
781 	nfs4_tag_type_t		re_tag2;
782 	seqid4			re_seqid1;
783 	seqid4			re_seqid2;
784 } nfs4_revent_t;
785 
786 typedef enum {
787 	RM_EVENT,
788 	RM_FACT
789 } nfs4_msg_type_t;
790 
791 typedef struct nfs4_debug_msg {
792 	timespec_t		msg_time;
793 	nfs4_msg_type_t		msg_type;
794 	char			*msg_srv;
795 	char			*msg_mntpt;
796 	union {
797 		nfs4_rfact_t	msg_fact;
798 		nfs4_revent_t	msg_event;
799 	} rmsg_u;
800 	nfs4_msg_status_t	msg_status;
801 	list_node_t		msg_node;
802 } nfs4_debug_msg_t;
803 
804 /*
805  * NFS private data per mounted file system
806  *	The mi_lock mutex protects the following fields:
807  *		mi_flags
808  *		mi_in_recovery
809  *		mi_recovflags
810  *		mi_recovthread
811  *		mi_error
812  *		mi_printed
813  *		mi_down
814  *		mi_stsize
815  *		mi_curread
816  *		mi_curwrite
817  *		mi_timers
818  *		mi_curr_serv
819  *		mi_klmconfig
820  *		mi_oo_list
821  *		mi_foo_list
822  *		mi_foo_num
823  *		mi_foo_max
824  *		mi_lost_state
825  *		mi_bseqid_list
826  *		mi_ephemeral
827  *		mi_ephemeral_tree
828  *
829  *	Normally the netconfig information for the mount comes from
830  *	mi_curr_serv and mi_klmconfig is NULL.  If NLM calls need to use a
831  *	different transport, mi_klmconfig contains the necessary netconfig
832  *	information.
833  *
834  *	The mi_async_lock mutex protects the following fields:
835  *		mi_async_reqs
836  *		mi_async_req_count
837  *		mi_async_tail
838  *		mi_async_curr
839  *		mi_async_clusters
840  *		mi_async_init_clusters
841  *		mi_threads
842  *		mi_inactive_thread
843  *		mi_manager_thread
844  *
845  *	The nfs4_server_t::s_lock protects the following fields:
846  *		mi_clientid
847  *		mi_clientid_next
848  *		mi_clientid_prev
849  *		mi_open_files
850  *
851  *	The mntinfo4_t::mi_recovlock protects the following fields:
852  *		mi_srvsettime
853  *		mi_srvset_cnt
854  *		mi_srv
855  *
856  * Changing mi_srv from one nfs4_server_t to a different one requires
857  * holding the mi_recovlock as RW_WRITER.
858  * Exception: setting mi_srv the first time in mount/mountroot is done
859  * holding the mi_recovlock as RW_READER.
860  *
861  *	Locking order:
862  *	  mi4_globals::mig_lock > mi_async_lock
863  *	  mi_async_lock > nfs4_server_t::s_lock > mi_lock
864  *	  mi_recovlock > mi_rename_lock > nfs_rtable4_lock
865  *	  nfs4_server_t::s_recovlock > mi_recovlock
866  *	  rnode4_t::r_rwlock > mi_rename_lock
867  *	  nfs_rtable4_lock > mi_lock
868  *	  nfs4_server_t::s_lock > mi_msg_list_lock
869  *	  mi_recovlock > nfs4_server_t::s_lock
870  *	  mi_recovlock > nfs4_server_lst_lock
871  *
872  * The 'mi_oo_list' represents the hash buckets that contain the
873  * nfs4_open_owenrs for this particular mntinfo4.
874  *
875  * The 'mi_foo_list' represents the freed nfs4_open_owners for this mntinfo4.
876  * 'mi_foo_num' is the current number of freed open owners on the list,
877  * 'mi_foo_max' is the maximum number of freed open owners that are allowable
878  * on the list.
879  *
880  * mi_rootfh and mi_srvparentfh are read-only once created, but that just
881  * refers to the pointer.  The contents must be updated to keep in sync
882  * with mi_curr_serv.
883  *
884  * The mi_msg_list_lock protects against adding/deleting entries to the
885  * mi_msg_list, and also the updating/retrieving of mi_lease_period;
886  *
887  * 'mi_zone' is initialized at structure creation time, and never
888  * changes; it may be read without a lock.
889  *
890  * mi_zone_node is linkage into the mi4_globals.mig_list, and is
891  * protected by mi4_globals.mig_list_lock.
892  *
893  * If MI4_EPHEMERAL is set in mi_flags, then mi_ephemeral points to an
894  * ephemeral structure for this ephemeral mount point. It can not be
895  * NULL. Also, mi_ephemeral_tree points to the root of the ephemeral
896  * tree.
897  *
898  * If MI4_EPHEMERAL is not set in mi_flags, then mi_ephemeral has
899  * to be NULL. If mi_ephemeral_tree is non-NULL, then this node
900  * is the enclosing mntinfo4 for the ephemeral tree.
901  */
902 struct zone;
903 struct nfs4_ephemeral;
904 struct nfs4_ephemeral_tree;
905 struct nfs4_server;
906 typedef struct mntinfo4 {
907 	kmutex_t	mi_lock;	/* protects mntinfo4 fields */
908 	struct servinfo4 *mi_servers;   /* server list */
909 	struct servinfo4 *mi_curr_serv; /* current server */
910 	struct nfs4_sharedfh *mi_rootfh; /* root filehandle */
911 	struct nfs4_sharedfh *mi_srvparentfh; /* root's parent on server */
912 	kcondvar_t	mi_failover_cv;	/* failover synchronization */
913 	struct vfs	*mi_vfsp;	/* back pointer to vfs */
914 	enum vtype	mi_type;	/* file type of the root vnode */
915 	uint_t		mi_flags;	/* see below */
916 	uint_t		mi_recovflags;	/* if recovery active; see below */
917 	kthread_t	*mi_recovthread; /* active recov thread or NULL */
918 	uint_t		mi_error;	/* only set/valid when MI4_RECOV_FAIL */
919 					/* is set in mi_flags */
920 	int		mi_tsize;	/* transfer size (bytes) */
921 					/* really read size */
922 	int		mi_stsize;	/* server's max transfer size (bytes) */
923 					/* really write size */
924 	int		mi_timeo;	/* inital timeout in 10th sec */
925 	int		mi_retrans;	/* times to retry request */
926 	hrtime_t	mi_acregmin;	/* min time to hold cached file attr */
927 	hrtime_t	mi_acregmax;	/* max time to hold cached file attr */
928 	hrtime_t	mi_acdirmin;	/* min time to hold cached dir attr */
929 	hrtime_t	mi_acdirmax;	/* max time to hold cached dir attr */
930 	len_t		mi_maxfilesize; /* for pathconf _PC_FILESIZEBITS */
931 	int		mi_curread;	/* current read size */
932 	int		mi_curwrite;	/* current write size */
933 	uint_t 		mi_count; 	/* ref count */
934 	/*
935 	 * async I/O management.  There may be a pool of threads to handle
936 	 * async I/O requests, etc., plus there is always one thread that
937 	 * handles over-the-wire requests for VOP_INACTIVE.  The async pool
938 	 * can also help out with VOP_INACTIVE.
939 	 */
940 	struct nfs4_async_reqs *mi_async_reqs[NFS4_ASYNC_TYPES];
941 	struct nfs4_async_reqs *mi_async_tail[NFS4_ASYNC_TYPES];
942 	struct nfs4_async_reqs **mi_async_curr;	/* current async queue */
943 	uint_t		mi_async_clusters[NFS4_ASYNC_TYPES];
944 	uint_t		mi_async_init_clusters;
945 	uint_t		mi_async_req_count; /* # outstanding work requests */
946 	kcondvar_t	mi_async_reqs_cv; /* signaled when there's work */
947 	ushort_t	mi_threads;	/* number of active async threads */
948 	ushort_t	mi_max_threads;	/* max number of async threads */
949 	kthread_t	*mi_manager_thread; /* async manager thread id */
950 	kthread_t	*mi_inactive_thread; /* inactive thread id */
951 	kcondvar_t	mi_inact_req_cv; /* notify VOP_INACTIVE thread */
952 	kcondvar_t	mi_async_work_cv; /* tell workers to work */
953 	kcondvar_t	mi_async_cv;	/* all pool threads exited */
954 	kmutex_t	mi_async_lock;
955 	/*
956 	 * Other stuff
957 	 */
958 	struct pathcnf	*mi_pathconf;	/* static pathconf kludge */
959 	rpcprog_t	mi_prog;	/* RPC program number */
960 	rpcvers_t	mi_vers;	/* RPC program version number */
961 	char		**mi_rfsnames;	/* mapping to proc names */
962 	kstat_named_t	*mi_reqs;	/* count of requests */
963 	clock_t		mi_printftime;	/* last error printf time */
964 	nfs_rwlock_t	mi_recovlock;	/* separate ops from recovery (v4) */
965 	time_t		mi_grace_wait;	/* non-zero represents time to wait */
966 	/* when we switched nfs4_server_t - only for observability purposes */
967 	time_t		mi_srvsettime;
968 	nfs_rwlock_t	mi_rename_lock;	/* atomic volfh rename  */
969 	struct nfs4_fname *mi_fname;	/* root fname */
970 	list_t		mi_lost_state;	/* resend list */
971 	list_t		mi_bseqid_list; /* bad seqid list */
972 	/*
973 	 * Client Side Failover stats
974 	 */
975 	uint_t		mi_noresponse;	/* server not responding count */
976 	uint_t		mi_failover; 	/* failover to new server count */
977 	uint_t		mi_remap;	/* remap to new server count */
978 	/*
979 	 * Kstat statistics
980 	 */
981 	struct kstat	*mi_io_kstats;
982 	struct kstat	*mi_ro_kstats;
983 	kstat_t		*mi_recov_ksp;	/* ptr to the recovery kstat */
984 
985 	/*
986 	 * Volatile fh flags (nfsv4)
987 	 */
988 	uint32_t	mi_fh_expire_type;
989 	/*
990 	 * Lease Management
991 	 */
992 	struct mntinfo4	*mi_clientid_next;
993 	struct mntinfo4	*mi_clientid_prev;
994 	clientid4	mi_clientid; /* redundant info found in nfs4_server */
995 	int		mi_open_files;	/* count of open files */
996 	int		mi_in_recovery;	/* count of recovery instances */
997 	kcondvar_t	mi_cv_in_recov; /* cv for recovery threads */
998 	/*
999 	 * Open owner stuff.
1000 	 */
1001 	struct nfs4_oo_hash_bucket	mi_oo_list[NFS4_NUM_OO_BUCKETS];
1002 	list_t				mi_foo_list;
1003 	int				mi_foo_num;
1004 	int				mi_foo_max;
1005 	/*
1006 	 * Shared filehandle pool.
1007 	 */
1008 	nfs_rwlock_t			mi_fh_lock;
1009 	avl_tree_t			mi_filehandles;
1010 
1011 	/*
1012 	 * Debug message queue.
1013 	 */
1014 	list_t			mi_msg_list;
1015 	int			mi_msg_count;
1016 	time_t			mi_lease_period;
1017 					/*
1018 					 * not guaranteed to be accurate.
1019 					 * only should be used by debug queue.
1020 					 */
1021 	kmutex_t		mi_msg_list_lock;
1022 	/*
1023 	 * Zones support.
1024 	 */
1025 	struct zone	*mi_zone; /* Zone mounted in */
1026 	list_node_t	mi_zone_node;  /* linkage into per-zone mi list */
1027 
1028 	/*
1029 	 * Links for unmounting ephemeral mounts.
1030 	 */
1031 	struct nfs4_ephemeral		*mi_ephemeral;
1032 	struct nfs4_ephemeral_tree	*mi_ephemeral_tree;
1033 
1034 	uint_t mi_srvset_cnt; /* increment when changing the nfs4_server_t */
1035 	struct nfs4_server *mi_srv; /* backpointer to nfs4_server_t */
1036 } mntinfo4_t;
1037 
1038 /*
1039  * The values for mi_flags.
1040  *
1041  *	MI4_HARD		 hard or soft mount
1042  *	MI4_PRINTED		 responding message printed
1043  *	MI4_INT			 allow INTR on hard mount
1044  * 	MI4_DOWN		 server is down
1045  *	MI4_NOAC		 don't cache attributes
1046  *	MI4_NOCTO		 no close-to-open consistency
1047  *	MI4_LLOCK		 local locking only (no lockmgr)
1048  *	MI4_GRPID		 System V group id inheritance
1049  *	MI4_SHUTDOWN		 System is rebooting or shutting down
1050  *	MI4_LINK		 server supports link
1051  *	MI4_SYMLINK		 server supports symlink
1052  *	MI4_EPHEMERAL_RECURSED	 an ephemeral mount being unmounted
1053  *				 due to a recursive call - no need
1054  *				 for additional recursion
1055  *	MI4_ACL			 server supports NFSv4 ACLs
1056  *	MI4_MIRRORMOUNT		 is a mirrormount
1057  *	MI4_NOPRINT		 don't print messages
1058  *	MI4_DIRECTIO		 do direct I/O
1059  *	MI4_RECOV_ACTIV		 filesystem has recovery a thread
1060  *	MI4_REMOVE_ON_LAST_CLOSE remove from server's list
1061  *	MI4_RECOV_FAIL		 client recovery failed
1062  *	MI4_PUBLIC		 public/url option used
1063  *	MI4_MOUNTING		 mount in progress, don't failover
1064  *	MI4_POSIX_LOCK		 if server is using POSIX locking
1065  *	MI4_LOCK_DEBUG		 cmn_err'd posix lock err msg
1066  *	MI4_DEAD		 zone has released it
1067  *	MI4_INACTIVE_IDLE	 inactive thread idle
1068  *	MI4_BADOWNER_DEBUG	 badowner error msg per mount
1069  *	MI4_ASYNC_MGR_STOP	 tell async manager to die
1070  *	MI4_TIMEDOUT		 saw a timeout during zone shutdown
1071  *	MI4_EPHEMERAL		 is an ephemeral mount
1072  */
1073 #define	MI4_HARD		 0x1
1074 #define	MI4_PRINTED		 0x2
1075 #define	MI4_INT			 0x4
1076 #define	MI4_DOWN		 0x8
1077 #define	MI4_NOAC		 0x10
1078 #define	MI4_NOCTO		 0x20
1079 #define	MI4_LLOCK		 0x80
1080 #define	MI4_GRPID		 0x100
1081 #define	MI4_SHUTDOWN		 0x200
1082 #define	MI4_LINK		 0x400
1083 #define	MI4_SYMLINK		 0x800
1084 #define	MI4_EPHEMERAL_RECURSED	 0x1000
1085 #define	MI4_ACL			 0x2000
1086 /* MI4_MIRRORMOUNT is also defined in nfsstat.c */
1087 #define	MI4_MIRRORMOUNT		 0x4000
1088 /* 0x8000 is available */
1089 /* 0x10000 is available */
1090 #define	MI4_NOPRINT		 0x20000
1091 #define	MI4_DIRECTIO		 0x40000
1092 /* 0x80000 is available */
1093 #define	MI4_RECOV_ACTIV		 0x100000
1094 #define	MI4_REMOVE_ON_LAST_CLOSE 0x200000
1095 #define	MI4_RECOV_FAIL		 0x400000
1096 #define	MI4_PUBLIC		 0x800000
1097 #define	MI4_MOUNTING		 0x1000000
1098 #define	MI4_POSIX_LOCK		 0x2000000
1099 #define	MI4_LOCK_DEBUG		 0x4000000
1100 #define	MI4_DEAD		 0x8000000
1101 #define	MI4_INACTIVE_IDLE	 0x10000000
1102 #define	MI4_BADOWNER_DEBUG	 0x20000000
1103 #define	MI4_ASYNC_MGR_STOP	 0x40000000
1104 #define	MI4_TIMEDOUT		 0x80000000
1105 
1106 /*
1107  * Note that when we add referrals, then MI4_EPHEMERAL
1108  * will be MI4_MIRRORMOUNT | MI4_REFERRAL.
1109  */
1110 #define	MI4_EPHEMERAL		MI4_MIRRORMOUNT
1111 
1112 #define	INTR4(vp)	(VTOMI4(vp)->mi_flags & MI4_INT)
1113 
1114 #define	FAILOVER_MOUNT4(mi)	(mi->mi_servers->sv_next)
1115 
1116 /*
1117  * Recovery flags.
1118  *
1119  * MI4R_NEED_CLIENTID is sort of redundant (it's the nfs4_server_t flag
1120  * that's important), but some flag is needed to indicate that recovery is
1121  * going on for the filesystem.
1122  */
1123 #define	MI4R_NEED_CLIENTID	0x1
1124 #define	MI4R_REOPEN_FILES	0x2
1125 #define	MI4R_NEED_SECINFO	0x4
1126 #define	MI4R_NEED_NEW_SERVER	0x8
1127 #define	MI4R_REMAP_FILES	0x10
1128 #define	MI4R_SRV_REBOOT		0x20	/* server has rebooted */
1129 #define	MI4R_LOST_STATE		0x40
1130 #define	MI4R_BAD_SEQID		0x80
1131 
1132 #define	MI4_HOLD(mi) {		\
1133 	mi_hold(mi);		\
1134 }
1135 
1136 #define	MI4_RELE(mi) {		\
1137 	mi_rele(mi);		\
1138 }
1139 
1140 /*
1141  * vfs pointer to mount info
1142  */
1143 #define	VFTOMI4(vfsp)	((mntinfo4_t *)((vfsp)->vfs_data))
1144 
1145 /*
1146  * vnode pointer to mount info
1147  */
1148 #define	VTOMI4(vp)	((mntinfo4_t *)(((vp)->v_vfsp)->vfs_data))
1149 
1150 /*
1151  * Lease Management
1152  *
1153  * lease_valid is initially set to NFS4_LEASE_NOT_STARTED.  This is when the
1154  * nfs4_server is first created.  lease_valid is then set to
1155  * NFS4_LEASE_UNITIALIZED when the renew thread is started.  The extra state of
1156  * NFS4_LEASE_NOT_STARTED is needed for client recovery (so we know if a thread
1157  * already exists when we do SETCLIENTID).  lease_valid is then set to
1158  * NFS4_LEASE_VALID (if it is at NFS4_LEASE_UNITIALIZED) when a state creating
1159  * operation (OPEN) is done. lease_valid stays at NFS4_LEASE_VALID as long as
1160  * the lease is renewed.  It is set to NFS4_LEASE_INVALID when the lease
1161  * expires.  Client recovery is needed to set the lease back to
1162  * NFS4_LEASE_VALID from NFS4_LEASE_INVALID.
1163  *
1164  * The s_cred is the credential used to mount the first file system for this
1165  * server.  It used as the credential for the renew thread's calls to the
1166  * server.
1167  *
1168  * The renew thread waits on the condition variable cv_thread_exit.  If the cv
1169  * is signalled, then the thread knows it must check s_thread_exit to see if
1170  * it should exit.  The cv is signaled when the last file system is unmounted
1171  * from a particular server.  s_thread_exit is set to 0 upon thread startup,
1172  * and set to NFS4_THREAD_EXIT, when the last file system is unmounted thereby
1173  * telling the thread to exit.  s_thread_exit is needed to avoid spurious
1174  * wakeups.
1175  *
1176  * state_ref_count is incremented every time a new file is opened and
1177  * decremented every time a file is closed otw.  This keeps track of whether
1178  * the nfs4_server has state associated with it or not.
1179  *
1180  * s_refcnt is the reference count for storage management of the struct
1181  * itself.
1182  *
1183  * mntinfo4_list points to the doubly linked list of mntinfo4s that share
1184  * this nfs4_server (ie: <clientid, saddr> pair) in the current zone.  This is
1185  * needed for a nfs4_server to get a mntinfo4 for use in rfs4call.
1186  *
1187  * s_recovlock is used to synchronize recovery operations.  The thread
1188  * that is recovering the client must acquire it as a writer.  If the
1189  * thread is using the clientid (including recovery operations on other
1190  * state), acquire it as a reader.
1191  *
1192  * The 's_otw_call_count' keeps track of the number of outstanding over the
1193  * wire requests for this structure.  The struct will not go away as long
1194  * as this is non-zero (or s_refcnt is non-zero).
1195  *
1196  * The 's_cv_otw_count' is used in conjuntion with the 's_otw_call_count'
1197  * variable to let the renew thread when an outstanding otw request has
1198  * finished.
1199  *
1200  * 'zoneid' and 'zone_globals' are set at creation of this structure
1201  * and are read-only after that; no lock is required to read them.
1202  *
1203  * s_lock protects: everything except cv_thread_exit and s_recovlock.
1204  *
1205  * s_program is used as the index into the nfs4_callback_globals's
1206  * nfs4prog2server table.  When a callback request comes in, we can
1207  * use that request's program number (minus NFS4_CALLBACK) as an index
1208  * into the nfs4prog2server.  That entry will hold the nfs4_server_t ptr.
1209  * We can then access that nfs4_server_t and its 's_deleg_list' (its list of
1210  * delegated rnode4_ts).
1211  *
1212  * Lock order:
1213  * nfs4_server::s_lock > mntinfo4::mi_lock
1214  * nfs_rtable4_lock > s_lock
1215  * nfs4_server_lst_lock > s_lock
1216  * s_recovlock > s_lock
1217  */
1218 struct nfs4_callback_globals;
1219 
1220 typedef struct nfs4_server {
1221 	struct nfs4_server	*forw;
1222 	struct nfs4_server	*back;
1223 	struct netbuf		saddr;
1224 	uint_t			s_flags; /* see below */
1225 	uint_t			s_refcnt;
1226 	clientid4		clientid;	/* what we get from server */
1227 	nfs_client_id4		clidtosend;	/* what we send to server */
1228 	mntinfo4_t		*mntinfo4_list;
1229 	int			lease_valid;
1230 	time_t			s_lease_time;
1231 	time_t			last_renewal_time;
1232 	timespec_t		propagation_delay;
1233 	cred_t			*s_cred;
1234 	kcondvar_t		cv_thread_exit;
1235 	int			s_thread_exit;
1236 	int			state_ref_count;
1237 	int			s_otw_call_count;
1238 	kcondvar_t		s_cv_otw_count;
1239 	kcondvar_t		s_clientid_pend;
1240 	kmutex_t		s_lock;
1241 	list_t			s_deleg_list;
1242 	rpcprog_t		s_program;
1243 	nfs_rwlock_t		s_recovlock;
1244 	kcondvar_t		wait_cb_null; /* used to wait for CB_NULL */
1245 	zoneid_t		zoneid;	/* zone using this nfs4_server_t */
1246 	struct nfs4_callback_globals *zone_globals;	/* globals */
1247 } nfs4_server_t;
1248 
1249 /* nfs4_server flags */
1250 #define	N4S_CLIENTID_SET	1	/* server has our clientid */
1251 #define	N4S_CLIENTID_PEND	0x2	/* server doesn't have clientid */
1252 #define	N4S_CB_PINGED		0x4	/* server has sent us a CB_NULL */
1253 #define	N4S_CB_WAITER		0x8	/* is/has wait{ing/ed} for cb_null */
1254 #define	N4S_INSERTED		0x10	/* list has reference for server */
1255 #define	N4S_BADOWNER_DEBUG	0x20	/* bad owner err msg per client */
1256 
1257 #define	N4S_CB_PAUSE_TIME	10000	/* Amount of time to pause (10ms) */
1258 
1259 struct lease_time_arg {
1260 	time_t	lease_time;
1261 };
1262 
1263 enum nfs4_delegreturn_policy {
1264 	IMMEDIATE,
1265 	FIRSTCLOSE,
1266 	LASTCLOSE,
1267 	INACTIVE
1268 };
1269 
1270 /*
1271  * Operation hints for the recovery framework (mostly).
1272  *
1273  * EXCEPTIONS:
1274  * OH_ACCESS, OH_GETACL, OH_GETATTR, OH_LOOKUP, OH_READDIR
1275  *	These hints exist to allow user visit/readdir a R4SRVSTUB dir.
1276  *	(dir represents the root of a server fs that has not yet been
1277  *	mounted at client)
1278  */
1279 typedef enum {
1280 	OH_OTHER,
1281 	OH_READ,
1282 	OH_WRITE,
1283 	OH_COMMIT,
1284 	OH_VFH_RENAME,
1285 	OH_MOUNT,
1286 	OH_CLOSE,
1287 	OH_LOCKU,
1288 	OH_DELEGRETURN,
1289 	OH_ACCESS,
1290 	OH_GETACL,
1291 	OH_GETATTR,
1292 	OH_LOOKUP,
1293 	OH_READDIR
1294 } nfs4_op_hint_t;
1295 
1296 /*
1297  * This data structure is used to track ephemeral mounts for both
1298  * mirror mounts and referrals.
1299  *
1300  * Note that each nfs4_ephemeral can only have one other nfs4_ephemeral
1301  * pointing at it. So we don't need two backpointers to walk
1302  * back up the tree.
1303  *
1304  * An ephemeral tree is pointed to by an enclosing non-ephemeral
1305  * mntinfo4. The root is also pointed to by its ephemeral
1306  * mntinfo4. ne_child will get us back to it, while ne_prior
1307  * will get us back to the non-ephemeral mntinfo4. This is an
1308  * edge case we will need to be wary of when walking back up the
1309  * tree.
1310  *
1311  * The way we handle this edge case is to have ne_prior be NULL
1312  * for the root nfs4_ephemeral node.
1313  */
1314 typedef struct nfs4_ephemeral {
1315 	mntinfo4_t		*ne_mount;	/* who encloses us */
1316 	struct nfs4_ephemeral	*ne_child;	/* first child node */
1317 	struct nfs4_ephemeral	*ne_peer;	/* next sibling */
1318 	struct nfs4_ephemeral	*ne_prior;	/* who points at us */
1319 	time_t			ne_ref_time;	/* time last referenced */
1320 	uint_t			ne_mount_to;	/* timeout at */
1321 	int			ne_state;	/* used to traverse */
1322 } nfs4_ephemeral_t;
1323 
1324 /*
1325  * State for the node (set in ne_state):
1326  */
1327 #define	NFS4_EPHEMERAL_OK		0x0
1328 #define	NFS4_EPHEMERAL_VISIT_CHILD	0x1
1329 #define	NFS4_EPHEMERAL_VISIT_SIBLING	0x2
1330 #define	NFS4_EPHEMERAL_PROCESS_ME	0x4
1331 #define	NFS4_EPHEMERAL_CHILD_ERROR	0x8
1332 #define	NFS4_EPHEMERAL_PEER_ERROR	0x10
1333 
1334 /*
1335  * These are the locks used in processing ephemeral data:
1336  *
1337  * mi->mi_lock
1338  *
1339  * net->net_tree_lock
1340  *     This lock is used to gate all tree operations.
1341  *     If it is held, then no other process may
1342  *     traverse the tree. This allows us to not
1343  *     throw a hold on each vfs_t in the tree.
1344  *     Can be held for a "long" time.
1345  *
1346  * net->net_cnt_lock
1347  *     Used to protect refcnt and status.
1348  *     Must be held for a really short time.
1349  *
1350  * nfs4_ephemeral_thread_lock
1351  *     Is only held to create the harvester for the zone.
1352  *     There is no ordering imposed on it.
1353  *     Held for a really short time.
1354  *
1355  * Some further detail on the interactions:
1356  *
1357  * net_tree_lock controls access to net_root. Access needs to first be
1358  * attempted in a non-blocking check.
1359  *
1360  * net_cnt_lock controls access to net_refcnt and net_status. It must only be
1361  * held for very short periods of time, unless the refcnt is 0 and the status
1362  * is INVALID.
1363  *
1364  * Before a caller can grab net_tree_lock, it must first grab net_cnt_lock
1365  * to bump the net_refcnt. It then releases it and does the action specific
1366  * algorithm to get the net_tree_lock. Once it has that, then it is okay to
1367  * grab the net_cnt_lock and change the status. The status can only be
1368  * changed if the caller has the net_tree_lock held as well.
1369  *
1370  * Note that the initial grab of net_cnt_lock must occur whilst
1371  * mi_lock is being held. This prevents stale data in that if the
1372  * ephemeral tree is non-NULL, then the harvester can not remove
1373  * the tree from the mntinfo node until it grabs that lock. I.e.,
1374  * we get the pointer to the tree and hold the lock atomically
1375  * with respect to being in mi_lock.
1376  *
1377  * When a caller is done with net_tree_lock, it can decrement the net_refcnt
1378  * either before it releases net_tree_lock or after.
1379  *
1380  * In either event, to decrement net_refcnt, it must hold net_cnt_lock.
1381  *
1382  * Note that the overall locking scheme for the nodes is to control access
1383  * via the tree. The current scheme could easily be extended such that
1384  * the enclosing root referenced a "forest" of trees. The underlying trees
1385  * would be autonomous with respect to locks.
1386  *
1387  * Note that net_next is controlled by external locks
1388  * particular to the data structure that the tree is being added to.
1389  */
1390 typedef struct nfs4_ephemeral_tree {
1391 	mntinfo4_t			*net_mount;
1392 	nfs4_ephemeral_t		*net_root;
1393 	struct nfs4_ephemeral_tree	*net_next;
1394 	kmutex_t			net_tree_lock;
1395 	kmutex_t			net_cnt_lock;
1396 	uint_t				net_status;
1397 	uint_t				net_refcnt;
1398 } nfs4_ephemeral_tree_t;
1399 
1400 /*
1401  * State for the tree (set in net_status):
1402  */
1403 #define	NFS4_EPHEMERAL_TREE_OK		0x0
1404 #define	NFS4_EPHEMERAL_TREE_BUILDING	0x1
1405 #define	NFS4_EPHEMERAL_TREE_DEROOTING	0x2
1406 #define	NFS4_EPHEMERAL_TREE_INVALID	0x4
1407 #define	NFS4_EPHEMERAL_TREE_MOUNTING	0x8
1408 #define	NFS4_EPHEMERAL_TREE_UMOUNTING	0x10
1409 #define	NFS4_EPHEMERAL_TREE_LOCKED	0x20
1410 
1411 #define	NFS4_EPHEMERAL_TREE_PROCESSING	(NFS4_EPHEMERAL_TREE_DEROOTING | \
1412 	NFS4_EPHEMERAL_TREE_INVALID | NFS4_EPHEMERAL_TREE_UMOUNTING | \
1413 	NFS4_EPHEMERAL_TREE_LOCKED)
1414 
1415 /*
1416  * This macro evaluates to non-zero if the given op releases state at the
1417  * server.
1418  */
1419 #define	OH_IS_STATE_RELE(op)	((op) == OH_CLOSE || (op) == OH_LOCKU || \
1420 				(op) == OH_DELEGRETURN)
1421 
1422 #ifdef _KERNEL
1423 
1424 extern void	nfs4_async_manager(struct vfs *);
1425 extern void	nfs4_async_manager_stop(struct vfs *);
1426 extern void	nfs4_async_stop(struct vfs *);
1427 extern int	nfs4_async_stop_sig(struct vfs *);
1428 extern int	nfs4_async_readahead(vnode_t *, u_offset_t, caddr_t,
1429 				struct seg *, cred_t *,
1430 				void (*)(vnode_t *, u_offset_t,
1431 				caddr_t, struct seg *, cred_t *));
1432 extern int	nfs4_async_putapage(vnode_t *, page_t *, u_offset_t, size_t,
1433 				int, cred_t *, int (*)(vnode_t *, page_t *,
1434 				u_offset_t, size_t, int, cred_t *));
1435 extern int	nfs4_async_pageio(vnode_t *, page_t *, u_offset_t, size_t,
1436 				int, cred_t *, int (*)(vnode_t *, page_t *,
1437 				u_offset_t, size_t, int, cred_t *));
1438 extern void	nfs4_async_commit(vnode_t *, page_t *, offset3, count3,
1439 				cred_t *, void (*)(vnode_t *, page_t *,
1440 				offset3, count3, cred_t *));
1441 extern void	nfs4_async_inactive(vnode_t *, cred_t *);
1442 extern void	nfs4_inactive_thread(mntinfo4_t *mi);
1443 extern void	nfs4_inactive_otw(vnode_t *, cred_t *);
1444 extern int	nfs4_putpages(vnode_t *, u_offset_t, size_t, int, cred_t *);
1445 
1446 extern int	nfs4_setopts(vnode_t *, model_t, struct nfs_args *);
1447 extern void	nfs4_mnt_kstat_init(struct vfs *);
1448 
1449 extern void	rfs4call(struct mntinfo4 *, struct COMPOUND4args_clnt *,
1450 			struct COMPOUND4res_clnt *, cred_t *, int *, int,
1451 			nfs4_error_t *);
1452 extern void	nfs4_acl_fill_cache(struct rnode4 *, vsecattr_t *);
1453 extern int	nfs4_attr_otw(vnode_t *, nfs4_tag_type_t,
1454 				nfs4_ga_res_t *, bitmap4, cred_t *);
1455 
1456 extern void	nfs4_attrcache_noinval(vnode_t *, nfs4_ga_res_t *, hrtime_t);
1457 extern void	nfs4_attr_cache(vnode_t *, nfs4_ga_res_t *,
1458 				hrtime_t, cred_t *, int,
1459 				change_info4 *);
1460 extern void	nfs4_purge_rddir_cache(vnode_t *);
1461 extern void	nfs4_invalidate_pages(vnode_t *, u_offset_t, cred_t *);
1462 extern void	nfs4_purge_caches(vnode_t *, int, cred_t *, int);
1463 extern void	nfs4_purge_stale_fh(int, vnode_t *, cred_t *);
1464 
1465 extern void	nfs4rename_update(vnode_t *, vnode_t *, nfs_fh4 *, char *);
1466 extern void	nfs4_update_paths(vnode_t *, char *, vnode_t *, char *,
1467 			vnode_t *);
1468 
1469 extern void	nfs4args_lookup_free(nfs_argop4 *, int);
1470 extern void	nfs4args_copen_free(OPEN4cargs *);
1471 
1472 extern void	nfs4_printfhandle(nfs4_fhandle_t *);
1473 
1474 extern void	nfs_free_mi4(mntinfo4_t *);
1475 extern void	sv4_free(servinfo4_t *);
1476 extern void	nfs4_mi_zonelist_add(mntinfo4_t *);
1477 extern int	nfs4_mi_zonelist_remove(mntinfo4_t *);
1478 extern int 	nfs4_secinfo_recov(mntinfo4_t *, vnode_t *, vnode_t *);
1479 extern void	nfs4_secinfo_init(void);
1480 extern void	nfs4_secinfo_fini(void);
1481 extern int	nfs4_secinfo_path(mntinfo4_t *, cred_t *, int);
1482 extern int 	nfs4_secinfo_vnode_otw(vnode_t *, char *, cred_t *);
1483 extern void	secinfo_free(sv_secinfo_t *);
1484 extern void	save_mnt_secinfo(servinfo4_t *);
1485 extern void	check_mnt_secinfo(servinfo4_t *, vnode_t *);
1486 extern int	vattr_to_fattr4(vattr_t *, vsecattr_t *, fattr4 *, int,
1487 				enum nfs_opnum4, bitmap4 supp_mask);
1488 extern int	nfs4_putapage(vnode_t *, page_t *, u_offset_t *, size_t *,
1489 			int, cred_t *);
1490 extern void	nfs4_write_error(vnode_t *, int, cred_t *);
1491 extern void	nfs4_lockcompletion(vnode_t *, int);
1492 extern bool_t	nfs4_map_lost_lock_conflict(vnode_t *);
1493 extern int	vtodv(vnode_t *, vnode_t **, cred_t *, bool_t);
1494 extern void	nfs4open_confirm(vnode_t *, seqid4*, stateid4 *, cred_t *,
1495 		    bool_t, bool_t *, nfs4_open_owner_t *, bool_t,
1496 		    nfs4_error_t *, int *);
1497 extern void	nfs4_error_zinit(nfs4_error_t *);
1498 extern void	nfs4_error_init(nfs4_error_t *, int);
1499 extern void	nfs4_free_args(struct nfs_args *);
1500 
1501 extern void 	mi_hold(mntinfo4_t *);
1502 extern void	mi_rele(mntinfo4_t *);
1503 
1504 extern sec_data_t	*copy_sec_data(sec_data_t *);
1505 extern gss_clntdata_t	*copy_sec_data_gss(gss_clntdata_t *);
1506 
1507 #ifdef DEBUG
1508 extern int	nfs4_consistent_type(vnode_t *);
1509 #endif
1510 
1511 extern void	nfs4_init_dot_entries(void);
1512 extern void	nfs4_destroy_dot_entries(void);
1513 extern struct nfs4_callback_globals	*nfs4_get_callback_globals(void);
1514 
1515 extern struct nfs4_server nfs4_server_lst;
1516 
1517 extern clock_t nfs_write_error_interval;
1518 
1519 #endif /* _KERNEL */
1520 
1521 /*
1522  * Flags for nfs4getfh_otw.
1523  */
1524 
1525 #define	NFS4_GETFH_PUBLIC	0x01
1526 #define	NFS4_GETFH_NEEDSOP	0x02
1527 
1528 /*
1529  * Found through rnodes.
1530  *
1531  * The os_open_ref_count keeps track the number of open file descriptor
1532  * refernces on this data structure.  It will be bumped for any successful
1533  * OTW OPEN call and any OPEN call that determines the OTW call is not
1534  * necessary and the open stream hasn't just been created (see
1535  * nfs4_is_otw_open_necessary).
1536  *
1537  * os_mapcnt is a count of the number of mmapped pages for a particular
1538  * open stream; this in conjunction w/ os_open_ref_count is used to
1539  * determine when to do a close to the server.  This is necessary because
1540  * of the semantics of doing open, mmap, close; the OTW close must be wait
1541  * until all open and mmap references have vanished.
1542  *
1543  * 'os_valid' tells us whether this structure is about to be freed or not,
1544  * if it is then don't return it in find_open_stream().
1545  *
1546  * 'os_final_close' is set when a CLOSE OTW was attempted.  This is needed
1547  * so we can properly count the os_open_ref_count in cases where we VOP_CLOSE
1548  * without a VOP_OPEN, and have nfs4_inactive() drive the OTW CLOSE.  It
1549  * also helps differentiate the VOP_OPEN/VN_RELE case from the VOP_CLOSE
1550  * that tried to close OTW but failed, and left the state cleanup to
1551  * nfs4_inactive/CLOSE_FORCE.
1552  *
1553  * 'os_force_close' is used to let us know if an intervening thread came
1554  * and reopened the open stream after we decided to issue a CLOSE_FORCE,
1555  * but before we could actually process the CLOSE_FORCE.
1556  *
1557  * 'os_pending_close' is set when an over-the-wire CLOSE is deferred to the
1558  * lost state queue.
1559  *
1560  * 'open_stateid' is set the last open stateid returned by the server unless
1561  * 'os_delegation' is 1, in which case 'open_stateid' refers to the
1562  * delegation stateid returned by the server.  This is used in cases where the
1563  * client tries to OPEN a file but already has a suitable delegation, so we
1564  * just stick the delegation stateid in the open stream.
1565  *
1566  * os_dc_openacc are open access bits which have been granted to the
1567  * open stream by virtue of a delegation, but which have not been seen
1568  * by the server.  This applies even if the open stream does not have
1569  * os_delegation set.  These bits are used when setting file locks to
1570  * determine whether an open with CLAIM_DELEGATE_CUR needs to be done
1571  * before the lock request can be sent to the server.  See
1572  * nfs4frlock_check_deleg().
1573  *
1574  * 'os_mmap_read/write' keep track of the read and write access our memory
1575  * maps require.  We need to keep track of this so we can provide the proper
1576  * access bits in the open/mmap/close/reboot/reopen case.
1577  *
1578  * 'os_failed_reopen' tells us that we failed to successfully reopen this
1579  * open stream; therefore, we should not use this open stateid as it is
1580  * not valid anymore. This flag is also used to indicate an unsuccessful
1581  * attempt to reopen a delegation open stream with CLAIM_DELEGATE_CUR.
1582  *
1583  * If 'os_orig_oo_name' is different than os_open_owner's oo_name
1584  * then this tells us that this open stream's open owner used a
1585  * bad seqid (that is, got NFS4ERR_BAD_SEQID).  If different, this open
1586  * stream will no longer be used for future OTW state releasing calls.
1587  *
1588  * Lock ordering:
1589  * rnode4_t::r_os_lock > os_sync_lock
1590  * os_sync_lock > rnode4_t::r_statelock
1591  * os_sync_lock > rnode4_t::r_statev4_lock
1592  * os_sync_lock > mntinfo4_t::mi_lock (via hold over rfs4call)
1593  *
1594  * The 'os_sync_lock' protects:
1595  *	open_stateid
1596  *	os_dc_openacc
1597  *	os_delegation
1598  *	os_failed_reopen
1599  *	os_final_close
1600  *	os_force_close
1601  *	os_mapcnt
1602  *	os_mmap_read
1603  *	os_mmap_write
1604  *	os_open_ref_count
1605  *	os_pending_close
1606  *	os_share_acc_read
1607  *	os_share_acc_write
1608  *	os_share_deny_none
1609  *	os_share_deny_read
1610  *	os_share_deny_write
1611  *	os_ref_count
1612  *	os_valid
1613  *
1614  * The rnode4_t::r_os_lock protects:
1615  *	os_node
1616  *
1617  * These fields are set at creation time and
1618  * read only after that:
1619  *	os_open_owner
1620  *	os_orig_oo_name
1621  */
1622 typedef struct nfs4_open_stream {
1623 	uint64_t		os_share_acc_read;
1624 	uint64_t		os_share_acc_write;
1625 	uint64_t		os_mmap_read;
1626 	uint64_t		os_mmap_write;
1627 	uint32_t		os_share_deny_none;
1628 	uint32_t		os_share_deny_read;
1629 	uint32_t		os_share_deny_write;
1630 	stateid4		open_stateid;
1631 	int			os_dc_openacc;
1632 	int			os_ref_count;
1633 	unsigned		os_valid:1;
1634 	unsigned 		os_delegation:1;
1635 	unsigned		os_final_close:1;
1636 	unsigned 		os_pending_close:1;
1637 	unsigned 		os_failed_reopen:1;
1638 	unsigned		os_force_close:1;
1639 	int			os_open_ref_count;
1640 	long			os_mapcnt;
1641 	list_node_t		os_node;
1642 	struct nfs4_open_owner	*os_open_owner;
1643 	uint64_t		os_orig_oo_name;
1644 	kmutex_t		os_sync_lock;
1645 } nfs4_open_stream_t;
1646 
1647 /*
1648  * This structure describes the format of the lock_owner_name
1649  * field of the lock owner.
1650  */
1651 
1652 typedef struct nfs4_lo_name {
1653 	uint64_t	ln_seq_num;
1654 	pid_t		ln_pid;
1655 } nfs4_lo_name_t;
1656 
1657 /*
1658  * Flags for lo_flags.
1659  */
1660 #define	NFS4_LOCK_SEQID_INUSE	0x1
1661 #define	NFS4_BAD_SEQID_LOCK	0x2
1662 
1663 /*
1664  * The lo_prev_rnode and lo_next_rnode are for a circular list that hangs
1665  * off the rnode.  If the links are NULL it means this object is not on the
1666  * list.
1667  *
1668  * 'lo_pending_rqsts' is non-zero if we ever tried to send a request and
1669  * didn't get a response back.  This is used to figure out if we have
1670  * possible remote v4 locks, so that we can clean up at process exit.  In
1671  * theory, the client should be able to figure out if the server received
1672  * the request (based on what seqid works), so maybe we can get rid of this
1673  * flag someday.
1674  *
1675  * 'lo_ref_count' tells us how many processes/threads are using this data
1676  * structure.  The rnode's list accounts for one reference.
1677  *
1678  * 'lo_just_created' is set to NFS4_JUST_CREATED when we first create the
1679  * data structure.  It is then set to NFS4_PERM_CREATED when a lock request
1680  * is successful using this lock owner structure.  We need to keep 'temporary'
1681  * lock owners around so we can properly keep the lock seqid synchronization
1682  * when multiple processes/threads are trying to create the lock owner for the
1683  * first time (especially with the DENIED error case).  Once
1684  * 'lo_just_created' is set to NFS4_PERM_CREATED, it doesn't change.
1685  *
1686  * 'lo_valid' tells us whether this structure is about to be freed or not,
1687  * if it is then don't return it from find_lock_owner().
1688  *
1689  * Retrieving and setting of 'lock_seqid' is protected by the
1690  * NFS4_LOCK_SEQID_INUSE flag.  Waiters for NFS4_LOCK_SEQID_INUSE should
1691  * use 'lo_cv_seqid_sync'.
1692  *
1693  * The setting of 'lock_stateid' is protected by the
1694  * NFS4_LOCK_SEQID_INUSE flag and 'lo_lock'.  The retrieving of the
1695  * 'lock_stateid' is protected by 'lo_lock', with the additional
1696  * requirement that the calling function can handle NFS4ERR_OLD_STATEID and
1697  * NFS4ERR_BAD_STATEID as appropiate.
1698  *
1699  * The setting of NFS4_BAD_SEQID_LOCK to lo_flags tells us whether this lock
1700  * owner used a bad seqid (that is, got NFS4ERR_BAD_SEQID).  With this set,
1701  * this lock owner will no longer be used for future OTW calls.  Once set,
1702  * it is never unset.
1703  *
1704  * Lock ordering:
1705  * rnode4_t::r_statev4_lock > lo_lock
1706  */
1707 typedef struct nfs4_lock_owner {
1708 	struct nfs4_lock_owner	*lo_next_rnode;
1709 	struct nfs4_lock_owner	*lo_prev_rnode;
1710 	int			lo_pid;
1711 	stateid4		lock_stateid;
1712 	seqid4			lock_seqid;
1713 	/*
1714 	 * Fix this to always be 12 bytes
1715 	 */
1716 	nfs4_lo_name_t		lock_owner_name;
1717 	int			lo_ref_count;
1718 	int			lo_valid;
1719 	int			lo_pending_rqsts;
1720 	int			lo_just_created;
1721 	int			lo_flags;
1722 	kcondvar_t		lo_cv_seqid_sync;
1723 	kmutex_t		lo_lock;
1724 	kthread_t		*lo_seqid_holder; /* debugging aid */
1725 } nfs4_lock_owner_t;
1726 
1727 /* for nfs4_lock_owner_t lookups */
1728 typedef enum {LOWN_ANY, LOWN_VALID_STATEID} lown_which_t;
1729 
1730 /* Number of times to retry a call that fails with state independent error */
1731 #define	NFS4_NUM_RECOV_RETRIES	3
1732 
1733 typedef enum {
1734 	NO_SID,
1735 	DEL_SID,
1736 	LOCK_SID,
1737 	OPEN_SID,
1738 	SPEC_SID
1739 } nfs4_stateid_type_t;
1740 
1741 typedef struct nfs4_stateid_types {
1742 	stateid4 d_sid;
1743 	stateid4 l_sid;
1744 	stateid4 o_sid;
1745 	nfs4_stateid_type_t cur_sid_type;
1746 } nfs4_stateid_types_t;
1747 
1748 /*
1749  * Per-zone data for dealing with callbacks.  Included here solely for the
1750  * benefit of MDB.
1751  */
1752 struct nfs4_callback_stats {
1753 	kstat_named_t	delegations;
1754 	kstat_named_t	cb_getattr;
1755 	kstat_named_t	cb_recall;
1756 	kstat_named_t	cb_null;
1757 	kstat_named_t	cb_dispatch;
1758 	kstat_named_t	delegaccept_r;
1759 	kstat_named_t	delegaccept_rw;
1760 	kstat_named_t	delegreturn;
1761 	kstat_named_t	callbacks;
1762 	kstat_named_t	claim_cur;
1763 	kstat_named_t	claim_cur_ok;
1764 	kstat_named_t	recall_trunc;
1765 	kstat_named_t	recall_failed;
1766 	kstat_named_t	return_limit_write;
1767 	kstat_named_t	return_limit_addmap;
1768 	kstat_named_t	deleg_recover;
1769 	kstat_named_t	cb_illegal;
1770 };
1771 
1772 struct nfs4_callback_globals {
1773 	kmutex_t nfs4_cb_lock;
1774 	kmutex_t nfs4_dlist_lock;
1775 	int nfs4_program_hint;
1776 	/* this table maps the program number to the nfs4_server structure */
1777 	struct nfs4_server **nfs4prog2server;
1778 	list_t nfs4_dlist;
1779 	list_t nfs4_cb_ports;
1780 	struct nfs4_callback_stats nfs4_callback_stats;
1781 #ifdef DEBUG
1782 	int nfs4_dlistadd_c;
1783 	int nfs4_dlistclean_c;
1784 #endif
1785 };
1786 
1787 typedef enum {
1788 	CLOSE_NORM,
1789 	CLOSE_DELMAP,
1790 	CLOSE_FORCE,
1791 	CLOSE_RESEND,
1792 	CLOSE_AFTER_RESEND
1793 } nfs4_close_type_t;
1794 
1795 /*
1796  * Structure to hold the bad seqid information that is passed
1797  * to the recovery framework.
1798  */
1799 typedef struct nfs4_bseqid_entry {
1800 	nfs4_open_owner_t	*bs_oop;
1801 	nfs4_lock_owner_t	*bs_lop;
1802 	vnode_t			*bs_vp;
1803 	pid_t			bs_pid;
1804 	nfs4_tag_type_t		bs_tag;
1805 	seqid4			bs_seqid;
1806 	list_node_t		bs_node;
1807 } nfs4_bseqid_entry_t;
1808 
1809 #ifdef _KERNEL
1810 
1811 extern void	nfs4close_one(vnode_t *, nfs4_open_stream_t *, cred_t *, int,
1812 		    nfs4_lost_rqst_t *, nfs4_error_t *, nfs4_close_type_t,
1813 		    size_t, uint_t, uint_t);
1814 extern void	nfs4close_notw(vnode_t *, nfs4_open_stream_t *, int *);
1815 extern void	nfs4_set_lock_stateid(nfs4_lock_owner_t *, stateid4);
1816 extern void	open_owner_hold(nfs4_open_owner_t *);
1817 extern void	open_owner_rele(nfs4_open_owner_t *);
1818 extern nfs4_open_stream_t	*find_or_create_open_stream(nfs4_open_owner_t *,
1819 					struct rnode4 *, int *);
1820 extern nfs4_open_stream_t *find_open_stream(nfs4_open_owner_t *,
1821 				struct rnode4 *);
1822 extern nfs4_open_stream_t *create_open_stream(nfs4_open_owner_t *oop,
1823 				struct rnode4 *rp);
1824 extern void	open_stream_hold(nfs4_open_stream_t *);
1825 extern void	open_stream_rele(nfs4_open_stream_t *, struct rnode4 *);
1826 extern int	nfs4close_all(vnode_t *, cred_t *);
1827 extern void	lock_owner_hold(nfs4_lock_owner_t *);
1828 extern void	lock_owner_rele(nfs4_lock_owner_t *);
1829 extern nfs4_lock_owner_t *create_lock_owner(struct rnode4 *, pid_t);
1830 extern nfs4_lock_owner_t *find_lock_owner(struct rnode4 *, pid_t, lown_which_t);
1831 extern void	nfs4_rnode_remove_lock_owner(struct rnode4 *,
1832 			nfs4_lock_owner_t *);
1833 extern void	nfs4_flush_lock_owners(struct rnode4 *);
1834 extern void nfs4_setlockowner_args(lock_owner4 *, struct rnode4 *, pid_t);
1835 extern void	nfs4_set_open_seqid(seqid4, nfs4_open_owner_t *,
1836 		    nfs4_tag_type_t);
1837 extern void	nfs4_set_lock_seqid(seqid4, nfs4_lock_owner_t *);
1838 extern void	nfs4_get_and_set_next_open_seqid(nfs4_open_owner_t *,
1839 		    nfs4_tag_type_t);
1840 extern void	nfs4_end_open_seqid_sync(nfs4_open_owner_t *);
1841 extern int	nfs4_start_open_seqid_sync(nfs4_open_owner_t *, mntinfo4_t *);
1842 extern void	nfs4_end_lock_seqid_sync(nfs4_lock_owner_t *);
1843 extern int	nfs4_start_lock_seqid_sync(nfs4_lock_owner_t *, mntinfo4_t *);
1844 extern void	nfs4_setup_lock_args(nfs4_lock_owner_t *, nfs4_open_owner_t *,
1845 			nfs4_open_stream_t *, clientid4, locker4 *);
1846 extern void	nfs4_destroy_open_owner(nfs4_open_owner_t *);
1847 
1848 extern void		nfs4_renew_lease_thread(nfs4_server_t *);
1849 extern nfs4_server_t	*find_nfs4_server(mntinfo4_t *);
1850 extern nfs4_server_t	*find_nfs4_server_all(mntinfo4_t *, int all);
1851 extern nfs4_server_t	*new_nfs4_server(servinfo4_t *,	cred_t *);
1852 extern void		nfs4_mark_srv_dead(nfs4_server_t *);
1853 extern nfs4_server_t	*servinfo4_to_nfs4_server(servinfo4_t *);
1854 extern void		nfs4_inc_state_ref_count(mntinfo4_t *);
1855 extern void		nfs4_inc_state_ref_count_nolock(nfs4_server_t *,
1856 				mntinfo4_t *);
1857 extern void		nfs4_dec_state_ref_count(mntinfo4_t *);
1858 extern void		nfs4_dec_state_ref_count_nolock(nfs4_server_t *,
1859 				mntinfo4_t *);
1860 extern clientid4	mi2clientid(mntinfo4_t *);
1861 extern int		nfs4_server_in_recovery(nfs4_server_t *);
1862 extern bool_t		nfs4_server_vlock(nfs4_server_t *, int);
1863 extern nfs4_open_owner_t *create_open_owner(cred_t *, mntinfo4_t *);
1864 extern uint64_t		nfs4_get_new_oo_name(void);
1865 extern nfs4_open_owner_t *find_open_owner(cred_t *, int, mntinfo4_t *);
1866 extern nfs4_open_owner_t *find_open_owner_nolock(cred_t *, int, mntinfo4_t *);
1867 extern void	nfs4frlock(nfs4_lock_call_type_t, vnode_t *, int, flock64_t *,
1868 			int, u_offset_t, cred_t *, nfs4_error_t *,
1869 			nfs4_lost_rqst_t *, int *);
1870 extern void	nfs4open_dg_save_lost_rqst(int, nfs4_lost_rqst_t *,
1871 		    nfs4_open_owner_t *, nfs4_open_stream_t *, cred_t *,
1872 		    vnode_t *, int, int);
1873 extern void	nfs4_open_downgrade(int, int, nfs4_open_owner_t *,
1874 		    nfs4_open_stream_t *, vnode_t *, cred_t *,
1875 		    nfs4_lost_rqst_t *, nfs4_error_t *, cred_t **, seqid4 *);
1876 extern seqid4	nfs4_get_open_seqid(nfs4_open_owner_t *);
1877 extern cred_t	*nfs4_get_otw_cred(cred_t *, mntinfo4_t *, nfs4_open_owner_t *);
1878 extern void	nfs4_init_stateid_types(nfs4_stateid_types_t *);
1879 extern void	nfs4_save_stateid(stateid4 *, nfs4_stateid_types_t *);
1880 
1881 extern kmutex_t nfs4_server_lst_lock;
1882 
1883 extern void	nfs4callback_destroy(nfs4_server_t *);
1884 extern void	nfs4_callback_init(void);
1885 extern void	nfs4_callback_fini(void);
1886 extern void	nfs4_cb_args(nfs4_server_t *, struct knetconfig *,
1887 			SETCLIENTID4args *);
1888 extern void	nfs4delegreturn_async(struct rnode4 *, int, bool_t);
1889 
1890 extern enum nfs4_delegreturn_policy nfs4_delegreturn_policy;
1891 
1892 extern void	nfs4_add_mi_to_server(nfs4_server_t *, mntinfo4_t *);
1893 extern void	nfs4_remove_mi_from_server(mntinfo4_t *, nfs4_server_t *);
1894 extern nfs4_server_t *nfs4_move_mi(mntinfo4_t *, servinfo4_t *, servinfo4_t *);
1895 extern bool_t	nfs4_fs_active(nfs4_server_t *);
1896 extern void	nfs4_server_rele(nfs4_server_t *);
1897 extern bool_t	inlease(nfs4_server_t *);
1898 extern bool_t	nfs4_has_pages(vnode_t *);
1899 extern void	nfs4_log_badowner(mntinfo4_t *, nfs_opnum4);
1900 
1901 #endif /* _KERNEL */
1902 
1903 /*
1904  * Client State Recovery
1905  */
1906 
1907 /*
1908  * The following defines are used for rs_flags in
1909  * a nfs4_recov_state_t structure.
1910  *
1911  * NFS4_RS_RENAME_HELD		Indicates that the mi_rename_lock was held.
1912  * NFS4_RS_GRACE_MSG		Set once we have uprintf'ed a grace message.
1913  * NFS4_RS_DELAY_MSG		Set once we have uprintf'ed a delay message.
1914  * NFS4_RS_RECALL_HELD1		r_deleg_recall_lock for vp1 was held.
1915  * NFS4_RS_RECALL_HELD2		r_deleg_recall_lock for vp2 was held.
1916  */
1917 #define	NFS4_RS_RENAME_HELD	0x000000001
1918 #define	NFS4_RS_GRACE_MSG	0x000000002
1919 #define	NFS4_RS_DELAY_MSG	0x000000004
1920 #define	NFS4_RS_RECALL_HELD1	0x000000008
1921 #define	NFS4_RS_RECALL_HELD2	0x000000010
1922 
1923 /*
1924  * Information that is retrieved from nfs4_start_op() and that is
1925  * passed into nfs4_end_op().
1926  *
1927  * rs_sp is a reference to the nfs4_server that was found, or NULL.
1928  *
1929  * rs_num_retry_despite_err is the number times client retried an
1930  * OTW op despite a recovery error.  It is only incremented for hints
1931  * exempt to normal R4RECOVERR processing
1932  * (OH_CLOSE/OH_LOCKU/OH_DELEGRETURN).  (XXX this special-case code
1933  * needs review for possible removal.)
1934  * It is initialized wherever nfs4_recov_state_t is declared -- usually
1935  * very near initialization of rs_flags.
1936  */
1937 typedef struct {
1938 	nfs4_server_t	*rs_sp;
1939 	int		rs_flags;
1940 	int		rs_num_retry_despite_err;
1941 } nfs4_recov_state_t;
1942 
1943 /*
1944  * Flags for nfs4_check_remap, nfs4_remap_file and nfs4_remap_root.
1945  */
1946 
1947 #define	NFS4_REMAP_CKATTRS	1
1948 #define	NFS4_REMAP_NEEDSOP	2
1949 
1950 #ifdef _KERNEL
1951 
1952 extern int	nfs4_is_otw_open_necessary(nfs4_open_owner_t *, int,
1953 			vnode_t *, int, int *, int, nfs4_recov_state_t *);
1954 extern void	nfs4setclientid(struct mntinfo4 *, struct cred *, bool_t,
1955 			nfs4_error_t *);
1956 extern void	nfs4_reopen(vnode_t *, nfs4_open_stream_t *, nfs4_error_t *,
1957 			open_claim_type4, bool_t, bool_t);
1958 extern void	nfs4_remap_root(struct mntinfo4 *, nfs4_error_t *, int);
1959 extern void	nfs4_check_remap(mntinfo4_t *mi, vnode_t *vp, int,
1960 			nfs4_error_t *);
1961 extern void	nfs4_remap_file(mntinfo4_t *mi, vnode_t *vp, int,
1962 			nfs4_error_t *);
1963 extern int	nfs4_make_dotdot(struct nfs4_sharedfh *, hrtime_t,
1964 			vnode_t *, cred_t *, vnode_t **, int);
1965 extern void	nfs4_fail_recov(vnode_t *, char *, int, nfsstat4);
1966 
1967 extern int	nfs4_needs_recovery(nfs4_error_t *, bool_t, vfs_t *);
1968 extern int	nfs4_recov_marks_dead(nfsstat4);
1969 extern bool_t	nfs4_start_recovery(nfs4_error_t *, struct mntinfo4 *,
1970 			vnode_t *, vnode_t *, stateid4 *,
1971 			nfs4_lost_rqst_t *, nfs_opnum4, nfs4_bseqid_entry_t *);
1972 extern int	nfs4_start_op(struct mntinfo4 *, vnode_t *, vnode_t *,
1973 			nfs4_recov_state_t *);
1974 extern void	nfs4_end_op(struct mntinfo4 *, vnode_t *, vnode_t *,
1975 			nfs4_recov_state_t *, bool_t);
1976 extern int	nfs4_start_fop(struct mntinfo4 *, vnode_t *, vnode_t *,
1977 			nfs4_op_hint_t, nfs4_recov_state_t *, bool_t *);
1978 extern void	nfs4_end_fop(struct mntinfo4 *, vnode_t *, vnode_t *,
1979 				nfs4_op_hint_t, nfs4_recov_state_t *, bool_t);
1980 extern char	*nfs4_recov_action_to_str(nfs4_recov_t);
1981 
1982 /*
1983  * In sequence, code desiring to unmount an ephemeral tree must
1984  * call nfs4_ephemeral_umount, nfs4_ephemeral_umount_activate,
1985  * and nfs4_ephemeral_umount_unlock. The _unlock must also be
1986  * called on all error paths that occur before it would naturally
1987  * be invoked.
1988  *
1989  * The caller must also provde a pointer to a boolean to keep track
1990  * of whether or not the code in _unlock is to be ran.
1991  */
1992 extern void	nfs4_ephemeral_umount_activate(mntinfo4_t *,
1993     bool_t *, bool_t *, nfs4_ephemeral_tree_t **);
1994 extern int	nfs4_ephemeral_umount(mntinfo4_t *, int, cred_t *,
1995     bool_t *, bool_t *, nfs4_ephemeral_tree_t **);
1996 extern void	nfs4_ephemeral_umount_unlock(bool_t *, bool_t *,
1997     nfs4_ephemeral_tree_t **);
1998 
1999 extern int	nfs4_record_ephemeral_mount(mntinfo4_t *mi, vnode_t *mvp);
2000 
2001 extern int	wait_for_recall(vnode_t *, vnode_t *, nfs4_op_hint_t,
2002 			nfs4_recov_state_t *);
2003 extern void	nfs4_end_op_recall(vnode_t *, vnode_t *, nfs4_recov_state_t *);
2004 extern void	nfs4_send_siglost(pid_t, mntinfo4_t *mi, vnode_t *vp, bool_t,
2005 		    int, nfsstat4);
2006 extern time_t	nfs4err_delay_time;
2007 extern void	nfs4_set_grace_wait(mntinfo4_t *);
2008 extern void	nfs4_set_delay_wait(vnode_t *);
2009 extern int	nfs4_wait_for_grace(mntinfo4_t *, nfs4_recov_state_t *);
2010 extern int	nfs4_wait_for_delay(vnode_t *, nfs4_recov_state_t *);
2011 extern nfs4_bseqid_entry_t *nfs4_create_bseqid_entry(nfs4_open_owner_t *,
2012 		    nfs4_lock_owner_t *, vnode_t *, pid_t, nfs4_tag_type_t,
2013 		    seqid4);
2014 
2015 extern void	nfs4_resend_open_otw(vnode_t **, nfs4_lost_rqst_t *,
2016 			nfs4_error_t *);
2017 extern void	nfs4_resend_delegreturn(nfs4_lost_rqst_t *, nfs4_error_t *,
2018 			nfs4_server_t *);
2019 extern int	nfs4_rpc_retry_error(int);
2020 extern int	nfs4_try_failover(nfs4_error_t *);
2021 extern void	nfs4_free_msg(nfs4_debug_msg_t *);
2022 extern void	nfs4_mnt_recov_kstat_init(vfs_t *);
2023 extern void	nfs4_mi_kstat_inc_delay(mntinfo4_t *);
2024 extern void	nfs4_mi_kstat_inc_no_grace(mntinfo4_t *);
2025 extern char	*nfs4_stat_to_str(nfsstat4);
2026 extern char	*nfs4_op_to_str(nfs_opnum4);
2027 
2028 extern void	nfs4_queue_event(nfs4_event_type_t, mntinfo4_t *, char *,
2029 		    uint_t, vnode_t *, vnode_t *, nfsstat4, char *, pid_t,
2030 		    nfs4_tag_type_t, nfs4_tag_type_t, seqid4, seqid4);
2031 extern void	nfs4_queue_fact(nfs4_fact_type_t, mntinfo4_t *, nfsstat4,
2032 		    nfs4_recov_t, nfs_opnum4, bool_t, char *, int, vnode_t *);
2033 #pragma	rarely_called(nfs4_queue_event)
2034 #pragma	rarely_called(nfs4_queue_fact)
2035 
2036 /* Used for preformed "." and ".." dirents */
2037 extern char	*nfs4_dot_entries;
2038 extern char	*nfs4_dot_dot_entry;
2039 
2040 #ifdef	DEBUG
2041 extern uint_t	nfs4_tsd_key;
2042 #endif
2043 
2044 #endif /* _KERNEL */
2045 
2046 /*
2047  * Filehandle management.
2048  *
2049  * Filehandles can change in v4, so rather than storing the filehandle
2050  * directly in the rnode, etc., we manage the filehandle through one of
2051  * these objects.
2052  * Locking: sfh_fh and sfh_tree is protected by the filesystem's
2053  * mi_fh_lock.  The reference count and flags are protected by sfh_lock.
2054  * sfh_mi is read-only.
2055  *
2056  * mntinfo4_t::mi_fh_lock > sfh_lock.
2057  */
2058 
2059 typedef struct nfs4_sharedfh {
2060 	nfs_fh4 sfh_fh;			/* key and current filehandle */
2061 	kmutex_t sfh_lock;
2062 	uint_t sfh_refcnt;		/* reference count */
2063 	uint_t sfh_flags;
2064 	mntinfo4_t *sfh_mi;		/* backptr to filesystem */
2065 	avl_node_t sfh_tree;		/* used by avl package */
2066 } nfs4_sharedfh_t;
2067 
2068 #define	SFH4_SAME(sfh1, sfh2)	((sfh1) == (sfh2))
2069 
2070 /*
2071  * Flags.
2072  */
2073 #define	SFH4_IN_TREE	0x1		/* currently in an AVL tree */
2074 
2075 #ifdef _KERNEL
2076 
2077 extern void sfh4_createtab(avl_tree_t *);
2078 extern nfs4_sharedfh_t *sfh4_get(const nfs_fh4 *, mntinfo4_t *);
2079 extern nfs4_sharedfh_t *sfh4_put(const nfs_fh4 *, mntinfo4_t *,
2080 				nfs4_sharedfh_t *);
2081 extern void sfh4_update(nfs4_sharedfh_t *, const nfs_fh4 *);
2082 extern void sfh4_copyval(const nfs4_sharedfh_t *, nfs4_fhandle_t *);
2083 extern void sfh4_hold(nfs4_sharedfh_t *);
2084 extern void sfh4_rele(nfs4_sharedfh_t **);
2085 extern void sfh4_printfhandle(const nfs4_sharedfh_t *);
2086 
2087 #endif
2088 
2089 /*
2090  * Path and file name management.
2091  *
2092  * This type stores the name of an entry in the filesystem and keeps enough
2093  * information that it can provide a complete path.  All fields are
2094  * protected by fn_lock, except for the reference count, which is managed
2095  * using atomic add/subtract.
2096  *
2097  * Additionally shared filehandle for this fname is stored.
2098  * Normally, fn_get() when it creates this fname stores the passed in
2099  * shared fh in fn_sfh by doing sfh_hold. Similarly the path which
2100  * destroys this fname releases the reference on this fh by doing sfh_rele.
2101  *
2102  * fn_get uses the fn_sfh to refine the comparision in cases
2103  * where we have matched the name but have differing file handles,
2104  * this normally happens due to
2105  *
2106  *	1. Server side rename of a file/directory.
2107  *	2. Another client renaming a file/directory on the server.
2108  *
2109  * Differing names but same filehandle is possible as in the case of hardlinks,
2110  * but differing filehandles with same name component will later confuse
2111  * the client and can cause various panics.
2112  *
2113  * Lock order: child and then parent.
2114  */
2115 
2116 typedef struct nfs4_fname {
2117 	struct nfs4_fname *fn_parent;	/* parent name; null if fs root */
2118 	char *fn_name;			/* the actual name */
2119 	ssize_t fn_len;			/* strlen(fn_name) */
2120 	uint32_t fn_refcnt;		/* reference count */
2121 	kmutex_t fn_lock;
2122 	avl_node_t fn_tree;
2123 	avl_tree_t fn_children;		/* children, if any */
2124 	nfs4_sharedfh_t *fn_sfh;	/* The fh for this fname */
2125 } nfs4_fname_t;
2126 
2127 #ifdef _KERNEL
2128 
2129 extern vnode_t	nfs4_xattr_notsupp_vnode;
2130 #define	NFS4_XATTR_DIR_NOTSUPP	&nfs4_xattr_notsupp_vnode
2131 
2132 extern nfs4_fname_t *fn_get(nfs4_fname_t *, char *, nfs4_sharedfh_t *);
2133 extern void fn_hold(nfs4_fname_t *);
2134 extern void fn_rele(nfs4_fname_t **);
2135 extern char *fn_name(nfs4_fname_t *);
2136 extern char *fn_path(nfs4_fname_t *);
2137 extern void fn_move(nfs4_fname_t *, nfs4_fname_t *, char *);
2138 extern nfs4_fname_t *fn_parent(nfs4_fname_t *);
2139 
2140 #endif
2141 
2142 /*
2143  * Per-zone data for managing client handles, included in this file for the
2144  * benefit of MDB.
2145  */
2146 struct nfs4_clnt {
2147 	struct chhead	*nfscl_chtable4;
2148 	kmutex_t	nfscl_chtable4_lock;
2149 	zoneid_t	nfscl_zoneid;
2150 	list_node_t	nfscl_node;
2151 	struct clstat4	nfscl_stat;
2152 };
2153 
2154 #ifdef	__cplusplus
2155 }
2156 #endif
2157 
2158 #endif /* _NFS4_CLNT_H */
2159