xref: /titanic_53/usr/src/uts/common/fs/vnode.c (revision 1b300de97c3a03432cd287d7e82a800782dcd236)
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
55a59a8b3Srsb  * Common Development and Distribution License (the "License").
65a59a8b3Srsb  * 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 /*
2248011479Ssn199410  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
457c478bd9Sstevel@tonic-gate #include <sys/errno.h>
467c478bd9Sstevel@tonic-gate #include <sys/cred.h>
477c478bd9Sstevel@tonic-gate #include <sys/user.h>
487c478bd9Sstevel@tonic-gate #include <sys/uio.h>
497c478bd9Sstevel@tonic-gate #include <sys/file.h>
507c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
517c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
52aa59c4cbSrsb #include <sys/vfs_opreg.h>
537c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
547c478bd9Sstevel@tonic-gate #include <sys/rwstlock.h>
557c478bd9Sstevel@tonic-gate #include <sys/fem.h>
567c478bd9Sstevel@tonic-gate #include <sys/stat.h>
577c478bd9Sstevel@tonic-gate #include <sys/mode.h>
587c478bd9Sstevel@tonic-gate #include <sys/conf.h>
597c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
607c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
617c478bd9Sstevel@tonic-gate #include <sys/systm.h>
627c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
637c478bd9Sstevel@tonic-gate #include <sys/debug.h>
647c478bd9Sstevel@tonic-gate #include <c2/audit.h>
657c478bd9Sstevel@tonic-gate #include <sys/acl.h>
667c478bd9Sstevel@tonic-gate #include <sys/nbmlock.h>
677c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
687c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /* Determine if this vnode is a file that is read-only */
717c478bd9Sstevel@tonic-gate #define	ISROFILE(vp)	\
727c478bd9Sstevel@tonic-gate 	((vp)->v_type != VCHR && (vp)->v_type != VBLK && \
737c478bd9Sstevel@tonic-gate 	    (vp)->v_type != VFIFO && vn_is_readonly(vp))
747c478bd9Sstevel@tonic-gate 
75108322fbScarlsonj /* Tunable via /etc/system; used only by admin/install */
76108322fbScarlsonj int nfs_global_client_only;
77108322fbScarlsonj 
787c478bd9Sstevel@tonic-gate /*
795a59a8b3Srsb  * Array of vopstats_t for per-FS-type vopstats.  This array has the same
805a59a8b3Srsb  * number of entries as and parallel to the vfssw table.  (Arguably, it could
815a59a8b3Srsb  * be part of the vfssw table.)  Once it's initialized, it's accessed using
825a59a8b3Srsb  * the same fstype index that is used to index into the vfssw table.
835a59a8b3Srsb  */
845a59a8b3Srsb vopstats_t **vopstats_fstype;
855a59a8b3Srsb 
865a59a8b3Srsb /* vopstats initialization template used for fast initialization via bcopy() */
875a59a8b3Srsb static vopstats_t *vs_templatep;
885a59a8b3Srsb 
895a59a8b3Srsb /* Kmem cache handle for vsk_anchor_t allocations */
905a59a8b3Srsb kmem_cache_t *vsk_anchor_cache;
915a59a8b3Srsb 
92df2381bfSpraks /* file events cleanup routine */
93df2381bfSpraks extern void free_fopdata(vnode_t *);
94df2381bfSpraks 
955a59a8b3Srsb /*
965a59a8b3Srsb  * Root of AVL tree for the kstats associated with vopstats.  Lock protects
975a59a8b3Srsb  * updates to vsktat_tree.
985a59a8b3Srsb  */
995a59a8b3Srsb avl_tree_t	vskstat_tree;
1005a59a8b3Srsb kmutex_t	vskstat_tree_lock;
1015a59a8b3Srsb 
1025a59a8b3Srsb /* Global variable which enables/disables the vopstats collection */
1035a59a8b3Srsb int vopstats_enabled = 1;
1045a59a8b3Srsb 
1055a59a8b3Srsb /*
106*1b300de9Sjwahlig  * forward declarations for internal vnode specific data (vsd)
107*1b300de9Sjwahlig  */
108*1b300de9Sjwahlig static void *vsd_realloc(void *, size_t, size_t);
109*1b300de9Sjwahlig 
110*1b300de9Sjwahlig /*
111*1b300de9Sjwahlig  * VSD -- VNODE SPECIFIC DATA
112*1b300de9Sjwahlig  * The v_data pointer is typically used by a file system to store a
113*1b300de9Sjwahlig  * pointer to the file system's private node (e.g. ufs inode, nfs rnode).
114*1b300de9Sjwahlig  * However, there are times when additional project private data needs
115*1b300de9Sjwahlig  * to be stored separately from the data (node) pointed to by v_data.
116*1b300de9Sjwahlig  * This additional data could be stored by the file system itself or
117*1b300de9Sjwahlig  * by a completely different kernel entity.  VSD provides a way for
118*1b300de9Sjwahlig  * callers to obtain a key and store a pointer to private data associated
119*1b300de9Sjwahlig  * with a vnode.
120*1b300de9Sjwahlig  *
121*1b300de9Sjwahlig  * Callers are responsible for protecting the vsd by holding v_lock
122*1b300de9Sjwahlig  * for calls to vsd_set() and vsd_get().
123*1b300de9Sjwahlig  */
124*1b300de9Sjwahlig 
125*1b300de9Sjwahlig /*
126*1b300de9Sjwahlig  * vsd_lock protects:
127*1b300de9Sjwahlig  *   vsd_nkeys - creation and deletion of vsd keys
128*1b300de9Sjwahlig  *   vsd_list - insertion and deletion of vsd_node in the vsd_list
129*1b300de9Sjwahlig  *   vsd_destructor - adding and removing destructors to the list
130*1b300de9Sjwahlig  */
131*1b300de9Sjwahlig static kmutex_t		vsd_lock;
132*1b300de9Sjwahlig static uint_t		vsd_nkeys;	 /* size of destructor array */
133*1b300de9Sjwahlig /* list of vsd_node's */
134*1b300de9Sjwahlig static list_t *vsd_list = NULL;
135*1b300de9Sjwahlig /* per-key destructor funcs */
136*1b300de9Sjwahlig static void 		(**vsd_destructor)(void *);
137*1b300de9Sjwahlig 
138*1b300de9Sjwahlig /*
1395a59a8b3Srsb  * The following is the common set of actions needed to update the
1405a59a8b3Srsb  * vopstats structure from a vnode op.  Both VOPSTATS_UPDATE() and
1415a59a8b3Srsb  * VOPSTATS_UPDATE_IO() do almost the same thing, except for the
1425a59a8b3Srsb  * recording of the bytes transferred.  Since the code is similar
1435a59a8b3Srsb  * but small, it is nearly a duplicate.  Consequently any changes
1445a59a8b3Srsb  * to one may need to be reflected in the other.
1455a59a8b3Srsb  * Rundown of the variables:
1465a59a8b3Srsb  * vp - Pointer to the vnode
1475a59a8b3Srsb  * counter - Partial name structure member to update in vopstats for counts
1485a59a8b3Srsb  * bytecounter - Partial name structure member to update in vopstats for bytes
1495a59a8b3Srsb  * bytesval - Value to update in vopstats for bytes
1505a59a8b3Srsb  * fstype - Index into vsanchor_fstype[], same as index into vfssw[]
1515a59a8b3Srsb  * vsp - Pointer to vopstats structure (either in vfs or vsanchor_fstype[i])
1525a59a8b3Srsb  */
1535a59a8b3Srsb 
1545a59a8b3Srsb #define	VOPSTATS_UPDATE(vp, counter) {					\
1555a59a8b3Srsb 	vfs_t *vfsp = (vp)->v_vfsp;					\
156ddfcde86Srsb 	if (vfsp && vfsp->vfs_implp &&					\
157ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {	\
1585a59a8b3Srsb 		vopstats_t *vsp = &vfsp->vfs_vopstats;			\
1592bb1cb30Sbmc 		uint64_t *stataddr = &(vsp->n##counter.value.ui64);	\
1602bb1cb30Sbmc 		extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
1612bb1cb30Sbmc 		    size_t, uint64_t *);				\
1622bb1cb30Sbmc 		__dtrace_probe___fsinfo_##counter(vp, 0, stataddr);	\
1632bb1cb30Sbmc 		(*stataddr)++;						\
1645a59a8b3Srsb 		if ((vsp = vfsp->vfs_fstypevsp) != NULL) {		\
1652bb1cb30Sbmc 			vsp->n##counter.value.ui64++;			\
1665a59a8b3Srsb 		}							\
1675a59a8b3Srsb 	}								\
1685a59a8b3Srsb }
1695a59a8b3Srsb 
1705a59a8b3Srsb #define	VOPSTATS_UPDATE_IO(vp, counter, bytecounter, bytesval) {	\
1715a59a8b3Srsb 	vfs_t *vfsp = (vp)->v_vfsp;					\
172ddfcde86Srsb 	if (vfsp && vfsp->vfs_implp &&					\
173ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {	\
1745a59a8b3Srsb 		vopstats_t *vsp = &vfsp->vfs_vopstats;			\
1752bb1cb30Sbmc 		uint64_t *stataddr = &(vsp->n##counter.value.ui64);	\
1762bb1cb30Sbmc 		extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
1772bb1cb30Sbmc 		    size_t, uint64_t *);				\
1782bb1cb30Sbmc 		__dtrace_probe___fsinfo_##counter(vp, bytesval, stataddr); \
1792bb1cb30Sbmc 		(*stataddr)++;						\
1805a59a8b3Srsb 		vsp->bytecounter.value.ui64 += bytesval;		\
1815a59a8b3Srsb 		if ((vsp = vfsp->vfs_fstypevsp) != NULL) {		\
1822bb1cb30Sbmc 			vsp->n##counter.value.ui64++;			\
1835a59a8b3Srsb 			vsp->bytecounter.value.ui64 += bytesval;	\
1845a59a8b3Srsb 		}							\
1855a59a8b3Srsb 	}								\
1865a59a8b3Srsb }
1875a59a8b3Srsb 
1885a59a8b3Srsb /*
189f48205beScasper  * If the filesystem does not support XIDs map credential
190f48205beScasper  * If the vfsp is NULL, perhaps we should also map?
191f48205beScasper  */
192f48205beScasper #define	VOPXID_MAP_CR(vp, cr)	{					\
193f48205beScasper 	vfs_t *vfsp = (vp)->v_vfsp;					\
194f48205beScasper 	if (vfsp != NULL && (vfsp->vfs_flag & VFS_XID) == 0)		\
195f48205beScasper 		cr = crgetmapped(cr);					\
196f48205beScasper 	}
197f48205beScasper 
198f48205beScasper /*
1997c478bd9Sstevel@tonic-gate  * Convert stat(2) formats to vnode types and vice versa.  (Knows about
2007c478bd9Sstevel@tonic-gate  * numerical order of S_IFMT and vnode types.)
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate enum vtype iftovt_tab[] = {
2037c478bd9Sstevel@tonic-gate 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
2047c478bd9Sstevel@tonic-gate 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
2057c478bd9Sstevel@tonic-gate };
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate ushort_t vttoif_tab[] = {
2087c478bd9Sstevel@tonic-gate 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO,
2097c478bd9Sstevel@tonic-gate 	S_IFDOOR, 0, S_IFSOCK, S_IFPORT, 0
2107c478bd9Sstevel@tonic-gate };
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * The system vnode cache.
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate kmem_cache_t *vn_cache;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * Vnode operations vector.
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static const fs_operation_trans_def_t vn_ops_table[] = {
2247c478bd9Sstevel@tonic-gate 	VOPNAME_OPEN, offsetof(struct vnodeops, vop_open),
2257c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	VOPNAME_CLOSE, offsetof(struct vnodeops, vop_close),
2287c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	VOPNAME_READ, offsetof(struct vnodeops, vop_read),
2317c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	VOPNAME_WRITE, offsetof(struct vnodeops, vop_write),
2347c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	VOPNAME_IOCTL, offsetof(struct vnodeops, vop_ioctl),
2377c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	VOPNAME_SETFL, offsetof(struct vnodeops, vop_setfl),
2407c478bd9Sstevel@tonic-gate 	    fs_setfl, fs_nosys,
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	VOPNAME_GETATTR, offsetof(struct vnodeops, vop_getattr),
2437c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	VOPNAME_SETATTR, offsetof(struct vnodeops, vop_setattr),
2467c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	VOPNAME_ACCESS, offsetof(struct vnodeops, vop_access),
2497c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	VOPNAME_LOOKUP, offsetof(struct vnodeops, vop_lookup),
2527c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	VOPNAME_CREATE, offsetof(struct vnodeops, vop_create),
2557c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	VOPNAME_REMOVE, offsetof(struct vnodeops, vop_remove),
2587c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	VOPNAME_LINK, offsetof(struct vnodeops, vop_link),
2617c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	VOPNAME_RENAME, offsetof(struct vnodeops, vop_rename),
2647c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	VOPNAME_MKDIR, offsetof(struct vnodeops, vop_mkdir),
2677c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	VOPNAME_RMDIR, offsetof(struct vnodeops, vop_rmdir),
2707c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	VOPNAME_READDIR, offsetof(struct vnodeops, vop_readdir),
2737c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	VOPNAME_SYMLINK, offsetof(struct vnodeops, vop_symlink),
2767c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	VOPNAME_READLINK, offsetof(struct vnodeops, vop_readlink),
2797c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	VOPNAME_FSYNC, offsetof(struct vnodeops, vop_fsync),
2827c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	VOPNAME_INACTIVE, offsetof(struct vnodeops, vop_inactive),
2857c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	VOPNAME_FID, offsetof(struct vnodeops, vop_fid),
2887c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	VOPNAME_RWLOCK, offsetof(struct vnodeops, vop_rwlock),
2917c478bd9Sstevel@tonic-gate 	    fs_rwlock, fs_rwlock,
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	VOPNAME_RWUNLOCK, offsetof(struct vnodeops, vop_rwunlock),
2947c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_rwunlock,
2957c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_rwunlock,	/* no errors allowed */
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	VOPNAME_SEEK, offsetof(struct vnodeops, vop_seek),
2987c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	VOPNAME_CMP, offsetof(struct vnodeops, vop_cmp),
3017c478bd9Sstevel@tonic-gate 	    fs_cmp, fs_cmp,		/* no errors allowed */
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	VOPNAME_FRLOCK, offsetof(struct vnodeops, vop_frlock),
3047c478bd9Sstevel@tonic-gate 	    fs_frlock, fs_nosys,
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	VOPNAME_SPACE, offsetof(struct vnodeops, vop_space),
3077c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	VOPNAME_REALVP, offsetof(struct vnodeops, vop_realvp),
3107c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	VOPNAME_GETPAGE, offsetof(struct vnodeops, vop_getpage),
3137c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	VOPNAME_PUTPAGE, offsetof(struct vnodeops, vop_putpage),
3167c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	VOPNAME_MAP, offsetof(struct vnodeops, vop_map),
3197c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_map,
3207c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_map,
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	VOPNAME_ADDMAP, offsetof(struct vnodeops, vop_addmap),
3237c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_addmap,
3247c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_addmap,
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	VOPNAME_DELMAP, offsetof(struct vnodeops, vop_delmap),
3277c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	VOPNAME_POLL, offsetof(struct vnodeops, vop_poll),
3307c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_poll, (fs_generic_func_p) fs_nosys_poll,
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	VOPNAME_DUMP, offsetof(struct vnodeops, vop_dump),
3337c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	VOPNAME_PATHCONF, offsetof(struct vnodeops, vop_pathconf),
3367c478bd9Sstevel@tonic-gate 	    fs_pathconf, fs_nosys,
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	VOPNAME_PAGEIO, offsetof(struct vnodeops, vop_pageio),
3397c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	VOPNAME_DUMPCTL, offsetof(struct vnodeops, vop_dumpctl),
3427c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	VOPNAME_DISPOSE, offsetof(struct vnodeops, vop_dispose),
3457c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_dispose,
3467c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nodispose,
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	VOPNAME_SETSECATTR, offsetof(struct vnodeops, vop_setsecattr),
3497c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	VOPNAME_GETSECATTR, offsetof(struct vnodeops, vop_getsecattr),
3527c478bd9Sstevel@tonic-gate 	    fs_fab_acl, fs_nosys,
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	VOPNAME_SHRLOCK, offsetof(struct vnodeops, vop_shrlock),
3557c478bd9Sstevel@tonic-gate 	    fs_shrlock, fs_nosys,
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	VOPNAME_VNEVENT, offsetof(struct vnodeops, vop_vnevent),
3587c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_vnevent_nosupport,
3597c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_vnevent_nosupport,
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	NULL, 0, NULL, NULL
3627c478bd9Sstevel@tonic-gate };
3637c478bd9Sstevel@tonic-gate 
3645a59a8b3Srsb /*
3655a59a8b3Srsb  * Used by the AVL routines to compare two vsk_anchor_t structures in the tree.
3665a59a8b3Srsb  * We use the f_fsid reported by VFS_STATVFS() since we use that for the
3675a59a8b3Srsb  * kstat name.
3685a59a8b3Srsb  */
3695a59a8b3Srsb static int
3705a59a8b3Srsb vska_compar(const void *n1, const void *n2)
3715a59a8b3Srsb {
3725a59a8b3Srsb 	int ret;
3735a59a8b3Srsb 	ulong_t p1 = ((vsk_anchor_t *)n1)->vsk_fsid;
3745a59a8b3Srsb 	ulong_t p2 = ((vsk_anchor_t *)n2)->vsk_fsid;
3755a59a8b3Srsb 
3765a59a8b3Srsb 	if (p1 < p2) {
3775a59a8b3Srsb 		ret = -1;
3785a59a8b3Srsb 	} else if (p1 > p2) {
3795a59a8b3Srsb 		ret = 1;
3805a59a8b3Srsb 	} else {
3815a59a8b3Srsb 		ret = 0;
3825a59a8b3Srsb 	}
3835a59a8b3Srsb 
3845a59a8b3Srsb 	return (ret);
3855a59a8b3Srsb }
3865a59a8b3Srsb 
3875a59a8b3Srsb /*
3885a59a8b3Srsb  * Used to create a single template which will be bcopy()ed to a newly
3895a59a8b3Srsb  * allocated vsanchor_combo_t structure in new_vsanchor(), below.
3905a59a8b3Srsb  */
3915a59a8b3Srsb static vopstats_t *
3925a59a8b3Srsb create_vopstats_template()
3935a59a8b3Srsb {
3945a59a8b3Srsb 	vopstats_t		*vsp;
3955a59a8b3Srsb 
3965a59a8b3Srsb 	vsp = kmem_alloc(sizeof (vopstats_t), KM_SLEEP);
3975a59a8b3Srsb 	bzero(vsp, sizeof (*vsp));	/* Start fresh */
3985a59a8b3Srsb 
3995a59a8b3Srsb 	/* VOP_OPEN */
4005a59a8b3Srsb 	kstat_named_init(&vsp->nopen, "nopen", KSTAT_DATA_UINT64);
4015a59a8b3Srsb 	/* VOP_CLOSE */
4025a59a8b3Srsb 	kstat_named_init(&vsp->nclose, "nclose", KSTAT_DATA_UINT64);
4035a59a8b3Srsb 	/* VOP_READ I/O */
4045a59a8b3Srsb 	kstat_named_init(&vsp->nread, "nread", KSTAT_DATA_UINT64);
4055a59a8b3Srsb 	kstat_named_init(&vsp->read_bytes, "read_bytes", KSTAT_DATA_UINT64);
4065a59a8b3Srsb 	/* VOP_WRITE I/O */
4075a59a8b3Srsb 	kstat_named_init(&vsp->nwrite, "nwrite", KSTAT_DATA_UINT64);
4085a59a8b3Srsb 	kstat_named_init(&vsp->write_bytes, "write_bytes", KSTAT_DATA_UINT64);
4095a59a8b3Srsb 	/* VOP_IOCTL */
4105a59a8b3Srsb 	kstat_named_init(&vsp->nioctl, "nioctl", KSTAT_DATA_UINT64);
4115a59a8b3Srsb 	/* VOP_SETFL */
4125a59a8b3Srsb 	kstat_named_init(&vsp->nsetfl, "nsetfl", KSTAT_DATA_UINT64);
4135a59a8b3Srsb 	/* VOP_GETATTR */
4145a59a8b3Srsb 	kstat_named_init(&vsp->ngetattr, "ngetattr", KSTAT_DATA_UINT64);
4155a59a8b3Srsb 	/* VOP_SETATTR */
4165a59a8b3Srsb 	kstat_named_init(&vsp->nsetattr, "nsetattr", KSTAT_DATA_UINT64);
4175a59a8b3Srsb 	/* VOP_ACCESS */
4185a59a8b3Srsb 	kstat_named_init(&vsp->naccess, "naccess", KSTAT_DATA_UINT64);
4195a59a8b3Srsb 	/* VOP_LOOKUP */
4205a59a8b3Srsb 	kstat_named_init(&vsp->nlookup, "nlookup", KSTAT_DATA_UINT64);
4215a59a8b3Srsb 	/* VOP_CREATE */
4225a59a8b3Srsb 	kstat_named_init(&vsp->ncreate, "ncreate", KSTAT_DATA_UINT64);
4235a59a8b3Srsb 	/* VOP_REMOVE */
4245a59a8b3Srsb 	kstat_named_init(&vsp->nremove, "nremove", KSTAT_DATA_UINT64);
4255a59a8b3Srsb 	/* VOP_LINK */
4265a59a8b3Srsb 	kstat_named_init(&vsp->nlink, "nlink", KSTAT_DATA_UINT64);
4275a59a8b3Srsb 	/* VOP_RENAME */
4285a59a8b3Srsb 	kstat_named_init(&vsp->nrename, "nrename", KSTAT_DATA_UINT64);
4295a59a8b3Srsb 	/* VOP_MKDIR */
4305a59a8b3Srsb 	kstat_named_init(&vsp->nmkdir, "nmkdir", KSTAT_DATA_UINT64);
4315a59a8b3Srsb 	/* VOP_RMDIR */
4325a59a8b3Srsb 	kstat_named_init(&vsp->nrmdir, "nrmdir", KSTAT_DATA_UINT64);
4335a59a8b3Srsb 	/* VOP_READDIR I/O */
4345a59a8b3Srsb 	kstat_named_init(&vsp->nreaddir, "nreaddir", KSTAT_DATA_UINT64);
4355a59a8b3Srsb 	kstat_named_init(&vsp->readdir_bytes, "readdir_bytes",
4365a59a8b3Srsb 	    KSTAT_DATA_UINT64);
4375a59a8b3Srsb 	/* VOP_SYMLINK */
4385a59a8b3Srsb 	kstat_named_init(&vsp->nsymlink, "nsymlink", KSTAT_DATA_UINT64);
4395a59a8b3Srsb 	/* VOP_READLINK */
4405a59a8b3Srsb 	kstat_named_init(&vsp->nreadlink, "nreadlink", KSTAT_DATA_UINT64);
4415a59a8b3Srsb 	/* VOP_FSYNC */
4425a59a8b3Srsb 	kstat_named_init(&vsp->nfsync, "nfsync", KSTAT_DATA_UINT64);
4435a59a8b3Srsb 	/* VOP_INACTIVE */
4445a59a8b3Srsb 	kstat_named_init(&vsp->ninactive, "ninactive", KSTAT_DATA_UINT64);
4455a59a8b3Srsb 	/* VOP_FID */
4465a59a8b3Srsb 	kstat_named_init(&vsp->nfid, "nfid", KSTAT_DATA_UINT64);
4475a59a8b3Srsb 	/* VOP_RWLOCK */
4485a59a8b3Srsb 	kstat_named_init(&vsp->nrwlock, "nrwlock", KSTAT_DATA_UINT64);
4495a59a8b3Srsb 	/* VOP_RWUNLOCK */
4505a59a8b3Srsb 	kstat_named_init(&vsp->nrwunlock, "nrwunlock", KSTAT_DATA_UINT64);
4515a59a8b3Srsb 	/* VOP_SEEK */
4525a59a8b3Srsb 	kstat_named_init(&vsp->nseek, "nseek", KSTAT_DATA_UINT64);
4535a59a8b3Srsb 	/* VOP_CMP */
4545a59a8b3Srsb 	kstat_named_init(&vsp->ncmp, "ncmp", KSTAT_DATA_UINT64);
4555a59a8b3Srsb 	/* VOP_FRLOCK */
4565a59a8b3Srsb 	kstat_named_init(&vsp->nfrlock, "nfrlock", KSTAT_DATA_UINT64);
4575a59a8b3Srsb 	/* VOP_SPACE */
4585a59a8b3Srsb 	kstat_named_init(&vsp->nspace, "nspace", KSTAT_DATA_UINT64);
4595a59a8b3Srsb 	/* VOP_REALVP */
4605a59a8b3Srsb 	kstat_named_init(&vsp->nrealvp, "nrealvp", KSTAT_DATA_UINT64);
4615a59a8b3Srsb 	/* VOP_GETPAGE */
4625a59a8b3Srsb 	kstat_named_init(&vsp->ngetpage, "ngetpage", KSTAT_DATA_UINT64);
4635a59a8b3Srsb 	/* VOP_PUTPAGE */
4645a59a8b3Srsb 	kstat_named_init(&vsp->nputpage, "nputpage", KSTAT_DATA_UINT64);
4655a59a8b3Srsb 	/* VOP_MAP */
4665a59a8b3Srsb 	kstat_named_init(&vsp->nmap, "nmap", KSTAT_DATA_UINT64);
4675a59a8b3Srsb 	/* VOP_ADDMAP */
4685a59a8b3Srsb 	kstat_named_init(&vsp->naddmap, "naddmap", KSTAT_DATA_UINT64);
4695a59a8b3Srsb 	/* VOP_DELMAP */
4705a59a8b3Srsb 	kstat_named_init(&vsp->ndelmap, "ndelmap", KSTAT_DATA_UINT64);
4715a59a8b3Srsb 	/* VOP_POLL */
4725a59a8b3Srsb 	kstat_named_init(&vsp->npoll, "npoll", KSTAT_DATA_UINT64);
4735a59a8b3Srsb 	/* VOP_DUMP */
4745a59a8b3Srsb 	kstat_named_init(&vsp->ndump, "ndump", KSTAT_DATA_UINT64);
4755a59a8b3Srsb 	/* VOP_PATHCONF */
4765a59a8b3Srsb 	kstat_named_init(&vsp->npathconf, "npathconf", KSTAT_DATA_UINT64);
4775a59a8b3Srsb 	/* VOP_PAGEIO */
4785a59a8b3Srsb 	kstat_named_init(&vsp->npageio, "npageio", KSTAT_DATA_UINT64);
4795a59a8b3Srsb 	/* VOP_DUMPCTL */
4805a59a8b3Srsb 	kstat_named_init(&vsp->ndumpctl, "ndumpctl", KSTAT_DATA_UINT64);
4815a59a8b3Srsb 	/* VOP_DISPOSE */
4825a59a8b3Srsb 	kstat_named_init(&vsp->ndispose, "ndispose", KSTAT_DATA_UINT64);
4835a59a8b3Srsb 	/* VOP_SETSECATTR */
4845a59a8b3Srsb 	kstat_named_init(&vsp->nsetsecattr, "nsetsecattr", KSTAT_DATA_UINT64);
4855a59a8b3Srsb 	/* VOP_GETSECATTR */
4865a59a8b3Srsb 	kstat_named_init(&vsp->ngetsecattr, "ngetsecattr", KSTAT_DATA_UINT64);
4875a59a8b3Srsb 	/* VOP_SHRLOCK */
4885a59a8b3Srsb 	kstat_named_init(&vsp->nshrlock, "nshrlock", KSTAT_DATA_UINT64);
4895a59a8b3Srsb 	/* VOP_VNEVENT */
4905a59a8b3Srsb 	kstat_named_init(&vsp->nvnevent, "nvnevent", KSTAT_DATA_UINT64);
4915a59a8b3Srsb 
4925a59a8b3Srsb 	return (vsp);
4935a59a8b3Srsb }
4945a59a8b3Srsb 
4955a59a8b3Srsb /*
4965a59a8b3Srsb  * Creates a kstat structure associated with a vopstats structure.
4975a59a8b3Srsb  */
4985a59a8b3Srsb kstat_t *
4995a59a8b3Srsb new_vskstat(char *ksname, vopstats_t *vsp)
5005a59a8b3Srsb {
5015a59a8b3Srsb 	kstat_t		*ksp;
5025a59a8b3Srsb 
5035a59a8b3Srsb 	if (!vopstats_enabled) {
5045a59a8b3Srsb 		return (NULL);
5055a59a8b3Srsb 	}
5065a59a8b3Srsb 
5075a59a8b3Srsb 	ksp = kstat_create("unix", 0, ksname, "misc", KSTAT_TYPE_NAMED,
5085a59a8b3Srsb 	    sizeof (vopstats_t)/sizeof (kstat_named_t),
5095a59a8b3Srsb 	    KSTAT_FLAG_VIRTUAL|KSTAT_FLAG_WRITABLE);
5105a59a8b3Srsb 	if (ksp) {
5115a59a8b3Srsb 		ksp->ks_data = vsp;
5125a59a8b3Srsb 		kstat_install(ksp);
5135a59a8b3Srsb 	}
5145a59a8b3Srsb 
5155a59a8b3Srsb 	return (ksp);
5165a59a8b3Srsb }
5175a59a8b3Srsb 
5185a59a8b3Srsb /*
5195a59a8b3Srsb  * Called from vfsinit() to initialize the support mechanisms for vopstats
5205a59a8b3Srsb  */
5215a59a8b3Srsb void
5225a59a8b3Srsb vopstats_startup()
5235a59a8b3Srsb {
5245a59a8b3Srsb 	if (!vopstats_enabled)
5255a59a8b3Srsb 		return;
5265a59a8b3Srsb 
5275a59a8b3Srsb 	/*
5285a59a8b3Srsb 	 * Creates the AVL tree which holds per-vfs vopstat anchors.  This
5295a59a8b3Srsb 	 * is necessary since we need to check if a kstat exists before we
5305a59a8b3Srsb 	 * attempt to create it.  Also, initialize its lock.
5315a59a8b3Srsb 	 */
5325a59a8b3Srsb 	avl_create(&vskstat_tree, vska_compar, sizeof (vsk_anchor_t),
5335a59a8b3Srsb 	    offsetof(vsk_anchor_t, vsk_node));
5345a59a8b3Srsb 	mutex_init(&vskstat_tree_lock, NULL, MUTEX_DEFAULT, NULL);
5355a59a8b3Srsb 
5365a59a8b3Srsb 	vsk_anchor_cache = kmem_cache_create("vsk_anchor_cache",
5375a59a8b3Srsb 	    sizeof (vsk_anchor_t), sizeof (uintptr_t), NULL, NULL, NULL,
5385a59a8b3Srsb 	    NULL, NULL, 0);
5395a59a8b3Srsb 
5405a59a8b3Srsb 	/*
5415a59a8b3Srsb 	 * Set up the array of pointers for the vopstats-by-FS-type.
5425a59a8b3Srsb 	 * The entries will be allocated/initialized as each file system
5435a59a8b3Srsb 	 * goes through modload/mod_installfs.
5445a59a8b3Srsb 	 */
5455a59a8b3Srsb 	vopstats_fstype = (vopstats_t **)kmem_zalloc(
5465a59a8b3Srsb 	    (sizeof (vopstats_t *) * nfstype), KM_SLEEP);
5475a59a8b3Srsb 
5485a59a8b3Srsb 	/* Set up the global vopstats initialization template */
5495a59a8b3Srsb 	vs_templatep = create_vopstats_template();
5505a59a8b3Srsb }
5515a59a8b3Srsb 
5525a59a8b3Srsb /*
5535a59a8b3Srsb  * We need to have the all of the counters zeroed.
5545a59a8b3Srsb  * The initialization of the vopstats_t includes on the order of
5555a59a8b3Srsb  * 50 calls to kstat_named_init().  Rather that do that on every call,
5565a59a8b3Srsb  * we do it once in a template (vs_templatep) then bcopy it over.
5575a59a8b3Srsb  */
5585a59a8b3Srsb void
5595a59a8b3Srsb initialize_vopstats(vopstats_t *vsp)
5605a59a8b3Srsb {
5615a59a8b3Srsb 	if (vsp == NULL)
5625a59a8b3Srsb 		return;
5635a59a8b3Srsb 
5645a59a8b3Srsb 	bcopy(vs_templatep, vsp, sizeof (vopstats_t));
5655a59a8b3Srsb }
5665a59a8b3Srsb 
5675a59a8b3Srsb /*
56882c7f3c4Srsb  * If possible, determine which vopstats by fstype to use and
56982c7f3c4Srsb  * return a pointer to the caller.
5705a59a8b3Srsb  */
57182c7f3c4Srsb vopstats_t *
57282c7f3c4Srsb get_fstype_vopstats(vfs_t *vfsp, struct vfssw *vswp)
5735a59a8b3Srsb {
5745a59a8b3Srsb 	int		fstype = 0;	/* Index into vfssw[] */
57582c7f3c4Srsb 	vopstats_t	*vsp = NULL;
5765a59a8b3Srsb 
5775a59a8b3Srsb 	if (vfsp == NULL || (vfsp->vfs_flag & VFS_STATS) == 0 ||
5785a59a8b3Srsb 	    !vopstats_enabled)
57982c7f3c4Srsb 		return (NULL);
5805a59a8b3Srsb 	/*
5815a59a8b3Srsb 	 * Set up the fstype.  We go to so much trouble because all versions
5825a59a8b3Srsb 	 * of NFS use the same fstype in their vfs even though they have
5835a59a8b3Srsb 	 * distinct entries in the vfssw[] table.
58482c7f3c4Srsb 	 * NOTE: A special vfs (e.g., EIO_vfs) may not have an entry.
5855a59a8b3Srsb 	 */
5865a59a8b3Srsb 	if (vswp) {
5875a59a8b3Srsb 		fstype = vswp - vfssw;	/* Gets us the index */
5885a59a8b3Srsb 	} else {
5895a59a8b3Srsb 		fstype = vfsp->vfs_fstype;
5905a59a8b3Srsb 	}
5915a59a8b3Srsb 
5925a59a8b3Srsb 	/*
5935a59a8b3Srsb 	 * Point to the per-fstype vopstats. The only valid values are
5945a59a8b3Srsb 	 * non-zero positive values less than the number of vfssw[] table
5955a59a8b3Srsb 	 * entries.
5965a59a8b3Srsb 	 */
5975a59a8b3Srsb 	if (fstype > 0 && fstype < nfstype) {
59882c7f3c4Srsb 		vsp = vopstats_fstype[fstype];
5995a59a8b3Srsb 	}
6005a59a8b3Srsb 
60182c7f3c4Srsb 	return (vsp);
60282c7f3c4Srsb }
60382c7f3c4Srsb 
60482c7f3c4Srsb /*
60582c7f3c4Srsb  * Generate a kstat name, create the kstat structure, and allocate a
60682c7f3c4Srsb  * vsk_anchor_t to hold it together.  Return the pointer to the vsk_anchor_t
60782c7f3c4Srsb  * to the caller.  This must only be called from a mount.
60882c7f3c4Srsb  */
60982c7f3c4Srsb vsk_anchor_t *
61082c7f3c4Srsb get_vskstat_anchor(vfs_t *vfsp)
61182c7f3c4Srsb {
61282c7f3c4Srsb 	char		kstatstr[KSTAT_STRLEN]; /* kstat name for vopstats */
61382c7f3c4Srsb 	statvfs64_t	statvfsbuf;		/* Needed to find f_fsid */
61482c7f3c4Srsb 	vsk_anchor_t	*vskp = NULL;		/* vfs <--> kstat anchor */
61582c7f3c4Srsb 	kstat_t		*ksp;			/* Ptr to new kstat */
61682c7f3c4Srsb 	avl_index_t	where;			/* Location in the AVL tree */
61782c7f3c4Srsb 
618ddfcde86Srsb 	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
619ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
62082c7f3c4Srsb 		return (NULL);
62182c7f3c4Srsb 
6225a59a8b3Srsb 	/* Need to get the fsid to build a kstat name */
6235a59a8b3Srsb 	if (VFS_STATVFS(vfsp, &statvfsbuf) == 0) {
6245a59a8b3Srsb 		/* Create a name for our kstats based on fsid */
6255a59a8b3Srsb 		(void) snprintf(kstatstr, KSTAT_STRLEN, "%s%lx",
6265a59a8b3Srsb 		    VOPSTATS_STR, statvfsbuf.f_fsid);
6275a59a8b3Srsb 
6285a59a8b3Srsb 		/* Allocate and initialize the vsk_anchor_t */
6295a59a8b3Srsb 		vskp = kmem_cache_alloc(vsk_anchor_cache, KM_SLEEP);
6305a59a8b3Srsb 		bzero(vskp, sizeof (*vskp));
6315a59a8b3Srsb 		vskp->vsk_fsid = statvfsbuf.f_fsid;
6325a59a8b3Srsb 
6335a59a8b3Srsb 		mutex_enter(&vskstat_tree_lock);
6345a59a8b3Srsb 		if (avl_find(&vskstat_tree, vskp, &where) == NULL) {
6355a59a8b3Srsb 			avl_insert(&vskstat_tree, vskp, where);
6365a59a8b3Srsb 			mutex_exit(&vskstat_tree_lock);
6375a59a8b3Srsb 
6385a59a8b3Srsb 			/*
6395a59a8b3Srsb 			 * Now that we've got the anchor in the AVL
6405a59a8b3Srsb 			 * tree, we can create the kstat.
6415a59a8b3Srsb 			 */
6425a59a8b3Srsb 			ksp = new_vskstat(kstatstr, &vfsp->vfs_vopstats);
6435a59a8b3Srsb 			if (ksp) {
6445a59a8b3Srsb 				vskp->vsk_ksp = ksp;
6455a59a8b3Srsb 			}
6465a59a8b3Srsb 		} else {
6475a59a8b3Srsb 			/* Oops, found one! Release memory and lock. */
6485a59a8b3Srsb 			mutex_exit(&vskstat_tree_lock);
6495a59a8b3Srsb 			kmem_cache_free(vsk_anchor_cache, vskp);
65082c7f3c4Srsb 			vskp = NULL;
6515a59a8b3Srsb 		}
6525a59a8b3Srsb 	}
65382c7f3c4Srsb 	return (vskp);
6545a59a8b3Srsb }
6555a59a8b3Srsb 
6565a59a8b3Srsb /*
6575a59a8b3Srsb  * We're in the process of tearing down the vfs and need to cleanup
6585a59a8b3Srsb  * the data structures associated with the vopstats. Must only be called
6595a59a8b3Srsb  * from dounmount().
6605a59a8b3Srsb  */
6615a59a8b3Srsb void
6625a59a8b3Srsb teardown_vopstats(vfs_t *vfsp)
6635a59a8b3Srsb {
6645a59a8b3Srsb 	vsk_anchor_t	*vskap;
6655a59a8b3Srsb 	avl_index_t	where;
6665a59a8b3Srsb 
667ddfcde86Srsb 	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
668ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
6695a59a8b3Srsb 		return;
6705a59a8b3Srsb 
6715a59a8b3Srsb 	/* This is a safe check since VFS_STATS must be set (see above) */
6725a59a8b3Srsb 	if ((vskap = vfsp->vfs_vskap) == NULL)
6735a59a8b3Srsb 		return;
6745a59a8b3Srsb 
6755a59a8b3Srsb 	/* Whack the pointer right away */
6765a59a8b3Srsb 	vfsp->vfs_vskap = NULL;
6775a59a8b3Srsb 
6785a59a8b3Srsb 	/* Lock the tree, remove the node, and delete the kstat */
6795a59a8b3Srsb 	mutex_enter(&vskstat_tree_lock);
6805a59a8b3Srsb 	if (avl_find(&vskstat_tree, vskap, &where)) {
6815a59a8b3Srsb 		avl_remove(&vskstat_tree, vskap);
6825a59a8b3Srsb 	}
6835a59a8b3Srsb 
6845a59a8b3Srsb 	if (vskap->vsk_ksp) {
6855a59a8b3Srsb 		kstat_delete(vskap->vsk_ksp);
6865a59a8b3Srsb 	}
6875a59a8b3Srsb 	mutex_exit(&vskstat_tree_lock);
6885a59a8b3Srsb 
6895a59a8b3Srsb 	kmem_cache_free(vsk_anchor_cache, vskap);
6905a59a8b3Srsb }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate  * Read or write a vnode.  Called from kernel code.
6947c478bd9Sstevel@tonic-gate  */
6957c478bd9Sstevel@tonic-gate int
6967c478bd9Sstevel@tonic-gate vn_rdwr(
6977c478bd9Sstevel@tonic-gate 	enum uio_rw rw,
6987c478bd9Sstevel@tonic-gate 	struct vnode *vp,
6997c478bd9Sstevel@tonic-gate 	caddr_t base,
7007c478bd9Sstevel@tonic-gate 	ssize_t len,
7017c478bd9Sstevel@tonic-gate 	offset_t offset,
7027c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
7037c478bd9Sstevel@tonic-gate 	int ioflag,
7047c478bd9Sstevel@tonic-gate 	rlim64_t ulimit,	/* meaningful only if rw is UIO_WRITE */
7057c478bd9Sstevel@tonic-gate 	cred_t *cr,
7067c478bd9Sstevel@tonic-gate 	ssize_t *residp)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	struct uio uio;
7097c478bd9Sstevel@tonic-gate 	struct iovec iov;
7107c478bd9Sstevel@tonic-gate 	int error;
7117c478bd9Sstevel@tonic-gate 	int in_crit = 0;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	if (rw == UIO_WRITE && ISROFILE(vp))
7147c478bd9Sstevel@tonic-gate 		return (EROFS);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if (len < 0)
7177c478bd9Sstevel@tonic-gate 		return (EIO);
7187c478bd9Sstevel@tonic-gate 
719f48205beScasper 	VOPXID_MAP_CR(vp, cr);
720f48205beScasper 
7217c478bd9Sstevel@tonic-gate 	iov.iov_base = base;
7227c478bd9Sstevel@tonic-gate 	iov.iov_len = len;
7237c478bd9Sstevel@tonic-gate 	uio.uio_iov = &iov;
7247c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
7257c478bd9Sstevel@tonic-gate 	uio.uio_loffset = offset;
7267c478bd9Sstevel@tonic-gate 	uio.uio_segflg = (short)seg;
7277c478bd9Sstevel@tonic-gate 	uio.uio_resid = len;
7287c478bd9Sstevel@tonic-gate 	uio.uio_llimit = ulimit;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	/*
7317c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
7327c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
7337c478bd9Sstevel@tonic-gate 	 */
7347c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
7357c478bd9Sstevel@tonic-gate 		int svmand;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
7387c478bd9Sstevel@tonic-gate 		in_crit = 1;
7397c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, cr, &svmand);
7407c478bd9Sstevel@tonic-gate 		if (error != 0)
7417c478bd9Sstevel@tonic-gate 			goto done;
7427c478bd9Sstevel@tonic-gate 		if (nbl_conflict(vp, rw == UIO_WRITE ? NBL_WRITE : NBL_READ,
7437c478bd9Sstevel@tonic-gate 		    uio.uio_offset, uio.uio_resid, svmand)) {
7447c478bd9Sstevel@tonic-gate 			error = EACCES;
7457c478bd9Sstevel@tonic-gate 			goto done;
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp,
7507c478bd9Sstevel@tonic-gate 	    rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
7517c478bd9Sstevel@tonic-gate 	if (rw == UIO_WRITE) {
7527c478bd9Sstevel@tonic-gate 		uio.uio_fmode = FWRITE;
7537c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_DEFAULT;
7547c478bd9Sstevel@tonic-gate 		error = VOP_WRITE(vp, &uio, ioflag, cr, NULL);
7557c478bd9Sstevel@tonic-gate 	} else {
7567c478bd9Sstevel@tonic-gate 		uio.uio_fmode = FREAD;
7577c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_CACHED;
7587c478bd9Sstevel@tonic-gate 		error = VOP_READ(vp, &uio, ioflag, cr, NULL);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE,
7617c478bd9Sstevel@tonic-gate 	    NULL);
7627c478bd9Sstevel@tonic-gate 	if (residp)
7637c478bd9Sstevel@tonic-gate 		*residp = uio.uio_resid;
7647c478bd9Sstevel@tonic-gate 	else if (uio.uio_resid)
7657c478bd9Sstevel@tonic-gate 		error = EIO;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate done:
7687c478bd9Sstevel@tonic-gate 	if (in_crit)
7697c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
7707c478bd9Sstevel@tonic-gate 	return (error);
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate /*
7747c478bd9Sstevel@tonic-gate  * Release a vnode.  Call VOP_INACTIVE on last reference or
7757c478bd9Sstevel@tonic-gate  * decrement reference count.
7767c478bd9Sstevel@tonic-gate  *
7777c478bd9Sstevel@tonic-gate  * To avoid race conditions, the v_count is left at 1 for
7787c478bd9Sstevel@tonic-gate  * the call to VOP_INACTIVE. This prevents another thread
7797c478bd9Sstevel@tonic-gate  * from reclaiming and releasing the vnode *before* the
7807c478bd9Sstevel@tonic-gate  * VOP_INACTIVE routine has a chance to destroy the vnode.
7817c478bd9Sstevel@tonic-gate  * We can't have more than 1 thread calling VOP_INACTIVE
7827c478bd9Sstevel@tonic-gate  * on a vnode.
7837c478bd9Sstevel@tonic-gate  */
7847c478bd9Sstevel@tonic-gate void
7857c478bd9Sstevel@tonic-gate vn_rele(vnode_t *vp)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0)
7887c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_rele: vnode ref count 0");
7897c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
7907c478bd9Sstevel@tonic-gate 	if (vp->v_count == 1) {
7917c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
7927c478bd9Sstevel@tonic-gate 		VOP_INACTIVE(vp, CRED());
7937c478bd9Sstevel@tonic-gate 	} else {
7947c478bd9Sstevel@tonic-gate 		vp->v_count--;
7957c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate /*
8007c478bd9Sstevel@tonic-gate  * Like vn_rele() except that it clears v_stream under v_lock.
8017c478bd9Sstevel@tonic-gate  * This is used by sockfs when it dismantels the association between
8027c478bd9Sstevel@tonic-gate  * the sockfs node and the vnode in the underlaying file system.
8037c478bd9Sstevel@tonic-gate  * v_lock has to be held to prevent a thread coming through the lookupname
8047c478bd9Sstevel@tonic-gate  * path from accessing a stream head that is going away.
8057c478bd9Sstevel@tonic-gate  */
8067c478bd9Sstevel@tonic-gate void
8077c478bd9Sstevel@tonic-gate vn_rele_stream(vnode_t *vp)
8087c478bd9Sstevel@tonic-gate {
8097c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0)
8107c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_rele: vnode ref count 0");
8117c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
8127c478bd9Sstevel@tonic-gate 	vp->v_stream = NULL;
8137c478bd9Sstevel@tonic-gate 	if (vp->v_count == 1) {
8147c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
8157c478bd9Sstevel@tonic-gate 		VOP_INACTIVE(vp, CRED());
8167c478bd9Sstevel@tonic-gate 	} else {
8177c478bd9Sstevel@tonic-gate 		vp->v_count--;
8187c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate int
8237c478bd9Sstevel@tonic-gate vn_open(
8247c478bd9Sstevel@tonic-gate 	char *pnamep,
8257c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
8267c478bd9Sstevel@tonic-gate 	int filemode,
8277c478bd9Sstevel@tonic-gate 	int createmode,
8287c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
8297c478bd9Sstevel@tonic-gate 	enum create crwhy,
8307c478bd9Sstevel@tonic-gate 	mode_t umask)
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	return (vn_openat(pnamep, seg, filemode,
8337c478bd9Sstevel@tonic-gate 	    createmode, vpp, crwhy, umask, NULL));
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate /*
8387c478bd9Sstevel@tonic-gate  * Open/create a vnode.
8397c478bd9Sstevel@tonic-gate  * This may be callable by the kernel, the only known use
8407c478bd9Sstevel@tonic-gate  * of user context being that the current user credentials
8417c478bd9Sstevel@tonic-gate  * are used for permissions.  crwhy is defined iff filemode & FCREAT.
8427c478bd9Sstevel@tonic-gate  */
8437c478bd9Sstevel@tonic-gate int
8447c478bd9Sstevel@tonic-gate vn_openat(
8457c478bd9Sstevel@tonic-gate 	char *pnamep,
8467c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
8477c478bd9Sstevel@tonic-gate 	int filemode,
8487c478bd9Sstevel@tonic-gate 	int createmode,
8497c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
8507c478bd9Sstevel@tonic-gate 	enum create crwhy,
8517c478bd9Sstevel@tonic-gate 	mode_t umask,
8527c478bd9Sstevel@tonic-gate 	struct vnode *startvp)
8537c478bd9Sstevel@tonic-gate {
8547c478bd9Sstevel@tonic-gate 	struct vnode *vp;
8557c478bd9Sstevel@tonic-gate 	int mode;
8567c478bd9Sstevel@tonic-gate 	int error;
8577c478bd9Sstevel@tonic-gate 	int in_crit = 0;
8587c478bd9Sstevel@tonic-gate 	struct vattr vattr;
8597c478bd9Sstevel@tonic-gate 	enum symfollow follow;
860dd29fa4aSprabahar 	int estale_retry = 0;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	mode = 0;
8637c478bd9Sstevel@tonic-gate 	if (filemode & FREAD)
8647c478bd9Sstevel@tonic-gate 		mode |= VREAD;
8657c478bd9Sstevel@tonic-gate 	if (filemode & (FWRITE|FTRUNC))
8667c478bd9Sstevel@tonic-gate 		mode |= VWRITE;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	/* symlink interpretation */
8697c478bd9Sstevel@tonic-gate 	if (filemode & FNOFOLLOW)
8707c478bd9Sstevel@tonic-gate 		follow = NO_FOLLOW;
8717c478bd9Sstevel@tonic-gate 	else
8727c478bd9Sstevel@tonic-gate 		follow = FOLLOW;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate top:
8757c478bd9Sstevel@tonic-gate 	if (filemode & FCREAT) {
8767c478bd9Sstevel@tonic-gate 		enum vcexcl excl;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		/*
8797c478bd9Sstevel@tonic-gate 		 * Wish to create a file.
8807c478bd9Sstevel@tonic-gate 		 */
8817c478bd9Sstevel@tonic-gate 		vattr.va_type = VREG;
8827c478bd9Sstevel@tonic-gate 		vattr.va_mode = createmode;
8837c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_TYPE|AT_MODE;
8847c478bd9Sstevel@tonic-gate 		if (filemode & FTRUNC) {
8857c478bd9Sstevel@tonic-gate 			vattr.va_size = 0;
8867c478bd9Sstevel@tonic-gate 			vattr.va_mask |= AT_SIZE;
8877c478bd9Sstevel@tonic-gate 		}
8887c478bd9Sstevel@tonic-gate 		if (filemode & FEXCL)
8897c478bd9Sstevel@tonic-gate 			excl = EXCL;
8907c478bd9Sstevel@tonic-gate 		else
8917c478bd9Sstevel@tonic-gate 			excl = NONEXCL;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		if (error =
8947c478bd9Sstevel@tonic-gate 		    vn_createat(pnamep, seg, &vattr, excl, mode, &vp, crwhy,
895*1b300de9Sjwahlig 		    (filemode & ~(FTRUNC|FEXCL)), umask, startvp))
8967c478bd9Sstevel@tonic-gate 			return (error);
8977c478bd9Sstevel@tonic-gate 	} else {
8987c478bd9Sstevel@tonic-gate 		/*
8997c478bd9Sstevel@tonic-gate 		 * Wish to open a file.  Just look it up.
9007c478bd9Sstevel@tonic-gate 		 */
9017c478bd9Sstevel@tonic-gate 		if (error = lookupnameat(pnamep, seg, follow,
9027c478bd9Sstevel@tonic-gate 		    NULLVPP, &vp, startvp)) {
903dd29fa4aSprabahar 			if ((error == ESTALE) &&
904dd29fa4aSprabahar 			    fs_need_estale_retry(estale_retry++))
9057c478bd9Sstevel@tonic-gate 				goto top;
9067c478bd9Sstevel@tonic-gate 			return (error);
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 		/*
9107c478bd9Sstevel@tonic-gate 		 * Get the attributes to check whether file is large.
9117c478bd9Sstevel@tonic-gate 		 * We do this only if the FOFFMAX flag is not set and
9127c478bd9Sstevel@tonic-gate 		 * only for regular files.
9137c478bd9Sstevel@tonic-gate 		 */
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 		if (!(filemode & FOFFMAX) && (vp->v_type == VREG)) {
9167c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
9177c478bd9Sstevel@tonic-gate 			if ((error = VOP_GETATTR(vp, &vattr, 0, CRED()))) {
9187c478bd9Sstevel@tonic-gate 				goto out;
9197c478bd9Sstevel@tonic-gate 			}
9207c478bd9Sstevel@tonic-gate 			if (vattr.va_size > (u_offset_t)MAXOFF32_T) {
9217c478bd9Sstevel@tonic-gate 				/*
9227c478bd9Sstevel@tonic-gate 				 * Large File API - regular open fails
9237c478bd9Sstevel@tonic-gate 				 * if FOFFMAX flag is set in file mode
9247c478bd9Sstevel@tonic-gate 				 */
9257c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
9267c478bd9Sstevel@tonic-gate 				goto out;
9277c478bd9Sstevel@tonic-gate 			}
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 		/*
9307c478bd9Sstevel@tonic-gate 		 * Can't write directories, active texts, or
9317c478bd9Sstevel@tonic-gate 		 * read-only filesystems.  Can't truncate files
9327c478bd9Sstevel@tonic-gate 		 * on which mandatory locking is in effect.
9337c478bd9Sstevel@tonic-gate 		 */
9347c478bd9Sstevel@tonic-gate 		if (filemode & (FWRITE|FTRUNC)) {
9357c478bd9Sstevel@tonic-gate 			/*
9367c478bd9Sstevel@tonic-gate 			 * Allow writable directory if VDIROPEN flag is set.
9377c478bd9Sstevel@tonic-gate 			 */
9387c478bd9Sstevel@tonic-gate 			if (vp->v_type == VDIR && !(vp->v_flag & VDIROPEN)) {
9397c478bd9Sstevel@tonic-gate 				error = EISDIR;
9407c478bd9Sstevel@tonic-gate 				goto out;
9417c478bd9Sstevel@tonic-gate 			}
9427c478bd9Sstevel@tonic-gate 			if (ISROFILE(vp)) {
9437c478bd9Sstevel@tonic-gate 				error = EROFS;
9447c478bd9Sstevel@tonic-gate 				goto out;
9457c478bd9Sstevel@tonic-gate 			}
9467c478bd9Sstevel@tonic-gate 			/*
9477c478bd9Sstevel@tonic-gate 			 * Can't truncate files on which mandatory locking
9487c478bd9Sstevel@tonic-gate 			 * or non-blocking mandatory locking is in effect.
9497c478bd9Sstevel@tonic-gate 			 */
9507c478bd9Sstevel@tonic-gate 			if (filemode & FTRUNC) {
9517c478bd9Sstevel@tonic-gate 				vnode_t *rvp;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 				if (VOP_REALVP(vp, &rvp) != 0)
9547c478bd9Sstevel@tonic-gate 					rvp = vp;
9557c478bd9Sstevel@tonic-gate 				if (nbl_need_check(vp)) {
9567c478bd9Sstevel@tonic-gate 					nbl_start_crit(vp, RW_READER);
9577c478bd9Sstevel@tonic-gate 					in_crit = 1;
9587c478bd9Sstevel@tonic-gate 					vattr.va_mask = AT_MODE|AT_SIZE;
9597c478bd9Sstevel@tonic-gate 					if ((error = VOP_GETATTR(vp, &vattr, 0,
9607c478bd9Sstevel@tonic-gate 					    CRED())) == 0) {
9617c478bd9Sstevel@tonic-gate 						if (rvp->v_filocks != NULL)
9627c478bd9Sstevel@tonic-gate 							if (MANDLOCK(vp,
9637c478bd9Sstevel@tonic-gate 							    vattr.va_mode))
9647c478bd9Sstevel@tonic-gate 								error = EAGAIN;
9657c478bd9Sstevel@tonic-gate 						if (!error) {
9667c478bd9Sstevel@tonic-gate 							if (nbl_conflict(vp,
9677c478bd9Sstevel@tonic-gate 							    NBL_WRITE, 0,
9687c478bd9Sstevel@tonic-gate 							    vattr.va_size, 0))
9697c478bd9Sstevel@tonic-gate 								error = EACCES;
9707c478bd9Sstevel@tonic-gate 						}
9717c478bd9Sstevel@tonic-gate 					}
9727c478bd9Sstevel@tonic-gate 				} else if (rvp->v_filocks != NULL) {
9737c478bd9Sstevel@tonic-gate 					vattr.va_mask = AT_MODE;
9747c478bd9Sstevel@tonic-gate 					if ((error = VOP_GETATTR(vp, &vattr,
9757c478bd9Sstevel@tonic-gate 					    0, CRED())) == 0 && MANDLOCK(vp,
9767c478bd9Sstevel@tonic-gate 					    vattr.va_mode))
9777c478bd9Sstevel@tonic-gate 						error = EAGAIN;
9787c478bd9Sstevel@tonic-gate 				}
9797c478bd9Sstevel@tonic-gate 			}
9807c478bd9Sstevel@tonic-gate 			if (error)
9817c478bd9Sstevel@tonic-gate 				goto out;
9827c478bd9Sstevel@tonic-gate 		}
9837c478bd9Sstevel@tonic-gate 		/*
9847c478bd9Sstevel@tonic-gate 		 * Check permissions.
9857c478bd9Sstevel@tonic-gate 		 */
9867c478bd9Sstevel@tonic-gate 		if (error = VOP_ACCESS(vp, mode, 0, CRED()))
9877c478bd9Sstevel@tonic-gate 			goto out;
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	/*
9917c478bd9Sstevel@tonic-gate 	 * Do remaining checks for FNOFOLLOW and FNOLINKS.
9927c478bd9Sstevel@tonic-gate 	 */
9937c478bd9Sstevel@tonic-gate 	if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) {
9949acbbeafSnn35248 		error = ELOOP;
9957c478bd9Sstevel@tonic-gate 		goto out;
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 	if (filemode & FNOLINKS) {
9987c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_NLINK;
9997c478bd9Sstevel@tonic-gate 		if ((error = VOP_GETATTR(vp, &vattr, 0, CRED()))) {
10007c478bd9Sstevel@tonic-gate 			goto out;
10017c478bd9Sstevel@tonic-gate 		}
10027c478bd9Sstevel@tonic-gate 		if (vattr.va_nlink != 1) {
10037c478bd9Sstevel@tonic-gate 			error = EMLINK;
10047c478bd9Sstevel@tonic-gate 			goto out;
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	/*
10097c478bd9Sstevel@tonic-gate 	 * Opening a socket corresponding to the AF_UNIX pathname
10107c478bd9Sstevel@tonic-gate 	 * in the filesystem name space is not supported.
10117c478bd9Sstevel@tonic-gate 	 * However, VSOCK nodes in namefs are supported in order
10127c478bd9Sstevel@tonic-gate 	 * to make fattach work for sockets.
10137c478bd9Sstevel@tonic-gate 	 *
10147c478bd9Sstevel@tonic-gate 	 * XXX This uses VOP_REALVP to distinguish between
10157c478bd9Sstevel@tonic-gate 	 * an unopened namefs node (where VOP_REALVP returns a
10167c478bd9Sstevel@tonic-gate 	 * different VSOCK vnode) and a VSOCK created by vn_create
10177c478bd9Sstevel@tonic-gate 	 * in some file system (where VOP_REALVP would never return
10187c478bd9Sstevel@tonic-gate 	 * a different vnode).
10197c478bd9Sstevel@tonic-gate 	 */
10207c478bd9Sstevel@tonic-gate 	if (vp->v_type == VSOCK) {
10217c478bd9Sstevel@tonic-gate 		struct vnode *nvp;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 		error = VOP_REALVP(vp, &nvp);
10247c478bd9Sstevel@tonic-gate 		if (error != 0 || nvp == NULL || nvp == vp ||
10257c478bd9Sstevel@tonic-gate 		    nvp->v_type != VSOCK) {
10267c478bd9Sstevel@tonic-gate 			error = EOPNOTSUPP;
10277c478bd9Sstevel@tonic-gate 			goto out;
10287c478bd9Sstevel@tonic-gate 		}
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 	/*
10317c478bd9Sstevel@tonic-gate 	 * Do opening protocol.
10327c478bd9Sstevel@tonic-gate 	 */
10337c478bd9Sstevel@tonic-gate 	error = VOP_OPEN(&vp, filemode, CRED());
10347c478bd9Sstevel@tonic-gate 	/*
10357c478bd9Sstevel@tonic-gate 	 * Truncate if required.
10367c478bd9Sstevel@tonic-gate 	 */
10377c478bd9Sstevel@tonic-gate 	if (error == 0 && (filemode & FTRUNC) && !(filemode & FCREAT)) {
10387c478bd9Sstevel@tonic-gate 		vattr.va_size = 0;
10397c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_SIZE;
10407c478bd9Sstevel@tonic-gate 		if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0)
10417c478bd9Sstevel@tonic-gate 			(void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED());
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate out:
10447c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	if (in_crit) {
10477c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
10487c478bd9Sstevel@tonic-gate 		in_crit = 0;
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 	if (error) {
10517c478bd9Sstevel@tonic-gate 		/*
10527c478bd9Sstevel@tonic-gate 		 * The following clause was added to handle a problem
10537c478bd9Sstevel@tonic-gate 		 * with NFS consistency.  It is possible that a lookup
10547c478bd9Sstevel@tonic-gate 		 * of the file to be opened succeeded, but the file
10557c478bd9Sstevel@tonic-gate 		 * itself doesn't actually exist on the server.  This
10567c478bd9Sstevel@tonic-gate 		 * is chiefly due to the DNLC containing an entry for
10577c478bd9Sstevel@tonic-gate 		 * the file which has been removed on the server.  In
10587c478bd9Sstevel@tonic-gate 		 * this case, we just start over.  If there was some
10597c478bd9Sstevel@tonic-gate 		 * other cause for the ESTALE error, then the lookup
10607c478bd9Sstevel@tonic-gate 		 * of the file will fail and the error will be returned
10617c478bd9Sstevel@tonic-gate 		 * above instead of looping around from here.
10627c478bd9Sstevel@tonic-gate 		 */
10637c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1064dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
10657c478bd9Sstevel@tonic-gate 			goto top;
10667c478bd9Sstevel@tonic-gate 	} else
10677c478bd9Sstevel@tonic-gate 		*vpp = vp;
10687c478bd9Sstevel@tonic-gate 	return (error);
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate int
10727c478bd9Sstevel@tonic-gate vn_create(
10737c478bd9Sstevel@tonic-gate 	char *pnamep,
10747c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
10757c478bd9Sstevel@tonic-gate 	struct vattr *vap,
10767c478bd9Sstevel@tonic-gate 	enum vcexcl excl,
10777c478bd9Sstevel@tonic-gate 	int mode,
10787c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
10797c478bd9Sstevel@tonic-gate 	enum create why,
10807c478bd9Sstevel@tonic-gate 	int flag,
10817c478bd9Sstevel@tonic-gate 	mode_t umask)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 	return (vn_createat(pnamep, seg, vap, excl, mode, vpp,
10847c478bd9Sstevel@tonic-gate 	    why, flag, umask, NULL));
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate /*
10887c478bd9Sstevel@tonic-gate  * Create a vnode (makenode).
10897c478bd9Sstevel@tonic-gate  */
10907c478bd9Sstevel@tonic-gate int
10917c478bd9Sstevel@tonic-gate vn_createat(
10927c478bd9Sstevel@tonic-gate 	char *pnamep,
10937c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
10947c478bd9Sstevel@tonic-gate 	struct vattr *vap,
10957c478bd9Sstevel@tonic-gate 	enum vcexcl excl,
10967c478bd9Sstevel@tonic-gate 	int mode,
10977c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
10987c478bd9Sstevel@tonic-gate 	enum create why,
10997c478bd9Sstevel@tonic-gate 	int flag,
11007c478bd9Sstevel@tonic-gate 	mode_t umask,
11017c478bd9Sstevel@tonic-gate 	struct vnode *startvp)
11027c478bd9Sstevel@tonic-gate {
11037c478bd9Sstevel@tonic-gate 	struct vnode *dvp;	/* ptr to parent dir vnode */
11047c478bd9Sstevel@tonic-gate 	struct vnode *vp = NULL;
11057c478bd9Sstevel@tonic-gate 	struct pathname pn;
11067c478bd9Sstevel@tonic-gate 	int error;
11077c478bd9Sstevel@tonic-gate 	int in_crit = 0;
11087c478bd9Sstevel@tonic-gate 	struct vattr vattr;
11097c478bd9Sstevel@tonic-gate 	enum symfollow follow;
1110dd29fa4aSprabahar 	int estale_retry = 0;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	/* symlink interpretation */
11157c478bd9Sstevel@tonic-gate 	if ((flag & FNOFOLLOW) || excl == EXCL)
11167c478bd9Sstevel@tonic-gate 		follow = NO_FOLLOW;
11177c478bd9Sstevel@tonic-gate 	else
11187c478bd9Sstevel@tonic-gate 		follow = FOLLOW;
11197c478bd9Sstevel@tonic-gate 	flag &= ~(FNOFOLLOW|FNOLINKS);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate top:
11227c478bd9Sstevel@tonic-gate 	/*
11237c478bd9Sstevel@tonic-gate 	 * Lookup directory.
11247c478bd9Sstevel@tonic-gate 	 * If new object is a file, call lower level to create it.
11257c478bd9Sstevel@tonic-gate 	 * Note that it is up to the lower level to enforce exclusive
11267c478bd9Sstevel@tonic-gate 	 * creation, if the file is already there.
11277c478bd9Sstevel@tonic-gate 	 * This allows the lower level to do whatever
11287c478bd9Sstevel@tonic-gate 	 * locking or protocol that is needed to prevent races.
11297c478bd9Sstevel@tonic-gate 	 * If the new object is directory call lower level to make
11307c478bd9Sstevel@tonic-gate 	 * the new directory, with "." and "..".
11317c478bd9Sstevel@tonic-gate 	 */
11327c478bd9Sstevel@tonic-gate 	if (error = pn_get(pnamep, seg, &pn))
11337c478bd9Sstevel@tonic-gate 		return (error);
11347c478bd9Sstevel@tonic-gate #ifdef  C2_AUDIT
11357c478bd9Sstevel@tonic-gate 	if (audit_active)
11367c478bd9Sstevel@tonic-gate 		audit_vncreate_start();
11377c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
11387c478bd9Sstevel@tonic-gate 	dvp = NULL;
11397c478bd9Sstevel@tonic-gate 	*vpp = NULL;
11407c478bd9Sstevel@tonic-gate 	/*
11417c478bd9Sstevel@tonic-gate 	 * lookup will find the parent directory for the vnode.
11427c478bd9Sstevel@tonic-gate 	 * When it is done the pn holds the name of the entry
11437c478bd9Sstevel@tonic-gate 	 * in the directory.
11447c478bd9Sstevel@tonic-gate 	 * If this is a non-exclusive create we also find the node itself.
11457c478bd9Sstevel@tonic-gate 	 */
11467c478bd9Sstevel@tonic-gate 	error = lookuppnat(&pn, NULL, follow, &dvp,
11477c478bd9Sstevel@tonic-gate 	    (excl == EXCL) ? NULLVPP : vpp, startvp);
11487c478bd9Sstevel@tonic-gate 	if (error) {
11497c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1150dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
11517c478bd9Sstevel@tonic-gate 			goto top;
11527c478bd9Sstevel@tonic-gate 		if (why == CRMKDIR && error == EINVAL)
11537c478bd9Sstevel@tonic-gate 			error = EEXIST;		/* SVID */
11547c478bd9Sstevel@tonic-gate 		return (error);
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if (why != CRMKNOD)
11587c478bd9Sstevel@tonic-gate 		vap->va_mode &= ~VSVTX;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	/*
11617c478bd9Sstevel@tonic-gate 	 * If default ACLs are defined for the directory don't apply the
11627c478bd9Sstevel@tonic-gate 	 * umask if umask is passed.
11637c478bd9Sstevel@tonic-gate 	 */
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	if (umask) {
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 		vsecattr_t vsec;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 		vsec.vsa_aclcnt = 0;
11707c478bd9Sstevel@tonic-gate 		vsec.vsa_aclentp = NULL;
11717c478bd9Sstevel@tonic-gate 		vsec.vsa_dfaclcnt = 0;
11727c478bd9Sstevel@tonic-gate 		vsec.vsa_dfaclentp = NULL;
11737c478bd9Sstevel@tonic-gate 		vsec.vsa_mask = VSA_DFACLCNT;
1174fa9e4066Sahrens 		error =  VOP_GETSECATTR(dvp, &vsec, 0, CRED());
1175fa9e4066Sahrens 		/*
1176fa9e4066Sahrens 		 * If error is ENOSYS then treat it as no error
1177fa9e4066Sahrens 		 * Don't want to force all file systems to support
1178fa9e4066Sahrens 		 * aclent_t style of ACL's.
1179fa9e4066Sahrens 		 */
1180fa9e4066Sahrens 		if (error == ENOSYS)
1181fa9e4066Sahrens 			error = 0;
1182fa9e4066Sahrens 		if (error) {
11837c478bd9Sstevel@tonic-gate 			if (*vpp != NULL)
11847c478bd9Sstevel@tonic-gate 				VN_RELE(*vpp);
11857c478bd9Sstevel@tonic-gate 			goto out;
1186fa9e4066Sahrens 		} else {
11877c478bd9Sstevel@tonic-gate 			/*
11887c478bd9Sstevel@tonic-gate 			 * Apply the umask if no default ACLs.
11897c478bd9Sstevel@tonic-gate 			 */
11907c478bd9Sstevel@tonic-gate 			if (vsec.vsa_dfaclcnt == 0)
11917c478bd9Sstevel@tonic-gate 				vap->va_mode &= ~umask;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 			/*
1194fa9e4066Sahrens 			 * VOP_GETSECATTR() may have allocated memory for
1195fa9e4066Sahrens 			 * ACLs we didn't request, so double-check and
1196fa9e4066Sahrens 			 * free it if necessary.
11977c478bd9Sstevel@tonic-gate 			 */
11987c478bd9Sstevel@tonic-gate 			if (vsec.vsa_aclcnt && vsec.vsa_aclentp != NULL)
11997c478bd9Sstevel@tonic-gate 				kmem_free((caddr_t)vsec.vsa_aclentp,
12007c478bd9Sstevel@tonic-gate 				    vsec.vsa_aclcnt * sizeof (aclent_t));
12017c478bd9Sstevel@tonic-gate 			if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp != NULL)
12027c478bd9Sstevel@tonic-gate 				kmem_free((caddr_t)vsec.vsa_dfaclentp,
12037c478bd9Sstevel@tonic-gate 				    vsec.vsa_dfaclcnt * sizeof (aclent_t));
12047c478bd9Sstevel@tonic-gate 		}
1205fa9e4066Sahrens 	}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	/*
12087c478bd9Sstevel@tonic-gate 	 * In general we want to generate EROFS if the file system is
12097c478bd9Sstevel@tonic-gate 	 * readonly.  However, POSIX (IEEE Std. 1003.1) section 5.3.1
12107c478bd9Sstevel@tonic-gate 	 * documents the open system call, and it says that O_CREAT has no
12117c478bd9Sstevel@tonic-gate 	 * effect if the file already exists.  Bug 1119649 states
12127c478bd9Sstevel@tonic-gate 	 * that open(path, O_CREAT, ...) fails when attempting to open an
12137c478bd9Sstevel@tonic-gate 	 * existing file on a read only file system.  Thus, the first part
12147c478bd9Sstevel@tonic-gate 	 * of the following if statement has 3 checks:
12157c478bd9Sstevel@tonic-gate 	 *	if the file exists &&
12167c478bd9Sstevel@tonic-gate 	 *		it is being open with write access &&
12177c478bd9Sstevel@tonic-gate 	 *		the file system is read only
12187c478bd9Sstevel@tonic-gate 	 *	then generate EROFS
12197c478bd9Sstevel@tonic-gate 	 */
12207c478bd9Sstevel@tonic-gate 	if ((*vpp != NULL && (mode & VWRITE) && ISROFILE(*vpp)) ||
12217c478bd9Sstevel@tonic-gate 	    (*vpp == NULL && dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
12227c478bd9Sstevel@tonic-gate 		if (*vpp)
12237c478bd9Sstevel@tonic-gate 			VN_RELE(*vpp);
12247c478bd9Sstevel@tonic-gate 		error = EROFS;
12257c478bd9Sstevel@tonic-gate 	} else if (excl == NONEXCL && *vpp != NULL) {
12267c478bd9Sstevel@tonic-gate 		vnode_t *rvp;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 		/*
12297c478bd9Sstevel@tonic-gate 		 * File already exists.  If a mandatory lock has been
12307c478bd9Sstevel@tonic-gate 		 * applied, return error.
12317c478bd9Sstevel@tonic-gate 		 */
12327c478bd9Sstevel@tonic-gate 		vp = *vpp;
12337c478bd9Sstevel@tonic-gate 		if (VOP_REALVP(vp, &rvp) != 0)
12347c478bd9Sstevel@tonic-gate 			rvp = vp;
12357c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_SIZE) && nbl_need_check(vp)) {
12367c478bd9Sstevel@tonic-gate 			nbl_start_crit(vp, RW_READER);
12377c478bd9Sstevel@tonic-gate 			in_crit = 1;
12387c478bd9Sstevel@tonic-gate 		}
12397c478bd9Sstevel@tonic-gate 		if (rvp->v_filocks != NULL || rvp->v_shrlocks != NULL) {
12407c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_MODE|AT_SIZE;
12417c478bd9Sstevel@tonic-gate 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED())) {
12427c478bd9Sstevel@tonic-gate 				goto out;
12437c478bd9Sstevel@tonic-gate 			}
12447c478bd9Sstevel@tonic-gate 			if (MANDLOCK(vp, vattr.va_mode)) {
12457c478bd9Sstevel@tonic-gate 				error = EAGAIN;
12467c478bd9Sstevel@tonic-gate 				goto out;
12477c478bd9Sstevel@tonic-gate 			}
12487c478bd9Sstevel@tonic-gate 			/*
12497c478bd9Sstevel@tonic-gate 			 * File cannot be truncated if non-blocking mandatory
12507c478bd9Sstevel@tonic-gate 			 * locks are currently on the file.
12517c478bd9Sstevel@tonic-gate 			 */
12527c478bd9Sstevel@tonic-gate 			if ((vap->va_mask & AT_SIZE) && in_crit) {
12537c478bd9Sstevel@tonic-gate 				u_offset_t offset;
12547c478bd9Sstevel@tonic-gate 				ssize_t length;
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 				offset = vap->va_size > vattr.va_size ?
12577c478bd9Sstevel@tonic-gate 				    vattr.va_size : vap->va_size;
12587c478bd9Sstevel@tonic-gate 				length = vap->va_size > vattr.va_size ?
12597c478bd9Sstevel@tonic-gate 				    vap->va_size - vattr.va_size :
12607c478bd9Sstevel@tonic-gate 				    vattr.va_size - vap->va_size;
12617c478bd9Sstevel@tonic-gate 				if (nbl_conflict(vp, NBL_WRITE, offset,
12627c478bd9Sstevel@tonic-gate 				    length, 0)) {
12637c478bd9Sstevel@tonic-gate 					error = EACCES;
12647c478bd9Sstevel@tonic-gate 					goto out;
12657c478bd9Sstevel@tonic-gate 				}
12667c478bd9Sstevel@tonic-gate 			}
12677c478bd9Sstevel@tonic-gate 		}
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 		/*
12707c478bd9Sstevel@tonic-gate 		 * If the file is the root of a VFS, we've crossed a
12717c478bd9Sstevel@tonic-gate 		 * mount point and the "containing" directory that we
12727c478bd9Sstevel@tonic-gate 		 * acquired above (dvp) is irrelevant because it's in
12737c478bd9Sstevel@tonic-gate 		 * a different file system.  We apply VOP_CREATE to the
12747c478bd9Sstevel@tonic-gate 		 * target itself instead of to the containing directory
12757c478bd9Sstevel@tonic-gate 		 * and supply a null path name to indicate (conventionally)
12767c478bd9Sstevel@tonic-gate 		 * the node itself as the "component" of interest.
12777c478bd9Sstevel@tonic-gate 		 *
12787c478bd9Sstevel@tonic-gate 		 * The intercession of the file system is necessary to
12797c478bd9Sstevel@tonic-gate 		 * ensure that the appropriate permission checks are
12807c478bd9Sstevel@tonic-gate 		 * done.
12817c478bd9Sstevel@tonic-gate 		 */
12827c478bd9Sstevel@tonic-gate 		if (vp->v_flag & VROOT) {
12837c478bd9Sstevel@tonic-gate 			ASSERT(why != CRMKDIR);
12847c478bd9Sstevel@tonic-gate 			error =
12857c478bd9Sstevel@tonic-gate 			    VOP_CREATE(vp, "", vap, excl, mode, vpp, CRED(),
12867c478bd9Sstevel@tonic-gate 			    flag);
12877c478bd9Sstevel@tonic-gate 			/*
12887c478bd9Sstevel@tonic-gate 			 * If the create succeeded, it will have created
12897c478bd9Sstevel@tonic-gate 			 * a new reference to the vnode.  Give up the
12907c478bd9Sstevel@tonic-gate 			 * original reference.  The assertion should not
12917c478bd9Sstevel@tonic-gate 			 * get triggered because NBMAND locks only apply to
12927c478bd9Sstevel@tonic-gate 			 * VREG files.  And if in_crit is non-zero for some
12937c478bd9Sstevel@tonic-gate 			 * reason, detect that here, rather than when we
12947c478bd9Sstevel@tonic-gate 			 * deference a null vp.
12957c478bd9Sstevel@tonic-gate 			 */
12967c478bd9Sstevel@tonic-gate 			ASSERT(in_crit == 0);
12977c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
12987c478bd9Sstevel@tonic-gate 			vp = NULL;
12997c478bd9Sstevel@tonic-gate 			goto out;
13007c478bd9Sstevel@tonic-gate 		}
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 		/*
13037c478bd9Sstevel@tonic-gate 		 * Large File API - non-large open (FOFFMAX flag not set)
13047c478bd9Sstevel@tonic-gate 		 * of regular file fails if the file size exceeds MAXOFF32_T.
13057c478bd9Sstevel@tonic-gate 		 */
13067c478bd9Sstevel@tonic-gate 		if (why != CRMKDIR &&
13077c478bd9Sstevel@tonic-gate 		    !(flag & FOFFMAX) &&
13087c478bd9Sstevel@tonic-gate 		    (vp->v_type == VREG)) {
13097c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
13107c478bd9Sstevel@tonic-gate 			if ((error = VOP_GETATTR(vp, &vattr, 0, CRED()))) {
13117c478bd9Sstevel@tonic-gate 				goto out;
13127c478bd9Sstevel@tonic-gate 			}
13137c478bd9Sstevel@tonic-gate 			if ((vattr.va_size > (u_offset_t)MAXOFF32_T)) {
13147c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
13157c478bd9Sstevel@tonic-gate 				goto out;
13167c478bd9Sstevel@tonic-gate 			}
13177c478bd9Sstevel@tonic-gate 		}
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	if (error == 0) {
13217c478bd9Sstevel@tonic-gate 		/*
13227c478bd9Sstevel@tonic-gate 		 * Call mkdir() if specified, otherwise create().
13237c478bd9Sstevel@tonic-gate 		 */
13247c478bd9Sstevel@tonic-gate 		int must_be_dir = pn_fixslash(&pn);	/* trailing '/'? */
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 		if (why == CRMKDIR)
13277c478bd9Sstevel@tonic-gate 			error = VOP_MKDIR(dvp, pn.pn_path, vap, vpp, CRED());
13287c478bd9Sstevel@tonic-gate 		else if (!must_be_dir)
13297c478bd9Sstevel@tonic-gate 			error = VOP_CREATE(dvp, pn.pn_path, vap,
13307c478bd9Sstevel@tonic-gate 			    excl, mode, vpp, CRED(), flag);
13317c478bd9Sstevel@tonic-gate 		else
13327c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
13337c478bd9Sstevel@tonic-gate 	}
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate out:
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
13387c478bd9Sstevel@tonic-gate 	if (audit_active)
13397c478bd9Sstevel@tonic-gate 		audit_vncreate_finish(*vpp, error);
13407c478bd9Sstevel@tonic-gate #endif  /* C2_AUDIT */
13417c478bd9Sstevel@tonic-gate 	if (in_crit) {
13427c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
13437c478bd9Sstevel@tonic-gate 		in_crit = 0;
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 	if (vp != NULL) {
13467c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
13477c478bd9Sstevel@tonic-gate 		vp = NULL;
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 	pn_free(&pn);
13507c478bd9Sstevel@tonic-gate 	VN_RELE(dvp);
13517c478bd9Sstevel@tonic-gate 	/*
13527c478bd9Sstevel@tonic-gate 	 * The following clause was added to handle a problem
13537c478bd9Sstevel@tonic-gate 	 * with NFS consistency.  It is possible that a lookup
13547c478bd9Sstevel@tonic-gate 	 * of the file to be created succeeded, but the file
13557c478bd9Sstevel@tonic-gate 	 * itself doesn't actually exist on the server.  This
13567c478bd9Sstevel@tonic-gate 	 * is chiefly due to the DNLC containing an entry for
13577c478bd9Sstevel@tonic-gate 	 * the file which has been removed on the server.  In
13587c478bd9Sstevel@tonic-gate 	 * this case, we just start over.  If there was some
13597c478bd9Sstevel@tonic-gate 	 * other cause for the ESTALE error, then the lookup
13607c478bd9Sstevel@tonic-gate 	 * of the file will fail and the error will be returned
13617c478bd9Sstevel@tonic-gate 	 * above instead of looping around from here.
13627c478bd9Sstevel@tonic-gate 	 */
1363dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
13647c478bd9Sstevel@tonic-gate 		goto top;
13657c478bd9Sstevel@tonic-gate 	return (error);
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate int
13697c478bd9Sstevel@tonic-gate vn_link(char *from, char *to, enum uio_seg seg)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate 	struct vnode *fvp;		/* from vnode ptr */
13727c478bd9Sstevel@tonic-gate 	struct vnode *tdvp;		/* to directory vnode ptr */
13737c478bd9Sstevel@tonic-gate 	struct pathname pn;
13747c478bd9Sstevel@tonic-gate 	int error;
13757c478bd9Sstevel@tonic-gate 	struct vattr vattr;
13767c478bd9Sstevel@tonic-gate 	dev_t fsid;
1377dd29fa4aSprabahar 	int estale_retry = 0;
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate top:
13807c478bd9Sstevel@tonic-gate 	fvp = tdvp = NULL;
13817c478bd9Sstevel@tonic-gate 	if (error = pn_get(to, seg, &pn))
13827c478bd9Sstevel@tonic-gate 		return (error);
13837c478bd9Sstevel@tonic-gate 	if (error = lookupname(from, seg, NO_FOLLOW, NULLVPP, &fvp))
13847c478bd9Sstevel@tonic-gate 		goto out;
13857c478bd9Sstevel@tonic-gate 	if (error = lookuppn(&pn, NULL, NO_FOLLOW, &tdvp, NULLVPP))
13867c478bd9Sstevel@tonic-gate 		goto out;
13877c478bd9Sstevel@tonic-gate 	/*
13887c478bd9Sstevel@tonic-gate 	 * Make sure both source vnode and target directory vnode are
13897c478bd9Sstevel@tonic-gate 	 * in the same vfs and that it is writeable.
13907c478bd9Sstevel@tonic-gate 	 */
13917c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_FSID;
13927c478bd9Sstevel@tonic-gate 	if (error = VOP_GETATTR(fvp, &vattr, 0, CRED()))
13937c478bd9Sstevel@tonic-gate 		goto out;
13947c478bd9Sstevel@tonic-gate 	fsid = vattr.va_fsid;
13957c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_FSID;
13967c478bd9Sstevel@tonic-gate 	if (error = VOP_GETATTR(tdvp, &vattr, 0, CRED()))
13977c478bd9Sstevel@tonic-gate 		goto out;
13987c478bd9Sstevel@tonic-gate 	if (fsid != vattr.va_fsid) {
13997c478bd9Sstevel@tonic-gate 		error = EXDEV;
14007c478bd9Sstevel@tonic-gate 		goto out;
14017c478bd9Sstevel@tonic-gate 	}
14027c478bd9Sstevel@tonic-gate 	if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) {
14037c478bd9Sstevel@tonic-gate 		error = EROFS;
14047c478bd9Sstevel@tonic-gate 		goto out;
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 	/*
14077c478bd9Sstevel@tonic-gate 	 * Do the link.
14087c478bd9Sstevel@tonic-gate 	 */
14097c478bd9Sstevel@tonic-gate 	(void) pn_fixslash(&pn);
14107c478bd9Sstevel@tonic-gate 	error = VOP_LINK(tdvp, fvp, pn.pn_path, CRED());
14117c478bd9Sstevel@tonic-gate out:
14127c478bd9Sstevel@tonic-gate 	pn_free(&pn);
14137c478bd9Sstevel@tonic-gate 	if (fvp)
14147c478bd9Sstevel@tonic-gate 		VN_RELE(fvp);
14157c478bd9Sstevel@tonic-gate 	if (tdvp)
14167c478bd9Sstevel@tonic-gate 		VN_RELE(tdvp);
1417dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
14187c478bd9Sstevel@tonic-gate 		goto top;
14197c478bd9Sstevel@tonic-gate 	return (error);
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate int
14237c478bd9Sstevel@tonic-gate vn_rename(char *from, char *to, enum uio_seg seg)
14247c478bd9Sstevel@tonic-gate {
14257c478bd9Sstevel@tonic-gate 	return (vn_renameat(NULL, from, NULL, to, seg));
14267c478bd9Sstevel@tonic-gate }
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate int
14297c478bd9Sstevel@tonic-gate vn_renameat(vnode_t *fdvp, char *fname, vnode_t *tdvp,
14307c478bd9Sstevel@tonic-gate 		char *tname, enum uio_seg seg)
14317c478bd9Sstevel@tonic-gate {
14327c478bd9Sstevel@tonic-gate 	int error;
14337c478bd9Sstevel@tonic-gate 	struct vattr vattr;
14347c478bd9Sstevel@tonic-gate 	struct pathname fpn;		/* from pathname */
14357c478bd9Sstevel@tonic-gate 	struct pathname tpn;		/* to pathname */
14367c478bd9Sstevel@tonic-gate 	dev_t fsid;
14377c478bd9Sstevel@tonic-gate 	int in_crit = 0;
14387c478bd9Sstevel@tonic-gate 	vnode_t *fromvp, *fvp;
14397c478bd9Sstevel@tonic-gate 	vnode_t *tovp;
1440dd29fa4aSprabahar 	int estale_retry = 0;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate top:
14437c478bd9Sstevel@tonic-gate 	fvp = fromvp = tovp = NULL;
14447c478bd9Sstevel@tonic-gate 	/*
14457c478bd9Sstevel@tonic-gate 	 * Get to and from pathnames.
14467c478bd9Sstevel@tonic-gate 	 */
14477c478bd9Sstevel@tonic-gate 	if (error = pn_get(fname, seg, &fpn))
14487c478bd9Sstevel@tonic-gate 		return (error);
14497c478bd9Sstevel@tonic-gate 	if (error = pn_get(tname, seg, &tpn)) {
14507c478bd9Sstevel@tonic-gate 		pn_free(&fpn);
14517c478bd9Sstevel@tonic-gate 		return (error);
14527c478bd9Sstevel@tonic-gate 	}
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	/*
14557c478bd9Sstevel@tonic-gate 	 * First we need to resolve the correct directories
14567c478bd9Sstevel@tonic-gate 	 * The passed in directories may only be a starting point,
14577c478bd9Sstevel@tonic-gate 	 * but we need the real directories the file(s) live in.
14587c478bd9Sstevel@tonic-gate 	 * For example the fname may be something like usr/lib/sparc
14597c478bd9Sstevel@tonic-gate 	 * and we were passed in the / directory, but we need to
14607c478bd9Sstevel@tonic-gate 	 * use the lib directory for the rename.
14617c478bd9Sstevel@tonic-gate 	 */
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate #ifdef  C2_AUDIT
14647c478bd9Sstevel@tonic-gate 	if (audit_active)
14657c478bd9Sstevel@tonic-gate 		audit_setfsat_path(1);
14667c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
14677c478bd9Sstevel@tonic-gate 	/*
14687c478bd9Sstevel@tonic-gate 	 * Lookup to and from directories.
14697c478bd9Sstevel@tonic-gate 	 */
14707c478bd9Sstevel@tonic-gate 	if (error = lookuppnat(&fpn, NULL, NO_FOLLOW, &fromvp, &fvp, fdvp)) {
14717c478bd9Sstevel@tonic-gate 		goto out;
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	/*
14757c478bd9Sstevel@tonic-gate 	 * Make sure there is an entry.
14767c478bd9Sstevel@tonic-gate 	 */
14777c478bd9Sstevel@tonic-gate 	if (fvp == NULL) {
14787c478bd9Sstevel@tonic-gate 		error = ENOENT;
14797c478bd9Sstevel@tonic-gate 		goto out;
14807c478bd9Sstevel@tonic-gate 	}
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate #ifdef  C2_AUDIT
14837c478bd9Sstevel@tonic-gate 	if (audit_active)
14847c478bd9Sstevel@tonic-gate 		audit_setfsat_path(3);
14857c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
14867c478bd9Sstevel@tonic-gate 	if (error = lookuppnat(&tpn, NULL, NO_FOLLOW, &tovp, NULLVPP, tdvp)) {
14877c478bd9Sstevel@tonic-gate 		goto out;
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	/*
14917c478bd9Sstevel@tonic-gate 	 * Make sure both the from vnode directory and the to directory
14927c478bd9Sstevel@tonic-gate 	 * are in the same vfs and the to directory is writable.
14937c478bd9Sstevel@tonic-gate 	 * We check fsid's, not vfs pointers, so loopback fs works.
14947c478bd9Sstevel@tonic-gate 	 */
14957c478bd9Sstevel@tonic-gate 	if (fromvp != tovp) {
14967c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_FSID;
14977c478bd9Sstevel@tonic-gate 		if (error = VOP_GETATTR(fromvp, &vattr, 0, CRED()))
14987c478bd9Sstevel@tonic-gate 			goto out;
14997c478bd9Sstevel@tonic-gate 		fsid = vattr.va_fsid;
15007c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_FSID;
15017c478bd9Sstevel@tonic-gate 		if (error = VOP_GETATTR(tovp, &vattr, 0, CRED()))
15027c478bd9Sstevel@tonic-gate 			goto out;
15037c478bd9Sstevel@tonic-gate 		if (fsid != vattr.va_fsid) {
15047c478bd9Sstevel@tonic-gate 			error = EXDEV;
15057c478bd9Sstevel@tonic-gate 			goto out;
15067c478bd9Sstevel@tonic-gate 		}
15077c478bd9Sstevel@tonic-gate 	}
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	if (tovp->v_vfsp->vfs_flag & VFS_RDONLY) {
15107c478bd9Sstevel@tonic-gate 		error = EROFS;
15117c478bd9Sstevel@tonic-gate 		goto out;
15127c478bd9Sstevel@tonic-gate 	}
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	if (nbl_need_check(fvp)) {
15157c478bd9Sstevel@tonic-gate 		nbl_start_crit(fvp, RW_READER);
15167c478bd9Sstevel@tonic-gate 		in_crit = 1;
15177c478bd9Sstevel@tonic-gate 		if (nbl_conflict(fvp, NBL_RENAME, 0, 0, 0)) {
15187c478bd9Sstevel@tonic-gate 			error = EACCES;
15197c478bd9Sstevel@tonic-gate 			goto out;
15207c478bd9Sstevel@tonic-gate 		}
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	/*
15247c478bd9Sstevel@tonic-gate 	 * Do the rename.
15257c478bd9Sstevel@tonic-gate 	 */
15267c478bd9Sstevel@tonic-gate 	(void) pn_fixslash(&tpn);
15277c478bd9Sstevel@tonic-gate 	error = VOP_RENAME(fromvp, fpn.pn_path, tovp, tpn.pn_path, CRED());
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate out:
15307c478bd9Sstevel@tonic-gate 	pn_free(&fpn);
15317c478bd9Sstevel@tonic-gate 	pn_free(&tpn);
15327c478bd9Sstevel@tonic-gate 	if (in_crit) {
15337c478bd9Sstevel@tonic-gate 		nbl_end_crit(fvp);
15347c478bd9Sstevel@tonic-gate 		in_crit = 0;
15357c478bd9Sstevel@tonic-gate 	}
15367c478bd9Sstevel@tonic-gate 	if (fromvp)
15377c478bd9Sstevel@tonic-gate 		VN_RELE(fromvp);
15387c478bd9Sstevel@tonic-gate 	if (tovp)
15397c478bd9Sstevel@tonic-gate 		VN_RELE(tovp);
15407c478bd9Sstevel@tonic-gate 	if (fvp)
15417c478bd9Sstevel@tonic-gate 		VN_RELE(fvp);
1542dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
15437c478bd9Sstevel@tonic-gate 		goto top;
15447c478bd9Sstevel@tonic-gate 	return (error);
15457c478bd9Sstevel@tonic-gate }
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate /*
15487c478bd9Sstevel@tonic-gate  * Remove a file or directory.
15497c478bd9Sstevel@tonic-gate  */
15507c478bd9Sstevel@tonic-gate int
15517c478bd9Sstevel@tonic-gate vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
15527c478bd9Sstevel@tonic-gate {
15537c478bd9Sstevel@tonic-gate 	return (vn_removeat(NULL, fnamep, seg, dirflag));
15547c478bd9Sstevel@tonic-gate }
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate int
15577c478bd9Sstevel@tonic-gate vn_removeat(vnode_t *startvp, char *fnamep, enum uio_seg seg, enum rm dirflag)
15587c478bd9Sstevel@tonic-gate {
15597c478bd9Sstevel@tonic-gate 	struct vnode *vp;		/* entry vnode */
15607c478bd9Sstevel@tonic-gate 	struct vnode *dvp;		/* ptr to parent dir vnode */
15617c478bd9Sstevel@tonic-gate 	struct vnode *coveredvp;
15627c478bd9Sstevel@tonic-gate 	struct pathname pn;		/* name of entry */
15637c478bd9Sstevel@tonic-gate 	enum vtype vtype;
15647c478bd9Sstevel@tonic-gate 	int error;
15657c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
15667c478bd9Sstevel@tonic-gate 	struct vfs *dvfsp;	/* ptr to parent dir vfs */
15677c478bd9Sstevel@tonic-gate 	int in_crit = 0;
1568dd29fa4aSprabahar 	int estale_retry = 0;
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate top:
15717c478bd9Sstevel@tonic-gate 	if (error = pn_get(fnamep, seg, &pn))
15727c478bd9Sstevel@tonic-gate 		return (error);
15737c478bd9Sstevel@tonic-gate 	dvp = vp = NULL;
15747c478bd9Sstevel@tonic-gate 	if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &dvp, &vp, startvp)) {
15757c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1576dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
15777c478bd9Sstevel@tonic-gate 			goto top;
15787c478bd9Sstevel@tonic-gate 		return (error);
15797c478bd9Sstevel@tonic-gate 	}
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	/*
15827c478bd9Sstevel@tonic-gate 	 * Make sure there is an entry.
15837c478bd9Sstevel@tonic-gate 	 */
15847c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
15857c478bd9Sstevel@tonic-gate 		error = ENOENT;
15867c478bd9Sstevel@tonic-gate 		goto out;
15877c478bd9Sstevel@tonic-gate 	}
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	vfsp = vp->v_vfsp;
15907c478bd9Sstevel@tonic-gate 	dvfsp = dvp->v_vfsp;
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	/*
15937c478bd9Sstevel@tonic-gate 	 * If the named file is the root of a mounted filesystem, fail,
15947c478bd9Sstevel@tonic-gate 	 * unless it's marked unlinkable.  In that case, unmount the
15957c478bd9Sstevel@tonic-gate 	 * filesystem and proceed to unlink the covered vnode.  (If the
15967c478bd9Sstevel@tonic-gate 	 * covered vnode is a directory, use rmdir instead of unlink,
15977c478bd9Sstevel@tonic-gate 	 * to avoid file system corruption.)
15987c478bd9Sstevel@tonic-gate 	 */
15997c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VROOT) {
1600b9c98e29Spf199842 		if ((vfsp->vfs_flag & VFS_UNLINKABLE) == 0) {
1601b9c98e29Spf199842 			error = EBUSY;
1602b9c98e29Spf199842 			goto out;
1603b9c98e29Spf199842 		}
1604b9c98e29Spf199842 
1605b9c98e29Spf199842 		/*
1606b9c98e29Spf199842 		 * Namefs specific code starts here.
1607b9c98e29Spf199842 		 */
1608b9c98e29Spf199842 
16097c478bd9Sstevel@tonic-gate 		if (dirflag == RMDIRECTORY) {
16107c478bd9Sstevel@tonic-gate 			/*
16117c478bd9Sstevel@tonic-gate 			 * User called rmdir(2) on a file that has
16127c478bd9Sstevel@tonic-gate 			 * been namefs mounted on top of.  Since
16137c478bd9Sstevel@tonic-gate 			 * namefs doesn't allow directories to
16147c478bd9Sstevel@tonic-gate 			 * be mounted on other files we know
16157c478bd9Sstevel@tonic-gate 			 * vp is not of type VDIR so fail to operation.
16167c478bd9Sstevel@tonic-gate 			 */
16177c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
16187c478bd9Sstevel@tonic-gate 			goto out;
16197c478bd9Sstevel@tonic-gate 		}
1620b9c98e29Spf199842 
1621b9c98e29Spf199842 		/*
1622b9c98e29Spf199842 		 * If VROOT is still set after grabbing vp->v_lock,
1623b9c98e29Spf199842 		 * noone has finished nm_unmount so far and coveredvp
1624b9c98e29Spf199842 		 * is valid.
1625b9c98e29Spf199842 		 * If we manage to grab vn_vfswlock(coveredvp) before releasing
1626b9c98e29Spf199842 		 * vp->v_lock, any race window is eliminated.
1627b9c98e29Spf199842 		 */
1628b9c98e29Spf199842 
1629b9c98e29Spf199842 		mutex_enter(&vp->v_lock);
1630b9c98e29Spf199842 		if ((vp->v_flag & VROOT) == 0) {
1631b9c98e29Spf199842 			/* Someone beat us to the unmount */
1632b9c98e29Spf199842 			mutex_exit(&vp->v_lock);
1633b9c98e29Spf199842 			error = EBUSY;
1634b9c98e29Spf199842 			goto out;
1635b9c98e29Spf199842 		}
1636b9c98e29Spf199842 		vfsp = vp->v_vfsp;
16377c478bd9Sstevel@tonic-gate 		coveredvp = vfsp->vfs_vnodecovered;
1638b9c98e29Spf199842 		ASSERT(coveredvp);
1639b9c98e29Spf199842 		/*
1640b9c98e29Spf199842 		 * Note: Implementation of vn_vfswlock shows that ordering of
1641b9c98e29Spf199842 		 * v_lock / vn_vfswlock is not an issue here.
1642b9c98e29Spf199842 		 */
1643b9c98e29Spf199842 		error = vn_vfswlock(coveredvp);
1644b9c98e29Spf199842 		mutex_exit(&vp->v_lock);
1645b9c98e29Spf199842 
1646b9c98e29Spf199842 		if (error)
1647b9c98e29Spf199842 			goto out;
1648b9c98e29Spf199842 
16497c478bd9Sstevel@tonic-gate 		VN_HOLD(coveredvp);
16507c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
16517c478bd9Sstevel@tonic-gate 		error = dounmount(vfsp, 0, CRED());
1652b9c98e29Spf199842 
16537c478bd9Sstevel@tonic-gate 		/*
16547c478bd9Sstevel@tonic-gate 		 * Unmounted the namefs file system; now get
16557c478bd9Sstevel@tonic-gate 		 * the object it was mounted over.
16567c478bd9Sstevel@tonic-gate 		 */
16577c478bd9Sstevel@tonic-gate 		vp = coveredvp;
16587c478bd9Sstevel@tonic-gate 		/*
16597c478bd9Sstevel@tonic-gate 		 * If namefs was mounted over a directory, then
16607c478bd9Sstevel@tonic-gate 		 * we want to use rmdir() instead of unlink().
16617c478bd9Sstevel@tonic-gate 		 */
16627c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR)
16637c478bd9Sstevel@tonic-gate 			dirflag = RMDIRECTORY;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 		if (error)
16667c478bd9Sstevel@tonic-gate 			goto out;
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	/*
16707c478bd9Sstevel@tonic-gate 	 * Make sure filesystem is writeable.
16717c478bd9Sstevel@tonic-gate 	 * We check the parent directory's vfs in case this is an lofs vnode.
16727c478bd9Sstevel@tonic-gate 	 */
16737c478bd9Sstevel@tonic-gate 	if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) {
16747c478bd9Sstevel@tonic-gate 		error = EROFS;
16757c478bd9Sstevel@tonic-gate 		goto out;
16767c478bd9Sstevel@tonic-gate 	}
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	vtype = vp->v_type;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	/*
16817c478bd9Sstevel@tonic-gate 	 * If there is the possibility of an nbmand share reservation, make
16827c478bd9Sstevel@tonic-gate 	 * sure it's okay to remove the file.  Keep a reference to the
16837c478bd9Sstevel@tonic-gate 	 * vnode, so that we can exit the nbl critical region after
16847c478bd9Sstevel@tonic-gate 	 * calling VOP_REMOVE.
16857c478bd9Sstevel@tonic-gate 	 * If there is no possibility of an nbmand share reservation,
16867c478bd9Sstevel@tonic-gate 	 * release the vnode reference now.  Filesystems like NFS may
16877c478bd9Sstevel@tonic-gate 	 * behave differently if there is an extra reference, so get rid of
16887c478bd9Sstevel@tonic-gate 	 * this one.  Fortunately, we can't have nbmand mounts on NFS
16897c478bd9Sstevel@tonic-gate 	 * filesystems.
16907c478bd9Sstevel@tonic-gate 	 */
16917c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
16927c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
16937c478bd9Sstevel@tonic-gate 		in_crit = 1;
16947c478bd9Sstevel@tonic-gate 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0)) {
16957c478bd9Sstevel@tonic-gate 			error = EACCES;
16967c478bd9Sstevel@tonic-gate 			goto out;
16977c478bd9Sstevel@tonic-gate 		}
16987c478bd9Sstevel@tonic-gate 	} else {
16997c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
17007c478bd9Sstevel@tonic-gate 		vp = NULL;
17017c478bd9Sstevel@tonic-gate 	}
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	if (dirflag == RMDIRECTORY) {
17047c478bd9Sstevel@tonic-gate 		/*
17057c478bd9Sstevel@tonic-gate 		 * Caller is using rmdir(2), which can only be applied to
17067c478bd9Sstevel@tonic-gate 		 * directories.
17077c478bd9Sstevel@tonic-gate 		 */
17087c478bd9Sstevel@tonic-gate 		if (vtype != VDIR) {
17097c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
17107c478bd9Sstevel@tonic-gate 		} else {
17117c478bd9Sstevel@tonic-gate 			vnode_t *cwd;
17127c478bd9Sstevel@tonic-gate 			proc_t *pp = curproc;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
17157c478bd9Sstevel@tonic-gate 			cwd = PTOU(pp)->u_cdir;
17167c478bd9Sstevel@tonic-gate 			VN_HOLD(cwd);
17177c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
17187c478bd9Sstevel@tonic-gate 			error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED());
17197c478bd9Sstevel@tonic-gate 			VN_RELE(cwd);
17207c478bd9Sstevel@tonic-gate 		}
17217c478bd9Sstevel@tonic-gate 	} else {
17227c478bd9Sstevel@tonic-gate 		/*
17237c478bd9Sstevel@tonic-gate 		 * Unlink(2) can be applied to anything.
17247c478bd9Sstevel@tonic-gate 		 */
17257c478bd9Sstevel@tonic-gate 		error = VOP_REMOVE(dvp, pn.pn_path, CRED());
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate out:
17297c478bd9Sstevel@tonic-gate 	pn_free(&pn);
17307c478bd9Sstevel@tonic-gate 	if (in_crit) {
17317c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
17327c478bd9Sstevel@tonic-gate 		in_crit = 0;
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 	if (vp != NULL)
17357c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
17367c478bd9Sstevel@tonic-gate 	if (dvp != NULL)
17377c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
1738dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
17397c478bd9Sstevel@tonic-gate 		goto top;
17407c478bd9Sstevel@tonic-gate 	return (error);
17417c478bd9Sstevel@tonic-gate }
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate /*
17447c478bd9Sstevel@tonic-gate  * Utility function to compare equality of vnodes.
17457c478bd9Sstevel@tonic-gate  * Compare the underlying real vnodes, if there are underlying vnodes.
17467c478bd9Sstevel@tonic-gate  * This is a more thorough comparison than the VN_CMP() macro provides.
17477c478bd9Sstevel@tonic-gate  */
17487c478bd9Sstevel@tonic-gate int
17497c478bd9Sstevel@tonic-gate vn_compare(vnode_t *vp1, vnode_t *vp2)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	vnode_t *realvp;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	if (vp1 != NULL && VOP_REALVP(vp1, &realvp) == 0)
17547c478bd9Sstevel@tonic-gate 		vp1 = realvp;
17557c478bd9Sstevel@tonic-gate 	if (vp2 != NULL && VOP_REALVP(vp2, &realvp) == 0)
17567c478bd9Sstevel@tonic-gate 		vp2 = realvp;
17577c478bd9Sstevel@tonic-gate 	return (VN_CMP(vp1, vp2));
17587c478bd9Sstevel@tonic-gate }
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate /*
17617c478bd9Sstevel@tonic-gate  * The number of locks to hash into.  This value must be a power
17627c478bd9Sstevel@tonic-gate  * of 2 minus 1 and should probably also be prime.
17637c478bd9Sstevel@tonic-gate  */
17647c478bd9Sstevel@tonic-gate #define	NUM_BUCKETS	1023
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate struct  vn_vfslocks_bucket {
17677c478bd9Sstevel@tonic-gate 	kmutex_t vb_lock;
17687c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vb_list;
17697c478bd9Sstevel@tonic-gate 	char pad[64 - sizeof (kmutex_t) - sizeof (void *)];
17707c478bd9Sstevel@tonic-gate };
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate /*
17737c478bd9Sstevel@tonic-gate  * Total number of buckets will be NUM_BUCKETS + 1 .
17747c478bd9Sstevel@tonic-gate  */
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate #pragma	align	64(vn_vfslocks_buckets)
17777c478bd9Sstevel@tonic-gate static	struct vn_vfslocks_bucket	vn_vfslocks_buckets[NUM_BUCKETS + 1];
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate #define	VN_VFSLOCKS_SHIFT	9
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate #define	VN_VFSLOCKS_HASH(vfsvpptr)	\
17827c478bd9Sstevel@tonic-gate 	((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS)
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate /*
17857c478bd9Sstevel@tonic-gate  * vn_vfslocks_getlock() uses an HASH scheme to generate
17867c478bd9Sstevel@tonic-gate  * rwstlock using vfs/vnode pointer passed to it.
17877c478bd9Sstevel@tonic-gate  *
17887c478bd9Sstevel@tonic-gate  * vn_vfslocks_rele() releases a reference in the
17897c478bd9Sstevel@tonic-gate  * HASH table which allows the entry allocated by
17907c478bd9Sstevel@tonic-gate  * vn_vfslocks_getlock() to be freed at a later
17917c478bd9Sstevel@tonic-gate  * stage when the refcount drops to zero.
17927c478bd9Sstevel@tonic-gate  */
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate vn_vfslocks_entry_t *
17957c478bd9Sstevel@tonic-gate vn_vfslocks_getlock(void *vfsvpptr)
17967c478bd9Sstevel@tonic-gate {
17977c478bd9Sstevel@tonic-gate 	struct vn_vfslocks_bucket *bp;
17987c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vep;
17997c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *tvep;
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	ASSERT(vfsvpptr != NULL);
18027c478bd9Sstevel@tonic-gate 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)];
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
18057c478bd9Sstevel@tonic-gate 	for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
18067c478bd9Sstevel@tonic-gate 		if (vep->ve_vpvfs == vfsvpptr) {
18077c478bd9Sstevel@tonic-gate 			vep->ve_refcnt++;
18087c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->vb_lock);
18097c478bd9Sstevel@tonic-gate 			return (vep);
18107c478bd9Sstevel@tonic-gate 		}
18117c478bd9Sstevel@tonic-gate 	}
18127c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
18137c478bd9Sstevel@tonic-gate 	vep = kmem_alloc(sizeof (*vep), KM_SLEEP);
18147c478bd9Sstevel@tonic-gate 	rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL);
18157c478bd9Sstevel@tonic-gate 	vep->ve_vpvfs = (char *)vfsvpptr;
18167c478bd9Sstevel@tonic-gate 	vep->ve_refcnt = 1;
18177c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
18187c478bd9Sstevel@tonic-gate 	for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) {
18197c478bd9Sstevel@tonic-gate 		if (tvep->ve_vpvfs == vfsvpptr) {
18207c478bd9Sstevel@tonic-gate 			tvep->ve_refcnt++;
18217c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->vb_lock);
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 			/*
18247c478bd9Sstevel@tonic-gate 			 * There is already an entry in the hash
18257c478bd9Sstevel@tonic-gate 			 * destroy what we just allocated.
18267c478bd9Sstevel@tonic-gate 			 */
18277c478bd9Sstevel@tonic-gate 			rwst_destroy(&vep->ve_lock);
18287c478bd9Sstevel@tonic-gate 			kmem_free(vep, sizeof (*vep));
18297c478bd9Sstevel@tonic-gate 			return (tvep);
18307c478bd9Sstevel@tonic-gate 		}
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 	vep->ve_next = bp->vb_list;
18337c478bd9Sstevel@tonic-gate 	bp->vb_list = vep;
18347c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
18357c478bd9Sstevel@tonic-gate 	return (vep);
18367c478bd9Sstevel@tonic-gate }
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate void
18397c478bd9Sstevel@tonic-gate vn_vfslocks_rele(vn_vfslocks_entry_t *vepent)
18407c478bd9Sstevel@tonic-gate {
18417c478bd9Sstevel@tonic-gate 	struct vn_vfslocks_bucket *bp;
18427c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vep;
18437c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *pvep;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	ASSERT(vepent != NULL);
18467c478bd9Sstevel@tonic-gate 	ASSERT(vepent->ve_vpvfs != NULL);
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)];
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
18517c478bd9Sstevel@tonic-gate 	vepent->ve_refcnt--;
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if ((int32_t)vepent->ve_refcnt < 0)
18547c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative");
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	if (vepent->ve_refcnt == 0) {
18577c478bd9Sstevel@tonic-gate 		for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
18587c478bd9Sstevel@tonic-gate 			if (vep->ve_vpvfs == vepent->ve_vpvfs) {
18597c478bd9Sstevel@tonic-gate 				if (bp->vb_list == vep)
18607c478bd9Sstevel@tonic-gate 					bp->vb_list = vep->ve_next;
18617c478bd9Sstevel@tonic-gate 				else {
18627c478bd9Sstevel@tonic-gate 					/* LINTED */
18637c478bd9Sstevel@tonic-gate 					pvep->ve_next = vep->ve_next;
18647c478bd9Sstevel@tonic-gate 				}
18657c478bd9Sstevel@tonic-gate 				mutex_exit(&bp->vb_lock);
18667c478bd9Sstevel@tonic-gate 				rwst_destroy(&vep->ve_lock);
18677c478bd9Sstevel@tonic-gate 				kmem_free(vep, sizeof (*vep));
18687c478bd9Sstevel@tonic-gate 				return;
18697c478bd9Sstevel@tonic-gate 			}
18707c478bd9Sstevel@tonic-gate 			pvep = vep;
18717c478bd9Sstevel@tonic-gate 		}
18727c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found");
18737c478bd9Sstevel@tonic-gate 	}
18747c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
18757c478bd9Sstevel@tonic-gate }
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate /*
18787c478bd9Sstevel@tonic-gate  * vn_vfswlock_wait is used to implement a lock which is logically a writers
18797c478bd9Sstevel@tonic-gate  * lock protecting the v_vfsmountedhere field.
18807c478bd9Sstevel@tonic-gate  * vn_vfswlock_wait has been modified to be similar to vn_vfswlock,
18817c478bd9Sstevel@tonic-gate  * except that it blocks to acquire the lock VVFSLOCK.
18827c478bd9Sstevel@tonic-gate  *
18837c478bd9Sstevel@tonic-gate  * traverse() and routines re-implementing part of traverse (e.g. autofs)
18847c478bd9Sstevel@tonic-gate  * need to hold this lock. mount(), vn_rename(), vn_remove() and so on
18857c478bd9Sstevel@tonic-gate  * need the non-blocking version of the writers lock i.e. vn_vfswlock
18867c478bd9Sstevel@tonic-gate  */
18877c478bd9Sstevel@tonic-gate int
18887c478bd9Sstevel@tonic-gate vn_vfswlock_wait(vnode_t *vp)
18897c478bd9Sstevel@tonic-gate {
18907c478bd9Sstevel@tonic-gate 	int retval;
18917c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
18927c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
18957c478bd9Sstevel@tonic-gate 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER);
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	if (retval == EINTR) {
18987c478bd9Sstevel@tonic-gate 		vn_vfslocks_rele(vpvfsentry);
18997c478bd9Sstevel@tonic-gate 		return (EINTR);
19007c478bd9Sstevel@tonic-gate 	}
19017c478bd9Sstevel@tonic-gate 	return (retval);
19027c478bd9Sstevel@tonic-gate }
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate int
19057c478bd9Sstevel@tonic-gate vn_vfsrlock_wait(vnode_t *vp)
19067c478bd9Sstevel@tonic-gate {
19077c478bd9Sstevel@tonic-gate 	int retval;
19087c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
19097c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
19127c478bd9Sstevel@tonic-gate 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER);
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	if (retval == EINTR) {
19157c478bd9Sstevel@tonic-gate 		vn_vfslocks_rele(vpvfsentry);
19167c478bd9Sstevel@tonic-gate 		return (EINTR);
19177c478bd9Sstevel@tonic-gate 	}
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	return (retval);
19207c478bd9Sstevel@tonic-gate }
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate /*
19247c478bd9Sstevel@tonic-gate  * vn_vfswlock is used to implement a lock which is logically a writers lock
19257c478bd9Sstevel@tonic-gate  * protecting the v_vfsmountedhere field.
19267c478bd9Sstevel@tonic-gate  */
19277c478bd9Sstevel@tonic-gate int
19287c478bd9Sstevel@tonic-gate vn_vfswlock(vnode_t *vp)
19297c478bd9Sstevel@tonic-gate {
19307c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	/*
19337c478bd9Sstevel@tonic-gate 	 * If vp is NULL then somebody is trying to lock the covered vnode
19347c478bd9Sstevel@tonic-gate 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
19357c478bd9Sstevel@tonic-gate 	 * only happen when unmounting /.  Since that operation will fail
19367c478bd9Sstevel@tonic-gate 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
19377c478bd9Sstevel@tonic-gate 	 */
19387c478bd9Sstevel@tonic-gate 	if (vp == NULL)
19397c478bd9Sstevel@tonic-gate 		return (EBUSY);
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
19447c478bd9Sstevel@tonic-gate 		return (0);
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
19477c478bd9Sstevel@tonic-gate 	return (EBUSY);
19487c478bd9Sstevel@tonic-gate }
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate int
19517c478bd9Sstevel@tonic-gate vn_vfsrlock(vnode_t *vp)
19527c478bd9Sstevel@tonic-gate {
19537c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	/*
19567c478bd9Sstevel@tonic-gate 	 * If vp is NULL then somebody is trying to lock the covered vnode
19577c478bd9Sstevel@tonic-gate 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
19587c478bd9Sstevel@tonic-gate 	 * only happen when unmounting /.  Since that operation will fail
19597c478bd9Sstevel@tonic-gate 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
19607c478bd9Sstevel@tonic-gate 	 */
19617c478bd9Sstevel@tonic-gate 	if (vp == NULL)
19627c478bd9Sstevel@tonic-gate 		return (EBUSY);
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
19677c478bd9Sstevel@tonic-gate 		return (0);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
19707c478bd9Sstevel@tonic-gate 	return (EBUSY);
19717c478bd9Sstevel@tonic-gate }
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate void
19747c478bd9Sstevel@tonic-gate vn_vfsunlock(vnode_t *vp)
19757c478bd9Sstevel@tonic-gate {
19767c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 	/*
19797c478bd9Sstevel@tonic-gate 	 * ve_refcnt needs to be decremented twice.
19807c478bd9Sstevel@tonic-gate 	 * 1. To release refernce after a call to vn_vfslocks_getlock()
19817c478bd9Sstevel@tonic-gate 	 * 2. To release the reference from the locking routines like
19827c478bd9Sstevel@tonic-gate 	 *    vn_vfsrlock/vn_vfswlock etc,.
19837c478bd9Sstevel@tonic-gate 	 */
19847c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
19857c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	rwst_exit(&vpvfsentry->ve_lock);
19887c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
19897c478bd9Sstevel@tonic-gate }
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate int
19927c478bd9Sstevel@tonic-gate vn_vfswlock_held(vnode_t *vp)
19937c478bd9Sstevel@tonic-gate {
19947c478bd9Sstevel@tonic-gate 	int held;
19957c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
20007c478bd9Sstevel@tonic-gate 	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
20037c478bd9Sstevel@tonic-gate 	return (held);
20047c478bd9Sstevel@tonic-gate }
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate int
20087c478bd9Sstevel@tonic-gate vn_make_ops(
20097c478bd9Sstevel@tonic-gate 	const char *name,			/* Name of file system */
20107c478bd9Sstevel@tonic-gate 	const fs_operation_def_t *templ,	/* Operation specification */
20117c478bd9Sstevel@tonic-gate 	vnodeops_t **actual)			/* Return the vnodeops */
20127c478bd9Sstevel@tonic-gate {
20137c478bd9Sstevel@tonic-gate 	int unused_ops;
20147c478bd9Sstevel@tonic-gate 	int error;
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	*actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP);
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	(*actual)->vnop_name = name;
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ);
20217c478bd9Sstevel@tonic-gate 	if (error) {
20227c478bd9Sstevel@tonic-gate 		kmem_free(*actual, sizeof (vnodeops_t));
20237c478bd9Sstevel@tonic-gate 	}
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate #if DEBUG
20267c478bd9Sstevel@tonic-gate 	if (unused_ops != 0)
20277c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied "
20287c478bd9Sstevel@tonic-gate 		    "but not used", name, unused_ops);
20297c478bd9Sstevel@tonic-gate #endif
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	return (error);
20327c478bd9Sstevel@tonic-gate }
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate /*
20357c478bd9Sstevel@tonic-gate  * Free the vnodeops created as a result of vn_make_ops()
20367c478bd9Sstevel@tonic-gate  */
20377c478bd9Sstevel@tonic-gate void
20387c478bd9Sstevel@tonic-gate vn_freevnodeops(vnodeops_t *vnops)
20397c478bd9Sstevel@tonic-gate {
20407c478bd9Sstevel@tonic-gate 	kmem_free(vnops, sizeof (vnodeops_t));
20417c478bd9Sstevel@tonic-gate }
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate /*
20447c478bd9Sstevel@tonic-gate  * Vnode cache.
20457c478bd9Sstevel@tonic-gate  */
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate /* ARGSUSED */
20487c478bd9Sstevel@tonic-gate static int
20497c478bd9Sstevel@tonic-gate vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
20507c478bd9Sstevel@tonic-gate {
20517c478bd9Sstevel@tonic-gate 	struct vnode *vp;
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	vp = buf;
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
20567c478bd9Sstevel@tonic-gate 	cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL);
20577c478bd9Sstevel@tonic-gate 	rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL);
20587c478bd9Sstevel@tonic-gate 	rw_init(&vp->v_mslock, NULL, RW_DEFAULT, NULL);
20597c478bd9Sstevel@tonic-gate 	vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
20607c478bd9Sstevel@tonic-gate 	vp->v_path = NULL;
20617c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
2062*1b300de9Sjwahlig 	vp->v_vsd = NULL;
2063df2381bfSpraks 	vp->v_fopdata = NULL;
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	return (0);
20667c478bd9Sstevel@tonic-gate }
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate /* ARGSUSED */
20697c478bd9Sstevel@tonic-gate static void
20707c478bd9Sstevel@tonic-gate vn_cache_destructor(void *buf, void *cdrarg)
20717c478bd9Sstevel@tonic-gate {
20727c478bd9Sstevel@tonic-gate 	struct vnode *vp;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	vp = buf;
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 	rw_destroy(&vp->v_mslock);
20777c478bd9Sstevel@tonic-gate 	rw_destroy(&vp->v_nbllock);
20787c478bd9Sstevel@tonic-gate 	cv_destroy(&vp->v_cv);
20797c478bd9Sstevel@tonic-gate 	mutex_destroy(&vp->v_lock);
20807c478bd9Sstevel@tonic-gate }
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate void
20837c478bd9Sstevel@tonic-gate vn_create_cache(void)
20847c478bd9Sstevel@tonic-gate {
20857c478bd9Sstevel@tonic-gate 	vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode), 64,
20867c478bd9Sstevel@tonic-gate 	    vn_cache_constructor, vn_cache_destructor, NULL, NULL,
20877c478bd9Sstevel@tonic-gate 	    NULL, 0);
20887c478bd9Sstevel@tonic-gate }
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate void
20917c478bd9Sstevel@tonic-gate vn_destroy_cache(void)
20927c478bd9Sstevel@tonic-gate {
20937c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(vn_cache);
20947c478bd9Sstevel@tonic-gate }
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate /*
20977c478bd9Sstevel@tonic-gate  * Used by file systems when fs-specific nodes (e.g., ufs inodes) are
20987c478bd9Sstevel@tonic-gate  * cached by the file system and vnodes remain associated.
20997c478bd9Sstevel@tonic-gate  */
21007c478bd9Sstevel@tonic-gate void
21017c478bd9Sstevel@tonic-gate vn_recycle(vnode_t *vp)
21027c478bd9Sstevel@tonic-gate {
21037c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_pages == NULL);
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	/*
21067c478bd9Sstevel@tonic-gate 	 * XXX - This really belongs in vn_reinit(), but we have some issues
21077c478bd9Sstevel@tonic-gate 	 * with the counts.  Best to have it here for clean initialization.
21087c478bd9Sstevel@tonic-gate 	 */
21097c478bd9Sstevel@tonic-gate 	vp->v_rdcnt = 0;
21107c478bd9Sstevel@tonic-gate 	vp->v_wrcnt = 0;
21117c478bd9Sstevel@tonic-gate 	vp->v_mmap_read = 0;
21127c478bd9Sstevel@tonic-gate 	vp->v_mmap_write = 0;
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 	/*
21157c478bd9Sstevel@tonic-gate 	 * If FEM was in use, make sure everything gets cleaned up
21167c478bd9Sstevel@tonic-gate 	 * NOTE: vp->v_femhead is initialized to NULL in the vnode
21177c478bd9Sstevel@tonic-gate 	 * constructor.
21187c478bd9Sstevel@tonic-gate 	 */
21197c478bd9Sstevel@tonic-gate 	if (vp->v_femhead) {
21207c478bd9Sstevel@tonic-gate 		/* XXX - There should be a free_femhead() that does all this */
21217c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_femhead->femh_list == NULL);
21227c478bd9Sstevel@tonic-gate 		mutex_destroy(&vp->v_femhead->femh_lock);
21237c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
21247c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;
21257c478bd9Sstevel@tonic-gate 	}
21267c478bd9Sstevel@tonic-gate 	if (vp->v_path) {
21277c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
21287c478bd9Sstevel@tonic-gate 		vp->v_path = NULL;
21297c478bd9Sstevel@tonic-gate 	}
2130df2381bfSpraks 
2131df2381bfSpraks 	if (vp->v_fopdata != NULL) {
2132df2381bfSpraks 		free_fopdata(vp);
2133df2381bfSpraks 	}
21347c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
2135*1b300de9Sjwahlig 	vsd_free(vp);
21367c478bd9Sstevel@tonic-gate }
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate /*
21397c478bd9Sstevel@tonic-gate  * Used to reset the vnode fields including those that are directly accessible
21407c478bd9Sstevel@tonic-gate  * as well as those which require an accessor function.
21417c478bd9Sstevel@tonic-gate  *
21427c478bd9Sstevel@tonic-gate  * Does not initialize:
21437c478bd9Sstevel@tonic-gate  *	synchronization objects: v_lock, v_nbllock, v_cv
21447c478bd9Sstevel@tonic-gate  *	v_data (since FS-nodes and vnodes point to each other and should
21457c478bd9Sstevel@tonic-gate  *		be updated simultaneously)
21467c478bd9Sstevel@tonic-gate  *	v_op (in case someone needs to make a VOP call on this object)
21477c478bd9Sstevel@tonic-gate  */
21487c478bd9Sstevel@tonic-gate void
21497c478bd9Sstevel@tonic-gate vn_reinit(vnode_t *vp)
21507c478bd9Sstevel@tonic-gate {
21517c478bd9Sstevel@tonic-gate 	vp->v_count = 1;
21527c478bd9Sstevel@tonic-gate 	vp->v_vfsp = NULL;
21537c478bd9Sstevel@tonic-gate 	vp->v_stream = NULL;
21547c478bd9Sstevel@tonic-gate 	vp->v_vfsmountedhere = NULL;
21557c478bd9Sstevel@tonic-gate 	vp->v_flag = 0;
21567c478bd9Sstevel@tonic-gate 	vp->v_type = VNON;
21577c478bd9Sstevel@tonic-gate 	vp->v_rdev = NODEV;
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	vp->v_filocks = NULL;
21607c478bd9Sstevel@tonic-gate 	vp->v_shrlocks = NULL;
21617c478bd9Sstevel@tonic-gate 	vp->v_pages = NULL;
21627c478bd9Sstevel@tonic-gate 	vp->v_npages = 0;
21637c478bd9Sstevel@tonic-gate 	vp->v_msnpages = 0;
21647c478bd9Sstevel@tonic-gate 	vp->v_scanfront = NULL;
21657c478bd9Sstevel@tonic-gate 	vp->v_scanback = NULL;
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	vp->v_locality = NULL;
21687c478bd9Sstevel@tonic-gate 	vp->v_scantime = 0;
21697c478bd9Sstevel@tonic-gate 	vp->v_mset = 0;
21707c478bd9Sstevel@tonic-gate 	vp->v_msflags = 0;
21717c478bd9Sstevel@tonic-gate 	vp->v_msnext = NULL;
21727c478bd9Sstevel@tonic-gate 	vp->v_msprev = NULL;
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	/* Handles v_femhead, v_path, and the r/w/map counts */
21757c478bd9Sstevel@tonic-gate 	vn_recycle(vp);
21767c478bd9Sstevel@tonic-gate }
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate vnode_t *
21797c478bd9Sstevel@tonic-gate vn_alloc(int kmflag)
21807c478bd9Sstevel@tonic-gate {
21817c478bd9Sstevel@tonic-gate 	vnode_t *vp;
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	vp = kmem_cache_alloc(vn_cache, kmflag);
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 	if (vp != NULL) {
21867c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2187df2381bfSpraks 		vp->v_fopdata = NULL;
21887c478bd9Sstevel@tonic-gate 		vn_reinit(vp);
21897c478bd9Sstevel@tonic-gate 	}
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 	return (vp);
21927c478bd9Sstevel@tonic-gate }
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate void
21957c478bd9Sstevel@tonic-gate vn_free(vnode_t *vp)
21967c478bd9Sstevel@tonic-gate {
21977c478bd9Sstevel@tonic-gate 	/*
21987c478bd9Sstevel@tonic-gate 	 * Some file systems call vn_free() with v_count of zero,
21997c478bd9Sstevel@tonic-gate 	 * some with v_count of 1.  In any case, the value should
22007c478bd9Sstevel@tonic-gate 	 * never be anything else.
22017c478bd9Sstevel@tonic-gate 	 */
22027c478bd9Sstevel@tonic-gate 	ASSERT((vp->v_count == 0) || (vp->v_count == 1));
22037c478bd9Sstevel@tonic-gate 	if (vp->v_path != NULL) {
22047c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
22057c478bd9Sstevel@tonic-gate 		vp->v_path = NULL;
22067c478bd9Sstevel@tonic-gate 	}
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/* If FEM was in use, make sure everything gets cleaned up */
22097c478bd9Sstevel@tonic-gate 	if (vp->v_femhead) {
22107c478bd9Sstevel@tonic-gate 		/* XXX - There should be a free_femhead() that does all this */
22117c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_femhead->femh_list == NULL);
22127c478bd9Sstevel@tonic-gate 		mutex_destroy(&vp->v_femhead->femh_lock);
22137c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
22147c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;
22157c478bd9Sstevel@tonic-gate 	}
2216df2381bfSpraks 
2217df2381bfSpraks 	if (vp->v_fopdata != NULL) {
2218df2381bfSpraks 		free_fopdata(vp);
2219df2381bfSpraks 	}
22207c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
2221*1b300de9Sjwahlig 	vsd_free(vp);
22227c478bd9Sstevel@tonic-gate 	kmem_cache_free(vn_cache, vp);
22237c478bd9Sstevel@tonic-gate }
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate /*
22267c478bd9Sstevel@tonic-gate  * vnode status changes, should define better states than 1, 0.
22277c478bd9Sstevel@tonic-gate  */
22287c478bd9Sstevel@tonic-gate void
22297c478bd9Sstevel@tonic-gate vn_reclaim(vnode_t *vp)
22307c478bd9Sstevel@tonic-gate {
22317c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
22327c478bd9Sstevel@tonic-gate 
2233ddfcde86Srsb 	if (vfsp == NULL ||
2234ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
22357c478bd9Sstevel@tonic-gate 		return;
22367c478bd9Sstevel@tonic-gate 	}
22377c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED);
22387c478bd9Sstevel@tonic-gate }
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate void
22417c478bd9Sstevel@tonic-gate vn_idle(vnode_t *vp)
22427c478bd9Sstevel@tonic-gate {
22437c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
22447c478bd9Sstevel@tonic-gate 
2245ddfcde86Srsb 	if (vfsp == NULL ||
2246ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
22477c478bd9Sstevel@tonic-gate 		return;
22487c478bd9Sstevel@tonic-gate 	}
22497c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_IDLED);
22507c478bd9Sstevel@tonic-gate }
22517c478bd9Sstevel@tonic-gate void
22527c478bd9Sstevel@tonic-gate vn_exists(vnode_t *vp)
22537c478bd9Sstevel@tonic-gate {
22547c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
22557c478bd9Sstevel@tonic-gate 
2256ddfcde86Srsb 	if (vfsp == NULL ||
2257ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
22587c478bd9Sstevel@tonic-gate 		return;
22597c478bd9Sstevel@tonic-gate 	}
22607c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS);
22617c478bd9Sstevel@tonic-gate }
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate void
22647c478bd9Sstevel@tonic-gate vn_invalid(vnode_t *vp)
22657c478bd9Sstevel@tonic-gate {
22667c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
22677c478bd9Sstevel@tonic-gate 
2268ddfcde86Srsb 	if (vfsp == NULL ||
2269ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
22707c478bd9Sstevel@tonic-gate 		return;
22717c478bd9Sstevel@tonic-gate 	}
22727c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED);
22737c478bd9Sstevel@tonic-gate }
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate /* Vnode event notification */
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate int
22787c478bd9Sstevel@tonic-gate vnevent_support(vnode_t *vp)
22797c478bd9Sstevel@tonic-gate {
22807c478bd9Sstevel@tonic-gate 	if (vp == NULL)
22817c478bd9Sstevel@tonic-gate 		return (EINVAL);
22827c478bd9Sstevel@tonic-gate 
2283df2381bfSpraks 	return (VOP_VNEVENT(vp, VE_SUPPORT, NULL, NULL));
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate void
2287df2381bfSpraks vnevent_rename_src(vnode_t *vp, vnode_t *dvp, char *name)
22887c478bd9Sstevel@tonic-gate {
22897c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
22907c478bd9Sstevel@tonic-gate 		return;
22917c478bd9Sstevel@tonic-gate 	}
2292df2381bfSpraks 	(void) VOP_VNEVENT(vp, VE_RENAME_SRC, dvp, name);
22937c478bd9Sstevel@tonic-gate }
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate void
2296df2381bfSpraks vnevent_rename_dest(vnode_t *vp, vnode_t *dvp, char *name)
22977c478bd9Sstevel@tonic-gate {
22987c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
22997c478bd9Sstevel@tonic-gate 		return;
23007c478bd9Sstevel@tonic-gate 	}
2301df2381bfSpraks 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST, dvp, name);
23027c478bd9Sstevel@tonic-gate }
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate void
2305df2381bfSpraks vnevent_rename_dest_dir(vnode_t *vp)
23067c478bd9Sstevel@tonic-gate {
23077c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
23087c478bd9Sstevel@tonic-gate 		return;
23097c478bd9Sstevel@tonic-gate 	}
2310df2381bfSpraks 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST_DIR, NULL, NULL);
23117c478bd9Sstevel@tonic-gate }
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate void
2314df2381bfSpraks vnevent_remove(vnode_t *vp, vnode_t *dvp, char *name)
23157c478bd9Sstevel@tonic-gate {
23167c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
23177c478bd9Sstevel@tonic-gate 		return;
23187c478bd9Sstevel@tonic-gate 	}
2319df2381bfSpraks 	(void) VOP_VNEVENT(vp, VE_REMOVE, dvp, name);
2320df2381bfSpraks }
2321df2381bfSpraks 
2322df2381bfSpraks void
2323df2381bfSpraks vnevent_rmdir(vnode_t *vp, vnode_t *dvp, char *name)
2324df2381bfSpraks {
2325df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2326df2381bfSpraks 		return;
2327df2381bfSpraks 	}
2328df2381bfSpraks 	(void) VOP_VNEVENT(vp, VE_RMDIR, dvp, name);
2329df2381bfSpraks }
2330df2381bfSpraks 
2331df2381bfSpraks void
2332df2381bfSpraks vnevent_create(vnode_t *vp)
2333df2381bfSpraks {
2334df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2335df2381bfSpraks 		return;
2336df2381bfSpraks 	}
2337df2381bfSpraks 	(void) VOP_VNEVENT(vp, VE_CREATE, NULL, NULL);
2338df2381bfSpraks }
2339df2381bfSpraks 
2340df2381bfSpraks void
2341df2381bfSpraks vnevent_link(vnode_t *vp)
2342df2381bfSpraks {
2343df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2344df2381bfSpraks 		return;
2345df2381bfSpraks 	}
2346df2381bfSpraks 	(void) VOP_VNEVENT(vp, VE_LINK, NULL, NULL);
2347df2381bfSpraks }
2348df2381bfSpraks 
2349df2381bfSpraks void
2350df2381bfSpraks vnevent_mountedover(vnode_t *vp)
2351df2381bfSpraks {
2352df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2353df2381bfSpraks 		return;
2354df2381bfSpraks 	}
2355df2381bfSpraks 	(void) VOP_VNEVENT(vp, VE_MOUNTEDOVER, NULL, NULL);
23567c478bd9Sstevel@tonic-gate }
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate /*
23597c478bd9Sstevel@tonic-gate  * Vnode accessors.
23607c478bd9Sstevel@tonic-gate  */
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate int
23637c478bd9Sstevel@tonic-gate vn_is_readonly(vnode_t *vp)
23647c478bd9Sstevel@tonic-gate {
23657c478bd9Sstevel@tonic-gate 	return (vp->v_vfsp->vfs_flag & VFS_RDONLY);
23667c478bd9Sstevel@tonic-gate }
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate int
23697c478bd9Sstevel@tonic-gate vn_has_flocks(vnode_t *vp)
23707c478bd9Sstevel@tonic-gate {
23717c478bd9Sstevel@tonic-gate 	return (vp->v_filocks != NULL);
23727c478bd9Sstevel@tonic-gate }
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate int
23757c478bd9Sstevel@tonic-gate vn_has_mandatory_locks(vnode_t *vp, int mode)
23767c478bd9Sstevel@tonic-gate {
23777c478bd9Sstevel@tonic-gate 	return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode)));
23787c478bd9Sstevel@tonic-gate }
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate int
23817c478bd9Sstevel@tonic-gate vn_has_cached_data(vnode_t *vp)
23827c478bd9Sstevel@tonic-gate {
23837c478bd9Sstevel@tonic-gate 	return (vp->v_pages != NULL);
23847c478bd9Sstevel@tonic-gate }
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate /*
23877c478bd9Sstevel@tonic-gate  * Return 0 if the vnode in question shouldn't be permitted into a zone via
23887c478bd9Sstevel@tonic-gate  * zone_enter(2).
23897c478bd9Sstevel@tonic-gate  */
23907c478bd9Sstevel@tonic-gate int
23917c478bd9Sstevel@tonic-gate vn_can_change_zones(vnode_t *vp)
23927c478bd9Sstevel@tonic-gate {
23937c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
23947c478bd9Sstevel@tonic-gate 	int allow = 1;
23957c478bd9Sstevel@tonic-gate 	vnode_t *rvp;
23967c478bd9Sstevel@tonic-gate 
2397108322fbScarlsonj 	if (nfs_global_client_only != 0)
2398108322fbScarlsonj 		return (1);
2399108322fbScarlsonj 
24007c478bd9Sstevel@tonic-gate 	/*
24017c478bd9Sstevel@tonic-gate 	 * We always want to look at the underlying vnode if there is one.
24027c478bd9Sstevel@tonic-gate 	 */
24037c478bd9Sstevel@tonic-gate 	if (VOP_REALVP(vp, &rvp) != 0)
24047c478bd9Sstevel@tonic-gate 		rvp = vp;
24057c478bd9Sstevel@tonic-gate 	/*
24067c478bd9Sstevel@tonic-gate 	 * Some pseudo filesystems (including doorfs) don't actually register
24077c478bd9Sstevel@tonic-gate 	 * their vfsops_t, so the following may return NULL; we happily let
24087c478bd9Sstevel@tonic-gate 	 * such vnodes switch zones.
24097c478bd9Sstevel@tonic-gate 	 */
24107c478bd9Sstevel@tonic-gate 	vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp));
24117c478bd9Sstevel@tonic-gate 	if (vswp != NULL) {
24127c478bd9Sstevel@tonic-gate 		if (vswp->vsw_flag & VSW_NOTZONESAFE)
24137c478bd9Sstevel@tonic-gate 			allow = 0;
24147c478bd9Sstevel@tonic-gate 		vfs_unrefvfssw(vswp);
24157c478bd9Sstevel@tonic-gate 	}
24167c478bd9Sstevel@tonic-gate 	return (allow);
24177c478bd9Sstevel@tonic-gate }
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate /*
24207c478bd9Sstevel@tonic-gate  * Return nonzero if the vnode is a mount point, zero if not.
24217c478bd9Sstevel@tonic-gate  */
24227c478bd9Sstevel@tonic-gate int
24237c478bd9Sstevel@tonic-gate vn_ismntpt(vnode_t *vp)
24247c478bd9Sstevel@tonic-gate {
24257c478bd9Sstevel@tonic-gate 	return (vp->v_vfsmountedhere != NULL);
24267c478bd9Sstevel@tonic-gate }
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate /* Retrieve the vfs (if any) mounted on this vnode */
24297c478bd9Sstevel@tonic-gate vfs_t *
24307c478bd9Sstevel@tonic-gate vn_mountedvfs(vnode_t *vp)
24317c478bd9Sstevel@tonic-gate {
24327c478bd9Sstevel@tonic-gate 	return (vp->v_vfsmountedhere);
24337c478bd9Sstevel@tonic-gate }
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate /*
24367c478bd9Sstevel@tonic-gate  * vn_is_opened() checks whether a particular file is opened and
24377c478bd9Sstevel@tonic-gate  * whether the open is for read and/or write.
24387c478bd9Sstevel@tonic-gate  *
24397c478bd9Sstevel@tonic-gate  * Vnode counts are only kept on regular files (v_type=VREG).
24407c478bd9Sstevel@tonic-gate  */
24417c478bd9Sstevel@tonic-gate int
24427c478bd9Sstevel@tonic-gate vn_is_opened(
24437c478bd9Sstevel@tonic-gate 	vnode_t *vp,
24447c478bd9Sstevel@tonic-gate 	v_mode_t mode)
24457c478bd9Sstevel@tonic-gate {
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate 	switch (mode) {
24507c478bd9Sstevel@tonic-gate 	case V_WRITE:
24517c478bd9Sstevel@tonic-gate 		if (vp->v_wrcnt)
24527c478bd9Sstevel@tonic-gate 			return (V_TRUE);
24537c478bd9Sstevel@tonic-gate 		break;
24547c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
24557c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt && vp->v_wrcnt)
24567c478bd9Sstevel@tonic-gate 			return (V_TRUE);
24577c478bd9Sstevel@tonic-gate 		break;
24587c478bd9Sstevel@tonic-gate 	case V_RDORWR:
24597c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt || vp->v_wrcnt)
24607c478bd9Sstevel@tonic-gate 			return (V_TRUE);
24617c478bd9Sstevel@tonic-gate 		break;
24627c478bd9Sstevel@tonic-gate 	case V_READ:
24637c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt)
24647c478bd9Sstevel@tonic-gate 			return (V_TRUE);
24657c478bd9Sstevel@tonic-gate 		break;
24667c478bd9Sstevel@tonic-gate 	}
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	return (V_FALSE);
24697c478bd9Sstevel@tonic-gate }
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate /*
24727c478bd9Sstevel@tonic-gate  * vn_is_mapped() checks whether a particular file is mapped and whether
24737c478bd9Sstevel@tonic-gate  * the file is mapped read and/or write.
24747c478bd9Sstevel@tonic-gate  */
24757c478bd9Sstevel@tonic-gate int
24767c478bd9Sstevel@tonic-gate vn_is_mapped(
24777c478bd9Sstevel@tonic-gate 	vnode_t *vp,
24787c478bd9Sstevel@tonic-gate 	v_mode_t mode)
24797c478bd9Sstevel@tonic-gate {
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate #if !defined(_LP64)
24847c478bd9Sstevel@tonic-gate 	switch (mode) {
24857c478bd9Sstevel@tonic-gate 	/*
24867c478bd9Sstevel@tonic-gate 	 * The atomic_add_64_nv functions force atomicity in the
24877c478bd9Sstevel@tonic-gate 	 * case of 32 bit architectures. Otherwise the 64 bit values
24887c478bd9Sstevel@tonic-gate 	 * require two fetches. The value of the fields may be
24897c478bd9Sstevel@tonic-gate 	 * (potentially) changed between the first fetch and the
24907c478bd9Sstevel@tonic-gate 	 * second
24917c478bd9Sstevel@tonic-gate 	 */
24927c478bd9Sstevel@tonic-gate 	case V_WRITE:
24937c478bd9Sstevel@tonic-gate 		if (atomic_add_64_nv((&(vp->v_mmap_write)), 0))
24947c478bd9Sstevel@tonic-gate 			return (V_TRUE);
24957c478bd9Sstevel@tonic-gate 		break;
24967c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
24977c478bd9Sstevel@tonic-gate 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) &&
24987c478bd9Sstevel@tonic-gate 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
24997c478bd9Sstevel@tonic-gate 			return (V_TRUE);
25007c478bd9Sstevel@tonic-gate 		break;
25017c478bd9Sstevel@tonic-gate 	case V_RDORWR:
25027c478bd9Sstevel@tonic-gate 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) ||
25037c478bd9Sstevel@tonic-gate 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
25047c478bd9Sstevel@tonic-gate 			return (V_TRUE);
25057c478bd9Sstevel@tonic-gate 		break;
25067c478bd9Sstevel@tonic-gate 	case V_READ:
25077c478bd9Sstevel@tonic-gate 		if (atomic_add_64_nv((&(vp->v_mmap_read)), 0))
25087c478bd9Sstevel@tonic-gate 			return (V_TRUE);
25097c478bd9Sstevel@tonic-gate 		break;
25107c478bd9Sstevel@tonic-gate 	}
25117c478bd9Sstevel@tonic-gate #else
25127c478bd9Sstevel@tonic-gate 	switch (mode) {
25137c478bd9Sstevel@tonic-gate 	case V_WRITE:
25147c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_write)
25157c478bd9Sstevel@tonic-gate 			return (V_TRUE);
25167c478bd9Sstevel@tonic-gate 		break;
25177c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
25187c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read && vp->v_mmap_write)
25197c478bd9Sstevel@tonic-gate 			return (V_TRUE);
25207c478bd9Sstevel@tonic-gate 		break;
25217c478bd9Sstevel@tonic-gate 	case V_RDORWR:
25227c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read || vp->v_mmap_write)
25237c478bd9Sstevel@tonic-gate 			return (V_TRUE);
25247c478bd9Sstevel@tonic-gate 		break;
25257c478bd9Sstevel@tonic-gate 	case V_READ:
25267c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read)
25277c478bd9Sstevel@tonic-gate 			return (V_TRUE);
25287c478bd9Sstevel@tonic-gate 		break;
25297c478bd9Sstevel@tonic-gate 	}
25307c478bd9Sstevel@tonic-gate #endif
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 	return (V_FALSE);
25337c478bd9Sstevel@tonic-gate }
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate /*
25367c478bd9Sstevel@tonic-gate  * Set the operations vector for a vnode.
25377c478bd9Sstevel@tonic-gate  *
25387c478bd9Sstevel@tonic-gate  * FEM ensures that the v_femhead pointer is filled in before the
25397c478bd9Sstevel@tonic-gate  * v_op pointer is changed.  This means that if the v_femhead pointer
25407c478bd9Sstevel@tonic-gate  * is NULL, and the v_op field hasn't changed since before which checked
25417c478bd9Sstevel@tonic-gate  * the v_femhead pointer; then our update is ok - we are not racing with
25427c478bd9Sstevel@tonic-gate  * FEM.
25437c478bd9Sstevel@tonic-gate  */
25447c478bd9Sstevel@tonic-gate void
25457c478bd9Sstevel@tonic-gate vn_setops(vnode_t *vp, vnodeops_t *vnodeops)
25467c478bd9Sstevel@tonic-gate {
25477c478bd9Sstevel@tonic-gate 	vnodeops_t	*op;
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
25507c478bd9Sstevel@tonic-gate 	ASSERT(vnodeops != NULL);
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	op = vp->v_op;
25537c478bd9Sstevel@tonic-gate 	membar_consumer();
25547c478bd9Sstevel@tonic-gate 	/*
25557c478bd9Sstevel@tonic-gate 	 * If vp->v_femhead == NULL, then we'll call casptr() to do the
25567c478bd9Sstevel@tonic-gate 	 * compare-and-swap on vp->v_op.  If either fails, then FEM is
25577c478bd9Sstevel@tonic-gate 	 * in effect on the vnode and we need to have FEM deal with it.
25587c478bd9Sstevel@tonic-gate 	 */
25597c478bd9Sstevel@tonic-gate 	if (vp->v_femhead != NULL || casptr(&vp->v_op, op, vnodeops) != op) {
25607c478bd9Sstevel@tonic-gate 		fem_setvnops(vp, vnodeops);
25617c478bd9Sstevel@tonic-gate 	}
25627c478bd9Sstevel@tonic-gate }
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate /*
25657c478bd9Sstevel@tonic-gate  * Retrieve the operations vector for a vnode
25667c478bd9Sstevel@tonic-gate  * As with vn_setops(above); make sure we aren't racing with FEM.
25677c478bd9Sstevel@tonic-gate  * FEM sets the v_op to a special, internal, vnodeops that wouldn't
25687c478bd9Sstevel@tonic-gate  * make sense to the callers of this routine.
25697c478bd9Sstevel@tonic-gate  */
25707c478bd9Sstevel@tonic-gate vnodeops_t *
25717c478bd9Sstevel@tonic-gate vn_getops(vnode_t *vp)
25727c478bd9Sstevel@tonic-gate {
25737c478bd9Sstevel@tonic-gate 	vnodeops_t	*op;
25747c478bd9Sstevel@tonic-gate 
25757c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 	op = vp->v_op;
25787c478bd9Sstevel@tonic-gate 	membar_consumer();
25797c478bd9Sstevel@tonic-gate 	if (vp->v_femhead == NULL && op == vp->v_op) {
25807c478bd9Sstevel@tonic-gate 		return (op);
25817c478bd9Sstevel@tonic-gate 	} else {
25827c478bd9Sstevel@tonic-gate 		return (fem_getvnops(vp));
25837c478bd9Sstevel@tonic-gate 	}
25847c478bd9Sstevel@tonic-gate }
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate /*
25877c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the vnodeops matches that of the vnode.
25887c478bd9Sstevel@tonic-gate  * Returns zero (0) if not.
25897c478bd9Sstevel@tonic-gate  */
25907c478bd9Sstevel@tonic-gate int
25917c478bd9Sstevel@tonic-gate vn_matchops(vnode_t *vp, vnodeops_t *vnodeops)
25927c478bd9Sstevel@tonic-gate {
25937c478bd9Sstevel@tonic-gate 	return (vn_getops(vp) == vnodeops);
25947c478bd9Sstevel@tonic-gate }
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate /*
25977c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the specified operation matches the
25987c478bd9Sstevel@tonic-gate  * corresponding operation for that the vnode.
25997c478bd9Sstevel@tonic-gate  * Returns zero (0) if not.
26007c478bd9Sstevel@tonic-gate  */
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate #define	MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0))
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate int
26057c478bd9Sstevel@tonic-gate vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp)
26067c478bd9Sstevel@tonic-gate {
26077c478bd9Sstevel@tonic-gate 	const fs_operation_trans_def_t *otdp;
26087c478bd9Sstevel@tonic-gate 	fs_generic_func_p *loc = NULL;
26097c478bd9Sstevel@tonic-gate 	vnodeops_t	*vop = vn_getops(vp);
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	ASSERT(vopname != NULL);
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 	for (otdp = vn_ops_table; otdp->name != NULL; otdp++) {
26147c478bd9Sstevel@tonic-gate 		if (MATCHNAME(otdp->name, vopname)) {
2615b9c98e29Spf199842 			loc = (fs_generic_func_p *)
2616b9c98e29Spf199842 			    ((char *)(vop) + otdp->offset);
26177c478bd9Sstevel@tonic-gate 			break;
26187c478bd9Sstevel@tonic-gate 		}
26197c478bd9Sstevel@tonic-gate 	}
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	return ((loc != NULL) && (*loc == funcp));
26227c478bd9Sstevel@tonic-gate }
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate /*
26257c478bd9Sstevel@tonic-gate  * fs_new_caller_id() needs to return a unique ID on a given local system.
26267c478bd9Sstevel@tonic-gate  * The IDs do not need to survive across reboots.  These are primarily
26277c478bd9Sstevel@tonic-gate  * used so that (FEM) monitors can detect particular callers (such as
26287c478bd9Sstevel@tonic-gate  * the NFS server) to a given vnode/vfs operation.
26297c478bd9Sstevel@tonic-gate  */
26307c478bd9Sstevel@tonic-gate u_longlong_t
26317c478bd9Sstevel@tonic-gate fs_new_caller_id()
26327c478bd9Sstevel@tonic-gate {
26337c478bd9Sstevel@tonic-gate 	static uint64_t next_caller_id = 0LL; /* First call returns 1 */
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 	return ((u_longlong_t)atomic_add_64_nv(&next_caller_id, 1));
26367c478bd9Sstevel@tonic-gate }
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate /*
26397c478bd9Sstevel@tonic-gate  * Given a starting vnode and a path, updates the path in the target vnode in
26407c478bd9Sstevel@tonic-gate  * a safe manner.  If the vnode already has path information embedded, then the
2641ca2c3138Seschrock  * cached path is left untouched.
26427c478bd9Sstevel@tonic-gate  */
264348011479Ssn199410 
264448011479Ssn199410 size_t max_vnode_path = 4 * MAXPATHLEN;
264548011479Ssn199410 
26467c478bd9Sstevel@tonic-gate void
26477c478bd9Sstevel@tonic-gate vn_setpath(vnode_t *rootvp, struct vnode *startvp, struct vnode *vp,
26487c478bd9Sstevel@tonic-gate     const char *path, size_t plen)
26497c478bd9Sstevel@tonic-gate {
26507c478bd9Sstevel@tonic-gate 	char	*rpath;
26517c478bd9Sstevel@tonic-gate 	vnode_t	*base;
26527c478bd9Sstevel@tonic-gate 	size_t	rpathlen, rpathalloc;
26537c478bd9Sstevel@tonic-gate 	int	doslash = 1;
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 	if (*path == '/') {
26567c478bd9Sstevel@tonic-gate 		base = rootvp;
26577c478bd9Sstevel@tonic-gate 		path++;
26587c478bd9Sstevel@tonic-gate 		plen--;
26597c478bd9Sstevel@tonic-gate 	} else {
26607c478bd9Sstevel@tonic-gate 		base = startvp;
26617c478bd9Sstevel@tonic-gate 	}
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 	/*
26647c478bd9Sstevel@tonic-gate 	 * We cannot grab base->v_lock while we hold vp->v_lock because of
26657c478bd9Sstevel@tonic-gate 	 * the potential for deadlock.
26667c478bd9Sstevel@tonic-gate 	 */
26677c478bd9Sstevel@tonic-gate 	mutex_enter(&base->v_lock);
26687c478bd9Sstevel@tonic-gate 	if (base->v_path == NULL) {
26697c478bd9Sstevel@tonic-gate 		mutex_exit(&base->v_lock);
26707c478bd9Sstevel@tonic-gate 		return;
26717c478bd9Sstevel@tonic-gate 	}
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 	rpathlen = strlen(base->v_path);
26747c478bd9Sstevel@tonic-gate 	rpathalloc = rpathlen + plen + 1;
26757c478bd9Sstevel@tonic-gate 	/* Avoid adding a slash if there's already one there */
26767c478bd9Sstevel@tonic-gate 	if (base->v_path[rpathlen-1] == '/')
26777c478bd9Sstevel@tonic-gate 		doslash = 0;
26787c478bd9Sstevel@tonic-gate 	else
26797c478bd9Sstevel@tonic-gate 		rpathalloc++;
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	/*
26827c478bd9Sstevel@tonic-gate 	 * We don't want to call kmem_alloc(KM_SLEEP) with kernel locks held,
26837c478bd9Sstevel@tonic-gate 	 * so we must do this dance.  If, by chance, something changes the path,
26847c478bd9Sstevel@tonic-gate 	 * just give up since there is no real harm.
26857c478bd9Sstevel@tonic-gate 	 */
26867c478bd9Sstevel@tonic-gate 	mutex_exit(&base->v_lock);
26877c478bd9Sstevel@tonic-gate 
268848011479Ssn199410 	/* Paths should stay within reason */
268948011479Ssn199410 	if (rpathalloc > max_vnode_path)
269048011479Ssn199410 		return;
269148011479Ssn199410 
26927c478bd9Sstevel@tonic-gate 	rpath = kmem_alloc(rpathalloc, KM_SLEEP);
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate 	mutex_enter(&base->v_lock);
26957c478bd9Sstevel@tonic-gate 	if (base->v_path == NULL || strlen(base->v_path) != rpathlen) {
26967c478bd9Sstevel@tonic-gate 		mutex_exit(&base->v_lock);
26977c478bd9Sstevel@tonic-gate 		kmem_free(rpath, rpathalloc);
26987c478bd9Sstevel@tonic-gate 		return;
26997c478bd9Sstevel@tonic-gate 	}
27007c478bd9Sstevel@tonic-gate 	bcopy(base->v_path, rpath, rpathlen);
27017c478bd9Sstevel@tonic-gate 	mutex_exit(&base->v_lock);
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate 	if (doslash)
27047c478bd9Sstevel@tonic-gate 		rpath[rpathlen++] = '/';
27057c478bd9Sstevel@tonic-gate 	bcopy(path, rpath + rpathlen, plen);
27067c478bd9Sstevel@tonic-gate 	rpath[rpathlen + plen] = '\0';
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
27097c478bd9Sstevel@tonic-gate 	if (vp->v_path != NULL) {
27107c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
27117c478bd9Sstevel@tonic-gate 		kmem_free(rpath, rpathalloc);
27127c478bd9Sstevel@tonic-gate 	} else {
27137c478bd9Sstevel@tonic-gate 		vp->v_path = rpath;
27147c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
27157c478bd9Sstevel@tonic-gate 	}
27167c478bd9Sstevel@tonic-gate }
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate /*
27197c478bd9Sstevel@tonic-gate  * Sets the path to the vnode to be the given string, regardless of current
27207c478bd9Sstevel@tonic-gate  * context.  The string must be a complete path from rootdir.  This is only used
27217c478bd9Sstevel@tonic-gate  * by fsop_root() for setting the path based on the mountpoint.
27227c478bd9Sstevel@tonic-gate  */
27237c478bd9Sstevel@tonic-gate void
27247c478bd9Sstevel@tonic-gate vn_setpath_str(struct vnode *vp, const char *str, size_t len)
27257c478bd9Sstevel@tonic-gate {
27267c478bd9Sstevel@tonic-gate 	char *buf = kmem_alloc(len + 1, KM_SLEEP);
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
27297c478bd9Sstevel@tonic-gate 	if (vp->v_path != NULL) {
27307c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
27317c478bd9Sstevel@tonic-gate 		kmem_free(buf, len + 1);
27327c478bd9Sstevel@tonic-gate 		return;
27337c478bd9Sstevel@tonic-gate 	}
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate 	vp->v_path = buf;
27367c478bd9Sstevel@tonic-gate 	bcopy(str, vp->v_path, len);
27377c478bd9Sstevel@tonic-gate 	vp->v_path[len] = '\0';
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
27407c478bd9Sstevel@tonic-gate }
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate /*
27437c478bd9Sstevel@tonic-gate  * Similar to vn_setpath_str(), this function sets the path of the destination
27447c478bd9Sstevel@tonic-gate  * vnode to the be the same as the source vnode.
27457c478bd9Sstevel@tonic-gate  */
27467c478bd9Sstevel@tonic-gate void
27477c478bd9Sstevel@tonic-gate vn_copypath(struct vnode *src, struct vnode *dst)
27487c478bd9Sstevel@tonic-gate {
27497c478bd9Sstevel@tonic-gate 	char *buf;
27507c478bd9Sstevel@tonic-gate 	int alloc;
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 	mutex_enter(&src->v_lock);
27537c478bd9Sstevel@tonic-gate 	if (src->v_path == NULL) {
27547c478bd9Sstevel@tonic-gate 		mutex_exit(&src->v_lock);
27557c478bd9Sstevel@tonic-gate 		return;
27567c478bd9Sstevel@tonic-gate 	}
27577c478bd9Sstevel@tonic-gate 	alloc = strlen(src->v_path) + 1;
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 	/* avoid kmem_alloc() with lock held */
27607c478bd9Sstevel@tonic-gate 	mutex_exit(&src->v_lock);
27617c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(alloc, KM_SLEEP);
27627c478bd9Sstevel@tonic-gate 	mutex_enter(&src->v_lock);
27637c478bd9Sstevel@tonic-gate 	if (src->v_path == NULL || strlen(src->v_path) + 1 != alloc) {
27647c478bd9Sstevel@tonic-gate 		mutex_exit(&src->v_lock);
27657c478bd9Sstevel@tonic-gate 		kmem_free(buf, alloc);
27667c478bd9Sstevel@tonic-gate 		return;
27677c478bd9Sstevel@tonic-gate 	}
27687c478bd9Sstevel@tonic-gate 	bcopy(src->v_path, buf, alloc);
27697c478bd9Sstevel@tonic-gate 	mutex_exit(&src->v_lock);
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	mutex_enter(&dst->v_lock);
27727c478bd9Sstevel@tonic-gate 	if (dst->v_path != NULL) {
27737c478bd9Sstevel@tonic-gate 		mutex_exit(&dst->v_lock);
27747c478bd9Sstevel@tonic-gate 		kmem_free(buf, alloc);
27757c478bd9Sstevel@tonic-gate 		return;
27767c478bd9Sstevel@tonic-gate 	}
27777c478bd9Sstevel@tonic-gate 	dst->v_path = buf;
27787c478bd9Sstevel@tonic-gate 	mutex_exit(&dst->v_lock);
27797c478bd9Sstevel@tonic-gate }
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate /*
27827c478bd9Sstevel@tonic-gate  * XXX Private interface for segvn routines that handle vnode
27837c478bd9Sstevel@tonic-gate  * large page segments.
27847c478bd9Sstevel@tonic-gate  *
27857c478bd9Sstevel@tonic-gate  * return 1 if vp's file system VOP_PAGEIO() implementation
27867c478bd9Sstevel@tonic-gate  * can be safely used instead of VOP_GETPAGE() for handling
27877c478bd9Sstevel@tonic-gate  * pagefaults against regular non swap files. VOP_PAGEIO()
27887c478bd9Sstevel@tonic-gate  * interface is considered safe here if its implementation
27897c478bd9Sstevel@tonic-gate  * is very close to VOP_GETPAGE() implementation.
27907c478bd9Sstevel@tonic-gate  * e.g. It zero's out the part of the page beyond EOF. Doesn't
27917c478bd9Sstevel@tonic-gate  * panic if there're file holes but instead returns an error.
27927c478bd9Sstevel@tonic-gate  * Doesn't assume file won't be changed by user writes, etc.
27937c478bd9Sstevel@tonic-gate  *
27947c478bd9Sstevel@tonic-gate  * return 0 otherwise.
27957c478bd9Sstevel@tonic-gate  *
27967c478bd9Sstevel@tonic-gate  * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs.
27977c478bd9Sstevel@tonic-gate  */
27987c478bd9Sstevel@tonic-gate int
27997c478bd9Sstevel@tonic-gate vn_vmpss_usepageio(vnode_t *vp)
28007c478bd9Sstevel@tonic-gate {
28017c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
28027c478bd9Sstevel@tonic-gate 	char *fsname = vfssw[vfsp->vfs_fstype].vsw_name;
28037c478bd9Sstevel@tonic-gate 	char *pageio_ok_fss[] = {"ufs", "nfs", NULL};
28047c478bd9Sstevel@tonic-gate 	char **fsok = pageio_ok_fss;
28057c478bd9Sstevel@tonic-gate 
28067c478bd9Sstevel@tonic-gate 	if (fsname == NULL) {
28077c478bd9Sstevel@tonic-gate 		return (0);
28087c478bd9Sstevel@tonic-gate 	}
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 	for (; *fsok; fsok++) {
28117c478bd9Sstevel@tonic-gate 		if (strcmp(*fsok, fsname) == 0) {
28127c478bd9Sstevel@tonic-gate 			return (1);
28137c478bd9Sstevel@tonic-gate 		}
28147c478bd9Sstevel@tonic-gate 	}
28157c478bd9Sstevel@tonic-gate 	return (0);
28167c478bd9Sstevel@tonic-gate }
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate /* VOP_XXX() macros call the corresponding fop_xxx() function */
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate int
28217c478bd9Sstevel@tonic-gate fop_open(
28227c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
28237c478bd9Sstevel@tonic-gate 	int mode,
28247c478bd9Sstevel@tonic-gate 	cred_t *cr)
28257c478bd9Sstevel@tonic-gate {
28267c478bd9Sstevel@tonic-gate 	int ret;
28277c478bd9Sstevel@tonic-gate 	vnode_t *vp = *vpp;
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
28307c478bd9Sstevel@tonic-gate 	/*
28317c478bd9Sstevel@tonic-gate 	 * Adding to the vnode counts before calling open
28327c478bd9Sstevel@tonic-gate 	 * avoids the need for a mutex. It circumvents a race
28337c478bd9Sstevel@tonic-gate 	 * condition where a query made on the vnode counts results in a
28347c478bd9Sstevel@tonic-gate 	 * false negative. The inquirer goes away believing the file is
28357c478bd9Sstevel@tonic-gate 	 * not open when there is an open on the file already under way.
28367c478bd9Sstevel@tonic-gate 	 *
28377c478bd9Sstevel@tonic-gate 	 * The counts are meant to prevent NFS from granting a delegation
28387c478bd9Sstevel@tonic-gate 	 * when it would be dangerous to do so.
28397c478bd9Sstevel@tonic-gate 	 *
28407c478bd9Sstevel@tonic-gate 	 * The vnode counts are only kept on regular files
28417c478bd9Sstevel@tonic-gate 	 */
28427c478bd9Sstevel@tonic-gate 	if ((*vpp)->v_type == VREG) {
28437c478bd9Sstevel@tonic-gate 		if (mode & FREAD)
28447c478bd9Sstevel@tonic-gate 			atomic_add_32(&((*vpp)->v_rdcnt), 1);
28457c478bd9Sstevel@tonic-gate 		if (mode & FWRITE)
28467c478bd9Sstevel@tonic-gate 			atomic_add_32(&((*vpp)->v_wrcnt), 1);
28477c478bd9Sstevel@tonic-gate 	}
28487c478bd9Sstevel@tonic-gate 
2849f48205beScasper 	VOPXID_MAP_CR(vp, cr);
2850f48205beScasper 
28517c478bd9Sstevel@tonic-gate 	ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr);
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 	if (ret) {
28547c478bd9Sstevel@tonic-gate 		/*
28557c478bd9Sstevel@tonic-gate 		 * Use the saved vp just in case the vnode ptr got trashed
28567c478bd9Sstevel@tonic-gate 		 * by the error.
28577c478bd9Sstevel@tonic-gate 		 */
28582bb1cb30Sbmc 		VOPSTATS_UPDATE(vp, open);
28597c478bd9Sstevel@tonic-gate 		if ((vp->v_type == VREG) && (mode & FREAD))
28607c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_rdcnt), -1);
28617c478bd9Sstevel@tonic-gate 		if ((vp->v_type == VREG) && (mode & FWRITE))
28627c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_wrcnt), -1);
28637c478bd9Sstevel@tonic-gate 	} else {
28647c478bd9Sstevel@tonic-gate 		/*
28657c478bd9Sstevel@tonic-gate 		 * Some filesystems will return a different vnode,
28667c478bd9Sstevel@tonic-gate 		 * but the same path was still used to open it.
28677c478bd9Sstevel@tonic-gate 		 * So if we do change the vnode and need to
28687c478bd9Sstevel@tonic-gate 		 * copy over the path, do so here, rather than special
28697c478bd9Sstevel@tonic-gate 		 * casing each filesystem. Adjust the vnode counts to
28707c478bd9Sstevel@tonic-gate 		 * reflect the vnode switch.
28717c478bd9Sstevel@tonic-gate 		 */
28722bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, open);
28737c478bd9Sstevel@tonic-gate 		if (*vpp != vp && *vpp != NULL) {
28747c478bd9Sstevel@tonic-gate 			vn_copypath(vp, *vpp);
28757c478bd9Sstevel@tonic-gate 			if (((*vpp)->v_type == VREG) && (mode & FREAD))
28767c478bd9Sstevel@tonic-gate 				atomic_add_32(&((*vpp)->v_rdcnt), 1);
28777c478bd9Sstevel@tonic-gate 			if ((vp->v_type == VREG) && (mode & FREAD))
28787c478bd9Sstevel@tonic-gate 				atomic_add_32(&(vp->v_rdcnt), -1);
28797c478bd9Sstevel@tonic-gate 			if (((*vpp)->v_type == VREG) && (mode & FWRITE))
28807c478bd9Sstevel@tonic-gate 				atomic_add_32(&((*vpp)->v_wrcnt), 1);
28817c478bd9Sstevel@tonic-gate 			if ((vp->v_type == VREG) && (mode & FWRITE))
28827c478bd9Sstevel@tonic-gate 				atomic_add_32(&(vp->v_wrcnt), -1);
28837c478bd9Sstevel@tonic-gate 		}
28847c478bd9Sstevel@tonic-gate 	}
28857c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
28867c478bd9Sstevel@tonic-gate 	return (ret);
28877c478bd9Sstevel@tonic-gate }
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate int
28907c478bd9Sstevel@tonic-gate fop_close(
28917c478bd9Sstevel@tonic-gate 	vnode_t *vp,
28927c478bd9Sstevel@tonic-gate 	int flag,
28937c478bd9Sstevel@tonic-gate 	int count,
28947c478bd9Sstevel@tonic-gate 	offset_t offset,
28957c478bd9Sstevel@tonic-gate 	cred_t *cr)
28967c478bd9Sstevel@tonic-gate {
28975a59a8b3Srsb 	int err;
28985a59a8b3Srsb 
2899f48205beScasper 	VOPXID_MAP_CR(vp, cr);
2900f48205beScasper 
29015a59a8b3Srsb 	err = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr);
29022bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, close);
29037c478bd9Sstevel@tonic-gate 	/*
29047c478bd9Sstevel@tonic-gate 	 * Check passed in count to handle possible dups. Vnode counts are only
29057c478bd9Sstevel@tonic-gate 	 * kept on regular files
29067c478bd9Sstevel@tonic-gate 	 */
29077c478bd9Sstevel@tonic-gate 	if ((vp->v_type == VREG) && (count == 1))  {
29087c478bd9Sstevel@tonic-gate 		if (flag & FREAD) {
29097c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_rdcnt > 0);
29107c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_rdcnt), -1);
29117c478bd9Sstevel@tonic-gate 		}
29127c478bd9Sstevel@tonic-gate 		if (flag & FWRITE) {
29137c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_wrcnt > 0);
29147c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_wrcnt), -1);
29157c478bd9Sstevel@tonic-gate 		}
29167c478bd9Sstevel@tonic-gate 	}
29175a59a8b3Srsb 	return (err);
29187c478bd9Sstevel@tonic-gate }
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate int
29217c478bd9Sstevel@tonic-gate fop_read(
29227c478bd9Sstevel@tonic-gate 	vnode_t *vp,
29237c478bd9Sstevel@tonic-gate 	uio_t *uiop,
29247c478bd9Sstevel@tonic-gate 	int ioflag,
29257c478bd9Sstevel@tonic-gate 	cred_t *cr,
29267c478bd9Sstevel@tonic-gate 	struct caller_context *ct)
29277c478bd9Sstevel@tonic-gate {
29285a59a8b3Srsb 	int	err;
29295a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
29305a59a8b3Srsb 
2931f48205beScasper 	VOPXID_MAP_CR(vp, cr);
2932f48205beScasper 
29335a59a8b3Srsb 	err = (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct);
29342bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, read,
29355a59a8b3Srsb 	    read_bytes, (resid_start - uiop->uio_resid));
29365a59a8b3Srsb 	return (err);
29377c478bd9Sstevel@tonic-gate }
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate int
29407c478bd9Sstevel@tonic-gate fop_write(
29417c478bd9Sstevel@tonic-gate 	vnode_t *vp,
29427c478bd9Sstevel@tonic-gate 	uio_t *uiop,
29437c478bd9Sstevel@tonic-gate 	int ioflag,
29447c478bd9Sstevel@tonic-gate 	cred_t *cr,
29457c478bd9Sstevel@tonic-gate 	struct caller_context *ct)
29467c478bd9Sstevel@tonic-gate {
29475a59a8b3Srsb 	int	err;
29485a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
29495a59a8b3Srsb 
2950f48205beScasper 	VOPXID_MAP_CR(vp, cr);
2951f48205beScasper 
29525a59a8b3Srsb 	err = (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct);
29532bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, write,
29545a59a8b3Srsb 	    write_bytes, (resid_start - uiop->uio_resid));
29555a59a8b3Srsb 	return (err);
29567c478bd9Sstevel@tonic-gate }
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate int
29597c478bd9Sstevel@tonic-gate fop_ioctl(
29607c478bd9Sstevel@tonic-gate 	vnode_t *vp,
29617c478bd9Sstevel@tonic-gate 	int cmd,
29627c478bd9Sstevel@tonic-gate 	intptr_t arg,
29637c478bd9Sstevel@tonic-gate 	int flag,
29647c478bd9Sstevel@tonic-gate 	cred_t *cr,
29657c478bd9Sstevel@tonic-gate 	int *rvalp)
29667c478bd9Sstevel@tonic-gate {
29675a59a8b3Srsb 	int	err;
29685a59a8b3Srsb 
2969f48205beScasper 	VOPXID_MAP_CR(vp, cr);
2970f48205beScasper 
29715a59a8b3Srsb 	err = (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp);
29722bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, ioctl);
29735a59a8b3Srsb 	return (err);
29747c478bd9Sstevel@tonic-gate }
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate int
29777c478bd9Sstevel@tonic-gate fop_setfl(
29787c478bd9Sstevel@tonic-gate 	vnode_t *vp,
29797c478bd9Sstevel@tonic-gate 	int oflags,
29807c478bd9Sstevel@tonic-gate 	int nflags,
29817c478bd9Sstevel@tonic-gate 	cred_t *cr)
29827c478bd9Sstevel@tonic-gate {
29835a59a8b3Srsb 	int	err;
29845a59a8b3Srsb 
2985f48205beScasper 	VOPXID_MAP_CR(vp, cr);
2986f48205beScasper 
29875a59a8b3Srsb 	err = (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr);
29882bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setfl);
29895a59a8b3Srsb 	return (err);
29907c478bd9Sstevel@tonic-gate }
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate int
29937c478bd9Sstevel@tonic-gate fop_getattr(
29947c478bd9Sstevel@tonic-gate 	vnode_t *vp,
29957c478bd9Sstevel@tonic-gate 	vattr_t *vap,
29967c478bd9Sstevel@tonic-gate 	int flags,
29977c478bd9Sstevel@tonic-gate 	cred_t *cr)
29987c478bd9Sstevel@tonic-gate {
29995a59a8b3Srsb 	int	err;
30005a59a8b3Srsb 
3001f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3002f48205beScasper 
30035a59a8b3Srsb 	err = (*(vp)->v_op->vop_getattr)(vp, vap, flags, cr);
30042bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getattr);
30055a59a8b3Srsb 	return (err);
30067c478bd9Sstevel@tonic-gate }
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate int
30097c478bd9Sstevel@tonic-gate fop_setattr(
30107c478bd9Sstevel@tonic-gate 	vnode_t *vp,
30117c478bd9Sstevel@tonic-gate 	vattr_t *vap,
30127c478bd9Sstevel@tonic-gate 	int flags,
30137c478bd9Sstevel@tonic-gate 	cred_t *cr,
30147c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
30157c478bd9Sstevel@tonic-gate {
30165a59a8b3Srsb 	int	err;
30175a59a8b3Srsb 
3018f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3019f48205beScasper 
30205a59a8b3Srsb 	err = (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct);
30212bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setattr);
30225a59a8b3Srsb 	return (err);
30237c478bd9Sstevel@tonic-gate }
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate int
30267c478bd9Sstevel@tonic-gate fop_access(
30277c478bd9Sstevel@tonic-gate 	vnode_t *vp,
30287c478bd9Sstevel@tonic-gate 	int mode,
30297c478bd9Sstevel@tonic-gate 	int flags,
30307c478bd9Sstevel@tonic-gate 	cred_t *cr)
30317c478bd9Sstevel@tonic-gate {
30325a59a8b3Srsb 	int	err;
30335a59a8b3Srsb 
3034f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3035f48205beScasper 
30365a59a8b3Srsb 	err = (*(vp)->v_op->vop_access)(vp, mode, flags, cr);
30372bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, access);
30385a59a8b3Srsb 	return (err);
30397c478bd9Sstevel@tonic-gate }
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate int
30427c478bd9Sstevel@tonic-gate fop_lookup(
30437c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
30447c478bd9Sstevel@tonic-gate 	char *nm,
30457c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
30467c478bd9Sstevel@tonic-gate 	pathname_t *pnp,
30477c478bd9Sstevel@tonic-gate 	int flags,
30487c478bd9Sstevel@tonic-gate 	vnode_t *rdir,
30497c478bd9Sstevel@tonic-gate 	cred_t *cr)
30507c478bd9Sstevel@tonic-gate {
3051ca2c3138Seschrock 	int ret;
3052ca2c3138Seschrock 
3053f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3054f48205beScasper 
3055ca2c3138Seschrock 	ret = (*(dvp)->v_op->vop_lookup)(dvp, nm, vpp, pnp, flags, rdir, cr);
30565a59a8b3Srsb 	if (ret == 0 && *vpp) {
30572bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, lookup);
30585a59a8b3Srsb 		if ((*vpp)->v_path == NULL) {
3059ca2c3138Seschrock 			vn_setpath(rootdir, dvp, *vpp, nm, strlen(nm));
30605a59a8b3Srsb 		}
30615a59a8b3Srsb 	}
3062ca2c3138Seschrock 
3063ca2c3138Seschrock 	return (ret);
30647c478bd9Sstevel@tonic-gate }
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate int
30677c478bd9Sstevel@tonic-gate fop_create(
30687c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
30697c478bd9Sstevel@tonic-gate 	char *name,
30707c478bd9Sstevel@tonic-gate 	vattr_t *vap,
30717c478bd9Sstevel@tonic-gate 	vcexcl_t excl,
30727c478bd9Sstevel@tonic-gate 	int mode,
30737c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
30747c478bd9Sstevel@tonic-gate 	cred_t *cr,
30757c478bd9Sstevel@tonic-gate 	int flag)
30767c478bd9Sstevel@tonic-gate {
30777c478bd9Sstevel@tonic-gate 	int ret;
30787c478bd9Sstevel@tonic-gate 
3079f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3080f48205beScasper 
30817c478bd9Sstevel@tonic-gate 	ret = (*(dvp)->v_op->vop_create)
30827c478bd9Sstevel@tonic-gate 	    (dvp, name, vap, excl, mode, vpp, cr, flag);
30835a59a8b3Srsb 	if (ret == 0 && *vpp) {
30842bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, create);
30855a59a8b3Srsb 		if ((*vpp)->v_path == NULL) {
3086ca2c3138Seschrock 			vn_setpath(rootdir, dvp, *vpp, name, strlen(name));
30875a59a8b3Srsb 		}
30885a59a8b3Srsb 	}
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 	return (ret);
30917c478bd9Sstevel@tonic-gate }
30927c478bd9Sstevel@tonic-gate 
30937c478bd9Sstevel@tonic-gate int
30947c478bd9Sstevel@tonic-gate fop_remove(
30957c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
30967c478bd9Sstevel@tonic-gate 	char *nm,
30977c478bd9Sstevel@tonic-gate 	cred_t *cr)
30987c478bd9Sstevel@tonic-gate {
30995a59a8b3Srsb 	int	err;
31005a59a8b3Srsb 
3101f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3102f48205beScasper 
31035a59a8b3Srsb 	err = (*(dvp)->v_op->vop_remove)(dvp, nm, cr);
31042bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, remove);
31055a59a8b3Srsb 	return (err);
31067c478bd9Sstevel@tonic-gate }
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate int
31097c478bd9Sstevel@tonic-gate fop_link(
31107c478bd9Sstevel@tonic-gate 	vnode_t *tdvp,
31117c478bd9Sstevel@tonic-gate 	vnode_t *svp,
31127c478bd9Sstevel@tonic-gate 	char *tnm,
31137c478bd9Sstevel@tonic-gate 	cred_t *cr)
31147c478bd9Sstevel@tonic-gate {
31155a59a8b3Srsb 	int	err;
31165a59a8b3Srsb 
3117f48205beScasper 	VOPXID_MAP_CR(tdvp, cr);
3118f48205beScasper 
31195a59a8b3Srsb 	err = (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr);
31202bb1cb30Sbmc 	VOPSTATS_UPDATE(tdvp, link);
31215a59a8b3Srsb 	return (err);
31227c478bd9Sstevel@tonic-gate }
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate int
31257c478bd9Sstevel@tonic-gate fop_rename(
31267c478bd9Sstevel@tonic-gate 	vnode_t *sdvp,
31277c478bd9Sstevel@tonic-gate 	char *snm,
31287c478bd9Sstevel@tonic-gate 	vnode_t *tdvp,
31297c478bd9Sstevel@tonic-gate 	char *tnm,
31307c478bd9Sstevel@tonic-gate 	cred_t *cr)
31317c478bd9Sstevel@tonic-gate {
31325a59a8b3Srsb 	int	err;
31335a59a8b3Srsb 
3134f48205beScasper 	VOPXID_MAP_CR(tdvp, cr);
3135f48205beScasper 
31365a59a8b3Srsb 	err = (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr);
31372bb1cb30Sbmc 	VOPSTATS_UPDATE(sdvp, rename);
31385a59a8b3Srsb 	return (err);
31397c478bd9Sstevel@tonic-gate }
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate int
31427c478bd9Sstevel@tonic-gate fop_mkdir(
31437c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
31447c478bd9Sstevel@tonic-gate 	char *dirname,
31457c478bd9Sstevel@tonic-gate 	vattr_t *vap,
31467c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
31477c478bd9Sstevel@tonic-gate 	cred_t *cr)
31487c478bd9Sstevel@tonic-gate {
31497c478bd9Sstevel@tonic-gate 	int ret;
31507c478bd9Sstevel@tonic-gate 
3151f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3152f48205beScasper 
31537c478bd9Sstevel@tonic-gate 	ret = (*(dvp)->v_op->vop_mkdir)(dvp, dirname, vap, vpp, cr);
31545a59a8b3Srsb 	if (ret == 0 && *vpp) {
31552bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, mkdir);
31565a59a8b3Srsb 		if ((*vpp)->v_path == NULL) {
31575a59a8b3Srsb 			vn_setpath(rootdir, dvp, *vpp, dirname,
31585a59a8b3Srsb 			    strlen(dirname));
31595a59a8b3Srsb 		}
31605a59a8b3Srsb 	}
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	return (ret);
31637c478bd9Sstevel@tonic-gate }
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate int
31667c478bd9Sstevel@tonic-gate fop_rmdir(
31677c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
31687c478bd9Sstevel@tonic-gate 	char *nm,
31697c478bd9Sstevel@tonic-gate 	vnode_t *cdir,
31707c478bd9Sstevel@tonic-gate 	cred_t *cr)
31717c478bd9Sstevel@tonic-gate {
31725a59a8b3Srsb 	int	err;
31735a59a8b3Srsb 
3174f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3175f48205beScasper 
31765a59a8b3Srsb 	err = (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr);
31772bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, rmdir);
31785a59a8b3Srsb 	return (err);
31797c478bd9Sstevel@tonic-gate }
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate int
31827c478bd9Sstevel@tonic-gate fop_readdir(
31837c478bd9Sstevel@tonic-gate 	vnode_t *vp,
31847c478bd9Sstevel@tonic-gate 	uio_t *uiop,
31857c478bd9Sstevel@tonic-gate 	cred_t *cr,
31867c478bd9Sstevel@tonic-gate 	int *eofp)
31877c478bd9Sstevel@tonic-gate {
31885a59a8b3Srsb 	int	err;
31895a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
31905a59a8b3Srsb 
3191f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3192f48205beScasper 
31935a59a8b3Srsb 	err = (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp);
31942bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, readdir,
31955a59a8b3Srsb 	    readdir_bytes, (resid_start - uiop->uio_resid));
31965a59a8b3Srsb 	return (err);
31977c478bd9Sstevel@tonic-gate }
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate int
32007c478bd9Sstevel@tonic-gate fop_symlink(
32017c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
32027c478bd9Sstevel@tonic-gate 	char *linkname,
32037c478bd9Sstevel@tonic-gate 	vattr_t *vap,
32047c478bd9Sstevel@tonic-gate 	char *target,
32057c478bd9Sstevel@tonic-gate 	cred_t *cr)
32067c478bd9Sstevel@tonic-gate {
32075a59a8b3Srsb 	int	err;
32085a59a8b3Srsb 
3209f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3210f48205beScasper 
32115a59a8b3Srsb 	err = (*(dvp)->v_op->vop_symlink) (dvp, linkname, vap, target, cr);
32122bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, symlink);
32135a59a8b3Srsb 	return (err);
32147c478bd9Sstevel@tonic-gate }
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate int
32177c478bd9Sstevel@tonic-gate fop_readlink(
32187c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32197c478bd9Sstevel@tonic-gate 	uio_t *uiop,
32207c478bd9Sstevel@tonic-gate 	cred_t *cr)
32217c478bd9Sstevel@tonic-gate {
32225a59a8b3Srsb 	int	err;
32235a59a8b3Srsb 
3224f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3225f48205beScasper 
32265a59a8b3Srsb 	err = (*(vp)->v_op->vop_readlink)(vp, uiop, cr);
32272bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, readlink);
32285a59a8b3Srsb 	return (err);
32297c478bd9Sstevel@tonic-gate }
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate int
32327c478bd9Sstevel@tonic-gate fop_fsync(
32337c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32347c478bd9Sstevel@tonic-gate 	int syncflag,
32357c478bd9Sstevel@tonic-gate 	cred_t *cr)
32367c478bd9Sstevel@tonic-gate {
32375a59a8b3Srsb 	int	err;
32385a59a8b3Srsb 
3239f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3240f48205beScasper 
32415a59a8b3Srsb 	err = (*(vp)->v_op->vop_fsync)(vp, syncflag, cr);
32422bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, fsync);
32435a59a8b3Srsb 	return (err);
32447c478bd9Sstevel@tonic-gate }
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate void
32477c478bd9Sstevel@tonic-gate fop_inactive(
32487c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32497c478bd9Sstevel@tonic-gate 	cred_t *cr)
32507c478bd9Sstevel@tonic-gate {
32515a59a8b3Srsb 	/* Need to update stats before vop call since we may lose the vnode */
32522bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, inactive);
3253f48205beScasper 
3254f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3255f48205beScasper 
32567c478bd9Sstevel@tonic-gate 	(*(vp)->v_op->vop_inactive)(vp, cr);
32577c478bd9Sstevel@tonic-gate }
32587c478bd9Sstevel@tonic-gate 
32597c478bd9Sstevel@tonic-gate int
32607c478bd9Sstevel@tonic-gate fop_fid(
32617c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32627c478bd9Sstevel@tonic-gate 	fid_t *fidp)
32637c478bd9Sstevel@tonic-gate {
32645a59a8b3Srsb 	int	err;
32655a59a8b3Srsb 
32665a59a8b3Srsb 	err = (*(vp)->v_op->vop_fid)(vp, fidp);
32672bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, fid);
32685a59a8b3Srsb 	return (err);
32697c478bd9Sstevel@tonic-gate }
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate int
32727c478bd9Sstevel@tonic-gate fop_rwlock(
32737c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32747c478bd9Sstevel@tonic-gate 	int write_lock,
32757c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
32767c478bd9Sstevel@tonic-gate {
32775a59a8b3Srsb 	int	ret;
32785a59a8b3Srsb 
32795a59a8b3Srsb 	ret = ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct));
32802bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, rwlock);
32815a59a8b3Srsb 	return (ret);
32827c478bd9Sstevel@tonic-gate }
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate void
32857c478bd9Sstevel@tonic-gate fop_rwunlock(
32867c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32877c478bd9Sstevel@tonic-gate 	int write_lock,
32887c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
32897c478bd9Sstevel@tonic-gate {
32907c478bd9Sstevel@tonic-gate 	(*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct);
32912bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, rwunlock);
32927c478bd9Sstevel@tonic-gate }
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate int
32957c478bd9Sstevel@tonic-gate fop_seek(
32967c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32977c478bd9Sstevel@tonic-gate 	offset_t ooff,
32987c478bd9Sstevel@tonic-gate 	offset_t *noffp)
32997c478bd9Sstevel@tonic-gate {
33005a59a8b3Srsb 	int	err;
33015a59a8b3Srsb 
33025a59a8b3Srsb 	err = (*(vp)->v_op->vop_seek)(vp, ooff, noffp);
33032bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, seek);
33045a59a8b3Srsb 	return (err);
33057c478bd9Sstevel@tonic-gate }
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate int
33087c478bd9Sstevel@tonic-gate fop_cmp(
33097c478bd9Sstevel@tonic-gate 	vnode_t *vp1,
33107c478bd9Sstevel@tonic-gate 	vnode_t *vp2)
33117c478bd9Sstevel@tonic-gate {
33125a59a8b3Srsb 	int	err;
33135a59a8b3Srsb 
33145a59a8b3Srsb 	err = (*(vp1)->v_op->vop_cmp)(vp1, vp2);
33152bb1cb30Sbmc 	VOPSTATS_UPDATE(vp1, cmp);
33165a59a8b3Srsb 	return (err);
33177c478bd9Sstevel@tonic-gate }
33187c478bd9Sstevel@tonic-gate 
33197c478bd9Sstevel@tonic-gate int
33207c478bd9Sstevel@tonic-gate fop_frlock(
33217c478bd9Sstevel@tonic-gate 	vnode_t *vp,
33227c478bd9Sstevel@tonic-gate 	int cmd,
33237c478bd9Sstevel@tonic-gate 	flock64_t *bfp,
33247c478bd9Sstevel@tonic-gate 	int flag,
33257c478bd9Sstevel@tonic-gate 	offset_t offset,
33267c478bd9Sstevel@tonic-gate 	struct flk_callback *flk_cbp,
33277c478bd9Sstevel@tonic-gate 	cred_t *cr)
33287c478bd9Sstevel@tonic-gate {
33295a59a8b3Srsb 	int	err;
33305a59a8b3Srsb 
3331f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3332f48205beScasper 
33335a59a8b3Srsb 	err = (*(vp)->v_op->vop_frlock)
33347c478bd9Sstevel@tonic-gate 	    (vp, cmd, bfp, flag, offset, flk_cbp, cr);
33352bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, frlock);
33365a59a8b3Srsb 	return (err);
33377c478bd9Sstevel@tonic-gate }
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate int
33407c478bd9Sstevel@tonic-gate fop_space(
33417c478bd9Sstevel@tonic-gate 	vnode_t *vp,
33427c478bd9Sstevel@tonic-gate 	int cmd,
33437c478bd9Sstevel@tonic-gate 	flock64_t *bfp,
33447c478bd9Sstevel@tonic-gate 	int flag,
33457c478bd9Sstevel@tonic-gate 	offset_t offset,
33467c478bd9Sstevel@tonic-gate 	cred_t *cr,
33477c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
33487c478bd9Sstevel@tonic-gate {
33495a59a8b3Srsb 	int	err;
33505a59a8b3Srsb 
3351f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3352f48205beScasper 
33535a59a8b3Srsb 	err = (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct);
33542bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, space);
33555a59a8b3Srsb 	return (err);
33567c478bd9Sstevel@tonic-gate }
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate int
33597c478bd9Sstevel@tonic-gate fop_realvp(
33607c478bd9Sstevel@tonic-gate 	vnode_t *vp,
33617c478bd9Sstevel@tonic-gate 	vnode_t **vpp)
33627c478bd9Sstevel@tonic-gate {
33635a59a8b3Srsb 	int	err;
33645a59a8b3Srsb 
33655a59a8b3Srsb 	err = (*(vp)->v_op->vop_realvp)(vp, vpp);
33662bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, realvp);
33675a59a8b3Srsb 	return (err);
33687c478bd9Sstevel@tonic-gate }
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate int
33717c478bd9Sstevel@tonic-gate fop_getpage(
33727c478bd9Sstevel@tonic-gate 	vnode_t *vp,
33737c478bd9Sstevel@tonic-gate 	offset_t off,
33747c478bd9Sstevel@tonic-gate 	size_t len,
33757c478bd9Sstevel@tonic-gate 	uint_t *protp,
33767c478bd9Sstevel@tonic-gate 	page_t **plarr,
33777c478bd9Sstevel@tonic-gate 	size_t plsz,
33787c478bd9Sstevel@tonic-gate 	struct seg *seg,
33797c478bd9Sstevel@tonic-gate 	caddr_t addr,
33807c478bd9Sstevel@tonic-gate 	enum seg_rw rw,
33817c478bd9Sstevel@tonic-gate 	cred_t *cr)
33827c478bd9Sstevel@tonic-gate {
33835a59a8b3Srsb 	int	err;
33845a59a8b3Srsb 
3385f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3386f48205beScasper 
33875a59a8b3Srsb 	err = (*(vp)->v_op->vop_getpage)
33887c478bd9Sstevel@tonic-gate 	    (vp, off, len, protp, plarr, plsz, seg, addr, rw, cr);
33892bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getpage);
33905a59a8b3Srsb 	return (err);
33917c478bd9Sstevel@tonic-gate }
33927c478bd9Sstevel@tonic-gate 
33937c478bd9Sstevel@tonic-gate int
33947c478bd9Sstevel@tonic-gate fop_putpage(
33957c478bd9Sstevel@tonic-gate 	vnode_t *vp,
33967c478bd9Sstevel@tonic-gate 	offset_t off,
33977c478bd9Sstevel@tonic-gate 	size_t len,
33987c478bd9Sstevel@tonic-gate 	int flags,
33997c478bd9Sstevel@tonic-gate 	cred_t *cr)
34007c478bd9Sstevel@tonic-gate {
34015a59a8b3Srsb 	int	err;
34025a59a8b3Srsb 
3403f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3404f48205beScasper 
34055a59a8b3Srsb 	err = (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr);
34062bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, putpage);
34075a59a8b3Srsb 	return (err);
34087c478bd9Sstevel@tonic-gate }
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate int
34117c478bd9Sstevel@tonic-gate fop_map(
34127c478bd9Sstevel@tonic-gate 	vnode_t *vp,
34137c478bd9Sstevel@tonic-gate 	offset_t off,
34147c478bd9Sstevel@tonic-gate 	struct as *as,
34157c478bd9Sstevel@tonic-gate 	caddr_t *addrp,
34167c478bd9Sstevel@tonic-gate 	size_t len,
34177c478bd9Sstevel@tonic-gate 	uchar_t prot,
34187c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
34197c478bd9Sstevel@tonic-gate 	uint_t flags,
34207c478bd9Sstevel@tonic-gate 	cred_t *cr)
34217c478bd9Sstevel@tonic-gate {
34225a59a8b3Srsb 	int	err;
34235a59a8b3Srsb 
3424f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3425f48205beScasper 
34265a59a8b3Srsb 	err = (*(vp)->v_op->vop_map)
34277c478bd9Sstevel@tonic-gate 	    (vp, off, as, addrp, len, prot, maxprot, flags, cr);
34282bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, map);
34295a59a8b3Srsb 	return (err);
34307c478bd9Sstevel@tonic-gate }
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate int
34337c478bd9Sstevel@tonic-gate fop_addmap(
34347c478bd9Sstevel@tonic-gate 	vnode_t *vp,
34357c478bd9Sstevel@tonic-gate 	offset_t off,
34367c478bd9Sstevel@tonic-gate 	struct as *as,
34377c478bd9Sstevel@tonic-gate 	caddr_t addr,
34387c478bd9Sstevel@tonic-gate 	size_t len,
34397c478bd9Sstevel@tonic-gate 	uchar_t prot,
34407c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
34417c478bd9Sstevel@tonic-gate 	uint_t flags,
34427c478bd9Sstevel@tonic-gate 	cred_t *cr)
34437c478bd9Sstevel@tonic-gate {
34447c478bd9Sstevel@tonic-gate 	int error;
34457c478bd9Sstevel@tonic-gate 	u_longlong_t delta;
34467c478bd9Sstevel@tonic-gate 
3447f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3448f48205beScasper 
34497c478bd9Sstevel@tonic-gate 	error = (*(vp)->v_op->vop_addmap)
34507c478bd9Sstevel@tonic-gate 	    (vp, off, as, addr, len, prot, maxprot, flags, cr);
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate 	if ((!error) && (vp->v_type == VREG)) {
34537c478bd9Sstevel@tonic-gate 		delta = (u_longlong_t)btopr(len);
34547c478bd9Sstevel@tonic-gate 		/*
34557c478bd9Sstevel@tonic-gate 		 * If file is declared MAP_PRIVATE, it can't be written back
34567c478bd9Sstevel@tonic-gate 		 * even if open for write. Handle as read.
34577c478bd9Sstevel@tonic-gate 		 */
34587c478bd9Sstevel@tonic-gate 		if (flags & MAP_PRIVATE) {
34597c478bd9Sstevel@tonic-gate 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
34607c478bd9Sstevel@tonic-gate 			    (int64_t)delta);
34617c478bd9Sstevel@tonic-gate 		} else {
34627c478bd9Sstevel@tonic-gate 			/*
34637c478bd9Sstevel@tonic-gate 			 * atomic_add_64 forces the fetch of a 64 bit value to
34647c478bd9Sstevel@tonic-gate 			 * be atomic on 32 bit machines
34657c478bd9Sstevel@tonic-gate 			 */
34667c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_WRITE)
34677c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
34687c478bd9Sstevel@tonic-gate 				    (int64_t)delta);
34697c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_READ)
34707c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
34717c478bd9Sstevel@tonic-gate 				    (int64_t)delta);
34727c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_EXEC)
34737c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
34747c478bd9Sstevel@tonic-gate 				    (int64_t)delta);
34757c478bd9Sstevel@tonic-gate 		}
34767c478bd9Sstevel@tonic-gate 	}
34772bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, addmap);
34787c478bd9Sstevel@tonic-gate 	return (error);
34797c478bd9Sstevel@tonic-gate }
34807c478bd9Sstevel@tonic-gate 
34817c478bd9Sstevel@tonic-gate int
34827c478bd9Sstevel@tonic-gate fop_delmap(
34837c478bd9Sstevel@tonic-gate 	vnode_t *vp,
34847c478bd9Sstevel@tonic-gate 	offset_t off,
34857c478bd9Sstevel@tonic-gate 	struct as *as,
34867c478bd9Sstevel@tonic-gate 	caddr_t addr,
34877c478bd9Sstevel@tonic-gate 	size_t len,
34887c478bd9Sstevel@tonic-gate 	uint_t prot,
34897c478bd9Sstevel@tonic-gate 	uint_t maxprot,
34907c478bd9Sstevel@tonic-gate 	uint_t flags,
34917c478bd9Sstevel@tonic-gate 	cred_t *cr)
34927c478bd9Sstevel@tonic-gate {
34937c478bd9Sstevel@tonic-gate 	int error;
34947c478bd9Sstevel@tonic-gate 	u_longlong_t delta;
3495f48205beScasper 
3496f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3497f48205beScasper 
34987c478bd9Sstevel@tonic-gate 	error = (*(vp)->v_op->vop_delmap)
34997c478bd9Sstevel@tonic-gate 	    (vp, off, as, addr, len, prot, maxprot, flags, cr);
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 	/*
35027c478bd9Sstevel@tonic-gate 	 * NFS calls into delmap twice, the first time
35037c478bd9Sstevel@tonic-gate 	 * it simply establishes a callback mechanism and returns EAGAIN
35047c478bd9Sstevel@tonic-gate 	 * while the real work is being done upon the second invocation.
35057c478bd9Sstevel@tonic-gate 	 * We have to detect this here and only decrement the counts upon
35067c478bd9Sstevel@tonic-gate 	 * the second delmap request.
35077c478bd9Sstevel@tonic-gate 	 */
35087c478bd9Sstevel@tonic-gate 	if ((error != EAGAIN) && (vp->v_type == VREG)) {
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 		delta = (u_longlong_t)btopr(len);
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate 		if (flags & MAP_PRIVATE) {
35137c478bd9Sstevel@tonic-gate 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
35147c478bd9Sstevel@tonic-gate 			    (int64_t)(-delta));
35157c478bd9Sstevel@tonic-gate 		} else {
35167c478bd9Sstevel@tonic-gate 			/*
35177c478bd9Sstevel@tonic-gate 			 * atomic_add_64 forces the fetch of a 64 bit value
35187c478bd9Sstevel@tonic-gate 			 * to be atomic on 32 bit machines
35197c478bd9Sstevel@tonic-gate 			 */
35207c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_WRITE)
35217c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
35227c478bd9Sstevel@tonic-gate 				    (int64_t)(-delta));
35237c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_READ)
35247c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
35257c478bd9Sstevel@tonic-gate 				    (int64_t)(-delta));
35267c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_EXEC)
35277c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
35287c478bd9Sstevel@tonic-gate 				    (int64_t)(-delta));
35297c478bd9Sstevel@tonic-gate 		}
35307c478bd9Sstevel@tonic-gate 	}
35312bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, delmap);
35327c478bd9Sstevel@tonic-gate 	return (error);
35337c478bd9Sstevel@tonic-gate }
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate int
35377c478bd9Sstevel@tonic-gate fop_poll(
35387c478bd9Sstevel@tonic-gate 	vnode_t *vp,
35397c478bd9Sstevel@tonic-gate 	short events,
35407c478bd9Sstevel@tonic-gate 	int anyyet,
35417c478bd9Sstevel@tonic-gate 	short *reventsp,
35427c478bd9Sstevel@tonic-gate 	struct pollhead **phpp)
35437c478bd9Sstevel@tonic-gate {
35445a59a8b3Srsb 	int	err;
35455a59a8b3Srsb 
35465a59a8b3Srsb 	err = (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp);
35472bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, poll);
35485a59a8b3Srsb 	return (err);
35497c478bd9Sstevel@tonic-gate }
35507c478bd9Sstevel@tonic-gate 
35517c478bd9Sstevel@tonic-gate int
35527c478bd9Sstevel@tonic-gate fop_dump(
35537c478bd9Sstevel@tonic-gate 	vnode_t *vp,
35547c478bd9Sstevel@tonic-gate 	caddr_t addr,
35557c478bd9Sstevel@tonic-gate 	int lbdn,
35567c478bd9Sstevel@tonic-gate 	int dblks)
35577c478bd9Sstevel@tonic-gate {
35585a59a8b3Srsb 	int	err;
35595a59a8b3Srsb 
35605a59a8b3Srsb 	err = (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks);
35612bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dump);
35625a59a8b3Srsb 	return (err);
35637c478bd9Sstevel@tonic-gate }
35647c478bd9Sstevel@tonic-gate 
35657c478bd9Sstevel@tonic-gate int
35667c478bd9Sstevel@tonic-gate fop_pathconf(
35677c478bd9Sstevel@tonic-gate 	vnode_t *vp,
35687c478bd9Sstevel@tonic-gate 	int cmd,
35697c478bd9Sstevel@tonic-gate 	ulong_t *valp,
35707c478bd9Sstevel@tonic-gate 	cred_t *cr)
35717c478bd9Sstevel@tonic-gate {
35725a59a8b3Srsb 	int	err;
35735a59a8b3Srsb 
3574f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3575f48205beScasper 
35765a59a8b3Srsb 	err = (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr);
35772bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, pathconf);
35785a59a8b3Srsb 	return (err);
35797c478bd9Sstevel@tonic-gate }
35807c478bd9Sstevel@tonic-gate 
35817c478bd9Sstevel@tonic-gate int
35827c478bd9Sstevel@tonic-gate fop_pageio(
35837c478bd9Sstevel@tonic-gate 	vnode_t *vp,
35847c478bd9Sstevel@tonic-gate 	struct page *pp,
35857c478bd9Sstevel@tonic-gate 	u_offset_t io_off,
35867c478bd9Sstevel@tonic-gate 	size_t io_len,
35877c478bd9Sstevel@tonic-gate 	int flags,
35887c478bd9Sstevel@tonic-gate 	cred_t *cr)
35897c478bd9Sstevel@tonic-gate {
35905a59a8b3Srsb 	int	err;
35915a59a8b3Srsb 
3592f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3593f48205beScasper 
35945a59a8b3Srsb 	err = (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr);
35952bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, pageio);
35965a59a8b3Srsb 	return (err);
35977c478bd9Sstevel@tonic-gate }
35987c478bd9Sstevel@tonic-gate 
35997c478bd9Sstevel@tonic-gate int
36007c478bd9Sstevel@tonic-gate fop_dumpctl(
36017c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36027c478bd9Sstevel@tonic-gate 	int action,
36037c478bd9Sstevel@tonic-gate 	int *blkp)
36047c478bd9Sstevel@tonic-gate {
36055a59a8b3Srsb 	int	err;
36065a59a8b3Srsb 	err = (*(vp)->v_op->vop_dumpctl)(vp, action, blkp);
36072bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dumpctl);
36085a59a8b3Srsb 	return (err);
36097c478bd9Sstevel@tonic-gate }
36107c478bd9Sstevel@tonic-gate 
36117c478bd9Sstevel@tonic-gate void
36127c478bd9Sstevel@tonic-gate fop_dispose(
36137c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36147c478bd9Sstevel@tonic-gate 	page_t *pp,
36157c478bd9Sstevel@tonic-gate 	int flag,
36167c478bd9Sstevel@tonic-gate 	int dn,
36177c478bd9Sstevel@tonic-gate 	cred_t *cr)
36187c478bd9Sstevel@tonic-gate {
36195a59a8b3Srsb 	/* Must do stats first since it's possible to lose the vnode */
36202bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dispose);
3621f48205beScasper 
3622f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3623f48205beScasper 
36247c478bd9Sstevel@tonic-gate 	(*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr);
36257c478bd9Sstevel@tonic-gate }
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate int
36287c478bd9Sstevel@tonic-gate fop_setsecattr(
36297c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36307c478bd9Sstevel@tonic-gate 	vsecattr_t *vsap,
36317c478bd9Sstevel@tonic-gate 	int flag,
36327c478bd9Sstevel@tonic-gate 	cred_t *cr)
36337c478bd9Sstevel@tonic-gate {
36345a59a8b3Srsb 	int	err;
36355a59a8b3Srsb 
3636f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3637f48205beScasper 
36385a59a8b3Srsb 	err = (*(vp)->v_op->vop_setsecattr) (vp, vsap, flag, cr);
36392bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setsecattr);
36405a59a8b3Srsb 	return (err);
36417c478bd9Sstevel@tonic-gate }
36427c478bd9Sstevel@tonic-gate 
36437c478bd9Sstevel@tonic-gate int
36447c478bd9Sstevel@tonic-gate fop_getsecattr(
36457c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36467c478bd9Sstevel@tonic-gate 	vsecattr_t *vsap,
36477c478bd9Sstevel@tonic-gate 	int flag,
36487c478bd9Sstevel@tonic-gate 	cred_t *cr)
36497c478bd9Sstevel@tonic-gate {
36505a59a8b3Srsb 	int	err;
36515a59a8b3Srsb 
3652f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3653f48205beScasper 
36545a59a8b3Srsb 	err = (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr);
36552bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getsecattr);
36565a59a8b3Srsb 	return (err);
36577c478bd9Sstevel@tonic-gate }
36587c478bd9Sstevel@tonic-gate 
36597c478bd9Sstevel@tonic-gate int
36607c478bd9Sstevel@tonic-gate fop_shrlock(
36617c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36627c478bd9Sstevel@tonic-gate 	int cmd,
36637c478bd9Sstevel@tonic-gate 	struct shrlock *shr,
36647c478bd9Sstevel@tonic-gate 	int flag,
36657c478bd9Sstevel@tonic-gate 	cred_t *cr)
36667c478bd9Sstevel@tonic-gate {
36675a59a8b3Srsb 	int	err;
36685a59a8b3Srsb 
3669f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3670f48205beScasper 
36715a59a8b3Srsb 	err = (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr);
36722bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, shrlock);
36735a59a8b3Srsb 	return (err);
36747c478bd9Sstevel@tonic-gate }
36757c478bd9Sstevel@tonic-gate 
36767c478bd9Sstevel@tonic-gate int
3677df2381bfSpraks fop_vnevent(vnode_t *vp, vnevent_t vnevent, vnode_t *dvp, char *fnm)
36787c478bd9Sstevel@tonic-gate {
36795a59a8b3Srsb 	int	err;
36805a59a8b3Srsb 
3681df2381bfSpraks 	err = (*(vp)->v_op->vop_vnevent)(vp, vnevent, dvp, fnm);
36822bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, vnevent);
36835a59a8b3Srsb 	return (err);
36847c478bd9Sstevel@tonic-gate }
3685*1b300de9Sjwahlig 
3686*1b300de9Sjwahlig /*
3687*1b300de9Sjwahlig  * Default destructor
3688*1b300de9Sjwahlig  *	Needed because NULL destructor means that the key is unused
3689*1b300de9Sjwahlig  */
3690*1b300de9Sjwahlig /* ARGSUSED */
3691*1b300de9Sjwahlig void
3692*1b300de9Sjwahlig vsd_defaultdestructor(void *value)
3693*1b300de9Sjwahlig {}
3694*1b300de9Sjwahlig 
3695*1b300de9Sjwahlig /*
3696*1b300de9Sjwahlig  * Create a key (index into per vnode array)
3697*1b300de9Sjwahlig  *	Locks out vsd_create, vsd_destroy, and vsd_free
3698*1b300de9Sjwahlig  *	May allocate memory with lock held
3699*1b300de9Sjwahlig  */
3700*1b300de9Sjwahlig void
3701*1b300de9Sjwahlig vsd_create(uint_t *keyp, void (*destructor)(void *))
3702*1b300de9Sjwahlig {
3703*1b300de9Sjwahlig 	int	i;
3704*1b300de9Sjwahlig 	uint_t	nkeys;
3705*1b300de9Sjwahlig 
3706*1b300de9Sjwahlig 	/*
3707*1b300de9Sjwahlig 	 * if key is allocated, do nothing
3708*1b300de9Sjwahlig 	 */
3709*1b300de9Sjwahlig 	mutex_enter(&vsd_lock);
3710*1b300de9Sjwahlig 	if (*keyp) {
3711*1b300de9Sjwahlig 		mutex_exit(&vsd_lock);
3712*1b300de9Sjwahlig 		return;
3713*1b300de9Sjwahlig 	}
3714*1b300de9Sjwahlig 	/*
3715*1b300de9Sjwahlig 	 * find an unused key
3716*1b300de9Sjwahlig 	 */
3717*1b300de9Sjwahlig 	if (destructor == NULL)
3718*1b300de9Sjwahlig 		destructor = vsd_defaultdestructor;
3719*1b300de9Sjwahlig 
3720*1b300de9Sjwahlig 	for (i = 0; i < vsd_nkeys; ++i)
3721*1b300de9Sjwahlig 		if (vsd_destructor[i] == NULL)
3722*1b300de9Sjwahlig 			break;
3723*1b300de9Sjwahlig 
3724*1b300de9Sjwahlig 	/*
3725*1b300de9Sjwahlig 	 * if no unused keys, increase the size of the destructor array
3726*1b300de9Sjwahlig 	 */
3727*1b300de9Sjwahlig 	if (i == vsd_nkeys) {
3728*1b300de9Sjwahlig 		if ((nkeys = (vsd_nkeys << 1)) == 0)
3729*1b300de9Sjwahlig 			nkeys = 1;
3730*1b300de9Sjwahlig 		vsd_destructor =
3731*1b300de9Sjwahlig 		    (void (**)(void *))vsd_realloc((void *)vsd_destructor,
3732*1b300de9Sjwahlig 		    (size_t)(vsd_nkeys * sizeof (void (*)(void *))),
3733*1b300de9Sjwahlig 		    (size_t)(nkeys * sizeof (void (*)(void *))));
3734*1b300de9Sjwahlig 		vsd_nkeys = nkeys;
3735*1b300de9Sjwahlig 	}
3736*1b300de9Sjwahlig 
3737*1b300de9Sjwahlig 	/*
3738*1b300de9Sjwahlig 	 * allocate the next available unused key
3739*1b300de9Sjwahlig 	 */
3740*1b300de9Sjwahlig 	vsd_destructor[i] = destructor;
3741*1b300de9Sjwahlig 	*keyp = i + 1;
3742*1b300de9Sjwahlig 
3743*1b300de9Sjwahlig 	/* create vsd_list, if it doesn't exist */
3744*1b300de9Sjwahlig 	if (vsd_list == NULL) {
3745*1b300de9Sjwahlig 		vsd_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
3746*1b300de9Sjwahlig 		list_create(vsd_list, sizeof (struct vsd_node),
3747*1b300de9Sjwahlig 		    offsetof(struct vsd_node, vs_nodes));
3748*1b300de9Sjwahlig 	}
3749*1b300de9Sjwahlig 
3750*1b300de9Sjwahlig 	mutex_exit(&vsd_lock);
3751*1b300de9Sjwahlig }
3752*1b300de9Sjwahlig 
3753*1b300de9Sjwahlig /*
3754*1b300de9Sjwahlig  * Destroy a key
3755*1b300de9Sjwahlig  *
3756*1b300de9Sjwahlig  * Assumes that the caller is preventing vsd_set and vsd_get
3757*1b300de9Sjwahlig  * Locks out vsd_create, vsd_destroy, and vsd_free
3758*1b300de9Sjwahlig  * May free memory with lock held
3759*1b300de9Sjwahlig  */
3760*1b300de9Sjwahlig void
3761*1b300de9Sjwahlig vsd_destroy(uint_t *keyp)
3762*1b300de9Sjwahlig {
3763*1b300de9Sjwahlig 	uint_t key;
3764*1b300de9Sjwahlig 	struct vsd_node *vsd;
3765*1b300de9Sjwahlig 
3766*1b300de9Sjwahlig 	/*
3767*1b300de9Sjwahlig 	 * protect the key namespace and our destructor lists
3768*1b300de9Sjwahlig 	 */
3769*1b300de9Sjwahlig 	mutex_enter(&vsd_lock);
3770*1b300de9Sjwahlig 	key = *keyp;
3771*1b300de9Sjwahlig 	*keyp = 0;
3772*1b300de9Sjwahlig 
3773*1b300de9Sjwahlig 	ASSERT(key <= vsd_nkeys);
3774*1b300de9Sjwahlig 
3775*1b300de9Sjwahlig 	/*
3776*1b300de9Sjwahlig 	 * if the key is valid
3777*1b300de9Sjwahlig 	 */
3778*1b300de9Sjwahlig 	if (key != 0) {
3779*1b300de9Sjwahlig 		uint_t k = key - 1;
3780*1b300de9Sjwahlig 		/*
3781*1b300de9Sjwahlig 		 * for every vnode with VSD, call key's destructor
3782*1b300de9Sjwahlig 		 */
3783*1b300de9Sjwahlig 		for (vsd = list_head(vsd_list); vsd != NULL;
3784*1b300de9Sjwahlig 		    vsd = list_next(vsd_list, vsd)) {
3785*1b300de9Sjwahlig 			/*
3786*1b300de9Sjwahlig 			 * no VSD for key in this vnode
3787*1b300de9Sjwahlig 			 */
3788*1b300de9Sjwahlig 			if (key > vsd->vs_nkeys)
3789*1b300de9Sjwahlig 				continue;
3790*1b300de9Sjwahlig 			/*
3791*1b300de9Sjwahlig 			 * call destructor for key
3792*1b300de9Sjwahlig 			 */
3793*1b300de9Sjwahlig 			if (vsd->vs_value[k] && vsd_destructor[k])
3794*1b300de9Sjwahlig 				(*vsd_destructor[k])(vsd->vs_value[k]);
3795*1b300de9Sjwahlig 			/*
3796*1b300de9Sjwahlig 			 * reset value for key
3797*1b300de9Sjwahlig 			 */
3798*1b300de9Sjwahlig 			vsd->vs_value[k] = NULL;
3799*1b300de9Sjwahlig 		}
3800*1b300de9Sjwahlig 		/*
3801*1b300de9Sjwahlig 		 * actually free the key (NULL destructor == unused)
3802*1b300de9Sjwahlig 		 */
3803*1b300de9Sjwahlig 		vsd_destructor[k] = NULL;
3804*1b300de9Sjwahlig 	}
3805*1b300de9Sjwahlig 
3806*1b300de9Sjwahlig 	mutex_exit(&vsd_lock);
3807*1b300de9Sjwahlig }
3808*1b300de9Sjwahlig 
3809*1b300de9Sjwahlig /*
3810*1b300de9Sjwahlig  * Quickly return the per vnode value that was stored with the specified key
3811*1b300de9Sjwahlig  * Assumes the caller is protecting key from vsd_create and vsd_destroy
3812*1b300de9Sjwahlig  * Assumes the caller is holding v_lock to protect the vsd.
3813*1b300de9Sjwahlig  */
3814*1b300de9Sjwahlig void *
3815*1b300de9Sjwahlig vsd_get(vnode_t *vp, uint_t key)
3816*1b300de9Sjwahlig {
3817*1b300de9Sjwahlig 	struct vsd_node *vsd;
3818*1b300de9Sjwahlig 
3819*1b300de9Sjwahlig 	/*
3820*1b300de9Sjwahlig 	 * The caller needs to pass a valid vnode.
3821*1b300de9Sjwahlig 	 */
3822*1b300de9Sjwahlig 	ASSERT(vp != NULL);
3823*1b300de9Sjwahlig 	if (vp == NULL)
3824*1b300de9Sjwahlig 		return (NULL);
3825*1b300de9Sjwahlig 
3826*1b300de9Sjwahlig 	vsd = vp->v_vsd;
3827*1b300de9Sjwahlig 
3828*1b300de9Sjwahlig 	if (key && vsd != NULL && key <= vsd->vs_nkeys)
3829*1b300de9Sjwahlig 		return (vsd->vs_value[key - 1]);
3830*1b300de9Sjwahlig 	return (NULL);
3831*1b300de9Sjwahlig }
3832*1b300de9Sjwahlig 
3833*1b300de9Sjwahlig /*
3834*1b300de9Sjwahlig  * Set a per vnode value indexed with the specified key
3835*1b300de9Sjwahlig  * Assumes the caller is holding v_lock to protect the vsd.
3836*1b300de9Sjwahlig  */
3837*1b300de9Sjwahlig int
3838*1b300de9Sjwahlig vsd_set(vnode_t *vp, uint_t key, void *value)
3839*1b300de9Sjwahlig {
3840*1b300de9Sjwahlig 	struct vsd_node *vsd = vp->v_vsd;
3841*1b300de9Sjwahlig 
3842*1b300de9Sjwahlig 	if (key == 0)
3843*1b300de9Sjwahlig 		return (EINVAL);
3844*1b300de9Sjwahlig 	if (vsd == NULL)
3845*1b300de9Sjwahlig 		vsd = vp->v_vsd = kmem_zalloc(sizeof (*vsd), KM_SLEEP);
3846*1b300de9Sjwahlig 
3847*1b300de9Sjwahlig 	/*
3848*1b300de9Sjwahlig 	 * If the vsd was just allocated, vs_nkeys will be 0, so the following
3849*1b300de9Sjwahlig 	 * code won't happen and we will continue down and allocate space for
3850*1b300de9Sjwahlig 	 * the vs_value array.
3851*1b300de9Sjwahlig 	 * If the caller is replacing one value with another, then it is up
3852*1b300de9Sjwahlig 	 * to the caller to free/rele/destroy the previous value (if needed).
3853*1b300de9Sjwahlig 	 */
3854*1b300de9Sjwahlig 	if (key <= vsd->vs_nkeys) {
3855*1b300de9Sjwahlig 		vsd->vs_value[key - 1] = value;
3856*1b300de9Sjwahlig 		return (0);
3857*1b300de9Sjwahlig 	}
3858*1b300de9Sjwahlig 
3859*1b300de9Sjwahlig 	ASSERT(key <= vsd_nkeys);
3860*1b300de9Sjwahlig 
3861*1b300de9Sjwahlig 	if (vsd->vs_nkeys == 0) {
3862*1b300de9Sjwahlig 		mutex_enter(&vsd_lock);	/* lock out vsd_destroy() */
3863*1b300de9Sjwahlig 		/*
3864*1b300de9Sjwahlig 		 * Link onto list of all VSD nodes.
3865*1b300de9Sjwahlig 		 */
3866*1b300de9Sjwahlig 		list_insert_head(vsd_list, vsd);
3867*1b300de9Sjwahlig 		mutex_exit(&vsd_lock);
3868*1b300de9Sjwahlig 	}
3869*1b300de9Sjwahlig 
3870*1b300de9Sjwahlig 	/*
3871*1b300de9Sjwahlig 	 * Allocate vnode local storage and set the value for key
3872*1b300de9Sjwahlig 	 */
3873*1b300de9Sjwahlig 	vsd->vs_value = vsd_realloc(vsd->vs_value,
3874*1b300de9Sjwahlig 	    vsd->vs_nkeys * sizeof (void *),
3875*1b300de9Sjwahlig 	    key * sizeof (void *));
3876*1b300de9Sjwahlig 	vsd->vs_nkeys = key;
3877*1b300de9Sjwahlig 	vsd->vs_value[key - 1] = value;
3878*1b300de9Sjwahlig 
3879*1b300de9Sjwahlig 	return (0);
3880*1b300de9Sjwahlig }
3881*1b300de9Sjwahlig 
3882*1b300de9Sjwahlig /*
3883*1b300de9Sjwahlig  * Called from vn_free() to run the destructor function for each vsd
3884*1b300de9Sjwahlig  *	Locks out vsd_create and vsd_destroy
3885*1b300de9Sjwahlig  *	Assumes that the destructor *DOES NOT* use vsd
3886*1b300de9Sjwahlig  */
3887*1b300de9Sjwahlig void
3888*1b300de9Sjwahlig vsd_free(vnode_t *vp)
3889*1b300de9Sjwahlig {
3890*1b300de9Sjwahlig 	int i;
3891*1b300de9Sjwahlig 	struct vsd_node *vsd = vp->v_vsd;
3892*1b300de9Sjwahlig 
3893*1b300de9Sjwahlig 	if (vsd == NULL)
3894*1b300de9Sjwahlig 		return;
3895*1b300de9Sjwahlig 
3896*1b300de9Sjwahlig 	if (vsd->vs_nkeys == 0) {
3897*1b300de9Sjwahlig 		kmem_free(vsd, sizeof (*vsd));
3898*1b300de9Sjwahlig 		vp->v_vsd = NULL;
3899*1b300de9Sjwahlig 		return;
3900*1b300de9Sjwahlig 	}
3901*1b300de9Sjwahlig 
3902*1b300de9Sjwahlig 	/*
3903*1b300de9Sjwahlig 	 * lock out vsd_create and vsd_destroy, call
3904*1b300de9Sjwahlig 	 * the destructor, and mark the value as destroyed.
3905*1b300de9Sjwahlig 	 */
3906*1b300de9Sjwahlig 	mutex_enter(&vsd_lock);
3907*1b300de9Sjwahlig 
3908*1b300de9Sjwahlig 	for (i = 0; i < vsd->vs_nkeys; i++) {
3909*1b300de9Sjwahlig 		if (vsd->vs_value[i] && vsd_destructor[i])
3910*1b300de9Sjwahlig 			(*vsd_destructor[i])(vsd->vs_value[i]);
3911*1b300de9Sjwahlig 		vsd->vs_value[i] = NULL;
3912*1b300de9Sjwahlig 	}
3913*1b300de9Sjwahlig 
3914*1b300de9Sjwahlig 	/*
3915*1b300de9Sjwahlig 	 * remove from linked list of VSD nodes
3916*1b300de9Sjwahlig 	 */
3917*1b300de9Sjwahlig 	list_remove(vsd_list, vsd);
3918*1b300de9Sjwahlig 
3919*1b300de9Sjwahlig 	mutex_exit(&vsd_lock);
3920*1b300de9Sjwahlig 
3921*1b300de9Sjwahlig 	/*
3922*1b300de9Sjwahlig 	 * free up the VSD
3923*1b300de9Sjwahlig 	 */
3924*1b300de9Sjwahlig 	kmem_free(vsd->vs_value, vsd->vs_nkeys * sizeof (void *));
3925*1b300de9Sjwahlig 	kmem_free(vsd, sizeof (struct vsd_node));
3926*1b300de9Sjwahlig 	vp->v_vsd = NULL;
3927*1b300de9Sjwahlig }
3928*1b300de9Sjwahlig 
3929*1b300de9Sjwahlig /*
3930*1b300de9Sjwahlig  * realloc
3931*1b300de9Sjwahlig  */
3932*1b300de9Sjwahlig static void *
3933*1b300de9Sjwahlig vsd_realloc(void *old, size_t osize, size_t nsize)
3934*1b300de9Sjwahlig {
3935*1b300de9Sjwahlig 	void *new;
3936*1b300de9Sjwahlig 
3937*1b300de9Sjwahlig 	new = kmem_zalloc(nsize, KM_SLEEP);
3938*1b300de9Sjwahlig 	if (old) {
3939*1b300de9Sjwahlig 		bcopy(old, new, osize);
3940*1b300de9Sjwahlig 		kmem_free(old, osize);
3941*1b300de9Sjwahlig 	}
3942*1b300de9Sjwahlig 	return (new);
3943*1b300de9Sjwahlig }
3944