xref: /titanic_53/usr/src/uts/common/fs/vnode.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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 /*
1061b300de9Sjwahlig  * forward declarations for internal vnode specific data (vsd)
1071b300de9Sjwahlig  */
1081b300de9Sjwahlig static void *vsd_realloc(void *, size_t, size_t);
1091b300de9Sjwahlig 
1101b300de9Sjwahlig /*
1111b300de9Sjwahlig  * VSD -- VNODE SPECIFIC DATA
1121b300de9Sjwahlig  * The v_data pointer is typically used by a file system to store a
1131b300de9Sjwahlig  * pointer to the file system's private node (e.g. ufs inode, nfs rnode).
1141b300de9Sjwahlig  * However, there are times when additional project private data needs
1151b300de9Sjwahlig  * to be stored separately from the data (node) pointed to by v_data.
1161b300de9Sjwahlig  * This additional data could be stored by the file system itself or
1171b300de9Sjwahlig  * by a completely different kernel entity.  VSD provides a way for
1181b300de9Sjwahlig  * callers to obtain a key and store a pointer to private data associated
1191b300de9Sjwahlig  * with a vnode.
1201b300de9Sjwahlig  *
1211b300de9Sjwahlig  * Callers are responsible for protecting the vsd by holding v_lock
1221b300de9Sjwahlig  * for calls to vsd_set() and vsd_get().
1231b300de9Sjwahlig  */
1241b300de9Sjwahlig 
1251b300de9Sjwahlig /*
1261b300de9Sjwahlig  * vsd_lock protects:
1271b300de9Sjwahlig  *   vsd_nkeys - creation and deletion of vsd keys
1281b300de9Sjwahlig  *   vsd_list - insertion and deletion of vsd_node in the vsd_list
1291b300de9Sjwahlig  *   vsd_destructor - adding and removing destructors to the list
1301b300de9Sjwahlig  */
1311b300de9Sjwahlig static kmutex_t		vsd_lock;
1321b300de9Sjwahlig static uint_t		vsd_nkeys;	 /* size of destructor array */
1331b300de9Sjwahlig /* list of vsd_node's */
1341b300de9Sjwahlig static list_t *vsd_list = NULL;
1351b300de9Sjwahlig /* per-key destructor funcs */
1361b300de9Sjwahlig static void 		(**vsd_destructor)(void *);
1371b300de9Sjwahlig 
1381b300de9Sjwahlig /*
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 
364*da6c28aaSamw /* Extensible attribute (xva) routines. */
365*da6c28aaSamw 
366*da6c28aaSamw /*
367*da6c28aaSamw  * Zero out the structure, set the size of the requested/returned bitmaps,
368*da6c28aaSamw  * set AT_XVATTR in the embedded vattr_t's va_mask, and set up the pointer
369*da6c28aaSamw  * to the returned attributes array.
370*da6c28aaSamw  */
371*da6c28aaSamw void
372*da6c28aaSamw xva_init(xvattr_t *xvap)
373*da6c28aaSamw {
374*da6c28aaSamw 	bzero(xvap, sizeof (xvattr_t));
375*da6c28aaSamw 	xvap->xva_mapsize = XVA_MAPSIZE;
376*da6c28aaSamw 	xvap->xva_magic = XVA_MAGIC;
377*da6c28aaSamw 	xvap->xva_vattr.va_mask = AT_XVATTR;
378*da6c28aaSamw 	xvap->xva_rtnattrmapp = &(xvap->xva_rtnattrmap)[0];
379*da6c28aaSamw }
380*da6c28aaSamw 
381*da6c28aaSamw /*
382*da6c28aaSamw  * If AT_XVATTR is set, returns a pointer to the embedded xoptattr_t
383*da6c28aaSamw  * structure.  Otherwise, returns NULL.
384*da6c28aaSamw  */
385*da6c28aaSamw xoptattr_t *
386*da6c28aaSamw xva_getxoptattr(xvattr_t *xvap)
387*da6c28aaSamw {
388*da6c28aaSamw 	xoptattr_t *xoap = NULL;
389*da6c28aaSamw 	if (xvap->xva_vattr.va_mask & AT_XVATTR)
390*da6c28aaSamw 		xoap = &xvap->xva_xoptattrs;
391*da6c28aaSamw 	return (xoap);
392*da6c28aaSamw }
393*da6c28aaSamw 
3945a59a8b3Srsb /*
3955a59a8b3Srsb  * Used by the AVL routines to compare two vsk_anchor_t structures in the tree.
3965a59a8b3Srsb  * We use the f_fsid reported by VFS_STATVFS() since we use that for the
3975a59a8b3Srsb  * kstat name.
3985a59a8b3Srsb  */
3995a59a8b3Srsb static int
4005a59a8b3Srsb vska_compar(const void *n1, const void *n2)
4015a59a8b3Srsb {
4025a59a8b3Srsb 	int ret;
4035a59a8b3Srsb 	ulong_t p1 = ((vsk_anchor_t *)n1)->vsk_fsid;
4045a59a8b3Srsb 	ulong_t p2 = ((vsk_anchor_t *)n2)->vsk_fsid;
4055a59a8b3Srsb 
4065a59a8b3Srsb 	if (p1 < p2) {
4075a59a8b3Srsb 		ret = -1;
4085a59a8b3Srsb 	} else if (p1 > p2) {
4095a59a8b3Srsb 		ret = 1;
4105a59a8b3Srsb 	} else {
4115a59a8b3Srsb 		ret = 0;
4125a59a8b3Srsb 	}
4135a59a8b3Srsb 
4145a59a8b3Srsb 	return (ret);
4155a59a8b3Srsb }
4165a59a8b3Srsb 
4175a59a8b3Srsb /*
4185a59a8b3Srsb  * Used to create a single template which will be bcopy()ed to a newly
4195a59a8b3Srsb  * allocated vsanchor_combo_t structure in new_vsanchor(), below.
4205a59a8b3Srsb  */
4215a59a8b3Srsb static vopstats_t *
4225a59a8b3Srsb create_vopstats_template()
4235a59a8b3Srsb {
4245a59a8b3Srsb 	vopstats_t		*vsp;
4255a59a8b3Srsb 
4265a59a8b3Srsb 	vsp = kmem_alloc(sizeof (vopstats_t), KM_SLEEP);
4275a59a8b3Srsb 	bzero(vsp, sizeof (*vsp));	/* Start fresh */
4285a59a8b3Srsb 
4295a59a8b3Srsb 	/* VOP_OPEN */
4305a59a8b3Srsb 	kstat_named_init(&vsp->nopen, "nopen", KSTAT_DATA_UINT64);
4315a59a8b3Srsb 	/* VOP_CLOSE */
4325a59a8b3Srsb 	kstat_named_init(&vsp->nclose, "nclose", KSTAT_DATA_UINT64);
4335a59a8b3Srsb 	/* VOP_READ I/O */
4345a59a8b3Srsb 	kstat_named_init(&vsp->nread, "nread", KSTAT_DATA_UINT64);
4355a59a8b3Srsb 	kstat_named_init(&vsp->read_bytes, "read_bytes", KSTAT_DATA_UINT64);
4365a59a8b3Srsb 	/* VOP_WRITE I/O */
4375a59a8b3Srsb 	kstat_named_init(&vsp->nwrite, "nwrite", KSTAT_DATA_UINT64);
4385a59a8b3Srsb 	kstat_named_init(&vsp->write_bytes, "write_bytes", KSTAT_DATA_UINT64);
4395a59a8b3Srsb 	/* VOP_IOCTL */
4405a59a8b3Srsb 	kstat_named_init(&vsp->nioctl, "nioctl", KSTAT_DATA_UINT64);
4415a59a8b3Srsb 	/* VOP_SETFL */
4425a59a8b3Srsb 	kstat_named_init(&vsp->nsetfl, "nsetfl", KSTAT_DATA_UINT64);
4435a59a8b3Srsb 	/* VOP_GETATTR */
4445a59a8b3Srsb 	kstat_named_init(&vsp->ngetattr, "ngetattr", KSTAT_DATA_UINT64);
4455a59a8b3Srsb 	/* VOP_SETATTR */
4465a59a8b3Srsb 	kstat_named_init(&vsp->nsetattr, "nsetattr", KSTAT_DATA_UINT64);
4475a59a8b3Srsb 	/* VOP_ACCESS */
4485a59a8b3Srsb 	kstat_named_init(&vsp->naccess, "naccess", KSTAT_DATA_UINT64);
4495a59a8b3Srsb 	/* VOP_LOOKUP */
4505a59a8b3Srsb 	kstat_named_init(&vsp->nlookup, "nlookup", KSTAT_DATA_UINT64);
4515a59a8b3Srsb 	/* VOP_CREATE */
4525a59a8b3Srsb 	kstat_named_init(&vsp->ncreate, "ncreate", KSTAT_DATA_UINT64);
4535a59a8b3Srsb 	/* VOP_REMOVE */
4545a59a8b3Srsb 	kstat_named_init(&vsp->nremove, "nremove", KSTAT_DATA_UINT64);
4555a59a8b3Srsb 	/* VOP_LINK */
4565a59a8b3Srsb 	kstat_named_init(&vsp->nlink, "nlink", KSTAT_DATA_UINT64);
4575a59a8b3Srsb 	/* VOP_RENAME */
4585a59a8b3Srsb 	kstat_named_init(&vsp->nrename, "nrename", KSTAT_DATA_UINT64);
4595a59a8b3Srsb 	/* VOP_MKDIR */
4605a59a8b3Srsb 	kstat_named_init(&vsp->nmkdir, "nmkdir", KSTAT_DATA_UINT64);
4615a59a8b3Srsb 	/* VOP_RMDIR */
4625a59a8b3Srsb 	kstat_named_init(&vsp->nrmdir, "nrmdir", KSTAT_DATA_UINT64);
4635a59a8b3Srsb 	/* VOP_READDIR I/O */
4645a59a8b3Srsb 	kstat_named_init(&vsp->nreaddir, "nreaddir", KSTAT_DATA_UINT64);
4655a59a8b3Srsb 	kstat_named_init(&vsp->readdir_bytes, "readdir_bytes",
4665a59a8b3Srsb 	    KSTAT_DATA_UINT64);
4675a59a8b3Srsb 	/* VOP_SYMLINK */
4685a59a8b3Srsb 	kstat_named_init(&vsp->nsymlink, "nsymlink", KSTAT_DATA_UINT64);
4695a59a8b3Srsb 	/* VOP_READLINK */
4705a59a8b3Srsb 	kstat_named_init(&vsp->nreadlink, "nreadlink", KSTAT_DATA_UINT64);
4715a59a8b3Srsb 	/* VOP_FSYNC */
4725a59a8b3Srsb 	kstat_named_init(&vsp->nfsync, "nfsync", KSTAT_DATA_UINT64);
4735a59a8b3Srsb 	/* VOP_INACTIVE */
4745a59a8b3Srsb 	kstat_named_init(&vsp->ninactive, "ninactive", KSTAT_DATA_UINT64);
4755a59a8b3Srsb 	/* VOP_FID */
4765a59a8b3Srsb 	kstat_named_init(&vsp->nfid, "nfid", KSTAT_DATA_UINT64);
4775a59a8b3Srsb 	/* VOP_RWLOCK */
4785a59a8b3Srsb 	kstat_named_init(&vsp->nrwlock, "nrwlock", KSTAT_DATA_UINT64);
4795a59a8b3Srsb 	/* VOP_RWUNLOCK */
4805a59a8b3Srsb 	kstat_named_init(&vsp->nrwunlock, "nrwunlock", KSTAT_DATA_UINT64);
4815a59a8b3Srsb 	/* VOP_SEEK */
4825a59a8b3Srsb 	kstat_named_init(&vsp->nseek, "nseek", KSTAT_DATA_UINT64);
4835a59a8b3Srsb 	/* VOP_CMP */
4845a59a8b3Srsb 	kstat_named_init(&vsp->ncmp, "ncmp", KSTAT_DATA_UINT64);
4855a59a8b3Srsb 	/* VOP_FRLOCK */
4865a59a8b3Srsb 	kstat_named_init(&vsp->nfrlock, "nfrlock", KSTAT_DATA_UINT64);
4875a59a8b3Srsb 	/* VOP_SPACE */
4885a59a8b3Srsb 	kstat_named_init(&vsp->nspace, "nspace", KSTAT_DATA_UINT64);
4895a59a8b3Srsb 	/* VOP_REALVP */
4905a59a8b3Srsb 	kstat_named_init(&vsp->nrealvp, "nrealvp", KSTAT_DATA_UINT64);
4915a59a8b3Srsb 	/* VOP_GETPAGE */
4925a59a8b3Srsb 	kstat_named_init(&vsp->ngetpage, "ngetpage", KSTAT_DATA_UINT64);
4935a59a8b3Srsb 	/* VOP_PUTPAGE */
4945a59a8b3Srsb 	kstat_named_init(&vsp->nputpage, "nputpage", KSTAT_DATA_UINT64);
4955a59a8b3Srsb 	/* VOP_MAP */
4965a59a8b3Srsb 	kstat_named_init(&vsp->nmap, "nmap", KSTAT_DATA_UINT64);
4975a59a8b3Srsb 	/* VOP_ADDMAP */
4985a59a8b3Srsb 	kstat_named_init(&vsp->naddmap, "naddmap", KSTAT_DATA_UINT64);
4995a59a8b3Srsb 	/* VOP_DELMAP */
5005a59a8b3Srsb 	kstat_named_init(&vsp->ndelmap, "ndelmap", KSTAT_DATA_UINT64);
5015a59a8b3Srsb 	/* VOP_POLL */
5025a59a8b3Srsb 	kstat_named_init(&vsp->npoll, "npoll", KSTAT_DATA_UINT64);
5035a59a8b3Srsb 	/* VOP_DUMP */
5045a59a8b3Srsb 	kstat_named_init(&vsp->ndump, "ndump", KSTAT_DATA_UINT64);
5055a59a8b3Srsb 	/* VOP_PATHCONF */
5065a59a8b3Srsb 	kstat_named_init(&vsp->npathconf, "npathconf", KSTAT_DATA_UINT64);
5075a59a8b3Srsb 	/* VOP_PAGEIO */
5085a59a8b3Srsb 	kstat_named_init(&vsp->npageio, "npageio", KSTAT_DATA_UINT64);
5095a59a8b3Srsb 	/* VOP_DUMPCTL */
5105a59a8b3Srsb 	kstat_named_init(&vsp->ndumpctl, "ndumpctl", KSTAT_DATA_UINT64);
5115a59a8b3Srsb 	/* VOP_DISPOSE */
5125a59a8b3Srsb 	kstat_named_init(&vsp->ndispose, "ndispose", KSTAT_DATA_UINT64);
5135a59a8b3Srsb 	/* VOP_SETSECATTR */
5145a59a8b3Srsb 	kstat_named_init(&vsp->nsetsecattr, "nsetsecattr", KSTAT_DATA_UINT64);
5155a59a8b3Srsb 	/* VOP_GETSECATTR */
5165a59a8b3Srsb 	kstat_named_init(&vsp->ngetsecattr, "ngetsecattr", KSTAT_DATA_UINT64);
5175a59a8b3Srsb 	/* VOP_SHRLOCK */
5185a59a8b3Srsb 	kstat_named_init(&vsp->nshrlock, "nshrlock", KSTAT_DATA_UINT64);
5195a59a8b3Srsb 	/* VOP_VNEVENT */
5205a59a8b3Srsb 	kstat_named_init(&vsp->nvnevent, "nvnevent", KSTAT_DATA_UINT64);
5215a59a8b3Srsb 
5225a59a8b3Srsb 	return (vsp);
5235a59a8b3Srsb }
5245a59a8b3Srsb 
5255a59a8b3Srsb /*
5265a59a8b3Srsb  * Creates a kstat structure associated with a vopstats structure.
5275a59a8b3Srsb  */
5285a59a8b3Srsb kstat_t *
5295a59a8b3Srsb new_vskstat(char *ksname, vopstats_t *vsp)
5305a59a8b3Srsb {
5315a59a8b3Srsb 	kstat_t		*ksp;
5325a59a8b3Srsb 
5335a59a8b3Srsb 	if (!vopstats_enabled) {
5345a59a8b3Srsb 		return (NULL);
5355a59a8b3Srsb 	}
5365a59a8b3Srsb 
5375a59a8b3Srsb 	ksp = kstat_create("unix", 0, ksname, "misc", KSTAT_TYPE_NAMED,
5385a59a8b3Srsb 	    sizeof (vopstats_t)/sizeof (kstat_named_t),
5395a59a8b3Srsb 	    KSTAT_FLAG_VIRTUAL|KSTAT_FLAG_WRITABLE);
5405a59a8b3Srsb 	if (ksp) {
5415a59a8b3Srsb 		ksp->ks_data = vsp;
5425a59a8b3Srsb 		kstat_install(ksp);
5435a59a8b3Srsb 	}
5445a59a8b3Srsb 
5455a59a8b3Srsb 	return (ksp);
5465a59a8b3Srsb }
5475a59a8b3Srsb 
5485a59a8b3Srsb /*
5495a59a8b3Srsb  * Called from vfsinit() to initialize the support mechanisms for vopstats
5505a59a8b3Srsb  */
5515a59a8b3Srsb void
5525a59a8b3Srsb vopstats_startup()
5535a59a8b3Srsb {
5545a59a8b3Srsb 	if (!vopstats_enabled)
5555a59a8b3Srsb 		return;
5565a59a8b3Srsb 
5575a59a8b3Srsb 	/*
5585a59a8b3Srsb 	 * Creates the AVL tree which holds per-vfs vopstat anchors.  This
5595a59a8b3Srsb 	 * is necessary since we need to check if a kstat exists before we
5605a59a8b3Srsb 	 * attempt to create it.  Also, initialize its lock.
5615a59a8b3Srsb 	 */
5625a59a8b3Srsb 	avl_create(&vskstat_tree, vska_compar, sizeof (vsk_anchor_t),
5635a59a8b3Srsb 	    offsetof(vsk_anchor_t, vsk_node));
5645a59a8b3Srsb 	mutex_init(&vskstat_tree_lock, NULL, MUTEX_DEFAULT, NULL);
5655a59a8b3Srsb 
5665a59a8b3Srsb 	vsk_anchor_cache = kmem_cache_create("vsk_anchor_cache",
5675a59a8b3Srsb 	    sizeof (vsk_anchor_t), sizeof (uintptr_t), NULL, NULL, NULL,
5685a59a8b3Srsb 	    NULL, NULL, 0);
5695a59a8b3Srsb 
5705a59a8b3Srsb 	/*
5715a59a8b3Srsb 	 * Set up the array of pointers for the vopstats-by-FS-type.
5725a59a8b3Srsb 	 * The entries will be allocated/initialized as each file system
5735a59a8b3Srsb 	 * goes through modload/mod_installfs.
5745a59a8b3Srsb 	 */
5755a59a8b3Srsb 	vopstats_fstype = (vopstats_t **)kmem_zalloc(
5765a59a8b3Srsb 	    (sizeof (vopstats_t *) * nfstype), KM_SLEEP);
5775a59a8b3Srsb 
5785a59a8b3Srsb 	/* Set up the global vopstats initialization template */
5795a59a8b3Srsb 	vs_templatep = create_vopstats_template();
5805a59a8b3Srsb }
5815a59a8b3Srsb 
5825a59a8b3Srsb /*
5835a59a8b3Srsb  * We need to have the all of the counters zeroed.
5845a59a8b3Srsb  * The initialization of the vopstats_t includes on the order of
5855a59a8b3Srsb  * 50 calls to kstat_named_init().  Rather that do that on every call,
5865a59a8b3Srsb  * we do it once in a template (vs_templatep) then bcopy it over.
5875a59a8b3Srsb  */
5885a59a8b3Srsb void
5895a59a8b3Srsb initialize_vopstats(vopstats_t *vsp)
5905a59a8b3Srsb {
5915a59a8b3Srsb 	if (vsp == NULL)
5925a59a8b3Srsb 		return;
5935a59a8b3Srsb 
5945a59a8b3Srsb 	bcopy(vs_templatep, vsp, sizeof (vopstats_t));
5955a59a8b3Srsb }
5965a59a8b3Srsb 
5975a59a8b3Srsb /*
59882c7f3c4Srsb  * If possible, determine which vopstats by fstype to use and
59982c7f3c4Srsb  * return a pointer to the caller.
6005a59a8b3Srsb  */
60182c7f3c4Srsb vopstats_t *
60282c7f3c4Srsb get_fstype_vopstats(vfs_t *vfsp, struct vfssw *vswp)
6035a59a8b3Srsb {
6045a59a8b3Srsb 	int		fstype = 0;	/* Index into vfssw[] */
60582c7f3c4Srsb 	vopstats_t	*vsp = NULL;
6065a59a8b3Srsb 
6075a59a8b3Srsb 	if (vfsp == NULL || (vfsp->vfs_flag & VFS_STATS) == 0 ||
6085a59a8b3Srsb 	    !vopstats_enabled)
60982c7f3c4Srsb 		return (NULL);
6105a59a8b3Srsb 	/*
6115a59a8b3Srsb 	 * Set up the fstype.  We go to so much trouble because all versions
6125a59a8b3Srsb 	 * of NFS use the same fstype in their vfs even though they have
6135a59a8b3Srsb 	 * distinct entries in the vfssw[] table.
61482c7f3c4Srsb 	 * NOTE: A special vfs (e.g., EIO_vfs) may not have an entry.
6155a59a8b3Srsb 	 */
6165a59a8b3Srsb 	if (vswp) {
6175a59a8b3Srsb 		fstype = vswp - vfssw;	/* Gets us the index */
6185a59a8b3Srsb 	} else {
6195a59a8b3Srsb 		fstype = vfsp->vfs_fstype;
6205a59a8b3Srsb 	}
6215a59a8b3Srsb 
6225a59a8b3Srsb 	/*
6235a59a8b3Srsb 	 * Point to the per-fstype vopstats. The only valid values are
6245a59a8b3Srsb 	 * non-zero positive values less than the number of vfssw[] table
6255a59a8b3Srsb 	 * entries.
6265a59a8b3Srsb 	 */
6275a59a8b3Srsb 	if (fstype > 0 && fstype < nfstype) {
62882c7f3c4Srsb 		vsp = vopstats_fstype[fstype];
6295a59a8b3Srsb 	}
6305a59a8b3Srsb 
63182c7f3c4Srsb 	return (vsp);
63282c7f3c4Srsb }
63382c7f3c4Srsb 
63482c7f3c4Srsb /*
63582c7f3c4Srsb  * Generate a kstat name, create the kstat structure, and allocate a
63682c7f3c4Srsb  * vsk_anchor_t to hold it together.  Return the pointer to the vsk_anchor_t
63782c7f3c4Srsb  * to the caller.  This must only be called from a mount.
63882c7f3c4Srsb  */
63982c7f3c4Srsb vsk_anchor_t *
64082c7f3c4Srsb get_vskstat_anchor(vfs_t *vfsp)
64182c7f3c4Srsb {
64282c7f3c4Srsb 	char		kstatstr[KSTAT_STRLEN]; /* kstat name for vopstats */
64382c7f3c4Srsb 	statvfs64_t	statvfsbuf;		/* Needed to find f_fsid */
64482c7f3c4Srsb 	vsk_anchor_t	*vskp = NULL;		/* vfs <--> kstat anchor */
64582c7f3c4Srsb 	kstat_t		*ksp;			/* Ptr to new kstat */
64682c7f3c4Srsb 	avl_index_t	where;			/* Location in the AVL tree */
64782c7f3c4Srsb 
648ddfcde86Srsb 	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
649ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
65082c7f3c4Srsb 		return (NULL);
65182c7f3c4Srsb 
6525a59a8b3Srsb 	/* Need to get the fsid to build a kstat name */
6535a59a8b3Srsb 	if (VFS_STATVFS(vfsp, &statvfsbuf) == 0) {
6545a59a8b3Srsb 		/* Create a name for our kstats based on fsid */
6555a59a8b3Srsb 		(void) snprintf(kstatstr, KSTAT_STRLEN, "%s%lx",
6565a59a8b3Srsb 		    VOPSTATS_STR, statvfsbuf.f_fsid);
6575a59a8b3Srsb 
6585a59a8b3Srsb 		/* Allocate and initialize the vsk_anchor_t */
6595a59a8b3Srsb 		vskp = kmem_cache_alloc(vsk_anchor_cache, KM_SLEEP);
6605a59a8b3Srsb 		bzero(vskp, sizeof (*vskp));
6615a59a8b3Srsb 		vskp->vsk_fsid = statvfsbuf.f_fsid;
6625a59a8b3Srsb 
6635a59a8b3Srsb 		mutex_enter(&vskstat_tree_lock);
6645a59a8b3Srsb 		if (avl_find(&vskstat_tree, vskp, &where) == NULL) {
6655a59a8b3Srsb 			avl_insert(&vskstat_tree, vskp, where);
6665a59a8b3Srsb 			mutex_exit(&vskstat_tree_lock);
6675a59a8b3Srsb 
6685a59a8b3Srsb 			/*
6695a59a8b3Srsb 			 * Now that we've got the anchor in the AVL
6705a59a8b3Srsb 			 * tree, we can create the kstat.
6715a59a8b3Srsb 			 */
6725a59a8b3Srsb 			ksp = new_vskstat(kstatstr, &vfsp->vfs_vopstats);
6735a59a8b3Srsb 			if (ksp) {
6745a59a8b3Srsb 				vskp->vsk_ksp = ksp;
6755a59a8b3Srsb 			}
6765a59a8b3Srsb 		} else {
6775a59a8b3Srsb 			/* Oops, found one! Release memory and lock. */
6785a59a8b3Srsb 			mutex_exit(&vskstat_tree_lock);
6795a59a8b3Srsb 			kmem_cache_free(vsk_anchor_cache, vskp);
68082c7f3c4Srsb 			vskp = NULL;
6815a59a8b3Srsb 		}
6825a59a8b3Srsb 	}
68382c7f3c4Srsb 	return (vskp);
6845a59a8b3Srsb }
6855a59a8b3Srsb 
6865a59a8b3Srsb /*
6875a59a8b3Srsb  * We're in the process of tearing down the vfs and need to cleanup
6885a59a8b3Srsb  * the data structures associated with the vopstats. Must only be called
6895a59a8b3Srsb  * from dounmount().
6905a59a8b3Srsb  */
6915a59a8b3Srsb void
6925a59a8b3Srsb teardown_vopstats(vfs_t *vfsp)
6935a59a8b3Srsb {
6945a59a8b3Srsb 	vsk_anchor_t	*vskap;
6955a59a8b3Srsb 	avl_index_t	where;
6965a59a8b3Srsb 
697ddfcde86Srsb 	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
698ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
6995a59a8b3Srsb 		return;
7005a59a8b3Srsb 
7015a59a8b3Srsb 	/* This is a safe check since VFS_STATS must be set (see above) */
7025a59a8b3Srsb 	if ((vskap = vfsp->vfs_vskap) == NULL)
7035a59a8b3Srsb 		return;
7045a59a8b3Srsb 
7055a59a8b3Srsb 	/* Whack the pointer right away */
7065a59a8b3Srsb 	vfsp->vfs_vskap = NULL;
7075a59a8b3Srsb 
7085a59a8b3Srsb 	/* Lock the tree, remove the node, and delete the kstat */
7095a59a8b3Srsb 	mutex_enter(&vskstat_tree_lock);
7105a59a8b3Srsb 	if (avl_find(&vskstat_tree, vskap, &where)) {
7115a59a8b3Srsb 		avl_remove(&vskstat_tree, vskap);
7125a59a8b3Srsb 	}
7135a59a8b3Srsb 
7145a59a8b3Srsb 	if (vskap->vsk_ksp) {
7155a59a8b3Srsb 		kstat_delete(vskap->vsk_ksp);
7165a59a8b3Srsb 	}
7175a59a8b3Srsb 	mutex_exit(&vskstat_tree_lock);
7185a59a8b3Srsb 
7195a59a8b3Srsb 	kmem_cache_free(vsk_anchor_cache, vskap);
7205a59a8b3Srsb }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate /*
7237c478bd9Sstevel@tonic-gate  * Read or write a vnode.  Called from kernel code.
7247c478bd9Sstevel@tonic-gate  */
7257c478bd9Sstevel@tonic-gate int
7267c478bd9Sstevel@tonic-gate vn_rdwr(
7277c478bd9Sstevel@tonic-gate 	enum uio_rw rw,
7287c478bd9Sstevel@tonic-gate 	struct vnode *vp,
7297c478bd9Sstevel@tonic-gate 	caddr_t base,
7307c478bd9Sstevel@tonic-gate 	ssize_t len,
7317c478bd9Sstevel@tonic-gate 	offset_t offset,
7327c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
7337c478bd9Sstevel@tonic-gate 	int ioflag,
7347c478bd9Sstevel@tonic-gate 	rlim64_t ulimit,	/* meaningful only if rw is UIO_WRITE */
7357c478bd9Sstevel@tonic-gate 	cred_t *cr,
7367c478bd9Sstevel@tonic-gate 	ssize_t *residp)
7377c478bd9Sstevel@tonic-gate {
7387c478bd9Sstevel@tonic-gate 	struct uio uio;
7397c478bd9Sstevel@tonic-gate 	struct iovec iov;
7407c478bd9Sstevel@tonic-gate 	int error;
7417c478bd9Sstevel@tonic-gate 	int in_crit = 0;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	if (rw == UIO_WRITE && ISROFILE(vp))
7447c478bd9Sstevel@tonic-gate 		return (EROFS);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	if (len < 0)
7477c478bd9Sstevel@tonic-gate 		return (EIO);
7487c478bd9Sstevel@tonic-gate 
749f48205beScasper 	VOPXID_MAP_CR(vp, cr);
750f48205beScasper 
7517c478bd9Sstevel@tonic-gate 	iov.iov_base = base;
7527c478bd9Sstevel@tonic-gate 	iov.iov_len = len;
7537c478bd9Sstevel@tonic-gate 	uio.uio_iov = &iov;
7547c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
7557c478bd9Sstevel@tonic-gate 	uio.uio_loffset = offset;
7567c478bd9Sstevel@tonic-gate 	uio.uio_segflg = (short)seg;
7577c478bd9Sstevel@tonic-gate 	uio.uio_resid = len;
7587c478bd9Sstevel@tonic-gate 	uio.uio_llimit = ulimit;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	/*
7617c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
7627c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
7637c478bd9Sstevel@tonic-gate 	 */
7647c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
7657c478bd9Sstevel@tonic-gate 		int svmand;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
7687c478bd9Sstevel@tonic-gate 		in_crit = 1;
7697c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, cr, &svmand);
7707c478bd9Sstevel@tonic-gate 		if (error != 0)
7717c478bd9Sstevel@tonic-gate 			goto done;
7727c478bd9Sstevel@tonic-gate 		if (nbl_conflict(vp, rw == UIO_WRITE ? NBL_WRITE : NBL_READ,
773*da6c28aaSamw 		    uio.uio_offset, uio.uio_resid, svmand, NULL)) {
7747c478bd9Sstevel@tonic-gate 			error = EACCES;
7757c478bd9Sstevel@tonic-gate 			goto done;
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp,
7807c478bd9Sstevel@tonic-gate 	    rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
7817c478bd9Sstevel@tonic-gate 	if (rw == UIO_WRITE) {
7827c478bd9Sstevel@tonic-gate 		uio.uio_fmode = FWRITE;
7837c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_DEFAULT;
7847c478bd9Sstevel@tonic-gate 		error = VOP_WRITE(vp, &uio, ioflag, cr, NULL);
7857c478bd9Sstevel@tonic-gate 	} else {
7867c478bd9Sstevel@tonic-gate 		uio.uio_fmode = FREAD;
7877c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_CACHED;
7887c478bd9Sstevel@tonic-gate 		error = VOP_READ(vp, &uio, ioflag, cr, NULL);
7897c478bd9Sstevel@tonic-gate 	}
790*da6c28aaSamw 	VOP_RWUNLOCK(vp,
791*da6c28aaSamw 	    rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
7927c478bd9Sstevel@tonic-gate 	if (residp)
7937c478bd9Sstevel@tonic-gate 		*residp = uio.uio_resid;
7947c478bd9Sstevel@tonic-gate 	else if (uio.uio_resid)
7957c478bd9Sstevel@tonic-gate 		error = EIO;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate done:
7987c478bd9Sstevel@tonic-gate 	if (in_crit)
7997c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
8007c478bd9Sstevel@tonic-gate 	return (error);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /*
8047c478bd9Sstevel@tonic-gate  * Release a vnode.  Call VOP_INACTIVE on last reference or
8057c478bd9Sstevel@tonic-gate  * decrement reference count.
8067c478bd9Sstevel@tonic-gate  *
8077c478bd9Sstevel@tonic-gate  * To avoid race conditions, the v_count is left at 1 for
8087c478bd9Sstevel@tonic-gate  * the call to VOP_INACTIVE. This prevents another thread
8097c478bd9Sstevel@tonic-gate  * from reclaiming and releasing the vnode *before* the
8107c478bd9Sstevel@tonic-gate  * VOP_INACTIVE routine has a chance to destroy the vnode.
8117c478bd9Sstevel@tonic-gate  * We can't have more than 1 thread calling VOP_INACTIVE
8127c478bd9Sstevel@tonic-gate  * on a vnode.
8137c478bd9Sstevel@tonic-gate  */
8147c478bd9Sstevel@tonic-gate void
8157c478bd9Sstevel@tonic-gate vn_rele(vnode_t *vp)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0)
8187c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_rele: vnode ref count 0");
8197c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
8207c478bd9Sstevel@tonic-gate 	if (vp->v_count == 1) {
8217c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
822*da6c28aaSamw 		VOP_INACTIVE(vp, CRED(), NULL);
8237c478bd9Sstevel@tonic-gate 	} else {
8247c478bd9Sstevel@tonic-gate 		vp->v_count--;
8257c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate /*
8307c478bd9Sstevel@tonic-gate  * Like vn_rele() except that it clears v_stream under v_lock.
8317c478bd9Sstevel@tonic-gate  * This is used by sockfs when it dismantels the association between
8327c478bd9Sstevel@tonic-gate  * the sockfs node and the vnode in the underlaying file system.
8337c478bd9Sstevel@tonic-gate  * v_lock has to be held to prevent a thread coming through the lookupname
8347c478bd9Sstevel@tonic-gate  * path from accessing a stream head that is going away.
8357c478bd9Sstevel@tonic-gate  */
8367c478bd9Sstevel@tonic-gate void
8377c478bd9Sstevel@tonic-gate vn_rele_stream(vnode_t *vp)
8387c478bd9Sstevel@tonic-gate {
8397c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0)
8407c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_rele: vnode ref count 0");
8417c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
8427c478bd9Sstevel@tonic-gate 	vp->v_stream = NULL;
8437c478bd9Sstevel@tonic-gate 	if (vp->v_count == 1) {
8447c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
845*da6c28aaSamw 		VOP_INACTIVE(vp, CRED(), NULL);
8467c478bd9Sstevel@tonic-gate 	} else {
8477c478bd9Sstevel@tonic-gate 		vp->v_count--;
8487c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate int
8537c478bd9Sstevel@tonic-gate vn_open(
8547c478bd9Sstevel@tonic-gate 	char *pnamep,
8557c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
8567c478bd9Sstevel@tonic-gate 	int filemode,
8577c478bd9Sstevel@tonic-gate 	int createmode,
8587c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
8597c478bd9Sstevel@tonic-gate 	enum create crwhy,
8607c478bd9Sstevel@tonic-gate 	mode_t umask)
8617c478bd9Sstevel@tonic-gate {
862*da6c28aaSamw 	return (vn_openat(pnamep, seg, filemode, createmode, vpp, crwhy,
863*da6c28aaSamw 	    umask, NULL, -1));
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate /*
8687c478bd9Sstevel@tonic-gate  * Open/create a vnode.
8697c478bd9Sstevel@tonic-gate  * This may be callable by the kernel, the only known use
8707c478bd9Sstevel@tonic-gate  * of user context being that the current user credentials
8717c478bd9Sstevel@tonic-gate  * are used for permissions.  crwhy is defined iff filemode & FCREAT.
8727c478bd9Sstevel@tonic-gate  */
8737c478bd9Sstevel@tonic-gate int
8747c478bd9Sstevel@tonic-gate vn_openat(
8757c478bd9Sstevel@tonic-gate 	char *pnamep,
8767c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
8777c478bd9Sstevel@tonic-gate 	int filemode,
8787c478bd9Sstevel@tonic-gate 	int createmode,
8797c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
8807c478bd9Sstevel@tonic-gate 	enum create crwhy,
8817c478bd9Sstevel@tonic-gate 	mode_t umask,
882*da6c28aaSamw 	struct vnode *startvp,
883*da6c28aaSamw 	int fd)
8847c478bd9Sstevel@tonic-gate {
8857c478bd9Sstevel@tonic-gate 	struct vnode *vp;
8867c478bd9Sstevel@tonic-gate 	int mode;
887*da6c28aaSamw 	int accessflags;
8887c478bd9Sstevel@tonic-gate 	int error;
8897c478bd9Sstevel@tonic-gate 	int in_crit = 0;
890*da6c28aaSamw 	int open_done = 0;
891*da6c28aaSamw 	int shrlock_done = 0;
8927c478bd9Sstevel@tonic-gate 	struct vattr vattr;
8937c478bd9Sstevel@tonic-gate 	enum symfollow follow;
894dd29fa4aSprabahar 	int estale_retry = 0;
895*da6c28aaSamw 	struct shrlock shr;
896*da6c28aaSamw 	struct shr_locowner shr_own;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	mode = 0;
899*da6c28aaSamw 	accessflags = 0;
9007c478bd9Sstevel@tonic-gate 	if (filemode & FREAD)
9017c478bd9Sstevel@tonic-gate 		mode |= VREAD;
9027c478bd9Sstevel@tonic-gate 	if (filemode & (FWRITE|FTRUNC))
9037c478bd9Sstevel@tonic-gate 		mode |= VWRITE;
904*da6c28aaSamw 	if (filemode & FXATTRDIROPEN)
905*da6c28aaSamw 		mode |= VEXEC;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	/* symlink interpretation */
9087c478bd9Sstevel@tonic-gate 	if (filemode & FNOFOLLOW)
9097c478bd9Sstevel@tonic-gate 		follow = NO_FOLLOW;
9107c478bd9Sstevel@tonic-gate 	else
9117c478bd9Sstevel@tonic-gate 		follow = FOLLOW;
9127c478bd9Sstevel@tonic-gate 
913*da6c28aaSamw 	if (filemode & FAPPEND)
914*da6c28aaSamw 		accessflags |= V_APPEND;
915*da6c28aaSamw 
9167c478bd9Sstevel@tonic-gate top:
9177c478bd9Sstevel@tonic-gate 	if (filemode & FCREAT) {
9187c478bd9Sstevel@tonic-gate 		enum vcexcl excl;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 		/*
9217c478bd9Sstevel@tonic-gate 		 * Wish to create a file.
9227c478bd9Sstevel@tonic-gate 		 */
9237c478bd9Sstevel@tonic-gate 		vattr.va_type = VREG;
9247c478bd9Sstevel@tonic-gate 		vattr.va_mode = createmode;
9257c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_TYPE|AT_MODE;
9267c478bd9Sstevel@tonic-gate 		if (filemode & FTRUNC) {
9277c478bd9Sstevel@tonic-gate 			vattr.va_size = 0;
9287c478bd9Sstevel@tonic-gate 			vattr.va_mask |= AT_SIZE;
9297c478bd9Sstevel@tonic-gate 		}
9307c478bd9Sstevel@tonic-gate 		if (filemode & FEXCL)
9317c478bd9Sstevel@tonic-gate 			excl = EXCL;
9327c478bd9Sstevel@tonic-gate 		else
9337c478bd9Sstevel@tonic-gate 			excl = NONEXCL;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 		if (error =
9367c478bd9Sstevel@tonic-gate 		    vn_createat(pnamep, seg, &vattr, excl, mode, &vp, crwhy,
9371b300de9Sjwahlig 		    (filemode & ~(FTRUNC|FEXCL)), umask, startvp))
9387c478bd9Sstevel@tonic-gate 			return (error);
9397c478bd9Sstevel@tonic-gate 	} else {
9407c478bd9Sstevel@tonic-gate 		/*
9417c478bd9Sstevel@tonic-gate 		 * Wish to open a file.  Just look it up.
9427c478bd9Sstevel@tonic-gate 		 */
9437c478bd9Sstevel@tonic-gate 		if (error = lookupnameat(pnamep, seg, follow,
9447c478bd9Sstevel@tonic-gate 		    NULLVPP, &vp, startvp)) {
945dd29fa4aSprabahar 			if ((error == ESTALE) &&
946dd29fa4aSprabahar 			    fs_need_estale_retry(estale_retry++))
9477c478bd9Sstevel@tonic-gate 				goto top;
9487c478bd9Sstevel@tonic-gate 			return (error);
9497c478bd9Sstevel@tonic-gate 		}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 		/*
9527c478bd9Sstevel@tonic-gate 		 * Get the attributes to check whether file is large.
9537c478bd9Sstevel@tonic-gate 		 * We do this only if the FOFFMAX flag is not set and
9547c478bd9Sstevel@tonic-gate 		 * only for regular files.
9557c478bd9Sstevel@tonic-gate 		 */
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 		if (!(filemode & FOFFMAX) && (vp->v_type == VREG)) {
9587c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
959*da6c28aaSamw 			if ((error = VOP_GETATTR(vp, &vattr, 0,
960*da6c28aaSamw 			    CRED(), NULL))) {
9617c478bd9Sstevel@tonic-gate 				goto out;
9627c478bd9Sstevel@tonic-gate 			}
9637c478bd9Sstevel@tonic-gate 			if (vattr.va_size > (u_offset_t)MAXOFF32_T) {
9647c478bd9Sstevel@tonic-gate 				/*
9657c478bd9Sstevel@tonic-gate 				 * Large File API - regular open fails
9667c478bd9Sstevel@tonic-gate 				 * if FOFFMAX flag is set in file mode
9677c478bd9Sstevel@tonic-gate 				 */
9687c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
9697c478bd9Sstevel@tonic-gate 				goto out;
9707c478bd9Sstevel@tonic-gate 			}
9717c478bd9Sstevel@tonic-gate 		}
9727c478bd9Sstevel@tonic-gate 		/*
9737c478bd9Sstevel@tonic-gate 		 * Can't write directories, active texts, or
9747c478bd9Sstevel@tonic-gate 		 * read-only filesystems.  Can't truncate files
9757c478bd9Sstevel@tonic-gate 		 * on which mandatory locking is in effect.
9767c478bd9Sstevel@tonic-gate 		 */
9777c478bd9Sstevel@tonic-gate 		if (filemode & (FWRITE|FTRUNC)) {
9787c478bd9Sstevel@tonic-gate 			/*
9797c478bd9Sstevel@tonic-gate 			 * Allow writable directory if VDIROPEN flag is set.
9807c478bd9Sstevel@tonic-gate 			 */
9817c478bd9Sstevel@tonic-gate 			if (vp->v_type == VDIR && !(vp->v_flag & VDIROPEN)) {
9827c478bd9Sstevel@tonic-gate 				error = EISDIR;
9837c478bd9Sstevel@tonic-gate 				goto out;
9847c478bd9Sstevel@tonic-gate 			}
9857c478bd9Sstevel@tonic-gate 			if (ISROFILE(vp)) {
9867c478bd9Sstevel@tonic-gate 				error = EROFS;
9877c478bd9Sstevel@tonic-gate 				goto out;
9887c478bd9Sstevel@tonic-gate 			}
9897c478bd9Sstevel@tonic-gate 			/*
990*da6c28aaSamw 			 * Can't truncate files on which
991*da6c28aaSamw 			 * sysv mandatory locking is in effect.
9927c478bd9Sstevel@tonic-gate 			 */
9937c478bd9Sstevel@tonic-gate 			if (filemode & FTRUNC) {
9947c478bd9Sstevel@tonic-gate 				vnode_t *rvp;
9957c478bd9Sstevel@tonic-gate 
996*da6c28aaSamw 				if (VOP_REALVP(vp, &rvp, NULL) != 0)
9977c478bd9Sstevel@tonic-gate 					rvp = vp;
998*da6c28aaSamw 				if (rvp->v_filocks != NULL) {
9997c478bd9Sstevel@tonic-gate 					vattr.va_mask = AT_MODE;
1000*da6c28aaSamw 					if ((error = VOP_GETATTR(vp,
1001*da6c28aaSamw 					    &vattr, 0, CRED(), NULL)) == 0 &&
1002*da6c28aaSamw 					    MANDLOCK(vp, vattr.va_mode))
10037c478bd9Sstevel@tonic-gate 						error = EAGAIN;
10047c478bd9Sstevel@tonic-gate 				}
10057c478bd9Sstevel@tonic-gate 			}
10067c478bd9Sstevel@tonic-gate 			if (error)
10077c478bd9Sstevel@tonic-gate 				goto out;
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 		/*
10107c478bd9Sstevel@tonic-gate 		 * Check permissions.
10117c478bd9Sstevel@tonic-gate 		 */
1012*da6c28aaSamw 		if (error = VOP_ACCESS(vp, mode, accessflags, CRED(), NULL))
10137c478bd9Sstevel@tonic-gate 			goto out;
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/*
10177c478bd9Sstevel@tonic-gate 	 * Do remaining checks for FNOFOLLOW and FNOLINKS.
10187c478bd9Sstevel@tonic-gate 	 */
10197c478bd9Sstevel@tonic-gate 	if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) {
10209acbbeafSnn35248 		error = ELOOP;
10217c478bd9Sstevel@tonic-gate 		goto out;
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	if (filemode & FNOLINKS) {
10247c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_NLINK;
1025*da6c28aaSamw 		if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))) {
10267c478bd9Sstevel@tonic-gate 			goto out;
10277c478bd9Sstevel@tonic-gate 		}
10287c478bd9Sstevel@tonic-gate 		if (vattr.va_nlink != 1) {
10297c478bd9Sstevel@tonic-gate 			error = EMLINK;
10307c478bd9Sstevel@tonic-gate 			goto out;
10317c478bd9Sstevel@tonic-gate 		}
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	/*
10357c478bd9Sstevel@tonic-gate 	 * Opening a socket corresponding to the AF_UNIX pathname
10367c478bd9Sstevel@tonic-gate 	 * in the filesystem name space is not supported.
10377c478bd9Sstevel@tonic-gate 	 * However, VSOCK nodes in namefs are supported in order
10387c478bd9Sstevel@tonic-gate 	 * to make fattach work for sockets.
10397c478bd9Sstevel@tonic-gate 	 *
10407c478bd9Sstevel@tonic-gate 	 * XXX This uses VOP_REALVP to distinguish between
10417c478bd9Sstevel@tonic-gate 	 * an unopened namefs node (where VOP_REALVP returns a
10427c478bd9Sstevel@tonic-gate 	 * different VSOCK vnode) and a VSOCK created by vn_create
10437c478bd9Sstevel@tonic-gate 	 * in some file system (where VOP_REALVP would never return
10447c478bd9Sstevel@tonic-gate 	 * a different vnode).
10457c478bd9Sstevel@tonic-gate 	 */
10467c478bd9Sstevel@tonic-gate 	if (vp->v_type == VSOCK) {
10477c478bd9Sstevel@tonic-gate 		struct vnode *nvp;
10487c478bd9Sstevel@tonic-gate 
1049*da6c28aaSamw 		error = VOP_REALVP(vp, &nvp, NULL);
10507c478bd9Sstevel@tonic-gate 		if (error != 0 || nvp == NULL || nvp == vp ||
10517c478bd9Sstevel@tonic-gate 		    nvp->v_type != VSOCK) {
10527c478bd9Sstevel@tonic-gate 			error = EOPNOTSUPP;
10537c478bd9Sstevel@tonic-gate 			goto out;
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate 	}
1056*da6c28aaSamw 
1057*da6c28aaSamw 	if ((vp->v_type == VREG) && nbl_need_check(vp)) {
1058*da6c28aaSamw 		/* get share reservation */
1059*da6c28aaSamw 		shr.s_access = 0;
1060*da6c28aaSamw 		if (filemode & FWRITE)
1061*da6c28aaSamw 			shr.s_access |= F_WRACC;
1062*da6c28aaSamw 		if (filemode & FREAD)
1063*da6c28aaSamw 			shr.s_access |= F_RDACC;
1064*da6c28aaSamw 		shr.s_deny = 0;
1065*da6c28aaSamw 		shr.s_sysid = 0;
1066*da6c28aaSamw 		shr.s_pid = ttoproc(curthread)->p_pid;
1067*da6c28aaSamw 		shr_own.sl_pid = shr.s_pid;
1068*da6c28aaSamw 		shr_own.sl_id = fd;
1069*da6c28aaSamw 		shr.s_own_len = sizeof (shr_own);
1070*da6c28aaSamw 		shr.s_owner = (caddr_t)&shr_own;
1071*da6c28aaSamw 		error = VOP_SHRLOCK(vp, F_SHARE_NBMAND, &shr, filemode, CRED(),
1072*da6c28aaSamw 		    NULL);
1073*da6c28aaSamw 		if (error)
1074*da6c28aaSamw 			goto out;
1075*da6c28aaSamw 		shrlock_done = 1;
1076*da6c28aaSamw 
1077*da6c28aaSamw 		/* nbmand conflict check if truncating file */
1078*da6c28aaSamw 		if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
1079*da6c28aaSamw 			nbl_start_crit(vp, RW_READER);
1080*da6c28aaSamw 			in_crit = 1;
1081*da6c28aaSamw 
1082*da6c28aaSamw 			vattr.va_mask = AT_SIZE;
1083*da6c28aaSamw 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
1084*da6c28aaSamw 				goto out;
1085*da6c28aaSamw 			if (nbl_conflict(vp, NBL_WRITE, 0, vattr.va_size, 0,
1086*da6c28aaSamw 			    NULL)) {
1087*da6c28aaSamw 				error = EACCES;
1088*da6c28aaSamw 				goto out;
1089*da6c28aaSamw 			}
1090*da6c28aaSamw 		}
1091*da6c28aaSamw 	}
1092*da6c28aaSamw 
10937c478bd9Sstevel@tonic-gate 	/*
10947c478bd9Sstevel@tonic-gate 	 * Do opening protocol.
10957c478bd9Sstevel@tonic-gate 	 */
1096*da6c28aaSamw 	error = VOP_OPEN(&vp, filemode, CRED(), NULL);
1097*da6c28aaSamw 	if (error)
1098*da6c28aaSamw 		goto out;
1099*da6c28aaSamw 	open_done = 1;
1100*da6c28aaSamw 
11017c478bd9Sstevel@tonic-gate 	/*
11027c478bd9Sstevel@tonic-gate 	 * Truncate if required.
11037c478bd9Sstevel@tonic-gate 	 */
1104*da6c28aaSamw 	if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
11057c478bd9Sstevel@tonic-gate 		vattr.va_size = 0;
11067c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_SIZE;
11077c478bd9Sstevel@tonic-gate 		if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0)
1108*da6c28aaSamw 			goto out;
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate out:
11117c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	if (in_crit) {
11147c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
11157c478bd9Sstevel@tonic-gate 		in_crit = 0;
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 	if (error) {
1118*da6c28aaSamw 		if (open_done) {
1119*da6c28aaSamw 			(void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED(),
1120*da6c28aaSamw 			    NULL);
1121*da6c28aaSamw 			open_done = 0;
1122*da6c28aaSamw 			shrlock_done = 0;
1123*da6c28aaSamw 		}
1124*da6c28aaSamw 		if (shrlock_done) {
1125*da6c28aaSamw 			(void) VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, CRED(),
1126*da6c28aaSamw 			    NULL);
1127*da6c28aaSamw 			shrlock_done = 0;
1128*da6c28aaSamw 		}
1129*da6c28aaSamw 
11307c478bd9Sstevel@tonic-gate 		/*
11317c478bd9Sstevel@tonic-gate 		 * The following clause was added to handle a problem
11327c478bd9Sstevel@tonic-gate 		 * with NFS consistency.  It is possible that a lookup
11337c478bd9Sstevel@tonic-gate 		 * of the file to be opened succeeded, but the file
11347c478bd9Sstevel@tonic-gate 		 * itself doesn't actually exist on the server.  This
11357c478bd9Sstevel@tonic-gate 		 * is chiefly due to the DNLC containing an entry for
11367c478bd9Sstevel@tonic-gate 		 * the file which has been removed on the server.  In
11377c478bd9Sstevel@tonic-gate 		 * this case, we just start over.  If there was some
11387c478bd9Sstevel@tonic-gate 		 * other cause for the ESTALE error, then the lookup
11397c478bd9Sstevel@tonic-gate 		 * of the file will fail and the error will be returned
11407c478bd9Sstevel@tonic-gate 		 * above instead of looping around from here.
11417c478bd9Sstevel@tonic-gate 		 */
11427c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1143dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
11447c478bd9Sstevel@tonic-gate 			goto top;
11457c478bd9Sstevel@tonic-gate 	} else
11467c478bd9Sstevel@tonic-gate 		*vpp = vp;
11477c478bd9Sstevel@tonic-gate 	return (error);
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate 
1150*da6c28aaSamw /*
1151*da6c28aaSamw  * The following two accessor functions are for the NFSv4 server.  Since there
1152*da6c28aaSamw  * is no VOP_OPEN_UP/DOWNGRADE we need a way for the NFS server to keep the
1153*da6c28aaSamw  * vnode open counts correct when a client "upgrades" an open or does an
1154*da6c28aaSamw  * open_downgrade.  In NFS, an upgrade or downgrade can not only change the
1155*da6c28aaSamw  * open mode (add or subtract read or write), but also change the share/deny
1156*da6c28aaSamw  * modes.  However, share reservations are not integrated with OPEN, yet, so
1157*da6c28aaSamw  * we need to handle each separately.  These functions are cleaner than having
1158*da6c28aaSamw  * the NFS server manipulate the counts directly, however, nobody else should
1159*da6c28aaSamw  * use these functions.
1160*da6c28aaSamw  */
1161*da6c28aaSamw void
1162*da6c28aaSamw vn_open_upgrade(
1163*da6c28aaSamw 	vnode_t *vp,
1164*da6c28aaSamw 	int filemode)
1165*da6c28aaSamw {
1166*da6c28aaSamw 	ASSERT(vp->v_type == VREG);
1167*da6c28aaSamw 
1168*da6c28aaSamw 	if (filemode & FREAD)
1169*da6c28aaSamw 		atomic_add_32(&(vp->v_rdcnt), 1);
1170*da6c28aaSamw 	if (filemode & FWRITE)
1171*da6c28aaSamw 		atomic_add_32(&(vp->v_wrcnt), 1);
1172*da6c28aaSamw 
1173*da6c28aaSamw }
1174*da6c28aaSamw 
1175*da6c28aaSamw void
1176*da6c28aaSamw vn_open_downgrade(
1177*da6c28aaSamw 	vnode_t *vp,
1178*da6c28aaSamw 	int filemode)
1179*da6c28aaSamw {
1180*da6c28aaSamw 	ASSERT(vp->v_type == VREG);
1181*da6c28aaSamw 
1182*da6c28aaSamw 	if (filemode & FREAD) {
1183*da6c28aaSamw 		ASSERT(vp->v_rdcnt > 0);
1184*da6c28aaSamw 		atomic_add_32(&(vp->v_rdcnt), -1);
1185*da6c28aaSamw 	}
1186*da6c28aaSamw 	if (filemode & FWRITE) {
1187*da6c28aaSamw 		ASSERT(vp->v_wrcnt > 0);
1188*da6c28aaSamw 		atomic_add_32(&(vp->v_wrcnt), -1);
1189*da6c28aaSamw 	}
1190*da6c28aaSamw 
1191*da6c28aaSamw }
1192*da6c28aaSamw 
11937c478bd9Sstevel@tonic-gate int
11947c478bd9Sstevel@tonic-gate vn_create(
11957c478bd9Sstevel@tonic-gate 	char *pnamep,
11967c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
11977c478bd9Sstevel@tonic-gate 	struct vattr *vap,
11987c478bd9Sstevel@tonic-gate 	enum vcexcl excl,
11997c478bd9Sstevel@tonic-gate 	int mode,
12007c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
12017c478bd9Sstevel@tonic-gate 	enum create why,
12027c478bd9Sstevel@tonic-gate 	int flag,
12037c478bd9Sstevel@tonic-gate 	mode_t umask)
12047c478bd9Sstevel@tonic-gate {
1205*da6c28aaSamw 	return (vn_createat(pnamep, seg, vap, excl, mode, vpp, why, flag,
1206*da6c28aaSamw 	    umask, NULL));
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate /*
12107c478bd9Sstevel@tonic-gate  * Create a vnode (makenode).
12117c478bd9Sstevel@tonic-gate  */
12127c478bd9Sstevel@tonic-gate int
12137c478bd9Sstevel@tonic-gate vn_createat(
12147c478bd9Sstevel@tonic-gate 	char *pnamep,
12157c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
12167c478bd9Sstevel@tonic-gate 	struct vattr *vap,
12177c478bd9Sstevel@tonic-gate 	enum vcexcl excl,
12187c478bd9Sstevel@tonic-gate 	int mode,
12197c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
12207c478bd9Sstevel@tonic-gate 	enum create why,
12217c478bd9Sstevel@tonic-gate 	int flag,
12227c478bd9Sstevel@tonic-gate 	mode_t umask,
12237c478bd9Sstevel@tonic-gate 	struct vnode *startvp)
12247c478bd9Sstevel@tonic-gate {
12257c478bd9Sstevel@tonic-gate 	struct vnode *dvp;	/* ptr to parent dir vnode */
12267c478bd9Sstevel@tonic-gate 	struct vnode *vp = NULL;
12277c478bd9Sstevel@tonic-gate 	struct pathname pn;
12287c478bd9Sstevel@tonic-gate 	int error;
12297c478bd9Sstevel@tonic-gate 	int in_crit = 0;
12307c478bd9Sstevel@tonic-gate 	struct vattr vattr;
12317c478bd9Sstevel@tonic-gate 	enum symfollow follow;
1232dd29fa4aSprabahar 	int estale_retry = 0;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	/* symlink interpretation */
12377c478bd9Sstevel@tonic-gate 	if ((flag & FNOFOLLOW) || excl == EXCL)
12387c478bd9Sstevel@tonic-gate 		follow = NO_FOLLOW;
12397c478bd9Sstevel@tonic-gate 	else
12407c478bd9Sstevel@tonic-gate 		follow = FOLLOW;
12417c478bd9Sstevel@tonic-gate 	flag &= ~(FNOFOLLOW|FNOLINKS);
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate top:
12447c478bd9Sstevel@tonic-gate 	/*
12457c478bd9Sstevel@tonic-gate 	 * Lookup directory.
12467c478bd9Sstevel@tonic-gate 	 * If new object is a file, call lower level to create it.
12477c478bd9Sstevel@tonic-gate 	 * Note that it is up to the lower level to enforce exclusive
12487c478bd9Sstevel@tonic-gate 	 * creation, if the file is already there.
12497c478bd9Sstevel@tonic-gate 	 * This allows the lower level to do whatever
12507c478bd9Sstevel@tonic-gate 	 * locking or protocol that is needed to prevent races.
12517c478bd9Sstevel@tonic-gate 	 * If the new object is directory call lower level to make
12527c478bd9Sstevel@tonic-gate 	 * the new directory, with "." and "..".
12537c478bd9Sstevel@tonic-gate 	 */
12547c478bd9Sstevel@tonic-gate 	if (error = pn_get(pnamep, seg, &pn))
12557c478bd9Sstevel@tonic-gate 		return (error);
12567c478bd9Sstevel@tonic-gate #ifdef  C2_AUDIT
12577c478bd9Sstevel@tonic-gate 	if (audit_active)
12587c478bd9Sstevel@tonic-gate 		audit_vncreate_start();
12597c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
12607c478bd9Sstevel@tonic-gate 	dvp = NULL;
12617c478bd9Sstevel@tonic-gate 	*vpp = NULL;
12627c478bd9Sstevel@tonic-gate 	/*
12637c478bd9Sstevel@tonic-gate 	 * lookup will find the parent directory for the vnode.
12647c478bd9Sstevel@tonic-gate 	 * When it is done the pn holds the name of the entry
12657c478bd9Sstevel@tonic-gate 	 * in the directory.
12667c478bd9Sstevel@tonic-gate 	 * If this is a non-exclusive create we also find the node itself.
12677c478bd9Sstevel@tonic-gate 	 */
12687c478bd9Sstevel@tonic-gate 	error = lookuppnat(&pn, NULL, follow, &dvp,
12697c478bd9Sstevel@tonic-gate 	    (excl == EXCL) ? NULLVPP : vpp, startvp);
12707c478bd9Sstevel@tonic-gate 	if (error) {
12717c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1272dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
12737c478bd9Sstevel@tonic-gate 			goto top;
12747c478bd9Sstevel@tonic-gate 		if (why == CRMKDIR && error == EINVAL)
12757c478bd9Sstevel@tonic-gate 			error = EEXIST;		/* SVID */
12767c478bd9Sstevel@tonic-gate 		return (error);
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	if (why != CRMKNOD)
12807c478bd9Sstevel@tonic-gate 		vap->va_mode &= ~VSVTX;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	/*
12837c478bd9Sstevel@tonic-gate 	 * If default ACLs are defined for the directory don't apply the
12847c478bd9Sstevel@tonic-gate 	 * umask if umask is passed.
12857c478bd9Sstevel@tonic-gate 	 */
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	if (umask) {
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 		vsecattr_t vsec;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 		vsec.vsa_aclcnt = 0;
12927c478bd9Sstevel@tonic-gate 		vsec.vsa_aclentp = NULL;
12937c478bd9Sstevel@tonic-gate 		vsec.vsa_dfaclcnt = 0;
12947c478bd9Sstevel@tonic-gate 		vsec.vsa_dfaclentp = NULL;
12957c478bd9Sstevel@tonic-gate 		vsec.vsa_mask = VSA_DFACLCNT;
1296*da6c28aaSamw 		error = VOP_GETSECATTR(dvp, &vsec, 0, CRED(), NULL);
1297fa9e4066Sahrens 		/*
1298fa9e4066Sahrens 		 * If error is ENOSYS then treat it as no error
1299fa9e4066Sahrens 		 * Don't want to force all file systems to support
1300fa9e4066Sahrens 		 * aclent_t style of ACL's.
1301fa9e4066Sahrens 		 */
1302fa9e4066Sahrens 		if (error == ENOSYS)
1303fa9e4066Sahrens 			error = 0;
1304fa9e4066Sahrens 		if (error) {
13057c478bd9Sstevel@tonic-gate 			if (*vpp != NULL)
13067c478bd9Sstevel@tonic-gate 				VN_RELE(*vpp);
13077c478bd9Sstevel@tonic-gate 			goto out;
1308fa9e4066Sahrens 		} else {
13097c478bd9Sstevel@tonic-gate 			/*
13107c478bd9Sstevel@tonic-gate 			 * Apply the umask if no default ACLs.
13117c478bd9Sstevel@tonic-gate 			 */
13127c478bd9Sstevel@tonic-gate 			if (vsec.vsa_dfaclcnt == 0)
13137c478bd9Sstevel@tonic-gate 				vap->va_mode &= ~umask;
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 			/*
1316fa9e4066Sahrens 			 * VOP_GETSECATTR() may have allocated memory for
1317fa9e4066Sahrens 			 * ACLs we didn't request, so double-check and
1318fa9e4066Sahrens 			 * free it if necessary.
13197c478bd9Sstevel@tonic-gate 			 */
13207c478bd9Sstevel@tonic-gate 			if (vsec.vsa_aclcnt && vsec.vsa_aclentp != NULL)
13217c478bd9Sstevel@tonic-gate 				kmem_free((caddr_t)vsec.vsa_aclentp,
13227c478bd9Sstevel@tonic-gate 				    vsec.vsa_aclcnt * sizeof (aclent_t));
13237c478bd9Sstevel@tonic-gate 			if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp != NULL)
13247c478bd9Sstevel@tonic-gate 				kmem_free((caddr_t)vsec.vsa_dfaclentp,
13257c478bd9Sstevel@tonic-gate 				    vsec.vsa_dfaclcnt * sizeof (aclent_t));
13267c478bd9Sstevel@tonic-gate 		}
1327fa9e4066Sahrens 	}
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	/*
13307c478bd9Sstevel@tonic-gate 	 * In general we want to generate EROFS if the file system is
13317c478bd9Sstevel@tonic-gate 	 * readonly.  However, POSIX (IEEE Std. 1003.1) section 5.3.1
13327c478bd9Sstevel@tonic-gate 	 * documents the open system call, and it says that O_CREAT has no
13337c478bd9Sstevel@tonic-gate 	 * effect if the file already exists.  Bug 1119649 states
13347c478bd9Sstevel@tonic-gate 	 * that open(path, O_CREAT, ...) fails when attempting to open an
13357c478bd9Sstevel@tonic-gate 	 * existing file on a read only file system.  Thus, the first part
13367c478bd9Sstevel@tonic-gate 	 * of the following if statement has 3 checks:
13377c478bd9Sstevel@tonic-gate 	 *	if the file exists &&
13387c478bd9Sstevel@tonic-gate 	 *		it is being open with write access &&
13397c478bd9Sstevel@tonic-gate 	 *		the file system is read only
13407c478bd9Sstevel@tonic-gate 	 *	then generate EROFS
13417c478bd9Sstevel@tonic-gate 	 */
13427c478bd9Sstevel@tonic-gate 	if ((*vpp != NULL && (mode & VWRITE) && ISROFILE(*vpp)) ||
13437c478bd9Sstevel@tonic-gate 	    (*vpp == NULL && dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
13447c478bd9Sstevel@tonic-gate 		if (*vpp)
13457c478bd9Sstevel@tonic-gate 			VN_RELE(*vpp);
13467c478bd9Sstevel@tonic-gate 		error = EROFS;
13477c478bd9Sstevel@tonic-gate 	} else if (excl == NONEXCL && *vpp != NULL) {
13487c478bd9Sstevel@tonic-gate 		vnode_t *rvp;
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 		/*
13517c478bd9Sstevel@tonic-gate 		 * File already exists.  If a mandatory lock has been
13527c478bd9Sstevel@tonic-gate 		 * applied, return error.
13537c478bd9Sstevel@tonic-gate 		 */
13547c478bd9Sstevel@tonic-gate 		vp = *vpp;
1355*da6c28aaSamw 		if (VOP_REALVP(vp, &rvp, NULL) != 0)
13567c478bd9Sstevel@tonic-gate 			rvp = vp;
13577c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_SIZE) && nbl_need_check(vp)) {
13587c478bd9Sstevel@tonic-gate 			nbl_start_crit(vp, RW_READER);
13597c478bd9Sstevel@tonic-gate 			in_crit = 1;
13607c478bd9Sstevel@tonic-gate 		}
13617c478bd9Sstevel@tonic-gate 		if (rvp->v_filocks != NULL || rvp->v_shrlocks != NULL) {
13627c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_MODE|AT_SIZE;
1363*da6c28aaSamw 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL)) {
13647c478bd9Sstevel@tonic-gate 				goto out;
13657c478bd9Sstevel@tonic-gate 			}
13667c478bd9Sstevel@tonic-gate 			if (MANDLOCK(vp, vattr.va_mode)) {
13677c478bd9Sstevel@tonic-gate 				error = EAGAIN;
13687c478bd9Sstevel@tonic-gate 				goto out;
13697c478bd9Sstevel@tonic-gate 			}
13707c478bd9Sstevel@tonic-gate 			/*
13717c478bd9Sstevel@tonic-gate 			 * File cannot be truncated if non-blocking mandatory
13727c478bd9Sstevel@tonic-gate 			 * locks are currently on the file.
13737c478bd9Sstevel@tonic-gate 			 */
13747c478bd9Sstevel@tonic-gate 			if ((vap->va_mask & AT_SIZE) && in_crit) {
13757c478bd9Sstevel@tonic-gate 				u_offset_t offset;
13767c478bd9Sstevel@tonic-gate 				ssize_t length;
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 				offset = vap->va_size > vattr.va_size ?
13797c478bd9Sstevel@tonic-gate 				    vattr.va_size : vap->va_size;
13807c478bd9Sstevel@tonic-gate 				length = vap->va_size > vattr.va_size ?
13817c478bd9Sstevel@tonic-gate 				    vap->va_size - vattr.va_size :
13827c478bd9Sstevel@tonic-gate 				    vattr.va_size - vap->va_size;
13837c478bd9Sstevel@tonic-gate 				if (nbl_conflict(vp, NBL_WRITE, offset,
1384*da6c28aaSamw 				    length, 0, NULL)) {
13857c478bd9Sstevel@tonic-gate 					error = EACCES;
13867c478bd9Sstevel@tonic-gate 					goto out;
13877c478bd9Sstevel@tonic-gate 				}
13887c478bd9Sstevel@tonic-gate 			}
13897c478bd9Sstevel@tonic-gate 		}
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 		/*
13927c478bd9Sstevel@tonic-gate 		 * If the file is the root of a VFS, we've crossed a
13937c478bd9Sstevel@tonic-gate 		 * mount point and the "containing" directory that we
13947c478bd9Sstevel@tonic-gate 		 * acquired above (dvp) is irrelevant because it's in
13957c478bd9Sstevel@tonic-gate 		 * a different file system.  We apply VOP_CREATE to the
13967c478bd9Sstevel@tonic-gate 		 * target itself instead of to the containing directory
13977c478bd9Sstevel@tonic-gate 		 * and supply a null path name to indicate (conventionally)
13987c478bd9Sstevel@tonic-gate 		 * the node itself as the "component" of interest.
13997c478bd9Sstevel@tonic-gate 		 *
14007c478bd9Sstevel@tonic-gate 		 * The intercession of the file system is necessary to
14017c478bd9Sstevel@tonic-gate 		 * ensure that the appropriate permission checks are
14027c478bd9Sstevel@tonic-gate 		 * done.
14037c478bd9Sstevel@tonic-gate 		 */
14047c478bd9Sstevel@tonic-gate 		if (vp->v_flag & VROOT) {
14057c478bd9Sstevel@tonic-gate 			ASSERT(why != CRMKDIR);
1406*da6c28aaSamw 			error = VOP_CREATE(vp, "", vap, excl, mode, vpp,
1407*da6c28aaSamw 			    CRED(), flag, NULL, NULL);
14087c478bd9Sstevel@tonic-gate 			/*
14097c478bd9Sstevel@tonic-gate 			 * If the create succeeded, it will have created
14107c478bd9Sstevel@tonic-gate 			 * a new reference to the vnode.  Give up the
14117c478bd9Sstevel@tonic-gate 			 * original reference.  The assertion should not
14127c478bd9Sstevel@tonic-gate 			 * get triggered because NBMAND locks only apply to
14137c478bd9Sstevel@tonic-gate 			 * VREG files.  And if in_crit is non-zero for some
14147c478bd9Sstevel@tonic-gate 			 * reason, detect that here, rather than when we
14157c478bd9Sstevel@tonic-gate 			 * deference a null vp.
14167c478bd9Sstevel@tonic-gate 			 */
14177c478bd9Sstevel@tonic-gate 			ASSERT(in_crit == 0);
14187c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
14197c478bd9Sstevel@tonic-gate 			vp = NULL;
14207c478bd9Sstevel@tonic-gate 			goto out;
14217c478bd9Sstevel@tonic-gate 		}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 		/*
14247c478bd9Sstevel@tonic-gate 		 * Large File API - non-large open (FOFFMAX flag not set)
14257c478bd9Sstevel@tonic-gate 		 * of regular file fails if the file size exceeds MAXOFF32_T.
14267c478bd9Sstevel@tonic-gate 		 */
14277c478bd9Sstevel@tonic-gate 		if (why != CRMKDIR &&
14287c478bd9Sstevel@tonic-gate 		    !(flag & FOFFMAX) &&
14297c478bd9Sstevel@tonic-gate 		    (vp->v_type == VREG)) {
14307c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
1431*da6c28aaSamw 			if ((error = VOP_GETATTR(vp, &vattr, 0,
1432*da6c28aaSamw 			    CRED(), NULL))) {
14337c478bd9Sstevel@tonic-gate 				goto out;
14347c478bd9Sstevel@tonic-gate 			}
14357c478bd9Sstevel@tonic-gate 			if ((vattr.va_size > (u_offset_t)MAXOFF32_T)) {
14367c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
14377c478bd9Sstevel@tonic-gate 				goto out;
14387c478bd9Sstevel@tonic-gate 			}
14397c478bd9Sstevel@tonic-gate 		}
14407c478bd9Sstevel@tonic-gate 	}
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	if (error == 0) {
14437c478bd9Sstevel@tonic-gate 		/*
14447c478bd9Sstevel@tonic-gate 		 * Call mkdir() if specified, otherwise create().
14457c478bd9Sstevel@tonic-gate 		 */
14467c478bd9Sstevel@tonic-gate 		int must_be_dir = pn_fixslash(&pn);	/* trailing '/'? */
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 		if (why == CRMKDIR)
1449*da6c28aaSamw 			/*
1450*da6c28aaSamw 			 * N.B., if vn_createat() ever requests
1451*da6c28aaSamw 			 * case-insensitive behavior then it will need
1452*da6c28aaSamw 			 * to be passed to VOP_MKDIR().  VOP_CREATE()
1453*da6c28aaSamw 			 * will already get it via "flag"
1454*da6c28aaSamw 			 */
1455*da6c28aaSamw 			error = VOP_MKDIR(dvp, pn.pn_path, vap, vpp, CRED(),
1456*da6c28aaSamw 			    NULL, 0, NULL);
14577c478bd9Sstevel@tonic-gate 		else if (!must_be_dir)
14587c478bd9Sstevel@tonic-gate 			error = VOP_CREATE(dvp, pn.pn_path, vap,
1459*da6c28aaSamw 			    excl, mode, vpp, CRED(), flag, NULL, NULL);
14607c478bd9Sstevel@tonic-gate 		else
14617c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
14627c478bd9Sstevel@tonic-gate 	}
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate out:
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
14677c478bd9Sstevel@tonic-gate 	if (audit_active)
14687c478bd9Sstevel@tonic-gate 		audit_vncreate_finish(*vpp, error);
14697c478bd9Sstevel@tonic-gate #endif  /* C2_AUDIT */
14707c478bd9Sstevel@tonic-gate 	if (in_crit) {
14717c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
14727c478bd9Sstevel@tonic-gate 		in_crit = 0;
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 	if (vp != NULL) {
14757c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
14767c478bd9Sstevel@tonic-gate 		vp = NULL;
14777c478bd9Sstevel@tonic-gate 	}
14787c478bd9Sstevel@tonic-gate 	pn_free(&pn);
14797c478bd9Sstevel@tonic-gate 	VN_RELE(dvp);
14807c478bd9Sstevel@tonic-gate 	/*
14817c478bd9Sstevel@tonic-gate 	 * The following clause was added to handle a problem
14827c478bd9Sstevel@tonic-gate 	 * with NFS consistency.  It is possible that a lookup
14837c478bd9Sstevel@tonic-gate 	 * of the file to be created succeeded, but the file
14847c478bd9Sstevel@tonic-gate 	 * itself doesn't actually exist on the server.  This
14857c478bd9Sstevel@tonic-gate 	 * is chiefly due to the DNLC containing an entry for
14867c478bd9Sstevel@tonic-gate 	 * the file which has been removed on the server.  In
14877c478bd9Sstevel@tonic-gate 	 * this case, we just start over.  If there was some
14887c478bd9Sstevel@tonic-gate 	 * other cause for the ESTALE error, then the lookup
14897c478bd9Sstevel@tonic-gate 	 * of the file will fail and the error will be returned
14907c478bd9Sstevel@tonic-gate 	 * above instead of looping around from here.
14917c478bd9Sstevel@tonic-gate 	 */
1492dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
14937c478bd9Sstevel@tonic-gate 		goto top;
14947c478bd9Sstevel@tonic-gate 	return (error);
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate int
14987c478bd9Sstevel@tonic-gate vn_link(char *from, char *to, enum uio_seg seg)
14997c478bd9Sstevel@tonic-gate {
15007c478bd9Sstevel@tonic-gate 	struct vnode *fvp;		/* from vnode ptr */
15017c478bd9Sstevel@tonic-gate 	struct vnode *tdvp;		/* to directory vnode ptr */
15027c478bd9Sstevel@tonic-gate 	struct pathname pn;
15037c478bd9Sstevel@tonic-gate 	int error;
15047c478bd9Sstevel@tonic-gate 	struct vattr vattr;
15057c478bd9Sstevel@tonic-gate 	dev_t fsid;
1506dd29fa4aSprabahar 	int estale_retry = 0;
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate top:
15097c478bd9Sstevel@tonic-gate 	fvp = tdvp = NULL;
15107c478bd9Sstevel@tonic-gate 	if (error = pn_get(to, seg, &pn))
15117c478bd9Sstevel@tonic-gate 		return (error);
15127c478bd9Sstevel@tonic-gate 	if (error = lookupname(from, seg, NO_FOLLOW, NULLVPP, &fvp))
15137c478bd9Sstevel@tonic-gate 		goto out;
15147c478bd9Sstevel@tonic-gate 	if (error = lookuppn(&pn, NULL, NO_FOLLOW, &tdvp, NULLVPP))
15157c478bd9Sstevel@tonic-gate 		goto out;
15167c478bd9Sstevel@tonic-gate 	/*
15177c478bd9Sstevel@tonic-gate 	 * Make sure both source vnode and target directory vnode are
15187c478bd9Sstevel@tonic-gate 	 * in the same vfs and that it is writeable.
15197c478bd9Sstevel@tonic-gate 	 */
15207c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_FSID;
1521*da6c28aaSamw 	if (error = VOP_GETATTR(fvp, &vattr, 0, CRED(), NULL))
15227c478bd9Sstevel@tonic-gate 		goto out;
15237c478bd9Sstevel@tonic-gate 	fsid = vattr.va_fsid;
15247c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_FSID;
1525*da6c28aaSamw 	if (error = VOP_GETATTR(tdvp, &vattr, 0, CRED(), NULL))
15267c478bd9Sstevel@tonic-gate 		goto out;
15277c478bd9Sstevel@tonic-gate 	if (fsid != vattr.va_fsid) {
15287c478bd9Sstevel@tonic-gate 		error = EXDEV;
15297c478bd9Sstevel@tonic-gate 		goto out;
15307c478bd9Sstevel@tonic-gate 	}
15317c478bd9Sstevel@tonic-gate 	if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) {
15327c478bd9Sstevel@tonic-gate 		error = EROFS;
15337c478bd9Sstevel@tonic-gate 		goto out;
15347c478bd9Sstevel@tonic-gate 	}
15357c478bd9Sstevel@tonic-gate 	/*
15367c478bd9Sstevel@tonic-gate 	 * Do the link.
15377c478bd9Sstevel@tonic-gate 	 */
15387c478bd9Sstevel@tonic-gate 	(void) pn_fixslash(&pn);
1539*da6c28aaSamw 	error = VOP_LINK(tdvp, fvp, pn.pn_path, CRED(), NULL, 0);
15407c478bd9Sstevel@tonic-gate out:
15417c478bd9Sstevel@tonic-gate 	pn_free(&pn);
15427c478bd9Sstevel@tonic-gate 	if (fvp)
15437c478bd9Sstevel@tonic-gate 		VN_RELE(fvp);
15447c478bd9Sstevel@tonic-gate 	if (tdvp)
15457c478bd9Sstevel@tonic-gate 		VN_RELE(tdvp);
1546dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
15477c478bd9Sstevel@tonic-gate 		goto top;
15487c478bd9Sstevel@tonic-gate 	return (error);
15497c478bd9Sstevel@tonic-gate }
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate int
15527c478bd9Sstevel@tonic-gate vn_rename(char *from, char *to, enum uio_seg seg)
15537c478bd9Sstevel@tonic-gate {
15547c478bd9Sstevel@tonic-gate 	return (vn_renameat(NULL, from, NULL, to, seg));
15557c478bd9Sstevel@tonic-gate }
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate int
15587c478bd9Sstevel@tonic-gate vn_renameat(vnode_t *fdvp, char *fname, vnode_t *tdvp,
15597c478bd9Sstevel@tonic-gate 		char *tname, enum uio_seg seg)
15607c478bd9Sstevel@tonic-gate {
15617c478bd9Sstevel@tonic-gate 	int error;
15627c478bd9Sstevel@tonic-gate 	struct vattr vattr;
15637c478bd9Sstevel@tonic-gate 	struct pathname fpn;		/* from pathname */
15647c478bd9Sstevel@tonic-gate 	struct pathname tpn;		/* to pathname */
15657c478bd9Sstevel@tonic-gate 	dev_t fsid;
1566*da6c28aaSamw 	int in_crit_src, in_crit_targ;
15677c478bd9Sstevel@tonic-gate 	vnode_t *fromvp, *fvp;
1568*da6c28aaSamw 	vnode_t *tovp, *targvp;
1569dd29fa4aSprabahar 	int estale_retry = 0;
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate top:
1572*da6c28aaSamw 	fvp = fromvp = tovp = targvp = NULL;
1573*da6c28aaSamw 	in_crit_src = in_crit_targ = 0;
15747c478bd9Sstevel@tonic-gate 	/*
15757c478bd9Sstevel@tonic-gate 	 * Get to and from pathnames.
15767c478bd9Sstevel@tonic-gate 	 */
15777c478bd9Sstevel@tonic-gate 	if (error = pn_get(fname, seg, &fpn))
15787c478bd9Sstevel@tonic-gate 		return (error);
15797c478bd9Sstevel@tonic-gate 	if (error = pn_get(tname, seg, &tpn)) {
15807c478bd9Sstevel@tonic-gate 		pn_free(&fpn);
15817c478bd9Sstevel@tonic-gate 		return (error);
15827c478bd9Sstevel@tonic-gate 	}
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	/*
15857c478bd9Sstevel@tonic-gate 	 * First we need to resolve the correct directories
15867c478bd9Sstevel@tonic-gate 	 * The passed in directories may only be a starting point,
15877c478bd9Sstevel@tonic-gate 	 * but we need the real directories the file(s) live in.
15887c478bd9Sstevel@tonic-gate 	 * For example the fname may be something like usr/lib/sparc
15897c478bd9Sstevel@tonic-gate 	 * and we were passed in the / directory, but we need to
15907c478bd9Sstevel@tonic-gate 	 * use the lib directory for the rename.
15917c478bd9Sstevel@tonic-gate 	 */
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate #ifdef  C2_AUDIT
15947c478bd9Sstevel@tonic-gate 	if (audit_active)
15957c478bd9Sstevel@tonic-gate 		audit_setfsat_path(1);
15967c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
15977c478bd9Sstevel@tonic-gate 	/*
15987c478bd9Sstevel@tonic-gate 	 * Lookup to and from directories.
15997c478bd9Sstevel@tonic-gate 	 */
16007c478bd9Sstevel@tonic-gate 	if (error = lookuppnat(&fpn, NULL, NO_FOLLOW, &fromvp, &fvp, fdvp)) {
16017c478bd9Sstevel@tonic-gate 		goto out;
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	/*
16057c478bd9Sstevel@tonic-gate 	 * Make sure there is an entry.
16067c478bd9Sstevel@tonic-gate 	 */
16077c478bd9Sstevel@tonic-gate 	if (fvp == NULL) {
16087c478bd9Sstevel@tonic-gate 		error = ENOENT;
16097c478bd9Sstevel@tonic-gate 		goto out;
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate #ifdef  C2_AUDIT
16137c478bd9Sstevel@tonic-gate 	if (audit_active)
16147c478bd9Sstevel@tonic-gate 		audit_setfsat_path(3);
16157c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
1616*da6c28aaSamw 	if (error = lookuppnat(&tpn, NULL, NO_FOLLOW, &tovp, &targvp, tdvp)) {
16177c478bd9Sstevel@tonic-gate 		goto out;
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	/*
16217c478bd9Sstevel@tonic-gate 	 * Make sure both the from vnode directory and the to directory
16227c478bd9Sstevel@tonic-gate 	 * are in the same vfs and the to directory is writable.
16237c478bd9Sstevel@tonic-gate 	 * We check fsid's, not vfs pointers, so loopback fs works.
16247c478bd9Sstevel@tonic-gate 	 */
16257c478bd9Sstevel@tonic-gate 	if (fromvp != tovp) {
16267c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_FSID;
1627*da6c28aaSamw 		if (error = VOP_GETATTR(fromvp, &vattr, 0, CRED(), NULL))
16287c478bd9Sstevel@tonic-gate 			goto out;
16297c478bd9Sstevel@tonic-gate 		fsid = vattr.va_fsid;
16307c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_FSID;
1631*da6c28aaSamw 		if (error = VOP_GETATTR(tovp, &vattr, 0, CRED(), NULL))
16327c478bd9Sstevel@tonic-gate 			goto out;
16337c478bd9Sstevel@tonic-gate 		if (fsid != vattr.va_fsid) {
16347c478bd9Sstevel@tonic-gate 			error = EXDEV;
16357c478bd9Sstevel@tonic-gate 			goto out;
16367c478bd9Sstevel@tonic-gate 		}
16377c478bd9Sstevel@tonic-gate 	}
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	if (tovp->v_vfsp->vfs_flag & VFS_RDONLY) {
16407c478bd9Sstevel@tonic-gate 		error = EROFS;
16417c478bd9Sstevel@tonic-gate 		goto out;
16427c478bd9Sstevel@tonic-gate 	}
16437c478bd9Sstevel@tonic-gate 
1644*da6c28aaSamw 	if (targvp && (fvp != targvp)) {
1645*da6c28aaSamw 		nbl_start_crit(targvp, RW_READER);
1646*da6c28aaSamw 		in_crit_targ = 1;
1647*da6c28aaSamw 		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
1648*da6c28aaSamw 			error = EACCES;
1649*da6c28aaSamw 			goto out;
1650*da6c28aaSamw 		}
1651*da6c28aaSamw 	}
1652*da6c28aaSamw 
16537c478bd9Sstevel@tonic-gate 	if (nbl_need_check(fvp)) {
16547c478bd9Sstevel@tonic-gate 		nbl_start_crit(fvp, RW_READER);
1655*da6c28aaSamw 		in_crit_src = 1;
1656*da6c28aaSamw 		if (nbl_conflict(fvp, NBL_RENAME, 0, 0, 0, NULL)) {
16577c478bd9Sstevel@tonic-gate 			error = EACCES;
16587c478bd9Sstevel@tonic-gate 			goto out;
16597c478bd9Sstevel@tonic-gate 		}
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	/*
16637c478bd9Sstevel@tonic-gate 	 * Do the rename.
16647c478bd9Sstevel@tonic-gate 	 */
16657c478bd9Sstevel@tonic-gate 	(void) pn_fixslash(&tpn);
1666*da6c28aaSamw 	error = VOP_RENAME(fromvp, fpn.pn_path, tovp, tpn.pn_path, CRED(),
1667*da6c28aaSamw 	    NULL, 0);
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate out:
16707c478bd9Sstevel@tonic-gate 	pn_free(&fpn);
16717c478bd9Sstevel@tonic-gate 	pn_free(&tpn);
1672*da6c28aaSamw 	if (in_crit_src)
16737c478bd9Sstevel@tonic-gate 		nbl_end_crit(fvp);
1674*da6c28aaSamw 	if (in_crit_targ)
1675*da6c28aaSamw 		nbl_end_crit(targvp);
16767c478bd9Sstevel@tonic-gate 	if (fromvp)
16777c478bd9Sstevel@tonic-gate 		VN_RELE(fromvp);
16787c478bd9Sstevel@tonic-gate 	if (tovp)
16797c478bd9Sstevel@tonic-gate 		VN_RELE(tovp);
1680*da6c28aaSamw 	if (targvp)
1681*da6c28aaSamw 		VN_RELE(targvp);
16827c478bd9Sstevel@tonic-gate 	if (fvp)
16837c478bd9Sstevel@tonic-gate 		VN_RELE(fvp);
1684dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
16857c478bd9Sstevel@tonic-gate 		goto top;
16867c478bd9Sstevel@tonic-gate 	return (error);
16877c478bd9Sstevel@tonic-gate }
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate /*
16907c478bd9Sstevel@tonic-gate  * Remove a file or directory.
16917c478bd9Sstevel@tonic-gate  */
16927c478bd9Sstevel@tonic-gate int
16937c478bd9Sstevel@tonic-gate vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
16947c478bd9Sstevel@tonic-gate {
16957c478bd9Sstevel@tonic-gate 	return (vn_removeat(NULL, fnamep, seg, dirflag));
16967c478bd9Sstevel@tonic-gate }
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate int
16997c478bd9Sstevel@tonic-gate vn_removeat(vnode_t *startvp, char *fnamep, enum uio_seg seg, enum rm dirflag)
17007c478bd9Sstevel@tonic-gate {
17017c478bd9Sstevel@tonic-gate 	struct vnode *vp;		/* entry vnode */
17027c478bd9Sstevel@tonic-gate 	struct vnode *dvp;		/* ptr to parent dir vnode */
17037c478bd9Sstevel@tonic-gate 	struct vnode *coveredvp;
17047c478bd9Sstevel@tonic-gate 	struct pathname pn;		/* name of entry */
17057c478bd9Sstevel@tonic-gate 	enum vtype vtype;
17067c478bd9Sstevel@tonic-gate 	int error;
17077c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
17087c478bd9Sstevel@tonic-gate 	struct vfs *dvfsp;	/* ptr to parent dir vfs */
17097c478bd9Sstevel@tonic-gate 	int in_crit = 0;
1710dd29fa4aSprabahar 	int estale_retry = 0;
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate top:
17137c478bd9Sstevel@tonic-gate 	if (error = pn_get(fnamep, seg, &pn))
17147c478bd9Sstevel@tonic-gate 		return (error);
17157c478bd9Sstevel@tonic-gate 	dvp = vp = NULL;
17167c478bd9Sstevel@tonic-gate 	if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &dvp, &vp, startvp)) {
17177c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1718dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
17197c478bd9Sstevel@tonic-gate 			goto top;
17207c478bd9Sstevel@tonic-gate 		return (error);
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	/*
17247c478bd9Sstevel@tonic-gate 	 * Make sure there is an entry.
17257c478bd9Sstevel@tonic-gate 	 */
17267c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
17277c478bd9Sstevel@tonic-gate 		error = ENOENT;
17287c478bd9Sstevel@tonic-gate 		goto out;
17297c478bd9Sstevel@tonic-gate 	}
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	vfsp = vp->v_vfsp;
17327c478bd9Sstevel@tonic-gate 	dvfsp = dvp->v_vfsp;
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	/*
17357c478bd9Sstevel@tonic-gate 	 * If the named file is the root of a mounted filesystem, fail,
17367c478bd9Sstevel@tonic-gate 	 * unless it's marked unlinkable.  In that case, unmount the
17377c478bd9Sstevel@tonic-gate 	 * filesystem and proceed to unlink the covered vnode.  (If the
17387c478bd9Sstevel@tonic-gate 	 * covered vnode is a directory, use rmdir instead of unlink,
17397c478bd9Sstevel@tonic-gate 	 * to avoid file system corruption.)
17407c478bd9Sstevel@tonic-gate 	 */
17417c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VROOT) {
1742b9c98e29Spf199842 		if ((vfsp->vfs_flag & VFS_UNLINKABLE) == 0) {
1743b9c98e29Spf199842 			error = EBUSY;
1744b9c98e29Spf199842 			goto out;
1745b9c98e29Spf199842 		}
1746b9c98e29Spf199842 
1747b9c98e29Spf199842 		/*
1748b9c98e29Spf199842 		 * Namefs specific code starts here.
1749b9c98e29Spf199842 		 */
1750b9c98e29Spf199842 
17517c478bd9Sstevel@tonic-gate 		if (dirflag == RMDIRECTORY) {
17527c478bd9Sstevel@tonic-gate 			/*
17537c478bd9Sstevel@tonic-gate 			 * User called rmdir(2) on a file that has
17547c478bd9Sstevel@tonic-gate 			 * been namefs mounted on top of.  Since
17557c478bd9Sstevel@tonic-gate 			 * namefs doesn't allow directories to
17567c478bd9Sstevel@tonic-gate 			 * be mounted on other files we know
17577c478bd9Sstevel@tonic-gate 			 * vp is not of type VDIR so fail to operation.
17587c478bd9Sstevel@tonic-gate 			 */
17597c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
17607c478bd9Sstevel@tonic-gate 			goto out;
17617c478bd9Sstevel@tonic-gate 		}
1762b9c98e29Spf199842 
1763b9c98e29Spf199842 		/*
1764b9c98e29Spf199842 		 * If VROOT is still set after grabbing vp->v_lock,
1765b9c98e29Spf199842 		 * noone has finished nm_unmount so far and coveredvp
1766b9c98e29Spf199842 		 * is valid.
1767b9c98e29Spf199842 		 * If we manage to grab vn_vfswlock(coveredvp) before releasing
1768b9c98e29Spf199842 		 * vp->v_lock, any race window is eliminated.
1769b9c98e29Spf199842 		 */
1770b9c98e29Spf199842 
1771b9c98e29Spf199842 		mutex_enter(&vp->v_lock);
1772b9c98e29Spf199842 		if ((vp->v_flag & VROOT) == 0) {
1773b9c98e29Spf199842 			/* Someone beat us to the unmount */
1774b9c98e29Spf199842 			mutex_exit(&vp->v_lock);
1775b9c98e29Spf199842 			error = EBUSY;
1776b9c98e29Spf199842 			goto out;
1777b9c98e29Spf199842 		}
1778b9c98e29Spf199842 		vfsp = vp->v_vfsp;
17797c478bd9Sstevel@tonic-gate 		coveredvp = vfsp->vfs_vnodecovered;
1780b9c98e29Spf199842 		ASSERT(coveredvp);
1781b9c98e29Spf199842 		/*
1782b9c98e29Spf199842 		 * Note: Implementation of vn_vfswlock shows that ordering of
1783b9c98e29Spf199842 		 * v_lock / vn_vfswlock is not an issue here.
1784b9c98e29Spf199842 		 */
1785b9c98e29Spf199842 		error = vn_vfswlock(coveredvp);
1786b9c98e29Spf199842 		mutex_exit(&vp->v_lock);
1787b9c98e29Spf199842 
1788b9c98e29Spf199842 		if (error)
1789b9c98e29Spf199842 			goto out;
1790b9c98e29Spf199842 
17917c478bd9Sstevel@tonic-gate 		VN_HOLD(coveredvp);
17927c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
17937c478bd9Sstevel@tonic-gate 		error = dounmount(vfsp, 0, CRED());
1794b9c98e29Spf199842 
17957c478bd9Sstevel@tonic-gate 		/*
17967c478bd9Sstevel@tonic-gate 		 * Unmounted the namefs file system; now get
17977c478bd9Sstevel@tonic-gate 		 * the object it was mounted over.
17987c478bd9Sstevel@tonic-gate 		 */
17997c478bd9Sstevel@tonic-gate 		vp = coveredvp;
18007c478bd9Sstevel@tonic-gate 		/*
18017c478bd9Sstevel@tonic-gate 		 * If namefs was mounted over a directory, then
18027c478bd9Sstevel@tonic-gate 		 * we want to use rmdir() instead of unlink().
18037c478bd9Sstevel@tonic-gate 		 */
18047c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR)
18057c478bd9Sstevel@tonic-gate 			dirflag = RMDIRECTORY;
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 		if (error)
18087c478bd9Sstevel@tonic-gate 			goto out;
18097c478bd9Sstevel@tonic-gate 	}
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	/*
18127c478bd9Sstevel@tonic-gate 	 * Make sure filesystem is writeable.
18137c478bd9Sstevel@tonic-gate 	 * We check the parent directory's vfs in case this is an lofs vnode.
18147c478bd9Sstevel@tonic-gate 	 */
18157c478bd9Sstevel@tonic-gate 	if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) {
18167c478bd9Sstevel@tonic-gate 		error = EROFS;
18177c478bd9Sstevel@tonic-gate 		goto out;
18187c478bd9Sstevel@tonic-gate 	}
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	vtype = vp->v_type;
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	/*
18237c478bd9Sstevel@tonic-gate 	 * If there is the possibility of an nbmand share reservation, make
18247c478bd9Sstevel@tonic-gate 	 * sure it's okay to remove the file.  Keep a reference to the
18257c478bd9Sstevel@tonic-gate 	 * vnode, so that we can exit the nbl critical region after
18267c478bd9Sstevel@tonic-gate 	 * calling VOP_REMOVE.
18277c478bd9Sstevel@tonic-gate 	 * If there is no possibility of an nbmand share reservation,
18287c478bd9Sstevel@tonic-gate 	 * release the vnode reference now.  Filesystems like NFS may
18297c478bd9Sstevel@tonic-gate 	 * behave differently if there is an extra reference, so get rid of
18307c478bd9Sstevel@tonic-gate 	 * this one.  Fortunately, we can't have nbmand mounts on NFS
18317c478bd9Sstevel@tonic-gate 	 * filesystems.
18327c478bd9Sstevel@tonic-gate 	 */
18337c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
18347c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
18357c478bd9Sstevel@tonic-gate 		in_crit = 1;
1836*da6c28aaSamw 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
18377c478bd9Sstevel@tonic-gate 			error = EACCES;
18387c478bd9Sstevel@tonic-gate 			goto out;
18397c478bd9Sstevel@tonic-gate 		}
18407c478bd9Sstevel@tonic-gate 	} else {
18417c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
18427c478bd9Sstevel@tonic-gate 		vp = NULL;
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	if (dirflag == RMDIRECTORY) {
18467c478bd9Sstevel@tonic-gate 		/*
18477c478bd9Sstevel@tonic-gate 		 * Caller is using rmdir(2), which can only be applied to
18487c478bd9Sstevel@tonic-gate 		 * directories.
18497c478bd9Sstevel@tonic-gate 		 */
18507c478bd9Sstevel@tonic-gate 		if (vtype != VDIR) {
18517c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
18527c478bd9Sstevel@tonic-gate 		} else {
18537c478bd9Sstevel@tonic-gate 			vnode_t *cwd;
18547c478bd9Sstevel@tonic-gate 			proc_t *pp = curproc;
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
18577c478bd9Sstevel@tonic-gate 			cwd = PTOU(pp)->u_cdir;
18587c478bd9Sstevel@tonic-gate 			VN_HOLD(cwd);
18597c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
1860*da6c28aaSamw 			error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED(),
1861*da6c28aaSamw 			    NULL, 0);
18627c478bd9Sstevel@tonic-gate 			VN_RELE(cwd);
18637c478bd9Sstevel@tonic-gate 		}
18647c478bd9Sstevel@tonic-gate 	} else {
18657c478bd9Sstevel@tonic-gate 		/*
18667c478bd9Sstevel@tonic-gate 		 * Unlink(2) can be applied to anything.
18677c478bd9Sstevel@tonic-gate 		 */
1868*da6c28aaSamw 		error = VOP_REMOVE(dvp, pn.pn_path, CRED(), NULL, 0);
18697c478bd9Sstevel@tonic-gate 	}
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate out:
18727c478bd9Sstevel@tonic-gate 	pn_free(&pn);
18737c478bd9Sstevel@tonic-gate 	if (in_crit) {
18747c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
18757c478bd9Sstevel@tonic-gate 		in_crit = 0;
18767c478bd9Sstevel@tonic-gate 	}
18777c478bd9Sstevel@tonic-gate 	if (vp != NULL)
18787c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
18797c478bd9Sstevel@tonic-gate 	if (dvp != NULL)
18807c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
1881dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
18827c478bd9Sstevel@tonic-gate 		goto top;
18837c478bd9Sstevel@tonic-gate 	return (error);
18847c478bd9Sstevel@tonic-gate }
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate /*
18877c478bd9Sstevel@tonic-gate  * Utility function to compare equality of vnodes.
18887c478bd9Sstevel@tonic-gate  * Compare the underlying real vnodes, if there are underlying vnodes.
18897c478bd9Sstevel@tonic-gate  * This is a more thorough comparison than the VN_CMP() macro provides.
18907c478bd9Sstevel@tonic-gate  */
18917c478bd9Sstevel@tonic-gate int
18927c478bd9Sstevel@tonic-gate vn_compare(vnode_t *vp1, vnode_t *vp2)
18937c478bd9Sstevel@tonic-gate {
18947c478bd9Sstevel@tonic-gate 	vnode_t *realvp;
18957c478bd9Sstevel@tonic-gate 
1896*da6c28aaSamw 	if (vp1 != NULL && VOP_REALVP(vp1, &realvp, NULL) == 0)
18977c478bd9Sstevel@tonic-gate 		vp1 = realvp;
1898*da6c28aaSamw 	if (vp2 != NULL && VOP_REALVP(vp2, &realvp, NULL) == 0)
18997c478bd9Sstevel@tonic-gate 		vp2 = realvp;
19007c478bd9Sstevel@tonic-gate 	return (VN_CMP(vp1, vp2));
19017c478bd9Sstevel@tonic-gate }
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate /*
19047c478bd9Sstevel@tonic-gate  * The number of locks to hash into.  This value must be a power
19057c478bd9Sstevel@tonic-gate  * of 2 minus 1 and should probably also be prime.
19067c478bd9Sstevel@tonic-gate  */
19077c478bd9Sstevel@tonic-gate #define	NUM_BUCKETS	1023
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate struct  vn_vfslocks_bucket {
19107c478bd9Sstevel@tonic-gate 	kmutex_t vb_lock;
19117c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vb_list;
19127c478bd9Sstevel@tonic-gate 	char pad[64 - sizeof (kmutex_t) - sizeof (void *)];
19137c478bd9Sstevel@tonic-gate };
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate /*
19167c478bd9Sstevel@tonic-gate  * Total number of buckets will be NUM_BUCKETS + 1 .
19177c478bd9Sstevel@tonic-gate  */
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate #pragma	align	64(vn_vfslocks_buckets)
19207c478bd9Sstevel@tonic-gate static	struct vn_vfslocks_bucket	vn_vfslocks_buckets[NUM_BUCKETS + 1];
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate #define	VN_VFSLOCKS_SHIFT	9
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate #define	VN_VFSLOCKS_HASH(vfsvpptr)	\
19257c478bd9Sstevel@tonic-gate 	((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS)
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate /*
19287c478bd9Sstevel@tonic-gate  * vn_vfslocks_getlock() uses an HASH scheme to generate
19297c478bd9Sstevel@tonic-gate  * rwstlock using vfs/vnode pointer passed to it.
19307c478bd9Sstevel@tonic-gate  *
19317c478bd9Sstevel@tonic-gate  * vn_vfslocks_rele() releases a reference in the
19327c478bd9Sstevel@tonic-gate  * HASH table which allows the entry allocated by
19337c478bd9Sstevel@tonic-gate  * vn_vfslocks_getlock() to be freed at a later
19347c478bd9Sstevel@tonic-gate  * stage when the refcount drops to zero.
19357c478bd9Sstevel@tonic-gate  */
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate vn_vfslocks_entry_t *
19387c478bd9Sstevel@tonic-gate vn_vfslocks_getlock(void *vfsvpptr)
19397c478bd9Sstevel@tonic-gate {
19407c478bd9Sstevel@tonic-gate 	struct vn_vfslocks_bucket *bp;
19417c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vep;
19427c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *tvep;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	ASSERT(vfsvpptr != NULL);
19457c478bd9Sstevel@tonic-gate 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)];
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
19487c478bd9Sstevel@tonic-gate 	for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
19497c478bd9Sstevel@tonic-gate 		if (vep->ve_vpvfs == vfsvpptr) {
19507c478bd9Sstevel@tonic-gate 			vep->ve_refcnt++;
19517c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->vb_lock);
19527c478bd9Sstevel@tonic-gate 			return (vep);
19537c478bd9Sstevel@tonic-gate 		}
19547c478bd9Sstevel@tonic-gate 	}
19557c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
19567c478bd9Sstevel@tonic-gate 	vep = kmem_alloc(sizeof (*vep), KM_SLEEP);
19577c478bd9Sstevel@tonic-gate 	rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL);
19587c478bd9Sstevel@tonic-gate 	vep->ve_vpvfs = (char *)vfsvpptr;
19597c478bd9Sstevel@tonic-gate 	vep->ve_refcnt = 1;
19607c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
19617c478bd9Sstevel@tonic-gate 	for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) {
19627c478bd9Sstevel@tonic-gate 		if (tvep->ve_vpvfs == vfsvpptr) {
19637c478bd9Sstevel@tonic-gate 			tvep->ve_refcnt++;
19647c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->vb_lock);
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 			/*
19677c478bd9Sstevel@tonic-gate 			 * There is already an entry in the hash
19687c478bd9Sstevel@tonic-gate 			 * destroy what we just allocated.
19697c478bd9Sstevel@tonic-gate 			 */
19707c478bd9Sstevel@tonic-gate 			rwst_destroy(&vep->ve_lock);
19717c478bd9Sstevel@tonic-gate 			kmem_free(vep, sizeof (*vep));
19727c478bd9Sstevel@tonic-gate 			return (tvep);
19737c478bd9Sstevel@tonic-gate 		}
19747c478bd9Sstevel@tonic-gate 	}
19757c478bd9Sstevel@tonic-gate 	vep->ve_next = bp->vb_list;
19767c478bd9Sstevel@tonic-gate 	bp->vb_list = vep;
19777c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
19787c478bd9Sstevel@tonic-gate 	return (vep);
19797c478bd9Sstevel@tonic-gate }
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate void
19827c478bd9Sstevel@tonic-gate vn_vfslocks_rele(vn_vfslocks_entry_t *vepent)
19837c478bd9Sstevel@tonic-gate {
19847c478bd9Sstevel@tonic-gate 	struct vn_vfslocks_bucket *bp;
19857c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vep;
19867c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *pvep;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	ASSERT(vepent != NULL);
19897c478bd9Sstevel@tonic-gate 	ASSERT(vepent->ve_vpvfs != NULL);
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)];
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
19947c478bd9Sstevel@tonic-gate 	vepent->ve_refcnt--;
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	if ((int32_t)vepent->ve_refcnt < 0)
19977c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative");
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	if (vepent->ve_refcnt == 0) {
20007c478bd9Sstevel@tonic-gate 		for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
20017c478bd9Sstevel@tonic-gate 			if (vep->ve_vpvfs == vepent->ve_vpvfs) {
20027c478bd9Sstevel@tonic-gate 				if (bp->vb_list == vep)
20037c478bd9Sstevel@tonic-gate 					bp->vb_list = vep->ve_next;
20047c478bd9Sstevel@tonic-gate 				else {
20057c478bd9Sstevel@tonic-gate 					/* LINTED */
20067c478bd9Sstevel@tonic-gate 					pvep->ve_next = vep->ve_next;
20077c478bd9Sstevel@tonic-gate 				}
20087c478bd9Sstevel@tonic-gate 				mutex_exit(&bp->vb_lock);
20097c478bd9Sstevel@tonic-gate 				rwst_destroy(&vep->ve_lock);
20107c478bd9Sstevel@tonic-gate 				kmem_free(vep, sizeof (*vep));
20117c478bd9Sstevel@tonic-gate 				return;
20127c478bd9Sstevel@tonic-gate 			}
20137c478bd9Sstevel@tonic-gate 			pvep = vep;
20147c478bd9Sstevel@tonic-gate 		}
20157c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found");
20167c478bd9Sstevel@tonic-gate 	}
20177c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
20187c478bd9Sstevel@tonic-gate }
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate /*
20217c478bd9Sstevel@tonic-gate  * vn_vfswlock_wait is used to implement a lock which is logically a writers
20227c478bd9Sstevel@tonic-gate  * lock protecting the v_vfsmountedhere field.
20237c478bd9Sstevel@tonic-gate  * vn_vfswlock_wait has been modified to be similar to vn_vfswlock,
20247c478bd9Sstevel@tonic-gate  * except that it blocks to acquire the lock VVFSLOCK.
20257c478bd9Sstevel@tonic-gate  *
20267c478bd9Sstevel@tonic-gate  * traverse() and routines re-implementing part of traverse (e.g. autofs)
20277c478bd9Sstevel@tonic-gate  * need to hold this lock. mount(), vn_rename(), vn_remove() and so on
20287c478bd9Sstevel@tonic-gate  * need the non-blocking version of the writers lock i.e. vn_vfswlock
20297c478bd9Sstevel@tonic-gate  */
20307c478bd9Sstevel@tonic-gate int
20317c478bd9Sstevel@tonic-gate vn_vfswlock_wait(vnode_t *vp)
20327c478bd9Sstevel@tonic-gate {
20337c478bd9Sstevel@tonic-gate 	int retval;
20347c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
20357c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
20387c478bd9Sstevel@tonic-gate 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER);
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	if (retval == EINTR) {
20417c478bd9Sstevel@tonic-gate 		vn_vfslocks_rele(vpvfsentry);
20427c478bd9Sstevel@tonic-gate 		return (EINTR);
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate 	return (retval);
20457c478bd9Sstevel@tonic-gate }
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate int
20487c478bd9Sstevel@tonic-gate vn_vfsrlock_wait(vnode_t *vp)
20497c478bd9Sstevel@tonic-gate {
20507c478bd9Sstevel@tonic-gate 	int retval;
20517c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
20527c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
20557c478bd9Sstevel@tonic-gate 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER);
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	if (retval == EINTR) {
20587c478bd9Sstevel@tonic-gate 		vn_vfslocks_rele(vpvfsentry);
20597c478bd9Sstevel@tonic-gate 		return (EINTR);
20607c478bd9Sstevel@tonic-gate 	}
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	return (retval);
20637c478bd9Sstevel@tonic-gate }
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate /*
20677c478bd9Sstevel@tonic-gate  * vn_vfswlock is used to implement a lock which is logically a writers lock
20687c478bd9Sstevel@tonic-gate  * protecting the v_vfsmountedhere field.
20697c478bd9Sstevel@tonic-gate  */
20707c478bd9Sstevel@tonic-gate int
20717c478bd9Sstevel@tonic-gate vn_vfswlock(vnode_t *vp)
20727c478bd9Sstevel@tonic-gate {
20737c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	/*
20767c478bd9Sstevel@tonic-gate 	 * If vp is NULL then somebody is trying to lock the covered vnode
20777c478bd9Sstevel@tonic-gate 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
20787c478bd9Sstevel@tonic-gate 	 * only happen when unmounting /.  Since that operation will fail
20797c478bd9Sstevel@tonic-gate 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
20807c478bd9Sstevel@tonic-gate 	 */
20817c478bd9Sstevel@tonic-gate 	if (vp == NULL)
20827c478bd9Sstevel@tonic-gate 		return (EBUSY);
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
20877c478bd9Sstevel@tonic-gate 		return (0);
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
20907c478bd9Sstevel@tonic-gate 	return (EBUSY);
20917c478bd9Sstevel@tonic-gate }
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate int
20947c478bd9Sstevel@tonic-gate vn_vfsrlock(vnode_t *vp)
20957c478bd9Sstevel@tonic-gate {
20967c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	/*
20997c478bd9Sstevel@tonic-gate 	 * If vp is NULL then somebody is trying to lock the covered vnode
21007c478bd9Sstevel@tonic-gate 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
21017c478bd9Sstevel@tonic-gate 	 * only happen when unmounting /.  Since that operation will fail
21027c478bd9Sstevel@tonic-gate 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
21037c478bd9Sstevel@tonic-gate 	 */
21047c478bd9Sstevel@tonic-gate 	if (vp == NULL)
21057c478bd9Sstevel@tonic-gate 		return (EBUSY);
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
21107c478bd9Sstevel@tonic-gate 		return (0);
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
21137c478bd9Sstevel@tonic-gate 	return (EBUSY);
21147c478bd9Sstevel@tonic-gate }
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate void
21177c478bd9Sstevel@tonic-gate vn_vfsunlock(vnode_t *vp)
21187c478bd9Sstevel@tonic-gate {
21197c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	/*
21227c478bd9Sstevel@tonic-gate 	 * ve_refcnt needs to be decremented twice.
21237c478bd9Sstevel@tonic-gate 	 * 1. To release refernce after a call to vn_vfslocks_getlock()
21247c478bd9Sstevel@tonic-gate 	 * 2. To release the reference from the locking routines like
21257c478bd9Sstevel@tonic-gate 	 *    vn_vfsrlock/vn_vfswlock etc,.
21267c478bd9Sstevel@tonic-gate 	 */
21277c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
21287c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	rwst_exit(&vpvfsentry->ve_lock);
21317c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
21327c478bd9Sstevel@tonic-gate }
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate int
21357c478bd9Sstevel@tonic-gate vn_vfswlock_held(vnode_t *vp)
21367c478bd9Sstevel@tonic-gate {
21377c478bd9Sstevel@tonic-gate 	int held;
21387c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
21437c478bd9Sstevel@tonic-gate 	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
21467c478bd9Sstevel@tonic-gate 	return (held);
21477c478bd9Sstevel@tonic-gate }
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate int
21517c478bd9Sstevel@tonic-gate vn_make_ops(
21527c478bd9Sstevel@tonic-gate 	const char *name,			/* Name of file system */
21537c478bd9Sstevel@tonic-gate 	const fs_operation_def_t *templ,	/* Operation specification */
21547c478bd9Sstevel@tonic-gate 	vnodeops_t **actual)			/* Return the vnodeops */
21557c478bd9Sstevel@tonic-gate {
21567c478bd9Sstevel@tonic-gate 	int unused_ops;
21577c478bd9Sstevel@tonic-gate 	int error;
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	*actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP);
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	(*actual)->vnop_name = name;
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ);
21647c478bd9Sstevel@tonic-gate 	if (error) {
21657c478bd9Sstevel@tonic-gate 		kmem_free(*actual, sizeof (vnodeops_t));
21667c478bd9Sstevel@tonic-gate 	}
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate #if DEBUG
21697c478bd9Sstevel@tonic-gate 	if (unused_ops != 0)
21707c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied "
21717c478bd9Sstevel@tonic-gate 		    "but not used", name, unused_ops);
21727c478bd9Sstevel@tonic-gate #endif
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	return (error);
21757c478bd9Sstevel@tonic-gate }
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate /*
21787c478bd9Sstevel@tonic-gate  * Free the vnodeops created as a result of vn_make_ops()
21797c478bd9Sstevel@tonic-gate  */
21807c478bd9Sstevel@tonic-gate void
21817c478bd9Sstevel@tonic-gate vn_freevnodeops(vnodeops_t *vnops)
21827c478bd9Sstevel@tonic-gate {
21837c478bd9Sstevel@tonic-gate 	kmem_free(vnops, sizeof (vnodeops_t));
21847c478bd9Sstevel@tonic-gate }
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate /*
21877c478bd9Sstevel@tonic-gate  * Vnode cache.
21887c478bd9Sstevel@tonic-gate  */
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate /* ARGSUSED */
21917c478bd9Sstevel@tonic-gate static int
21927c478bd9Sstevel@tonic-gate vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
21937c478bd9Sstevel@tonic-gate {
21947c478bd9Sstevel@tonic-gate 	struct vnode *vp;
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	vp = buf;
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
21997c478bd9Sstevel@tonic-gate 	cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL);
22007c478bd9Sstevel@tonic-gate 	rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL);
22017c478bd9Sstevel@tonic-gate 	rw_init(&vp->v_mslock, NULL, RW_DEFAULT, NULL);
22027c478bd9Sstevel@tonic-gate 	vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
22037c478bd9Sstevel@tonic-gate 	vp->v_path = NULL;
22047c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
22051b300de9Sjwahlig 	vp->v_vsd = NULL;
2206df2381bfSpraks 	vp->v_fopdata = NULL;
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	return (0);
22097c478bd9Sstevel@tonic-gate }
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate /* ARGSUSED */
22127c478bd9Sstevel@tonic-gate static void
22137c478bd9Sstevel@tonic-gate vn_cache_destructor(void *buf, void *cdrarg)
22147c478bd9Sstevel@tonic-gate {
22157c478bd9Sstevel@tonic-gate 	struct vnode *vp;
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	vp = buf;
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 	rw_destroy(&vp->v_mslock);
22207c478bd9Sstevel@tonic-gate 	rw_destroy(&vp->v_nbllock);
22217c478bd9Sstevel@tonic-gate 	cv_destroy(&vp->v_cv);
22227c478bd9Sstevel@tonic-gate 	mutex_destroy(&vp->v_lock);
22237c478bd9Sstevel@tonic-gate }
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate void
22267c478bd9Sstevel@tonic-gate vn_create_cache(void)
22277c478bd9Sstevel@tonic-gate {
22287c478bd9Sstevel@tonic-gate 	vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode), 64,
22297c478bd9Sstevel@tonic-gate 	    vn_cache_constructor, vn_cache_destructor, NULL, NULL,
22307c478bd9Sstevel@tonic-gate 	    NULL, 0);
22317c478bd9Sstevel@tonic-gate }
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate void
22347c478bd9Sstevel@tonic-gate vn_destroy_cache(void)
22357c478bd9Sstevel@tonic-gate {
22367c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(vn_cache);
22377c478bd9Sstevel@tonic-gate }
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate /*
22407c478bd9Sstevel@tonic-gate  * Used by file systems when fs-specific nodes (e.g., ufs inodes) are
22417c478bd9Sstevel@tonic-gate  * cached by the file system and vnodes remain associated.
22427c478bd9Sstevel@tonic-gate  */
22437c478bd9Sstevel@tonic-gate void
22447c478bd9Sstevel@tonic-gate vn_recycle(vnode_t *vp)
22457c478bd9Sstevel@tonic-gate {
22467c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_pages == NULL);
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	/*
22497c478bd9Sstevel@tonic-gate 	 * XXX - This really belongs in vn_reinit(), but we have some issues
22507c478bd9Sstevel@tonic-gate 	 * with the counts.  Best to have it here for clean initialization.
22517c478bd9Sstevel@tonic-gate 	 */
22527c478bd9Sstevel@tonic-gate 	vp->v_rdcnt = 0;
22537c478bd9Sstevel@tonic-gate 	vp->v_wrcnt = 0;
22547c478bd9Sstevel@tonic-gate 	vp->v_mmap_read = 0;
22557c478bd9Sstevel@tonic-gate 	vp->v_mmap_write = 0;
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 	/*
22587c478bd9Sstevel@tonic-gate 	 * If FEM was in use, make sure everything gets cleaned up
22597c478bd9Sstevel@tonic-gate 	 * NOTE: vp->v_femhead is initialized to NULL in the vnode
22607c478bd9Sstevel@tonic-gate 	 * constructor.
22617c478bd9Sstevel@tonic-gate 	 */
22627c478bd9Sstevel@tonic-gate 	if (vp->v_femhead) {
22637c478bd9Sstevel@tonic-gate 		/* XXX - There should be a free_femhead() that does all this */
22647c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_femhead->femh_list == NULL);
22657c478bd9Sstevel@tonic-gate 		mutex_destroy(&vp->v_femhead->femh_lock);
22667c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
22677c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;
22687c478bd9Sstevel@tonic-gate 	}
22697c478bd9Sstevel@tonic-gate 	if (vp->v_path) {
22707c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
22717c478bd9Sstevel@tonic-gate 		vp->v_path = NULL;
22727c478bd9Sstevel@tonic-gate 	}
2273df2381bfSpraks 
2274df2381bfSpraks 	if (vp->v_fopdata != NULL) {
2275df2381bfSpraks 		free_fopdata(vp);
2276df2381bfSpraks 	}
22777c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
22781b300de9Sjwahlig 	vsd_free(vp);
22797c478bd9Sstevel@tonic-gate }
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate /*
22827c478bd9Sstevel@tonic-gate  * Used to reset the vnode fields including those that are directly accessible
22837c478bd9Sstevel@tonic-gate  * as well as those which require an accessor function.
22847c478bd9Sstevel@tonic-gate  *
22857c478bd9Sstevel@tonic-gate  * Does not initialize:
22867c478bd9Sstevel@tonic-gate  *	synchronization objects: v_lock, v_nbllock, v_cv
22877c478bd9Sstevel@tonic-gate  *	v_data (since FS-nodes and vnodes point to each other and should
22887c478bd9Sstevel@tonic-gate  *		be updated simultaneously)
22897c478bd9Sstevel@tonic-gate  *	v_op (in case someone needs to make a VOP call on this object)
22907c478bd9Sstevel@tonic-gate  */
22917c478bd9Sstevel@tonic-gate void
22927c478bd9Sstevel@tonic-gate vn_reinit(vnode_t *vp)
22937c478bd9Sstevel@tonic-gate {
22947c478bd9Sstevel@tonic-gate 	vp->v_count = 1;
22957c478bd9Sstevel@tonic-gate 	vp->v_vfsp = NULL;
22967c478bd9Sstevel@tonic-gate 	vp->v_stream = NULL;
22977c478bd9Sstevel@tonic-gate 	vp->v_vfsmountedhere = NULL;
22987c478bd9Sstevel@tonic-gate 	vp->v_flag = 0;
22997c478bd9Sstevel@tonic-gate 	vp->v_type = VNON;
23007c478bd9Sstevel@tonic-gate 	vp->v_rdev = NODEV;
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 	vp->v_filocks = NULL;
23037c478bd9Sstevel@tonic-gate 	vp->v_shrlocks = NULL;
23047c478bd9Sstevel@tonic-gate 	vp->v_pages = NULL;
23057c478bd9Sstevel@tonic-gate 	vp->v_npages = 0;
23067c478bd9Sstevel@tonic-gate 	vp->v_msnpages = 0;
23077c478bd9Sstevel@tonic-gate 	vp->v_scanfront = NULL;
23087c478bd9Sstevel@tonic-gate 	vp->v_scanback = NULL;
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 	vp->v_locality = NULL;
23117c478bd9Sstevel@tonic-gate 	vp->v_scantime = 0;
23127c478bd9Sstevel@tonic-gate 	vp->v_mset = 0;
23137c478bd9Sstevel@tonic-gate 	vp->v_msflags = 0;
23147c478bd9Sstevel@tonic-gate 	vp->v_msnext = NULL;
23157c478bd9Sstevel@tonic-gate 	vp->v_msprev = NULL;
2316*da6c28aaSamw 	vp->v_xattrdir = NULL;
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 	/* Handles v_femhead, v_path, and the r/w/map counts */
23197c478bd9Sstevel@tonic-gate 	vn_recycle(vp);
23207c478bd9Sstevel@tonic-gate }
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate vnode_t *
23237c478bd9Sstevel@tonic-gate vn_alloc(int kmflag)
23247c478bd9Sstevel@tonic-gate {
23257c478bd9Sstevel@tonic-gate 	vnode_t *vp;
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 	vp = kmem_cache_alloc(vn_cache, kmflag);
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 	if (vp != NULL) {
23307c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2331df2381bfSpraks 		vp->v_fopdata = NULL;
23327c478bd9Sstevel@tonic-gate 		vn_reinit(vp);
23337c478bd9Sstevel@tonic-gate 	}
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	return (vp);
23367c478bd9Sstevel@tonic-gate }
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate void
23397c478bd9Sstevel@tonic-gate vn_free(vnode_t *vp)
23407c478bd9Sstevel@tonic-gate {
2341*da6c28aaSamw 	ASSERT(vp->v_shrlocks == NULL);
2342*da6c28aaSamw 	ASSERT(vp->v_filocks == NULL);
2343*da6c28aaSamw 
23447c478bd9Sstevel@tonic-gate 	/*
23457c478bd9Sstevel@tonic-gate 	 * Some file systems call vn_free() with v_count of zero,
23467c478bd9Sstevel@tonic-gate 	 * some with v_count of 1.  In any case, the value should
23477c478bd9Sstevel@tonic-gate 	 * never be anything else.
23487c478bd9Sstevel@tonic-gate 	 */
23497c478bd9Sstevel@tonic-gate 	ASSERT((vp->v_count == 0) || (vp->v_count == 1));
23507c478bd9Sstevel@tonic-gate 	if (vp->v_path != NULL) {
23517c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
23527c478bd9Sstevel@tonic-gate 		vp->v_path = NULL;
23537c478bd9Sstevel@tonic-gate 	}
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	/* If FEM was in use, make sure everything gets cleaned up */
23567c478bd9Sstevel@tonic-gate 	if (vp->v_femhead) {
23577c478bd9Sstevel@tonic-gate 		/* XXX - There should be a free_femhead() that does all this */
23587c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_femhead->femh_list == NULL);
23597c478bd9Sstevel@tonic-gate 		mutex_destroy(&vp->v_femhead->femh_lock);
23607c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
23617c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;
23627c478bd9Sstevel@tonic-gate 	}
2363df2381bfSpraks 
2364df2381bfSpraks 	if (vp->v_fopdata != NULL) {
2365df2381bfSpraks 		free_fopdata(vp);
2366df2381bfSpraks 	}
23677c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
23681b300de9Sjwahlig 	vsd_free(vp);
23697c478bd9Sstevel@tonic-gate 	kmem_cache_free(vn_cache, vp);
23707c478bd9Sstevel@tonic-gate }
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate /*
23737c478bd9Sstevel@tonic-gate  * vnode status changes, should define better states than 1, 0.
23747c478bd9Sstevel@tonic-gate  */
23757c478bd9Sstevel@tonic-gate void
23767c478bd9Sstevel@tonic-gate vn_reclaim(vnode_t *vp)
23777c478bd9Sstevel@tonic-gate {
23787c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
23797c478bd9Sstevel@tonic-gate 
2380ddfcde86Srsb 	if (vfsp == NULL ||
2381ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
23827c478bd9Sstevel@tonic-gate 		return;
23837c478bd9Sstevel@tonic-gate 	}
23847c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED);
23857c478bd9Sstevel@tonic-gate }
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate void
23887c478bd9Sstevel@tonic-gate vn_idle(vnode_t *vp)
23897c478bd9Sstevel@tonic-gate {
23907c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
23917c478bd9Sstevel@tonic-gate 
2392ddfcde86Srsb 	if (vfsp == NULL ||
2393ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
23947c478bd9Sstevel@tonic-gate 		return;
23957c478bd9Sstevel@tonic-gate 	}
23967c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_IDLED);
23977c478bd9Sstevel@tonic-gate }
23987c478bd9Sstevel@tonic-gate void
23997c478bd9Sstevel@tonic-gate vn_exists(vnode_t *vp)
24007c478bd9Sstevel@tonic-gate {
24017c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
24027c478bd9Sstevel@tonic-gate 
2403ddfcde86Srsb 	if (vfsp == NULL ||
2404ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
24057c478bd9Sstevel@tonic-gate 		return;
24067c478bd9Sstevel@tonic-gate 	}
24077c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS);
24087c478bd9Sstevel@tonic-gate }
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate void
24117c478bd9Sstevel@tonic-gate vn_invalid(vnode_t *vp)
24127c478bd9Sstevel@tonic-gate {
24137c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
24147c478bd9Sstevel@tonic-gate 
2415ddfcde86Srsb 	if (vfsp == NULL ||
2416ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
24177c478bd9Sstevel@tonic-gate 		return;
24187c478bd9Sstevel@tonic-gate 	}
24197c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED);
24207c478bd9Sstevel@tonic-gate }
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate /* Vnode event notification */
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate int
2425*da6c28aaSamw vnevent_support(vnode_t *vp, caller_context_t *ct)
24267c478bd9Sstevel@tonic-gate {
24277c478bd9Sstevel@tonic-gate 	if (vp == NULL)
24287c478bd9Sstevel@tonic-gate 		return (EINVAL);
24297c478bd9Sstevel@tonic-gate 
2430*da6c28aaSamw 	return (VOP_VNEVENT(vp, VE_SUPPORT, NULL, NULL, ct));
24317c478bd9Sstevel@tonic-gate }
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate void
2434*da6c28aaSamw vnevent_rename_src(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
24357c478bd9Sstevel@tonic-gate {
24367c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
24377c478bd9Sstevel@tonic-gate 		return;
24387c478bd9Sstevel@tonic-gate 	}
2439*da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RENAME_SRC, dvp, name, ct);
24407c478bd9Sstevel@tonic-gate }
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate void
2443*da6c28aaSamw vnevent_rename_dest(vnode_t *vp, vnode_t *dvp, char *name,
2444*da6c28aaSamw     caller_context_t *ct)
24457c478bd9Sstevel@tonic-gate {
24467c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
24477c478bd9Sstevel@tonic-gate 		return;
24487c478bd9Sstevel@tonic-gate 	}
2449*da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST, dvp, name, ct);
24507c478bd9Sstevel@tonic-gate }
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate void
2453*da6c28aaSamw vnevent_rename_dest_dir(vnode_t *vp, caller_context_t *ct)
24547c478bd9Sstevel@tonic-gate {
24557c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
24567c478bd9Sstevel@tonic-gate 		return;
24577c478bd9Sstevel@tonic-gate 	}
2458*da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST_DIR, NULL, NULL, ct);
24597c478bd9Sstevel@tonic-gate }
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate void
2462*da6c28aaSamw vnevent_remove(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
24637c478bd9Sstevel@tonic-gate {
24647c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
24657c478bd9Sstevel@tonic-gate 		return;
24667c478bd9Sstevel@tonic-gate 	}
2467*da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_REMOVE, dvp, name, ct);
2468df2381bfSpraks }
2469df2381bfSpraks 
2470df2381bfSpraks void
2471*da6c28aaSamw vnevent_rmdir(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2472df2381bfSpraks {
2473df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2474df2381bfSpraks 		return;
2475df2381bfSpraks 	}
2476*da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RMDIR, dvp, name, ct);
2477df2381bfSpraks }
2478df2381bfSpraks 
2479df2381bfSpraks void
2480*da6c28aaSamw vnevent_create(vnode_t *vp, caller_context_t *ct)
2481df2381bfSpraks {
2482df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2483df2381bfSpraks 		return;
2484df2381bfSpraks 	}
2485*da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_CREATE, NULL, NULL, ct);
2486df2381bfSpraks }
2487df2381bfSpraks 
2488df2381bfSpraks void
2489*da6c28aaSamw vnevent_link(vnode_t *vp, caller_context_t *ct)
2490df2381bfSpraks {
2491df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2492df2381bfSpraks 		return;
2493df2381bfSpraks 	}
2494*da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_LINK, NULL, NULL, ct);
2495df2381bfSpraks }
2496df2381bfSpraks 
2497df2381bfSpraks void
2498*da6c28aaSamw vnevent_mountedover(vnode_t *vp, caller_context_t *ct)
2499df2381bfSpraks {
2500df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2501df2381bfSpraks 		return;
2502df2381bfSpraks 	}
2503*da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_MOUNTEDOVER, NULL, NULL, ct);
25047c478bd9Sstevel@tonic-gate }
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate /*
25077c478bd9Sstevel@tonic-gate  * Vnode accessors.
25087c478bd9Sstevel@tonic-gate  */
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate int
25117c478bd9Sstevel@tonic-gate vn_is_readonly(vnode_t *vp)
25127c478bd9Sstevel@tonic-gate {
25137c478bd9Sstevel@tonic-gate 	return (vp->v_vfsp->vfs_flag & VFS_RDONLY);
25147c478bd9Sstevel@tonic-gate }
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate int
25177c478bd9Sstevel@tonic-gate vn_has_flocks(vnode_t *vp)
25187c478bd9Sstevel@tonic-gate {
25197c478bd9Sstevel@tonic-gate 	return (vp->v_filocks != NULL);
25207c478bd9Sstevel@tonic-gate }
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate int
25237c478bd9Sstevel@tonic-gate vn_has_mandatory_locks(vnode_t *vp, int mode)
25247c478bd9Sstevel@tonic-gate {
25257c478bd9Sstevel@tonic-gate 	return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode)));
25267c478bd9Sstevel@tonic-gate }
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate int
25297c478bd9Sstevel@tonic-gate vn_has_cached_data(vnode_t *vp)
25307c478bd9Sstevel@tonic-gate {
25317c478bd9Sstevel@tonic-gate 	return (vp->v_pages != NULL);
25327c478bd9Sstevel@tonic-gate }
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate /*
25357c478bd9Sstevel@tonic-gate  * Return 0 if the vnode in question shouldn't be permitted into a zone via
25367c478bd9Sstevel@tonic-gate  * zone_enter(2).
25377c478bd9Sstevel@tonic-gate  */
25387c478bd9Sstevel@tonic-gate int
25397c478bd9Sstevel@tonic-gate vn_can_change_zones(vnode_t *vp)
25407c478bd9Sstevel@tonic-gate {
25417c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
25427c478bd9Sstevel@tonic-gate 	int allow = 1;
25437c478bd9Sstevel@tonic-gate 	vnode_t *rvp;
25447c478bd9Sstevel@tonic-gate 
2545108322fbScarlsonj 	if (nfs_global_client_only != 0)
2546108322fbScarlsonj 		return (1);
2547108322fbScarlsonj 
25487c478bd9Sstevel@tonic-gate 	/*
25497c478bd9Sstevel@tonic-gate 	 * We always want to look at the underlying vnode if there is one.
25507c478bd9Sstevel@tonic-gate 	 */
2551*da6c28aaSamw 	if (VOP_REALVP(vp, &rvp, NULL) != 0)
25527c478bd9Sstevel@tonic-gate 		rvp = vp;
25537c478bd9Sstevel@tonic-gate 	/*
25547c478bd9Sstevel@tonic-gate 	 * Some pseudo filesystems (including doorfs) don't actually register
25557c478bd9Sstevel@tonic-gate 	 * their vfsops_t, so the following may return NULL; we happily let
25567c478bd9Sstevel@tonic-gate 	 * such vnodes switch zones.
25577c478bd9Sstevel@tonic-gate 	 */
25587c478bd9Sstevel@tonic-gate 	vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp));
25597c478bd9Sstevel@tonic-gate 	if (vswp != NULL) {
25607c478bd9Sstevel@tonic-gate 		if (vswp->vsw_flag & VSW_NOTZONESAFE)
25617c478bd9Sstevel@tonic-gate 			allow = 0;
25627c478bd9Sstevel@tonic-gate 		vfs_unrefvfssw(vswp);
25637c478bd9Sstevel@tonic-gate 	}
25647c478bd9Sstevel@tonic-gate 	return (allow);
25657c478bd9Sstevel@tonic-gate }
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate /*
25687c478bd9Sstevel@tonic-gate  * Return nonzero if the vnode is a mount point, zero if not.
25697c478bd9Sstevel@tonic-gate  */
25707c478bd9Sstevel@tonic-gate int
25717c478bd9Sstevel@tonic-gate vn_ismntpt(vnode_t *vp)
25727c478bd9Sstevel@tonic-gate {
25737c478bd9Sstevel@tonic-gate 	return (vp->v_vfsmountedhere != NULL);
25747c478bd9Sstevel@tonic-gate }
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate /* Retrieve the vfs (if any) mounted on this vnode */
25777c478bd9Sstevel@tonic-gate vfs_t *
25787c478bd9Sstevel@tonic-gate vn_mountedvfs(vnode_t *vp)
25797c478bd9Sstevel@tonic-gate {
25807c478bd9Sstevel@tonic-gate 	return (vp->v_vfsmountedhere);
25817c478bd9Sstevel@tonic-gate }
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate /*
2584*da6c28aaSamw  * vn_has_other_opens() checks whether a particular file is opened by more than
2585*da6c28aaSamw  * just the caller and whether the open is for read and/or write.
2586*da6c28aaSamw  * This routine is for calling after the caller has already called VOP_OPEN()
2587*da6c28aaSamw  * and the caller wishes to know if they are the only one with it open for
2588*da6c28aaSamw  * the mode(s) specified.
2589*da6c28aaSamw  *
2590*da6c28aaSamw  * Vnode counts are only kept on regular files (v_type=VREG).
2591*da6c28aaSamw  */
2592*da6c28aaSamw int
2593*da6c28aaSamw vn_has_other_opens(
2594*da6c28aaSamw 	vnode_t *vp,
2595*da6c28aaSamw 	v_mode_t mode)
2596*da6c28aaSamw {
2597*da6c28aaSamw 
2598*da6c28aaSamw 	ASSERT(vp != NULL);
2599*da6c28aaSamw 
2600*da6c28aaSamw 	switch (mode) {
2601*da6c28aaSamw 	case V_WRITE:
2602*da6c28aaSamw 		if (vp->v_wrcnt > 1)
2603*da6c28aaSamw 			return (V_TRUE);
2604*da6c28aaSamw 		break;
2605*da6c28aaSamw 	case V_RDORWR:
2606*da6c28aaSamw 		if ((vp->v_rdcnt > 1) || (vp->v_wrcnt > 1))
2607*da6c28aaSamw 			return (V_TRUE);
2608*da6c28aaSamw 		break;
2609*da6c28aaSamw 	case V_RDANDWR:
2610*da6c28aaSamw 		if ((vp->v_rdcnt > 1) && (vp->v_wrcnt > 1))
2611*da6c28aaSamw 			return (V_TRUE);
2612*da6c28aaSamw 		break;
2613*da6c28aaSamw 	case V_READ:
2614*da6c28aaSamw 		if (vp->v_rdcnt > 1)
2615*da6c28aaSamw 			return (V_TRUE);
2616*da6c28aaSamw 		break;
2617*da6c28aaSamw 	}
2618*da6c28aaSamw 
2619*da6c28aaSamw 	return (V_FALSE);
2620*da6c28aaSamw }
2621*da6c28aaSamw 
2622*da6c28aaSamw /*
26237c478bd9Sstevel@tonic-gate  * vn_is_opened() checks whether a particular file is opened and
26247c478bd9Sstevel@tonic-gate  * whether the open is for read and/or write.
26257c478bd9Sstevel@tonic-gate  *
26267c478bd9Sstevel@tonic-gate  * Vnode counts are only kept on regular files (v_type=VREG).
26277c478bd9Sstevel@tonic-gate  */
26287c478bd9Sstevel@tonic-gate int
26297c478bd9Sstevel@tonic-gate vn_is_opened(
26307c478bd9Sstevel@tonic-gate 	vnode_t *vp,
26317c478bd9Sstevel@tonic-gate 	v_mode_t mode)
26327c478bd9Sstevel@tonic-gate {
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	switch (mode) {
26377c478bd9Sstevel@tonic-gate 	case V_WRITE:
26387c478bd9Sstevel@tonic-gate 		if (vp->v_wrcnt)
26397c478bd9Sstevel@tonic-gate 			return (V_TRUE);
26407c478bd9Sstevel@tonic-gate 		break;
26417c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
26427c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt && vp->v_wrcnt)
26437c478bd9Sstevel@tonic-gate 			return (V_TRUE);
26447c478bd9Sstevel@tonic-gate 		break;
26457c478bd9Sstevel@tonic-gate 	case V_RDORWR:
26467c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt || vp->v_wrcnt)
26477c478bd9Sstevel@tonic-gate 			return (V_TRUE);
26487c478bd9Sstevel@tonic-gate 		break;
26497c478bd9Sstevel@tonic-gate 	case V_READ:
26507c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt)
26517c478bd9Sstevel@tonic-gate 			return (V_TRUE);
26527c478bd9Sstevel@tonic-gate 		break;
26537c478bd9Sstevel@tonic-gate 	}
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 	return (V_FALSE);
26567c478bd9Sstevel@tonic-gate }
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate /*
26597c478bd9Sstevel@tonic-gate  * vn_is_mapped() checks whether a particular file is mapped and whether
26607c478bd9Sstevel@tonic-gate  * the file is mapped read and/or write.
26617c478bd9Sstevel@tonic-gate  */
26627c478bd9Sstevel@tonic-gate int
26637c478bd9Sstevel@tonic-gate vn_is_mapped(
26647c478bd9Sstevel@tonic-gate 	vnode_t *vp,
26657c478bd9Sstevel@tonic-gate 	v_mode_t mode)
26667c478bd9Sstevel@tonic-gate {
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate #if !defined(_LP64)
26717c478bd9Sstevel@tonic-gate 	switch (mode) {
26727c478bd9Sstevel@tonic-gate 	/*
26737c478bd9Sstevel@tonic-gate 	 * The atomic_add_64_nv functions force atomicity in the
26747c478bd9Sstevel@tonic-gate 	 * case of 32 bit architectures. Otherwise the 64 bit values
26757c478bd9Sstevel@tonic-gate 	 * require two fetches. The value of the fields may be
26767c478bd9Sstevel@tonic-gate 	 * (potentially) changed between the first fetch and the
26777c478bd9Sstevel@tonic-gate 	 * second
26787c478bd9Sstevel@tonic-gate 	 */
26797c478bd9Sstevel@tonic-gate 	case V_WRITE:
26807c478bd9Sstevel@tonic-gate 		if (atomic_add_64_nv((&(vp->v_mmap_write)), 0))
26817c478bd9Sstevel@tonic-gate 			return (V_TRUE);
26827c478bd9Sstevel@tonic-gate 		break;
26837c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
26847c478bd9Sstevel@tonic-gate 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) &&
26857c478bd9Sstevel@tonic-gate 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
26867c478bd9Sstevel@tonic-gate 			return (V_TRUE);
26877c478bd9Sstevel@tonic-gate 		break;
26887c478bd9Sstevel@tonic-gate 	case V_RDORWR:
26897c478bd9Sstevel@tonic-gate 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) ||
26907c478bd9Sstevel@tonic-gate 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
26917c478bd9Sstevel@tonic-gate 			return (V_TRUE);
26927c478bd9Sstevel@tonic-gate 		break;
26937c478bd9Sstevel@tonic-gate 	case V_READ:
26947c478bd9Sstevel@tonic-gate 		if (atomic_add_64_nv((&(vp->v_mmap_read)), 0))
26957c478bd9Sstevel@tonic-gate 			return (V_TRUE);
26967c478bd9Sstevel@tonic-gate 		break;
26977c478bd9Sstevel@tonic-gate 	}
26987c478bd9Sstevel@tonic-gate #else
26997c478bd9Sstevel@tonic-gate 	switch (mode) {
27007c478bd9Sstevel@tonic-gate 	case V_WRITE:
27017c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_write)
27027c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27037c478bd9Sstevel@tonic-gate 		break;
27047c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
27057c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read && vp->v_mmap_write)
27067c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27077c478bd9Sstevel@tonic-gate 		break;
27087c478bd9Sstevel@tonic-gate 	case V_RDORWR:
27097c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read || vp->v_mmap_write)
27107c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27117c478bd9Sstevel@tonic-gate 		break;
27127c478bd9Sstevel@tonic-gate 	case V_READ:
27137c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read)
27147c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27157c478bd9Sstevel@tonic-gate 		break;
27167c478bd9Sstevel@tonic-gate 	}
27177c478bd9Sstevel@tonic-gate #endif
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	return (V_FALSE);
27207c478bd9Sstevel@tonic-gate }
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate /*
27237c478bd9Sstevel@tonic-gate  * Set the operations vector for a vnode.
27247c478bd9Sstevel@tonic-gate  *
27257c478bd9Sstevel@tonic-gate  * FEM ensures that the v_femhead pointer is filled in before the
27267c478bd9Sstevel@tonic-gate  * v_op pointer is changed.  This means that if the v_femhead pointer
27277c478bd9Sstevel@tonic-gate  * is NULL, and the v_op field hasn't changed since before which checked
27287c478bd9Sstevel@tonic-gate  * the v_femhead pointer; then our update is ok - we are not racing with
27297c478bd9Sstevel@tonic-gate  * FEM.
27307c478bd9Sstevel@tonic-gate  */
27317c478bd9Sstevel@tonic-gate void
27327c478bd9Sstevel@tonic-gate vn_setops(vnode_t *vp, vnodeops_t *vnodeops)
27337c478bd9Sstevel@tonic-gate {
27347c478bd9Sstevel@tonic-gate 	vnodeops_t	*op;
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
27377c478bd9Sstevel@tonic-gate 	ASSERT(vnodeops != NULL);
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate 	op = vp->v_op;
27407c478bd9Sstevel@tonic-gate 	membar_consumer();
27417c478bd9Sstevel@tonic-gate 	/*
27427c478bd9Sstevel@tonic-gate 	 * If vp->v_femhead == NULL, then we'll call casptr() to do the
27437c478bd9Sstevel@tonic-gate 	 * compare-and-swap on vp->v_op.  If either fails, then FEM is
27447c478bd9Sstevel@tonic-gate 	 * in effect on the vnode and we need to have FEM deal with it.
27457c478bd9Sstevel@tonic-gate 	 */
27467c478bd9Sstevel@tonic-gate 	if (vp->v_femhead != NULL || casptr(&vp->v_op, op, vnodeops) != op) {
27477c478bd9Sstevel@tonic-gate 		fem_setvnops(vp, vnodeops);
27487c478bd9Sstevel@tonic-gate 	}
27497c478bd9Sstevel@tonic-gate }
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate /*
27527c478bd9Sstevel@tonic-gate  * Retrieve the operations vector for a vnode
27537c478bd9Sstevel@tonic-gate  * As with vn_setops(above); make sure we aren't racing with FEM.
27547c478bd9Sstevel@tonic-gate  * FEM sets the v_op to a special, internal, vnodeops that wouldn't
27557c478bd9Sstevel@tonic-gate  * make sense to the callers of this routine.
27567c478bd9Sstevel@tonic-gate  */
27577c478bd9Sstevel@tonic-gate vnodeops_t *
27587c478bd9Sstevel@tonic-gate vn_getops(vnode_t *vp)
27597c478bd9Sstevel@tonic-gate {
27607c478bd9Sstevel@tonic-gate 	vnodeops_t	*op;
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 	op = vp->v_op;
27657c478bd9Sstevel@tonic-gate 	membar_consumer();
27667c478bd9Sstevel@tonic-gate 	if (vp->v_femhead == NULL && op == vp->v_op) {
27677c478bd9Sstevel@tonic-gate 		return (op);
27687c478bd9Sstevel@tonic-gate 	} else {
27697c478bd9Sstevel@tonic-gate 		return (fem_getvnops(vp));
27707c478bd9Sstevel@tonic-gate 	}
27717c478bd9Sstevel@tonic-gate }
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate /*
27747c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the vnodeops matches that of the vnode.
27757c478bd9Sstevel@tonic-gate  * Returns zero (0) if not.
27767c478bd9Sstevel@tonic-gate  */
27777c478bd9Sstevel@tonic-gate int
27787c478bd9Sstevel@tonic-gate vn_matchops(vnode_t *vp, vnodeops_t *vnodeops)
27797c478bd9Sstevel@tonic-gate {
27807c478bd9Sstevel@tonic-gate 	return (vn_getops(vp) == vnodeops);
27817c478bd9Sstevel@tonic-gate }
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate /*
27847c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the specified operation matches the
27857c478bd9Sstevel@tonic-gate  * corresponding operation for that the vnode.
27867c478bd9Sstevel@tonic-gate  * Returns zero (0) if not.
27877c478bd9Sstevel@tonic-gate  */
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate #define	MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0))
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate int
27927c478bd9Sstevel@tonic-gate vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp)
27937c478bd9Sstevel@tonic-gate {
27947c478bd9Sstevel@tonic-gate 	const fs_operation_trans_def_t *otdp;
27957c478bd9Sstevel@tonic-gate 	fs_generic_func_p *loc = NULL;
27967c478bd9Sstevel@tonic-gate 	vnodeops_t	*vop = vn_getops(vp);
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	ASSERT(vopname != NULL);
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate 	for (otdp = vn_ops_table; otdp->name != NULL; otdp++) {
28017c478bd9Sstevel@tonic-gate 		if (MATCHNAME(otdp->name, vopname)) {
2802b9c98e29Spf199842 			loc = (fs_generic_func_p *)
2803b9c98e29Spf199842 			    ((char *)(vop) + otdp->offset);
28047c478bd9Sstevel@tonic-gate 			break;
28057c478bd9Sstevel@tonic-gate 		}
28067c478bd9Sstevel@tonic-gate 	}
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	return ((loc != NULL) && (*loc == funcp));
28097c478bd9Sstevel@tonic-gate }
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate /*
28127c478bd9Sstevel@tonic-gate  * fs_new_caller_id() needs to return a unique ID on a given local system.
28137c478bd9Sstevel@tonic-gate  * The IDs do not need to survive across reboots.  These are primarily
28147c478bd9Sstevel@tonic-gate  * used so that (FEM) monitors can detect particular callers (such as
28157c478bd9Sstevel@tonic-gate  * the NFS server) to a given vnode/vfs operation.
28167c478bd9Sstevel@tonic-gate  */
28177c478bd9Sstevel@tonic-gate u_longlong_t
28187c478bd9Sstevel@tonic-gate fs_new_caller_id()
28197c478bd9Sstevel@tonic-gate {
28207c478bd9Sstevel@tonic-gate 	static uint64_t next_caller_id = 0LL; /* First call returns 1 */
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	return ((u_longlong_t)atomic_add_64_nv(&next_caller_id, 1));
28237c478bd9Sstevel@tonic-gate }
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate /*
28267c478bd9Sstevel@tonic-gate  * Given a starting vnode and a path, updates the path in the target vnode in
28277c478bd9Sstevel@tonic-gate  * a safe manner.  If the vnode already has path information embedded, then the
2828ca2c3138Seschrock  * cached path is left untouched.
28297c478bd9Sstevel@tonic-gate  */
283048011479Ssn199410 
283148011479Ssn199410 size_t max_vnode_path = 4 * MAXPATHLEN;
283248011479Ssn199410 
28337c478bd9Sstevel@tonic-gate void
28347c478bd9Sstevel@tonic-gate vn_setpath(vnode_t *rootvp, struct vnode *startvp, struct vnode *vp,
28357c478bd9Sstevel@tonic-gate     const char *path, size_t plen)
28367c478bd9Sstevel@tonic-gate {
28377c478bd9Sstevel@tonic-gate 	char	*rpath;
28387c478bd9Sstevel@tonic-gate 	vnode_t	*base;
28397c478bd9Sstevel@tonic-gate 	size_t	rpathlen, rpathalloc;
28407c478bd9Sstevel@tonic-gate 	int	doslash = 1;
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	if (*path == '/') {
28437c478bd9Sstevel@tonic-gate 		base = rootvp;
28447c478bd9Sstevel@tonic-gate 		path++;
28457c478bd9Sstevel@tonic-gate 		plen--;
28467c478bd9Sstevel@tonic-gate 	} else {
28477c478bd9Sstevel@tonic-gate 		base = startvp;
28487c478bd9Sstevel@tonic-gate 	}
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 	/*
28517c478bd9Sstevel@tonic-gate 	 * We cannot grab base->v_lock while we hold vp->v_lock because of
28527c478bd9Sstevel@tonic-gate 	 * the potential for deadlock.
28537c478bd9Sstevel@tonic-gate 	 */
28547c478bd9Sstevel@tonic-gate 	mutex_enter(&base->v_lock);
28557c478bd9Sstevel@tonic-gate 	if (base->v_path == NULL) {
28567c478bd9Sstevel@tonic-gate 		mutex_exit(&base->v_lock);
28577c478bd9Sstevel@tonic-gate 		return;
28587c478bd9Sstevel@tonic-gate 	}
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 	rpathlen = strlen(base->v_path);
28617c478bd9Sstevel@tonic-gate 	rpathalloc = rpathlen + plen + 1;
28627c478bd9Sstevel@tonic-gate 	/* Avoid adding a slash if there's already one there */
28637c478bd9Sstevel@tonic-gate 	if (base->v_path[rpathlen-1] == '/')
28647c478bd9Sstevel@tonic-gate 		doslash = 0;
28657c478bd9Sstevel@tonic-gate 	else
28667c478bd9Sstevel@tonic-gate 		rpathalloc++;
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate 	/*
28697c478bd9Sstevel@tonic-gate 	 * We don't want to call kmem_alloc(KM_SLEEP) with kernel locks held,
28707c478bd9Sstevel@tonic-gate 	 * so we must do this dance.  If, by chance, something changes the path,
28717c478bd9Sstevel@tonic-gate 	 * just give up since there is no real harm.
28727c478bd9Sstevel@tonic-gate 	 */
28737c478bd9Sstevel@tonic-gate 	mutex_exit(&base->v_lock);
28747c478bd9Sstevel@tonic-gate 
287548011479Ssn199410 	/* Paths should stay within reason */
287648011479Ssn199410 	if (rpathalloc > max_vnode_path)
287748011479Ssn199410 		return;
287848011479Ssn199410 
28797c478bd9Sstevel@tonic-gate 	rpath = kmem_alloc(rpathalloc, KM_SLEEP);
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate 	mutex_enter(&base->v_lock);
28827c478bd9Sstevel@tonic-gate 	if (base->v_path == NULL || strlen(base->v_path) != rpathlen) {
28837c478bd9Sstevel@tonic-gate 		mutex_exit(&base->v_lock);
28847c478bd9Sstevel@tonic-gate 		kmem_free(rpath, rpathalloc);
28857c478bd9Sstevel@tonic-gate 		return;
28867c478bd9Sstevel@tonic-gate 	}
28877c478bd9Sstevel@tonic-gate 	bcopy(base->v_path, rpath, rpathlen);
28887c478bd9Sstevel@tonic-gate 	mutex_exit(&base->v_lock);
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	if (doslash)
28917c478bd9Sstevel@tonic-gate 		rpath[rpathlen++] = '/';
28927c478bd9Sstevel@tonic-gate 	bcopy(path, rpath + rpathlen, plen);
28937c478bd9Sstevel@tonic-gate 	rpath[rpathlen + plen] = '\0';
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
28967c478bd9Sstevel@tonic-gate 	if (vp->v_path != NULL) {
28977c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
28987c478bd9Sstevel@tonic-gate 		kmem_free(rpath, rpathalloc);
28997c478bd9Sstevel@tonic-gate 	} else {
29007c478bd9Sstevel@tonic-gate 		vp->v_path = rpath;
29017c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
29027c478bd9Sstevel@tonic-gate 	}
29037c478bd9Sstevel@tonic-gate }
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate /*
29067c478bd9Sstevel@tonic-gate  * Sets the path to the vnode to be the given string, regardless of current
29077c478bd9Sstevel@tonic-gate  * context.  The string must be a complete path from rootdir.  This is only used
29087c478bd9Sstevel@tonic-gate  * by fsop_root() for setting the path based on the mountpoint.
29097c478bd9Sstevel@tonic-gate  */
29107c478bd9Sstevel@tonic-gate void
29117c478bd9Sstevel@tonic-gate vn_setpath_str(struct vnode *vp, const char *str, size_t len)
29127c478bd9Sstevel@tonic-gate {
29137c478bd9Sstevel@tonic-gate 	char *buf = kmem_alloc(len + 1, KM_SLEEP);
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
29167c478bd9Sstevel@tonic-gate 	if (vp->v_path != NULL) {
29177c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
29187c478bd9Sstevel@tonic-gate 		kmem_free(buf, len + 1);
29197c478bd9Sstevel@tonic-gate 		return;
29207c478bd9Sstevel@tonic-gate 	}
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 	vp->v_path = buf;
29237c478bd9Sstevel@tonic-gate 	bcopy(str, vp->v_path, len);
29247c478bd9Sstevel@tonic-gate 	vp->v_path[len] = '\0';
29257c478bd9Sstevel@tonic-gate 
29267c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
29277c478bd9Sstevel@tonic-gate }
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate /*
29307c478bd9Sstevel@tonic-gate  * Similar to vn_setpath_str(), this function sets the path of the destination
29317c478bd9Sstevel@tonic-gate  * vnode to the be the same as the source vnode.
29327c478bd9Sstevel@tonic-gate  */
29337c478bd9Sstevel@tonic-gate void
29347c478bd9Sstevel@tonic-gate vn_copypath(struct vnode *src, struct vnode *dst)
29357c478bd9Sstevel@tonic-gate {
29367c478bd9Sstevel@tonic-gate 	char *buf;
29377c478bd9Sstevel@tonic-gate 	int alloc;
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate 	mutex_enter(&src->v_lock);
29407c478bd9Sstevel@tonic-gate 	if (src->v_path == NULL) {
29417c478bd9Sstevel@tonic-gate 		mutex_exit(&src->v_lock);
29427c478bd9Sstevel@tonic-gate 		return;
29437c478bd9Sstevel@tonic-gate 	}
29447c478bd9Sstevel@tonic-gate 	alloc = strlen(src->v_path) + 1;
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	/* avoid kmem_alloc() with lock held */
29477c478bd9Sstevel@tonic-gate 	mutex_exit(&src->v_lock);
29487c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(alloc, KM_SLEEP);
29497c478bd9Sstevel@tonic-gate 	mutex_enter(&src->v_lock);
29507c478bd9Sstevel@tonic-gate 	if (src->v_path == NULL || strlen(src->v_path) + 1 != alloc) {
29517c478bd9Sstevel@tonic-gate 		mutex_exit(&src->v_lock);
29527c478bd9Sstevel@tonic-gate 		kmem_free(buf, alloc);
29537c478bd9Sstevel@tonic-gate 		return;
29547c478bd9Sstevel@tonic-gate 	}
29557c478bd9Sstevel@tonic-gate 	bcopy(src->v_path, buf, alloc);
29567c478bd9Sstevel@tonic-gate 	mutex_exit(&src->v_lock);
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate 	mutex_enter(&dst->v_lock);
29597c478bd9Sstevel@tonic-gate 	if (dst->v_path != NULL) {
29607c478bd9Sstevel@tonic-gate 		mutex_exit(&dst->v_lock);
29617c478bd9Sstevel@tonic-gate 		kmem_free(buf, alloc);
29627c478bd9Sstevel@tonic-gate 		return;
29637c478bd9Sstevel@tonic-gate 	}
29647c478bd9Sstevel@tonic-gate 	dst->v_path = buf;
29657c478bd9Sstevel@tonic-gate 	mutex_exit(&dst->v_lock);
29667c478bd9Sstevel@tonic-gate }
29677c478bd9Sstevel@tonic-gate 
29687c478bd9Sstevel@tonic-gate /*
29697c478bd9Sstevel@tonic-gate  * XXX Private interface for segvn routines that handle vnode
29707c478bd9Sstevel@tonic-gate  * large page segments.
29717c478bd9Sstevel@tonic-gate  *
29727c478bd9Sstevel@tonic-gate  * return 1 if vp's file system VOP_PAGEIO() implementation
29737c478bd9Sstevel@tonic-gate  * can be safely used instead of VOP_GETPAGE() for handling
29747c478bd9Sstevel@tonic-gate  * pagefaults against regular non swap files. VOP_PAGEIO()
29757c478bd9Sstevel@tonic-gate  * interface is considered safe here if its implementation
29767c478bd9Sstevel@tonic-gate  * is very close to VOP_GETPAGE() implementation.
29777c478bd9Sstevel@tonic-gate  * e.g. It zero's out the part of the page beyond EOF. Doesn't
29787c478bd9Sstevel@tonic-gate  * panic if there're file holes but instead returns an error.
29797c478bd9Sstevel@tonic-gate  * Doesn't assume file won't be changed by user writes, etc.
29807c478bd9Sstevel@tonic-gate  *
29817c478bd9Sstevel@tonic-gate  * return 0 otherwise.
29827c478bd9Sstevel@tonic-gate  *
29837c478bd9Sstevel@tonic-gate  * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs.
29847c478bd9Sstevel@tonic-gate  */
29857c478bd9Sstevel@tonic-gate int
29867c478bd9Sstevel@tonic-gate vn_vmpss_usepageio(vnode_t *vp)
29877c478bd9Sstevel@tonic-gate {
29887c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
29897c478bd9Sstevel@tonic-gate 	char *fsname = vfssw[vfsp->vfs_fstype].vsw_name;
29907c478bd9Sstevel@tonic-gate 	char *pageio_ok_fss[] = {"ufs", "nfs", NULL};
29917c478bd9Sstevel@tonic-gate 	char **fsok = pageio_ok_fss;
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 	if (fsname == NULL) {
29947c478bd9Sstevel@tonic-gate 		return (0);
29957c478bd9Sstevel@tonic-gate 	}
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate 	for (; *fsok; fsok++) {
29987c478bd9Sstevel@tonic-gate 		if (strcmp(*fsok, fsname) == 0) {
29997c478bd9Sstevel@tonic-gate 			return (1);
30007c478bd9Sstevel@tonic-gate 		}
30017c478bd9Sstevel@tonic-gate 	}
30027c478bd9Sstevel@tonic-gate 	return (0);
30037c478bd9Sstevel@tonic-gate }
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate /* VOP_XXX() macros call the corresponding fop_xxx() function */
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate int
30087c478bd9Sstevel@tonic-gate fop_open(
30097c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
30107c478bd9Sstevel@tonic-gate 	int mode,
3011*da6c28aaSamw 	cred_t *cr,
3012*da6c28aaSamw 	caller_context_t *ct)
30137c478bd9Sstevel@tonic-gate {
30147c478bd9Sstevel@tonic-gate 	int ret;
30157c478bd9Sstevel@tonic-gate 	vnode_t *vp = *vpp;
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
30187c478bd9Sstevel@tonic-gate 	/*
30197c478bd9Sstevel@tonic-gate 	 * Adding to the vnode counts before calling open
30207c478bd9Sstevel@tonic-gate 	 * avoids the need for a mutex. It circumvents a race
30217c478bd9Sstevel@tonic-gate 	 * condition where a query made on the vnode counts results in a
30227c478bd9Sstevel@tonic-gate 	 * false negative. The inquirer goes away believing the file is
30237c478bd9Sstevel@tonic-gate 	 * not open when there is an open on the file already under way.
30247c478bd9Sstevel@tonic-gate 	 *
30257c478bd9Sstevel@tonic-gate 	 * The counts are meant to prevent NFS from granting a delegation
30267c478bd9Sstevel@tonic-gate 	 * when it would be dangerous to do so.
30277c478bd9Sstevel@tonic-gate 	 *
30287c478bd9Sstevel@tonic-gate 	 * The vnode counts are only kept on regular files
30297c478bd9Sstevel@tonic-gate 	 */
30307c478bd9Sstevel@tonic-gate 	if ((*vpp)->v_type == VREG) {
30317c478bd9Sstevel@tonic-gate 		if (mode & FREAD)
30327c478bd9Sstevel@tonic-gate 			atomic_add_32(&((*vpp)->v_rdcnt), 1);
30337c478bd9Sstevel@tonic-gate 		if (mode & FWRITE)
30347c478bd9Sstevel@tonic-gate 			atomic_add_32(&((*vpp)->v_wrcnt), 1);
30357c478bd9Sstevel@tonic-gate 	}
30367c478bd9Sstevel@tonic-gate 
3037f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3038f48205beScasper 
3039*da6c28aaSamw 	ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr, ct);
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	if (ret) {
30427c478bd9Sstevel@tonic-gate 		/*
30437c478bd9Sstevel@tonic-gate 		 * Use the saved vp just in case the vnode ptr got trashed
30447c478bd9Sstevel@tonic-gate 		 * by the error.
30457c478bd9Sstevel@tonic-gate 		 */
30462bb1cb30Sbmc 		VOPSTATS_UPDATE(vp, open);
30477c478bd9Sstevel@tonic-gate 		if ((vp->v_type == VREG) && (mode & FREAD))
30487c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_rdcnt), -1);
30497c478bd9Sstevel@tonic-gate 		if ((vp->v_type == VREG) && (mode & FWRITE))
30507c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_wrcnt), -1);
30517c478bd9Sstevel@tonic-gate 	} else {
30527c478bd9Sstevel@tonic-gate 		/*
30537c478bd9Sstevel@tonic-gate 		 * Some filesystems will return a different vnode,
30547c478bd9Sstevel@tonic-gate 		 * but the same path was still used to open it.
30557c478bd9Sstevel@tonic-gate 		 * So if we do change the vnode and need to
30567c478bd9Sstevel@tonic-gate 		 * copy over the path, do so here, rather than special
30577c478bd9Sstevel@tonic-gate 		 * casing each filesystem. Adjust the vnode counts to
30587c478bd9Sstevel@tonic-gate 		 * reflect the vnode switch.
30597c478bd9Sstevel@tonic-gate 		 */
30602bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, open);
30617c478bd9Sstevel@tonic-gate 		if (*vpp != vp && *vpp != NULL) {
30627c478bd9Sstevel@tonic-gate 			vn_copypath(vp, *vpp);
30637c478bd9Sstevel@tonic-gate 			if (((*vpp)->v_type == VREG) && (mode & FREAD))
30647c478bd9Sstevel@tonic-gate 				atomic_add_32(&((*vpp)->v_rdcnt), 1);
30657c478bd9Sstevel@tonic-gate 			if ((vp->v_type == VREG) && (mode & FREAD))
30667c478bd9Sstevel@tonic-gate 				atomic_add_32(&(vp->v_rdcnt), -1);
30677c478bd9Sstevel@tonic-gate 			if (((*vpp)->v_type == VREG) && (mode & FWRITE))
30687c478bd9Sstevel@tonic-gate 				atomic_add_32(&((*vpp)->v_wrcnt), 1);
30697c478bd9Sstevel@tonic-gate 			if ((vp->v_type == VREG) && (mode & FWRITE))
30707c478bd9Sstevel@tonic-gate 				atomic_add_32(&(vp->v_wrcnt), -1);
30717c478bd9Sstevel@tonic-gate 		}
30727c478bd9Sstevel@tonic-gate 	}
30737c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
30747c478bd9Sstevel@tonic-gate 	return (ret);
30757c478bd9Sstevel@tonic-gate }
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate int
30787c478bd9Sstevel@tonic-gate fop_close(
30797c478bd9Sstevel@tonic-gate 	vnode_t *vp,
30807c478bd9Sstevel@tonic-gate 	int flag,
30817c478bd9Sstevel@tonic-gate 	int count,
30827c478bd9Sstevel@tonic-gate 	offset_t offset,
3083*da6c28aaSamw 	cred_t *cr,
3084*da6c28aaSamw 	caller_context_t *ct)
30857c478bd9Sstevel@tonic-gate {
30865a59a8b3Srsb 	int err;
30875a59a8b3Srsb 
3088f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3089f48205beScasper 
3090*da6c28aaSamw 	err = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr, ct);
30912bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, close);
30927c478bd9Sstevel@tonic-gate 	/*
30937c478bd9Sstevel@tonic-gate 	 * Check passed in count to handle possible dups. Vnode counts are only
30947c478bd9Sstevel@tonic-gate 	 * kept on regular files
30957c478bd9Sstevel@tonic-gate 	 */
30967c478bd9Sstevel@tonic-gate 	if ((vp->v_type == VREG) && (count == 1))  {
30977c478bd9Sstevel@tonic-gate 		if (flag & FREAD) {
30987c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_rdcnt > 0);
30997c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_rdcnt), -1);
31007c478bd9Sstevel@tonic-gate 		}
31017c478bd9Sstevel@tonic-gate 		if (flag & FWRITE) {
31027c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_wrcnt > 0);
31037c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_wrcnt), -1);
31047c478bd9Sstevel@tonic-gate 		}
31057c478bd9Sstevel@tonic-gate 	}
31065a59a8b3Srsb 	return (err);
31077c478bd9Sstevel@tonic-gate }
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate int
31107c478bd9Sstevel@tonic-gate fop_read(
31117c478bd9Sstevel@tonic-gate 	vnode_t *vp,
31127c478bd9Sstevel@tonic-gate 	uio_t *uiop,
31137c478bd9Sstevel@tonic-gate 	int ioflag,
31147c478bd9Sstevel@tonic-gate 	cred_t *cr,
3115*da6c28aaSamw 	caller_context_t *ct)
31167c478bd9Sstevel@tonic-gate {
31175a59a8b3Srsb 	int	err;
31185a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
31195a59a8b3Srsb 
3120f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3121f48205beScasper 
31225a59a8b3Srsb 	err = (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct);
31232bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, read,
31245a59a8b3Srsb 	    read_bytes, (resid_start - uiop->uio_resid));
31255a59a8b3Srsb 	return (err);
31267c478bd9Sstevel@tonic-gate }
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate int
31297c478bd9Sstevel@tonic-gate fop_write(
31307c478bd9Sstevel@tonic-gate 	vnode_t *vp,
31317c478bd9Sstevel@tonic-gate 	uio_t *uiop,
31327c478bd9Sstevel@tonic-gate 	int ioflag,
31337c478bd9Sstevel@tonic-gate 	cred_t *cr,
3134*da6c28aaSamw 	caller_context_t *ct)
31357c478bd9Sstevel@tonic-gate {
31365a59a8b3Srsb 	int	err;
31375a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
31385a59a8b3Srsb 
3139f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3140f48205beScasper 
31415a59a8b3Srsb 	err = (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct);
31422bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, write,
31435a59a8b3Srsb 	    write_bytes, (resid_start - uiop->uio_resid));
31445a59a8b3Srsb 	return (err);
31457c478bd9Sstevel@tonic-gate }
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate int
31487c478bd9Sstevel@tonic-gate fop_ioctl(
31497c478bd9Sstevel@tonic-gate 	vnode_t *vp,
31507c478bd9Sstevel@tonic-gate 	int cmd,
31517c478bd9Sstevel@tonic-gate 	intptr_t arg,
31527c478bd9Sstevel@tonic-gate 	int flag,
31537c478bd9Sstevel@tonic-gate 	cred_t *cr,
3154*da6c28aaSamw 	int *rvalp,
3155*da6c28aaSamw 	caller_context_t *ct)
31567c478bd9Sstevel@tonic-gate {
31575a59a8b3Srsb 	int	err;
31585a59a8b3Srsb 
3159f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3160f48205beScasper 
3161*da6c28aaSamw 	err = (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp, ct);
31622bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, ioctl);
31635a59a8b3Srsb 	return (err);
31647c478bd9Sstevel@tonic-gate }
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate int
31677c478bd9Sstevel@tonic-gate fop_setfl(
31687c478bd9Sstevel@tonic-gate 	vnode_t *vp,
31697c478bd9Sstevel@tonic-gate 	int oflags,
31707c478bd9Sstevel@tonic-gate 	int nflags,
3171*da6c28aaSamw 	cred_t *cr,
3172*da6c28aaSamw 	caller_context_t *ct)
31737c478bd9Sstevel@tonic-gate {
31745a59a8b3Srsb 	int	err;
31755a59a8b3Srsb 
3176f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3177f48205beScasper 
3178*da6c28aaSamw 	err = (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr, ct);
31792bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setfl);
31805a59a8b3Srsb 	return (err);
31817c478bd9Sstevel@tonic-gate }
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate int
31847c478bd9Sstevel@tonic-gate fop_getattr(
31857c478bd9Sstevel@tonic-gate 	vnode_t *vp,
31867c478bd9Sstevel@tonic-gate 	vattr_t *vap,
31877c478bd9Sstevel@tonic-gate 	int flags,
3188*da6c28aaSamw 	cred_t *cr,
3189*da6c28aaSamw 	caller_context_t *ct)
31907c478bd9Sstevel@tonic-gate {
31915a59a8b3Srsb 	int	err;
31925a59a8b3Srsb 
3193f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3194f48205beScasper 
3195*da6c28aaSamw 	/*
3196*da6c28aaSamw 	 * If this file system doesn't understand the xvattr extensions
3197*da6c28aaSamw 	 * then turn off the xvattr bit.
3198*da6c28aaSamw 	 */
3199*da6c28aaSamw 	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3200*da6c28aaSamw 		vap->va_mask &= ~AT_XVATTR;
3201*da6c28aaSamw 	}
3202*da6c28aaSamw 
3203*da6c28aaSamw 	/*
3204*da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
3205*da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
3206*da6c28aaSamw 	 */
3207*da6c28aaSamw 	if ((flags & ATTR_NOACLCHECK) &&
3208*da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3209*da6c28aaSamw 		return (EINVAL);
3210*da6c28aaSamw 	}
3211*da6c28aaSamw 	err = (*(vp)->v_op->vop_getattr)(vp, vap, flags, cr, ct);
32122bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getattr);
32135a59a8b3Srsb 	return (err);
32147c478bd9Sstevel@tonic-gate }
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate int
32177c478bd9Sstevel@tonic-gate fop_setattr(
32187c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32197c478bd9Sstevel@tonic-gate 	vattr_t *vap,
32207c478bd9Sstevel@tonic-gate 	int flags,
32217c478bd9Sstevel@tonic-gate 	cred_t *cr,
32227c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
32237c478bd9Sstevel@tonic-gate {
32245a59a8b3Srsb 	int	err;
32255a59a8b3Srsb 
3226f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3227f48205beScasper 
3228*da6c28aaSamw 	/*
3229*da6c28aaSamw 	 * If this file system doesn't understand the xvattr extensions
3230*da6c28aaSamw 	 * then turn off the xvattr bit.
3231*da6c28aaSamw 	 */
3232*da6c28aaSamw 	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3233*da6c28aaSamw 		vap->va_mask &= ~AT_XVATTR;
3234*da6c28aaSamw 	}
3235*da6c28aaSamw 
3236*da6c28aaSamw 	/*
3237*da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
3238*da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
3239*da6c28aaSamw 	 */
3240*da6c28aaSamw 	if ((flags & ATTR_NOACLCHECK) &&
3241*da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3242*da6c28aaSamw 		return (EINVAL);
3243*da6c28aaSamw 	}
32445a59a8b3Srsb 	err = (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct);
32452bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setattr);
32465a59a8b3Srsb 	return (err);
32477c478bd9Sstevel@tonic-gate }
32487c478bd9Sstevel@tonic-gate 
32497c478bd9Sstevel@tonic-gate int
32507c478bd9Sstevel@tonic-gate fop_access(
32517c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32527c478bd9Sstevel@tonic-gate 	int mode,
32537c478bd9Sstevel@tonic-gate 	int flags,
3254*da6c28aaSamw 	cred_t *cr,
3255*da6c28aaSamw 	caller_context_t *ct)
32567c478bd9Sstevel@tonic-gate {
32575a59a8b3Srsb 	int	err;
32585a59a8b3Srsb 
3259*da6c28aaSamw 	if ((flags & V_ACE_MASK) &&
3260*da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3261*da6c28aaSamw 		return (EINVAL);
3262*da6c28aaSamw 	}
3263*da6c28aaSamw 
3264f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3265f48205beScasper 
3266*da6c28aaSamw 	err = (*(vp)->v_op->vop_access)(vp, mode, flags, cr, ct);
32672bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, access);
32685a59a8b3Srsb 	return (err);
32697c478bd9Sstevel@tonic-gate }
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate int
32727c478bd9Sstevel@tonic-gate fop_lookup(
32737c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
32747c478bd9Sstevel@tonic-gate 	char *nm,
32757c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
32767c478bd9Sstevel@tonic-gate 	pathname_t *pnp,
32777c478bd9Sstevel@tonic-gate 	int flags,
32787c478bd9Sstevel@tonic-gate 	vnode_t *rdir,
3279*da6c28aaSamw 	cred_t *cr,
3280*da6c28aaSamw 	caller_context_t *ct,
3281*da6c28aaSamw 	int *deflags,		/* Returned per-dirent flags */
3282*da6c28aaSamw 	pathname_t *ppnp)	/* Returned case-preserved name in directory */
32837c478bd9Sstevel@tonic-gate {
3284ca2c3138Seschrock 	int ret;
3285ca2c3138Seschrock 
3286*da6c28aaSamw 	/*
3287*da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3288*da6c28aaSamw 	 * and said access is requested, fail quickly.  It is required
3289*da6c28aaSamw 	 * that if the vfs supports case-insensitive lookup, it also
3290*da6c28aaSamw 	 * supports extended dirent flags.
3291*da6c28aaSamw 	 */
3292*da6c28aaSamw 	if (flags & FIGNORECASE &&
3293*da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3294*da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3295*da6c28aaSamw 		return (EINVAL);
3296*da6c28aaSamw 
3297f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3298f48205beScasper 
3299*da6c28aaSamw 	if ((flags & LOOKUP_XATTR) && (flags & LOOKUP_HAVE_SYSATTR_DIR) == 0) {
3300*da6c28aaSamw 		ret = xattr_dir_lookup(dvp, vpp, flags, cr);
3301*da6c28aaSamw 	} else {
3302*da6c28aaSamw 		ret = (*(dvp)->v_op->vop_lookup)
3303*da6c28aaSamw 		    (dvp, nm, vpp, pnp, flags, rdir, cr, ct, deflags, ppnp);
3304*da6c28aaSamw 	}
33055a59a8b3Srsb 	if (ret == 0 && *vpp) {
33062bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, lookup);
33075a59a8b3Srsb 		if ((*vpp)->v_path == NULL) {
3308ca2c3138Seschrock 			vn_setpath(rootdir, dvp, *vpp, nm, strlen(nm));
33095a59a8b3Srsb 		}
33105a59a8b3Srsb 	}
3311ca2c3138Seschrock 
3312ca2c3138Seschrock 	return (ret);
33137c478bd9Sstevel@tonic-gate }
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate int
33167c478bd9Sstevel@tonic-gate fop_create(
33177c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
33187c478bd9Sstevel@tonic-gate 	char *name,
33197c478bd9Sstevel@tonic-gate 	vattr_t *vap,
33207c478bd9Sstevel@tonic-gate 	vcexcl_t excl,
33217c478bd9Sstevel@tonic-gate 	int mode,
33227c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
33237c478bd9Sstevel@tonic-gate 	cred_t *cr,
3324*da6c28aaSamw 	int flags,
3325*da6c28aaSamw 	caller_context_t *ct,
3326*da6c28aaSamw 	vsecattr_t *vsecp)	/* ACL to set during create */
33277c478bd9Sstevel@tonic-gate {
33287c478bd9Sstevel@tonic-gate 	int ret;
33297c478bd9Sstevel@tonic-gate 
3330*da6c28aaSamw 	if (vsecp != NULL &&
3331*da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3332*da6c28aaSamw 		return (EINVAL);
3333*da6c28aaSamw 	}
3334*da6c28aaSamw 	/*
3335*da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3336*da6c28aaSamw 	 * and said access is requested, fail quickly.
3337*da6c28aaSamw 	 */
3338*da6c28aaSamw 	if (flags & FIGNORECASE &&
3339*da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3340*da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3341*da6c28aaSamw 		return (EINVAL);
3342*da6c28aaSamw 
3343f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3344f48205beScasper 
33457c478bd9Sstevel@tonic-gate 	ret = (*(dvp)->v_op->vop_create)
3346*da6c28aaSamw 	    (dvp, name, vap, excl, mode, vpp, cr, flags, ct, vsecp);
33475a59a8b3Srsb 	if (ret == 0 && *vpp) {
33482bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, create);
33495a59a8b3Srsb 		if ((*vpp)->v_path == NULL) {
3350ca2c3138Seschrock 			vn_setpath(rootdir, dvp, *vpp, name, strlen(name));
33515a59a8b3Srsb 		}
33525a59a8b3Srsb 	}
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate 	return (ret);
33557c478bd9Sstevel@tonic-gate }
33567c478bd9Sstevel@tonic-gate 
33577c478bd9Sstevel@tonic-gate int
33587c478bd9Sstevel@tonic-gate fop_remove(
33597c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
33607c478bd9Sstevel@tonic-gate 	char *nm,
3361*da6c28aaSamw 	cred_t *cr,
3362*da6c28aaSamw 	caller_context_t *ct,
3363*da6c28aaSamw 	int flags)
33647c478bd9Sstevel@tonic-gate {
33655a59a8b3Srsb 	int	err;
33665a59a8b3Srsb 
3367*da6c28aaSamw 	/*
3368*da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3369*da6c28aaSamw 	 * and said access is requested, fail quickly.
3370*da6c28aaSamw 	 */
3371*da6c28aaSamw 	if (flags & FIGNORECASE &&
3372*da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3373*da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3374*da6c28aaSamw 		return (EINVAL);
3375*da6c28aaSamw 
3376f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3377f48205beScasper 
3378*da6c28aaSamw 	err = (*(dvp)->v_op->vop_remove)(dvp, nm, cr, ct, flags);
33792bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, remove);
33805a59a8b3Srsb 	return (err);
33817c478bd9Sstevel@tonic-gate }
33827c478bd9Sstevel@tonic-gate 
33837c478bd9Sstevel@tonic-gate int
33847c478bd9Sstevel@tonic-gate fop_link(
33857c478bd9Sstevel@tonic-gate 	vnode_t *tdvp,
33867c478bd9Sstevel@tonic-gate 	vnode_t *svp,
33877c478bd9Sstevel@tonic-gate 	char *tnm,
3388*da6c28aaSamw 	cred_t *cr,
3389*da6c28aaSamw 	caller_context_t *ct,
3390*da6c28aaSamw 	int flags)
33917c478bd9Sstevel@tonic-gate {
33925a59a8b3Srsb 	int	err;
33935a59a8b3Srsb 
3394*da6c28aaSamw 	/*
3395*da6c28aaSamw 	 * If the target file system doesn't support case-insensitive access
3396*da6c28aaSamw 	 * and said access is requested, fail quickly.
3397*da6c28aaSamw 	 */
3398*da6c28aaSamw 	if (flags & FIGNORECASE &&
3399*da6c28aaSamw 	    (vfs_has_feature(tdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3400*da6c28aaSamw 	    vfs_has_feature(tdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3401*da6c28aaSamw 		return (EINVAL);
3402*da6c28aaSamw 
3403f48205beScasper 	VOPXID_MAP_CR(tdvp, cr);
3404f48205beScasper 
3405*da6c28aaSamw 	err = (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr, ct, flags);
34062bb1cb30Sbmc 	VOPSTATS_UPDATE(tdvp, link);
34075a59a8b3Srsb 	return (err);
34087c478bd9Sstevel@tonic-gate }
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate int
34117c478bd9Sstevel@tonic-gate fop_rename(
34127c478bd9Sstevel@tonic-gate 	vnode_t *sdvp,
34137c478bd9Sstevel@tonic-gate 	char *snm,
34147c478bd9Sstevel@tonic-gate 	vnode_t *tdvp,
34157c478bd9Sstevel@tonic-gate 	char *tnm,
3416*da6c28aaSamw 	cred_t *cr,
3417*da6c28aaSamw 	caller_context_t *ct,
3418*da6c28aaSamw 	int flags)
34197c478bd9Sstevel@tonic-gate {
34205a59a8b3Srsb 	int	err;
34215a59a8b3Srsb 
3422*da6c28aaSamw 	/*
3423*da6c28aaSamw 	 * If the file system involved does not support
3424*da6c28aaSamw 	 * case-insensitive access and said access is requested, fail
3425*da6c28aaSamw 	 * quickly.
3426*da6c28aaSamw 	 */
3427*da6c28aaSamw 	if (flags & FIGNORECASE &&
3428*da6c28aaSamw 	    ((vfs_has_feature(sdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3429*da6c28aaSamw 	    vfs_has_feature(sdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0)))
3430*da6c28aaSamw 		return (EINVAL);
3431*da6c28aaSamw 
3432f48205beScasper 	VOPXID_MAP_CR(tdvp, cr);
3433f48205beScasper 
3434*da6c28aaSamw 	err = (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr, ct, flags);
34352bb1cb30Sbmc 	VOPSTATS_UPDATE(sdvp, rename);
34365a59a8b3Srsb 	return (err);
34377c478bd9Sstevel@tonic-gate }
34387c478bd9Sstevel@tonic-gate 
34397c478bd9Sstevel@tonic-gate int
34407c478bd9Sstevel@tonic-gate fop_mkdir(
34417c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
34427c478bd9Sstevel@tonic-gate 	char *dirname,
34437c478bd9Sstevel@tonic-gate 	vattr_t *vap,
34447c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
3445*da6c28aaSamw 	cred_t *cr,
3446*da6c28aaSamw 	caller_context_t *ct,
3447*da6c28aaSamw 	int flags,
3448*da6c28aaSamw 	vsecattr_t *vsecp)	/* ACL to set during create */
34497c478bd9Sstevel@tonic-gate {
34507c478bd9Sstevel@tonic-gate 	int ret;
34517c478bd9Sstevel@tonic-gate 
3452*da6c28aaSamw 	if (vsecp != NULL &&
3453*da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3454*da6c28aaSamw 		return (EINVAL);
3455*da6c28aaSamw 	}
3456*da6c28aaSamw 	/*
3457*da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3458*da6c28aaSamw 	 * and said access is requested, fail quickly.
3459*da6c28aaSamw 	 */
3460*da6c28aaSamw 	if (flags & FIGNORECASE &&
3461*da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3462*da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3463*da6c28aaSamw 		return (EINVAL);
3464*da6c28aaSamw 
3465f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3466f48205beScasper 
3467*da6c28aaSamw 	ret = (*(dvp)->v_op->vop_mkdir)
3468*da6c28aaSamw 	    (dvp, dirname, vap, vpp, cr, ct, flags, vsecp);
34695a59a8b3Srsb 	if (ret == 0 && *vpp) {
34702bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, mkdir);
34715a59a8b3Srsb 		if ((*vpp)->v_path == NULL) {
34725a59a8b3Srsb 			vn_setpath(rootdir, dvp, *vpp, dirname,
34735a59a8b3Srsb 			    strlen(dirname));
34745a59a8b3Srsb 		}
34755a59a8b3Srsb 	}
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate 	return (ret);
34787c478bd9Sstevel@tonic-gate }
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate int
34817c478bd9Sstevel@tonic-gate fop_rmdir(
34827c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
34837c478bd9Sstevel@tonic-gate 	char *nm,
34847c478bd9Sstevel@tonic-gate 	vnode_t *cdir,
3485*da6c28aaSamw 	cred_t *cr,
3486*da6c28aaSamw 	caller_context_t *ct,
3487*da6c28aaSamw 	int flags)
34887c478bd9Sstevel@tonic-gate {
34895a59a8b3Srsb 	int	err;
34905a59a8b3Srsb 
3491*da6c28aaSamw 	/*
3492*da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3493*da6c28aaSamw 	 * and said access is requested, fail quickly.
3494*da6c28aaSamw 	 */
3495*da6c28aaSamw 	if (flags & FIGNORECASE &&
3496*da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3497*da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3498*da6c28aaSamw 		return (EINVAL);
3499*da6c28aaSamw 
3500f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3501f48205beScasper 
3502*da6c28aaSamw 	err = (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr, ct, flags);
35032bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, rmdir);
35045a59a8b3Srsb 	return (err);
35057c478bd9Sstevel@tonic-gate }
35067c478bd9Sstevel@tonic-gate 
35077c478bd9Sstevel@tonic-gate int
35087c478bd9Sstevel@tonic-gate fop_readdir(
35097c478bd9Sstevel@tonic-gate 	vnode_t *vp,
35107c478bd9Sstevel@tonic-gate 	uio_t *uiop,
35117c478bd9Sstevel@tonic-gate 	cred_t *cr,
3512*da6c28aaSamw 	int *eofp,
3513*da6c28aaSamw 	caller_context_t *ct,
3514*da6c28aaSamw 	int flags)
35157c478bd9Sstevel@tonic-gate {
35165a59a8b3Srsb 	int	err;
35175a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
35185a59a8b3Srsb 
3519*da6c28aaSamw 	/*
3520*da6c28aaSamw 	 * If this file system doesn't support retrieving directory
3521*da6c28aaSamw 	 * entry flags and said access is requested, fail quickly.
3522*da6c28aaSamw 	 */
3523*da6c28aaSamw 	if (flags & V_RDDIR_ENTFLAGS &&
3524*da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS) == 0)
3525*da6c28aaSamw 		return (EINVAL);
3526*da6c28aaSamw 
3527f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3528f48205beScasper 
3529*da6c28aaSamw 	err = (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp, ct, flags);
35302bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, readdir,
35315a59a8b3Srsb 	    readdir_bytes, (resid_start - uiop->uio_resid));
35325a59a8b3Srsb 	return (err);
35337c478bd9Sstevel@tonic-gate }
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate int
35367c478bd9Sstevel@tonic-gate fop_symlink(
35377c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
35387c478bd9Sstevel@tonic-gate 	char *linkname,
35397c478bd9Sstevel@tonic-gate 	vattr_t *vap,
35407c478bd9Sstevel@tonic-gate 	char *target,
3541*da6c28aaSamw 	cred_t *cr,
3542*da6c28aaSamw 	caller_context_t *ct,
3543*da6c28aaSamw 	int flags)
35447c478bd9Sstevel@tonic-gate {
35455a59a8b3Srsb 	int	err;
35465a59a8b3Srsb 
3547*da6c28aaSamw 	/*
3548*da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3549*da6c28aaSamw 	 * and said access is requested, fail quickly.
3550*da6c28aaSamw 	 */
3551*da6c28aaSamw 	if (flags & FIGNORECASE &&
3552*da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3553*da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3554*da6c28aaSamw 		return (EINVAL);
3555*da6c28aaSamw 
3556f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3557f48205beScasper 
3558*da6c28aaSamw 	err = (*(dvp)->v_op->vop_symlink)
3559*da6c28aaSamw 	    (dvp, linkname, vap, target, cr, ct, flags);
35602bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, symlink);
35615a59a8b3Srsb 	return (err);
35627c478bd9Sstevel@tonic-gate }
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate int
35657c478bd9Sstevel@tonic-gate fop_readlink(
35667c478bd9Sstevel@tonic-gate 	vnode_t *vp,
35677c478bd9Sstevel@tonic-gate 	uio_t *uiop,
3568*da6c28aaSamw 	cred_t *cr,
3569*da6c28aaSamw 	caller_context_t *ct)
35707c478bd9Sstevel@tonic-gate {
35715a59a8b3Srsb 	int	err;
35725a59a8b3Srsb 
3573f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3574f48205beScasper 
3575*da6c28aaSamw 	err = (*(vp)->v_op->vop_readlink)(vp, uiop, cr, ct);
35762bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, readlink);
35775a59a8b3Srsb 	return (err);
35787c478bd9Sstevel@tonic-gate }
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate int
35817c478bd9Sstevel@tonic-gate fop_fsync(
35827c478bd9Sstevel@tonic-gate 	vnode_t *vp,
35837c478bd9Sstevel@tonic-gate 	int syncflag,
3584*da6c28aaSamw 	cred_t *cr,
3585*da6c28aaSamw 	caller_context_t *ct)
35867c478bd9Sstevel@tonic-gate {
35875a59a8b3Srsb 	int	err;
35885a59a8b3Srsb 
3589f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3590f48205beScasper 
3591*da6c28aaSamw 	err = (*(vp)->v_op->vop_fsync)(vp, syncflag, cr, ct);
35922bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, fsync);
35935a59a8b3Srsb 	return (err);
35947c478bd9Sstevel@tonic-gate }
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate void
35977c478bd9Sstevel@tonic-gate fop_inactive(
35987c478bd9Sstevel@tonic-gate 	vnode_t *vp,
3599*da6c28aaSamw 	cred_t *cr,
3600*da6c28aaSamw 	caller_context_t *ct)
36017c478bd9Sstevel@tonic-gate {
36025a59a8b3Srsb 	/* Need to update stats before vop call since we may lose the vnode */
36032bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, inactive);
3604f48205beScasper 
3605f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3606f48205beScasper 
3607*da6c28aaSamw 	(*(vp)->v_op->vop_inactive)(vp, cr, ct);
36087c478bd9Sstevel@tonic-gate }
36097c478bd9Sstevel@tonic-gate 
36107c478bd9Sstevel@tonic-gate int
36117c478bd9Sstevel@tonic-gate fop_fid(
36127c478bd9Sstevel@tonic-gate 	vnode_t *vp,
3613*da6c28aaSamw 	fid_t *fidp,
3614*da6c28aaSamw 	caller_context_t *ct)
36157c478bd9Sstevel@tonic-gate {
36165a59a8b3Srsb 	int	err;
36175a59a8b3Srsb 
3618*da6c28aaSamw 	err = (*(vp)->v_op->vop_fid)(vp, fidp, ct);
36192bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, fid);
36205a59a8b3Srsb 	return (err);
36217c478bd9Sstevel@tonic-gate }
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate int
36247c478bd9Sstevel@tonic-gate fop_rwlock(
36257c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36267c478bd9Sstevel@tonic-gate 	int write_lock,
36277c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
36287c478bd9Sstevel@tonic-gate {
36295a59a8b3Srsb 	int	ret;
36305a59a8b3Srsb 
36315a59a8b3Srsb 	ret = ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct));
36322bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, rwlock);
36335a59a8b3Srsb 	return (ret);
36347c478bd9Sstevel@tonic-gate }
36357c478bd9Sstevel@tonic-gate 
36367c478bd9Sstevel@tonic-gate void
36377c478bd9Sstevel@tonic-gate fop_rwunlock(
36387c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36397c478bd9Sstevel@tonic-gate 	int write_lock,
36407c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
36417c478bd9Sstevel@tonic-gate {
36427c478bd9Sstevel@tonic-gate 	(*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct);
36432bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, rwunlock);
36447c478bd9Sstevel@tonic-gate }
36457c478bd9Sstevel@tonic-gate 
36467c478bd9Sstevel@tonic-gate int
36477c478bd9Sstevel@tonic-gate fop_seek(
36487c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36497c478bd9Sstevel@tonic-gate 	offset_t ooff,
3650*da6c28aaSamw 	offset_t *noffp,
3651*da6c28aaSamw 	caller_context_t *ct)
36527c478bd9Sstevel@tonic-gate {
36535a59a8b3Srsb 	int	err;
36545a59a8b3Srsb 
3655*da6c28aaSamw 	err = (*(vp)->v_op->vop_seek)(vp, ooff, noffp, ct);
36562bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, seek);
36575a59a8b3Srsb 	return (err);
36587c478bd9Sstevel@tonic-gate }
36597c478bd9Sstevel@tonic-gate 
36607c478bd9Sstevel@tonic-gate int
36617c478bd9Sstevel@tonic-gate fop_cmp(
36627c478bd9Sstevel@tonic-gate 	vnode_t *vp1,
3663*da6c28aaSamw 	vnode_t *vp2,
3664*da6c28aaSamw 	caller_context_t *ct)
36657c478bd9Sstevel@tonic-gate {
36665a59a8b3Srsb 	int	err;
36675a59a8b3Srsb 
3668*da6c28aaSamw 	err = (*(vp1)->v_op->vop_cmp)(vp1, vp2, ct);
36692bb1cb30Sbmc 	VOPSTATS_UPDATE(vp1, cmp);
36705a59a8b3Srsb 	return (err);
36717c478bd9Sstevel@tonic-gate }
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate int
36747c478bd9Sstevel@tonic-gate fop_frlock(
36757c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36767c478bd9Sstevel@tonic-gate 	int cmd,
36777c478bd9Sstevel@tonic-gate 	flock64_t *bfp,
36787c478bd9Sstevel@tonic-gate 	int flag,
36797c478bd9Sstevel@tonic-gate 	offset_t offset,
36807c478bd9Sstevel@tonic-gate 	struct flk_callback *flk_cbp,
3681*da6c28aaSamw 	cred_t *cr,
3682*da6c28aaSamw 	caller_context_t *ct)
36837c478bd9Sstevel@tonic-gate {
36845a59a8b3Srsb 	int	err;
36855a59a8b3Srsb 
3686f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3687f48205beScasper 
36885a59a8b3Srsb 	err = (*(vp)->v_op->vop_frlock)
3689*da6c28aaSamw 	    (vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
36902bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, frlock);
36915a59a8b3Srsb 	return (err);
36927c478bd9Sstevel@tonic-gate }
36937c478bd9Sstevel@tonic-gate 
36947c478bd9Sstevel@tonic-gate int
36957c478bd9Sstevel@tonic-gate fop_space(
36967c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36977c478bd9Sstevel@tonic-gate 	int cmd,
36987c478bd9Sstevel@tonic-gate 	flock64_t *bfp,
36997c478bd9Sstevel@tonic-gate 	int flag,
37007c478bd9Sstevel@tonic-gate 	offset_t offset,
37017c478bd9Sstevel@tonic-gate 	cred_t *cr,
37027c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
37037c478bd9Sstevel@tonic-gate {
37045a59a8b3Srsb 	int	err;
37055a59a8b3Srsb 
3706f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3707f48205beScasper 
37085a59a8b3Srsb 	err = (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct);
37092bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, space);
37105a59a8b3Srsb 	return (err);
37117c478bd9Sstevel@tonic-gate }
37127c478bd9Sstevel@tonic-gate 
37137c478bd9Sstevel@tonic-gate int
37147c478bd9Sstevel@tonic-gate fop_realvp(
37157c478bd9Sstevel@tonic-gate 	vnode_t *vp,
3716*da6c28aaSamw 	vnode_t **vpp,
3717*da6c28aaSamw 	caller_context_t *ct)
37187c478bd9Sstevel@tonic-gate {
37195a59a8b3Srsb 	int	err;
37205a59a8b3Srsb 
3721*da6c28aaSamw 	err = (*(vp)->v_op->vop_realvp)(vp, vpp, ct);
37222bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, realvp);
37235a59a8b3Srsb 	return (err);
37247c478bd9Sstevel@tonic-gate }
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate int
37277c478bd9Sstevel@tonic-gate fop_getpage(
37287c478bd9Sstevel@tonic-gate 	vnode_t *vp,
37297c478bd9Sstevel@tonic-gate 	offset_t off,
37307c478bd9Sstevel@tonic-gate 	size_t len,
37317c478bd9Sstevel@tonic-gate 	uint_t *protp,
37327c478bd9Sstevel@tonic-gate 	page_t **plarr,
37337c478bd9Sstevel@tonic-gate 	size_t plsz,
37347c478bd9Sstevel@tonic-gate 	struct seg *seg,
37357c478bd9Sstevel@tonic-gate 	caddr_t addr,
37367c478bd9Sstevel@tonic-gate 	enum seg_rw rw,
3737*da6c28aaSamw 	cred_t *cr,
3738*da6c28aaSamw 	caller_context_t *ct)
37397c478bd9Sstevel@tonic-gate {
37405a59a8b3Srsb 	int	err;
37415a59a8b3Srsb 
3742f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3743f48205beScasper 
37445a59a8b3Srsb 	err = (*(vp)->v_op->vop_getpage)
3745*da6c28aaSamw 	    (vp, off, len, protp, plarr, plsz, seg, addr, rw, cr, ct);
37462bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getpage);
37475a59a8b3Srsb 	return (err);
37487c478bd9Sstevel@tonic-gate }
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate int
37517c478bd9Sstevel@tonic-gate fop_putpage(
37527c478bd9Sstevel@tonic-gate 	vnode_t *vp,
37537c478bd9Sstevel@tonic-gate 	offset_t off,
37547c478bd9Sstevel@tonic-gate 	size_t len,
37557c478bd9Sstevel@tonic-gate 	int flags,
3756*da6c28aaSamw 	cred_t *cr,
3757*da6c28aaSamw 	caller_context_t *ct)
37587c478bd9Sstevel@tonic-gate {
37595a59a8b3Srsb 	int	err;
37605a59a8b3Srsb 
3761f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3762f48205beScasper 
3763*da6c28aaSamw 	err = (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr, ct);
37642bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, putpage);
37655a59a8b3Srsb 	return (err);
37667c478bd9Sstevel@tonic-gate }
37677c478bd9Sstevel@tonic-gate 
37687c478bd9Sstevel@tonic-gate int
37697c478bd9Sstevel@tonic-gate fop_map(
37707c478bd9Sstevel@tonic-gate 	vnode_t *vp,
37717c478bd9Sstevel@tonic-gate 	offset_t off,
37727c478bd9Sstevel@tonic-gate 	struct as *as,
37737c478bd9Sstevel@tonic-gate 	caddr_t *addrp,
37747c478bd9Sstevel@tonic-gate 	size_t len,
37757c478bd9Sstevel@tonic-gate 	uchar_t prot,
37767c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
37777c478bd9Sstevel@tonic-gate 	uint_t flags,
3778*da6c28aaSamw 	cred_t *cr,
3779*da6c28aaSamw 	caller_context_t *ct)
37807c478bd9Sstevel@tonic-gate {
37815a59a8b3Srsb 	int	err;
37825a59a8b3Srsb 
3783f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3784f48205beScasper 
37855a59a8b3Srsb 	err = (*(vp)->v_op->vop_map)
3786*da6c28aaSamw 	    (vp, off, as, addrp, len, prot, maxprot, flags, cr, ct);
37872bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, map);
37885a59a8b3Srsb 	return (err);
37897c478bd9Sstevel@tonic-gate }
37907c478bd9Sstevel@tonic-gate 
37917c478bd9Sstevel@tonic-gate int
37927c478bd9Sstevel@tonic-gate fop_addmap(
37937c478bd9Sstevel@tonic-gate 	vnode_t *vp,
37947c478bd9Sstevel@tonic-gate 	offset_t off,
37957c478bd9Sstevel@tonic-gate 	struct as *as,
37967c478bd9Sstevel@tonic-gate 	caddr_t addr,
37977c478bd9Sstevel@tonic-gate 	size_t len,
37987c478bd9Sstevel@tonic-gate 	uchar_t prot,
37997c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
38007c478bd9Sstevel@tonic-gate 	uint_t flags,
3801*da6c28aaSamw 	cred_t *cr,
3802*da6c28aaSamw 	caller_context_t *ct)
38037c478bd9Sstevel@tonic-gate {
38047c478bd9Sstevel@tonic-gate 	int error;
38057c478bd9Sstevel@tonic-gate 	u_longlong_t delta;
38067c478bd9Sstevel@tonic-gate 
3807f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3808f48205beScasper 
38097c478bd9Sstevel@tonic-gate 	error = (*(vp)->v_op->vop_addmap)
3810*da6c28aaSamw 	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
38117c478bd9Sstevel@tonic-gate 
38127c478bd9Sstevel@tonic-gate 	if ((!error) && (vp->v_type == VREG)) {
38137c478bd9Sstevel@tonic-gate 		delta = (u_longlong_t)btopr(len);
38147c478bd9Sstevel@tonic-gate 		/*
38157c478bd9Sstevel@tonic-gate 		 * If file is declared MAP_PRIVATE, it can't be written back
38167c478bd9Sstevel@tonic-gate 		 * even if open for write. Handle as read.
38177c478bd9Sstevel@tonic-gate 		 */
38187c478bd9Sstevel@tonic-gate 		if (flags & MAP_PRIVATE) {
38197c478bd9Sstevel@tonic-gate 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
38207c478bd9Sstevel@tonic-gate 			    (int64_t)delta);
38217c478bd9Sstevel@tonic-gate 		} else {
38227c478bd9Sstevel@tonic-gate 			/*
38237c478bd9Sstevel@tonic-gate 			 * atomic_add_64 forces the fetch of a 64 bit value to
38247c478bd9Sstevel@tonic-gate 			 * be atomic on 32 bit machines
38257c478bd9Sstevel@tonic-gate 			 */
38267c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_WRITE)
38277c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
38287c478bd9Sstevel@tonic-gate 				    (int64_t)delta);
38297c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_READ)
38307c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
38317c478bd9Sstevel@tonic-gate 				    (int64_t)delta);
38327c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_EXEC)
38337c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
38347c478bd9Sstevel@tonic-gate 				    (int64_t)delta);
38357c478bd9Sstevel@tonic-gate 		}
38367c478bd9Sstevel@tonic-gate 	}
38372bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, addmap);
38387c478bd9Sstevel@tonic-gate 	return (error);
38397c478bd9Sstevel@tonic-gate }
38407c478bd9Sstevel@tonic-gate 
38417c478bd9Sstevel@tonic-gate int
38427c478bd9Sstevel@tonic-gate fop_delmap(
38437c478bd9Sstevel@tonic-gate 	vnode_t *vp,
38447c478bd9Sstevel@tonic-gate 	offset_t off,
38457c478bd9Sstevel@tonic-gate 	struct as *as,
38467c478bd9Sstevel@tonic-gate 	caddr_t addr,
38477c478bd9Sstevel@tonic-gate 	size_t len,
38487c478bd9Sstevel@tonic-gate 	uint_t prot,
38497c478bd9Sstevel@tonic-gate 	uint_t maxprot,
38507c478bd9Sstevel@tonic-gate 	uint_t flags,
3851*da6c28aaSamw 	cred_t *cr,
3852*da6c28aaSamw 	caller_context_t *ct)
38537c478bd9Sstevel@tonic-gate {
38547c478bd9Sstevel@tonic-gate 	int error;
38557c478bd9Sstevel@tonic-gate 	u_longlong_t delta;
3856f48205beScasper 
3857f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3858f48205beScasper 
38597c478bd9Sstevel@tonic-gate 	error = (*(vp)->v_op->vop_delmap)
3860*da6c28aaSamw 	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
38617c478bd9Sstevel@tonic-gate 
38627c478bd9Sstevel@tonic-gate 	/*
38637c478bd9Sstevel@tonic-gate 	 * NFS calls into delmap twice, the first time
38647c478bd9Sstevel@tonic-gate 	 * it simply establishes a callback mechanism and returns EAGAIN
38657c478bd9Sstevel@tonic-gate 	 * while the real work is being done upon the second invocation.
38667c478bd9Sstevel@tonic-gate 	 * We have to detect this here and only decrement the counts upon
38677c478bd9Sstevel@tonic-gate 	 * the second delmap request.
38687c478bd9Sstevel@tonic-gate 	 */
38697c478bd9Sstevel@tonic-gate 	if ((error != EAGAIN) && (vp->v_type == VREG)) {
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate 		delta = (u_longlong_t)btopr(len);
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 		if (flags & MAP_PRIVATE) {
38747c478bd9Sstevel@tonic-gate 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
38757c478bd9Sstevel@tonic-gate 			    (int64_t)(-delta));
38767c478bd9Sstevel@tonic-gate 		} else {
38777c478bd9Sstevel@tonic-gate 			/*
38787c478bd9Sstevel@tonic-gate 			 * atomic_add_64 forces the fetch of a 64 bit value
38797c478bd9Sstevel@tonic-gate 			 * to be atomic on 32 bit machines
38807c478bd9Sstevel@tonic-gate 			 */
38817c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_WRITE)
38827c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
38837c478bd9Sstevel@tonic-gate 				    (int64_t)(-delta));
38847c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_READ)
38857c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
38867c478bd9Sstevel@tonic-gate 				    (int64_t)(-delta));
38877c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_EXEC)
38887c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
38897c478bd9Sstevel@tonic-gate 				    (int64_t)(-delta));
38907c478bd9Sstevel@tonic-gate 		}
38917c478bd9Sstevel@tonic-gate 	}
38922bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, delmap);
38937c478bd9Sstevel@tonic-gate 	return (error);
38947c478bd9Sstevel@tonic-gate }
38957c478bd9Sstevel@tonic-gate 
38967c478bd9Sstevel@tonic-gate 
38977c478bd9Sstevel@tonic-gate int
38987c478bd9Sstevel@tonic-gate fop_poll(
38997c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39007c478bd9Sstevel@tonic-gate 	short events,
39017c478bd9Sstevel@tonic-gate 	int anyyet,
39027c478bd9Sstevel@tonic-gate 	short *reventsp,
3903*da6c28aaSamw 	struct pollhead **phpp,
3904*da6c28aaSamw 	caller_context_t *ct)
39057c478bd9Sstevel@tonic-gate {
39065a59a8b3Srsb 	int	err;
39075a59a8b3Srsb 
3908*da6c28aaSamw 	err = (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp, ct);
39092bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, poll);
39105a59a8b3Srsb 	return (err);
39117c478bd9Sstevel@tonic-gate }
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate int
39147c478bd9Sstevel@tonic-gate fop_dump(
39157c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39167c478bd9Sstevel@tonic-gate 	caddr_t addr,
39177c478bd9Sstevel@tonic-gate 	int lbdn,
3918*da6c28aaSamw 	int dblks,
3919*da6c28aaSamw 	caller_context_t *ct)
39207c478bd9Sstevel@tonic-gate {
39215a59a8b3Srsb 	int	err;
39225a59a8b3Srsb 
3923*da6c28aaSamw 	err = (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks, ct);
39242bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dump);
39255a59a8b3Srsb 	return (err);
39267c478bd9Sstevel@tonic-gate }
39277c478bd9Sstevel@tonic-gate 
39287c478bd9Sstevel@tonic-gate int
39297c478bd9Sstevel@tonic-gate fop_pathconf(
39307c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39317c478bd9Sstevel@tonic-gate 	int cmd,
39327c478bd9Sstevel@tonic-gate 	ulong_t *valp,
3933*da6c28aaSamw 	cred_t *cr,
3934*da6c28aaSamw 	caller_context_t *ct)
39357c478bd9Sstevel@tonic-gate {
39365a59a8b3Srsb 	int	err;
39375a59a8b3Srsb 
3938f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3939f48205beScasper 
3940*da6c28aaSamw 	err = (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr, ct);
39412bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, pathconf);
39425a59a8b3Srsb 	return (err);
39437c478bd9Sstevel@tonic-gate }
39447c478bd9Sstevel@tonic-gate 
39457c478bd9Sstevel@tonic-gate int
39467c478bd9Sstevel@tonic-gate fop_pageio(
39477c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39487c478bd9Sstevel@tonic-gate 	struct page *pp,
39497c478bd9Sstevel@tonic-gate 	u_offset_t io_off,
39507c478bd9Sstevel@tonic-gate 	size_t io_len,
39517c478bd9Sstevel@tonic-gate 	int flags,
3952*da6c28aaSamw 	cred_t *cr,
3953*da6c28aaSamw 	caller_context_t *ct)
39547c478bd9Sstevel@tonic-gate {
39555a59a8b3Srsb 	int	err;
39565a59a8b3Srsb 
3957f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3958f48205beScasper 
3959*da6c28aaSamw 	err = (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr, ct);
39602bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, pageio);
39615a59a8b3Srsb 	return (err);
39627c478bd9Sstevel@tonic-gate }
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate int
39657c478bd9Sstevel@tonic-gate fop_dumpctl(
39667c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39677c478bd9Sstevel@tonic-gate 	int action,
3968*da6c28aaSamw 	int *blkp,
3969*da6c28aaSamw 	caller_context_t *ct)
39707c478bd9Sstevel@tonic-gate {
39715a59a8b3Srsb 	int	err;
3972*da6c28aaSamw 	err = (*(vp)->v_op->vop_dumpctl)(vp, action, blkp, ct);
39732bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dumpctl);
39745a59a8b3Srsb 	return (err);
39757c478bd9Sstevel@tonic-gate }
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate void
39787c478bd9Sstevel@tonic-gate fop_dispose(
39797c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39807c478bd9Sstevel@tonic-gate 	page_t *pp,
39817c478bd9Sstevel@tonic-gate 	int flag,
39827c478bd9Sstevel@tonic-gate 	int dn,
3983*da6c28aaSamw 	cred_t *cr,
3984*da6c28aaSamw 	caller_context_t *ct)
39857c478bd9Sstevel@tonic-gate {
39865a59a8b3Srsb 	/* Must do stats first since it's possible to lose the vnode */
39872bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dispose);
3988f48205beScasper 
3989f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3990f48205beScasper 
3991*da6c28aaSamw 	(*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr, ct);
39927c478bd9Sstevel@tonic-gate }
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate int
39957c478bd9Sstevel@tonic-gate fop_setsecattr(
39967c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39977c478bd9Sstevel@tonic-gate 	vsecattr_t *vsap,
39987c478bd9Sstevel@tonic-gate 	int flag,
3999*da6c28aaSamw 	cred_t *cr,
4000*da6c28aaSamw 	caller_context_t *ct)
40017c478bd9Sstevel@tonic-gate {
40025a59a8b3Srsb 	int	err;
40035a59a8b3Srsb 
4004f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4005f48205beScasper 
4006*da6c28aaSamw 	/*
4007*da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
4008*da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
4009*da6c28aaSamw 	 */
4010*da6c28aaSamw 	if ((flag & ATTR_NOACLCHECK) &&
4011*da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4012*da6c28aaSamw 		return (EINVAL);
4013*da6c28aaSamw 	}
4014*da6c28aaSamw 	err = (*(vp)->v_op->vop_setsecattr) (vp, vsap, flag, cr, ct);
40152bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setsecattr);
40165a59a8b3Srsb 	return (err);
40177c478bd9Sstevel@tonic-gate }
40187c478bd9Sstevel@tonic-gate 
40197c478bd9Sstevel@tonic-gate int
40207c478bd9Sstevel@tonic-gate fop_getsecattr(
40217c478bd9Sstevel@tonic-gate 	vnode_t *vp,
40227c478bd9Sstevel@tonic-gate 	vsecattr_t *vsap,
40237c478bd9Sstevel@tonic-gate 	int flag,
4024*da6c28aaSamw 	cred_t *cr,
4025*da6c28aaSamw 	caller_context_t *ct)
40267c478bd9Sstevel@tonic-gate {
40275a59a8b3Srsb 	int	err;
40285a59a8b3Srsb 
4029*da6c28aaSamw 	/*
4030*da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
4031*da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
4032*da6c28aaSamw 	 */
4033*da6c28aaSamw 	if ((flag & ATTR_NOACLCHECK) &&
4034*da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4035*da6c28aaSamw 		return (EINVAL);
4036*da6c28aaSamw 	}
4037*da6c28aaSamw 
4038f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4039f48205beScasper 
4040*da6c28aaSamw 	err = (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr, ct);
40412bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getsecattr);
40425a59a8b3Srsb 	return (err);
40437c478bd9Sstevel@tonic-gate }
40447c478bd9Sstevel@tonic-gate 
40457c478bd9Sstevel@tonic-gate int
40467c478bd9Sstevel@tonic-gate fop_shrlock(
40477c478bd9Sstevel@tonic-gate 	vnode_t *vp,
40487c478bd9Sstevel@tonic-gate 	int cmd,
40497c478bd9Sstevel@tonic-gate 	struct shrlock *shr,
40507c478bd9Sstevel@tonic-gate 	int flag,
4051*da6c28aaSamw 	cred_t *cr,
4052*da6c28aaSamw 	caller_context_t *ct)
40537c478bd9Sstevel@tonic-gate {
40545a59a8b3Srsb 	int	err;
40555a59a8b3Srsb 
4056f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4057f48205beScasper 
4058*da6c28aaSamw 	err = (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr, ct);
40592bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, shrlock);
40605a59a8b3Srsb 	return (err);
40617c478bd9Sstevel@tonic-gate }
40627c478bd9Sstevel@tonic-gate 
40637c478bd9Sstevel@tonic-gate int
4064*da6c28aaSamw fop_vnevent(vnode_t *vp, vnevent_t vnevent, vnode_t *dvp, char *fnm,
4065*da6c28aaSamw     caller_context_t *ct)
40667c478bd9Sstevel@tonic-gate {
40675a59a8b3Srsb 	int	err;
40685a59a8b3Srsb 
4069*da6c28aaSamw 	err = (*(vp)->v_op->vop_vnevent)(vp, vnevent, dvp, fnm, ct);
40702bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, vnevent);
40715a59a8b3Srsb 	return (err);
40727c478bd9Sstevel@tonic-gate }
40731b300de9Sjwahlig 
40741b300de9Sjwahlig /*
40751b300de9Sjwahlig  * Default destructor
40761b300de9Sjwahlig  *	Needed because NULL destructor means that the key is unused
40771b300de9Sjwahlig  */
40781b300de9Sjwahlig /* ARGSUSED */
40791b300de9Sjwahlig void
40801b300de9Sjwahlig vsd_defaultdestructor(void *value)
40811b300de9Sjwahlig {}
40821b300de9Sjwahlig 
40831b300de9Sjwahlig /*
40841b300de9Sjwahlig  * Create a key (index into per vnode array)
40851b300de9Sjwahlig  *	Locks out vsd_create, vsd_destroy, and vsd_free
40861b300de9Sjwahlig  *	May allocate memory with lock held
40871b300de9Sjwahlig  */
40881b300de9Sjwahlig void
40891b300de9Sjwahlig vsd_create(uint_t *keyp, void (*destructor)(void *))
40901b300de9Sjwahlig {
40911b300de9Sjwahlig 	int	i;
40921b300de9Sjwahlig 	uint_t	nkeys;
40931b300de9Sjwahlig 
40941b300de9Sjwahlig 	/*
40951b300de9Sjwahlig 	 * if key is allocated, do nothing
40961b300de9Sjwahlig 	 */
40971b300de9Sjwahlig 	mutex_enter(&vsd_lock);
40981b300de9Sjwahlig 	if (*keyp) {
40991b300de9Sjwahlig 		mutex_exit(&vsd_lock);
41001b300de9Sjwahlig 		return;
41011b300de9Sjwahlig 	}
41021b300de9Sjwahlig 	/*
41031b300de9Sjwahlig 	 * find an unused key
41041b300de9Sjwahlig 	 */
41051b300de9Sjwahlig 	if (destructor == NULL)
41061b300de9Sjwahlig 		destructor = vsd_defaultdestructor;
41071b300de9Sjwahlig 
41081b300de9Sjwahlig 	for (i = 0; i < vsd_nkeys; ++i)
41091b300de9Sjwahlig 		if (vsd_destructor[i] == NULL)
41101b300de9Sjwahlig 			break;
41111b300de9Sjwahlig 
41121b300de9Sjwahlig 	/*
41131b300de9Sjwahlig 	 * if no unused keys, increase the size of the destructor array
41141b300de9Sjwahlig 	 */
41151b300de9Sjwahlig 	if (i == vsd_nkeys) {
41161b300de9Sjwahlig 		if ((nkeys = (vsd_nkeys << 1)) == 0)
41171b300de9Sjwahlig 			nkeys = 1;
41181b300de9Sjwahlig 		vsd_destructor =
41191b300de9Sjwahlig 		    (void (**)(void *))vsd_realloc((void *)vsd_destructor,
41201b300de9Sjwahlig 		    (size_t)(vsd_nkeys * sizeof (void (*)(void *))),
41211b300de9Sjwahlig 		    (size_t)(nkeys * sizeof (void (*)(void *))));
41221b300de9Sjwahlig 		vsd_nkeys = nkeys;
41231b300de9Sjwahlig 	}
41241b300de9Sjwahlig 
41251b300de9Sjwahlig 	/*
41261b300de9Sjwahlig 	 * allocate the next available unused key
41271b300de9Sjwahlig 	 */
41281b300de9Sjwahlig 	vsd_destructor[i] = destructor;
41291b300de9Sjwahlig 	*keyp = i + 1;
41301b300de9Sjwahlig 
41311b300de9Sjwahlig 	/* create vsd_list, if it doesn't exist */
41321b300de9Sjwahlig 	if (vsd_list == NULL) {
41331b300de9Sjwahlig 		vsd_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
41341b300de9Sjwahlig 		list_create(vsd_list, sizeof (struct vsd_node),
41351b300de9Sjwahlig 		    offsetof(struct vsd_node, vs_nodes));
41361b300de9Sjwahlig 	}
41371b300de9Sjwahlig 
41381b300de9Sjwahlig 	mutex_exit(&vsd_lock);
41391b300de9Sjwahlig }
41401b300de9Sjwahlig 
41411b300de9Sjwahlig /*
41421b300de9Sjwahlig  * Destroy a key
41431b300de9Sjwahlig  *
41441b300de9Sjwahlig  * Assumes that the caller is preventing vsd_set and vsd_get
41451b300de9Sjwahlig  * Locks out vsd_create, vsd_destroy, and vsd_free
41461b300de9Sjwahlig  * May free memory with lock held
41471b300de9Sjwahlig  */
41481b300de9Sjwahlig void
41491b300de9Sjwahlig vsd_destroy(uint_t *keyp)
41501b300de9Sjwahlig {
41511b300de9Sjwahlig 	uint_t key;
41521b300de9Sjwahlig 	struct vsd_node *vsd;
41531b300de9Sjwahlig 
41541b300de9Sjwahlig 	/*
41551b300de9Sjwahlig 	 * protect the key namespace and our destructor lists
41561b300de9Sjwahlig 	 */
41571b300de9Sjwahlig 	mutex_enter(&vsd_lock);
41581b300de9Sjwahlig 	key = *keyp;
41591b300de9Sjwahlig 	*keyp = 0;
41601b300de9Sjwahlig 
41611b300de9Sjwahlig 	ASSERT(key <= vsd_nkeys);
41621b300de9Sjwahlig 
41631b300de9Sjwahlig 	/*
41641b300de9Sjwahlig 	 * if the key is valid
41651b300de9Sjwahlig 	 */
41661b300de9Sjwahlig 	if (key != 0) {
41671b300de9Sjwahlig 		uint_t k = key - 1;
41681b300de9Sjwahlig 		/*
41691b300de9Sjwahlig 		 * for every vnode with VSD, call key's destructor
41701b300de9Sjwahlig 		 */
41711b300de9Sjwahlig 		for (vsd = list_head(vsd_list); vsd != NULL;
41721b300de9Sjwahlig 		    vsd = list_next(vsd_list, vsd)) {
41731b300de9Sjwahlig 			/*
41741b300de9Sjwahlig 			 * no VSD for key in this vnode
41751b300de9Sjwahlig 			 */
41761b300de9Sjwahlig 			if (key > vsd->vs_nkeys)
41771b300de9Sjwahlig 				continue;
41781b300de9Sjwahlig 			/*
41791b300de9Sjwahlig 			 * call destructor for key
41801b300de9Sjwahlig 			 */
41811b300de9Sjwahlig 			if (vsd->vs_value[k] && vsd_destructor[k])
41821b300de9Sjwahlig 				(*vsd_destructor[k])(vsd->vs_value[k]);
41831b300de9Sjwahlig 			/*
41841b300de9Sjwahlig 			 * reset value for key
41851b300de9Sjwahlig 			 */
41861b300de9Sjwahlig 			vsd->vs_value[k] = NULL;
41871b300de9Sjwahlig 		}
41881b300de9Sjwahlig 		/*
41891b300de9Sjwahlig 		 * actually free the key (NULL destructor == unused)
41901b300de9Sjwahlig 		 */
41911b300de9Sjwahlig 		vsd_destructor[k] = NULL;
41921b300de9Sjwahlig 	}
41931b300de9Sjwahlig 
41941b300de9Sjwahlig 	mutex_exit(&vsd_lock);
41951b300de9Sjwahlig }
41961b300de9Sjwahlig 
41971b300de9Sjwahlig /*
41981b300de9Sjwahlig  * Quickly return the per vnode value that was stored with the specified key
41991b300de9Sjwahlig  * Assumes the caller is protecting key from vsd_create and vsd_destroy
42001b300de9Sjwahlig  * Assumes the caller is holding v_lock to protect the vsd.
42011b300de9Sjwahlig  */
42021b300de9Sjwahlig void *
42031b300de9Sjwahlig vsd_get(vnode_t *vp, uint_t key)
42041b300de9Sjwahlig {
42051b300de9Sjwahlig 	struct vsd_node *vsd;
42061b300de9Sjwahlig 
42071b300de9Sjwahlig 	/*
42081b300de9Sjwahlig 	 * The caller needs to pass a valid vnode.
42091b300de9Sjwahlig 	 */
42101b300de9Sjwahlig 	ASSERT(vp != NULL);
42111b300de9Sjwahlig 	if (vp == NULL)
42121b300de9Sjwahlig 		return (NULL);
42131b300de9Sjwahlig 
42141b300de9Sjwahlig 	vsd = vp->v_vsd;
42151b300de9Sjwahlig 
42161b300de9Sjwahlig 	if (key && vsd != NULL && key <= vsd->vs_nkeys)
42171b300de9Sjwahlig 		return (vsd->vs_value[key - 1]);
42181b300de9Sjwahlig 	return (NULL);
42191b300de9Sjwahlig }
42201b300de9Sjwahlig 
42211b300de9Sjwahlig /*
42221b300de9Sjwahlig  * Set a per vnode value indexed with the specified key
42231b300de9Sjwahlig  * Assumes the caller is holding v_lock to protect the vsd.
42241b300de9Sjwahlig  */
42251b300de9Sjwahlig int
42261b300de9Sjwahlig vsd_set(vnode_t *vp, uint_t key, void *value)
42271b300de9Sjwahlig {
42281b300de9Sjwahlig 	struct vsd_node *vsd = vp->v_vsd;
42291b300de9Sjwahlig 
42301b300de9Sjwahlig 	if (key == 0)
42311b300de9Sjwahlig 		return (EINVAL);
42321b300de9Sjwahlig 	if (vsd == NULL)
42331b300de9Sjwahlig 		vsd = vp->v_vsd = kmem_zalloc(sizeof (*vsd), KM_SLEEP);
42341b300de9Sjwahlig 
42351b300de9Sjwahlig 	/*
42361b300de9Sjwahlig 	 * If the vsd was just allocated, vs_nkeys will be 0, so the following
42371b300de9Sjwahlig 	 * code won't happen and we will continue down and allocate space for
42381b300de9Sjwahlig 	 * the vs_value array.
42391b300de9Sjwahlig 	 * If the caller is replacing one value with another, then it is up
42401b300de9Sjwahlig 	 * to the caller to free/rele/destroy the previous value (if needed).
42411b300de9Sjwahlig 	 */
42421b300de9Sjwahlig 	if (key <= vsd->vs_nkeys) {
42431b300de9Sjwahlig 		vsd->vs_value[key - 1] = value;
42441b300de9Sjwahlig 		return (0);
42451b300de9Sjwahlig 	}
42461b300de9Sjwahlig 
42471b300de9Sjwahlig 	ASSERT(key <= vsd_nkeys);
42481b300de9Sjwahlig 
42491b300de9Sjwahlig 	if (vsd->vs_nkeys == 0) {
42501b300de9Sjwahlig 		mutex_enter(&vsd_lock);	/* lock out vsd_destroy() */
42511b300de9Sjwahlig 		/*
42521b300de9Sjwahlig 		 * Link onto list of all VSD nodes.
42531b300de9Sjwahlig 		 */
42541b300de9Sjwahlig 		list_insert_head(vsd_list, vsd);
42551b300de9Sjwahlig 		mutex_exit(&vsd_lock);
42561b300de9Sjwahlig 	}
42571b300de9Sjwahlig 
42581b300de9Sjwahlig 	/*
42591b300de9Sjwahlig 	 * Allocate vnode local storage and set the value for key
42601b300de9Sjwahlig 	 */
42611b300de9Sjwahlig 	vsd->vs_value = vsd_realloc(vsd->vs_value,
42621b300de9Sjwahlig 	    vsd->vs_nkeys * sizeof (void *),
42631b300de9Sjwahlig 	    key * sizeof (void *));
42641b300de9Sjwahlig 	vsd->vs_nkeys = key;
42651b300de9Sjwahlig 	vsd->vs_value[key - 1] = value;
42661b300de9Sjwahlig 
42671b300de9Sjwahlig 	return (0);
42681b300de9Sjwahlig }
42691b300de9Sjwahlig 
42701b300de9Sjwahlig /*
42711b300de9Sjwahlig  * Called from vn_free() to run the destructor function for each vsd
42721b300de9Sjwahlig  *	Locks out vsd_create and vsd_destroy
42731b300de9Sjwahlig  *	Assumes that the destructor *DOES NOT* use vsd
42741b300de9Sjwahlig  */
42751b300de9Sjwahlig void
42761b300de9Sjwahlig vsd_free(vnode_t *vp)
42771b300de9Sjwahlig {
42781b300de9Sjwahlig 	int i;
42791b300de9Sjwahlig 	struct vsd_node *vsd = vp->v_vsd;
42801b300de9Sjwahlig 
42811b300de9Sjwahlig 	if (vsd == NULL)
42821b300de9Sjwahlig 		return;
42831b300de9Sjwahlig 
42841b300de9Sjwahlig 	if (vsd->vs_nkeys == 0) {
42851b300de9Sjwahlig 		kmem_free(vsd, sizeof (*vsd));
42861b300de9Sjwahlig 		vp->v_vsd = NULL;
42871b300de9Sjwahlig 		return;
42881b300de9Sjwahlig 	}
42891b300de9Sjwahlig 
42901b300de9Sjwahlig 	/*
42911b300de9Sjwahlig 	 * lock out vsd_create and vsd_destroy, call
42921b300de9Sjwahlig 	 * the destructor, and mark the value as destroyed.
42931b300de9Sjwahlig 	 */
42941b300de9Sjwahlig 	mutex_enter(&vsd_lock);
42951b300de9Sjwahlig 
42961b300de9Sjwahlig 	for (i = 0; i < vsd->vs_nkeys; i++) {
42971b300de9Sjwahlig 		if (vsd->vs_value[i] && vsd_destructor[i])
42981b300de9Sjwahlig 			(*vsd_destructor[i])(vsd->vs_value[i]);
42991b300de9Sjwahlig 		vsd->vs_value[i] = NULL;
43001b300de9Sjwahlig 	}
43011b300de9Sjwahlig 
43021b300de9Sjwahlig 	/*
43031b300de9Sjwahlig 	 * remove from linked list of VSD nodes
43041b300de9Sjwahlig 	 */
43051b300de9Sjwahlig 	list_remove(vsd_list, vsd);
43061b300de9Sjwahlig 
43071b300de9Sjwahlig 	mutex_exit(&vsd_lock);
43081b300de9Sjwahlig 
43091b300de9Sjwahlig 	/*
43101b300de9Sjwahlig 	 * free up the VSD
43111b300de9Sjwahlig 	 */
43121b300de9Sjwahlig 	kmem_free(vsd->vs_value, vsd->vs_nkeys * sizeof (void *));
43131b300de9Sjwahlig 	kmem_free(vsd, sizeof (struct vsd_node));
43141b300de9Sjwahlig 	vp->v_vsd = NULL;
43151b300de9Sjwahlig }
43161b300de9Sjwahlig 
43171b300de9Sjwahlig /*
43181b300de9Sjwahlig  * realloc
43191b300de9Sjwahlig  */
43201b300de9Sjwahlig static void *
43211b300de9Sjwahlig vsd_realloc(void *old, size_t osize, size_t nsize)
43221b300de9Sjwahlig {
43231b300de9Sjwahlig 	void *new;
43241b300de9Sjwahlig 
43251b300de9Sjwahlig 	new = kmem_zalloc(nsize, KM_SLEEP);
43261b300de9Sjwahlig 	if (old) {
43271b300de9Sjwahlig 		bcopy(old, new, osize);
43281b300de9Sjwahlig 		kmem_free(old, osize);
43291b300de9Sjwahlig 	}
43301b300de9Sjwahlig 	return (new);
43311b300de9Sjwahlig }
4332