xref: /titanic_41/usr/src/uts/common/fs/nfs/nfs_client.c (revision 4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  *  	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
26  *	All rights reserved.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/thread.h>
33 #include <sys/t_lock.h>
34 #include <sys/time.h>
35 #include <sys/vnode.h>
36 #include <sys/vfs.h>
37 #include <sys/errno.h>
38 #include <sys/buf.h>
39 #include <sys/stat.h>
40 #include <sys/cred.h>
41 #include <sys/kmem.h>
42 #include <sys/debug.h>
43 #include <sys/dnlc.h>
44 #include <sys/vmsystm.h>
45 #include <sys/flock.h>
46 #include <sys/share.h>
47 #include <sys/cmn_err.h>
48 #include <sys/tiuser.h>
49 #include <sys/sysmacros.h>
50 #include <sys/callb.h>
51 #include <sys/acl.h>
52 #include <sys/kstat.h>
53 #include <sys/signal.h>
54 #include <sys/list.h>
55 #include <sys/zone.h>
56 
57 #include <rpc/types.h>
58 #include <rpc/xdr.h>
59 #include <rpc/auth.h>
60 #include <rpc/clnt.h>
61 
62 #include <nfs/nfs.h>
63 #include <nfs/nfs_clnt.h>
64 
65 #include <nfs/rnode.h>
66 #include <nfs/nfs_acl.h>
67 #include <nfs/lm.h>
68 
69 #include <vm/hat.h>
70 #include <vm/as.h>
71 #include <vm/page.h>
72 #include <vm/pvn.h>
73 #include <vm/seg.h>
74 #include <vm/seg_map.h>
75 #include <vm/seg_vn.h>
76 
77 static void	nfs3_attr_cache(vnode_t *, vattr_t *, vattr_t *, hrtime_t,
78 			cred_t *);
79 static int	nfs_getattr_cache(vnode_t *, struct vattr *);
80 static int	nfs_remove_locking_id(vnode_t *, int, char *, char *, int *);
81 
82 struct mi_globals {
83 	kmutex_t	mig_lock;  /* lock protecting mig_list */
84 	list_t		mig_list;  /* list of NFS v2 or v3 mounts in zone */
85 	boolean_t	mig_destructor_called;
86 };
87 
88 static zone_key_t mi_list_key;
89 
90 /* Debugging flag for PC file shares. */
91 extern int	share_debug;
92 
93 /*
94  * Attributes caching:
95  *
96  * Attributes are cached in the rnode in struct vattr form.
97  * There is a time associated with the cached attributes (r_attrtime)
98  * which tells whether the attributes are valid. The time is initialized
99  * to the difference between current time and the modify time of the vnode
100  * when new attributes are cached. This allows the attributes for
101  * files that have changed recently to be timed out sooner than for files
102  * that have not changed for a long time. There are minimum and maximum
103  * timeout values that can be set per mount point.
104  */
105 
106 int
107 nfs_waitfor_purge_complete(vnode_t *vp)
108 {
109 	rnode_t *rp;
110 	k_sigset_t smask;
111 
112 	rp = VTOR(vp);
113 	if (rp->r_serial != NULL && rp->r_serial != curthread) {
114 		mutex_enter(&rp->r_statelock);
115 		sigintr(&smask, VTOMI(vp)->mi_flags & MI_INT);
116 		while (rp->r_serial != NULL) {
117 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
118 				sigunintr(&smask);
119 				mutex_exit(&rp->r_statelock);
120 				return (EINTR);
121 			}
122 		}
123 		sigunintr(&smask);
124 		mutex_exit(&rp->r_statelock);
125 	}
126 	return (0);
127 }
128 
129 /*
130  * Validate caches by checking cached attributes. If the cached
131  * attributes have timed out, then get new attributes from the server.
132  * As a side affect, this will do cache invalidation if the attributes
133  * have changed.
134  *
135  * If the attributes have not timed out and if there is a cache
136  * invalidation being done by some other thread, then wait until that
137  * thread has completed the cache invalidation.
138  */
139 int
140 nfs_validate_caches(vnode_t *vp, cred_t *cr)
141 {
142 	int error;
143 	struct vattr va;
144 
145 	if (ATTRCACHE_VALID(vp)) {
146 		error = nfs_waitfor_purge_complete(vp);
147 		if (error)
148 			return (error);
149 		return (0);
150 	}
151 
152 	va.va_mask = AT_ALL;
153 	return (nfs_getattr_otw(vp, &va, cr));
154 }
155 
156 /*
157  * Validate caches by checking cached attributes. If the cached
158  * attributes have timed out, then get new attributes from the server.
159  * As a side affect, this will do cache invalidation if the attributes
160  * have changed.
161  *
162  * If the attributes have not timed out and if there is a cache
163  * invalidation being done by some other thread, then wait until that
164  * thread has completed the cache invalidation.
165  */
166 int
167 nfs3_validate_caches(vnode_t *vp, cred_t *cr)
168 {
169 	int error;
170 	struct vattr va;
171 
172 	if (ATTRCACHE_VALID(vp)) {
173 		error = nfs_waitfor_purge_complete(vp);
174 		if (error)
175 			return (error);
176 		return (0);
177 	}
178 
179 	va.va_mask = AT_ALL;
180 	return (nfs3_getattr_otw(vp, &va, cr));
181 }
182 
183 /*
184  * Purge all of the various NFS `data' caches.
185  */
186 void
187 nfs_purge_caches(vnode_t *vp, int purge_dnlc, cred_t *cr)
188 {
189 	rnode_t *rp;
190 	char *contents;
191 	int size;
192 	int error;
193 
194 	/*
195 	 * Purge the DNLC for any entries which refer to this file.
196 	 * Avoid recursive entry into dnlc_purge_vp() in case of a directory.
197 	 */
198 	rp = VTOR(vp);
199 	mutex_enter(&rp->r_statelock);
200 	if (vp->v_count > 1 &&
201 	    (vp->v_type == VDIR || purge_dnlc == NFS_PURGE_DNLC) &&
202 	    !(rp->r_flags & RINDNLCPURGE)) {
203 		/*
204 		 * Set the RINDNLCPURGE flag to prevent recursive entry
205 		 * into dnlc_purge_vp()
206 		 */
207 		if (vp->v_type == VDIR)
208 			rp->r_flags |= RINDNLCPURGE;
209 		mutex_exit(&rp->r_statelock);
210 		dnlc_purge_vp(vp);
211 		mutex_enter(&rp->r_statelock);
212 		if (rp->r_flags & RINDNLCPURGE)
213 			rp->r_flags &= ~RINDNLCPURGE;
214 	}
215 
216 	/*
217 	 * Clear any readdir state bits and purge the readlink response cache.
218 	 */
219 	contents = rp->r_symlink.contents;
220 	size = rp->r_symlink.size;
221 	rp->r_symlink.contents = NULL;
222 	mutex_exit(&rp->r_statelock);
223 
224 	if (contents != NULL) {
225 
226 		kmem_free((void *)contents, size);
227 	}
228 
229 	/*
230 	 * Flush the page cache.
231 	 */
232 	if (vn_has_cached_data(vp)) {
233 		error = VOP_PUTPAGE(vp, (u_offset_t)0, 0, B_INVAL, cr, NULL);
234 		if (error && (error == ENOSPC || error == EDQUOT)) {
235 			mutex_enter(&rp->r_statelock);
236 			if (!rp->r_error)
237 				rp->r_error = error;
238 			mutex_exit(&rp->r_statelock);
239 		}
240 	}
241 
242 	/*
243 	 * Flush the readdir response cache.
244 	 */
245 	if (HAVE_RDDIR_CACHE(rp))
246 		nfs_purge_rddir_cache(vp);
247 }
248 
249 /*
250  * Purge the readdir cache of all entries
251  */
252 void
253 nfs_purge_rddir_cache(vnode_t *vp)
254 {
255 	rnode_t *rp;
256 	rddir_cache *rdc;
257 	rddir_cache *nrdc;
258 
259 	rp = VTOR(vp);
260 top:
261 	mutex_enter(&rp->r_statelock);
262 	rp->r_direof = NULL;
263 	rp->r_flags &= ~RLOOKUP;
264 	rp->r_flags |= RREADDIRPLUS;
265 	rdc = avl_first(&rp->r_dir);
266 	while (rdc != NULL) {
267 		nrdc = AVL_NEXT(&rp->r_dir, rdc);
268 		avl_remove(&rp->r_dir, rdc);
269 		rddir_cache_rele(rdc);
270 		rdc = nrdc;
271 	}
272 	mutex_exit(&rp->r_statelock);
273 }
274 
275 /*
276  * Do a cache check based on the post-operation attributes.
277  * Then make them the new cached attributes.  If no attributes
278  * were returned, then mark the attributes as timed out.
279  */
280 void
281 nfs3_cache_post_op_attr(vnode_t *vp, post_op_attr *poap, hrtime_t t, cred_t *cr)
282 {
283 	vattr_t attr;
284 
285 	if (!poap->attributes) {
286 		PURGE_ATTRCACHE(vp);
287 		return;
288 	}
289 	(void) nfs3_cache_fattr3(vp, &poap->attr, &attr, t, cr);
290 }
291 
292 /*
293  * Same as above, but using a vattr
294  */
295 void
296 nfs3_cache_post_op_vattr(vnode_t *vp, post_op_vattr *poap, hrtime_t t,
297     cred_t *cr)
298 {
299 	if (!poap->attributes) {
300 		PURGE_ATTRCACHE(vp);
301 		return;
302 	}
303 	nfs_attr_cache(vp, poap->fres.vap, t, cr);
304 }
305 
306 /*
307  * Do a cache check based on the weak cache consistency attributes.
308  * These consist of a small set of pre-operation attributes and the
309  * full set of post-operation attributes.
310  *
311  * If we are given the pre-operation attributes, then use them to
312  * check the validity of the various caches.  Then, if we got the
313  * post-operation attributes, make them the new cached attributes.
314  * If we didn't get the post-operation attributes, then mark the
315  * attribute cache as timed out so that the next reference will
316  * cause a GETATTR to the server to refresh with the current
317  * attributes.
318  *
319  * Otherwise, if we didn't get the pre-operation attributes, but
320  * we did get the post-operation attributes, then use these
321  * attributes to check the validity of the various caches.  This
322  * will probably cause a flush of the caches because if the
323  * operation succeeded, the attributes of the object were changed
324  * in some way from the old post-operation attributes.  This
325  * should be okay because it is the safe thing to do.  After
326  * checking the data caches, then we make these the new cached
327  * attributes.
328  *
329  * Otherwise, we didn't get either the pre- or post-operation
330  * attributes.  Simply mark the attribute cache as timed out so
331  * the next reference will cause a GETATTR to the server to
332  * refresh with the current attributes.
333  *
334  * If an error occurred trying to convert the over the wire
335  * attributes to a vattr, then simply mark the attribute cache as
336  * timed out.
337  */
338 void
339 nfs3_cache_wcc_data(vnode_t *vp, wcc_data *wccp, hrtime_t t, cred_t *cr)
340 {
341 	vattr_t bva;
342 	vattr_t ava;
343 
344 	if (wccp->after.attributes) {
345 		if (fattr3_to_vattr(vp, &wccp->after.attr, &ava)) {
346 			PURGE_ATTRCACHE(vp);
347 			return;
348 		}
349 		if (wccp->before.attributes) {
350 			bva.va_ctime.tv_sec = wccp->before.attr.ctime.seconds;
351 			bva.va_ctime.tv_nsec = wccp->before.attr.ctime.nseconds;
352 			bva.va_mtime.tv_sec = wccp->before.attr.mtime.seconds;
353 			bva.va_mtime.tv_nsec = wccp->before.attr.mtime.nseconds;
354 			bva.va_size = wccp->before.attr.size;
355 			nfs3_attr_cache(vp, &bva, &ava, t, cr);
356 		} else
357 			nfs_attr_cache(vp, &ava, t, cr);
358 	} else {
359 		PURGE_ATTRCACHE(vp);
360 	}
361 }
362 
363 /*
364  * Set attributes cache for given vnode using nfsattr.
365  *
366  * This routine does not do cache validation with the attributes.
367  *
368  * If an error occurred trying to convert the over the wire
369  * attributes to a vattr, then simply mark the attribute cache as
370  * timed out.
371  */
372 void
373 nfs_attrcache(vnode_t *vp, struct nfsfattr *na, hrtime_t t)
374 {
375 	rnode_t *rp;
376 	struct vattr va;
377 
378 	if (!nattr_to_vattr(vp, na, &va)) {
379 		rp = VTOR(vp);
380 		mutex_enter(&rp->r_statelock);
381 		if (rp->r_mtime <= t)
382 			nfs_attrcache_va(vp, &va);
383 		mutex_exit(&rp->r_statelock);
384 	} else {
385 		PURGE_ATTRCACHE(vp);
386 	}
387 }
388 
389 /*
390  * Set attributes cache for given vnode using fattr3.
391  *
392  * This routine does not do cache validation with the attributes.
393  *
394  * If an error occurred trying to convert the over the wire
395  * attributes to a vattr, then simply mark the attribute cache as
396  * timed out.
397  */
398 void
399 nfs3_attrcache(vnode_t *vp, fattr3 *na, hrtime_t t)
400 {
401 	rnode_t *rp;
402 	struct vattr va;
403 
404 	if (!fattr3_to_vattr(vp, na, &va)) {
405 		rp = VTOR(vp);
406 		mutex_enter(&rp->r_statelock);
407 		if (rp->r_mtime <= t)
408 			nfs_attrcache_va(vp, &va);
409 		mutex_exit(&rp->r_statelock);
410 	} else {
411 		PURGE_ATTRCACHE(vp);
412 	}
413 }
414 
415 /*
416  * Do a cache check based on attributes returned over the wire.  The
417  * new attributes are cached.
418  *
419  * If an error occurred trying to convert the over the wire attributes
420  * to a vattr, then just return that error.
421  *
422  * As a side affect, the vattr argument is filled in with the converted
423  * attributes.
424  */
425 int
426 nfs_cache_fattr(vnode_t *vp, struct nfsfattr *na, vattr_t *vap, hrtime_t t,
427     cred_t *cr)
428 {
429 	int error;
430 
431 	error = nattr_to_vattr(vp, na, vap);
432 	if (error)
433 		return (error);
434 	nfs_attr_cache(vp, vap, t, cr);
435 	return (0);
436 }
437 
438 /*
439  * Do a cache check based on attributes returned over the wire.  The
440  * new attributes are cached.
441  *
442  * If an error occurred trying to convert the over the wire attributes
443  * to a vattr, then just return that error.
444  *
445  * As a side affect, the vattr argument is filled in with the converted
446  * attributes.
447  */
448 int
449 nfs3_cache_fattr3(vnode_t *vp, fattr3 *na, vattr_t *vap, hrtime_t t, cred_t *cr)
450 {
451 	int error;
452 
453 	error = fattr3_to_vattr(vp, na, vap);
454 	if (error)
455 		return (error);
456 	nfs_attr_cache(vp, vap, t, cr);
457 	return (0);
458 }
459 
460 /*
461  * Use the passed in virtual attributes to check to see whether the
462  * data and metadata caches are valid, cache the new attributes, and
463  * then do the cache invalidation if required.
464  *
465  * The cache validation and caching of the new attributes is done
466  * atomically via the use of the mutex, r_statelock.  If required,
467  * the cache invalidation is done atomically w.r.t. the cache
468  * validation and caching of the attributes via the pseudo lock,
469  * r_serial.
470  *
471  * This routine is used to do cache validation and attributes caching
472  * for operations with a single set of post operation attributes.
473  */
474 void
475 nfs_attr_cache(vnode_t *vp, vattr_t *vap, hrtime_t t, cred_t *cr)
476 {
477 	rnode_t *rp;
478 	int mtime_changed = 0;
479 	int ctime_changed = 0;
480 	vsecattr_t *vsp;
481 	int was_serial;
482 	len_t preattr_rsize;
483 	boolean_t writeattr_set = B_FALSE;
484 	boolean_t cachepurge_set = B_FALSE;
485 
486 	rp = VTOR(vp);
487 
488 	mutex_enter(&rp->r_statelock);
489 
490 	if (rp->r_serial != curthread) {
491 		klwp_t *lwp = ttolwp(curthread);
492 
493 		was_serial = 0;
494 		if (lwp != NULL)
495 			lwp->lwp_nostop++;
496 		while (rp->r_serial != NULL) {
497 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
498 				mutex_exit(&rp->r_statelock);
499 				if (lwp != NULL)
500 					lwp->lwp_nostop--;
501 				return;
502 			}
503 		}
504 		if (lwp != NULL)
505 			lwp->lwp_nostop--;
506 	} else
507 		was_serial = 1;
508 
509 	if (rp->r_mtime > t) {
510 		if (!CACHE_VALID(rp, vap->va_mtime, vap->va_size))
511 			PURGE_ATTRCACHE_LOCKED(rp);
512 		mutex_exit(&rp->r_statelock);
513 		return;
514 	}
515 
516 	/*
517 	 * Write thread after writing data to file on remote server,
518 	 * will always set RWRITEATTR to indicate that file on remote
519 	 * server was modified with a WRITE operation and would have
520 	 * marked attribute cache as timed out. If RWRITEATTR
521 	 * is set, then do not check for mtime and ctime change.
522 	 */
523 	if (!(rp->r_flags & RWRITEATTR)) {
524 		if (!CACHE_VALID(rp, vap->va_mtime, vap->va_size))
525 			mtime_changed = 1;
526 
527 		if (rp->r_attr.va_ctime.tv_sec != vap->va_ctime.tv_sec ||
528 		    rp->r_attr.va_ctime.tv_nsec != vap->va_ctime.tv_nsec)
529 			ctime_changed = 1;
530 	} else {
531 		writeattr_set = B_TRUE;
532 	}
533 
534 	preattr_rsize = rp->r_size;
535 
536 	nfs_attrcache_va(vp, vap);
537 
538 	/*
539 	 * If we have updated filesize in nfs_attrcache_va, as soon as we
540 	 * drop statelock we will be in transition of purging all
541 	 * our caches and updating them. It is possible for another
542 	 * thread to pick this new file size and read in zeroed data.
543 	 * stall other threads till cache purge is complete.
544 	 */
545 	if ((vp->v_type == VREG) && (rp->r_size != preattr_rsize)) {
546 		/*
547 		 * If RWRITEATTR was set and we have updated the file
548 		 * size, Server's returned file size need not necessarily
549 		 * be because of this Client's WRITE. We need to purge
550 		 * all caches.
551 		 */
552 		if (writeattr_set)
553 			mtime_changed = 1;
554 
555 		if (mtime_changed && !(rp->r_flags & RINCACHEPURGE)) {
556 			rp->r_flags |= RINCACHEPURGE;
557 			cachepurge_set = B_TRUE;
558 		}
559 	}
560 
561 	if (!mtime_changed && !ctime_changed) {
562 		mutex_exit(&rp->r_statelock);
563 		return;
564 	}
565 
566 	rp->r_serial = curthread;
567 
568 	mutex_exit(&rp->r_statelock);
569 
570 	if (mtime_changed)
571 		nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
572 
573 	if ((rp->r_flags & RINCACHEPURGE) && cachepurge_set) {
574 		mutex_enter(&rp->r_statelock);
575 		rp->r_flags &= ~RINCACHEPURGE;
576 		cv_broadcast(&rp->r_cv);
577 		mutex_exit(&rp->r_statelock);
578 		cachepurge_set = B_FALSE;
579 	}
580 
581 	if (ctime_changed) {
582 		(void) nfs_access_purge_rp(rp);
583 		if (rp->r_secattr != NULL) {
584 			mutex_enter(&rp->r_statelock);
585 			vsp = rp->r_secattr;
586 			rp->r_secattr = NULL;
587 			mutex_exit(&rp->r_statelock);
588 			if (vsp != NULL)
589 				nfs_acl_free(vsp);
590 		}
591 	}
592 
593 	if (!was_serial) {
594 		mutex_enter(&rp->r_statelock);
595 		rp->r_serial = NULL;
596 		cv_broadcast(&rp->r_cv);
597 		mutex_exit(&rp->r_statelock);
598 	}
599 }
600 
601 /*
602  * Use the passed in "before" virtual attributes to check to see
603  * whether the data and metadata caches are valid, cache the "after"
604  * new attributes, and then do the cache invalidation if required.
605  *
606  * The cache validation and caching of the new attributes is done
607  * atomically via the use of the mutex, r_statelock.  If required,
608  * the cache invalidation is done atomically w.r.t. the cache
609  * validation and caching of the attributes via the pseudo lock,
610  * r_serial.
611  *
612  * This routine is used to do cache validation and attributes caching
613  * for operations with both pre operation attributes and post operation
614  * attributes.
615  */
616 static void
617 nfs3_attr_cache(vnode_t *vp, vattr_t *bvap, vattr_t *avap, hrtime_t t,
618     cred_t *cr)
619 {
620 	rnode_t *rp;
621 	int mtime_changed = 0;
622 	int ctime_changed = 0;
623 	vsecattr_t *vsp;
624 	int was_serial;
625 	len_t preattr_rsize;
626 	boolean_t writeattr_set = B_FALSE;
627 	boolean_t cachepurge_set = B_FALSE;
628 
629 	rp = VTOR(vp);
630 
631 	mutex_enter(&rp->r_statelock);
632 
633 	if (rp->r_serial != curthread) {
634 		klwp_t *lwp = ttolwp(curthread);
635 
636 		was_serial = 0;
637 		if (lwp != NULL)
638 			lwp->lwp_nostop++;
639 		while (rp->r_serial != NULL) {
640 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
641 				mutex_exit(&rp->r_statelock);
642 				if (lwp != NULL)
643 					lwp->lwp_nostop--;
644 				return;
645 			}
646 		}
647 		if (lwp != NULL)
648 			lwp->lwp_nostop--;
649 	} else
650 		was_serial = 1;
651 
652 	if (rp->r_mtime > t) {
653 		if (!CACHE_VALID(rp, avap->va_mtime, avap->va_size))
654 			PURGE_ATTRCACHE_LOCKED(rp);
655 		mutex_exit(&rp->r_statelock);
656 		return;
657 	}
658 
659 	/*
660 	 * Write thread after writing data to file on remote server,
661 	 * will always set RWRITEATTR to indicate that file on remote
662 	 * server was modified with a WRITE operation and would have
663 	 * marked attribute cache as timed out. If RWRITEATTR
664 	 * is set, then do not check for mtime and ctime change.
665 	 */
666 	if (!(rp->r_flags & RWRITEATTR)) {
667 		if (!CACHE_VALID(rp, bvap->va_mtime, bvap->va_size))
668 			mtime_changed = 1;
669 
670 		if (rp->r_attr.va_ctime.tv_sec != bvap->va_ctime.tv_sec ||
671 		    rp->r_attr.va_ctime.tv_nsec != bvap->va_ctime.tv_nsec)
672 			ctime_changed = 1;
673 	} else {
674 		writeattr_set = B_TRUE;
675 	}
676 
677 	preattr_rsize = rp->r_size;
678 
679 	nfs_attrcache_va(vp, avap);
680 
681 	/*
682 	 * If we have updated filesize in nfs_attrcache_va, as soon as we
683 	 * drop statelock we will be in transition of purging all
684 	 * our caches and updating them. It is possible for another
685 	 * thread to pick this new file size and read in zeroed data.
686 	 * stall other threads till cache purge is complete.
687 	 */
688 	if ((vp->v_type == VREG) && (rp->r_size != preattr_rsize)) {
689 		/*
690 		 * If RWRITEATTR was set and we have updated the file
691 		 * size, Server's returned file size need not necessarily
692 		 * be because of this Client's WRITE. We need to purge
693 		 * all caches.
694 		 */
695 		if (writeattr_set)
696 			mtime_changed = 1;
697 
698 		if (mtime_changed && !(rp->r_flags & RINCACHEPURGE)) {
699 			rp->r_flags |= RINCACHEPURGE;
700 			cachepurge_set = B_TRUE;
701 		}
702 	}
703 
704 	if (!mtime_changed && !ctime_changed) {
705 		mutex_exit(&rp->r_statelock);
706 		return;
707 	}
708 
709 	rp->r_serial = curthread;
710 
711 	mutex_exit(&rp->r_statelock);
712 
713 	if (mtime_changed)
714 		nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
715 
716 	if ((rp->r_flags & RINCACHEPURGE) && cachepurge_set) {
717 		mutex_enter(&rp->r_statelock);
718 		rp->r_flags &= ~RINCACHEPURGE;
719 		cv_broadcast(&rp->r_cv);
720 		mutex_exit(&rp->r_statelock);
721 		cachepurge_set = B_FALSE;
722 	}
723 
724 	if (ctime_changed) {
725 		(void) nfs_access_purge_rp(rp);
726 		if (rp->r_secattr != NULL) {
727 			mutex_enter(&rp->r_statelock);
728 			vsp = rp->r_secattr;
729 			rp->r_secattr = NULL;
730 			mutex_exit(&rp->r_statelock);
731 			if (vsp != NULL)
732 				nfs_acl_free(vsp);
733 		}
734 	}
735 
736 	if (!was_serial) {
737 		mutex_enter(&rp->r_statelock);
738 		rp->r_serial = NULL;
739 		cv_broadcast(&rp->r_cv);
740 		mutex_exit(&rp->r_statelock);
741 	}
742 }
743 
744 /*
745  * Set attributes cache for given vnode using virtual attributes.
746  *
747  * Set the timeout value on the attribute cache and fill it
748  * with the passed in attributes.
749  *
750  * The caller must be holding r_statelock.
751  */
752 void
753 nfs_attrcache_va(vnode_t *vp, struct vattr *va)
754 {
755 	rnode_t *rp;
756 	mntinfo_t *mi;
757 	hrtime_t delta;
758 	hrtime_t now;
759 
760 	rp = VTOR(vp);
761 
762 	ASSERT(MUTEX_HELD(&rp->r_statelock));
763 
764 	now = gethrtime();
765 
766 	mi = VTOMI(vp);
767 
768 	/*
769 	 * Delta is the number of nanoseconds that we will
770 	 * cache the attributes of the file.  It is based on
771 	 * the number of nanoseconds since the last time that
772 	 * we detected a change.  The assumption is that files
773 	 * that changed recently are likely to change again.
774 	 * There is a minimum and a maximum for regular files
775 	 * and for directories which is enforced though.
776 	 *
777 	 * Using the time since last change was detected
778 	 * eliminates direct comparison or calculation
779 	 * using mixed client and server times.  NFS does
780 	 * not make any assumptions regarding the client
781 	 * and server clocks being synchronized.
782 	 */
783 	if (va->va_mtime.tv_sec != rp->r_attr.va_mtime.tv_sec ||
784 	    va->va_mtime.tv_nsec != rp->r_attr.va_mtime.tv_nsec ||
785 	    va->va_size != rp->r_attr.va_size)
786 		rp->r_mtime = now;
787 
788 	if ((mi->mi_flags & MI_NOAC) || (vp->v_flag & VNOCACHE))
789 		delta = 0;
790 	else {
791 		delta = now - rp->r_mtime;
792 		if (vp->v_type == VDIR) {
793 			if (delta < mi->mi_acdirmin)
794 				delta = mi->mi_acdirmin;
795 			else if (delta > mi->mi_acdirmax)
796 				delta = mi->mi_acdirmax;
797 		} else {
798 			if (delta < mi->mi_acregmin)
799 				delta = mi->mi_acregmin;
800 			else if (delta > mi->mi_acregmax)
801 				delta = mi->mi_acregmax;
802 		}
803 	}
804 	rp->r_attrtime = now + delta;
805 	rp->r_attr = *va;
806 	/*
807 	 * Update the size of the file if there is no cached data or if
808 	 * the cached data is clean and there is no data being written
809 	 * out.
810 	 */
811 	if (rp->r_size != va->va_size &&
812 	    (!vn_has_cached_data(vp) ||
813 	    (!(rp->r_flags & RDIRTY) && rp->r_count == 0)))
814 		rp->r_size = va->va_size;
815 	nfs_setswaplike(vp, va);
816 	rp->r_flags &= ~RWRITEATTR;
817 }
818 
819 /*
820  * Fill in attribute from the cache.
821  * If valid, then return 0 to indicate that no error occurred,
822  * otherwise return 1 to indicate that an error occurred.
823  */
824 static int
825 nfs_getattr_cache(vnode_t *vp, struct vattr *vap)
826 {
827 	rnode_t *rp;
828 	uint_t mask = vap->va_mask;
829 
830 	rp = VTOR(vp);
831 	mutex_enter(&rp->r_statelock);
832 	if (ATTRCACHE_VALID(vp)) {
833 		/*
834 		 * Cached attributes are valid
835 		 */
836 		*vap = rp->r_attr;
837 		/*
838 		 * Set the caller's va_mask to the set of attributes
839 		 * that were requested ANDed with the attributes that
840 		 * are available.  If attributes were requested that
841 		 * are not available, those bits must be turned off
842 		 * in the callers va_mask.
843 		 */
844 		vap->va_mask &= mask;
845 		mutex_exit(&rp->r_statelock);
846 		return (0);
847 	}
848 	mutex_exit(&rp->r_statelock);
849 	return (1);
850 }
851 
852 /*
853  * Get attributes over-the-wire and update attributes cache
854  * if no error occurred in the over-the-wire operation.
855  * Return 0 if successful, otherwise error.
856  */
857 int
858 nfs_getattr_otw(vnode_t *vp, struct vattr *vap, cred_t *cr)
859 {
860 	int error;
861 	struct nfsattrstat ns;
862 	int douprintf;
863 	mntinfo_t *mi;
864 	failinfo_t fi;
865 	hrtime_t t;
866 
867 	mi = VTOMI(vp);
868 	fi.vp = vp;
869 	fi.fhp = NULL;		/* no need to update, filehandle not copied */
870 	fi.copyproc = nfscopyfh;
871 	fi.lookupproc = nfslookup;
872 	fi.xattrdirproc = acl_getxattrdir2;
873 
874 	if (mi->mi_flags & MI_ACL) {
875 		error = acl_getattr2_otw(vp, vap, cr);
876 		if (mi->mi_flags & MI_ACL)
877 			return (error);
878 	}
879 
880 	douprintf = 1;
881 
882 	t = gethrtime();
883 
884 	error = rfs2call(mi, RFS_GETATTR,
885 	    xdr_fhandle, (caddr_t)VTOFH(vp),
886 	    xdr_attrstat, (caddr_t)&ns, cr,
887 	    &douprintf, &ns.ns_status, 0, &fi);
888 
889 	if (!error) {
890 		error = geterrno(ns.ns_status);
891 		if (!error)
892 			error = nfs_cache_fattr(vp, &ns.ns_attr, vap, t, cr);
893 		else {
894 			PURGE_STALE_FH(error, vp, cr);
895 		}
896 	}
897 
898 	return (error);
899 }
900 
901 /*
902  * Return either cached ot remote attributes. If get remote attr
903  * use them to check and invalidate caches, then cache the new attributes.
904  */
905 int
906 nfsgetattr(vnode_t *vp, struct vattr *vap, cred_t *cr)
907 {
908 	int error;
909 	rnode_t *rp;
910 
911 	/*
912 	 * If we've got cached attributes, we're done, otherwise go
913 	 * to the server to get attributes, which will update the cache
914 	 * in the process.
915 	 */
916 	error = nfs_getattr_cache(vp, vap);
917 	if (error)
918 		error = nfs_getattr_otw(vp, vap, cr);
919 
920 	/* Return the client's view of file size */
921 	rp = VTOR(vp);
922 	mutex_enter(&rp->r_statelock);
923 	vap->va_size = rp->r_size;
924 	mutex_exit(&rp->r_statelock);
925 
926 	return (error);
927 }
928 
929 /*
930  * Get attributes over-the-wire and update attributes cache
931  * if no error occurred in the over-the-wire operation.
932  * Return 0 if successful, otherwise error.
933  */
934 int
935 nfs3_getattr_otw(vnode_t *vp, struct vattr *vap, cred_t *cr)
936 {
937 	int error;
938 	GETATTR3args args;
939 	GETATTR3vres res;
940 	int douprintf;
941 	failinfo_t fi;
942 	hrtime_t t;
943 
944 	args.object = *VTOFH3(vp);
945 	fi.vp = vp;
946 	fi.fhp = (caddr_t)&args.object;
947 	fi.copyproc = nfs3copyfh;
948 	fi.lookupproc = nfs3lookup;
949 	fi.xattrdirproc = acl_getxattrdir3;
950 	res.fres.vp = vp;
951 	res.fres.vap = vap;
952 
953 	douprintf = 1;
954 
955 	t = gethrtime();
956 
957 	error = rfs3call(VTOMI(vp), NFSPROC3_GETATTR,
958 	    xdr_nfs_fh3, (caddr_t)&args,
959 	    xdr_GETATTR3vres, (caddr_t)&res, cr,
960 	    &douprintf, &res.status, 0, &fi);
961 
962 	if (error)
963 		return (error);
964 
965 	error = geterrno3(res.status);
966 	if (error) {
967 		PURGE_STALE_FH(error, vp, cr);
968 		return (error);
969 	}
970 
971 	/*
972 	 * Catch status codes that indicate fattr3 to vattr translation failure
973 	 */
974 	if (res.fres.status)
975 		return (res.fres.status);
976 
977 	nfs_attr_cache(vp, vap, t, cr);
978 	return (0);
979 }
980 
981 /*
982  * Return either cached or remote attributes. If get remote attr
983  * use them to check and invalidate caches, then cache the new attributes.
984  */
985 int
986 nfs3getattr(vnode_t *vp, struct vattr *vap, cred_t *cr)
987 {
988 	int error;
989 	rnode_t *rp;
990 
991 	/*
992 	 * If we've got cached attributes, we're done, otherwise go
993 	 * to the server to get attributes, which will update the cache
994 	 * in the process.
995 	 */
996 	error = nfs_getattr_cache(vp, vap);
997 	if (error)
998 		error = nfs3_getattr_otw(vp, vap, cr);
999 
1000 	/* Return the client's view of file size */
1001 	rp = VTOR(vp);
1002 	mutex_enter(&rp->r_statelock);
1003 	vap->va_size = rp->r_size;
1004 	mutex_exit(&rp->r_statelock);
1005 
1006 	return (error);
1007 }
1008 
1009 vtype_t nf_to_vt[] = {
1010 	VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK
1011 };
1012 /*
1013  * Convert NFS Version 2 over the network attributes to the local
1014  * virtual attributes.  The mapping between the UID_NOBODY/GID_NOBODY
1015  * network representation and the local representation is done here.
1016  * Returns 0 for success, error if failed due to overflow.
1017  */
1018 int
1019 nattr_to_vattr(vnode_t *vp, struct nfsfattr *na, struct vattr *vap)
1020 {
1021 	/* overflow in time attributes? */
1022 #ifndef _LP64
1023 	if (!NFS2_FATTR_TIME_OK(na))
1024 		return (EOVERFLOW);
1025 #endif
1026 
1027 	vap->va_mask = AT_ALL;
1028 
1029 	if (na->na_type < NFNON || na->na_type > NFSOC)
1030 		vap->va_type = VBAD;
1031 	else
1032 		vap->va_type = nf_to_vt[na->na_type];
1033 	vap->va_mode = na->na_mode;
1034 	vap->va_uid = (na->na_uid == NFS_UID_NOBODY) ? UID_NOBODY : na->na_uid;
1035 	vap->va_gid = (na->na_gid == NFS_GID_NOBODY) ? GID_NOBODY : na->na_gid;
1036 	vap->va_fsid = vp->v_vfsp->vfs_dev;
1037 	vap->va_nodeid = na->na_nodeid;
1038 	vap->va_nlink = na->na_nlink;
1039 	vap->va_size = na->na_size;	/* keep for cache validation */
1040 	/*
1041 	 * nfs protocol defines times as unsigned so don't extend sign,
1042 	 * unless sysadmin set nfs_allow_preepoch_time.
1043 	 */
1044 	NFS_TIME_T_CONVERT(vap->va_atime.tv_sec, na->na_atime.tv_sec);
1045 	vap->va_atime.tv_nsec = (uint32_t)(na->na_atime.tv_usec * 1000);
1046 	NFS_TIME_T_CONVERT(vap->va_mtime.tv_sec, na->na_mtime.tv_sec);
1047 	vap->va_mtime.tv_nsec = (uint32_t)(na->na_mtime.tv_usec * 1000);
1048 	NFS_TIME_T_CONVERT(vap->va_ctime.tv_sec, na->na_ctime.tv_sec);
1049 	vap->va_ctime.tv_nsec = (uint32_t)(na->na_ctime.tv_usec * 1000);
1050 	/*
1051 	 * Shannon's law - uncompress the received dev_t
1052 	 * if the top half of is zero indicating a response
1053 	 * from an `older style' OS. Except for when it is a
1054 	 * `new style' OS sending the maj device of zero,
1055 	 * in which case the algorithm still works because the
1056 	 * fact that it is a new style server
1057 	 * is hidden by the minor device not being greater
1058 	 * than 255 (a requirement in this case).
1059 	 */
1060 	if ((na->na_rdev & 0xffff0000) == 0)
1061 		vap->va_rdev = nfsv2_expdev(na->na_rdev);
1062 	else
1063 		vap->va_rdev = expldev(na->na_rdev);
1064 
1065 	vap->va_nblocks = na->na_blocks;
1066 	switch (na->na_type) {
1067 	case NFBLK:
1068 		vap->va_blksize = DEV_BSIZE;
1069 		break;
1070 
1071 	case NFCHR:
1072 		vap->va_blksize = MAXBSIZE;
1073 		break;
1074 
1075 	case NFSOC:
1076 	default:
1077 		vap->va_blksize = na->na_blocksize;
1078 		break;
1079 	}
1080 	/*
1081 	 * This bit of ugliness is a hack to preserve the
1082 	 * over-the-wire protocols for named-pipe vnodes.
1083 	 * It remaps the special over-the-wire type to the
1084 	 * VFIFO type. (see note in nfs.h)
1085 	 */
1086 	if (NA_ISFIFO(na)) {
1087 		vap->va_type = VFIFO;
1088 		vap->va_mode = (vap->va_mode & ~S_IFMT) | S_IFIFO;
1089 		vap->va_rdev = 0;
1090 		vap->va_blksize = na->na_blocksize;
1091 	}
1092 	vap->va_seq = 0;
1093 	return (0);
1094 }
1095 
1096 /*
1097  * Convert NFS Version 3 over the network attributes to the local
1098  * virtual attributes.  The mapping between the UID_NOBODY/GID_NOBODY
1099  * network representation and the local representation is done here.
1100  */
1101 vtype_t nf3_to_vt[] = {
1102 	VBAD, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
1103 };
1104 
1105 int
1106 fattr3_to_vattr(vnode_t *vp, fattr3 *na, struct vattr *vap)
1107 {
1108 
1109 #ifndef _LP64
1110 	/* overflow in time attributes? */
1111 	if (!NFS3_FATTR_TIME_OK(na))
1112 		return (EOVERFLOW);
1113 #endif
1114 	if (!NFS3_SIZE_OK(na->size))
1115 		/* file too big */
1116 		return (EFBIG);
1117 
1118 	vap->va_mask = AT_ALL;
1119 
1120 	if (na->type < NF3REG || na->type > NF3FIFO)
1121 		vap->va_type = VBAD;
1122 	else
1123 		vap->va_type = nf3_to_vt[na->type];
1124 	vap->va_mode = na->mode;
1125 	vap->va_uid = (na->uid == NFS_UID_NOBODY) ? UID_NOBODY : (uid_t)na->uid;
1126 	vap->va_gid = (na->gid == NFS_GID_NOBODY) ? GID_NOBODY : (gid_t)na->gid;
1127 	vap->va_fsid = vp->v_vfsp->vfs_dev;
1128 	vap->va_nodeid = na->fileid;
1129 	vap->va_nlink = na->nlink;
1130 	vap->va_size = na->size;
1131 
1132 	/*
1133 	 * nfs protocol defines times as unsigned so don't extend sign,
1134 	 * unless sysadmin set nfs_allow_preepoch_time.
1135 	 */
1136 	NFS_TIME_T_CONVERT(vap->va_atime.tv_sec, na->atime.seconds);
1137 	vap->va_atime.tv_nsec = (uint32_t)na->atime.nseconds;
1138 	NFS_TIME_T_CONVERT(vap->va_mtime.tv_sec, na->mtime.seconds);
1139 	vap->va_mtime.tv_nsec = (uint32_t)na->mtime.nseconds;
1140 	NFS_TIME_T_CONVERT(vap->va_ctime.tv_sec, na->ctime.seconds);
1141 	vap->va_ctime.tv_nsec = (uint32_t)na->ctime.nseconds;
1142 
1143 	switch (na->type) {
1144 	case NF3BLK:
1145 		vap->va_rdev = makedevice(na->rdev.specdata1,
1146 		    na->rdev.specdata2);
1147 		vap->va_blksize = DEV_BSIZE;
1148 		vap->va_nblocks = 0;
1149 		break;
1150 	case NF3CHR:
1151 		vap->va_rdev = makedevice(na->rdev.specdata1,
1152 		    na->rdev.specdata2);
1153 		vap->va_blksize = MAXBSIZE;
1154 		vap->va_nblocks = 0;
1155 		break;
1156 	case NF3REG:
1157 	case NF3DIR:
1158 	case NF3LNK:
1159 		vap->va_rdev = 0;
1160 		vap->va_blksize = MAXBSIZE;
1161 		vap->va_nblocks = (u_longlong_t)
1162 		    ((na->used + (size3)DEV_BSIZE - (size3)1) /
1163 		    (size3)DEV_BSIZE);
1164 		break;
1165 	case NF3SOCK:
1166 	case NF3FIFO:
1167 	default:
1168 		vap->va_rdev = 0;
1169 		vap->va_blksize = MAXBSIZE;
1170 		vap->va_nblocks = 0;
1171 		break;
1172 	}
1173 	vap->va_seq = 0;
1174 	return (0);
1175 }
1176 
1177 /*
1178  * Asynchronous I/O parameters.  nfs_async_threads is the high-water mark
1179  * for the demand-based allocation of async threads per-mount.  The
1180  * nfs_async_timeout is the amount of time a thread will live after it
1181  * becomes idle, unless new I/O requests are received before the thread
1182  * dies.  See nfs_async_putpage and nfs_async_start.
1183  */
1184 
1185 int nfs_async_timeout = -1;	/* uninitialized */
1186 
1187 static void	nfs_async_start(struct vfs *);
1188 
1189 static void
1190 free_async_args(struct nfs_async_reqs *args)
1191 {
1192 	rnode_t *rp;
1193 
1194 	if (args->a_io != NFS_INACTIVE) {
1195 		rp = VTOR(args->a_vp);
1196 		mutex_enter(&rp->r_statelock);
1197 		rp->r_count--;
1198 		if (args->a_io == NFS_PUTAPAGE ||
1199 		    args->a_io == NFS_PAGEIO)
1200 			rp->r_awcount--;
1201 		cv_broadcast(&rp->r_cv);
1202 		mutex_exit(&rp->r_statelock);
1203 		VN_RELE(args->a_vp);
1204 	}
1205 	crfree(args->a_cred);
1206 	kmem_free(args, sizeof (*args));
1207 }
1208 
1209 /*
1210  * Cross-zone thread creation and NFS access is disallowed, yet fsflush() and
1211  * pageout(), running in the global zone, have legitimate reasons to do
1212  * VOP_PUTPAGE(B_ASYNC) on other zones' NFS mounts.  We avoid the problem by
1213  * use of a a per-mount "asynchronous requests manager thread" which is
1214  * signaled by the various asynchronous work routines when there is
1215  * asynchronous work to be done.  It is responsible for creating new
1216  * worker threads if necessary, and notifying existing worker threads
1217  * that there is work to be done.
1218  *
1219  * In other words, it will "take the specifications from the customers and
1220  * give them to the engineers."
1221  *
1222  * Worker threads die off of their own accord if they are no longer
1223  * needed.
1224  *
1225  * This thread is killed when the zone is going away or the filesystem
1226  * is being unmounted.
1227  */
1228 void
1229 nfs_async_manager(vfs_t *vfsp)
1230 {
1231 	callb_cpr_t cprinfo;
1232 	mntinfo_t *mi;
1233 	uint_t max_threads;
1234 
1235 	mi = VFTOMI(vfsp);
1236 
1237 	CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr,
1238 	    "nfs_async_manager");
1239 
1240 	mutex_enter(&mi->mi_async_lock);
1241 	/*
1242 	 * We want to stash the max number of threads that this mount was
1243 	 * allowed so we can use it later when the variable is set to zero as
1244 	 * part of the zone/mount going away.
1245 	 *
1246 	 * We want to be able to create at least one thread to handle
1247 	 * asyncrhonous inactive calls.
1248 	 */
1249 	max_threads = MAX(mi->mi_max_threads, 1);
1250 	mutex_enter(&mi->mi_lock);
1251 	/*
1252 	 * We don't want to wait for mi_max_threads to go to zero, since that
1253 	 * happens as part of a failed unmount, but this thread should only
1254 	 * exit when the mount/zone is really going away.
1255 	 *
1256 	 * Once MI_ASYNC_MGR_STOP is set, no more async operations will be
1257 	 * attempted: the various _async_*() functions know to do things
1258 	 * inline if mi_max_threads == 0.  Henceforth we just drain out the
1259 	 * outstanding requests.
1260 	 *
1261 	 * Note that we still create zthreads even if we notice the zone is
1262 	 * shutting down (MI_ASYNC_MGR_STOP is set); this may cause the zone
1263 	 * shutdown sequence to take slightly longer in some cases, but
1264 	 * doesn't violate the protocol, as all threads will exit as soon as
1265 	 * they're done processing the remaining requests.
1266 	 */
1267 	while (!(mi->mi_flags & MI_ASYNC_MGR_STOP) ||
1268 	    mi->mi_async_req_count > 0) {
1269 		mutex_exit(&mi->mi_lock);
1270 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
1271 		cv_wait(&mi->mi_async_reqs_cv, &mi->mi_async_lock);
1272 		CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock);
1273 		while (mi->mi_async_req_count > 0) {
1274 			/*
1275 			 * Paranoia: If the mount started out having
1276 			 * (mi->mi_max_threads == 0), and the value was
1277 			 * later changed (via a debugger or somesuch),
1278 			 * we could be confused since we will think we
1279 			 * can't create any threads, and the calling
1280 			 * code (which looks at the current value of
1281 			 * mi->mi_max_threads, now non-zero) thinks we
1282 			 * can.
1283 			 *
1284 			 * So, because we're paranoid, we create threads
1285 			 * up to the maximum of the original and the
1286 			 * current value. This means that future
1287 			 * (debugger-induced) lowerings of
1288 			 * mi->mi_max_threads are ignored for our
1289 			 * purposes, but who told them they could change
1290 			 * random values on a live kernel anyhow?
1291 			 */
1292 			if (mi->mi_threads <
1293 			    MAX(mi->mi_max_threads, max_threads)) {
1294 				mi->mi_threads++;
1295 				mutex_exit(&mi->mi_async_lock);
1296 				VFS_HOLD(vfsp);	/* hold for new thread */
1297 				(void) zthread_create(NULL, 0, nfs_async_start,
1298 				    vfsp, 0, minclsyspri);
1299 				mutex_enter(&mi->mi_async_lock);
1300 			}
1301 			cv_signal(&mi->mi_async_work_cv);
1302 			ASSERT(mi->mi_async_req_count != 0);
1303 			mi->mi_async_req_count--;
1304 		}
1305 		mutex_enter(&mi->mi_lock);
1306 	}
1307 	mutex_exit(&mi->mi_lock);
1308 	/*
1309 	 * Let everyone know we're done.
1310 	 */
1311 	mi->mi_manager_thread = NULL;
1312 	cv_broadcast(&mi->mi_async_cv);
1313 
1314 	/*
1315 	 * There is no explicit call to mutex_exit(&mi->mi_async_lock)
1316 	 * since CALLB_CPR_EXIT is actually responsible for releasing
1317 	 * 'mi_async_lock'.
1318 	 */
1319 	CALLB_CPR_EXIT(&cprinfo);
1320 	VFS_RELE(vfsp);	/* release thread's hold */
1321 	zthread_exit();
1322 }
1323 
1324 /*
1325  * Signal (and wait for) the async manager thread to clean up and go away.
1326  */
1327 void
1328 nfs_async_manager_stop(vfs_t *vfsp)
1329 {
1330 	mntinfo_t *mi = VFTOMI(vfsp);
1331 
1332 	mutex_enter(&mi->mi_async_lock);
1333 	mutex_enter(&mi->mi_lock);
1334 	mi->mi_flags |= MI_ASYNC_MGR_STOP;
1335 	mutex_exit(&mi->mi_lock);
1336 	cv_broadcast(&mi->mi_async_reqs_cv);
1337 	while (mi->mi_manager_thread != NULL)
1338 		cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
1339 	mutex_exit(&mi->mi_async_lock);
1340 }
1341 
1342 int
1343 nfs_async_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr,
1344 	struct seg *seg, cred_t *cr, void (*readahead)(vnode_t *,
1345 	u_offset_t, caddr_t, struct seg *, cred_t *))
1346 {
1347 	rnode_t *rp;
1348 	mntinfo_t *mi;
1349 	struct nfs_async_reqs *args;
1350 
1351 	rp = VTOR(vp);
1352 	ASSERT(rp->r_freef == NULL);
1353 
1354 	mi = VTOMI(vp);
1355 
1356 	/*
1357 	 * If addr falls in a different segment, don't bother doing readahead.
1358 	 */
1359 	if (addr >= seg->s_base + seg->s_size)
1360 		return (-1);
1361 
1362 	/*
1363 	 * If we can't allocate a request structure, punt on the readahead.
1364 	 */
1365 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1366 		return (-1);
1367 
1368 	/*
1369 	 * If a lock operation is pending, don't initiate any new
1370 	 * readaheads.  Otherwise, bump r_count to indicate the new
1371 	 * asynchronous I/O.
1372 	 */
1373 	if (!nfs_rw_tryenter(&rp->r_lkserlock, RW_READER)) {
1374 		kmem_free(args, sizeof (*args));
1375 		return (-1);
1376 	}
1377 	mutex_enter(&rp->r_statelock);
1378 	rp->r_count++;
1379 	mutex_exit(&rp->r_statelock);
1380 	nfs_rw_exit(&rp->r_lkserlock);
1381 
1382 	args->a_next = NULL;
1383 #ifdef DEBUG
1384 	args->a_queuer = curthread;
1385 #endif
1386 	VN_HOLD(vp);
1387 	args->a_vp = vp;
1388 	ASSERT(cr != NULL);
1389 	crhold(cr);
1390 	args->a_cred = cr;
1391 	args->a_io = NFS_READ_AHEAD;
1392 	args->a_nfs_readahead = readahead;
1393 	args->a_nfs_blkoff = blkoff;
1394 	args->a_nfs_seg = seg;
1395 	args->a_nfs_addr = addr;
1396 
1397 	mutex_enter(&mi->mi_async_lock);
1398 
1399 	/*
1400 	 * If asyncio has been disabled, don't bother readahead.
1401 	 */
1402 	if (mi->mi_max_threads == 0) {
1403 		mutex_exit(&mi->mi_async_lock);
1404 		goto noasync;
1405 	}
1406 
1407 	/*
1408 	 * Link request structure into the async list and
1409 	 * wakeup async thread to do the i/o.
1410 	 */
1411 	if (mi->mi_async_reqs[NFS_READ_AHEAD] == NULL) {
1412 		mi->mi_async_reqs[NFS_READ_AHEAD] = args;
1413 		mi->mi_async_tail[NFS_READ_AHEAD] = args;
1414 	} else {
1415 		mi->mi_async_tail[NFS_READ_AHEAD]->a_next = args;
1416 		mi->mi_async_tail[NFS_READ_AHEAD] = args;
1417 	}
1418 
1419 	if (mi->mi_io_kstats) {
1420 		mutex_enter(&mi->mi_lock);
1421 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1422 		mutex_exit(&mi->mi_lock);
1423 	}
1424 
1425 	mi->mi_async_req_count++;
1426 	ASSERT(mi->mi_async_req_count != 0);
1427 	cv_signal(&mi->mi_async_reqs_cv);
1428 	mutex_exit(&mi->mi_async_lock);
1429 	return (0);
1430 
1431 noasync:
1432 	mutex_enter(&rp->r_statelock);
1433 	rp->r_count--;
1434 	cv_broadcast(&rp->r_cv);
1435 	mutex_exit(&rp->r_statelock);
1436 	VN_RELE(vp);
1437 	crfree(cr);
1438 	kmem_free(args, sizeof (*args));
1439 	return (-1);
1440 }
1441 
1442 int
1443 nfs_async_putapage(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
1444 	int flags, cred_t *cr, int (*putapage)(vnode_t *, page_t *,
1445 	u_offset_t, size_t, int, cred_t *))
1446 {
1447 	rnode_t *rp;
1448 	mntinfo_t *mi;
1449 	struct nfs_async_reqs *args;
1450 
1451 	ASSERT(flags & B_ASYNC);
1452 	ASSERT(vp->v_vfsp != NULL);
1453 
1454 	rp = VTOR(vp);
1455 	ASSERT(rp->r_count > 0);
1456 
1457 	mi = VTOMI(vp);
1458 
1459 	/*
1460 	 * If we can't allocate a request structure, do the putpage
1461 	 * operation synchronously in this thread's context.
1462 	 */
1463 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1464 		goto noasync;
1465 
1466 	args->a_next = NULL;
1467 #ifdef DEBUG
1468 	args->a_queuer = curthread;
1469 #endif
1470 	VN_HOLD(vp);
1471 	args->a_vp = vp;
1472 	ASSERT(cr != NULL);
1473 	crhold(cr);
1474 	args->a_cred = cr;
1475 	args->a_io = NFS_PUTAPAGE;
1476 	args->a_nfs_putapage = putapage;
1477 	args->a_nfs_pp = pp;
1478 	args->a_nfs_off = off;
1479 	args->a_nfs_len = (uint_t)len;
1480 	args->a_nfs_flags = flags;
1481 
1482 	mutex_enter(&mi->mi_async_lock);
1483 
1484 	/*
1485 	 * If asyncio has been disabled, then make a synchronous request.
1486 	 * This check is done a second time in case async io was diabled
1487 	 * while this thread was blocked waiting for memory pressure to
1488 	 * reduce or for the queue to drain.
1489 	 */
1490 	if (mi->mi_max_threads == 0) {
1491 		mutex_exit(&mi->mi_async_lock);
1492 		goto noasync;
1493 	}
1494 
1495 	/*
1496 	 * Link request structure into the async list and
1497 	 * wakeup async thread to do the i/o.
1498 	 */
1499 	if (mi->mi_async_reqs[NFS_PUTAPAGE] == NULL) {
1500 		mi->mi_async_reqs[NFS_PUTAPAGE] = args;
1501 		mi->mi_async_tail[NFS_PUTAPAGE] = args;
1502 	} else {
1503 		mi->mi_async_tail[NFS_PUTAPAGE]->a_next = args;
1504 		mi->mi_async_tail[NFS_PUTAPAGE] = args;
1505 	}
1506 
1507 	mutex_enter(&rp->r_statelock);
1508 	rp->r_count++;
1509 	rp->r_awcount++;
1510 	mutex_exit(&rp->r_statelock);
1511 
1512 	if (mi->mi_io_kstats) {
1513 		mutex_enter(&mi->mi_lock);
1514 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1515 		mutex_exit(&mi->mi_lock);
1516 	}
1517 
1518 	mi->mi_async_req_count++;
1519 	ASSERT(mi->mi_async_req_count != 0);
1520 	cv_signal(&mi->mi_async_reqs_cv);
1521 	mutex_exit(&mi->mi_async_lock);
1522 	return (0);
1523 
1524 noasync:
1525 	if (args != NULL) {
1526 		VN_RELE(vp);
1527 		crfree(cr);
1528 		kmem_free(args, sizeof (*args));
1529 	}
1530 
1531 	if (curproc == proc_pageout || curproc == proc_fsflush) {
1532 		/*
1533 		 * If we get here in the context of the pageout/fsflush,
1534 		 * we refuse to do a sync write, because this may hang
1535 		 * pageout (and the machine). In this case, we just
1536 		 * re-mark the page as dirty and punt on the page.
1537 		 *
1538 		 * Make sure B_FORCE isn't set.  We can re-mark the
1539 		 * pages as dirty and unlock the pages in one swoop by
1540 		 * passing in B_ERROR to pvn_write_done().  However,
1541 		 * we should make sure B_FORCE isn't set - we don't
1542 		 * want the page tossed before it gets written out.
1543 		 */
1544 		if (flags & B_FORCE)
1545 			flags &= ~(B_INVAL | B_FORCE);
1546 		pvn_write_done(pp, flags | B_ERROR);
1547 		return (0);
1548 	}
1549 	if (nfs_zone() != mi->mi_zone) {
1550 		/*
1551 		 * So this was a cross-zone sync putpage.  We pass in B_ERROR
1552 		 * to pvn_write_done() to re-mark the pages as dirty and unlock
1553 		 * them.
1554 		 *
1555 		 * We don't want to clear B_FORCE here as the caller presumably
1556 		 * knows what they're doing if they set it.
1557 		 */
1558 		pvn_write_done(pp, flags | B_ERROR);
1559 		return (EPERM);
1560 	}
1561 	return ((*putapage)(vp, pp, off, len, flags, cr));
1562 }
1563 
1564 int
1565 nfs_async_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
1566 	int flags, cred_t *cr, int (*pageio)(vnode_t *, page_t *, u_offset_t,
1567 	size_t, int, cred_t *))
1568 {
1569 	rnode_t *rp;
1570 	mntinfo_t *mi;
1571 	struct nfs_async_reqs *args;
1572 
1573 	ASSERT(flags & B_ASYNC);
1574 	ASSERT(vp->v_vfsp != NULL);
1575 
1576 	rp = VTOR(vp);
1577 	ASSERT(rp->r_count > 0);
1578 
1579 	mi = VTOMI(vp);
1580 
1581 	/*
1582 	 * If we can't allocate a request structure, do the pageio
1583 	 * request synchronously in this thread's context.
1584 	 */
1585 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1586 		goto noasync;
1587 
1588 	args->a_next = NULL;
1589 #ifdef DEBUG
1590 	args->a_queuer = curthread;
1591 #endif
1592 	VN_HOLD(vp);
1593 	args->a_vp = vp;
1594 	ASSERT(cr != NULL);
1595 	crhold(cr);
1596 	args->a_cred = cr;
1597 	args->a_io = NFS_PAGEIO;
1598 	args->a_nfs_pageio = pageio;
1599 	args->a_nfs_pp = pp;
1600 	args->a_nfs_off = io_off;
1601 	args->a_nfs_len = (uint_t)io_len;
1602 	args->a_nfs_flags = flags;
1603 
1604 	mutex_enter(&mi->mi_async_lock);
1605 
1606 	/*
1607 	 * If asyncio has been disabled, then make a synchronous request.
1608 	 * This check is done a second time in case async io was diabled
1609 	 * while this thread was blocked waiting for memory pressure to
1610 	 * reduce or for the queue to drain.
1611 	 */
1612 	if (mi->mi_max_threads == 0) {
1613 		mutex_exit(&mi->mi_async_lock);
1614 		goto noasync;
1615 	}
1616 
1617 	/*
1618 	 * Link request structure into the async list and
1619 	 * wakeup async thread to do the i/o.
1620 	 */
1621 	if (mi->mi_async_reqs[NFS_PAGEIO] == NULL) {
1622 		mi->mi_async_reqs[NFS_PAGEIO] = args;
1623 		mi->mi_async_tail[NFS_PAGEIO] = args;
1624 	} else {
1625 		mi->mi_async_tail[NFS_PAGEIO]->a_next = args;
1626 		mi->mi_async_tail[NFS_PAGEIO] = args;
1627 	}
1628 
1629 	mutex_enter(&rp->r_statelock);
1630 	rp->r_count++;
1631 	rp->r_awcount++;
1632 	mutex_exit(&rp->r_statelock);
1633 
1634 	if (mi->mi_io_kstats) {
1635 		mutex_enter(&mi->mi_lock);
1636 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1637 		mutex_exit(&mi->mi_lock);
1638 	}
1639 
1640 	mi->mi_async_req_count++;
1641 	ASSERT(mi->mi_async_req_count != 0);
1642 	cv_signal(&mi->mi_async_reqs_cv);
1643 	mutex_exit(&mi->mi_async_lock);
1644 	return (0);
1645 
1646 noasync:
1647 	if (args != NULL) {
1648 		VN_RELE(vp);
1649 		crfree(cr);
1650 		kmem_free(args, sizeof (*args));
1651 	}
1652 
1653 	/*
1654 	 * If we can't do it ASYNC, for reads we do nothing (but cleanup
1655 	 * the page list), for writes we do it synchronously, except for
1656 	 * proc_pageout/proc_fsflush as described below.
1657 	 */
1658 	if (flags & B_READ) {
1659 		pvn_read_done(pp, flags | B_ERROR);
1660 		return (0);
1661 	}
1662 
1663 	if (curproc == proc_pageout || curproc == proc_fsflush) {
1664 		/*
1665 		 * If we get here in the context of the pageout/fsflush,
1666 		 * we refuse to do a sync write, because this may hang
1667 		 * pageout/fsflush (and the machine). In this case, we just
1668 		 * re-mark the page as dirty and punt on the page.
1669 		 *
1670 		 * Make sure B_FORCE isn't set.  We can re-mark the
1671 		 * pages as dirty and unlock the pages in one swoop by
1672 		 * passing in B_ERROR to pvn_write_done().  However,
1673 		 * we should make sure B_FORCE isn't set - we don't
1674 		 * want the page tossed before it gets written out.
1675 		 */
1676 		if (flags & B_FORCE)
1677 			flags &= ~(B_INVAL | B_FORCE);
1678 		pvn_write_done(pp, flags | B_ERROR);
1679 		return (0);
1680 	}
1681 
1682 	if (nfs_zone() != mi->mi_zone) {
1683 		/*
1684 		 * So this was a cross-zone sync pageio.  We pass in B_ERROR
1685 		 * to pvn_write_done() to re-mark the pages as dirty and unlock
1686 		 * them.
1687 		 *
1688 		 * We don't want to clear B_FORCE here as the caller presumably
1689 		 * knows what they're doing if they set it.
1690 		 */
1691 		pvn_write_done(pp, flags | B_ERROR);
1692 		return (EPERM);
1693 	}
1694 	return ((*pageio)(vp, pp, io_off, io_len, flags, cr));
1695 }
1696 
1697 void
1698 nfs_async_readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr,
1699 	int (*readdir)(vnode_t *, rddir_cache *, cred_t *))
1700 {
1701 	rnode_t *rp;
1702 	mntinfo_t *mi;
1703 	struct nfs_async_reqs *args;
1704 
1705 	rp = VTOR(vp);
1706 	ASSERT(rp->r_freef == NULL);
1707 
1708 	mi = VTOMI(vp);
1709 
1710 	/*
1711 	 * If we can't allocate a request structure, do the readdir
1712 	 * operation synchronously in this thread's context.
1713 	 */
1714 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1715 		goto noasync;
1716 
1717 	args->a_next = NULL;
1718 #ifdef DEBUG
1719 	args->a_queuer = curthread;
1720 #endif
1721 	VN_HOLD(vp);
1722 	args->a_vp = vp;
1723 	ASSERT(cr != NULL);
1724 	crhold(cr);
1725 	args->a_cred = cr;
1726 	args->a_io = NFS_READDIR;
1727 	args->a_nfs_readdir = readdir;
1728 	args->a_nfs_rdc = rdc;
1729 
1730 	mutex_enter(&mi->mi_async_lock);
1731 
1732 	/*
1733 	 * If asyncio has been disabled, then make a synchronous request.
1734 	 */
1735 	if (mi->mi_max_threads == 0) {
1736 		mutex_exit(&mi->mi_async_lock);
1737 		goto noasync;
1738 	}
1739 
1740 	/*
1741 	 * Link request structure into the async list and
1742 	 * wakeup async thread to do the i/o.
1743 	 */
1744 	if (mi->mi_async_reqs[NFS_READDIR] == NULL) {
1745 		mi->mi_async_reqs[NFS_READDIR] = args;
1746 		mi->mi_async_tail[NFS_READDIR] = args;
1747 	} else {
1748 		mi->mi_async_tail[NFS_READDIR]->a_next = args;
1749 		mi->mi_async_tail[NFS_READDIR] = args;
1750 	}
1751 
1752 	mutex_enter(&rp->r_statelock);
1753 	rp->r_count++;
1754 	mutex_exit(&rp->r_statelock);
1755 
1756 	if (mi->mi_io_kstats) {
1757 		mutex_enter(&mi->mi_lock);
1758 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1759 		mutex_exit(&mi->mi_lock);
1760 	}
1761 
1762 	mi->mi_async_req_count++;
1763 	ASSERT(mi->mi_async_req_count != 0);
1764 	cv_signal(&mi->mi_async_reqs_cv);
1765 	mutex_exit(&mi->mi_async_lock);
1766 	return;
1767 
1768 noasync:
1769 	if (args != NULL) {
1770 		VN_RELE(vp);
1771 		crfree(cr);
1772 		kmem_free(args, sizeof (*args));
1773 	}
1774 
1775 	rdc->entries = NULL;
1776 	mutex_enter(&rp->r_statelock);
1777 	ASSERT(rdc->flags & RDDIR);
1778 	rdc->flags &= ~RDDIR;
1779 	rdc->flags |= RDDIRREQ;
1780 	/*
1781 	 * Check the flag to see if RDDIRWAIT is set. If RDDIRWAIT
1782 	 * is set, wakeup the thread sleeping in cv_wait_sig().
1783 	 * The woken up thread will reset the flag to RDDIR and will
1784 	 * continue with the readdir opeartion.
1785 	 */
1786 	if (rdc->flags & RDDIRWAIT) {
1787 		rdc->flags &= ~RDDIRWAIT;
1788 		cv_broadcast(&rdc->cv);
1789 	}
1790 	mutex_exit(&rp->r_statelock);
1791 	rddir_cache_rele(rdc);
1792 }
1793 
1794 void
1795 nfs_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
1796 	cred_t *cr, void (*commit)(vnode_t *, page_t *, offset3, count3,
1797 	cred_t *))
1798 {
1799 	rnode_t *rp;
1800 	mntinfo_t *mi;
1801 	struct nfs_async_reqs *args;
1802 	page_t *pp;
1803 
1804 	rp = VTOR(vp);
1805 	mi = VTOMI(vp);
1806 
1807 	/*
1808 	 * If we can't allocate a request structure, do the commit
1809 	 * operation synchronously in this thread's context.
1810 	 */
1811 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1812 		goto noasync;
1813 
1814 	args->a_next = NULL;
1815 #ifdef DEBUG
1816 	args->a_queuer = curthread;
1817 #endif
1818 	VN_HOLD(vp);
1819 	args->a_vp = vp;
1820 	ASSERT(cr != NULL);
1821 	crhold(cr);
1822 	args->a_cred = cr;
1823 	args->a_io = NFS_COMMIT;
1824 	args->a_nfs_commit = commit;
1825 	args->a_nfs_plist = plist;
1826 	args->a_nfs_offset = offset;
1827 	args->a_nfs_count = count;
1828 
1829 	mutex_enter(&mi->mi_async_lock);
1830 
1831 	/*
1832 	 * If asyncio has been disabled, then make a synchronous request.
1833 	 * This check is done a second time in case async io was diabled
1834 	 * while this thread was blocked waiting for memory pressure to
1835 	 * reduce or for the queue to drain.
1836 	 */
1837 	if (mi->mi_max_threads == 0) {
1838 		mutex_exit(&mi->mi_async_lock);
1839 		goto noasync;
1840 	}
1841 
1842 	/*
1843 	 * Link request structure into the async list and
1844 	 * wakeup async thread to do the i/o.
1845 	 */
1846 	if (mi->mi_async_reqs[NFS_COMMIT] == NULL) {
1847 		mi->mi_async_reqs[NFS_COMMIT] = args;
1848 		mi->mi_async_tail[NFS_COMMIT] = args;
1849 	} else {
1850 		mi->mi_async_tail[NFS_COMMIT]->a_next = args;
1851 		mi->mi_async_tail[NFS_COMMIT] = args;
1852 	}
1853 
1854 	mutex_enter(&rp->r_statelock);
1855 	rp->r_count++;
1856 	mutex_exit(&rp->r_statelock);
1857 
1858 	if (mi->mi_io_kstats) {
1859 		mutex_enter(&mi->mi_lock);
1860 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1861 		mutex_exit(&mi->mi_lock);
1862 	}
1863 
1864 	mi->mi_async_req_count++;
1865 	ASSERT(mi->mi_async_req_count != 0);
1866 	cv_signal(&mi->mi_async_reqs_cv);
1867 	mutex_exit(&mi->mi_async_lock);
1868 	return;
1869 
1870 noasync:
1871 	if (args != NULL) {
1872 		VN_RELE(vp);
1873 		crfree(cr);
1874 		kmem_free(args, sizeof (*args));
1875 	}
1876 
1877 	if (curproc == proc_pageout || curproc == proc_fsflush ||
1878 	    nfs_zone() != mi->mi_zone) {
1879 		while (plist != NULL) {
1880 			pp = plist;
1881 			page_sub(&plist, pp);
1882 			pp->p_fsdata = C_COMMIT;
1883 			page_unlock(pp);
1884 		}
1885 		return;
1886 	}
1887 	(*commit)(vp, plist, offset, count, cr);
1888 }
1889 
1890 void
1891 nfs_async_inactive(vnode_t *vp, cred_t *cr,
1892     void (*inactive)(vnode_t *, cred_t *, caller_context_t *))
1893 {
1894 	mntinfo_t *mi;
1895 	struct nfs_async_reqs *args;
1896 
1897 	mi = VTOMI(vp);
1898 
1899 	args = kmem_alloc(sizeof (*args), KM_SLEEP);
1900 	args->a_next = NULL;
1901 #ifdef DEBUG
1902 	args->a_queuer = curthread;
1903 #endif
1904 	args->a_vp = vp;
1905 	ASSERT(cr != NULL);
1906 	crhold(cr);
1907 	args->a_cred = cr;
1908 	args->a_io = NFS_INACTIVE;
1909 	args->a_nfs_inactive = inactive;
1910 
1911 	/*
1912 	 * Note that we don't check mi->mi_max_threads here, since we
1913 	 * *need* to get rid of this vnode regardless of whether someone
1914 	 * set nfs3_max_threads/nfs_max_threads to zero in /etc/system.
1915 	 *
1916 	 * The manager thread knows about this and is willing to create
1917 	 * at least one thread to accommodate us.
1918 	 */
1919 	mutex_enter(&mi->mi_async_lock);
1920 	if (mi->mi_manager_thread == NULL) {
1921 		rnode_t *rp = VTOR(vp);
1922 
1923 		mutex_exit(&mi->mi_async_lock);
1924 		crfree(cr);	/* drop our reference */
1925 		kmem_free(args, sizeof (*args));
1926 		/*
1927 		 * We can't do an over-the-wire call since we're in the wrong
1928 		 * zone, so we need to clean up state as best we can and then
1929 		 * throw away the vnode.
1930 		 */
1931 		mutex_enter(&rp->r_statelock);
1932 		if (rp->r_unldvp != NULL) {
1933 			vnode_t *unldvp;
1934 			char *unlname;
1935 			cred_t *unlcred;
1936 
1937 			unldvp = rp->r_unldvp;
1938 			rp->r_unldvp = NULL;
1939 			unlname = rp->r_unlname;
1940 			rp->r_unlname = NULL;
1941 			unlcred = rp->r_unlcred;
1942 			rp->r_unlcred = NULL;
1943 			mutex_exit(&rp->r_statelock);
1944 
1945 			VN_RELE(unldvp);
1946 			kmem_free(unlname, MAXNAMELEN);
1947 			crfree(unlcred);
1948 		} else {
1949 			mutex_exit(&rp->r_statelock);
1950 		}
1951 		/*
1952 		 * No need to explicitly throw away any cached pages.  The
1953 		 * eventual rinactive() will attempt a synchronous
1954 		 * VOP_PUTPAGE() which will immediately fail since the request
1955 		 * is coming from the wrong zone, and then will proceed to call
1956 		 * nfs_invalidate_pages() which will clean things up for us.
1957 		 */
1958 		rp_addfree(VTOR(vp), cr);
1959 		return;
1960 	}
1961 
1962 	if (mi->mi_async_reqs[NFS_INACTIVE] == NULL) {
1963 		mi->mi_async_reqs[NFS_INACTIVE] = args;
1964 	} else {
1965 		mi->mi_async_tail[NFS_INACTIVE]->a_next = args;
1966 	}
1967 	mi->mi_async_tail[NFS_INACTIVE] = args;
1968 	/*
1969 	 * Don't increment r_count, since we're trying to get rid of the vnode.
1970 	 */
1971 
1972 	mi->mi_async_req_count++;
1973 	ASSERT(mi->mi_async_req_count != 0);
1974 	cv_signal(&mi->mi_async_reqs_cv);
1975 	mutex_exit(&mi->mi_async_lock);
1976 }
1977 
1978 /*
1979  * The async queues for each mounted file system are arranged as a
1980  * set of queues, one for each async i/o type.  Requests are taken
1981  * from the queues in a round-robin fashion.  A number of consecutive
1982  * requests are taken from each queue before moving on to the next
1983  * queue.  This functionality may allow the NFS Version 2 server to do
1984  * write clustering, even if the client is mixing writes and reads
1985  * because it will take multiple write requests from the queue
1986  * before processing any of the other async i/o types.
1987  *
1988  * XXX The nfs_async_start thread is unsafe in the light of the present
1989  * model defined by cpr to suspend the system. Specifically over the
1990  * wire calls are cpr-unsafe. The thread should be reevaluated in
1991  * case of future updates to the cpr model.
1992  */
1993 static void
1994 nfs_async_start(struct vfs *vfsp)
1995 {
1996 	struct nfs_async_reqs *args;
1997 	mntinfo_t *mi = VFTOMI(vfsp);
1998 	clock_t time_left = 1;
1999 	callb_cpr_t cprinfo;
2000 	int i;
2001 
2002 	/*
2003 	 * Dynamic initialization of nfs_async_timeout to allow nfs to be
2004 	 * built in an implementation independent manner.
2005 	 */
2006 	if (nfs_async_timeout == -1)
2007 		nfs_async_timeout = NFS_ASYNC_TIMEOUT;
2008 
2009 	CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr, "nas");
2010 
2011 	mutex_enter(&mi->mi_async_lock);
2012 	for (;;) {
2013 		/*
2014 		 * Find the next queue containing an entry.  We start
2015 		 * at the current queue pointer and then round robin
2016 		 * through all of them until we either find a non-empty
2017 		 * queue or have looked through all of them.
2018 		 */
2019 		for (i = 0; i < NFS_ASYNC_TYPES; i++) {
2020 			args = *mi->mi_async_curr;
2021 			if (args != NULL)
2022 				break;
2023 			mi->mi_async_curr++;
2024 			if (mi->mi_async_curr ==
2025 			    &mi->mi_async_reqs[NFS_ASYNC_TYPES])
2026 				mi->mi_async_curr = &mi->mi_async_reqs[0];
2027 		}
2028 		/*
2029 		 * If we didn't find a entry, then block until woken up
2030 		 * again and then look through the queues again.
2031 		 */
2032 		if (args == NULL) {
2033 			/*
2034 			 * Exiting is considered to be safe for CPR as well
2035 			 */
2036 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
2037 
2038 			/*
2039 			 * Wakeup thread waiting to unmount the file
2040 			 * system only if all async threads are inactive.
2041 			 *
2042 			 * If we've timed-out and there's nothing to do,
2043 			 * then get rid of this thread.
2044 			 */
2045 			if (mi->mi_max_threads == 0 || time_left <= 0) {
2046 				if (--mi->mi_threads == 0)
2047 					cv_signal(&mi->mi_async_cv);
2048 				CALLB_CPR_EXIT(&cprinfo);
2049 				VFS_RELE(vfsp);	/* release thread's hold */
2050 				zthread_exit();
2051 				/* NOTREACHED */
2052 			}
2053 			time_left = cv_timedwait(&mi->mi_async_work_cv,
2054 			    &mi->mi_async_lock, nfs_async_timeout + lbolt);
2055 
2056 			CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock);
2057 
2058 			continue;
2059 		}
2060 		time_left = 1;
2061 
2062 		/*
2063 		 * Remove the request from the async queue and then
2064 		 * update the current async request queue pointer.  If
2065 		 * the current queue is empty or we have removed enough
2066 		 * consecutive entries from it, then reset the counter
2067 		 * for this queue and then move the current pointer to
2068 		 * the next queue.
2069 		 */
2070 		*mi->mi_async_curr = args->a_next;
2071 		if (*mi->mi_async_curr == NULL ||
2072 		    --mi->mi_async_clusters[args->a_io] == 0) {
2073 			mi->mi_async_clusters[args->a_io] =
2074 			    mi->mi_async_init_clusters;
2075 			mi->mi_async_curr++;
2076 			if (mi->mi_async_curr ==
2077 			    &mi->mi_async_reqs[NFS_ASYNC_TYPES])
2078 				mi->mi_async_curr = &mi->mi_async_reqs[0];
2079 		}
2080 
2081 		if (args->a_io != NFS_INACTIVE && mi->mi_io_kstats) {
2082 			mutex_enter(&mi->mi_lock);
2083 			kstat_waitq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
2084 			mutex_exit(&mi->mi_lock);
2085 		}
2086 
2087 		mutex_exit(&mi->mi_async_lock);
2088 
2089 		/*
2090 		 * Obtain arguments from the async request structure.
2091 		 */
2092 		if (args->a_io == NFS_READ_AHEAD && mi->mi_max_threads > 0) {
2093 			(*args->a_nfs_readahead)(args->a_vp, args->a_nfs_blkoff,
2094 			    args->a_nfs_addr, args->a_nfs_seg,
2095 			    args->a_cred);
2096 		} else if (args->a_io == NFS_PUTAPAGE) {
2097 			(void) (*args->a_nfs_putapage)(args->a_vp,
2098 			    args->a_nfs_pp, args->a_nfs_off,
2099 			    args->a_nfs_len, args->a_nfs_flags,
2100 			    args->a_cred);
2101 		} else if (args->a_io == NFS_PAGEIO) {
2102 			(void) (*args->a_nfs_pageio)(args->a_vp,
2103 			    args->a_nfs_pp, args->a_nfs_off,
2104 			    args->a_nfs_len, args->a_nfs_flags,
2105 			    args->a_cred);
2106 		} else if (args->a_io == NFS_READDIR) {
2107 			(void) ((*args->a_nfs_readdir)(args->a_vp,
2108 			    args->a_nfs_rdc, args->a_cred));
2109 		} else if (args->a_io == NFS_COMMIT) {
2110 			(*args->a_nfs_commit)(args->a_vp, args->a_nfs_plist,
2111 			    args->a_nfs_offset, args->a_nfs_count,
2112 			    args->a_cred);
2113 		} else if (args->a_io == NFS_INACTIVE) {
2114 			(*args->a_nfs_inactive)(args->a_vp, args->a_cred, NULL);
2115 		}
2116 
2117 		/*
2118 		 * Now, release the vnode and free the credentials
2119 		 * structure.
2120 		 */
2121 		free_async_args(args);
2122 		/*
2123 		 * Reacquire the mutex because it will be needed above.
2124 		 */
2125 		mutex_enter(&mi->mi_async_lock);
2126 	}
2127 }
2128 
2129 void
2130 nfs_async_stop(struct vfs *vfsp)
2131 {
2132 	mntinfo_t *mi = VFTOMI(vfsp);
2133 
2134 	/*
2135 	 * Wait for all outstanding async operations to complete and for the
2136 	 * worker threads to exit.
2137 	 */
2138 	mutex_enter(&mi->mi_async_lock);
2139 	mi->mi_max_threads = 0;
2140 	cv_broadcast(&mi->mi_async_work_cv);
2141 	while (mi->mi_threads != 0)
2142 		cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
2143 	mutex_exit(&mi->mi_async_lock);
2144 }
2145 
2146 /*
2147  * nfs_async_stop_sig:
2148  * Wait for all outstanding putpage operation to complete. If a signal
2149  * is deliver we will abort and return non-zero. If we can put all the
2150  * pages we will return 0. This routine is called from nfs_unmount and
2151  * nfs3_unmount to make these operations interruptible.
2152  */
2153 int
2154 nfs_async_stop_sig(struct vfs *vfsp)
2155 {
2156 	mntinfo_t *mi = VFTOMI(vfsp);
2157 	ushort_t omax;
2158 	int rval;
2159 
2160 	/*
2161 	 * Wait for all outstanding async operations to complete and for the
2162 	 * worker threads to exit.
2163 	 */
2164 	mutex_enter(&mi->mi_async_lock);
2165 	omax = mi->mi_max_threads;
2166 	mi->mi_max_threads = 0;
2167 	/*
2168 	 * Tell all the worker threads to exit.
2169 	 */
2170 	cv_broadcast(&mi->mi_async_work_cv);
2171 	while (mi->mi_threads != 0) {
2172 		if (!cv_wait_sig(&mi->mi_async_cv, &mi->mi_async_lock))
2173 			break;
2174 	}
2175 	rval = (mi->mi_threads != 0);	/* Interrupted */
2176 	if (rval)
2177 		mi->mi_max_threads = omax;
2178 	mutex_exit(&mi->mi_async_lock);
2179 
2180 	return (rval);
2181 }
2182 
2183 int
2184 writerp(rnode_t *rp, caddr_t base, int tcount, struct uio *uio, int pgcreated)
2185 {
2186 	int pagecreate;
2187 	int n;
2188 	int saved_n;
2189 	caddr_t saved_base;
2190 	u_offset_t offset;
2191 	int error;
2192 	int sm_error;
2193 	vnode_t *vp = RTOV(rp);
2194 
2195 	ASSERT(tcount <= MAXBSIZE && tcount <= uio->uio_resid);
2196 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_WRITER));
2197 	if (!vpm_enable) {
2198 		ASSERT(((uintptr_t)base & MAXBOFFSET) + tcount <= MAXBSIZE);
2199 	}
2200 
2201 	/*
2202 	 * Move bytes in at most PAGESIZE chunks. We must avoid
2203 	 * spanning pages in uiomove() because page faults may cause
2204 	 * the cache to be invalidated out from under us. The r_size is not
2205 	 * updated until after the uiomove. If we push the last page of a
2206 	 * file before r_size is correct, we will lose the data written past
2207 	 * the current (and invalid) r_size.
2208 	 */
2209 	do {
2210 		offset = uio->uio_loffset;
2211 		pagecreate = 0;
2212 
2213 		/*
2214 		 * n is the number of bytes required to satisfy the request
2215 		 *   or the number of bytes to fill out the page.
2216 		 */
2217 		n = (int)MIN((PAGESIZE - (offset & PAGEOFFSET)), tcount);
2218 
2219 		/*
2220 		 * Check to see if we can skip reading in the page
2221 		 * and just allocate the memory.  We can do this
2222 		 * if we are going to rewrite the entire mapping
2223 		 * or if we are going to write to or beyond the current
2224 		 * end of file from the beginning of the mapping.
2225 		 *
2226 		 * The read of r_size is now protected by r_statelock.
2227 		 */
2228 		mutex_enter(&rp->r_statelock);
2229 		/*
2230 		 * When pgcreated is nonzero the caller has already done
2231 		 * a segmap_getmapflt with forcefault 0 and S_WRITE. With
2232 		 * segkpm this means we already have at least one page
2233 		 * created and mapped at base.
2234 		 */
2235 		pagecreate = pgcreated ||
2236 		    ((offset & PAGEOFFSET) == 0 &&
2237 		    (n == PAGESIZE || ((offset + n) >= rp->r_size)));
2238 
2239 		mutex_exit(&rp->r_statelock);
2240 		if (!vpm_enable && pagecreate) {
2241 			/*
2242 			 * The last argument tells segmap_pagecreate() to
2243 			 * always lock the page, as opposed to sometimes
2244 			 * returning with the page locked. This way we avoid a
2245 			 * fault on the ensuing uiomove(), but also
2246 			 * more importantly (to fix bug 1094402) we can
2247 			 * call segmap_fault() to unlock the page in all
2248 			 * cases. An alternative would be to modify
2249 			 * segmap_pagecreate() to tell us when it is
2250 			 * locking a page, but that's a fairly major
2251 			 * interface change.
2252 			 */
2253 			if (pgcreated == 0)
2254 				(void) segmap_pagecreate(segkmap, base,
2255 				    (uint_t)n, 1);
2256 			saved_base = base;
2257 			saved_n = n;
2258 		}
2259 
2260 		/*
2261 		 * The number of bytes of data in the last page can not
2262 		 * be accurately be determined while page is being
2263 		 * uiomove'd to and the size of the file being updated.
2264 		 * Thus, inform threads which need to know accurately
2265 		 * how much data is in the last page of the file.  They
2266 		 * will not do the i/o immediately, but will arrange for
2267 		 * the i/o to happen later when this modify operation
2268 		 * will have finished.
2269 		 */
2270 		ASSERT(!(rp->r_flags & RMODINPROGRESS));
2271 		mutex_enter(&rp->r_statelock);
2272 		rp->r_flags |= RMODINPROGRESS;
2273 		rp->r_modaddr = (offset & MAXBMASK);
2274 		mutex_exit(&rp->r_statelock);
2275 
2276 		if (vpm_enable) {
2277 			/*
2278 			 * Copy data. If new pages are created, part of
2279 			 * the page that is not written will be initizliazed
2280 			 * with zeros.
2281 			 */
2282 			error = vpm_data_copy(vp, offset, n, uio,
2283 			    !pagecreate, NULL, 0, S_WRITE);
2284 		} else {
2285 			error = uiomove(base, n, UIO_WRITE, uio);
2286 		}
2287 
2288 		/*
2289 		 * r_size is the maximum number of
2290 		 * bytes known to be in the file.
2291 		 * Make sure it is at least as high as the
2292 		 * first unwritten byte pointed to by uio_loffset.
2293 		 */
2294 		mutex_enter(&rp->r_statelock);
2295 		if (rp->r_size < uio->uio_loffset)
2296 			rp->r_size = uio->uio_loffset;
2297 		rp->r_flags &= ~RMODINPROGRESS;
2298 		rp->r_flags |= RDIRTY;
2299 		mutex_exit(&rp->r_statelock);
2300 
2301 		/* n = # of bytes written */
2302 		n = (int)(uio->uio_loffset - offset);
2303 
2304 		if (!vpm_enable) {
2305 			base += n;
2306 		}
2307 		tcount -= n;
2308 		/*
2309 		 * If we created pages w/o initializing them completely,
2310 		 * we need to zero the part that wasn't set up.
2311 		 * This happens on a most EOF write cases and if
2312 		 * we had some sort of error during the uiomove.
2313 		 */
2314 		if (!vpm_enable && pagecreate) {
2315 			if ((uio->uio_loffset & PAGEOFFSET) || n == 0)
2316 				(void) kzero(base, PAGESIZE - n);
2317 
2318 			if (pgcreated) {
2319 				/*
2320 				 * Caller is responsible for this page,
2321 				 * it was not created in this loop.
2322 				 */
2323 				pgcreated = 0;
2324 			} else {
2325 				/*
2326 				 * For bug 1094402: segmap_pagecreate locks
2327 				 * page. Unlock it. This also unlocks the
2328 				 * pages allocated by page_create_va() in
2329 				 * segmap_pagecreate().
2330 				 */
2331 				sm_error = segmap_fault(kas.a_hat, segkmap,
2332 				    saved_base, saved_n,
2333 				    F_SOFTUNLOCK, S_WRITE);
2334 				if (error == 0)
2335 					error = sm_error;
2336 			}
2337 		}
2338 	} while (tcount > 0 && error == 0);
2339 
2340 	return (error);
2341 }
2342 
2343 int
2344 nfs_putpages(vnode_t *vp, u_offset_t off, size_t len, int flags, cred_t *cr)
2345 {
2346 	rnode_t *rp;
2347 	page_t *pp;
2348 	u_offset_t eoff;
2349 	u_offset_t io_off;
2350 	size_t io_len;
2351 	int error;
2352 	int rdirty;
2353 	int err;
2354 
2355 	rp = VTOR(vp);
2356 	ASSERT(rp->r_count > 0);
2357 
2358 	if (!vn_has_cached_data(vp))
2359 		return (0);
2360 
2361 	ASSERT(vp->v_type != VCHR);
2362 
2363 	/*
2364 	 * If ROUTOFSPACE is set, then all writes turn into B_INVAL
2365 	 * writes.  B_FORCE is set to force the VM system to actually
2366 	 * invalidate the pages, even if the i/o failed.  The pages
2367 	 * need to get invalidated because they can't be written out
2368 	 * because there isn't any space left on either the server's
2369 	 * file system or in the user's disk quota.  The B_FREE bit
2370 	 * is cleared to avoid confusion as to whether this is a
2371 	 * request to place the page on the freelist or to destroy
2372 	 * it.
2373 	 */
2374 	if ((rp->r_flags & ROUTOFSPACE) ||
2375 	    (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED))
2376 		flags = (flags & ~B_FREE) | B_INVAL | B_FORCE;
2377 
2378 	if (len == 0) {
2379 		/*
2380 		 * If doing a full file synchronous operation, then clear
2381 		 * the RDIRTY bit.  If a page gets dirtied while the flush
2382 		 * is happening, then RDIRTY will get set again.  The
2383 		 * RDIRTY bit must get cleared before the flush so that
2384 		 * we don't lose this information.
2385 		 *
2386 		 * If there are no full file async write operations
2387 		 * pending and RDIRTY bit is set, clear it.
2388 		 */
2389 		if (off == (u_offset_t)0 &&
2390 		    !(flags & B_ASYNC) &&
2391 		    (rp->r_flags & RDIRTY)) {
2392 			mutex_enter(&rp->r_statelock);
2393 			rdirty = (rp->r_flags & RDIRTY);
2394 			rp->r_flags &= ~RDIRTY;
2395 			mutex_exit(&rp->r_statelock);
2396 		} else if (flags & B_ASYNC && off == (u_offset_t)0) {
2397 			mutex_enter(&rp->r_statelock);
2398 			if (rp->r_flags & RDIRTY && rp->r_awcount == 0) {
2399 				rdirty = (rp->r_flags & RDIRTY);
2400 				rp->r_flags &= ~RDIRTY;
2401 			}
2402 			mutex_exit(&rp->r_statelock);
2403 		} else
2404 			rdirty = 0;
2405 
2406 		/*
2407 		 * Search the entire vp list for pages >= off, and flush
2408 		 * the dirty pages.
2409 		 */
2410 		error = pvn_vplist_dirty(vp, off, rp->r_putapage,
2411 		    flags, cr);
2412 
2413 		/*
2414 		 * If an error occurred and the file was marked as dirty
2415 		 * before and we aren't forcibly invalidating pages, then
2416 		 * reset the RDIRTY flag.
2417 		 */
2418 		if (error && rdirty &&
2419 		    (flags & (B_INVAL | B_FORCE)) != (B_INVAL | B_FORCE)) {
2420 			mutex_enter(&rp->r_statelock);
2421 			rp->r_flags |= RDIRTY;
2422 			mutex_exit(&rp->r_statelock);
2423 		}
2424 	} else {
2425 		/*
2426 		 * Do a range from [off...off + len) looking for pages
2427 		 * to deal with.
2428 		 */
2429 		error = 0;
2430 #ifdef lint
2431 		io_len = 0;
2432 #endif
2433 		eoff = off + len;
2434 		mutex_enter(&rp->r_statelock);
2435 		for (io_off = off; io_off < eoff && io_off < rp->r_size;
2436 		    io_off += io_len) {
2437 			mutex_exit(&rp->r_statelock);
2438 			/*
2439 			 * If we are not invalidating, synchronously
2440 			 * freeing or writing pages use the routine
2441 			 * page_lookup_nowait() to prevent reclaiming
2442 			 * them from the free list.
2443 			 */
2444 			if ((flags & B_INVAL) || !(flags & B_ASYNC)) {
2445 				pp = page_lookup(vp, io_off,
2446 				    (flags & (B_INVAL | B_FREE)) ?
2447 				    SE_EXCL : SE_SHARED);
2448 			} else {
2449 				pp = page_lookup_nowait(vp, io_off,
2450 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
2451 			}
2452 
2453 			if (pp == NULL || !pvn_getdirty(pp, flags))
2454 				io_len = PAGESIZE;
2455 			else {
2456 				err = (*rp->r_putapage)(vp, pp, &io_off,
2457 				    &io_len, flags, cr);
2458 				if (!error)
2459 					error = err;
2460 				/*
2461 				 * "io_off" and "io_len" are returned as
2462 				 * the range of pages we actually wrote.
2463 				 * This allows us to skip ahead more quickly
2464 				 * since several pages may've been dealt
2465 				 * with by this iteration of the loop.
2466 				 */
2467 			}
2468 			mutex_enter(&rp->r_statelock);
2469 		}
2470 		mutex_exit(&rp->r_statelock);
2471 	}
2472 
2473 	return (error);
2474 }
2475 
2476 void
2477 nfs_invalidate_pages(vnode_t *vp, u_offset_t off, cred_t *cr)
2478 {
2479 	rnode_t *rp;
2480 
2481 	rp = VTOR(vp);
2482 	mutex_enter(&rp->r_statelock);
2483 	while (rp->r_flags & RTRUNCATE)
2484 		cv_wait(&rp->r_cv, &rp->r_statelock);
2485 	rp->r_flags |= RTRUNCATE;
2486 	if (off == (u_offset_t)0) {
2487 		rp->r_flags &= ~RDIRTY;
2488 		if (!(rp->r_flags & RSTALE))
2489 			rp->r_error = 0;
2490 	}
2491 	rp->r_truncaddr = off;
2492 	mutex_exit(&rp->r_statelock);
2493 	(void) pvn_vplist_dirty(vp, off, rp->r_putapage,
2494 	    B_INVAL | B_TRUNC, cr);
2495 	mutex_enter(&rp->r_statelock);
2496 	rp->r_flags &= ~RTRUNCATE;
2497 	cv_broadcast(&rp->r_cv);
2498 	mutex_exit(&rp->r_statelock);
2499 }
2500 
2501 static int nfs_write_error_to_cons_only = 0;
2502 #define	MSG(x)	(nfs_write_error_to_cons_only ? (x) : (x) + 1)
2503 
2504 /*
2505  * Print a file handle
2506  */
2507 void
2508 nfs_printfhandle(nfs_fhandle *fhp)
2509 {
2510 	int *ip;
2511 	char *buf;
2512 	size_t bufsize;
2513 	char *cp;
2514 
2515 	/*
2516 	 * 13 == "(file handle:"
2517 	 * maximum of NFS_FHANDLE / sizeof (*ip) elements in fh_buf times
2518 	 *	1 == ' '
2519 	 *	8 == maximum strlen of "%x"
2520 	 * 3 == ")\n\0"
2521 	 */
2522 	bufsize = 13 + ((NFS_FHANDLE_LEN / sizeof (*ip)) * (1 + 8)) + 3;
2523 	buf = kmem_alloc(bufsize, KM_NOSLEEP);
2524 	if (buf == NULL)
2525 		return;
2526 
2527 	cp = buf;
2528 	(void) strcpy(cp, "(file handle:");
2529 	while (*cp != '\0')
2530 		cp++;
2531 	for (ip = (int *)fhp->fh_buf;
2532 	    ip < (int *)&fhp->fh_buf[fhp->fh_len];
2533 	    ip++) {
2534 		(void) sprintf(cp, " %x", *ip);
2535 		while (*cp != '\0')
2536 			cp++;
2537 	}
2538 	(void) strcpy(cp, ")\n");
2539 
2540 	zcmn_err(getzoneid(), CE_CONT, MSG("^%s"), buf);
2541 
2542 	kmem_free(buf, bufsize);
2543 }
2544 
2545 /*
2546  * Notify the system administrator that an NFS write error has
2547  * occurred.
2548  */
2549 
2550 /* seconds between ENOSPC/EDQUOT messages */
2551 clock_t nfs_write_error_interval = 5;
2552 
2553 void
2554 nfs_write_error(vnode_t *vp, int error, cred_t *cr)
2555 {
2556 	mntinfo_t *mi;
2557 
2558 	mi = VTOMI(vp);
2559 	/*
2560 	 * In case of forced unmount or zone shutdown, do not print any
2561 	 * messages since it can flood the console with error messages.
2562 	 */
2563 	if (FS_OR_ZONE_GONE(mi->mi_vfsp))
2564 		return;
2565 
2566 	/*
2567 	 * No use in flooding the console with ENOSPC
2568 	 * messages from the same file system.
2569 	 */
2570 	if ((error != ENOSPC && error != EDQUOT) ||
2571 	    lbolt - mi->mi_printftime > 0) {
2572 		zoneid_t zoneid = mi->mi_zone->zone_id;
2573 
2574 #ifdef DEBUG
2575 		nfs_perror(error, "NFS%ld write error on host %s: %m.\n",
2576 		    mi->mi_vers, VTOR(vp)->r_server->sv_hostname, NULL);
2577 #else
2578 		nfs_perror(error, "NFS write error on host %s: %m.\n",
2579 		    VTOR(vp)->r_server->sv_hostname, NULL);
2580 #endif
2581 		if (error == ENOSPC || error == EDQUOT) {
2582 			zcmn_err(zoneid, CE_CONT,
2583 			    MSG("^File: userid=%d, groupid=%d\n"),
2584 			    crgetuid(cr), crgetgid(cr));
2585 			if (crgetuid(CRED()) != crgetuid(cr) ||
2586 			    crgetgid(CRED()) != crgetgid(cr)) {
2587 				zcmn_err(zoneid, CE_CONT,
2588 				    MSG("^User: userid=%d, groupid=%d\n"),
2589 				    crgetuid(CRED()), crgetgid(CRED()));
2590 			}
2591 			mi->mi_printftime = lbolt +
2592 			    nfs_write_error_interval * hz;
2593 		}
2594 		nfs_printfhandle(&VTOR(vp)->r_fh);
2595 #ifdef DEBUG
2596 		if (error == EACCES) {
2597 			zcmn_err(zoneid, CE_CONT,
2598 			    MSG("^nfs_bio: cred is%s kcred\n"),
2599 			    cr == kcred ? "" : " not");
2600 		}
2601 #endif
2602 	}
2603 }
2604 
2605 /* ARGSUSED */
2606 static void *
2607 nfs_mi_init(zoneid_t zoneid)
2608 {
2609 	struct mi_globals *mig;
2610 
2611 	mig = kmem_alloc(sizeof (*mig), KM_SLEEP);
2612 	mutex_init(&mig->mig_lock, NULL, MUTEX_DEFAULT, NULL);
2613 	list_create(&mig->mig_list, sizeof (mntinfo_t),
2614 	    offsetof(mntinfo_t, mi_zone_node));
2615 	mig->mig_destructor_called = B_FALSE;
2616 	return (mig);
2617 }
2618 
2619 /*
2620  * Callback routine to tell all NFS mounts in the zone to stop creating new
2621  * threads.  Existing threads should exit.
2622  */
2623 /* ARGSUSED */
2624 static void
2625 nfs_mi_shutdown(zoneid_t zoneid, void *data)
2626 {
2627 	struct mi_globals *mig = data;
2628 	mntinfo_t *mi;
2629 
2630 	ASSERT(mig != NULL);
2631 again:
2632 	mutex_enter(&mig->mig_lock);
2633 	for (mi = list_head(&mig->mig_list); mi != NULL;
2634 	    mi = list_next(&mig->mig_list, mi)) {
2635 
2636 		/*
2637 		 * If we've done the shutdown work for this FS, skip.
2638 		 * Once we go off the end of the list, we're done.
2639 		 */
2640 		if (mi->mi_flags & MI_DEAD)
2641 			continue;
2642 
2643 		/*
2644 		 * We will do work, so not done.  Get a hold on the FS.
2645 		 */
2646 		VFS_HOLD(mi->mi_vfsp);
2647 
2648 		/*
2649 		 * purge the DNLC for this filesystem
2650 		 */
2651 		(void) dnlc_purge_vfsp(mi->mi_vfsp, 0);
2652 
2653 		mutex_enter(&mi->mi_async_lock);
2654 		/*
2655 		 * Tell existing async worker threads to exit.
2656 		 */
2657 		mi->mi_max_threads = 0;
2658 		cv_broadcast(&mi->mi_async_work_cv);
2659 		/*
2660 		 * Set MI_ASYNC_MGR_STOP so the async manager thread starts
2661 		 * getting ready to exit when it's done with its current work.
2662 		 * Also set MI_DEAD to note we've acted on this FS.
2663 		 */
2664 		mutex_enter(&mi->mi_lock);
2665 		mi->mi_flags |= (MI_ASYNC_MGR_STOP|MI_DEAD);
2666 		mutex_exit(&mi->mi_lock);
2667 		/*
2668 		 * Wake up the async manager thread.
2669 		 */
2670 		cv_broadcast(&mi->mi_async_reqs_cv);
2671 		mutex_exit(&mi->mi_async_lock);
2672 
2673 		/*
2674 		 * Drop lock and release FS, which may change list, then repeat.
2675 		 * We're done when every mi has been done or the list is empty.
2676 		 */
2677 		mutex_exit(&mig->mig_lock);
2678 		VFS_RELE(mi->mi_vfsp);
2679 		goto again;
2680 	}
2681 	mutex_exit(&mig->mig_lock);
2682 }
2683 
2684 static void
2685 nfs_mi_free_globals(struct mi_globals *mig)
2686 {
2687 	list_destroy(&mig->mig_list);	/* makes sure the list is empty */
2688 	mutex_destroy(&mig->mig_lock);
2689 	kmem_free(mig, sizeof (*mig));
2690 
2691 }
2692 
2693 /* ARGSUSED */
2694 static void
2695 nfs_mi_destroy(zoneid_t zoneid, void *data)
2696 {
2697 	struct mi_globals *mig = data;
2698 
2699 	ASSERT(mig != NULL);
2700 	mutex_enter(&mig->mig_lock);
2701 	if (list_head(&mig->mig_list) != NULL) {
2702 		/* Still waiting for VFS_FREEVFS() */
2703 		mig->mig_destructor_called = B_TRUE;
2704 		mutex_exit(&mig->mig_lock);
2705 		return;
2706 	}
2707 	nfs_mi_free_globals(mig);
2708 }
2709 
2710 /*
2711  * Add an NFS mount to the per-zone list of NFS mounts.
2712  */
2713 void
2714 nfs_mi_zonelist_add(mntinfo_t *mi)
2715 {
2716 	struct mi_globals *mig;
2717 
2718 	mig = zone_getspecific(mi_list_key, mi->mi_zone);
2719 	mutex_enter(&mig->mig_lock);
2720 	list_insert_head(&mig->mig_list, mi);
2721 	mutex_exit(&mig->mig_lock);
2722 }
2723 
2724 /*
2725  * Remove an NFS mount from the per-zone list of NFS mounts.
2726  */
2727 static void
2728 nfs_mi_zonelist_remove(mntinfo_t *mi)
2729 {
2730 	struct mi_globals *mig;
2731 
2732 	mig = zone_getspecific(mi_list_key, mi->mi_zone);
2733 	mutex_enter(&mig->mig_lock);
2734 	list_remove(&mig->mig_list, mi);
2735 	/*
2736 	 * We can be called asynchronously by VFS_FREEVFS() after the zone
2737 	 * shutdown/destroy callbacks have executed; if so, clean up the zone's
2738 	 * mi globals.
2739 	 */
2740 	if (list_head(&mig->mig_list) == NULL &&
2741 	    mig->mig_destructor_called == B_TRUE) {
2742 		nfs_mi_free_globals(mig);
2743 		return;
2744 	}
2745 	mutex_exit(&mig->mig_lock);
2746 }
2747 
2748 /*
2749  * NFS Client initialization routine.  This routine should only be called
2750  * once.  It performs the following tasks:
2751  *	- Initalize all global locks
2752  * 	- Call sub-initialization routines (localize access to variables)
2753  */
2754 int
2755 nfs_clntinit(void)
2756 {
2757 #ifdef DEBUG
2758 	static boolean_t nfs_clntup = B_FALSE;
2759 #endif
2760 	int error;
2761 
2762 #ifdef DEBUG
2763 	ASSERT(nfs_clntup == B_FALSE);
2764 #endif
2765 
2766 	error = nfs_subrinit();
2767 	if (error)
2768 		return (error);
2769 
2770 	error = nfs_vfsinit();
2771 	if (error) {
2772 		/*
2773 		 * Cleanup nfs_subrinit() work
2774 		 */
2775 		nfs_subrfini();
2776 		return (error);
2777 	}
2778 	zone_key_create(&mi_list_key, nfs_mi_init, nfs_mi_shutdown,
2779 	    nfs_mi_destroy);
2780 
2781 	nfs4_clnt_init();
2782 
2783 #ifdef DEBUG
2784 	nfs_clntup = B_TRUE;
2785 #endif
2786 
2787 	return (0);
2788 }
2789 
2790 /*
2791  * This routine is only called if the NFS Client has been initialized but
2792  * the module failed to be installed. This routine will cleanup the previously
2793  * allocated/initialized work.
2794  */
2795 void
2796 nfs_clntfini(void)
2797 {
2798 	(void) zone_key_delete(mi_list_key);
2799 	nfs_subrfini();
2800 	nfs_vfsfini();
2801 	nfs4_clnt_fini();
2802 }
2803 
2804 /*
2805  * nfs_lockrelease:
2806  *
2807  * Release any locks on the given vnode that are held by the current
2808  * process.
2809  */
2810 void
2811 nfs_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
2812 {
2813 	flock64_t ld;
2814 	struct shrlock shr;
2815 	char *buf;
2816 	int remote_lock_possible;
2817 	int ret;
2818 
2819 	ASSERT((uintptr_t)vp > KERNELBASE);
2820 
2821 	/*
2822 	 * Generate an explicit unlock operation for the entire file.  As a
2823 	 * partial optimization, only generate the unlock if there is a
2824 	 * lock registered for the file.  We could check whether this
2825 	 * particular process has any locks on the file, but that would
2826 	 * require the local locking code to provide yet another query
2827 	 * routine.  Note that no explicit synchronization is needed here.
2828 	 * At worst, flk_has_remote_locks() will return a false positive,
2829 	 * in which case the unlock call wastes time but doesn't harm
2830 	 * correctness.
2831 	 *
2832 	 * In addition, an unlock request is generated if the process
2833 	 * is listed as possibly having a lock on the file because the
2834 	 * server and client lock managers may have gotten out of sync.
2835 	 * N.B. It is important to make sure nfs_remove_locking_id() is
2836 	 * called here even if flk_has_remote_locks(vp) reports true.
2837 	 * If it is not called and there is an entry on the process id
2838 	 * list, that entry will never get removed.
2839 	 */
2840 	remote_lock_possible = nfs_remove_locking_id(vp, RLMPL_PID,
2841 	    (char *)&(ttoproc(curthread)->p_pid), NULL, NULL);
2842 	if (remote_lock_possible || flk_has_remote_locks(vp)) {
2843 		ld.l_type = F_UNLCK;	/* set to unlock entire file */
2844 		ld.l_whence = 0;	/* unlock from start of file */
2845 		ld.l_start = 0;
2846 		ld.l_len = 0;		/* do entire file */
2847 		ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, cr,
2848 		    NULL);
2849 
2850 		if (ret != 0) {
2851 			/*
2852 			 * If VOP_FRLOCK fails, make sure we unregister
2853 			 * local locks before we continue.
2854 			 */
2855 			ld.l_pid = ttoproc(curthread)->p_pid;
2856 			lm_register_lock_locally(vp, NULL, &ld, flag, offset);
2857 #ifdef DEBUG
2858 			nfs_perror(ret,
2859 			    "NFS lock release error on vp %p: %m.\n",
2860 			    (void *)vp, NULL);
2861 #endif
2862 		}
2863 
2864 		/*
2865 		 * The call to VOP_FRLOCK may put the pid back on the
2866 		 * list.  We need to remove it.
2867 		 */
2868 		(void) nfs_remove_locking_id(vp, RLMPL_PID,
2869 		    (char *)&(ttoproc(curthread)->p_pid), NULL, NULL);
2870 	}
2871 
2872 	/*
2873 	 * As long as the vp has a share matching our pid,
2874 	 * pluck it off and unshare it.  There are circumstances in
2875 	 * which the call to nfs_remove_locking_id() may put the
2876 	 * owner back on the list, in which case we simply do a
2877 	 * redundant and harmless unshare.
2878 	 */
2879 	buf = kmem_alloc(MAX_SHR_OWNER_LEN, KM_SLEEP);
2880 	while (nfs_remove_locking_id(vp, RLMPL_OWNER,
2881 	    (char *)NULL, buf, &shr.s_own_len)) {
2882 		shr.s_owner = buf;
2883 		shr.s_access = 0;
2884 		shr.s_deny = 0;
2885 		shr.s_sysid = 0;
2886 		shr.s_pid = curproc->p_pid;
2887 
2888 		ret = VOP_SHRLOCK(vp, F_UNSHARE, &shr, flag, cr, NULL);
2889 #ifdef DEBUG
2890 		if (ret != 0) {
2891 			nfs_perror(ret,
2892 			    "NFS share release error on vp %p: %m.\n",
2893 			    (void *)vp, NULL);
2894 		}
2895 #endif
2896 	}
2897 	kmem_free(buf, MAX_SHR_OWNER_LEN);
2898 }
2899 
2900 /*
2901  * nfs_lockcompletion:
2902  *
2903  * If the vnode has a lock that makes it unsafe to cache the file, mark it
2904  * as non cachable (set VNOCACHE bit).
2905  */
2906 
2907 void
2908 nfs_lockcompletion(vnode_t *vp, int cmd)
2909 {
2910 #ifdef DEBUG
2911 	rnode_t *rp = VTOR(vp);
2912 
2913 	ASSERT(nfs_rw_lock_held(&rp->r_lkserlock, RW_WRITER));
2914 #endif
2915 
2916 	if (cmd == F_SETLK || cmd == F_SETLKW) {
2917 		if (!lm_safemap(vp)) {
2918 			mutex_enter(&vp->v_lock);
2919 			vp->v_flag |= VNOCACHE;
2920 			mutex_exit(&vp->v_lock);
2921 		} else {
2922 			mutex_enter(&vp->v_lock);
2923 			vp->v_flag &= ~VNOCACHE;
2924 			mutex_exit(&vp->v_lock);
2925 		}
2926 	}
2927 	/*
2928 	 * The cached attributes of the file are stale after acquiring
2929 	 * the lock on the file. They were updated when the file was
2930 	 * opened, but not updated when the lock was acquired. Therefore the
2931 	 * cached attributes are invalidated after the lock is obtained.
2932 	 */
2933 	PURGE_ATTRCACHE(vp);
2934 }
2935 
2936 /*
2937  * The lock manager holds state making it possible for the client
2938  * and server to be out of sync.  For example, if the response from
2939  * the server granting a lock request is lost, the server will think
2940  * the lock is granted and the client will think the lock is lost.
2941  * The client can tell when it is not positive if it is in sync with
2942  * the server.
2943  *
2944  * To deal with this, a list of processes for which the client is
2945  * not sure if the server holds a lock is attached to the rnode.
2946  * When such a process closes the rnode, an unlock request is sent
2947  * to the server to unlock the entire file.
2948  *
2949  * The list is kept as a singularly linked NULL terminated list.
2950  * Because it is only added to under extreme error conditions, the
2951  * list shouldn't get very big.  DEBUG kernels print a message if
2952  * the list gets bigger than nfs_lmpl_high_water.  This is arbitrarily
2953  * choosen to be 8, but can be tuned at runtime.
2954  */
2955 #ifdef DEBUG
2956 /* int nfs_lmpl_high_water = 8; */
2957 int nfs_lmpl_high_water = 128;
2958 int nfs_cnt_add_locking_id = 0;
2959 int nfs_len_add_locking_id = 0;
2960 #endif /* DEBUG */
2961 
2962 /*
2963  * Record that the nfs lock manager server may be holding a lock on
2964  * a vnode for a process.
2965  *
2966  * Because the nfs lock manager server holds state, it is possible
2967  * for the server to get out of sync with the client.  This routine is called
2968  * from the client when it is no longer sure if the server is in sync
2969  * with the client.  nfs_lockrelease() will then notice this and send
2970  * an unlock request when the file is closed
2971  */
2972 void
2973 nfs_add_locking_id(vnode_t *vp, pid_t pid, int type, char *id, int len)
2974 {
2975 	rnode_t *rp;
2976 	lmpl_t *new;
2977 	lmpl_t *cur;
2978 	lmpl_t **lmplp;
2979 #ifdef DEBUG
2980 	int list_len = 1;
2981 #endif /* DEBUG */
2982 
2983 #ifdef DEBUG
2984 	++nfs_cnt_add_locking_id;
2985 #endif /* DEBUG */
2986 	/*
2987 	 * allocate new lmpl_t now so we don't sleep
2988 	 * later after grabbing mutexes
2989 	 */
2990 	ASSERT(len < MAX_SHR_OWNER_LEN);
2991 	new = kmem_alloc(sizeof (*new), KM_SLEEP);
2992 	new->lmpl_type = type;
2993 	new->lmpl_pid = pid;
2994 	new->lmpl_owner = kmem_alloc(len, KM_SLEEP);
2995 	bcopy(id, new->lmpl_owner, len);
2996 	new->lmpl_own_len = len;
2997 	new->lmpl_next = (lmpl_t *)NULL;
2998 #ifdef DEBUG
2999 	if (type == RLMPL_PID) {
3000 		ASSERT(len == sizeof (pid_t));
3001 		ASSERT(pid == *(pid_t *)new->lmpl_owner);
3002 	} else {
3003 		ASSERT(type == RLMPL_OWNER);
3004 	}
3005 #endif
3006 
3007 	rp = VTOR(vp);
3008 	mutex_enter(&rp->r_statelock);
3009 
3010 	/*
3011 	 * Add this id to the list for this rnode only if the
3012 	 * rnode is active and the id is not already there.
3013 	 */
3014 	ASSERT(rp->r_flags & RHASHED);
3015 	lmplp = &(rp->r_lmpl);
3016 	for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL; cur = cur->lmpl_next) {
3017 		if (cur->lmpl_pid == pid &&
3018 		    cur->lmpl_type == type &&
3019 		    cur->lmpl_own_len == len &&
3020 		    bcmp(cur->lmpl_owner, new->lmpl_owner, len) == 0) {
3021 			kmem_free(new->lmpl_owner, len);
3022 			kmem_free(new, sizeof (*new));
3023 			break;
3024 		}
3025 		lmplp = &cur->lmpl_next;
3026 #ifdef DEBUG
3027 		++list_len;
3028 #endif /* DEBUG */
3029 	}
3030 	if (cur == (lmpl_t *)NULL) {
3031 		*lmplp = new;
3032 #ifdef DEBUG
3033 		if (list_len > nfs_len_add_locking_id) {
3034 			nfs_len_add_locking_id = list_len;
3035 		}
3036 		if (list_len > nfs_lmpl_high_water) {
3037 			cmn_err(CE_WARN, "nfs_add_locking_id: long list "
3038 			    "vp=%p is %d", (void *)vp, list_len);
3039 		}
3040 #endif /* DEBUG */
3041 	}
3042 
3043 #ifdef DEBUG
3044 	if (share_debug) {
3045 		int nitems = 0;
3046 		int npids = 0;
3047 		int nowners = 0;
3048 
3049 		/*
3050 		 * Count the number of things left on r_lmpl after the remove.
3051 		 */
3052 		for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL;
3053 		    cur = cur->lmpl_next) {
3054 			nitems++;
3055 			if (cur->lmpl_type == RLMPL_PID) {
3056 				npids++;
3057 			} else if (cur->lmpl_type == RLMPL_OWNER) {
3058 				nowners++;
3059 			} else {
3060 				cmn_err(CE_PANIC, "nfs_add_locking_id: "
3061 				    "unrecognized lmpl_type %d",
3062 				    cur->lmpl_type);
3063 			}
3064 		}
3065 
3066 		cmn_err(CE_CONT, "nfs_add_locking_id(%s): %d PIDs + %d "
3067 		    "OWNs = %d items left on r_lmpl\n",
3068 		    (type == RLMPL_PID) ? "P" : "O", npids, nowners, nitems);
3069 	}
3070 #endif
3071 
3072 	mutex_exit(&rp->r_statelock);
3073 }
3074 
3075 /*
3076  * Remove an id from the lock manager id list.
3077  *
3078  * If the id is not in the list return 0.  If it was found and
3079  * removed, return 1.
3080  */
3081 static int
3082 nfs_remove_locking_id(vnode_t *vp, int type, char *id, char *rid, int *rlen)
3083 {
3084 	lmpl_t *cur;
3085 	lmpl_t **lmplp;
3086 	rnode_t *rp;
3087 	int rv = 0;
3088 
3089 	ASSERT(type == RLMPL_PID || type == RLMPL_OWNER);
3090 
3091 	rp = VTOR(vp);
3092 
3093 	mutex_enter(&rp->r_statelock);
3094 	ASSERT(rp->r_flags & RHASHED);
3095 	lmplp = &(rp->r_lmpl);
3096 
3097 	/*
3098 	 * Search through the list and remove the entry for this id
3099 	 * if it is there.  The special case id == NULL allows removal
3100 	 * of the first share on the r_lmpl list belonging to the
3101 	 * current process (if any), without regard to further details
3102 	 * of its identity.
3103 	 */
3104 	for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL; cur = cur->lmpl_next) {
3105 		if (cur->lmpl_type == type &&
3106 		    cur->lmpl_pid == curproc->p_pid &&
3107 		    (id == (char *)NULL ||
3108 		    bcmp(cur->lmpl_owner, id, cur->lmpl_own_len) == 0)) {
3109 			*lmplp = cur->lmpl_next;
3110 			ASSERT(cur->lmpl_own_len < MAX_SHR_OWNER_LEN);
3111 			if (rid != NULL) {
3112 				bcopy(cur->lmpl_owner, rid, cur->lmpl_own_len);
3113 				*rlen = cur->lmpl_own_len;
3114 			}
3115 			kmem_free(cur->lmpl_owner, cur->lmpl_own_len);
3116 			kmem_free(cur, sizeof (*cur));
3117 			rv = 1;
3118 			break;
3119 		}
3120 		lmplp = &cur->lmpl_next;
3121 	}
3122 
3123 #ifdef DEBUG
3124 	if (share_debug) {
3125 		int nitems = 0;
3126 		int npids = 0;
3127 		int nowners = 0;
3128 
3129 		/*
3130 		 * Count the number of things left on r_lmpl after the remove.
3131 		 */
3132 		for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL;
3133 		    cur = cur->lmpl_next) {
3134 			nitems++;
3135 			if (cur->lmpl_type == RLMPL_PID) {
3136 				npids++;
3137 			} else if (cur->lmpl_type == RLMPL_OWNER) {
3138 				nowners++;
3139 			} else {
3140 				cmn_err(CE_PANIC,
3141 				    "nrli: unrecognized lmpl_type %d",
3142 				    cur->lmpl_type);
3143 			}
3144 		}
3145 
3146 		cmn_err(CE_CONT,
3147 		"nrli(%s): %d PIDs + %d OWNs = %d items left on r_lmpl\n",
3148 		    (type == RLMPL_PID) ? "P" : "O",
3149 		    npids,
3150 		    nowners,
3151 		    nitems);
3152 	}
3153 #endif
3154 
3155 	mutex_exit(&rp->r_statelock);
3156 	return (rv);
3157 }
3158 
3159 void
3160 nfs_free_mi(mntinfo_t *mi)
3161 {
3162 	ASSERT(mi->mi_flags & MI_ASYNC_MGR_STOP);
3163 	ASSERT(mi->mi_manager_thread == NULL);
3164 	ASSERT(mi->mi_threads == 0);
3165 
3166 	/*
3167 	 * Remove the node from the global list before we start tearing it down.
3168 	 */
3169 	nfs_mi_zonelist_remove(mi);
3170 	if (mi->mi_klmconfig) {
3171 		lm_free_config(mi->mi_klmconfig);
3172 		kmem_free(mi->mi_klmconfig, sizeof (struct knetconfig));
3173 	}
3174 	mutex_destroy(&mi->mi_lock);
3175 	mutex_destroy(&mi->mi_remap_lock);
3176 	mutex_destroy(&mi->mi_async_lock);
3177 	cv_destroy(&mi->mi_failover_cv);
3178 	cv_destroy(&mi->mi_async_work_cv);
3179 	cv_destroy(&mi->mi_async_reqs_cv);
3180 	cv_destroy(&mi->mi_async_cv);
3181 	zone_rele(mi->mi_zone);
3182 	kmem_free(mi, sizeof (*mi));
3183 }
3184 
3185 static int
3186 mnt_kstat_update(kstat_t *ksp, int rw)
3187 {
3188 	mntinfo_t *mi;
3189 	struct mntinfo_kstat *mik;
3190 	vfs_t *vfsp;
3191 	int i;
3192 
3193 	/* this is a read-only kstat. Bail out on a write */
3194 	if (rw == KSTAT_WRITE)
3195 		return (EACCES);
3196 
3197 	/*
3198 	 * We don't want to wait here as kstat_chain_lock could be held by
3199 	 * dounmount(). dounmount() takes vfs_reflock before the chain lock
3200 	 * and thus could lead to a deadlock.
3201 	 */
3202 	vfsp = (struct vfs *)ksp->ks_private;
3203 
3204 
3205 	mi = VFTOMI(vfsp);
3206 
3207 	mik = (struct mntinfo_kstat *)ksp->ks_data;
3208 
3209 	(void) strcpy(mik->mik_proto, mi->mi_curr_serv->sv_knconf->knc_proto);
3210 	mik->mik_vers = (uint32_t)mi->mi_vers;
3211 	mik->mik_flags = mi->mi_flags;
3212 	mik->mik_secmod = mi->mi_curr_serv->sv_secdata->secmod;
3213 	mik->mik_curread = (uint32_t)mi->mi_curread;
3214 	mik->mik_curwrite = (uint32_t)mi->mi_curwrite;
3215 	mik->mik_retrans = mi->mi_retrans;
3216 	mik->mik_timeo = mi->mi_timeo;
3217 	mik->mik_acregmin = HR2SEC(mi->mi_acregmin);
3218 	mik->mik_acregmax = HR2SEC(mi->mi_acregmax);
3219 	mik->mik_acdirmin = HR2SEC(mi->mi_acdirmin);
3220 	mik->mik_acdirmax = HR2SEC(mi->mi_acdirmax);
3221 	for (i = 0; i < NFS_CALLTYPES + 1; i++) {
3222 		mik->mik_timers[i].srtt = (uint32_t)mi->mi_timers[i].rt_srtt;
3223 		mik->mik_timers[i].deviate =
3224 		    (uint32_t)mi->mi_timers[i].rt_deviate;
3225 		mik->mik_timers[i].rtxcur =
3226 		    (uint32_t)mi->mi_timers[i].rt_rtxcur;
3227 	}
3228 	mik->mik_noresponse = (uint32_t)mi->mi_noresponse;
3229 	mik->mik_failover = (uint32_t)mi->mi_failover;
3230 	mik->mik_remap = (uint32_t)mi->mi_remap;
3231 	(void) strcpy(mik->mik_curserver, mi->mi_curr_serv->sv_hostname);
3232 
3233 	return (0);
3234 }
3235 
3236 void
3237 nfs_mnt_kstat_init(struct vfs *vfsp)
3238 {
3239 	mntinfo_t *mi = VFTOMI(vfsp);
3240 
3241 	/*
3242 	 * Create the version specific kstats.
3243 	 *
3244 	 * PSARC 2001/697 Contract Private Interface
3245 	 * All nfs kstats are under SunMC contract
3246 	 * Please refer to the PSARC listed above and contact
3247 	 * SunMC before making any changes!
3248 	 *
3249 	 * Changes must be reviewed by Solaris File Sharing
3250 	 * Changes must be communicated to contract-2001-697@sun.com
3251 	 *
3252 	 */
3253 
3254 	mi->mi_io_kstats = kstat_create_zone("nfs", getminor(vfsp->vfs_dev),
3255 	    NULL, "nfs", KSTAT_TYPE_IO, 1, 0, mi->mi_zone->zone_id);
3256 	if (mi->mi_io_kstats) {
3257 		if (mi->mi_zone->zone_id != GLOBAL_ZONEID)
3258 			kstat_zone_add(mi->mi_io_kstats, GLOBAL_ZONEID);
3259 		mi->mi_io_kstats->ks_lock = &mi->mi_lock;
3260 		kstat_install(mi->mi_io_kstats);
3261 	}
3262 
3263 	if ((mi->mi_ro_kstats = kstat_create_zone("nfs",
3264 	    getminor(vfsp->vfs_dev), "mntinfo", "misc", KSTAT_TYPE_RAW,
3265 	    sizeof (struct mntinfo_kstat), 0, mi->mi_zone->zone_id)) != NULL) {
3266 		if (mi->mi_zone->zone_id != GLOBAL_ZONEID)
3267 			kstat_zone_add(mi->mi_ro_kstats, GLOBAL_ZONEID);
3268 		mi->mi_ro_kstats->ks_update = mnt_kstat_update;
3269 		mi->mi_ro_kstats->ks_private = (void *)vfsp;
3270 		kstat_install(mi->mi_ro_kstats);
3271 	}
3272 }
3273 
3274 nfs_delmapcall_t *
3275 nfs_init_delmapcall()
3276 {
3277 	nfs_delmapcall_t	*delmap_call;
3278 
3279 	delmap_call = kmem_alloc(sizeof (nfs_delmapcall_t), KM_SLEEP);
3280 	delmap_call->call_id = curthread;
3281 	delmap_call->error = 0;
3282 
3283 	return (delmap_call);
3284 }
3285 
3286 void
3287 nfs_free_delmapcall(nfs_delmapcall_t *delmap_call)
3288 {
3289 	kmem_free(delmap_call, sizeof (nfs_delmapcall_t));
3290 }
3291 
3292 /*
3293  * Searches for the current delmap caller (based on curthread) in the list of
3294  * callers.  If it is found, we remove it and free the delmap caller.
3295  * Returns:
3296  *	0 if the caller wasn't found
3297  *	1 if the caller was found, removed and freed.  *errp is set to what
3298  * 	the result of the delmap was.
3299  */
3300 int
3301 nfs_find_and_delete_delmapcall(rnode_t *rp, int *errp)
3302 {
3303 	nfs_delmapcall_t	*delmap_call;
3304 
3305 	/*
3306 	 * If the list doesn't exist yet, we create it and return
3307 	 * that the caller wasn't found.  No list = no callers.
3308 	 */
3309 	mutex_enter(&rp->r_statelock);
3310 	if (!(rp->r_flags & RDELMAPLIST)) {
3311 		/* The list does not exist */
3312 		list_create(&rp->r_indelmap, sizeof (nfs_delmapcall_t),
3313 		    offsetof(nfs_delmapcall_t, call_node));
3314 		rp->r_flags |= RDELMAPLIST;
3315 		mutex_exit(&rp->r_statelock);
3316 		return (0);
3317 	} else {
3318 		/* The list exists so search it */
3319 		for (delmap_call = list_head(&rp->r_indelmap);
3320 		    delmap_call != NULL;
3321 		    delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
3322 			if (delmap_call->call_id == curthread) {
3323 				/* current caller is in the list */
3324 				*errp = delmap_call->error;
3325 				list_remove(&rp->r_indelmap, delmap_call);
3326 				mutex_exit(&rp->r_statelock);
3327 				nfs_free_delmapcall(delmap_call);
3328 				return (1);
3329 			}
3330 		}
3331 	}
3332 	mutex_exit(&rp->r_statelock);
3333 	return (0);
3334 }
3335