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