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