xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_client.c (revision fd8a887e)
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 
829 	rp = VTOR(vp);
830 	mutex_enter(&rp->r_statelock);
831 	if (ATTRCACHE_VALID(vp)) {
832 		/*
833 		 * Cached attributes are valid
834 		 */
835 		*vap = rp->r_attr;
836 		mutex_exit(&rp->r_statelock);
837 		return (0);
838 	}
839 	mutex_exit(&rp->r_statelock);
840 	return (1);
841 }
842 
843 /*
844  * Get attributes over-the-wire and update attributes cache
845  * if no error occurred in the over-the-wire operation.
846  * Return 0 if successful, otherwise error.
847  */
848 int
849 nfs_getattr_otw(vnode_t *vp, struct vattr *vap, cred_t *cr)
850 {
851 	int error;
852 	struct nfsattrstat ns;
853 	int douprintf;
854 	mntinfo_t *mi;
855 	failinfo_t fi;
856 	hrtime_t t;
857 
858 	mi = VTOMI(vp);
859 	fi.vp = vp;
860 	fi.fhp = NULL;		/* no need to update, filehandle not copied */
861 	fi.copyproc = nfscopyfh;
862 	fi.lookupproc = nfslookup;
863 	fi.xattrdirproc = acl_getxattrdir2;
864 
865 	if (mi->mi_flags & MI_ACL) {
866 		error = acl_getattr2_otw(vp, vap, cr);
867 		if (mi->mi_flags & MI_ACL)
868 			return (error);
869 	}
870 
871 	douprintf = 1;
872 
873 	t = gethrtime();
874 
875 	error = rfs2call(mi, RFS_GETATTR, xdr_fhandle, (caddr_t)VTOFH(vp),
876 	    xdr_attrstat, (caddr_t)&ns, cr, &douprintf, &ns.ns_status, 0, &fi);
877 
878 	if (!error) {
879 		error = geterrno(ns.ns_status);
880 		if (!error)
881 			error = nfs_cache_fattr(vp, &ns.ns_attr, vap, t, cr);
882 		else {
883 			PURGE_STALE_FH(error, vp, cr);
884 		}
885 	}
886 
887 	return (error);
888 }
889 
890 /*
891  * Return either cached ot remote attributes. If get remote attr
892  * use them to check and invalidate caches, then cache the new attributes.
893  */
894 int
895 nfsgetattr(vnode_t *vp, struct vattr *vap, cred_t *cr)
896 {
897 	int error;
898 	rnode_t *rp;
899 
900 	/*
901 	 * If we've got cached attributes, we're done, otherwise go
902 	 * to the server to get attributes, which will update the cache
903 	 * in the process.
904 	 */
905 	error = nfs_getattr_cache(vp, vap);
906 	if (error)
907 		error = nfs_getattr_otw(vp, vap, cr);
908 
909 	/* Return the client's view of file size */
910 	rp = VTOR(vp);
911 	mutex_enter(&rp->r_statelock);
912 	vap->va_size = rp->r_size;
913 	mutex_exit(&rp->r_statelock);
914 
915 	return (error);
916 }
917 
918 /*
919  * Get attributes over-the-wire and update attributes cache
920  * if no error occurred in the over-the-wire operation.
921  * Return 0 if successful, otherwise error.
922  */
923 int
924 nfs3_getattr_otw(vnode_t *vp, struct vattr *vap, cred_t *cr)
925 {
926 	int error;
927 	GETATTR3args args;
928 	GETATTR3vres res;
929 	int douprintf;
930 	failinfo_t fi;
931 	hrtime_t t;
932 
933 	args.object = *VTOFH3(vp);
934 	fi.vp = vp;
935 	fi.fhp = (caddr_t)&args.object;
936 	fi.copyproc = nfs3copyfh;
937 	fi.lookupproc = nfs3lookup;
938 	fi.xattrdirproc = acl_getxattrdir3;
939 	res.fres.vp = vp;
940 	res.fres.vap = vap;
941 
942 	douprintf = 1;
943 
944 	t = gethrtime();
945 
946 	error = rfs3call(VTOMI(vp), NFSPROC3_GETATTR,
947 	    xdr_nfs_fh3, (caddr_t)&args,
948 	    xdr_GETATTR3vres, (caddr_t)&res, cr,
949 	    &douprintf, &res.status, 0, &fi);
950 
951 	if (error)
952 		return (error);
953 
954 	error = geterrno3(res.status);
955 	if (error) {
956 		PURGE_STALE_FH(error, vp, cr);
957 		return (error);
958 	}
959 
960 	/*
961 	 * Catch status codes that indicate fattr3 to vattr translation failure
962 	 */
963 	if (res.fres.status)
964 		return (res.fres.status);
965 
966 	nfs_attr_cache(vp, vap, t, cr);
967 	return (0);
968 }
969 
970 /*
971  * Return either cached or remote attributes. If get remote attr
972  * use them to check and invalidate caches, then cache the new attributes.
973  */
974 int
975 nfs3getattr(vnode_t *vp, struct vattr *vap, cred_t *cr)
976 {
977 	int error;
978 	rnode_t *rp;
979 
980 	/*
981 	 * If we've got cached attributes, we're done, otherwise go
982 	 * to the server to get attributes, which will update the cache
983 	 * in the process.
984 	 */
985 	error = nfs_getattr_cache(vp, vap);
986 	if (error)
987 		error = nfs3_getattr_otw(vp, vap, cr);
988 
989 	/* Return the client's view of file size */
990 	rp = VTOR(vp);
991 	mutex_enter(&rp->r_statelock);
992 	vap->va_size = rp->r_size;
993 	mutex_exit(&rp->r_statelock);
994 
995 	return (error);
996 }
997 
998 vtype_t nf_to_vt[] = {
999 	VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK
1000 };
1001 /*
1002  * Convert NFS Version 2 over the network attributes to the local
1003  * virtual attributes.  The mapping between the UID_NOBODY/GID_NOBODY
1004  * network representation and the local representation is done here.
1005  * Returns 0 for success, error if failed due to overflow.
1006  */
1007 int
1008 nattr_to_vattr(vnode_t *vp, struct nfsfattr *na, struct vattr *vap)
1009 {
1010 	/* overflow in time attributes? */
1011 #ifndef _LP64
1012 	if (!NFS2_FATTR_TIME_OK(na))
1013 		return (EOVERFLOW);
1014 #endif
1015 
1016 	if (na->na_type < NFNON || na->na_type > NFSOC)
1017 		vap->va_type = VBAD;
1018 	else
1019 		vap->va_type = nf_to_vt[na->na_type];
1020 	vap->va_mode = na->na_mode;
1021 	vap->va_uid = (na->na_uid == NFS_UID_NOBODY) ? UID_NOBODY : na->na_uid;
1022 	vap->va_gid = (na->na_gid == NFS_GID_NOBODY) ? GID_NOBODY : na->na_gid;
1023 	vap->va_fsid = vp->v_vfsp->vfs_dev;
1024 	vap->va_nodeid = na->na_nodeid;
1025 	vap->va_nlink = na->na_nlink;
1026 	vap->va_size = na->na_size;	/* keep for cache validation */
1027 	/*
1028 	 * nfs protocol defines times as unsigned so don't extend sign,
1029 	 * unless sysadmin set nfs_allow_preepoch_time.
1030 	 */
1031 	NFS_TIME_T_CONVERT(vap->va_atime.tv_sec, na->na_atime.tv_sec);
1032 	vap->va_atime.tv_nsec = (uint32_t)(na->na_atime.tv_usec * 1000);
1033 	NFS_TIME_T_CONVERT(vap->va_mtime.tv_sec, na->na_mtime.tv_sec);
1034 	vap->va_mtime.tv_nsec = (uint32_t)(na->na_mtime.tv_usec * 1000);
1035 	NFS_TIME_T_CONVERT(vap->va_ctime.tv_sec, na->na_ctime.tv_sec);
1036 	vap->va_ctime.tv_nsec = (uint32_t)(na->na_ctime.tv_usec * 1000);
1037 	/*
1038 	 * Shannon's law - uncompress the received dev_t
1039 	 * if the top half of is zero indicating a response
1040 	 * from an `older style' OS. Except for when it is a
1041 	 * `new style' OS sending the maj device of zero,
1042 	 * in which case the algorithm still works because the
1043 	 * fact that it is a new style server
1044 	 * is hidden by the minor device not being greater
1045 	 * than 255 (a requirement in this case).
1046 	 */
1047 	if ((na->na_rdev & 0xffff0000) == 0)
1048 		vap->va_rdev = nfsv2_expdev(na->na_rdev);
1049 	else
1050 		vap->va_rdev = expldev(na->na_rdev);
1051 
1052 	vap->va_nblocks = na->na_blocks;
1053 	switch (na->na_type) {
1054 	case NFBLK:
1055 		vap->va_blksize = DEV_BSIZE;
1056 		break;
1057 
1058 	case NFCHR:
1059 		vap->va_blksize = MAXBSIZE;
1060 		break;
1061 
1062 	case NFSOC:
1063 	default:
1064 		vap->va_blksize = na->na_blocksize;
1065 		break;
1066 	}
1067 	/*
1068 	 * This bit of ugliness is a hack to preserve the
1069 	 * over-the-wire protocols for named-pipe vnodes.
1070 	 * It remaps the special over-the-wire type to the
1071 	 * VFIFO type. (see note in nfs.h)
1072 	 */
1073 	if (NA_ISFIFO(na)) {
1074 		vap->va_type = VFIFO;
1075 		vap->va_mode = (vap->va_mode & ~S_IFMT) | S_IFIFO;
1076 		vap->va_rdev = 0;
1077 		vap->va_blksize = na->na_blocksize;
1078 	}
1079 	vap->va_seq = 0;
1080 	return (0);
1081 }
1082 
1083 /*
1084  * Convert NFS Version 3 over the network attributes to the local
1085  * virtual attributes.  The mapping between the UID_NOBODY/GID_NOBODY
1086  * network representation and the local representation is done here.
1087  */
1088 vtype_t nf3_to_vt[] = {
1089 	VBAD, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
1090 };
1091 
1092 int
1093 fattr3_to_vattr(vnode_t *vp, fattr3 *na, struct vattr *vap)
1094 {
1095 
1096 #ifndef _LP64
1097 	/* overflow in time attributes? */
1098 	if (!NFS3_FATTR_TIME_OK(na))
1099 		return (EOVERFLOW);
1100 #endif
1101 	if (!NFS3_SIZE_OK(na->size))
1102 		/* file too big */
1103 		return (EFBIG);
1104 
1105 	vap->va_mask = AT_ALL;
1106 
1107 	if (na->type < NF3REG || na->type > NF3FIFO)
1108 		vap->va_type = VBAD;
1109 	else
1110 		vap->va_type = nf3_to_vt[na->type];
1111 	vap->va_mode = na->mode;
1112 	vap->va_uid = (na->uid == NFS_UID_NOBODY) ? UID_NOBODY : (uid_t)na->uid;
1113 	vap->va_gid = (na->gid == NFS_GID_NOBODY) ? GID_NOBODY : (gid_t)na->gid;
1114 	vap->va_fsid = vp->v_vfsp->vfs_dev;
1115 	vap->va_nodeid = na->fileid;
1116 	vap->va_nlink = na->nlink;
1117 	vap->va_size = na->size;
1118 
1119 	/*
1120 	 * nfs protocol defines times as unsigned so don't extend sign,
1121 	 * unless sysadmin set nfs_allow_preepoch_time.
1122 	 */
1123 	NFS_TIME_T_CONVERT(vap->va_atime.tv_sec, na->atime.seconds);
1124 	vap->va_atime.tv_nsec = (uint32_t)na->atime.nseconds;
1125 	NFS_TIME_T_CONVERT(vap->va_mtime.tv_sec, na->mtime.seconds);
1126 	vap->va_mtime.tv_nsec = (uint32_t)na->mtime.nseconds;
1127 	NFS_TIME_T_CONVERT(vap->va_ctime.tv_sec, na->ctime.seconds);
1128 	vap->va_ctime.tv_nsec = (uint32_t)na->ctime.nseconds;
1129 
1130 	switch (na->type) {
1131 	case NF3BLK:
1132 		vap->va_rdev = makedevice(na->rdev.specdata1,
1133 		    na->rdev.specdata2);
1134 		vap->va_blksize = DEV_BSIZE;
1135 		vap->va_nblocks = 0;
1136 		break;
1137 	case NF3CHR:
1138 		vap->va_rdev = makedevice(na->rdev.specdata1,
1139 		    na->rdev.specdata2);
1140 		vap->va_blksize = MAXBSIZE;
1141 		vap->va_nblocks = 0;
1142 		break;
1143 	case NF3REG:
1144 	case NF3DIR:
1145 	case NF3LNK:
1146 		vap->va_rdev = 0;
1147 		vap->va_blksize = MAXBSIZE;
1148 		vap->va_nblocks = (u_longlong_t)
1149 		    ((na->used + (size3)DEV_BSIZE - (size3)1) /
1150 		    (size3)DEV_BSIZE);
1151 		break;
1152 	case NF3SOCK:
1153 	case NF3FIFO:
1154 	default:
1155 		vap->va_rdev = 0;
1156 		vap->va_blksize = MAXBSIZE;
1157 		vap->va_nblocks = 0;
1158 		break;
1159 	}
1160 	vap->va_seq = 0;
1161 	return (0);
1162 }
1163 
1164 /*
1165  * Asynchronous I/O parameters.  nfs_async_threads is the high-water mark
1166  * for the demand-based allocation of async threads per-mount.  The
1167  * nfs_async_timeout is the amount of time a thread will live after it
1168  * becomes idle, unless new I/O requests are received before the thread
1169  * dies.  See nfs_async_putpage and nfs_async_start.
1170  */
1171 
1172 int nfs_async_timeout = -1;	/* uninitialized */
1173 
1174 static void	nfs_async_start(struct vfs *);
1175 
1176 static void
1177 free_async_args(struct nfs_async_reqs *args)
1178 {
1179 	rnode_t *rp;
1180 
1181 	if (args->a_io != NFS_INACTIVE) {
1182 		rp = VTOR(args->a_vp);
1183 		mutex_enter(&rp->r_statelock);
1184 		rp->r_count--;
1185 		if (args->a_io == NFS_PUTAPAGE ||
1186 		    args->a_io == NFS_PAGEIO)
1187 			rp->r_awcount--;
1188 		cv_broadcast(&rp->r_cv);
1189 		mutex_exit(&rp->r_statelock);
1190 		VN_RELE(args->a_vp);
1191 	}
1192 	crfree(args->a_cred);
1193 	kmem_free(args, sizeof (*args));
1194 }
1195 
1196 /*
1197  * Cross-zone thread creation and NFS access is disallowed, yet fsflush() and
1198  * pageout(), running in the global zone, have legitimate reasons to do
1199  * VOP_PUTPAGE(B_ASYNC) on other zones' NFS mounts.  We avoid the problem by
1200  * use of a a per-mount "asynchronous requests manager thread" which is
1201  * signaled by the various asynchronous work routines when there is
1202  * asynchronous work to be done.  It is responsible for creating new
1203  * worker threads if necessary, and notifying existing worker threads
1204  * that there is work to be done.
1205  *
1206  * In other words, it will "take the specifications from the customers and
1207  * give them to the engineers."
1208  *
1209  * Worker threads die off of their own accord if they are no longer
1210  * needed.
1211  *
1212  * This thread is killed when the zone is going away or the filesystem
1213  * is being unmounted.
1214  */
1215 void
1216 nfs_async_manager(vfs_t *vfsp)
1217 {
1218 	callb_cpr_t cprinfo;
1219 	mntinfo_t *mi;
1220 	uint_t max_threads;
1221 
1222 	mi = VFTOMI(vfsp);
1223 
1224 	CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr,
1225 	    "nfs_async_manager");
1226 
1227 	mutex_enter(&mi->mi_async_lock);
1228 	/*
1229 	 * We want to stash the max number of threads that this mount was
1230 	 * allowed so we can use it later when the variable is set to zero as
1231 	 * part of the zone/mount going away.
1232 	 *
1233 	 * We want to be able to create at least one thread to handle
1234 	 * asynchronous inactive calls.
1235 	 */
1236 	max_threads = MAX(mi->mi_max_threads, 1);
1237 	mutex_enter(&mi->mi_lock);
1238 	/*
1239 	 * We don't want to wait for mi_max_threads to go to zero, since that
1240 	 * happens as part of a failed unmount, but this thread should only
1241 	 * exit when the mount/zone is really going away.
1242 	 *
1243 	 * Once MI_ASYNC_MGR_STOP is set, no more async operations will be
1244 	 * attempted: the various _async_*() functions know to do things
1245 	 * inline if mi_max_threads == 0.  Henceforth we just drain out the
1246 	 * outstanding requests.
1247 	 *
1248 	 * Note that we still create zthreads even if we notice the zone is
1249 	 * shutting down (MI_ASYNC_MGR_STOP is set); this may cause the zone
1250 	 * shutdown sequence to take slightly longer in some cases, but
1251 	 * doesn't violate the protocol, as all threads will exit as soon as
1252 	 * they're done processing the remaining requests.
1253 	 */
1254 	while (!(mi->mi_flags & MI_ASYNC_MGR_STOP) ||
1255 	    mi->mi_async_req_count > 0) {
1256 		mutex_exit(&mi->mi_lock);
1257 		while (mi->mi_async_req_count > 0) {
1258 			/*
1259 			 * Paranoia: If the mount started out having
1260 			 * (mi->mi_max_threads == 0), and the value was
1261 			 * later changed (via a debugger or somesuch),
1262 			 * we could be confused since we will think we
1263 			 * can't create any threads, and the calling
1264 			 * code (which looks at the current value of
1265 			 * mi->mi_max_threads, now non-zero) thinks we
1266 			 * can.
1267 			 *
1268 			 * So, because we're paranoid, we create threads
1269 			 * up to the maximum of the original and the
1270 			 * current value. This means that future
1271 			 * (debugger-induced) lowerings of
1272 			 * mi->mi_max_threads are ignored for our
1273 			 * purposes, but who told them they could change
1274 			 * random values on a live kernel anyhow?
1275 			 */
1276 			if (mi->mi_threads <
1277 			    MAX(mi->mi_max_threads, max_threads)) {
1278 				mi->mi_threads++;
1279 				mutex_exit(&mi->mi_async_lock);
1280 				VFS_HOLD(vfsp);	/* hold for new thread */
1281 				(void) zthread_create(NULL, 0, nfs_async_start,
1282 				    vfsp, 0, minclsyspri);
1283 				mutex_enter(&mi->mi_async_lock);
1284 			}
1285 			cv_signal(&mi->mi_async_work_cv);
1286 			ASSERT(mi->mi_async_req_count != 0);
1287 			mi->mi_async_req_count--;
1288 		}
1289 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
1290 		cv_wait(&mi->mi_async_reqs_cv, &mi->mi_async_lock);
1291 		CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock);
1292 		mutex_enter(&mi->mi_lock);
1293 	}
1294 	mutex_exit(&mi->mi_lock);
1295 	/*
1296 	 * Let everyone know we're done.
1297 	 */
1298 	mi->mi_manager_thread = NULL;
1299 	cv_broadcast(&mi->mi_async_cv);
1300 
1301 	/*
1302 	 * There is no explicit call to mutex_exit(&mi->mi_async_lock)
1303 	 * since CALLB_CPR_EXIT is actually responsible for releasing
1304 	 * 'mi_async_lock'.
1305 	 */
1306 	CALLB_CPR_EXIT(&cprinfo);
1307 	VFS_RELE(vfsp);	/* release thread's hold */
1308 	zthread_exit();
1309 }
1310 
1311 /*
1312  * Signal (and wait for) the async manager thread to clean up and go away.
1313  */
1314 void
1315 nfs_async_manager_stop(vfs_t *vfsp)
1316 {
1317 	mntinfo_t *mi = VFTOMI(vfsp);
1318 
1319 	mutex_enter(&mi->mi_async_lock);
1320 	mutex_enter(&mi->mi_lock);
1321 	mi->mi_flags |= MI_ASYNC_MGR_STOP;
1322 	mutex_exit(&mi->mi_lock);
1323 	cv_broadcast(&mi->mi_async_reqs_cv);
1324 	while (mi->mi_manager_thread != NULL)
1325 		cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
1326 	mutex_exit(&mi->mi_async_lock);
1327 }
1328 
1329 int
1330 nfs_async_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr,
1331 	struct seg *seg, cred_t *cr, void (*readahead)(vnode_t *,
1332 	u_offset_t, caddr_t, struct seg *, cred_t *))
1333 {
1334 	rnode_t *rp;
1335 	mntinfo_t *mi;
1336 	struct nfs_async_reqs *args;
1337 
1338 	rp = VTOR(vp);
1339 	ASSERT(rp->r_freef == NULL);
1340 
1341 	mi = VTOMI(vp);
1342 
1343 	/*
1344 	 * If addr falls in a different segment, don't bother doing readahead.
1345 	 */
1346 	if (addr >= seg->s_base + seg->s_size)
1347 		return (-1);
1348 
1349 	/*
1350 	 * If we can't allocate a request structure, punt on the readahead.
1351 	 */
1352 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1353 		return (-1);
1354 
1355 	/*
1356 	 * If a lock operation is pending, don't initiate any new
1357 	 * readaheads.  Otherwise, bump r_count to indicate the new
1358 	 * asynchronous I/O.
1359 	 */
1360 	if (!nfs_rw_tryenter(&rp->r_lkserlock, RW_READER)) {
1361 		kmem_free(args, sizeof (*args));
1362 		return (-1);
1363 	}
1364 	mutex_enter(&rp->r_statelock);
1365 	rp->r_count++;
1366 	mutex_exit(&rp->r_statelock);
1367 	nfs_rw_exit(&rp->r_lkserlock);
1368 
1369 	args->a_next = NULL;
1370 #ifdef DEBUG
1371 	args->a_queuer = curthread;
1372 #endif
1373 	VN_HOLD(vp);
1374 	args->a_vp = vp;
1375 	ASSERT(cr != NULL);
1376 	crhold(cr);
1377 	args->a_cred = cr;
1378 	args->a_io = NFS_READ_AHEAD;
1379 	args->a_nfs_readahead = readahead;
1380 	args->a_nfs_blkoff = blkoff;
1381 	args->a_nfs_seg = seg;
1382 	args->a_nfs_addr = addr;
1383 
1384 	mutex_enter(&mi->mi_async_lock);
1385 
1386 	/*
1387 	 * If asyncio has been disabled, don't bother readahead.
1388 	 */
1389 	if (mi->mi_max_threads == 0) {
1390 		mutex_exit(&mi->mi_async_lock);
1391 		goto noasync;
1392 	}
1393 
1394 	/*
1395 	 * Link request structure into the async list and
1396 	 * wakeup async thread to do the i/o.
1397 	 */
1398 	if (mi->mi_async_reqs[NFS_READ_AHEAD] == NULL) {
1399 		mi->mi_async_reqs[NFS_READ_AHEAD] = args;
1400 		mi->mi_async_tail[NFS_READ_AHEAD] = args;
1401 	} else {
1402 		mi->mi_async_tail[NFS_READ_AHEAD]->a_next = args;
1403 		mi->mi_async_tail[NFS_READ_AHEAD] = args;
1404 	}
1405 
1406 	if (mi->mi_io_kstats) {
1407 		mutex_enter(&mi->mi_lock);
1408 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1409 		mutex_exit(&mi->mi_lock);
1410 	}
1411 
1412 	mi->mi_async_req_count++;
1413 	ASSERT(mi->mi_async_req_count != 0);
1414 	cv_signal(&mi->mi_async_reqs_cv);
1415 	mutex_exit(&mi->mi_async_lock);
1416 	return (0);
1417 
1418 noasync:
1419 	mutex_enter(&rp->r_statelock);
1420 	rp->r_count--;
1421 	cv_broadcast(&rp->r_cv);
1422 	mutex_exit(&rp->r_statelock);
1423 	VN_RELE(vp);
1424 	crfree(cr);
1425 	kmem_free(args, sizeof (*args));
1426 	return (-1);
1427 }
1428 
1429 int
1430 nfs_async_putapage(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
1431 	int flags, cred_t *cr, int (*putapage)(vnode_t *, page_t *,
1432 	u_offset_t, size_t, int, cred_t *))
1433 {
1434 	rnode_t *rp;
1435 	mntinfo_t *mi;
1436 	struct nfs_async_reqs *args;
1437 
1438 	ASSERT(flags & B_ASYNC);
1439 	ASSERT(vp->v_vfsp != NULL);
1440 
1441 	rp = VTOR(vp);
1442 	ASSERT(rp->r_count > 0);
1443 
1444 	mi = VTOMI(vp);
1445 
1446 	/*
1447 	 * If we can't allocate a request structure, do the putpage
1448 	 * operation synchronously in this thread's context.
1449 	 */
1450 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1451 		goto noasync;
1452 
1453 	args->a_next = NULL;
1454 #ifdef DEBUG
1455 	args->a_queuer = curthread;
1456 #endif
1457 	VN_HOLD(vp);
1458 	args->a_vp = vp;
1459 	ASSERT(cr != NULL);
1460 	crhold(cr);
1461 	args->a_cred = cr;
1462 	args->a_io = NFS_PUTAPAGE;
1463 	args->a_nfs_putapage = putapage;
1464 	args->a_nfs_pp = pp;
1465 	args->a_nfs_off = off;
1466 	args->a_nfs_len = (uint_t)len;
1467 	args->a_nfs_flags = flags;
1468 
1469 	mutex_enter(&mi->mi_async_lock);
1470 
1471 	/*
1472 	 * If asyncio has been disabled, then make a synchronous request.
1473 	 * This check is done a second time in case async io was diabled
1474 	 * while this thread was blocked waiting for memory pressure to
1475 	 * reduce or for the queue to drain.
1476 	 */
1477 	if (mi->mi_max_threads == 0) {
1478 		mutex_exit(&mi->mi_async_lock);
1479 		goto noasync;
1480 	}
1481 
1482 	/*
1483 	 * Link request structure into the async list and
1484 	 * wakeup async thread to do the i/o.
1485 	 */
1486 	if (mi->mi_async_reqs[NFS_PUTAPAGE] == NULL) {
1487 		mi->mi_async_reqs[NFS_PUTAPAGE] = args;
1488 		mi->mi_async_tail[NFS_PUTAPAGE] = args;
1489 	} else {
1490 		mi->mi_async_tail[NFS_PUTAPAGE]->a_next = args;
1491 		mi->mi_async_tail[NFS_PUTAPAGE] = args;
1492 	}
1493 
1494 	mutex_enter(&rp->r_statelock);
1495 	rp->r_count++;
1496 	rp->r_awcount++;
1497 	mutex_exit(&rp->r_statelock);
1498 
1499 	if (mi->mi_io_kstats) {
1500 		mutex_enter(&mi->mi_lock);
1501 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1502 		mutex_exit(&mi->mi_lock);
1503 	}
1504 
1505 	mi->mi_async_req_count++;
1506 	ASSERT(mi->mi_async_req_count != 0);
1507 	cv_signal(&mi->mi_async_reqs_cv);
1508 	mutex_exit(&mi->mi_async_lock);
1509 	return (0);
1510 
1511 noasync:
1512 	if (args != NULL) {
1513 		VN_RELE(vp);
1514 		crfree(cr);
1515 		kmem_free(args, sizeof (*args));
1516 	}
1517 
1518 	if (curproc == proc_pageout || curproc == proc_fsflush) {
1519 		/*
1520 		 * If we get here in the context of the pageout/fsflush,
1521 		 * we refuse to do a sync write, because this may hang
1522 		 * pageout (and the machine). In this case, we just
1523 		 * re-mark the page as dirty and punt on the page.
1524 		 *
1525 		 * Make sure B_FORCE isn't set.  We can re-mark the
1526 		 * pages as dirty and unlock the pages in one swoop by
1527 		 * passing in B_ERROR to pvn_write_done().  However,
1528 		 * we should make sure B_FORCE isn't set - we don't
1529 		 * want the page tossed before it gets written out.
1530 		 */
1531 		if (flags & B_FORCE)
1532 			flags &= ~(B_INVAL | B_FORCE);
1533 		pvn_write_done(pp, flags | B_ERROR);
1534 		return (0);
1535 	}
1536 	if (nfs_zone() != mi->mi_zone) {
1537 		/*
1538 		 * So this was a cross-zone sync putpage.  We pass in B_ERROR
1539 		 * to pvn_write_done() to re-mark the pages as dirty and unlock
1540 		 * them.
1541 		 *
1542 		 * We don't want to clear B_FORCE here as the caller presumably
1543 		 * knows what they're doing if they set it.
1544 		 */
1545 		pvn_write_done(pp, flags | B_ERROR);
1546 		return (EPERM);
1547 	}
1548 	return ((*putapage)(vp, pp, off, len, flags, cr));
1549 }
1550 
1551 int
1552 nfs_async_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
1553 	int flags, cred_t *cr, int (*pageio)(vnode_t *, page_t *, u_offset_t,
1554 	size_t, int, cred_t *))
1555 {
1556 	rnode_t *rp;
1557 	mntinfo_t *mi;
1558 	struct nfs_async_reqs *args;
1559 
1560 	ASSERT(flags & B_ASYNC);
1561 	ASSERT(vp->v_vfsp != NULL);
1562 
1563 	rp = VTOR(vp);
1564 	ASSERT(rp->r_count > 0);
1565 
1566 	mi = VTOMI(vp);
1567 
1568 	/*
1569 	 * If we can't allocate a request structure, do the pageio
1570 	 * request synchronously in this thread's context.
1571 	 */
1572 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1573 		goto noasync;
1574 
1575 	args->a_next = NULL;
1576 #ifdef DEBUG
1577 	args->a_queuer = curthread;
1578 #endif
1579 	VN_HOLD(vp);
1580 	args->a_vp = vp;
1581 	ASSERT(cr != NULL);
1582 	crhold(cr);
1583 	args->a_cred = cr;
1584 	args->a_io = NFS_PAGEIO;
1585 	args->a_nfs_pageio = pageio;
1586 	args->a_nfs_pp = pp;
1587 	args->a_nfs_off = io_off;
1588 	args->a_nfs_len = (uint_t)io_len;
1589 	args->a_nfs_flags = flags;
1590 
1591 	mutex_enter(&mi->mi_async_lock);
1592 
1593 	/*
1594 	 * If asyncio has been disabled, then make a synchronous request.
1595 	 * This check is done a second time in case async io was diabled
1596 	 * while this thread was blocked waiting for memory pressure to
1597 	 * reduce or for the queue to drain.
1598 	 */
1599 	if (mi->mi_max_threads == 0) {
1600 		mutex_exit(&mi->mi_async_lock);
1601 		goto noasync;
1602 	}
1603 
1604 	/*
1605 	 * Link request structure into the async list and
1606 	 * wakeup async thread to do the i/o.
1607 	 */
1608 	if (mi->mi_async_reqs[NFS_PAGEIO] == NULL) {
1609 		mi->mi_async_reqs[NFS_PAGEIO] = args;
1610 		mi->mi_async_tail[NFS_PAGEIO] = args;
1611 	} else {
1612 		mi->mi_async_tail[NFS_PAGEIO]->a_next = args;
1613 		mi->mi_async_tail[NFS_PAGEIO] = args;
1614 	}
1615 
1616 	mutex_enter(&rp->r_statelock);
1617 	rp->r_count++;
1618 	rp->r_awcount++;
1619 	mutex_exit(&rp->r_statelock);
1620 
1621 	if (mi->mi_io_kstats) {
1622 		mutex_enter(&mi->mi_lock);
1623 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1624 		mutex_exit(&mi->mi_lock);
1625 	}
1626 
1627 	mi->mi_async_req_count++;
1628 	ASSERT(mi->mi_async_req_count != 0);
1629 	cv_signal(&mi->mi_async_reqs_cv);
1630 	mutex_exit(&mi->mi_async_lock);
1631 	return (0);
1632 
1633 noasync:
1634 	if (args != NULL) {
1635 		VN_RELE(vp);
1636 		crfree(cr);
1637 		kmem_free(args, sizeof (*args));
1638 	}
1639 
1640 	/*
1641 	 * If we can't do it ASYNC, for reads we do nothing (but cleanup
1642 	 * the page list), for writes we do it synchronously, except for
1643 	 * proc_pageout/proc_fsflush as described below.
1644 	 */
1645 	if (flags & B_READ) {
1646 		pvn_read_done(pp, flags | B_ERROR);
1647 		return (0);
1648 	}
1649 
1650 	if (curproc == proc_pageout || curproc == proc_fsflush) {
1651 		/*
1652 		 * If we get here in the context of the pageout/fsflush,
1653 		 * we refuse to do a sync write, because this may hang
1654 		 * pageout/fsflush (and the machine). In this case, we just
1655 		 * re-mark the page as dirty and punt on the page.
1656 		 *
1657 		 * Make sure B_FORCE isn't set.  We can re-mark the
1658 		 * pages as dirty and unlock the pages in one swoop by
1659 		 * passing in B_ERROR to pvn_write_done().  However,
1660 		 * we should make sure B_FORCE isn't set - we don't
1661 		 * want the page tossed before it gets written out.
1662 		 */
1663 		if (flags & B_FORCE)
1664 			flags &= ~(B_INVAL | B_FORCE);
1665 		pvn_write_done(pp, flags | B_ERROR);
1666 		return (0);
1667 	}
1668 
1669 	if (nfs_zone() != mi->mi_zone) {
1670 		/*
1671 		 * So this was a cross-zone sync pageio.  We pass in B_ERROR
1672 		 * to pvn_write_done() to re-mark the pages as dirty and unlock
1673 		 * them.
1674 		 *
1675 		 * We don't want to clear B_FORCE here as the caller presumably
1676 		 * knows what they're doing if they set it.
1677 		 */
1678 		pvn_write_done(pp, flags | B_ERROR);
1679 		return (EPERM);
1680 	}
1681 	return ((*pageio)(vp, pp, io_off, io_len, flags, cr));
1682 }
1683 
1684 void
1685 nfs_async_readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr,
1686 	int (*readdir)(vnode_t *, rddir_cache *, cred_t *))
1687 {
1688 	rnode_t *rp;
1689 	mntinfo_t *mi;
1690 	struct nfs_async_reqs *args;
1691 
1692 	rp = VTOR(vp);
1693 	ASSERT(rp->r_freef == NULL);
1694 
1695 	mi = VTOMI(vp);
1696 
1697 	/*
1698 	 * If we can't allocate a request structure, do the readdir
1699 	 * operation synchronously in this thread's context.
1700 	 */
1701 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1702 		goto noasync;
1703 
1704 	args->a_next = NULL;
1705 #ifdef DEBUG
1706 	args->a_queuer = curthread;
1707 #endif
1708 	VN_HOLD(vp);
1709 	args->a_vp = vp;
1710 	ASSERT(cr != NULL);
1711 	crhold(cr);
1712 	args->a_cred = cr;
1713 	args->a_io = NFS_READDIR;
1714 	args->a_nfs_readdir = readdir;
1715 	args->a_nfs_rdc = rdc;
1716 
1717 	mutex_enter(&mi->mi_async_lock);
1718 
1719 	/*
1720 	 * If asyncio has been disabled, then make a synchronous request.
1721 	 */
1722 	if (mi->mi_max_threads == 0) {
1723 		mutex_exit(&mi->mi_async_lock);
1724 		goto noasync;
1725 	}
1726 
1727 	/*
1728 	 * Link request structure into the async list and
1729 	 * wakeup async thread to do the i/o.
1730 	 */
1731 	if (mi->mi_async_reqs[NFS_READDIR] == NULL) {
1732 		mi->mi_async_reqs[NFS_READDIR] = args;
1733 		mi->mi_async_tail[NFS_READDIR] = args;
1734 	} else {
1735 		mi->mi_async_tail[NFS_READDIR]->a_next = args;
1736 		mi->mi_async_tail[NFS_READDIR] = args;
1737 	}
1738 
1739 	mutex_enter(&rp->r_statelock);
1740 	rp->r_count++;
1741 	mutex_exit(&rp->r_statelock);
1742 
1743 	if (mi->mi_io_kstats) {
1744 		mutex_enter(&mi->mi_lock);
1745 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1746 		mutex_exit(&mi->mi_lock);
1747 	}
1748 
1749 	mi->mi_async_req_count++;
1750 	ASSERT(mi->mi_async_req_count != 0);
1751 	cv_signal(&mi->mi_async_reqs_cv);
1752 	mutex_exit(&mi->mi_async_lock);
1753 	return;
1754 
1755 noasync:
1756 	if (args != NULL) {
1757 		VN_RELE(vp);
1758 		crfree(cr);
1759 		kmem_free(args, sizeof (*args));
1760 	}
1761 
1762 	rdc->entries = NULL;
1763 	mutex_enter(&rp->r_statelock);
1764 	ASSERT(rdc->flags & RDDIR);
1765 	rdc->flags &= ~RDDIR;
1766 	rdc->flags |= RDDIRREQ;
1767 	/*
1768 	 * Check the flag to see if RDDIRWAIT is set. If RDDIRWAIT
1769 	 * is set, wakeup the thread sleeping in cv_wait_sig().
1770 	 * The woken up thread will reset the flag to RDDIR and will
1771 	 * continue with the readdir opeartion.
1772 	 */
1773 	if (rdc->flags & RDDIRWAIT) {
1774 		rdc->flags &= ~RDDIRWAIT;
1775 		cv_broadcast(&rdc->cv);
1776 	}
1777 	mutex_exit(&rp->r_statelock);
1778 	rddir_cache_rele(rdc);
1779 }
1780 
1781 void
1782 nfs_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
1783 	cred_t *cr, void (*commit)(vnode_t *, page_t *, offset3, count3,
1784 	cred_t *))
1785 {
1786 	rnode_t *rp;
1787 	mntinfo_t *mi;
1788 	struct nfs_async_reqs *args;
1789 	page_t *pp;
1790 
1791 	rp = VTOR(vp);
1792 	mi = VTOMI(vp);
1793 
1794 	/*
1795 	 * If we can't allocate a request structure, do the commit
1796 	 * operation synchronously in this thread's context.
1797 	 */
1798 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
1799 		goto noasync;
1800 
1801 	args->a_next = NULL;
1802 #ifdef DEBUG
1803 	args->a_queuer = curthread;
1804 #endif
1805 	VN_HOLD(vp);
1806 	args->a_vp = vp;
1807 	ASSERT(cr != NULL);
1808 	crhold(cr);
1809 	args->a_cred = cr;
1810 	args->a_io = NFS_COMMIT;
1811 	args->a_nfs_commit = commit;
1812 	args->a_nfs_plist = plist;
1813 	args->a_nfs_offset = offset;
1814 	args->a_nfs_count = count;
1815 
1816 	mutex_enter(&mi->mi_async_lock);
1817 
1818 	/*
1819 	 * If asyncio has been disabled, then make a synchronous request.
1820 	 * This check is done a second time in case async io was diabled
1821 	 * while this thread was blocked waiting for memory pressure to
1822 	 * reduce or for the queue to drain.
1823 	 */
1824 	if (mi->mi_max_threads == 0) {
1825 		mutex_exit(&mi->mi_async_lock);
1826 		goto noasync;
1827 	}
1828 
1829 	/*
1830 	 * Link request structure into the async list and
1831 	 * wakeup async thread to do the i/o.
1832 	 */
1833 	if (mi->mi_async_reqs[NFS_COMMIT] == NULL) {
1834 		mi->mi_async_reqs[NFS_COMMIT] = args;
1835 		mi->mi_async_tail[NFS_COMMIT] = args;
1836 	} else {
1837 		mi->mi_async_tail[NFS_COMMIT]->a_next = args;
1838 		mi->mi_async_tail[NFS_COMMIT] = args;
1839 	}
1840 
1841 	mutex_enter(&rp->r_statelock);
1842 	rp->r_count++;
1843 	mutex_exit(&rp->r_statelock);
1844 
1845 	if (mi->mi_io_kstats) {
1846 		mutex_enter(&mi->mi_lock);
1847 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1848 		mutex_exit(&mi->mi_lock);
1849 	}
1850 
1851 	mi->mi_async_req_count++;
1852 	ASSERT(mi->mi_async_req_count != 0);
1853 	cv_signal(&mi->mi_async_reqs_cv);
1854 	mutex_exit(&mi->mi_async_lock);
1855 	return;
1856 
1857 noasync:
1858 	if (args != NULL) {
1859 		VN_RELE(vp);
1860 		crfree(cr);
1861 		kmem_free(args, sizeof (*args));
1862 	}
1863 
1864 	if (curproc == proc_pageout || curproc == proc_fsflush ||
1865 	    nfs_zone() != mi->mi_zone) {
1866 		while (plist != NULL) {
1867 			pp = plist;
1868 			page_sub(&plist, pp);
1869 			pp->p_fsdata = C_COMMIT;
1870 			page_unlock(pp);
1871 		}
1872 		return;
1873 	}
1874 	(*commit)(vp, plist, offset, count, cr);
1875 }
1876 
1877 void
1878 nfs_async_inactive(vnode_t *vp, cred_t *cr,
1879     void (*inactive)(vnode_t *, cred_t *, caller_context_t *))
1880 {
1881 	mntinfo_t *mi;
1882 	struct nfs_async_reqs *args;
1883 
1884 	mi = VTOMI(vp);
1885 
1886 	args = kmem_alloc(sizeof (*args), KM_SLEEP);
1887 	args->a_next = NULL;
1888 #ifdef DEBUG
1889 	args->a_queuer = curthread;
1890 #endif
1891 	args->a_vp = vp;
1892 	ASSERT(cr != NULL);
1893 	crhold(cr);
1894 	args->a_cred = cr;
1895 	args->a_io = NFS_INACTIVE;
1896 	args->a_nfs_inactive = inactive;
1897 
1898 	/*
1899 	 * Note that we don't check mi->mi_max_threads here, since we
1900 	 * *need* to get rid of this vnode regardless of whether someone
1901 	 * set nfs3_max_threads/nfs_max_threads to zero in /etc/system.
1902 	 *
1903 	 * The manager thread knows about this and is willing to create
1904 	 * at least one thread to accommodate us.
1905 	 */
1906 	mutex_enter(&mi->mi_async_lock);
1907 	if (mi->mi_manager_thread == NULL) {
1908 		rnode_t *rp = VTOR(vp);
1909 
1910 		mutex_exit(&mi->mi_async_lock);
1911 		crfree(cr);	/* drop our reference */
1912 		kmem_free(args, sizeof (*args));
1913 		/*
1914 		 * We can't do an over-the-wire call since we're in the wrong
1915 		 * zone, so we need to clean up state as best we can and then
1916 		 * throw away the vnode.
1917 		 */
1918 		mutex_enter(&rp->r_statelock);
1919 		if (rp->r_unldvp != NULL) {
1920 			vnode_t *unldvp;
1921 			char *unlname;
1922 			cred_t *unlcred;
1923 
1924 			unldvp = rp->r_unldvp;
1925 			rp->r_unldvp = NULL;
1926 			unlname = rp->r_unlname;
1927 			rp->r_unlname = NULL;
1928 			unlcred = rp->r_unlcred;
1929 			rp->r_unlcred = NULL;
1930 			mutex_exit(&rp->r_statelock);
1931 
1932 			VN_RELE(unldvp);
1933 			kmem_free(unlname, MAXNAMELEN);
1934 			crfree(unlcred);
1935 		} else {
1936 			mutex_exit(&rp->r_statelock);
1937 		}
1938 		/*
1939 		 * No need to explicitly throw away any cached pages.  The
1940 		 * eventual rinactive() will attempt a synchronous
1941 		 * VOP_PUTPAGE() which will immediately fail since the request
1942 		 * is coming from the wrong zone, and then will proceed to call
1943 		 * nfs_invalidate_pages() which will clean things up for us.
1944 		 */
1945 		rp_addfree(VTOR(vp), cr);
1946 		return;
1947 	}
1948 
1949 	if (mi->mi_async_reqs[NFS_INACTIVE] == NULL) {
1950 		mi->mi_async_reqs[NFS_INACTIVE] = args;
1951 	} else {
1952 		mi->mi_async_tail[NFS_INACTIVE]->a_next = args;
1953 	}
1954 	mi->mi_async_tail[NFS_INACTIVE] = args;
1955 	/*
1956 	 * Don't increment r_count, since we're trying to get rid of the vnode.
1957 	 */
1958 
1959 	mi->mi_async_req_count++;
1960 	ASSERT(mi->mi_async_req_count != 0);
1961 	cv_signal(&mi->mi_async_reqs_cv);
1962 	mutex_exit(&mi->mi_async_lock);
1963 }
1964 
1965 /*
1966  * The async queues for each mounted file system are arranged as a
1967  * set of queues, one for each async i/o type.  Requests are taken
1968  * from the queues in a round-robin fashion.  A number of consecutive
1969  * requests are taken from each queue before moving on to the next
1970  * queue.  This functionality may allow the NFS Version 2 server to do
1971  * write clustering, even if the client is mixing writes and reads
1972  * because it will take multiple write requests from the queue
1973  * before processing any of the other async i/o types.
1974  *
1975  * XXX The nfs_async_start thread is unsafe in the light of the present
1976  * model defined by cpr to suspend the system. Specifically over the
1977  * wire calls are cpr-unsafe. The thread should be reevaluated in
1978  * case of future updates to the cpr model.
1979  */
1980 static void
1981 nfs_async_start(struct vfs *vfsp)
1982 {
1983 	struct nfs_async_reqs *args;
1984 	mntinfo_t *mi = VFTOMI(vfsp);
1985 	clock_t time_left = 1;
1986 	callb_cpr_t cprinfo;
1987 	int i;
1988 
1989 	/*
1990 	 * Dynamic initialization of nfs_async_timeout to allow nfs to be
1991 	 * built in an implementation independent manner.
1992 	 */
1993 	if (nfs_async_timeout == -1)
1994 		nfs_async_timeout = NFS_ASYNC_TIMEOUT;
1995 
1996 	CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr, "nas");
1997 
1998 	mutex_enter(&mi->mi_async_lock);
1999 	for (;;) {
2000 		/*
2001 		 * Find the next queue containing an entry.  We start
2002 		 * at the current queue pointer and then round robin
2003 		 * through all of them until we either find a non-empty
2004 		 * queue or have looked through all of them.
2005 		 */
2006 		for (i = 0; i < NFS_ASYNC_TYPES; i++) {
2007 			args = *mi->mi_async_curr;
2008 			if (args != NULL)
2009 				break;
2010 			mi->mi_async_curr++;
2011 			if (mi->mi_async_curr ==
2012 			    &mi->mi_async_reqs[NFS_ASYNC_TYPES])
2013 				mi->mi_async_curr = &mi->mi_async_reqs[0];
2014 		}
2015 		/*
2016 		 * If we didn't find a entry, then block until woken up
2017 		 * again and then look through the queues again.
2018 		 */
2019 		if (args == NULL) {
2020 			/*
2021 			 * Exiting is considered to be safe for CPR as well
2022 			 */
2023 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
2024 
2025 			/*
2026 			 * Wakeup thread waiting to unmount the file
2027 			 * system only if all async threads are inactive.
2028 			 *
2029 			 * If we've timed-out and there's nothing to do,
2030 			 * then get rid of this thread.
2031 			 */
2032 			if (mi->mi_max_threads == 0 || time_left <= 0) {
2033 				if (--mi->mi_threads == 0)
2034 					cv_signal(&mi->mi_async_cv);
2035 				CALLB_CPR_EXIT(&cprinfo);
2036 				VFS_RELE(vfsp);	/* release thread's hold */
2037 				zthread_exit();
2038 				/* NOTREACHED */
2039 			}
2040 			time_left = cv_timedwait(&mi->mi_async_work_cv,
2041 			    &mi->mi_async_lock, nfs_async_timeout + lbolt);
2042 
2043 			CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock);
2044 
2045 			continue;
2046 		}
2047 		time_left = 1;
2048 
2049 		/*
2050 		 * Remove the request from the async queue and then
2051 		 * update the current async request queue pointer.  If
2052 		 * the current queue is empty or we have removed enough
2053 		 * consecutive entries from it, then reset the counter
2054 		 * for this queue and then move the current pointer to
2055 		 * the next queue.
2056 		 */
2057 		*mi->mi_async_curr = args->a_next;
2058 		if (*mi->mi_async_curr == NULL ||
2059 		    --mi->mi_async_clusters[args->a_io] == 0) {
2060 			mi->mi_async_clusters[args->a_io] =
2061 			    mi->mi_async_init_clusters;
2062 			mi->mi_async_curr++;
2063 			if (mi->mi_async_curr ==
2064 			    &mi->mi_async_reqs[NFS_ASYNC_TYPES])
2065 				mi->mi_async_curr = &mi->mi_async_reqs[0];
2066 		}
2067 
2068 		if (args->a_io != NFS_INACTIVE && mi->mi_io_kstats) {
2069 			mutex_enter(&mi->mi_lock);
2070 			kstat_waitq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
2071 			mutex_exit(&mi->mi_lock);
2072 		}
2073 
2074 		mutex_exit(&mi->mi_async_lock);
2075 
2076 		/*
2077 		 * Obtain arguments from the async request structure.
2078 		 */
2079 		if (args->a_io == NFS_READ_AHEAD && mi->mi_max_threads > 0) {
2080 			(*args->a_nfs_readahead)(args->a_vp, args->a_nfs_blkoff,
2081 			    args->a_nfs_addr, args->a_nfs_seg, args->a_cred);
2082 		} else if (args->a_io == NFS_PUTAPAGE) {
2083 			(void) (*args->a_nfs_putapage)(args->a_vp,
2084 			    args->a_nfs_pp, args->a_nfs_off, args->a_nfs_len,
2085 			    args->a_nfs_flags, args->a_cred);
2086 		} else if (args->a_io == NFS_PAGEIO) {
2087 			(void) (*args->a_nfs_pageio)(args->a_vp, args->a_nfs_pp,
2088 			    args->a_nfs_off, args->a_nfs_len, args->a_nfs_flags,
2089 			    args->a_cred);
2090 		} else if (args->a_io == NFS_READDIR) {
2091 			(void) ((*args->a_nfs_readdir)(args->a_vp,
2092 			    args->a_nfs_rdc, args->a_cred));
2093 		} else if (args->a_io == NFS_COMMIT) {
2094 			(*args->a_nfs_commit)(args->a_vp, args->a_nfs_plist,
2095 			    args->a_nfs_offset, args->a_nfs_count,
2096 			    args->a_cred);
2097 		} else if (args->a_io == NFS_INACTIVE) {
2098 			(*args->a_nfs_inactive)(args->a_vp, args->a_cred, NULL);
2099 		}
2100 
2101 		/*
2102 		 * Now, release the vnode and free the credentials
2103 		 * structure.
2104 		 */
2105 		free_async_args(args);
2106 		/*
2107 		 * Reacquire the mutex because it will be needed above.
2108 		 */
2109 		mutex_enter(&mi->mi_async_lock);
2110 	}
2111 }
2112 
2113 void
2114 nfs_async_stop(struct vfs *vfsp)
2115 {
2116 	mntinfo_t *mi = VFTOMI(vfsp);
2117 
2118 	/*
2119 	 * Wait for all outstanding async operations to complete and for the
2120 	 * worker threads to exit.
2121 	 */
2122 	mutex_enter(&mi->mi_async_lock);
2123 	mi->mi_max_threads = 0;
2124 	cv_broadcast(&mi->mi_async_work_cv);
2125 	while (mi->mi_threads != 0)
2126 		cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
2127 	mutex_exit(&mi->mi_async_lock);
2128 }
2129 
2130 /*
2131  * nfs_async_stop_sig:
2132  * Wait for all outstanding putpage operation to complete. If a signal
2133  * is deliver we will abort and return non-zero. If we can put all the
2134  * pages we will return 0. This routine is called from nfs_unmount and
2135  * nfs3_unmount to make these operations interruptible.
2136  */
2137 int
2138 nfs_async_stop_sig(struct vfs *vfsp)
2139 {
2140 	mntinfo_t *mi = VFTOMI(vfsp);
2141 	ushort_t omax;
2142 	int rval;
2143 
2144 	/*
2145 	 * Wait for all outstanding async operations to complete and for the
2146 	 * worker threads to exit.
2147 	 */
2148 	mutex_enter(&mi->mi_async_lock);
2149 	omax = mi->mi_max_threads;
2150 	mi->mi_max_threads = 0;
2151 	/*
2152 	 * Tell all the worker threads to exit.
2153 	 */
2154 	cv_broadcast(&mi->mi_async_work_cv);
2155 	while (mi->mi_threads != 0) {
2156 		if (!cv_wait_sig(&mi->mi_async_cv, &mi->mi_async_lock))
2157 			break;
2158 	}
2159 	rval = (mi->mi_threads != 0);	/* Interrupted */
2160 	if (rval)
2161 		mi->mi_max_threads = omax;
2162 	mutex_exit(&mi->mi_async_lock);
2163 
2164 	return (rval);
2165 }
2166 
2167 int
2168 writerp(rnode_t *rp, caddr_t base, int tcount, struct uio *uio, int pgcreated)
2169 {
2170 	int pagecreate;
2171 	int n;
2172 	int saved_n;
2173 	caddr_t saved_base;
2174 	u_offset_t offset;
2175 	int error;
2176 	int sm_error;
2177 	vnode_t *vp = RTOV(rp);
2178 
2179 	ASSERT(tcount <= MAXBSIZE && tcount <= uio->uio_resid);
2180 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_WRITER));
2181 	if (!vpm_enable) {
2182 		ASSERT(((uintptr_t)base & MAXBOFFSET) + tcount <= MAXBSIZE);
2183 	}
2184 
2185 	/*
2186 	 * Move bytes in at most PAGESIZE chunks. We must avoid
2187 	 * spanning pages in uiomove() because page faults may cause
2188 	 * the cache to be invalidated out from under us. The r_size is not
2189 	 * updated until after the uiomove. If we push the last page of a
2190 	 * file before r_size is correct, we will lose the data written past
2191 	 * the current (and invalid) r_size.
2192 	 */
2193 	do {
2194 		offset = uio->uio_loffset;
2195 		pagecreate = 0;
2196 
2197 		/*
2198 		 * n is the number of bytes required to satisfy the request
2199 		 *   or the number of bytes to fill out the page.
2200 		 */
2201 		n = (int)MIN((PAGESIZE - (offset & PAGEOFFSET)), tcount);
2202 
2203 		/*
2204 		 * Check to see if we can skip reading in the page
2205 		 * and just allocate the memory.  We can do this
2206 		 * if we are going to rewrite the entire mapping
2207 		 * or if we are going to write to or beyond the current
2208 		 * end of file from the beginning of the mapping.
2209 		 *
2210 		 * The read of r_size is now protected by r_statelock.
2211 		 */
2212 		mutex_enter(&rp->r_statelock);
2213 		/*
2214 		 * When pgcreated is nonzero the caller has already done
2215 		 * a segmap_getmapflt with forcefault 0 and S_WRITE. With
2216 		 * segkpm this means we already have at least one page
2217 		 * created and mapped at base.
2218 		 */
2219 		pagecreate = pgcreated ||
2220 		    ((offset & PAGEOFFSET) == 0 &&
2221 		    (n == PAGESIZE || ((offset + n) >= rp->r_size)));
2222 
2223 		mutex_exit(&rp->r_statelock);
2224 		if (!vpm_enable && pagecreate) {
2225 			/*
2226 			 * The last argument tells segmap_pagecreate() to
2227 			 * always lock the page, as opposed to sometimes
2228 			 * returning with the page locked. This way we avoid a
2229 			 * fault on the ensuing uiomove(), but also
2230 			 * more importantly (to fix bug 1094402) we can
2231 			 * call segmap_fault() to unlock the page in all
2232 			 * cases. An alternative would be to modify
2233 			 * segmap_pagecreate() to tell us when it is
2234 			 * locking a page, but that's a fairly major
2235 			 * interface change.
2236 			 */
2237 			if (pgcreated == 0)
2238 				(void) segmap_pagecreate(segkmap, base,
2239 				    (uint_t)n, 1);
2240 			saved_base = base;
2241 			saved_n = n;
2242 		}
2243 
2244 		/*
2245 		 * The number of bytes of data in the last page can not
2246 		 * be accurately be determined while page is being
2247 		 * uiomove'd to and the size of the file being updated.
2248 		 * Thus, inform threads which need to know accurately
2249 		 * how much data is in the last page of the file.  They
2250 		 * will not do the i/o immediately, but will arrange for
2251 		 * the i/o to happen later when this modify operation
2252 		 * will have finished.
2253 		 */
2254 		ASSERT(!(rp->r_flags & RMODINPROGRESS));
2255 		mutex_enter(&rp->r_statelock);
2256 		rp->r_flags |= RMODINPROGRESS;
2257 		rp->r_modaddr = (offset & MAXBMASK);
2258 		mutex_exit(&rp->r_statelock);
2259 
2260 		if (vpm_enable) {
2261 			/*
2262 			 * Copy data. If new pages are created, part of
2263 			 * the page that is not written will be initizliazed
2264 			 * with zeros.
2265 			 */
2266 			error = vpm_data_copy(vp, offset, n, uio, !pagecreate,
2267 			    NULL, 0, S_WRITE);
2268 		} else {
2269 			error = uiomove(base, n, UIO_WRITE, uio);
2270 		}
2271 
2272 		/*
2273 		 * r_size is the maximum number of
2274 		 * bytes known to be in the file.
2275 		 * Make sure it is at least as high as the
2276 		 * first unwritten byte pointed to by uio_loffset.
2277 		 */
2278 		mutex_enter(&rp->r_statelock);
2279 		if (rp->r_size < uio->uio_loffset)
2280 			rp->r_size = uio->uio_loffset;
2281 		rp->r_flags &= ~RMODINPROGRESS;
2282 		rp->r_flags |= RDIRTY;
2283 		mutex_exit(&rp->r_statelock);
2284 
2285 		/* n = # of bytes written */
2286 		n = (int)(uio->uio_loffset - offset);
2287 
2288 		if (!vpm_enable) {
2289 			base += n;
2290 		}
2291 		tcount -= n;
2292 		/*
2293 		 * If we created pages w/o initializing them completely,
2294 		 * we need to zero the part that wasn't set up.
2295 		 * This happens on a most EOF write cases and if
2296 		 * we had some sort of error during the uiomove.
2297 		 */
2298 		if (!vpm_enable && pagecreate) {
2299 			if ((uio->uio_loffset & PAGEOFFSET) || n == 0)
2300 				(void) kzero(base, PAGESIZE - n);
2301 
2302 			if (pgcreated) {
2303 				/*
2304 				 * Caller is responsible for this page,
2305 				 * it was not created in this loop.
2306 				 */
2307 				pgcreated = 0;
2308 			} else {
2309 				/*
2310 				 * For bug 1094402: segmap_pagecreate locks
2311 				 * page. Unlock it. This also unlocks the
2312 				 * pages allocated by page_create_va() in
2313 				 * segmap_pagecreate().
2314 				 */
2315 				sm_error = segmap_fault(kas.a_hat, segkmap,
2316 				    saved_base, saved_n, F_SOFTUNLOCK, S_WRITE);
2317 				if (error == 0)
2318 					error = sm_error;
2319 			}
2320 		}
2321 	} while (tcount > 0 && error == 0);
2322 
2323 	return (error);
2324 }
2325 
2326 int
2327 nfs_putpages(vnode_t *vp, u_offset_t off, size_t len, int flags, cred_t *cr)
2328 {
2329 	rnode_t *rp;
2330 	page_t *pp;
2331 	u_offset_t eoff;
2332 	u_offset_t io_off;
2333 	size_t io_len;
2334 	int error;
2335 	int rdirty;
2336 	int err;
2337 
2338 	rp = VTOR(vp);
2339 	ASSERT(rp->r_count > 0);
2340 
2341 	if (!vn_has_cached_data(vp))
2342 		return (0);
2343 
2344 	ASSERT(vp->v_type != VCHR);
2345 
2346 	/*
2347 	 * If ROUTOFSPACE is set, then all writes turn into B_INVAL
2348 	 * writes.  B_FORCE is set to force the VM system to actually
2349 	 * invalidate the pages, even if the i/o failed.  The pages
2350 	 * need to get invalidated because they can't be written out
2351 	 * because there isn't any space left on either the server's
2352 	 * file system or in the user's disk quota.  The B_FREE bit
2353 	 * is cleared to avoid confusion as to whether this is a
2354 	 * request to place the page on the freelist or to destroy
2355 	 * it.
2356 	 */
2357 	if ((rp->r_flags & ROUTOFSPACE) ||
2358 	    (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED))
2359 		flags = (flags & ~B_FREE) | B_INVAL | B_FORCE;
2360 
2361 	if (len == 0) {
2362 		/*
2363 		 * If doing a full file synchronous operation, then clear
2364 		 * the RDIRTY bit.  If a page gets dirtied while the flush
2365 		 * is happening, then RDIRTY will get set again.  The
2366 		 * RDIRTY bit must get cleared before the flush so that
2367 		 * we don't lose this information.
2368 		 *
2369 		 * If there are no full file async write operations
2370 		 * pending and RDIRTY bit is set, clear it.
2371 		 */
2372 		if (off == (u_offset_t)0 &&
2373 		    !(flags & B_ASYNC) &&
2374 		    (rp->r_flags & RDIRTY)) {
2375 			mutex_enter(&rp->r_statelock);
2376 			rdirty = (rp->r_flags & RDIRTY);
2377 			rp->r_flags &= ~RDIRTY;
2378 			mutex_exit(&rp->r_statelock);
2379 		} else if (flags & B_ASYNC && off == (u_offset_t)0) {
2380 			mutex_enter(&rp->r_statelock);
2381 			if (rp->r_flags & RDIRTY && rp->r_awcount == 0) {
2382 				rdirty = (rp->r_flags & RDIRTY);
2383 				rp->r_flags &= ~RDIRTY;
2384 			}
2385 			mutex_exit(&rp->r_statelock);
2386 		} else
2387 			rdirty = 0;
2388 
2389 		/*
2390 		 * Search the entire vp list for pages >= off, and flush
2391 		 * the dirty pages.
2392 		 */
2393 		error = pvn_vplist_dirty(vp, off, rp->r_putapage, flags, cr);
2394 
2395 		/*
2396 		 * If an error occurred and the file was marked as dirty
2397 		 * before and we aren't forcibly invalidating pages, then
2398 		 * reset the RDIRTY flag.
2399 		 */
2400 		if (error && rdirty &&
2401 		    (flags & (B_INVAL | B_FORCE)) != (B_INVAL | B_FORCE)) {
2402 			mutex_enter(&rp->r_statelock);
2403 			rp->r_flags |= RDIRTY;
2404 			mutex_exit(&rp->r_statelock);
2405 		}
2406 	} else {
2407 		/*
2408 		 * Do a range from [off...off + len) looking for pages
2409 		 * to deal with.
2410 		 */
2411 		error = 0;
2412 #ifdef lint
2413 		io_len = 0;
2414 #endif
2415 		eoff = off + len;
2416 		mutex_enter(&rp->r_statelock);
2417 		for (io_off = off; io_off < eoff && io_off < rp->r_size;
2418 		    io_off += io_len) {
2419 			mutex_exit(&rp->r_statelock);
2420 			/*
2421 			 * If we are not invalidating, synchronously
2422 			 * freeing or writing pages use the routine
2423 			 * page_lookup_nowait() to prevent reclaiming
2424 			 * them from the free list.
2425 			 */
2426 			if ((flags & B_INVAL) || !(flags & B_ASYNC)) {
2427 				pp = page_lookup(vp, io_off,
2428 				    (flags & (B_INVAL | B_FREE)) ?
2429 				    SE_EXCL : SE_SHARED);
2430 			} else {
2431 				pp = page_lookup_nowait(vp, io_off,
2432 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
2433 			}
2434 
2435 			if (pp == NULL || !pvn_getdirty(pp, flags))
2436 				io_len = PAGESIZE;
2437 			else {
2438 				err = (*rp->r_putapage)(vp, pp, &io_off,
2439 				    &io_len, flags, cr);
2440 				if (!error)
2441 					error = err;
2442 				/*
2443 				 * "io_off" and "io_len" are returned as
2444 				 * the range of pages we actually wrote.
2445 				 * This allows us to skip ahead more quickly
2446 				 * since several pages may've been dealt
2447 				 * with by this iteration of the loop.
2448 				 */
2449 			}
2450 			mutex_enter(&rp->r_statelock);
2451 		}
2452 		mutex_exit(&rp->r_statelock);
2453 	}
2454 
2455 	return (error);
2456 }
2457 
2458 void
2459 nfs_invalidate_pages(vnode_t *vp, u_offset_t off, cred_t *cr)
2460 {
2461 	rnode_t *rp;
2462 
2463 	rp = VTOR(vp);
2464 	mutex_enter(&rp->r_statelock);
2465 	while (rp->r_flags & RTRUNCATE)
2466 		cv_wait(&rp->r_cv, &rp->r_statelock);
2467 	rp->r_flags |= RTRUNCATE;
2468 	if (off == (u_offset_t)0) {
2469 		rp->r_flags &= ~RDIRTY;
2470 		if (!(rp->r_flags & RSTALE))
2471 			rp->r_error = 0;
2472 	}
2473 	rp->r_truncaddr = off;
2474 	mutex_exit(&rp->r_statelock);
2475 	(void) pvn_vplist_dirty(vp, off, rp->r_putapage,
2476 	    B_INVAL | B_TRUNC, cr);
2477 	mutex_enter(&rp->r_statelock);
2478 	rp->r_flags &= ~RTRUNCATE;
2479 	cv_broadcast(&rp->r_cv);
2480 	mutex_exit(&rp->r_statelock);
2481 }
2482 
2483 static int nfs_write_error_to_cons_only = 0;
2484 #define	MSG(x)	(nfs_write_error_to_cons_only ? (x) : (x) + 1)
2485 
2486 /*
2487  * Print a file handle
2488  */
2489 void
2490 nfs_printfhandle(nfs_fhandle *fhp)
2491 {
2492 	int *ip;
2493 	char *buf;
2494 	size_t bufsize;
2495 	char *cp;
2496 
2497 	/*
2498 	 * 13 == "(file handle:"
2499 	 * maximum of NFS_FHANDLE / sizeof (*ip) elements in fh_buf times
2500 	 *	1 == ' '
2501 	 *	8 == maximum strlen of "%x"
2502 	 * 3 == ")\n\0"
2503 	 */
2504 	bufsize = 13 + ((NFS_FHANDLE_LEN / sizeof (*ip)) * (1 + 8)) + 3;
2505 	buf = kmem_alloc(bufsize, KM_NOSLEEP);
2506 	if (buf == NULL)
2507 		return;
2508 
2509 	cp = buf;
2510 	(void) strcpy(cp, "(file handle:");
2511 	while (*cp != '\0')
2512 		cp++;
2513 	for (ip = (int *)fhp->fh_buf;
2514 	    ip < (int *)&fhp->fh_buf[fhp->fh_len];
2515 	    ip++) {
2516 		(void) sprintf(cp, " %x", *ip);
2517 		while (*cp != '\0')
2518 			cp++;
2519 	}
2520 	(void) strcpy(cp, ")\n");
2521 
2522 	zcmn_err(getzoneid(), CE_CONT, MSG("^%s"), buf);
2523 
2524 	kmem_free(buf, bufsize);
2525 }
2526 
2527 /*
2528  * Notify the system administrator that an NFS write error has
2529  * occurred.
2530  */
2531 
2532 /* seconds between ENOSPC/EDQUOT messages */
2533 clock_t nfs_write_error_interval = 5;
2534 
2535 void
2536 nfs_write_error(vnode_t *vp, int error, cred_t *cr)
2537 {
2538 	mntinfo_t *mi;
2539 
2540 	mi = VTOMI(vp);
2541 	/*
2542 	 * In case of forced unmount or zone shutdown, do not print any
2543 	 * messages since it can flood the console with error messages.
2544 	 */
2545 	if (FS_OR_ZONE_GONE(mi->mi_vfsp))
2546 		return;
2547 
2548 	/*
2549 	 * No use in flooding the console with ENOSPC
2550 	 * messages from the same file system.
2551 	 */
2552 	if ((error != ENOSPC && error != EDQUOT) ||
2553 	    lbolt - mi->mi_printftime > 0) {
2554 		zoneid_t zoneid = mi->mi_zone->zone_id;
2555 
2556 #ifdef DEBUG
2557 		nfs_perror(error, "NFS%ld write error on host %s: %m.\n",
2558 		    mi->mi_vers, VTOR(vp)->r_server->sv_hostname, NULL);
2559 #else
2560 		nfs_perror(error, "NFS write error on host %s: %m.\n",
2561 		    VTOR(vp)->r_server->sv_hostname, NULL);
2562 #endif
2563 		if (error == ENOSPC || error == EDQUOT) {
2564 			zcmn_err(zoneid, CE_CONT,
2565 			    MSG("^File: userid=%d, groupid=%d\n"),
2566 			    crgetuid(cr), crgetgid(cr));
2567 			if (crgetuid(CRED()) != crgetuid(cr) ||
2568 			    crgetgid(CRED()) != crgetgid(cr)) {
2569 				zcmn_err(zoneid, CE_CONT,
2570 				    MSG("^User: userid=%d, groupid=%d\n"),
2571 				    crgetuid(CRED()), crgetgid(CRED()));
2572 			}
2573 			mi->mi_printftime = lbolt +
2574 			    nfs_write_error_interval * hz;
2575 		}
2576 		nfs_printfhandle(&VTOR(vp)->r_fh);
2577 #ifdef DEBUG
2578 		if (error == EACCES) {
2579 			zcmn_err(zoneid, CE_CONT,
2580 			    MSG("^nfs_bio: cred is%s kcred\n"),
2581 			    cr == kcred ? "" : " not");
2582 		}
2583 #endif
2584 	}
2585 }
2586 
2587 /* ARGSUSED */
2588 static void *
2589 nfs_mi_init(zoneid_t zoneid)
2590 {
2591 	struct mi_globals *mig;
2592 
2593 	mig = kmem_alloc(sizeof (*mig), KM_SLEEP);
2594 	mutex_init(&mig->mig_lock, NULL, MUTEX_DEFAULT, NULL);
2595 	list_create(&mig->mig_list, sizeof (mntinfo_t),
2596 	    offsetof(mntinfo_t, mi_zone_node));
2597 	mig->mig_destructor_called = B_FALSE;
2598 	return (mig);
2599 }
2600 
2601 /*
2602  * Callback routine to tell all NFS mounts in the zone to stop creating new
2603  * threads.  Existing threads should exit.
2604  */
2605 /* ARGSUSED */
2606 static void
2607 nfs_mi_shutdown(zoneid_t zoneid, void *data)
2608 {
2609 	struct mi_globals *mig = data;
2610 	mntinfo_t *mi;
2611 
2612 	ASSERT(mig != NULL);
2613 again:
2614 	mutex_enter(&mig->mig_lock);
2615 	for (mi = list_head(&mig->mig_list); mi != NULL;
2616 	    mi = list_next(&mig->mig_list, mi)) {
2617 
2618 		/*
2619 		 * If we've done the shutdown work for this FS, skip.
2620 		 * Once we go off the end of the list, we're done.
2621 		 */
2622 		if (mi->mi_flags & MI_DEAD)
2623 			continue;
2624 
2625 		/*
2626 		 * We will do work, so not done.  Get a hold on the FS.
2627 		 */
2628 		VFS_HOLD(mi->mi_vfsp);
2629 
2630 		/*
2631 		 * purge the DNLC for this filesystem
2632 		 */
2633 		(void) dnlc_purge_vfsp(mi->mi_vfsp, 0);
2634 
2635 		mutex_enter(&mi->mi_async_lock);
2636 		/*
2637 		 * Tell existing async worker threads to exit.
2638 		 */
2639 		mi->mi_max_threads = 0;
2640 		cv_broadcast(&mi->mi_async_work_cv);
2641 		/*
2642 		 * Set MI_ASYNC_MGR_STOP so the async manager thread starts
2643 		 * getting ready to exit when it's done with its current work.
2644 		 * Also set MI_DEAD to note we've acted on this FS.
2645 		 */
2646 		mutex_enter(&mi->mi_lock);
2647 		mi->mi_flags |= (MI_ASYNC_MGR_STOP|MI_DEAD);
2648 		mutex_exit(&mi->mi_lock);
2649 		/*
2650 		 * Wake up the async manager thread.
2651 		 */
2652 		cv_broadcast(&mi->mi_async_reqs_cv);
2653 		mutex_exit(&mi->mi_async_lock);
2654 
2655 		/*
2656 		 * Drop lock and release FS, which may change list, then repeat.
2657 		 * We're done when every mi has been done or the list is empty.
2658 		 */
2659 		mutex_exit(&mig->mig_lock);
2660 		VFS_RELE(mi->mi_vfsp);
2661 		goto again;
2662 	}
2663 	mutex_exit(&mig->mig_lock);
2664 }
2665 
2666 static void
2667 nfs_mi_free_globals(struct mi_globals *mig)
2668 {
2669 	list_destroy(&mig->mig_list);	/* makes sure the list is empty */
2670 	mutex_destroy(&mig->mig_lock);
2671 	kmem_free(mig, sizeof (*mig));
2672 
2673 }
2674 
2675 /* ARGSUSED */
2676 static void
2677 nfs_mi_destroy(zoneid_t zoneid, void *data)
2678 {
2679 	struct mi_globals *mig = data;
2680 
2681 	ASSERT(mig != NULL);
2682 	mutex_enter(&mig->mig_lock);
2683 	if (list_head(&mig->mig_list) != NULL) {
2684 		/* Still waiting for VFS_FREEVFS() */
2685 		mig->mig_destructor_called = B_TRUE;
2686 		mutex_exit(&mig->mig_lock);
2687 		return;
2688 	}
2689 	nfs_mi_free_globals(mig);
2690 }
2691 
2692 /*
2693  * Add an NFS mount to the per-zone list of NFS mounts.
2694  */
2695 void
2696 nfs_mi_zonelist_add(mntinfo_t *mi)
2697 {
2698 	struct mi_globals *mig;
2699 
2700 	mig = zone_getspecific(mi_list_key, mi->mi_zone);
2701 	mutex_enter(&mig->mig_lock);
2702 	list_insert_head(&mig->mig_list, mi);
2703 	mutex_exit(&mig->mig_lock);
2704 }
2705 
2706 /*
2707  * Remove an NFS mount from the per-zone list of NFS mounts.
2708  */
2709 static void
2710 nfs_mi_zonelist_remove(mntinfo_t *mi)
2711 {
2712 	struct mi_globals *mig;
2713 
2714 	mig = zone_getspecific(mi_list_key, mi->mi_zone);
2715 	mutex_enter(&mig->mig_lock);
2716 	list_remove(&mig->mig_list, mi);
2717 	/*
2718 	 * We can be called asynchronously by VFS_FREEVFS() after the zone
2719 	 * shutdown/destroy callbacks have executed; if so, clean up the zone's
2720 	 * mi globals.
2721 	 */
2722 	if (list_head(&mig->mig_list) == NULL &&
2723 	    mig->mig_destructor_called == B_TRUE) {
2724 		nfs_mi_free_globals(mig);
2725 		return;
2726 	}
2727 	mutex_exit(&mig->mig_lock);
2728 }
2729 
2730 /*
2731  * NFS Client initialization routine.  This routine should only be called
2732  * once.  It performs the following tasks:
2733  *	- Initalize all global locks
2734  * 	- Call sub-initialization routines (localize access to variables)
2735  */
2736 int
2737 nfs_clntinit(void)
2738 {
2739 #ifdef DEBUG
2740 	static boolean_t nfs_clntup = B_FALSE;
2741 #endif
2742 	int error;
2743 
2744 #ifdef DEBUG
2745 	ASSERT(nfs_clntup == B_FALSE);
2746 #endif
2747 
2748 	error = nfs_subrinit();
2749 	if (error)
2750 		return (error);
2751 
2752 	error = nfs_vfsinit();
2753 	if (error) {
2754 		/*
2755 		 * Cleanup nfs_subrinit() work
2756 		 */
2757 		nfs_subrfini();
2758 		return (error);
2759 	}
2760 	zone_key_create(&mi_list_key, nfs_mi_init, nfs_mi_shutdown,
2761 	    nfs_mi_destroy);
2762 
2763 	nfs4_clnt_init();
2764 
2765 #ifdef DEBUG
2766 	nfs_clntup = B_TRUE;
2767 #endif
2768 
2769 	return (0);
2770 }
2771 
2772 /*
2773  * This routine is only called if the NFS Client has been initialized but
2774  * the module failed to be installed. This routine will cleanup the previously
2775  * allocated/initialized work.
2776  */
2777 void
2778 nfs_clntfini(void)
2779 {
2780 	(void) zone_key_delete(mi_list_key);
2781 	nfs_subrfini();
2782 	nfs_vfsfini();
2783 	nfs4_clnt_fini();
2784 }
2785 
2786 /*
2787  * nfs_lockrelease:
2788  *
2789  * Release any locks on the given vnode that are held by the current
2790  * process.
2791  */
2792 void
2793 nfs_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
2794 {
2795 	flock64_t ld;
2796 	struct shrlock shr;
2797 	char *buf;
2798 	int remote_lock_possible;
2799 	int ret;
2800 
2801 	ASSERT((uintptr_t)vp > KERNELBASE);
2802 
2803 	/*
2804 	 * Generate an explicit unlock operation for the entire file.  As a
2805 	 * partial optimization, only generate the unlock if there is a
2806 	 * lock registered for the file.  We could check whether this
2807 	 * particular process has any locks on the file, but that would
2808 	 * require the local locking code to provide yet another query
2809 	 * routine.  Note that no explicit synchronization is needed here.
2810 	 * At worst, flk_has_remote_locks() will return a false positive,
2811 	 * in which case the unlock call wastes time but doesn't harm
2812 	 * correctness.
2813 	 *
2814 	 * In addition, an unlock request is generated if the process
2815 	 * is listed as possibly having a lock on the file because the
2816 	 * server and client lock managers may have gotten out of sync.
2817 	 * N.B. It is important to make sure nfs_remove_locking_id() is
2818 	 * called here even if flk_has_remote_locks(vp) reports true.
2819 	 * If it is not called and there is an entry on the process id
2820 	 * list, that entry will never get removed.
2821 	 */
2822 	remote_lock_possible = nfs_remove_locking_id(vp, RLMPL_PID,
2823 	    (char *)&(ttoproc(curthread)->p_pid), NULL, NULL);
2824 	if (remote_lock_possible || flk_has_remote_locks(vp)) {
2825 		ld.l_type = F_UNLCK;	/* set to unlock entire file */
2826 		ld.l_whence = 0;	/* unlock from start of file */
2827 		ld.l_start = 0;
2828 		ld.l_len = 0;		/* do entire file */
2829 		ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, cr,
2830 		    NULL);
2831 
2832 		if (ret != 0) {
2833 			/*
2834 			 * If VOP_FRLOCK fails, make sure we unregister
2835 			 * local locks before we continue.
2836 			 */
2837 			ld.l_pid = ttoproc(curthread)->p_pid;
2838 			lm_register_lock_locally(vp, NULL, &ld, flag, offset);
2839 #ifdef DEBUG
2840 			nfs_perror(ret,
2841 			    "NFS lock release error on vp %p: %m.\n",
2842 			    (void *)vp, NULL);
2843 #endif
2844 		}
2845 
2846 		/*
2847 		 * The call to VOP_FRLOCK may put the pid back on the
2848 		 * list.  We need to remove it.
2849 		 */
2850 		(void) nfs_remove_locking_id(vp, RLMPL_PID,
2851 		    (char *)&(ttoproc(curthread)->p_pid), NULL, NULL);
2852 	}
2853 
2854 	/*
2855 	 * As long as the vp has a share matching our pid,
2856 	 * pluck it off and unshare it.  There are circumstances in
2857 	 * which the call to nfs_remove_locking_id() may put the
2858 	 * owner back on the list, in which case we simply do a
2859 	 * redundant and harmless unshare.
2860 	 */
2861 	buf = kmem_alloc(MAX_SHR_OWNER_LEN, KM_SLEEP);
2862 	while (nfs_remove_locking_id(vp, RLMPL_OWNER,
2863 	    (char *)NULL, buf, &shr.s_own_len)) {
2864 		shr.s_owner = buf;
2865 		shr.s_access = 0;
2866 		shr.s_deny = 0;
2867 		shr.s_sysid = 0;
2868 		shr.s_pid = curproc->p_pid;
2869 
2870 		ret = VOP_SHRLOCK(vp, F_UNSHARE, &shr, flag, cr, NULL);
2871 #ifdef DEBUG
2872 		if (ret != 0) {
2873 			nfs_perror(ret,
2874 			    "NFS share release error on vp %p: %m.\n",
2875 			    (void *)vp, NULL);
2876 		}
2877 #endif
2878 	}
2879 	kmem_free(buf, MAX_SHR_OWNER_LEN);
2880 }
2881 
2882 /*
2883  * nfs_lockcompletion:
2884  *
2885  * If the vnode has a lock that makes it unsafe to cache the file, mark it
2886  * as non cachable (set VNOCACHE bit).
2887  */
2888 
2889 void
2890 nfs_lockcompletion(vnode_t *vp, int cmd)
2891 {
2892 #ifdef DEBUG
2893 	rnode_t *rp = VTOR(vp);
2894 
2895 	ASSERT(nfs_rw_lock_held(&rp->r_lkserlock, RW_WRITER));
2896 #endif
2897 
2898 	if (cmd == F_SETLK || cmd == F_SETLKW) {
2899 		if (!lm_safemap(vp)) {
2900 			mutex_enter(&vp->v_lock);
2901 			vp->v_flag |= VNOCACHE;
2902 			mutex_exit(&vp->v_lock);
2903 		} else {
2904 			mutex_enter(&vp->v_lock);
2905 			vp->v_flag &= ~VNOCACHE;
2906 			mutex_exit(&vp->v_lock);
2907 		}
2908 	}
2909 	/*
2910 	 * The cached attributes of the file are stale after acquiring
2911 	 * the lock on the file. They were updated when the file was
2912 	 * opened, but not updated when the lock was acquired. Therefore the
2913 	 * cached attributes are invalidated after the lock is obtained.
2914 	 */
2915 	PURGE_ATTRCACHE(vp);
2916 }
2917 
2918 /*
2919  * The lock manager holds state making it possible for the client
2920  * and server to be out of sync.  For example, if the response from
2921  * the server granting a lock request is lost, the server will think
2922  * the lock is granted and the client will think the lock is lost.
2923  * The client can tell when it is not positive if it is in sync with
2924  * the server.
2925  *
2926  * To deal with this, a list of processes for which the client is
2927  * not sure if the server holds a lock is attached to the rnode.
2928  * When such a process closes the rnode, an unlock request is sent
2929  * to the server to unlock the entire file.
2930  *
2931  * The list is kept as a singularly linked NULL terminated list.
2932  * Because it is only added to under extreme error conditions, the
2933  * list shouldn't get very big.  DEBUG kernels print a message if
2934  * the list gets bigger than nfs_lmpl_high_water.  This is arbitrarily
2935  * choosen to be 8, but can be tuned at runtime.
2936  */
2937 #ifdef DEBUG
2938 /* int nfs_lmpl_high_water = 8; */
2939 int nfs_lmpl_high_water = 128;
2940 int nfs_cnt_add_locking_id = 0;
2941 int nfs_len_add_locking_id = 0;
2942 #endif /* DEBUG */
2943 
2944 /*
2945  * Record that the nfs lock manager server may be holding a lock on
2946  * a vnode for a process.
2947  *
2948  * Because the nfs lock manager server holds state, it is possible
2949  * for the server to get out of sync with the client.  This routine is called
2950  * from the client when it is no longer sure if the server is in sync
2951  * with the client.  nfs_lockrelease() will then notice this and send
2952  * an unlock request when the file is closed
2953  */
2954 void
2955 nfs_add_locking_id(vnode_t *vp, pid_t pid, int type, char *id, int len)
2956 {
2957 	rnode_t *rp;
2958 	lmpl_t *new;
2959 	lmpl_t *cur;
2960 	lmpl_t **lmplp;
2961 #ifdef DEBUG
2962 	int list_len = 1;
2963 #endif /* DEBUG */
2964 
2965 #ifdef DEBUG
2966 	++nfs_cnt_add_locking_id;
2967 #endif /* DEBUG */
2968 	/*
2969 	 * allocate new lmpl_t now so we don't sleep
2970 	 * later after grabbing mutexes
2971 	 */
2972 	ASSERT(len < MAX_SHR_OWNER_LEN);
2973 	new = kmem_alloc(sizeof (*new), KM_SLEEP);
2974 	new->lmpl_type = type;
2975 	new->lmpl_pid = pid;
2976 	new->lmpl_owner = kmem_alloc(len, KM_SLEEP);
2977 	bcopy(id, new->lmpl_owner, len);
2978 	new->lmpl_own_len = len;
2979 	new->lmpl_next = (lmpl_t *)NULL;
2980 #ifdef DEBUG
2981 	if (type == RLMPL_PID) {
2982 		ASSERT(len == sizeof (pid_t));
2983 		ASSERT(pid == *(pid_t *)new->lmpl_owner);
2984 	} else {
2985 		ASSERT(type == RLMPL_OWNER);
2986 	}
2987 #endif
2988 
2989 	rp = VTOR(vp);
2990 	mutex_enter(&rp->r_statelock);
2991 
2992 	/*
2993 	 * Add this id to the list for this rnode only if the
2994 	 * rnode is active and the id is not already there.
2995 	 */
2996 	ASSERT(rp->r_flags & RHASHED);
2997 	lmplp = &(rp->r_lmpl);
2998 	for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL; cur = cur->lmpl_next) {
2999 		if (cur->lmpl_pid == pid &&
3000 		    cur->lmpl_type == type &&
3001 		    cur->lmpl_own_len == len &&
3002 		    bcmp(cur->lmpl_owner, new->lmpl_owner, len) == 0) {
3003 			kmem_free(new->lmpl_owner, len);
3004 			kmem_free(new, sizeof (*new));
3005 			break;
3006 		}
3007 		lmplp = &cur->lmpl_next;
3008 #ifdef DEBUG
3009 		++list_len;
3010 #endif /* DEBUG */
3011 	}
3012 	if (cur == (lmpl_t *)NULL) {
3013 		*lmplp = new;
3014 #ifdef DEBUG
3015 		if (list_len > nfs_len_add_locking_id) {
3016 			nfs_len_add_locking_id = list_len;
3017 		}
3018 		if (list_len > nfs_lmpl_high_water) {
3019 			cmn_err(CE_WARN, "nfs_add_locking_id: long list "
3020 			    "vp=%p is %d", (void *)vp, list_len);
3021 		}
3022 #endif /* DEBUG */
3023 	}
3024 
3025 #ifdef DEBUG
3026 	if (share_debug) {
3027 		int nitems = 0;
3028 		int npids = 0;
3029 		int nowners = 0;
3030 
3031 		/*
3032 		 * Count the number of things left on r_lmpl after the remove.
3033 		 */
3034 		for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL;
3035 		    cur = cur->lmpl_next) {
3036 			nitems++;
3037 			if (cur->lmpl_type == RLMPL_PID) {
3038 				npids++;
3039 			} else if (cur->lmpl_type == RLMPL_OWNER) {
3040 				nowners++;
3041 			} else {
3042 				cmn_err(CE_PANIC, "nfs_add_locking_id: "
3043 				    "unrecognized lmpl_type %d",
3044 				    cur->lmpl_type);
3045 			}
3046 		}
3047 
3048 		cmn_err(CE_CONT, "nfs_add_locking_id(%s): %d PIDs + %d "
3049 		    "OWNs = %d items left on r_lmpl\n",
3050 		    (type == RLMPL_PID) ? "P" : "O", npids, nowners, nitems);
3051 	}
3052 #endif
3053 
3054 	mutex_exit(&rp->r_statelock);
3055 }
3056 
3057 /*
3058  * Remove an id from the lock manager id list.
3059  *
3060  * If the id is not in the list return 0.  If it was found and
3061  * removed, return 1.
3062  */
3063 static int
3064 nfs_remove_locking_id(vnode_t *vp, int type, char *id, char *rid, int *rlen)
3065 {
3066 	lmpl_t *cur;
3067 	lmpl_t **lmplp;
3068 	rnode_t *rp;
3069 	int rv = 0;
3070 
3071 	ASSERT(type == RLMPL_PID || type == RLMPL_OWNER);
3072 
3073 	rp = VTOR(vp);
3074 
3075 	mutex_enter(&rp->r_statelock);
3076 	ASSERT(rp->r_flags & RHASHED);
3077 	lmplp = &(rp->r_lmpl);
3078 
3079 	/*
3080 	 * Search through the list and remove the entry for this id
3081 	 * if it is there.  The special case id == NULL allows removal
3082 	 * of the first share on the r_lmpl list belonging to the
3083 	 * current process (if any), without regard to further details
3084 	 * of its identity.
3085 	 */
3086 	for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL; cur = cur->lmpl_next) {
3087 		if (cur->lmpl_type == type &&
3088 		    cur->lmpl_pid == curproc->p_pid &&
3089 		    (id == (char *)NULL ||
3090 		    bcmp(cur->lmpl_owner, id, cur->lmpl_own_len) == 0)) {
3091 			*lmplp = cur->lmpl_next;
3092 			ASSERT(cur->lmpl_own_len < MAX_SHR_OWNER_LEN);
3093 			if (rid != NULL) {
3094 				bcopy(cur->lmpl_owner, rid, cur->lmpl_own_len);
3095 				*rlen = cur->lmpl_own_len;
3096 			}
3097 			kmem_free(cur->lmpl_owner, cur->lmpl_own_len);
3098 			kmem_free(cur, sizeof (*cur));
3099 			rv = 1;
3100 			break;
3101 		}
3102 		lmplp = &cur->lmpl_next;
3103 	}
3104 
3105 #ifdef DEBUG
3106 	if (share_debug) {
3107 		int nitems = 0;
3108 		int npids = 0;
3109 		int nowners = 0;
3110 
3111 		/*
3112 		 * Count the number of things left on r_lmpl after the remove.
3113 		 */
3114 		for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL;
3115 		    cur = cur->lmpl_next) {
3116 			nitems++;
3117 			if (cur->lmpl_type == RLMPL_PID) {
3118 				npids++;
3119 			} else if (cur->lmpl_type == RLMPL_OWNER) {
3120 				nowners++;
3121 			} else {
3122 				cmn_err(CE_PANIC,
3123 				    "nrli: unrecognized lmpl_type %d",
3124 				    cur->lmpl_type);
3125 			}
3126 		}
3127 
3128 		cmn_err(CE_CONT,
3129 		    "nrli(%s): %d PIDs + %d OWNs = %d items left on r_lmpl\n",
3130 		    (type == RLMPL_PID) ? "P" : "O", npids, nowners, nitems);
3131 	}
3132 #endif
3133 
3134 	mutex_exit(&rp->r_statelock);
3135 	return (rv);
3136 }
3137 
3138 void
3139 nfs_free_mi(mntinfo_t *mi)
3140 {
3141 	ASSERT(mi->mi_flags & MI_ASYNC_MGR_STOP);
3142 	ASSERT(mi->mi_manager_thread == NULL);
3143 	ASSERT(mi->mi_threads == 0);
3144 
3145 	/*
3146 	 * Remove the node from the global list before we start tearing it down.
3147 	 */
3148 	nfs_mi_zonelist_remove(mi);
3149 	if (mi->mi_klmconfig) {
3150 		lm_free_config(mi->mi_klmconfig);
3151 		kmem_free(mi->mi_klmconfig, sizeof (struct knetconfig));
3152 	}
3153 	mutex_destroy(&mi->mi_lock);
3154 	mutex_destroy(&mi->mi_remap_lock);
3155 	mutex_destroy(&mi->mi_async_lock);
3156 	cv_destroy(&mi->mi_failover_cv);
3157 	cv_destroy(&mi->mi_async_work_cv);
3158 	cv_destroy(&mi->mi_async_reqs_cv);
3159 	cv_destroy(&mi->mi_async_cv);
3160 	zone_rele(mi->mi_zone);
3161 	kmem_free(mi, sizeof (*mi));
3162 }
3163 
3164 static int
3165 mnt_kstat_update(kstat_t *ksp, int rw)
3166 {
3167 	mntinfo_t *mi;
3168 	struct mntinfo_kstat *mik;
3169 	vfs_t *vfsp;
3170 	int i;
3171 
3172 	/* this is a read-only kstat. Bail out on a write */
3173 	if (rw == KSTAT_WRITE)
3174 		return (EACCES);
3175 
3176 	/*
3177 	 * We don't want to wait here as kstat_chain_lock could be held by
3178 	 * dounmount(). dounmount() takes vfs_reflock before the chain lock
3179 	 * and thus could lead to a deadlock.
3180 	 */
3181 	vfsp = (struct vfs *)ksp->ks_private;
3182 
3183 
3184 	mi = VFTOMI(vfsp);
3185 
3186 	mik = (struct mntinfo_kstat *)ksp->ks_data;
3187 
3188 	(void) strcpy(mik->mik_proto, mi->mi_curr_serv->sv_knconf->knc_proto);
3189 	mik->mik_vers = (uint32_t)mi->mi_vers;
3190 	mik->mik_flags = mi->mi_flags;
3191 	mik->mik_secmod = mi->mi_curr_serv->sv_secdata->secmod;
3192 	mik->mik_curread = (uint32_t)mi->mi_curread;
3193 	mik->mik_curwrite = (uint32_t)mi->mi_curwrite;
3194 	mik->mik_retrans = mi->mi_retrans;
3195 	mik->mik_timeo = mi->mi_timeo;
3196 	mik->mik_acregmin = HR2SEC(mi->mi_acregmin);
3197 	mik->mik_acregmax = HR2SEC(mi->mi_acregmax);
3198 	mik->mik_acdirmin = HR2SEC(mi->mi_acdirmin);
3199 	mik->mik_acdirmax = HR2SEC(mi->mi_acdirmax);
3200 	for (i = 0; i < NFS_CALLTYPES + 1; i++) {
3201 		mik->mik_timers[i].srtt = (uint32_t)mi->mi_timers[i].rt_srtt;
3202 		mik->mik_timers[i].deviate =
3203 		    (uint32_t)mi->mi_timers[i].rt_deviate;
3204 		mik->mik_timers[i].rtxcur =
3205 		    (uint32_t)mi->mi_timers[i].rt_rtxcur;
3206 	}
3207 	mik->mik_noresponse = (uint32_t)mi->mi_noresponse;
3208 	mik->mik_failover = (uint32_t)mi->mi_failover;
3209 	mik->mik_remap = (uint32_t)mi->mi_remap;
3210 	(void) strcpy(mik->mik_curserver, mi->mi_curr_serv->sv_hostname);
3211 
3212 	return (0);
3213 }
3214 
3215 void
3216 nfs_mnt_kstat_init(struct vfs *vfsp)
3217 {
3218 	mntinfo_t *mi = VFTOMI(vfsp);
3219 
3220 	/*
3221 	 * Create the version specific kstats.
3222 	 *
3223 	 * PSARC 2001/697 Contract Private Interface
3224 	 * All nfs kstats are under SunMC contract
3225 	 * Please refer to the PSARC listed above and contact
3226 	 * SunMC before making any changes!
3227 	 *
3228 	 * Changes must be reviewed by Solaris File Sharing
3229 	 * Changes must be communicated to contract-2001-697@sun.com
3230 	 *
3231 	 */
3232 
3233 	mi->mi_io_kstats = kstat_create_zone("nfs", getminor(vfsp->vfs_dev),
3234 	    NULL, "nfs", KSTAT_TYPE_IO, 1, 0, mi->mi_zone->zone_id);
3235 	if (mi->mi_io_kstats) {
3236 		if (mi->mi_zone->zone_id != GLOBAL_ZONEID)
3237 			kstat_zone_add(mi->mi_io_kstats, GLOBAL_ZONEID);
3238 		mi->mi_io_kstats->ks_lock = &mi->mi_lock;
3239 		kstat_install(mi->mi_io_kstats);
3240 	}
3241 
3242 	if ((mi->mi_ro_kstats = kstat_create_zone("nfs",
3243 	    getminor(vfsp->vfs_dev), "mntinfo", "misc", KSTAT_TYPE_RAW,
3244 	    sizeof (struct mntinfo_kstat), 0, mi->mi_zone->zone_id)) != NULL) {
3245 		if (mi->mi_zone->zone_id != GLOBAL_ZONEID)
3246 			kstat_zone_add(mi->mi_ro_kstats, GLOBAL_ZONEID);
3247 		mi->mi_ro_kstats->ks_update = mnt_kstat_update;
3248 		mi->mi_ro_kstats->ks_private = (void *)vfsp;
3249 		kstat_install(mi->mi_ro_kstats);
3250 	}
3251 }
3252 
3253 nfs_delmapcall_t *
3254 nfs_init_delmapcall()
3255 {
3256 	nfs_delmapcall_t	*delmap_call;
3257 
3258 	delmap_call = kmem_alloc(sizeof (nfs_delmapcall_t), KM_SLEEP);
3259 	delmap_call->call_id = curthread;
3260 	delmap_call->error = 0;
3261 
3262 	return (delmap_call);
3263 }
3264 
3265 void
3266 nfs_free_delmapcall(nfs_delmapcall_t *delmap_call)
3267 {
3268 	kmem_free(delmap_call, sizeof (nfs_delmapcall_t));
3269 }
3270 
3271 /*
3272  * Searches for the current delmap caller (based on curthread) in the list of
3273  * callers.  If it is found, we remove it and free the delmap caller.
3274  * Returns:
3275  *	0 if the caller wasn't found
3276  *	1 if the caller was found, removed and freed.  *errp is set to what
3277  * 	the result of the delmap was.
3278  */
3279 int
3280 nfs_find_and_delete_delmapcall(rnode_t *rp, int *errp)
3281 {
3282 	nfs_delmapcall_t	*delmap_call;
3283 
3284 	/*
3285 	 * If the list doesn't exist yet, we create it and return
3286 	 * that the caller wasn't found.  No list = no callers.
3287 	 */
3288 	mutex_enter(&rp->r_statelock);
3289 	if (!(rp->r_flags & RDELMAPLIST)) {
3290 		/* The list does not exist */
3291 		list_create(&rp->r_indelmap, sizeof (nfs_delmapcall_t),
3292 		    offsetof(nfs_delmapcall_t, call_node));
3293 		rp->r_flags |= RDELMAPLIST;
3294 		mutex_exit(&rp->r_statelock);
3295 		return (0);
3296 	} else {
3297 		/* The list exists so search it */
3298 		for (delmap_call = list_head(&rp->r_indelmap);
3299 		    delmap_call != NULL;
3300 		    delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
3301 			if (delmap_call->call_id == curthread) {
3302 				/* current caller is in the list */
3303 				*errp = delmap_call->error;
3304 				list_remove(&rp->r_indelmap, delmap_call);
3305 				mutex_exit(&rp->r_statelock);
3306 				nfs_free_delmapcall(delmap_call);
3307 				return (1);
3308 			}
3309 		}
3310 	}
3311 	mutex_exit(&rp->r_statelock);
3312 	return (0);
3313 }
3314