xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_client.c (revision 0dfe541e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55dae4443Sdh145677  * Common Development and Distribution License (the "License").
65dae4443Sdh145677  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*0dfe541eSEvan Layton 
227c478bd9Sstevel@tonic-gate /*
23a19609f8Sjv227347  * Copyright (c) 1986, 2010, Oracle and/or its affiliates. All rights reserved.
24*0dfe541eSEvan Layton  */
25*0dfe541eSEvan Layton 
26*0dfe541eSEvan Layton /*
277c478bd9Sstevel@tonic-gate  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
287c478bd9Sstevel@tonic-gate  *	All rights reserved.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
31*0dfe541eSEvan Layton /*
32*0dfe541eSEvan Layton  * Copyright 2018 Nexenta Systems, Inc.
33*0dfe541eSEvan Layton  */
34*0dfe541eSEvan Layton 
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <sys/thread.h>
397c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
407c478bd9Sstevel@tonic-gate #include <sys/time.h>
417c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
427c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
437c478bd9Sstevel@tonic-gate #include <sys/errno.h>
447c478bd9Sstevel@tonic-gate #include <sys/buf.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <sys/cred.h>
477c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
487c478bd9Sstevel@tonic-gate #include <sys/debug.h>
497c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
507c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
517c478bd9Sstevel@tonic-gate #include <sys/flock.h>
527c478bd9Sstevel@tonic-gate #include <sys/share.h>
537c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
547c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
557c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
567c478bd9Sstevel@tonic-gate #include <sys/callb.h>
577c478bd9Sstevel@tonic-gate #include <sys/acl.h>
587c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
597c478bd9Sstevel@tonic-gate #include <sys/signal.h>
607c478bd9Sstevel@tonic-gate #include <sys/list.h>
617c478bd9Sstevel@tonic-gate #include <sys/zone.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <rpc/types.h>
647c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
657c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
667c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
697c478bd9Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
70*0dfe541eSEvan Layton #include <nfs/nfs_cmd.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #include <nfs/rnode.h>
737c478bd9Sstevel@tonic-gate #include <nfs/nfs_acl.h>
747c478bd9Sstevel@tonic-gate #include <nfs/lm.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include <vm/hat.h>
777c478bd9Sstevel@tonic-gate #include <vm/as.h>
787c478bd9Sstevel@tonic-gate #include <vm/page.h>
797c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
807c478bd9Sstevel@tonic-gate #include <vm/seg.h>
817c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
827c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static void	nfs3_attr_cache(vnode_t *, vattr_t *, vattr_t *, hrtime_t,
857c478bd9Sstevel@tonic-gate 			cred_t *);
867c478bd9Sstevel@tonic-gate static int	nfs_getattr_cache(vnode_t *, struct vattr *);
877c478bd9Sstevel@tonic-gate static int	nfs_remove_locking_id(vnode_t *, int, char *, char *, int *);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate struct mi_globals {
907c478bd9Sstevel@tonic-gate 	kmutex_t	mig_lock;  /* lock protecting mig_list */
917c478bd9Sstevel@tonic-gate 	list_t		mig_list;  /* list of NFS v2 or v3 mounts in zone */
927c478bd9Sstevel@tonic-gate 	boolean_t	mig_destructor_called;
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static zone_key_t mi_list_key;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /* Debugging flag for PC file shares. */
987c478bd9Sstevel@tonic-gate extern int	share_debug;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * Attributes caching:
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * Attributes are cached in the rnode in struct vattr form.
1047c478bd9Sstevel@tonic-gate  * There is a time associated with the cached attributes (r_attrtime)
1057c478bd9Sstevel@tonic-gate  * which tells whether the attributes are valid. The time is initialized
1067c478bd9Sstevel@tonic-gate  * to the difference between current time and the modify time of the vnode
1077c478bd9Sstevel@tonic-gate  * when new attributes are cached. This allows the attributes for
1087c478bd9Sstevel@tonic-gate  * files that have changed recently to be timed out sooner than for files
1097c478bd9Sstevel@tonic-gate  * that have not changed for a long time. There are minimum and maximum
1107c478bd9Sstevel@tonic-gate  * timeout values that can be set per mount point.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate int
nfs_waitfor_purge_complete(vnode_t * vp)1147c478bd9Sstevel@tonic-gate nfs_waitfor_purge_complete(vnode_t *vp)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	rnode_t *rp;
1177c478bd9Sstevel@tonic-gate 	k_sigset_t smask;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
1207c478bd9Sstevel@tonic-gate 	if (rp->r_serial != NULL && rp->r_serial != curthread) {
1217c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
1227c478bd9Sstevel@tonic-gate 		sigintr(&smask, VTOMI(vp)->mi_flags & MI_INT);
1237c478bd9Sstevel@tonic-gate 		while (rp->r_serial != NULL) {
1247c478bd9Sstevel@tonic-gate 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
1257c478bd9Sstevel@tonic-gate 				sigunintr(&smask);
1267c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
1277c478bd9Sstevel@tonic-gate 				return (EINTR);
1287c478bd9Sstevel@tonic-gate 			}
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 		sigunintr(&smask);
1317c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	return (0);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Validate caches by checking cached attributes. If the cached
1387c478bd9Sstevel@tonic-gate  * attributes have timed out, then get new attributes from the server.
1397c478bd9Sstevel@tonic-gate  * As a side affect, this will do cache invalidation if the attributes
1407c478bd9Sstevel@tonic-gate  * have changed.
1417c478bd9Sstevel@tonic-gate  *
1427c478bd9Sstevel@tonic-gate  * If the attributes have not timed out and if there is a cache
1437c478bd9Sstevel@tonic-gate  * invalidation being done by some other thread, then wait until that
1447c478bd9Sstevel@tonic-gate  * thread has completed the cache invalidation.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate int
nfs_validate_caches(vnode_t * vp,cred_t * cr)1477c478bd9Sstevel@tonic-gate nfs_validate_caches(vnode_t *vp, cred_t *cr)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	int error;
1507c478bd9Sstevel@tonic-gate 	struct vattr va;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (ATTRCACHE_VALID(vp)) {
1537c478bd9Sstevel@tonic-gate 		error = nfs_waitfor_purge_complete(vp);
1547c478bd9Sstevel@tonic-gate 		if (error)
1557c478bd9Sstevel@tonic-gate 			return (error);
1567c478bd9Sstevel@tonic-gate 		return (0);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
1607c478bd9Sstevel@tonic-gate 	return (nfs_getattr_otw(vp, &va, cr));
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * Validate caches by checking cached attributes. If the cached
1657c478bd9Sstevel@tonic-gate  * attributes have timed out, then get new attributes from the server.
1667c478bd9Sstevel@tonic-gate  * As a side affect, this will do cache invalidation if the attributes
1677c478bd9Sstevel@tonic-gate  * have changed.
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  * If the attributes have not timed out and if there is a cache
1707c478bd9Sstevel@tonic-gate  * invalidation being done by some other thread, then wait until that
1717c478bd9Sstevel@tonic-gate  * thread has completed the cache invalidation.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate int
nfs3_validate_caches(vnode_t * vp,cred_t * cr)1747c478bd9Sstevel@tonic-gate nfs3_validate_caches(vnode_t *vp, cred_t *cr)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	int error;
1777c478bd9Sstevel@tonic-gate 	struct vattr va;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (ATTRCACHE_VALID(vp)) {
1807c478bd9Sstevel@tonic-gate 		error = nfs_waitfor_purge_complete(vp);
1817c478bd9Sstevel@tonic-gate 		if (error)
1827c478bd9Sstevel@tonic-gate 			return (error);
1837c478bd9Sstevel@tonic-gate 		return (0);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
1877c478bd9Sstevel@tonic-gate 	return (nfs3_getattr_otw(vp, &va, cr));
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * Purge all of the various NFS `data' caches.
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate void
nfs_purge_caches(vnode_t * vp,int purge_dnlc,cred_t * cr)1947c478bd9Sstevel@tonic-gate nfs_purge_caches(vnode_t *vp, int purge_dnlc, cred_t *cr)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	rnode_t *rp;
1977c478bd9Sstevel@tonic-gate 	char *contents;
1987c478bd9Sstevel@tonic-gate 	int size;
1997c478bd9Sstevel@tonic-gate 	int error;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * Purge the DNLC for any entries which refer to this file.
2037c478bd9Sstevel@tonic-gate 	 * Avoid recursive entry into dnlc_purge_vp() in case of a directory.
2047c478bd9Sstevel@tonic-gate 	 */
2057c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
2067c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
2077c478bd9Sstevel@tonic-gate 	if (vp->v_count > 1 &&
2087c478bd9Sstevel@tonic-gate 	    (vp->v_type == VDIR || purge_dnlc == NFS_PURGE_DNLC) &&
2097c478bd9Sstevel@tonic-gate 	    !(rp->r_flags & RINDNLCPURGE)) {
2107c478bd9Sstevel@tonic-gate 		/*
2117c478bd9Sstevel@tonic-gate 		 * Set the RINDNLCPURGE flag to prevent recursive entry
2127c478bd9Sstevel@tonic-gate 		 * into dnlc_purge_vp()
2137c478bd9Sstevel@tonic-gate 		 */
2147c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR)
2157c478bd9Sstevel@tonic-gate 			rp->r_flags |= RINDNLCPURGE;
2167c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
2177c478bd9Sstevel@tonic-gate 		dnlc_purge_vp(vp);
2187c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
2197c478bd9Sstevel@tonic-gate 		if (rp->r_flags & RINDNLCPURGE)
2207c478bd9Sstevel@tonic-gate 			rp->r_flags &= ~RINDNLCPURGE;
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * Clear any readdir state bits and purge the readlink response cache.
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	contents = rp->r_symlink.contents;
2277c478bd9Sstevel@tonic-gate 	size = rp->r_symlink.size;
2287c478bd9Sstevel@tonic-gate 	rp->r_symlink.contents = NULL;
2297c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (contents != NULL) {
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		kmem_free((void *)contents, size);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2377c478bd9Sstevel@tonic-gate 	 * Flush the page cache.
2387c478bd9Sstevel@tonic-gate 	 */
2397c478bd9Sstevel@tonic-gate 	if (vn_has_cached_data(vp)) {
240da6c28aaSamw 		error = VOP_PUTPAGE(vp, (u_offset_t)0, 0, B_INVAL, cr, NULL);
2417c478bd9Sstevel@tonic-gate 		if (error && (error == ENOSPC || error == EDQUOT)) {
2427c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
2437c478bd9Sstevel@tonic-gate 			if (!rp->r_error)
2447c478bd9Sstevel@tonic-gate 				rp->r_error = error;
2457c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * Flush the readdir response cache.
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 	if (HAVE_RDDIR_CACHE(rp))
2537c478bd9Sstevel@tonic-gate 		nfs_purge_rddir_cache(vp);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  * Purge the readdir cache of all entries
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate void
nfs_purge_rddir_cache(vnode_t * vp)2607c478bd9Sstevel@tonic-gate nfs_purge_rddir_cache(vnode_t *vp)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	rnode_t *rp;
2637c478bd9Sstevel@tonic-gate 	rddir_cache *rdc;
2647c478bd9Sstevel@tonic-gate 	rddir_cache *nrdc;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
2677c478bd9Sstevel@tonic-gate top:
2687c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
2697c478bd9Sstevel@tonic-gate 	rp->r_direof = NULL;
2707c478bd9Sstevel@tonic-gate 	rp->r_flags &= ~RLOOKUP;
2717c478bd9Sstevel@tonic-gate 	rp->r_flags |= RREADDIRPLUS;
2727c478bd9Sstevel@tonic-gate 	rdc = avl_first(&rp->r_dir);
2737c478bd9Sstevel@tonic-gate 	while (rdc != NULL) {
2747c478bd9Sstevel@tonic-gate 		nrdc = AVL_NEXT(&rp->r_dir, rdc);
2757c478bd9Sstevel@tonic-gate 		avl_remove(&rp->r_dir, rdc);
2767c478bd9Sstevel@tonic-gate 		rddir_cache_rele(rdc);
2777c478bd9Sstevel@tonic-gate 		rdc = nrdc;
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate  * Do a cache check based on the post-operation attributes.
2847c478bd9Sstevel@tonic-gate  * Then make them the new cached attributes.  If no attributes
2857c478bd9Sstevel@tonic-gate  * were returned, then mark the attributes as timed out.
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate void
nfs3_cache_post_op_attr(vnode_t * vp,post_op_attr * poap,hrtime_t t,cred_t * cr)2887c478bd9Sstevel@tonic-gate nfs3_cache_post_op_attr(vnode_t *vp, post_op_attr *poap, hrtime_t t, cred_t *cr)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	vattr_t attr;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if (!poap->attributes) {
2937c478bd9Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
2947c478bd9Sstevel@tonic-gate 		return;
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 	(void) nfs3_cache_fattr3(vp, &poap->attr, &attr, t, cr);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate  * Same as above, but using a vattr
3017c478bd9Sstevel@tonic-gate  */
3027c478bd9Sstevel@tonic-gate void
nfs3_cache_post_op_vattr(vnode_t * vp,post_op_vattr * poap,hrtime_t t,cred_t * cr)3037c478bd9Sstevel@tonic-gate nfs3_cache_post_op_vattr(vnode_t *vp, post_op_vattr *poap, hrtime_t t,
3047c478bd9Sstevel@tonic-gate     cred_t *cr)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	if (!poap->attributes) {
3077c478bd9Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
3087c478bd9Sstevel@tonic-gate 		return;
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 	nfs_attr_cache(vp, poap->fres.vap, t, cr);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate  * Do a cache check based on the weak cache consistency attributes.
3157c478bd9Sstevel@tonic-gate  * These consist of a small set of pre-operation attributes and the
3167c478bd9Sstevel@tonic-gate  * full set of post-operation attributes.
3177c478bd9Sstevel@tonic-gate  *
3187c478bd9Sstevel@tonic-gate  * If we are given the pre-operation attributes, then use them to
3197c478bd9Sstevel@tonic-gate  * check the validity of the various caches.  Then, if we got the
3207c478bd9Sstevel@tonic-gate  * post-operation attributes, make them the new cached attributes.
3217c478bd9Sstevel@tonic-gate  * If we didn't get the post-operation attributes, then mark the
3227c478bd9Sstevel@tonic-gate  * attribute cache as timed out so that the next reference will
3237c478bd9Sstevel@tonic-gate  * cause a GETATTR to the server to refresh with the current
3247c478bd9Sstevel@tonic-gate  * attributes.
3257c478bd9Sstevel@tonic-gate  *
3267c478bd9Sstevel@tonic-gate  * Otherwise, if we didn't get the pre-operation attributes, but
3277c478bd9Sstevel@tonic-gate  * we did get the post-operation attributes, then use these
3287c478bd9Sstevel@tonic-gate  * attributes to check the validity of the various caches.  This
3297c478bd9Sstevel@tonic-gate  * will probably cause a flush of the caches because if the
3307c478bd9Sstevel@tonic-gate  * operation succeeded, the attributes of the object were changed
3317c478bd9Sstevel@tonic-gate  * in some way from the old post-operation attributes.  This
3327c478bd9Sstevel@tonic-gate  * should be okay because it is the safe thing to do.  After
3337c478bd9Sstevel@tonic-gate  * checking the data caches, then we make these the new cached
3347c478bd9Sstevel@tonic-gate  * attributes.
3357c478bd9Sstevel@tonic-gate  *
3367c478bd9Sstevel@tonic-gate  * Otherwise, we didn't get either the pre- or post-operation
3377c478bd9Sstevel@tonic-gate  * attributes.  Simply mark the attribute cache as timed out so
3387c478bd9Sstevel@tonic-gate  * the next reference will cause a GETATTR to the server to
3397c478bd9Sstevel@tonic-gate  * refresh with the current attributes.
3407c478bd9Sstevel@tonic-gate  *
3417c478bd9Sstevel@tonic-gate  * If an error occurred trying to convert the over the wire
3427c478bd9Sstevel@tonic-gate  * attributes to a vattr, then simply mark the attribute cache as
3437c478bd9Sstevel@tonic-gate  * timed out.
3447c478bd9Sstevel@tonic-gate  */
3457c478bd9Sstevel@tonic-gate void
nfs3_cache_wcc_data(vnode_t * vp,wcc_data * wccp,hrtime_t t,cred_t * cr)3467c478bd9Sstevel@tonic-gate nfs3_cache_wcc_data(vnode_t *vp, wcc_data *wccp, hrtime_t t, cred_t *cr)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	vattr_t bva;
3497c478bd9Sstevel@tonic-gate 	vattr_t ava;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (wccp->after.attributes) {
3527c478bd9Sstevel@tonic-gate 		if (fattr3_to_vattr(vp, &wccp->after.attr, &ava)) {
3537c478bd9Sstevel@tonic-gate 			PURGE_ATTRCACHE(vp);
3547c478bd9Sstevel@tonic-gate 			return;
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 		if (wccp->before.attributes) {
3577c478bd9Sstevel@tonic-gate 			bva.va_ctime.tv_sec = wccp->before.attr.ctime.seconds;
3587c478bd9Sstevel@tonic-gate 			bva.va_ctime.tv_nsec = wccp->before.attr.ctime.nseconds;
3597c478bd9Sstevel@tonic-gate 			bva.va_mtime.tv_sec = wccp->before.attr.mtime.seconds;
3607c478bd9Sstevel@tonic-gate 			bva.va_mtime.tv_nsec = wccp->before.attr.mtime.nseconds;
3617c478bd9Sstevel@tonic-gate 			bva.va_size = wccp->before.attr.size;
3627c478bd9Sstevel@tonic-gate 			nfs3_attr_cache(vp, &bva, &ava, t, cr);
3637c478bd9Sstevel@tonic-gate 		} else
3647c478bd9Sstevel@tonic-gate 			nfs_attr_cache(vp, &ava, t, cr);
3657c478bd9Sstevel@tonic-gate 	} else {
3667c478bd9Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate  * Set attributes cache for given vnode using nfsattr.
3727c478bd9Sstevel@tonic-gate  *
3737c478bd9Sstevel@tonic-gate  * This routine does not do cache validation with the attributes.
3747c478bd9Sstevel@tonic-gate  *
3757c478bd9Sstevel@tonic-gate  * If an error occurred trying to convert the over the wire
3767c478bd9Sstevel@tonic-gate  * attributes to a vattr, then simply mark the attribute cache as
3777c478bd9Sstevel@tonic-gate  * timed out.
3787c478bd9Sstevel@tonic-gate  */
3797c478bd9Sstevel@tonic-gate void
nfs_attrcache(vnode_t * vp,struct nfsfattr * na,hrtime_t t)3807c478bd9Sstevel@tonic-gate nfs_attrcache(vnode_t *vp, struct nfsfattr *na, hrtime_t t)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	rnode_t *rp;
3837c478bd9Sstevel@tonic-gate 	struct vattr va;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (!nattr_to_vattr(vp, na, &va)) {
3867c478bd9Sstevel@tonic-gate 		rp = VTOR(vp);
3877c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
3887c478bd9Sstevel@tonic-gate 		if (rp->r_mtime <= t)
3897c478bd9Sstevel@tonic-gate 			nfs_attrcache_va(vp, &va);
3907c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
3917c478bd9Sstevel@tonic-gate 	} else {
3927c478bd9Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * Set attributes cache for given vnode using fattr3.
3987c478bd9Sstevel@tonic-gate  *
3997c478bd9Sstevel@tonic-gate  * This routine does not do cache validation with the attributes.
4007c478bd9Sstevel@tonic-gate  *
4017c478bd9Sstevel@tonic-gate  * If an error occurred trying to convert the over the wire
4027c478bd9Sstevel@tonic-gate  * attributes to a vattr, then simply mark the attribute cache as
4037c478bd9Sstevel@tonic-gate  * timed out.
4047c478bd9Sstevel@tonic-gate  */
4057c478bd9Sstevel@tonic-gate void
nfs3_attrcache(vnode_t * vp,fattr3 * na,hrtime_t t)4067c478bd9Sstevel@tonic-gate nfs3_attrcache(vnode_t *vp, fattr3 *na, hrtime_t t)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	rnode_t *rp;
4097c478bd9Sstevel@tonic-gate 	struct vattr va;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (!fattr3_to_vattr(vp, na, &va)) {
4127c478bd9Sstevel@tonic-gate 		rp = VTOR(vp);
4137c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
4147c478bd9Sstevel@tonic-gate 		if (rp->r_mtime <= t)
4157c478bd9Sstevel@tonic-gate 			nfs_attrcache_va(vp, &va);
4167c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
4177c478bd9Sstevel@tonic-gate 	} else {
4187c478bd9Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate  * Do a cache check based on attributes returned over the wire.  The
4247c478bd9Sstevel@tonic-gate  * new attributes are cached.
4257c478bd9Sstevel@tonic-gate  *
4267c478bd9Sstevel@tonic-gate  * If an error occurred trying to convert the over the wire attributes
4277c478bd9Sstevel@tonic-gate  * to a vattr, then just return that error.
4287c478bd9Sstevel@tonic-gate  *
4297c478bd9Sstevel@tonic-gate  * As a side affect, the vattr argument is filled in with the converted
4307c478bd9Sstevel@tonic-gate  * attributes.
4317c478bd9Sstevel@tonic-gate  */
4327c478bd9Sstevel@tonic-gate int
nfs_cache_fattr(vnode_t * vp,struct nfsfattr * na,vattr_t * vap,hrtime_t t,cred_t * cr)4337c478bd9Sstevel@tonic-gate nfs_cache_fattr(vnode_t *vp, struct nfsfattr *na, vattr_t *vap, hrtime_t t,
4347c478bd9Sstevel@tonic-gate     cred_t *cr)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	int error;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	error = nattr_to_vattr(vp, na, vap);
4397c478bd9Sstevel@tonic-gate 	if (error)
4407c478bd9Sstevel@tonic-gate 		return (error);
4417c478bd9Sstevel@tonic-gate 	nfs_attr_cache(vp, vap, t, cr);
4427c478bd9Sstevel@tonic-gate 	return (0);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate /*
4467c478bd9Sstevel@tonic-gate  * Do a cache check based on attributes returned over the wire.  The
4477c478bd9Sstevel@tonic-gate  * new attributes are cached.
4487c478bd9Sstevel@tonic-gate  *
4497c478bd9Sstevel@tonic-gate  * If an error occurred trying to convert the over the wire attributes
4507c478bd9Sstevel@tonic-gate  * to a vattr, then just return that error.
4517c478bd9Sstevel@tonic-gate  *
4527c478bd9Sstevel@tonic-gate  * As a side affect, the vattr argument is filled in with the converted
4537c478bd9Sstevel@tonic-gate  * attributes.
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate int
nfs3_cache_fattr3(vnode_t * vp,fattr3 * na,vattr_t * vap,hrtime_t t,cred_t * cr)4567c478bd9Sstevel@tonic-gate nfs3_cache_fattr3(vnode_t *vp, fattr3 *na, vattr_t *vap, hrtime_t t, cred_t *cr)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	int error;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	error = fattr3_to_vattr(vp, na, vap);
4617c478bd9Sstevel@tonic-gate 	if (error)
4627c478bd9Sstevel@tonic-gate 		return (error);
4637c478bd9Sstevel@tonic-gate 	nfs_attr_cache(vp, vap, t, cr);
4647c478bd9Sstevel@tonic-gate 	return (0);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate  * Use the passed in virtual attributes to check to see whether the
4697c478bd9Sstevel@tonic-gate  * data and metadata caches are valid, cache the new attributes, and
4707c478bd9Sstevel@tonic-gate  * then do the cache invalidation if required.
4717c478bd9Sstevel@tonic-gate  *
4727c478bd9Sstevel@tonic-gate  * The cache validation and caching of the new attributes is done
4737c478bd9Sstevel@tonic-gate  * atomically via the use of the mutex, r_statelock.  If required,
4747c478bd9Sstevel@tonic-gate  * the cache invalidation is done atomically w.r.t. the cache
4757c478bd9Sstevel@tonic-gate  * validation and caching of the attributes via the pseudo lock,
4767c478bd9Sstevel@tonic-gate  * r_serial.
4777c478bd9Sstevel@tonic-gate  *
4787c478bd9Sstevel@tonic-gate  * This routine is used to do cache validation and attributes caching
4797c478bd9Sstevel@tonic-gate  * for operations with a single set of post operation attributes.
4807c478bd9Sstevel@tonic-gate  */
4817c478bd9Sstevel@tonic-gate void
nfs_attr_cache(vnode_t * vp,vattr_t * vap,hrtime_t t,cred_t * cr)4827c478bd9Sstevel@tonic-gate nfs_attr_cache(vnode_t *vp, vattr_t *vap, hrtime_t t, cred_t *cr)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	rnode_t *rp;
4855e4df02aSvv149972 	int mtime_changed = 0;
4865e4df02aSvv149972 	int ctime_changed = 0;
4877c478bd9Sstevel@tonic-gate 	vsecattr_t *vsp;
4887c478bd9Sstevel@tonic-gate 	int was_serial;
4895e4df02aSvv149972 	len_t preattr_rsize;
4905e4df02aSvv149972 	boolean_t writeattr_set = B_FALSE;
4915e4df02aSvv149972 	boolean_t cachepurge_set = B_FALSE;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (rp->r_serial != curthread) {
4987c478bd9Sstevel@tonic-gate 		klwp_t *lwp = ttolwp(curthread);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		was_serial = 0;
5017c478bd9Sstevel@tonic-gate 		if (lwp != NULL)
5027c478bd9Sstevel@tonic-gate 			lwp->lwp_nostop++;
5037c478bd9Sstevel@tonic-gate 		while (rp->r_serial != NULL) {
5047c478bd9Sstevel@tonic-gate 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
5057c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
5067c478bd9Sstevel@tonic-gate 				if (lwp != NULL)
5077c478bd9Sstevel@tonic-gate 					lwp->lwp_nostop--;
5087c478bd9Sstevel@tonic-gate 				return;
5097c478bd9Sstevel@tonic-gate 			}
5107c478bd9Sstevel@tonic-gate 		}
5117c478bd9Sstevel@tonic-gate 		if (lwp != NULL)
5127c478bd9Sstevel@tonic-gate 			lwp->lwp_nostop--;
5137c478bd9Sstevel@tonic-gate 	} else
5147c478bd9Sstevel@tonic-gate 		was_serial = 1;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	if (rp->r_mtime > t) {
51700fdf600Smaheshvs 		if (!CACHE_VALID(rp, vap->va_mtime, vap->va_size))
51800fdf600Smaheshvs 			PURGE_ATTRCACHE_LOCKED(rp);
5197c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
5207c478bd9Sstevel@tonic-gate 		return;
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5235e4df02aSvv149972 	/*
5245e4df02aSvv149972 	 * Write thread after writing data to file on remote server,
5255e4df02aSvv149972 	 * will always set RWRITEATTR to indicate that file on remote
5265e4df02aSvv149972 	 * server was modified with a WRITE operation and would have
5275e4df02aSvv149972 	 * marked attribute cache as timed out. If RWRITEATTR
5285e4df02aSvv149972 	 * is set, then do not check for mtime and ctime change.
5295e4df02aSvv149972 	 */
5307c478bd9Sstevel@tonic-gate 	if (!(rp->r_flags & RWRITEATTR)) {
5317c478bd9Sstevel@tonic-gate 		if (!CACHE_VALID(rp, vap->va_mtime, vap->va_size))
5327c478bd9Sstevel@tonic-gate 			mtime_changed = 1;
5335e4df02aSvv149972 
5347c478bd9Sstevel@tonic-gate 		if (rp->r_attr.va_ctime.tv_sec != vap->va_ctime.tv_sec ||
5357c478bd9Sstevel@tonic-gate 		    rp->r_attr.va_ctime.tv_nsec != vap->va_ctime.tv_nsec)
5367c478bd9Sstevel@tonic-gate 			ctime_changed = 1;
5377c478bd9Sstevel@tonic-gate 	} else {
5385e4df02aSvv149972 		writeattr_set = B_TRUE;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5415e4df02aSvv149972 	preattr_rsize = rp->r_size;
5425e4df02aSvv149972 
5437c478bd9Sstevel@tonic-gate 	nfs_attrcache_va(vp, vap);
5447c478bd9Sstevel@tonic-gate 
5455e4df02aSvv149972 	/*
5465e4df02aSvv149972 	 * If we have updated filesize in nfs_attrcache_va, as soon as we
5475e4df02aSvv149972 	 * drop statelock we will be in transition of purging all
5485e4df02aSvv149972 	 * our caches and updating them. It is possible for another
5495e4df02aSvv149972 	 * thread to pick this new file size and read in zeroed data.
5505e4df02aSvv149972 	 * stall other threads till cache purge is complete.
5515e4df02aSvv149972 	 */
5525e4df02aSvv149972 	if ((vp->v_type == VREG) && (rp->r_size != preattr_rsize)) {
5535e4df02aSvv149972 		/*
5545e4df02aSvv149972 		 * If RWRITEATTR was set and we have updated the file
5555e4df02aSvv149972 		 * size, Server's returned file size need not necessarily
5565e4df02aSvv149972 		 * be because of this Client's WRITE. We need to purge
5575e4df02aSvv149972 		 * all caches.
5585e4df02aSvv149972 		 */
5595e4df02aSvv149972 		if (writeattr_set)
5605e4df02aSvv149972 			mtime_changed = 1;
5615e4df02aSvv149972 
5625e4df02aSvv149972 		if (mtime_changed && !(rp->r_flags & RINCACHEPURGE)) {
5635e4df02aSvv149972 			rp->r_flags |= RINCACHEPURGE;
5645e4df02aSvv149972 			cachepurge_set = B_TRUE;
5655e4df02aSvv149972 		}
5665e4df02aSvv149972 	}
5675e4df02aSvv149972 
5687c478bd9Sstevel@tonic-gate 	if (!mtime_changed && !ctime_changed) {
5697c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
5707c478bd9Sstevel@tonic-gate 		return;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	rp->r_serial = curthread;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (mtime_changed)
5787c478bd9Sstevel@tonic-gate 		nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
5797c478bd9Sstevel@tonic-gate 
5805e4df02aSvv149972 	if ((rp->r_flags & RINCACHEPURGE) && cachepurge_set) {
5815e4df02aSvv149972 		mutex_enter(&rp->r_statelock);
5825e4df02aSvv149972 		rp->r_flags &= ~RINCACHEPURGE;
5835e4df02aSvv149972 		cv_broadcast(&rp->r_cv);
5845e4df02aSvv149972 		mutex_exit(&rp->r_statelock);
5855e4df02aSvv149972 		cachepurge_set = B_FALSE;
5865e4df02aSvv149972 	}
5875e4df02aSvv149972 
5887c478bd9Sstevel@tonic-gate 	if (ctime_changed) {
5897c478bd9Sstevel@tonic-gate 		(void) nfs_access_purge_rp(rp);
5907c478bd9Sstevel@tonic-gate 		if (rp->r_secattr != NULL) {
5917c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
5927c478bd9Sstevel@tonic-gate 			vsp = rp->r_secattr;
5937c478bd9Sstevel@tonic-gate 			rp->r_secattr = NULL;
5947c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
5957c478bd9Sstevel@tonic-gate 			if (vsp != NULL)
5967c478bd9Sstevel@tonic-gate 				nfs_acl_free(vsp);
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (!was_serial) {
6017c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
6027c478bd9Sstevel@tonic-gate 		rp->r_serial = NULL;
6037c478bd9Sstevel@tonic-gate 		cv_broadcast(&rp->r_cv);
6047c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate /*
6097c478bd9Sstevel@tonic-gate  * Use the passed in "before" virtual attributes to check to see
6107c478bd9Sstevel@tonic-gate  * whether the data and metadata caches are valid, cache the "after"
6117c478bd9Sstevel@tonic-gate  * new attributes, and then do the cache invalidation if required.
6127c478bd9Sstevel@tonic-gate  *
6137c478bd9Sstevel@tonic-gate  * The cache validation and caching of the new attributes is done
6147c478bd9Sstevel@tonic-gate  * atomically via the use of the mutex, r_statelock.  If required,
6157c478bd9Sstevel@tonic-gate  * the cache invalidation is done atomically w.r.t. the cache
6167c478bd9Sstevel@tonic-gate  * validation and caching of the attributes via the pseudo lock,
6177c478bd9Sstevel@tonic-gate  * r_serial.
6187c478bd9Sstevel@tonic-gate  *
6197c478bd9Sstevel@tonic-gate  * This routine is used to do cache validation and attributes caching
6207c478bd9Sstevel@tonic-gate  * for operations with both pre operation attributes and post operation
6217c478bd9Sstevel@tonic-gate  * attributes.
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate static void
nfs3_attr_cache(vnode_t * vp,vattr_t * bvap,vattr_t * avap,hrtime_t t,cred_t * cr)6247c478bd9Sstevel@tonic-gate nfs3_attr_cache(vnode_t *vp, vattr_t *bvap, vattr_t *avap, hrtime_t t,
6257c478bd9Sstevel@tonic-gate     cred_t *cr)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	rnode_t *rp;
6285e4df02aSvv149972 	int mtime_changed = 0;
6295e4df02aSvv149972 	int ctime_changed = 0;
6307c478bd9Sstevel@tonic-gate 	vsecattr_t *vsp;
6317c478bd9Sstevel@tonic-gate 	int was_serial;
6325e4df02aSvv149972 	len_t preattr_rsize;
6335e4df02aSvv149972 	boolean_t writeattr_set = B_FALSE;
6345e4df02aSvv149972 	boolean_t cachepurge_set = B_FALSE;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	if (rp->r_serial != curthread) {
6417c478bd9Sstevel@tonic-gate 		klwp_t *lwp = ttolwp(curthread);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		was_serial = 0;
6447c478bd9Sstevel@tonic-gate 		if (lwp != NULL)
6457c478bd9Sstevel@tonic-gate 			lwp->lwp_nostop++;
6467c478bd9Sstevel@tonic-gate 		while (rp->r_serial != NULL) {
6477c478bd9Sstevel@tonic-gate 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
6487c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
6497c478bd9Sstevel@tonic-gate 				if (lwp != NULL)
6507c478bd9Sstevel@tonic-gate 					lwp->lwp_nostop--;
6517c478bd9Sstevel@tonic-gate 				return;
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 		}
6547c478bd9Sstevel@tonic-gate 		if (lwp != NULL)
6557c478bd9Sstevel@tonic-gate 			lwp->lwp_nostop--;
6567c478bd9Sstevel@tonic-gate 	} else
6577c478bd9Sstevel@tonic-gate 		was_serial = 1;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if (rp->r_mtime > t) {
66000fdf600Smaheshvs 		if (!CACHE_VALID(rp, avap->va_mtime, avap->va_size))
66100fdf600Smaheshvs 			PURGE_ATTRCACHE_LOCKED(rp);
6627c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
6637c478bd9Sstevel@tonic-gate 		return;
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate 
6665e4df02aSvv149972 	/*
6675e4df02aSvv149972 	 * Write thread after writing data to file on remote server,
6685e4df02aSvv149972 	 * will always set RWRITEATTR to indicate that file on remote
6695e4df02aSvv149972 	 * server was modified with a WRITE operation and would have
6705e4df02aSvv149972 	 * marked attribute cache as timed out. If RWRITEATTR
6715e4df02aSvv149972 	 * is set, then do not check for mtime and ctime change.
6725e4df02aSvv149972 	 */
6737c478bd9Sstevel@tonic-gate 	if (!(rp->r_flags & RWRITEATTR)) {
6747c478bd9Sstevel@tonic-gate 		if (!CACHE_VALID(rp, bvap->va_mtime, bvap->va_size))
6757c478bd9Sstevel@tonic-gate 			mtime_changed = 1;
6765e4df02aSvv149972 
6777c478bd9Sstevel@tonic-gate 		if (rp->r_attr.va_ctime.tv_sec != bvap->va_ctime.tv_sec ||
6787c478bd9Sstevel@tonic-gate 		    rp->r_attr.va_ctime.tv_nsec != bvap->va_ctime.tv_nsec)
6797c478bd9Sstevel@tonic-gate 			ctime_changed = 1;
6807c478bd9Sstevel@tonic-gate 	} else {
6815e4df02aSvv149972 		writeattr_set = B_TRUE;
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 
6845e4df02aSvv149972 	preattr_rsize = rp->r_size;
6855e4df02aSvv149972 
6867c478bd9Sstevel@tonic-gate 	nfs_attrcache_va(vp, avap);
6877c478bd9Sstevel@tonic-gate 
6885e4df02aSvv149972 	/*
6895e4df02aSvv149972 	 * If we have updated filesize in nfs_attrcache_va, as soon as we
6905e4df02aSvv149972 	 * drop statelock we will be in transition of purging all
6915e4df02aSvv149972 	 * our caches and updating them. It is possible for another
6925e4df02aSvv149972 	 * thread to pick this new file size and read in zeroed data.
6935e4df02aSvv149972 	 * stall other threads till cache purge is complete.
6945e4df02aSvv149972 	 */
6955e4df02aSvv149972 	if ((vp->v_type == VREG) && (rp->r_size != preattr_rsize)) {
6965e4df02aSvv149972 		/*
6975e4df02aSvv149972 		 * If RWRITEATTR was set and we have updated the file
6985e4df02aSvv149972 		 * size, Server's returned file size need not necessarily
6995e4df02aSvv149972 		 * be because of this Client's WRITE. We need to purge
7005e4df02aSvv149972 		 * all caches.
7015e4df02aSvv149972 		 */
7025e4df02aSvv149972 		if (writeattr_set)
7035e4df02aSvv149972 			mtime_changed = 1;
7045e4df02aSvv149972 
7055e4df02aSvv149972 		if (mtime_changed && !(rp->r_flags & RINCACHEPURGE)) {
7065e4df02aSvv149972 			rp->r_flags |= RINCACHEPURGE;
7075e4df02aSvv149972 			cachepurge_set = B_TRUE;
7085e4df02aSvv149972 		}
7095e4df02aSvv149972 	}
7105e4df02aSvv149972 
7117c478bd9Sstevel@tonic-gate 	if (!mtime_changed && !ctime_changed) {
7127c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
7137c478bd9Sstevel@tonic-gate 		return;
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	rp->r_serial = curthread;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if (mtime_changed)
7217c478bd9Sstevel@tonic-gate 		nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
7227c478bd9Sstevel@tonic-gate 
7235e4df02aSvv149972 	if ((rp->r_flags & RINCACHEPURGE) && cachepurge_set) {
7245e4df02aSvv149972 		mutex_enter(&rp->r_statelock);
7255e4df02aSvv149972 		rp->r_flags &= ~RINCACHEPURGE;
7265e4df02aSvv149972 		cv_broadcast(&rp->r_cv);
7275e4df02aSvv149972 		mutex_exit(&rp->r_statelock);
7285e4df02aSvv149972 		cachepurge_set = B_FALSE;
7295e4df02aSvv149972 	}
7305e4df02aSvv149972 
7317c478bd9Sstevel@tonic-gate 	if (ctime_changed) {
7327c478bd9Sstevel@tonic-gate 		(void) nfs_access_purge_rp(rp);
7337c478bd9Sstevel@tonic-gate 		if (rp->r_secattr != NULL) {
7347c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
7357c478bd9Sstevel@tonic-gate 			vsp = rp->r_secattr;
7367c478bd9Sstevel@tonic-gate 			rp->r_secattr = NULL;
7377c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
7387c478bd9Sstevel@tonic-gate 			if (vsp != NULL)
7397c478bd9Sstevel@tonic-gate 				nfs_acl_free(vsp);
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	if (!was_serial) {
7447c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
7457c478bd9Sstevel@tonic-gate 		rp->r_serial = NULL;
7467c478bd9Sstevel@tonic-gate 		cv_broadcast(&rp->r_cv);
7477c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate /*
7527c478bd9Sstevel@tonic-gate  * Set attributes cache for given vnode using virtual attributes.
7537c478bd9Sstevel@tonic-gate  *
7547c478bd9Sstevel@tonic-gate  * Set the timeout value on the attribute cache and fill it
7557c478bd9Sstevel@tonic-gate  * with the passed in attributes.
7567c478bd9Sstevel@tonic-gate  *
7577c478bd9Sstevel@tonic-gate  * The caller must be holding r_statelock.
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate void
nfs_attrcache_va(vnode_t * vp,struct vattr * va)7607c478bd9Sstevel@tonic-gate nfs_attrcache_va(vnode_t *vp, struct vattr *va)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	rnode_t *rp;
7637c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
7647c478bd9Sstevel@tonic-gate 	hrtime_t delta;
7657c478bd9Sstevel@tonic-gate 	hrtime_t now;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&rp->r_statelock));
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	now = gethrtime();
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/*
7767c478bd9Sstevel@tonic-gate 	 * Delta is the number of nanoseconds that we will
7777c478bd9Sstevel@tonic-gate 	 * cache the attributes of the file.  It is based on
7787c478bd9Sstevel@tonic-gate 	 * the number of nanoseconds since the last time that
7797c478bd9Sstevel@tonic-gate 	 * we detected a change.  The assumption is that files
7807c478bd9Sstevel@tonic-gate 	 * that changed recently are likely to change again.
7817c478bd9Sstevel@tonic-gate 	 * There is a minimum and a maximum for regular files
7827c478bd9Sstevel@tonic-gate 	 * and for directories which is enforced though.
7837c478bd9Sstevel@tonic-gate 	 *
7847c478bd9Sstevel@tonic-gate 	 * Using the time since last change was detected
7857c478bd9Sstevel@tonic-gate 	 * eliminates direct comparison or calculation
7867c478bd9Sstevel@tonic-gate 	 * using mixed client and server times.  NFS does
7877c478bd9Sstevel@tonic-gate 	 * not make any assumptions regarding the client
7887c478bd9Sstevel@tonic-gate 	 * and server clocks being synchronized.
7897c478bd9Sstevel@tonic-gate 	 */
7907c478bd9Sstevel@tonic-gate 	if (va->va_mtime.tv_sec != rp->r_attr.va_mtime.tv_sec ||
7917c478bd9Sstevel@tonic-gate 	    va->va_mtime.tv_nsec != rp->r_attr.va_mtime.tv_nsec ||
7927c478bd9Sstevel@tonic-gate 	    va->va_size != rp->r_attr.va_size)
7937c478bd9Sstevel@tonic-gate 		rp->r_mtime = now;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if ((mi->mi_flags & MI_NOAC) || (vp->v_flag & VNOCACHE))
7967c478bd9Sstevel@tonic-gate 		delta = 0;
7977c478bd9Sstevel@tonic-gate 	else {
7987c478bd9Sstevel@tonic-gate 		delta = now - rp->r_mtime;
7997c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
8007c478bd9Sstevel@tonic-gate 			if (delta < mi->mi_acdirmin)
8017c478bd9Sstevel@tonic-gate 				delta = mi->mi_acdirmin;
8027c478bd9Sstevel@tonic-gate 			else if (delta > mi->mi_acdirmax)
8037c478bd9Sstevel@tonic-gate 				delta = mi->mi_acdirmax;
8047c478bd9Sstevel@tonic-gate 		} else {
8057c478bd9Sstevel@tonic-gate 			if (delta < mi->mi_acregmin)
8067c478bd9Sstevel@tonic-gate 				delta = mi->mi_acregmin;
8077c478bd9Sstevel@tonic-gate 			else if (delta > mi->mi_acregmax)
8087c478bd9Sstevel@tonic-gate 				delta = mi->mi_acregmax;
8097c478bd9Sstevel@tonic-gate 		}
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 	rp->r_attrtime = now + delta;
8127c478bd9Sstevel@tonic-gate 	rp->r_attr = *va;
8137c478bd9Sstevel@tonic-gate 	/*
8147c478bd9Sstevel@tonic-gate 	 * Update the size of the file if there is no cached data or if
8157c478bd9Sstevel@tonic-gate 	 * the cached data is clean and there is no data being written
8167c478bd9Sstevel@tonic-gate 	 * out.
8177c478bd9Sstevel@tonic-gate 	 */
8187c478bd9Sstevel@tonic-gate 	if (rp->r_size != va->va_size &&
8197c478bd9Sstevel@tonic-gate 	    (!vn_has_cached_data(vp) ||
8207c478bd9Sstevel@tonic-gate 	    (!(rp->r_flags & RDIRTY) && rp->r_count == 0)))
8217c478bd9Sstevel@tonic-gate 		rp->r_size = va->va_size;
8227c478bd9Sstevel@tonic-gate 	nfs_setswaplike(vp, va);
8237c478bd9Sstevel@tonic-gate 	rp->r_flags &= ~RWRITEATTR;
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate /*
8277c478bd9Sstevel@tonic-gate  * Fill in attribute from the cache.
8287c478bd9Sstevel@tonic-gate  * If valid, then return 0 to indicate that no error occurred,
8297c478bd9Sstevel@tonic-gate  * otherwise return 1 to indicate that an error occurred.
8307c478bd9Sstevel@tonic-gate  */
8317c478bd9Sstevel@tonic-gate static int
nfs_getattr_cache(vnode_t * vp,struct vattr * vap)8327c478bd9Sstevel@tonic-gate nfs_getattr_cache(vnode_t *vp, struct vattr *vap)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	rnode_t *rp;
83500ba712dSGarima Tripathi 	uint_t mask = vap->va_mask;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
8387c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
8397c478bd9Sstevel@tonic-gate 	if (ATTRCACHE_VALID(vp)) {
8407c478bd9Sstevel@tonic-gate 		/*
8417c478bd9Sstevel@tonic-gate 		 * Cached attributes are valid
8427c478bd9Sstevel@tonic-gate 		 */
8437c478bd9Sstevel@tonic-gate 		*vap = rp->r_attr;
84400ba712dSGarima Tripathi 		/*
84500ba712dSGarima Tripathi 		 * Set the caller's va_mask to the set of attributes
84600ba712dSGarima Tripathi 		 * that were requested ANDed with the attributes that
84700ba712dSGarima Tripathi 		 * are available.  If attributes were requested that
84800ba712dSGarima Tripathi 		 * are not available, those bits must be turned off
84900ba712dSGarima Tripathi 		 * in the callers va_mask.
85000ba712dSGarima Tripathi 		 */
85100ba712dSGarima Tripathi 		vap->va_mask &= mask;
8527c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
8537c478bd9Sstevel@tonic-gate 		return (0);
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
8567c478bd9Sstevel@tonic-gate 	return (1);
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate /*
8607c478bd9Sstevel@tonic-gate  * Get attributes over-the-wire and update attributes cache
8617c478bd9Sstevel@tonic-gate  * if no error occurred in the over-the-wire operation.
8627c478bd9Sstevel@tonic-gate  * Return 0 if successful, otherwise error.
8637c478bd9Sstevel@tonic-gate  */
8647c478bd9Sstevel@tonic-gate int
nfs_getattr_otw(vnode_t * vp,struct vattr * vap,cred_t * cr)8657c478bd9Sstevel@tonic-gate nfs_getattr_otw(vnode_t *vp, struct vattr *vap, cred_t *cr)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	int error;
8687c478bd9Sstevel@tonic-gate 	struct nfsattrstat ns;
8697c478bd9Sstevel@tonic-gate 	int douprintf;
8707c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
8717c478bd9Sstevel@tonic-gate 	failinfo_t fi;
8727c478bd9Sstevel@tonic-gate 	hrtime_t t;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
8757c478bd9Sstevel@tonic-gate 	fi.vp = vp;
8767c478bd9Sstevel@tonic-gate 	fi.fhp = NULL;		/* no need to update, filehandle not copied */
8777c478bd9Sstevel@tonic-gate 	fi.copyproc = nfscopyfh;
8787c478bd9Sstevel@tonic-gate 	fi.lookupproc = nfslookup;
8797c478bd9Sstevel@tonic-gate 	fi.xattrdirproc = acl_getxattrdir2;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	if (mi->mi_flags & MI_ACL) {
8827c478bd9Sstevel@tonic-gate 		error = acl_getattr2_otw(vp, vap, cr);
8837c478bd9Sstevel@tonic-gate 		if (mi->mi_flags & MI_ACL)
8847c478bd9Sstevel@tonic-gate 			return (error);
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	douprintf = 1;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	t = gethrtime();
8907c478bd9Sstevel@tonic-gate 
891ba3bce04SSuhasini Peddada 	error = rfs2call(mi, RFS_GETATTR,
892ba3bce04SSuhasini Peddada 	    xdr_fhandle, (caddr_t)VTOFH(vp),
893ba3bce04SSuhasini Peddada 	    xdr_attrstat, (caddr_t)&ns, cr,
894ba3bce04SSuhasini Peddada 	    &douprintf, &ns.ns_status, 0, &fi);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if (!error) {
8977c478bd9Sstevel@tonic-gate 		error = geterrno(ns.ns_status);
8987c478bd9Sstevel@tonic-gate 		if (!error)
8997c478bd9Sstevel@tonic-gate 			error = nfs_cache_fattr(vp, &ns.ns_attr, vap, t, cr);
9007c478bd9Sstevel@tonic-gate 		else {
9017c478bd9Sstevel@tonic-gate 			PURGE_STALE_FH(error, vp, cr);
9027c478bd9Sstevel@tonic-gate 		}
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	return (error);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /*
9097c478bd9Sstevel@tonic-gate  * Return either cached ot remote attributes. If get remote attr
9107c478bd9Sstevel@tonic-gate  * use them to check and invalidate caches, then cache the new attributes.
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate int
nfsgetattr(vnode_t * vp,struct vattr * vap,cred_t * cr)9137c478bd9Sstevel@tonic-gate nfsgetattr(vnode_t *vp, struct vattr *vap, cred_t *cr)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 	int error;
9167c478bd9Sstevel@tonic-gate 	rnode_t *rp;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	/*
9197c478bd9Sstevel@tonic-gate 	 * If we've got cached attributes, we're done, otherwise go
9207c478bd9Sstevel@tonic-gate 	 * to the server to get attributes, which will update the cache
9217c478bd9Sstevel@tonic-gate 	 * in the process.
9227c478bd9Sstevel@tonic-gate 	 */
9237c478bd9Sstevel@tonic-gate 	error = nfs_getattr_cache(vp, vap);
9247c478bd9Sstevel@tonic-gate 	if (error)
9257c478bd9Sstevel@tonic-gate 		error = nfs_getattr_otw(vp, vap, cr);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/* Return the client's view of file size */
9287c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
9297c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
9307c478bd9Sstevel@tonic-gate 	vap->va_size = rp->r_size;
9317c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	return (error);
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate /*
9377c478bd9Sstevel@tonic-gate  * Get attributes over-the-wire and update attributes cache
9387c478bd9Sstevel@tonic-gate  * if no error occurred in the over-the-wire operation.
9397c478bd9Sstevel@tonic-gate  * Return 0 if successful, otherwise error.
9407c478bd9Sstevel@tonic-gate  */
9417c478bd9Sstevel@tonic-gate int
nfs3_getattr_otw(vnode_t * vp,struct vattr * vap,cred_t * cr)9427c478bd9Sstevel@tonic-gate nfs3_getattr_otw(vnode_t *vp, struct vattr *vap, cred_t *cr)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	int error;
9457c478bd9Sstevel@tonic-gate 	GETATTR3args args;
9467c478bd9Sstevel@tonic-gate 	GETATTR3vres res;
9477c478bd9Sstevel@tonic-gate 	int douprintf;
9487c478bd9Sstevel@tonic-gate 	failinfo_t fi;
9497c478bd9Sstevel@tonic-gate 	hrtime_t t;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	args.object = *VTOFH3(vp);
9527c478bd9Sstevel@tonic-gate 	fi.vp = vp;
9537c478bd9Sstevel@tonic-gate 	fi.fhp = (caddr_t)&args.object;
9547c478bd9Sstevel@tonic-gate 	fi.copyproc = nfs3copyfh;
9557c478bd9Sstevel@tonic-gate 	fi.lookupproc = nfs3lookup;
9567c478bd9Sstevel@tonic-gate 	fi.xattrdirproc = acl_getxattrdir3;
9577c478bd9Sstevel@tonic-gate 	res.fres.vp = vp;
9587c478bd9Sstevel@tonic-gate 	res.fres.vap = vap;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	douprintf = 1;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	t = gethrtime();
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	error = rfs3call(VTOMI(vp), NFSPROC3_GETATTR,
9657c478bd9Sstevel@tonic-gate 	    xdr_nfs_fh3, (caddr_t)&args,
9667c478bd9Sstevel@tonic-gate 	    xdr_GETATTR3vres, (caddr_t)&res, cr,
9677c478bd9Sstevel@tonic-gate 	    &douprintf, &res.status, 0, &fi);
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (error)
9707c478bd9Sstevel@tonic-gate 		return (error);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	error = geterrno3(res.status);
9737c478bd9Sstevel@tonic-gate 	if (error) {
9747c478bd9Sstevel@tonic-gate 		PURGE_STALE_FH(error, vp, cr);
9757c478bd9Sstevel@tonic-gate 		return (error);
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	/*
9797c478bd9Sstevel@tonic-gate 	 * Catch status codes that indicate fattr3 to vattr translation failure
9807c478bd9Sstevel@tonic-gate 	 */
9817c478bd9Sstevel@tonic-gate 	if (res.fres.status)
9827c478bd9Sstevel@tonic-gate 		return (res.fres.status);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	nfs_attr_cache(vp, vap, t, cr);
9857c478bd9Sstevel@tonic-gate 	return (0);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate /*
9897c478bd9Sstevel@tonic-gate  * Return either cached or remote attributes. If get remote attr
9907c478bd9Sstevel@tonic-gate  * use them to check and invalidate caches, then cache the new attributes.
9917c478bd9Sstevel@tonic-gate  */
9927c478bd9Sstevel@tonic-gate int
nfs3getattr(vnode_t * vp,struct vattr * vap,cred_t * cr)9937c478bd9Sstevel@tonic-gate nfs3getattr(vnode_t *vp, struct vattr *vap, cred_t *cr)
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate 	int error;
9967c478bd9Sstevel@tonic-gate 	rnode_t *rp;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	/*
9997c478bd9Sstevel@tonic-gate 	 * If we've got cached attributes, we're done, otherwise go
10007c478bd9Sstevel@tonic-gate 	 * to the server to get attributes, which will update the cache
10017c478bd9Sstevel@tonic-gate 	 * in the process.
10027c478bd9Sstevel@tonic-gate 	 */
10037c478bd9Sstevel@tonic-gate 	error = nfs_getattr_cache(vp, vap);
10047c478bd9Sstevel@tonic-gate 	if (error)
10057c478bd9Sstevel@tonic-gate 		error = nfs3_getattr_otw(vp, vap, cr);
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	/* Return the client's view of file size */
10087c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
10097c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
10107c478bd9Sstevel@tonic-gate 	vap->va_size = rp->r_size;
10117c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	return (error);
10147c478bd9Sstevel@tonic-gate }
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate vtype_t nf_to_vt[] = {
10177c478bd9Sstevel@tonic-gate 	VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK
10187c478bd9Sstevel@tonic-gate };
10197c478bd9Sstevel@tonic-gate /*
10207c478bd9Sstevel@tonic-gate  * Convert NFS Version 2 over the network attributes to the local
10217c478bd9Sstevel@tonic-gate  * virtual attributes.  The mapping between the UID_NOBODY/GID_NOBODY
10227c478bd9Sstevel@tonic-gate  * network representation and the local representation is done here.
10237c478bd9Sstevel@tonic-gate  * Returns 0 for success, error if failed due to overflow.
10247c478bd9Sstevel@tonic-gate  */
10257c478bd9Sstevel@tonic-gate int
nattr_to_vattr(vnode_t * vp,struct nfsfattr * na,struct vattr * vap)10267c478bd9Sstevel@tonic-gate nattr_to_vattr(vnode_t *vp, struct nfsfattr *na, struct vattr *vap)
10277c478bd9Sstevel@tonic-gate {
10287c478bd9Sstevel@tonic-gate 	/* overflow in time attributes? */
10297c478bd9Sstevel@tonic-gate #ifndef _LP64
10307c478bd9Sstevel@tonic-gate 	if (!NFS2_FATTR_TIME_OK(na))
10317c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
10327c478bd9Sstevel@tonic-gate #endif
10337c478bd9Sstevel@tonic-gate 
103400ba712dSGarima Tripathi 	vap->va_mask = AT_ALL;
103500ba712dSGarima Tripathi 
10367c478bd9Sstevel@tonic-gate 	if (na->na_type < NFNON || na->na_type > NFSOC)
10377c478bd9Sstevel@tonic-gate 		vap->va_type = VBAD;
10387c478bd9Sstevel@tonic-gate 	else
10397c478bd9Sstevel@tonic-gate 		vap->va_type = nf_to_vt[na->na_type];
10407c478bd9Sstevel@tonic-gate 	vap->va_mode = na->na_mode;
10417c478bd9Sstevel@tonic-gate 	vap->va_uid = (na->na_uid == NFS_UID_NOBODY) ? UID_NOBODY : na->na_uid;
10427c478bd9Sstevel@tonic-gate 	vap->va_gid = (na->na_gid == NFS_GID_NOBODY) ? GID_NOBODY : na->na_gid;
10437c478bd9Sstevel@tonic-gate 	vap->va_fsid = vp->v_vfsp->vfs_dev;
10447c478bd9Sstevel@tonic-gate 	vap->va_nodeid = na->na_nodeid;
10457c478bd9Sstevel@tonic-gate 	vap->va_nlink = na->na_nlink;
10467c478bd9Sstevel@tonic-gate 	vap->va_size = na->na_size;	/* keep for cache validation */
10477c478bd9Sstevel@tonic-gate 	/*
10487c478bd9Sstevel@tonic-gate 	 * nfs protocol defines times as unsigned so don't extend sign,
10497c478bd9Sstevel@tonic-gate 	 * unless sysadmin set nfs_allow_preepoch_time.
10507c478bd9Sstevel@tonic-gate 	 */
10517c478bd9Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_atime.tv_sec, na->na_atime.tv_sec);
10527c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_nsec = (uint32_t)(na->na_atime.tv_usec * 1000);
10537c478bd9Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_mtime.tv_sec, na->na_mtime.tv_sec);
10547c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_nsec = (uint32_t)(na->na_mtime.tv_usec * 1000);
10557c478bd9Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_ctime.tv_sec, na->na_ctime.tv_sec);
10567c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = (uint32_t)(na->na_ctime.tv_usec * 1000);
10577c478bd9Sstevel@tonic-gate 	/*
10587c478bd9Sstevel@tonic-gate 	 * Shannon's law - uncompress the received dev_t
10597c478bd9Sstevel@tonic-gate 	 * if the top half of is zero indicating a response
10607c478bd9Sstevel@tonic-gate 	 * from an `older style' OS. Except for when it is a
10617c478bd9Sstevel@tonic-gate 	 * `new style' OS sending the maj device of zero,
10627c478bd9Sstevel@tonic-gate 	 * in which case the algorithm still works because the
10637c478bd9Sstevel@tonic-gate 	 * fact that it is a new style server
10647c478bd9Sstevel@tonic-gate 	 * is hidden by the minor device not being greater
10657c478bd9Sstevel@tonic-gate 	 * than 255 (a requirement in this case).
10667c478bd9Sstevel@tonic-gate 	 */
10677c478bd9Sstevel@tonic-gate 	if ((na->na_rdev & 0xffff0000) == 0)
10687c478bd9Sstevel@tonic-gate 		vap->va_rdev = nfsv2_expdev(na->na_rdev);
10697c478bd9Sstevel@tonic-gate 	else
10707c478bd9Sstevel@tonic-gate 		vap->va_rdev = expldev(na->na_rdev);
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	vap->va_nblocks = na->na_blocks;
10737c478bd9Sstevel@tonic-gate 	switch (na->na_type) {
10747c478bd9Sstevel@tonic-gate 	case NFBLK:
10757c478bd9Sstevel@tonic-gate 		vap->va_blksize = DEV_BSIZE;
10767c478bd9Sstevel@tonic-gate 		break;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	case NFCHR:
10797c478bd9Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;
10807c478bd9Sstevel@tonic-gate 		break;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	case NFSOC:
10837c478bd9Sstevel@tonic-gate 	default:
10847c478bd9Sstevel@tonic-gate 		vap->va_blksize = na->na_blocksize;
10857c478bd9Sstevel@tonic-gate 		break;
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate 	/*
10887c478bd9Sstevel@tonic-gate 	 * This bit of ugliness is a hack to preserve the
10897c478bd9Sstevel@tonic-gate 	 * over-the-wire protocols for named-pipe vnodes.
10907c478bd9Sstevel@tonic-gate 	 * It remaps the special over-the-wire type to the
10917c478bd9Sstevel@tonic-gate 	 * VFIFO type. (see note in nfs.h)
10927c478bd9Sstevel@tonic-gate 	 */
10937c478bd9Sstevel@tonic-gate 	if (NA_ISFIFO(na)) {
10947c478bd9Sstevel@tonic-gate 		vap->va_type = VFIFO;
10957c478bd9Sstevel@tonic-gate 		vap->va_mode = (vap->va_mode & ~S_IFMT) | S_IFIFO;
10967c478bd9Sstevel@tonic-gate 		vap->va_rdev = 0;
10977c478bd9Sstevel@tonic-gate 		vap->va_blksize = na->na_blocksize;
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate 	vap->va_seq = 0;
11007c478bd9Sstevel@tonic-gate 	return (0);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate /*
11047c478bd9Sstevel@tonic-gate  * Convert NFS Version 3 over the network attributes to the local
11057c478bd9Sstevel@tonic-gate  * virtual attributes.  The mapping between the UID_NOBODY/GID_NOBODY
11067c478bd9Sstevel@tonic-gate  * network representation and the local representation is done here.
11077c478bd9Sstevel@tonic-gate  */
11087c478bd9Sstevel@tonic-gate vtype_t nf3_to_vt[] = {
11097c478bd9Sstevel@tonic-gate 	VBAD, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
11107c478bd9Sstevel@tonic-gate };
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate int
fattr3_to_vattr(vnode_t * vp,fattr3 * na,struct vattr * vap)11137c478bd9Sstevel@tonic-gate fattr3_to_vattr(vnode_t *vp, fattr3 *na, struct vattr *vap)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate #ifndef _LP64
11177c478bd9Sstevel@tonic-gate 	/* overflow in time attributes? */
11187c478bd9Sstevel@tonic-gate 	if (!NFS3_FATTR_TIME_OK(na))
11197c478bd9Sstevel@tonic-gate 		return (EOVERFLOW);
11207c478bd9Sstevel@tonic-gate #endif
11217c478bd9Sstevel@tonic-gate 	if (!NFS3_SIZE_OK(na->size))
11227c478bd9Sstevel@tonic-gate 		/* file too big */
11237c478bd9Sstevel@tonic-gate 		return (EFBIG);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	vap->va_mask = AT_ALL;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	if (na->type < NF3REG || na->type > NF3FIFO)
11287c478bd9Sstevel@tonic-gate 		vap->va_type = VBAD;
11297c478bd9Sstevel@tonic-gate 	else
11307c478bd9Sstevel@tonic-gate 		vap->va_type = nf3_to_vt[na->type];
11317c478bd9Sstevel@tonic-gate 	vap->va_mode = na->mode;
11327c478bd9Sstevel@tonic-gate 	vap->va_uid = (na->uid == NFS_UID_NOBODY) ? UID_NOBODY : (uid_t)na->uid;
11337c478bd9Sstevel@tonic-gate 	vap->va_gid = (na->gid == NFS_GID_NOBODY) ? GID_NOBODY : (gid_t)na->gid;
11347c478bd9Sstevel@tonic-gate 	vap->va_fsid = vp->v_vfsp->vfs_dev;
11357c478bd9Sstevel@tonic-gate 	vap->va_nodeid = na->fileid;
11367c478bd9Sstevel@tonic-gate 	vap->va_nlink = na->nlink;
11377c478bd9Sstevel@tonic-gate 	vap->va_size = na->size;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	/*
11407c478bd9Sstevel@tonic-gate 	 * nfs protocol defines times as unsigned so don't extend sign,
11417c478bd9Sstevel@tonic-gate 	 * unless sysadmin set nfs_allow_preepoch_time.
11427c478bd9Sstevel@tonic-gate 	 */
11437c478bd9Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_atime.tv_sec, na->atime.seconds);
11447c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_nsec = (uint32_t)na->atime.nseconds;
11457c478bd9Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_mtime.tv_sec, na->mtime.seconds);
11467c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_nsec = (uint32_t)na->mtime.nseconds;
11477c478bd9Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_ctime.tv_sec, na->ctime.seconds);
11487c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = (uint32_t)na->ctime.nseconds;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	switch (na->type) {
11517c478bd9Sstevel@tonic-gate 	case NF3BLK:
11527c478bd9Sstevel@tonic-gate 		vap->va_rdev = makedevice(na->rdev.specdata1,
11537c478bd9Sstevel@tonic-gate 		    na->rdev.specdata2);
11547c478bd9Sstevel@tonic-gate 		vap->va_blksize = DEV_BSIZE;
11557c478bd9Sstevel@tonic-gate 		vap->va_nblocks = 0;
11567c478bd9Sstevel@tonic-gate 		break;
11577c478bd9Sstevel@tonic-gate 	case NF3CHR:
11587c478bd9Sstevel@tonic-gate 		vap->va_rdev = makedevice(na->rdev.specdata1,
11597c478bd9Sstevel@tonic-gate 		    na->rdev.specdata2);
11607c478bd9Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;
11617c478bd9Sstevel@tonic-gate 		vap->va_nblocks = 0;
11627c478bd9Sstevel@tonic-gate 		break;
11637c478bd9Sstevel@tonic-gate 	case NF3REG:
11647c478bd9Sstevel@tonic-gate 	case NF3DIR:
11657c478bd9Sstevel@tonic-gate 	case NF3LNK:
11667c478bd9Sstevel@tonic-gate 		vap->va_rdev = 0;
11677c478bd9Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;
11687c478bd9Sstevel@tonic-gate 		vap->va_nblocks = (u_longlong_t)
11697c478bd9Sstevel@tonic-gate 		    ((na->used + (size3)DEV_BSIZE - (size3)1) /
11707c478bd9Sstevel@tonic-gate 		    (size3)DEV_BSIZE);
11717c478bd9Sstevel@tonic-gate 		break;
11727c478bd9Sstevel@tonic-gate 	case NF3SOCK:
11737c478bd9Sstevel@tonic-gate 	case NF3FIFO:
11747c478bd9Sstevel@tonic-gate 	default:
11757c478bd9Sstevel@tonic-gate 		vap->va_rdev = 0;
11767c478bd9Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;
11777c478bd9Sstevel@tonic-gate 		vap->va_nblocks = 0;
11787c478bd9Sstevel@tonic-gate 		break;
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 	vap->va_seq = 0;
11817c478bd9Sstevel@tonic-gate 	return (0);
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate /*
11857c478bd9Sstevel@tonic-gate  * Asynchronous I/O parameters.  nfs_async_threads is the high-water mark
11867c478bd9Sstevel@tonic-gate  * for the demand-based allocation of async threads per-mount.  The
11877c478bd9Sstevel@tonic-gate  * nfs_async_timeout is the amount of time a thread will live after it
11887c478bd9Sstevel@tonic-gate  * becomes idle, unless new I/O requests are received before the thread
11897c478bd9Sstevel@tonic-gate  * dies.  See nfs_async_putpage and nfs_async_start.
11907c478bd9Sstevel@tonic-gate  */
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate int nfs_async_timeout = -1;	/* uninitialized */
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate static void	nfs_async_start(struct vfs *);
11950776f5e6SVallish Vaidyeshwara static void	nfs_async_pgops_start(struct vfs *);
11960776f5e6SVallish Vaidyeshwara static void	nfs_async_common_start(struct vfs *, int);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate static void
free_async_args(struct nfs_async_reqs * args)11997c478bd9Sstevel@tonic-gate free_async_args(struct nfs_async_reqs *args)
12007c478bd9Sstevel@tonic-gate {
12017c478bd9Sstevel@tonic-gate 	rnode_t *rp;
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	if (args->a_io != NFS_INACTIVE) {
12047c478bd9Sstevel@tonic-gate 		rp = VTOR(args->a_vp);
12057c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
12067c478bd9Sstevel@tonic-gate 		rp->r_count--;
12077c478bd9Sstevel@tonic-gate 		if (args->a_io == NFS_PUTAPAGE ||
12087c478bd9Sstevel@tonic-gate 		    args->a_io == NFS_PAGEIO)
12097c478bd9Sstevel@tonic-gate 			rp->r_awcount--;
12107c478bd9Sstevel@tonic-gate 		cv_broadcast(&rp->r_cv);
12117c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
12127c478bd9Sstevel@tonic-gate 		VN_RELE(args->a_vp);
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 	crfree(args->a_cred);
12157c478bd9Sstevel@tonic-gate 	kmem_free(args, sizeof (*args));
12167c478bd9Sstevel@tonic-gate }
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate /*
12197c478bd9Sstevel@tonic-gate  * Cross-zone thread creation and NFS access is disallowed, yet fsflush() and
12207c478bd9Sstevel@tonic-gate  * pageout(), running in the global zone, have legitimate reasons to do
12217c478bd9Sstevel@tonic-gate  * VOP_PUTPAGE(B_ASYNC) on other zones' NFS mounts.  We avoid the problem by
12227c478bd9Sstevel@tonic-gate  * use of a a per-mount "asynchronous requests manager thread" which is
12237c478bd9Sstevel@tonic-gate  * signaled by the various asynchronous work routines when there is
12247c478bd9Sstevel@tonic-gate  * asynchronous work to be done.  It is responsible for creating new
12257c478bd9Sstevel@tonic-gate  * worker threads if necessary, and notifying existing worker threads
12267c478bd9Sstevel@tonic-gate  * that there is work to be done.
12277c478bd9Sstevel@tonic-gate  *
12287c478bd9Sstevel@tonic-gate  * In other words, it will "take the specifications from the customers and
12297c478bd9Sstevel@tonic-gate  * give them to the engineers."
12307c478bd9Sstevel@tonic-gate  *
12317c478bd9Sstevel@tonic-gate  * Worker threads die off of their own accord if they are no longer
12327c478bd9Sstevel@tonic-gate  * needed.
12337c478bd9Sstevel@tonic-gate  *
12347c478bd9Sstevel@tonic-gate  * This thread is killed when the zone is going away or the filesystem
12357c478bd9Sstevel@tonic-gate  * is being unmounted.
12367c478bd9Sstevel@tonic-gate  */
12377c478bd9Sstevel@tonic-gate void
nfs_async_manager(vfs_t * vfsp)12387c478bd9Sstevel@tonic-gate nfs_async_manager(vfs_t *vfsp)
12397c478bd9Sstevel@tonic-gate {
12407c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
12417c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
12427c478bd9Sstevel@tonic-gate 	uint_t max_threads;
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr,
12477c478bd9Sstevel@tonic-gate 	    "nfs_async_manager");
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
12507c478bd9Sstevel@tonic-gate 	/*
12517c478bd9Sstevel@tonic-gate 	 * We want to stash the max number of threads that this mount was
12527c478bd9Sstevel@tonic-gate 	 * allowed so we can use it later when the variable is set to zero as
12537c478bd9Sstevel@tonic-gate 	 * part of the zone/mount going away.
12547c478bd9Sstevel@tonic-gate 	 *
12557c478bd9Sstevel@tonic-gate 	 * We want to be able to create at least one thread to handle
1256388e50fcSMarcel Telka 	 * asynchronous inactive calls.
12577c478bd9Sstevel@tonic-gate 	 */
12587c478bd9Sstevel@tonic-gate 	max_threads = MAX(mi->mi_max_threads, 1);
12597c478bd9Sstevel@tonic-gate 	/*
12607c478bd9Sstevel@tonic-gate 	 * We don't want to wait for mi_max_threads to go to zero, since that
12617c478bd9Sstevel@tonic-gate 	 * happens as part of a failed unmount, but this thread should only
12627c478bd9Sstevel@tonic-gate 	 * exit when the mount/zone is really going away.
12637c478bd9Sstevel@tonic-gate 	 *
12647c478bd9Sstevel@tonic-gate 	 * Once MI_ASYNC_MGR_STOP is set, no more async operations will be
12657c478bd9Sstevel@tonic-gate 	 * attempted: the various _async_*() functions know to do things
12667c478bd9Sstevel@tonic-gate 	 * inline if mi_max_threads == 0.  Henceforth we just drain out the
12677c478bd9Sstevel@tonic-gate 	 * outstanding requests.
12687c478bd9Sstevel@tonic-gate 	 *
12697c478bd9Sstevel@tonic-gate 	 * Note that we still create zthreads even if we notice the zone is
12707c478bd9Sstevel@tonic-gate 	 * shutting down (MI_ASYNC_MGR_STOP is set); this may cause the zone
12717c478bd9Sstevel@tonic-gate 	 * shutdown sequence to take slightly longer in some cases, but
12727c478bd9Sstevel@tonic-gate 	 * doesn't violate the protocol, as all threads will exit as soon as
12737c478bd9Sstevel@tonic-gate 	 * they're done processing the remaining requests.
12747c478bd9Sstevel@tonic-gate 	 */
1275388e50fcSMarcel Telka 	for (;;) {
12767c478bd9Sstevel@tonic-gate 		while (mi->mi_async_req_count > 0) {
12777c478bd9Sstevel@tonic-gate 			/*
12787c478bd9Sstevel@tonic-gate 			 * Paranoia: If the mount started out having
12797c478bd9Sstevel@tonic-gate 			 * (mi->mi_max_threads == 0), and the value was
12807c478bd9Sstevel@tonic-gate 			 * later changed (via a debugger or somesuch),
12817c478bd9Sstevel@tonic-gate 			 * we could be confused since we will think we
12827c478bd9Sstevel@tonic-gate 			 * can't create any threads, and the calling
12837c478bd9Sstevel@tonic-gate 			 * code (which looks at the current value of
12847c478bd9Sstevel@tonic-gate 			 * mi->mi_max_threads, now non-zero) thinks we
12857c478bd9Sstevel@tonic-gate 			 * can.
12867c478bd9Sstevel@tonic-gate 			 *
12877c478bd9Sstevel@tonic-gate 			 * So, because we're paranoid, we create threads
12887c478bd9Sstevel@tonic-gate 			 * up to the maximum of the original and the
12897c478bd9Sstevel@tonic-gate 			 * current value. This means that future
12907c478bd9Sstevel@tonic-gate 			 * (debugger-induced) lowerings of
12917c478bd9Sstevel@tonic-gate 			 * mi->mi_max_threads are ignored for our
12927c478bd9Sstevel@tonic-gate 			 * purposes, but who told them they could change
12937c478bd9Sstevel@tonic-gate 			 * random values on a live kernel anyhow?
12947c478bd9Sstevel@tonic-gate 			 */
12950776f5e6SVallish Vaidyeshwara 			if (mi->mi_threads[NFS_ASYNC_QUEUE] <
12967c478bd9Sstevel@tonic-gate 			    MAX(mi->mi_max_threads, max_threads)) {
12970776f5e6SVallish Vaidyeshwara 				mi->mi_threads[NFS_ASYNC_QUEUE]++;
12987c478bd9Sstevel@tonic-gate 				mutex_exit(&mi->mi_async_lock);
12997c478bd9Sstevel@tonic-gate 				VFS_HOLD(vfsp);	/* hold for new thread */
13007c478bd9Sstevel@tonic-gate 				(void) zthread_create(NULL, 0, nfs_async_start,
13017c478bd9Sstevel@tonic-gate 				    vfsp, 0, minclsyspri);
13027c478bd9Sstevel@tonic-gate 				mutex_enter(&mi->mi_async_lock);
13030776f5e6SVallish Vaidyeshwara 			} else if (mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE] <
13040776f5e6SVallish Vaidyeshwara 			    NUM_ASYNC_PGOPS_THREADS) {
13050776f5e6SVallish Vaidyeshwara 				mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE]++;
13060776f5e6SVallish Vaidyeshwara 				mutex_exit(&mi->mi_async_lock);
13070776f5e6SVallish Vaidyeshwara 				VFS_HOLD(vfsp); /* hold for new thread */
13080776f5e6SVallish Vaidyeshwara 				(void) zthread_create(NULL, 0,
13090776f5e6SVallish Vaidyeshwara 				    nfs_async_pgops_start, vfsp, 0,
13100776f5e6SVallish Vaidyeshwara 				    minclsyspri);
13110776f5e6SVallish Vaidyeshwara 				mutex_enter(&mi->mi_async_lock);
13127c478bd9Sstevel@tonic-gate 			}
13130776f5e6SVallish Vaidyeshwara 			NFS_WAKE_ASYNC_WORKER(mi->mi_async_work_cv);
13147c478bd9Sstevel@tonic-gate 			ASSERT(mi->mi_async_req_count != 0);
13157c478bd9Sstevel@tonic-gate 			mi->mi_async_req_count--;
13167c478bd9Sstevel@tonic-gate 		}
1317388e50fcSMarcel Telka 
13187c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
1319388e50fcSMarcel Telka 		if (mi->mi_flags & MI_ASYNC_MGR_STOP) {
1320388e50fcSMarcel Telka 			mutex_exit(&mi->mi_lock);
1321388e50fcSMarcel Telka 			break;
13227c478bd9Sstevel@tonic-gate 		}
13237c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
1324388e50fcSMarcel Telka 
1325388e50fcSMarcel Telka 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
1326388e50fcSMarcel Telka 		cv_wait(&mi->mi_async_reqs_cv, &mi->mi_async_lock);
1327388e50fcSMarcel Telka 		CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock);
1328388e50fcSMarcel Telka 	}
13297c478bd9Sstevel@tonic-gate 	/*
13307c478bd9Sstevel@tonic-gate 	 * Let everyone know we're done.
13317c478bd9Sstevel@tonic-gate 	 */
13327c478bd9Sstevel@tonic-gate 	mi->mi_manager_thread = NULL;
13337c478bd9Sstevel@tonic-gate 	cv_broadcast(&mi->mi_async_cv);
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	/*
13367c478bd9Sstevel@tonic-gate 	 * There is no explicit call to mutex_exit(&mi->mi_async_lock)
13377c478bd9Sstevel@tonic-gate 	 * since CALLB_CPR_EXIT is actually responsible for releasing
13387c478bd9Sstevel@tonic-gate 	 * 'mi_async_lock'.
13397c478bd9Sstevel@tonic-gate 	 */
13407c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
13417c478bd9Sstevel@tonic-gate 	VFS_RELE(vfsp);	/* release thread's hold */
13427c478bd9Sstevel@tonic-gate 	zthread_exit();
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate /*
13467c478bd9Sstevel@tonic-gate  * Signal (and wait for) the async manager thread to clean up and go away.
13477c478bd9Sstevel@tonic-gate  */
13487c478bd9Sstevel@tonic-gate void
nfs_async_manager_stop(vfs_t * vfsp)13497c478bd9Sstevel@tonic-gate nfs_async_manager_stop(vfs_t *vfsp)
13507c478bd9Sstevel@tonic-gate {
13517c478bd9Sstevel@tonic-gate 	mntinfo_t *mi = VFTOMI(vfsp);
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
13547c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_lock);
13557c478bd9Sstevel@tonic-gate 	mi->mi_flags |= MI_ASYNC_MGR_STOP;
13567c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_lock);
13577c478bd9Sstevel@tonic-gate 	cv_broadcast(&mi->mi_async_reqs_cv);
13587c478bd9Sstevel@tonic-gate 	while (mi->mi_manager_thread != NULL)
13597c478bd9Sstevel@tonic-gate 		cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
13607c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
13617c478bd9Sstevel@tonic-gate }
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate int
nfs_async_readahead(vnode_t * vp,u_offset_t blkoff,caddr_t addr,struct seg * seg,cred_t * cr,void (* readahead)(vnode_t *,u_offset_t,caddr_t,struct seg *,cred_t *))13647c478bd9Sstevel@tonic-gate nfs_async_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr,
13657c478bd9Sstevel@tonic-gate     struct seg *seg, cred_t *cr, void (*readahead)(vnode_t *,
13667c478bd9Sstevel@tonic-gate     u_offset_t, caddr_t, struct seg *, cred_t *))
13677c478bd9Sstevel@tonic-gate {
13687c478bd9Sstevel@tonic-gate 	rnode_t *rp;
13697c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
13707c478bd9Sstevel@tonic-gate 	struct nfs_async_reqs *args;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
13737c478bd9Sstevel@tonic-gate 	ASSERT(rp->r_freef == NULL);
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	/*
13787c478bd9Sstevel@tonic-gate 	 * If addr falls in a different segment, don't bother doing readahead.
13797c478bd9Sstevel@tonic-gate 	 */
13807c478bd9Sstevel@tonic-gate 	if (addr >= seg->s_base + seg->s_size)
13817c478bd9Sstevel@tonic-gate 		return (-1);
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	/*
13847c478bd9Sstevel@tonic-gate 	 * If we can't allocate a request structure, punt on the readahead.
13857c478bd9Sstevel@tonic-gate 	 */
13867c478bd9Sstevel@tonic-gate 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
13877c478bd9Sstevel@tonic-gate 		return (-1);
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * If a lock operation is pending, don't initiate any new
13917c478bd9Sstevel@tonic-gate 	 * readaheads.  Otherwise, bump r_count to indicate the new
13927c478bd9Sstevel@tonic-gate 	 * asynchronous I/O.
13937c478bd9Sstevel@tonic-gate 	 */
13947c478bd9Sstevel@tonic-gate 	if (!nfs_rw_tryenter(&rp->r_lkserlock, RW_READER)) {
13957c478bd9Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
13967c478bd9Sstevel@tonic-gate 		return (-1);
13977c478bd9Sstevel@tonic-gate 	}
13987c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
13997c478bd9Sstevel@tonic-gate 	rp->r_count++;
14007c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
14017c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&rp->r_lkserlock);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	args->a_next = NULL;
14047c478bd9Sstevel@tonic-gate #ifdef DEBUG
14057c478bd9Sstevel@tonic-gate 	args->a_queuer = curthread;
14067c478bd9Sstevel@tonic-gate #endif
14077c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
14087c478bd9Sstevel@tonic-gate 	args->a_vp = vp;
14097c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
14107c478bd9Sstevel@tonic-gate 	crhold(cr);
14117c478bd9Sstevel@tonic-gate 	args->a_cred = cr;
14127c478bd9Sstevel@tonic-gate 	args->a_io = NFS_READ_AHEAD;
14137c478bd9Sstevel@tonic-gate 	args->a_nfs_readahead = readahead;
14147c478bd9Sstevel@tonic-gate 	args->a_nfs_blkoff = blkoff;
14157c478bd9Sstevel@tonic-gate 	args->a_nfs_seg = seg;
14167c478bd9Sstevel@tonic-gate 	args->a_nfs_addr = addr;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	/*
14217c478bd9Sstevel@tonic-gate 	 * If asyncio has been disabled, don't bother readahead.
14227c478bd9Sstevel@tonic-gate 	 */
14237c478bd9Sstevel@tonic-gate 	if (mi->mi_max_threads == 0) {
14247c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
14257c478bd9Sstevel@tonic-gate 		goto noasync;
14267c478bd9Sstevel@tonic-gate 	}
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	/*
14297c478bd9Sstevel@tonic-gate 	 * Link request structure into the async list and
14307c478bd9Sstevel@tonic-gate 	 * wakeup async thread to do the i/o.
14317c478bd9Sstevel@tonic-gate 	 */
14327c478bd9Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_READ_AHEAD] == NULL) {
14337c478bd9Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_READ_AHEAD] = args;
14347c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READ_AHEAD] = args;
14357c478bd9Sstevel@tonic-gate 	} else {
14367c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READ_AHEAD]->a_next = args;
14377c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READ_AHEAD] = args;
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
14417c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
14427c478bd9Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
14437c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	mi->mi_async_req_count++;
14477c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
14487c478bd9Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
14497c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
14507c478bd9Sstevel@tonic-gate 	return (0);
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate noasync:
14537c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
14547c478bd9Sstevel@tonic-gate 	rp->r_count--;
14557c478bd9Sstevel@tonic-gate 	cv_broadcast(&rp->r_cv);
14567c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
14577c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
14587c478bd9Sstevel@tonic-gate 	crfree(cr);
14597c478bd9Sstevel@tonic-gate 	kmem_free(args, sizeof (*args));
14607c478bd9Sstevel@tonic-gate 	return (-1);
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate int
nfs_async_putapage(vnode_t * vp,page_t * pp,u_offset_t off,size_t len,int flags,cred_t * cr,int (* putapage)(vnode_t *,page_t *,u_offset_t,size_t,int,cred_t *))14647c478bd9Sstevel@tonic-gate nfs_async_putapage(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
14657c478bd9Sstevel@tonic-gate     int flags, cred_t *cr, int (*putapage)(vnode_t *, page_t *,
14667c478bd9Sstevel@tonic-gate     u_offset_t, size_t, int, cred_t *))
14677c478bd9Sstevel@tonic-gate {
14687c478bd9Sstevel@tonic-gate 	rnode_t *rp;
14697c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
14707c478bd9Sstevel@tonic-gate 	struct nfs_async_reqs *args;
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	ASSERT(flags & B_ASYNC);
14737c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_vfsp != NULL);
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
14767c478bd9Sstevel@tonic-gate 	ASSERT(rp->r_count > 0);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	/*
14817c478bd9Sstevel@tonic-gate 	 * If we can't allocate a request structure, do the putpage
14827c478bd9Sstevel@tonic-gate 	 * operation synchronously in this thread's context.
14837c478bd9Sstevel@tonic-gate 	 */
14847c478bd9Sstevel@tonic-gate 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
14857c478bd9Sstevel@tonic-gate 		goto noasync;
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	args->a_next = NULL;
14887c478bd9Sstevel@tonic-gate #ifdef DEBUG
14897c478bd9Sstevel@tonic-gate 	args->a_queuer = curthread;
14907c478bd9Sstevel@tonic-gate #endif
14917c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
14927c478bd9Sstevel@tonic-gate 	args->a_vp = vp;
14937c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
14947c478bd9Sstevel@tonic-gate 	crhold(cr);
14957c478bd9Sstevel@tonic-gate 	args->a_cred = cr;
14967c478bd9Sstevel@tonic-gate 	args->a_io = NFS_PUTAPAGE;
14977c478bd9Sstevel@tonic-gate 	args->a_nfs_putapage = putapage;
14987c478bd9Sstevel@tonic-gate 	args->a_nfs_pp = pp;
14997c478bd9Sstevel@tonic-gate 	args->a_nfs_off = off;
15007c478bd9Sstevel@tonic-gate 	args->a_nfs_len = (uint_t)len;
15017c478bd9Sstevel@tonic-gate 	args->a_nfs_flags = flags;
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	/*
15067c478bd9Sstevel@tonic-gate 	 * If asyncio has been disabled, then make a synchronous request.
15077c478bd9Sstevel@tonic-gate 	 * This check is done a second time in case async io was diabled
15087c478bd9Sstevel@tonic-gate 	 * while this thread was blocked waiting for memory pressure to
15097c478bd9Sstevel@tonic-gate 	 * reduce or for the queue to drain.
15107c478bd9Sstevel@tonic-gate 	 */
15117c478bd9Sstevel@tonic-gate 	if (mi->mi_max_threads == 0) {
15127c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
15137c478bd9Sstevel@tonic-gate 		goto noasync;
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	/*
15177c478bd9Sstevel@tonic-gate 	 * Link request structure into the async list and
15187c478bd9Sstevel@tonic-gate 	 * wakeup async thread to do the i/o.
15197c478bd9Sstevel@tonic-gate 	 */
15207c478bd9Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_PUTAPAGE] == NULL) {
15217c478bd9Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_PUTAPAGE] = args;
15227c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PUTAPAGE] = args;
15237c478bd9Sstevel@tonic-gate 	} else {
15247c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PUTAPAGE]->a_next = args;
15257c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PUTAPAGE] = args;
15267c478bd9Sstevel@tonic-gate 	}
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
15297c478bd9Sstevel@tonic-gate 	rp->r_count++;
15307c478bd9Sstevel@tonic-gate 	rp->r_awcount++;
15317c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
15347c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
15357c478bd9Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
15367c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	mi->mi_async_req_count++;
15407c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
15417c478bd9Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
15427c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
15437c478bd9Sstevel@tonic-gate 	return (0);
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate noasync:
15467c478bd9Sstevel@tonic-gate 	if (args != NULL) {
15477c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
15487c478bd9Sstevel@tonic-gate 		crfree(cr);
15497c478bd9Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
15507c478bd9Sstevel@tonic-gate 	}
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	if (curproc == proc_pageout || curproc == proc_fsflush) {
15537c478bd9Sstevel@tonic-gate 		/*
15547c478bd9Sstevel@tonic-gate 		 * If we get here in the context of the pageout/fsflush,
15557c478bd9Sstevel@tonic-gate 		 * we refuse to do a sync write, because this may hang
15567c478bd9Sstevel@tonic-gate 		 * pageout (and the machine). In this case, we just
15577c478bd9Sstevel@tonic-gate 		 * re-mark the page as dirty and punt on the page.
15587c478bd9Sstevel@tonic-gate 		 *
15597c478bd9Sstevel@tonic-gate 		 * Make sure B_FORCE isn't set.  We can re-mark the
15607c478bd9Sstevel@tonic-gate 		 * pages as dirty and unlock the pages in one swoop by
15617c478bd9Sstevel@tonic-gate 		 * passing in B_ERROR to pvn_write_done().  However,
15627c478bd9Sstevel@tonic-gate 		 * we should make sure B_FORCE isn't set - we don't
15637c478bd9Sstevel@tonic-gate 		 * want the page tossed before it gets written out.
15647c478bd9Sstevel@tonic-gate 		 */
15657c478bd9Sstevel@tonic-gate 		if (flags & B_FORCE)
15667c478bd9Sstevel@tonic-gate 			flags &= ~(B_INVAL | B_FORCE);
15677c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, flags | B_ERROR);
15687c478bd9Sstevel@tonic-gate 		return (0);
15697c478bd9Sstevel@tonic-gate 	}
1570108322fbScarlsonj 	if (nfs_zone() != mi->mi_zone) {
15717c478bd9Sstevel@tonic-gate 		/*
15727c478bd9Sstevel@tonic-gate 		 * So this was a cross-zone sync putpage.  We pass in B_ERROR
15737c478bd9Sstevel@tonic-gate 		 * to pvn_write_done() to re-mark the pages as dirty and unlock
15747c478bd9Sstevel@tonic-gate 		 * them.
15757c478bd9Sstevel@tonic-gate 		 *
15767c478bd9Sstevel@tonic-gate 		 * We don't want to clear B_FORCE here as the caller presumably
15777c478bd9Sstevel@tonic-gate 		 * knows what they're doing if they set it.
15787c478bd9Sstevel@tonic-gate 		 */
15797c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, flags | B_ERROR);
15807c478bd9Sstevel@tonic-gate 		return (EPERM);
15817c478bd9Sstevel@tonic-gate 	}
15827c478bd9Sstevel@tonic-gate 	return ((*putapage)(vp, pp, off, len, flags, cr));
15837c478bd9Sstevel@tonic-gate }
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate int
nfs_async_pageio(vnode_t * vp,page_t * pp,u_offset_t io_off,size_t io_len,int flags,cred_t * cr,int (* pageio)(vnode_t *,page_t *,u_offset_t,size_t,int,cred_t *))15867c478bd9Sstevel@tonic-gate nfs_async_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
15877c478bd9Sstevel@tonic-gate     int flags, cred_t *cr, int (*pageio)(vnode_t *, page_t *, u_offset_t,
15887c478bd9Sstevel@tonic-gate     size_t, int, cred_t *))
15897c478bd9Sstevel@tonic-gate {
15907c478bd9Sstevel@tonic-gate 	rnode_t *rp;
15917c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
15927c478bd9Sstevel@tonic-gate 	struct nfs_async_reqs *args;
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	ASSERT(flags & B_ASYNC);
15957c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_vfsp != NULL);
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
15987c478bd9Sstevel@tonic-gate 	ASSERT(rp->r_count > 0);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	/*
16037c478bd9Sstevel@tonic-gate 	 * If we can't allocate a request structure, do the pageio
16047c478bd9Sstevel@tonic-gate 	 * request synchronously in this thread's context.
16057c478bd9Sstevel@tonic-gate 	 */
16067c478bd9Sstevel@tonic-gate 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
16077c478bd9Sstevel@tonic-gate 		goto noasync;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	args->a_next = NULL;
16107c478bd9Sstevel@tonic-gate #ifdef DEBUG
16117c478bd9Sstevel@tonic-gate 	args->a_queuer = curthread;
16127c478bd9Sstevel@tonic-gate #endif
16137c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
16147c478bd9Sstevel@tonic-gate 	args->a_vp = vp;
16157c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
16167c478bd9Sstevel@tonic-gate 	crhold(cr);
16177c478bd9Sstevel@tonic-gate 	args->a_cred = cr;
16187c478bd9Sstevel@tonic-gate 	args->a_io = NFS_PAGEIO;
16197c478bd9Sstevel@tonic-gate 	args->a_nfs_pageio = pageio;
16207c478bd9Sstevel@tonic-gate 	args->a_nfs_pp = pp;
16217c478bd9Sstevel@tonic-gate 	args->a_nfs_off = io_off;
16227c478bd9Sstevel@tonic-gate 	args->a_nfs_len = (uint_t)io_len;
16237c478bd9Sstevel@tonic-gate 	args->a_nfs_flags = flags;
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	/*
16287c478bd9Sstevel@tonic-gate 	 * If asyncio has been disabled, then make a synchronous request.
16297c478bd9Sstevel@tonic-gate 	 * This check is done a second time in case async io was diabled
16307c478bd9Sstevel@tonic-gate 	 * while this thread was blocked waiting for memory pressure to
16317c478bd9Sstevel@tonic-gate 	 * reduce or for the queue to drain.
16327c478bd9Sstevel@tonic-gate 	 */
16337c478bd9Sstevel@tonic-gate 	if (mi->mi_max_threads == 0) {
16347c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
16357c478bd9Sstevel@tonic-gate 		goto noasync;
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	/*
16397c478bd9Sstevel@tonic-gate 	 * Link request structure into the async list and
16407c478bd9Sstevel@tonic-gate 	 * wakeup async thread to do the i/o.
16417c478bd9Sstevel@tonic-gate 	 */
16427c478bd9Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_PAGEIO] == NULL) {
16437c478bd9Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_PAGEIO] = args;
16447c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PAGEIO] = args;
16457c478bd9Sstevel@tonic-gate 	} else {
16467c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PAGEIO]->a_next = args;
16477c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PAGEIO] = args;
16487c478bd9Sstevel@tonic-gate 	}
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
16517c478bd9Sstevel@tonic-gate 	rp->r_count++;
16527c478bd9Sstevel@tonic-gate 	rp->r_awcount++;
16537c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
16567c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
16577c478bd9Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
16587c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
16597c478bd9Sstevel@tonic-gate 	}
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	mi->mi_async_req_count++;
16627c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
16637c478bd9Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
16647c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
16657c478bd9Sstevel@tonic-gate 	return (0);
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate noasync:
16687c478bd9Sstevel@tonic-gate 	if (args != NULL) {
16697c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
16707c478bd9Sstevel@tonic-gate 		crfree(cr);
16717c478bd9Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
16727c478bd9Sstevel@tonic-gate 	}
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	/*
16757c478bd9Sstevel@tonic-gate 	 * If we can't do it ASYNC, for reads we do nothing (but cleanup
16767c478bd9Sstevel@tonic-gate 	 * the page list), for writes we do it synchronously, except for
16777c478bd9Sstevel@tonic-gate 	 * proc_pageout/proc_fsflush as described below.
16787c478bd9Sstevel@tonic-gate 	 */
16797c478bd9Sstevel@tonic-gate 	if (flags & B_READ) {
16807c478bd9Sstevel@tonic-gate 		pvn_read_done(pp, flags | B_ERROR);
16817c478bd9Sstevel@tonic-gate 		return (0);
16827c478bd9Sstevel@tonic-gate 	}
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	if (curproc == proc_pageout || curproc == proc_fsflush) {
16857c478bd9Sstevel@tonic-gate 		/*
16867c478bd9Sstevel@tonic-gate 		 * If we get here in the context of the pageout/fsflush,
16877c478bd9Sstevel@tonic-gate 		 * we refuse to do a sync write, because this may hang
16887c478bd9Sstevel@tonic-gate 		 * pageout/fsflush (and the machine). In this case, we just
16897c478bd9Sstevel@tonic-gate 		 * re-mark the page as dirty and punt on the page.
16907c478bd9Sstevel@tonic-gate 		 *
16917c478bd9Sstevel@tonic-gate 		 * Make sure B_FORCE isn't set.  We can re-mark the
16927c478bd9Sstevel@tonic-gate 		 * pages as dirty and unlock the pages in one swoop by
16937c478bd9Sstevel@tonic-gate 		 * passing in B_ERROR to pvn_write_done().  However,
16947c478bd9Sstevel@tonic-gate 		 * we should make sure B_FORCE isn't set - we don't
16957c478bd9Sstevel@tonic-gate 		 * want the page tossed before it gets written out.
16967c478bd9Sstevel@tonic-gate 		 */
16977c478bd9Sstevel@tonic-gate 		if (flags & B_FORCE)
16987c478bd9Sstevel@tonic-gate 			flags &= ~(B_INVAL | B_FORCE);
16997c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, flags | B_ERROR);
17007c478bd9Sstevel@tonic-gate 		return (0);
17017c478bd9Sstevel@tonic-gate 	}
17027c478bd9Sstevel@tonic-gate 
1703108322fbScarlsonj 	if (nfs_zone() != mi->mi_zone) {
17047c478bd9Sstevel@tonic-gate 		/*
17057c478bd9Sstevel@tonic-gate 		 * So this was a cross-zone sync pageio.  We pass in B_ERROR
17067c478bd9Sstevel@tonic-gate 		 * to pvn_write_done() to re-mark the pages as dirty and unlock
17077c478bd9Sstevel@tonic-gate 		 * them.
17087c478bd9Sstevel@tonic-gate 		 *
17097c478bd9Sstevel@tonic-gate 		 * We don't want to clear B_FORCE here as the caller presumably
17107c478bd9Sstevel@tonic-gate 		 * knows what they're doing if they set it.
17117c478bd9Sstevel@tonic-gate 		 */
17127c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, flags | B_ERROR);
17137c478bd9Sstevel@tonic-gate 		return (EPERM);
17147c478bd9Sstevel@tonic-gate 	}
17157c478bd9Sstevel@tonic-gate 	return ((*pageio)(vp, pp, io_off, io_len, flags, cr));
17167c478bd9Sstevel@tonic-gate }
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate void
nfs_async_readdir(vnode_t * vp,rddir_cache * rdc,cred_t * cr,int (* readdir)(vnode_t *,rddir_cache *,cred_t *))17197c478bd9Sstevel@tonic-gate nfs_async_readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr,
17207c478bd9Sstevel@tonic-gate     int (*readdir)(vnode_t *, rddir_cache *, cred_t *))
17217c478bd9Sstevel@tonic-gate {
17227c478bd9Sstevel@tonic-gate 	rnode_t *rp;
17237c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
17247c478bd9Sstevel@tonic-gate 	struct nfs_async_reqs *args;
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
17277c478bd9Sstevel@tonic-gate 	ASSERT(rp->r_freef == NULL);
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	/*
17327c478bd9Sstevel@tonic-gate 	 * If we can't allocate a request structure, do the readdir
17337c478bd9Sstevel@tonic-gate 	 * operation synchronously in this thread's context.
17347c478bd9Sstevel@tonic-gate 	 */
17357c478bd9Sstevel@tonic-gate 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
17367c478bd9Sstevel@tonic-gate 		goto noasync;
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	args->a_next = NULL;
17397c478bd9Sstevel@tonic-gate #ifdef DEBUG
17407c478bd9Sstevel@tonic-gate 	args->a_queuer = curthread;
17417c478bd9Sstevel@tonic-gate #endif
17427c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
17437c478bd9Sstevel@tonic-gate 	args->a_vp = vp;
17447c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
17457c478bd9Sstevel@tonic-gate 	crhold(cr);
17467c478bd9Sstevel@tonic-gate 	args->a_cred = cr;
17477c478bd9Sstevel@tonic-gate 	args->a_io = NFS_READDIR;
17487c478bd9Sstevel@tonic-gate 	args->a_nfs_readdir = readdir;
17497c478bd9Sstevel@tonic-gate 	args->a_nfs_rdc = rdc;
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	/*
17547c478bd9Sstevel@tonic-gate 	 * If asyncio has been disabled, then make a synchronous request.
17557c478bd9Sstevel@tonic-gate 	 */
17567c478bd9Sstevel@tonic-gate 	if (mi->mi_max_threads == 0) {
17577c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
17587c478bd9Sstevel@tonic-gate 		goto noasync;
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	/*
17627c478bd9Sstevel@tonic-gate 	 * Link request structure into the async list and
17637c478bd9Sstevel@tonic-gate 	 * wakeup async thread to do the i/o.
17647c478bd9Sstevel@tonic-gate 	 */
17657c478bd9Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_READDIR] == NULL) {
17667c478bd9Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_READDIR] = args;
17677c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READDIR] = args;
17687c478bd9Sstevel@tonic-gate 	} else {
17697c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READDIR]->a_next = args;
17707c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READDIR] = args;
17717c478bd9Sstevel@tonic-gate 	}
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
17747c478bd9Sstevel@tonic-gate 	rp->r_count++;
17757c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
17787c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
17797c478bd9Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
17807c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
17817c478bd9Sstevel@tonic-gate 	}
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	mi->mi_async_req_count++;
17847c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
17857c478bd9Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
17867c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
17877c478bd9Sstevel@tonic-gate 	return;
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate noasync:
17907c478bd9Sstevel@tonic-gate 	if (args != NULL) {
17917c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
17927c478bd9Sstevel@tonic-gate 		crfree(cr);
17937c478bd9Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
17947c478bd9Sstevel@tonic-gate 	}
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	rdc->entries = NULL;
17977c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
17987c478bd9Sstevel@tonic-gate 	ASSERT(rdc->flags & RDDIR);
17997c478bd9Sstevel@tonic-gate 	rdc->flags &= ~RDDIR;
18007c478bd9Sstevel@tonic-gate 	rdc->flags |= RDDIRREQ;
18017c478bd9Sstevel@tonic-gate 	/*
18027c478bd9Sstevel@tonic-gate 	 * Check the flag to see if RDDIRWAIT is set. If RDDIRWAIT
18037c478bd9Sstevel@tonic-gate 	 * is set, wakeup the thread sleeping in cv_wait_sig().
18047c478bd9Sstevel@tonic-gate 	 * The woken up thread will reset the flag to RDDIR and will
18057c478bd9Sstevel@tonic-gate 	 * continue with the readdir opeartion.
18067c478bd9Sstevel@tonic-gate 	 */
18077c478bd9Sstevel@tonic-gate 	if (rdc->flags & RDDIRWAIT) {
18087c478bd9Sstevel@tonic-gate 		rdc->flags &= ~RDDIRWAIT;
18097c478bd9Sstevel@tonic-gate 		cv_broadcast(&rdc->cv);
18107c478bd9Sstevel@tonic-gate 	}
18117c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
18127c478bd9Sstevel@tonic-gate 	rddir_cache_rele(rdc);
18137c478bd9Sstevel@tonic-gate }
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate void
nfs_async_commit(vnode_t * vp,page_t * plist,offset3 offset,count3 count,cred_t * cr,void (* commit)(vnode_t *,page_t *,offset3,count3,cred_t *))18167c478bd9Sstevel@tonic-gate nfs_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
1817e010bda9SMarcel Telka     cred_t *cr, void (*commit)(vnode_t *, page_t *, offset3, count3, cred_t *))
18187c478bd9Sstevel@tonic-gate {
18197c478bd9Sstevel@tonic-gate 	rnode_t *rp;
18207c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
18217c478bd9Sstevel@tonic-gate 	struct nfs_async_reqs *args;
18227c478bd9Sstevel@tonic-gate 	page_t *pp;
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
18257c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	/*
18287c478bd9Sstevel@tonic-gate 	 * If we can't allocate a request structure, do the commit
18297c478bd9Sstevel@tonic-gate 	 * operation synchronously in this thread's context.
18307c478bd9Sstevel@tonic-gate 	 */
18317c478bd9Sstevel@tonic-gate 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
18327c478bd9Sstevel@tonic-gate 		goto noasync;
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	args->a_next = NULL;
18357c478bd9Sstevel@tonic-gate #ifdef DEBUG
18367c478bd9Sstevel@tonic-gate 	args->a_queuer = curthread;
18377c478bd9Sstevel@tonic-gate #endif
18387c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
18397c478bd9Sstevel@tonic-gate 	args->a_vp = vp;
18407c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
18417c478bd9Sstevel@tonic-gate 	crhold(cr);
18427c478bd9Sstevel@tonic-gate 	args->a_cred = cr;
18437c478bd9Sstevel@tonic-gate 	args->a_io = NFS_COMMIT;
18447c478bd9Sstevel@tonic-gate 	args->a_nfs_commit = commit;
18457c478bd9Sstevel@tonic-gate 	args->a_nfs_plist = plist;
18467c478bd9Sstevel@tonic-gate 	args->a_nfs_offset = offset;
18477c478bd9Sstevel@tonic-gate 	args->a_nfs_count = count;
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	/*
18527c478bd9Sstevel@tonic-gate 	 * If asyncio has been disabled, then make a synchronous request.
18537c478bd9Sstevel@tonic-gate 	 * This check is done a second time in case async io was diabled
18547c478bd9Sstevel@tonic-gate 	 * while this thread was blocked waiting for memory pressure to
18557c478bd9Sstevel@tonic-gate 	 * reduce or for the queue to drain.
18567c478bd9Sstevel@tonic-gate 	 */
18577c478bd9Sstevel@tonic-gate 	if (mi->mi_max_threads == 0) {
18587c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
18597c478bd9Sstevel@tonic-gate 		goto noasync;
18607c478bd9Sstevel@tonic-gate 	}
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	/*
18637c478bd9Sstevel@tonic-gate 	 * Link request structure into the async list and
18647c478bd9Sstevel@tonic-gate 	 * wakeup async thread to do the i/o.
18657c478bd9Sstevel@tonic-gate 	 */
18667c478bd9Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_COMMIT] == NULL) {
18677c478bd9Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_COMMIT] = args;
18687c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_COMMIT] = args;
18697c478bd9Sstevel@tonic-gate 	} else {
18707c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_COMMIT]->a_next = args;
18717c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_COMMIT] = args;
18727c478bd9Sstevel@tonic-gate 	}
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
18757c478bd9Sstevel@tonic-gate 	rp->r_count++;
18767c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
18797c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
18807c478bd9Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
18817c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
18827c478bd9Sstevel@tonic-gate 	}
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	mi->mi_async_req_count++;
18857c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
18867c478bd9Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
18877c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
18887c478bd9Sstevel@tonic-gate 	return;
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate noasync:
18917c478bd9Sstevel@tonic-gate 	if (args != NULL) {
18927c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
18937c478bd9Sstevel@tonic-gate 		crfree(cr);
18947c478bd9Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
18957c478bd9Sstevel@tonic-gate 	}
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	if (curproc == proc_pageout || curproc == proc_fsflush ||
1898108322fbScarlsonj 	    nfs_zone() != mi->mi_zone) {
18997c478bd9Sstevel@tonic-gate 		while (plist != NULL) {
19007c478bd9Sstevel@tonic-gate 			pp = plist;
19017c478bd9Sstevel@tonic-gate 			page_sub(&plist, pp);
19027c478bd9Sstevel@tonic-gate 			pp->p_fsdata = C_COMMIT;
19037c478bd9Sstevel@tonic-gate 			page_unlock(pp);
19047c478bd9Sstevel@tonic-gate 		}
19057c478bd9Sstevel@tonic-gate 		return;
19067c478bd9Sstevel@tonic-gate 	}
19077c478bd9Sstevel@tonic-gate 	(*commit)(vp, plist, offset, count, cr);
19087c478bd9Sstevel@tonic-gate }
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate void
nfs_async_inactive(vnode_t * vp,cred_t * cr,void (* inactive)(vnode_t *,cred_t *,caller_context_t *))19117c478bd9Sstevel@tonic-gate nfs_async_inactive(vnode_t *vp, cred_t *cr,
1912da6c28aaSamw     void (*inactive)(vnode_t *, cred_t *, caller_context_t *))
19137c478bd9Sstevel@tonic-gate {
19147c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
19157c478bd9Sstevel@tonic-gate 	struct nfs_async_reqs *args;
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	args = kmem_alloc(sizeof (*args), KM_SLEEP);
19207c478bd9Sstevel@tonic-gate 	args->a_next = NULL;
19217c478bd9Sstevel@tonic-gate #ifdef DEBUG
19227c478bd9Sstevel@tonic-gate 	args->a_queuer = curthread;
19237c478bd9Sstevel@tonic-gate #endif
19247c478bd9Sstevel@tonic-gate 	args->a_vp = vp;
19257c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
19267c478bd9Sstevel@tonic-gate 	crhold(cr);
19277c478bd9Sstevel@tonic-gate 	args->a_cred = cr;
19287c478bd9Sstevel@tonic-gate 	args->a_io = NFS_INACTIVE;
19297c478bd9Sstevel@tonic-gate 	args->a_nfs_inactive = inactive;
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	/*
19327c478bd9Sstevel@tonic-gate 	 * Note that we don't check mi->mi_max_threads here, since we
19337c478bd9Sstevel@tonic-gate 	 * *need* to get rid of this vnode regardless of whether someone
19347c478bd9Sstevel@tonic-gate 	 * set nfs3_max_threads/nfs_max_threads to zero in /etc/system.
19357c478bd9Sstevel@tonic-gate 	 *
19367c478bd9Sstevel@tonic-gate 	 * The manager thread knows about this and is willing to create
1937da6c28aaSamw 	 * at least one thread to accommodate us.
19387c478bd9Sstevel@tonic-gate 	 */
19397c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
19407c478bd9Sstevel@tonic-gate 	if (mi->mi_manager_thread == NULL) {
19417c478bd9Sstevel@tonic-gate 		rnode_t *rp = VTOR(vp);
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
19447c478bd9Sstevel@tonic-gate 		crfree(cr);	/* drop our reference */
19457c478bd9Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
19467c478bd9Sstevel@tonic-gate 		/*
19477c478bd9Sstevel@tonic-gate 		 * We can't do an over-the-wire call since we're in the wrong
19487c478bd9Sstevel@tonic-gate 		 * zone, so we need to clean up state as best we can and then
19497c478bd9Sstevel@tonic-gate 		 * throw away the vnode.
19507c478bd9Sstevel@tonic-gate 		 */
19517c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
19527c478bd9Sstevel@tonic-gate 		if (rp->r_unldvp != NULL) {
19537c478bd9Sstevel@tonic-gate 			vnode_t *unldvp;
19547c478bd9Sstevel@tonic-gate 			char *unlname;
19557c478bd9Sstevel@tonic-gate 			cred_t *unlcred;
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 			unldvp = rp->r_unldvp;
19587c478bd9Sstevel@tonic-gate 			rp->r_unldvp = NULL;
19597c478bd9Sstevel@tonic-gate 			unlname = rp->r_unlname;
19607c478bd9Sstevel@tonic-gate 			rp->r_unlname = NULL;
19617c478bd9Sstevel@tonic-gate 			unlcred = rp->r_unlcred;
19627c478bd9Sstevel@tonic-gate 			rp->r_unlcred = NULL;
19637c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 			VN_RELE(unldvp);
19667c478bd9Sstevel@tonic-gate 			kmem_free(unlname, MAXNAMELEN);
19677c478bd9Sstevel@tonic-gate 			crfree(unlcred);
19687c478bd9Sstevel@tonic-gate 		} else {
19697c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
19707c478bd9Sstevel@tonic-gate 		}
19717c478bd9Sstevel@tonic-gate 		/*
19727c478bd9Sstevel@tonic-gate 		 * No need to explicitly throw away any cached pages.  The
19737c478bd9Sstevel@tonic-gate 		 * eventual rinactive() will attempt a synchronous
19747c478bd9Sstevel@tonic-gate 		 * VOP_PUTPAGE() which will immediately fail since the request
19757c478bd9Sstevel@tonic-gate 		 * is coming from the wrong zone, and then will proceed to call
19767c478bd9Sstevel@tonic-gate 		 * nfs_invalidate_pages() which will clean things up for us.
19777c478bd9Sstevel@tonic-gate 		 */
19787c478bd9Sstevel@tonic-gate 		rp_addfree(VTOR(vp), cr);
19797c478bd9Sstevel@tonic-gate 		return;
19807c478bd9Sstevel@tonic-gate 	}
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_INACTIVE] == NULL) {
19837c478bd9Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_INACTIVE] = args;
19847c478bd9Sstevel@tonic-gate 	} else {
19857c478bd9Sstevel@tonic-gate 		mi->mi_async_tail[NFS_INACTIVE]->a_next = args;
19867c478bd9Sstevel@tonic-gate 	}
19877c478bd9Sstevel@tonic-gate 	mi->mi_async_tail[NFS_INACTIVE] = args;
19887c478bd9Sstevel@tonic-gate 	/*
19897c478bd9Sstevel@tonic-gate 	 * Don't increment r_count, since we're trying to get rid of the vnode.
19907c478bd9Sstevel@tonic-gate 	 */
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	mi->mi_async_req_count++;
19937c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
19947c478bd9Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
19957c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
19967c478bd9Sstevel@tonic-gate }
19977c478bd9Sstevel@tonic-gate 
19980776f5e6SVallish Vaidyeshwara static void
nfs_async_start(struct vfs * vfsp)19990776f5e6SVallish Vaidyeshwara nfs_async_start(struct vfs *vfsp)
20000776f5e6SVallish Vaidyeshwara {
20010776f5e6SVallish Vaidyeshwara 	nfs_async_common_start(vfsp, NFS_ASYNC_QUEUE);
20020776f5e6SVallish Vaidyeshwara }
20030776f5e6SVallish Vaidyeshwara 
20040776f5e6SVallish Vaidyeshwara static void
nfs_async_pgops_start(struct vfs * vfsp)20050776f5e6SVallish Vaidyeshwara nfs_async_pgops_start(struct vfs *vfsp)
20060776f5e6SVallish Vaidyeshwara {
20070776f5e6SVallish Vaidyeshwara 	nfs_async_common_start(vfsp, NFS_ASYNC_PGOPS_QUEUE);
20080776f5e6SVallish Vaidyeshwara }
20090776f5e6SVallish Vaidyeshwara 
20107c478bd9Sstevel@tonic-gate /*
20117c478bd9Sstevel@tonic-gate  * The async queues for each mounted file system are arranged as a
20127c478bd9Sstevel@tonic-gate  * set of queues, one for each async i/o type.  Requests are taken
20137c478bd9Sstevel@tonic-gate  * from the queues in a round-robin fashion.  A number of consecutive
20147c478bd9Sstevel@tonic-gate  * requests are taken from each queue before moving on to the next
20157c478bd9Sstevel@tonic-gate  * queue.  This functionality may allow the NFS Version 2 server to do
20167c478bd9Sstevel@tonic-gate  * write clustering, even if the client is mixing writes and reads
20177c478bd9Sstevel@tonic-gate  * because it will take multiple write requests from the queue
20187c478bd9Sstevel@tonic-gate  * before processing any of the other async i/o types.
20197c478bd9Sstevel@tonic-gate  *
20200776f5e6SVallish Vaidyeshwara  * XXX The nfs_async_common_start thread is unsafe in the light of the present
20217c478bd9Sstevel@tonic-gate  * model defined by cpr to suspend the system. Specifically over the
20227c478bd9Sstevel@tonic-gate  * wire calls are cpr-unsafe. The thread should be reevaluated in
20237c478bd9Sstevel@tonic-gate  * case of future updates to the cpr model.
20247c478bd9Sstevel@tonic-gate  */
20257c478bd9Sstevel@tonic-gate static void
nfs_async_common_start(struct vfs * vfsp,int async_queue)20260776f5e6SVallish Vaidyeshwara nfs_async_common_start(struct vfs *vfsp, int async_queue)
20277c478bd9Sstevel@tonic-gate {
20287c478bd9Sstevel@tonic-gate 	struct nfs_async_reqs *args;
20297c478bd9Sstevel@tonic-gate 	mntinfo_t *mi = VFTOMI(vfsp);
20307c478bd9Sstevel@tonic-gate 	clock_t time_left = 1;
20317c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
20327c478bd9Sstevel@tonic-gate 	int i;
20330776f5e6SVallish Vaidyeshwara 	int async_types;
20340776f5e6SVallish Vaidyeshwara 	kcondvar_t *async_work_cv;
20350776f5e6SVallish Vaidyeshwara 
20360776f5e6SVallish Vaidyeshwara 	if (async_queue == NFS_ASYNC_QUEUE) {
20370776f5e6SVallish Vaidyeshwara 		async_types = NFS_ASYNC_TYPES;
20380776f5e6SVallish Vaidyeshwara 		async_work_cv = &mi->mi_async_work_cv[NFS_ASYNC_QUEUE];
20390776f5e6SVallish Vaidyeshwara 	} else {
20400776f5e6SVallish Vaidyeshwara 		async_types = NFS_ASYNC_PGOPS_TYPES;
20410776f5e6SVallish Vaidyeshwara 		async_work_cv = &mi->mi_async_work_cv[NFS_ASYNC_PGOPS_QUEUE];
20420776f5e6SVallish Vaidyeshwara 	}
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	/*
20457c478bd9Sstevel@tonic-gate 	 * Dynamic initialization of nfs_async_timeout to allow nfs to be
20467c478bd9Sstevel@tonic-gate 	 * built in an implementation independent manner.
20477c478bd9Sstevel@tonic-gate 	 */
20487c478bd9Sstevel@tonic-gate 	if (nfs_async_timeout == -1)
20497c478bd9Sstevel@tonic-gate 		nfs_async_timeout = NFS_ASYNC_TIMEOUT;
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr, "nas");
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
20547c478bd9Sstevel@tonic-gate 	for (;;) {
20557c478bd9Sstevel@tonic-gate 		/*
20567c478bd9Sstevel@tonic-gate 		 * Find the next queue containing an entry.  We start
20577c478bd9Sstevel@tonic-gate 		 * at the current queue pointer and then round robin
20587c478bd9Sstevel@tonic-gate 		 * through all of them until we either find a non-empty
20597c478bd9Sstevel@tonic-gate 		 * queue or have looked through all of them.
20607c478bd9Sstevel@tonic-gate 		 */
20610776f5e6SVallish Vaidyeshwara 		for (i = 0; i < async_types; i++) {
20620776f5e6SVallish Vaidyeshwara 			args = *mi->mi_async_curr[async_queue];
20637c478bd9Sstevel@tonic-gate 			if (args != NULL)
20647c478bd9Sstevel@tonic-gate 				break;
20650776f5e6SVallish Vaidyeshwara 			mi->mi_async_curr[async_queue]++;
20660776f5e6SVallish Vaidyeshwara 			if (mi->mi_async_curr[async_queue] ==
20670776f5e6SVallish Vaidyeshwara 			    &mi->mi_async_reqs[async_types]) {
20680776f5e6SVallish Vaidyeshwara 				mi->mi_async_curr[async_queue] =
20690776f5e6SVallish Vaidyeshwara 				    &mi->mi_async_reqs[0];
20700776f5e6SVallish Vaidyeshwara 			}
20717c478bd9Sstevel@tonic-gate 		}
20727c478bd9Sstevel@tonic-gate 		/*
20737c478bd9Sstevel@tonic-gate 		 * If we didn't find a entry, then block until woken up
20747c478bd9Sstevel@tonic-gate 		 * again and then look through the queues again.
20757c478bd9Sstevel@tonic-gate 		 */
20767c478bd9Sstevel@tonic-gate 		if (args == NULL) {
20777c478bd9Sstevel@tonic-gate 			/*
20787c478bd9Sstevel@tonic-gate 			 * Exiting is considered to be safe for CPR as well
20797c478bd9Sstevel@tonic-gate 			 */
20807c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 			/*
20837c478bd9Sstevel@tonic-gate 			 * Wakeup thread waiting to unmount the file
20847c478bd9Sstevel@tonic-gate 			 * system only if all async threads are inactive.
20857c478bd9Sstevel@tonic-gate 			 *
20867c478bd9Sstevel@tonic-gate 			 * If we've timed-out and there's nothing to do,
20877c478bd9Sstevel@tonic-gate 			 * then get rid of this thread.
20887c478bd9Sstevel@tonic-gate 			 */
20897c478bd9Sstevel@tonic-gate 			if (mi->mi_max_threads == 0 || time_left <= 0) {
20900776f5e6SVallish Vaidyeshwara 				--mi->mi_threads[async_queue];
20910776f5e6SVallish Vaidyeshwara 
20920776f5e6SVallish Vaidyeshwara 				if (mi->mi_threads[NFS_ASYNC_QUEUE] == 0 &&
20930776f5e6SVallish Vaidyeshwara 				    mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE] == 0)
20947c478bd9Sstevel@tonic-gate 					cv_signal(&mi->mi_async_cv);
20957c478bd9Sstevel@tonic-gate 				CALLB_CPR_EXIT(&cprinfo);
20967c478bd9Sstevel@tonic-gate 				VFS_RELE(vfsp);	/* release thread's hold */
20977c478bd9Sstevel@tonic-gate 				zthread_exit();
20987c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
20997c478bd9Sstevel@tonic-gate 			}
21000776f5e6SVallish Vaidyeshwara 			time_left = cv_reltimedwait(async_work_cv,
2101d3d50737SRafael Vanoni 			    &mi->mi_async_lock, nfs_async_timeout,
2102d3d50737SRafael Vanoni 			    TR_CLOCK_TICK);
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock);
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 			continue;
21077c478bd9Sstevel@tonic-gate 		}
21087c478bd9Sstevel@tonic-gate 		time_left = 1;
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 		/*
21117c478bd9Sstevel@tonic-gate 		 * Remove the request from the async queue and then
21127c478bd9Sstevel@tonic-gate 		 * update the current async request queue pointer.  If
21137c478bd9Sstevel@tonic-gate 		 * the current queue is empty or we have removed enough
21147c478bd9Sstevel@tonic-gate 		 * consecutive entries from it, then reset the counter
21157c478bd9Sstevel@tonic-gate 		 * for this queue and then move the current pointer to
21167c478bd9Sstevel@tonic-gate 		 * the next queue.
21177c478bd9Sstevel@tonic-gate 		 */
21180776f5e6SVallish Vaidyeshwara 		*mi->mi_async_curr[async_queue] = args->a_next;
21190776f5e6SVallish Vaidyeshwara 		if (*mi->mi_async_curr[async_queue] == NULL ||
21207c478bd9Sstevel@tonic-gate 		    --mi->mi_async_clusters[args->a_io] == 0) {
21217c478bd9Sstevel@tonic-gate 			mi->mi_async_clusters[args->a_io] =
21227c478bd9Sstevel@tonic-gate 			    mi->mi_async_init_clusters;
21230776f5e6SVallish Vaidyeshwara 			mi->mi_async_curr[async_queue]++;
21240776f5e6SVallish Vaidyeshwara 			if (mi->mi_async_curr[async_queue] ==
21250776f5e6SVallish Vaidyeshwara 			    &mi->mi_async_reqs[async_types]) {
21260776f5e6SVallish Vaidyeshwara 				mi->mi_async_curr[async_queue] =
21270776f5e6SVallish Vaidyeshwara 				    &mi->mi_async_reqs[0];
21280776f5e6SVallish Vaidyeshwara 			}
21297c478bd9Sstevel@tonic-gate 		}
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 		if (args->a_io != NFS_INACTIVE && mi->mi_io_kstats) {
21327c478bd9Sstevel@tonic-gate 			mutex_enter(&mi->mi_lock);
21337c478bd9Sstevel@tonic-gate 			kstat_waitq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
21347c478bd9Sstevel@tonic-gate 			mutex_exit(&mi->mi_lock);
21357c478bd9Sstevel@tonic-gate 		}
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 		/*
21407c478bd9Sstevel@tonic-gate 		 * Obtain arguments from the async request structure.
21417c478bd9Sstevel@tonic-gate 		 */
21427c478bd9Sstevel@tonic-gate 		if (args->a_io == NFS_READ_AHEAD && mi->mi_max_threads > 0) {
21437c478bd9Sstevel@tonic-gate 			(*args->a_nfs_readahead)(args->a_vp, args->a_nfs_blkoff,
2144ba3bce04SSuhasini Peddada 			    args->a_nfs_addr, args->a_nfs_seg,
2145ba3bce04SSuhasini Peddada 			    args->a_cred);
21467c478bd9Sstevel@tonic-gate 		} else if (args->a_io == NFS_PUTAPAGE) {
21477c478bd9Sstevel@tonic-gate 			(void) (*args->a_nfs_putapage)(args->a_vp,
2148ba3bce04SSuhasini Peddada 			    args->a_nfs_pp, args->a_nfs_off,
2149ba3bce04SSuhasini Peddada 			    args->a_nfs_len, args->a_nfs_flags,
2150ba3bce04SSuhasini Peddada 			    args->a_cred);
21517c478bd9Sstevel@tonic-gate 		} else if (args->a_io == NFS_PAGEIO) {
2152ba3bce04SSuhasini Peddada 			(void) (*args->a_nfs_pageio)(args->a_vp,
2153ba3bce04SSuhasini Peddada 			    args->a_nfs_pp, args->a_nfs_off,
2154ba3bce04SSuhasini Peddada 			    args->a_nfs_len, args->a_nfs_flags,
21557c478bd9Sstevel@tonic-gate 			    args->a_cred);
21567c478bd9Sstevel@tonic-gate 		} else if (args->a_io == NFS_READDIR) {
21577c478bd9Sstevel@tonic-gate 			(void) ((*args->a_nfs_readdir)(args->a_vp,
21587c478bd9Sstevel@tonic-gate 			    args->a_nfs_rdc, args->a_cred));
21597c478bd9Sstevel@tonic-gate 		} else if (args->a_io == NFS_COMMIT) {
21607c478bd9Sstevel@tonic-gate 			(*args->a_nfs_commit)(args->a_vp, args->a_nfs_plist,
21617c478bd9Sstevel@tonic-gate 			    args->a_nfs_offset, args->a_nfs_count,
21627c478bd9Sstevel@tonic-gate 			    args->a_cred);
21637c478bd9Sstevel@tonic-gate 		} else if (args->a_io == NFS_INACTIVE) {
2164da6c28aaSamw 			(*args->a_nfs_inactive)(args->a_vp, args->a_cred, NULL);
21657c478bd9Sstevel@tonic-gate 		}
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 		/*
21687c478bd9Sstevel@tonic-gate 		 * Now, release the vnode and free the credentials
21697c478bd9Sstevel@tonic-gate 		 * structure.
21707c478bd9Sstevel@tonic-gate 		 */
21717c478bd9Sstevel@tonic-gate 		free_async_args(args);
21727c478bd9Sstevel@tonic-gate 		/*
21737c478bd9Sstevel@tonic-gate 		 * Reacquire the mutex because it will be needed above.
21747c478bd9Sstevel@tonic-gate 		 */
21757c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_async_lock);
21767c478bd9Sstevel@tonic-gate 	}
21777c478bd9Sstevel@tonic-gate }
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate void
nfs_async_stop(struct vfs * vfsp)21807c478bd9Sstevel@tonic-gate nfs_async_stop(struct vfs *vfsp)
21817c478bd9Sstevel@tonic-gate {
21827c478bd9Sstevel@tonic-gate 	mntinfo_t *mi = VFTOMI(vfsp);
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	/*
21857c478bd9Sstevel@tonic-gate 	 * Wait for all outstanding async operations to complete and for the
21867c478bd9Sstevel@tonic-gate 	 * worker threads to exit.
21877c478bd9Sstevel@tonic-gate 	 */
21887c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
21897c478bd9Sstevel@tonic-gate 	mi->mi_max_threads = 0;
21900776f5e6SVallish Vaidyeshwara 	NFS_WAKEALL_ASYNC_WORKERS(mi->mi_async_work_cv);
21910776f5e6SVallish Vaidyeshwara 	while (mi->mi_threads[NFS_ASYNC_QUEUE] != 0 ||
21920776f5e6SVallish Vaidyeshwara 	    mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE] != 0)
21937c478bd9Sstevel@tonic-gate 		cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
21947c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
21957c478bd9Sstevel@tonic-gate }
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate /*
21987c478bd9Sstevel@tonic-gate  * nfs_async_stop_sig:
21997c478bd9Sstevel@tonic-gate  * Wait for all outstanding putpage operation to complete. If a signal
22007c478bd9Sstevel@tonic-gate  * is deliver we will abort and return non-zero. If we can put all the
22017c478bd9Sstevel@tonic-gate  * pages we will return 0. This routine is called from nfs_unmount and
2202da6c28aaSamw  * nfs3_unmount to make these operations interruptible.
22037c478bd9Sstevel@tonic-gate  */
22047c478bd9Sstevel@tonic-gate int
nfs_async_stop_sig(struct vfs * vfsp)22057c478bd9Sstevel@tonic-gate nfs_async_stop_sig(struct vfs *vfsp)
22067c478bd9Sstevel@tonic-gate {
22077c478bd9Sstevel@tonic-gate 	mntinfo_t *mi = VFTOMI(vfsp);
22087c478bd9Sstevel@tonic-gate 	ushort_t omax;
22097c478bd9Sstevel@tonic-gate 	int rval;
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 	/*
22127c478bd9Sstevel@tonic-gate 	 * Wait for all outstanding async operations to complete and for the
22137c478bd9Sstevel@tonic-gate 	 * worker threads to exit.
22147c478bd9Sstevel@tonic-gate 	 */
22157c478bd9Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
22167c478bd9Sstevel@tonic-gate 	omax = mi->mi_max_threads;
22177c478bd9Sstevel@tonic-gate 	mi->mi_max_threads = 0;
22187c478bd9Sstevel@tonic-gate 	/*
22197c478bd9Sstevel@tonic-gate 	 * Tell all the worker threads to exit.
22207c478bd9Sstevel@tonic-gate 	 */
22210776f5e6SVallish Vaidyeshwara 	NFS_WAKEALL_ASYNC_WORKERS(mi->mi_async_work_cv);
22220776f5e6SVallish Vaidyeshwara 	while (mi->mi_threads[NFS_ASYNC_QUEUE] != 0 ||
22230776f5e6SVallish Vaidyeshwara 	    mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE] != 0) {
22247c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig(&mi->mi_async_cv, &mi->mi_async_lock))
22257c478bd9Sstevel@tonic-gate 			break;
22267c478bd9Sstevel@tonic-gate 	}
22270776f5e6SVallish Vaidyeshwara 	rval = (mi->mi_threads[NFS_ASYNC_QUEUE] != 0 ||
22280776f5e6SVallish Vaidyeshwara 	    mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE]  != 0); /* Interrupted */
22297c478bd9Sstevel@tonic-gate 	if (rval)
22307c478bd9Sstevel@tonic-gate 		mi->mi_max_threads = omax;
22317c478bd9Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	return (rval);
22347c478bd9Sstevel@tonic-gate }
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate int
writerp(rnode_t * rp,caddr_t base,int tcount,struct uio * uio,int pgcreated)22377c478bd9Sstevel@tonic-gate writerp(rnode_t *rp, caddr_t base, int tcount, struct uio *uio, int pgcreated)
22387c478bd9Sstevel@tonic-gate {
22397c478bd9Sstevel@tonic-gate 	int pagecreate;
22407c478bd9Sstevel@tonic-gate 	int n;
22417c478bd9Sstevel@tonic-gate 	int saved_n;
22427c478bd9Sstevel@tonic-gate 	caddr_t saved_base;
22437c478bd9Sstevel@tonic-gate 	u_offset_t offset;
22447c478bd9Sstevel@tonic-gate 	int error;
22457c478bd9Sstevel@tonic-gate 	int sm_error;
2246a5652762Spraks 	vnode_t *vp = RTOV(rp);
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	ASSERT(tcount <= MAXBSIZE && tcount <= uio->uio_resid);
22497c478bd9Sstevel@tonic-gate 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_WRITER));
2250a5652762Spraks 	if (!vpm_enable) {
2251a5652762Spraks 		ASSERT(((uintptr_t)base & MAXBOFFSET) + tcount <= MAXBSIZE);
2252a5652762Spraks 	}
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 	/*
22557c478bd9Sstevel@tonic-gate 	 * Move bytes in at most PAGESIZE chunks. We must avoid
22567c478bd9Sstevel@tonic-gate 	 * spanning pages in uiomove() because page faults may cause
22577c478bd9Sstevel@tonic-gate 	 * the cache to be invalidated out from under us. The r_size is not
22587c478bd9Sstevel@tonic-gate 	 * updated until after the uiomove. If we push the last page of a
22597c478bd9Sstevel@tonic-gate 	 * file before r_size is correct, we will lose the data written past
22607c478bd9Sstevel@tonic-gate 	 * the current (and invalid) r_size.
22617c478bd9Sstevel@tonic-gate 	 */
22627c478bd9Sstevel@tonic-gate 	do {
22637c478bd9Sstevel@tonic-gate 		offset = uio->uio_loffset;
22647c478bd9Sstevel@tonic-gate 		pagecreate = 0;
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 		/*
22677c478bd9Sstevel@tonic-gate 		 * n is the number of bytes required to satisfy the request
22687c478bd9Sstevel@tonic-gate 		 *   or the number of bytes to fill out the page.
22697c478bd9Sstevel@tonic-gate 		 */
2270a5652762Spraks 		n = (int)MIN((PAGESIZE - (offset & PAGEOFFSET)), tcount);
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 		/*
22737c478bd9Sstevel@tonic-gate 		 * Check to see if we can skip reading in the page
22747c478bd9Sstevel@tonic-gate 		 * and just allocate the memory.  We can do this
22757c478bd9Sstevel@tonic-gate 		 * if we are going to rewrite the entire mapping
22767c478bd9Sstevel@tonic-gate 		 * or if we are going to write to or beyond the current
22777c478bd9Sstevel@tonic-gate 		 * end of file from the beginning of the mapping.
22787c478bd9Sstevel@tonic-gate 		 *
22797c478bd9Sstevel@tonic-gate 		 * The read of r_size is now protected by r_statelock.
22807c478bd9Sstevel@tonic-gate 		 */
22817c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
22827c478bd9Sstevel@tonic-gate 		/*
22837c478bd9Sstevel@tonic-gate 		 * When pgcreated is nonzero the caller has already done
22847c478bd9Sstevel@tonic-gate 		 * a segmap_getmapflt with forcefault 0 and S_WRITE. With
22857c478bd9Sstevel@tonic-gate 		 * segkpm this means we already have at least one page
22867c478bd9Sstevel@tonic-gate 		 * created and mapped at base.
22877c478bd9Sstevel@tonic-gate 		 */
22887c478bd9Sstevel@tonic-gate 		pagecreate = pgcreated ||
2289a5652762Spraks 		    ((offset & PAGEOFFSET) == 0 &&
22907c478bd9Sstevel@tonic-gate 		    (n == PAGESIZE || ((offset + n) >= rp->r_size)));
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
2293a5652762Spraks 		if (!vpm_enable && pagecreate) {
22947c478bd9Sstevel@tonic-gate 			/*
22957c478bd9Sstevel@tonic-gate 			 * The last argument tells segmap_pagecreate() to
22967c478bd9Sstevel@tonic-gate 			 * always lock the page, as opposed to sometimes
22977c478bd9Sstevel@tonic-gate 			 * returning with the page locked. This way we avoid a
22987c478bd9Sstevel@tonic-gate 			 * fault on the ensuing uiomove(), but also
22997c478bd9Sstevel@tonic-gate 			 * more importantly (to fix bug 1094402) we can
23007c478bd9Sstevel@tonic-gate 			 * call segmap_fault() to unlock the page in all
23017c478bd9Sstevel@tonic-gate 			 * cases. An alternative would be to modify
23027c478bd9Sstevel@tonic-gate 			 * segmap_pagecreate() to tell us when it is
23037c478bd9Sstevel@tonic-gate 			 * locking a page, but that's a fairly major
23047c478bd9Sstevel@tonic-gate 			 * interface change.
23057c478bd9Sstevel@tonic-gate 			 */
23067c478bd9Sstevel@tonic-gate 			if (pgcreated == 0)
23077c478bd9Sstevel@tonic-gate 				(void) segmap_pagecreate(segkmap, base,
23087c478bd9Sstevel@tonic-gate 				    (uint_t)n, 1);
23097c478bd9Sstevel@tonic-gate 			saved_base = base;
23107c478bd9Sstevel@tonic-gate 			saved_n = n;
23117c478bd9Sstevel@tonic-gate 		}
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 		/*
23147c478bd9Sstevel@tonic-gate 		 * The number of bytes of data in the last page can not
23157c478bd9Sstevel@tonic-gate 		 * be accurately be determined while page is being
23167c478bd9Sstevel@tonic-gate 		 * uiomove'd to and the size of the file being updated.
23177c478bd9Sstevel@tonic-gate 		 * Thus, inform threads which need to know accurately
23187c478bd9Sstevel@tonic-gate 		 * how much data is in the last page of the file.  They
23197c478bd9Sstevel@tonic-gate 		 * will not do the i/o immediately, but will arrange for
23207c478bd9Sstevel@tonic-gate 		 * the i/o to happen later when this modify operation
23217c478bd9Sstevel@tonic-gate 		 * will have finished.
23227c478bd9Sstevel@tonic-gate 		 */
23237c478bd9Sstevel@tonic-gate 		ASSERT(!(rp->r_flags & RMODINPROGRESS));
23247c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
23257c478bd9Sstevel@tonic-gate 		rp->r_flags |= RMODINPROGRESS;
23267c478bd9Sstevel@tonic-gate 		rp->r_modaddr = (offset & MAXBMASK);
23277c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
23287c478bd9Sstevel@tonic-gate 
2329a5652762Spraks 		if (vpm_enable) {
2330a5652762Spraks 			/*
2331a5652762Spraks 			 * Copy data. If new pages are created, part of
2332a5652762Spraks 			 * the page that is not written will be initizliazed
2333a5652762Spraks 			 * with zeros.
2334a5652762Spraks 			 */
2335ba3bce04SSuhasini Peddada 			error = vpm_data_copy(vp, offset, n, uio,
2336ba3bce04SSuhasini Peddada 			    !pagecreate, NULL, 0, S_WRITE);
2337a5652762Spraks 		} else {
23387c478bd9Sstevel@tonic-gate 			error = uiomove(base, n, UIO_WRITE, uio);
2339a5652762Spraks 		}
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 		/*
23427c478bd9Sstevel@tonic-gate 		 * r_size is the maximum number of
23437c478bd9Sstevel@tonic-gate 		 * bytes known to be in the file.
23447c478bd9Sstevel@tonic-gate 		 * Make sure it is at least as high as the
23457c478bd9Sstevel@tonic-gate 		 * first unwritten byte pointed to by uio_loffset.
23467c478bd9Sstevel@tonic-gate 		 */
23477c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
23487c478bd9Sstevel@tonic-gate 		if (rp->r_size < uio->uio_loffset)
23497c478bd9Sstevel@tonic-gate 			rp->r_size = uio->uio_loffset;
23507c478bd9Sstevel@tonic-gate 		rp->r_flags &= ~RMODINPROGRESS;
23517c478bd9Sstevel@tonic-gate 		rp->r_flags |= RDIRTY;
23527c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate 		/* n = # of bytes written */
23557c478bd9Sstevel@tonic-gate 		n = (int)(uio->uio_loffset - offset);
2356a5652762Spraks 
2357a5652762Spraks 		if (!vpm_enable) {
23587c478bd9Sstevel@tonic-gate 			base += n;
2359a5652762Spraks 		}
23607c478bd9Sstevel@tonic-gate 		tcount -= n;
23617c478bd9Sstevel@tonic-gate 		/*
23627c478bd9Sstevel@tonic-gate 		 * If we created pages w/o initializing them completely,
23637c478bd9Sstevel@tonic-gate 		 * we need to zero the part that wasn't set up.
23647c478bd9Sstevel@tonic-gate 		 * This happens on a most EOF write cases and if
23657c478bd9Sstevel@tonic-gate 		 * we had some sort of error during the uiomove.
23667c478bd9Sstevel@tonic-gate 		 */
2367a5652762Spraks 		if (!vpm_enable && pagecreate) {
23687c478bd9Sstevel@tonic-gate 			if ((uio->uio_loffset & PAGEOFFSET) || n == 0)
23697c478bd9Sstevel@tonic-gate 				(void) kzero(base, PAGESIZE - n);
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 			if (pgcreated) {
23727c478bd9Sstevel@tonic-gate 				/*
23737c478bd9Sstevel@tonic-gate 				 * Caller is responsible for this page,
23747c478bd9Sstevel@tonic-gate 				 * it was not created in this loop.
23757c478bd9Sstevel@tonic-gate 				 */
23767c478bd9Sstevel@tonic-gate 				pgcreated = 0;
23777c478bd9Sstevel@tonic-gate 			} else {
23787c478bd9Sstevel@tonic-gate 				/*
23797c478bd9Sstevel@tonic-gate 				 * For bug 1094402: segmap_pagecreate locks
23807c478bd9Sstevel@tonic-gate 				 * page. Unlock it. This also unlocks the
23817c478bd9Sstevel@tonic-gate 				 * pages allocated by page_create_va() in
23827c478bd9Sstevel@tonic-gate 				 * segmap_pagecreate().
23837c478bd9Sstevel@tonic-gate 				 */
23847c478bd9Sstevel@tonic-gate 				sm_error = segmap_fault(kas.a_hat, segkmap,
2385ba3bce04SSuhasini Peddada 				    saved_base, saved_n,
2386ba3bce04SSuhasini Peddada 				    F_SOFTUNLOCK, S_WRITE);
23877c478bd9Sstevel@tonic-gate 				if (error == 0)
23887c478bd9Sstevel@tonic-gate 					error = sm_error;
23897c478bd9Sstevel@tonic-gate 			}
23907c478bd9Sstevel@tonic-gate 		}
23917c478bd9Sstevel@tonic-gate 	} while (tcount > 0 && error == 0);
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 	return (error);
23947c478bd9Sstevel@tonic-gate }
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate int
nfs_putpages(vnode_t * vp,u_offset_t off,size_t len,int flags,cred_t * cr)23977c478bd9Sstevel@tonic-gate nfs_putpages(vnode_t *vp, u_offset_t off, size_t len, int flags, cred_t *cr)
23987c478bd9Sstevel@tonic-gate {
23997c478bd9Sstevel@tonic-gate 	rnode_t *rp;
24007c478bd9Sstevel@tonic-gate 	page_t *pp;
24017c478bd9Sstevel@tonic-gate 	u_offset_t eoff;
24027c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
24037c478bd9Sstevel@tonic-gate 	size_t io_len;
24047c478bd9Sstevel@tonic-gate 	int error;
24057c478bd9Sstevel@tonic-gate 	int rdirty;
24067c478bd9Sstevel@tonic-gate 	int err;
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
24097c478bd9Sstevel@tonic-gate 	ASSERT(rp->r_count > 0);
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate 	if (!vn_has_cached_data(vp))
24127c478bd9Sstevel@tonic-gate 		return (0);
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type != VCHR);
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	/*
24177c478bd9Sstevel@tonic-gate 	 * If ROUTOFSPACE is set, then all writes turn into B_INVAL
24187c478bd9Sstevel@tonic-gate 	 * writes.  B_FORCE is set to force the VM system to actually
24197c478bd9Sstevel@tonic-gate 	 * invalidate the pages, even if the i/o failed.  The pages
24207c478bd9Sstevel@tonic-gate 	 * need to get invalidated because they can't be written out
24217c478bd9Sstevel@tonic-gate 	 * because there isn't any space left on either the server's
24227c478bd9Sstevel@tonic-gate 	 * file system or in the user's disk quota.  The B_FREE bit
24237c478bd9Sstevel@tonic-gate 	 * is cleared to avoid confusion as to whether this is a
24247c478bd9Sstevel@tonic-gate 	 * request to place the page on the freelist or to destroy
24257c478bd9Sstevel@tonic-gate 	 * it.
24267c478bd9Sstevel@tonic-gate 	 */
24277c478bd9Sstevel@tonic-gate 	if ((rp->r_flags & ROUTOFSPACE) ||
24287c478bd9Sstevel@tonic-gate 	    (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED))
24297c478bd9Sstevel@tonic-gate 		flags = (flags & ~B_FREE) | B_INVAL | B_FORCE;
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	if (len == 0) {
24327c478bd9Sstevel@tonic-gate 		/*
24337c478bd9Sstevel@tonic-gate 		 * If doing a full file synchronous operation, then clear
24347c478bd9Sstevel@tonic-gate 		 * the RDIRTY bit.  If a page gets dirtied while the flush
24357c478bd9Sstevel@tonic-gate 		 * is happening, then RDIRTY will get set again.  The
24367c478bd9Sstevel@tonic-gate 		 * RDIRTY bit must get cleared before the flush so that
24377c478bd9Sstevel@tonic-gate 		 * we don't lose this information.
24385dae4443Sdh145677 		 *
24395dae4443Sdh145677 		 * If there are no full file async write operations
24405dae4443Sdh145677 		 * pending and RDIRTY bit is set, clear it.
24417c478bd9Sstevel@tonic-gate 		 */
24427c478bd9Sstevel@tonic-gate 		if (off == (u_offset_t)0 &&
24437c478bd9Sstevel@tonic-gate 		    !(flags & B_ASYNC) &&
24447c478bd9Sstevel@tonic-gate 		    (rp->r_flags & RDIRTY)) {
24457c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
24467c478bd9Sstevel@tonic-gate 			rdirty = (rp->r_flags & RDIRTY);
24477c478bd9Sstevel@tonic-gate 			rp->r_flags &= ~RDIRTY;
24487c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
24495dae4443Sdh145677 		} else if (flags & B_ASYNC && off == (u_offset_t)0) {
24505dae4443Sdh145677 			mutex_enter(&rp->r_statelock);
24515dae4443Sdh145677 			if (rp->r_flags & RDIRTY && rp->r_awcount == 0) {
24525dae4443Sdh145677 				rdirty = (rp->r_flags & RDIRTY);
24535dae4443Sdh145677 				rp->r_flags &= ~RDIRTY;
24545dae4443Sdh145677 			}
24555dae4443Sdh145677 			mutex_exit(&rp->r_statelock);
24567c478bd9Sstevel@tonic-gate 		} else
24577c478bd9Sstevel@tonic-gate 			rdirty = 0;
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 		/*
24607c478bd9Sstevel@tonic-gate 		 * Search the entire vp list for pages >= off, and flush
24617c478bd9Sstevel@tonic-gate 		 * the dirty pages.
24627c478bd9Sstevel@tonic-gate 		 */
2463ba3bce04SSuhasini Peddada 		error = pvn_vplist_dirty(vp, off, rp->r_putapage,
2464ba3bce04SSuhasini Peddada 		    flags, cr);
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 		/*
2467da6c28aaSamw 		 * If an error occurred and the file was marked as dirty
24687c478bd9Sstevel@tonic-gate 		 * before and we aren't forcibly invalidating pages, then
24697c478bd9Sstevel@tonic-gate 		 * reset the RDIRTY flag.
24707c478bd9Sstevel@tonic-gate 		 */
24717c478bd9Sstevel@tonic-gate 		if (error && rdirty &&
24727c478bd9Sstevel@tonic-gate 		    (flags & (B_INVAL | B_FORCE)) != (B_INVAL | B_FORCE)) {
24737c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
24747c478bd9Sstevel@tonic-gate 			rp->r_flags |= RDIRTY;
24757c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
24767c478bd9Sstevel@tonic-gate 		}
24777c478bd9Sstevel@tonic-gate 	} else {
24787c478bd9Sstevel@tonic-gate 		/*
24797c478bd9Sstevel@tonic-gate 		 * Do a range from [off...off + len) looking for pages
24807c478bd9Sstevel@tonic-gate 		 * to deal with.
24817c478bd9Sstevel@tonic-gate 		 */
24827c478bd9Sstevel@tonic-gate 		error = 0;
24837c478bd9Sstevel@tonic-gate #ifdef lint
24847c478bd9Sstevel@tonic-gate 		io_len = 0;
24857c478bd9Sstevel@tonic-gate #endif
24867c478bd9Sstevel@tonic-gate 		eoff = off + len;
24877c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
24887c478bd9Sstevel@tonic-gate 		for (io_off = off; io_off < eoff && io_off < rp->r_size;
24897c478bd9Sstevel@tonic-gate 		    io_off += io_len) {
24907c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
24917c478bd9Sstevel@tonic-gate 			/*
24927c478bd9Sstevel@tonic-gate 			 * If we are not invalidating, synchronously
24937c478bd9Sstevel@tonic-gate 			 * freeing or writing pages use the routine
24947c478bd9Sstevel@tonic-gate 			 * page_lookup_nowait() to prevent reclaiming
24957c478bd9Sstevel@tonic-gate 			 * them from the free list.
24967c478bd9Sstevel@tonic-gate 			 */
24977c478bd9Sstevel@tonic-gate 			if ((flags & B_INVAL) || !(flags & B_ASYNC)) {
24987c478bd9Sstevel@tonic-gate 				pp = page_lookup(vp, io_off,
24997c478bd9Sstevel@tonic-gate 				    (flags & (B_INVAL | B_FREE)) ?
25007c478bd9Sstevel@tonic-gate 				    SE_EXCL : SE_SHARED);
25017c478bd9Sstevel@tonic-gate 			} else {
25027c478bd9Sstevel@tonic-gate 				pp = page_lookup_nowait(vp, io_off,
25037c478bd9Sstevel@tonic-gate 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
25047c478bd9Sstevel@tonic-gate 			}
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate 			if (pp == NULL || !pvn_getdirty(pp, flags))
25077c478bd9Sstevel@tonic-gate 				io_len = PAGESIZE;
25087c478bd9Sstevel@tonic-gate 			else {
25097c478bd9Sstevel@tonic-gate 				err = (*rp->r_putapage)(vp, pp, &io_off,
25107c478bd9Sstevel@tonic-gate 				    &io_len, flags, cr);
25117c478bd9Sstevel@tonic-gate 				if (!error)
25127c478bd9Sstevel@tonic-gate 					error = err;
25137c478bd9Sstevel@tonic-gate 				/*
25147c478bd9Sstevel@tonic-gate 				 * "io_off" and "io_len" are returned as
25157c478bd9Sstevel@tonic-gate 				 * the range of pages we actually wrote.
25167c478bd9Sstevel@tonic-gate 				 * This allows us to skip ahead more quickly
25177c478bd9Sstevel@tonic-gate 				 * since several pages may've been dealt
25187c478bd9Sstevel@tonic-gate 				 * with by this iteration of the loop.
25197c478bd9Sstevel@tonic-gate 				 */
25207c478bd9Sstevel@tonic-gate 			}
25217c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
25227c478bd9Sstevel@tonic-gate 		}
25237c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
25247c478bd9Sstevel@tonic-gate 	}
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	return (error);
25277c478bd9Sstevel@tonic-gate }
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate void
nfs_invalidate_pages(vnode_t * vp,u_offset_t off,cred_t * cr)25307c478bd9Sstevel@tonic-gate nfs_invalidate_pages(vnode_t *vp, u_offset_t off, cred_t *cr)
25317c478bd9Sstevel@tonic-gate {
25327c478bd9Sstevel@tonic-gate 	rnode_t *rp;
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
25357c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
25367c478bd9Sstevel@tonic-gate 	while (rp->r_flags & RTRUNCATE)
25377c478bd9Sstevel@tonic-gate 		cv_wait(&rp->r_cv, &rp->r_statelock);
25387c478bd9Sstevel@tonic-gate 	rp->r_flags |= RTRUNCATE;
25397c478bd9Sstevel@tonic-gate 	if (off == (u_offset_t)0) {
25407c478bd9Sstevel@tonic-gate 		rp->r_flags &= ~RDIRTY;
25417c478bd9Sstevel@tonic-gate 		if (!(rp->r_flags & RSTALE))
25427c478bd9Sstevel@tonic-gate 			rp->r_error = 0;
25437c478bd9Sstevel@tonic-gate 	}
25447c478bd9Sstevel@tonic-gate 	rp->r_truncaddr = off;
25457c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
25467c478bd9Sstevel@tonic-gate 	(void) pvn_vplist_dirty(vp, off, rp->r_putapage,
25477c478bd9Sstevel@tonic-gate 	    B_INVAL | B_TRUNC, cr);
25487c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
25497c478bd9Sstevel@tonic-gate 	rp->r_flags &= ~RTRUNCATE;
25507c478bd9Sstevel@tonic-gate 	cv_broadcast(&rp->r_cv);
25517c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
25527c478bd9Sstevel@tonic-gate }
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate static int nfs_write_error_to_cons_only = 0;
25557c478bd9Sstevel@tonic-gate #define	MSG(x)	(nfs_write_error_to_cons_only ? (x) : (x) + 1)
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate /*
25587c478bd9Sstevel@tonic-gate  * Print a file handle
25597c478bd9Sstevel@tonic-gate  */
25607c478bd9Sstevel@tonic-gate void
nfs_printfhandle(nfs_fhandle * fhp)25617c478bd9Sstevel@tonic-gate nfs_printfhandle(nfs_fhandle *fhp)
25627c478bd9Sstevel@tonic-gate {
25637c478bd9Sstevel@tonic-gate 	int *ip;
25647c478bd9Sstevel@tonic-gate 	char *buf;
25657c478bd9Sstevel@tonic-gate 	size_t bufsize;
25667c478bd9Sstevel@tonic-gate 	char *cp;
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	/*
25697c478bd9Sstevel@tonic-gate 	 * 13 == "(file handle:"
25707c478bd9Sstevel@tonic-gate 	 * maximum of NFS_FHANDLE / sizeof (*ip) elements in fh_buf times
25717c478bd9Sstevel@tonic-gate 	 *	1 == ' '
25727c478bd9Sstevel@tonic-gate 	 *	8 == maximum strlen of "%x"
25737c478bd9Sstevel@tonic-gate 	 * 3 == ")\n\0"
25747c478bd9Sstevel@tonic-gate 	 */
25757c478bd9Sstevel@tonic-gate 	bufsize = 13 + ((NFS_FHANDLE_LEN / sizeof (*ip)) * (1 + 8)) + 3;
25767c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(bufsize, KM_NOSLEEP);
25777c478bd9Sstevel@tonic-gate 	if (buf == NULL)
25787c478bd9Sstevel@tonic-gate 		return;
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	cp = buf;
25817c478bd9Sstevel@tonic-gate 	(void) strcpy(cp, "(file handle:");
25827c478bd9Sstevel@tonic-gate 	while (*cp != '\0')
25837c478bd9Sstevel@tonic-gate 		cp++;
25847c478bd9Sstevel@tonic-gate 	for (ip = (int *)fhp->fh_buf;
25857c478bd9Sstevel@tonic-gate 	    ip < (int *)&fhp->fh_buf[fhp->fh_len];
25867c478bd9Sstevel@tonic-gate 	    ip++) {
25877c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, " %x", *ip);
25887c478bd9Sstevel@tonic-gate 		while (*cp != '\0')
25897c478bd9Sstevel@tonic-gate 			cp++;
25907c478bd9Sstevel@tonic-gate 	}
25917c478bd9Sstevel@tonic-gate 	(void) strcpy(cp, ")\n");
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	zcmn_err(getzoneid(), CE_CONT, MSG("^%s"), buf);
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	kmem_free(buf, bufsize);
25967c478bd9Sstevel@tonic-gate }
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate /*
25997c478bd9Sstevel@tonic-gate  * Notify the system administrator that an NFS write error has
26007c478bd9Sstevel@tonic-gate  * occurred.
26017c478bd9Sstevel@tonic-gate  */
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate /* seconds between ENOSPC/EDQUOT messages */
26047c478bd9Sstevel@tonic-gate clock_t nfs_write_error_interval = 5;
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate void
nfs_write_error(vnode_t * vp,int error,cred_t * cr)26077c478bd9Sstevel@tonic-gate nfs_write_error(vnode_t *vp, int error, cred_t *cr)
26087c478bd9Sstevel@tonic-gate {
26097c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
2610d3d50737SRafael Vanoni 	clock_t now;
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
26137c478bd9Sstevel@tonic-gate 	/*
26147c478bd9Sstevel@tonic-gate 	 * In case of forced unmount or zone shutdown, do not print any
26157c478bd9Sstevel@tonic-gate 	 * messages since it can flood the console with error messages.
26167c478bd9Sstevel@tonic-gate 	 */
26177c478bd9Sstevel@tonic-gate 	if (FS_OR_ZONE_GONE(mi->mi_vfsp))
26187c478bd9Sstevel@tonic-gate 		return;
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	/*
26217c478bd9Sstevel@tonic-gate 	 * No use in flooding the console with ENOSPC
26227c478bd9Sstevel@tonic-gate 	 * messages from the same file system.
26237c478bd9Sstevel@tonic-gate 	 */
2624d3d50737SRafael Vanoni 	now = ddi_get_lbolt();
26257c478bd9Sstevel@tonic-gate 	if ((error != ENOSPC && error != EDQUOT) ||
2626d3d50737SRafael Vanoni 	    now - mi->mi_printftime > 0) {
26277c478bd9Sstevel@tonic-gate 		zoneid_t zoneid = mi->mi_zone->zone_id;
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate #ifdef DEBUG
26307c478bd9Sstevel@tonic-gate 		nfs_perror(error, "NFS%ld write error on host %s: %m.\n",
26317c478bd9Sstevel@tonic-gate 		    mi->mi_vers, VTOR(vp)->r_server->sv_hostname, NULL);
26327c478bd9Sstevel@tonic-gate #else
26337c478bd9Sstevel@tonic-gate 		nfs_perror(error, "NFS write error on host %s: %m.\n",
26347c478bd9Sstevel@tonic-gate 		    VTOR(vp)->r_server->sv_hostname, NULL);
26357c478bd9Sstevel@tonic-gate #endif
26367c478bd9Sstevel@tonic-gate 		if (error == ENOSPC || error == EDQUOT) {
26377c478bd9Sstevel@tonic-gate 			zcmn_err(zoneid, CE_CONT,
26387c478bd9Sstevel@tonic-gate 			    MSG("^File: userid=%d, groupid=%d\n"),
26397c478bd9Sstevel@tonic-gate 			    crgetuid(cr), crgetgid(cr));
26407c478bd9Sstevel@tonic-gate 			if (crgetuid(CRED()) != crgetuid(cr) ||
26417c478bd9Sstevel@tonic-gate 			    crgetgid(CRED()) != crgetgid(cr)) {
26427c478bd9Sstevel@tonic-gate 				zcmn_err(zoneid, CE_CONT,
26437c478bd9Sstevel@tonic-gate 				    MSG("^User: userid=%d, groupid=%d\n"),
26447c478bd9Sstevel@tonic-gate 				    crgetuid(CRED()), crgetgid(CRED()));
26457c478bd9Sstevel@tonic-gate 			}
2646d3d50737SRafael Vanoni 			mi->mi_printftime = now +
26477c478bd9Sstevel@tonic-gate 			    nfs_write_error_interval * hz;
26487c478bd9Sstevel@tonic-gate 		}
26497c478bd9Sstevel@tonic-gate 		nfs_printfhandle(&VTOR(vp)->r_fh);
26507c478bd9Sstevel@tonic-gate #ifdef DEBUG
26517c478bd9Sstevel@tonic-gate 		if (error == EACCES) {
26527c478bd9Sstevel@tonic-gate 			zcmn_err(zoneid, CE_CONT,
26537c478bd9Sstevel@tonic-gate 			    MSG("^nfs_bio: cred is%s kcred\n"),
26547c478bd9Sstevel@tonic-gate 			    cr == kcred ? "" : " not");
26557c478bd9Sstevel@tonic-gate 		}
26567c478bd9Sstevel@tonic-gate #endif
26577c478bd9Sstevel@tonic-gate 	}
26587c478bd9Sstevel@tonic-gate }
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate /* ARGSUSED */
26617c478bd9Sstevel@tonic-gate static void *
nfs_mi_init(zoneid_t zoneid)26627c478bd9Sstevel@tonic-gate nfs_mi_init(zoneid_t zoneid)
26637c478bd9Sstevel@tonic-gate {
26647c478bd9Sstevel@tonic-gate 	struct mi_globals *mig;
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate 	mig = kmem_alloc(sizeof (*mig), KM_SLEEP);
26677c478bd9Sstevel@tonic-gate 	mutex_init(&mig->mig_lock, NULL, MUTEX_DEFAULT, NULL);
26687c478bd9Sstevel@tonic-gate 	list_create(&mig->mig_list, sizeof (mntinfo_t),
26697c478bd9Sstevel@tonic-gate 	    offsetof(mntinfo_t, mi_zone_node));
26707c478bd9Sstevel@tonic-gate 	mig->mig_destructor_called = B_FALSE;
26717c478bd9Sstevel@tonic-gate 	return (mig);
26727c478bd9Sstevel@tonic-gate }
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate /*
26757c478bd9Sstevel@tonic-gate  * Callback routine to tell all NFS mounts in the zone to stop creating new
26767c478bd9Sstevel@tonic-gate  * threads.  Existing threads should exit.
26777c478bd9Sstevel@tonic-gate  */
26787c478bd9Sstevel@tonic-gate /* ARGSUSED */
26797c478bd9Sstevel@tonic-gate static void
nfs_mi_shutdown(zoneid_t zoneid,void * data)26807c478bd9Sstevel@tonic-gate nfs_mi_shutdown(zoneid_t zoneid, void *data)
26817c478bd9Sstevel@tonic-gate {
26827c478bd9Sstevel@tonic-gate 	struct mi_globals *mig = data;
26837c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	ASSERT(mig != NULL);
26863fd6cc29Sthurlow again:
26877c478bd9Sstevel@tonic-gate 	mutex_enter(&mig->mig_lock);
26887c478bd9Sstevel@tonic-gate 	for (mi = list_head(&mig->mig_list); mi != NULL;
26897c478bd9Sstevel@tonic-gate 	    mi = list_next(&mig->mig_list, mi)) {
26903fd6cc29Sthurlow 
26913fd6cc29Sthurlow 		/*
26923fd6cc29Sthurlow 		 * If we've done the shutdown work for this FS, skip.
26933fd6cc29Sthurlow 		 * Once we go off the end of the list, we're done.
26943fd6cc29Sthurlow 		 */
26953fd6cc29Sthurlow 		if (mi->mi_flags & MI_DEAD)
26963fd6cc29Sthurlow 			continue;
26973fd6cc29Sthurlow 
26983fd6cc29Sthurlow 		/*
26993fd6cc29Sthurlow 		 * We will do work, so not done.  Get a hold on the FS.
27003fd6cc29Sthurlow 		 */
27013fd6cc29Sthurlow 		VFS_HOLD(mi->mi_vfsp);
27023fd6cc29Sthurlow 
27037c478bd9Sstevel@tonic-gate 		/*
27047c478bd9Sstevel@tonic-gate 		 * purge the DNLC for this filesystem
27057c478bd9Sstevel@tonic-gate 		 */
27067c478bd9Sstevel@tonic-gate 		(void) dnlc_purge_vfsp(mi->mi_vfsp, 0);
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_async_lock);
27097c478bd9Sstevel@tonic-gate 		/*
27107c478bd9Sstevel@tonic-gate 		 * Tell existing async worker threads to exit.
27117c478bd9Sstevel@tonic-gate 		 */
27127c478bd9Sstevel@tonic-gate 		mi->mi_max_threads = 0;
27130776f5e6SVallish Vaidyeshwara 		NFS_WAKEALL_ASYNC_WORKERS(mi->mi_async_work_cv);
27147c478bd9Sstevel@tonic-gate 		/*
27157c478bd9Sstevel@tonic-gate 		 * Set MI_ASYNC_MGR_STOP so the async manager thread starts
27167c478bd9Sstevel@tonic-gate 		 * getting ready to exit when it's done with its current work.
27173fd6cc29Sthurlow 		 * Also set MI_DEAD to note we've acted on this FS.
27187c478bd9Sstevel@tonic-gate 		 */
27197c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
27203fd6cc29Sthurlow 		mi->mi_flags |= (MI_ASYNC_MGR_STOP|MI_DEAD);
27217c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
27227c478bd9Sstevel@tonic-gate 		/*
27237c478bd9Sstevel@tonic-gate 		 * Wake up the async manager thread.
27247c478bd9Sstevel@tonic-gate 		 */
27257c478bd9Sstevel@tonic-gate 		cv_broadcast(&mi->mi_async_reqs_cv);
27267c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
27273fd6cc29Sthurlow 
27283fd6cc29Sthurlow 		/*
27293fd6cc29Sthurlow 		 * Drop lock and release FS, which may change list, then repeat.
27303fd6cc29Sthurlow 		 * We're done when every mi has been done or the list is empty.
27313fd6cc29Sthurlow 		 */
27323fd6cc29Sthurlow 		mutex_exit(&mig->mig_lock);
27333fd6cc29Sthurlow 		VFS_RELE(mi->mi_vfsp);
27343fd6cc29Sthurlow 		goto again;
27357c478bd9Sstevel@tonic-gate 	}
27367c478bd9Sstevel@tonic-gate 	mutex_exit(&mig->mig_lock);
27377c478bd9Sstevel@tonic-gate }
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate static void
nfs_mi_free_globals(struct mi_globals * mig)27407c478bd9Sstevel@tonic-gate nfs_mi_free_globals(struct mi_globals *mig)
27417c478bd9Sstevel@tonic-gate {
27427c478bd9Sstevel@tonic-gate 	list_destroy(&mig->mig_list);	/* makes sure the list is empty */
27437c478bd9Sstevel@tonic-gate 	mutex_destroy(&mig->mig_lock);
27447c478bd9Sstevel@tonic-gate 	kmem_free(mig, sizeof (*mig));
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate }
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate /* ARGSUSED */
27497c478bd9Sstevel@tonic-gate static void
nfs_mi_destroy(zoneid_t zoneid,void * data)27507c478bd9Sstevel@tonic-gate nfs_mi_destroy(zoneid_t zoneid, void *data)
27517c478bd9Sstevel@tonic-gate {
27527c478bd9Sstevel@tonic-gate 	struct mi_globals *mig = data;
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	ASSERT(mig != NULL);
27557c478bd9Sstevel@tonic-gate 	mutex_enter(&mig->mig_lock);
27567c478bd9Sstevel@tonic-gate 	if (list_head(&mig->mig_list) != NULL) {
27577c478bd9Sstevel@tonic-gate 		/* Still waiting for VFS_FREEVFS() */
27587c478bd9Sstevel@tonic-gate 		mig->mig_destructor_called = B_TRUE;
27597c478bd9Sstevel@tonic-gate 		mutex_exit(&mig->mig_lock);
27607c478bd9Sstevel@tonic-gate 		return;
27617c478bd9Sstevel@tonic-gate 	}
27627c478bd9Sstevel@tonic-gate 	nfs_mi_free_globals(mig);
27637c478bd9Sstevel@tonic-gate }
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate /*
27667c478bd9Sstevel@tonic-gate  * Add an NFS mount to the per-zone list of NFS mounts.
27677c478bd9Sstevel@tonic-gate  */
27687c478bd9Sstevel@tonic-gate void
nfs_mi_zonelist_add(mntinfo_t * mi)27697c478bd9Sstevel@tonic-gate nfs_mi_zonelist_add(mntinfo_t *mi)
27707c478bd9Sstevel@tonic-gate {
27717c478bd9Sstevel@tonic-gate 	struct mi_globals *mig;
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate 	mig = zone_getspecific(mi_list_key, mi->mi_zone);
27747c478bd9Sstevel@tonic-gate 	mutex_enter(&mig->mig_lock);
27757c478bd9Sstevel@tonic-gate 	list_insert_head(&mig->mig_list, mi);
27767c478bd9Sstevel@tonic-gate 	mutex_exit(&mig->mig_lock);
27777c478bd9Sstevel@tonic-gate }
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate /*
27807c478bd9Sstevel@tonic-gate  * Remove an NFS mount from the per-zone list of NFS mounts.
27817c478bd9Sstevel@tonic-gate  */
27827c478bd9Sstevel@tonic-gate static void
nfs_mi_zonelist_remove(mntinfo_t * mi)27837c478bd9Sstevel@tonic-gate nfs_mi_zonelist_remove(mntinfo_t *mi)
27847c478bd9Sstevel@tonic-gate {
27857c478bd9Sstevel@tonic-gate 	struct mi_globals *mig;
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	mig = zone_getspecific(mi_list_key, mi->mi_zone);
27887c478bd9Sstevel@tonic-gate 	mutex_enter(&mig->mig_lock);
27897c478bd9Sstevel@tonic-gate 	list_remove(&mig->mig_list, mi);
27907c478bd9Sstevel@tonic-gate 	/*
27917c478bd9Sstevel@tonic-gate 	 * We can be called asynchronously by VFS_FREEVFS() after the zone
27927c478bd9Sstevel@tonic-gate 	 * shutdown/destroy callbacks have executed; if so, clean up the zone's
27937c478bd9Sstevel@tonic-gate 	 * mi globals.
27947c478bd9Sstevel@tonic-gate 	 */
27957c478bd9Sstevel@tonic-gate 	if (list_head(&mig->mig_list) == NULL &&
27967c478bd9Sstevel@tonic-gate 	    mig->mig_destructor_called == B_TRUE) {
27977c478bd9Sstevel@tonic-gate 		nfs_mi_free_globals(mig);
27987c478bd9Sstevel@tonic-gate 		return;
27997c478bd9Sstevel@tonic-gate 	}
28007c478bd9Sstevel@tonic-gate 	mutex_exit(&mig->mig_lock);
28017c478bd9Sstevel@tonic-gate }
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate /*
28047c478bd9Sstevel@tonic-gate  * NFS Client initialization routine.  This routine should only be called
28057c478bd9Sstevel@tonic-gate  * once.  It performs the following tasks:
28067c478bd9Sstevel@tonic-gate  *	- Initalize all global locks
28077c478bd9Sstevel@tonic-gate  *	- Call sub-initialization routines (localize access to variables)
28087c478bd9Sstevel@tonic-gate  */
28097c478bd9Sstevel@tonic-gate int
nfs_clntinit(void)28107c478bd9Sstevel@tonic-gate nfs_clntinit(void)
28117c478bd9Sstevel@tonic-gate {
28127c478bd9Sstevel@tonic-gate #ifdef DEBUG
28137c478bd9Sstevel@tonic-gate 	static boolean_t nfs_clntup = B_FALSE;
28147c478bd9Sstevel@tonic-gate #endif
28157c478bd9Sstevel@tonic-gate 	int error;
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate #ifdef DEBUG
28187c478bd9Sstevel@tonic-gate 	ASSERT(nfs_clntup == B_FALSE);
28197c478bd9Sstevel@tonic-gate #endif
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	error = nfs_subrinit();
28227c478bd9Sstevel@tonic-gate 	if (error)
28237c478bd9Sstevel@tonic-gate 		return (error);
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 	error = nfs_vfsinit();
28267c478bd9Sstevel@tonic-gate 	if (error) {
28277c478bd9Sstevel@tonic-gate 		/*
28287c478bd9Sstevel@tonic-gate 		 * Cleanup nfs_subrinit() work
28297c478bd9Sstevel@tonic-gate 		 */
28307c478bd9Sstevel@tonic-gate 		nfs_subrfini();
28317c478bd9Sstevel@tonic-gate 		return (error);
28327c478bd9Sstevel@tonic-gate 	}
28337c478bd9Sstevel@tonic-gate 	zone_key_create(&mi_list_key, nfs_mi_init, nfs_mi_shutdown,
28347c478bd9Sstevel@tonic-gate 	    nfs_mi_destroy);
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 	nfs4_clnt_init();
28377c478bd9Sstevel@tonic-gate 
2838*0dfe541eSEvan Layton 	nfscmd_init();
2839*0dfe541eSEvan Layton 
28407c478bd9Sstevel@tonic-gate #ifdef DEBUG
28417c478bd9Sstevel@tonic-gate 	nfs_clntup = B_TRUE;
28427c478bd9Sstevel@tonic-gate #endif
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 	return (0);
28457c478bd9Sstevel@tonic-gate }
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate /*
28487c478bd9Sstevel@tonic-gate  * This routine is only called if the NFS Client has been initialized but
28497c478bd9Sstevel@tonic-gate  * the module failed to be installed. This routine will cleanup the previously
28507c478bd9Sstevel@tonic-gate  * allocated/initialized work.
28517c478bd9Sstevel@tonic-gate  */
28527c478bd9Sstevel@tonic-gate void
nfs_clntfini(void)28537c478bd9Sstevel@tonic-gate nfs_clntfini(void)
28547c478bd9Sstevel@tonic-gate {
28557c478bd9Sstevel@tonic-gate 	(void) zone_key_delete(mi_list_key);
28567c478bd9Sstevel@tonic-gate 	nfs_subrfini();
28577c478bd9Sstevel@tonic-gate 	nfs_vfsfini();
28587c478bd9Sstevel@tonic-gate 	nfs4_clnt_fini();
2859*0dfe541eSEvan Layton 	nfscmd_fini();
28607c478bd9Sstevel@tonic-gate }
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate /*
28637c478bd9Sstevel@tonic-gate  * nfs_lockrelease:
28647c478bd9Sstevel@tonic-gate  *
28657c478bd9Sstevel@tonic-gate  * Release any locks on the given vnode that are held by the current
28667c478bd9Sstevel@tonic-gate  * process.
28677c478bd9Sstevel@tonic-gate  */
28687c478bd9Sstevel@tonic-gate void
nfs_lockrelease(vnode_t * vp,int flag,offset_t offset,cred_t * cr)28697c478bd9Sstevel@tonic-gate nfs_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
28707c478bd9Sstevel@tonic-gate {
28717c478bd9Sstevel@tonic-gate 	flock64_t ld;
28727c478bd9Sstevel@tonic-gate 	struct shrlock shr;
28737c478bd9Sstevel@tonic-gate 	char *buf;
28747c478bd9Sstevel@tonic-gate 	int remote_lock_possible;
28757c478bd9Sstevel@tonic-gate 	int ret;
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	ASSERT((uintptr_t)vp > KERNELBASE);
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 	/*
28807c478bd9Sstevel@tonic-gate 	 * Generate an explicit unlock operation for the entire file.  As a
28817c478bd9Sstevel@tonic-gate 	 * partial optimization, only generate the unlock if there is a
28827c478bd9Sstevel@tonic-gate 	 * lock registered for the file.  We could check whether this
28837c478bd9Sstevel@tonic-gate 	 * particular process has any locks on the file, but that would
28847c478bd9Sstevel@tonic-gate 	 * require the local locking code to provide yet another query
28857c478bd9Sstevel@tonic-gate 	 * routine.  Note that no explicit synchronization is needed here.
28867c478bd9Sstevel@tonic-gate 	 * At worst, flk_has_remote_locks() will return a false positive,
28877c478bd9Sstevel@tonic-gate 	 * in which case the unlock call wastes time but doesn't harm
28887c478bd9Sstevel@tonic-gate 	 * correctness.
28897c478bd9Sstevel@tonic-gate 	 *
28907c478bd9Sstevel@tonic-gate 	 * In addition, an unlock request is generated if the process
28917c478bd9Sstevel@tonic-gate 	 * is listed as possibly having a lock on the file because the
28927c478bd9Sstevel@tonic-gate 	 * server and client lock managers may have gotten out of sync.
28937c478bd9Sstevel@tonic-gate 	 * N.B. It is important to make sure nfs_remove_locking_id() is
28947c478bd9Sstevel@tonic-gate 	 * called here even if flk_has_remote_locks(vp) reports true.
28957c478bd9Sstevel@tonic-gate 	 * If it is not called and there is an entry on the process id
28967c478bd9Sstevel@tonic-gate 	 * list, that entry will never get removed.
28977c478bd9Sstevel@tonic-gate 	 */
28987c478bd9Sstevel@tonic-gate 	remote_lock_possible = nfs_remove_locking_id(vp, RLMPL_PID,
28997c478bd9Sstevel@tonic-gate 	    (char *)&(ttoproc(curthread)->p_pid), NULL, NULL);
29007c478bd9Sstevel@tonic-gate 	if (remote_lock_possible || flk_has_remote_locks(vp)) {
29017c478bd9Sstevel@tonic-gate 		ld.l_type = F_UNLCK;	/* set to unlock entire file */
29027c478bd9Sstevel@tonic-gate 		ld.l_whence = 0;	/* unlock from start of file */
29037c478bd9Sstevel@tonic-gate 		ld.l_start = 0;
29047c478bd9Sstevel@tonic-gate 		ld.l_len = 0;		/* do entire file */
2905da6c28aaSamw 		ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, cr,
2906da6c28aaSamw 		    NULL);
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 		if (ret != 0) {
29097c478bd9Sstevel@tonic-gate 			/*
29107c478bd9Sstevel@tonic-gate 			 * If VOP_FRLOCK fails, make sure we unregister
29117c478bd9Sstevel@tonic-gate 			 * local locks before we continue.
29127c478bd9Sstevel@tonic-gate 			 */
29137c478bd9Sstevel@tonic-gate 			ld.l_pid = ttoproc(curthread)->p_pid;
29147c478bd9Sstevel@tonic-gate 			lm_register_lock_locally(vp, NULL, &ld, flag, offset);
29157c478bd9Sstevel@tonic-gate #ifdef DEBUG
29167c478bd9Sstevel@tonic-gate 			nfs_perror(ret,
29177c478bd9Sstevel@tonic-gate 			    "NFS lock release error on vp %p: %m.\n",
29187c478bd9Sstevel@tonic-gate 			    (void *)vp, NULL);
29197c478bd9Sstevel@tonic-gate #endif
29207c478bd9Sstevel@tonic-gate 		}
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 		/*
29237c478bd9Sstevel@tonic-gate 		 * The call to VOP_FRLOCK may put the pid back on the
29247c478bd9Sstevel@tonic-gate 		 * list.  We need to remove it.
29257c478bd9Sstevel@tonic-gate 		 */
29267c478bd9Sstevel@tonic-gate 		(void) nfs_remove_locking_id(vp, RLMPL_PID,
29277c478bd9Sstevel@tonic-gate 		    (char *)&(ttoproc(curthread)->p_pid), NULL, NULL);
29287c478bd9Sstevel@tonic-gate 	}
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	/*
29317c478bd9Sstevel@tonic-gate 	 * As long as the vp has a share matching our pid,
29327c478bd9Sstevel@tonic-gate 	 * pluck it off and unshare it.  There are circumstances in
29337c478bd9Sstevel@tonic-gate 	 * which the call to nfs_remove_locking_id() may put the
29347c478bd9Sstevel@tonic-gate 	 * owner back on the list, in which case we simply do a
29357c478bd9Sstevel@tonic-gate 	 * redundant and harmless unshare.
29367c478bd9Sstevel@tonic-gate 	 */
29377c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(MAX_SHR_OWNER_LEN, KM_SLEEP);
29387c478bd9Sstevel@tonic-gate 	while (nfs_remove_locking_id(vp, RLMPL_OWNER,
29397c478bd9Sstevel@tonic-gate 	    (char *)NULL, buf, &shr.s_own_len)) {
29407c478bd9Sstevel@tonic-gate 		shr.s_owner = buf;
29417c478bd9Sstevel@tonic-gate 		shr.s_access = 0;
29427c478bd9Sstevel@tonic-gate 		shr.s_deny = 0;
29437c478bd9Sstevel@tonic-gate 		shr.s_sysid = 0;
29447c478bd9Sstevel@tonic-gate 		shr.s_pid = curproc->p_pid;
29457c478bd9Sstevel@tonic-gate 
2946da6c28aaSamw 		ret = VOP_SHRLOCK(vp, F_UNSHARE, &shr, flag, cr, NULL);
29477c478bd9Sstevel@tonic-gate #ifdef DEBUG
29487c478bd9Sstevel@tonic-gate 		if (ret != 0) {
29497c478bd9Sstevel@tonic-gate 			nfs_perror(ret,
29507c478bd9Sstevel@tonic-gate 			    "NFS share release error on vp %p: %m.\n",
29517c478bd9Sstevel@tonic-gate 			    (void *)vp, NULL);
29527c478bd9Sstevel@tonic-gate 		}
29537c478bd9Sstevel@tonic-gate #endif
29547c478bd9Sstevel@tonic-gate 	}
29557c478bd9Sstevel@tonic-gate 	kmem_free(buf, MAX_SHR_OWNER_LEN);
29567c478bd9Sstevel@tonic-gate }
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate /*
29597c478bd9Sstevel@tonic-gate  * nfs_lockcompletion:
29607c478bd9Sstevel@tonic-gate  *
29617c478bd9Sstevel@tonic-gate  * If the vnode has a lock that makes it unsafe to cache the file, mark it
29627c478bd9Sstevel@tonic-gate  * as non cachable (set VNOCACHE bit).
29637c478bd9Sstevel@tonic-gate  */
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate void
nfs_lockcompletion(vnode_t * vp,int cmd)29667c478bd9Sstevel@tonic-gate nfs_lockcompletion(vnode_t *vp, int cmd)
29677c478bd9Sstevel@tonic-gate {
29687c478bd9Sstevel@tonic-gate #ifdef DEBUG
29697c478bd9Sstevel@tonic-gate 	rnode_t *rp = VTOR(vp);
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	ASSERT(nfs_rw_lock_held(&rp->r_lkserlock, RW_WRITER));
29727c478bd9Sstevel@tonic-gate #endif
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 	if (cmd == F_SETLK || cmd == F_SETLKW) {
29757c478bd9Sstevel@tonic-gate 		if (!lm_safemap(vp)) {
29767c478bd9Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
29777c478bd9Sstevel@tonic-gate 			vp->v_flag |= VNOCACHE;
29787c478bd9Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
29797c478bd9Sstevel@tonic-gate 		} else {
29807c478bd9Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
29817c478bd9Sstevel@tonic-gate 			vp->v_flag &= ~VNOCACHE;
29827c478bd9Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
29837c478bd9Sstevel@tonic-gate 		}
29847c478bd9Sstevel@tonic-gate 	}
29857c478bd9Sstevel@tonic-gate 	/*
29867c478bd9Sstevel@tonic-gate 	 * The cached attributes of the file are stale after acquiring
29877c478bd9Sstevel@tonic-gate 	 * the lock on the file. They were updated when the file was
29887c478bd9Sstevel@tonic-gate 	 * opened, but not updated when the lock was acquired. Therefore the
29897c478bd9Sstevel@tonic-gate 	 * cached attributes are invalidated after the lock is obtained.
29907c478bd9Sstevel@tonic-gate 	 */
29917c478bd9Sstevel@tonic-gate 	PURGE_ATTRCACHE(vp);
29927c478bd9Sstevel@tonic-gate }
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate /*
29957c478bd9Sstevel@tonic-gate  * The lock manager holds state making it possible for the client
29967c478bd9Sstevel@tonic-gate  * and server to be out of sync.  For example, if the response from
29977c478bd9Sstevel@tonic-gate  * the server granting a lock request is lost, the server will think
29987c478bd9Sstevel@tonic-gate  * the lock is granted and the client will think the lock is lost.
29997c478bd9Sstevel@tonic-gate  * The client can tell when it is not positive if it is in sync with
30007c478bd9Sstevel@tonic-gate  * the server.
30017c478bd9Sstevel@tonic-gate  *
30027c478bd9Sstevel@tonic-gate  * To deal with this, a list of processes for which the client is
30037c478bd9Sstevel@tonic-gate  * not sure if the server holds a lock is attached to the rnode.
30047c478bd9Sstevel@tonic-gate  * When such a process closes the rnode, an unlock request is sent
30057c478bd9Sstevel@tonic-gate  * to the server to unlock the entire file.
30067c478bd9Sstevel@tonic-gate  *
30077c478bd9Sstevel@tonic-gate  * The list is kept as a singularly linked NULL terminated list.
30087c478bd9Sstevel@tonic-gate  * Because it is only added to under extreme error conditions, the
30097c478bd9Sstevel@tonic-gate  * list shouldn't get very big.  DEBUG kernels print a message if
30107c478bd9Sstevel@tonic-gate  * the list gets bigger than nfs_lmpl_high_water.  This is arbitrarily
30117c478bd9Sstevel@tonic-gate  * choosen to be 8, but can be tuned at runtime.
30127c478bd9Sstevel@tonic-gate  */
30137c478bd9Sstevel@tonic-gate #ifdef DEBUG
30147c478bd9Sstevel@tonic-gate /* int nfs_lmpl_high_water = 8; */
30157c478bd9Sstevel@tonic-gate int nfs_lmpl_high_water = 128;
30167c478bd9Sstevel@tonic-gate int nfs_cnt_add_locking_id = 0;
30177c478bd9Sstevel@tonic-gate int nfs_len_add_locking_id = 0;
30187c478bd9Sstevel@tonic-gate #endif /* DEBUG */
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate /*
30217c478bd9Sstevel@tonic-gate  * Record that the nfs lock manager server may be holding a lock on
30227c478bd9Sstevel@tonic-gate  * a vnode for a process.
30237c478bd9Sstevel@tonic-gate  *
30247c478bd9Sstevel@tonic-gate  * Because the nfs lock manager server holds state, it is possible
30257c478bd9Sstevel@tonic-gate  * for the server to get out of sync with the client.  This routine is called
30267c478bd9Sstevel@tonic-gate  * from the client when it is no longer sure if the server is in sync
30277c478bd9Sstevel@tonic-gate  * with the client.  nfs_lockrelease() will then notice this and send
30287c478bd9Sstevel@tonic-gate  * an unlock request when the file is closed
30297c478bd9Sstevel@tonic-gate  */
30307c478bd9Sstevel@tonic-gate void
nfs_add_locking_id(vnode_t * vp,pid_t pid,int type,char * id,int len)30317c478bd9Sstevel@tonic-gate nfs_add_locking_id(vnode_t *vp, pid_t pid, int type, char *id, int len)
30327c478bd9Sstevel@tonic-gate {
30337c478bd9Sstevel@tonic-gate 	rnode_t *rp;
30347c478bd9Sstevel@tonic-gate 	lmpl_t *new;
30357c478bd9Sstevel@tonic-gate 	lmpl_t *cur;
30367c478bd9Sstevel@tonic-gate 	lmpl_t **lmplp;
30377c478bd9Sstevel@tonic-gate #ifdef DEBUG
30387c478bd9Sstevel@tonic-gate 	int list_len = 1;
30397c478bd9Sstevel@tonic-gate #endif /* DEBUG */
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate #ifdef DEBUG
30427c478bd9Sstevel@tonic-gate 	++nfs_cnt_add_locking_id;
30437c478bd9Sstevel@tonic-gate #endif /* DEBUG */
30447c478bd9Sstevel@tonic-gate 	/*
30457c478bd9Sstevel@tonic-gate 	 * allocate new lmpl_t now so we don't sleep
30467c478bd9Sstevel@tonic-gate 	 * later after grabbing mutexes
30477c478bd9Sstevel@tonic-gate 	 */
30487c478bd9Sstevel@tonic-gate 	ASSERT(len < MAX_SHR_OWNER_LEN);
30497c478bd9Sstevel@tonic-gate 	new = kmem_alloc(sizeof (*new), KM_SLEEP);
30507c478bd9Sstevel@tonic-gate 	new->lmpl_type = type;
30517c478bd9Sstevel@tonic-gate 	new->lmpl_pid = pid;
30527c478bd9Sstevel@tonic-gate 	new->lmpl_owner = kmem_alloc(len, KM_SLEEP);
30537c478bd9Sstevel@tonic-gate 	bcopy(id, new->lmpl_owner, len);
30547c478bd9Sstevel@tonic-gate 	new->lmpl_own_len = len;
30557c478bd9Sstevel@tonic-gate 	new->lmpl_next = (lmpl_t *)NULL;
30567c478bd9Sstevel@tonic-gate #ifdef DEBUG
30577c478bd9Sstevel@tonic-gate 	if (type == RLMPL_PID) {
30587c478bd9Sstevel@tonic-gate 		ASSERT(len == sizeof (pid_t));
30597c478bd9Sstevel@tonic-gate 		ASSERT(pid == *(pid_t *)new->lmpl_owner);
30607c478bd9Sstevel@tonic-gate 	} else {
30617c478bd9Sstevel@tonic-gate 		ASSERT(type == RLMPL_OWNER);
30627c478bd9Sstevel@tonic-gate 	}
30637c478bd9Sstevel@tonic-gate #endif
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
30667c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 	/*
30697c478bd9Sstevel@tonic-gate 	 * Add this id to the list for this rnode only if the
30707c478bd9Sstevel@tonic-gate 	 * rnode is active and the id is not already there.
30717c478bd9Sstevel@tonic-gate 	 */
30727c478bd9Sstevel@tonic-gate 	ASSERT(rp->r_flags & RHASHED);
30737c478bd9Sstevel@tonic-gate 	lmplp = &(rp->r_lmpl);
30747c478bd9Sstevel@tonic-gate 	for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL; cur = cur->lmpl_next) {
30757c478bd9Sstevel@tonic-gate 		if (cur->lmpl_pid == pid &&
30767c478bd9Sstevel@tonic-gate 		    cur->lmpl_type == type &&
30777c478bd9Sstevel@tonic-gate 		    cur->lmpl_own_len == len &&
30787c478bd9Sstevel@tonic-gate 		    bcmp(cur->lmpl_owner, new->lmpl_owner, len) == 0) {
30797c478bd9Sstevel@tonic-gate 			kmem_free(new->lmpl_owner, len);
30807c478bd9Sstevel@tonic-gate 			kmem_free(new, sizeof (*new));
30817c478bd9Sstevel@tonic-gate 			break;
30827c478bd9Sstevel@tonic-gate 		}
30837c478bd9Sstevel@tonic-gate 		lmplp = &cur->lmpl_next;
30847c478bd9Sstevel@tonic-gate #ifdef DEBUG
30857c478bd9Sstevel@tonic-gate 		++list_len;
30867c478bd9Sstevel@tonic-gate #endif /* DEBUG */
30877c478bd9Sstevel@tonic-gate 	}
30887c478bd9Sstevel@tonic-gate 	if (cur == (lmpl_t *)NULL) {
30897c478bd9Sstevel@tonic-gate 		*lmplp = new;
30907c478bd9Sstevel@tonic-gate #ifdef DEBUG
30917c478bd9Sstevel@tonic-gate 		if (list_len > nfs_len_add_locking_id) {
30927c478bd9Sstevel@tonic-gate 			nfs_len_add_locking_id = list_len;
30937c478bd9Sstevel@tonic-gate 		}
30947c478bd9Sstevel@tonic-gate 		if (list_len > nfs_lmpl_high_water) {
30957c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "nfs_add_locking_id: long list "
30967c478bd9Sstevel@tonic-gate 			    "vp=%p is %d", (void *)vp, list_len);
30977c478bd9Sstevel@tonic-gate 		}
30987c478bd9Sstevel@tonic-gate #endif /* DEBUG */
30997c478bd9Sstevel@tonic-gate 	}
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate #ifdef DEBUG
31027c478bd9Sstevel@tonic-gate 	if (share_debug) {
31037c478bd9Sstevel@tonic-gate 		int nitems = 0;
31047c478bd9Sstevel@tonic-gate 		int npids = 0;
31057c478bd9Sstevel@tonic-gate 		int nowners = 0;
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 		/*
31087c478bd9Sstevel@tonic-gate 		 * Count the number of things left on r_lmpl after the remove.
31097c478bd9Sstevel@tonic-gate 		 */
31107c478bd9Sstevel@tonic-gate 		for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL;
31117c478bd9Sstevel@tonic-gate 		    cur = cur->lmpl_next) {
31127c478bd9Sstevel@tonic-gate 			nitems++;
31137c478bd9Sstevel@tonic-gate 			if (cur->lmpl_type == RLMPL_PID) {
31147c478bd9Sstevel@tonic-gate 				npids++;
31157c478bd9Sstevel@tonic-gate 			} else if (cur->lmpl_type == RLMPL_OWNER) {
31167c478bd9Sstevel@tonic-gate 				nowners++;
31177c478bd9Sstevel@tonic-gate 			} else {
31187c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC, "nfs_add_locking_id: "
3119da6c28aaSamw 				    "unrecognized lmpl_type %d",
31207c478bd9Sstevel@tonic-gate 				    cur->lmpl_type);
31217c478bd9Sstevel@tonic-gate 			}
31227c478bd9Sstevel@tonic-gate 		}
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "nfs_add_locking_id(%s): %d PIDs + %d "
31257c478bd9Sstevel@tonic-gate 		    "OWNs = %d items left on r_lmpl\n",
31267c478bd9Sstevel@tonic-gate 		    (type == RLMPL_PID) ? "P" : "O", npids, nowners, nitems);
31277c478bd9Sstevel@tonic-gate 	}
31287c478bd9Sstevel@tonic-gate #endif
31297c478bd9Sstevel@tonic-gate 
31307c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
31317c478bd9Sstevel@tonic-gate }
31327c478bd9Sstevel@tonic-gate 
31337c478bd9Sstevel@tonic-gate /*
31347c478bd9Sstevel@tonic-gate  * Remove an id from the lock manager id list.
31357c478bd9Sstevel@tonic-gate  *
31367c478bd9Sstevel@tonic-gate  * If the id is not in the list return 0.  If it was found and
31377c478bd9Sstevel@tonic-gate  * removed, return 1.
31387c478bd9Sstevel@tonic-gate  */
31397c478bd9Sstevel@tonic-gate static int
nfs_remove_locking_id(vnode_t * vp,int type,char * id,char * rid,int * rlen)31407c478bd9Sstevel@tonic-gate nfs_remove_locking_id(vnode_t *vp, int type, char *id, char *rid, int *rlen)
31417c478bd9Sstevel@tonic-gate {
31427c478bd9Sstevel@tonic-gate 	lmpl_t *cur;
31437c478bd9Sstevel@tonic-gate 	lmpl_t **lmplp;
31447c478bd9Sstevel@tonic-gate 	rnode_t *rp;
31457c478bd9Sstevel@tonic-gate 	int rv = 0;
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate 	ASSERT(type == RLMPL_PID || type == RLMPL_OWNER);
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
31527c478bd9Sstevel@tonic-gate 	ASSERT(rp->r_flags & RHASHED);
31537c478bd9Sstevel@tonic-gate 	lmplp = &(rp->r_lmpl);
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	/*
31567c478bd9Sstevel@tonic-gate 	 * Search through the list and remove the entry for this id
31577c478bd9Sstevel@tonic-gate 	 * if it is there.  The special case id == NULL allows removal
31587c478bd9Sstevel@tonic-gate 	 * of the first share on the r_lmpl list belonging to the
31597c478bd9Sstevel@tonic-gate 	 * current process (if any), without regard to further details
31607c478bd9Sstevel@tonic-gate 	 * of its identity.
31617c478bd9Sstevel@tonic-gate 	 */
31627c478bd9Sstevel@tonic-gate 	for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL; cur = cur->lmpl_next) {
31637c478bd9Sstevel@tonic-gate 		if (cur->lmpl_type == type &&
31647c478bd9Sstevel@tonic-gate 		    cur->lmpl_pid == curproc->p_pid &&
31657c478bd9Sstevel@tonic-gate 		    (id == (char *)NULL ||
31667c478bd9Sstevel@tonic-gate 		    bcmp(cur->lmpl_owner, id, cur->lmpl_own_len) == 0)) {
31677c478bd9Sstevel@tonic-gate 			*lmplp = cur->lmpl_next;
31687c478bd9Sstevel@tonic-gate 			ASSERT(cur->lmpl_own_len < MAX_SHR_OWNER_LEN);
31697c478bd9Sstevel@tonic-gate 			if (rid != NULL) {
31707c478bd9Sstevel@tonic-gate 				bcopy(cur->lmpl_owner, rid, cur->lmpl_own_len);
31717c478bd9Sstevel@tonic-gate 				*rlen = cur->lmpl_own_len;
31727c478bd9Sstevel@tonic-gate 			}
31737c478bd9Sstevel@tonic-gate 			kmem_free(cur->lmpl_owner, cur->lmpl_own_len);
31747c478bd9Sstevel@tonic-gate 			kmem_free(cur, sizeof (*cur));
31757c478bd9Sstevel@tonic-gate 			rv = 1;
31767c478bd9Sstevel@tonic-gate 			break;
31777c478bd9Sstevel@tonic-gate 		}
31787c478bd9Sstevel@tonic-gate 		lmplp = &cur->lmpl_next;
31797c478bd9Sstevel@tonic-gate 	}
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate #ifdef DEBUG
31827c478bd9Sstevel@tonic-gate 	if (share_debug) {
31837c478bd9Sstevel@tonic-gate 		int nitems = 0;
31847c478bd9Sstevel@tonic-gate 		int npids = 0;
31857c478bd9Sstevel@tonic-gate 		int nowners = 0;
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 		/*
31887c478bd9Sstevel@tonic-gate 		 * Count the number of things left on r_lmpl after the remove.
31897c478bd9Sstevel@tonic-gate 		 */
31907c478bd9Sstevel@tonic-gate 		for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL;
31917c478bd9Sstevel@tonic-gate 		    cur = cur->lmpl_next) {
31927c478bd9Sstevel@tonic-gate 			nitems++;
31937c478bd9Sstevel@tonic-gate 			if (cur->lmpl_type == RLMPL_PID) {
31947c478bd9Sstevel@tonic-gate 				npids++;
31957c478bd9Sstevel@tonic-gate 			} else if (cur->lmpl_type == RLMPL_OWNER) {
31967c478bd9Sstevel@tonic-gate 				nowners++;
31977c478bd9Sstevel@tonic-gate 			} else {
31987c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC,
3199da6c28aaSamw 				    "nrli: unrecognized lmpl_type %d",
32007c478bd9Sstevel@tonic-gate 				    cur->lmpl_type);
32017c478bd9Sstevel@tonic-gate 			}
32027c478bd9Sstevel@tonic-gate 		}
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
32057c478bd9Sstevel@tonic-gate 		"nrli(%s): %d PIDs + %d OWNs = %d items left on r_lmpl\n",
3206ba3bce04SSuhasini Peddada 		    (type == RLMPL_PID) ? "P" : "O",
3207ba3bce04SSuhasini Peddada 		    npids,
3208ba3bce04SSuhasini Peddada 		    nowners,
3209ba3bce04SSuhasini Peddada 		    nitems);
32107c478bd9Sstevel@tonic-gate 	}
32117c478bd9Sstevel@tonic-gate #endif
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
32147c478bd9Sstevel@tonic-gate 	return (rv);
32157c478bd9Sstevel@tonic-gate }
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate void
nfs_free_mi(mntinfo_t * mi)32187c478bd9Sstevel@tonic-gate nfs_free_mi(mntinfo_t *mi)
32197c478bd9Sstevel@tonic-gate {
32207c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_flags & MI_ASYNC_MGR_STOP);
32217c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_manager_thread == NULL);
32220776f5e6SVallish Vaidyeshwara 	ASSERT(mi->mi_threads[NFS_ASYNC_QUEUE] == 0 &&
32230776f5e6SVallish Vaidyeshwara 	    mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE] == 0);
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate 	/*
32267c478bd9Sstevel@tonic-gate 	 * Remove the node from the global list before we start tearing it down.
32277c478bd9Sstevel@tonic-gate 	 */
32287c478bd9Sstevel@tonic-gate 	nfs_mi_zonelist_remove(mi);
32297c478bd9Sstevel@tonic-gate 	if (mi->mi_klmconfig) {
32307c478bd9Sstevel@tonic-gate 		lm_free_config(mi->mi_klmconfig);
32317c478bd9Sstevel@tonic-gate 		kmem_free(mi->mi_klmconfig, sizeof (struct knetconfig));
32327c478bd9Sstevel@tonic-gate 	}
32337c478bd9Sstevel@tonic-gate 	mutex_destroy(&mi->mi_lock);
3234e8dc3b7dSvv149972 	mutex_destroy(&mi->mi_remap_lock);
32357c478bd9Sstevel@tonic-gate 	mutex_destroy(&mi->mi_async_lock);
3236e010bda9SMarcel Telka 	mutex_destroy(&mi->mi_rnodes_lock);
32377c478bd9Sstevel@tonic-gate 	cv_destroy(&mi->mi_failover_cv);
32380776f5e6SVallish Vaidyeshwara 	cv_destroy(&mi->mi_async_work_cv[NFS_ASYNC_QUEUE]);
32390776f5e6SVallish Vaidyeshwara 	cv_destroy(&mi->mi_async_work_cv[NFS_ASYNC_PGOPS_QUEUE]);
32407c478bd9Sstevel@tonic-gate 	cv_destroy(&mi->mi_async_reqs_cv);
32417c478bd9Sstevel@tonic-gate 	cv_destroy(&mi->mi_async_cv);
3242e010bda9SMarcel Telka 	list_destroy(&mi->mi_rnodes);
3243a19609f8Sjv227347 	zone_rele_ref(&mi->mi_zone_ref, ZONE_REF_NFS);
32447c478bd9Sstevel@tonic-gate 	kmem_free(mi, sizeof (*mi));
32457c478bd9Sstevel@tonic-gate }
32467c478bd9Sstevel@tonic-gate 
32477c478bd9Sstevel@tonic-gate static int
mnt_kstat_update(kstat_t * ksp,int rw)32487c478bd9Sstevel@tonic-gate mnt_kstat_update(kstat_t *ksp, int rw)
32497c478bd9Sstevel@tonic-gate {
32507c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
32517c478bd9Sstevel@tonic-gate 	struct mntinfo_kstat *mik;
32527c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
32537c478bd9Sstevel@tonic-gate 	int i;
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 	/* this is a read-only kstat. Bail out on a write */
32567c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
32577c478bd9Sstevel@tonic-gate 		return (EACCES);
32587c478bd9Sstevel@tonic-gate 
32597c478bd9Sstevel@tonic-gate 	/*
32607c478bd9Sstevel@tonic-gate 	 * We don't want to wait here as kstat_chain_lock could be held by
32617c478bd9Sstevel@tonic-gate 	 * dounmount(). dounmount() takes vfs_reflock before the chain lock
32627c478bd9Sstevel@tonic-gate 	 * and thus could lead to a deadlock.
32637c478bd9Sstevel@tonic-gate 	 */
32647c478bd9Sstevel@tonic-gate 	vfsp = (struct vfs *)ksp->ks_private;
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate 
32677c478bd9Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 	mik = (struct mntinfo_kstat *)ksp->ks_data;
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate 	(void) strcpy(mik->mik_proto, mi->mi_curr_serv->sv_knconf->knc_proto);
32727c478bd9Sstevel@tonic-gate 	mik->mik_vers = (uint32_t)mi->mi_vers;
32737c478bd9Sstevel@tonic-gate 	mik->mik_flags = mi->mi_flags;
32747c478bd9Sstevel@tonic-gate 	mik->mik_secmod = mi->mi_curr_serv->sv_secdata->secmod;
32757c478bd9Sstevel@tonic-gate 	mik->mik_curread = (uint32_t)mi->mi_curread;
32767c478bd9Sstevel@tonic-gate 	mik->mik_curwrite = (uint32_t)mi->mi_curwrite;
32777c478bd9Sstevel@tonic-gate 	mik->mik_retrans = mi->mi_retrans;
32787c478bd9Sstevel@tonic-gate 	mik->mik_timeo = mi->mi_timeo;
32797c478bd9Sstevel@tonic-gate 	mik->mik_acregmin = HR2SEC(mi->mi_acregmin);
32807c478bd9Sstevel@tonic-gate 	mik->mik_acregmax = HR2SEC(mi->mi_acregmax);
32817c478bd9Sstevel@tonic-gate 	mik->mik_acdirmin = HR2SEC(mi->mi_acdirmin);
32827c478bd9Sstevel@tonic-gate 	mik->mik_acdirmax = HR2SEC(mi->mi_acdirmax);
32837c478bd9Sstevel@tonic-gate 	for (i = 0; i < NFS_CALLTYPES + 1; i++) {
32847c478bd9Sstevel@tonic-gate 		mik->mik_timers[i].srtt = (uint32_t)mi->mi_timers[i].rt_srtt;
32857c478bd9Sstevel@tonic-gate 		mik->mik_timers[i].deviate =
32867c478bd9Sstevel@tonic-gate 		    (uint32_t)mi->mi_timers[i].rt_deviate;
32877c478bd9Sstevel@tonic-gate 		mik->mik_timers[i].rtxcur =
32887c478bd9Sstevel@tonic-gate 		    (uint32_t)mi->mi_timers[i].rt_rtxcur;
32897c478bd9Sstevel@tonic-gate 	}
32907c478bd9Sstevel@tonic-gate 	mik->mik_noresponse = (uint32_t)mi->mi_noresponse;
32917c478bd9Sstevel@tonic-gate 	mik->mik_failover = (uint32_t)mi->mi_failover;
32927c478bd9Sstevel@tonic-gate 	mik->mik_remap = (uint32_t)mi->mi_remap;
32937c478bd9Sstevel@tonic-gate 	(void) strcpy(mik->mik_curserver, mi->mi_curr_serv->sv_hostname);
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate 	return (0);
32967c478bd9Sstevel@tonic-gate }
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate void
nfs_mnt_kstat_init(struct vfs * vfsp)32997c478bd9Sstevel@tonic-gate nfs_mnt_kstat_init(struct vfs *vfsp)
33007c478bd9Sstevel@tonic-gate {
33017c478bd9Sstevel@tonic-gate 	mntinfo_t *mi = VFTOMI(vfsp);
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 	/*
33047c478bd9Sstevel@tonic-gate 	 * Create the version specific kstats.
33057c478bd9Sstevel@tonic-gate 	 *
33067c478bd9Sstevel@tonic-gate 	 * PSARC 2001/697 Contract Private Interface
33077c478bd9Sstevel@tonic-gate 	 * All nfs kstats are under SunMC contract
33087c478bd9Sstevel@tonic-gate 	 * Please refer to the PSARC listed above and contact
33097c478bd9Sstevel@tonic-gate 	 * SunMC before making any changes!
33107c478bd9Sstevel@tonic-gate 	 *
33117c478bd9Sstevel@tonic-gate 	 * Changes must be reviewed by Solaris File Sharing
33127c478bd9Sstevel@tonic-gate 	 * Changes must be communicated to contract-2001-697@sun.com
33137c478bd9Sstevel@tonic-gate 	 *
33147c478bd9Sstevel@tonic-gate 	 */
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 	mi->mi_io_kstats = kstat_create_zone("nfs", getminor(vfsp->vfs_dev),
33177c478bd9Sstevel@tonic-gate 	    NULL, "nfs", KSTAT_TYPE_IO, 1, 0, mi->mi_zone->zone_id);
33187c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
33197c478bd9Sstevel@tonic-gate 		if (mi->mi_zone->zone_id != GLOBAL_ZONEID)
33207c478bd9Sstevel@tonic-gate 			kstat_zone_add(mi->mi_io_kstats, GLOBAL_ZONEID);
33217c478bd9Sstevel@tonic-gate 		mi->mi_io_kstats->ks_lock = &mi->mi_lock;
33227c478bd9Sstevel@tonic-gate 		kstat_install(mi->mi_io_kstats);
33237c478bd9Sstevel@tonic-gate 	}
33247c478bd9Sstevel@tonic-gate 
33257c478bd9Sstevel@tonic-gate 	if ((mi->mi_ro_kstats = kstat_create_zone("nfs",
33267c478bd9Sstevel@tonic-gate 	    getminor(vfsp->vfs_dev), "mntinfo", "misc", KSTAT_TYPE_RAW,
33277c478bd9Sstevel@tonic-gate 	    sizeof (struct mntinfo_kstat), 0, mi->mi_zone->zone_id)) != NULL) {
33287c478bd9Sstevel@tonic-gate 		if (mi->mi_zone->zone_id != GLOBAL_ZONEID)
33297c478bd9Sstevel@tonic-gate 			kstat_zone_add(mi->mi_ro_kstats, GLOBAL_ZONEID);
33307c478bd9Sstevel@tonic-gate 		mi->mi_ro_kstats->ks_update = mnt_kstat_update;
33317c478bd9Sstevel@tonic-gate 		mi->mi_ro_kstats->ks_private = (void *)vfsp;
33327c478bd9Sstevel@tonic-gate 		kstat_install(mi->mi_ro_kstats);
33337c478bd9Sstevel@tonic-gate 	}
33347c478bd9Sstevel@tonic-gate }
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate nfs_delmapcall_t *
nfs_init_delmapcall()33377c478bd9Sstevel@tonic-gate nfs_init_delmapcall()
33387c478bd9Sstevel@tonic-gate {
33397c478bd9Sstevel@tonic-gate 	nfs_delmapcall_t	*delmap_call;
33407c478bd9Sstevel@tonic-gate 
33417c478bd9Sstevel@tonic-gate 	delmap_call = kmem_alloc(sizeof (nfs_delmapcall_t), KM_SLEEP);
33427c478bd9Sstevel@tonic-gate 	delmap_call->call_id = curthread;
33437c478bd9Sstevel@tonic-gate 	delmap_call->error = 0;
33447c478bd9Sstevel@tonic-gate 
33457c478bd9Sstevel@tonic-gate 	return (delmap_call);
33467c478bd9Sstevel@tonic-gate }
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate void
nfs_free_delmapcall(nfs_delmapcall_t * delmap_call)33497c478bd9Sstevel@tonic-gate nfs_free_delmapcall(nfs_delmapcall_t *delmap_call)
33507c478bd9Sstevel@tonic-gate {
33517c478bd9Sstevel@tonic-gate 	kmem_free(delmap_call, sizeof (nfs_delmapcall_t));
33527c478bd9Sstevel@tonic-gate }
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate /*
33557c478bd9Sstevel@tonic-gate  * Searches for the current delmap caller (based on curthread) in the list of
33567c478bd9Sstevel@tonic-gate  * callers.  If it is found, we remove it and free the delmap caller.
33577c478bd9Sstevel@tonic-gate  * Returns:
33587c478bd9Sstevel@tonic-gate  *	0 if the caller wasn't found
33597c478bd9Sstevel@tonic-gate  *	1 if the caller was found, removed and freed.  *errp is set to what
33607c478bd9Sstevel@tonic-gate  *	the result of the delmap was.
33617c478bd9Sstevel@tonic-gate  */
33627c478bd9Sstevel@tonic-gate int
nfs_find_and_delete_delmapcall(rnode_t * rp,int * errp)33637c478bd9Sstevel@tonic-gate nfs_find_and_delete_delmapcall(rnode_t *rp, int *errp)
33647c478bd9Sstevel@tonic-gate {
33657c478bd9Sstevel@tonic-gate 	nfs_delmapcall_t	*delmap_call;
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate 	/*
33687c478bd9Sstevel@tonic-gate 	 * If the list doesn't exist yet, we create it and return
33697c478bd9Sstevel@tonic-gate 	 * that the caller wasn't found.  No list = no callers.
33707c478bd9Sstevel@tonic-gate 	 */
33717c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
33727c478bd9Sstevel@tonic-gate 	if (!(rp->r_flags & RDELMAPLIST)) {
33737c478bd9Sstevel@tonic-gate 		/* The list does not exist */
33747c478bd9Sstevel@tonic-gate 		list_create(&rp->r_indelmap, sizeof (nfs_delmapcall_t),
33757c478bd9Sstevel@tonic-gate 		    offsetof(nfs_delmapcall_t, call_node));
33767c478bd9Sstevel@tonic-gate 		rp->r_flags |= RDELMAPLIST;
33777c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
33787c478bd9Sstevel@tonic-gate 		return (0);
33797c478bd9Sstevel@tonic-gate 	} else {
33807c478bd9Sstevel@tonic-gate 		/* The list exists so search it */
33817c478bd9Sstevel@tonic-gate 		for (delmap_call = list_head(&rp->r_indelmap);
33827c478bd9Sstevel@tonic-gate 		    delmap_call != NULL;
33837c478bd9Sstevel@tonic-gate 		    delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
33847c478bd9Sstevel@tonic-gate 			if (delmap_call->call_id == curthread) {
33857c478bd9Sstevel@tonic-gate 				/* current caller is in the list */
33867c478bd9Sstevel@tonic-gate 				*errp = delmap_call->error;
33877c478bd9Sstevel@tonic-gate 				list_remove(&rp->r_indelmap, delmap_call);
33887c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
33897c478bd9Sstevel@tonic-gate 				nfs_free_delmapcall(delmap_call);
33907c478bd9Sstevel@tonic-gate 				return (1);
33917c478bd9Sstevel@tonic-gate 			}
33927c478bd9Sstevel@tonic-gate 		}
33937c478bd9Sstevel@tonic-gate 	}
33947c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
33957c478bd9Sstevel@tonic-gate 	return (0);
33967c478bd9Sstevel@tonic-gate }
3397