xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs4_client.c (revision 16ade92d9ce9c9ab33a25f7a2fdd00b581b6efda)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  *  	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
29  *	All Rights Reserved
30  */
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/thread.h>
38 #include <sys/t_lock.h>
39 #include <sys/time.h>
40 #include <sys/vnode.h>
41 #include <sys/vfs.h>
42 #include <sys/errno.h>
43 #include <sys/buf.h>
44 #include <sys/stat.h>
45 #include <sys/cred.h>
46 #include <sys/kmem.h>
47 #include <sys/debug.h>
48 #include <sys/dnlc.h>
49 #include <sys/vmsystm.h>
50 #include <sys/flock.h>
51 #include <sys/share.h>
52 #include <sys/cmn_err.h>
53 #include <sys/tiuser.h>
54 #include <sys/sysmacros.h>
55 #include <sys/callb.h>
56 #include <sys/acl.h>
57 #include <sys/kstat.h>
58 #include <sys/signal.h>
59 #include <sys/disp.h>
60 #include <sys/atomic.h>
61 #include <sys/list.h>
62 #include <sys/sdt.h>
63 
64 #include <rpc/types.h>
65 #include <rpc/xdr.h>
66 #include <rpc/auth.h>
67 #include <rpc/clnt.h>
68 
69 #include <nfs/nfs.h>
70 #include <nfs/nfs_clnt.h>
71 #include <nfs/nfs_acl.h>
72 
73 #include <nfs/nfs4.h>
74 #include <nfs/rnode4.h>
75 #include <nfs/nfs4_clnt.h>
76 
77 #include <vm/hat.h>
78 #include <vm/as.h>
79 #include <vm/page.h>
80 #include <vm/pvn.h>
81 #include <vm/seg.h>
82 #include <vm/seg_map.h>
83 #include <vm/seg_vn.h>
84 
85 #include <sys/ddi.h>
86 
87 /*
88  * Arguments to page-flush thread.
89  */
90 typedef struct {
91 	vnode_t *vp;
92 	cred_t *cr;
93 } pgflush_t;
94 
95 #ifdef DEBUG
96 int nfs4_client_lease_debug;
97 int nfs4_sharedfh_debug;
98 int nfs4_fname_debug;
99 
100 /* temporary: panic if v_type is inconsistent with r_attr va_type */
101 int nfs4_vtype_debug;
102 
103 uint_t nfs4_tsd_key;
104 #endif
105 
106 static time_t	nfs4_client_resumed = 0;
107 static	callb_id_t cid = 0;
108 
109 static int	nfs4renew(nfs4_server_t *);
110 static void	nfs4_attrcache_va(vnode_t *, nfs4_ga_res_t *, int);
111 static void	nfs4_pgflush_thread(pgflush_t *);
112 static void	flush_pages(vnode_t *, cred_t *);
113 
114 static boolean_t nfs4_client_cpr_callb(void *, int);
115 
116 struct mi4_globals {
117 	kmutex_t	mig_lock;  /* lock protecting mig_list */
118 	list_t		mig_list;  /* list of NFS v4 mounts in zone */
119 	boolean_t	mig_destructor_called;
120 };
121 
122 static zone_key_t mi4_list_key;
123 
124 /*
125  * Attributes caching:
126  *
127  * Attributes are cached in the rnode in struct vattr form.
128  * There is a time associated with the cached attributes (r_time_attr_inval)
129  * which tells whether the attributes are valid. The time is initialized
130  * to the difference between current time and the modify time of the vnode
131  * when new attributes are cached. This allows the attributes for
132  * files that have changed recently to be timed out sooner than for files
133  * that have not changed for a long time. There are minimum and maximum
134  * timeout values that can be set per mount point.
135  */
136 
137 /*
138  * If a cache purge is in progress, wait for it to finish.
139  *
140  * The current thread must not be in the middle of an
141  * nfs4_start_op/nfs4_end_op region.  Otherwise, there could be a deadlock
142  * between this thread, a recovery thread, and the page flush thread.
143  */
144 int
145 nfs4_waitfor_purge_complete(vnode_t *vp)
146 {
147 	rnode4_t *rp;
148 	k_sigset_t smask;
149 
150 	rp = VTOR4(vp);
151 	if ((rp->r_serial != NULL && rp->r_serial != curthread) ||
152 	    ((rp->r_flags & R4PGFLUSH) && rp->r_pgflush != curthread)) {
153 		mutex_enter(&rp->r_statelock);
154 		sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT);
155 		while ((rp->r_serial != NULL && rp->r_serial != curthread) ||
156 		    ((rp->r_flags & R4PGFLUSH) &&
157 		    rp->r_pgflush != curthread)) {
158 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
159 				sigunintr(&smask);
160 				mutex_exit(&rp->r_statelock);
161 				return (EINTR);
162 			}
163 		}
164 		sigunintr(&smask);
165 		mutex_exit(&rp->r_statelock);
166 	}
167 	return (0);
168 }
169 
170 /*
171  * Validate caches by checking cached attributes. If they have timed out,
172  * then get new attributes from the server.  As a side effect, cache
173  * invalidation is done if the attributes have changed.
174  *
175  * If the attributes have not timed out and if there is a cache
176  * invalidation being done by some other thread, then wait until that
177  * thread has completed the cache invalidation.
178  */
179 int
180 nfs4_validate_caches(vnode_t *vp, cred_t *cr)
181 {
182 	int error;
183 	nfs4_ga_res_t gar;
184 
185 	if (ATTRCACHE4_VALID(vp)) {
186 		error = nfs4_waitfor_purge_complete(vp);
187 		if (error)
188 			return (error);
189 		return (0);
190 	}
191 
192 	gar.n4g_va.va_mask = AT_ALL;
193 	return (nfs4_getattr_otw(vp, &gar, cr, 0));
194 }
195 
196 /*
197  * Fill in attribute from the cache.
198  * If valid, then return 0 to indicate that no error occurred,
199  * otherwise return 1 to indicate that an error occurred.
200  */
201 static int
202 nfs4_getattr_cache(vnode_t *vp, struct vattr *vap)
203 {
204 	rnode4_t *rp;
205 
206 	rp = VTOR4(vp);
207 	mutex_enter(&rp->r_statelock);
208 	mutex_enter(&rp->r_statev4_lock);
209 	if (ATTRCACHE4_VALID(vp)) {
210 		mutex_exit(&rp->r_statev4_lock);
211 		/*
212 		 * Cached attributes are valid
213 		 */
214 		*vap = rp->r_attr;
215 		mutex_exit(&rp->r_statelock);
216 		return (0);
217 	}
218 	mutex_exit(&rp->r_statev4_lock);
219 	mutex_exit(&rp->r_statelock);
220 	return (1);
221 }
222 
223 
224 /*
225  * If returned error is ESTALE flush all caches.  The nfs4_purge_caches()
226  * call is synchronous because all the pages were invalidated by the
227  * nfs4_invalidate_pages() call.
228  */
229 void
230 nfs4_purge_stale_fh(int errno, vnode_t *vp, cred_t *cr)
231 {
232 	struct rnode4 *rp = VTOR4(vp);
233 
234 	/* Ensure that the ..._end_op() call has been done */
235 	ASSERT(tsd_get(nfs4_tsd_key) == NULL);
236 
237 	if (errno != ESTALE)
238 		return;
239 
240 	mutex_enter(&rp->r_statelock);
241 	rp->r_flags |= R4STALE;
242 	if (!rp->r_error)
243 		rp->r_error = errno;
244 	mutex_exit(&rp->r_statelock);
245 	if (nfs4_has_pages(vp))
246 		nfs4_invalidate_pages(vp, (u_offset_t)0, cr);
247 	nfs4_purge_caches(vp, NFS4_PURGE_DNLC, cr, FALSE);
248 }
249 
250 /*
251  * Purge all of the various NFS `data' caches.  If "asyncpg" is TRUE, the
252  * page purge is done asynchronously.
253  */
254 void
255 nfs4_purge_caches(vnode_t *vp, int purge_dnlc, cred_t *cr, int asyncpg)
256 {
257 	rnode4_t *rp;
258 	char *contents;
259 	vnode_t *xattr;
260 	int size;
261 	int pgflush;			/* are we the page flush thread? */
262 
263 	/*
264 	 * Purge the DNLC for any entries which refer to this file.
265 	 */
266 	if (vp->v_count > 1 &&
267 	    (vp->v_type == VDIR || purge_dnlc == NFS4_PURGE_DNLC))
268 		dnlc_purge_vp(vp);
269 
270 	/*
271 	 * Clear any readdir state bits and purge the readlink response cache.
272 	 */
273 	rp = VTOR4(vp);
274 	mutex_enter(&rp->r_statelock);
275 	rp->r_flags &= ~R4LOOKUP;
276 	contents = rp->r_symlink.contents;
277 	size = rp->r_symlink.size;
278 	rp->r_symlink.contents = NULL;
279 
280 	xattr = rp->r_xattr_dir;
281 	rp->r_xattr_dir = NULL;
282 
283 	/*
284 	 * Purge pathconf cache too.
285 	 */
286 	rp->r_pathconf.pc4_xattr_valid = 0;
287 	rp->r_pathconf.pc4_cache_valid = 0;
288 
289 	pgflush = (curthread == rp->r_pgflush);
290 	mutex_exit(&rp->r_statelock);
291 
292 	if (contents != NULL) {
293 
294 		kmem_free((void *)contents, size);
295 	}
296 
297 	if (xattr != NULL)
298 		VN_RELE(xattr);
299 
300 	/*
301 	 * Flush the page cache.  If the current thread is the page flush
302 	 * thread, don't initiate a new page flush.  There's no need for
303 	 * it, and doing it correctly is hard.
304 	 */
305 	if (nfs4_has_pages(vp) && !pgflush) {
306 		if (!asyncpg) {
307 			(void) nfs4_waitfor_purge_complete(vp);
308 			flush_pages(vp, cr);
309 		} else {
310 			pgflush_t *args;
311 
312 			/*
313 			 * We don't hold r_statelock while creating the
314 			 * thread, in case the call blocks.  So we use a
315 			 * flag to indicate that a page flush thread is
316 			 * active.
317 			 */
318 			mutex_enter(&rp->r_statelock);
319 			if (rp->r_flags & R4PGFLUSH) {
320 				mutex_exit(&rp->r_statelock);
321 			} else {
322 				rp->r_flags |= R4PGFLUSH;
323 				mutex_exit(&rp->r_statelock);
324 
325 				args = kmem_alloc(sizeof (pgflush_t),
326 						KM_SLEEP);
327 				args->vp = vp;
328 				VN_HOLD(args->vp);
329 				args->cr = cr;
330 				crhold(args->cr);
331 				(void) zthread_create(NULL, 0,
332 						nfs4_pgflush_thread, args, 0,
333 						minclsyspri);
334 			}
335 		}
336 	}
337 
338 	/*
339 	 * Flush the readdir response cache.
340 	 */
341 	nfs4_purge_rddir_cache(vp);
342 }
343 
344 /*
345  * Invalidate all pages for the given file, after writing back the dirty
346  * ones.
347  */
348 
349 static void
350 flush_pages(vnode_t *vp, cred_t *cr)
351 {
352 	int error;
353 	rnode4_t *rp = VTOR4(vp);
354 
355 	error = VOP_PUTPAGE(vp, (u_offset_t)0, 0, B_INVAL, cr);
356 	if (error == ENOSPC || error == EDQUOT) {
357 		mutex_enter(&rp->r_statelock);
358 		if (!rp->r_error)
359 			rp->r_error = error;
360 		mutex_exit(&rp->r_statelock);
361 	}
362 }
363 
364 /*
365  * Page flush thread.
366  */
367 
368 static void
369 nfs4_pgflush_thread(pgflush_t *args)
370 {
371 	rnode4_t *rp = VTOR4(args->vp);
372 
373 	/* remember which thread we are, so we don't deadlock ourselves */
374 	mutex_enter(&rp->r_statelock);
375 	ASSERT(rp->r_pgflush == NULL);
376 	rp->r_pgflush = curthread;
377 	mutex_exit(&rp->r_statelock);
378 
379 	flush_pages(args->vp, args->cr);
380 
381 	mutex_enter(&rp->r_statelock);
382 	rp->r_pgflush = NULL;
383 	rp->r_flags &= ~R4PGFLUSH;
384 	cv_broadcast(&rp->r_cv);
385 	mutex_exit(&rp->r_statelock);
386 
387 	VN_RELE(args->vp);
388 	crfree(args->cr);
389 	kmem_free(args, sizeof (pgflush_t));
390 	zthread_exit();
391 }
392 
393 /*
394  * Purge the readdir cache of all entries which are not currently
395  * being filled.
396  */
397 void
398 nfs4_purge_rddir_cache(vnode_t *vp)
399 {
400 	rnode4_t *rp;
401 
402 	rp = VTOR4(vp);
403 
404 	mutex_enter(&rp->r_statelock);
405 	rp->r_direof = NULL;
406 	rp->r_flags &= ~R4LOOKUP;
407 	rp->r_flags |= R4READDIRWATTR;
408 	rddir4_cache_purge(rp);
409 	mutex_exit(&rp->r_statelock);
410 }
411 
412 /*
413  * Set attributes cache for given vnode using virtual attributes.  There is
414  * no cache validation, but if the attributes are deemed to be stale, they
415  * are ignored.  This corresponds to nfs3_attrcache().
416  *
417  * Set the timeout value on the attribute cache and fill it
418  * with the passed in attributes.
419  */
420 void
421 nfs4_attrcache_noinval(vnode_t *vp, nfs4_ga_res_t *garp, hrtime_t t)
422 {
423 	rnode4_t *rp = VTOR4(vp);
424 
425 	mutex_enter(&rp->r_statelock);
426 	if (rp->r_time_attr_saved <= t)
427 		nfs4_attrcache_va(vp, garp, FALSE);
428 	mutex_exit(&rp->r_statelock);
429 }
430 
431 /*
432  * Use the passed in virtual attributes to check to see whether the
433  * data and metadata caches are valid, cache the new attributes, and
434  * then do the cache invalidation if required.
435  *
436  * The cache validation and caching of the new attributes is done
437  * atomically via the use of the mutex, r_statelock.  If required,
438  * the cache invalidation is done atomically w.r.t. the cache
439  * validation and caching of the attributes via the pseudo lock,
440  * r_serial.
441  *
442  * This routine is used to do cache validation and attributes caching
443  * for operations with a single set of post operation attributes.
444  */
445 
446 void
447 nfs4_attr_cache(vnode_t *vp, nfs4_ga_res_t *garp,
448 		hrtime_t t, cred_t *cr, int async,
449 		change_info4 *cinfo)
450 {
451 	rnode4_t *rp;
452 	int mtime_changed;
453 	int ctime_changed;
454 	vsecattr_t *vsp;
455 	int was_serial, set_time_cache_inval, recov;
456 	vattr_t *vap = &garp->n4g_va;
457 	mntinfo4_t *mi = VTOMI4(vp);
458 
459 	ASSERT(mi->mi_vfsp->vfs_dev == garp->n4g_va.va_fsid);
460 
461 	/* Is curthread the recovery thread? */
462 	mutex_enter(&mi->mi_lock);
463 	recov = (VTOMI4(vp)->mi_recovthread == curthread);
464 	mutex_exit(&mi->mi_lock);
465 
466 	rp = VTOR4(vp);
467 	mutex_enter(&rp->r_statelock);
468 	was_serial = (rp->r_serial == curthread);
469 	if (rp->r_serial && !was_serial) {
470 		klwp_t *lwp = ttolwp(curthread);
471 
472 		/*
473 		 * If we're the recovery thread, then purge current attrs
474 		 * and bail out to avoid potential deadlock between another
475 		 * thread caching attrs (r_serial thread), recov thread,
476 		 * and an async writer thread.
477 		 */
478 		if (recov) {
479 			PURGE_ATTRCACHE4_LOCKED(rp);
480 			mutex_exit(&rp->r_statelock);
481 			return;
482 		}
483 
484 		if (lwp != NULL)
485 			lwp->lwp_nostop++;
486 		while (rp->r_serial != NULL) {
487 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
488 				mutex_exit(&rp->r_statelock);
489 				if (lwp != NULL)
490 					lwp->lwp_nostop--;
491 				return;
492 			}
493 		}
494 		if (lwp != NULL)
495 			lwp->lwp_nostop--;
496 	}
497 
498 	/*
499 	 * If there is a page flush thread, the current thread needs to
500 	 * bail out, to prevent a possible deadlock between the current
501 	 * thread (which might be in a start_op/end_op region), the
502 	 * recovery thread, and the page flush thread.  Expire the
503 	 * attribute cache, so that any attributes the current thread was
504 	 * going to set are not lost.
505 	 */
506 	if ((rp->r_flags & R4PGFLUSH) && rp->r_pgflush != curthread) {
507 		PURGE_ATTRCACHE4_LOCKED(rp);
508 		mutex_exit(&rp->r_statelock);
509 		return;
510 	}
511 
512 	if (rp->r_time_attr_saved > t) {
513 		/*
514 		 * Attributes have been cached since these attributes were
515 		 * made, so don't act on them.
516 		 */
517 		mutex_exit(&rp->r_statelock);
518 		return;
519 	}
520 	set_time_cache_inval = 0;
521 	if (cinfo) {
522 		/*
523 		 * Only directory modifying callers pass non-NULL cinfo.
524 		 */
525 		ASSERT(vp->v_type == VDIR);
526 		/*
527 		 * If the cache timeout either doesn't exist or hasn't expired,
528 		 * and dir didn't changed on server before dirmod op
529 		 * and dir didn't change after dirmod op but before getattr
530 		 * then there's a chance that the client's cached data for
531 		 * this object is current (not stale).  No immediate cache
532 		 * flush is required.
533 		 *
534 		 */
535 		if ((! rp->r_time_cache_inval || t < rp->r_time_cache_inval) &&
536 		    cinfo->before == rp->r_change &&
537 		    (garp->n4g_change_valid &&
538 		    cinfo->after == garp->n4g_change)) {
539 
540 			/*
541 			 * If atomic isn't set, then the before/after info
542 			 * cannot be blindly trusted.  For this case, we tell
543 			 * nfs4_attrcache_va to cache the attrs but also
544 			 * establish an absolute maximum cache timeout.  When
545 			 * the timeout is reached, caches will be flushed.
546 			 */
547 			if (! cinfo->atomic)
548 				set_time_cache_inval = 1;
549 
550 			mtime_changed = 0;
551 			ctime_changed = 0;
552 		} else {
553 
554 			/*
555 			 * We're not sure exactly what changed, but we know
556 			 * what to do.  flush all caches for dir.  remove the
557 			 * attr timeout.
558 			 *
559 			 * a) timeout expired.  flush all caches.
560 			 * b) r_change != cinfo.before.  flush all caches.
561 			 * c) r_change == cinfo.before, but cinfo.after !=
562 			 *    post-op getattr(change).  flush all caches.
563 			 * d) post-op getattr(change) not provided by server.
564 			 *    flush all caches.
565 			 */
566 			mtime_changed = 1;
567 			ctime_changed = 1;
568 			rp->r_time_cache_inval = 0;
569 		}
570 	} else {
571 		if (!(rp->r_flags & R4WRITEMODIFIED)) {
572 			if (!CACHE4_VALID(rp, vap->va_mtime, vap->va_size))
573 				mtime_changed = 1;
574 			else
575 				mtime_changed = 0;
576 			if (rp->r_attr.va_ctime.tv_sec !=
577 			    vap->va_ctime.tv_sec ||
578 			    rp->r_attr.va_ctime.tv_nsec !=
579 			    vap->va_ctime.tv_nsec)
580 				ctime_changed = 1;
581 			else
582 				ctime_changed = 0;
583 		} else {
584 			mtime_changed = 0;
585 			ctime_changed = 0;
586 		}
587 	}
588 
589 	nfs4_attrcache_va(vp, garp, set_time_cache_inval);
590 
591 	if (!mtime_changed && !ctime_changed) {
592 		mutex_exit(&rp->r_statelock);
593 		return;
594 	}
595 
596 	rp->r_serial = curthread;
597 
598 	mutex_exit(&rp->r_statelock);
599 
600 	/*
601 	 * If we're the recov thread, then force async nfs4_purge_caches
602 	 * to avoid potential deadlock.
603 	 */
604 	if (mtime_changed)
605 		nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, recov ? 1 : async);
606 
607 	if (ctime_changed) {
608 		(void) nfs4_access_purge_rp(rp);
609 		if (rp->r_secattr != NULL) {
610 			mutex_enter(&rp->r_statelock);
611 			vsp = rp->r_secattr;
612 			rp->r_secattr = NULL;
613 			mutex_exit(&rp->r_statelock);
614 			if (vsp != NULL)
615 				nfs4_acl_free_cache(vsp);
616 		}
617 	}
618 
619 	if (!was_serial) {
620 		mutex_enter(&rp->r_statelock);
621 		rp->r_serial = NULL;
622 		cv_broadcast(&rp->r_cv);
623 		mutex_exit(&rp->r_statelock);
624 	}
625 }
626 
627 /*
628  * Set attributes cache for given vnode using virtual attributes.
629  *
630  * Set the timeout value on the attribute cache and fill it
631  * with the passed in attributes.
632  *
633  * The caller must be holding r_statelock.
634  */
635 static void
636 nfs4_attrcache_va(vnode_t *vp, nfs4_ga_res_t *garp, int set_cache_timeout)
637 {
638 	rnode4_t *rp;
639 	mntinfo4_t *mi;
640 	hrtime_t delta;
641 	hrtime_t now;
642 	vattr_t *vap = &garp->n4g_va;
643 
644 	rp = VTOR4(vp);
645 
646 	ASSERT(MUTEX_HELD(&rp->r_statelock));
647 	ASSERT(vap->va_mask == AT_ALL);
648 
649 	/* Switch to master before checking v_flag */
650 	if (IS_SHADOW(vp, rp))
651 		vp = RTOV4(rp);
652 
653 	now = gethrtime();
654 
655 	mi = VTOMI4(vp);
656 
657 	/*
658 	 * Only establish a new cache timeout (if requested).  Never
659 	 * extend a timeout.  Never clear a timeout.  Clearing a timeout
660 	 * is done by nfs4_update_dircaches (ancestor in our call chain)
661 	 */
662 	if (set_cache_timeout && ! rp->r_time_cache_inval)
663 		rp->r_time_cache_inval = now + mi->mi_acdirmax;
664 
665 	/*
666 	 * Delta is the number of nanoseconds that we will
667 	 * cache the attributes of the file.  It is based on
668 	 * the number of nanoseconds since the last time that
669 	 * we detected a change.  The assumption is that files
670 	 * that changed recently are likely to change again.
671 	 * There is a minimum and a maximum for regular files
672 	 * and for directories which is enforced though.
673 	 *
674 	 * Using the time since last change was detected
675 	 * eliminates direct comparison or calculation
676 	 * using mixed client and server times.  NFS does
677 	 * not make any assumptions regarding the client
678 	 * and server clocks being synchronized.
679 	 */
680 	if (vap->va_mtime.tv_sec != rp->r_attr.va_mtime.tv_sec ||
681 	    vap->va_mtime.tv_nsec != rp->r_attr.va_mtime.tv_nsec ||
682 	    vap->va_size != rp->r_attr.va_size) {
683 		rp->r_time_attr_saved = now;
684 	}
685 
686 	if ((mi->mi_flags & MI4_NOAC) || (vp->v_flag & VNOCACHE))
687 		delta = 0;
688 	else {
689 		delta = now - rp->r_time_attr_saved;
690 		if (vp->v_type == VDIR) {
691 			if (delta < mi->mi_acdirmin)
692 				delta = mi->mi_acdirmin;
693 			else if (delta > mi->mi_acdirmax)
694 				delta = mi->mi_acdirmax;
695 		} else {
696 			if (delta < mi->mi_acregmin)
697 				delta = mi->mi_acregmin;
698 			else if (delta > mi->mi_acregmax)
699 				delta = mi->mi_acregmax;
700 		}
701 	}
702 	rp->r_time_attr_inval = now + delta;
703 
704 	rp->r_attr = *vap;
705 	if (garp->n4g_change_valid)
706 		rp->r_change = garp->n4g_change;
707 
708 	/*
709 	 * The attributes that were returned may be valid and can
710 	 * be used, but they may not be allowed to be cached.
711 	 * Reset the timers to cause immediate invalidation and
712 	 * clear r_change so no VERIFY operations will suceed
713 	 */
714 	if (garp->n4g_attrwhy == NFS4_GETATTR_NOCACHE_OK) {
715 		rp->r_time_attr_inval = now;
716 		rp->r_time_attr_saved = now;
717 		rp->r_change = 0;
718 	}
719 
720 	/*
721 	 * If mounted_on_fileid returned AND the object is a stub,
722 	 * then set object's va_nodeid to the mounted over fid
723 	 * returned by server.
724 	 *
725 	 * If mounted_on_fileid not provided/supported, then
726 	 * just set it to 0 for now.  Eventually it would be
727 	 * better to set it to a hashed version of FH.  This
728 	 * would probably be good enough to provide a unique
729 	 * fid/d_ino within a dir.
730 	 *
731 	 * We don't need to carry mounted_on_fileid in the
732 	 * rnode as long as the client never requests fileid
733 	 * without also requesting mounted_on_fileid.  For
734 	 * now, it stays.
735 	 */
736 	if (garp->n4g_mon_fid_valid) {
737 		rp->r_mntd_fid = garp->n4g_mon_fid;
738 
739 		if (rp->r_flags & R4SRVSTUB)
740 			rp->r_attr.va_nodeid = rp->r_mntd_fid;
741 	}
742 
743 	/*
744 	 * Check to see if there are valid pathconf bits to
745 	 * cache in the rnode.
746 	 */
747 	if (garp->n4g_ext_res) {
748 		if (garp->n4g_ext_res->n4g_pc4.pc4_cache_valid) {
749 			rp->r_pathconf = garp->n4g_ext_res->n4g_pc4;
750 		} else {
751 			if (garp->n4g_ext_res->n4g_pc4.pc4_xattr_valid) {
752 				rp->r_pathconf.pc4_xattr_valid = TRUE;
753 				rp->r_pathconf.pc4_xattr_exists =
754 				    garp->n4g_ext_res->n4g_pc4.pc4_xattr_exists;
755 			}
756 		}
757 	}
758 	/*
759 	 * Update the size of the file if there is no cached data or if
760 	 * the cached data is clean and there is no data being written
761 	 * out.
762 	 */
763 	if (rp->r_size != vap->va_size &&
764 	    (!vn_has_cached_data(vp) ||
765 	    (!(rp->r_flags & R4DIRTY) && rp->r_count == 0))) {
766 		rp->r_size = vap->va_size;
767 	}
768 	nfs_setswaplike(vp, vap);
769 	rp->r_flags &= ~R4WRITEMODIFIED;
770 }
771 
772 /*
773  * Get attributes over-the-wire and update attributes cache
774  * if no error occurred in the over-the-wire operation.
775  * Return 0 if successful, otherwise error.
776  */
777 int
778 nfs4_getattr_otw(vnode_t *vp, nfs4_ga_res_t *garp, cred_t *cr, int get_acl)
779 {
780 	mntinfo4_t *mi = VTOMI4(vp);
781 	hrtime_t t;
782 	nfs4_recov_state_t recov_state;
783 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
784 
785 	recov_state.rs_flags = 0;
786 	recov_state.rs_num_retry_despite_err = 0;
787 
788 	/* Save the original mount point security flavor */
789 	(void) save_mnt_secinfo(mi->mi_curr_serv);
790 
791 recov_retry:
792 	if ((e.error = nfs4_start_fop(mi, vp, NULL, OH_GETATTR,
793 						&recov_state, NULL))) {
794 		(void) check_mnt_secinfo(mi->mi_curr_serv, vp);
795 		return (e.error);
796 	}
797 
798 	t = gethrtime();
799 
800 	nfs4_getattr_otw_norecovery(vp, garp, &e, cr, get_acl);
801 
802 	if (nfs4_needs_recovery(&e, FALSE, vp->v_vfsp)) {
803 		if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
804 		    NULL, OP_GETATTR, NULL) == FALSE)  {
805 			nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_GETATTR,
806 					&recov_state, 1);
807 			goto recov_retry;
808 		}
809 	}
810 
811 	nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_GETATTR, &recov_state, 0);
812 
813 	if (!e.error) {
814 		if (e.stat == NFS4_OK) {
815 			nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL);
816 		} else {
817 			e.error = geterrno4(e.stat);
818 
819 			nfs4_purge_stale_fh(e.error, vp, cr);
820 		}
821 	}
822 
823 	/*
824 	 * If getattr a node that is a stub for a crossed
825 	 * mount point, keep the original secinfo flavor for
826 	 * the current file system, not the crossed one.
827 	 */
828 	(void) check_mnt_secinfo(mi->mi_curr_serv, vp);
829 
830 	return (e.error);
831 }
832 
833 /*
834  * Generate a compound to get attributes over-the-wire.
835  */
836 void
837 nfs4_getattr_otw_norecovery(vnode_t *vp, nfs4_ga_res_t *garp,
838 		nfs4_error_t *ep, cred_t *cr, int get_acl)
839 {
840 	COMPOUND4args_clnt args;
841 	COMPOUND4res_clnt res;
842 	int doqueue;
843 	rnode4_t *rp = VTOR4(vp);
844 	nfs_argop4 argop[2];
845 
846 	args.ctag = TAG_GETATTR;
847 
848 	args.array_len = 2;
849 	args.array = argop;
850 
851 	/* putfh */
852 	argop[0].argop = OP_CPUTFH;
853 	argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
854 
855 	/* getattr */
856 	/*
857 	 * Unlike nfs version 2 and 3, where getattr returns all the
858 	 * attributes, nfs version 4 returns only the ones explicitely
859 	 * asked for. This creates problems, as some system functions
860 	 * (e.g. cache check) require certain attributes and if the
861 	 * cached node lacks some attributes such as uid/gid, it can
862 	 * affect system utilities (e.g. "ls") that rely on the information
863 	 * to be there. This can lead to anything from system crashes to
864 	 * corrupted information processed by user apps.
865 	 * So to ensure that all bases are covered, request at least
866 	 * the AT_ALL attribute mask.
867 	 */
868 	argop[1].argop = OP_GETATTR;
869 	argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
870 	if (get_acl)
871 		argop[1].nfs_argop4_u.opgetattr.attr_request |= FATTR4_ACL_MASK;
872 	argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
873 
874 	doqueue = 1;
875 
876 	rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, ep);
877 
878 	if (ep->error)
879 		return;
880 
881 	if (res.status != NFS4_OK) {
882 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
883 		return;
884 	}
885 
886 	*garp = res.array[1].nfs_resop4_u.opgetattr.ga_res;
887 
888 	(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
889 }
890 
891 /*
892  * Return either cached or remote attributes. If get remote attr
893  * use them to check and invalidate caches, then cache the new attributes.
894  */
895 int
896 nfs4getattr(vnode_t *vp, vattr_t *vap, cred_t *cr)
897 {
898 	int error;
899 	rnode4_t *rp;
900 	nfs4_ga_res_t gar;
901 
902 	ASSERT(nfs4_consistent_type(vp));
903 
904 	/*
905 	 * If we've got cached attributes, we're done, otherwise go
906 	 * to the server to get attributes, which will update the cache
907 	 * in the process.
908 	 */
909 	rp = VTOR4(vp);
910 	mutex_enter(&rp->r_statelock);
911 	mutex_enter(&rp->r_statev4_lock);
912 	if (ATTRCACHE4_VALID(vp)) {
913 		mutex_exit(&rp->r_statev4_lock);
914 		/*
915 		 * Cached attributes are valid
916 		 * Return the client's view of file size
917 		 */
918 		*vap = rp->r_attr;
919 		vap->va_size = rp->r_size;
920 		mutex_exit(&rp->r_statelock);
921 
922 		ASSERT(nfs4_consistent_type(vp));
923 
924 		return (0);
925 	}
926 	mutex_exit(&rp->r_statev4_lock);
927 	mutex_exit(&rp->r_statelock);
928 
929 	error = nfs4_getattr_otw(vp, &gar, cr, 0);
930 	if (!error)
931 		*vap = gar.n4g_va;
932 
933 	/* Return the client's view of file size */
934 	mutex_enter(&rp->r_statelock);
935 	vap->va_size = rp->r_size;
936 	mutex_exit(&rp->r_statelock);
937 
938 	ASSERT(nfs4_consistent_type(vp));
939 
940 	return (error);
941 }
942 
943 int
944 nfs4_attr_otw(vnode_t *vp, nfs4_tag_type_t tag_type,
945 		nfs4_ga_res_t *garp, bitmap4 reqbitmap, cred_t *cr)
946 {
947 	COMPOUND4args_clnt args;
948 	COMPOUND4res_clnt res;
949 	int doqueue;
950 	nfs_argop4 argop[2];
951 	mntinfo4_t *mi = VTOMI4(vp);
952 	bool_t needrecov = FALSE;
953 	nfs4_recov_state_t recov_state;
954 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
955 	nfs4_ga_ext_res_t *gerp;
956 
957 	recov_state.rs_flags = 0;
958 	recov_state.rs_num_retry_despite_err = 0;
959 
960 recov_retry:
961 	args.ctag = tag_type;
962 
963 	args.array_len = 2;
964 	args.array = argop;
965 
966 	e.error = nfs4_start_fop(mi, vp, NULL, OH_GETATTR, &recov_state, NULL);
967 	if (e.error)
968 		return (e.error);
969 
970 	/* putfh */
971 	argop[0].argop = OP_CPUTFH;
972 	argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
973 
974 	/* getattr */
975 	argop[1].argop = OP_GETATTR;
976 	argop[1].nfs_argop4_u.opgetattr.attr_request = reqbitmap;
977 	argop[1].nfs_argop4_u.opgetattr.mi = mi;
978 
979 	doqueue = 1;
980 
981 	NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
982 	    "nfs4_attr_otw: %s call, rp %s", needrecov ? "recov" : "first",
983 	    rnode4info(VTOR4(vp))));
984 
985 	rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
986 
987 	needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
988 	if (!needrecov && e.error) {
989 		nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_GETATTR, &recov_state,
990 			    needrecov);
991 		return (e.error);
992 	}
993 
994 	if (needrecov) {
995 		bool_t abort;
996 
997 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
998 		    "nfs4_attr_otw: initiating recovery\n"));
999 
1000 		abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
1001 			    NULL, OP_GETATTR, NULL);
1002 		nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_GETATTR, &recov_state,
1003 				needrecov);
1004 		if (!e.error) {
1005 			(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1006 			e.error = geterrno4(res.status);
1007 		}
1008 		if (abort == FALSE)
1009 			goto recov_retry;
1010 		return (e.error);
1011 	}
1012 
1013 	if (res.status) {
1014 		e.error = geterrno4(res.status);
1015 	} else {
1016 		gerp = garp->n4g_ext_res;
1017 		bcopy(&res.array[1].nfs_resop4_u.opgetattr.ga_res,
1018 			garp, sizeof (nfs4_ga_res_t));
1019 		garp->n4g_ext_res = gerp;
1020 		if (garp->n4g_ext_res &&
1021 		    res.array[1].nfs_resop4_u.opgetattr.ga_res.n4g_ext_res)
1022 			bcopy(res.array[1].nfs_resop4_u.opgetattr.
1023 				ga_res.n4g_ext_res,
1024 				garp->n4g_ext_res, sizeof (nfs4_ga_ext_res_t));
1025 	}
1026 	(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1027 	nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_GETATTR, &recov_state,
1028 		    needrecov);
1029 	return (e.error);
1030 }
1031 
1032 /*
1033  * Asynchronous I/O parameters.  nfs_async_threads is the high-water mark
1034  * for the demand-based allocation of async threads per-mount.  The
1035  * nfs_async_timeout is the amount of time a thread will live after it
1036  * becomes idle, unless new I/O requests are received before the thread
1037  * dies.  See nfs4_async_putpage and nfs4_async_start.
1038  */
1039 
1040 static void	nfs4_async_start(struct vfs *);
1041 
1042 static void
1043 free_async_args4(struct nfs4_async_reqs *args)
1044 {
1045 	rnode4_t *rp;
1046 
1047 	if (args->a_io != NFS4_INACTIVE) {
1048 		rp = VTOR4(args->a_vp);
1049 		mutex_enter(&rp->r_statelock);
1050 		rp->r_count--;
1051 		if (args->a_io == NFS4_PUTAPAGE ||
1052 		    args->a_io == NFS4_PAGEIO)
1053 			rp->r_awcount--;
1054 		cv_broadcast(&rp->r_cv);
1055 		mutex_exit(&rp->r_statelock);
1056 		VN_RELE(args->a_vp);
1057 	}
1058 	crfree(args->a_cred);
1059 	kmem_free(args, sizeof (*args));
1060 }
1061 
1062 /*
1063  * Cross-zone thread creation and NFS access is disallowed, yet fsflush() and
1064  * pageout(), running in the global zone, have legitimate reasons to do
1065  * VOP_PUTPAGE(B_ASYNC) on other zones' NFS mounts.  We avoid the problem by
1066  * use of a a per-mount "asynchronous requests manager thread" which is
1067  * signaled by the various asynchronous work routines when there is
1068  * asynchronous work to be done.  It is responsible for creating new
1069  * worker threads if necessary, and notifying existing worker threads
1070  * that there is work to be done.
1071  *
1072  * In other words, it will "take the specifications from the customers and
1073  * give them to the engineers."
1074  *
1075  * Worker threads die off of their own accord if they are no longer
1076  * needed.
1077  *
1078  * This thread is killed when the zone is going away or the filesystem
1079  * is being unmounted.
1080  */
1081 void
1082 nfs4_async_manager(vfs_t *vfsp)
1083 {
1084 	callb_cpr_t cprinfo;
1085 	mntinfo4_t *mi;
1086 	uint_t max_threads;
1087 
1088 	mi = VFTOMI4(vfsp);
1089 
1090 	CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr,
1091 		    "nfs4_async_manager");
1092 
1093 	mutex_enter(&mi->mi_async_lock);
1094 	/*
1095 	 * We want to stash the max number of threads that this mount was
1096 	 * allowed so we can use it later when the variable is set to zero as
1097 	 * part of the zone/mount going away.
1098 	 *
1099 	 * We want to be able to create at least one thread to handle
1100 	 * asyncrhonous inactive calls.
1101 	 */
1102 	max_threads = MAX(mi->mi_max_threads, 1);
1103 	mutex_enter(&mi->mi_lock);
1104 	/*
1105 	 * We don't want to wait for mi_max_threads to go to zero, since that
1106 	 * happens as part of a failed unmount, but this thread should only
1107 	 * exit when the mount is really going away.
1108 	 *
1109 	 * Once MI4_ASYNC_MGR_STOP is set, no more async operations will be
1110 	 * attempted: the various _async_*() functions know to do things
1111 	 * inline if mi_max_threads == 0.  Henceforth we just drain out the
1112 	 * outstanding requests.
1113 	 *
1114 	 * Note that we still create zthreads even if we notice the zone is
1115 	 * shutting down (MI4_ASYNC_MGR_STOP is set); this may cause the zone
1116 	 * shutdown sequence to take slightly longer in some cases, but
1117 	 * doesn't violate the protocol, as all threads will exit as soon as
1118 	 * they're done processing the remaining requests.
1119 	 */
1120 	while (!(mi->mi_flags & MI4_ASYNC_MGR_STOP) ||
1121 	    mi->mi_async_req_count > 0) {
1122 		mutex_exit(&mi->mi_lock);
1123 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
1124 		cv_wait(&mi->mi_async_reqs_cv, &mi->mi_async_lock);
1125 		CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock);
1126 		while (mi->mi_async_req_count > 0) {
1127 			/*
1128 			 * Paranoia: If the mount started out having
1129 			 * (mi->mi_max_threads == 0), and the value was
1130 			 * later changed (via a debugger or somesuch),
1131 			 * we could be confused since we will think we
1132 			 * can't create any threads, and the calling
1133 			 * code (which looks at the current value of
1134 			 * mi->mi_max_threads, now non-zero) thinks we
1135 			 * can.
1136 			 *
1137 			 * So, because we're paranoid, we create threads
1138 			 * up to the maximum of the original and the
1139 			 * current value. This means that future
1140 			 * (debugger-induced) alterations of
1141 			 * mi->mi_max_threads are ignored for our
1142 			 * purposes, but who told them they could change
1143 			 * random values on a live kernel anyhow?
1144 			 */
1145 			if (mi->mi_threads <
1146 			    MAX(mi->mi_max_threads, max_threads)) {
1147 				mi->mi_threads++;
1148 				mutex_exit(&mi->mi_async_lock);
1149 				VFS_HOLD(vfsp);	/* hold for new thread */
1150 				(void) zthread_create(NULL, 0, nfs4_async_start,
1151 				    vfsp, 0, minclsyspri);
1152 				mutex_enter(&mi->mi_async_lock);
1153 			}
1154 			cv_signal(&mi->mi_async_work_cv);
1155 			ASSERT(mi->mi_async_req_count != 0);
1156 			mi->mi_async_req_count--;
1157 		}
1158 		mutex_enter(&mi->mi_lock);
1159 	}
1160 	mutex_exit(&mi->mi_lock);
1161 
1162 	NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE,
1163 	    "nfs4_async_manager exiting for vfs %p\n", (void *)mi->mi_vfsp));
1164 	/*
1165 	 * Let everyone know we're done.
1166 	 */
1167 	mi->mi_manager_thread = NULL;
1168 	/*
1169 	 * Wake up the inactive thread.
1170 	 */
1171 	cv_broadcast(&mi->mi_inact_req_cv);
1172 	/*
1173 	 * Wake up anyone sitting in nfs4_async_manager_stop()
1174 	 */
1175 	cv_broadcast(&mi->mi_async_cv);
1176 	/*
1177 	 * There is no explicit call to mutex_exit(&mi->mi_async_lock)
1178 	 * since CALLB_CPR_EXIT is actually responsible for releasing
1179 	 * 'mi_async_lock'.
1180 	 */
1181 	CALLB_CPR_EXIT(&cprinfo);
1182 	VFS_RELE(vfsp);	/* release thread's hold */
1183 	zthread_exit();
1184 }
1185 
1186 /*
1187  * Signal (and wait for) the async manager thread to clean up and go away.
1188  */
1189 void
1190 nfs4_async_manager_stop(vfs_t *vfsp)
1191 {
1192 	mntinfo4_t *mi = VFTOMI4(vfsp);
1193 
1194 	mutex_enter(&mi->mi_async_lock);
1195 	mutex_enter(&mi->mi_lock);
1196 	mi->mi_flags |= MI4_ASYNC_MGR_STOP;
1197 	mutex_exit(&mi->mi_lock);
1198 	cv_broadcast(&mi->mi_async_reqs_cv);
1199 	/*
1200 	 * Wait for the async manager thread to die.
1201 	 */
1202 	while (mi->mi_manager_thread != NULL)
1203 		cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
1204 	mutex_exit(&mi->mi_async_lock);
1205 }
1206 
1207 int
1208 nfs4_async_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr,
1209 	struct seg *seg, cred_t *cr, void (*readahead)(vnode_t *,
1210 	u_offset_t, caddr_t, struct seg *, cred_t *))
1211 {
1212 	rnode4_t *rp;
1213 	mntinfo4_t *mi;
1214 	struct nfs4_async_reqs *args;
1215 
1216 	rp = VTOR4(vp);
1217 	ASSERT(rp->r_freef == NULL);
1218 
1219 	mi = VTOMI4(vp);
1220 
1221 	/*
1222 	 * If addr falls in a different segment, don't bother doing readahead.
1223 	 */
1224 	if (addr >= seg->s_base + seg->s_size)
1225 		return (-1);
1226 
1227 	/*
1228 	 * If we can't allocate a request structure, punt on the readahead.
1229 	 */
1230 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1231 		return (-1);
1232 
1233 	/*
1234 	 * If a lock operation is pending, don't initiate any new
1235 	 * readaheads.  Otherwise, bump r_count to indicate the new
1236 	 * asynchronous I/O.
1237 	 */
1238 	if (!nfs_rw_tryenter(&rp->r_lkserlock, RW_READER)) {
1239 		kmem_free(args, sizeof (*args));
1240 		return (-1);
1241 	}
1242 	mutex_enter(&rp->r_statelock);
1243 	rp->r_count++;
1244 	mutex_exit(&rp->r_statelock);
1245 	nfs_rw_exit(&rp->r_lkserlock);
1246 
1247 	args->a_next = NULL;
1248 #ifdef DEBUG
1249 	args->a_queuer = curthread;
1250 #endif
1251 	VN_HOLD(vp);
1252 	args->a_vp = vp;
1253 	ASSERT(cr != NULL);
1254 	crhold(cr);
1255 	args->a_cred = cr;
1256 	args->a_io = NFS4_READ_AHEAD;
1257 	args->a_nfs4_readahead = readahead;
1258 	args->a_nfs4_blkoff = blkoff;
1259 	args->a_nfs4_seg = seg;
1260 	args->a_nfs4_addr = addr;
1261 
1262 	mutex_enter(&mi->mi_async_lock);
1263 
1264 	/*
1265 	 * If asyncio has been disabled, don't bother readahead.
1266 	 */
1267 	if (mi->mi_max_threads == 0) {
1268 		mutex_exit(&mi->mi_async_lock);
1269 		goto noasync;
1270 	}
1271 
1272 	/*
1273 	 * Link request structure into the async list and
1274 	 * wakeup async thread to do the i/o.
1275 	 */
1276 	if (mi->mi_async_reqs[NFS4_READ_AHEAD] == NULL) {
1277 		mi->mi_async_reqs[NFS4_READ_AHEAD] = args;
1278 		mi->mi_async_tail[NFS4_READ_AHEAD] = args;
1279 	} else {
1280 		mi->mi_async_tail[NFS4_READ_AHEAD]->a_next = args;
1281 		mi->mi_async_tail[NFS4_READ_AHEAD] = args;
1282 	}
1283 
1284 	if (mi->mi_io_kstats) {
1285 		mutex_enter(&mi->mi_lock);
1286 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1287 		mutex_exit(&mi->mi_lock);
1288 	}
1289 
1290 	mi->mi_async_req_count++;
1291 	ASSERT(mi->mi_async_req_count != 0);
1292 	cv_signal(&mi->mi_async_reqs_cv);
1293 	mutex_exit(&mi->mi_async_lock);
1294 	return (0);
1295 
1296 noasync:
1297 	mutex_enter(&rp->r_statelock);
1298 	rp->r_count--;
1299 	cv_broadcast(&rp->r_cv);
1300 	mutex_exit(&rp->r_statelock);
1301 	VN_RELE(vp);
1302 	crfree(cr);
1303 	kmem_free(args, sizeof (*args));
1304 	return (-1);
1305 }
1306 
1307 /*
1308  * The async queues for each mounted file system are arranged as a
1309  * set of queues, one for each async i/o type.  Requests are taken
1310  * from the queues in a round-robin fashion.  A number of consecutive
1311  * requests are taken from each queue before moving on to the next
1312  * queue.  This functionality may allow the NFS Version 2 server to do
1313  * write clustering, even if the client is mixing writes and reads
1314  * because it will take multiple write requests from the queue
1315  * before processing any of the other async i/o types.
1316  *
1317  * XXX The nfs4_async_start thread is unsafe in the light of the present
1318  * model defined by cpr to suspend the system. Specifically over the
1319  * wire calls are cpr-unsafe. The thread should be reevaluated in
1320  * case of future updates to the cpr model.
1321  */
1322 static void
1323 nfs4_async_start(struct vfs *vfsp)
1324 {
1325 	struct nfs4_async_reqs *args;
1326 	mntinfo4_t *mi = VFTOMI4(vfsp);
1327 	clock_t time_left = 1;
1328 	callb_cpr_t cprinfo;
1329 	int i;
1330 	extern int nfs_async_timeout;
1331 
1332 	/*
1333 	 * Dynamic initialization of nfs_async_timeout to allow nfs to be
1334 	 * built in an implementation independent manner.
1335 	 */
1336 	if (nfs_async_timeout == -1)
1337 		nfs_async_timeout = NFS_ASYNC_TIMEOUT;
1338 
1339 	CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr, "nas");
1340 
1341 	mutex_enter(&mi->mi_async_lock);
1342 	for (;;) {
1343 		/*
1344 		 * Find the next queue containing an entry.  We start
1345 		 * at the current queue pointer and then round robin
1346 		 * through all of them until we either find a non-empty
1347 		 * queue or have looked through all of them.
1348 		 */
1349 		for (i = 0; i < NFS4_ASYNC_TYPES; i++) {
1350 			args = *mi->mi_async_curr;
1351 			if (args != NULL)
1352 				break;
1353 			mi->mi_async_curr++;
1354 			if (mi->mi_async_curr ==
1355 			    &mi->mi_async_reqs[NFS4_ASYNC_TYPES])
1356 				mi->mi_async_curr = &mi->mi_async_reqs[0];
1357 		}
1358 		/*
1359 		 * If we didn't find a entry, then block until woken up
1360 		 * again and then look through the queues again.
1361 		 */
1362 		if (args == NULL) {
1363 			/*
1364 			 * Exiting is considered to be safe for CPR as well
1365 			 */
1366 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
1367 
1368 			/*
1369 			 * Wakeup thread waiting to unmount the file
1370 			 * system only if all async threads are inactive.
1371 			 *
1372 			 * If we've timed-out and there's nothing to do,
1373 			 * then get rid of this thread.
1374 			 */
1375 			if (mi->mi_max_threads == 0 || time_left <= 0) {
1376 				if (--mi->mi_threads == 0)
1377 					cv_signal(&mi->mi_async_cv);
1378 				CALLB_CPR_EXIT(&cprinfo);
1379 				VFS_RELE(vfsp);	/* release thread's hold */
1380 				zthread_exit();
1381 				/* NOTREACHED */
1382 			}
1383 			time_left = cv_timedwait(&mi->mi_async_work_cv,
1384 			    &mi->mi_async_lock, nfs_async_timeout + lbolt);
1385 
1386 			CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock);
1387 
1388 			continue;
1389 		} else {
1390 			time_left = 1;
1391 		}
1392 
1393 		/*
1394 		 * Remove the request from the async queue and then
1395 		 * update the current async request queue pointer.  If
1396 		 * the current queue is empty or we have removed enough
1397 		 * consecutive entries from it, then reset the counter
1398 		 * for this queue and then move the current pointer to
1399 		 * the next queue.
1400 		 */
1401 		*mi->mi_async_curr = args->a_next;
1402 		if (*mi->mi_async_curr == NULL ||
1403 		    --mi->mi_async_clusters[args->a_io] == 0) {
1404 			mi->mi_async_clusters[args->a_io] =
1405 						mi->mi_async_init_clusters;
1406 			mi->mi_async_curr++;
1407 			if (mi->mi_async_curr ==
1408 			    &mi->mi_async_reqs[NFS4_ASYNC_TYPES])
1409 				mi->mi_async_curr = &mi->mi_async_reqs[0];
1410 		}
1411 
1412 		if (args->a_io != NFS4_INACTIVE && mi->mi_io_kstats) {
1413 			mutex_enter(&mi->mi_lock);
1414 			kstat_waitq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
1415 			mutex_exit(&mi->mi_lock);
1416 		}
1417 
1418 		mutex_exit(&mi->mi_async_lock);
1419 
1420 		/*
1421 		 * Obtain arguments from the async request structure.
1422 		 */
1423 		if (args->a_io == NFS4_READ_AHEAD && mi->mi_max_threads > 0) {
1424 			(*args->a_nfs4_readahead)(args->a_vp,
1425 					args->a_nfs4_blkoff,
1426 					args->a_nfs4_addr, args->a_nfs4_seg,
1427 					args->a_cred);
1428 		} else if (args->a_io == NFS4_PUTAPAGE) {
1429 			(void) (*args->a_nfs4_putapage)(args->a_vp,
1430 					args->a_nfs4_pp, args->a_nfs4_off,
1431 					args->a_nfs4_len, args->a_nfs4_flags,
1432 					args->a_cred);
1433 		} else if (args->a_io == NFS4_PAGEIO) {
1434 			(void) (*args->a_nfs4_pageio)(args->a_vp,
1435 					args->a_nfs4_pp, args->a_nfs4_off,
1436 					args->a_nfs4_len, args->a_nfs4_flags,
1437 					args->a_cred);
1438 		} else if (args->a_io == NFS4_READDIR) {
1439 			(void) ((*args->a_nfs4_readdir)(args->a_vp,
1440 					args->a_nfs4_rdc, args->a_cred));
1441 		} else if (args->a_io == NFS4_COMMIT) {
1442 			(*args->a_nfs4_commit)(args->a_vp, args->a_nfs4_plist,
1443 					args->a_nfs4_offset, args->a_nfs4_count,
1444 					args->a_cred);
1445 		} else if (args->a_io == NFS4_INACTIVE) {
1446 			nfs4_inactive_otw(args->a_vp, args->a_cred);
1447 		}
1448 
1449 		/*
1450 		 * Now, release the vnode and free the credentials
1451 		 * structure.
1452 		 */
1453 		free_async_args4(args);
1454 		/*
1455 		 * Reacquire the mutex because it will be needed above.
1456 		 */
1457 		mutex_enter(&mi->mi_async_lock);
1458 	}
1459 }
1460 
1461 /*
1462  * nfs4_inactive_thread - look for vnodes that need over-the-wire calls as
1463  * part of VOP_INACTIVE.
1464  */
1465 
1466 void
1467 nfs4_inactive_thread(mntinfo4_t *mi)
1468 {
1469 	struct nfs4_async_reqs *args;
1470 	callb_cpr_t cprinfo;
1471 	int call_nfs_free_mi4 = 0;
1472 	vfs_t *vfsp = mi->mi_vfsp;
1473 
1474 	CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr,
1475 		    "nfs4_inactive_thread");
1476 
1477 	for (;;) {
1478 		mutex_enter(&mi->mi_async_lock);
1479 		args = mi->mi_async_reqs[NFS4_INACTIVE];
1480 		if (args == NULL) {
1481 			mutex_enter(&mi->mi_lock);
1482 			/*
1483 			 * During regular operation (ie, unmount
1484 			 * or a failed mount), the async manager thread always
1485 			 * exits before MI4_DEAD is set by nfs_free_mi4().
1486 			 *
1487 			 * When a zone is shutting down, however, we set
1488 			 * MI4_DEAD before the async manager thread is done, and
1489 			 * we don't want to exit until the async manager is done
1490 			 * with its work; hence the check for mi_manager_thread
1491 			 * being NULL.
1492 			 *
1493 			 * The async manager thread will cv_broadcast() on
1494 			 * mi_inact_req_cv when it's done, at which point we'll
1495 			 * wake up and exit.
1496 			 */
1497 			if (mi->mi_manager_thread == NULL &&
1498 			    (mi->mi_flags & MI4_DEAD))
1499 				goto die;
1500 			mi->mi_flags |= MI4_INACTIVE_IDLE;
1501 			mutex_exit(&mi->mi_lock);
1502 			cv_signal(&mi->mi_async_cv);
1503 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
1504 			cv_wait(&mi->mi_inact_req_cv, &mi->mi_async_lock);
1505 			CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock);
1506 			mutex_exit(&mi->mi_async_lock);
1507 		} else {
1508 			mutex_enter(&mi->mi_lock);
1509 			mi->mi_flags &= ~MI4_INACTIVE_IDLE;
1510 			mutex_exit(&mi->mi_lock);
1511 			mi->mi_async_reqs[NFS4_INACTIVE] = args->a_next;
1512 			mutex_exit(&mi->mi_async_lock);
1513 			nfs4_inactive_otw(args->a_vp, args->a_cred);
1514 			crfree(args->a_cred);
1515 			kmem_free(args, sizeof (*args));
1516 		}
1517 	}
1518 die:
1519 	mutex_exit(&mi->mi_lock);
1520 	call_nfs_free_mi4 = (mi->mi_inactive_thread == NULL);
1521 	mi->mi_inactive_thread = NULL;
1522 	cv_signal(&mi->mi_async_cv);
1523 	/*
1524 	 * There is no explicit call to mutex_exit(&mi->mi_async_lock) since
1525 	 * CALLB_CPR_EXIT is actually responsible for releasing 'mi_async_lock'.
1526 	 */
1527 	CALLB_CPR_EXIT(&cprinfo);
1528 	if (call_nfs_free_mi4) {
1529 		if (mi->mi_io_kstats) {
1530 			kstat_delete(mi->mi_io_kstats);
1531 			mi->mi_io_kstats = NULL;
1532 		}
1533 		if (mi->mi_ro_kstats) {
1534 			kstat_delete(mi->mi_ro_kstats);
1535 			mi->mi_ro_kstats = NULL;
1536 		}
1537 		if (mi->mi_recov_ksp) {
1538 			kstat_delete(mi->mi_recov_ksp);
1539 			mi->mi_recov_ksp = NULL;
1540 		}
1541 		nfs_free_mi4(mi);
1542 	}
1543 	NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE,
1544 	    "nfs4_inactive_thread exiting for vfs %p\n", (void *)vfsp));
1545 	zthread_exit();
1546 	/* NOTREACHED */
1547 }
1548 
1549 /*
1550  * nfs_async_stop:
1551  * Wait for all outstanding putpage operations and the inactive thread to
1552  * complete; nfs4_async_stop_sig() without interruptibility.
1553  */
1554 void
1555 nfs4_async_stop(struct vfs *vfsp)
1556 {
1557 	mntinfo4_t *mi = VFTOMI4(vfsp);
1558 
1559 	/*
1560 	 * Wait for all outstanding async operations to complete and for
1561 	 * worker threads to exit.
1562 	 */
1563 	mutex_enter(&mi->mi_async_lock);
1564 	mi->mi_max_threads = 0;
1565 	cv_broadcast(&mi->mi_async_work_cv);
1566 	while (mi->mi_threads != 0)
1567 		cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
1568 
1569 	/*
1570 	 * Wait for the inactive thread to finish doing what it's doing.  It
1571 	 * won't exit until the last reference to the vfs_t goes away.
1572 	 */
1573 	if (mi->mi_inactive_thread != NULL) {
1574 		mutex_enter(&mi->mi_lock);
1575 		while (!(mi->mi_flags & MI4_INACTIVE_IDLE) ||
1576 		    (mi->mi_async_reqs[NFS4_INACTIVE] != NULL)) {
1577 			mutex_exit(&mi->mi_lock);
1578 			cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
1579 			mutex_enter(&mi->mi_lock);
1580 		}
1581 		mutex_exit(&mi->mi_lock);
1582 	}
1583 	mutex_exit(&mi->mi_async_lock);
1584 }
1585 
1586 /*
1587  * nfs_async_stop_sig:
1588  * Wait for all outstanding putpage operations and the inactive thread to
1589  * complete. If a signal is delivered we will abort and return non-zero;
1590  * otherwise return 0. Since this routine is called from nfs4_unmount, we
1591  * need to make it interruptable.
1592  */
1593 int
1594 nfs4_async_stop_sig(struct vfs *vfsp)
1595 {
1596 	mntinfo4_t *mi = VFTOMI4(vfsp);
1597 	ushort_t omax;
1598 	bool_t intr = FALSE;
1599 
1600 	/*
1601 	 * Wait for all outstanding putpage operations to complete and for
1602 	 * worker threads to exit.
1603 	 */
1604 	mutex_enter(&mi->mi_async_lock);
1605 	omax = mi->mi_max_threads;
1606 	mi->mi_max_threads = 0;
1607 	cv_broadcast(&mi->mi_async_work_cv);
1608 	while (mi->mi_threads != 0) {
1609 		if (!cv_wait_sig(&mi->mi_async_cv, &mi->mi_async_lock)) {
1610 			intr = TRUE;
1611 			goto interrupted;
1612 		}
1613 	}
1614 
1615 	/*
1616 	 * Wait for the inactive thread to finish doing what it's doing.  It
1617 	 * won't exit until the a last reference to the vfs_t goes away.
1618 	 */
1619 	if (mi->mi_inactive_thread != NULL) {
1620 		mutex_enter(&mi->mi_lock);
1621 		while (!(mi->mi_flags & MI4_INACTIVE_IDLE) ||
1622 		    (mi->mi_async_reqs[NFS4_INACTIVE] != NULL)) {
1623 			mutex_exit(&mi->mi_lock);
1624 			if (!cv_wait_sig(&mi->mi_async_cv,
1625 			    &mi->mi_async_lock)) {
1626 				intr = TRUE;
1627 				goto interrupted;
1628 			}
1629 			mutex_enter(&mi->mi_lock);
1630 		}
1631 		mutex_exit(&mi->mi_lock);
1632 	}
1633 interrupted:
1634 	if (intr)
1635 		mi->mi_max_threads = omax;
1636 	mutex_exit(&mi->mi_async_lock);
1637 
1638 	return (intr);
1639 }
1640 
1641 int
1642 nfs4_async_putapage(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
1643 	int flags, cred_t *cr, int (*putapage)(vnode_t *, page_t *,
1644 	u_offset_t, size_t, int, cred_t *))
1645 {
1646 	rnode4_t *rp;
1647 	mntinfo4_t *mi;
1648 	struct nfs4_async_reqs *args;
1649 
1650 	ASSERT(flags & B_ASYNC);
1651 	ASSERT(vp->v_vfsp != NULL);
1652 
1653 	rp = VTOR4(vp);
1654 	ASSERT(rp->r_count > 0);
1655 
1656 	mi = VTOMI4(vp);
1657 
1658 	/*
1659 	 * If we can't allocate a request structure, do the putpage
1660 	 * operation synchronously in this thread's context.
1661 	 */
1662 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1663 		goto noasync;
1664 
1665 	args->a_next = NULL;
1666 #ifdef DEBUG
1667 	args->a_queuer = curthread;
1668 #endif
1669 	VN_HOLD(vp);
1670 	args->a_vp = vp;
1671 	ASSERT(cr != NULL);
1672 	crhold(cr);
1673 	args->a_cred = cr;
1674 	args->a_io = NFS4_PUTAPAGE;
1675 	args->a_nfs4_putapage = putapage;
1676 	args->a_nfs4_pp = pp;
1677 	args->a_nfs4_off = off;
1678 	args->a_nfs4_len = (uint_t)len;
1679 	args->a_nfs4_flags = flags;
1680 
1681 	mutex_enter(&mi->mi_async_lock);
1682 
1683 	/*
1684 	 * If asyncio has been disabled, then make a synchronous request.
1685 	 * This check is done a second time in case async io was diabled
1686 	 * while this thread was blocked waiting for memory pressure to
1687 	 * reduce or for the queue to drain.
1688 	 */
1689 	if (mi->mi_max_threads == 0) {
1690 		mutex_exit(&mi->mi_async_lock);
1691 
1692 		VN_RELE(vp);
1693 		crfree(cr);
1694 		kmem_free(args, sizeof (*args));
1695 		goto noasync;
1696 	}
1697 
1698 	/*
1699 	 * Link request structure into the async list and
1700 	 * wakeup async thread to do the i/o.
1701 	 */
1702 	if (mi->mi_async_reqs[NFS4_PUTAPAGE] == NULL) {
1703 		mi->mi_async_reqs[NFS4_PUTAPAGE] = args;
1704 		mi->mi_async_tail[NFS4_PUTAPAGE] = args;
1705 	} else {
1706 		mi->mi_async_tail[NFS4_PUTAPAGE]->a_next = args;
1707 		mi->mi_async_tail[NFS4_PUTAPAGE] = args;
1708 	}
1709 
1710 	mutex_enter(&rp->r_statelock);
1711 	rp->r_count++;
1712 	rp->r_awcount++;
1713 	mutex_exit(&rp->r_statelock);
1714 
1715 	if (mi->mi_io_kstats) {
1716 		mutex_enter(&mi->mi_lock);
1717 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1718 		mutex_exit(&mi->mi_lock);
1719 	}
1720 
1721 	mi->mi_async_req_count++;
1722 	ASSERT(mi->mi_async_req_count != 0);
1723 	cv_signal(&mi->mi_async_reqs_cv);
1724 	mutex_exit(&mi->mi_async_lock);
1725 	return (0);
1726 
1727 noasync:
1728 
1729 	if (curproc == proc_pageout || curproc == proc_fsflush ||
1730 	    curproc->p_zone == mi->mi_zone) {
1731 		/*
1732 		 * If we get here in the context of the pageout/fsflush,
1733 		 * or we have run out of memory or we're attempting to
1734 		 * unmount we refuse to do a sync write, because this may
1735 		 * hang pageout/fsflush and the machine. In this case,
1736 		 * we just re-mark the page as dirty and punt on the page.
1737 		 *
1738 		 * Make sure B_FORCE isn't set.  We can re-mark the
1739 		 * pages as dirty and unlock the pages in one swoop by
1740 		 * passing in B_ERROR to pvn_write_done().  However,
1741 		 * we should make sure B_FORCE isn't set - we don't
1742 		 * want the page tossed before it gets written out.
1743 		 */
1744 		if (flags & B_FORCE)
1745 			flags &= ~(B_INVAL | B_FORCE);
1746 		pvn_write_done(pp, flags | B_ERROR);
1747 		return (0);
1748 	}
1749 
1750 	/*
1751 	 * We'll get here only if (curproc->p_zone != mi->mi_zone)
1752 	 * which means that this was a cross-zone sync putpage.
1753 	 *
1754 	 * We pass in B_ERROR to pvn_write_done() to re-mark the pages
1755 	 * as dirty and unlock them.
1756 	 *
1757 	 * We don't want to clear B_FORCE here as the caller presumably
1758 	 * knows what they're doing if they set it.
1759 	 */
1760 	pvn_write_done(pp, flags | B_ERROR);
1761 	return (EPERM);
1762 }
1763 
1764 int
1765 nfs4_async_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
1766 	int flags, cred_t *cr, int (*pageio)(vnode_t *, page_t *, u_offset_t,
1767 	size_t, int, cred_t *))
1768 {
1769 	rnode4_t *rp;
1770 	mntinfo4_t *mi;
1771 	struct nfs4_async_reqs *args;
1772 
1773 	ASSERT(flags & B_ASYNC);
1774 	ASSERT(vp->v_vfsp != NULL);
1775 
1776 	rp = VTOR4(vp);
1777 	ASSERT(rp->r_count > 0);
1778 
1779 	mi = VTOMI4(vp);
1780 
1781 	/*
1782 	 * If we can't allocate a request structure, do the pageio
1783 	 * request synchronously in this thread's context.
1784 	 */
1785 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1786 		goto noasync;
1787 
1788 	args->a_next = NULL;
1789 #ifdef DEBUG
1790 	args->a_queuer = curthread;
1791 #endif
1792 	VN_HOLD(vp);
1793 	args->a_vp = vp;
1794 	ASSERT(cr != NULL);
1795 	crhold(cr);
1796 	args->a_cred = cr;
1797 	args->a_io = NFS4_PAGEIO;
1798 	args->a_nfs4_pageio = pageio;
1799 	args->a_nfs4_pp = pp;
1800 	args->a_nfs4_off = io_off;
1801 	args->a_nfs4_len = (uint_t)io_len;
1802 	args->a_nfs4_flags = flags;
1803 
1804 	mutex_enter(&mi->mi_async_lock);
1805 
1806 	/*
1807 	 * If asyncio has been disabled, then make a synchronous request.
1808 	 * This check is done a second time in case async io was diabled
1809 	 * while this thread was blocked waiting for memory pressure to
1810 	 * reduce or for the queue to drain.
1811 	 */
1812 	if (mi->mi_max_threads == 0) {
1813 		mutex_exit(&mi->mi_async_lock);
1814 
1815 		VN_RELE(vp);
1816 		crfree(cr);
1817 		kmem_free(args, sizeof (*args));
1818 		goto noasync;
1819 	}
1820 
1821 	/*
1822 	 * Link request structure into the async list and
1823 	 * wakeup async thread to do the i/o.
1824 	 */
1825 	if (mi->mi_async_reqs[NFS4_PAGEIO] == NULL) {
1826 		mi->mi_async_reqs[NFS4_PAGEIO] = args;
1827 		mi->mi_async_tail[NFS4_PAGEIO] = args;
1828 	} else {
1829 		mi->mi_async_tail[NFS4_PAGEIO]->a_next = args;
1830 		mi->mi_async_tail[NFS4_PAGEIO] = args;
1831 	}
1832 
1833 	mutex_enter(&rp->r_statelock);
1834 	rp->r_count++;
1835 	rp->r_awcount++;
1836 	mutex_exit(&rp->r_statelock);
1837 
1838 	if (mi->mi_io_kstats) {
1839 		mutex_enter(&mi->mi_lock);
1840 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1841 		mutex_exit(&mi->mi_lock);
1842 	}
1843 
1844 	mi->mi_async_req_count++;
1845 	ASSERT(mi->mi_async_req_count != 0);
1846 	cv_signal(&mi->mi_async_reqs_cv);
1847 	mutex_exit(&mi->mi_async_lock);
1848 	return (0);
1849 
1850 noasync:
1851 	/*
1852 	 * If we can't do it ASYNC, for reads we do nothing (but cleanup
1853 	 * the page list), for writes we do it synchronously, except for
1854 	 * proc_pageout/proc_fsflush as described below.
1855 	 */
1856 	if (flags & B_READ) {
1857 		pvn_read_done(pp, flags | B_ERROR);
1858 		return (0);
1859 	}
1860 
1861 	if (curproc == proc_pageout || curproc == proc_fsflush) {
1862 		/*
1863 		 * If we get here in the context of the pageout/fsflush,
1864 		 * we refuse to do a sync write, because this may hang
1865 		 * pageout/fsflush (and the machine). In this case, we just
1866 		 * re-mark the page as dirty and punt on the page.
1867 		 *
1868 		 * Make sure B_FORCE isn't set.  We can re-mark the
1869 		 * pages as dirty and unlock the pages in one swoop by
1870 		 * passing in B_ERROR to pvn_write_done().  However,
1871 		 * we should make sure B_FORCE isn't set - we don't
1872 		 * want the page tossed before it gets written out.
1873 		 */
1874 		if (flags & B_FORCE)
1875 			flags &= ~(B_INVAL | B_FORCE);
1876 		pvn_write_done(pp, flags | B_ERROR);
1877 		return (0);
1878 	}
1879 
1880 	if (curproc->p_zone != mi->mi_zone) {
1881 		/*
1882 		 * So this was a cross-zone sync pageio.  We pass in B_ERROR
1883 		 * to pvn_write_done() to re-mark the pages as dirty and unlock
1884 		 * them.
1885 		 *
1886 		 * We don't want to clear B_FORCE here as the caller presumably
1887 		 * knows what they're doing if they set it.
1888 		 */
1889 		pvn_write_done(pp, flags | B_ERROR);
1890 		return (EPERM);
1891 	}
1892 	return ((*pageio)(vp, pp, io_off, io_len, flags, cr));
1893 }
1894 
1895 void
1896 nfs4_async_readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr,
1897 	int (*readdir)(vnode_t *, rddir4_cache *, cred_t *))
1898 {
1899 	rnode4_t *rp;
1900 	mntinfo4_t *mi;
1901 	struct nfs4_async_reqs *args;
1902 
1903 	rp = VTOR4(vp);
1904 	ASSERT(rp->r_freef == NULL);
1905 
1906 	mi = VTOMI4(vp);
1907 
1908 	/*
1909 	 * If we can't allocate a request structure, skip the readdir.
1910 	 */
1911 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1912 		goto noasync;
1913 
1914 	args->a_next = NULL;
1915 #ifdef DEBUG
1916 	args->a_queuer = curthread;
1917 #endif
1918 	VN_HOLD(vp);
1919 	args->a_vp = vp;
1920 	ASSERT(cr != NULL);
1921 	crhold(cr);
1922 	args->a_cred = cr;
1923 	args->a_io = NFS4_READDIR;
1924 	args->a_nfs4_readdir = readdir;
1925 	args->a_nfs4_rdc = rdc;
1926 
1927 	mutex_enter(&mi->mi_async_lock);
1928 
1929 	/*
1930 	 * If asyncio has been disabled, then skip this request
1931 	 */
1932 	if (mi->mi_max_threads == 0) {
1933 		mutex_exit(&mi->mi_async_lock);
1934 
1935 		VN_RELE(vp);
1936 		crfree(cr);
1937 		kmem_free(args, sizeof (*args));
1938 		goto noasync;
1939 	}
1940 
1941 	/*
1942 	 * Link request structure into the async list and
1943 	 * wakeup async thread to do the i/o.
1944 	 */
1945 	if (mi->mi_async_reqs[NFS4_READDIR] == NULL) {
1946 		mi->mi_async_reqs[NFS4_READDIR] = args;
1947 		mi->mi_async_tail[NFS4_READDIR] = args;
1948 	} else {
1949 		mi->mi_async_tail[NFS4_READDIR]->a_next = args;
1950 		mi->mi_async_tail[NFS4_READDIR] = args;
1951 	}
1952 
1953 	mutex_enter(&rp->r_statelock);
1954 	rp->r_count++;
1955 	mutex_exit(&rp->r_statelock);
1956 
1957 	if (mi->mi_io_kstats) {
1958 		mutex_enter(&mi->mi_lock);
1959 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1960 		mutex_exit(&mi->mi_lock);
1961 	}
1962 
1963 	mi->mi_async_req_count++;
1964 	ASSERT(mi->mi_async_req_count != 0);
1965 	cv_signal(&mi->mi_async_reqs_cv);
1966 	mutex_exit(&mi->mi_async_lock);
1967 	return;
1968 
1969 noasync:
1970 	mutex_enter(&rp->r_statelock);
1971 	rdc->entries = NULL;
1972 	/*
1973 	 * Indicate that no one is trying to fill this entry and
1974 	 * it still needs to be filled.
1975 	 */
1976 	rdc->flags &= ~RDDIR;
1977 	rdc->flags |= RDDIRREQ;
1978 	rddir4_cache_rele(rp, rdc);
1979 	mutex_exit(&rp->r_statelock);
1980 }
1981 
1982 void
1983 nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
1984 	cred_t *cr, void (*commit)(vnode_t *, page_t *, offset3, count3,
1985 	cred_t *))
1986 {
1987 	rnode4_t *rp;
1988 	mntinfo4_t *mi;
1989 	struct nfs4_async_reqs *args;
1990 	page_t *pp;
1991 
1992 	rp = VTOR4(vp);
1993 	mi = VTOMI4(vp);
1994 
1995 	/*
1996 	 * If we can't allocate a request structure, do the commit
1997 	 * operation synchronously in this thread's context.
1998 	 */
1999 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
2000 		goto noasync;
2001 
2002 	args->a_next = NULL;
2003 #ifdef DEBUG
2004 	args->a_queuer = curthread;
2005 #endif
2006 	VN_HOLD(vp);
2007 	args->a_vp = vp;
2008 	ASSERT(cr != NULL);
2009 	crhold(cr);
2010 	args->a_cred = cr;
2011 	args->a_io = NFS4_COMMIT;
2012 	args->a_nfs4_commit = commit;
2013 	args->a_nfs4_plist = plist;
2014 	args->a_nfs4_offset = offset;
2015 	args->a_nfs4_count = count;
2016 
2017 	mutex_enter(&mi->mi_async_lock);
2018 
2019 	/*
2020 	 * If asyncio has been disabled, then make a synchronous request.
2021 	 * This check is done a second time in case async io was diabled
2022 	 * while this thread was blocked waiting for memory pressure to
2023 	 * reduce or for the queue to drain.
2024 	 */
2025 	if (mi->mi_max_threads == 0) {
2026 		mutex_exit(&mi->mi_async_lock);
2027 
2028 		VN_RELE(vp);
2029 		crfree(cr);
2030 		kmem_free(args, sizeof (*args));
2031 		goto noasync;
2032 	}
2033 
2034 	/*
2035 	 * Link request structure into the async list and
2036 	 * wakeup async thread to do the i/o.
2037 	 */
2038 	if (mi->mi_async_reqs[NFS4_COMMIT] == NULL) {
2039 		mi->mi_async_reqs[NFS4_COMMIT] = args;
2040 		mi->mi_async_tail[NFS4_COMMIT] = args;
2041 	} else {
2042 		mi->mi_async_tail[NFS4_COMMIT]->a_next = args;
2043 		mi->mi_async_tail[NFS4_COMMIT] = args;
2044 	}
2045 
2046 	mutex_enter(&rp->r_statelock);
2047 	rp->r_count++;
2048 	mutex_exit(&rp->r_statelock);
2049 
2050 	if (mi->mi_io_kstats) {
2051 		mutex_enter(&mi->mi_lock);
2052 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
2053 		mutex_exit(&mi->mi_lock);
2054 	}
2055 
2056 	mi->mi_async_req_count++;
2057 	ASSERT(mi->mi_async_req_count != 0);
2058 	cv_signal(&mi->mi_async_reqs_cv);
2059 	mutex_exit(&mi->mi_async_lock);
2060 	return;
2061 
2062 noasync:
2063 	if (curproc == proc_pageout || curproc == proc_fsflush ||
2064 	    curproc->p_zone != mi->mi_zone) {
2065 		while (plist != NULL) {
2066 			pp = plist;
2067 			page_sub(&plist, pp);
2068 			pp->p_fsdata = C_COMMIT;
2069 			page_unlock(pp);
2070 		}
2071 		return;
2072 	}
2073 	(*commit)(vp, plist, offset, count, cr);
2074 }
2075 
2076 /*
2077  * nfs4_async_inactive - hand off a VOP_INACTIVE call to a thread.  The
2078  * reference to the vnode is handed over to the thread; the caller should
2079  * no longer refer to the vnode.
2080  *
2081  * Unlike most of the async routines, this handoff is needed for
2082  * correctness reasons, not just performance.  So doing operations in the
2083  * context of the current thread is not an option.
2084  */
2085 void
2086 nfs4_async_inactive(vnode_t *vp, cred_t *cr)
2087 {
2088 	mntinfo4_t *mi;
2089 	struct nfs4_async_reqs *args;
2090 	boolean_t signal_inactive_thread = B_FALSE;
2091 
2092 	mi = VTOMI4(vp);
2093 
2094 	args = kmem_alloc(sizeof (*args), KM_SLEEP);
2095 	args->a_next = NULL;
2096 #ifdef DEBUG
2097 	args->a_queuer = curthread;
2098 #endif
2099 	args->a_vp = vp;
2100 	ASSERT(cr != NULL);
2101 	crhold(cr);
2102 	args->a_cred = cr;
2103 	args->a_io = NFS4_INACTIVE;
2104 
2105 	/*
2106 	 * Note that we don't check mi->mi_max_threads here, since we
2107 	 * *need* to get rid of this vnode regardless of whether someone
2108 	 * set nfs4_max_threads to zero in /etc/system.
2109 	 *
2110 	 * The manager thread knows about this and is willing to create
2111 	 * at least one thread to accomodate us.
2112 	 */
2113 	mutex_enter(&mi->mi_async_lock);
2114 	if (mi->mi_inactive_thread == NULL) {
2115 		rnode4_t *rp;
2116 		vnode_t *unldvp = NULL;
2117 		char *unlname;
2118 		cred_t *unlcred;
2119 
2120 		mutex_exit(&mi->mi_async_lock);
2121 		/*
2122 		 * We just need to free up the memory associated with the
2123 		 * vnode, which can be safely done from within the current
2124 		 * context.
2125 		 */
2126 		crfree(cr);	/* drop our reference */
2127 		kmem_free(args, sizeof (*args));
2128 		rp = VTOR4(vp);
2129 		mutex_enter(&rp->r_statelock);
2130 		if (rp->r_unldvp != NULL) {
2131 			unldvp = rp->r_unldvp;
2132 			rp->r_unldvp = NULL;
2133 			unlname = rp->r_unlname;
2134 			rp->r_unlname = NULL;
2135 			unlcred = rp->r_unlcred;
2136 			rp->r_unlcred = NULL;
2137 		}
2138 		mutex_exit(&rp->r_statelock);
2139 		/*
2140 		 * No need to explicitly throw away any cached pages.  The
2141 		 * eventual r4inactive() will attempt a synchronous
2142 		 * VOP_PUTPAGE() which will immediately fail since the request
2143 		 * is coming from the wrong zone, and then will proceed to call
2144 		 * nfs4_invalidate_pages() which will clean things up for us.
2145 		 *
2146 		 * Throw away the delegation here so rp4_addfree()'s attempt to
2147 		 * return any existing delegations becomes a no-op.
2148 		 */
2149 		if (rp->r_deleg_type != OPEN_DELEGATE_NONE)
2150 			(void) nfs4delegreturn(rp, NFS4_DR_DISCARD);
2151 		nfs4_clear_open_streams(rp);
2152 
2153 		rp4_addfree(rp, cr);
2154 		if (unldvp != NULL) {
2155 			kmem_free(unlname, MAXNAMELEN);
2156 			VN_RELE(unldvp);
2157 			crfree(unlcred);
2158 		}
2159 		return;
2160 	}
2161 
2162 	if (mi->mi_manager_thread == NULL) {
2163 		/*
2164 		 * We want to talk to the inactive thread.
2165 		 */
2166 		signal_inactive_thread = B_TRUE;
2167 	}
2168 
2169 	/*
2170 	 * Enqueue the vnode and wake up either the special thread (empty
2171 	 * list) or an async thread.
2172 	 */
2173 	if (mi->mi_async_reqs[NFS4_INACTIVE] == NULL) {
2174 		mi->mi_async_reqs[NFS4_INACTIVE] = args;
2175 		mi->mi_async_tail[NFS4_INACTIVE] = args;
2176 		signal_inactive_thread = B_TRUE;
2177 	} else {
2178 		mi->mi_async_tail[NFS4_INACTIVE]->a_next = args;
2179 		mi->mi_async_tail[NFS4_INACTIVE] = args;
2180 	}
2181 	if (signal_inactive_thread) {
2182 		cv_signal(&mi->mi_inact_req_cv);
2183 	} else  {
2184 		mi->mi_async_req_count++;
2185 		ASSERT(mi->mi_async_req_count != 0);
2186 		cv_signal(&mi->mi_async_reqs_cv);
2187 	}
2188 
2189 	mutex_exit(&mi->mi_async_lock);
2190 }
2191 
2192 int
2193 writerp4(rnode4_t *rp, caddr_t base, int tcount, struct uio *uio, int pgcreated)
2194 {
2195 	int pagecreate;
2196 	int n;
2197 	int saved_n;
2198 	caddr_t saved_base;
2199 	u_offset_t offset;
2200 	int error;
2201 	int sm_error;
2202 
2203 	ASSERT(tcount <= MAXBSIZE && tcount <= uio->uio_resid);
2204 	ASSERT(((uintptr_t)base & MAXBOFFSET) + tcount <= MAXBSIZE);
2205 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_WRITER));
2206 
2207 	/*
2208 	 * Move bytes in at most PAGESIZE chunks. We must avoid
2209 	 * spanning pages in uiomove() because page faults may cause
2210 	 * the cache to be invalidated out from under us. The r_size is not
2211 	 * updated until after the uiomove. If we push the last page of a
2212 	 * file before r_size is correct, we will lose the data written past
2213 	 * the current (and invalid) r_size.
2214 	 */
2215 	do {
2216 		offset = uio->uio_loffset;
2217 		pagecreate = 0;
2218 
2219 		/*
2220 		 * n is the number of bytes required to satisfy the request
2221 		 *   or the number of bytes to fill out the page.
2222 		 */
2223 		n = (int)MIN((PAGESIZE - ((uintptr_t)base & PAGEOFFSET)),
2224 		    tcount);
2225 
2226 		/*
2227 		 * Check to see if we can skip reading in the page
2228 		 * and just allocate the memory.  We can do this
2229 		 * if we are going to rewrite the entire mapping
2230 		 * or if we are going to write to or beyond the current
2231 		 * end of file from the beginning of the mapping.
2232 		 *
2233 		 * The read of r_size is now protected by r_statelock.
2234 		 */
2235 		mutex_enter(&rp->r_statelock);
2236 		/*
2237 		 * When pgcreated is nonzero the caller has already done
2238 		 * a segmap_getmapflt with forcefault 0 and S_WRITE. With
2239 		 * segkpm this means we already have at least one page
2240 		 * created and mapped at base.
2241 		 */
2242 		pagecreate = pgcreated ||
2243 			(((uintptr_t)base & PAGEOFFSET) == 0 &&
2244 			(n == PAGESIZE || ((offset + n) >= rp->r_size)));
2245 
2246 		mutex_exit(&rp->r_statelock);
2247 
2248 		if (pagecreate) {
2249 			/*
2250 			 * The last argument tells segmap_pagecreate() to
2251 			 * always lock the page, as opposed to sometimes
2252 			 * returning with the page locked. This way we avoid a
2253 			 * fault on the ensuing uiomove(), but also
2254 			 * more importantly (to fix bug 1094402) we can
2255 			 * call segmap_fault() to unlock the page in all
2256 			 * cases. An alternative would be to modify
2257 			 * segmap_pagecreate() to tell us when it is
2258 			 * locking a page, but that's a fairly major
2259 			 * interface change.
2260 			 */
2261 			if (pgcreated == 0)
2262 				(void) segmap_pagecreate(segkmap, base,
2263 							(uint_t)n, 1);
2264 			saved_base = base;
2265 			saved_n = n;
2266 		}
2267 
2268 		/*
2269 		 * The number of bytes of data in the last page can not
2270 		 * be accurately be determined while page is being
2271 		 * uiomove'd to and the size of the file being updated.
2272 		 * Thus, inform threads which need to know accurately
2273 		 * how much data is in the last page of the file.  They
2274 		 * will not do the i/o immediately, but will arrange for
2275 		 * the i/o to happen later when this modify operation
2276 		 * will have finished.
2277 		 */
2278 		ASSERT(!(rp->r_flags & R4MODINPROGRESS));
2279 		mutex_enter(&rp->r_statelock);
2280 		rp->r_flags |= R4MODINPROGRESS;
2281 		rp->r_modaddr = (offset & MAXBMASK);
2282 		mutex_exit(&rp->r_statelock);
2283 
2284 		error = uiomove(base, n, UIO_WRITE, uio);
2285 
2286 		/*
2287 		 * r_size is the maximum number of
2288 		 * bytes known to be in the file.
2289 		 * Make sure it is at least as high as the
2290 		 * first unwritten byte pointed to by uio_loffset.
2291 		 */
2292 		mutex_enter(&rp->r_statelock);
2293 		if (rp->r_size < uio->uio_loffset)
2294 			rp->r_size = uio->uio_loffset;
2295 		rp->r_flags &= ~R4MODINPROGRESS;
2296 		rp->r_flags |= R4DIRTY;
2297 		mutex_exit(&rp->r_statelock);
2298 
2299 		/* n = # of bytes written */
2300 		n = (int)(uio->uio_loffset - offset);
2301 		base += n;
2302 		tcount -= n;
2303 		/*
2304 		 * If we created pages w/o initializing them completely,
2305 		 * we need to zero the part that wasn't set up.
2306 		 * This happens on a most EOF write cases and if
2307 		 * we had some sort of error during the uiomove.
2308 		 */
2309 		if (pagecreate) {
2310 			if ((uio->uio_loffset & PAGEOFFSET) || n == 0)
2311 				(void) kzero(base, PAGESIZE - n);
2312 
2313 			if (pgcreated) {
2314 				/*
2315 				 * Caller is responsible for this page,
2316 				 * it was not created in this loop.
2317 				 */
2318 				pgcreated = 0;
2319 			} else {
2320 				/*
2321 				 * For bug 1094402: segmap_pagecreate locks
2322 				 * page. Unlock it. This also unlocks the
2323 				 * pages allocated by page_create_va() in
2324 				 * segmap_pagecreate().
2325 				 */
2326 				sm_error = segmap_fault(kas.a_hat, segkmap,
2327 						saved_base, saved_n,
2328 						F_SOFTUNLOCK, S_WRITE);
2329 				if (error == 0)
2330 					error = sm_error;
2331 			}
2332 		}
2333 	} while (tcount > 0 && error == 0);
2334 
2335 	return (error);
2336 }
2337 
2338 int
2339 nfs4_putpages(vnode_t *vp, u_offset_t off, size_t len, int flags, cred_t *cr)
2340 {
2341 	rnode4_t *rp;
2342 	page_t *pp;
2343 	u_offset_t eoff;
2344 	u_offset_t io_off;
2345 	size_t io_len;
2346 	int error;
2347 	int rdirty;
2348 	int err;
2349 
2350 	rp = VTOR4(vp);
2351 	ASSERT(rp->r_count > 0);
2352 
2353 	if (!nfs4_has_pages(vp))
2354 		return (0);
2355 
2356 	ASSERT(vp->v_type != VCHR);
2357 
2358 	/*
2359 	 * If R4OUTOFSPACE is set, then all writes turn into B_INVAL
2360 	 * writes.  B_FORCE is set to force the VM system to actually
2361 	 * invalidate the pages, even if the i/o failed.  The pages
2362 	 * need to get invalidated because they can't be written out
2363 	 * because there isn't any space left on either the server's
2364 	 * file system or in the user's disk quota.  The B_FREE bit
2365 	 * is cleared to avoid confusion as to whether this is a
2366 	 * request to place the page on the freelist or to destroy
2367 	 * it.
2368 	 */
2369 	if ((rp->r_flags & R4OUTOFSPACE) ||
2370 	    (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED))
2371 		flags = (flags & ~B_FREE) | B_INVAL | B_FORCE;
2372 
2373 	if (len == 0) {
2374 		/*
2375 		 * If doing a full file synchronous operation, then clear
2376 		 * the R4DIRTY bit.  If a page gets dirtied while the flush
2377 		 * is happening, then R4DIRTY will get set again.  The
2378 		 * R4DIRTY bit must get cleared before the flush so that
2379 		 * we don't lose this information.
2380 		 */
2381 		if (off == (u_offset_t)0 &&
2382 		    !(flags & B_ASYNC) &&
2383 		    (rp->r_flags & R4DIRTY)) {
2384 			mutex_enter(&rp->r_statelock);
2385 			rdirty = (rp->r_flags & R4DIRTY);
2386 			rp->r_flags &= ~R4DIRTY;
2387 			mutex_exit(&rp->r_statelock);
2388 		} else
2389 			rdirty = 0;
2390 
2391 		/*
2392 		 * Search the entire vp list for pages >= off, and flush
2393 		 * the dirty pages.
2394 		 */
2395 		error = pvn_vplist_dirty(vp, off, rp->r_putapage,
2396 					flags, cr);
2397 
2398 		/*
2399 		 * If an error occured and the file was marked as dirty
2400 		 * before and we aren't forcibly invalidating pages, then
2401 		 * reset the R4DIRTY flag.
2402 		 */
2403 		if (error && rdirty &&
2404 		    (flags & (B_INVAL | B_FORCE)) != (B_INVAL | B_FORCE)) {
2405 			mutex_enter(&rp->r_statelock);
2406 			rp->r_flags |= R4DIRTY;
2407 			mutex_exit(&rp->r_statelock);
2408 		}
2409 	} else {
2410 		/*
2411 		 * Do a range from [off...off + len) looking for pages
2412 		 * to deal with.
2413 		 */
2414 		error = 0;
2415 		io_len = 0;
2416 		eoff = off + len;
2417 		mutex_enter(&rp->r_statelock);
2418 		for (io_off = off; io_off < eoff && io_off < rp->r_size;
2419 		    io_off += io_len) {
2420 			mutex_exit(&rp->r_statelock);
2421 			/*
2422 			 * If we are not invalidating, synchronously
2423 			 * freeing or writing pages use the routine
2424 			 * page_lookup_nowait() to prevent reclaiming
2425 			 * them from the free list.
2426 			 */
2427 			if ((flags & B_INVAL) || !(flags & B_ASYNC)) {
2428 				pp = page_lookup(vp, io_off,
2429 				    (flags & (B_INVAL | B_FREE)) ?
2430 				    SE_EXCL : SE_SHARED);
2431 			} else {
2432 				pp = page_lookup_nowait(vp, io_off,
2433 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
2434 			}
2435 
2436 			if (pp == NULL || !pvn_getdirty(pp, flags))
2437 				io_len = PAGESIZE;
2438 			else {
2439 				err = (*rp->r_putapage)(vp, pp, &io_off,
2440 				    &io_len, flags, cr);
2441 				if (!error)
2442 					error = err;
2443 				/*
2444 				 * "io_off" and "io_len" are returned as
2445 				 * the range of pages we actually wrote.
2446 				 * This allows us to skip ahead more quickly
2447 				 * since several pages may've been dealt
2448 				 * with by this iteration of the loop.
2449 				 */
2450 			}
2451 			mutex_enter(&rp->r_statelock);
2452 		}
2453 		mutex_exit(&rp->r_statelock);
2454 	}
2455 
2456 	return (error);
2457 }
2458 
2459 void
2460 nfs4_invalidate_pages(vnode_t *vp, u_offset_t off, cred_t *cr)
2461 {
2462 	rnode4_t *rp;
2463 
2464 	rp = VTOR4(vp);
2465 	if (IS_SHADOW(vp, rp))
2466 		vp = RTOV4(rp);
2467 	mutex_enter(&rp->r_statelock);
2468 	while (rp->r_flags & R4TRUNCATE)
2469 		cv_wait(&rp->r_cv, &rp->r_statelock);
2470 	rp->r_flags |= R4TRUNCATE;
2471 	if (off == (u_offset_t)0) {
2472 		rp->r_flags &= ~R4DIRTY;
2473 		if (!(rp->r_flags & R4STALE))
2474 			rp->r_error = 0;
2475 	}
2476 	rp->r_truncaddr = off;
2477 	mutex_exit(&rp->r_statelock);
2478 	(void) pvn_vplist_dirty(vp, off, rp->r_putapage,
2479 		B_INVAL | B_TRUNC, cr);
2480 	mutex_enter(&rp->r_statelock);
2481 	rp->r_flags &= ~R4TRUNCATE;
2482 	cv_broadcast(&rp->r_cv);
2483 	mutex_exit(&rp->r_statelock);
2484 }
2485 
2486 static int
2487 nfs4_mnt_kstat_update(kstat_t *ksp, int rw)
2488 {
2489 	mntinfo4_t *mi;
2490 	struct mntinfo_kstat *mik;
2491 	vfs_t *vfsp;
2492 
2493 	/* this is a read-only kstat. Bail out on a write */
2494 	if (rw == KSTAT_WRITE)
2495 		return (EACCES);
2496 
2497 
2498 	/*
2499 	 * We don't want to wait here as kstat_chain_lock could be held by
2500 	 * dounmount(). dounmount() takes vfs_reflock before the chain lock
2501 	 * and thus could lead to a deadlock.
2502 	 */
2503 	vfsp = (struct vfs *)ksp->ks_private;
2504 
2505 	mi = VFTOMI4(vfsp);
2506 	mik = (struct mntinfo_kstat *)ksp->ks_data;
2507 
2508 	(void) strcpy(mik->mik_proto, mi->mi_curr_serv->sv_knconf->knc_proto);
2509 
2510 	mik->mik_vers = (uint32_t)mi->mi_vers;
2511 	mik->mik_flags = mi->mi_flags;
2512 	/*
2513 	 * The sv_secdata holds the flavor the client specifies.
2514 	 * If the client uses default and a security negotiation
2515 	 * occurs, sv_currsec will point to the current flavor
2516 	 * selected from the server flavor list.
2517 	 * sv_currsec is NULL if no security negotiation takes place.
2518 	 */
2519 	mik->mik_secmod = mi->mi_curr_serv->sv_currsec ?
2520 			mi->mi_curr_serv->sv_currsec->secmod :
2521 			mi->mi_curr_serv->sv_secdata->secmod;
2522 	mik->mik_curread = (uint32_t)mi->mi_curread;
2523 	mik->mik_curwrite = (uint32_t)mi->mi_curwrite;
2524 	mik->mik_retrans = mi->mi_retrans;
2525 	mik->mik_timeo = mi->mi_timeo;
2526 	mik->mik_acregmin = HR2SEC(mi->mi_acregmin);
2527 	mik->mik_acregmax = HR2SEC(mi->mi_acregmax);
2528 	mik->mik_acdirmin = HR2SEC(mi->mi_acdirmin);
2529 	mik->mik_acdirmax = HR2SEC(mi->mi_acdirmax);
2530 	mik->mik_noresponse = (uint32_t)mi->mi_noresponse;
2531 	mik->mik_failover = (uint32_t)mi->mi_failover;
2532 	mik->mik_remap = (uint32_t)mi->mi_remap;
2533 
2534 	(void) strcpy(mik->mik_curserver, mi->mi_curr_serv->sv_hostname);
2535 
2536 	return (0);
2537 }
2538 
2539 void
2540 nfs4_mnt_kstat_init(struct vfs *vfsp)
2541 {
2542 	mntinfo4_t *mi = VFTOMI4(vfsp);
2543 
2544 	/*
2545 	 * PSARC 2001/697 Contract Private Interface
2546 	 * All nfs kstats are under SunMC contract
2547 	 * Please refer to the PSARC listed above and contact
2548 	 * SunMC before making any changes!
2549 	 *
2550 	 * Changes must be reviewed by Solaris File Sharing
2551 	 * Changes must be communicated to contract-2001-697@sun.com
2552 	 *
2553 	 */
2554 
2555 	mi->mi_io_kstats = kstat_create_zone("nfs", getminor(vfsp->vfs_dev),
2556 	    NULL, "nfs", KSTAT_TYPE_IO, 1, 0, mi->mi_zone->zone_id);
2557 	if (mi->mi_io_kstats) {
2558 		if (mi->mi_zone->zone_id != GLOBAL_ZONEID)
2559 			kstat_zone_add(mi->mi_io_kstats, GLOBAL_ZONEID);
2560 		mi->mi_io_kstats->ks_lock = &mi->mi_lock;
2561 		kstat_install(mi->mi_io_kstats);
2562 	}
2563 
2564 	if ((mi->mi_ro_kstats = kstat_create_zone("nfs",
2565 	    getminor(vfsp->vfs_dev), "mntinfo", "misc", KSTAT_TYPE_RAW,
2566 	    sizeof (struct mntinfo_kstat), 0, mi->mi_zone->zone_id)) != NULL) {
2567 		if (mi->mi_zone->zone_id != GLOBAL_ZONEID)
2568 			kstat_zone_add(mi->mi_ro_kstats, GLOBAL_ZONEID);
2569 		mi->mi_ro_kstats->ks_update = nfs4_mnt_kstat_update;
2570 		mi->mi_ro_kstats->ks_private = (void *)vfsp;
2571 		kstat_install(mi->mi_ro_kstats);
2572 	}
2573 
2574 	nfs4_mnt_recov_kstat_init(vfsp);
2575 }
2576 
2577 void
2578 nfs4_write_error(vnode_t *vp, int error, cred_t *cr)
2579 {
2580 	mntinfo4_t *mi;
2581 
2582 	mi = VTOMI4(vp);
2583 	/*
2584 	 * In case of forced unmount, do not print any messages
2585 	 * since it can flood the console with error messages.
2586 	 */
2587 	if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED)
2588 		return;
2589 
2590 	/*
2591 	 * If the mount point is dead, not recoverable, do not
2592 	 * print error messages that can flood the console.
2593 	 */
2594 	if (mi->mi_flags & MI4_RECOV_FAIL)
2595 		return;
2596 
2597 	/*
2598 	 * No use in flooding the console with ENOSPC
2599 	 * messages from the same file system.
2600 	 */
2601 	if ((error != ENOSPC && error != EDQUOT) ||
2602 	    lbolt - mi->mi_printftime > 0) {
2603 		zoneid_t zoneid = mi->mi_zone->zone_id;
2604 
2605 #ifdef DEBUG
2606 		nfs_perror(error, "NFS%ld write error on host %s: %m.\n",
2607 		    mi->mi_vers, VTOR4(vp)->r_server->sv_hostname, NULL);
2608 #else
2609 		nfs_perror(error, "NFS write error on host %s: %m.\n",
2610 		    VTOR4(vp)->r_server->sv_hostname, NULL);
2611 #endif
2612 		if (error == ENOSPC || error == EDQUOT) {
2613 			zcmn_err(zoneid, CE_CONT,
2614 			    "^File: userid=%d, groupid=%d\n",
2615 			    crgetuid(cr), crgetgid(cr));
2616 			if (crgetuid(curthread->t_cred) != crgetuid(cr) ||
2617 			    crgetgid(curthread->t_cred) != crgetgid(cr)) {
2618 				zcmn_err(zoneid, CE_CONT,
2619 				    "^User: userid=%d, groupid=%d\n",
2620 				    crgetuid(curthread->t_cred),
2621 				    crgetgid(curthread->t_cred));
2622 			}
2623 			mi->mi_printftime = lbolt +
2624 			    nfs_write_error_interval * hz;
2625 		}
2626 		sfh4_printfhandle(VTOR4(vp)->r_fh);
2627 #ifdef DEBUG
2628 		if (error == EACCES) {
2629 			zcmn_err(zoneid, CE_CONT,
2630 			    "nfs_bio: cred is%s kcred\n",
2631 			    cr == kcred ? "" : " not");
2632 		}
2633 #endif
2634 	}
2635 }
2636 
2637 /*
2638  * Return non-zero if the given file can be safely memory mapped.  Locks
2639  * are safe if whole-file (length and offset are both zero).
2640  */
2641 
2642 #define	SAFE_LOCK(flk)	((flk).l_start == 0 && (flk).l_len == 0)
2643 
2644 static int
2645 nfs4_safemap(const vnode_t *vp)
2646 {
2647 	locklist_t	*llp, *next_llp;
2648 	int		safe = 1;
2649 	rnode4_t	*rp = VTOR4(vp);
2650 
2651 	ASSERT(nfs_rw_lock_held(&rp->r_lkserlock, RW_WRITER));
2652 
2653 	NFS4_DEBUG(nfs4_client_map_debug, (CE_NOTE, "nfs4_safemap: "
2654 		"vp = %p", (void *)vp));
2655 
2656 	/*
2657 	 * Review all the locks for the vnode, both ones that have been
2658 	 * acquired and ones that are pending.  We assume that
2659 	 * flk_active_locks_for_vp() has merged any locks that can be
2660 	 * merged (so that if a process has the entire file locked, it is
2661 	 * represented as a single lock).
2662 	 *
2663 	 * Note that we can't bail out of the loop if we find a non-safe
2664 	 * lock, because we have to free all the elements in the llp list.
2665 	 * We might be able to speed up this code slightly by not looking
2666 	 * at each lock's l_start and l_len fields once we've found a
2667 	 * non-safe lock.
2668 	 */
2669 
2670 	llp = flk_active_locks_for_vp(vp);
2671 	while (llp) {
2672 		NFS4_DEBUG(nfs4_client_map_debug, (CE_NOTE,
2673 		    "nfs4_safemap: active lock (%" PRId64 ", %" PRId64 ")",
2674 		    llp->ll_flock.l_start, llp->ll_flock.l_len));
2675 		if (!SAFE_LOCK(llp->ll_flock)) {
2676 			safe = 0;
2677 			NFS4_DEBUG(nfs4_client_map_debug, (CE_NOTE,
2678 			    "nfs4_safemap: unsafe active lock (%" PRId64
2679 			    ", %" PRId64 ")", llp->ll_flock.l_start,
2680 			    llp->ll_flock.l_len));
2681 		}
2682 		next_llp = llp->ll_next;
2683 		VN_RELE(llp->ll_vp);
2684 		kmem_free(llp, sizeof (*llp));
2685 		llp = next_llp;
2686 	}
2687 
2688 	NFS4_DEBUG(nfs4_client_map_debug, (CE_NOTE, "nfs4_safemap: %s",
2689 		safe ? "safe" : "unsafe"));
2690 	return (safe);
2691 }
2692 
2693 /*
2694  * Return whether there is a lost LOCK or LOCKU queued up for the given
2695  * file that would make an mmap request unsafe.  cf. nfs4_safemap().
2696  */
2697 
2698 bool_t
2699 nfs4_map_lost_lock_conflict(vnode_t *vp)
2700 {
2701 	bool_t conflict = FALSE;
2702 	nfs4_lost_rqst_t *lrp;
2703 	mntinfo4_t *mi = VTOMI4(vp);
2704 
2705 	mutex_enter(&mi->mi_lock);
2706 	for (lrp = list_head(&mi->mi_lost_state); lrp != NULL;
2707 	    lrp = list_next(&mi->mi_lost_state, lrp)) {
2708 		if (lrp->lr_op != OP_LOCK && lrp->lr_op != OP_LOCKU)
2709 			continue;
2710 		ASSERT(lrp->lr_vp != NULL);
2711 		if (!VOP_CMP(lrp->lr_vp, vp))
2712 			continue;	/* different file */
2713 		if (!SAFE_LOCK(*lrp->lr_flk)) {
2714 			conflict = TRUE;
2715 			break;
2716 		}
2717 	}
2718 
2719 	mutex_exit(&mi->mi_lock);
2720 	return (conflict);
2721 }
2722 
2723 /*
2724  * nfs_lockcompletion:
2725  *
2726  * If the vnode has a lock that makes it unsafe to cache the file, mark it
2727  * as non cachable (set VNOCACHE bit).
2728  */
2729 
2730 void
2731 nfs4_lockcompletion(vnode_t *vp, int cmd)
2732 {
2733 	rnode4_t *rp = VTOR4(vp);
2734 
2735 	ASSERT(nfs_rw_lock_held(&rp->r_lkserlock, RW_WRITER));
2736 	ASSERT(!IS_SHADOW(vp, rp));
2737 
2738 	if (cmd == F_SETLK || cmd == F_SETLKW) {
2739 
2740 		if (!nfs4_safemap(vp)) {
2741 			mutex_enter(&vp->v_lock);
2742 			vp->v_flag |= VNOCACHE;
2743 			mutex_exit(&vp->v_lock);
2744 		} else {
2745 			mutex_enter(&vp->v_lock);
2746 			vp->v_flag &= ~VNOCACHE;
2747 			mutex_exit(&vp->v_lock);
2748 		}
2749 	}
2750 	/*
2751 	 * The cached attributes of the file are stale after acquiring
2752 	 * the lock on the file. They were updated when the file was
2753 	 * opened, but not updated when the lock was acquired. Therefore the
2754 	 * cached attributes are invalidated after the lock is obtained.
2755 	 */
2756 	PURGE_ATTRCACHE4(vp);
2757 }
2758 
2759 /* ARGSUSED */
2760 static void *
2761 nfs4_mi_init(zoneid_t zoneid)
2762 {
2763 	struct mi4_globals *mig;
2764 
2765 	mig = kmem_alloc(sizeof (*mig), KM_SLEEP);
2766 	mutex_init(&mig->mig_lock, NULL, MUTEX_DEFAULT, NULL);
2767 	list_create(&mig->mig_list, sizeof (mntinfo4_t),
2768 	    offsetof(mntinfo4_t, mi_zone_node));
2769 	mig->mig_destructor_called = B_FALSE;
2770 	return (mig);
2771 }
2772 
2773 /*
2774  * Callback routine to tell all NFSv4 mounts in the zone to start tearing down
2775  * state and killing off threads.
2776  */
2777 /* ARGSUSED */
2778 static void
2779 nfs4_mi_shutdown(zoneid_t zoneid, void *data)
2780 {
2781 	struct mi4_globals *mig = data;
2782 	mntinfo4_t *mi;
2783 	nfs4_server_t *np;
2784 
2785 	NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE,
2786 	    "nfs4_mi_shutdown zone %d\n", zoneid));
2787 	ASSERT(mig != NULL);
2788 	mutex_enter(&mig->mig_lock);
2789 	for (mi = list_head(&mig->mig_list); mi != NULL;
2790 	    mi = list_next(&mig->mig_list, mi)) {
2791 		NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE,
2792 		    "nfs4_mi_shutdown stopping vfs %p\n", (void *)mi->mi_vfsp));
2793 		/*
2794 		 * purge the DNLC for this filesystem
2795 		 */
2796 		(void) dnlc_purge_vfsp(mi->mi_vfsp, 0);
2797 
2798 		mutex_enter(&mi->mi_async_lock);
2799 		/*
2800 		 * Tell existing async worker threads to exit.
2801 		 */
2802 		mi->mi_max_threads = 0;
2803 		cv_broadcast(&mi->mi_async_work_cv);
2804 		/*
2805 		 * Set the appropriate flags so both the async manager and the
2806 		 * inactive thread start getting ready to exit when they're done
2807 		 * with their current work.
2808 		 */
2809 		mutex_enter(&mi->mi_lock);
2810 		mi->mi_flags |= (MI4_ASYNC_MGR_STOP|MI4_DEAD);
2811 		mutex_exit(&mi->mi_lock);
2812 		/*
2813 		 * Wake up async manager thread.  When it is done it will wake
2814 		 * up the inactive thread which will then exit.
2815 		 */
2816 		cv_broadcast(&mi->mi_async_reqs_cv);
2817 		mutex_exit(&mi->mi_async_lock);
2818 	}
2819 	mutex_exit(&mig->mig_lock);
2820 	/*
2821 	 * Tell each renew thread in the zone to exit
2822 	 */
2823 	mutex_enter(&nfs4_server_lst_lock);
2824 	for (np = nfs4_server_lst.forw; np != &nfs4_server_lst; np = np->forw) {
2825 		mutex_enter(&np->s_lock);
2826 		if (np->zoneid == zoneid) {
2827 			/*
2828 			 * We add another hold onto the nfs4_server_t
2829 			 * because this will make sure tha the nfs4_server_t
2830 			 * stays around until nfs4_callback_fini_zone destroys
2831 			 * the zone. This way, the renew thread can
2832 			 * unconditionally release its holds on the
2833 			 * nfs4_server_t.
2834 			 */
2835 			np->s_refcnt++;
2836 			nfs4_mark_srv_dead(np);
2837 		}
2838 		mutex_exit(&np->s_lock);
2839 	}
2840 	mutex_exit(&nfs4_server_lst_lock);
2841 }
2842 
2843 static void
2844 nfs4_mi_free_globals(struct mi4_globals *mig)
2845 {
2846 	list_destroy(&mig->mig_list);	/* makes sure the list is empty */
2847 	mutex_destroy(&mig->mig_lock);
2848 	kmem_free(mig, sizeof (*mig));
2849 
2850 }
2851 
2852 /* ARGSUSED */
2853 static void
2854 nfs4_mi_destroy(zoneid_t zoneid, void *data)
2855 {
2856 	struct mi4_globals *mig = data;
2857 
2858 	NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE,
2859 	    "nfs4_mi_destroy zone %d\n", zoneid));
2860 	ASSERT(mig != NULL);
2861 	mutex_enter(&mig->mig_lock);
2862 	if (list_head(&mig->mig_list) != NULL) {
2863 		/* Still waiting for VFS_FREEVFS() */
2864 		mig->mig_destructor_called = B_TRUE;
2865 		mutex_exit(&mig->mig_lock);
2866 		return;
2867 	}
2868 	nfs4_mi_free_globals(mig);
2869 }
2870 
2871 /*
2872  * Add an NFS mount to the per-zone list of NFS mounts.
2873  */
2874 void
2875 nfs4_mi_zonelist_add(mntinfo4_t *mi)
2876 {
2877 	struct mi4_globals *mig;
2878 
2879 	mig = zone_getspecific(mi4_list_key, mi->mi_zone);
2880 	mutex_enter(&mig->mig_lock);
2881 	list_insert_head(&mig->mig_list, mi);
2882 	mutex_exit(&mig->mig_lock);
2883 }
2884 
2885 /*
2886  * Remove an NFS mount from the per-zone list of NFS mounts.
2887  */
2888 static void
2889 nfs4_mi_zonelist_remove(mntinfo4_t *mi)
2890 {
2891 	struct mi4_globals *mig;
2892 
2893 	mig = zone_getspecific(mi4_list_key, mi->mi_zone);
2894 	mutex_enter(&mig->mig_lock);
2895 	list_remove(&mig->mig_list, mi);
2896 	/*
2897 	 * We can be called asynchronously by VFS_FREEVFS() after the zone
2898 	 * shutdown/destroy callbacks have executed; if so, clean up the zone's
2899 	 * mi globals.
2900 	 */
2901 	if (list_head(&mig->mig_list) == NULL &&
2902 	    mig->mig_destructor_called == B_TRUE) {
2903 		nfs4_mi_free_globals(mig);
2904 		return;
2905 	}
2906 	mutex_exit(&mig->mig_lock);
2907 }
2908 
2909 void
2910 nfs_free_mi4(mntinfo4_t *mi)
2911 {
2912 	nfs4_open_owner_t	*foop;
2913 	nfs4_oo_hash_bucket_t	*bucketp;
2914 	nfs4_debug_msg_t	*msgp;
2915 	int i;
2916 
2917 	/*
2918 	 * Tell the thread for over the wire inactive calls to exit.
2919 	 *
2920 	 * By the time we get here the last VFS_RELE() has already been called,
2921 	 * or this is an aborted mount; in either case the async manager thread
2922 	 * and the recovery thread should be dead by now.
2923 	 */
2924 	mutex_enter(&mi->mi_lock);
2925 	ASSERT(mi->mi_recovthread == NULL);
2926 	ASSERT(mi->mi_flags & MI4_ASYNC_MGR_STOP);
2927 	mi->mi_flags |= MI4_DEAD;
2928 	mutex_exit(&mi->mi_lock);
2929 
2930 	mutex_enter(&mi->mi_async_lock);
2931 	ASSERT(mi->mi_threads == 0);
2932 	ASSERT(mi->mi_manager_thread == NULL);
2933 
2934 	/*
2935 	 * If we are the inactive thread NULL mi_inactive_thread
2936 	 * then return. The inactive thread will detect MI4_DEAD
2937 	 * and call nfs_free_mi4 directly so that the cleanup and
2938 	 * thread exit can occur.
2939 	 */
2940 	if (mi->mi_inactive_thread == curthread) {
2941 		mi->mi_inactive_thread = NULL;
2942 		mutex_exit(&mi->mi_async_lock);
2943 		return;
2944 	}
2945 
2946 	/*
2947 	 * Wake up the inactive thread.
2948 	 */
2949 	cv_signal(&mi->mi_inact_req_cv);
2950 
2951 	/*
2952 	 * Wait for the inactive thread to exit.
2953 	 */
2954 	while (mi->mi_inactive_thread != NULL) {
2955 		cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
2956 	}
2957 
2958 	mutex_exit(&mi->mi_async_lock);
2959 
2960 	mutex_enter(&mi->mi_msg_list_lock);
2961 	while (msgp = list_head(&mi->mi_msg_list)) {
2962 		list_remove(&mi->mi_msg_list, msgp);
2963 		nfs4_free_msg(msgp);
2964 	}
2965 	mutex_exit(&mi->mi_msg_list_lock);
2966 	list_destroy(&mi->mi_msg_list);
2967 
2968 	if (mi->mi_rootfh != NULL)
2969 		sfh4_rele(&mi->mi_rootfh);
2970 	if (mi->mi_srvparentfh != NULL)
2971 		sfh4_rele(&mi->mi_srvparentfh);
2972 
2973 	mutex_destroy(&mi->mi_lock);
2974 	mutex_destroy(&mi->mi_async_lock);
2975 	mutex_destroy(&mi->mi_msg_list_lock);
2976 	mutex_destroy(&mi->mi_fileid_lock);
2977 	nfs_rw_destroy(&mi->mi_recovlock);
2978 	nfs_rw_destroy(&mi->mi_rename_lock);
2979 	nfs_rw_destroy(&mi->mi_fh_lock);
2980 	cv_destroy(&mi->mi_failover_cv);
2981 	cv_destroy(&mi->mi_async_reqs_cv);
2982 	cv_destroy(&mi->mi_async_work_cv);
2983 	cv_destroy(&mi->mi_async_cv);
2984 	cv_destroy(&mi->mi_inact_req_cv);
2985 
2986 	/*
2987 	 * Destroy the oo hash lists and mutexes for the cred hash table.
2988 	 */
2989 	for (i = 0; i < NFS4_NUM_OO_BUCKETS; i++) {
2990 		bucketp = &(mi->mi_oo_list[i]);
2991 		/* Destroy any remaining open owners on the list */
2992 		foop = list_head(&bucketp->b_oo_hash_list);
2993 		while (foop != NULL) {
2994 			list_remove(&bucketp->b_oo_hash_list, foop);
2995 			nfs4_destroy_open_owner(foop);
2996 			foop = list_head(&bucketp->b_oo_hash_list);
2997 		}
2998 		list_destroy(&bucketp->b_oo_hash_list);
2999 		mutex_destroy(&bucketp->b_lock);
3000 	}
3001 
3002 	/*
3003 	 * Empty and destroy the freed open owner list.
3004 	 */
3005 	foop = list_head(&mi->mi_foo_list);
3006 	while (foop != NULL) {
3007 		list_remove(&mi->mi_foo_list, foop);
3008 		nfs4_destroy_open_owner(foop);
3009 		foop = list_head(&mi->mi_foo_list);
3010 	}
3011 
3012 	list_destroy(&mi->mi_foo_list);
3013 	list_destroy(&mi->mi_bseqid_list);
3014 	list_destroy(&mi->mi_lost_state);
3015 	avl_destroy(&mi->mi_filehandles);
3016 	avl_destroy(&mi->mi_fileid_map);
3017 	fn_rele(&mi->mi_fname);
3018 	nfs4_mi_zonelist_remove(mi);
3019 	zone_rele(mi->mi_zone);
3020 
3021 	kmem_free(mi, sizeof (*mi));
3022 }
3023 
3024 vnode_t    nfs4_xattr_notsupp_vnode;
3025 
3026 void
3027 nfs4_clnt_init(void)
3028 {
3029 	nfs4_vnops_init();
3030 	(void) nfs4_rnode_init();
3031 	(void) nfs4_shadow_init();
3032 	(void) nfs4_acache_init();
3033 	(void) nfs4_subr_init();
3034 	nfs4_acl_init();
3035 	nfs_idmap_init();
3036 	nfs4_callback_init();
3037 	nfs4_secinfo_init();
3038 #ifdef	DEBUG
3039 	tsd_create(&nfs4_tsd_key, NULL);
3040 #endif
3041 
3042 	/*
3043 	 * Add a CPR callback so that we can update client
3044 	 * lease after a suspend and resume.
3045 	 */
3046 	cid = callb_add(nfs4_client_cpr_callb, 0, CB_CL_CPR_RPC, "nfs4");
3047 
3048 	zone_key_create(&mi4_list_key, nfs4_mi_init, nfs4_mi_shutdown,
3049 	    nfs4_mi_destroy);
3050 
3051 	/*
3052 	 * Initialise the reference count of the notsupp xattr cache vnode to 1
3053 	 * so that it never goes away (VOP_INACTIVE isn't called on it).
3054 	 */
3055 	nfs4_xattr_notsupp_vnode.v_count = 1;
3056 }
3057 
3058 void
3059 nfs4_clnt_fini(void)
3060 {
3061 	(void) zone_key_delete(mi4_list_key);
3062 	nfs4_vnops_fini();
3063 	(void) nfs4_rnode_fini();
3064 	(void) nfs4_shadow_fini();
3065 	(void) nfs4_acache_fini();
3066 	(void) nfs4_subr_fini();
3067 	nfs_idmap_fini();
3068 	nfs4_callback_fini();
3069 	nfs4_secinfo_fini();
3070 #ifdef	DEBUG
3071 	tsd_destroy(&nfs4_tsd_key);
3072 #endif
3073 	if (cid)
3074 		(void) callb_delete(cid);
3075 }
3076 
3077 /*ARGSUSED*/
3078 static boolean_t
3079 nfs4_client_cpr_callb(void *arg, int code)
3080 {
3081 	/*
3082 	 * We get called for Suspend and Resume events.
3083 	 * For the suspend case we simply don't care!
3084 	 */
3085 	if (code == CB_CODE_CPR_CHKPT) {
3086 		return (B_TRUE);
3087 	}
3088 
3089 	/*
3090 	 * When we get to here we are in the process of
3091 	 * resuming the system from a previous suspend.
3092 	 */
3093 	nfs4_client_resumed = gethrestime_sec();
3094 	return (B_TRUE);
3095 }
3096 
3097 void
3098 nfs4_renew_lease_thread(nfs4_server_t *sp)
3099 {
3100 	int	error = 0;
3101 	time_t	tmp_last_renewal_time, tmp_time, tmp_now_time, kip_secs;
3102 	clock_t	tick_delay = 0;
3103 	clock_t time_left = 0;
3104 	callb_cpr_t cpr_info;
3105 	kmutex_t cpr_lock;
3106 
3107 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3108 		"nfs4_renew_lease_thread: acting on sp 0x%p", (void*)sp));
3109 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
3110 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr, "nfsv4Lease");
3111 
3112 	mutex_enter(&sp->s_lock);
3113 	/* sp->s_lease_time is set via a GETATTR */
3114 	sp->last_renewal_time = gethrestime_sec();
3115 	sp->lease_valid = NFS4_LEASE_UNINITIALIZED;
3116 	ASSERT(sp->s_refcnt >= 1);
3117 
3118 	for (;;) {
3119 		if (!sp->state_ref_count ||
3120 			sp->lease_valid != NFS4_LEASE_VALID) {
3121 
3122 			kip_secs = MAX((sp->s_lease_time >> 1) -
3123 				(3 * sp->propagation_delay.tv_sec), 1);
3124 
3125 			tick_delay = SEC_TO_TICK(kip_secs);
3126 
3127 			NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3128 				"nfs4_renew_lease_thread: no renew : thread "
3129 				"wait %ld secs", kip_secs));
3130 
3131 			NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3132 				"nfs4_renew_lease_thread: no renew : "
3133 				"state_ref_count %d, lease_valid %d",
3134 				sp->state_ref_count, sp->lease_valid));
3135 
3136 			mutex_enter(&cpr_lock);
3137 			CALLB_CPR_SAFE_BEGIN(&cpr_info);
3138 			mutex_exit(&cpr_lock);
3139 			time_left = cv_timedwait(&sp->cv_thread_exit,
3140 				&sp->s_lock, tick_delay + lbolt);
3141 			mutex_enter(&cpr_lock);
3142 			CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock);
3143 			mutex_exit(&cpr_lock);
3144 
3145 			NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3146 				"nfs4_renew_lease_thread: no renew: "
3147 				"time left %ld", time_left));
3148 
3149 			if (sp->s_thread_exit == NFS4_THREAD_EXIT)
3150 				goto die;
3151 			continue;
3152 		}
3153 
3154 		tmp_last_renewal_time = sp->last_renewal_time;
3155 
3156 		tmp_time = gethrestime_sec() - sp->last_renewal_time +
3157 			(3 * sp->propagation_delay.tv_sec);
3158 
3159 		NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3160 			"nfs4_renew_lease_thread: tmp_time %ld, "
3161 			"sp->last_renewal_time %ld", tmp_time,
3162 			sp->last_renewal_time));
3163 
3164 		kip_secs = MAX((sp->s_lease_time >> 1) - tmp_time, 1);
3165 
3166 		tick_delay = SEC_TO_TICK(kip_secs);
3167 
3168 		NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3169 			"nfs4_renew_lease_thread: valid lease: sleep for %ld "
3170 			"secs", kip_secs));
3171 
3172 		mutex_enter(&cpr_lock);
3173 		CALLB_CPR_SAFE_BEGIN(&cpr_info);
3174 		mutex_exit(&cpr_lock);
3175 		time_left = cv_timedwait(&sp->cv_thread_exit, &sp->s_lock,
3176 			tick_delay + lbolt);
3177 		mutex_enter(&cpr_lock);
3178 		CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock);
3179 		mutex_exit(&cpr_lock);
3180 
3181 		NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3182 			"nfs4_renew_lease_thread: valid lease: time left %ld :"
3183 			"sp last_renewal_time %ld, nfs4_client_resumed %ld, "
3184 			"tmp_last_renewal_time %ld", time_left,
3185 			sp->last_renewal_time, nfs4_client_resumed,
3186 			tmp_last_renewal_time));
3187 
3188 		if (sp->s_thread_exit == NFS4_THREAD_EXIT)
3189 			goto die;
3190 
3191 		if (tmp_last_renewal_time == sp->last_renewal_time ||
3192 			(nfs4_client_resumed != 0 &&
3193 			nfs4_client_resumed > sp->last_renewal_time)) {
3194 			/*
3195 			 * Issue RENEW op since we haven't renewed the lease
3196 			 * since we slept.
3197 			 */
3198 			tmp_now_time = gethrestime_sec();
3199 			error = nfs4renew(sp);
3200 			/*
3201 			 * Need to re-acquire sp's lock, nfs4renew()
3202 			 * relinqueshes it.
3203 			 */
3204 			mutex_enter(&sp->s_lock);
3205 
3206 			/*
3207 			 * See if someone changed s_thread_exit while we gave
3208 			 * up s_lock.
3209 			 */
3210 			if (sp->s_thread_exit == NFS4_THREAD_EXIT)
3211 				goto die;
3212 
3213 			if (!error) {
3214 				/*
3215 				 * check to see if we implicitly renewed while
3216 				 * we waited for a reply for our RENEW call.
3217 				 */
3218 				if (tmp_last_renewal_time ==
3219 					sp->last_renewal_time) {
3220 					/* no implicit renew came */
3221 					sp->last_renewal_time = tmp_now_time;
3222 				} else {
3223 					NFS4_DEBUG(nfs4_client_lease_debug,
3224 						(CE_NOTE, "renew_thread: did "
3225 						"implicit renewal before reply "
3226 						"from server for RENEW"));
3227 				}
3228 			} else {
3229 				/* figure out error */
3230 				NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3231 					"renew_thread: nfs4renew returned error"
3232 					" %d", error));
3233 			}
3234 
3235 		}
3236 	}
3237 
3238 die:
3239 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3240 		"nfs4_renew_lease_thread: thread exiting"));
3241 
3242 	while (sp->s_otw_call_count != 0) {
3243 		NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3244 			"nfs4_renew_lease_thread: waiting for outstanding "
3245 			"otw calls to finish for sp 0x%p, current "
3246 			"s_otw_call_count %d", (void *)sp,
3247 			sp->s_otw_call_count));
3248 		mutex_enter(&cpr_lock);
3249 		CALLB_CPR_SAFE_BEGIN(&cpr_info);
3250 		mutex_exit(&cpr_lock);
3251 		cv_wait(&sp->s_cv_otw_count, &sp->s_lock);
3252 		mutex_enter(&cpr_lock);
3253 		CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock);
3254 		mutex_exit(&cpr_lock);
3255 	}
3256 	mutex_exit(&sp->s_lock);
3257 
3258 	nfs4_server_rele(sp);		/* free the thread's reference */
3259 	nfs4_server_rele(sp);		/* free the list's reference */
3260 	sp = NULL;
3261 
3262 done:
3263 	mutex_enter(&cpr_lock);
3264 	CALLB_CPR_EXIT(&cpr_info);	/* drops cpr_lock */
3265 	mutex_destroy(&cpr_lock);
3266 
3267 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3268 		"nfs4_renew_lease_thread: renew thread exit officially"));
3269 
3270 	zthread_exit();
3271 	/* NOT REACHED */
3272 }
3273 
3274 /*
3275  * Send out a RENEW op to the server.
3276  * Assumes sp is locked down.
3277  */
3278 static int
3279 nfs4renew(nfs4_server_t *sp)
3280 {
3281 	COMPOUND4args_clnt args;
3282 	COMPOUND4res_clnt res;
3283 	nfs_argop4 argop[1];
3284 	int doqueue = 1;
3285 	int rpc_error;
3286 	cred_t *cr;
3287 	mntinfo4_t *mi;
3288 	timespec_t prop_time, after_time;
3289 	int needrecov = FALSE;
3290 	nfs4_recov_state_t recov_state;
3291 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3292 
3293 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "nfs4renew"));
3294 
3295 	recov_state.rs_flags = 0;
3296 	recov_state.rs_num_retry_despite_err = 0;
3297 
3298 recov_retry:
3299 	mi = sp->mntinfo4_list;
3300 	VFS_HOLD(mi->mi_vfsp);
3301 	mutex_exit(&sp->s_lock);
3302 	ASSERT(mi != NULL);
3303 
3304 	e.error = nfs4_start_op(mi, NULL, NULL, &recov_state);
3305 	if (e.error) {
3306 		VFS_RELE(mi->mi_vfsp);
3307 		return (e.error);
3308 	}
3309 
3310 	/* Check to see if we're dealing with a marked-dead sp */
3311 	mutex_enter(&sp->s_lock);
3312 	if (sp->s_thread_exit == NFS4_THREAD_EXIT) {
3313 		mutex_exit(&sp->s_lock);
3314 		nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov);
3315 		VFS_RELE(mi->mi_vfsp);
3316 		return (0);
3317 	}
3318 
3319 	/* Make sure mi hasn't changed on us */
3320 	if (mi != sp->mntinfo4_list) {
3321 		/* Must drop sp's lock to avoid a recursive mutex enter */
3322 		mutex_exit(&sp->s_lock);
3323 		nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov);
3324 		VFS_RELE(mi->mi_vfsp);
3325 		mutex_enter(&sp->s_lock);
3326 		goto recov_retry;
3327 	}
3328 	mutex_exit(&sp->s_lock);
3329 
3330 	args.ctag = TAG_RENEW;
3331 
3332 	args.array_len = 1;
3333 	args.array = argop;
3334 
3335 	argop[0].argop = OP_RENEW;
3336 
3337 	mutex_enter(&sp->s_lock);
3338 	argop[0].nfs_argop4_u.oprenew.clientid = sp->clientid;
3339 	cr = sp->s_cred;
3340 	crhold(cr);
3341 	mutex_exit(&sp->s_lock);
3342 
3343 	ASSERT(cr != NULL);
3344 
3345 	/* used to figure out RTT for sp */
3346 	gethrestime(&prop_time);
3347 
3348 	NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
3349 	    "nfs4renew: %s call, sp 0x%p", needrecov ? "recov" : "first",
3350 	    (void*)sp));
3351 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "before: %ld s %ld ns ",
3352 		prop_time.tv_sec, prop_time.tv_nsec));
3353 
3354 	DTRACE_PROBE2(nfs4__renew__start, nfs4_server_t *, sp,
3355 			mntinfo4_t *, mi);
3356 
3357 	rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3358 	crfree(cr);
3359 
3360 	DTRACE_PROBE2(nfs4__renew__end, nfs4_server_t *, sp,
3361 			mntinfo4_t *, mi);
3362 
3363 	gethrestime(&after_time);
3364 
3365 	mutex_enter(&sp->s_lock);
3366 	sp->propagation_delay.tv_sec =
3367 		MAX(1, after_time.tv_sec - prop_time.tv_sec);
3368 	mutex_exit(&sp->s_lock);
3369 
3370 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "after : %ld s %ld ns ",
3371 		after_time.tv_sec, after_time.tv_nsec));
3372 
3373 	if (e.error == 0 && res.status == NFS4ERR_CB_PATH_DOWN) {
3374 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3375 		nfs4_delegreturn_all(sp);
3376 		nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov);
3377 		VFS_RELE(mi->mi_vfsp);
3378 		/*
3379 		 * If the server returns CB_PATH_DOWN, it has renewed
3380 		 * the lease and informed us that the callback path is
3381 		 * down.  Since the lease is renewed, just return 0 and
3382 		 * let the renew thread proceed as normal.
3383 		 */
3384 		return (0);
3385 	}
3386 
3387 	needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3388 	if (!needrecov && e.error) {
3389 		nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov);
3390 		VFS_RELE(mi->mi_vfsp);
3391 		return (e.error);
3392 	}
3393 
3394 	rpc_error = e.error;
3395 
3396 	if (needrecov) {
3397 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3398 		    "nfs4renew: initiating recovery\n"));
3399 
3400 		if (nfs4_start_recovery(&e, mi, NULL, NULL, NULL, NULL,
3401 		    OP_RENEW, NULL) == FALSE) {
3402 			nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov);
3403 			VFS_RELE(mi->mi_vfsp);
3404 			if (!e.error)
3405 				(void) xdr_free(xdr_COMPOUND4res_clnt,
3406 								(caddr_t)&res);
3407 			mutex_enter(&sp->s_lock);
3408 			goto recov_retry;
3409 		}
3410 		/* fall through for res.status case */
3411 	}
3412 
3413 	if (res.status) {
3414 		if (res.status == NFS4ERR_LEASE_MOVED) {
3415 			/*EMPTY*/
3416 			/*
3417 			 * XXX need to try every mntinfo4 in sp->mntinfo4_list
3418 			 * to renew the lease on that server
3419 			 */
3420 		}
3421 		e.error = geterrno4(res.status);
3422 	}
3423 
3424 	if (!rpc_error)
3425 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3426 
3427 	nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov);
3428 
3429 	VFS_RELE(mi->mi_vfsp);
3430 
3431 	return (e.error);
3432 }
3433 
3434 void
3435 nfs4_inc_state_ref_count(mntinfo4_t *mi)
3436 {
3437 	nfs4_server_t	*sp;
3438 
3439 	/* this locks down sp if it is found */
3440 	sp = find_nfs4_server(mi);
3441 
3442 	if (sp != NULL) {
3443 		nfs4_inc_state_ref_count_nolock(sp, mi);
3444 		mutex_exit(&sp->s_lock);
3445 		nfs4_server_rele(sp);
3446 	}
3447 }
3448 
3449 /*
3450  * Bump the number of OPEN files (ie: those with state) so we know if this
3451  * nfs4_server has any state to maintain a lease for or not.
3452  *
3453  * Also, marks the nfs4_server's lease valid if it hasn't been done so already.
3454  */
3455 void
3456 nfs4_inc_state_ref_count_nolock(nfs4_server_t *sp, mntinfo4_t *mi)
3457 {
3458 	ASSERT(mutex_owned(&sp->s_lock));
3459 
3460 	sp->state_ref_count++;
3461 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3462 		"nfs4_inc_state_ref_count: state_ref_count now %d",
3463 		sp->state_ref_count));
3464 
3465 	if (sp->lease_valid == NFS4_LEASE_UNINITIALIZED)
3466 		sp->lease_valid = NFS4_LEASE_VALID;
3467 
3468 	/*
3469 	 * If this call caused the lease to be marked valid and/or
3470 	 * took the state_ref_count from 0 to 1, then start the time
3471 	 * on lease renewal.
3472 	 */
3473 	if (sp->lease_valid == NFS4_LEASE_VALID && sp->state_ref_count == 1)
3474 		sp->last_renewal_time = gethrestime_sec();
3475 
3476 	/* update the number of open files for mi */
3477 	mi->mi_open_files++;
3478 }
3479 
3480 void
3481 nfs4_dec_state_ref_count(mntinfo4_t *mi)
3482 {
3483 	nfs4_server_t	*sp;
3484 
3485 	/* this locks down sp if it is found */
3486 	sp = find_nfs4_server_all(mi, 1);
3487 
3488 	if (sp != NULL) {
3489 		nfs4_dec_state_ref_count_nolock(sp, mi);
3490 		mutex_exit(&sp->s_lock);
3491 		nfs4_server_rele(sp);
3492 	}
3493 }
3494 
3495 /*
3496  * Decrement the number of OPEN files (ie: those with state) so we know if
3497  * this nfs4_server has any state to maintain a lease for or not.
3498  */
3499 void
3500 nfs4_dec_state_ref_count_nolock(nfs4_server_t *sp, mntinfo4_t *mi)
3501 {
3502 	ASSERT(mutex_owned(&sp->s_lock));
3503 	ASSERT(sp->state_ref_count != 0);
3504 	sp->state_ref_count--;
3505 
3506 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3507 		"nfs4_dec_state_ref_count: state ref count now %d",
3508 		sp->state_ref_count));
3509 
3510 	mi->mi_open_files--;
3511 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3512 		"nfs4_dec_state_ref_count: mi open files %d, v4 flags 0x%x",
3513 		mi->mi_open_files, mi->mi_flags));
3514 
3515 	/* We don't have to hold the mi_lock to test mi_flags */
3516 	if (mi->mi_open_files == 0 &&
3517 	    (mi->mi_flags & MI4_REMOVE_ON_LAST_CLOSE)) {
3518 		NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3519 			"nfs4_dec_state_ref_count: remove mntinfo4 %p since "
3520 			"we have closed the last open file", (void*)mi));
3521 		nfs4_remove_mi_from_server(mi, sp);
3522 	}
3523 }
3524 
3525 bool_t
3526 inlease(nfs4_server_t *sp)
3527 {
3528 	bool_t result;
3529 
3530 	ASSERT(mutex_owned(&sp->s_lock));
3531 
3532 	if (sp->lease_valid == NFS4_LEASE_VALID &&
3533 	    gethrestime_sec() < sp->last_renewal_time + sp->s_lease_time)
3534 		result = TRUE;
3535 	else
3536 		result = FALSE;
3537 
3538 	return (result);
3539 }
3540 
3541 
3542 /*
3543  * Return non-zero if the given nfs4_server_t is going through recovery.
3544  */
3545 
3546 int
3547 nfs4_server_in_recovery(nfs4_server_t *sp)
3548 {
3549 	return (nfs_rw_lock_held(&sp->s_recovlock, RW_WRITER));
3550 }
3551 
3552 /*
3553  * Compare two shared filehandle objects.  Returns -1, 0, or +1, if the
3554  * first is less than, equal to, or greater than the second.
3555  */
3556 
3557 int
3558 sfh4cmp(const void *p1, const void *p2)
3559 {
3560 	const nfs4_sharedfh_t *sfh1 = (const nfs4_sharedfh_t *)p1;
3561 	const nfs4_sharedfh_t *sfh2 = (const nfs4_sharedfh_t *)p2;
3562 
3563 	return (nfs4cmpfh(&sfh1->sfh_fh, &sfh2->sfh_fh));
3564 }
3565 
3566 /*
3567  * Create a table for shared filehandle objects.
3568  */
3569 
3570 void
3571 sfh4_createtab(avl_tree_t *tab)
3572 {
3573 	avl_create(tab, sfh4cmp, sizeof (nfs4_sharedfh_t),
3574 		offsetof(nfs4_sharedfh_t, sfh_tree));
3575 }
3576 
3577 /*
3578  * Return a shared filehandle object for the given filehandle.  The caller
3579  * is responsible for eventually calling sfh4_rele().
3580  */
3581 
3582 nfs4_sharedfh_t *
3583 sfh4_put(const nfs_fh4 *fh, mntinfo4_t *mi, nfs4_sharedfh_t *key)
3584 {
3585 	nfs4_sharedfh_t *sfh, *nsfh;
3586 	avl_index_t where;
3587 	nfs4_sharedfh_t skey;
3588 
3589 	if (!key) {
3590 		skey.sfh_fh = *fh;
3591 		key = &skey;
3592 	}
3593 
3594 	nsfh = kmem_alloc(sizeof (nfs4_sharedfh_t), KM_SLEEP);
3595 	nsfh->sfh_fh.nfs_fh4_len = fh->nfs_fh4_len;
3596 	/*
3597 	 * We allocate the largest possible filehandle size because it's
3598 	 * not that big, and it saves us from possibly having to resize the
3599 	 * buffer later.
3600 	 */
3601 	nsfh->sfh_fh.nfs_fh4_val = kmem_alloc(NFS4_FHSIZE, KM_SLEEP);
3602 	bcopy(fh->nfs_fh4_val, nsfh->sfh_fh.nfs_fh4_val, fh->nfs_fh4_len);
3603 	mutex_init(&nsfh->sfh_lock, NULL, MUTEX_DEFAULT, NULL);
3604 	nsfh->sfh_refcnt = 1;
3605 	nsfh->sfh_flags = SFH4_IN_TREE;
3606 	nsfh->sfh_mi = mi;
3607 	NFS4_DEBUG(nfs4_sharedfh_debug, (CE_NOTE, "sfh4_get: new object (%p)",
3608 			(void *)nsfh));
3609 
3610 	(void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_WRITER, 0);
3611 	sfh = avl_find(&mi->mi_filehandles, key, &where);
3612 	if (sfh != NULL) {
3613 		mutex_enter(&sfh->sfh_lock);
3614 		sfh->sfh_refcnt++;
3615 		mutex_exit(&sfh->sfh_lock);
3616 		nfs_rw_exit(&mi->mi_fh_lock);
3617 		/* free our speculative allocs */
3618 		kmem_free(nsfh->sfh_fh.nfs_fh4_val, NFS4_FHSIZE);
3619 		kmem_free(nsfh, sizeof (nfs4_sharedfh_t));
3620 		return (sfh);
3621 	}
3622 
3623 	avl_insert(&mi->mi_filehandles, nsfh, where);
3624 	nfs_rw_exit(&mi->mi_fh_lock);
3625 
3626 	return (nsfh);
3627 }
3628 
3629 /*
3630  * Return a shared filehandle object for the given filehandle.  The caller
3631  * is responsible for eventually calling sfh4_rele().
3632  */
3633 
3634 nfs4_sharedfh_t *
3635 sfh4_get(const nfs_fh4 *fh, mntinfo4_t *mi)
3636 {
3637 	nfs4_sharedfh_t *sfh;
3638 	nfs4_sharedfh_t key;
3639 
3640 	ASSERT(fh->nfs_fh4_len <= NFS4_FHSIZE);
3641 
3642 #ifdef DEBUG
3643 	if (nfs4_sharedfh_debug) {
3644 		nfs4_fhandle_t fhandle;
3645 
3646 		fhandle.fh_len = fh->nfs_fh4_len;
3647 		bcopy(fh->nfs_fh4_val, fhandle.fh_buf, fhandle.fh_len);
3648 		zcmn_err(mi->mi_zone->zone_id, CE_NOTE, "sfh4_get:");
3649 		nfs4_printfhandle(&fhandle);
3650 	}
3651 #endif
3652 
3653 	/*
3654 	 * If there's already an object for the given filehandle, bump the
3655 	 * reference count and return it.  Otherwise, create a new object
3656 	 * and add it to the AVL tree.
3657 	 */
3658 
3659 	key.sfh_fh = *fh;
3660 
3661 	(void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
3662 	sfh = avl_find(&mi->mi_filehandles, &key, NULL);
3663 	if (sfh != NULL) {
3664 		mutex_enter(&sfh->sfh_lock);
3665 		sfh->sfh_refcnt++;
3666 		NFS4_DEBUG(nfs4_sharedfh_debug, (CE_NOTE,
3667 			"sfh4_get: found existing %p, new refcnt=%d",
3668 			(void *)sfh, sfh->sfh_refcnt));
3669 		mutex_exit(&sfh->sfh_lock);
3670 		nfs_rw_exit(&mi->mi_fh_lock);
3671 		return (sfh);
3672 	}
3673 	nfs_rw_exit(&mi->mi_fh_lock);
3674 
3675 	return (sfh4_put(fh, mi, &key));
3676 }
3677 
3678 /*
3679  * Get a reference to the given shared filehandle object.
3680  */
3681 
3682 void
3683 sfh4_hold(nfs4_sharedfh_t *sfh)
3684 {
3685 	ASSERT(sfh->sfh_refcnt > 0);
3686 
3687 	mutex_enter(&sfh->sfh_lock);
3688 	sfh->sfh_refcnt++;
3689 	NFS4_DEBUG(nfs4_sharedfh_debug,
3690 		(CE_NOTE, "sfh4_hold %p, new refcnt=%d",
3691 		(void *)sfh, sfh->sfh_refcnt));
3692 	mutex_exit(&sfh->sfh_lock);
3693 }
3694 
3695 /*
3696  * Release a reference to the given shared filehandle object and null out
3697  * the given pointer.
3698  */
3699 
3700 void
3701 sfh4_rele(nfs4_sharedfh_t **sfhpp)
3702 {
3703 	mntinfo4_t *mi;
3704 	nfs4_sharedfh_t *sfh = *sfhpp;
3705 
3706 	ASSERT(sfh->sfh_refcnt > 0);
3707 
3708 	mutex_enter(&sfh->sfh_lock);
3709 	if (sfh->sfh_refcnt > 1) {
3710 		sfh->sfh_refcnt--;
3711 		NFS4_DEBUG(nfs4_sharedfh_debug, (CE_NOTE,
3712 		    "sfh4_rele %p, new refcnt=%d",
3713 		    (void *)sfh, sfh->sfh_refcnt));
3714 		mutex_exit(&sfh->sfh_lock);
3715 		goto finish;
3716 	}
3717 	mutex_exit(&sfh->sfh_lock);
3718 
3719 	/*
3720 	 * Possibly the last reference, so get the lock for the table in
3721 	 * case it's time to remove the object from the table.
3722 	 */
3723 	mi = sfh->sfh_mi;
3724 	(void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_WRITER, 0);
3725 	mutex_enter(&sfh->sfh_lock);
3726 	sfh->sfh_refcnt--;
3727 	if (sfh->sfh_refcnt > 0) {
3728 		NFS4_DEBUG(nfs4_sharedfh_debug, (CE_NOTE,
3729 		    "sfh4_rele %p, new refcnt=%d",
3730 		    (void *)sfh, sfh->sfh_refcnt));
3731 		mutex_exit(&sfh->sfh_lock);
3732 		nfs_rw_exit(&mi->mi_fh_lock);
3733 		goto finish;
3734 	}
3735 
3736 	NFS4_DEBUG(nfs4_sharedfh_debug, (CE_NOTE,
3737 		"sfh4_rele %p, last ref", (void *)sfh));
3738 	if (sfh->sfh_flags & SFH4_IN_TREE) {
3739 		avl_remove(&mi->mi_filehandles, sfh);
3740 		sfh->sfh_flags &= ~SFH4_IN_TREE;
3741 	}
3742 	mutex_exit(&sfh->sfh_lock);
3743 	nfs_rw_exit(&mi->mi_fh_lock);
3744 	mutex_destroy(&sfh->sfh_lock);
3745 	kmem_free(sfh->sfh_fh.nfs_fh4_val, NFS4_FHSIZE);
3746 	kmem_free(sfh, sizeof (nfs4_sharedfh_t));
3747 
3748 finish:
3749 	*sfhpp = NULL;
3750 }
3751 
3752 /*
3753  * Update the filehandle for the given shared filehandle object.
3754  */
3755 
3756 int nfs4_warn_dupfh = 0;	/* if set, always warn about dup fhs below */
3757 
3758 void
3759 sfh4_update(nfs4_sharedfh_t *sfh, const nfs_fh4 *newfh)
3760 {
3761 	mntinfo4_t *mi = sfh->sfh_mi;
3762 	nfs4_sharedfh_t *dupsfh;
3763 	avl_index_t where;
3764 	nfs4_sharedfh_t key;
3765 
3766 #ifdef DEBUG
3767 	mutex_enter(&sfh->sfh_lock);
3768 	ASSERT(sfh->sfh_refcnt > 0);
3769 	mutex_exit(&sfh->sfh_lock);
3770 #endif
3771 	ASSERT(newfh->nfs_fh4_len <= NFS4_FHSIZE);
3772 
3773 	/*
3774 	 * The basic plan is to remove the shared filehandle object from
3775 	 * the table, update it to have the new filehandle, then reinsert
3776 	 * it.
3777 	 */
3778 
3779 	(void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_WRITER, 0);
3780 	mutex_enter(&sfh->sfh_lock);
3781 	if (sfh->sfh_flags & SFH4_IN_TREE) {
3782 		avl_remove(&mi->mi_filehandles, sfh);
3783 		sfh->sfh_flags &= ~SFH4_IN_TREE;
3784 	}
3785 	mutex_exit(&sfh->sfh_lock);
3786 	sfh->sfh_fh.nfs_fh4_len = newfh->nfs_fh4_len;
3787 	bcopy(newfh->nfs_fh4_val, sfh->sfh_fh.nfs_fh4_val,
3788 	    sfh->sfh_fh.nfs_fh4_len);
3789 
3790 	/*
3791 	 * XXX If there is already a shared filehandle object with the new
3792 	 * filehandle, we're in trouble, because the rnode code assumes
3793 	 * that there is only one shared filehandle object for a given
3794 	 * filehandle.  So issue a warning (for read-write mounts only)
3795 	 * and don't try to re-insert the given object into the table.
3796 	 * Hopefully the given object will quickly go away and everyone
3797 	 * will use the new object.
3798 	 */
3799 	key.sfh_fh = *newfh;
3800 	dupsfh = avl_find(&mi->mi_filehandles, &key, &where);
3801 	if (dupsfh != NULL) {
3802 		if (!(mi->mi_vfsp->vfs_flag & VFS_RDONLY) || nfs4_warn_dupfh) {
3803 			zcmn_err(mi->mi_zone->zone_id, CE_WARN, "sfh4_update: "
3804 			    "duplicate filehandle detected");
3805 			sfh4_printfhandle(dupsfh);
3806 		}
3807 	} else {
3808 		avl_insert(&mi->mi_filehandles, sfh, where);
3809 		mutex_enter(&sfh->sfh_lock);
3810 		sfh->sfh_flags |= SFH4_IN_TREE;
3811 		mutex_exit(&sfh->sfh_lock);
3812 	}
3813 	nfs_rw_exit(&mi->mi_fh_lock);
3814 }
3815 
3816 /*
3817  * Copy out the current filehandle for the given shared filehandle object.
3818  */
3819 
3820 void
3821 sfh4_copyval(const nfs4_sharedfh_t *sfh, nfs4_fhandle_t *fhp)
3822 {
3823 	mntinfo4_t *mi = sfh->sfh_mi;
3824 
3825 	ASSERT(sfh->sfh_refcnt > 0);
3826 
3827 	(void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
3828 	fhp->fh_len = sfh->sfh_fh.nfs_fh4_len;
3829 	ASSERT(fhp->fh_len <= NFS4_FHSIZE);
3830 	bcopy(sfh->sfh_fh.nfs_fh4_val, fhp->fh_buf, fhp->fh_len);
3831 	nfs_rw_exit(&mi->mi_fh_lock);
3832 }
3833 
3834 /*
3835  * Print out the filehandle for the given shared filehandle object.
3836  */
3837 
3838 void
3839 sfh4_printfhandle(const nfs4_sharedfh_t *sfh)
3840 {
3841 	nfs4_fhandle_t fhandle;
3842 
3843 	sfh4_copyval(sfh, &fhandle);
3844 	nfs4_printfhandle(&fhandle);
3845 }
3846 
3847 /*
3848  * Compare 2 fnames.  Returns -1 if the first is "less" than the second, 0
3849  * if they're the same, +1 if the first is "greater" than the second.  The
3850  * caller (or whoever's calling the AVL package) is responsible for
3851  * handling locking issues.
3852  */
3853 
3854 static int
3855 fncmp(const void *p1, const void *p2)
3856 {
3857 	const nfs4_fname_t *f1 = p1;
3858 	const nfs4_fname_t *f2 = p2;
3859 	int res;
3860 
3861 	res = strcmp(f1->fn_name, f2->fn_name);
3862 	/*
3863 	 * The AVL package wants +/-1, not arbitrary positive or negative
3864 	 * integers.
3865 	 */
3866 	if (res > 0)
3867 		res = 1;
3868 	else if (res < 0)
3869 		res = -1;
3870 	return (res);
3871 }
3872 
3873 /*
3874  * Get or create an fname with the given name, as a child of the given
3875  * fname.  The caller is responsible for eventually releasing the reference
3876  * (fn_rele()).  parent may be NULL.
3877  */
3878 
3879 nfs4_fname_t *
3880 fn_get(nfs4_fname_t *parent, char *name)
3881 {
3882 	nfs4_fname_t key;
3883 	nfs4_fname_t *fnp;
3884 	avl_index_t where;
3885 
3886 	key.fn_name = name;
3887 
3888 	/*
3889 	 * If there's already an fname registered with the given name, bump
3890 	 * its reference count and return it.  Otherwise, create a new one
3891 	 * and add it to the parent's AVL tree.
3892 	 */
3893 
3894 	if (parent != NULL) {
3895 		mutex_enter(&parent->fn_lock);
3896 		fnp = avl_find(&parent->fn_children, &key, &where);
3897 		if (fnp != NULL) {
3898 			fn_hold(fnp);
3899 			mutex_exit(&parent->fn_lock);
3900 			return (fnp);
3901 		}
3902 	}
3903 
3904 	fnp = kmem_alloc(sizeof (nfs4_fname_t), KM_SLEEP);
3905 	mutex_init(&fnp->fn_lock, NULL, MUTEX_DEFAULT, NULL);
3906 	fnp->fn_parent = parent;
3907 	if (parent != NULL)
3908 		fn_hold(parent);
3909 	fnp->fn_len = strlen(name);
3910 	ASSERT(fnp->fn_len < MAXNAMELEN);
3911 	fnp->fn_name = kmem_alloc(fnp->fn_len + 1, KM_SLEEP);
3912 	(void) strcpy(fnp->fn_name, name);
3913 	fnp->fn_refcnt = 1;
3914 	avl_create(&fnp->fn_children, fncmp, sizeof (nfs4_fname_t),
3915 	    offsetof(nfs4_fname_t, fn_tree));
3916 	NFS4_DEBUG(nfs4_fname_debug, (CE_NOTE,
3917 		"fn_get %p:%s, a new nfs4_fname_t!",
3918 		(void *)fnp, fnp->fn_name));
3919 	if (parent != NULL) {
3920 		avl_insert(&parent->fn_children, fnp, where);
3921 		mutex_exit(&parent->fn_lock);
3922 	}
3923 
3924 	return (fnp);
3925 }
3926 
3927 void
3928 fn_hold(nfs4_fname_t *fnp)
3929 {
3930 	atomic_add_32(&fnp->fn_refcnt, 1);
3931 	NFS4_DEBUG(nfs4_fname_debug, (CE_NOTE,
3932 		"fn_hold %p:%s, new refcnt=%d",
3933 		(void *)fnp, fnp->fn_name, fnp->fn_refcnt));
3934 }
3935 
3936 /*
3937  * Decrement the reference count of the given fname, and destroy it if its
3938  * reference count goes to zero.  Nulls out the given pointer.
3939  */
3940 
3941 void
3942 fn_rele(nfs4_fname_t **fnpp)
3943 {
3944 	nfs4_fname_t *parent;
3945 	uint32_t newref;
3946 	nfs4_fname_t *fnp;
3947 
3948 recur:
3949 	fnp = *fnpp;
3950 	*fnpp = NULL;
3951 
3952 	mutex_enter(&fnp->fn_lock);
3953 	parent = fnp->fn_parent;
3954 	if (parent != NULL)
3955 		mutex_enter(&parent->fn_lock);	/* prevent new references */
3956 	newref = atomic_add_32_nv(&fnp->fn_refcnt, -1);
3957 	if (newref > 0) {
3958 		NFS4_DEBUG(nfs4_fname_debug, (CE_NOTE,
3959 			"fn_rele %p:%s, new refcnt=%d",
3960 			(void *)fnp, fnp->fn_name, fnp->fn_refcnt));
3961 		if (parent != NULL)
3962 			mutex_exit(&parent->fn_lock);
3963 		mutex_exit(&fnp->fn_lock);
3964 		return;
3965 	}
3966 
3967 	NFS4_DEBUG(nfs4_fname_debug, (CE_NOTE,
3968 		"fn_rele %p:%s, last reference, deleting...",
3969 		(void *)fnp, fnp->fn_name));
3970 	if (parent != NULL) {
3971 		avl_remove(&parent->fn_children, fnp);
3972 		mutex_exit(&parent->fn_lock);
3973 	}
3974 	kmem_free(fnp->fn_name, fnp->fn_len + 1);
3975 	mutex_destroy(&fnp->fn_lock);
3976 	avl_destroy(&fnp->fn_children);
3977 	kmem_free(fnp, sizeof (nfs4_fname_t));
3978 	/*
3979 	 * Recursivly fn_rele the parent.
3980 	 * Use goto instead of a recursive call to avoid stack overflow.
3981 	 */
3982 	if (parent != NULL) {
3983 		fnpp = &parent;
3984 		goto recur;
3985 	}
3986 }
3987 
3988 /*
3989  * Returns the single component name of the given fname, in a MAXNAMELEN
3990  * string buffer, which the caller is responsible for freeing.  Note that
3991  * the name may become invalid as a result of fn_move().
3992  */
3993 
3994 char *
3995 fn_name(nfs4_fname_t *fnp)
3996 {
3997 	char *name;
3998 
3999 	ASSERT(fnp->fn_len < MAXNAMELEN);
4000 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4001 	mutex_enter(&fnp->fn_lock);
4002 	(void) strcpy(name, fnp->fn_name);
4003 	mutex_exit(&fnp->fn_lock);
4004 
4005 	return (name);
4006 }
4007 
4008 
4009 /*
4010  * fn_path_realloc
4011  *
4012  * This function, used only by fn_path, constructs
4013  * a new string which looks like "prepend" + "/" + "current".
4014  * by allocating a new string and freeing the old one.
4015  */
4016 static void
4017 fn_path_realloc(char **curses, char *prepend)
4018 {
4019 	int len, curlen = 0;
4020 	char *news;
4021 
4022 	if (*curses == NULL) {
4023 		/*
4024 		 * Prime the pump, allocate just the
4025 		 * space for prepend and return that.
4026 		 */
4027 		len = strlen(prepend) + 1;
4028 		news = kmem_alloc(len, KM_SLEEP);
4029 		(void) strncpy(news, prepend, len);
4030 	} else {
4031 		/*
4032 		 * Allocate the space  for a new string
4033 		 * +1 +1 is for the "/" and the NULL
4034 		 * byte at the end of it all.
4035 		 */
4036 		curlen = strlen(*curses);
4037 		len = curlen + strlen(prepend) + 1 + 1;
4038 		news = kmem_alloc(len, KM_SLEEP);
4039 		(void) strncpy(news, prepend, len);
4040 		(void) strcat(news, "/");
4041 		(void) strcat(news, *curses);
4042 		kmem_free(*curses, curlen + 1);
4043 	}
4044 	*curses = news;
4045 }
4046 
4047 /*
4048  * Returns the path name (starting from the fs root) for the given fname.
4049  * The caller is responsible for freeing.  Note that the path may be or
4050  * become invalid as a result of fn_move().
4051  */
4052 
4053 char *
4054 fn_path(nfs4_fname_t *fnp)
4055 {
4056 	char *path;
4057 	nfs4_fname_t *nextfnp;
4058 
4059 	if (fnp == NULL)
4060 		return (NULL);
4061 
4062 	path = NULL;
4063 
4064 	/* walk up the tree constructing the pathname.  */
4065 
4066 	fn_hold(fnp);			/* adjust for later rele */
4067 	do {
4068 		mutex_enter(&fnp->fn_lock);
4069 		/*
4070 		 * Add fn_name in front of the current path
4071 		 */
4072 		fn_path_realloc(&path, fnp->fn_name);
4073 		nextfnp = fnp->fn_parent;
4074 		if (nextfnp != NULL)
4075 			fn_hold(nextfnp);
4076 		mutex_exit(&fnp->fn_lock);
4077 		fn_rele(&fnp);
4078 		fnp = nextfnp;
4079 	} while (fnp != NULL);
4080 
4081 	return (path);
4082 }
4083 
4084 /*
4085  * Return a reference to the parent of the given fname, which the caller is
4086  * responsible for eventually releasing.
4087  */
4088 
4089 nfs4_fname_t *
4090 fn_parent(nfs4_fname_t *fnp)
4091 {
4092 	nfs4_fname_t *parent;
4093 
4094 	mutex_enter(&fnp->fn_lock);
4095 	parent = fnp->fn_parent;
4096 	if (parent != NULL)
4097 		fn_hold(parent);
4098 	mutex_exit(&fnp->fn_lock);
4099 
4100 	return (parent);
4101 }
4102 
4103 /*
4104  * Update fnp so that its parent is newparent and its name is newname.
4105  */
4106 
4107 void
4108 fn_move(nfs4_fname_t *fnp, nfs4_fname_t *newparent, char *newname)
4109 {
4110 	nfs4_fname_t *parent, *tmpfnp;
4111 	ssize_t newlen;
4112 	nfs4_fname_t key;
4113 	avl_index_t where;
4114 
4115 	/*
4116 	 * This assert exists to catch the client trying to rename
4117 	 * a dir to be a child of itself.  This happened at a recent
4118 	 * bakeoff against a 3rd party (broken) server which allowed
4119 	 * the rename to succeed.  If it trips it means that:
4120 	 *	a) the code in nfs4rename that detects this case is broken
4121 	 *	b) the server is broken (since it allowed the bogus rename)
4122 	 *
4123 	 * For non-DEBUG kernels, prepare for a recursive mutex_enter
4124 	 * panic below from:  mutex_enter(&newparent->fn_lock);
4125 	 */
4126 	ASSERT(fnp != newparent);
4127 
4128 	/*
4129 	 * Remove fnp from its current parent, change its name, then add it
4130 	 * to newparent.
4131 	 */
4132 	mutex_enter(&fnp->fn_lock);
4133 	parent = fnp->fn_parent;
4134 	mutex_enter(&parent->fn_lock);
4135 	avl_remove(&parent->fn_children, fnp);
4136 	mutex_exit(&parent->fn_lock);
4137 	fn_rele(&fnp->fn_parent);
4138 
4139 	newlen = strlen(newname);
4140 	if (newlen != fnp->fn_len) {
4141 		ASSERT(newlen < MAXNAMELEN);
4142 		kmem_free(fnp->fn_name, fnp->fn_len + 1);
4143 		fnp->fn_name = kmem_alloc(newlen + 1, KM_SLEEP);
4144 		fnp->fn_len = newlen;
4145 	}
4146 	(void) strcpy(fnp->fn_name, newname);
4147 
4148 again:
4149 	mutex_enter(&newparent->fn_lock);
4150 	key.fn_name = fnp->fn_name;
4151 	tmpfnp = avl_find(&newparent->fn_children, &key, &where);
4152 	if (tmpfnp != NULL) {
4153 		/*
4154 		 * This could be due to a file that was unlinked while
4155 		 * open, or perhaps the rnode is in the free list.  Remove
4156 		 * it from newparent and let it go away on its own.  The
4157 		 * contorted code is to deal with lock order issues and
4158 		 * race conditions.
4159 		 */
4160 		fn_hold(tmpfnp);
4161 		mutex_exit(&newparent->fn_lock);
4162 		mutex_enter(&tmpfnp->fn_lock);
4163 		if (tmpfnp->fn_parent == newparent) {
4164 			mutex_enter(&newparent->fn_lock);
4165 			avl_remove(&newparent->fn_children, tmpfnp);
4166 			mutex_exit(&newparent->fn_lock);
4167 			fn_rele(&tmpfnp->fn_parent);
4168 		}
4169 		mutex_exit(&tmpfnp->fn_lock);
4170 		fn_rele(&tmpfnp);
4171 		goto again;
4172 	}
4173 	fnp->fn_parent = newparent;
4174 	fn_hold(newparent);
4175 	avl_insert(&newparent->fn_children, fnp, where);
4176 	mutex_exit(&newparent->fn_lock);
4177 	mutex_exit(&fnp->fn_lock);
4178 }
4179 
4180 #ifdef DEBUG
4181 /*
4182  * Return non-zero if the type information makes sense for the given vnode.
4183  * Otherwise panic.
4184  */
4185 int
4186 nfs4_consistent_type(vnode_t *vp)
4187 {
4188 	rnode4_t *rp = VTOR4(vp);
4189 
4190 	if (nfs4_vtype_debug && vp->v_type != VNON &&
4191 	    rp->r_attr.va_type != VNON && vp->v_type != rp->r_attr.va_type) {
4192 		cmn_err(CE_PANIC, "vnode %p type mismatch; v_type=%d, "
4193 			"rnode attr type=%d", (void *)vp, vp->v_type,
4194 			rp->r_attr.va_type);
4195 	}
4196 
4197 	return (1);
4198 }
4199 #endif /* DEBUG */
4200