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