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