xref: /onnv-gate/usr/src/uts/common/fs/nfs/nfs_client.c (revision 13096:b02331b7b26d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51832Sdh145677  * Common Development and Distribution License (the "License").
61832Sdh145677  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*13096SJordan.Vaughan@Sun.com  * Copyright (c) 1986, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  *
240Sstevel@tonic-gate  *  	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
250Sstevel@tonic-gate  *	All rights reserved.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/thread.h>
320Sstevel@tonic-gate #include <sys/t_lock.h>
330Sstevel@tonic-gate #include <sys/time.h>
340Sstevel@tonic-gate #include <sys/vnode.h>
350Sstevel@tonic-gate #include <sys/vfs.h>
360Sstevel@tonic-gate #include <sys/errno.h>
370Sstevel@tonic-gate #include <sys/buf.h>
380Sstevel@tonic-gate #include <sys/stat.h>
390Sstevel@tonic-gate #include <sys/cred.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/debug.h>
420Sstevel@tonic-gate #include <sys/dnlc.h>
430Sstevel@tonic-gate #include <sys/vmsystm.h>
440Sstevel@tonic-gate #include <sys/flock.h>
450Sstevel@tonic-gate #include <sys/share.h>
460Sstevel@tonic-gate #include <sys/cmn_err.h>
470Sstevel@tonic-gate #include <sys/tiuser.h>
480Sstevel@tonic-gate #include <sys/sysmacros.h>
490Sstevel@tonic-gate #include <sys/callb.h>
500Sstevel@tonic-gate #include <sys/acl.h>
510Sstevel@tonic-gate #include <sys/kstat.h>
520Sstevel@tonic-gate #include <sys/signal.h>
530Sstevel@tonic-gate #include <sys/list.h>
540Sstevel@tonic-gate #include <sys/zone.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include <rpc/types.h>
570Sstevel@tonic-gate #include <rpc/xdr.h>
580Sstevel@tonic-gate #include <rpc/auth.h>
590Sstevel@tonic-gate #include <rpc/clnt.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include <nfs/nfs.h>
620Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <nfs/rnode.h>
650Sstevel@tonic-gate #include <nfs/nfs_acl.h>
660Sstevel@tonic-gate #include <nfs/lm.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <vm/hat.h>
690Sstevel@tonic-gate #include <vm/as.h>
700Sstevel@tonic-gate #include <vm/page.h>
710Sstevel@tonic-gate #include <vm/pvn.h>
720Sstevel@tonic-gate #include <vm/seg.h>
730Sstevel@tonic-gate #include <vm/seg_map.h>
740Sstevel@tonic-gate #include <vm/seg_vn.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate static void	nfs3_attr_cache(vnode_t *, vattr_t *, vattr_t *, hrtime_t,
770Sstevel@tonic-gate 			cred_t *);
780Sstevel@tonic-gate static int	nfs_getattr_cache(vnode_t *, struct vattr *);
790Sstevel@tonic-gate static int	nfs_remove_locking_id(vnode_t *, int, char *, char *, int *);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate struct mi_globals {
820Sstevel@tonic-gate 	kmutex_t	mig_lock;  /* lock protecting mig_list */
830Sstevel@tonic-gate 	list_t		mig_list;  /* list of NFS v2 or v3 mounts in zone */
840Sstevel@tonic-gate 	boolean_t	mig_destructor_called;
850Sstevel@tonic-gate };
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static zone_key_t mi_list_key;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /* Debugging flag for PC file shares. */
900Sstevel@tonic-gate extern int	share_debug;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * Attributes caching:
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * Attributes are cached in the rnode in struct vattr form.
960Sstevel@tonic-gate  * There is a time associated with the cached attributes (r_attrtime)
970Sstevel@tonic-gate  * which tells whether the attributes are valid. The time is initialized
980Sstevel@tonic-gate  * to the difference between current time and the modify time of the vnode
990Sstevel@tonic-gate  * when new attributes are cached. This allows the attributes for
1000Sstevel@tonic-gate  * files that have changed recently to be timed out sooner than for files
1010Sstevel@tonic-gate  * that have not changed for a long time. There are minimum and maximum
1020Sstevel@tonic-gate  * timeout values that can be set per mount point.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate int
nfs_waitfor_purge_complete(vnode_t * vp)1060Sstevel@tonic-gate nfs_waitfor_purge_complete(vnode_t *vp)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	rnode_t *rp;
1090Sstevel@tonic-gate 	k_sigset_t smask;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	rp = VTOR(vp);
1120Sstevel@tonic-gate 	if (rp->r_serial != NULL && rp->r_serial != curthread) {
1130Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
1140Sstevel@tonic-gate 		sigintr(&smask, VTOMI(vp)->mi_flags & MI_INT);
1150Sstevel@tonic-gate 		while (rp->r_serial != NULL) {
1160Sstevel@tonic-gate 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
1170Sstevel@tonic-gate 				sigunintr(&smask);
1180Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
1190Sstevel@tonic-gate 				return (EINTR);
1200Sstevel@tonic-gate 			}
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 		sigunintr(&smask);
1230Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 	return (0);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * Validate caches by checking cached attributes. If the cached
1300Sstevel@tonic-gate  * attributes have timed out, then get new attributes from the server.
1310Sstevel@tonic-gate  * As a side affect, this will do cache invalidation if the attributes
1320Sstevel@tonic-gate  * have changed.
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  * If the attributes have not timed out and if there is a cache
1350Sstevel@tonic-gate  * invalidation being done by some other thread, then wait until that
1360Sstevel@tonic-gate  * thread has completed the cache invalidation.
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate int
nfs_validate_caches(vnode_t * vp,cred_t * cr)1390Sstevel@tonic-gate nfs_validate_caches(vnode_t *vp, cred_t *cr)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	int error;
1420Sstevel@tonic-gate 	struct vattr va;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (ATTRCACHE_VALID(vp)) {
1450Sstevel@tonic-gate 		error = nfs_waitfor_purge_complete(vp);
1460Sstevel@tonic-gate 		if (error)
1470Sstevel@tonic-gate 			return (error);
1480Sstevel@tonic-gate 		return (0);
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	va.va_mask = AT_ALL;
1520Sstevel@tonic-gate 	return (nfs_getattr_otw(vp, &va, cr));
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * Validate caches by checking cached attributes. If the cached
1570Sstevel@tonic-gate  * attributes have timed out, then get new attributes from the server.
1580Sstevel@tonic-gate  * As a side affect, this will do cache invalidation if the attributes
1590Sstevel@tonic-gate  * have changed.
1600Sstevel@tonic-gate  *
1610Sstevel@tonic-gate  * If the attributes have not timed out and if there is a cache
1620Sstevel@tonic-gate  * invalidation being done by some other thread, then wait until that
1630Sstevel@tonic-gate  * thread has completed the cache invalidation.
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate int
nfs3_validate_caches(vnode_t * vp,cred_t * cr)1660Sstevel@tonic-gate nfs3_validate_caches(vnode_t *vp, cred_t *cr)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	int error;
1690Sstevel@tonic-gate 	struct vattr va;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (ATTRCACHE_VALID(vp)) {
1720Sstevel@tonic-gate 		error = nfs_waitfor_purge_complete(vp);
1730Sstevel@tonic-gate 		if (error)
1740Sstevel@tonic-gate 			return (error);
1750Sstevel@tonic-gate 		return (0);
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	va.va_mask = AT_ALL;
1790Sstevel@tonic-gate 	return (nfs3_getattr_otw(vp, &va, cr));
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate  * Purge all of the various NFS `data' caches.
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate void
nfs_purge_caches(vnode_t * vp,int purge_dnlc,cred_t * cr)1860Sstevel@tonic-gate nfs_purge_caches(vnode_t *vp, int purge_dnlc, cred_t *cr)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	rnode_t *rp;
1890Sstevel@tonic-gate 	char *contents;
1900Sstevel@tonic-gate 	int size;
1910Sstevel@tonic-gate 	int error;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/*
1940Sstevel@tonic-gate 	 * Purge the DNLC for any entries which refer to this file.
1950Sstevel@tonic-gate 	 * Avoid recursive entry into dnlc_purge_vp() in case of a directory.
1960Sstevel@tonic-gate 	 */
1970Sstevel@tonic-gate 	rp = VTOR(vp);
1980Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
1990Sstevel@tonic-gate 	if (vp->v_count > 1 &&
2000Sstevel@tonic-gate 	    (vp->v_type == VDIR || purge_dnlc == NFS_PURGE_DNLC) &&
2010Sstevel@tonic-gate 	    !(rp->r_flags & RINDNLCPURGE)) {
2020Sstevel@tonic-gate 		/*
2030Sstevel@tonic-gate 		 * Set the RINDNLCPURGE flag to prevent recursive entry
2040Sstevel@tonic-gate 		 * into dnlc_purge_vp()
2050Sstevel@tonic-gate 		 */
2060Sstevel@tonic-gate 		if (vp->v_type == VDIR)
2070Sstevel@tonic-gate 			rp->r_flags |= RINDNLCPURGE;
2080Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
2090Sstevel@tonic-gate 		dnlc_purge_vp(vp);
2100Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
2110Sstevel@tonic-gate 		if (rp->r_flags & RINDNLCPURGE)
2120Sstevel@tonic-gate 			rp->r_flags &= ~RINDNLCPURGE;
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	/*
2160Sstevel@tonic-gate 	 * Clear any readdir state bits and purge the readlink response cache.
2170Sstevel@tonic-gate 	 */
2180Sstevel@tonic-gate 	contents = rp->r_symlink.contents;
2190Sstevel@tonic-gate 	size = rp->r_symlink.size;
2200Sstevel@tonic-gate 	rp->r_symlink.contents = NULL;
2210Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (contents != NULL) {
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		kmem_free((void *)contents, size);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/*
2290Sstevel@tonic-gate 	 * Flush the page cache.
2300Sstevel@tonic-gate 	 */
2310Sstevel@tonic-gate 	if (vn_has_cached_data(vp)) {
2325331Samw 		error = VOP_PUTPAGE(vp, (u_offset_t)0, 0, B_INVAL, cr, NULL);
2330Sstevel@tonic-gate 		if (error && (error == ENOSPC || error == EDQUOT)) {
2340Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
2350Sstevel@tonic-gate 			if (!rp->r_error)
2360Sstevel@tonic-gate 				rp->r_error = error;
2370Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/*
2420Sstevel@tonic-gate 	 * Flush the readdir response cache.
2430Sstevel@tonic-gate 	 */
2440Sstevel@tonic-gate 	if (HAVE_RDDIR_CACHE(rp))
2450Sstevel@tonic-gate 		nfs_purge_rddir_cache(vp);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  * Purge the readdir cache of all entries
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate void
nfs_purge_rddir_cache(vnode_t * vp)2520Sstevel@tonic-gate nfs_purge_rddir_cache(vnode_t *vp)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	rnode_t *rp;
2550Sstevel@tonic-gate 	rddir_cache *rdc;
2560Sstevel@tonic-gate 	rddir_cache *nrdc;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	rp = VTOR(vp);
2590Sstevel@tonic-gate top:
2600Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
2610Sstevel@tonic-gate 	rp->r_direof = NULL;
2620Sstevel@tonic-gate 	rp->r_flags &= ~RLOOKUP;
2630Sstevel@tonic-gate 	rp->r_flags |= RREADDIRPLUS;
2640Sstevel@tonic-gate 	rdc = avl_first(&rp->r_dir);
2650Sstevel@tonic-gate 	while (rdc != NULL) {
2660Sstevel@tonic-gate 		nrdc = AVL_NEXT(&rp->r_dir, rdc);
2670Sstevel@tonic-gate 		avl_remove(&rp->r_dir, rdc);
2680Sstevel@tonic-gate 		rddir_cache_rele(rdc);
2690Sstevel@tonic-gate 		rdc = nrdc;
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate  * Do a cache check based on the post-operation attributes.
2760Sstevel@tonic-gate  * Then make them the new cached attributes.  If no attributes
2770Sstevel@tonic-gate  * were returned, then mark the attributes as timed out.
2780Sstevel@tonic-gate  */
2790Sstevel@tonic-gate void
nfs3_cache_post_op_attr(vnode_t * vp,post_op_attr * poap,hrtime_t t,cred_t * cr)2800Sstevel@tonic-gate nfs3_cache_post_op_attr(vnode_t *vp, post_op_attr *poap, hrtime_t t, cred_t *cr)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	vattr_t attr;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if (!poap->attributes) {
2850Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
2860Sstevel@tonic-gate 		return;
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	(void) nfs3_cache_fattr3(vp, &poap->attr, &attr, t, cr);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate  * Same as above, but using a vattr
2930Sstevel@tonic-gate  */
2940Sstevel@tonic-gate void
nfs3_cache_post_op_vattr(vnode_t * vp,post_op_vattr * poap,hrtime_t t,cred_t * cr)2950Sstevel@tonic-gate nfs3_cache_post_op_vattr(vnode_t *vp, post_op_vattr *poap, hrtime_t t,
2960Sstevel@tonic-gate     cred_t *cr)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	if (!poap->attributes) {
2990Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
3000Sstevel@tonic-gate 		return;
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 	nfs_attr_cache(vp, poap->fres.vap, t, cr);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate  * Do a cache check based on the weak cache consistency attributes.
3070Sstevel@tonic-gate  * These consist of a small set of pre-operation attributes and the
3080Sstevel@tonic-gate  * full set of post-operation attributes.
3090Sstevel@tonic-gate  *
3100Sstevel@tonic-gate  * If we are given the pre-operation attributes, then use them to
3110Sstevel@tonic-gate  * check the validity of the various caches.  Then, if we got the
3120Sstevel@tonic-gate  * post-operation attributes, make them the new cached attributes.
3130Sstevel@tonic-gate  * If we didn't get the post-operation attributes, then mark the
3140Sstevel@tonic-gate  * attribute cache as timed out so that the next reference will
3150Sstevel@tonic-gate  * cause a GETATTR to the server to refresh with the current
3160Sstevel@tonic-gate  * attributes.
3170Sstevel@tonic-gate  *
3180Sstevel@tonic-gate  * Otherwise, if we didn't get the pre-operation attributes, but
3190Sstevel@tonic-gate  * we did get the post-operation attributes, then use these
3200Sstevel@tonic-gate  * attributes to check the validity of the various caches.  This
3210Sstevel@tonic-gate  * will probably cause a flush of the caches because if the
3220Sstevel@tonic-gate  * operation succeeded, the attributes of the object were changed
3230Sstevel@tonic-gate  * in some way from the old post-operation attributes.  This
3240Sstevel@tonic-gate  * should be okay because it is the safe thing to do.  After
3250Sstevel@tonic-gate  * checking the data caches, then we make these the new cached
3260Sstevel@tonic-gate  * attributes.
3270Sstevel@tonic-gate  *
3280Sstevel@tonic-gate  * Otherwise, we didn't get either the pre- or post-operation
3290Sstevel@tonic-gate  * attributes.  Simply mark the attribute cache as timed out so
3300Sstevel@tonic-gate  * the next reference will cause a GETATTR to the server to
3310Sstevel@tonic-gate  * refresh with the current attributes.
3320Sstevel@tonic-gate  *
3330Sstevel@tonic-gate  * If an error occurred trying to convert the over the wire
3340Sstevel@tonic-gate  * attributes to a vattr, then simply mark the attribute cache as
3350Sstevel@tonic-gate  * timed out.
3360Sstevel@tonic-gate  */
3370Sstevel@tonic-gate void
nfs3_cache_wcc_data(vnode_t * vp,wcc_data * wccp,hrtime_t t,cred_t * cr)3380Sstevel@tonic-gate nfs3_cache_wcc_data(vnode_t *vp, wcc_data *wccp, hrtime_t t, cred_t *cr)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	vattr_t bva;
3410Sstevel@tonic-gate 	vattr_t ava;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (wccp->after.attributes) {
3440Sstevel@tonic-gate 		if (fattr3_to_vattr(vp, &wccp->after.attr, &ava)) {
3450Sstevel@tonic-gate 			PURGE_ATTRCACHE(vp);
3460Sstevel@tonic-gate 			return;
3470Sstevel@tonic-gate 		}
3480Sstevel@tonic-gate 		if (wccp->before.attributes) {
3490Sstevel@tonic-gate 			bva.va_ctime.tv_sec = wccp->before.attr.ctime.seconds;
3500Sstevel@tonic-gate 			bva.va_ctime.tv_nsec = wccp->before.attr.ctime.nseconds;
3510Sstevel@tonic-gate 			bva.va_mtime.tv_sec = wccp->before.attr.mtime.seconds;
3520Sstevel@tonic-gate 			bva.va_mtime.tv_nsec = wccp->before.attr.mtime.nseconds;
3530Sstevel@tonic-gate 			bva.va_size = wccp->before.attr.size;
3540Sstevel@tonic-gate 			nfs3_attr_cache(vp, &bva, &ava, t, cr);
3550Sstevel@tonic-gate 		} else
3560Sstevel@tonic-gate 			nfs_attr_cache(vp, &ava, t, cr);
3570Sstevel@tonic-gate 	} else {
3580Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate /*
3630Sstevel@tonic-gate  * Set attributes cache for given vnode using nfsattr.
3640Sstevel@tonic-gate  *
3650Sstevel@tonic-gate  * This routine does not do cache validation with the attributes.
3660Sstevel@tonic-gate  *
3670Sstevel@tonic-gate  * If an error occurred trying to convert the over the wire
3680Sstevel@tonic-gate  * attributes to a vattr, then simply mark the attribute cache as
3690Sstevel@tonic-gate  * timed out.
3700Sstevel@tonic-gate  */
3710Sstevel@tonic-gate void
nfs_attrcache(vnode_t * vp,struct nfsfattr * na,hrtime_t t)3720Sstevel@tonic-gate nfs_attrcache(vnode_t *vp, struct nfsfattr *na, hrtime_t t)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	rnode_t *rp;
3750Sstevel@tonic-gate 	struct vattr va;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	if (!nattr_to_vattr(vp, na, &va)) {
3780Sstevel@tonic-gate 		rp = VTOR(vp);
3790Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
3800Sstevel@tonic-gate 		if (rp->r_mtime <= t)
3810Sstevel@tonic-gate 			nfs_attrcache_va(vp, &va);
3820Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
3830Sstevel@tonic-gate 	} else {
3840Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate  * Set attributes cache for given vnode using fattr3.
3900Sstevel@tonic-gate  *
3910Sstevel@tonic-gate  * This routine does not do cache validation with the attributes.
3920Sstevel@tonic-gate  *
3930Sstevel@tonic-gate  * If an error occurred trying to convert the over the wire
3940Sstevel@tonic-gate  * attributes to a vattr, then simply mark the attribute cache as
3950Sstevel@tonic-gate  * timed out.
3960Sstevel@tonic-gate  */
3970Sstevel@tonic-gate void
nfs3_attrcache(vnode_t * vp,fattr3 * na,hrtime_t t)3980Sstevel@tonic-gate nfs3_attrcache(vnode_t *vp, fattr3 *na, hrtime_t t)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	rnode_t *rp;
4010Sstevel@tonic-gate 	struct vattr va;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	if (!fattr3_to_vattr(vp, na, &va)) {
4040Sstevel@tonic-gate 		rp = VTOR(vp);
4050Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
4060Sstevel@tonic-gate 		if (rp->r_mtime <= t)
4070Sstevel@tonic-gate 			nfs_attrcache_va(vp, &va);
4080Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
4090Sstevel@tonic-gate 	} else {
4100Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate  * Do a cache check based on attributes returned over the wire.  The
4160Sstevel@tonic-gate  * new attributes are cached.
4170Sstevel@tonic-gate  *
4180Sstevel@tonic-gate  * If an error occurred trying to convert the over the wire attributes
4190Sstevel@tonic-gate  * to a vattr, then just return that error.
4200Sstevel@tonic-gate  *
4210Sstevel@tonic-gate  * As a side affect, the vattr argument is filled in with the converted
4220Sstevel@tonic-gate  * attributes.
4230Sstevel@tonic-gate  */
4240Sstevel@tonic-gate int
nfs_cache_fattr(vnode_t * vp,struct nfsfattr * na,vattr_t * vap,hrtime_t t,cred_t * cr)4250Sstevel@tonic-gate nfs_cache_fattr(vnode_t *vp, struct nfsfattr *na, vattr_t *vap, hrtime_t t,
4260Sstevel@tonic-gate     cred_t *cr)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	int error;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	error = nattr_to_vattr(vp, na, vap);
4310Sstevel@tonic-gate 	if (error)
4320Sstevel@tonic-gate 		return (error);
4330Sstevel@tonic-gate 	nfs_attr_cache(vp, vap, t, cr);
4340Sstevel@tonic-gate 	return (0);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate  * Do a cache check based on attributes returned over the wire.  The
4390Sstevel@tonic-gate  * new attributes are cached.
4400Sstevel@tonic-gate  *
4410Sstevel@tonic-gate  * If an error occurred trying to convert the over the wire attributes
4420Sstevel@tonic-gate  * to a vattr, then just return that error.
4430Sstevel@tonic-gate  *
4440Sstevel@tonic-gate  * As a side affect, the vattr argument is filled in with the converted
4450Sstevel@tonic-gate  * attributes.
4460Sstevel@tonic-gate  */
4470Sstevel@tonic-gate int
nfs3_cache_fattr3(vnode_t * vp,fattr3 * na,vattr_t * vap,hrtime_t t,cred_t * cr)4480Sstevel@tonic-gate nfs3_cache_fattr3(vnode_t *vp, fattr3 *na, vattr_t *vap, hrtime_t t, cred_t *cr)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	int error;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	error = fattr3_to_vattr(vp, na, vap);
4530Sstevel@tonic-gate 	if (error)
4540Sstevel@tonic-gate 		return (error);
4550Sstevel@tonic-gate 	nfs_attr_cache(vp, vap, t, cr);
4560Sstevel@tonic-gate 	return (0);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate /*
4600Sstevel@tonic-gate  * Use the passed in virtual attributes to check to see whether the
4610Sstevel@tonic-gate  * data and metadata caches are valid, cache the new attributes, and
4620Sstevel@tonic-gate  * then do the cache invalidation if required.
4630Sstevel@tonic-gate  *
4640Sstevel@tonic-gate  * The cache validation and caching of the new attributes is done
4650Sstevel@tonic-gate  * atomically via the use of the mutex, r_statelock.  If required,
4660Sstevel@tonic-gate  * the cache invalidation is done atomically w.r.t. the cache
4670Sstevel@tonic-gate  * validation and caching of the attributes via the pseudo lock,
4680Sstevel@tonic-gate  * r_serial.
4690Sstevel@tonic-gate  *
4700Sstevel@tonic-gate  * This routine is used to do cache validation and attributes caching
4710Sstevel@tonic-gate  * for operations with a single set of post operation attributes.
4720Sstevel@tonic-gate  */
4730Sstevel@tonic-gate void
nfs_attr_cache(vnode_t * vp,vattr_t * vap,hrtime_t t,cred_t * cr)4740Sstevel@tonic-gate nfs_attr_cache(vnode_t *vp, vattr_t *vap, hrtime_t t, cred_t *cr)
4750Sstevel@tonic-gate {
4760Sstevel@tonic-gate 	rnode_t *rp;
4775486Svv149972 	int mtime_changed = 0;
4785486Svv149972 	int ctime_changed = 0;
4790Sstevel@tonic-gate 	vsecattr_t *vsp;
4800Sstevel@tonic-gate 	int was_serial;
4815486Svv149972 	len_t preattr_rsize;
4825486Svv149972 	boolean_t writeattr_set = B_FALSE;
4835486Svv149972 	boolean_t cachepurge_set = B_FALSE;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	rp = VTOR(vp);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if (rp->r_serial != curthread) {
4900Sstevel@tonic-gate 		klwp_t *lwp = ttolwp(curthread);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		was_serial = 0;
4930Sstevel@tonic-gate 		if (lwp != NULL)
4940Sstevel@tonic-gate 			lwp->lwp_nostop++;
4950Sstevel@tonic-gate 		while (rp->r_serial != NULL) {
4960Sstevel@tonic-gate 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
4970Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
4980Sstevel@tonic-gate 				if (lwp != NULL)
4990Sstevel@tonic-gate 					lwp->lwp_nostop--;
5000Sstevel@tonic-gate 				return;
5010Sstevel@tonic-gate 			}
5020Sstevel@tonic-gate 		}
5030Sstevel@tonic-gate 		if (lwp != NULL)
5040Sstevel@tonic-gate 			lwp->lwp_nostop--;
5050Sstevel@tonic-gate 	} else
5060Sstevel@tonic-gate 		was_serial = 1;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	if (rp->r_mtime > t) {
5093279Smaheshvs 		if (!CACHE_VALID(rp, vap->va_mtime, vap->va_size))
5103279Smaheshvs 			PURGE_ATTRCACHE_LOCKED(rp);
5110Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
5120Sstevel@tonic-gate 		return;
5130Sstevel@tonic-gate 	}
5140Sstevel@tonic-gate 
5155486Svv149972 	/*
5165486Svv149972 	 * Write thread after writing data to file on remote server,
5175486Svv149972 	 * will always set RWRITEATTR to indicate that file on remote
5185486Svv149972 	 * server was modified with a WRITE operation and would have
5195486Svv149972 	 * marked attribute cache as timed out. If RWRITEATTR
5205486Svv149972 	 * is set, then do not check for mtime and ctime change.
5215486Svv149972 	 */
5220Sstevel@tonic-gate 	if (!(rp->r_flags & RWRITEATTR)) {
5230Sstevel@tonic-gate 		if (!CACHE_VALID(rp, vap->va_mtime, vap->va_size))
5240Sstevel@tonic-gate 			mtime_changed = 1;
5255486Svv149972 
5260Sstevel@tonic-gate 		if (rp->r_attr.va_ctime.tv_sec != vap->va_ctime.tv_sec ||
5270Sstevel@tonic-gate 		    rp->r_attr.va_ctime.tv_nsec != vap->va_ctime.tv_nsec)
5280Sstevel@tonic-gate 			ctime_changed = 1;
5290Sstevel@tonic-gate 	} else {
5305486Svv149972 		writeattr_set = B_TRUE;
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5335486Svv149972 	preattr_rsize = rp->r_size;
5345486Svv149972 
5350Sstevel@tonic-gate 	nfs_attrcache_va(vp, vap);
5360Sstevel@tonic-gate 
5375486Svv149972 	/*
5385486Svv149972 	 * If we have updated filesize in nfs_attrcache_va, as soon as we
5395486Svv149972 	 * drop statelock we will be in transition of purging all
5405486Svv149972 	 * our caches and updating them. It is possible for another
5415486Svv149972 	 * thread to pick this new file size and read in zeroed data.
5425486Svv149972 	 * stall other threads till cache purge is complete.
5435486Svv149972 	 */
5445486Svv149972 	if ((vp->v_type == VREG) && (rp->r_size != preattr_rsize)) {
5455486Svv149972 		/*
5465486Svv149972 		 * If RWRITEATTR was set and we have updated the file
5475486Svv149972 		 * size, Server's returned file size need not necessarily
5485486Svv149972 		 * be because of this Client's WRITE. We need to purge
5495486Svv149972 		 * all caches.
5505486Svv149972 		 */
5515486Svv149972 		if (writeattr_set)
5525486Svv149972 			mtime_changed = 1;
5535486Svv149972 
5545486Svv149972 		if (mtime_changed && !(rp->r_flags & RINCACHEPURGE)) {
5555486Svv149972 			rp->r_flags |= RINCACHEPURGE;
5565486Svv149972 			cachepurge_set = B_TRUE;
5575486Svv149972 		}
5585486Svv149972 	}
5595486Svv149972 
5600Sstevel@tonic-gate 	if (!mtime_changed && !ctime_changed) {
5610Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
5620Sstevel@tonic-gate 		return;
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	rp->r_serial = curthread;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	if (mtime_changed)
5700Sstevel@tonic-gate 		nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
5710Sstevel@tonic-gate 
5725486Svv149972 	if ((rp->r_flags & RINCACHEPURGE) && cachepurge_set) {
5735486Svv149972 		mutex_enter(&rp->r_statelock);
5745486Svv149972 		rp->r_flags &= ~RINCACHEPURGE;
5755486Svv149972 		cv_broadcast(&rp->r_cv);
5765486Svv149972 		mutex_exit(&rp->r_statelock);
5775486Svv149972 		cachepurge_set = B_FALSE;
5785486Svv149972 	}
5795486Svv149972 
5800Sstevel@tonic-gate 	if (ctime_changed) {
5810Sstevel@tonic-gate 		(void) nfs_access_purge_rp(rp);
5820Sstevel@tonic-gate 		if (rp->r_secattr != NULL) {
5830Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
5840Sstevel@tonic-gate 			vsp = rp->r_secattr;
5850Sstevel@tonic-gate 			rp->r_secattr = NULL;
5860Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
5870Sstevel@tonic-gate 			if (vsp != NULL)
5880Sstevel@tonic-gate 				nfs_acl_free(vsp);
5890Sstevel@tonic-gate 		}
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	if (!was_serial) {
5930Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
5940Sstevel@tonic-gate 		rp->r_serial = NULL;
5950Sstevel@tonic-gate 		cv_broadcast(&rp->r_cv);
5960Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate /*
6010Sstevel@tonic-gate  * Use the passed in "before" virtual attributes to check to see
6020Sstevel@tonic-gate  * whether the data and metadata caches are valid, cache the "after"
6030Sstevel@tonic-gate  * new attributes, and then do the cache invalidation if required.
6040Sstevel@tonic-gate  *
6050Sstevel@tonic-gate  * The cache validation and caching of the new attributes is done
6060Sstevel@tonic-gate  * atomically via the use of the mutex, r_statelock.  If required,
6070Sstevel@tonic-gate  * the cache invalidation is done atomically w.r.t. the cache
6080Sstevel@tonic-gate  * validation and caching of the attributes via the pseudo lock,
6090Sstevel@tonic-gate  * r_serial.
6100Sstevel@tonic-gate  *
6110Sstevel@tonic-gate  * This routine is used to do cache validation and attributes caching
6120Sstevel@tonic-gate  * for operations with both pre operation attributes and post operation
6130Sstevel@tonic-gate  * attributes.
6140Sstevel@tonic-gate  */
6150Sstevel@tonic-gate static void
nfs3_attr_cache(vnode_t * vp,vattr_t * bvap,vattr_t * avap,hrtime_t t,cred_t * cr)6160Sstevel@tonic-gate nfs3_attr_cache(vnode_t *vp, vattr_t *bvap, vattr_t *avap, hrtime_t t,
6170Sstevel@tonic-gate     cred_t *cr)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate 	rnode_t *rp;
6205486Svv149972 	int mtime_changed = 0;
6215486Svv149972 	int ctime_changed = 0;
6220Sstevel@tonic-gate 	vsecattr_t *vsp;
6230Sstevel@tonic-gate 	int was_serial;
6245486Svv149972 	len_t preattr_rsize;
6255486Svv149972 	boolean_t writeattr_set = B_FALSE;
6265486Svv149972 	boolean_t cachepurge_set = B_FALSE;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	rp = VTOR(vp);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	if (rp->r_serial != curthread) {
6330Sstevel@tonic-gate 		klwp_t *lwp = ttolwp(curthread);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		was_serial = 0;
6360Sstevel@tonic-gate 		if (lwp != NULL)
6370Sstevel@tonic-gate 			lwp->lwp_nostop++;
6380Sstevel@tonic-gate 		while (rp->r_serial != NULL) {
6390Sstevel@tonic-gate 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
6400Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
6410Sstevel@tonic-gate 				if (lwp != NULL)
6420Sstevel@tonic-gate 					lwp->lwp_nostop--;
6430Sstevel@tonic-gate 				return;
6440Sstevel@tonic-gate 			}
6450Sstevel@tonic-gate 		}
6460Sstevel@tonic-gate 		if (lwp != NULL)
6470Sstevel@tonic-gate 			lwp->lwp_nostop--;
6480Sstevel@tonic-gate 	} else
6490Sstevel@tonic-gate 		was_serial = 1;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	if (rp->r_mtime > t) {
6523279Smaheshvs 		if (!CACHE_VALID(rp, avap->va_mtime, avap->va_size))
6533279Smaheshvs 			PURGE_ATTRCACHE_LOCKED(rp);
6540Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
6550Sstevel@tonic-gate 		return;
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6585486Svv149972 	/*
6595486Svv149972 	 * Write thread after writing data to file on remote server,
6605486Svv149972 	 * will always set RWRITEATTR to indicate that file on remote
6615486Svv149972 	 * server was modified with a WRITE operation and would have
6625486Svv149972 	 * marked attribute cache as timed out. If RWRITEATTR
6635486Svv149972 	 * is set, then do not check for mtime and ctime change.
6645486Svv149972 	 */
6650Sstevel@tonic-gate 	if (!(rp->r_flags & RWRITEATTR)) {
6660Sstevel@tonic-gate 		if (!CACHE_VALID(rp, bvap->va_mtime, bvap->va_size))
6670Sstevel@tonic-gate 			mtime_changed = 1;
6685486Svv149972 
6690Sstevel@tonic-gate 		if (rp->r_attr.va_ctime.tv_sec != bvap->va_ctime.tv_sec ||
6700Sstevel@tonic-gate 		    rp->r_attr.va_ctime.tv_nsec != bvap->va_ctime.tv_nsec)
6710Sstevel@tonic-gate 			ctime_changed = 1;
6720Sstevel@tonic-gate 	} else {
6735486Svv149972 		writeattr_set = B_TRUE;
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
6765486Svv149972 	preattr_rsize = rp->r_size;
6775486Svv149972 
6780Sstevel@tonic-gate 	nfs_attrcache_va(vp, avap);
6790Sstevel@tonic-gate 
6805486Svv149972 	/*
6815486Svv149972 	 * If we have updated filesize in nfs_attrcache_va, as soon as we
6825486Svv149972 	 * drop statelock we will be in transition of purging all
6835486Svv149972 	 * our caches and updating them. It is possible for another
6845486Svv149972 	 * thread to pick this new file size and read in zeroed data.
6855486Svv149972 	 * stall other threads till cache purge is complete.
6865486Svv149972 	 */
6875486Svv149972 	if ((vp->v_type == VREG) && (rp->r_size != preattr_rsize)) {
6885486Svv149972 		/*
6895486Svv149972 		 * If RWRITEATTR was set and we have updated the file
6905486Svv149972 		 * size, Server's returned file size need not necessarily
6915486Svv149972 		 * be because of this Client's WRITE. We need to purge
6925486Svv149972 		 * all caches.
6935486Svv149972 		 */
6945486Svv149972 		if (writeattr_set)
6955486Svv149972 			mtime_changed = 1;
6965486Svv149972 
6975486Svv149972 		if (mtime_changed && !(rp->r_flags & RINCACHEPURGE)) {
6985486Svv149972 			rp->r_flags |= RINCACHEPURGE;
6995486Svv149972 			cachepurge_set = B_TRUE;
7005486Svv149972 		}
7015486Svv149972 	}
7025486Svv149972 
7030Sstevel@tonic-gate 	if (!mtime_changed && !ctime_changed) {
7040Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
7050Sstevel@tonic-gate 		return;
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	rp->r_serial = curthread;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	if (mtime_changed)
7130Sstevel@tonic-gate 		nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
7140Sstevel@tonic-gate 
7155486Svv149972 	if ((rp->r_flags & RINCACHEPURGE) && cachepurge_set) {
7165486Svv149972 		mutex_enter(&rp->r_statelock);
7175486Svv149972 		rp->r_flags &= ~RINCACHEPURGE;
7185486Svv149972 		cv_broadcast(&rp->r_cv);
7195486Svv149972 		mutex_exit(&rp->r_statelock);
7205486Svv149972 		cachepurge_set = B_FALSE;
7215486Svv149972 	}
7225486Svv149972 
7230Sstevel@tonic-gate 	if (ctime_changed) {
7240Sstevel@tonic-gate 		(void) nfs_access_purge_rp(rp);
7250Sstevel@tonic-gate 		if (rp->r_secattr != NULL) {
7260Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
7270Sstevel@tonic-gate 			vsp = rp->r_secattr;
7280Sstevel@tonic-gate 			rp->r_secattr = NULL;
7290Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
7300Sstevel@tonic-gate 			if (vsp != NULL)
7310Sstevel@tonic-gate 				nfs_acl_free(vsp);
7320Sstevel@tonic-gate 		}
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	if (!was_serial) {
7360Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
7370Sstevel@tonic-gate 		rp->r_serial = NULL;
7380Sstevel@tonic-gate 		cv_broadcast(&rp->r_cv);
7390Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate /*
7440Sstevel@tonic-gate  * Set attributes cache for given vnode using virtual attributes.
7450Sstevel@tonic-gate  *
7460Sstevel@tonic-gate  * Set the timeout value on the attribute cache and fill it
7470Sstevel@tonic-gate  * with the passed in attributes.
7480Sstevel@tonic-gate  *
7490Sstevel@tonic-gate  * The caller must be holding r_statelock.
7500Sstevel@tonic-gate  */
7510Sstevel@tonic-gate void
nfs_attrcache_va(vnode_t * vp,struct vattr * va)7520Sstevel@tonic-gate nfs_attrcache_va(vnode_t *vp, struct vattr *va)
7530Sstevel@tonic-gate {
7540Sstevel@tonic-gate 	rnode_t *rp;
7550Sstevel@tonic-gate 	mntinfo_t *mi;
7560Sstevel@tonic-gate 	hrtime_t delta;
7570Sstevel@tonic-gate 	hrtime_t now;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	rp = VTOR(vp);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&rp->r_statelock));
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	now = gethrtime();
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	mi = VTOMI(vp);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	/*
7680Sstevel@tonic-gate 	 * Delta is the number of nanoseconds that we will
7690Sstevel@tonic-gate 	 * cache the attributes of the file.  It is based on
7700Sstevel@tonic-gate 	 * the number of nanoseconds since the last time that
7710Sstevel@tonic-gate 	 * we detected a change.  The assumption is that files
7720Sstevel@tonic-gate 	 * that changed recently are likely to change again.
7730Sstevel@tonic-gate 	 * There is a minimum and a maximum for regular files
7740Sstevel@tonic-gate 	 * and for directories which is enforced though.
7750Sstevel@tonic-gate 	 *
7760Sstevel@tonic-gate 	 * Using the time since last change was detected
7770Sstevel@tonic-gate 	 * eliminates direct comparison or calculation
7780Sstevel@tonic-gate 	 * using mixed client and server times.  NFS does
7790Sstevel@tonic-gate 	 * not make any assumptions regarding the client
7800Sstevel@tonic-gate 	 * and server clocks being synchronized.
7810Sstevel@tonic-gate 	 */
7820Sstevel@tonic-gate 	if (va->va_mtime.tv_sec != rp->r_attr.va_mtime.tv_sec ||
7830Sstevel@tonic-gate 	    va->va_mtime.tv_nsec != rp->r_attr.va_mtime.tv_nsec ||
7840Sstevel@tonic-gate 	    va->va_size != rp->r_attr.va_size)
7850Sstevel@tonic-gate 		rp->r_mtime = now;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	if ((mi->mi_flags & MI_NOAC) || (vp->v_flag & VNOCACHE))
7880Sstevel@tonic-gate 		delta = 0;
7890Sstevel@tonic-gate 	else {
7900Sstevel@tonic-gate 		delta = now - rp->r_mtime;
7910Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
7920Sstevel@tonic-gate 			if (delta < mi->mi_acdirmin)
7930Sstevel@tonic-gate 				delta = mi->mi_acdirmin;
7940Sstevel@tonic-gate 			else if (delta > mi->mi_acdirmax)
7950Sstevel@tonic-gate 				delta = mi->mi_acdirmax;
7960Sstevel@tonic-gate 		} else {
7970Sstevel@tonic-gate 			if (delta < mi->mi_acregmin)
7980Sstevel@tonic-gate 				delta = mi->mi_acregmin;
7990Sstevel@tonic-gate 			else if (delta > mi->mi_acregmax)
8000Sstevel@tonic-gate 				delta = mi->mi_acregmax;
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate 	}
8030Sstevel@tonic-gate 	rp->r_attrtime = now + delta;
8040Sstevel@tonic-gate 	rp->r_attr = *va;
8050Sstevel@tonic-gate 	/*
8060Sstevel@tonic-gate 	 * Update the size of the file if there is no cached data or if
8070Sstevel@tonic-gate 	 * the cached data is clean and there is no data being written
8080Sstevel@tonic-gate 	 * out.
8090Sstevel@tonic-gate 	 */
8100Sstevel@tonic-gate 	if (rp->r_size != va->va_size &&
8110Sstevel@tonic-gate 	    (!vn_has_cached_data(vp) ||
8120Sstevel@tonic-gate 	    (!(rp->r_flags & RDIRTY) && rp->r_count == 0)))
8130Sstevel@tonic-gate 		rp->r_size = va->va_size;
8140Sstevel@tonic-gate 	nfs_setswaplike(vp, va);
8150Sstevel@tonic-gate 	rp->r_flags &= ~RWRITEATTR;
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate /*
8190Sstevel@tonic-gate  * Fill in attribute from the cache.
8200Sstevel@tonic-gate  * If valid, then return 0 to indicate that no error occurred,
8210Sstevel@tonic-gate  * otherwise return 1 to indicate that an error occurred.
8220Sstevel@tonic-gate  */
8230Sstevel@tonic-gate static int
nfs_getattr_cache(vnode_t * vp,struct vattr * vap)8240Sstevel@tonic-gate nfs_getattr_cache(vnode_t *vp, struct vattr *vap)
8250Sstevel@tonic-gate {
8260Sstevel@tonic-gate 	rnode_t *rp;
8279750SGarima.Tripathi@Sun.COM 	uint_t mask = vap->va_mask;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	rp = VTOR(vp);
8300Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
8310Sstevel@tonic-gate 	if (ATTRCACHE_VALID(vp)) {
8320Sstevel@tonic-gate 		/*
8330Sstevel@tonic-gate 		 * Cached attributes are valid
8340Sstevel@tonic-gate 		 */
8350Sstevel@tonic-gate 		*vap = rp->r_attr;
8369750SGarima.Tripathi@Sun.COM 		/*
8379750SGarima.Tripathi@Sun.COM 		 * Set the caller's va_mask to the set of attributes
8389750SGarima.Tripathi@Sun.COM 		 * that were requested ANDed with the attributes that
8399750SGarima.Tripathi@Sun.COM 		 * are available.  If attributes were requested that
8409750SGarima.Tripathi@Sun.COM 		 * are not available, those bits must be turned off
8419750SGarima.Tripathi@Sun.COM 		 * in the callers va_mask.
8429750SGarima.Tripathi@Sun.COM 		 */
8439750SGarima.Tripathi@Sun.COM 		vap->va_mask &= mask;
8440Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
8450Sstevel@tonic-gate 		return (0);
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
8480Sstevel@tonic-gate 	return (1);
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate /*
8520Sstevel@tonic-gate  * Get attributes over-the-wire and update attributes cache
8530Sstevel@tonic-gate  * if no error occurred in the over-the-wire operation.
8540Sstevel@tonic-gate  * Return 0 if successful, otherwise error.
8550Sstevel@tonic-gate  */
8560Sstevel@tonic-gate int
nfs_getattr_otw(vnode_t * vp,struct vattr * vap,cred_t * cr)8570Sstevel@tonic-gate nfs_getattr_otw(vnode_t *vp, struct vattr *vap, cred_t *cr)
8580Sstevel@tonic-gate {
8590Sstevel@tonic-gate 	int error;
8600Sstevel@tonic-gate 	struct nfsattrstat ns;
8610Sstevel@tonic-gate 	int douprintf;
8620Sstevel@tonic-gate 	mntinfo_t *mi;
8630Sstevel@tonic-gate 	failinfo_t fi;
8640Sstevel@tonic-gate 	hrtime_t t;
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	mi = VTOMI(vp);
8670Sstevel@tonic-gate 	fi.vp = vp;
8680Sstevel@tonic-gate 	fi.fhp = NULL;		/* no need to update, filehandle not copied */
8690Sstevel@tonic-gate 	fi.copyproc = nfscopyfh;
8700Sstevel@tonic-gate 	fi.lookupproc = nfslookup;
8710Sstevel@tonic-gate 	fi.xattrdirproc = acl_getxattrdir2;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	if (mi->mi_flags & MI_ACL) {
8740Sstevel@tonic-gate 		error = acl_getattr2_otw(vp, vap, cr);
8750Sstevel@tonic-gate 		if (mi->mi_flags & MI_ACL)
8760Sstevel@tonic-gate 			return (error);
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	douprintf = 1;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	t = gethrtime();
8820Sstevel@tonic-gate 
8838879SSuhasini.Peddada@Sun.COM 	error = rfs2call(mi, RFS_GETATTR,
8849750SGarima.Tripathi@Sun.COM 	    xdr_fhandle, (caddr_t)VTOFH(vp),
8859750SGarima.Tripathi@Sun.COM 	    xdr_attrstat, (caddr_t)&ns, cr,
8869750SGarima.Tripathi@Sun.COM 	    &douprintf, &ns.ns_status, 0, &fi);
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	if (!error) {
8890Sstevel@tonic-gate 		error = geterrno(ns.ns_status);
8900Sstevel@tonic-gate 		if (!error)
8910Sstevel@tonic-gate 			error = nfs_cache_fattr(vp, &ns.ns_attr, vap, t, cr);
8920Sstevel@tonic-gate 		else {
8930Sstevel@tonic-gate 			PURGE_STALE_FH(error, vp, cr);
8940Sstevel@tonic-gate 		}
8950Sstevel@tonic-gate 	}
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	return (error);
8980Sstevel@tonic-gate }
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate /*
9010Sstevel@tonic-gate  * Return either cached ot remote attributes. If get remote attr
9020Sstevel@tonic-gate  * use them to check and invalidate caches, then cache the new attributes.
9030Sstevel@tonic-gate  */
9040Sstevel@tonic-gate int
nfsgetattr(vnode_t * vp,struct vattr * vap,cred_t * cr)9050Sstevel@tonic-gate nfsgetattr(vnode_t *vp, struct vattr *vap, cred_t *cr)
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate 	int error;
9080Sstevel@tonic-gate 	rnode_t *rp;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/*
9110Sstevel@tonic-gate 	 * If we've got cached attributes, we're done, otherwise go
9120Sstevel@tonic-gate 	 * to the server to get attributes, which will update the cache
9130Sstevel@tonic-gate 	 * in the process.
9140Sstevel@tonic-gate 	 */
9150Sstevel@tonic-gate 	error = nfs_getattr_cache(vp, vap);
9160Sstevel@tonic-gate 	if (error)
9170Sstevel@tonic-gate 		error = nfs_getattr_otw(vp, vap, cr);
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	/* Return the client's view of file size */
9200Sstevel@tonic-gate 	rp = VTOR(vp);
9210Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
9220Sstevel@tonic-gate 	vap->va_size = rp->r_size;
9230Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	return (error);
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate /*
9290Sstevel@tonic-gate  * Get attributes over-the-wire and update attributes cache
9300Sstevel@tonic-gate  * if no error occurred in the over-the-wire operation.
9310Sstevel@tonic-gate  * Return 0 if successful, otherwise error.
9320Sstevel@tonic-gate  */
9330Sstevel@tonic-gate int
nfs3_getattr_otw(vnode_t * vp,struct vattr * vap,cred_t * cr)9340Sstevel@tonic-gate nfs3_getattr_otw(vnode_t *vp, struct vattr *vap, cred_t *cr)
9350Sstevel@tonic-gate {
9360Sstevel@tonic-gate 	int error;
9370Sstevel@tonic-gate 	GETATTR3args args;
9380Sstevel@tonic-gate 	GETATTR3vres res;
9390Sstevel@tonic-gate 	int douprintf;
9400Sstevel@tonic-gate 	failinfo_t fi;
9410Sstevel@tonic-gate 	hrtime_t t;
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	args.object = *VTOFH3(vp);
9440Sstevel@tonic-gate 	fi.vp = vp;
9450Sstevel@tonic-gate 	fi.fhp = (caddr_t)&args.object;
9460Sstevel@tonic-gate 	fi.copyproc = nfs3copyfh;
9470Sstevel@tonic-gate 	fi.lookupproc = nfs3lookup;
9480Sstevel@tonic-gate 	fi.xattrdirproc = acl_getxattrdir3;
9490Sstevel@tonic-gate 	res.fres.vp = vp;
9500Sstevel@tonic-gate 	res.fres.vap = vap;
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	douprintf = 1;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	t = gethrtime();
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	error = rfs3call(VTOMI(vp), NFSPROC3_GETATTR,
9570Sstevel@tonic-gate 	    xdr_nfs_fh3, (caddr_t)&args,
9580Sstevel@tonic-gate 	    xdr_GETATTR3vres, (caddr_t)&res, cr,
9590Sstevel@tonic-gate 	    &douprintf, &res.status, 0, &fi);
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	if (error)
9620Sstevel@tonic-gate 		return (error);
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	error = geterrno3(res.status);
9650Sstevel@tonic-gate 	if (error) {
9660Sstevel@tonic-gate 		PURGE_STALE_FH(error, vp, cr);
9670Sstevel@tonic-gate 		return (error);
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	/*
9710Sstevel@tonic-gate 	 * Catch status codes that indicate fattr3 to vattr translation failure
9720Sstevel@tonic-gate 	 */
9730Sstevel@tonic-gate 	if (res.fres.status)
9740Sstevel@tonic-gate 		return (res.fres.status);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	nfs_attr_cache(vp, vap, t, cr);
9770Sstevel@tonic-gate 	return (0);
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate /*
9810Sstevel@tonic-gate  * Return either cached or remote attributes. If get remote attr
9820Sstevel@tonic-gate  * use them to check and invalidate caches, then cache the new attributes.
9830Sstevel@tonic-gate  */
9840Sstevel@tonic-gate int
nfs3getattr(vnode_t * vp,struct vattr * vap,cred_t * cr)9850Sstevel@tonic-gate nfs3getattr(vnode_t *vp, struct vattr *vap, cred_t *cr)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate 	int error;
9880Sstevel@tonic-gate 	rnode_t *rp;
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	/*
9910Sstevel@tonic-gate 	 * If we've got cached attributes, we're done, otherwise go
9920Sstevel@tonic-gate 	 * to the server to get attributes, which will update the cache
9930Sstevel@tonic-gate 	 * in the process.
9940Sstevel@tonic-gate 	 */
9950Sstevel@tonic-gate 	error = nfs_getattr_cache(vp, vap);
9960Sstevel@tonic-gate 	if (error)
9970Sstevel@tonic-gate 		error = nfs3_getattr_otw(vp, vap, cr);
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	/* Return the client's view of file size */
10000Sstevel@tonic-gate 	rp = VTOR(vp);
10010Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
10020Sstevel@tonic-gate 	vap->va_size = rp->r_size;
10030Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	return (error);
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate vtype_t nf_to_vt[] = {
10090Sstevel@tonic-gate 	VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK
10100Sstevel@tonic-gate };
10110Sstevel@tonic-gate /*
10120Sstevel@tonic-gate  * Convert NFS Version 2 over the network attributes to the local
10130Sstevel@tonic-gate  * virtual attributes.  The mapping between the UID_NOBODY/GID_NOBODY
10140Sstevel@tonic-gate  * network representation and the local representation is done here.
10150Sstevel@tonic-gate  * Returns 0 for success, error if failed due to overflow.
10160Sstevel@tonic-gate  */
10170Sstevel@tonic-gate int
nattr_to_vattr(vnode_t * vp,struct nfsfattr * na,struct vattr * vap)10180Sstevel@tonic-gate nattr_to_vattr(vnode_t *vp, struct nfsfattr *na, struct vattr *vap)
10190Sstevel@tonic-gate {
10200Sstevel@tonic-gate 	/* overflow in time attributes? */
10210Sstevel@tonic-gate #ifndef _LP64
10220Sstevel@tonic-gate 	if (!NFS2_FATTR_TIME_OK(na))
10230Sstevel@tonic-gate 		return (EOVERFLOW);
10240Sstevel@tonic-gate #endif
10250Sstevel@tonic-gate 
10269750SGarima.Tripathi@Sun.COM 	vap->va_mask = AT_ALL;
10279750SGarima.Tripathi@Sun.COM 
10280Sstevel@tonic-gate 	if (na->na_type < NFNON || na->na_type > NFSOC)
10290Sstevel@tonic-gate 		vap->va_type = VBAD;
10300Sstevel@tonic-gate 	else
10310Sstevel@tonic-gate 		vap->va_type = nf_to_vt[na->na_type];
10320Sstevel@tonic-gate 	vap->va_mode = na->na_mode;
10330Sstevel@tonic-gate 	vap->va_uid = (na->na_uid == NFS_UID_NOBODY) ? UID_NOBODY : na->na_uid;
10340Sstevel@tonic-gate 	vap->va_gid = (na->na_gid == NFS_GID_NOBODY) ? GID_NOBODY : na->na_gid;
10350Sstevel@tonic-gate 	vap->va_fsid = vp->v_vfsp->vfs_dev;
10360Sstevel@tonic-gate 	vap->va_nodeid = na->na_nodeid;
10370Sstevel@tonic-gate 	vap->va_nlink = na->na_nlink;
10380Sstevel@tonic-gate 	vap->va_size = na->na_size;	/* keep for cache validation */
10390Sstevel@tonic-gate 	/*
10400Sstevel@tonic-gate 	 * nfs protocol defines times as unsigned so don't extend sign,
10410Sstevel@tonic-gate 	 * unless sysadmin set nfs_allow_preepoch_time.
10420Sstevel@tonic-gate 	 */
10430Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_atime.tv_sec, na->na_atime.tv_sec);
10440Sstevel@tonic-gate 	vap->va_atime.tv_nsec = (uint32_t)(na->na_atime.tv_usec * 1000);
10450Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_mtime.tv_sec, na->na_mtime.tv_sec);
10460Sstevel@tonic-gate 	vap->va_mtime.tv_nsec = (uint32_t)(na->na_mtime.tv_usec * 1000);
10470Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_ctime.tv_sec, na->na_ctime.tv_sec);
10480Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = (uint32_t)(na->na_ctime.tv_usec * 1000);
10490Sstevel@tonic-gate 	/*
10500Sstevel@tonic-gate 	 * Shannon's law - uncompress the received dev_t
10510Sstevel@tonic-gate 	 * if the top half of is zero indicating a response
10520Sstevel@tonic-gate 	 * from an `older style' OS. Except for when it is a
10530Sstevel@tonic-gate 	 * `new style' OS sending the maj device of zero,
10540Sstevel@tonic-gate 	 * in which case the algorithm still works because the
10550Sstevel@tonic-gate 	 * fact that it is a new style server
10560Sstevel@tonic-gate 	 * is hidden by the minor device not being greater
10570Sstevel@tonic-gate 	 * than 255 (a requirement in this case).
10580Sstevel@tonic-gate 	 */
10590Sstevel@tonic-gate 	if ((na->na_rdev & 0xffff0000) == 0)
10600Sstevel@tonic-gate 		vap->va_rdev = nfsv2_expdev(na->na_rdev);
10610Sstevel@tonic-gate 	else
10620Sstevel@tonic-gate 		vap->va_rdev = expldev(na->na_rdev);
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	vap->va_nblocks = na->na_blocks;
10650Sstevel@tonic-gate 	switch (na->na_type) {
10660Sstevel@tonic-gate 	case NFBLK:
10670Sstevel@tonic-gate 		vap->va_blksize = DEV_BSIZE;
10680Sstevel@tonic-gate 		break;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	case NFCHR:
10710Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;
10720Sstevel@tonic-gate 		break;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	case NFSOC:
10750Sstevel@tonic-gate 	default:
10760Sstevel@tonic-gate 		vap->va_blksize = na->na_blocksize;
10770Sstevel@tonic-gate 		break;
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 	/*
10800Sstevel@tonic-gate 	 * This bit of ugliness is a hack to preserve the
10810Sstevel@tonic-gate 	 * over-the-wire protocols for named-pipe vnodes.
10820Sstevel@tonic-gate 	 * It remaps the special over-the-wire type to the
10830Sstevel@tonic-gate 	 * VFIFO type. (see note in nfs.h)
10840Sstevel@tonic-gate 	 */
10850Sstevel@tonic-gate 	if (NA_ISFIFO(na)) {
10860Sstevel@tonic-gate 		vap->va_type = VFIFO;
10870Sstevel@tonic-gate 		vap->va_mode = (vap->va_mode & ~S_IFMT) | S_IFIFO;
10880Sstevel@tonic-gate 		vap->va_rdev = 0;
10890Sstevel@tonic-gate 		vap->va_blksize = na->na_blocksize;
10900Sstevel@tonic-gate 	}
10910Sstevel@tonic-gate 	vap->va_seq = 0;
10920Sstevel@tonic-gate 	return (0);
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate /*
10960Sstevel@tonic-gate  * Convert NFS Version 3 over the network attributes to the local
10970Sstevel@tonic-gate  * virtual attributes.  The mapping between the UID_NOBODY/GID_NOBODY
10980Sstevel@tonic-gate  * network representation and the local representation is done here.
10990Sstevel@tonic-gate  */
11000Sstevel@tonic-gate vtype_t nf3_to_vt[] = {
11010Sstevel@tonic-gate 	VBAD, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
11020Sstevel@tonic-gate };
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate int
fattr3_to_vattr(vnode_t * vp,fattr3 * na,struct vattr * vap)11050Sstevel@tonic-gate fattr3_to_vattr(vnode_t *vp, fattr3 *na, struct vattr *vap)
11060Sstevel@tonic-gate {
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate #ifndef _LP64
11090Sstevel@tonic-gate 	/* overflow in time attributes? */
11100Sstevel@tonic-gate 	if (!NFS3_FATTR_TIME_OK(na))
11110Sstevel@tonic-gate 		return (EOVERFLOW);
11120Sstevel@tonic-gate #endif
11130Sstevel@tonic-gate 	if (!NFS3_SIZE_OK(na->size))
11140Sstevel@tonic-gate 		/* file too big */
11150Sstevel@tonic-gate 		return (EFBIG);
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	vap->va_mask = AT_ALL;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	if (na->type < NF3REG || na->type > NF3FIFO)
11200Sstevel@tonic-gate 		vap->va_type = VBAD;
11210Sstevel@tonic-gate 	else
11220Sstevel@tonic-gate 		vap->va_type = nf3_to_vt[na->type];
11230Sstevel@tonic-gate 	vap->va_mode = na->mode;
11240Sstevel@tonic-gate 	vap->va_uid = (na->uid == NFS_UID_NOBODY) ? UID_NOBODY : (uid_t)na->uid;
11250Sstevel@tonic-gate 	vap->va_gid = (na->gid == NFS_GID_NOBODY) ? GID_NOBODY : (gid_t)na->gid;
11260Sstevel@tonic-gate 	vap->va_fsid = vp->v_vfsp->vfs_dev;
11270Sstevel@tonic-gate 	vap->va_nodeid = na->fileid;
11280Sstevel@tonic-gate 	vap->va_nlink = na->nlink;
11290Sstevel@tonic-gate 	vap->va_size = na->size;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	/*
11320Sstevel@tonic-gate 	 * nfs protocol defines times as unsigned so don't extend sign,
11330Sstevel@tonic-gate 	 * unless sysadmin set nfs_allow_preepoch_time.
11340Sstevel@tonic-gate 	 */
11350Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_atime.tv_sec, na->atime.seconds);
11360Sstevel@tonic-gate 	vap->va_atime.tv_nsec = (uint32_t)na->atime.nseconds;
11370Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_mtime.tv_sec, na->mtime.seconds);
11380Sstevel@tonic-gate 	vap->va_mtime.tv_nsec = (uint32_t)na->mtime.nseconds;
11390Sstevel@tonic-gate 	NFS_TIME_T_CONVERT(vap->va_ctime.tv_sec, na->ctime.seconds);
11400Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = (uint32_t)na->ctime.nseconds;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	switch (na->type) {
11430Sstevel@tonic-gate 	case NF3BLK:
11440Sstevel@tonic-gate 		vap->va_rdev = makedevice(na->rdev.specdata1,
11459750SGarima.Tripathi@Sun.COM 		    na->rdev.specdata2);
11460Sstevel@tonic-gate 		vap->va_blksize = DEV_BSIZE;
11470Sstevel@tonic-gate 		vap->va_nblocks = 0;
11480Sstevel@tonic-gate 		break;
11490Sstevel@tonic-gate 	case NF3CHR:
11500Sstevel@tonic-gate 		vap->va_rdev = makedevice(na->rdev.specdata1,
11519750SGarima.Tripathi@Sun.COM 		    na->rdev.specdata2);
11520Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;
11530Sstevel@tonic-gate 		vap->va_nblocks = 0;
11540Sstevel@tonic-gate 		break;
11550Sstevel@tonic-gate 	case NF3REG:
11560Sstevel@tonic-gate 	case NF3DIR:
11570Sstevel@tonic-gate 	case NF3LNK:
11580Sstevel@tonic-gate 		vap->va_rdev = 0;
11590Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;
11600Sstevel@tonic-gate 		vap->va_nblocks = (u_longlong_t)
11610Sstevel@tonic-gate 		    ((na->used + (size3)DEV_BSIZE - (size3)1) /
11620Sstevel@tonic-gate 		    (size3)DEV_BSIZE);
11630Sstevel@tonic-gate 		break;
11640Sstevel@tonic-gate 	case NF3SOCK:
11650Sstevel@tonic-gate 	case NF3FIFO:
11660Sstevel@tonic-gate 	default:
11670Sstevel@tonic-gate 		vap->va_rdev = 0;
11680Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;
11690Sstevel@tonic-gate 		vap->va_nblocks = 0;
11700Sstevel@tonic-gate 		break;
11710Sstevel@tonic-gate 	}
11720Sstevel@tonic-gate 	vap->va_seq = 0;
11730Sstevel@tonic-gate 	return (0);
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate /*
11770Sstevel@tonic-gate  * Asynchronous I/O parameters.  nfs_async_threads is the high-water mark
11780Sstevel@tonic-gate  * for the demand-based allocation of async threads per-mount.  The
11790Sstevel@tonic-gate  * nfs_async_timeout is the amount of time a thread will live after it
11800Sstevel@tonic-gate  * becomes idle, unless new I/O requests are received before the thread
11810Sstevel@tonic-gate  * dies.  See nfs_async_putpage and nfs_async_start.
11820Sstevel@tonic-gate  */
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate int nfs_async_timeout = -1;	/* uninitialized */
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate static void	nfs_async_start(struct vfs *);
118711507SVallish.Vaidyeshwara@Sun.COM static void	nfs_async_pgops_start(struct vfs *);
118811507SVallish.Vaidyeshwara@Sun.COM static void	nfs_async_common_start(struct vfs *, int);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate static void
free_async_args(struct nfs_async_reqs * args)11910Sstevel@tonic-gate free_async_args(struct nfs_async_reqs *args)
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate 	rnode_t *rp;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	if (args->a_io != NFS_INACTIVE) {
11960Sstevel@tonic-gate 		rp = VTOR(args->a_vp);
11970Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
11980Sstevel@tonic-gate 		rp->r_count--;
11990Sstevel@tonic-gate 		if (args->a_io == NFS_PUTAPAGE ||
12000Sstevel@tonic-gate 		    args->a_io == NFS_PAGEIO)
12010Sstevel@tonic-gate 			rp->r_awcount--;
12020Sstevel@tonic-gate 		cv_broadcast(&rp->r_cv);
12030Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
12040Sstevel@tonic-gate 		VN_RELE(args->a_vp);
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 	crfree(args->a_cred);
12070Sstevel@tonic-gate 	kmem_free(args, sizeof (*args));
12080Sstevel@tonic-gate }
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate /*
12110Sstevel@tonic-gate  * Cross-zone thread creation and NFS access is disallowed, yet fsflush() and
12120Sstevel@tonic-gate  * pageout(), running in the global zone, have legitimate reasons to do
12130Sstevel@tonic-gate  * VOP_PUTPAGE(B_ASYNC) on other zones' NFS mounts.  We avoid the problem by
12140Sstevel@tonic-gate  * use of a a per-mount "asynchronous requests manager thread" which is
12150Sstevel@tonic-gate  * signaled by the various asynchronous work routines when there is
12160Sstevel@tonic-gate  * asynchronous work to be done.  It is responsible for creating new
12170Sstevel@tonic-gate  * worker threads if necessary, and notifying existing worker threads
12180Sstevel@tonic-gate  * that there is work to be done.
12190Sstevel@tonic-gate  *
12200Sstevel@tonic-gate  * In other words, it will "take the specifications from the customers and
12210Sstevel@tonic-gate  * give them to the engineers."
12220Sstevel@tonic-gate  *
12230Sstevel@tonic-gate  * Worker threads die off of their own accord if they are no longer
12240Sstevel@tonic-gate  * needed.
12250Sstevel@tonic-gate  *
12260Sstevel@tonic-gate  * This thread is killed when the zone is going away or the filesystem
12270Sstevel@tonic-gate  * is being unmounted.
12280Sstevel@tonic-gate  */
12290Sstevel@tonic-gate void
nfs_async_manager(vfs_t * vfsp)12300Sstevel@tonic-gate nfs_async_manager(vfs_t *vfsp)
12310Sstevel@tonic-gate {
12320Sstevel@tonic-gate 	callb_cpr_t cprinfo;
12330Sstevel@tonic-gate 	mntinfo_t *mi;
12340Sstevel@tonic-gate 	uint_t max_threads;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr,
12399750SGarima.Tripathi@Sun.COM 	    "nfs_async_manager");
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
12420Sstevel@tonic-gate 	/*
12430Sstevel@tonic-gate 	 * We want to stash the max number of threads that this mount was
12440Sstevel@tonic-gate 	 * allowed so we can use it later when the variable is set to zero as
12450Sstevel@tonic-gate 	 * part of the zone/mount going away.
12460Sstevel@tonic-gate 	 *
12470Sstevel@tonic-gate 	 * We want to be able to create at least one thread to handle
124811286SMarcel.Telka@Sun.COM 	 * asynchronous inactive calls.
12490Sstevel@tonic-gate 	 */
12500Sstevel@tonic-gate 	max_threads = MAX(mi->mi_max_threads, 1);
12510Sstevel@tonic-gate 	/*
12520Sstevel@tonic-gate 	 * We don't want to wait for mi_max_threads to go to zero, since that
12530Sstevel@tonic-gate 	 * happens as part of a failed unmount, but this thread should only
12540Sstevel@tonic-gate 	 * exit when the mount/zone is really going away.
12550Sstevel@tonic-gate 	 *
12560Sstevel@tonic-gate 	 * Once MI_ASYNC_MGR_STOP is set, no more async operations will be
12570Sstevel@tonic-gate 	 * attempted: the various _async_*() functions know to do things
12580Sstevel@tonic-gate 	 * inline if mi_max_threads == 0.  Henceforth we just drain out the
12590Sstevel@tonic-gate 	 * outstanding requests.
12600Sstevel@tonic-gate 	 *
12610Sstevel@tonic-gate 	 * Note that we still create zthreads even if we notice the zone is
12620Sstevel@tonic-gate 	 * shutting down (MI_ASYNC_MGR_STOP is set); this may cause the zone
12630Sstevel@tonic-gate 	 * shutdown sequence to take slightly longer in some cases, but
12640Sstevel@tonic-gate 	 * doesn't violate the protocol, as all threads will exit as soon as
12650Sstevel@tonic-gate 	 * they're done processing the remaining requests.
12660Sstevel@tonic-gate 	 */
126711286SMarcel.Telka@Sun.COM 	for (;;) {
12680Sstevel@tonic-gate 		while (mi->mi_async_req_count > 0) {
12690Sstevel@tonic-gate 			/*
12700Sstevel@tonic-gate 			 * Paranoia: If the mount started out having
12710Sstevel@tonic-gate 			 * (mi->mi_max_threads == 0), and the value was
12720Sstevel@tonic-gate 			 * later changed (via a debugger or somesuch),
12730Sstevel@tonic-gate 			 * we could be confused since we will think we
12740Sstevel@tonic-gate 			 * can't create any threads, and the calling
12750Sstevel@tonic-gate 			 * code (which looks at the current value of
12760Sstevel@tonic-gate 			 * mi->mi_max_threads, now non-zero) thinks we
12770Sstevel@tonic-gate 			 * can.
12780Sstevel@tonic-gate 			 *
12790Sstevel@tonic-gate 			 * So, because we're paranoid, we create threads
12800Sstevel@tonic-gate 			 * up to the maximum of the original and the
12810Sstevel@tonic-gate 			 * current value. This means that future
12820Sstevel@tonic-gate 			 * (debugger-induced) lowerings of
12830Sstevel@tonic-gate 			 * mi->mi_max_threads are ignored for our
12840Sstevel@tonic-gate 			 * purposes, but who told them they could change
12850Sstevel@tonic-gate 			 * random values on a live kernel anyhow?
12860Sstevel@tonic-gate 			 */
128711507SVallish.Vaidyeshwara@Sun.COM 			if (mi->mi_threads[NFS_ASYNC_QUEUE] <
12880Sstevel@tonic-gate 			    MAX(mi->mi_max_threads, max_threads)) {
128911507SVallish.Vaidyeshwara@Sun.COM 				mi->mi_threads[NFS_ASYNC_QUEUE]++;
12900Sstevel@tonic-gate 				mutex_exit(&mi->mi_async_lock);
12910Sstevel@tonic-gate 				VFS_HOLD(vfsp);	/* hold for new thread */
12920Sstevel@tonic-gate 				(void) zthread_create(NULL, 0, nfs_async_start,
12930Sstevel@tonic-gate 				    vfsp, 0, minclsyspri);
12940Sstevel@tonic-gate 				mutex_enter(&mi->mi_async_lock);
129511507SVallish.Vaidyeshwara@Sun.COM 			} else if (mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE] <
129611507SVallish.Vaidyeshwara@Sun.COM 			    NUM_ASYNC_PGOPS_THREADS) {
129711507SVallish.Vaidyeshwara@Sun.COM 				mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE]++;
129811507SVallish.Vaidyeshwara@Sun.COM 				mutex_exit(&mi->mi_async_lock);
129911507SVallish.Vaidyeshwara@Sun.COM 				VFS_HOLD(vfsp); /* hold for new thread */
130011507SVallish.Vaidyeshwara@Sun.COM 				(void) zthread_create(NULL, 0,
130111507SVallish.Vaidyeshwara@Sun.COM 				    nfs_async_pgops_start, vfsp, 0,
130211507SVallish.Vaidyeshwara@Sun.COM 				    minclsyspri);
130311507SVallish.Vaidyeshwara@Sun.COM 				mutex_enter(&mi->mi_async_lock);
13040Sstevel@tonic-gate 			}
130511507SVallish.Vaidyeshwara@Sun.COM 			NFS_WAKE_ASYNC_WORKER(mi->mi_async_work_cv);
13060Sstevel@tonic-gate 			ASSERT(mi->mi_async_req_count != 0);
13070Sstevel@tonic-gate 			mi->mi_async_req_count--;
13080Sstevel@tonic-gate 		}
130911286SMarcel.Telka@Sun.COM 
13100Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
131111286SMarcel.Telka@Sun.COM 		if (mi->mi_flags & MI_ASYNC_MGR_STOP) {
131211286SMarcel.Telka@Sun.COM 			mutex_exit(&mi->mi_lock);
131311286SMarcel.Telka@Sun.COM 			break;
131411286SMarcel.Telka@Sun.COM 		}
131511286SMarcel.Telka@Sun.COM 		mutex_exit(&mi->mi_lock);
131611286SMarcel.Telka@Sun.COM 
131711286SMarcel.Telka@Sun.COM 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
131811286SMarcel.Telka@Sun.COM 		cv_wait(&mi->mi_async_reqs_cv, &mi->mi_async_lock);
131911286SMarcel.Telka@Sun.COM 		CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock);
13200Sstevel@tonic-gate 	}
13210Sstevel@tonic-gate 	/*
13220Sstevel@tonic-gate 	 * Let everyone know we're done.
13230Sstevel@tonic-gate 	 */
13240Sstevel@tonic-gate 	mi->mi_manager_thread = NULL;
13250Sstevel@tonic-gate 	cv_broadcast(&mi->mi_async_cv);
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	/*
13280Sstevel@tonic-gate 	 * There is no explicit call to mutex_exit(&mi->mi_async_lock)
13290Sstevel@tonic-gate 	 * since CALLB_CPR_EXIT is actually responsible for releasing
13300Sstevel@tonic-gate 	 * 'mi_async_lock'.
13310Sstevel@tonic-gate 	 */
13320Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
13330Sstevel@tonic-gate 	VFS_RELE(vfsp);	/* release thread's hold */
13340Sstevel@tonic-gate 	zthread_exit();
13350Sstevel@tonic-gate }
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate /*
13380Sstevel@tonic-gate  * Signal (and wait for) the async manager thread to clean up and go away.
13390Sstevel@tonic-gate  */
13400Sstevel@tonic-gate void
nfs_async_manager_stop(vfs_t * vfsp)13410Sstevel@tonic-gate nfs_async_manager_stop(vfs_t *vfsp)
13420Sstevel@tonic-gate {
13430Sstevel@tonic-gate 	mntinfo_t *mi = VFTOMI(vfsp);
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
13460Sstevel@tonic-gate 	mutex_enter(&mi->mi_lock);
13470Sstevel@tonic-gate 	mi->mi_flags |= MI_ASYNC_MGR_STOP;
13480Sstevel@tonic-gate 	mutex_exit(&mi->mi_lock);
13490Sstevel@tonic-gate 	cv_broadcast(&mi->mi_async_reqs_cv);
13500Sstevel@tonic-gate 	while (mi->mi_manager_thread != NULL)
13510Sstevel@tonic-gate 		cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
13520Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
13530Sstevel@tonic-gate }
13540Sstevel@tonic-gate 
13550Sstevel@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 *))13560Sstevel@tonic-gate nfs_async_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr,
13570Sstevel@tonic-gate 	struct seg *seg, cred_t *cr, void (*readahead)(vnode_t *,
13580Sstevel@tonic-gate 	u_offset_t, caddr_t, struct seg *, cred_t *))
13590Sstevel@tonic-gate {
13600Sstevel@tonic-gate 	rnode_t *rp;
13610Sstevel@tonic-gate 	mntinfo_t *mi;
13620Sstevel@tonic-gate 	struct nfs_async_reqs *args;
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	rp = VTOR(vp);
13650Sstevel@tonic-gate 	ASSERT(rp->r_freef == NULL);
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	mi = VTOMI(vp);
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	/*
13700Sstevel@tonic-gate 	 * If addr falls in a different segment, don't bother doing readahead.
13710Sstevel@tonic-gate 	 */
13720Sstevel@tonic-gate 	if (addr >= seg->s_base + seg->s_size)
13730Sstevel@tonic-gate 		return (-1);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	/*
13760Sstevel@tonic-gate 	 * If we can't allocate a request structure, punt on the readahead.
13770Sstevel@tonic-gate 	 */
13780Sstevel@tonic-gate 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
13790Sstevel@tonic-gate 		return (-1);
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	/*
13820Sstevel@tonic-gate 	 * If a lock operation is pending, don't initiate any new
13830Sstevel@tonic-gate 	 * readaheads.  Otherwise, bump r_count to indicate the new
13840Sstevel@tonic-gate 	 * asynchronous I/O.
13850Sstevel@tonic-gate 	 */
13860Sstevel@tonic-gate 	if (!nfs_rw_tryenter(&rp->r_lkserlock, RW_READER)) {
13870Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
13880Sstevel@tonic-gate 		return (-1);
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
13910Sstevel@tonic-gate 	rp->r_count++;
13920Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
13930Sstevel@tonic-gate 	nfs_rw_exit(&rp->r_lkserlock);
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	args->a_next = NULL;
13960Sstevel@tonic-gate #ifdef DEBUG
13970Sstevel@tonic-gate 	args->a_queuer = curthread;
13980Sstevel@tonic-gate #endif
13990Sstevel@tonic-gate 	VN_HOLD(vp);
14000Sstevel@tonic-gate 	args->a_vp = vp;
14010Sstevel@tonic-gate 	ASSERT(cr != NULL);
14020Sstevel@tonic-gate 	crhold(cr);
14030Sstevel@tonic-gate 	args->a_cred = cr;
14040Sstevel@tonic-gate 	args->a_io = NFS_READ_AHEAD;
14050Sstevel@tonic-gate 	args->a_nfs_readahead = readahead;
14060Sstevel@tonic-gate 	args->a_nfs_blkoff = blkoff;
14070Sstevel@tonic-gate 	args->a_nfs_seg = seg;
14080Sstevel@tonic-gate 	args->a_nfs_addr = addr;
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	/*
14130Sstevel@tonic-gate 	 * If asyncio has been disabled, don't bother readahead.
14140Sstevel@tonic-gate 	 */
14150Sstevel@tonic-gate 	if (mi->mi_max_threads == 0) {
14160Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
14170Sstevel@tonic-gate 		goto noasync;
14180Sstevel@tonic-gate 	}
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	/*
14210Sstevel@tonic-gate 	 * Link request structure into the async list and
14220Sstevel@tonic-gate 	 * wakeup async thread to do the i/o.
14230Sstevel@tonic-gate 	 */
14240Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_READ_AHEAD] == NULL) {
14250Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_READ_AHEAD] = args;
14260Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READ_AHEAD] = args;
14270Sstevel@tonic-gate 	} else {
14280Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READ_AHEAD]->a_next = args;
14290Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READ_AHEAD] = args;
14300Sstevel@tonic-gate 	}
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
14330Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
14340Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
14350Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
14360Sstevel@tonic-gate 	}
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	mi->mi_async_req_count++;
14390Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
14400Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
14410Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
14420Sstevel@tonic-gate 	return (0);
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate noasync:
14450Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
14460Sstevel@tonic-gate 	rp->r_count--;
14470Sstevel@tonic-gate 	cv_broadcast(&rp->r_cv);
14480Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
14490Sstevel@tonic-gate 	VN_RELE(vp);
14500Sstevel@tonic-gate 	crfree(cr);
14510Sstevel@tonic-gate 	kmem_free(args, sizeof (*args));
14520Sstevel@tonic-gate 	return (-1);
14530Sstevel@tonic-gate }
14540Sstevel@tonic-gate 
14550Sstevel@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 *))14560Sstevel@tonic-gate nfs_async_putapage(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
14570Sstevel@tonic-gate 	int flags, cred_t *cr, int (*putapage)(vnode_t *, page_t *,
14580Sstevel@tonic-gate 	u_offset_t, size_t, int, cred_t *))
14590Sstevel@tonic-gate {
14600Sstevel@tonic-gate 	rnode_t *rp;
14610Sstevel@tonic-gate 	mntinfo_t *mi;
14620Sstevel@tonic-gate 	struct nfs_async_reqs *args;
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	ASSERT(flags & B_ASYNC);
14650Sstevel@tonic-gate 	ASSERT(vp->v_vfsp != NULL);
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	rp = VTOR(vp);
14680Sstevel@tonic-gate 	ASSERT(rp->r_count > 0);
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	mi = VTOMI(vp);
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 	/*
14730Sstevel@tonic-gate 	 * If we can't allocate a request structure, do the putpage
14740Sstevel@tonic-gate 	 * operation synchronously in this thread's context.
14750Sstevel@tonic-gate 	 */
14760Sstevel@tonic-gate 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
14770Sstevel@tonic-gate 		goto noasync;
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	args->a_next = NULL;
14800Sstevel@tonic-gate #ifdef DEBUG
14810Sstevel@tonic-gate 	args->a_queuer = curthread;
14820Sstevel@tonic-gate #endif
14830Sstevel@tonic-gate 	VN_HOLD(vp);
14840Sstevel@tonic-gate 	args->a_vp = vp;
14850Sstevel@tonic-gate 	ASSERT(cr != NULL);
14860Sstevel@tonic-gate 	crhold(cr);
14870Sstevel@tonic-gate 	args->a_cred = cr;
14880Sstevel@tonic-gate 	args->a_io = NFS_PUTAPAGE;
14890Sstevel@tonic-gate 	args->a_nfs_putapage = putapage;
14900Sstevel@tonic-gate 	args->a_nfs_pp = pp;
14910Sstevel@tonic-gate 	args->a_nfs_off = off;
14920Sstevel@tonic-gate 	args->a_nfs_len = (uint_t)len;
14930Sstevel@tonic-gate 	args->a_nfs_flags = flags;
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 	/*
14980Sstevel@tonic-gate 	 * If asyncio has been disabled, then make a synchronous request.
14990Sstevel@tonic-gate 	 * This check is done a second time in case async io was diabled
15000Sstevel@tonic-gate 	 * while this thread was blocked waiting for memory pressure to
15010Sstevel@tonic-gate 	 * reduce or for the queue to drain.
15020Sstevel@tonic-gate 	 */
15030Sstevel@tonic-gate 	if (mi->mi_max_threads == 0) {
15040Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
15050Sstevel@tonic-gate 		goto noasync;
15060Sstevel@tonic-gate 	}
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	/*
15090Sstevel@tonic-gate 	 * Link request structure into the async list and
15100Sstevel@tonic-gate 	 * wakeup async thread to do the i/o.
15110Sstevel@tonic-gate 	 */
15120Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_PUTAPAGE] == NULL) {
15130Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_PUTAPAGE] = args;
15140Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PUTAPAGE] = args;
15150Sstevel@tonic-gate 	} else {
15160Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PUTAPAGE]->a_next = args;
15170Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PUTAPAGE] = args;
15180Sstevel@tonic-gate 	}
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
15210Sstevel@tonic-gate 	rp->r_count++;
15220Sstevel@tonic-gate 	rp->r_awcount++;
15230Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
15260Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
15270Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
15280Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
15290Sstevel@tonic-gate 	}
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	mi->mi_async_req_count++;
15320Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
15330Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
15340Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
15350Sstevel@tonic-gate 	return (0);
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate noasync:
15380Sstevel@tonic-gate 	if (args != NULL) {
15390Sstevel@tonic-gate 		VN_RELE(vp);
15400Sstevel@tonic-gate 		crfree(cr);
15410Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
15420Sstevel@tonic-gate 	}
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	if (curproc == proc_pageout || curproc == proc_fsflush) {
15450Sstevel@tonic-gate 		/*
15460Sstevel@tonic-gate 		 * If we get here in the context of the pageout/fsflush,
15470Sstevel@tonic-gate 		 * we refuse to do a sync write, because this may hang
15480Sstevel@tonic-gate 		 * pageout (and the machine). In this case, we just
15490Sstevel@tonic-gate 		 * re-mark the page as dirty and punt on the page.
15500Sstevel@tonic-gate 		 *
15510Sstevel@tonic-gate 		 * Make sure B_FORCE isn't set.  We can re-mark the
15520Sstevel@tonic-gate 		 * pages as dirty and unlock the pages in one swoop by
15530Sstevel@tonic-gate 		 * passing in B_ERROR to pvn_write_done().  However,
15540Sstevel@tonic-gate 		 * we should make sure B_FORCE isn't set - we don't
15550Sstevel@tonic-gate 		 * want the page tossed before it gets written out.
15560Sstevel@tonic-gate 		 */
15570Sstevel@tonic-gate 		if (flags & B_FORCE)
15580Sstevel@tonic-gate 			flags &= ~(B_INVAL | B_FORCE);
15590Sstevel@tonic-gate 		pvn_write_done(pp, flags | B_ERROR);
15600Sstevel@tonic-gate 		return (0);
15610Sstevel@tonic-gate 	}
1562766Scarlsonj 	if (nfs_zone() != mi->mi_zone) {
15630Sstevel@tonic-gate 		/*
15640Sstevel@tonic-gate 		 * So this was a cross-zone sync putpage.  We pass in B_ERROR
15650Sstevel@tonic-gate 		 * to pvn_write_done() to re-mark the pages as dirty and unlock
15660Sstevel@tonic-gate 		 * them.
15670Sstevel@tonic-gate 		 *
15680Sstevel@tonic-gate 		 * We don't want to clear B_FORCE here as the caller presumably
15690Sstevel@tonic-gate 		 * knows what they're doing if they set it.
15700Sstevel@tonic-gate 		 */
15710Sstevel@tonic-gate 		pvn_write_done(pp, flags | B_ERROR);
15720Sstevel@tonic-gate 		return (EPERM);
15730Sstevel@tonic-gate 	}
15740Sstevel@tonic-gate 	return ((*putapage)(vp, pp, off, len, flags, cr));
15750Sstevel@tonic-gate }
15760Sstevel@tonic-gate 
15770Sstevel@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 *))15780Sstevel@tonic-gate nfs_async_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
15790Sstevel@tonic-gate 	int flags, cred_t *cr, int (*pageio)(vnode_t *, page_t *, u_offset_t,
15800Sstevel@tonic-gate 	size_t, int, cred_t *))
15810Sstevel@tonic-gate {
15820Sstevel@tonic-gate 	rnode_t *rp;
15830Sstevel@tonic-gate 	mntinfo_t *mi;
15840Sstevel@tonic-gate 	struct nfs_async_reqs *args;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	ASSERT(flags & B_ASYNC);
15870Sstevel@tonic-gate 	ASSERT(vp->v_vfsp != NULL);
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	rp = VTOR(vp);
15900Sstevel@tonic-gate 	ASSERT(rp->r_count > 0);
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	mi = VTOMI(vp);
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	/*
15950Sstevel@tonic-gate 	 * If we can't allocate a request structure, do the pageio
15960Sstevel@tonic-gate 	 * request synchronously in this thread's context.
15970Sstevel@tonic-gate 	 */
15980Sstevel@tonic-gate 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
15990Sstevel@tonic-gate 		goto noasync;
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	args->a_next = NULL;
16020Sstevel@tonic-gate #ifdef DEBUG
16030Sstevel@tonic-gate 	args->a_queuer = curthread;
16040Sstevel@tonic-gate #endif
16050Sstevel@tonic-gate 	VN_HOLD(vp);
16060Sstevel@tonic-gate 	args->a_vp = vp;
16070Sstevel@tonic-gate 	ASSERT(cr != NULL);
16080Sstevel@tonic-gate 	crhold(cr);
16090Sstevel@tonic-gate 	args->a_cred = cr;
16100Sstevel@tonic-gate 	args->a_io = NFS_PAGEIO;
16110Sstevel@tonic-gate 	args->a_nfs_pageio = pageio;
16120Sstevel@tonic-gate 	args->a_nfs_pp = pp;
16130Sstevel@tonic-gate 	args->a_nfs_off = io_off;
16140Sstevel@tonic-gate 	args->a_nfs_len = (uint_t)io_len;
16150Sstevel@tonic-gate 	args->a_nfs_flags = flags;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	/*
16200Sstevel@tonic-gate 	 * If asyncio has been disabled, then make a synchronous request.
16210Sstevel@tonic-gate 	 * This check is done a second time in case async io was diabled
16220Sstevel@tonic-gate 	 * while this thread was blocked waiting for memory pressure to
16230Sstevel@tonic-gate 	 * reduce or for the queue to drain.
16240Sstevel@tonic-gate 	 */
16250Sstevel@tonic-gate 	if (mi->mi_max_threads == 0) {
16260Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
16270Sstevel@tonic-gate 		goto noasync;
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	/*
16310Sstevel@tonic-gate 	 * Link request structure into the async list and
16320Sstevel@tonic-gate 	 * wakeup async thread to do the i/o.
16330Sstevel@tonic-gate 	 */
16340Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_PAGEIO] == NULL) {
16350Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_PAGEIO] = args;
16360Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PAGEIO] = args;
16370Sstevel@tonic-gate 	} else {
16380Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PAGEIO]->a_next = args;
16390Sstevel@tonic-gate 		mi->mi_async_tail[NFS_PAGEIO] = args;
16400Sstevel@tonic-gate 	}
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
16430Sstevel@tonic-gate 	rp->r_count++;
16440Sstevel@tonic-gate 	rp->r_awcount++;
16450Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
16480Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
16490Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
16500Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
16510Sstevel@tonic-gate 	}
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	mi->mi_async_req_count++;
16540Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
16550Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
16560Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
16570Sstevel@tonic-gate 	return (0);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate noasync:
16600Sstevel@tonic-gate 	if (args != NULL) {
16610Sstevel@tonic-gate 		VN_RELE(vp);
16620Sstevel@tonic-gate 		crfree(cr);
16630Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
16640Sstevel@tonic-gate 	}
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	/*
16670Sstevel@tonic-gate 	 * If we can't do it ASYNC, for reads we do nothing (but cleanup
16680Sstevel@tonic-gate 	 * the page list), for writes we do it synchronously, except for
16690Sstevel@tonic-gate 	 * proc_pageout/proc_fsflush as described below.
16700Sstevel@tonic-gate 	 */
16710Sstevel@tonic-gate 	if (flags & B_READ) {
16720Sstevel@tonic-gate 		pvn_read_done(pp, flags | B_ERROR);
16730Sstevel@tonic-gate 		return (0);
16740Sstevel@tonic-gate 	}
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	if (curproc == proc_pageout || curproc == proc_fsflush) {
16770Sstevel@tonic-gate 		/*
16780Sstevel@tonic-gate 		 * If we get here in the context of the pageout/fsflush,
16790Sstevel@tonic-gate 		 * we refuse to do a sync write, because this may hang
16800Sstevel@tonic-gate 		 * pageout/fsflush (and the machine). In this case, we just
16810Sstevel@tonic-gate 		 * re-mark the page as dirty and punt on the page.
16820Sstevel@tonic-gate 		 *
16830Sstevel@tonic-gate 		 * Make sure B_FORCE isn't set.  We can re-mark the
16840Sstevel@tonic-gate 		 * pages as dirty and unlock the pages in one swoop by
16850Sstevel@tonic-gate 		 * passing in B_ERROR to pvn_write_done().  However,
16860Sstevel@tonic-gate 		 * we should make sure B_FORCE isn't set - we don't
16870Sstevel@tonic-gate 		 * want the page tossed before it gets written out.
16880Sstevel@tonic-gate 		 */
16890Sstevel@tonic-gate 		if (flags & B_FORCE)
16900Sstevel@tonic-gate 			flags &= ~(B_INVAL | B_FORCE);
16910Sstevel@tonic-gate 		pvn_write_done(pp, flags | B_ERROR);
16920Sstevel@tonic-gate 		return (0);
16930Sstevel@tonic-gate 	}
16940Sstevel@tonic-gate 
1695766Scarlsonj 	if (nfs_zone() != mi->mi_zone) {
16960Sstevel@tonic-gate 		/*
16970Sstevel@tonic-gate 		 * So this was a cross-zone sync pageio.  We pass in B_ERROR
16980Sstevel@tonic-gate 		 * to pvn_write_done() to re-mark the pages as dirty and unlock
16990Sstevel@tonic-gate 		 * them.
17000Sstevel@tonic-gate 		 *
17010Sstevel@tonic-gate 		 * We don't want to clear B_FORCE here as the caller presumably
17020Sstevel@tonic-gate 		 * knows what they're doing if they set it.
17030Sstevel@tonic-gate 		 */
17040Sstevel@tonic-gate 		pvn_write_done(pp, flags | B_ERROR);
17050Sstevel@tonic-gate 		return (EPERM);
17060Sstevel@tonic-gate 	}
17070Sstevel@tonic-gate 	return ((*pageio)(vp, pp, io_off, io_len, flags, cr));
17080Sstevel@tonic-gate }
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate void
nfs_async_readdir(vnode_t * vp,rddir_cache * rdc,cred_t * cr,int (* readdir)(vnode_t *,rddir_cache *,cred_t *))17110Sstevel@tonic-gate nfs_async_readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr,
17120Sstevel@tonic-gate 	int (*readdir)(vnode_t *, rddir_cache *, cred_t *))
17130Sstevel@tonic-gate {
17140Sstevel@tonic-gate 	rnode_t *rp;
17150Sstevel@tonic-gate 	mntinfo_t *mi;
17160Sstevel@tonic-gate 	struct nfs_async_reqs *args;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	rp = VTOR(vp);
17190Sstevel@tonic-gate 	ASSERT(rp->r_freef == NULL);
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	mi = VTOMI(vp);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	/*
17240Sstevel@tonic-gate 	 * If we can't allocate a request structure, do the readdir
17250Sstevel@tonic-gate 	 * operation synchronously in this thread's context.
17260Sstevel@tonic-gate 	 */
17270Sstevel@tonic-gate 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
17280Sstevel@tonic-gate 		goto noasync;
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	args->a_next = NULL;
17310Sstevel@tonic-gate #ifdef DEBUG
17320Sstevel@tonic-gate 	args->a_queuer = curthread;
17330Sstevel@tonic-gate #endif
17340Sstevel@tonic-gate 	VN_HOLD(vp);
17350Sstevel@tonic-gate 	args->a_vp = vp;
17360Sstevel@tonic-gate 	ASSERT(cr != NULL);
17370Sstevel@tonic-gate 	crhold(cr);
17380Sstevel@tonic-gate 	args->a_cred = cr;
17390Sstevel@tonic-gate 	args->a_io = NFS_READDIR;
17400Sstevel@tonic-gate 	args->a_nfs_readdir = readdir;
17410Sstevel@tonic-gate 	args->a_nfs_rdc = rdc;
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	/*
17460Sstevel@tonic-gate 	 * If asyncio has been disabled, then make a synchronous request.
17470Sstevel@tonic-gate 	 */
17480Sstevel@tonic-gate 	if (mi->mi_max_threads == 0) {
17490Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
17500Sstevel@tonic-gate 		goto noasync;
17510Sstevel@tonic-gate 	}
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	/*
17540Sstevel@tonic-gate 	 * Link request structure into the async list and
17550Sstevel@tonic-gate 	 * wakeup async thread to do the i/o.
17560Sstevel@tonic-gate 	 */
17570Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_READDIR] == NULL) {
17580Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_READDIR] = args;
17590Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READDIR] = args;
17600Sstevel@tonic-gate 	} else {
17610Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READDIR]->a_next = args;
17620Sstevel@tonic-gate 		mi->mi_async_tail[NFS_READDIR] = args;
17630Sstevel@tonic-gate 	}
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
17660Sstevel@tonic-gate 	rp->r_count++;
17670Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
17700Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
17710Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
17720Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
17730Sstevel@tonic-gate 	}
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	mi->mi_async_req_count++;
17760Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
17770Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
17780Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
17790Sstevel@tonic-gate 	return;
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate noasync:
17820Sstevel@tonic-gate 	if (args != NULL) {
17830Sstevel@tonic-gate 		VN_RELE(vp);
17840Sstevel@tonic-gate 		crfree(cr);
17850Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
17860Sstevel@tonic-gate 	}
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 	rdc->entries = NULL;
17890Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
17900Sstevel@tonic-gate 	ASSERT(rdc->flags & RDDIR);
17910Sstevel@tonic-gate 	rdc->flags &= ~RDDIR;
17920Sstevel@tonic-gate 	rdc->flags |= RDDIRREQ;
17930Sstevel@tonic-gate 	/*
17940Sstevel@tonic-gate 	 * Check the flag to see if RDDIRWAIT is set. If RDDIRWAIT
17950Sstevel@tonic-gate 	 * is set, wakeup the thread sleeping in cv_wait_sig().
17960Sstevel@tonic-gate 	 * The woken up thread will reset the flag to RDDIR and will
17970Sstevel@tonic-gate 	 * continue with the readdir opeartion.
17980Sstevel@tonic-gate 	 */
17990Sstevel@tonic-gate 	if (rdc->flags & RDDIRWAIT) {
18000Sstevel@tonic-gate 		rdc->flags &= ~RDDIRWAIT;
18010Sstevel@tonic-gate 		cv_broadcast(&rdc->cv);
18020Sstevel@tonic-gate 	}
18030Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
18040Sstevel@tonic-gate 	rddir_cache_rele(rdc);
18050Sstevel@tonic-gate }
18060Sstevel@tonic-gate 
18070Sstevel@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 *))18080Sstevel@tonic-gate nfs_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
18090Sstevel@tonic-gate 	cred_t *cr, void (*commit)(vnode_t *, page_t *, offset3, count3,
18100Sstevel@tonic-gate 	cred_t *))
18110Sstevel@tonic-gate {
18120Sstevel@tonic-gate 	rnode_t *rp;
18130Sstevel@tonic-gate 	mntinfo_t *mi;
18140Sstevel@tonic-gate 	struct nfs_async_reqs *args;
18150Sstevel@tonic-gate 	page_t *pp;
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	rp = VTOR(vp);
18180Sstevel@tonic-gate 	mi = VTOMI(vp);
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 	/*
18210Sstevel@tonic-gate 	 * If we can't allocate a request structure, do the commit
18220Sstevel@tonic-gate 	 * operation synchronously in this thread's context.
18230Sstevel@tonic-gate 	 */
18240Sstevel@tonic-gate 	if ((args = kmem_alloc(sizeof (*args), KM_NOSLEEP)) == NULL)
18250Sstevel@tonic-gate 		goto noasync;
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	args->a_next = NULL;
18280Sstevel@tonic-gate #ifdef DEBUG
18290Sstevel@tonic-gate 	args->a_queuer = curthread;
18300Sstevel@tonic-gate #endif
18310Sstevel@tonic-gate 	VN_HOLD(vp);
18320Sstevel@tonic-gate 	args->a_vp = vp;
18330Sstevel@tonic-gate 	ASSERT(cr != NULL);
18340Sstevel@tonic-gate 	crhold(cr);
18350Sstevel@tonic-gate 	args->a_cred = cr;
18360Sstevel@tonic-gate 	args->a_io = NFS_COMMIT;
18370Sstevel@tonic-gate 	args->a_nfs_commit = commit;
18380Sstevel@tonic-gate 	args->a_nfs_plist = plist;
18390Sstevel@tonic-gate 	args->a_nfs_offset = offset;
18400Sstevel@tonic-gate 	args->a_nfs_count = count;
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 	/*
18450Sstevel@tonic-gate 	 * If asyncio has been disabled, then make a synchronous request.
18460Sstevel@tonic-gate 	 * This check is done a second time in case async io was diabled
18470Sstevel@tonic-gate 	 * while this thread was blocked waiting for memory pressure to
18480Sstevel@tonic-gate 	 * reduce or for the queue to drain.
18490Sstevel@tonic-gate 	 */
18500Sstevel@tonic-gate 	if (mi->mi_max_threads == 0) {
18510Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
18520Sstevel@tonic-gate 		goto noasync;
18530Sstevel@tonic-gate 	}
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	/*
18560Sstevel@tonic-gate 	 * Link request structure into the async list and
18570Sstevel@tonic-gate 	 * wakeup async thread to do the i/o.
18580Sstevel@tonic-gate 	 */
18590Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_COMMIT] == NULL) {
18600Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_COMMIT] = args;
18610Sstevel@tonic-gate 		mi->mi_async_tail[NFS_COMMIT] = args;
18620Sstevel@tonic-gate 	} else {
18630Sstevel@tonic-gate 		mi->mi_async_tail[NFS_COMMIT]->a_next = args;
18640Sstevel@tonic-gate 		mi->mi_async_tail[NFS_COMMIT] = args;
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
18680Sstevel@tonic-gate 	rp->r_count++;
18690Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
18720Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
18730Sstevel@tonic-gate 		kstat_waitq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
18740Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
18750Sstevel@tonic-gate 	}
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 	mi->mi_async_req_count++;
18780Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
18790Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
18800Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
18810Sstevel@tonic-gate 	return;
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate noasync:
18840Sstevel@tonic-gate 	if (args != NULL) {
18850Sstevel@tonic-gate 		VN_RELE(vp);
18860Sstevel@tonic-gate 		crfree(cr);
18870Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
18880Sstevel@tonic-gate 	}
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 	if (curproc == proc_pageout || curproc == proc_fsflush ||
1891766Scarlsonj 	    nfs_zone() != mi->mi_zone) {
18920Sstevel@tonic-gate 		while (plist != NULL) {
18930Sstevel@tonic-gate 			pp = plist;
18940Sstevel@tonic-gate 			page_sub(&plist, pp);
18950Sstevel@tonic-gate 			pp->p_fsdata = C_COMMIT;
18960Sstevel@tonic-gate 			page_unlock(pp);
18970Sstevel@tonic-gate 		}
18980Sstevel@tonic-gate 		return;
18990Sstevel@tonic-gate 	}
19000Sstevel@tonic-gate 	(*commit)(vp, plist, offset, count, cr);
19010Sstevel@tonic-gate }
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate void
nfs_async_inactive(vnode_t * vp,cred_t * cr,void (* inactive)(vnode_t *,cred_t *,caller_context_t *))19040Sstevel@tonic-gate nfs_async_inactive(vnode_t *vp, cred_t *cr,
19055331Samw     void (*inactive)(vnode_t *, cred_t *, caller_context_t *))
19060Sstevel@tonic-gate {
19070Sstevel@tonic-gate 	mntinfo_t *mi;
19080Sstevel@tonic-gate 	struct nfs_async_reqs *args;
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 	mi = VTOMI(vp);
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	args = kmem_alloc(sizeof (*args), KM_SLEEP);
19130Sstevel@tonic-gate 	args->a_next = NULL;
19140Sstevel@tonic-gate #ifdef DEBUG
19150Sstevel@tonic-gate 	args->a_queuer = curthread;
19160Sstevel@tonic-gate #endif
19170Sstevel@tonic-gate 	args->a_vp = vp;
19180Sstevel@tonic-gate 	ASSERT(cr != NULL);
19190Sstevel@tonic-gate 	crhold(cr);
19200Sstevel@tonic-gate 	args->a_cred = cr;
19210Sstevel@tonic-gate 	args->a_io = NFS_INACTIVE;
19220Sstevel@tonic-gate 	args->a_nfs_inactive = inactive;
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	/*
19250Sstevel@tonic-gate 	 * Note that we don't check mi->mi_max_threads here, since we
19260Sstevel@tonic-gate 	 * *need* to get rid of this vnode regardless of whether someone
19270Sstevel@tonic-gate 	 * set nfs3_max_threads/nfs_max_threads to zero in /etc/system.
19280Sstevel@tonic-gate 	 *
19290Sstevel@tonic-gate 	 * The manager thread knows about this and is willing to create
19305331Samw 	 * at least one thread to accommodate us.
19310Sstevel@tonic-gate 	 */
19320Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
19330Sstevel@tonic-gate 	if (mi->mi_manager_thread == NULL) {
19340Sstevel@tonic-gate 		rnode_t *rp = VTOR(vp);
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
19370Sstevel@tonic-gate 		crfree(cr);	/* drop our reference */
19380Sstevel@tonic-gate 		kmem_free(args, sizeof (*args));
19390Sstevel@tonic-gate 		/*
19400Sstevel@tonic-gate 		 * We can't do an over-the-wire call since we're in the wrong
19410Sstevel@tonic-gate 		 * zone, so we need to clean up state as best we can and then
19420Sstevel@tonic-gate 		 * throw away the vnode.
19430Sstevel@tonic-gate 		 */
19440Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
19450Sstevel@tonic-gate 		if (rp->r_unldvp != NULL) {
19460Sstevel@tonic-gate 			vnode_t *unldvp;
19470Sstevel@tonic-gate 			char *unlname;
19480Sstevel@tonic-gate 			cred_t *unlcred;
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 			unldvp = rp->r_unldvp;
19510Sstevel@tonic-gate 			rp->r_unldvp = NULL;
19520Sstevel@tonic-gate 			unlname = rp->r_unlname;
19530Sstevel@tonic-gate 			rp->r_unlname = NULL;
19540Sstevel@tonic-gate 			unlcred = rp->r_unlcred;
19550Sstevel@tonic-gate 			rp->r_unlcred = NULL;
19560Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 			VN_RELE(unldvp);
19590Sstevel@tonic-gate 			kmem_free(unlname, MAXNAMELEN);
19600Sstevel@tonic-gate 			crfree(unlcred);
19610Sstevel@tonic-gate 		} else {
19620Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
19630Sstevel@tonic-gate 		}
19640Sstevel@tonic-gate 		/*
19650Sstevel@tonic-gate 		 * No need to explicitly throw away any cached pages.  The
19660Sstevel@tonic-gate 		 * eventual rinactive() will attempt a synchronous
19670Sstevel@tonic-gate 		 * VOP_PUTPAGE() which will immediately fail since the request
19680Sstevel@tonic-gate 		 * is coming from the wrong zone, and then will proceed to call
19690Sstevel@tonic-gate 		 * nfs_invalidate_pages() which will clean things up for us.
19700Sstevel@tonic-gate 		 */
19710Sstevel@tonic-gate 		rp_addfree(VTOR(vp), cr);
19720Sstevel@tonic-gate 		return;
19730Sstevel@tonic-gate 	}
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 	if (mi->mi_async_reqs[NFS_INACTIVE] == NULL) {
19760Sstevel@tonic-gate 		mi->mi_async_reqs[NFS_INACTIVE] = args;
19770Sstevel@tonic-gate 	} else {
19780Sstevel@tonic-gate 		mi->mi_async_tail[NFS_INACTIVE]->a_next = args;
19790Sstevel@tonic-gate 	}
19800Sstevel@tonic-gate 	mi->mi_async_tail[NFS_INACTIVE] = args;
19810Sstevel@tonic-gate 	/*
19820Sstevel@tonic-gate 	 * Don't increment r_count, since we're trying to get rid of the vnode.
19830Sstevel@tonic-gate 	 */
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	mi->mi_async_req_count++;
19860Sstevel@tonic-gate 	ASSERT(mi->mi_async_req_count != 0);
19870Sstevel@tonic-gate 	cv_signal(&mi->mi_async_reqs_cv);
19880Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
19890Sstevel@tonic-gate }
19900Sstevel@tonic-gate 
199111507SVallish.Vaidyeshwara@Sun.COM static void
nfs_async_start(struct vfs * vfsp)199211507SVallish.Vaidyeshwara@Sun.COM nfs_async_start(struct vfs *vfsp)
199311507SVallish.Vaidyeshwara@Sun.COM {
199411507SVallish.Vaidyeshwara@Sun.COM 	nfs_async_common_start(vfsp, NFS_ASYNC_QUEUE);
199511507SVallish.Vaidyeshwara@Sun.COM }
199611507SVallish.Vaidyeshwara@Sun.COM 
199711507SVallish.Vaidyeshwara@Sun.COM static void
nfs_async_pgops_start(struct vfs * vfsp)199811507SVallish.Vaidyeshwara@Sun.COM nfs_async_pgops_start(struct vfs *vfsp)
199911507SVallish.Vaidyeshwara@Sun.COM {
200011507SVallish.Vaidyeshwara@Sun.COM 	nfs_async_common_start(vfsp, NFS_ASYNC_PGOPS_QUEUE);
200111507SVallish.Vaidyeshwara@Sun.COM }
200211507SVallish.Vaidyeshwara@Sun.COM 
20030Sstevel@tonic-gate /*
20040Sstevel@tonic-gate  * The async queues for each mounted file system are arranged as a
20050Sstevel@tonic-gate  * set of queues, one for each async i/o type.  Requests are taken
20060Sstevel@tonic-gate  * from the queues in a round-robin fashion.  A number of consecutive
20070Sstevel@tonic-gate  * requests are taken from each queue before moving on to the next
20080Sstevel@tonic-gate  * queue.  This functionality may allow the NFS Version 2 server to do
20090Sstevel@tonic-gate  * write clustering, even if the client is mixing writes and reads
20100Sstevel@tonic-gate  * because it will take multiple write requests from the queue
20110Sstevel@tonic-gate  * before processing any of the other async i/o types.
20120Sstevel@tonic-gate  *
201311507SVallish.Vaidyeshwara@Sun.COM  * XXX The nfs_async_common_start thread is unsafe in the light of the present
20140Sstevel@tonic-gate  * model defined by cpr to suspend the system. Specifically over the
20150Sstevel@tonic-gate  * wire calls are cpr-unsafe. The thread should be reevaluated in
20160Sstevel@tonic-gate  * case of future updates to the cpr model.
20170Sstevel@tonic-gate  */
20180Sstevel@tonic-gate static void
nfs_async_common_start(struct vfs * vfsp,int async_queue)201911507SVallish.Vaidyeshwara@Sun.COM nfs_async_common_start(struct vfs *vfsp, int async_queue)
20200Sstevel@tonic-gate {
20210Sstevel@tonic-gate 	struct nfs_async_reqs *args;
20220Sstevel@tonic-gate 	mntinfo_t *mi = VFTOMI(vfsp);
20230Sstevel@tonic-gate 	clock_t time_left = 1;
20240Sstevel@tonic-gate 	callb_cpr_t cprinfo;
20250Sstevel@tonic-gate 	int i;
202611507SVallish.Vaidyeshwara@Sun.COM 	int async_types;
202711507SVallish.Vaidyeshwara@Sun.COM 	kcondvar_t *async_work_cv;
202811507SVallish.Vaidyeshwara@Sun.COM 
202911507SVallish.Vaidyeshwara@Sun.COM 	if (async_queue == NFS_ASYNC_QUEUE) {
203011507SVallish.Vaidyeshwara@Sun.COM 		async_types = NFS_ASYNC_TYPES;
203111507SVallish.Vaidyeshwara@Sun.COM 		async_work_cv = &mi->mi_async_work_cv[NFS_ASYNC_QUEUE];
203211507SVallish.Vaidyeshwara@Sun.COM 	} else {
203311507SVallish.Vaidyeshwara@Sun.COM 		async_types = NFS_ASYNC_PGOPS_TYPES;
203411507SVallish.Vaidyeshwara@Sun.COM 		async_work_cv = &mi->mi_async_work_cv[NFS_ASYNC_PGOPS_QUEUE];
203511507SVallish.Vaidyeshwara@Sun.COM 	}
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 	/*
20380Sstevel@tonic-gate 	 * Dynamic initialization of nfs_async_timeout to allow nfs to be
20390Sstevel@tonic-gate 	 * built in an implementation independent manner.
20400Sstevel@tonic-gate 	 */
20410Sstevel@tonic-gate 	if (nfs_async_timeout == -1)
20420Sstevel@tonic-gate 		nfs_async_timeout = NFS_ASYNC_TIMEOUT;
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &mi->mi_async_lock, callb_generic_cpr, "nas");
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
20470Sstevel@tonic-gate 	for (;;) {
20480Sstevel@tonic-gate 		/*
20490Sstevel@tonic-gate 		 * Find the next queue containing an entry.  We start
20500Sstevel@tonic-gate 		 * at the current queue pointer and then round robin
20510Sstevel@tonic-gate 		 * through all of them until we either find a non-empty
20520Sstevel@tonic-gate 		 * queue or have looked through all of them.
20530Sstevel@tonic-gate 		 */
205411507SVallish.Vaidyeshwara@Sun.COM 		for (i = 0; i < async_types; i++) {
205511507SVallish.Vaidyeshwara@Sun.COM 			args = *mi->mi_async_curr[async_queue];
20560Sstevel@tonic-gate 			if (args != NULL)
20570Sstevel@tonic-gate 				break;
205811507SVallish.Vaidyeshwara@Sun.COM 			mi->mi_async_curr[async_queue]++;
205911507SVallish.Vaidyeshwara@Sun.COM 			if (mi->mi_async_curr[async_queue] ==
206011507SVallish.Vaidyeshwara@Sun.COM 			    &mi->mi_async_reqs[async_types]) {
206111507SVallish.Vaidyeshwara@Sun.COM 				mi->mi_async_curr[async_queue] =
206211507SVallish.Vaidyeshwara@Sun.COM 				    &mi->mi_async_reqs[0];
206311507SVallish.Vaidyeshwara@Sun.COM 			}
20640Sstevel@tonic-gate 		}
20650Sstevel@tonic-gate 		/*
20660Sstevel@tonic-gate 		 * If we didn't find a entry, then block until woken up
20670Sstevel@tonic-gate 		 * again and then look through the queues again.
20680Sstevel@tonic-gate 		 */
20690Sstevel@tonic-gate 		if (args == NULL) {
20700Sstevel@tonic-gate 			/*
20710Sstevel@tonic-gate 			 * Exiting is considered to be safe for CPR as well
20720Sstevel@tonic-gate 			 */
20730Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 			/*
20760Sstevel@tonic-gate 			 * Wakeup thread waiting to unmount the file
20770Sstevel@tonic-gate 			 * system only if all async threads are inactive.
20780Sstevel@tonic-gate 			 *
20790Sstevel@tonic-gate 			 * If we've timed-out and there's nothing to do,
20800Sstevel@tonic-gate 			 * then get rid of this thread.
20810Sstevel@tonic-gate 			 */
20820Sstevel@tonic-gate 			if (mi->mi_max_threads == 0 || time_left <= 0) {
208311507SVallish.Vaidyeshwara@Sun.COM 				--mi->mi_threads[async_queue];
208411507SVallish.Vaidyeshwara@Sun.COM 
208511507SVallish.Vaidyeshwara@Sun.COM 				if (mi->mi_threads[NFS_ASYNC_QUEUE] == 0 &&
208611507SVallish.Vaidyeshwara@Sun.COM 				    mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE] == 0)
20870Sstevel@tonic-gate 					cv_signal(&mi->mi_async_cv);
20880Sstevel@tonic-gate 				CALLB_CPR_EXIT(&cprinfo);
20890Sstevel@tonic-gate 				VFS_RELE(vfsp);	/* release thread's hold */
20900Sstevel@tonic-gate 				zthread_exit();
20910Sstevel@tonic-gate 				/* NOTREACHED */
20920Sstevel@tonic-gate 			}
209311507SVallish.Vaidyeshwara@Sun.COM 			time_left = cv_reltimedwait(async_work_cv,
209411066Srafael.vanoni@sun.com 			    &mi->mi_async_lock, nfs_async_timeout,
209511066Srafael.vanoni@sun.com 			    TR_CLOCK_TICK);
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &mi->mi_async_lock);
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 			continue;
21000Sstevel@tonic-gate 		}
21010Sstevel@tonic-gate 		time_left = 1;
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 		/*
21040Sstevel@tonic-gate 		 * Remove the request from the async queue and then
21050Sstevel@tonic-gate 		 * update the current async request queue pointer.  If
21060Sstevel@tonic-gate 		 * the current queue is empty or we have removed enough
21070Sstevel@tonic-gate 		 * consecutive entries from it, then reset the counter
21080Sstevel@tonic-gate 		 * for this queue and then move the current pointer to
21090Sstevel@tonic-gate 		 * the next queue.
21100Sstevel@tonic-gate 		 */
211111507SVallish.Vaidyeshwara@Sun.COM 		*mi->mi_async_curr[async_queue] = args->a_next;
211211507SVallish.Vaidyeshwara@Sun.COM 		if (*mi->mi_async_curr[async_queue] == NULL ||
21130Sstevel@tonic-gate 		    --mi->mi_async_clusters[args->a_io] == 0) {
21140Sstevel@tonic-gate 			mi->mi_async_clusters[args->a_io] =
21159750SGarima.Tripathi@Sun.COM 			    mi->mi_async_init_clusters;
211611507SVallish.Vaidyeshwara@Sun.COM 			mi->mi_async_curr[async_queue]++;
211711507SVallish.Vaidyeshwara@Sun.COM 			if (mi->mi_async_curr[async_queue] ==
211811507SVallish.Vaidyeshwara@Sun.COM 			    &mi->mi_async_reqs[async_types]) {
211911507SVallish.Vaidyeshwara@Sun.COM 				mi->mi_async_curr[async_queue] =
212011507SVallish.Vaidyeshwara@Sun.COM 				    &mi->mi_async_reqs[0];
212111507SVallish.Vaidyeshwara@Sun.COM 			}
21220Sstevel@tonic-gate 		}
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 		if (args->a_io != NFS_INACTIVE && mi->mi_io_kstats) {
21250Sstevel@tonic-gate 			mutex_enter(&mi->mi_lock);
21260Sstevel@tonic-gate 			kstat_waitq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
21270Sstevel@tonic-gate 			mutex_exit(&mi->mi_lock);
21280Sstevel@tonic-gate 		}
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 		/*
21330Sstevel@tonic-gate 		 * Obtain arguments from the async request structure.
21340Sstevel@tonic-gate 		 */
21350Sstevel@tonic-gate 		if (args->a_io == NFS_READ_AHEAD && mi->mi_max_threads > 0) {
21360Sstevel@tonic-gate 			(*args->a_nfs_readahead)(args->a_vp, args->a_nfs_blkoff,
21379750SGarima.Tripathi@Sun.COM 			    args->a_nfs_addr, args->a_nfs_seg,
21389750SGarima.Tripathi@Sun.COM 			    args->a_cred);
21390Sstevel@tonic-gate 		} else if (args->a_io == NFS_PUTAPAGE) {
21400Sstevel@tonic-gate 			(void) (*args->a_nfs_putapage)(args->a_vp,
21419750SGarima.Tripathi@Sun.COM 			    args->a_nfs_pp, args->a_nfs_off,
21429750SGarima.Tripathi@Sun.COM 			    args->a_nfs_len, args->a_nfs_flags,
21439750SGarima.Tripathi@Sun.COM 			    args->a_cred);
21440Sstevel@tonic-gate 		} else if (args->a_io == NFS_PAGEIO) {
21458879SSuhasini.Peddada@Sun.COM 			(void) (*args->a_nfs_pageio)(args->a_vp,
21469750SGarima.Tripathi@Sun.COM 			    args->a_nfs_pp, args->a_nfs_off,
21479750SGarima.Tripathi@Sun.COM 			    args->a_nfs_len, args->a_nfs_flags,
21489750SGarima.Tripathi@Sun.COM 			    args->a_cred);
21490Sstevel@tonic-gate 		} else if (args->a_io == NFS_READDIR) {
21500Sstevel@tonic-gate 			(void) ((*args->a_nfs_readdir)(args->a_vp,
21519750SGarima.Tripathi@Sun.COM 			    args->a_nfs_rdc, args->a_cred));
21520Sstevel@tonic-gate 		} else if (args->a_io == NFS_COMMIT) {
21530Sstevel@tonic-gate 			(*args->a_nfs_commit)(args->a_vp, args->a_nfs_plist,
21549750SGarima.Tripathi@Sun.COM 			    args->a_nfs_offset, args->a_nfs_count,
21559750SGarima.Tripathi@Sun.COM 			    args->a_cred);
21560Sstevel@tonic-gate 		} else if (args->a_io == NFS_INACTIVE) {
21575331Samw 			(*args->a_nfs_inactive)(args->a_vp, args->a_cred, NULL);
21580Sstevel@tonic-gate 		}
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 		/*
21610Sstevel@tonic-gate 		 * Now, release the vnode and free the credentials
21620Sstevel@tonic-gate 		 * structure.
21630Sstevel@tonic-gate 		 */
21640Sstevel@tonic-gate 		free_async_args(args);
21650Sstevel@tonic-gate 		/*
21660Sstevel@tonic-gate 		 * Reacquire the mutex because it will be needed above.
21670Sstevel@tonic-gate 		 */
21680Sstevel@tonic-gate 		mutex_enter(&mi->mi_async_lock);
21690Sstevel@tonic-gate 	}
21700Sstevel@tonic-gate }
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate void
nfs_async_stop(struct vfs * vfsp)21730Sstevel@tonic-gate nfs_async_stop(struct vfs *vfsp)
21740Sstevel@tonic-gate {
21750Sstevel@tonic-gate 	mntinfo_t *mi = VFTOMI(vfsp);
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 	/*
21780Sstevel@tonic-gate 	 * Wait for all outstanding async operations to complete and for the
21790Sstevel@tonic-gate 	 * worker threads to exit.
21800Sstevel@tonic-gate 	 */
21810Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
21820Sstevel@tonic-gate 	mi->mi_max_threads = 0;
218311507SVallish.Vaidyeshwara@Sun.COM 	NFS_WAKEALL_ASYNC_WORKERS(mi->mi_async_work_cv);
218411507SVallish.Vaidyeshwara@Sun.COM 	while (mi->mi_threads[NFS_ASYNC_QUEUE] != 0 ||
218511507SVallish.Vaidyeshwara@Sun.COM 	    mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE] != 0)
21860Sstevel@tonic-gate 		cv_wait(&mi->mi_async_cv, &mi->mi_async_lock);
21870Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
21880Sstevel@tonic-gate }
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate /*
21910Sstevel@tonic-gate  * nfs_async_stop_sig:
21920Sstevel@tonic-gate  * Wait for all outstanding putpage operation to complete. If a signal
21930Sstevel@tonic-gate  * is deliver we will abort and return non-zero. If we can put all the
21940Sstevel@tonic-gate  * pages we will return 0. This routine is called from nfs_unmount and
21955331Samw  * nfs3_unmount to make these operations interruptible.
21960Sstevel@tonic-gate  */
21970Sstevel@tonic-gate int
nfs_async_stop_sig(struct vfs * vfsp)21980Sstevel@tonic-gate nfs_async_stop_sig(struct vfs *vfsp)
21990Sstevel@tonic-gate {
22000Sstevel@tonic-gate 	mntinfo_t *mi = VFTOMI(vfsp);
22010Sstevel@tonic-gate 	ushort_t omax;
22020Sstevel@tonic-gate 	int rval;
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 	/*
22050Sstevel@tonic-gate 	 * Wait for all outstanding async operations to complete and for the
22060Sstevel@tonic-gate 	 * worker threads to exit.
22070Sstevel@tonic-gate 	 */
22080Sstevel@tonic-gate 	mutex_enter(&mi->mi_async_lock);
22090Sstevel@tonic-gate 	omax = mi->mi_max_threads;
22100Sstevel@tonic-gate 	mi->mi_max_threads = 0;
22110Sstevel@tonic-gate 	/*
22120Sstevel@tonic-gate 	 * Tell all the worker threads to exit.
22130Sstevel@tonic-gate 	 */
221411507SVallish.Vaidyeshwara@Sun.COM 	NFS_WAKEALL_ASYNC_WORKERS(mi->mi_async_work_cv);
221511507SVallish.Vaidyeshwara@Sun.COM 	while (mi->mi_threads[NFS_ASYNC_QUEUE] != 0 ||
221611507SVallish.Vaidyeshwara@Sun.COM 	    mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE] != 0) {
22170Sstevel@tonic-gate 		if (!cv_wait_sig(&mi->mi_async_cv, &mi->mi_async_lock))
22180Sstevel@tonic-gate 			break;
22190Sstevel@tonic-gate 	}
222011507SVallish.Vaidyeshwara@Sun.COM 	rval = (mi->mi_threads[NFS_ASYNC_QUEUE] != 0 ||
222111507SVallish.Vaidyeshwara@Sun.COM 	    mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE]  != 0); /* Interrupted */
22220Sstevel@tonic-gate 	if (rval)
22230Sstevel@tonic-gate 		mi->mi_max_threads = omax;
22240Sstevel@tonic-gate 	mutex_exit(&mi->mi_async_lock);
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 	return (rval);
22270Sstevel@tonic-gate }
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate int
writerp(rnode_t * rp,caddr_t base,int tcount,struct uio * uio,int pgcreated)22300Sstevel@tonic-gate writerp(rnode_t *rp, caddr_t base, int tcount, struct uio *uio, int pgcreated)
22310Sstevel@tonic-gate {
22320Sstevel@tonic-gate 	int pagecreate;
22330Sstevel@tonic-gate 	int n;
22340Sstevel@tonic-gate 	int saved_n;
22350Sstevel@tonic-gate 	caddr_t saved_base;
22360Sstevel@tonic-gate 	u_offset_t offset;
22370Sstevel@tonic-gate 	int error;
22380Sstevel@tonic-gate 	int sm_error;
22391841Spraks 	vnode_t *vp = RTOV(rp);
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate 	ASSERT(tcount <= MAXBSIZE && tcount <= uio->uio_resid);
22420Sstevel@tonic-gate 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_WRITER));
22431841Spraks 	if (!vpm_enable) {
22441841Spraks 		ASSERT(((uintptr_t)base & MAXBOFFSET) + tcount <= MAXBSIZE);
22451841Spraks 	}
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate 	/*
22480Sstevel@tonic-gate 	 * Move bytes in at most PAGESIZE chunks. We must avoid
22490Sstevel@tonic-gate 	 * spanning pages in uiomove() because page faults may cause
22500Sstevel@tonic-gate 	 * the cache to be invalidated out from under us. The r_size is not
22510Sstevel@tonic-gate 	 * updated until after the uiomove. If we push the last page of a
22520Sstevel@tonic-gate 	 * file before r_size is correct, we will lose the data written past
22530Sstevel@tonic-gate 	 * the current (and invalid) r_size.
22540Sstevel@tonic-gate 	 */
22550Sstevel@tonic-gate 	do {
22560Sstevel@tonic-gate 		offset = uio->uio_loffset;
22570Sstevel@tonic-gate 		pagecreate = 0;
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 		/*
22600Sstevel@tonic-gate 		 * n is the number of bytes required to satisfy the request
22610Sstevel@tonic-gate 		 *   or the number of bytes to fill out the page.
22620Sstevel@tonic-gate 		 */
22631841Spraks 		n = (int)MIN((PAGESIZE - (offset & PAGEOFFSET)), tcount);
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 		/*
22660Sstevel@tonic-gate 		 * Check to see if we can skip reading in the page
22670Sstevel@tonic-gate 		 * and just allocate the memory.  We can do this
22680Sstevel@tonic-gate 		 * if we are going to rewrite the entire mapping
22690Sstevel@tonic-gate 		 * or if we are going to write to or beyond the current
22700Sstevel@tonic-gate 		 * end of file from the beginning of the mapping.
22710Sstevel@tonic-gate 		 *
22720Sstevel@tonic-gate 		 * The read of r_size is now protected by r_statelock.
22730Sstevel@tonic-gate 		 */
22740Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
22750Sstevel@tonic-gate 		/*
22760Sstevel@tonic-gate 		 * When pgcreated is nonzero the caller has already done
22770Sstevel@tonic-gate 		 * a segmap_getmapflt with forcefault 0 and S_WRITE. With
22780Sstevel@tonic-gate 		 * segkpm this means we already have at least one page
22790Sstevel@tonic-gate 		 * created and mapped at base.
22800Sstevel@tonic-gate 		 */
22810Sstevel@tonic-gate 		pagecreate = pgcreated ||
22829750SGarima.Tripathi@Sun.COM 		    ((offset & PAGEOFFSET) == 0 &&
22839750SGarima.Tripathi@Sun.COM 		    (n == PAGESIZE || ((offset + n) >= rp->r_size)));
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
22861841Spraks 		if (!vpm_enable && pagecreate) {
22870Sstevel@tonic-gate 			/*
22880Sstevel@tonic-gate 			 * The last argument tells segmap_pagecreate() to
22890Sstevel@tonic-gate 			 * always lock the page, as opposed to sometimes
22900Sstevel@tonic-gate 			 * returning with the page locked. This way we avoid a
22910Sstevel@tonic-gate 			 * fault on the ensuing uiomove(), but also
22920Sstevel@tonic-gate 			 * more importantly (to fix bug 1094402) we can
22930Sstevel@tonic-gate 			 * call segmap_fault() to unlock the page in all
22940Sstevel@tonic-gate 			 * cases. An alternative would be to modify
22950Sstevel@tonic-gate 			 * segmap_pagecreate() to tell us when it is
22960Sstevel@tonic-gate 			 * locking a page, but that's a fairly major
22970Sstevel@tonic-gate 			 * interface change.
22980Sstevel@tonic-gate 			 */
22990Sstevel@tonic-gate 			if (pgcreated == 0)
23000Sstevel@tonic-gate 				(void) segmap_pagecreate(segkmap, base,
23019750SGarima.Tripathi@Sun.COM 				    (uint_t)n, 1);
23020Sstevel@tonic-gate 			saved_base = base;
23030Sstevel@tonic-gate 			saved_n = n;
23040Sstevel@tonic-gate 		}
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate 		/*
23070Sstevel@tonic-gate 		 * The number of bytes of data in the last page can not
23080Sstevel@tonic-gate 		 * be accurately be determined while page is being
23090Sstevel@tonic-gate 		 * uiomove'd to and the size of the file being updated.
23100Sstevel@tonic-gate 		 * Thus, inform threads which need to know accurately
23110Sstevel@tonic-gate 		 * how much data is in the last page of the file.  They
23120Sstevel@tonic-gate 		 * will not do the i/o immediately, but will arrange for
23130Sstevel@tonic-gate 		 * the i/o to happen later when this modify operation
23140Sstevel@tonic-gate 		 * will have finished.
23150Sstevel@tonic-gate 		 */
23160Sstevel@tonic-gate 		ASSERT(!(rp->r_flags & RMODINPROGRESS));
23170Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
23180Sstevel@tonic-gate 		rp->r_flags |= RMODINPROGRESS;
23190Sstevel@tonic-gate 		rp->r_modaddr = (offset & MAXBMASK);
23200Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
23210Sstevel@tonic-gate 
23221841Spraks 		if (vpm_enable) {
23231841Spraks 			/*
23241841Spraks 			 * Copy data. If new pages are created, part of
23251841Spraks 			 * the page that is not written will be initizliazed
23261841Spraks 			 * with zeros.
23271841Spraks 			 */
23288879SSuhasini.Peddada@Sun.COM 			error = vpm_data_copy(vp, offset, n, uio,
23299750SGarima.Tripathi@Sun.COM 			    !pagecreate, NULL, 0, S_WRITE);
23301841Spraks 		} else {
23311841Spraks 			error = uiomove(base, n, UIO_WRITE, uio);
23321841Spraks 		}
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 		/*
23350Sstevel@tonic-gate 		 * r_size is the maximum number of
23360Sstevel@tonic-gate 		 * bytes known to be in the file.
23370Sstevel@tonic-gate 		 * Make sure it is at least as high as the
23380Sstevel@tonic-gate 		 * first unwritten byte pointed to by uio_loffset.
23390Sstevel@tonic-gate 		 */
23400Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
23410Sstevel@tonic-gate 		if (rp->r_size < uio->uio_loffset)
23420Sstevel@tonic-gate 			rp->r_size = uio->uio_loffset;
23430Sstevel@tonic-gate 		rp->r_flags &= ~RMODINPROGRESS;
23440Sstevel@tonic-gate 		rp->r_flags |= RDIRTY;
23450Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 		/* n = # of bytes written */
23480Sstevel@tonic-gate 		n = (int)(uio->uio_loffset - offset);
23491841Spraks 
23501841Spraks 		if (!vpm_enable) {
23511841Spraks 			base += n;
23521841Spraks 		}
23530Sstevel@tonic-gate 		tcount -= n;
23540Sstevel@tonic-gate 		/*
23550Sstevel@tonic-gate 		 * If we created pages w/o initializing them completely,
23560Sstevel@tonic-gate 		 * we need to zero the part that wasn't set up.
23570Sstevel@tonic-gate 		 * This happens on a most EOF write cases and if
23580Sstevel@tonic-gate 		 * we had some sort of error during the uiomove.
23590Sstevel@tonic-gate 		 */
23601841Spraks 		if (!vpm_enable && pagecreate) {
23610Sstevel@tonic-gate 			if ((uio->uio_loffset & PAGEOFFSET) || n == 0)
23620Sstevel@tonic-gate 				(void) kzero(base, PAGESIZE - n);
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 			if (pgcreated) {
23650Sstevel@tonic-gate 				/*
23660Sstevel@tonic-gate 				 * Caller is responsible for this page,
23670Sstevel@tonic-gate 				 * it was not created in this loop.
23680Sstevel@tonic-gate 				 */
23690Sstevel@tonic-gate 				pgcreated = 0;
23700Sstevel@tonic-gate 			} else {
23710Sstevel@tonic-gate 				/*
23720Sstevel@tonic-gate 				 * For bug 1094402: segmap_pagecreate locks
23730Sstevel@tonic-gate 				 * page. Unlock it. This also unlocks the
23740Sstevel@tonic-gate 				 * pages allocated by page_create_va() in
23750Sstevel@tonic-gate 				 * segmap_pagecreate().
23760Sstevel@tonic-gate 				 */
23770Sstevel@tonic-gate 				sm_error = segmap_fault(kas.a_hat, segkmap,
23789750SGarima.Tripathi@Sun.COM 				    saved_base, saved_n,
23799750SGarima.Tripathi@Sun.COM 				    F_SOFTUNLOCK, S_WRITE);
23800Sstevel@tonic-gate 				if (error == 0)
23810Sstevel@tonic-gate 					error = sm_error;
23820Sstevel@tonic-gate 			}
23830Sstevel@tonic-gate 		}
23840Sstevel@tonic-gate 	} while (tcount > 0 && error == 0);
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	return (error);
23870Sstevel@tonic-gate }
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate int
nfs_putpages(vnode_t * vp,u_offset_t off,size_t len,int flags,cred_t * cr)23900Sstevel@tonic-gate nfs_putpages(vnode_t *vp, u_offset_t off, size_t len, int flags, cred_t *cr)
23910Sstevel@tonic-gate {
23920Sstevel@tonic-gate 	rnode_t *rp;
23930Sstevel@tonic-gate 	page_t *pp;
23940Sstevel@tonic-gate 	u_offset_t eoff;
23950Sstevel@tonic-gate 	u_offset_t io_off;
23960Sstevel@tonic-gate 	size_t io_len;
23970Sstevel@tonic-gate 	int error;
23980Sstevel@tonic-gate 	int rdirty;
23990Sstevel@tonic-gate 	int err;
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate 	rp = VTOR(vp);
24020Sstevel@tonic-gate 	ASSERT(rp->r_count > 0);
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	if (!vn_has_cached_data(vp))
24050Sstevel@tonic-gate 		return (0);
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	ASSERT(vp->v_type != VCHR);
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	/*
24100Sstevel@tonic-gate 	 * If ROUTOFSPACE is set, then all writes turn into B_INVAL
24110Sstevel@tonic-gate 	 * writes.  B_FORCE is set to force the VM system to actually
24120Sstevel@tonic-gate 	 * invalidate the pages, even if the i/o failed.  The pages
24130Sstevel@tonic-gate 	 * need to get invalidated because they can't be written out
24140Sstevel@tonic-gate 	 * because there isn't any space left on either the server's
24150Sstevel@tonic-gate 	 * file system or in the user's disk quota.  The B_FREE bit
24160Sstevel@tonic-gate 	 * is cleared to avoid confusion as to whether this is a
24170Sstevel@tonic-gate 	 * request to place the page on the freelist or to destroy
24180Sstevel@tonic-gate 	 * it.
24190Sstevel@tonic-gate 	 */
24200Sstevel@tonic-gate 	if ((rp->r_flags & ROUTOFSPACE) ||
24210Sstevel@tonic-gate 	    (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED))
24220Sstevel@tonic-gate 		flags = (flags & ~B_FREE) | B_INVAL | B_FORCE;
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	if (len == 0) {
24250Sstevel@tonic-gate 		/*
24260Sstevel@tonic-gate 		 * If doing a full file synchronous operation, then clear
24270Sstevel@tonic-gate 		 * the RDIRTY bit.  If a page gets dirtied while the flush
24280Sstevel@tonic-gate 		 * is happening, then RDIRTY will get set again.  The
24290Sstevel@tonic-gate 		 * RDIRTY bit must get cleared before the flush so that
24300Sstevel@tonic-gate 		 * we don't lose this information.
24311832Sdh145677 		 *
24321832Sdh145677 		 * If there are no full file async write operations
24331832Sdh145677 		 * pending and RDIRTY bit is set, clear it.
24340Sstevel@tonic-gate 		 */
24350Sstevel@tonic-gate 		if (off == (u_offset_t)0 &&
24360Sstevel@tonic-gate 		    !(flags & B_ASYNC) &&
24370Sstevel@tonic-gate 		    (rp->r_flags & RDIRTY)) {
24380Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
24390Sstevel@tonic-gate 			rdirty = (rp->r_flags & RDIRTY);
24400Sstevel@tonic-gate 			rp->r_flags &= ~RDIRTY;
24410Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
24421832Sdh145677 		} else if (flags & B_ASYNC && off == (u_offset_t)0) {
24431832Sdh145677 			mutex_enter(&rp->r_statelock);
24441832Sdh145677 			if (rp->r_flags & RDIRTY && rp->r_awcount == 0) {
24451832Sdh145677 				rdirty = (rp->r_flags & RDIRTY);
24461832Sdh145677 				rp->r_flags &= ~RDIRTY;
24471832Sdh145677 			}
24481832Sdh145677 			mutex_exit(&rp->r_statelock);
24490Sstevel@tonic-gate 		} else
24500Sstevel@tonic-gate 			rdirty = 0;
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate 		/*
24530Sstevel@tonic-gate 		 * Search the entire vp list for pages >= off, and flush
24540Sstevel@tonic-gate 		 * the dirty pages.
24550Sstevel@tonic-gate 		 */
24568879SSuhasini.Peddada@Sun.COM 		error = pvn_vplist_dirty(vp, off, rp->r_putapage,
24579750SGarima.Tripathi@Sun.COM 		    flags, cr);
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 		/*
24605331Samw 		 * If an error occurred and the file was marked as dirty
24610Sstevel@tonic-gate 		 * before and we aren't forcibly invalidating pages, then
24620Sstevel@tonic-gate 		 * reset the RDIRTY flag.
24630Sstevel@tonic-gate 		 */
24640Sstevel@tonic-gate 		if (error && rdirty &&
24650Sstevel@tonic-gate 		    (flags & (B_INVAL | B_FORCE)) != (B_INVAL | B_FORCE)) {
24660Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
24670Sstevel@tonic-gate 			rp->r_flags |= RDIRTY;
24680Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
24690Sstevel@tonic-gate 		}
24700Sstevel@tonic-gate 	} else {
24710Sstevel@tonic-gate 		/*
24720Sstevel@tonic-gate 		 * Do a range from [off...off + len) looking for pages
24730Sstevel@tonic-gate 		 * to deal with.
24740Sstevel@tonic-gate 		 */
24750Sstevel@tonic-gate 		error = 0;
24760Sstevel@tonic-gate #ifdef lint
24770Sstevel@tonic-gate 		io_len = 0;
24780Sstevel@tonic-gate #endif
24790Sstevel@tonic-gate 		eoff = off + len;
24800Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
24810Sstevel@tonic-gate 		for (io_off = off; io_off < eoff && io_off < rp->r_size;
24820Sstevel@tonic-gate 		    io_off += io_len) {
24830Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
24840Sstevel@tonic-gate 			/*
24850Sstevel@tonic-gate 			 * If we are not invalidating, synchronously
24860Sstevel@tonic-gate 			 * freeing or writing pages use the routine
24870Sstevel@tonic-gate 			 * page_lookup_nowait() to prevent reclaiming
24880Sstevel@tonic-gate 			 * them from the free list.
24890Sstevel@tonic-gate 			 */
24900Sstevel@tonic-gate 			if ((flags & B_INVAL) || !(flags & B_ASYNC)) {
24910Sstevel@tonic-gate 				pp = page_lookup(vp, io_off,
24920Sstevel@tonic-gate 				    (flags & (B_INVAL | B_FREE)) ?
24930Sstevel@tonic-gate 				    SE_EXCL : SE_SHARED);
24940Sstevel@tonic-gate 			} else {
24950Sstevel@tonic-gate 				pp = page_lookup_nowait(vp, io_off,
24960Sstevel@tonic-gate 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
24970Sstevel@tonic-gate 			}
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 			if (pp == NULL || !pvn_getdirty(pp, flags))
25000Sstevel@tonic-gate 				io_len = PAGESIZE;
25010Sstevel@tonic-gate 			else {
25020Sstevel@tonic-gate 				err = (*rp->r_putapage)(vp, pp, &io_off,
25030Sstevel@tonic-gate 				    &io_len, flags, cr);
25040Sstevel@tonic-gate 				if (!error)
25050Sstevel@tonic-gate 					error = err;
25060Sstevel@tonic-gate 				/*
25070Sstevel@tonic-gate 				 * "io_off" and "io_len" are returned as
25080Sstevel@tonic-gate 				 * the range of pages we actually wrote.
25090Sstevel@tonic-gate 				 * This allows us to skip ahead more quickly
25100Sstevel@tonic-gate 				 * since several pages may've been dealt
25110Sstevel@tonic-gate 				 * with by this iteration of the loop.
25120Sstevel@tonic-gate 				 */
25130Sstevel@tonic-gate 			}
25140Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
25150Sstevel@tonic-gate 		}
25160Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
25170Sstevel@tonic-gate 	}
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 	return (error);
25200Sstevel@tonic-gate }
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate void
nfs_invalidate_pages(vnode_t * vp,u_offset_t off,cred_t * cr)25230Sstevel@tonic-gate nfs_invalidate_pages(vnode_t *vp, u_offset_t off, cred_t *cr)
25240Sstevel@tonic-gate {
25250Sstevel@tonic-gate 	rnode_t *rp;
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 	rp = VTOR(vp);
25280Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
25290Sstevel@tonic-gate 	while (rp->r_flags & RTRUNCATE)
25300Sstevel@tonic-gate 		cv_wait(&rp->r_cv, &rp->r_statelock);
25310Sstevel@tonic-gate 	rp->r_flags |= RTRUNCATE;
25320Sstevel@tonic-gate 	if (off == (u_offset_t)0) {
25330Sstevel@tonic-gate 		rp->r_flags &= ~RDIRTY;
25340Sstevel@tonic-gate 		if (!(rp->r_flags & RSTALE))
25350Sstevel@tonic-gate 			rp->r_error = 0;
25360Sstevel@tonic-gate 	}
25370Sstevel@tonic-gate 	rp->r_truncaddr = off;
25380Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
25390Sstevel@tonic-gate 	(void) pvn_vplist_dirty(vp, off, rp->r_putapage,
25409750SGarima.Tripathi@Sun.COM 	    B_INVAL | B_TRUNC, cr);
25410Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
25420Sstevel@tonic-gate 	rp->r_flags &= ~RTRUNCATE;
25430Sstevel@tonic-gate 	cv_broadcast(&rp->r_cv);
25440Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
25450Sstevel@tonic-gate }
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate static int nfs_write_error_to_cons_only = 0;
25480Sstevel@tonic-gate #define	MSG(x)	(nfs_write_error_to_cons_only ? (x) : (x) + 1)
25490Sstevel@tonic-gate 
25500Sstevel@tonic-gate /*
25510Sstevel@tonic-gate  * Print a file handle
25520Sstevel@tonic-gate  */
25530Sstevel@tonic-gate void
nfs_printfhandle(nfs_fhandle * fhp)25540Sstevel@tonic-gate nfs_printfhandle(nfs_fhandle *fhp)
25550Sstevel@tonic-gate {
25560Sstevel@tonic-gate 	int *ip;
25570Sstevel@tonic-gate 	char *buf;
25580Sstevel@tonic-gate 	size_t bufsize;
25590Sstevel@tonic-gate 	char *cp;
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 	/*
25620Sstevel@tonic-gate 	 * 13 == "(file handle:"
25630Sstevel@tonic-gate 	 * maximum of NFS_FHANDLE / sizeof (*ip) elements in fh_buf times
25640Sstevel@tonic-gate 	 *	1 == ' '
25650Sstevel@tonic-gate 	 *	8 == maximum strlen of "%x"
25660Sstevel@tonic-gate 	 * 3 == ")\n\0"
25670Sstevel@tonic-gate 	 */
25680Sstevel@tonic-gate 	bufsize = 13 + ((NFS_FHANDLE_LEN / sizeof (*ip)) * (1 + 8)) + 3;
25690Sstevel@tonic-gate 	buf = kmem_alloc(bufsize, KM_NOSLEEP);
25700Sstevel@tonic-gate 	if (buf == NULL)
25710Sstevel@tonic-gate 		return;
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 	cp = buf;
25740Sstevel@tonic-gate 	(void) strcpy(cp, "(file handle:");
25750Sstevel@tonic-gate 	while (*cp != '\0')
25760Sstevel@tonic-gate 		cp++;
25770Sstevel@tonic-gate 	for (ip = (int *)fhp->fh_buf;
25780Sstevel@tonic-gate 	    ip < (int *)&fhp->fh_buf[fhp->fh_len];
25790Sstevel@tonic-gate 	    ip++) {
25800Sstevel@tonic-gate 		(void) sprintf(cp, " %x", *ip);
25810Sstevel@tonic-gate 		while (*cp != '\0')
25820Sstevel@tonic-gate 			cp++;
25830Sstevel@tonic-gate 	}
25840Sstevel@tonic-gate 	(void) strcpy(cp, ")\n");
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate 	zcmn_err(getzoneid(), CE_CONT, MSG("^%s"), buf);
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate 	kmem_free(buf, bufsize);
25890Sstevel@tonic-gate }
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate /*
25920Sstevel@tonic-gate  * Notify the system administrator that an NFS write error has
25930Sstevel@tonic-gate  * occurred.
25940Sstevel@tonic-gate  */
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate /* seconds between ENOSPC/EDQUOT messages */
25970Sstevel@tonic-gate clock_t nfs_write_error_interval = 5;
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate void
nfs_write_error(vnode_t * vp,int error,cred_t * cr)26000Sstevel@tonic-gate nfs_write_error(vnode_t *vp, int error, cred_t *cr)
26010Sstevel@tonic-gate {
26020Sstevel@tonic-gate 	mntinfo_t *mi;
260311066Srafael.vanoni@sun.com 	clock_t now;
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 	mi = VTOMI(vp);
26060Sstevel@tonic-gate 	/*
26070Sstevel@tonic-gate 	 * In case of forced unmount or zone shutdown, do not print any
26080Sstevel@tonic-gate 	 * messages since it can flood the console with error messages.
26090Sstevel@tonic-gate 	 */
26100Sstevel@tonic-gate 	if (FS_OR_ZONE_GONE(mi->mi_vfsp))
26110Sstevel@tonic-gate 		return;
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 	/*
26140Sstevel@tonic-gate 	 * No use in flooding the console with ENOSPC
26150Sstevel@tonic-gate 	 * messages from the same file system.
26160Sstevel@tonic-gate 	 */
261711066Srafael.vanoni@sun.com 	now = ddi_get_lbolt();
26180Sstevel@tonic-gate 	if ((error != ENOSPC && error != EDQUOT) ||
261911066Srafael.vanoni@sun.com 	    now - mi->mi_printftime > 0) {
26200Sstevel@tonic-gate 		zoneid_t zoneid = mi->mi_zone->zone_id;
26210Sstevel@tonic-gate 
26220Sstevel@tonic-gate #ifdef DEBUG
26230Sstevel@tonic-gate 		nfs_perror(error, "NFS%ld write error on host %s: %m.\n",
26240Sstevel@tonic-gate 		    mi->mi_vers, VTOR(vp)->r_server->sv_hostname, NULL);
26250Sstevel@tonic-gate #else
26260Sstevel@tonic-gate 		nfs_perror(error, "NFS write error on host %s: %m.\n",
26270Sstevel@tonic-gate 		    VTOR(vp)->r_server->sv_hostname, NULL);
26280Sstevel@tonic-gate #endif
26290Sstevel@tonic-gate 		if (error == ENOSPC || error == EDQUOT) {
26300Sstevel@tonic-gate 			zcmn_err(zoneid, CE_CONT,
26310Sstevel@tonic-gate 			    MSG("^File: userid=%d, groupid=%d\n"),
26320Sstevel@tonic-gate 			    crgetuid(cr), crgetgid(cr));
26330Sstevel@tonic-gate 			if (crgetuid(CRED()) != crgetuid(cr) ||
26340Sstevel@tonic-gate 			    crgetgid(CRED()) != crgetgid(cr)) {
26350Sstevel@tonic-gate 				zcmn_err(zoneid, CE_CONT,
26360Sstevel@tonic-gate 				    MSG("^User: userid=%d, groupid=%d\n"),
26370Sstevel@tonic-gate 				    crgetuid(CRED()), crgetgid(CRED()));
26380Sstevel@tonic-gate 			}
263911066Srafael.vanoni@sun.com 			mi->mi_printftime = now +
26400Sstevel@tonic-gate 			    nfs_write_error_interval * hz;
26410Sstevel@tonic-gate 		}
26420Sstevel@tonic-gate 		nfs_printfhandle(&VTOR(vp)->r_fh);
26430Sstevel@tonic-gate #ifdef DEBUG
26440Sstevel@tonic-gate 		if (error == EACCES) {
26450Sstevel@tonic-gate 			zcmn_err(zoneid, CE_CONT,
26460Sstevel@tonic-gate 			    MSG("^nfs_bio: cred is%s kcred\n"),
26470Sstevel@tonic-gate 			    cr == kcred ? "" : " not");
26480Sstevel@tonic-gate 		}
26490Sstevel@tonic-gate #endif
26500Sstevel@tonic-gate 	}
26510Sstevel@tonic-gate }
26520Sstevel@tonic-gate 
26530Sstevel@tonic-gate /* ARGSUSED */
26540Sstevel@tonic-gate static void *
nfs_mi_init(zoneid_t zoneid)26550Sstevel@tonic-gate nfs_mi_init(zoneid_t zoneid)
26560Sstevel@tonic-gate {
26570Sstevel@tonic-gate 	struct mi_globals *mig;
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 	mig = kmem_alloc(sizeof (*mig), KM_SLEEP);
26600Sstevel@tonic-gate 	mutex_init(&mig->mig_lock, NULL, MUTEX_DEFAULT, NULL);
26610Sstevel@tonic-gate 	list_create(&mig->mig_list, sizeof (mntinfo_t),
26620Sstevel@tonic-gate 	    offsetof(mntinfo_t, mi_zone_node));
26630Sstevel@tonic-gate 	mig->mig_destructor_called = B_FALSE;
26640Sstevel@tonic-gate 	return (mig);
26650Sstevel@tonic-gate }
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate /*
26680Sstevel@tonic-gate  * Callback routine to tell all NFS mounts in the zone to stop creating new
26690Sstevel@tonic-gate  * threads.  Existing threads should exit.
26700Sstevel@tonic-gate  */
26710Sstevel@tonic-gate /* ARGSUSED */
26720Sstevel@tonic-gate static void
nfs_mi_shutdown(zoneid_t zoneid,void * data)26730Sstevel@tonic-gate nfs_mi_shutdown(zoneid_t zoneid, void *data)
26740Sstevel@tonic-gate {
26750Sstevel@tonic-gate 	struct mi_globals *mig = data;
26760Sstevel@tonic-gate 	mntinfo_t *mi;
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 	ASSERT(mig != NULL);
2679264Sthurlow again:
26800Sstevel@tonic-gate 	mutex_enter(&mig->mig_lock);
26810Sstevel@tonic-gate 	for (mi = list_head(&mig->mig_list); mi != NULL;
26820Sstevel@tonic-gate 	    mi = list_next(&mig->mig_list, mi)) {
2683264Sthurlow 
2684264Sthurlow 		/*
2685264Sthurlow 		 * If we've done the shutdown work for this FS, skip.
2686264Sthurlow 		 * Once we go off the end of the list, we're done.
2687264Sthurlow 		 */
2688264Sthurlow 		if (mi->mi_flags & MI_DEAD)
2689264Sthurlow 			continue;
2690264Sthurlow 
2691264Sthurlow 		/*
2692264Sthurlow 		 * We will do work, so not done.  Get a hold on the FS.
2693264Sthurlow 		 */
2694264Sthurlow 		VFS_HOLD(mi->mi_vfsp);
2695264Sthurlow 
26960Sstevel@tonic-gate 		/*
26970Sstevel@tonic-gate 		 * purge the DNLC for this filesystem
26980Sstevel@tonic-gate 		 */
26990Sstevel@tonic-gate 		(void) dnlc_purge_vfsp(mi->mi_vfsp, 0);
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate 		mutex_enter(&mi->mi_async_lock);
27020Sstevel@tonic-gate 		/*
27030Sstevel@tonic-gate 		 * Tell existing async worker threads to exit.
27040Sstevel@tonic-gate 		 */
27050Sstevel@tonic-gate 		mi->mi_max_threads = 0;
270611507SVallish.Vaidyeshwara@Sun.COM 		NFS_WAKEALL_ASYNC_WORKERS(mi->mi_async_work_cv);
27070Sstevel@tonic-gate 		/*
27080Sstevel@tonic-gate 		 * Set MI_ASYNC_MGR_STOP so the async manager thread starts
27090Sstevel@tonic-gate 		 * getting ready to exit when it's done with its current work.
2710264Sthurlow 		 * Also set MI_DEAD to note we've acted on this FS.
27110Sstevel@tonic-gate 		 */
27120Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
2713264Sthurlow 		mi->mi_flags |= (MI_ASYNC_MGR_STOP|MI_DEAD);
27140Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
27150Sstevel@tonic-gate 		/*
27160Sstevel@tonic-gate 		 * Wake up the async manager thread.
27170Sstevel@tonic-gate 		 */
27180Sstevel@tonic-gate 		cv_broadcast(&mi->mi_async_reqs_cv);
27190Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
2720264Sthurlow 
2721264Sthurlow 		/*
2722264Sthurlow 		 * Drop lock and release FS, which may change list, then repeat.
2723264Sthurlow 		 * We're done when every mi has been done or the list is empty.
2724264Sthurlow 		 */
2725264Sthurlow 		mutex_exit(&mig->mig_lock);
2726264Sthurlow 		VFS_RELE(mi->mi_vfsp);
2727264Sthurlow 		goto again;
27280Sstevel@tonic-gate 	}
27290Sstevel@tonic-gate 	mutex_exit(&mig->mig_lock);
27300Sstevel@tonic-gate }
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate static void
nfs_mi_free_globals(struct mi_globals * mig)27330Sstevel@tonic-gate nfs_mi_free_globals(struct mi_globals *mig)
27340Sstevel@tonic-gate {
27350Sstevel@tonic-gate 	list_destroy(&mig->mig_list);	/* makes sure the list is empty */
27360Sstevel@tonic-gate 	mutex_destroy(&mig->mig_lock);
27370Sstevel@tonic-gate 	kmem_free(mig, sizeof (*mig));
27380Sstevel@tonic-gate 
27390Sstevel@tonic-gate }
27400Sstevel@tonic-gate 
27410Sstevel@tonic-gate /* ARGSUSED */
27420Sstevel@tonic-gate static void
nfs_mi_destroy(zoneid_t zoneid,void * data)27430Sstevel@tonic-gate nfs_mi_destroy(zoneid_t zoneid, void *data)
27440Sstevel@tonic-gate {
27450Sstevel@tonic-gate 	struct mi_globals *mig = data;
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate 	ASSERT(mig != NULL);
27480Sstevel@tonic-gate 	mutex_enter(&mig->mig_lock);
27490Sstevel@tonic-gate 	if (list_head(&mig->mig_list) != NULL) {
27500Sstevel@tonic-gate 		/* Still waiting for VFS_FREEVFS() */
27510Sstevel@tonic-gate 		mig->mig_destructor_called = B_TRUE;
27520Sstevel@tonic-gate 		mutex_exit(&mig->mig_lock);
27530Sstevel@tonic-gate 		return;
27540Sstevel@tonic-gate 	}
27550Sstevel@tonic-gate 	nfs_mi_free_globals(mig);
27560Sstevel@tonic-gate }
27570Sstevel@tonic-gate 
27580Sstevel@tonic-gate /*
27590Sstevel@tonic-gate  * Add an NFS mount to the per-zone list of NFS mounts.
27600Sstevel@tonic-gate  */
27610Sstevel@tonic-gate void
nfs_mi_zonelist_add(mntinfo_t * mi)27620Sstevel@tonic-gate nfs_mi_zonelist_add(mntinfo_t *mi)
27630Sstevel@tonic-gate {
27640Sstevel@tonic-gate 	struct mi_globals *mig;
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	mig = zone_getspecific(mi_list_key, mi->mi_zone);
27670Sstevel@tonic-gate 	mutex_enter(&mig->mig_lock);
27680Sstevel@tonic-gate 	list_insert_head(&mig->mig_list, mi);
27690Sstevel@tonic-gate 	mutex_exit(&mig->mig_lock);
27700Sstevel@tonic-gate }
27710Sstevel@tonic-gate 
27720Sstevel@tonic-gate /*
27730Sstevel@tonic-gate  * Remove an NFS mount from the per-zone list of NFS mounts.
27740Sstevel@tonic-gate  */
27750Sstevel@tonic-gate static void
nfs_mi_zonelist_remove(mntinfo_t * mi)27760Sstevel@tonic-gate nfs_mi_zonelist_remove(mntinfo_t *mi)
27770Sstevel@tonic-gate {
27780Sstevel@tonic-gate 	struct mi_globals *mig;
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate 	mig = zone_getspecific(mi_list_key, mi->mi_zone);
27810Sstevel@tonic-gate 	mutex_enter(&mig->mig_lock);
27820Sstevel@tonic-gate 	list_remove(&mig->mig_list, mi);
27830Sstevel@tonic-gate 	/*
27840Sstevel@tonic-gate 	 * We can be called asynchronously by VFS_FREEVFS() after the zone
27850Sstevel@tonic-gate 	 * shutdown/destroy callbacks have executed; if so, clean up the zone's
27860Sstevel@tonic-gate 	 * mi globals.
27870Sstevel@tonic-gate 	 */
27880Sstevel@tonic-gate 	if (list_head(&mig->mig_list) == NULL &&
27890Sstevel@tonic-gate 	    mig->mig_destructor_called == B_TRUE) {
27900Sstevel@tonic-gate 		nfs_mi_free_globals(mig);
27910Sstevel@tonic-gate 		return;
27920Sstevel@tonic-gate 	}
27930Sstevel@tonic-gate 	mutex_exit(&mig->mig_lock);
27940Sstevel@tonic-gate }
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate /*
27970Sstevel@tonic-gate  * NFS Client initialization routine.  This routine should only be called
27980Sstevel@tonic-gate  * once.  It performs the following tasks:
27990Sstevel@tonic-gate  *	- Initalize all global locks
28000Sstevel@tonic-gate  * 	- Call sub-initialization routines (localize access to variables)
28010Sstevel@tonic-gate  */
28020Sstevel@tonic-gate int
nfs_clntinit(void)28030Sstevel@tonic-gate nfs_clntinit(void)
28040Sstevel@tonic-gate {
28050Sstevel@tonic-gate #ifdef DEBUG
28060Sstevel@tonic-gate 	static boolean_t nfs_clntup = B_FALSE;
28070Sstevel@tonic-gate #endif
28080Sstevel@tonic-gate 	int error;
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate #ifdef DEBUG
28110Sstevel@tonic-gate 	ASSERT(nfs_clntup == B_FALSE);
28120Sstevel@tonic-gate #endif
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate 	error = nfs_subrinit();
28150Sstevel@tonic-gate 	if (error)
28160Sstevel@tonic-gate 		return (error);
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 	error = nfs_vfsinit();
28190Sstevel@tonic-gate 	if (error) {
28200Sstevel@tonic-gate 		/*
28210Sstevel@tonic-gate 		 * Cleanup nfs_subrinit() work
28220Sstevel@tonic-gate 		 */
28230Sstevel@tonic-gate 		nfs_subrfini();
28240Sstevel@tonic-gate 		return (error);
28250Sstevel@tonic-gate 	}
28260Sstevel@tonic-gate 	zone_key_create(&mi_list_key, nfs_mi_init, nfs_mi_shutdown,
28270Sstevel@tonic-gate 	    nfs_mi_destroy);
28280Sstevel@tonic-gate 
28290Sstevel@tonic-gate 	nfs4_clnt_init();
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate #ifdef DEBUG
28320Sstevel@tonic-gate 	nfs_clntup = B_TRUE;
28330Sstevel@tonic-gate #endif
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate 	return (0);
28360Sstevel@tonic-gate }
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate /*
28390Sstevel@tonic-gate  * This routine is only called if the NFS Client has been initialized but
28400Sstevel@tonic-gate  * the module failed to be installed. This routine will cleanup the previously
28410Sstevel@tonic-gate  * allocated/initialized work.
28420Sstevel@tonic-gate  */
28430Sstevel@tonic-gate void
nfs_clntfini(void)28440Sstevel@tonic-gate nfs_clntfini(void)
28450Sstevel@tonic-gate {
28460Sstevel@tonic-gate 	(void) zone_key_delete(mi_list_key);
28470Sstevel@tonic-gate 	nfs_subrfini();
28480Sstevel@tonic-gate 	nfs_vfsfini();
28490Sstevel@tonic-gate 	nfs4_clnt_fini();
28500Sstevel@tonic-gate }
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate /*
28530Sstevel@tonic-gate  * nfs_lockrelease:
28540Sstevel@tonic-gate  *
28550Sstevel@tonic-gate  * Release any locks on the given vnode that are held by the current
28560Sstevel@tonic-gate  * process.
28570Sstevel@tonic-gate  */
28580Sstevel@tonic-gate void
nfs_lockrelease(vnode_t * vp,int flag,offset_t offset,cred_t * cr)28590Sstevel@tonic-gate nfs_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
28600Sstevel@tonic-gate {
28610Sstevel@tonic-gate 	flock64_t ld;
28620Sstevel@tonic-gate 	struct shrlock shr;
28630Sstevel@tonic-gate 	char *buf;
28640Sstevel@tonic-gate 	int remote_lock_possible;
28650Sstevel@tonic-gate 	int ret;
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate 	ASSERT((uintptr_t)vp > KERNELBASE);
28680Sstevel@tonic-gate 
28690Sstevel@tonic-gate 	/*
28700Sstevel@tonic-gate 	 * Generate an explicit unlock operation for the entire file.  As a
28710Sstevel@tonic-gate 	 * partial optimization, only generate the unlock if there is a
28720Sstevel@tonic-gate 	 * lock registered for the file.  We could check whether this
28730Sstevel@tonic-gate 	 * particular process has any locks on the file, but that would
28740Sstevel@tonic-gate 	 * require the local locking code to provide yet another query
28750Sstevel@tonic-gate 	 * routine.  Note that no explicit synchronization is needed here.
28760Sstevel@tonic-gate 	 * At worst, flk_has_remote_locks() will return a false positive,
28770Sstevel@tonic-gate 	 * in which case the unlock call wastes time but doesn't harm
28780Sstevel@tonic-gate 	 * correctness.
28790Sstevel@tonic-gate 	 *
28800Sstevel@tonic-gate 	 * In addition, an unlock request is generated if the process
28810Sstevel@tonic-gate 	 * is listed as possibly having a lock on the file because the
28820Sstevel@tonic-gate 	 * server and client lock managers may have gotten out of sync.
28830Sstevel@tonic-gate 	 * N.B. It is important to make sure nfs_remove_locking_id() is
28840Sstevel@tonic-gate 	 * called here even if flk_has_remote_locks(vp) reports true.
28850Sstevel@tonic-gate 	 * If it is not called and there is an entry on the process id
28860Sstevel@tonic-gate 	 * list, that entry will never get removed.
28870Sstevel@tonic-gate 	 */
28880Sstevel@tonic-gate 	remote_lock_possible = nfs_remove_locking_id(vp, RLMPL_PID,
28890Sstevel@tonic-gate 	    (char *)&(ttoproc(curthread)->p_pid), NULL, NULL);
28900Sstevel@tonic-gate 	if (remote_lock_possible || flk_has_remote_locks(vp)) {
28910Sstevel@tonic-gate 		ld.l_type = F_UNLCK;	/* set to unlock entire file */
28920Sstevel@tonic-gate 		ld.l_whence = 0;	/* unlock from start of file */
28930Sstevel@tonic-gate 		ld.l_start = 0;
28940Sstevel@tonic-gate 		ld.l_len = 0;		/* do entire file */
28955331Samw 		ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, cr,
28969750SGarima.Tripathi@Sun.COM 		    NULL);
28970Sstevel@tonic-gate 
28980Sstevel@tonic-gate 		if (ret != 0) {
28990Sstevel@tonic-gate 			/*
29000Sstevel@tonic-gate 			 * If VOP_FRLOCK fails, make sure we unregister
29010Sstevel@tonic-gate 			 * local locks before we continue.
29020Sstevel@tonic-gate 			 */
29030Sstevel@tonic-gate 			ld.l_pid = ttoproc(curthread)->p_pid;
29040Sstevel@tonic-gate 			lm_register_lock_locally(vp, NULL, &ld, flag, offset);
29050Sstevel@tonic-gate #ifdef DEBUG
29060Sstevel@tonic-gate 			nfs_perror(ret,
29070Sstevel@tonic-gate 			    "NFS lock release error on vp %p: %m.\n",
29080Sstevel@tonic-gate 			    (void *)vp, NULL);
29090Sstevel@tonic-gate #endif
29100Sstevel@tonic-gate 		}
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate 		/*
29130Sstevel@tonic-gate 		 * The call to VOP_FRLOCK may put the pid back on the
29140Sstevel@tonic-gate 		 * list.  We need to remove it.
29150Sstevel@tonic-gate 		 */
29160Sstevel@tonic-gate 		(void) nfs_remove_locking_id(vp, RLMPL_PID,
29170Sstevel@tonic-gate 		    (char *)&(ttoproc(curthread)->p_pid), NULL, NULL);
29180Sstevel@tonic-gate 	}
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 	/*
29210Sstevel@tonic-gate 	 * As long as the vp has a share matching our pid,
29220Sstevel@tonic-gate 	 * pluck it off and unshare it.  There are circumstances in
29230Sstevel@tonic-gate 	 * which the call to nfs_remove_locking_id() may put the
29240Sstevel@tonic-gate 	 * owner back on the list, in which case we simply do a
29250Sstevel@tonic-gate 	 * redundant and harmless unshare.
29260Sstevel@tonic-gate 	 */
29270Sstevel@tonic-gate 	buf = kmem_alloc(MAX_SHR_OWNER_LEN, KM_SLEEP);
29280Sstevel@tonic-gate 	while (nfs_remove_locking_id(vp, RLMPL_OWNER,
29290Sstevel@tonic-gate 	    (char *)NULL, buf, &shr.s_own_len)) {
29300Sstevel@tonic-gate 		shr.s_owner = buf;
29310Sstevel@tonic-gate 		shr.s_access = 0;
29320Sstevel@tonic-gate 		shr.s_deny = 0;
29330Sstevel@tonic-gate 		shr.s_sysid = 0;
29340Sstevel@tonic-gate 		shr.s_pid = curproc->p_pid;
29350Sstevel@tonic-gate 
29365331Samw 		ret = VOP_SHRLOCK(vp, F_UNSHARE, &shr, flag, cr, NULL);
29370Sstevel@tonic-gate #ifdef DEBUG
29380Sstevel@tonic-gate 		if (ret != 0) {
29390Sstevel@tonic-gate 			nfs_perror(ret,
29400Sstevel@tonic-gate 			    "NFS share release error on vp %p: %m.\n",
29410Sstevel@tonic-gate 			    (void *)vp, NULL);
29420Sstevel@tonic-gate 		}
29430Sstevel@tonic-gate #endif
29440Sstevel@tonic-gate 	}
29450Sstevel@tonic-gate 	kmem_free(buf, MAX_SHR_OWNER_LEN);
29460Sstevel@tonic-gate }
29470Sstevel@tonic-gate 
29480Sstevel@tonic-gate /*
29490Sstevel@tonic-gate  * nfs_lockcompletion:
29500Sstevel@tonic-gate  *
29510Sstevel@tonic-gate  * If the vnode has a lock that makes it unsafe to cache the file, mark it
29520Sstevel@tonic-gate  * as non cachable (set VNOCACHE bit).
29530Sstevel@tonic-gate  */
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate void
nfs_lockcompletion(vnode_t * vp,int cmd)29560Sstevel@tonic-gate nfs_lockcompletion(vnode_t *vp, int cmd)
29570Sstevel@tonic-gate {
29580Sstevel@tonic-gate #ifdef DEBUG
29590Sstevel@tonic-gate 	rnode_t *rp = VTOR(vp);
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 	ASSERT(nfs_rw_lock_held(&rp->r_lkserlock, RW_WRITER));
29620Sstevel@tonic-gate #endif
29630Sstevel@tonic-gate 
29640Sstevel@tonic-gate 	if (cmd == F_SETLK || cmd == F_SETLKW) {
29650Sstevel@tonic-gate 		if (!lm_safemap(vp)) {
29660Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
29670Sstevel@tonic-gate 			vp->v_flag |= VNOCACHE;
29680Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
29690Sstevel@tonic-gate 		} else {
29700Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
29710Sstevel@tonic-gate 			vp->v_flag &= ~VNOCACHE;
29720Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
29730Sstevel@tonic-gate 		}
29740Sstevel@tonic-gate 	}
29750Sstevel@tonic-gate 	/*
29760Sstevel@tonic-gate 	 * The cached attributes of the file are stale after acquiring
29770Sstevel@tonic-gate 	 * the lock on the file. They were updated when the file was
29780Sstevel@tonic-gate 	 * opened, but not updated when the lock was acquired. Therefore the
29790Sstevel@tonic-gate 	 * cached attributes are invalidated after the lock is obtained.
29800Sstevel@tonic-gate 	 */
29810Sstevel@tonic-gate 	PURGE_ATTRCACHE(vp);
29820Sstevel@tonic-gate }
29830Sstevel@tonic-gate 
29840Sstevel@tonic-gate /*
29850Sstevel@tonic-gate  * The lock manager holds state making it possible for the client
29860Sstevel@tonic-gate  * and server to be out of sync.  For example, if the response from
29870Sstevel@tonic-gate  * the server granting a lock request is lost, the server will think
29880Sstevel@tonic-gate  * the lock is granted and the client will think the lock is lost.
29890Sstevel@tonic-gate  * The client can tell when it is not positive if it is in sync with
29900Sstevel@tonic-gate  * the server.
29910Sstevel@tonic-gate  *
29920Sstevel@tonic-gate  * To deal with this, a list of processes for which the client is
29930Sstevel@tonic-gate  * not sure if the server holds a lock is attached to the rnode.
29940Sstevel@tonic-gate  * When such a process closes the rnode, an unlock request is sent
29950Sstevel@tonic-gate  * to the server to unlock the entire file.
29960Sstevel@tonic-gate  *
29970Sstevel@tonic-gate  * The list is kept as a singularly linked NULL terminated list.
29980Sstevel@tonic-gate  * Because it is only added to under extreme error conditions, the
29990Sstevel@tonic-gate  * list shouldn't get very big.  DEBUG kernels print a message if
30000Sstevel@tonic-gate  * the list gets bigger than nfs_lmpl_high_water.  This is arbitrarily
30010Sstevel@tonic-gate  * choosen to be 8, but can be tuned at runtime.
30020Sstevel@tonic-gate  */
30030Sstevel@tonic-gate #ifdef DEBUG
30040Sstevel@tonic-gate /* int nfs_lmpl_high_water = 8; */
30050Sstevel@tonic-gate int nfs_lmpl_high_water = 128;
30060Sstevel@tonic-gate int nfs_cnt_add_locking_id = 0;
30070Sstevel@tonic-gate int nfs_len_add_locking_id = 0;
30080Sstevel@tonic-gate #endif /* DEBUG */
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate /*
30110Sstevel@tonic-gate  * Record that the nfs lock manager server may be holding a lock on
30120Sstevel@tonic-gate  * a vnode for a process.
30130Sstevel@tonic-gate  *
30140Sstevel@tonic-gate  * Because the nfs lock manager server holds state, it is possible
30150Sstevel@tonic-gate  * for the server to get out of sync with the client.  This routine is called
30160Sstevel@tonic-gate  * from the client when it is no longer sure if the server is in sync
30170Sstevel@tonic-gate  * with the client.  nfs_lockrelease() will then notice this and send
30180Sstevel@tonic-gate  * an unlock request when the file is closed
30190Sstevel@tonic-gate  */
30200Sstevel@tonic-gate void
nfs_add_locking_id(vnode_t * vp,pid_t pid,int type,char * id,int len)30210Sstevel@tonic-gate nfs_add_locking_id(vnode_t *vp, pid_t pid, int type, char *id, int len)
30220Sstevel@tonic-gate {
30230Sstevel@tonic-gate 	rnode_t *rp;
30240Sstevel@tonic-gate 	lmpl_t *new;
30250Sstevel@tonic-gate 	lmpl_t *cur;
30260Sstevel@tonic-gate 	lmpl_t **lmplp;
30270Sstevel@tonic-gate #ifdef DEBUG
30280Sstevel@tonic-gate 	int list_len = 1;
30290Sstevel@tonic-gate #endif /* DEBUG */
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate #ifdef DEBUG
30320Sstevel@tonic-gate 	++nfs_cnt_add_locking_id;
30330Sstevel@tonic-gate #endif /* DEBUG */
30340Sstevel@tonic-gate 	/*
30350Sstevel@tonic-gate 	 * allocate new lmpl_t now so we don't sleep
30360Sstevel@tonic-gate 	 * later after grabbing mutexes
30370Sstevel@tonic-gate 	 */
30380Sstevel@tonic-gate 	ASSERT(len < MAX_SHR_OWNER_LEN);
30390Sstevel@tonic-gate 	new = kmem_alloc(sizeof (*new), KM_SLEEP);
30400Sstevel@tonic-gate 	new->lmpl_type = type;
30410Sstevel@tonic-gate 	new->lmpl_pid = pid;
30420Sstevel@tonic-gate 	new->lmpl_owner = kmem_alloc(len, KM_SLEEP);
30430Sstevel@tonic-gate 	bcopy(id, new->lmpl_owner, len);
30440Sstevel@tonic-gate 	new->lmpl_own_len = len;
30450Sstevel@tonic-gate 	new->lmpl_next = (lmpl_t *)NULL;
30460Sstevel@tonic-gate #ifdef DEBUG
30470Sstevel@tonic-gate 	if (type == RLMPL_PID) {
30480Sstevel@tonic-gate 		ASSERT(len == sizeof (pid_t));
30490Sstevel@tonic-gate 		ASSERT(pid == *(pid_t *)new->lmpl_owner);
30500Sstevel@tonic-gate 	} else {
30510Sstevel@tonic-gate 		ASSERT(type == RLMPL_OWNER);
30520Sstevel@tonic-gate 	}
30530Sstevel@tonic-gate #endif
30540Sstevel@tonic-gate 
30550Sstevel@tonic-gate 	rp = VTOR(vp);
30560Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate 	/*
30590Sstevel@tonic-gate 	 * Add this id to the list for this rnode only if the
30600Sstevel@tonic-gate 	 * rnode is active and the id is not already there.
30610Sstevel@tonic-gate 	 */
30620Sstevel@tonic-gate 	ASSERT(rp->r_flags & RHASHED);
30630Sstevel@tonic-gate 	lmplp = &(rp->r_lmpl);
30640Sstevel@tonic-gate 	for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL; cur = cur->lmpl_next) {
30650Sstevel@tonic-gate 		if (cur->lmpl_pid == pid &&
30660Sstevel@tonic-gate 		    cur->lmpl_type == type &&
30670Sstevel@tonic-gate 		    cur->lmpl_own_len == len &&
30680Sstevel@tonic-gate 		    bcmp(cur->lmpl_owner, new->lmpl_owner, len) == 0) {
30690Sstevel@tonic-gate 			kmem_free(new->lmpl_owner, len);
30700Sstevel@tonic-gate 			kmem_free(new, sizeof (*new));
30710Sstevel@tonic-gate 			break;
30720Sstevel@tonic-gate 		}
30730Sstevel@tonic-gate 		lmplp = &cur->lmpl_next;
30740Sstevel@tonic-gate #ifdef DEBUG
30750Sstevel@tonic-gate 		++list_len;
30760Sstevel@tonic-gate #endif /* DEBUG */
30770Sstevel@tonic-gate 	}
30780Sstevel@tonic-gate 	if (cur == (lmpl_t *)NULL) {
30790Sstevel@tonic-gate 		*lmplp = new;
30800Sstevel@tonic-gate #ifdef DEBUG
30810Sstevel@tonic-gate 		if (list_len > nfs_len_add_locking_id) {
30820Sstevel@tonic-gate 			nfs_len_add_locking_id = list_len;
30830Sstevel@tonic-gate 		}
30840Sstevel@tonic-gate 		if (list_len > nfs_lmpl_high_water) {
30850Sstevel@tonic-gate 			cmn_err(CE_WARN, "nfs_add_locking_id: long list "
30860Sstevel@tonic-gate 			    "vp=%p is %d", (void *)vp, list_len);
30870Sstevel@tonic-gate 		}
30880Sstevel@tonic-gate #endif /* DEBUG */
30890Sstevel@tonic-gate 	}
30900Sstevel@tonic-gate 
30910Sstevel@tonic-gate #ifdef DEBUG
30920Sstevel@tonic-gate 	if (share_debug) {
30930Sstevel@tonic-gate 		int nitems = 0;
30940Sstevel@tonic-gate 		int npids = 0;
30950Sstevel@tonic-gate 		int nowners = 0;
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate 		/*
30980Sstevel@tonic-gate 		 * Count the number of things left on r_lmpl after the remove.
30990Sstevel@tonic-gate 		 */
31000Sstevel@tonic-gate 		for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL;
31010Sstevel@tonic-gate 		    cur = cur->lmpl_next) {
31020Sstevel@tonic-gate 			nitems++;
31030Sstevel@tonic-gate 			if (cur->lmpl_type == RLMPL_PID) {
31040Sstevel@tonic-gate 				npids++;
31050Sstevel@tonic-gate 			} else if (cur->lmpl_type == RLMPL_OWNER) {
31060Sstevel@tonic-gate 				nowners++;
31070Sstevel@tonic-gate 			} else {
31080Sstevel@tonic-gate 				cmn_err(CE_PANIC, "nfs_add_locking_id: "
31095331Samw 				    "unrecognized lmpl_type %d",
31100Sstevel@tonic-gate 				    cur->lmpl_type);
31110Sstevel@tonic-gate 			}
31120Sstevel@tonic-gate 		}
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 		cmn_err(CE_CONT, "nfs_add_locking_id(%s): %d PIDs + %d "
31150Sstevel@tonic-gate 		    "OWNs = %d items left on r_lmpl\n",
31160Sstevel@tonic-gate 		    (type == RLMPL_PID) ? "P" : "O", npids, nowners, nitems);
31170Sstevel@tonic-gate 	}
31180Sstevel@tonic-gate #endif
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
31210Sstevel@tonic-gate }
31220Sstevel@tonic-gate 
31230Sstevel@tonic-gate /*
31240Sstevel@tonic-gate  * Remove an id from the lock manager id list.
31250Sstevel@tonic-gate  *
31260Sstevel@tonic-gate  * If the id is not in the list return 0.  If it was found and
31270Sstevel@tonic-gate  * removed, return 1.
31280Sstevel@tonic-gate  */
31290Sstevel@tonic-gate static int
nfs_remove_locking_id(vnode_t * vp,int type,char * id,char * rid,int * rlen)31300Sstevel@tonic-gate nfs_remove_locking_id(vnode_t *vp, int type, char *id, char *rid, int *rlen)
31310Sstevel@tonic-gate {
31320Sstevel@tonic-gate 	lmpl_t *cur;
31330Sstevel@tonic-gate 	lmpl_t **lmplp;
31340Sstevel@tonic-gate 	rnode_t *rp;
31350Sstevel@tonic-gate 	int rv = 0;
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 	ASSERT(type == RLMPL_PID || type == RLMPL_OWNER);
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate 	rp = VTOR(vp);
31400Sstevel@tonic-gate 
31410Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
31420Sstevel@tonic-gate 	ASSERT(rp->r_flags & RHASHED);
31430Sstevel@tonic-gate 	lmplp = &(rp->r_lmpl);
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate 	/*
31460Sstevel@tonic-gate 	 * Search through the list and remove the entry for this id
31470Sstevel@tonic-gate 	 * if it is there.  The special case id == NULL allows removal
31480Sstevel@tonic-gate 	 * of the first share on the r_lmpl list belonging to the
31490Sstevel@tonic-gate 	 * current process (if any), without regard to further details
31500Sstevel@tonic-gate 	 * of its identity.
31510Sstevel@tonic-gate 	 */
31520Sstevel@tonic-gate 	for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL; cur = cur->lmpl_next) {
31530Sstevel@tonic-gate 		if (cur->lmpl_type == type &&
31540Sstevel@tonic-gate 		    cur->lmpl_pid == curproc->p_pid &&
31550Sstevel@tonic-gate 		    (id == (char *)NULL ||
31560Sstevel@tonic-gate 		    bcmp(cur->lmpl_owner, id, cur->lmpl_own_len) == 0)) {
31570Sstevel@tonic-gate 			*lmplp = cur->lmpl_next;
31580Sstevel@tonic-gate 			ASSERT(cur->lmpl_own_len < MAX_SHR_OWNER_LEN);
31590Sstevel@tonic-gate 			if (rid != NULL) {
31600Sstevel@tonic-gate 				bcopy(cur->lmpl_owner, rid, cur->lmpl_own_len);
31610Sstevel@tonic-gate 				*rlen = cur->lmpl_own_len;
31620Sstevel@tonic-gate 			}
31630Sstevel@tonic-gate 			kmem_free(cur->lmpl_owner, cur->lmpl_own_len);
31640Sstevel@tonic-gate 			kmem_free(cur, sizeof (*cur));
31650Sstevel@tonic-gate 			rv = 1;
31660Sstevel@tonic-gate 			break;
31670Sstevel@tonic-gate 		}
31680Sstevel@tonic-gate 		lmplp = &cur->lmpl_next;
31690Sstevel@tonic-gate 	}
31700Sstevel@tonic-gate 
31710Sstevel@tonic-gate #ifdef DEBUG
31720Sstevel@tonic-gate 	if (share_debug) {
31730Sstevel@tonic-gate 		int nitems = 0;
31740Sstevel@tonic-gate 		int npids = 0;
31750Sstevel@tonic-gate 		int nowners = 0;
31760Sstevel@tonic-gate 
31770Sstevel@tonic-gate 		/*
31780Sstevel@tonic-gate 		 * Count the number of things left on r_lmpl after the remove.
31790Sstevel@tonic-gate 		 */
31800Sstevel@tonic-gate 		for (cur = rp->r_lmpl; cur != (lmpl_t *)NULL;
31819750SGarima.Tripathi@Sun.COM 		    cur = cur->lmpl_next) {
31820Sstevel@tonic-gate 			nitems++;
31830Sstevel@tonic-gate 			if (cur->lmpl_type == RLMPL_PID) {
31840Sstevel@tonic-gate 				npids++;
31850Sstevel@tonic-gate 			} else if (cur->lmpl_type == RLMPL_OWNER) {
31860Sstevel@tonic-gate 				nowners++;
31870Sstevel@tonic-gate 			} else {
31880Sstevel@tonic-gate 				cmn_err(CE_PANIC,
31899750SGarima.Tripathi@Sun.COM 				    "nrli: unrecognized lmpl_type %d",
31909750SGarima.Tripathi@Sun.COM 				    cur->lmpl_type);
31910Sstevel@tonic-gate 			}
31920Sstevel@tonic-gate 		}
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 		cmn_err(CE_CONT,
31958879SSuhasini.Peddada@Sun.COM 		"nrli(%s): %d PIDs + %d OWNs = %d items left on r_lmpl\n",
31969750SGarima.Tripathi@Sun.COM 		    (type == RLMPL_PID) ? "P" : "O",
31979750SGarima.Tripathi@Sun.COM 		    npids,
31989750SGarima.Tripathi@Sun.COM 		    nowners,
31999750SGarima.Tripathi@Sun.COM 		    nitems);
32000Sstevel@tonic-gate 	}
32010Sstevel@tonic-gate #endif
32020Sstevel@tonic-gate 
32030Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
32040Sstevel@tonic-gate 	return (rv);
32050Sstevel@tonic-gate }
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate void
nfs_free_mi(mntinfo_t * mi)32080Sstevel@tonic-gate nfs_free_mi(mntinfo_t *mi)
32090Sstevel@tonic-gate {
32100Sstevel@tonic-gate 	ASSERT(mi->mi_flags & MI_ASYNC_MGR_STOP);
32110Sstevel@tonic-gate 	ASSERT(mi->mi_manager_thread == NULL);
321211507SVallish.Vaidyeshwara@Sun.COM 	ASSERT(mi->mi_threads[NFS_ASYNC_QUEUE] == 0 &&
321311507SVallish.Vaidyeshwara@Sun.COM 	    mi->mi_threads[NFS_ASYNC_PGOPS_QUEUE] == 0);
32140Sstevel@tonic-gate 
32150Sstevel@tonic-gate 	/*
32160Sstevel@tonic-gate 	 * Remove the node from the global list before we start tearing it down.
32170Sstevel@tonic-gate 	 */
32180Sstevel@tonic-gate 	nfs_mi_zonelist_remove(mi);
32190Sstevel@tonic-gate 	if (mi->mi_klmconfig) {
32200Sstevel@tonic-gate 		lm_free_config(mi->mi_klmconfig);
32210Sstevel@tonic-gate 		kmem_free(mi->mi_klmconfig, sizeof (struct knetconfig));
32220Sstevel@tonic-gate 	}
32230Sstevel@tonic-gate 	mutex_destroy(&mi->mi_lock);
32241068Svv149972 	mutex_destroy(&mi->mi_remap_lock);
32250Sstevel@tonic-gate 	mutex_destroy(&mi->mi_async_lock);
32260Sstevel@tonic-gate 	cv_destroy(&mi->mi_failover_cv);
322711507SVallish.Vaidyeshwara@Sun.COM 	cv_destroy(&mi->mi_async_work_cv[NFS_ASYNC_QUEUE]);
322811507SVallish.Vaidyeshwara@Sun.COM 	cv_destroy(&mi->mi_async_work_cv[NFS_ASYNC_PGOPS_QUEUE]);
32290Sstevel@tonic-gate 	cv_destroy(&mi->mi_async_reqs_cv);
32300Sstevel@tonic-gate 	cv_destroy(&mi->mi_async_cv);
3231*13096SJordan.Vaughan@Sun.com 	zone_rele_ref(&mi->mi_zone_ref, ZONE_REF_NFS);
32320Sstevel@tonic-gate 	kmem_free(mi, sizeof (*mi));
32330Sstevel@tonic-gate }
32340Sstevel@tonic-gate 
32350Sstevel@tonic-gate static int
mnt_kstat_update(kstat_t * ksp,int rw)32360Sstevel@tonic-gate mnt_kstat_update(kstat_t *ksp, int rw)
32370Sstevel@tonic-gate {
32380Sstevel@tonic-gate 	mntinfo_t *mi;
32390Sstevel@tonic-gate 	struct mntinfo_kstat *mik;
32400Sstevel@tonic-gate 	vfs_t *vfsp;
32410Sstevel@tonic-gate 	int i;
32420Sstevel@tonic-gate 
32430Sstevel@tonic-gate 	/* this is a read-only kstat. Bail out on a write */
32440Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
32450Sstevel@tonic-gate 		return (EACCES);
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate 	/*
32480Sstevel@tonic-gate 	 * We don't want to wait here as kstat_chain_lock could be held by
32490Sstevel@tonic-gate 	 * dounmount(). dounmount() takes vfs_reflock before the chain lock
32500Sstevel@tonic-gate 	 * and thus could lead to a deadlock.
32510Sstevel@tonic-gate 	 */
32520Sstevel@tonic-gate 	vfsp = (struct vfs *)ksp->ks_private;
32530Sstevel@tonic-gate 
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
32560Sstevel@tonic-gate 
32570Sstevel@tonic-gate 	mik = (struct mntinfo_kstat *)ksp->ks_data;
32580Sstevel@tonic-gate 
32590Sstevel@tonic-gate 	(void) strcpy(mik->mik_proto, mi->mi_curr_serv->sv_knconf->knc_proto);
32600Sstevel@tonic-gate 	mik->mik_vers = (uint32_t)mi->mi_vers;
32610Sstevel@tonic-gate 	mik->mik_flags = mi->mi_flags;
32620Sstevel@tonic-gate 	mik->mik_secmod = mi->mi_curr_serv->sv_secdata->secmod;
32630Sstevel@tonic-gate 	mik->mik_curread = (uint32_t)mi->mi_curread;
32640Sstevel@tonic-gate 	mik->mik_curwrite = (uint32_t)mi->mi_curwrite;
32650Sstevel@tonic-gate 	mik->mik_retrans = mi->mi_retrans;
32660Sstevel@tonic-gate 	mik->mik_timeo = mi->mi_timeo;
32670Sstevel@tonic-gate 	mik->mik_acregmin = HR2SEC(mi->mi_acregmin);
32680Sstevel@tonic-gate 	mik->mik_acregmax = HR2SEC(mi->mi_acregmax);
32690Sstevel@tonic-gate 	mik->mik_acdirmin = HR2SEC(mi->mi_acdirmin);
32700Sstevel@tonic-gate 	mik->mik_acdirmax = HR2SEC(mi->mi_acdirmax);
32710Sstevel@tonic-gate 	for (i = 0; i < NFS_CALLTYPES + 1; i++) {
32720Sstevel@tonic-gate 		mik->mik_timers[i].srtt = (uint32_t)mi->mi_timers[i].rt_srtt;
32730Sstevel@tonic-gate 		mik->mik_timers[i].deviate =
32740Sstevel@tonic-gate 		    (uint32_t)mi->mi_timers[i].rt_deviate;
32750Sstevel@tonic-gate 		mik->mik_timers[i].rtxcur =
32760Sstevel@tonic-gate 		    (uint32_t)mi->mi_timers[i].rt_rtxcur;
32770Sstevel@tonic-gate 	}
32780Sstevel@tonic-gate 	mik->mik_noresponse = (uint32_t)mi->mi_noresponse;
32790Sstevel@tonic-gate 	mik->mik_failover = (uint32_t)mi->mi_failover;
32800Sstevel@tonic-gate 	mik->mik_remap = (uint32_t)mi->mi_remap;
32810Sstevel@tonic-gate 	(void) strcpy(mik->mik_curserver, mi->mi_curr_serv->sv_hostname);
32820Sstevel@tonic-gate 
32830Sstevel@tonic-gate 	return (0);
32840Sstevel@tonic-gate }
32850Sstevel@tonic-gate 
32860Sstevel@tonic-gate void
nfs_mnt_kstat_init(struct vfs * vfsp)32870Sstevel@tonic-gate nfs_mnt_kstat_init(struct vfs *vfsp)
32880Sstevel@tonic-gate {
32890Sstevel@tonic-gate 	mntinfo_t *mi = VFTOMI(vfsp);
32900Sstevel@tonic-gate 
32910Sstevel@tonic-gate 	/*
32920Sstevel@tonic-gate 	 * Create the version specific kstats.
32930Sstevel@tonic-gate 	 *
32940Sstevel@tonic-gate 	 * PSARC 2001/697 Contract Private Interface
32950Sstevel@tonic-gate 	 * All nfs kstats are under SunMC contract
32960Sstevel@tonic-gate 	 * Please refer to the PSARC listed above and contact
32970Sstevel@tonic-gate 	 * SunMC before making any changes!
32980Sstevel@tonic-gate 	 *
32990Sstevel@tonic-gate 	 * Changes must be reviewed by Solaris File Sharing
33000Sstevel@tonic-gate 	 * Changes must be communicated to contract-2001-697@sun.com
33010Sstevel@tonic-gate 	 *
33020Sstevel@tonic-gate 	 */
33030Sstevel@tonic-gate 
33040Sstevel@tonic-gate 	mi->mi_io_kstats = kstat_create_zone("nfs", getminor(vfsp->vfs_dev),
33050Sstevel@tonic-gate 	    NULL, "nfs", KSTAT_TYPE_IO, 1, 0, mi->mi_zone->zone_id);
33060Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
33070Sstevel@tonic-gate 		if (mi->mi_zone->zone_id != GLOBAL_ZONEID)
33080Sstevel@tonic-gate 			kstat_zone_add(mi->mi_io_kstats, GLOBAL_ZONEID);
33090Sstevel@tonic-gate 		mi->mi_io_kstats->ks_lock = &mi->mi_lock;
33100Sstevel@tonic-gate 		kstat_install(mi->mi_io_kstats);
33110Sstevel@tonic-gate 	}
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 	if ((mi->mi_ro_kstats = kstat_create_zone("nfs",
33140Sstevel@tonic-gate 	    getminor(vfsp->vfs_dev), "mntinfo", "misc", KSTAT_TYPE_RAW,
33150Sstevel@tonic-gate 	    sizeof (struct mntinfo_kstat), 0, mi->mi_zone->zone_id)) != NULL) {
33160Sstevel@tonic-gate 		if (mi->mi_zone->zone_id != GLOBAL_ZONEID)
33170Sstevel@tonic-gate 			kstat_zone_add(mi->mi_ro_kstats, GLOBAL_ZONEID);
33180Sstevel@tonic-gate 		mi->mi_ro_kstats->ks_update = mnt_kstat_update;
33190Sstevel@tonic-gate 		mi->mi_ro_kstats->ks_private = (void *)vfsp;
33200Sstevel@tonic-gate 		kstat_install(mi->mi_ro_kstats);
33210Sstevel@tonic-gate 	}
33220Sstevel@tonic-gate }
33230Sstevel@tonic-gate 
33240Sstevel@tonic-gate nfs_delmapcall_t *
nfs_init_delmapcall()33250Sstevel@tonic-gate nfs_init_delmapcall()
33260Sstevel@tonic-gate {
33270Sstevel@tonic-gate 	nfs_delmapcall_t	*delmap_call;
33280Sstevel@tonic-gate 
33290Sstevel@tonic-gate 	delmap_call = kmem_alloc(sizeof (nfs_delmapcall_t), KM_SLEEP);
33300Sstevel@tonic-gate 	delmap_call->call_id = curthread;
33310Sstevel@tonic-gate 	delmap_call->error = 0;
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 	return (delmap_call);
33340Sstevel@tonic-gate }
33350Sstevel@tonic-gate 
33360Sstevel@tonic-gate void
nfs_free_delmapcall(nfs_delmapcall_t * delmap_call)33370Sstevel@tonic-gate nfs_free_delmapcall(nfs_delmapcall_t *delmap_call)
33380Sstevel@tonic-gate {
33390Sstevel@tonic-gate 	kmem_free(delmap_call, sizeof (nfs_delmapcall_t));
33400Sstevel@tonic-gate }
33410Sstevel@tonic-gate 
33420Sstevel@tonic-gate /*
33430Sstevel@tonic-gate  * Searches for the current delmap caller (based on curthread) in the list of
33440Sstevel@tonic-gate  * callers.  If it is found, we remove it and free the delmap caller.
33450Sstevel@tonic-gate  * Returns:
33460Sstevel@tonic-gate  *	0 if the caller wasn't found
33470Sstevel@tonic-gate  *	1 if the caller was found, removed and freed.  *errp is set to what
33480Sstevel@tonic-gate  * 	the result of the delmap was.
33490Sstevel@tonic-gate  */
33500Sstevel@tonic-gate int
nfs_find_and_delete_delmapcall(rnode_t * rp,int * errp)33510Sstevel@tonic-gate nfs_find_and_delete_delmapcall(rnode_t *rp, int *errp)
33520Sstevel@tonic-gate {
33530Sstevel@tonic-gate 	nfs_delmapcall_t	*delmap_call;
33540Sstevel@tonic-gate 
33550Sstevel@tonic-gate 	/*
33560Sstevel@tonic-gate 	 * If the list doesn't exist yet, we create it and return
33570Sstevel@tonic-gate 	 * that the caller wasn't found.  No list = no callers.
33580Sstevel@tonic-gate 	 */
33590Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
33600Sstevel@tonic-gate 	if (!(rp->r_flags & RDELMAPLIST)) {
33610Sstevel@tonic-gate 		/* The list does not exist */
33620Sstevel@tonic-gate 		list_create(&rp->r_indelmap, sizeof (nfs_delmapcall_t),
33630Sstevel@tonic-gate 		    offsetof(nfs_delmapcall_t, call_node));
33640Sstevel@tonic-gate 		rp->r_flags |= RDELMAPLIST;
33650Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
33660Sstevel@tonic-gate 		return (0);
33670Sstevel@tonic-gate 	} else {
33680Sstevel@tonic-gate 		/* The list exists so search it */
33690Sstevel@tonic-gate 		for (delmap_call = list_head(&rp->r_indelmap);
33700Sstevel@tonic-gate 		    delmap_call != NULL;
33710Sstevel@tonic-gate 		    delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
33720Sstevel@tonic-gate 			if (delmap_call->call_id == curthread) {
33730Sstevel@tonic-gate 				/* current caller is in the list */
33740Sstevel@tonic-gate 				*errp = delmap_call->error;
33750Sstevel@tonic-gate 				list_remove(&rp->r_indelmap, delmap_call);
33760Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
33770Sstevel@tonic-gate 				nfs_free_delmapcall(delmap_call);
33780Sstevel@tonic-gate 				return (1);
33790Sstevel@tonic-gate 			}
33800Sstevel@tonic-gate 		}
33810Sstevel@tonic-gate 	}
33820Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
33830Sstevel@tonic-gate 	return (0);
33840Sstevel@tonic-gate }
3385