xref: /titanic_53/usr/src/uts/common/fs/vnode.c (revision 72102e7461c97dc268d21d9dd8f02da45f174acd)
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  */
21794f0adbSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23cb15d5d9SPeter Rival  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24*72102e74SBryan Cantrill  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
437c478bd9Sstevel@tonic-gate #include <sys/errno.h>
447c478bd9Sstevel@tonic-gate #include <sys/cred.h>
457c478bd9Sstevel@tonic-gate #include <sys/user.h>
467c478bd9Sstevel@tonic-gate #include <sys/uio.h>
477c478bd9Sstevel@tonic-gate #include <sys/file.h>
487c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
497c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
50aa59c4cbSrsb #include <sys/vfs_opreg.h>
517c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
527c478bd9Sstevel@tonic-gate #include <sys/rwstlock.h>
537c478bd9Sstevel@tonic-gate #include <sys/fem.h>
547c478bd9Sstevel@tonic-gate #include <sys/stat.h>
557c478bd9Sstevel@tonic-gate #include <sys/mode.h>
567c478bd9Sstevel@tonic-gate #include <sys/conf.h>
577c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
587c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
597c478bd9Sstevel@tonic-gate #include <sys/systm.h>
607c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
617c478bd9Sstevel@tonic-gate #include <sys/debug.h>
627c478bd9Sstevel@tonic-gate #include <c2/audit.h>
637c478bd9Sstevel@tonic-gate #include <sys/acl.h>
647c478bd9Sstevel@tonic-gate #include <sys/nbmlock.h>
657c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
667c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
679d3574bfSNeil Perrin #include <sys/taskq.h>
687a286c47SDai Ngo #include <fs/fs_reparse.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 /*
1117a286c47SDai Ngo  * forward declarations for reparse point functions
1127a286c47SDai Ngo  */
1137a286c47SDai Ngo static int fs_reparse_mark(char *target, vattr_t *vap, xvattr_t *xvattr);
1147a286c47SDai Ngo 
1157a286c47SDai Ngo /*
1161b300de9Sjwahlig  * VSD -- VNODE SPECIFIC DATA
1171b300de9Sjwahlig  * The v_data pointer is typically used by a file system to store a
1181b300de9Sjwahlig  * pointer to the file system's private node (e.g. ufs inode, nfs rnode).
1191b300de9Sjwahlig  * However, there are times when additional project private data needs
1201b300de9Sjwahlig  * to be stored separately from the data (node) pointed to by v_data.
1211b300de9Sjwahlig  * This additional data could be stored by the file system itself or
1221b300de9Sjwahlig  * by a completely different kernel entity.  VSD provides a way for
1231b300de9Sjwahlig  * callers to obtain a key and store a pointer to private data associated
1241b300de9Sjwahlig  * with a vnode.
1251b300de9Sjwahlig  *
126d216dff5SRobert Mastors  * Callers are responsible for protecting the vsd by holding v_vsd_lock
1271b300de9Sjwahlig  * for calls to vsd_set() and vsd_get().
1281b300de9Sjwahlig  */
1291b300de9Sjwahlig 
1301b300de9Sjwahlig /*
1311b300de9Sjwahlig  * vsd_lock protects:
1321b300de9Sjwahlig  *   vsd_nkeys - creation and deletion of vsd keys
1331b300de9Sjwahlig  *   vsd_list - insertion and deletion of vsd_node in the vsd_list
1341b300de9Sjwahlig  *   vsd_destructor - adding and removing destructors to the list
1351b300de9Sjwahlig  */
1361b300de9Sjwahlig static kmutex_t		vsd_lock;
1371b300de9Sjwahlig static uint_t		vsd_nkeys;	 /* size of destructor array */
1381b300de9Sjwahlig /* list of vsd_node's */
1391b300de9Sjwahlig static list_t *vsd_list = NULL;
1401b300de9Sjwahlig /* per-key destructor funcs */
1411b300de9Sjwahlig static void 		(**vsd_destructor)(void *);
1421b300de9Sjwahlig 
1431b300de9Sjwahlig /*
1445a59a8b3Srsb  * The following is the common set of actions needed to update the
1455a59a8b3Srsb  * vopstats structure from a vnode op.  Both VOPSTATS_UPDATE() and
1465a59a8b3Srsb  * VOPSTATS_UPDATE_IO() do almost the same thing, except for the
1475a59a8b3Srsb  * recording of the bytes transferred.  Since the code is similar
1485a59a8b3Srsb  * but small, it is nearly a duplicate.  Consequently any changes
1495a59a8b3Srsb  * to one may need to be reflected in the other.
1505a59a8b3Srsb  * Rundown of the variables:
1515a59a8b3Srsb  * vp - Pointer to the vnode
1525a59a8b3Srsb  * counter - Partial name structure member to update in vopstats for counts
1535a59a8b3Srsb  * bytecounter - Partial name structure member to update in vopstats for bytes
1545a59a8b3Srsb  * bytesval - Value to update in vopstats for bytes
1555a59a8b3Srsb  * fstype - Index into vsanchor_fstype[], same as index into vfssw[]
1565a59a8b3Srsb  * vsp - Pointer to vopstats structure (either in vfs or vsanchor_fstype[i])
1575a59a8b3Srsb  */
1585a59a8b3Srsb 
1595a59a8b3Srsb #define	VOPSTATS_UPDATE(vp, counter) {					\
1605a59a8b3Srsb 	vfs_t *vfsp = (vp)->v_vfsp;					\
161ddfcde86Srsb 	if (vfsp && vfsp->vfs_implp &&					\
162ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {	\
1635a59a8b3Srsb 		vopstats_t *vsp = &vfsp->vfs_vopstats;			\
1642bb1cb30Sbmc 		uint64_t *stataddr = &(vsp->n##counter.value.ui64);	\
1652bb1cb30Sbmc 		extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
1662bb1cb30Sbmc 		    size_t, uint64_t *);				\
1672bb1cb30Sbmc 		__dtrace_probe___fsinfo_##counter(vp, 0, stataddr);	\
1682bb1cb30Sbmc 		(*stataddr)++;						\
1695a59a8b3Srsb 		if ((vsp = vfsp->vfs_fstypevsp) != NULL) {		\
1702bb1cb30Sbmc 			vsp->n##counter.value.ui64++;			\
1715a59a8b3Srsb 		}							\
1725a59a8b3Srsb 	}								\
1735a59a8b3Srsb }
1745a59a8b3Srsb 
1755a59a8b3Srsb #define	VOPSTATS_UPDATE_IO(vp, counter, bytecounter, bytesval) {	\
1765a59a8b3Srsb 	vfs_t *vfsp = (vp)->v_vfsp;					\
177ddfcde86Srsb 	if (vfsp && vfsp->vfs_implp &&					\
178ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {	\
1795a59a8b3Srsb 		vopstats_t *vsp = &vfsp->vfs_vopstats;			\
1802bb1cb30Sbmc 		uint64_t *stataddr = &(vsp->n##counter.value.ui64);	\
1812bb1cb30Sbmc 		extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
1822bb1cb30Sbmc 		    size_t, uint64_t *);				\
1832bb1cb30Sbmc 		__dtrace_probe___fsinfo_##counter(vp, bytesval, stataddr); \
1842bb1cb30Sbmc 		(*stataddr)++;						\
1855a59a8b3Srsb 		vsp->bytecounter.value.ui64 += bytesval;		\
1865a59a8b3Srsb 		if ((vsp = vfsp->vfs_fstypevsp) != NULL) {		\
1872bb1cb30Sbmc 			vsp->n##counter.value.ui64++;			\
1885a59a8b3Srsb 			vsp->bytecounter.value.ui64 += bytesval;	\
1895a59a8b3Srsb 		}							\
1905a59a8b3Srsb 	}								\
1915a59a8b3Srsb }
1925a59a8b3Srsb 
1935a59a8b3Srsb /*
194f48205beScasper  * If the filesystem does not support XIDs map credential
195f48205beScasper  * If the vfsp is NULL, perhaps we should also map?
196f48205beScasper  */
197f48205beScasper #define	VOPXID_MAP_CR(vp, cr)	{					\
198f48205beScasper 	vfs_t *vfsp = (vp)->v_vfsp;					\
199f48205beScasper 	if (vfsp != NULL && (vfsp->vfs_flag & VFS_XID) == 0)		\
200f48205beScasper 		cr = crgetmapped(cr);					\
201f48205beScasper 	}
202f48205beScasper 
203f48205beScasper /*
2047c478bd9Sstevel@tonic-gate  * Convert stat(2) formats to vnode types and vice versa.  (Knows about
2057c478bd9Sstevel@tonic-gate  * numerical order of S_IFMT and vnode types.)
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate enum vtype iftovt_tab[] = {
2087c478bd9Sstevel@tonic-gate 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
2097c478bd9Sstevel@tonic-gate 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
2107c478bd9Sstevel@tonic-gate };
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate ushort_t vttoif_tab[] = {
2137c478bd9Sstevel@tonic-gate 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO,
2147c478bd9Sstevel@tonic-gate 	S_IFDOOR, 0, S_IFSOCK, S_IFPORT, 0
2157c478bd9Sstevel@tonic-gate };
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * The system vnode cache.
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate kmem_cache_t *vn_cache;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * Vnode operations vector.
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate static const fs_operation_trans_def_t vn_ops_table[] = {
2297c478bd9Sstevel@tonic-gate 	VOPNAME_OPEN, offsetof(struct vnodeops, vop_open),
2307c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	VOPNAME_CLOSE, offsetof(struct vnodeops, vop_close),
2337c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	VOPNAME_READ, offsetof(struct vnodeops, vop_read),
2367c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	VOPNAME_WRITE, offsetof(struct vnodeops, vop_write),
2397c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	VOPNAME_IOCTL, offsetof(struct vnodeops, vop_ioctl),
2427c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	VOPNAME_SETFL, offsetof(struct vnodeops, vop_setfl),
2457c478bd9Sstevel@tonic-gate 	    fs_setfl, fs_nosys,
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	VOPNAME_GETATTR, offsetof(struct vnodeops, vop_getattr),
2487c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	VOPNAME_SETATTR, offsetof(struct vnodeops, vop_setattr),
2517c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	VOPNAME_ACCESS, offsetof(struct vnodeops, vop_access),
2547c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	VOPNAME_LOOKUP, offsetof(struct vnodeops, vop_lookup),
2577c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	VOPNAME_CREATE, offsetof(struct vnodeops, vop_create),
2607c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	VOPNAME_REMOVE, offsetof(struct vnodeops, vop_remove),
2637c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	VOPNAME_LINK, offsetof(struct vnodeops, vop_link),
2667c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	VOPNAME_RENAME, offsetof(struct vnodeops, vop_rename),
2697c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	VOPNAME_MKDIR, offsetof(struct vnodeops, vop_mkdir),
2727c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	VOPNAME_RMDIR, offsetof(struct vnodeops, vop_rmdir),
2757c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	VOPNAME_READDIR, offsetof(struct vnodeops, vop_readdir),
2787c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	VOPNAME_SYMLINK, offsetof(struct vnodeops, vop_symlink),
2817c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	VOPNAME_READLINK, offsetof(struct vnodeops, vop_readlink),
2847c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	VOPNAME_FSYNC, offsetof(struct vnodeops, vop_fsync),
2877c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	VOPNAME_INACTIVE, offsetof(struct vnodeops, vop_inactive),
2907c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	VOPNAME_FID, offsetof(struct vnodeops, vop_fid),
2937c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	VOPNAME_RWLOCK, offsetof(struct vnodeops, vop_rwlock),
2967c478bd9Sstevel@tonic-gate 	    fs_rwlock, fs_rwlock,
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	VOPNAME_RWUNLOCK, offsetof(struct vnodeops, vop_rwunlock),
2997c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_rwunlock,
3007c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_rwunlock,	/* no errors allowed */
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	VOPNAME_SEEK, offsetof(struct vnodeops, vop_seek),
3037c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	VOPNAME_CMP, offsetof(struct vnodeops, vop_cmp),
3067c478bd9Sstevel@tonic-gate 	    fs_cmp, fs_cmp,		/* no errors allowed */
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	VOPNAME_FRLOCK, offsetof(struct vnodeops, vop_frlock),
3097c478bd9Sstevel@tonic-gate 	    fs_frlock, fs_nosys,
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	VOPNAME_SPACE, offsetof(struct vnodeops, vop_space),
3127c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	VOPNAME_REALVP, offsetof(struct vnodeops, vop_realvp),
3157c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	VOPNAME_GETPAGE, offsetof(struct vnodeops, vop_getpage),
3187c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	VOPNAME_PUTPAGE, offsetof(struct vnodeops, vop_putpage),
3217c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	VOPNAME_MAP, offsetof(struct vnodeops, vop_map),
3247c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_map,
3257c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_map,
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	VOPNAME_ADDMAP, offsetof(struct vnodeops, vop_addmap),
3287c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_addmap,
3297c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_addmap,
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	VOPNAME_DELMAP, offsetof(struct vnodeops, vop_delmap),
3327c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	VOPNAME_POLL, offsetof(struct vnodeops, vop_poll),
3357c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_poll, (fs_generic_func_p) fs_nosys_poll,
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	VOPNAME_DUMP, offsetof(struct vnodeops, vop_dump),
3387c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	VOPNAME_PATHCONF, offsetof(struct vnodeops, vop_pathconf),
3417c478bd9Sstevel@tonic-gate 	    fs_pathconf, fs_nosys,
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	VOPNAME_PAGEIO, offsetof(struct vnodeops, vop_pageio),
3447c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	VOPNAME_DUMPCTL, offsetof(struct vnodeops, vop_dumpctl),
3477c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	VOPNAME_DISPOSE, offsetof(struct vnodeops, vop_dispose),
3507c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_dispose,
3517c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nodispose,
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	VOPNAME_SETSECATTR, offsetof(struct vnodeops, vop_setsecattr),
3547c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	VOPNAME_GETSECATTR, offsetof(struct vnodeops, vop_getsecattr),
3577c478bd9Sstevel@tonic-gate 	    fs_fab_acl, fs_nosys,
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	VOPNAME_SHRLOCK, offsetof(struct vnodeops, vop_shrlock),
3607c478bd9Sstevel@tonic-gate 	    fs_shrlock, fs_nosys,
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	VOPNAME_VNEVENT, offsetof(struct vnodeops, vop_vnevent),
3637c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_vnevent_nosupport,
3647c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_vnevent_nosupport,
3657c478bd9Sstevel@tonic-gate 
366c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	VOPNAME_REQZCBUF, offsetof(struct vnodeops, vop_reqzcbuf),
367c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    fs_nosys, fs_nosys,
368c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
369c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	VOPNAME_RETZCBUF, offsetof(struct vnodeops, vop_retzcbuf),
370c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    fs_nosys, fs_nosys,
371c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
3727c478bd9Sstevel@tonic-gate 	NULL, 0, NULL, NULL
3737c478bd9Sstevel@tonic-gate };
3747c478bd9Sstevel@tonic-gate 
375da6c28aaSamw /* Extensible attribute (xva) routines. */
376da6c28aaSamw 
377da6c28aaSamw /*
378da6c28aaSamw  * Zero out the structure, set the size of the requested/returned bitmaps,
379da6c28aaSamw  * set AT_XVATTR in the embedded vattr_t's va_mask, and set up the pointer
380da6c28aaSamw  * to the returned attributes array.
381da6c28aaSamw  */
382da6c28aaSamw void
383da6c28aaSamw xva_init(xvattr_t *xvap)
384da6c28aaSamw {
385da6c28aaSamw 	bzero(xvap, sizeof (xvattr_t));
386da6c28aaSamw 	xvap->xva_mapsize = XVA_MAPSIZE;
387da6c28aaSamw 	xvap->xva_magic = XVA_MAGIC;
388da6c28aaSamw 	xvap->xva_vattr.va_mask = AT_XVATTR;
389da6c28aaSamw 	xvap->xva_rtnattrmapp = &(xvap->xva_rtnattrmap)[0];
390da6c28aaSamw }
391da6c28aaSamw 
392da6c28aaSamw /*
393da6c28aaSamw  * If AT_XVATTR is set, returns a pointer to the embedded xoptattr_t
394da6c28aaSamw  * structure.  Otherwise, returns NULL.
395da6c28aaSamw  */
396da6c28aaSamw xoptattr_t *
397da6c28aaSamw xva_getxoptattr(xvattr_t *xvap)
398da6c28aaSamw {
399da6c28aaSamw 	xoptattr_t *xoap = NULL;
400da6c28aaSamw 	if (xvap->xva_vattr.va_mask & AT_XVATTR)
401da6c28aaSamw 		xoap = &xvap->xva_xoptattrs;
402da6c28aaSamw 	return (xoap);
403da6c28aaSamw }
404da6c28aaSamw 
4055a59a8b3Srsb /*
4065a59a8b3Srsb  * Used by the AVL routines to compare two vsk_anchor_t structures in the tree.
4075a59a8b3Srsb  * We use the f_fsid reported by VFS_STATVFS() since we use that for the
4085a59a8b3Srsb  * kstat name.
4095a59a8b3Srsb  */
4105a59a8b3Srsb static int
4115a59a8b3Srsb vska_compar(const void *n1, const void *n2)
4125a59a8b3Srsb {
4135a59a8b3Srsb 	int ret;
4145a59a8b3Srsb 	ulong_t p1 = ((vsk_anchor_t *)n1)->vsk_fsid;
4155a59a8b3Srsb 	ulong_t p2 = ((vsk_anchor_t *)n2)->vsk_fsid;
4165a59a8b3Srsb 
4175a59a8b3Srsb 	if (p1 < p2) {
4185a59a8b3Srsb 		ret = -1;
4195a59a8b3Srsb 	} else if (p1 > p2) {
4205a59a8b3Srsb 		ret = 1;
4215a59a8b3Srsb 	} else {
4225a59a8b3Srsb 		ret = 0;
4235a59a8b3Srsb 	}
4245a59a8b3Srsb 
4255a59a8b3Srsb 	return (ret);
4265a59a8b3Srsb }
4275a59a8b3Srsb 
4285a59a8b3Srsb /*
4295a59a8b3Srsb  * Used to create a single template which will be bcopy()ed to a newly
4305a59a8b3Srsb  * allocated vsanchor_combo_t structure in new_vsanchor(), below.
4315a59a8b3Srsb  */
4325a59a8b3Srsb static vopstats_t *
4335a59a8b3Srsb create_vopstats_template()
4345a59a8b3Srsb {
4355a59a8b3Srsb 	vopstats_t		*vsp;
4365a59a8b3Srsb 
4375a59a8b3Srsb 	vsp = kmem_alloc(sizeof (vopstats_t), KM_SLEEP);
4385a59a8b3Srsb 	bzero(vsp, sizeof (*vsp));	/* Start fresh */
4395a59a8b3Srsb 
4405a59a8b3Srsb 	/* VOP_OPEN */
4415a59a8b3Srsb 	kstat_named_init(&vsp->nopen, "nopen", KSTAT_DATA_UINT64);
4425a59a8b3Srsb 	/* VOP_CLOSE */
4435a59a8b3Srsb 	kstat_named_init(&vsp->nclose, "nclose", KSTAT_DATA_UINT64);
4445a59a8b3Srsb 	/* VOP_READ I/O */
4455a59a8b3Srsb 	kstat_named_init(&vsp->nread, "nread", KSTAT_DATA_UINT64);
4465a59a8b3Srsb 	kstat_named_init(&vsp->read_bytes, "read_bytes", KSTAT_DATA_UINT64);
4475a59a8b3Srsb 	/* VOP_WRITE I/O */
4485a59a8b3Srsb 	kstat_named_init(&vsp->nwrite, "nwrite", KSTAT_DATA_UINT64);
4495a59a8b3Srsb 	kstat_named_init(&vsp->write_bytes, "write_bytes", KSTAT_DATA_UINT64);
4505a59a8b3Srsb 	/* VOP_IOCTL */
4515a59a8b3Srsb 	kstat_named_init(&vsp->nioctl, "nioctl", KSTAT_DATA_UINT64);
4525a59a8b3Srsb 	/* VOP_SETFL */
4535a59a8b3Srsb 	kstat_named_init(&vsp->nsetfl, "nsetfl", KSTAT_DATA_UINT64);
4545a59a8b3Srsb 	/* VOP_GETATTR */
4555a59a8b3Srsb 	kstat_named_init(&vsp->ngetattr, "ngetattr", KSTAT_DATA_UINT64);
4565a59a8b3Srsb 	/* VOP_SETATTR */
4575a59a8b3Srsb 	kstat_named_init(&vsp->nsetattr, "nsetattr", KSTAT_DATA_UINT64);
4585a59a8b3Srsb 	/* VOP_ACCESS */
4595a59a8b3Srsb 	kstat_named_init(&vsp->naccess, "naccess", KSTAT_DATA_UINT64);
4605a59a8b3Srsb 	/* VOP_LOOKUP */
4615a59a8b3Srsb 	kstat_named_init(&vsp->nlookup, "nlookup", KSTAT_DATA_UINT64);
4625a59a8b3Srsb 	/* VOP_CREATE */
4635a59a8b3Srsb 	kstat_named_init(&vsp->ncreate, "ncreate", KSTAT_DATA_UINT64);
4645a59a8b3Srsb 	/* VOP_REMOVE */
4655a59a8b3Srsb 	kstat_named_init(&vsp->nremove, "nremove", KSTAT_DATA_UINT64);
4665a59a8b3Srsb 	/* VOP_LINK */
4675a59a8b3Srsb 	kstat_named_init(&vsp->nlink, "nlink", KSTAT_DATA_UINT64);
4685a59a8b3Srsb 	/* VOP_RENAME */
4695a59a8b3Srsb 	kstat_named_init(&vsp->nrename, "nrename", KSTAT_DATA_UINT64);
4705a59a8b3Srsb 	/* VOP_MKDIR */
4715a59a8b3Srsb 	kstat_named_init(&vsp->nmkdir, "nmkdir", KSTAT_DATA_UINT64);
4725a59a8b3Srsb 	/* VOP_RMDIR */
4735a59a8b3Srsb 	kstat_named_init(&vsp->nrmdir, "nrmdir", KSTAT_DATA_UINT64);
4745a59a8b3Srsb 	/* VOP_READDIR I/O */
4755a59a8b3Srsb 	kstat_named_init(&vsp->nreaddir, "nreaddir", KSTAT_DATA_UINT64);
4765a59a8b3Srsb 	kstat_named_init(&vsp->readdir_bytes, "readdir_bytes",
4775a59a8b3Srsb 	    KSTAT_DATA_UINT64);
4785a59a8b3Srsb 	/* VOP_SYMLINK */
4795a59a8b3Srsb 	kstat_named_init(&vsp->nsymlink, "nsymlink", KSTAT_DATA_UINT64);
4805a59a8b3Srsb 	/* VOP_READLINK */
4815a59a8b3Srsb 	kstat_named_init(&vsp->nreadlink, "nreadlink", KSTAT_DATA_UINT64);
4825a59a8b3Srsb 	/* VOP_FSYNC */
4835a59a8b3Srsb 	kstat_named_init(&vsp->nfsync, "nfsync", KSTAT_DATA_UINT64);
4845a59a8b3Srsb 	/* VOP_INACTIVE */
4855a59a8b3Srsb 	kstat_named_init(&vsp->ninactive, "ninactive", KSTAT_DATA_UINT64);
4865a59a8b3Srsb 	/* VOP_FID */
4875a59a8b3Srsb 	kstat_named_init(&vsp->nfid, "nfid", KSTAT_DATA_UINT64);
4885a59a8b3Srsb 	/* VOP_RWLOCK */
4895a59a8b3Srsb 	kstat_named_init(&vsp->nrwlock, "nrwlock", KSTAT_DATA_UINT64);
4905a59a8b3Srsb 	/* VOP_RWUNLOCK */
4915a59a8b3Srsb 	kstat_named_init(&vsp->nrwunlock, "nrwunlock", KSTAT_DATA_UINT64);
4925a59a8b3Srsb 	/* VOP_SEEK */
4935a59a8b3Srsb 	kstat_named_init(&vsp->nseek, "nseek", KSTAT_DATA_UINT64);
4945a59a8b3Srsb 	/* VOP_CMP */
4955a59a8b3Srsb 	kstat_named_init(&vsp->ncmp, "ncmp", KSTAT_DATA_UINT64);
4965a59a8b3Srsb 	/* VOP_FRLOCK */
4975a59a8b3Srsb 	kstat_named_init(&vsp->nfrlock, "nfrlock", KSTAT_DATA_UINT64);
4985a59a8b3Srsb 	/* VOP_SPACE */
4995a59a8b3Srsb 	kstat_named_init(&vsp->nspace, "nspace", KSTAT_DATA_UINT64);
5005a59a8b3Srsb 	/* VOP_REALVP */
5015a59a8b3Srsb 	kstat_named_init(&vsp->nrealvp, "nrealvp", KSTAT_DATA_UINT64);
5025a59a8b3Srsb 	/* VOP_GETPAGE */
5035a59a8b3Srsb 	kstat_named_init(&vsp->ngetpage, "ngetpage", KSTAT_DATA_UINT64);
5045a59a8b3Srsb 	/* VOP_PUTPAGE */
5055a59a8b3Srsb 	kstat_named_init(&vsp->nputpage, "nputpage", KSTAT_DATA_UINT64);
5065a59a8b3Srsb 	/* VOP_MAP */
5075a59a8b3Srsb 	kstat_named_init(&vsp->nmap, "nmap", KSTAT_DATA_UINT64);
5085a59a8b3Srsb 	/* VOP_ADDMAP */
5095a59a8b3Srsb 	kstat_named_init(&vsp->naddmap, "naddmap", KSTAT_DATA_UINT64);
5105a59a8b3Srsb 	/* VOP_DELMAP */
5115a59a8b3Srsb 	kstat_named_init(&vsp->ndelmap, "ndelmap", KSTAT_DATA_UINT64);
5125a59a8b3Srsb 	/* VOP_POLL */
5135a59a8b3Srsb 	kstat_named_init(&vsp->npoll, "npoll", KSTAT_DATA_UINT64);
5145a59a8b3Srsb 	/* VOP_DUMP */
5155a59a8b3Srsb 	kstat_named_init(&vsp->ndump, "ndump", KSTAT_DATA_UINT64);
5165a59a8b3Srsb 	/* VOP_PATHCONF */
5175a59a8b3Srsb 	kstat_named_init(&vsp->npathconf, "npathconf", KSTAT_DATA_UINT64);
5185a59a8b3Srsb 	/* VOP_PAGEIO */
5195a59a8b3Srsb 	kstat_named_init(&vsp->npageio, "npageio", KSTAT_DATA_UINT64);
5205a59a8b3Srsb 	/* VOP_DUMPCTL */
5215a59a8b3Srsb 	kstat_named_init(&vsp->ndumpctl, "ndumpctl", KSTAT_DATA_UINT64);
5225a59a8b3Srsb 	/* VOP_DISPOSE */
5235a59a8b3Srsb 	kstat_named_init(&vsp->ndispose, "ndispose", KSTAT_DATA_UINT64);
5245a59a8b3Srsb 	/* VOP_SETSECATTR */
5255a59a8b3Srsb 	kstat_named_init(&vsp->nsetsecattr, "nsetsecattr", KSTAT_DATA_UINT64);
5265a59a8b3Srsb 	/* VOP_GETSECATTR */
5275a59a8b3Srsb 	kstat_named_init(&vsp->ngetsecattr, "ngetsecattr", KSTAT_DATA_UINT64);
5285a59a8b3Srsb 	/* VOP_SHRLOCK */
5295a59a8b3Srsb 	kstat_named_init(&vsp->nshrlock, "nshrlock", KSTAT_DATA_UINT64);
5305a59a8b3Srsb 	/* VOP_VNEVENT */
5315a59a8b3Srsb 	kstat_named_init(&vsp->nvnevent, "nvnevent", KSTAT_DATA_UINT64);
532c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	/* VOP_REQZCBUF */
533c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kstat_named_init(&vsp->nreqzcbuf, "nreqzcbuf", KSTAT_DATA_UINT64);
534c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	/* VOP_RETZCBUF */
535c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kstat_named_init(&vsp->nretzcbuf, "nretzcbuf", KSTAT_DATA_UINT64);
5365a59a8b3Srsb 
5375a59a8b3Srsb 	return (vsp);
5385a59a8b3Srsb }
5395a59a8b3Srsb 
5405a59a8b3Srsb /*
5415a59a8b3Srsb  * Creates a kstat structure associated with a vopstats structure.
5425a59a8b3Srsb  */
5435a59a8b3Srsb kstat_t *
5445a59a8b3Srsb new_vskstat(char *ksname, vopstats_t *vsp)
5455a59a8b3Srsb {
5465a59a8b3Srsb 	kstat_t		*ksp;
5475a59a8b3Srsb 
5485a59a8b3Srsb 	if (!vopstats_enabled) {
5495a59a8b3Srsb 		return (NULL);
5505a59a8b3Srsb 	}
5515a59a8b3Srsb 
5525a59a8b3Srsb 	ksp = kstat_create("unix", 0, ksname, "misc", KSTAT_TYPE_NAMED,
5535a59a8b3Srsb 	    sizeof (vopstats_t)/sizeof (kstat_named_t),
5545a59a8b3Srsb 	    KSTAT_FLAG_VIRTUAL|KSTAT_FLAG_WRITABLE);
5555a59a8b3Srsb 	if (ksp) {
5565a59a8b3Srsb 		ksp->ks_data = vsp;
5575a59a8b3Srsb 		kstat_install(ksp);
5585a59a8b3Srsb 	}
5595a59a8b3Srsb 
5605a59a8b3Srsb 	return (ksp);
5615a59a8b3Srsb }
5625a59a8b3Srsb 
5635a59a8b3Srsb /*
5645a59a8b3Srsb  * Called from vfsinit() to initialize the support mechanisms for vopstats
5655a59a8b3Srsb  */
5665a59a8b3Srsb void
5675a59a8b3Srsb vopstats_startup()
5685a59a8b3Srsb {
5695a59a8b3Srsb 	if (!vopstats_enabled)
5705a59a8b3Srsb 		return;
5715a59a8b3Srsb 
5725a59a8b3Srsb 	/*
5735a59a8b3Srsb 	 * Creates the AVL tree which holds per-vfs vopstat anchors.  This
5745a59a8b3Srsb 	 * is necessary since we need to check if a kstat exists before we
5755a59a8b3Srsb 	 * attempt to create it.  Also, initialize its lock.
5765a59a8b3Srsb 	 */
5775a59a8b3Srsb 	avl_create(&vskstat_tree, vska_compar, sizeof (vsk_anchor_t),
5785a59a8b3Srsb 	    offsetof(vsk_anchor_t, vsk_node));
5795a59a8b3Srsb 	mutex_init(&vskstat_tree_lock, NULL, MUTEX_DEFAULT, NULL);
5805a59a8b3Srsb 
5815a59a8b3Srsb 	vsk_anchor_cache = kmem_cache_create("vsk_anchor_cache",
5825a59a8b3Srsb 	    sizeof (vsk_anchor_t), sizeof (uintptr_t), NULL, NULL, NULL,
5835a59a8b3Srsb 	    NULL, NULL, 0);
5845a59a8b3Srsb 
5855a59a8b3Srsb 	/*
5865a59a8b3Srsb 	 * Set up the array of pointers for the vopstats-by-FS-type.
5875a59a8b3Srsb 	 * The entries will be allocated/initialized as each file system
5885a59a8b3Srsb 	 * goes through modload/mod_installfs.
5895a59a8b3Srsb 	 */
5905a59a8b3Srsb 	vopstats_fstype = (vopstats_t **)kmem_zalloc(
5915a59a8b3Srsb 	    (sizeof (vopstats_t *) * nfstype), KM_SLEEP);
5925a59a8b3Srsb 
5935a59a8b3Srsb 	/* Set up the global vopstats initialization template */
5945a59a8b3Srsb 	vs_templatep = create_vopstats_template();
5955a59a8b3Srsb }
5965a59a8b3Srsb 
5975a59a8b3Srsb /*
5985a59a8b3Srsb  * We need to have the all of the counters zeroed.
5995a59a8b3Srsb  * The initialization of the vopstats_t includes on the order of
6005a59a8b3Srsb  * 50 calls to kstat_named_init().  Rather that do that on every call,
6015a59a8b3Srsb  * we do it once in a template (vs_templatep) then bcopy it over.
6025a59a8b3Srsb  */
6035a59a8b3Srsb void
6045a59a8b3Srsb initialize_vopstats(vopstats_t *vsp)
6055a59a8b3Srsb {
6065a59a8b3Srsb 	if (vsp == NULL)
6075a59a8b3Srsb 		return;
6085a59a8b3Srsb 
6095a59a8b3Srsb 	bcopy(vs_templatep, vsp, sizeof (vopstats_t));
6105a59a8b3Srsb }
6115a59a8b3Srsb 
6125a59a8b3Srsb /*
61382c7f3c4Srsb  * If possible, determine which vopstats by fstype to use and
61482c7f3c4Srsb  * return a pointer to the caller.
6155a59a8b3Srsb  */
61682c7f3c4Srsb vopstats_t *
61782c7f3c4Srsb get_fstype_vopstats(vfs_t *vfsp, struct vfssw *vswp)
6185a59a8b3Srsb {
6195a59a8b3Srsb 	int		fstype = 0;	/* Index into vfssw[] */
62082c7f3c4Srsb 	vopstats_t	*vsp = NULL;
6215a59a8b3Srsb 
6225a59a8b3Srsb 	if (vfsp == NULL || (vfsp->vfs_flag & VFS_STATS) == 0 ||
6235a59a8b3Srsb 	    !vopstats_enabled)
62482c7f3c4Srsb 		return (NULL);
6255a59a8b3Srsb 	/*
6265a59a8b3Srsb 	 * Set up the fstype.  We go to so much trouble because all versions
6275a59a8b3Srsb 	 * of NFS use the same fstype in their vfs even though they have
6285a59a8b3Srsb 	 * distinct entries in the vfssw[] table.
62982c7f3c4Srsb 	 * NOTE: A special vfs (e.g., EIO_vfs) may not have an entry.
6305a59a8b3Srsb 	 */
6315a59a8b3Srsb 	if (vswp) {
6325a59a8b3Srsb 		fstype = vswp - vfssw;	/* Gets us the index */
6335a59a8b3Srsb 	} else {
6345a59a8b3Srsb 		fstype = vfsp->vfs_fstype;
6355a59a8b3Srsb 	}
6365a59a8b3Srsb 
6375a59a8b3Srsb 	/*
6385a59a8b3Srsb 	 * Point to the per-fstype vopstats. The only valid values are
6395a59a8b3Srsb 	 * non-zero positive values less than the number of vfssw[] table
6405a59a8b3Srsb 	 * entries.
6415a59a8b3Srsb 	 */
6425a59a8b3Srsb 	if (fstype > 0 && fstype < nfstype) {
64382c7f3c4Srsb 		vsp = vopstats_fstype[fstype];
6445a59a8b3Srsb 	}
6455a59a8b3Srsb 
64682c7f3c4Srsb 	return (vsp);
64782c7f3c4Srsb }
64882c7f3c4Srsb 
64982c7f3c4Srsb /*
65082c7f3c4Srsb  * Generate a kstat name, create the kstat structure, and allocate a
65182c7f3c4Srsb  * vsk_anchor_t to hold it together.  Return the pointer to the vsk_anchor_t
65282c7f3c4Srsb  * to the caller.  This must only be called from a mount.
65382c7f3c4Srsb  */
65482c7f3c4Srsb vsk_anchor_t *
65582c7f3c4Srsb get_vskstat_anchor(vfs_t *vfsp)
65682c7f3c4Srsb {
65782c7f3c4Srsb 	char		kstatstr[KSTAT_STRLEN]; /* kstat name for vopstats */
65882c7f3c4Srsb 	statvfs64_t	statvfsbuf;		/* Needed to find f_fsid */
65982c7f3c4Srsb 	vsk_anchor_t	*vskp = NULL;		/* vfs <--> kstat anchor */
66082c7f3c4Srsb 	kstat_t		*ksp;			/* Ptr to new kstat */
66182c7f3c4Srsb 	avl_index_t	where;			/* Location in the AVL tree */
66282c7f3c4Srsb 
663ddfcde86Srsb 	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
664ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
66582c7f3c4Srsb 		return (NULL);
66682c7f3c4Srsb 
6675a59a8b3Srsb 	/* Need to get the fsid to build a kstat name */
6685a59a8b3Srsb 	if (VFS_STATVFS(vfsp, &statvfsbuf) == 0) {
6695a59a8b3Srsb 		/* Create a name for our kstats based on fsid */
6705a59a8b3Srsb 		(void) snprintf(kstatstr, KSTAT_STRLEN, "%s%lx",
6715a59a8b3Srsb 		    VOPSTATS_STR, statvfsbuf.f_fsid);
6725a59a8b3Srsb 
6735a59a8b3Srsb 		/* Allocate and initialize the vsk_anchor_t */
6745a59a8b3Srsb 		vskp = kmem_cache_alloc(vsk_anchor_cache, KM_SLEEP);
6755a59a8b3Srsb 		bzero(vskp, sizeof (*vskp));
6765a59a8b3Srsb 		vskp->vsk_fsid = statvfsbuf.f_fsid;
6775a59a8b3Srsb 
6785a59a8b3Srsb 		mutex_enter(&vskstat_tree_lock);
6795a59a8b3Srsb 		if (avl_find(&vskstat_tree, vskp, &where) == NULL) {
6805a59a8b3Srsb 			avl_insert(&vskstat_tree, vskp, where);
6815a59a8b3Srsb 			mutex_exit(&vskstat_tree_lock);
6825a59a8b3Srsb 
6835a59a8b3Srsb 			/*
6845a59a8b3Srsb 			 * Now that we've got the anchor in the AVL
6855a59a8b3Srsb 			 * tree, we can create the kstat.
6865a59a8b3Srsb 			 */
6875a59a8b3Srsb 			ksp = new_vskstat(kstatstr, &vfsp->vfs_vopstats);
6885a59a8b3Srsb 			if (ksp) {
6895a59a8b3Srsb 				vskp->vsk_ksp = ksp;
6905a59a8b3Srsb 			}
6915a59a8b3Srsb 		} else {
6925a59a8b3Srsb 			/* Oops, found one! Release memory and lock. */
6935a59a8b3Srsb 			mutex_exit(&vskstat_tree_lock);
6945a59a8b3Srsb 			kmem_cache_free(vsk_anchor_cache, vskp);
69582c7f3c4Srsb 			vskp = NULL;
6965a59a8b3Srsb 		}
6975a59a8b3Srsb 	}
69882c7f3c4Srsb 	return (vskp);
6995a59a8b3Srsb }
7005a59a8b3Srsb 
7015a59a8b3Srsb /*
7025a59a8b3Srsb  * We're in the process of tearing down the vfs and need to cleanup
7035a59a8b3Srsb  * the data structures associated with the vopstats. Must only be called
7045a59a8b3Srsb  * from dounmount().
7055a59a8b3Srsb  */
7065a59a8b3Srsb void
7075a59a8b3Srsb teardown_vopstats(vfs_t *vfsp)
7085a59a8b3Srsb {
7095a59a8b3Srsb 	vsk_anchor_t	*vskap;
7105a59a8b3Srsb 	avl_index_t	where;
7115a59a8b3Srsb 
712ddfcde86Srsb 	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
713ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
7145a59a8b3Srsb 		return;
7155a59a8b3Srsb 
7165a59a8b3Srsb 	/* This is a safe check since VFS_STATS must be set (see above) */
7175a59a8b3Srsb 	if ((vskap = vfsp->vfs_vskap) == NULL)
7185a59a8b3Srsb 		return;
7195a59a8b3Srsb 
7205a59a8b3Srsb 	/* Whack the pointer right away */
7215a59a8b3Srsb 	vfsp->vfs_vskap = NULL;
7225a59a8b3Srsb 
7235a59a8b3Srsb 	/* Lock the tree, remove the node, and delete the kstat */
7245a59a8b3Srsb 	mutex_enter(&vskstat_tree_lock);
7255a59a8b3Srsb 	if (avl_find(&vskstat_tree, vskap, &where)) {
7265a59a8b3Srsb 		avl_remove(&vskstat_tree, vskap);
7275a59a8b3Srsb 	}
7285a59a8b3Srsb 
7295a59a8b3Srsb 	if (vskap->vsk_ksp) {
7305a59a8b3Srsb 		kstat_delete(vskap->vsk_ksp);
7315a59a8b3Srsb 	}
7325a59a8b3Srsb 	mutex_exit(&vskstat_tree_lock);
7335a59a8b3Srsb 
7345a59a8b3Srsb 	kmem_cache_free(vsk_anchor_cache, vskap);
7355a59a8b3Srsb }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate /*
7387c478bd9Sstevel@tonic-gate  * Read or write a vnode.  Called from kernel code.
7397c478bd9Sstevel@tonic-gate  */
7407c478bd9Sstevel@tonic-gate int
7417c478bd9Sstevel@tonic-gate vn_rdwr(
7427c478bd9Sstevel@tonic-gate 	enum uio_rw rw,
7437c478bd9Sstevel@tonic-gate 	struct vnode *vp,
7447c478bd9Sstevel@tonic-gate 	caddr_t base,
7457c478bd9Sstevel@tonic-gate 	ssize_t len,
7467c478bd9Sstevel@tonic-gate 	offset_t offset,
7477c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
7487c478bd9Sstevel@tonic-gate 	int ioflag,
7497c478bd9Sstevel@tonic-gate 	rlim64_t ulimit,	/* meaningful only if rw is UIO_WRITE */
7507c478bd9Sstevel@tonic-gate 	cred_t *cr,
7517c478bd9Sstevel@tonic-gate 	ssize_t *residp)
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate 	struct uio uio;
7547c478bd9Sstevel@tonic-gate 	struct iovec iov;
7557c478bd9Sstevel@tonic-gate 	int error;
7567c478bd9Sstevel@tonic-gate 	int in_crit = 0;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	if (rw == UIO_WRITE && ISROFILE(vp))
7597c478bd9Sstevel@tonic-gate 		return (EROFS);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	if (len < 0)
7627c478bd9Sstevel@tonic-gate 		return (EIO);
7637c478bd9Sstevel@tonic-gate 
764f48205beScasper 	VOPXID_MAP_CR(vp, cr);
765f48205beScasper 
7667c478bd9Sstevel@tonic-gate 	iov.iov_base = base;
7677c478bd9Sstevel@tonic-gate 	iov.iov_len = len;
7687c478bd9Sstevel@tonic-gate 	uio.uio_iov = &iov;
7697c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
7707c478bd9Sstevel@tonic-gate 	uio.uio_loffset = offset;
7717c478bd9Sstevel@tonic-gate 	uio.uio_segflg = (short)seg;
7727c478bd9Sstevel@tonic-gate 	uio.uio_resid = len;
7737c478bd9Sstevel@tonic-gate 	uio.uio_llimit = ulimit;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/*
7767c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
7777c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
7787c478bd9Sstevel@tonic-gate 	 */
7797c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
7807c478bd9Sstevel@tonic-gate 		int svmand;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
7837c478bd9Sstevel@tonic-gate 		in_crit = 1;
7847c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, cr, &svmand);
7857c478bd9Sstevel@tonic-gate 		if (error != 0)
7867c478bd9Sstevel@tonic-gate 			goto done;
7877c478bd9Sstevel@tonic-gate 		if (nbl_conflict(vp, rw == UIO_WRITE ? NBL_WRITE : NBL_READ,
788da6c28aaSamw 		    uio.uio_offset, uio.uio_resid, svmand, NULL)) {
7897c478bd9Sstevel@tonic-gate 			error = EACCES;
7907c478bd9Sstevel@tonic-gate 			goto done;
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp,
7957c478bd9Sstevel@tonic-gate 	    rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
7967c478bd9Sstevel@tonic-gate 	if (rw == UIO_WRITE) {
7977c478bd9Sstevel@tonic-gate 		uio.uio_fmode = FWRITE;
7987c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_DEFAULT;
7997c478bd9Sstevel@tonic-gate 		error = VOP_WRITE(vp, &uio, ioflag, cr, NULL);
8007c478bd9Sstevel@tonic-gate 	} else {
8017c478bd9Sstevel@tonic-gate 		uio.uio_fmode = FREAD;
8027c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_CACHED;
8037c478bd9Sstevel@tonic-gate 		error = VOP_READ(vp, &uio, ioflag, cr, NULL);
8047c478bd9Sstevel@tonic-gate 	}
805da6c28aaSamw 	VOP_RWUNLOCK(vp,
806da6c28aaSamw 	    rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
8077c478bd9Sstevel@tonic-gate 	if (residp)
8087c478bd9Sstevel@tonic-gate 		*residp = uio.uio_resid;
8097c478bd9Sstevel@tonic-gate 	else if (uio.uio_resid)
8107c478bd9Sstevel@tonic-gate 		error = EIO;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate done:
8137c478bd9Sstevel@tonic-gate 	if (in_crit)
8147c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
8157c478bd9Sstevel@tonic-gate 	return (error);
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate  * Release a vnode.  Call VOP_INACTIVE on last reference or
8207c478bd9Sstevel@tonic-gate  * decrement reference count.
8217c478bd9Sstevel@tonic-gate  *
8227c478bd9Sstevel@tonic-gate  * To avoid race conditions, the v_count is left at 1 for
8237c478bd9Sstevel@tonic-gate  * the call to VOP_INACTIVE. This prevents another thread
8247c478bd9Sstevel@tonic-gate  * from reclaiming and releasing the vnode *before* the
8257c478bd9Sstevel@tonic-gate  * VOP_INACTIVE routine has a chance to destroy the vnode.
8267c478bd9Sstevel@tonic-gate  * We can't have more than 1 thread calling VOP_INACTIVE
8277c478bd9Sstevel@tonic-gate  * on a vnode.
8287c478bd9Sstevel@tonic-gate  */
8297c478bd9Sstevel@tonic-gate void
8307c478bd9Sstevel@tonic-gate vn_rele(vnode_t *vp)
8317c478bd9Sstevel@tonic-gate {
832b5fca8f8Stomee 	VERIFY(vp->v_count > 0);
8337c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
8347c478bd9Sstevel@tonic-gate 	if (vp->v_count == 1) {
8357c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
836da6c28aaSamw 		VOP_INACTIVE(vp, CRED(), NULL);
837b5fca8f8Stomee 		return;
838b5fca8f8Stomee 	}
8397c478bd9Sstevel@tonic-gate 	vp->v_count--;
8407c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
8417c478bd9Sstevel@tonic-gate }
842b5fca8f8Stomee 
843b5fca8f8Stomee /*
844b5fca8f8Stomee  * Release a vnode referenced by the DNLC. Multiple DNLC references are treated
845b5fca8f8Stomee  * as a single reference, so v_count is not decremented until the last DNLC hold
846b5fca8f8Stomee  * is released. This makes it possible to distinguish vnodes that are referenced
847b5fca8f8Stomee  * only by the DNLC.
848b5fca8f8Stomee  */
849b5fca8f8Stomee void
850b5fca8f8Stomee vn_rele_dnlc(vnode_t *vp)
851b5fca8f8Stomee {
852b5fca8f8Stomee 	VERIFY((vp->v_count > 0) && (vp->v_count_dnlc > 0));
853b5fca8f8Stomee 	mutex_enter(&vp->v_lock);
854b5fca8f8Stomee 	if (--vp->v_count_dnlc == 0) {
855b5fca8f8Stomee 		if (vp->v_count == 1) {
856b5fca8f8Stomee 			mutex_exit(&vp->v_lock);
857b5fca8f8Stomee 			VOP_INACTIVE(vp, CRED(), NULL);
858b5fca8f8Stomee 			return;
859b5fca8f8Stomee 		}
860b5fca8f8Stomee 		vp->v_count--;
861b5fca8f8Stomee 	}
862b5fca8f8Stomee 	mutex_exit(&vp->v_lock);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate /*
8667c478bd9Sstevel@tonic-gate  * Like vn_rele() except that it clears v_stream under v_lock.
8677c478bd9Sstevel@tonic-gate  * This is used by sockfs when it dismantels the association between
8687c478bd9Sstevel@tonic-gate  * the sockfs node and the vnode in the underlaying file system.
8697c478bd9Sstevel@tonic-gate  * v_lock has to be held to prevent a thread coming through the lookupname
8707c478bd9Sstevel@tonic-gate  * path from accessing a stream head that is going away.
8717c478bd9Sstevel@tonic-gate  */
8727c478bd9Sstevel@tonic-gate void
8737c478bd9Sstevel@tonic-gate vn_rele_stream(vnode_t *vp)
8747c478bd9Sstevel@tonic-gate {
875b5fca8f8Stomee 	VERIFY(vp->v_count > 0);
8767c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
8777c478bd9Sstevel@tonic-gate 	vp->v_stream = NULL;
8787c478bd9Sstevel@tonic-gate 	if (vp->v_count == 1) {
8797c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
880da6c28aaSamw 		VOP_INACTIVE(vp, CRED(), NULL);
881b5fca8f8Stomee 		return;
882b5fca8f8Stomee 	}
8837c478bd9Sstevel@tonic-gate 	vp->v_count--;
8847c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8879d3574bfSNeil Perrin static void
8889d3574bfSNeil Perrin vn_rele_inactive(vnode_t *vp)
8899d3574bfSNeil Perrin {
8909d3574bfSNeil Perrin 	VOP_INACTIVE(vp, CRED(), NULL);
8919d3574bfSNeil Perrin }
8929d3574bfSNeil Perrin 
8939d3574bfSNeil Perrin /*
8949d3574bfSNeil Perrin  * Like vn_rele() except if we are going to call VOP_INACTIVE() then do it
8959d3574bfSNeil Perrin  * asynchronously using a taskq. This can avoid deadlocks caused by re-entering
8969d3574bfSNeil Perrin  * the file system as a result of releasing the vnode. Note, file systems
8979d3574bfSNeil Perrin  * already have to handle the race where the vnode is incremented before the
8989d3574bfSNeil Perrin  * inactive routine is called and does its locking.
8999d3574bfSNeil Perrin  *
9009d3574bfSNeil Perrin  * Warning: Excessive use of this routine can lead to performance problems.
9019d3574bfSNeil Perrin  * This is because taskqs throttle back allocation if too many are created.
9029d3574bfSNeil Perrin  */
9039d3574bfSNeil Perrin void
9049d3574bfSNeil Perrin vn_rele_async(vnode_t *vp, taskq_t *taskq)
9059d3574bfSNeil Perrin {
9069d3574bfSNeil Perrin 	VERIFY(vp->v_count > 0);
9079d3574bfSNeil Perrin 	mutex_enter(&vp->v_lock);
9089d3574bfSNeil Perrin 	if (vp->v_count == 1) {
9099d3574bfSNeil Perrin 		mutex_exit(&vp->v_lock);
9109d3574bfSNeil Perrin 		VERIFY(taskq_dispatch(taskq, (task_func_t *)vn_rele_inactive,
9119d3574bfSNeil Perrin 		    vp, TQ_SLEEP) != NULL);
9129d3574bfSNeil Perrin 		return;
9139d3574bfSNeil Perrin 	}
9149d3574bfSNeil Perrin 	vp->v_count--;
9159d3574bfSNeil Perrin 	mutex_exit(&vp->v_lock);
9169d3574bfSNeil Perrin }
9179d3574bfSNeil Perrin 
9187c478bd9Sstevel@tonic-gate int
9197c478bd9Sstevel@tonic-gate vn_open(
9207c478bd9Sstevel@tonic-gate 	char *pnamep,
9217c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
9227c478bd9Sstevel@tonic-gate 	int filemode,
9237c478bd9Sstevel@tonic-gate 	int createmode,
9247c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
9257c478bd9Sstevel@tonic-gate 	enum create crwhy,
9267c478bd9Sstevel@tonic-gate 	mode_t umask)
9277c478bd9Sstevel@tonic-gate {
928da6c28aaSamw 	return (vn_openat(pnamep, seg, filemode, createmode, vpp, crwhy,
929da6c28aaSamw 	    umask, NULL, -1));
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate  * Open/create a vnode.
9357c478bd9Sstevel@tonic-gate  * This may be callable by the kernel, the only known use
9367c478bd9Sstevel@tonic-gate  * of user context being that the current user credentials
9377c478bd9Sstevel@tonic-gate  * are used for permissions.  crwhy is defined iff filemode & FCREAT.
9387c478bd9Sstevel@tonic-gate  */
9397c478bd9Sstevel@tonic-gate int
9407c478bd9Sstevel@tonic-gate vn_openat(
9417c478bd9Sstevel@tonic-gate 	char *pnamep,
9427c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
9437c478bd9Sstevel@tonic-gate 	int filemode,
9447c478bd9Sstevel@tonic-gate 	int createmode,
9457c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
9467c478bd9Sstevel@tonic-gate 	enum create crwhy,
9477c478bd9Sstevel@tonic-gate 	mode_t umask,
948da6c28aaSamw 	struct vnode *startvp,
949da6c28aaSamw 	int fd)
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate 	struct vnode *vp;
9527c478bd9Sstevel@tonic-gate 	int mode;
953da6c28aaSamw 	int accessflags;
9547c478bd9Sstevel@tonic-gate 	int error;
9557c478bd9Sstevel@tonic-gate 	int in_crit = 0;
956da6c28aaSamw 	int open_done = 0;
957da6c28aaSamw 	int shrlock_done = 0;
9587c478bd9Sstevel@tonic-gate 	struct vattr vattr;
9597c478bd9Sstevel@tonic-gate 	enum symfollow follow;
960dd29fa4aSprabahar 	int estale_retry = 0;
961da6c28aaSamw 	struct shrlock shr;
962da6c28aaSamw 	struct shr_locowner shr_own;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	mode = 0;
965da6c28aaSamw 	accessflags = 0;
9667c478bd9Sstevel@tonic-gate 	if (filemode & FREAD)
9677c478bd9Sstevel@tonic-gate 		mode |= VREAD;
9687c478bd9Sstevel@tonic-gate 	if (filemode & (FWRITE|FTRUNC))
9697c478bd9Sstevel@tonic-gate 		mode |= VWRITE;
970794f0adbSRoger A. Faulkner 	if (filemode & (FSEARCH|FEXEC|FXATTRDIROPEN))
971da6c28aaSamw 		mode |= VEXEC;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	/* symlink interpretation */
9747c478bd9Sstevel@tonic-gate 	if (filemode & FNOFOLLOW)
9757c478bd9Sstevel@tonic-gate 		follow = NO_FOLLOW;
9767c478bd9Sstevel@tonic-gate 	else
9777c478bd9Sstevel@tonic-gate 		follow = FOLLOW;
9787c478bd9Sstevel@tonic-gate 
979da6c28aaSamw 	if (filemode & FAPPEND)
980da6c28aaSamw 		accessflags |= V_APPEND;
981da6c28aaSamw 
9827c478bd9Sstevel@tonic-gate top:
9837c478bd9Sstevel@tonic-gate 	if (filemode & FCREAT) {
9847c478bd9Sstevel@tonic-gate 		enum vcexcl excl;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		/*
9877c478bd9Sstevel@tonic-gate 		 * Wish to create a file.
9887c478bd9Sstevel@tonic-gate 		 */
9897c478bd9Sstevel@tonic-gate 		vattr.va_type = VREG;
9907c478bd9Sstevel@tonic-gate 		vattr.va_mode = createmode;
9917c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_TYPE|AT_MODE;
9927c478bd9Sstevel@tonic-gate 		if (filemode & FTRUNC) {
9937c478bd9Sstevel@tonic-gate 			vattr.va_size = 0;
9947c478bd9Sstevel@tonic-gate 			vattr.va_mask |= AT_SIZE;
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 		if (filemode & FEXCL)
9977c478bd9Sstevel@tonic-gate 			excl = EXCL;
9987c478bd9Sstevel@tonic-gate 		else
9997c478bd9Sstevel@tonic-gate 			excl = NONEXCL;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 		if (error =
10027c478bd9Sstevel@tonic-gate 		    vn_createat(pnamep, seg, &vattr, excl, mode, &vp, crwhy,
10031b300de9Sjwahlig 		    (filemode & ~(FTRUNC|FEXCL)), umask, startvp))
10047c478bd9Sstevel@tonic-gate 			return (error);
10057c478bd9Sstevel@tonic-gate 	} else {
10067c478bd9Sstevel@tonic-gate 		/*
10077c478bd9Sstevel@tonic-gate 		 * Wish to open a file.  Just look it up.
10087c478bd9Sstevel@tonic-gate 		 */
10097c478bd9Sstevel@tonic-gate 		if (error = lookupnameat(pnamep, seg, follow,
10107c478bd9Sstevel@tonic-gate 		    NULLVPP, &vp, startvp)) {
1011dd29fa4aSprabahar 			if ((error == ESTALE) &&
1012dd29fa4aSprabahar 			    fs_need_estale_retry(estale_retry++))
10137c478bd9Sstevel@tonic-gate 				goto top;
10147c478bd9Sstevel@tonic-gate 			return (error);
10157c478bd9Sstevel@tonic-gate 		}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 		/*
10187c478bd9Sstevel@tonic-gate 		 * Get the attributes to check whether file is large.
10197c478bd9Sstevel@tonic-gate 		 * We do this only if the FOFFMAX flag is not set and
10207c478bd9Sstevel@tonic-gate 		 * only for regular files.
10217c478bd9Sstevel@tonic-gate 		 */
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 		if (!(filemode & FOFFMAX) && (vp->v_type == VREG)) {
10247c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
1025da6c28aaSamw 			if ((error = VOP_GETATTR(vp, &vattr, 0,
1026da6c28aaSamw 			    CRED(), NULL))) {
10277c478bd9Sstevel@tonic-gate 				goto out;
10287c478bd9Sstevel@tonic-gate 			}
10297c478bd9Sstevel@tonic-gate 			if (vattr.va_size > (u_offset_t)MAXOFF32_T) {
10307c478bd9Sstevel@tonic-gate 				/*
10317c478bd9Sstevel@tonic-gate 				 * Large File API - regular open fails
10327c478bd9Sstevel@tonic-gate 				 * if FOFFMAX flag is set in file mode
10337c478bd9Sstevel@tonic-gate 				 */
10347c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
10357c478bd9Sstevel@tonic-gate 				goto out;
10367c478bd9Sstevel@tonic-gate 			}
10377c478bd9Sstevel@tonic-gate 		}
10387c478bd9Sstevel@tonic-gate 		/*
10397c478bd9Sstevel@tonic-gate 		 * Can't write directories, active texts, or
10407c478bd9Sstevel@tonic-gate 		 * read-only filesystems.  Can't truncate files
10417c478bd9Sstevel@tonic-gate 		 * on which mandatory locking is in effect.
10427c478bd9Sstevel@tonic-gate 		 */
10437c478bd9Sstevel@tonic-gate 		if (filemode & (FWRITE|FTRUNC)) {
10447c478bd9Sstevel@tonic-gate 			/*
10457c478bd9Sstevel@tonic-gate 			 * Allow writable directory if VDIROPEN flag is set.
10467c478bd9Sstevel@tonic-gate 			 */
10477c478bd9Sstevel@tonic-gate 			if (vp->v_type == VDIR && !(vp->v_flag & VDIROPEN)) {
10487c478bd9Sstevel@tonic-gate 				error = EISDIR;
10497c478bd9Sstevel@tonic-gate 				goto out;
10507c478bd9Sstevel@tonic-gate 			}
10517c478bd9Sstevel@tonic-gate 			if (ISROFILE(vp)) {
10527c478bd9Sstevel@tonic-gate 				error = EROFS;
10537c478bd9Sstevel@tonic-gate 				goto out;
10547c478bd9Sstevel@tonic-gate 			}
10557c478bd9Sstevel@tonic-gate 			/*
1056da6c28aaSamw 			 * Can't truncate files on which
1057da6c28aaSamw 			 * sysv mandatory locking is in effect.
10587c478bd9Sstevel@tonic-gate 			 */
10597c478bd9Sstevel@tonic-gate 			if (filemode & FTRUNC) {
10607c478bd9Sstevel@tonic-gate 				vnode_t *rvp;
10617c478bd9Sstevel@tonic-gate 
1062da6c28aaSamw 				if (VOP_REALVP(vp, &rvp, NULL) != 0)
10637c478bd9Sstevel@tonic-gate 					rvp = vp;
1064da6c28aaSamw 				if (rvp->v_filocks != NULL) {
10657c478bd9Sstevel@tonic-gate 					vattr.va_mask = AT_MODE;
1066da6c28aaSamw 					if ((error = VOP_GETATTR(vp,
1067da6c28aaSamw 					    &vattr, 0, CRED(), NULL)) == 0 &&
1068da6c28aaSamw 					    MANDLOCK(vp, vattr.va_mode))
10697c478bd9Sstevel@tonic-gate 						error = EAGAIN;
10707c478bd9Sstevel@tonic-gate 				}
10717c478bd9Sstevel@tonic-gate 			}
10727c478bd9Sstevel@tonic-gate 			if (error)
10737c478bd9Sstevel@tonic-gate 				goto out;
10747c478bd9Sstevel@tonic-gate 		}
10757c478bd9Sstevel@tonic-gate 		/*
10767c478bd9Sstevel@tonic-gate 		 * Check permissions.
10777c478bd9Sstevel@tonic-gate 		 */
1078da6c28aaSamw 		if (error = VOP_ACCESS(vp, mode, accessflags, CRED(), NULL))
10797c478bd9Sstevel@tonic-gate 			goto out;
1080794f0adbSRoger A. Faulkner 		/*
1081794f0adbSRoger A. Faulkner 		 * Require FSEARCH to return a directory.
1082794f0adbSRoger A. Faulkner 		 * Require FEXEC to return a regular file.
1083794f0adbSRoger A. Faulkner 		 */
1084794f0adbSRoger A. Faulkner 		if ((filemode & FSEARCH) && vp->v_type != VDIR) {
1085794f0adbSRoger A. Faulkner 			error = ENOTDIR;
1086794f0adbSRoger A. Faulkner 			goto out;
1087794f0adbSRoger A. Faulkner 		}
1088794f0adbSRoger A. Faulkner 		if ((filemode & FEXEC) && vp->v_type != VREG) {
1089794f0adbSRoger A. Faulkner 			error = ENOEXEC;	/* XXX: error code? */
1090794f0adbSRoger A. Faulkner 			goto out;
1091794f0adbSRoger A. Faulkner 		}
10927c478bd9Sstevel@tonic-gate 	}
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	/*
10957c478bd9Sstevel@tonic-gate 	 * Do remaining checks for FNOFOLLOW and FNOLINKS.
10967c478bd9Sstevel@tonic-gate 	 */
10977c478bd9Sstevel@tonic-gate 	if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) {
10989acbbeafSnn35248 		error = ELOOP;
10997c478bd9Sstevel@tonic-gate 		goto out;
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 	if (filemode & FNOLINKS) {
11027c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_NLINK;
1103da6c28aaSamw 		if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))) {
11047c478bd9Sstevel@tonic-gate 			goto out;
11057c478bd9Sstevel@tonic-gate 		}
11067c478bd9Sstevel@tonic-gate 		if (vattr.va_nlink != 1) {
11077c478bd9Sstevel@tonic-gate 			error = EMLINK;
11087c478bd9Sstevel@tonic-gate 			goto out;
11097c478bd9Sstevel@tonic-gate 		}
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	/*
11137c478bd9Sstevel@tonic-gate 	 * Opening a socket corresponding to the AF_UNIX pathname
11147c478bd9Sstevel@tonic-gate 	 * in the filesystem name space is not supported.
11157c478bd9Sstevel@tonic-gate 	 * However, VSOCK nodes in namefs are supported in order
11167c478bd9Sstevel@tonic-gate 	 * to make fattach work for sockets.
11177c478bd9Sstevel@tonic-gate 	 *
11187c478bd9Sstevel@tonic-gate 	 * XXX This uses VOP_REALVP to distinguish between
11197c478bd9Sstevel@tonic-gate 	 * an unopened namefs node (where VOP_REALVP returns a
11207c478bd9Sstevel@tonic-gate 	 * different VSOCK vnode) and a VSOCK created by vn_create
11217c478bd9Sstevel@tonic-gate 	 * in some file system (where VOP_REALVP would never return
11227c478bd9Sstevel@tonic-gate 	 * a different vnode).
11237c478bd9Sstevel@tonic-gate 	 */
11247c478bd9Sstevel@tonic-gate 	if (vp->v_type == VSOCK) {
11257c478bd9Sstevel@tonic-gate 		struct vnode *nvp;
11267c478bd9Sstevel@tonic-gate 
1127da6c28aaSamw 		error = VOP_REALVP(vp, &nvp, NULL);
11287c478bd9Sstevel@tonic-gate 		if (error != 0 || nvp == NULL || nvp == vp ||
11297c478bd9Sstevel@tonic-gate 		    nvp->v_type != VSOCK) {
11307c478bd9Sstevel@tonic-gate 			error = EOPNOTSUPP;
11317c478bd9Sstevel@tonic-gate 			goto out;
11327c478bd9Sstevel@tonic-gate 		}
11337c478bd9Sstevel@tonic-gate 	}
1134da6c28aaSamw 
1135da6c28aaSamw 	if ((vp->v_type == VREG) && nbl_need_check(vp)) {
1136da6c28aaSamw 		/* get share reservation */
1137da6c28aaSamw 		shr.s_access = 0;
1138da6c28aaSamw 		if (filemode & FWRITE)
1139da6c28aaSamw 			shr.s_access |= F_WRACC;
1140da6c28aaSamw 		if (filemode & FREAD)
1141da6c28aaSamw 			shr.s_access |= F_RDACC;
1142da6c28aaSamw 		shr.s_deny = 0;
1143da6c28aaSamw 		shr.s_sysid = 0;
1144da6c28aaSamw 		shr.s_pid = ttoproc(curthread)->p_pid;
1145da6c28aaSamw 		shr_own.sl_pid = shr.s_pid;
1146da6c28aaSamw 		shr_own.sl_id = fd;
1147da6c28aaSamw 		shr.s_own_len = sizeof (shr_own);
1148da6c28aaSamw 		shr.s_owner = (caddr_t)&shr_own;
1149da6c28aaSamw 		error = VOP_SHRLOCK(vp, F_SHARE_NBMAND, &shr, filemode, CRED(),
1150da6c28aaSamw 		    NULL);
1151da6c28aaSamw 		if (error)
1152da6c28aaSamw 			goto out;
1153da6c28aaSamw 		shrlock_done = 1;
1154da6c28aaSamw 
1155da6c28aaSamw 		/* nbmand conflict check if truncating file */
1156da6c28aaSamw 		if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
1157da6c28aaSamw 			nbl_start_crit(vp, RW_READER);
1158da6c28aaSamw 			in_crit = 1;
1159da6c28aaSamw 
1160da6c28aaSamw 			vattr.va_mask = AT_SIZE;
1161da6c28aaSamw 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
1162da6c28aaSamw 				goto out;
1163da6c28aaSamw 			if (nbl_conflict(vp, NBL_WRITE, 0, vattr.va_size, 0,
1164da6c28aaSamw 			    NULL)) {
1165da6c28aaSamw 				error = EACCES;
1166da6c28aaSamw 				goto out;
1167da6c28aaSamw 			}
1168da6c28aaSamw 		}
1169da6c28aaSamw 	}
1170da6c28aaSamw 
11717c478bd9Sstevel@tonic-gate 	/*
11727c478bd9Sstevel@tonic-gate 	 * Do opening protocol.
11737c478bd9Sstevel@tonic-gate 	 */
1174da6c28aaSamw 	error = VOP_OPEN(&vp, filemode, CRED(), NULL);
1175da6c28aaSamw 	if (error)
1176da6c28aaSamw 		goto out;
1177da6c28aaSamw 	open_done = 1;
1178da6c28aaSamw 
11797c478bd9Sstevel@tonic-gate 	/*
11807c478bd9Sstevel@tonic-gate 	 * Truncate if required.
11817c478bd9Sstevel@tonic-gate 	 */
1182da6c28aaSamw 	if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
11837c478bd9Sstevel@tonic-gate 		vattr.va_size = 0;
11847c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_SIZE;
11857c478bd9Sstevel@tonic-gate 		if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0)
1186da6c28aaSamw 			goto out;
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate out:
11897c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	if (in_crit) {
11927c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
11937c478bd9Sstevel@tonic-gate 		in_crit = 0;
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate 	if (error) {
1196da6c28aaSamw 		if (open_done) {
1197da6c28aaSamw 			(void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED(),
1198da6c28aaSamw 			    NULL);
1199da6c28aaSamw 			open_done = 0;
1200da6c28aaSamw 			shrlock_done = 0;
1201da6c28aaSamw 		}
1202da6c28aaSamw 		if (shrlock_done) {
1203da6c28aaSamw 			(void) VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, CRED(),
1204da6c28aaSamw 			    NULL);
1205da6c28aaSamw 			shrlock_done = 0;
1206da6c28aaSamw 		}
1207da6c28aaSamw 
12087c478bd9Sstevel@tonic-gate 		/*
12097c478bd9Sstevel@tonic-gate 		 * The following clause was added to handle a problem
12107c478bd9Sstevel@tonic-gate 		 * with NFS consistency.  It is possible that a lookup
12117c478bd9Sstevel@tonic-gate 		 * of the file to be opened succeeded, but the file
12127c478bd9Sstevel@tonic-gate 		 * itself doesn't actually exist on the server.  This
12137c478bd9Sstevel@tonic-gate 		 * is chiefly due to the DNLC containing an entry for
12147c478bd9Sstevel@tonic-gate 		 * the file which has been removed on the server.  In
12157c478bd9Sstevel@tonic-gate 		 * this case, we just start over.  If there was some
12167c478bd9Sstevel@tonic-gate 		 * other cause for the ESTALE error, then the lookup
12177c478bd9Sstevel@tonic-gate 		 * of the file will fail and the error will be returned
12187c478bd9Sstevel@tonic-gate 		 * above instead of looping around from here.
12197c478bd9Sstevel@tonic-gate 		 */
12207c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1221dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
12227c478bd9Sstevel@tonic-gate 			goto top;
12237c478bd9Sstevel@tonic-gate 	} else
12247c478bd9Sstevel@tonic-gate 		*vpp = vp;
12257c478bd9Sstevel@tonic-gate 	return (error);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
1228da6c28aaSamw /*
1229da6c28aaSamw  * The following two accessor functions are for the NFSv4 server.  Since there
1230da6c28aaSamw  * is no VOP_OPEN_UP/DOWNGRADE we need a way for the NFS server to keep the
1231da6c28aaSamw  * vnode open counts correct when a client "upgrades" an open or does an
1232da6c28aaSamw  * open_downgrade.  In NFS, an upgrade or downgrade can not only change the
1233da6c28aaSamw  * open mode (add or subtract read or write), but also change the share/deny
1234da6c28aaSamw  * modes.  However, share reservations are not integrated with OPEN, yet, so
1235da6c28aaSamw  * we need to handle each separately.  These functions are cleaner than having
1236da6c28aaSamw  * the NFS server manipulate the counts directly, however, nobody else should
1237da6c28aaSamw  * use these functions.
1238da6c28aaSamw  */
1239da6c28aaSamw void
1240da6c28aaSamw vn_open_upgrade(
1241da6c28aaSamw 	vnode_t *vp,
1242da6c28aaSamw 	int filemode)
1243da6c28aaSamw {
1244da6c28aaSamw 	ASSERT(vp->v_type == VREG);
1245da6c28aaSamw 
1246da6c28aaSamw 	if (filemode & FREAD)
1247da6c28aaSamw 		atomic_add_32(&(vp->v_rdcnt), 1);
1248da6c28aaSamw 	if (filemode & FWRITE)
1249da6c28aaSamw 		atomic_add_32(&(vp->v_wrcnt), 1);
1250da6c28aaSamw 
1251da6c28aaSamw }
1252da6c28aaSamw 
1253da6c28aaSamw void
1254da6c28aaSamw vn_open_downgrade(
1255da6c28aaSamw 	vnode_t *vp,
1256da6c28aaSamw 	int filemode)
1257da6c28aaSamw {
1258da6c28aaSamw 	ASSERT(vp->v_type == VREG);
1259da6c28aaSamw 
1260da6c28aaSamw 	if (filemode & FREAD) {
1261da6c28aaSamw 		ASSERT(vp->v_rdcnt > 0);
1262da6c28aaSamw 		atomic_add_32(&(vp->v_rdcnt), -1);
1263da6c28aaSamw 	}
1264da6c28aaSamw 	if (filemode & FWRITE) {
1265da6c28aaSamw 		ASSERT(vp->v_wrcnt > 0);
1266da6c28aaSamw 		atomic_add_32(&(vp->v_wrcnt), -1);
1267da6c28aaSamw 	}
1268da6c28aaSamw 
1269da6c28aaSamw }
1270da6c28aaSamw 
12717c478bd9Sstevel@tonic-gate int
12727c478bd9Sstevel@tonic-gate vn_create(
12737c478bd9Sstevel@tonic-gate 	char *pnamep,
12747c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
12757c478bd9Sstevel@tonic-gate 	struct vattr *vap,
12767c478bd9Sstevel@tonic-gate 	enum vcexcl excl,
12777c478bd9Sstevel@tonic-gate 	int mode,
12787c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
12797c478bd9Sstevel@tonic-gate 	enum create why,
12807c478bd9Sstevel@tonic-gate 	int flag,
12817c478bd9Sstevel@tonic-gate 	mode_t umask)
12827c478bd9Sstevel@tonic-gate {
1283da6c28aaSamw 	return (vn_createat(pnamep, seg, vap, excl, mode, vpp, why, flag,
1284da6c28aaSamw 	    umask, NULL));
12857c478bd9Sstevel@tonic-gate }
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate /*
12887c478bd9Sstevel@tonic-gate  * Create a vnode (makenode).
12897c478bd9Sstevel@tonic-gate  */
12907c478bd9Sstevel@tonic-gate int
12917c478bd9Sstevel@tonic-gate vn_createat(
12927c478bd9Sstevel@tonic-gate 	char *pnamep,
12937c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
12947c478bd9Sstevel@tonic-gate 	struct vattr *vap,
12957c478bd9Sstevel@tonic-gate 	enum vcexcl excl,
12967c478bd9Sstevel@tonic-gate 	int mode,
12977c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
12987c478bd9Sstevel@tonic-gate 	enum create why,
12997c478bd9Sstevel@tonic-gate 	int flag,
13007c478bd9Sstevel@tonic-gate 	mode_t umask,
13017c478bd9Sstevel@tonic-gate 	struct vnode *startvp)
13027c478bd9Sstevel@tonic-gate {
13037c478bd9Sstevel@tonic-gate 	struct vnode *dvp;	/* ptr to parent dir vnode */
13047c478bd9Sstevel@tonic-gate 	struct vnode *vp = NULL;
13057c478bd9Sstevel@tonic-gate 	struct pathname pn;
13067c478bd9Sstevel@tonic-gate 	int error;
13077c478bd9Sstevel@tonic-gate 	int in_crit = 0;
13087c478bd9Sstevel@tonic-gate 	struct vattr vattr;
13097c478bd9Sstevel@tonic-gate 	enum symfollow follow;
1310dd29fa4aSprabahar 	int estale_retry = 0;
1311005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	/* symlink interpretation */
13167c478bd9Sstevel@tonic-gate 	if ((flag & FNOFOLLOW) || excl == EXCL)
13177c478bd9Sstevel@tonic-gate 		follow = NO_FOLLOW;
13187c478bd9Sstevel@tonic-gate 	else
13197c478bd9Sstevel@tonic-gate 		follow = FOLLOW;
13207c478bd9Sstevel@tonic-gate 	flag &= ~(FNOFOLLOW|FNOLINKS);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate top:
13237c478bd9Sstevel@tonic-gate 	/*
13247c478bd9Sstevel@tonic-gate 	 * Lookup directory.
13257c478bd9Sstevel@tonic-gate 	 * If new object is a file, call lower level to create it.
13267c478bd9Sstevel@tonic-gate 	 * Note that it is up to the lower level to enforce exclusive
13277c478bd9Sstevel@tonic-gate 	 * creation, if the file is already there.
13287c478bd9Sstevel@tonic-gate 	 * This allows the lower level to do whatever
13297c478bd9Sstevel@tonic-gate 	 * locking or protocol that is needed to prevent races.
13307c478bd9Sstevel@tonic-gate 	 * If the new object is directory call lower level to make
13317c478bd9Sstevel@tonic-gate 	 * the new directory, with "." and "..".
13327c478bd9Sstevel@tonic-gate 	 */
13337c478bd9Sstevel@tonic-gate 	if (error = pn_get(pnamep, seg, &pn))
13347c478bd9Sstevel@tonic-gate 		return (error);
1335005d3febSMarek Pospisil 	if (auditing)
13367c478bd9Sstevel@tonic-gate 		audit_vncreate_start();
13377c478bd9Sstevel@tonic-gate 	dvp = NULL;
13387c478bd9Sstevel@tonic-gate 	*vpp = NULL;
13397c478bd9Sstevel@tonic-gate 	/*
13407c478bd9Sstevel@tonic-gate 	 * lookup will find the parent directory for the vnode.
13417c478bd9Sstevel@tonic-gate 	 * When it is done the pn holds the name of the entry
13427c478bd9Sstevel@tonic-gate 	 * in the directory.
13437c478bd9Sstevel@tonic-gate 	 * If this is a non-exclusive create we also find the node itself.
13447c478bd9Sstevel@tonic-gate 	 */
13457c478bd9Sstevel@tonic-gate 	error = lookuppnat(&pn, NULL, follow, &dvp,
13467c478bd9Sstevel@tonic-gate 	    (excl == EXCL) ? NULLVPP : vpp, startvp);
13477c478bd9Sstevel@tonic-gate 	if (error) {
13487c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1349dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
13507c478bd9Sstevel@tonic-gate 			goto top;
13517c478bd9Sstevel@tonic-gate 		if (why == CRMKDIR && error == EINVAL)
13527c478bd9Sstevel@tonic-gate 			error = EEXIST;		/* SVID */
13537c478bd9Sstevel@tonic-gate 		return (error);
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	if (why != CRMKNOD)
13577c478bd9Sstevel@tonic-gate 		vap->va_mode &= ~VSVTX;
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	/*
13607c478bd9Sstevel@tonic-gate 	 * If default ACLs are defined for the directory don't apply the
13617c478bd9Sstevel@tonic-gate 	 * umask if umask is passed.
13627c478bd9Sstevel@tonic-gate 	 */
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	if (umask) {
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 		vsecattr_t vsec;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 		vsec.vsa_aclcnt = 0;
13697c478bd9Sstevel@tonic-gate 		vsec.vsa_aclentp = NULL;
13707c478bd9Sstevel@tonic-gate 		vsec.vsa_dfaclcnt = 0;
13717c478bd9Sstevel@tonic-gate 		vsec.vsa_dfaclentp = NULL;
13727c478bd9Sstevel@tonic-gate 		vsec.vsa_mask = VSA_DFACLCNT;
1373da6c28aaSamw 		error = VOP_GETSECATTR(dvp, &vsec, 0, CRED(), NULL);
1374fa9e4066Sahrens 		/*
1375fa9e4066Sahrens 		 * If error is ENOSYS then treat it as no error
1376fa9e4066Sahrens 		 * Don't want to force all file systems to support
1377fa9e4066Sahrens 		 * aclent_t style of ACL's.
1378fa9e4066Sahrens 		 */
1379fa9e4066Sahrens 		if (error == ENOSYS)
1380fa9e4066Sahrens 			error = 0;
1381fa9e4066Sahrens 		if (error) {
13827c478bd9Sstevel@tonic-gate 			if (*vpp != NULL)
13837c478bd9Sstevel@tonic-gate 				VN_RELE(*vpp);
13847c478bd9Sstevel@tonic-gate 			goto out;
1385fa9e4066Sahrens 		} else {
13867c478bd9Sstevel@tonic-gate 			/*
13877c478bd9Sstevel@tonic-gate 			 * Apply the umask if no default ACLs.
13887c478bd9Sstevel@tonic-gate 			 */
13897c478bd9Sstevel@tonic-gate 			if (vsec.vsa_dfaclcnt == 0)
13907c478bd9Sstevel@tonic-gate 				vap->va_mode &= ~umask;
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 			/*
1393fa9e4066Sahrens 			 * VOP_GETSECATTR() may have allocated memory for
1394fa9e4066Sahrens 			 * ACLs we didn't request, so double-check and
1395fa9e4066Sahrens 			 * free it if necessary.
13967c478bd9Sstevel@tonic-gate 			 */
13977c478bd9Sstevel@tonic-gate 			if (vsec.vsa_aclcnt && vsec.vsa_aclentp != NULL)
13987c478bd9Sstevel@tonic-gate 				kmem_free((caddr_t)vsec.vsa_aclentp,
13997c478bd9Sstevel@tonic-gate 				    vsec.vsa_aclcnt * sizeof (aclent_t));
14007c478bd9Sstevel@tonic-gate 			if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp != NULL)
14017c478bd9Sstevel@tonic-gate 				kmem_free((caddr_t)vsec.vsa_dfaclentp,
14027c478bd9Sstevel@tonic-gate 				    vsec.vsa_dfaclcnt * sizeof (aclent_t));
14037c478bd9Sstevel@tonic-gate 		}
1404fa9e4066Sahrens 	}
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	/*
14077c478bd9Sstevel@tonic-gate 	 * In general we want to generate EROFS if the file system is
14087c478bd9Sstevel@tonic-gate 	 * readonly.  However, POSIX (IEEE Std. 1003.1) section 5.3.1
14097c478bd9Sstevel@tonic-gate 	 * documents the open system call, and it says that O_CREAT has no
14107c478bd9Sstevel@tonic-gate 	 * effect if the file already exists.  Bug 1119649 states
14117c478bd9Sstevel@tonic-gate 	 * that open(path, O_CREAT, ...) fails when attempting to open an
14127c478bd9Sstevel@tonic-gate 	 * existing file on a read only file system.  Thus, the first part
14137c478bd9Sstevel@tonic-gate 	 * of the following if statement has 3 checks:
14147c478bd9Sstevel@tonic-gate 	 *	if the file exists &&
14157c478bd9Sstevel@tonic-gate 	 *		it is being open with write access &&
14167c478bd9Sstevel@tonic-gate 	 *		the file system is read only
14177c478bd9Sstevel@tonic-gate 	 *	then generate EROFS
14187c478bd9Sstevel@tonic-gate 	 */
14197c478bd9Sstevel@tonic-gate 	if ((*vpp != NULL && (mode & VWRITE) && ISROFILE(*vpp)) ||
14207c478bd9Sstevel@tonic-gate 	    (*vpp == NULL && dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
14217c478bd9Sstevel@tonic-gate 		if (*vpp)
14227c478bd9Sstevel@tonic-gate 			VN_RELE(*vpp);
14237c478bd9Sstevel@tonic-gate 		error = EROFS;
14247c478bd9Sstevel@tonic-gate 	} else if (excl == NONEXCL && *vpp != NULL) {
14257c478bd9Sstevel@tonic-gate 		vnode_t *rvp;
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 		/*
14287c478bd9Sstevel@tonic-gate 		 * File already exists.  If a mandatory lock has been
14297c478bd9Sstevel@tonic-gate 		 * applied, return error.
14307c478bd9Sstevel@tonic-gate 		 */
14317c478bd9Sstevel@tonic-gate 		vp = *vpp;
1432da6c28aaSamw 		if (VOP_REALVP(vp, &rvp, NULL) != 0)
14337c478bd9Sstevel@tonic-gate 			rvp = vp;
14347c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_SIZE) && nbl_need_check(vp)) {
14357c478bd9Sstevel@tonic-gate 			nbl_start_crit(vp, RW_READER);
14367c478bd9Sstevel@tonic-gate 			in_crit = 1;
14377c478bd9Sstevel@tonic-gate 		}
14387c478bd9Sstevel@tonic-gate 		if (rvp->v_filocks != NULL || rvp->v_shrlocks != NULL) {
14397c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_MODE|AT_SIZE;
1440da6c28aaSamw 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL)) {
14417c478bd9Sstevel@tonic-gate 				goto out;
14427c478bd9Sstevel@tonic-gate 			}
14437c478bd9Sstevel@tonic-gate 			if (MANDLOCK(vp, vattr.va_mode)) {
14447c478bd9Sstevel@tonic-gate 				error = EAGAIN;
14457c478bd9Sstevel@tonic-gate 				goto out;
14467c478bd9Sstevel@tonic-gate 			}
14477c478bd9Sstevel@tonic-gate 			/*
14487c478bd9Sstevel@tonic-gate 			 * File cannot be truncated if non-blocking mandatory
14497c478bd9Sstevel@tonic-gate 			 * locks are currently on the file.
14507c478bd9Sstevel@tonic-gate 			 */
14517c478bd9Sstevel@tonic-gate 			if ((vap->va_mask & AT_SIZE) && in_crit) {
14527c478bd9Sstevel@tonic-gate 				u_offset_t offset;
14537c478bd9Sstevel@tonic-gate 				ssize_t length;
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 				offset = vap->va_size > vattr.va_size ?
14567c478bd9Sstevel@tonic-gate 				    vattr.va_size : vap->va_size;
14577c478bd9Sstevel@tonic-gate 				length = vap->va_size > vattr.va_size ?
14587c478bd9Sstevel@tonic-gate 				    vap->va_size - vattr.va_size :
14597c478bd9Sstevel@tonic-gate 				    vattr.va_size - vap->va_size;
14607c478bd9Sstevel@tonic-gate 				if (nbl_conflict(vp, NBL_WRITE, offset,
1461da6c28aaSamw 				    length, 0, NULL)) {
14627c478bd9Sstevel@tonic-gate 					error = EACCES;
14637c478bd9Sstevel@tonic-gate 					goto out;
14647c478bd9Sstevel@tonic-gate 				}
14657c478bd9Sstevel@tonic-gate 			}
14667c478bd9Sstevel@tonic-gate 		}
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 		/*
14697c478bd9Sstevel@tonic-gate 		 * If the file is the root of a VFS, we've crossed a
14707c478bd9Sstevel@tonic-gate 		 * mount point and the "containing" directory that we
14717c478bd9Sstevel@tonic-gate 		 * acquired above (dvp) is irrelevant because it's in
14727c478bd9Sstevel@tonic-gate 		 * a different file system.  We apply VOP_CREATE to the
14737c478bd9Sstevel@tonic-gate 		 * target itself instead of to the containing directory
14747c478bd9Sstevel@tonic-gate 		 * and supply a null path name to indicate (conventionally)
14757c478bd9Sstevel@tonic-gate 		 * the node itself as the "component" of interest.
14767c478bd9Sstevel@tonic-gate 		 *
14777c478bd9Sstevel@tonic-gate 		 * The intercession of the file system is necessary to
14787c478bd9Sstevel@tonic-gate 		 * ensure that the appropriate permission checks are
14797c478bd9Sstevel@tonic-gate 		 * done.
14807c478bd9Sstevel@tonic-gate 		 */
14817c478bd9Sstevel@tonic-gate 		if (vp->v_flag & VROOT) {
14827c478bd9Sstevel@tonic-gate 			ASSERT(why != CRMKDIR);
1483da6c28aaSamw 			error = VOP_CREATE(vp, "", vap, excl, mode, vpp,
1484da6c28aaSamw 			    CRED(), flag, NULL, NULL);
14857c478bd9Sstevel@tonic-gate 			/*
14867c478bd9Sstevel@tonic-gate 			 * If the create succeeded, it will have created
14877c478bd9Sstevel@tonic-gate 			 * a new reference to the vnode.  Give up the
14887c478bd9Sstevel@tonic-gate 			 * original reference.  The assertion should not
14897c478bd9Sstevel@tonic-gate 			 * get triggered because NBMAND locks only apply to
14907c478bd9Sstevel@tonic-gate 			 * VREG files.  And if in_crit is non-zero for some
14917c478bd9Sstevel@tonic-gate 			 * reason, detect that here, rather than when we
14927c478bd9Sstevel@tonic-gate 			 * deference a null vp.
14937c478bd9Sstevel@tonic-gate 			 */
14947c478bd9Sstevel@tonic-gate 			ASSERT(in_crit == 0);
14957c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
14967c478bd9Sstevel@tonic-gate 			vp = NULL;
14977c478bd9Sstevel@tonic-gate 			goto out;
14987c478bd9Sstevel@tonic-gate 		}
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 		/*
15017c478bd9Sstevel@tonic-gate 		 * Large File API - non-large open (FOFFMAX flag not set)
15027c478bd9Sstevel@tonic-gate 		 * of regular file fails if the file size exceeds MAXOFF32_T.
15037c478bd9Sstevel@tonic-gate 		 */
15047c478bd9Sstevel@tonic-gate 		if (why != CRMKDIR &&
15057c478bd9Sstevel@tonic-gate 		    !(flag & FOFFMAX) &&
15067c478bd9Sstevel@tonic-gate 		    (vp->v_type == VREG)) {
15077c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
1508da6c28aaSamw 			if ((error = VOP_GETATTR(vp, &vattr, 0,
1509da6c28aaSamw 			    CRED(), NULL))) {
15107c478bd9Sstevel@tonic-gate 				goto out;
15117c478bd9Sstevel@tonic-gate 			}
15127c478bd9Sstevel@tonic-gate 			if ((vattr.va_size > (u_offset_t)MAXOFF32_T)) {
15137c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
15147c478bd9Sstevel@tonic-gate 				goto out;
15157c478bd9Sstevel@tonic-gate 			}
15167c478bd9Sstevel@tonic-gate 		}
15177c478bd9Sstevel@tonic-gate 	}
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	if (error == 0) {
15207c478bd9Sstevel@tonic-gate 		/*
15217c478bd9Sstevel@tonic-gate 		 * Call mkdir() if specified, otherwise create().
15227c478bd9Sstevel@tonic-gate 		 */
15237c478bd9Sstevel@tonic-gate 		int must_be_dir = pn_fixslash(&pn);	/* trailing '/'? */
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 		if (why == CRMKDIR)
1526da6c28aaSamw 			/*
1527da6c28aaSamw 			 * N.B., if vn_createat() ever requests
1528da6c28aaSamw 			 * case-insensitive behavior then it will need
1529da6c28aaSamw 			 * to be passed to VOP_MKDIR().  VOP_CREATE()
1530da6c28aaSamw 			 * will already get it via "flag"
1531da6c28aaSamw 			 */
1532da6c28aaSamw 			error = VOP_MKDIR(dvp, pn.pn_path, vap, vpp, CRED(),
1533da6c28aaSamw 			    NULL, 0, NULL);
15347c478bd9Sstevel@tonic-gate 		else if (!must_be_dir)
15357c478bd9Sstevel@tonic-gate 			error = VOP_CREATE(dvp, pn.pn_path, vap,
1536da6c28aaSamw 			    excl, mode, vpp, CRED(), flag, NULL, NULL);
15377c478bd9Sstevel@tonic-gate 		else
15387c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
15397c478bd9Sstevel@tonic-gate 	}
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate out:
15427c478bd9Sstevel@tonic-gate 
1543005d3febSMarek Pospisil 	if (auditing)
15447c478bd9Sstevel@tonic-gate 		audit_vncreate_finish(*vpp, error);
15457c478bd9Sstevel@tonic-gate 	if (in_crit) {
15467c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
15477c478bd9Sstevel@tonic-gate 		in_crit = 0;
15487c478bd9Sstevel@tonic-gate 	}
15497c478bd9Sstevel@tonic-gate 	if (vp != NULL) {
15507c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
15517c478bd9Sstevel@tonic-gate 		vp = NULL;
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate 	pn_free(&pn);
15547c478bd9Sstevel@tonic-gate 	VN_RELE(dvp);
15557c478bd9Sstevel@tonic-gate 	/*
15567c478bd9Sstevel@tonic-gate 	 * The following clause was added to handle a problem
15577c478bd9Sstevel@tonic-gate 	 * with NFS consistency.  It is possible that a lookup
15587c478bd9Sstevel@tonic-gate 	 * of the file to be created succeeded, but the file
15597c478bd9Sstevel@tonic-gate 	 * itself doesn't actually exist on the server.  This
15607c478bd9Sstevel@tonic-gate 	 * is chiefly due to the DNLC containing an entry for
15617c478bd9Sstevel@tonic-gate 	 * the file which has been removed on the server.  In
15627c478bd9Sstevel@tonic-gate 	 * this case, we just start over.  If there was some
15637c478bd9Sstevel@tonic-gate 	 * other cause for the ESTALE error, then the lookup
15647c478bd9Sstevel@tonic-gate 	 * of the file will fail and the error will be returned
15657c478bd9Sstevel@tonic-gate 	 * above instead of looping around from here.
15667c478bd9Sstevel@tonic-gate 	 */
1567dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
15687c478bd9Sstevel@tonic-gate 		goto top;
15697c478bd9Sstevel@tonic-gate 	return (error);
15707c478bd9Sstevel@tonic-gate }
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate int
15737c478bd9Sstevel@tonic-gate vn_link(char *from, char *to, enum uio_seg seg)
15747c478bd9Sstevel@tonic-gate {
1575794f0adbSRoger A. Faulkner 	return (vn_linkat(NULL, from, NO_FOLLOW, NULL, to, seg));
1576794f0adbSRoger A. Faulkner }
1577794f0adbSRoger A. Faulkner 
1578794f0adbSRoger A. Faulkner int
1579794f0adbSRoger A. Faulkner vn_linkat(vnode_t *fstartvp, char *from, enum symfollow follow,
1580794f0adbSRoger A. Faulkner     vnode_t *tstartvp, char *to, enum uio_seg seg)
1581794f0adbSRoger A. Faulkner {
15827c478bd9Sstevel@tonic-gate 	struct vnode *fvp;		/* from vnode ptr */
15837c478bd9Sstevel@tonic-gate 	struct vnode *tdvp;		/* to directory vnode ptr */
15847c478bd9Sstevel@tonic-gate 	struct pathname pn;
15857c478bd9Sstevel@tonic-gate 	int error;
15867c478bd9Sstevel@tonic-gate 	struct vattr vattr;
15877c478bd9Sstevel@tonic-gate 	dev_t fsid;
1588dd29fa4aSprabahar 	int estale_retry = 0;
1589794f0adbSRoger A. Faulkner 	uint32_t auditing = AU_AUDITING();
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate top:
15927c478bd9Sstevel@tonic-gate 	fvp = tdvp = NULL;
15937c478bd9Sstevel@tonic-gate 	if (error = pn_get(to, seg, &pn))
15947c478bd9Sstevel@tonic-gate 		return (error);
1595794f0adbSRoger A. Faulkner 	if (auditing && fstartvp != NULL)
1596794f0adbSRoger A. Faulkner 		audit_setfsat_path(1);
1597794f0adbSRoger A. Faulkner 	if (error = lookupnameat(from, seg, follow, NULLVPP, &fvp, fstartvp))
15987c478bd9Sstevel@tonic-gate 		goto out;
1599794f0adbSRoger A. Faulkner 	if (auditing && tstartvp != NULL)
1600794f0adbSRoger A. Faulkner 		audit_setfsat_path(3);
1601794f0adbSRoger A. Faulkner 	if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &tdvp, NULLVPP, tstartvp))
16027c478bd9Sstevel@tonic-gate 		goto out;
16037c478bd9Sstevel@tonic-gate 	/*
16047c478bd9Sstevel@tonic-gate 	 * Make sure both source vnode and target directory vnode are
16057c478bd9Sstevel@tonic-gate 	 * in the same vfs and that it is writeable.
16067c478bd9Sstevel@tonic-gate 	 */
16077c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_FSID;
1608da6c28aaSamw 	if (error = VOP_GETATTR(fvp, &vattr, 0, CRED(), NULL))
16097c478bd9Sstevel@tonic-gate 		goto out;
16107c478bd9Sstevel@tonic-gate 	fsid = vattr.va_fsid;
16117c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_FSID;
1612da6c28aaSamw 	if (error = VOP_GETATTR(tdvp, &vattr, 0, CRED(), NULL))
16137c478bd9Sstevel@tonic-gate 		goto out;
16147c478bd9Sstevel@tonic-gate 	if (fsid != vattr.va_fsid) {
16157c478bd9Sstevel@tonic-gate 		error = EXDEV;
16167c478bd9Sstevel@tonic-gate 		goto out;
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 	if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) {
16197c478bd9Sstevel@tonic-gate 		error = EROFS;
16207c478bd9Sstevel@tonic-gate 		goto out;
16217c478bd9Sstevel@tonic-gate 	}
16227c478bd9Sstevel@tonic-gate 	/*
16237c478bd9Sstevel@tonic-gate 	 * Do the link.
16247c478bd9Sstevel@tonic-gate 	 */
16257c478bd9Sstevel@tonic-gate 	(void) pn_fixslash(&pn);
1626da6c28aaSamw 	error = VOP_LINK(tdvp, fvp, pn.pn_path, CRED(), NULL, 0);
16277c478bd9Sstevel@tonic-gate out:
16287c478bd9Sstevel@tonic-gate 	pn_free(&pn);
16297c478bd9Sstevel@tonic-gate 	if (fvp)
16307c478bd9Sstevel@tonic-gate 		VN_RELE(fvp);
16317c478bd9Sstevel@tonic-gate 	if (tdvp)
16327c478bd9Sstevel@tonic-gate 		VN_RELE(tdvp);
1633dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
16347c478bd9Sstevel@tonic-gate 		goto top;
16357c478bd9Sstevel@tonic-gate 	return (error);
16367c478bd9Sstevel@tonic-gate }
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate int
16397c478bd9Sstevel@tonic-gate vn_rename(char *from, char *to, enum uio_seg seg)
16407c478bd9Sstevel@tonic-gate {
16417c478bd9Sstevel@tonic-gate 	return (vn_renameat(NULL, from, NULL, to, seg));
16427c478bd9Sstevel@tonic-gate }
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate int
16457c478bd9Sstevel@tonic-gate vn_renameat(vnode_t *fdvp, char *fname, vnode_t *tdvp,
16467c478bd9Sstevel@tonic-gate 		char *tname, enum uio_seg seg)
16477c478bd9Sstevel@tonic-gate {
16487c478bd9Sstevel@tonic-gate 	int error;
16497c478bd9Sstevel@tonic-gate 	struct vattr vattr;
16507c478bd9Sstevel@tonic-gate 	struct pathname fpn;		/* from pathname */
16517c478bd9Sstevel@tonic-gate 	struct pathname tpn;		/* to pathname */
16527c478bd9Sstevel@tonic-gate 	dev_t fsid;
1653da6c28aaSamw 	int in_crit_src, in_crit_targ;
16547c478bd9Sstevel@tonic-gate 	vnode_t *fromvp, *fvp;
1655da6c28aaSamw 	vnode_t *tovp, *targvp;
1656dd29fa4aSprabahar 	int estale_retry = 0;
1657005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate top:
1660da6c28aaSamw 	fvp = fromvp = tovp = targvp = NULL;
1661da6c28aaSamw 	in_crit_src = in_crit_targ = 0;
16627c478bd9Sstevel@tonic-gate 	/*
16637c478bd9Sstevel@tonic-gate 	 * Get to and from pathnames.
16647c478bd9Sstevel@tonic-gate 	 */
16657c478bd9Sstevel@tonic-gate 	if (error = pn_get(fname, seg, &fpn))
16667c478bd9Sstevel@tonic-gate 		return (error);
16677c478bd9Sstevel@tonic-gate 	if (error = pn_get(tname, seg, &tpn)) {
16687c478bd9Sstevel@tonic-gate 		pn_free(&fpn);
16697c478bd9Sstevel@tonic-gate 		return (error);
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	/*
16737c478bd9Sstevel@tonic-gate 	 * First we need to resolve the correct directories
16747c478bd9Sstevel@tonic-gate 	 * The passed in directories may only be a starting point,
16757c478bd9Sstevel@tonic-gate 	 * but we need the real directories the file(s) live in.
16767c478bd9Sstevel@tonic-gate 	 * For example the fname may be something like usr/lib/sparc
16777c478bd9Sstevel@tonic-gate 	 * and we were passed in the / directory, but we need to
16787c478bd9Sstevel@tonic-gate 	 * use the lib directory for the rename.
16797c478bd9Sstevel@tonic-gate 	 */
16807c478bd9Sstevel@tonic-gate 
1681794f0adbSRoger A. Faulkner 	if (auditing && fdvp != NULL)
16827c478bd9Sstevel@tonic-gate 		audit_setfsat_path(1);
16837c478bd9Sstevel@tonic-gate 	/*
16847c478bd9Sstevel@tonic-gate 	 * Lookup to and from directories.
16857c478bd9Sstevel@tonic-gate 	 */
16867c478bd9Sstevel@tonic-gate 	if (error = lookuppnat(&fpn, NULL, NO_FOLLOW, &fromvp, &fvp, fdvp)) {
16877c478bd9Sstevel@tonic-gate 		goto out;
16887c478bd9Sstevel@tonic-gate 	}
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	/*
16917c478bd9Sstevel@tonic-gate 	 * Make sure there is an entry.
16927c478bd9Sstevel@tonic-gate 	 */
16937c478bd9Sstevel@tonic-gate 	if (fvp == NULL) {
16947c478bd9Sstevel@tonic-gate 		error = ENOENT;
16957c478bd9Sstevel@tonic-gate 		goto out;
16967c478bd9Sstevel@tonic-gate 	}
16977c478bd9Sstevel@tonic-gate 
1698794f0adbSRoger A. Faulkner 	if (auditing && tdvp != NULL)
16997c478bd9Sstevel@tonic-gate 		audit_setfsat_path(3);
1700da6c28aaSamw 	if (error = lookuppnat(&tpn, NULL, NO_FOLLOW, &tovp, &targvp, tdvp)) {
17017c478bd9Sstevel@tonic-gate 		goto out;
17027c478bd9Sstevel@tonic-gate 	}
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	/*
17057c478bd9Sstevel@tonic-gate 	 * Make sure both the from vnode directory and the to directory
17067c478bd9Sstevel@tonic-gate 	 * are in the same vfs and the to directory is writable.
17077c478bd9Sstevel@tonic-gate 	 * We check fsid's, not vfs pointers, so loopback fs works.
17087c478bd9Sstevel@tonic-gate 	 */
17097c478bd9Sstevel@tonic-gate 	if (fromvp != tovp) {
17107c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_FSID;
1711da6c28aaSamw 		if (error = VOP_GETATTR(fromvp, &vattr, 0, CRED(), NULL))
17127c478bd9Sstevel@tonic-gate 			goto out;
17137c478bd9Sstevel@tonic-gate 		fsid = vattr.va_fsid;
17147c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_FSID;
1715da6c28aaSamw 		if (error = VOP_GETATTR(tovp, &vattr, 0, CRED(), NULL))
17167c478bd9Sstevel@tonic-gate 			goto out;
17177c478bd9Sstevel@tonic-gate 		if (fsid != vattr.va_fsid) {
17187c478bd9Sstevel@tonic-gate 			error = EXDEV;
17197c478bd9Sstevel@tonic-gate 			goto out;
17207c478bd9Sstevel@tonic-gate 		}
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	if (tovp->v_vfsp->vfs_flag & VFS_RDONLY) {
17247c478bd9Sstevel@tonic-gate 		error = EROFS;
17257c478bd9Sstevel@tonic-gate 		goto out;
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 
1728da6c28aaSamw 	if (targvp && (fvp != targvp)) {
1729da6c28aaSamw 		nbl_start_crit(targvp, RW_READER);
1730da6c28aaSamw 		in_crit_targ = 1;
1731da6c28aaSamw 		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
1732da6c28aaSamw 			error = EACCES;
1733da6c28aaSamw 			goto out;
1734da6c28aaSamw 		}
1735da6c28aaSamw 	}
1736da6c28aaSamw 
17377c478bd9Sstevel@tonic-gate 	if (nbl_need_check(fvp)) {
17387c478bd9Sstevel@tonic-gate 		nbl_start_crit(fvp, RW_READER);
1739da6c28aaSamw 		in_crit_src = 1;
1740da6c28aaSamw 		if (nbl_conflict(fvp, NBL_RENAME, 0, 0, 0, NULL)) {
17417c478bd9Sstevel@tonic-gate 			error = EACCES;
17427c478bd9Sstevel@tonic-gate 			goto out;
17437c478bd9Sstevel@tonic-gate 		}
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	/*
17477c478bd9Sstevel@tonic-gate 	 * Do the rename.
17487c478bd9Sstevel@tonic-gate 	 */
17497c478bd9Sstevel@tonic-gate 	(void) pn_fixslash(&tpn);
1750da6c28aaSamw 	error = VOP_RENAME(fromvp, fpn.pn_path, tovp, tpn.pn_path, CRED(),
1751da6c28aaSamw 	    NULL, 0);
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate out:
17547c478bd9Sstevel@tonic-gate 	pn_free(&fpn);
17557c478bd9Sstevel@tonic-gate 	pn_free(&tpn);
1756da6c28aaSamw 	if (in_crit_src)
17577c478bd9Sstevel@tonic-gate 		nbl_end_crit(fvp);
1758da6c28aaSamw 	if (in_crit_targ)
1759da6c28aaSamw 		nbl_end_crit(targvp);
17607c478bd9Sstevel@tonic-gate 	if (fromvp)
17617c478bd9Sstevel@tonic-gate 		VN_RELE(fromvp);
17627c478bd9Sstevel@tonic-gate 	if (tovp)
17637c478bd9Sstevel@tonic-gate 		VN_RELE(tovp);
1764da6c28aaSamw 	if (targvp)
1765da6c28aaSamw 		VN_RELE(targvp);
17667c478bd9Sstevel@tonic-gate 	if (fvp)
17677c478bd9Sstevel@tonic-gate 		VN_RELE(fvp);
1768dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
17697c478bd9Sstevel@tonic-gate 		goto top;
17707c478bd9Sstevel@tonic-gate 	return (error);
17717c478bd9Sstevel@tonic-gate }
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate /*
17747c478bd9Sstevel@tonic-gate  * Remove a file or directory.
17757c478bd9Sstevel@tonic-gate  */
17767c478bd9Sstevel@tonic-gate int
17777c478bd9Sstevel@tonic-gate vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
17787c478bd9Sstevel@tonic-gate {
17797c478bd9Sstevel@tonic-gate 	return (vn_removeat(NULL, fnamep, seg, dirflag));
17807c478bd9Sstevel@tonic-gate }
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate int
17837c478bd9Sstevel@tonic-gate vn_removeat(vnode_t *startvp, char *fnamep, enum uio_seg seg, enum rm dirflag)
17847c478bd9Sstevel@tonic-gate {
17857c478bd9Sstevel@tonic-gate 	struct vnode *vp;		/* entry vnode */
17867c478bd9Sstevel@tonic-gate 	struct vnode *dvp;		/* ptr to parent dir vnode */
17877c478bd9Sstevel@tonic-gate 	struct vnode *coveredvp;
17887c478bd9Sstevel@tonic-gate 	struct pathname pn;		/* name of entry */
17897c478bd9Sstevel@tonic-gate 	enum vtype vtype;
17907c478bd9Sstevel@tonic-gate 	int error;
17917c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
17927c478bd9Sstevel@tonic-gate 	struct vfs *dvfsp;	/* ptr to parent dir vfs */
17937c478bd9Sstevel@tonic-gate 	int in_crit = 0;
1794dd29fa4aSprabahar 	int estale_retry = 0;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate top:
17977c478bd9Sstevel@tonic-gate 	if (error = pn_get(fnamep, seg, &pn))
17987c478bd9Sstevel@tonic-gate 		return (error);
17997c478bd9Sstevel@tonic-gate 	dvp = vp = NULL;
18007c478bd9Sstevel@tonic-gate 	if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &dvp, &vp, startvp)) {
18017c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1802dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
18037c478bd9Sstevel@tonic-gate 			goto top;
18047c478bd9Sstevel@tonic-gate 		return (error);
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	/*
18087c478bd9Sstevel@tonic-gate 	 * Make sure there is an entry.
18097c478bd9Sstevel@tonic-gate 	 */
18107c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
18117c478bd9Sstevel@tonic-gate 		error = ENOENT;
18127c478bd9Sstevel@tonic-gate 		goto out;
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	vfsp = vp->v_vfsp;
18167c478bd9Sstevel@tonic-gate 	dvfsp = dvp->v_vfsp;
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	/*
18197c478bd9Sstevel@tonic-gate 	 * If the named file is the root of a mounted filesystem, fail,
18207c478bd9Sstevel@tonic-gate 	 * unless it's marked unlinkable.  In that case, unmount the
18217c478bd9Sstevel@tonic-gate 	 * filesystem and proceed to unlink the covered vnode.  (If the
18227c478bd9Sstevel@tonic-gate 	 * covered vnode is a directory, use rmdir instead of unlink,
18237c478bd9Sstevel@tonic-gate 	 * to avoid file system corruption.)
18247c478bd9Sstevel@tonic-gate 	 */
18257c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VROOT) {
1826b9c98e29Spf199842 		if ((vfsp->vfs_flag & VFS_UNLINKABLE) == 0) {
1827b9c98e29Spf199842 			error = EBUSY;
1828b9c98e29Spf199842 			goto out;
1829b9c98e29Spf199842 		}
1830b9c98e29Spf199842 
1831b9c98e29Spf199842 		/*
1832b9c98e29Spf199842 		 * Namefs specific code starts here.
1833b9c98e29Spf199842 		 */
1834b9c98e29Spf199842 
18357c478bd9Sstevel@tonic-gate 		if (dirflag == RMDIRECTORY) {
18367c478bd9Sstevel@tonic-gate 			/*
18377c478bd9Sstevel@tonic-gate 			 * User called rmdir(2) on a file that has
18387c478bd9Sstevel@tonic-gate 			 * been namefs mounted on top of.  Since
18397c478bd9Sstevel@tonic-gate 			 * namefs doesn't allow directories to
18407c478bd9Sstevel@tonic-gate 			 * be mounted on other files we know
18417c478bd9Sstevel@tonic-gate 			 * vp is not of type VDIR so fail to operation.
18427c478bd9Sstevel@tonic-gate 			 */
18437c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
18447c478bd9Sstevel@tonic-gate 			goto out;
18457c478bd9Sstevel@tonic-gate 		}
1846b9c98e29Spf199842 
1847b9c98e29Spf199842 		/*
1848b9c98e29Spf199842 		 * If VROOT is still set after grabbing vp->v_lock,
1849b9c98e29Spf199842 		 * noone has finished nm_unmount so far and coveredvp
1850b9c98e29Spf199842 		 * is valid.
1851b9c98e29Spf199842 		 * If we manage to grab vn_vfswlock(coveredvp) before releasing
1852b9c98e29Spf199842 		 * vp->v_lock, any race window is eliminated.
1853b9c98e29Spf199842 		 */
1854b9c98e29Spf199842 
1855b9c98e29Spf199842 		mutex_enter(&vp->v_lock);
1856b9c98e29Spf199842 		if ((vp->v_flag & VROOT) == 0) {
1857b9c98e29Spf199842 			/* Someone beat us to the unmount */
1858b9c98e29Spf199842 			mutex_exit(&vp->v_lock);
1859b9c98e29Spf199842 			error = EBUSY;
1860b9c98e29Spf199842 			goto out;
1861b9c98e29Spf199842 		}
1862b9c98e29Spf199842 		vfsp = vp->v_vfsp;
18637c478bd9Sstevel@tonic-gate 		coveredvp = vfsp->vfs_vnodecovered;
1864b9c98e29Spf199842 		ASSERT(coveredvp);
1865b9c98e29Spf199842 		/*
1866b9c98e29Spf199842 		 * Note: Implementation of vn_vfswlock shows that ordering of
1867b9c98e29Spf199842 		 * v_lock / vn_vfswlock is not an issue here.
1868b9c98e29Spf199842 		 */
1869b9c98e29Spf199842 		error = vn_vfswlock(coveredvp);
1870b9c98e29Spf199842 		mutex_exit(&vp->v_lock);
1871b9c98e29Spf199842 
1872b9c98e29Spf199842 		if (error)
1873b9c98e29Spf199842 			goto out;
1874b9c98e29Spf199842 
18757c478bd9Sstevel@tonic-gate 		VN_HOLD(coveredvp);
18767c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
18777c478bd9Sstevel@tonic-gate 		error = dounmount(vfsp, 0, CRED());
1878b9c98e29Spf199842 
18797c478bd9Sstevel@tonic-gate 		/*
18807c478bd9Sstevel@tonic-gate 		 * Unmounted the namefs file system; now get
18817c478bd9Sstevel@tonic-gate 		 * the object it was mounted over.
18827c478bd9Sstevel@tonic-gate 		 */
18837c478bd9Sstevel@tonic-gate 		vp = coveredvp;
18847c478bd9Sstevel@tonic-gate 		/*
18857c478bd9Sstevel@tonic-gate 		 * If namefs was mounted over a directory, then
18867c478bd9Sstevel@tonic-gate 		 * we want to use rmdir() instead of unlink().
18877c478bd9Sstevel@tonic-gate 		 */
18887c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR)
18897c478bd9Sstevel@tonic-gate 			dirflag = RMDIRECTORY;
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 		if (error)
18927c478bd9Sstevel@tonic-gate 			goto out;
18937c478bd9Sstevel@tonic-gate 	}
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	/*
18967c478bd9Sstevel@tonic-gate 	 * Make sure filesystem is writeable.
18977c478bd9Sstevel@tonic-gate 	 * We check the parent directory's vfs in case this is an lofs vnode.
18987c478bd9Sstevel@tonic-gate 	 */
18997c478bd9Sstevel@tonic-gate 	if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) {
19007c478bd9Sstevel@tonic-gate 		error = EROFS;
19017c478bd9Sstevel@tonic-gate 		goto out;
19027c478bd9Sstevel@tonic-gate 	}
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	vtype = vp->v_type;
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	/*
19077c478bd9Sstevel@tonic-gate 	 * If there is the possibility of an nbmand share reservation, make
19087c478bd9Sstevel@tonic-gate 	 * sure it's okay to remove the file.  Keep a reference to the
19097c478bd9Sstevel@tonic-gate 	 * vnode, so that we can exit the nbl critical region after
19107c478bd9Sstevel@tonic-gate 	 * calling VOP_REMOVE.
19117c478bd9Sstevel@tonic-gate 	 * If there is no possibility of an nbmand share reservation,
19127c478bd9Sstevel@tonic-gate 	 * release the vnode reference now.  Filesystems like NFS may
19137c478bd9Sstevel@tonic-gate 	 * behave differently if there is an extra reference, so get rid of
19147c478bd9Sstevel@tonic-gate 	 * this one.  Fortunately, we can't have nbmand mounts on NFS
19157c478bd9Sstevel@tonic-gate 	 * filesystems.
19167c478bd9Sstevel@tonic-gate 	 */
19177c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
19187c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
19197c478bd9Sstevel@tonic-gate 		in_crit = 1;
1920da6c28aaSamw 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
19217c478bd9Sstevel@tonic-gate 			error = EACCES;
19227c478bd9Sstevel@tonic-gate 			goto out;
19237c478bd9Sstevel@tonic-gate 		}
19247c478bd9Sstevel@tonic-gate 	} else {
19257c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
19267c478bd9Sstevel@tonic-gate 		vp = NULL;
19277c478bd9Sstevel@tonic-gate 	}
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	if (dirflag == RMDIRECTORY) {
19307c478bd9Sstevel@tonic-gate 		/*
19317c478bd9Sstevel@tonic-gate 		 * Caller is using rmdir(2), which can only be applied to
19327c478bd9Sstevel@tonic-gate 		 * directories.
19337c478bd9Sstevel@tonic-gate 		 */
19347c478bd9Sstevel@tonic-gate 		if (vtype != VDIR) {
19357c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
19367c478bd9Sstevel@tonic-gate 		} else {
19377c478bd9Sstevel@tonic-gate 			vnode_t *cwd;
19387c478bd9Sstevel@tonic-gate 			proc_t *pp = curproc;
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
19417c478bd9Sstevel@tonic-gate 			cwd = PTOU(pp)->u_cdir;
19427c478bd9Sstevel@tonic-gate 			VN_HOLD(cwd);
19437c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
1944da6c28aaSamw 			error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED(),
1945da6c28aaSamw 			    NULL, 0);
19467c478bd9Sstevel@tonic-gate 			VN_RELE(cwd);
19477c478bd9Sstevel@tonic-gate 		}
19487c478bd9Sstevel@tonic-gate 	} else {
19497c478bd9Sstevel@tonic-gate 		/*
19507c478bd9Sstevel@tonic-gate 		 * Unlink(2) can be applied to anything.
19517c478bd9Sstevel@tonic-gate 		 */
1952da6c28aaSamw 		error = VOP_REMOVE(dvp, pn.pn_path, CRED(), NULL, 0);
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate out:
19567c478bd9Sstevel@tonic-gate 	pn_free(&pn);
19577c478bd9Sstevel@tonic-gate 	if (in_crit) {
19587c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
19597c478bd9Sstevel@tonic-gate 		in_crit = 0;
19607c478bd9Sstevel@tonic-gate 	}
19617c478bd9Sstevel@tonic-gate 	if (vp != NULL)
19627c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
19637c478bd9Sstevel@tonic-gate 	if (dvp != NULL)
19647c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
1965dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
19667c478bd9Sstevel@tonic-gate 		goto top;
19677c478bd9Sstevel@tonic-gate 	return (error);
19687c478bd9Sstevel@tonic-gate }
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate /*
19717c478bd9Sstevel@tonic-gate  * Utility function to compare equality of vnodes.
19727c478bd9Sstevel@tonic-gate  * Compare the underlying real vnodes, if there are underlying vnodes.
19737c478bd9Sstevel@tonic-gate  * This is a more thorough comparison than the VN_CMP() macro provides.
19747c478bd9Sstevel@tonic-gate  */
19757c478bd9Sstevel@tonic-gate int
19767c478bd9Sstevel@tonic-gate vn_compare(vnode_t *vp1, vnode_t *vp2)
19777c478bd9Sstevel@tonic-gate {
19787c478bd9Sstevel@tonic-gate 	vnode_t *realvp;
19797c478bd9Sstevel@tonic-gate 
1980da6c28aaSamw 	if (vp1 != NULL && VOP_REALVP(vp1, &realvp, NULL) == 0)
19817c478bd9Sstevel@tonic-gate 		vp1 = realvp;
1982da6c28aaSamw 	if (vp2 != NULL && VOP_REALVP(vp2, &realvp, NULL) == 0)
19837c478bd9Sstevel@tonic-gate 		vp2 = realvp;
19847c478bd9Sstevel@tonic-gate 	return (VN_CMP(vp1, vp2));
19857c478bd9Sstevel@tonic-gate }
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate /*
19887c478bd9Sstevel@tonic-gate  * The number of locks to hash into.  This value must be a power
19897c478bd9Sstevel@tonic-gate  * of 2 minus 1 and should probably also be prime.
19907c478bd9Sstevel@tonic-gate  */
19917c478bd9Sstevel@tonic-gate #define	NUM_BUCKETS	1023
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate struct  vn_vfslocks_bucket {
19947c478bd9Sstevel@tonic-gate 	kmutex_t vb_lock;
19957c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vb_list;
19967c478bd9Sstevel@tonic-gate 	char pad[64 - sizeof (kmutex_t) - sizeof (void *)];
19977c478bd9Sstevel@tonic-gate };
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate /*
20007c478bd9Sstevel@tonic-gate  * Total number of buckets will be NUM_BUCKETS + 1 .
20017c478bd9Sstevel@tonic-gate  */
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate #pragma	align	64(vn_vfslocks_buckets)
20047c478bd9Sstevel@tonic-gate static	struct vn_vfslocks_bucket	vn_vfslocks_buckets[NUM_BUCKETS + 1];
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate #define	VN_VFSLOCKS_SHIFT	9
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate #define	VN_VFSLOCKS_HASH(vfsvpptr)	\
20097c478bd9Sstevel@tonic-gate 	((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS)
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate /*
20127c478bd9Sstevel@tonic-gate  * vn_vfslocks_getlock() uses an HASH scheme to generate
20137c478bd9Sstevel@tonic-gate  * rwstlock using vfs/vnode pointer passed to it.
20147c478bd9Sstevel@tonic-gate  *
20157c478bd9Sstevel@tonic-gate  * vn_vfslocks_rele() releases a reference in the
20167c478bd9Sstevel@tonic-gate  * HASH table which allows the entry allocated by
20177c478bd9Sstevel@tonic-gate  * vn_vfslocks_getlock() to be freed at a later
20187c478bd9Sstevel@tonic-gate  * stage when the refcount drops to zero.
20197c478bd9Sstevel@tonic-gate  */
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate vn_vfslocks_entry_t *
20227c478bd9Sstevel@tonic-gate vn_vfslocks_getlock(void *vfsvpptr)
20237c478bd9Sstevel@tonic-gate {
20247c478bd9Sstevel@tonic-gate 	struct vn_vfslocks_bucket *bp;
20257c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vep;
20267c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *tvep;
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	ASSERT(vfsvpptr != NULL);
20297c478bd9Sstevel@tonic-gate 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)];
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
20327c478bd9Sstevel@tonic-gate 	for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
20337c478bd9Sstevel@tonic-gate 		if (vep->ve_vpvfs == vfsvpptr) {
20347c478bd9Sstevel@tonic-gate 			vep->ve_refcnt++;
20357c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->vb_lock);
20367c478bd9Sstevel@tonic-gate 			return (vep);
20377c478bd9Sstevel@tonic-gate 		}
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
20407c478bd9Sstevel@tonic-gate 	vep = kmem_alloc(sizeof (*vep), KM_SLEEP);
20417c478bd9Sstevel@tonic-gate 	rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL);
20427c478bd9Sstevel@tonic-gate 	vep->ve_vpvfs = (char *)vfsvpptr;
20437c478bd9Sstevel@tonic-gate 	vep->ve_refcnt = 1;
20447c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
20457c478bd9Sstevel@tonic-gate 	for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) {
20467c478bd9Sstevel@tonic-gate 		if (tvep->ve_vpvfs == vfsvpptr) {
20477c478bd9Sstevel@tonic-gate 			tvep->ve_refcnt++;
20487c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->vb_lock);
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 			/*
20517c478bd9Sstevel@tonic-gate 			 * There is already an entry in the hash
20527c478bd9Sstevel@tonic-gate 			 * destroy what we just allocated.
20537c478bd9Sstevel@tonic-gate 			 */
20547c478bd9Sstevel@tonic-gate 			rwst_destroy(&vep->ve_lock);
20557c478bd9Sstevel@tonic-gate 			kmem_free(vep, sizeof (*vep));
20567c478bd9Sstevel@tonic-gate 			return (tvep);
20577c478bd9Sstevel@tonic-gate 		}
20587c478bd9Sstevel@tonic-gate 	}
20597c478bd9Sstevel@tonic-gate 	vep->ve_next = bp->vb_list;
20607c478bd9Sstevel@tonic-gate 	bp->vb_list = vep;
20617c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
20627c478bd9Sstevel@tonic-gate 	return (vep);
20637c478bd9Sstevel@tonic-gate }
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate void
20667c478bd9Sstevel@tonic-gate vn_vfslocks_rele(vn_vfslocks_entry_t *vepent)
20677c478bd9Sstevel@tonic-gate {
20687c478bd9Sstevel@tonic-gate 	struct vn_vfslocks_bucket *bp;
20697c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vep;
20707c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *pvep;
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	ASSERT(vepent != NULL);
20737c478bd9Sstevel@tonic-gate 	ASSERT(vepent->ve_vpvfs != NULL);
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)];
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
20787c478bd9Sstevel@tonic-gate 	vepent->ve_refcnt--;
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	if ((int32_t)vepent->ve_refcnt < 0)
20817c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative");
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	if (vepent->ve_refcnt == 0) {
20847c478bd9Sstevel@tonic-gate 		for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
20857c478bd9Sstevel@tonic-gate 			if (vep->ve_vpvfs == vepent->ve_vpvfs) {
20867c478bd9Sstevel@tonic-gate 				if (bp->vb_list == vep)
20877c478bd9Sstevel@tonic-gate 					bp->vb_list = vep->ve_next;
20887c478bd9Sstevel@tonic-gate 				else {
20897c478bd9Sstevel@tonic-gate 					/* LINTED */
20907c478bd9Sstevel@tonic-gate 					pvep->ve_next = vep->ve_next;
20917c478bd9Sstevel@tonic-gate 				}
20927c478bd9Sstevel@tonic-gate 				mutex_exit(&bp->vb_lock);
20937c478bd9Sstevel@tonic-gate 				rwst_destroy(&vep->ve_lock);
20947c478bd9Sstevel@tonic-gate 				kmem_free(vep, sizeof (*vep));
20957c478bd9Sstevel@tonic-gate 				return;
20967c478bd9Sstevel@tonic-gate 			}
20977c478bd9Sstevel@tonic-gate 			pvep = vep;
20987c478bd9Sstevel@tonic-gate 		}
20997c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found");
21007c478bd9Sstevel@tonic-gate 	}
21017c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
21027c478bd9Sstevel@tonic-gate }
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate /*
21057c478bd9Sstevel@tonic-gate  * vn_vfswlock_wait is used to implement a lock which is logically a writers
21067c478bd9Sstevel@tonic-gate  * lock protecting the v_vfsmountedhere field.
21077c478bd9Sstevel@tonic-gate  * vn_vfswlock_wait has been modified to be similar to vn_vfswlock,
21087c478bd9Sstevel@tonic-gate  * except that it blocks to acquire the lock VVFSLOCK.
21097c478bd9Sstevel@tonic-gate  *
21107c478bd9Sstevel@tonic-gate  * traverse() and routines re-implementing part of traverse (e.g. autofs)
21117c478bd9Sstevel@tonic-gate  * need to hold this lock. mount(), vn_rename(), vn_remove() and so on
21127c478bd9Sstevel@tonic-gate  * need the non-blocking version of the writers lock i.e. vn_vfswlock
21137c478bd9Sstevel@tonic-gate  */
21147c478bd9Sstevel@tonic-gate int
21157c478bd9Sstevel@tonic-gate vn_vfswlock_wait(vnode_t *vp)
21167c478bd9Sstevel@tonic-gate {
21177c478bd9Sstevel@tonic-gate 	int retval;
21187c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
21197c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
21227c478bd9Sstevel@tonic-gate 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER);
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	if (retval == EINTR) {
21257c478bd9Sstevel@tonic-gate 		vn_vfslocks_rele(vpvfsentry);
21267c478bd9Sstevel@tonic-gate 		return (EINTR);
21277c478bd9Sstevel@tonic-gate 	}
21287c478bd9Sstevel@tonic-gate 	return (retval);
21297c478bd9Sstevel@tonic-gate }
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate int
21327c478bd9Sstevel@tonic-gate vn_vfsrlock_wait(vnode_t *vp)
21337c478bd9Sstevel@tonic-gate {
21347c478bd9Sstevel@tonic-gate 	int retval;
21357c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
21367c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
21397c478bd9Sstevel@tonic-gate 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER);
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	if (retval == EINTR) {
21427c478bd9Sstevel@tonic-gate 		vn_vfslocks_rele(vpvfsentry);
21437c478bd9Sstevel@tonic-gate 		return (EINTR);
21447c478bd9Sstevel@tonic-gate 	}
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	return (retval);
21477c478bd9Sstevel@tonic-gate }
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate /*
21517c478bd9Sstevel@tonic-gate  * vn_vfswlock is used to implement a lock which is logically a writers lock
21527c478bd9Sstevel@tonic-gate  * protecting the v_vfsmountedhere field.
21537c478bd9Sstevel@tonic-gate  */
21547c478bd9Sstevel@tonic-gate int
21557c478bd9Sstevel@tonic-gate vn_vfswlock(vnode_t *vp)
21567c478bd9Sstevel@tonic-gate {
21577c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	/*
21607c478bd9Sstevel@tonic-gate 	 * If vp is NULL then somebody is trying to lock the covered vnode
21617c478bd9Sstevel@tonic-gate 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
21627c478bd9Sstevel@tonic-gate 	 * only happen when unmounting /.  Since that operation will fail
21637c478bd9Sstevel@tonic-gate 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
21647c478bd9Sstevel@tonic-gate 	 */
21657c478bd9Sstevel@tonic-gate 	if (vp == NULL)
21667c478bd9Sstevel@tonic-gate 		return (EBUSY);
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
21717c478bd9Sstevel@tonic-gate 		return (0);
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
21747c478bd9Sstevel@tonic-gate 	return (EBUSY);
21757c478bd9Sstevel@tonic-gate }
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate int
21787c478bd9Sstevel@tonic-gate vn_vfsrlock(vnode_t *vp)
21797c478bd9Sstevel@tonic-gate {
21807c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 	/*
21837c478bd9Sstevel@tonic-gate 	 * If vp is NULL then somebody is trying to lock the covered vnode
21847c478bd9Sstevel@tonic-gate 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
21857c478bd9Sstevel@tonic-gate 	 * only happen when unmounting /.  Since that operation will fail
21867c478bd9Sstevel@tonic-gate 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
21877c478bd9Sstevel@tonic-gate 	 */
21887c478bd9Sstevel@tonic-gate 	if (vp == NULL)
21897c478bd9Sstevel@tonic-gate 		return (EBUSY);
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
21947c478bd9Sstevel@tonic-gate 		return (0);
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
21977c478bd9Sstevel@tonic-gate 	return (EBUSY);
21987c478bd9Sstevel@tonic-gate }
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate void
22017c478bd9Sstevel@tonic-gate vn_vfsunlock(vnode_t *vp)
22027c478bd9Sstevel@tonic-gate {
22037c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	/*
22067c478bd9Sstevel@tonic-gate 	 * ve_refcnt needs to be decremented twice.
22077c478bd9Sstevel@tonic-gate 	 * 1. To release refernce after a call to vn_vfslocks_getlock()
22087c478bd9Sstevel@tonic-gate 	 * 2. To release the reference from the locking routines like
22097c478bd9Sstevel@tonic-gate 	 *    vn_vfsrlock/vn_vfswlock etc,.
22107c478bd9Sstevel@tonic-gate 	 */
22117c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
22127c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 	rwst_exit(&vpvfsentry->ve_lock);
22157c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
22167c478bd9Sstevel@tonic-gate }
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate int
22197c478bd9Sstevel@tonic-gate vn_vfswlock_held(vnode_t *vp)
22207c478bd9Sstevel@tonic-gate {
22217c478bd9Sstevel@tonic-gate 	int held;
22227c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
22277c478bd9Sstevel@tonic-gate 	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
22307c478bd9Sstevel@tonic-gate 	return (held);
22317c478bd9Sstevel@tonic-gate }
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate int
22357c478bd9Sstevel@tonic-gate vn_make_ops(
22367c478bd9Sstevel@tonic-gate 	const char *name,			/* Name of file system */
22377c478bd9Sstevel@tonic-gate 	const fs_operation_def_t *templ,	/* Operation specification */
22387c478bd9Sstevel@tonic-gate 	vnodeops_t **actual)			/* Return the vnodeops */
22397c478bd9Sstevel@tonic-gate {
22407c478bd9Sstevel@tonic-gate 	int unused_ops;
22417c478bd9Sstevel@tonic-gate 	int error;
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 	*actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP);
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	(*actual)->vnop_name = name;
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ);
22487c478bd9Sstevel@tonic-gate 	if (error) {
22497c478bd9Sstevel@tonic-gate 		kmem_free(*actual, sizeof (vnodeops_t));
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate #if DEBUG
22537c478bd9Sstevel@tonic-gate 	if (unused_ops != 0)
22547c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied "
22557c478bd9Sstevel@tonic-gate 		    "but not used", name, unused_ops);
22567c478bd9Sstevel@tonic-gate #endif
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 	return (error);
22597c478bd9Sstevel@tonic-gate }
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate /*
22627c478bd9Sstevel@tonic-gate  * Free the vnodeops created as a result of vn_make_ops()
22637c478bd9Sstevel@tonic-gate  */
22647c478bd9Sstevel@tonic-gate void
22657c478bd9Sstevel@tonic-gate vn_freevnodeops(vnodeops_t *vnops)
22667c478bd9Sstevel@tonic-gate {
22677c478bd9Sstevel@tonic-gate 	kmem_free(vnops, sizeof (vnodeops_t));
22687c478bd9Sstevel@tonic-gate }
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate /*
22717c478bd9Sstevel@tonic-gate  * Vnode cache.
22727c478bd9Sstevel@tonic-gate  */
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate /* ARGSUSED */
22757c478bd9Sstevel@tonic-gate static int
22767c478bd9Sstevel@tonic-gate vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
22777c478bd9Sstevel@tonic-gate {
22787c478bd9Sstevel@tonic-gate 	struct vnode *vp;
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	vp = buf;
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 	mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
2283d216dff5SRobert Mastors 	mutex_init(&vp->v_vsd_lock, NULL, MUTEX_DEFAULT, NULL);
22847c478bd9Sstevel@tonic-gate 	cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL);
22857c478bd9Sstevel@tonic-gate 	rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL);
22867c478bd9Sstevel@tonic-gate 	vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
22877c478bd9Sstevel@tonic-gate 	vp->v_path = NULL;
22887c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
22891b300de9Sjwahlig 	vp->v_vsd = NULL;
2290df2381bfSpraks 	vp->v_fopdata = NULL;
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	return (0);
22937c478bd9Sstevel@tonic-gate }
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate /* ARGSUSED */
22967c478bd9Sstevel@tonic-gate static void
22977c478bd9Sstevel@tonic-gate vn_cache_destructor(void *buf, void *cdrarg)
22987c478bd9Sstevel@tonic-gate {
22997c478bd9Sstevel@tonic-gate 	struct vnode *vp;
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	vp = buf;
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	rw_destroy(&vp->v_nbllock);
23047c478bd9Sstevel@tonic-gate 	cv_destroy(&vp->v_cv);
2305d216dff5SRobert Mastors 	mutex_destroy(&vp->v_vsd_lock);
23067c478bd9Sstevel@tonic-gate 	mutex_destroy(&vp->v_lock);
23077c478bd9Sstevel@tonic-gate }
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate void
23107c478bd9Sstevel@tonic-gate vn_create_cache(void)
23117c478bd9Sstevel@tonic-gate {
2312cb15d5d9SPeter Rival 	/* LINTED */
2313cb15d5d9SPeter Rival 	ASSERT((1 << VNODE_ALIGN_LOG2) ==
2314cb15d5d9SPeter Rival 	    P2ROUNDUP(sizeof (struct vnode), VNODE_ALIGN));
2315cb15d5d9SPeter Rival 	vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode),
2316cb15d5d9SPeter Rival 	    VNODE_ALIGN, vn_cache_constructor, vn_cache_destructor, NULL, NULL,
23177c478bd9Sstevel@tonic-gate 	    NULL, 0);
23187c478bd9Sstevel@tonic-gate }
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate void
23217c478bd9Sstevel@tonic-gate vn_destroy_cache(void)
23227c478bd9Sstevel@tonic-gate {
23237c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(vn_cache);
23247c478bd9Sstevel@tonic-gate }
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate /*
23277c478bd9Sstevel@tonic-gate  * Used by file systems when fs-specific nodes (e.g., ufs inodes) are
23287c478bd9Sstevel@tonic-gate  * cached by the file system and vnodes remain associated.
23297c478bd9Sstevel@tonic-gate  */
23307c478bd9Sstevel@tonic-gate void
23317c478bd9Sstevel@tonic-gate vn_recycle(vnode_t *vp)
23327c478bd9Sstevel@tonic-gate {
23337c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_pages == NULL);
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	/*
23367c478bd9Sstevel@tonic-gate 	 * XXX - This really belongs in vn_reinit(), but we have some issues
23377c478bd9Sstevel@tonic-gate 	 * with the counts.  Best to have it here for clean initialization.
23387c478bd9Sstevel@tonic-gate 	 */
23397c478bd9Sstevel@tonic-gate 	vp->v_rdcnt = 0;
23407c478bd9Sstevel@tonic-gate 	vp->v_wrcnt = 0;
23417c478bd9Sstevel@tonic-gate 	vp->v_mmap_read = 0;
23427c478bd9Sstevel@tonic-gate 	vp->v_mmap_write = 0;
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 	/*
23457c478bd9Sstevel@tonic-gate 	 * If FEM was in use, make sure everything gets cleaned up
23467c478bd9Sstevel@tonic-gate 	 * NOTE: vp->v_femhead is initialized to NULL in the vnode
23477c478bd9Sstevel@tonic-gate 	 * constructor.
23487c478bd9Sstevel@tonic-gate 	 */
23497c478bd9Sstevel@tonic-gate 	if (vp->v_femhead) {
23507c478bd9Sstevel@tonic-gate 		/* XXX - There should be a free_femhead() that does all this */
23517c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_femhead->femh_list == NULL);
23527c478bd9Sstevel@tonic-gate 		mutex_destroy(&vp->v_femhead->femh_lock);
23537c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
23547c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;
23557c478bd9Sstevel@tonic-gate 	}
23567c478bd9Sstevel@tonic-gate 	if (vp->v_path) {
23577c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
23587c478bd9Sstevel@tonic-gate 		vp->v_path = NULL;
23597c478bd9Sstevel@tonic-gate 	}
2360df2381bfSpraks 
2361df2381bfSpraks 	if (vp->v_fopdata != NULL) {
2362df2381bfSpraks 		free_fopdata(vp);
2363df2381bfSpraks 	}
23647c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
23651b300de9Sjwahlig 	vsd_free(vp);
23667c478bd9Sstevel@tonic-gate }
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate /*
23697c478bd9Sstevel@tonic-gate  * Used to reset the vnode fields including those that are directly accessible
23707c478bd9Sstevel@tonic-gate  * as well as those which require an accessor function.
23717c478bd9Sstevel@tonic-gate  *
23727c478bd9Sstevel@tonic-gate  * Does not initialize:
2373d216dff5SRobert Mastors  *	synchronization objects: v_lock, v_vsd_lock, v_nbllock, v_cv
23747c478bd9Sstevel@tonic-gate  *	v_data (since FS-nodes and vnodes point to each other and should
23757c478bd9Sstevel@tonic-gate  *		be updated simultaneously)
23767c478bd9Sstevel@tonic-gate  *	v_op (in case someone needs to make a VOP call on this object)
23777c478bd9Sstevel@tonic-gate  */
23787c478bd9Sstevel@tonic-gate void
23797c478bd9Sstevel@tonic-gate vn_reinit(vnode_t *vp)
23807c478bd9Sstevel@tonic-gate {
23817c478bd9Sstevel@tonic-gate 	vp->v_count = 1;
2382b5fca8f8Stomee 	vp->v_count_dnlc = 0;
23837c478bd9Sstevel@tonic-gate 	vp->v_vfsp = NULL;
23847c478bd9Sstevel@tonic-gate 	vp->v_stream = NULL;
23857c478bd9Sstevel@tonic-gate 	vp->v_vfsmountedhere = NULL;
23867c478bd9Sstevel@tonic-gate 	vp->v_flag = 0;
23877c478bd9Sstevel@tonic-gate 	vp->v_type = VNON;
23887c478bd9Sstevel@tonic-gate 	vp->v_rdev = NODEV;
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 	vp->v_filocks = NULL;
23917c478bd9Sstevel@tonic-gate 	vp->v_shrlocks = NULL;
23927c478bd9Sstevel@tonic-gate 	vp->v_pages = NULL;
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate 	vp->v_locality = NULL;
2395da6c28aaSamw 	vp->v_xattrdir = NULL;
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate 	/* Handles v_femhead, v_path, and the r/w/map counts */
23987c478bd9Sstevel@tonic-gate 	vn_recycle(vp);
23997c478bd9Sstevel@tonic-gate }
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate vnode_t *
24027c478bd9Sstevel@tonic-gate vn_alloc(int kmflag)
24037c478bd9Sstevel@tonic-gate {
24047c478bd9Sstevel@tonic-gate 	vnode_t *vp;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	vp = kmem_cache_alloc(vn_cache, kmflag);
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	if (vp != NULL) {
24097c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2410df2381bfSpraks 		vp->v_fopdata = NULL;
24117c478bd9Sstevel@tonic-gate 		vn_reinit(vp);
24127c478bd9Sstevel@tonic-gate 	}
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	return (vp);
24157c478bd9Sstevel@tonic-gate }
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate void
24187c478bd9Sstevel@tonic-gate vn_free(vnode_t *vp)
24197c478bd9Sstevel@tonic-gate {
2420da6c28aaSamw 	ASSERT(vp->v_shrlocks == NULL);
2421da6c28aaSamw 	ASSERT(vp->v_filocks == NULL);
2422da6c28aaSamw 
24237c478bd9Sstevel@tonic-gate 	/*
24247c478bd9Sstevel@tonic-gate 	 * Some file systems call vn_free() with v_count of zero,
24257c478bd9Sstevel@tonic-gate 	 * some with v_count of 1.  In any case, the value should
24267c478bd9Sstevel@tonic-gate 	 * never be anything else.
24277c478bd9Sstevel@tonic-gate 	 */
24287c478bd9Sstevel@tonic-gate 	ASSERT((vp->v_count == 0) || (vp->v_count == 1));
2429b5fca8f8Stomee 	ASSERT(vp->v_count_dnlc == 0);
24307c478bd9Sstevel@tonic-gate 	if (vp->v_path != NULL) {
24317c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
24327c478bd9Sstevel@tonic-gate 		vp->v_path = NULL;
24337c478bd9Sstevel@tonic-gate 	}
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 	/* If FEM was in use, make sure everything gets cleaned up */
24367c478bd9Sstevel@tonic-gate 	if (vp->v_femhead) {
24377c478bd9Sstevel@tonic-gate 		/* XXX - There should be a free_femhead() that does all this */
24387c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_femhead->femh_list == NULL);
24397c478bd9Sstevel@tonic-gate 		mutex_destroy(&vp->v_femhead->femh_lock);
24407c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
24417c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;
24427c478bd9Sstevel@tonic-gate 	}
2443df2381bfSpraks 
2444df2381bfSpraks 	if (vp->v_fopdata != NULL) {
2445df2381bfSpraks 		free_fopdata(vp);
2446df2381bfSpraks 	}
24477c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
24481b300de9Sjwahlig 	vsd_free(vp);
24497c478bd9Sstevel@tonic-gate 	kmem_cache_free(vn_cache, vp);
24507c478bd9Sstevel@tonic-gate }
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate /*
24537c478bd9Sstevel@tonic-gate  * vnode status changes, should define better states than 1, 0.
24547c478bd9Sstevel@tonic-gate  */
24557c478bd9Sstevel@tonic-gate void
24567c478bd9Sstevel@tonic-gate vn_reclaim(vnode_t *vp)
24577c478bd9Sstevel@tonic-gate {
24587c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
24597c478bd9Sstevel@tonic-gate 
2460ddfcde86Srsb 	if (vfsp == NULL ||
2461ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
24627c478bd9Sstevel@tonic-gate 		return;
24637c478bd9Sstevel@tonic-gate 	}
24647c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED);
24657c478bd9Sstevel@tonic-gate }
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate void
24687c478bd9Sstevel@tonic-gate vn_idle(vnode_t *vp)
24697c478bd9Sstevel@tonic-gate {
24707c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
24717c478bd9Sstevel@tonic-gate 
2472ddfcde86Srsb 	if (vfsp == NULL ||
2473ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
24747c478bd9Sstevel@tonic-gate 		return;
24757c478bd9Sstevel@tonic-gate 	}
24767c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_IDLED);
24777c478bd9Sstevel@tonic-gate }
24787c478bd9Sstevel@tonic-gate void
24797c478bd9Sstevel@tonic-gate vn_exists(vnode_t *vp)
24807c478bd9Sstevel@tonic-gate {
24817c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
24827c478bd9Sstevel@tonic-gate 
2483ddfcde86Srsb 	if (vfsp == NULL ||
2484ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
24857c478bd9Sstevel@tonic-gate 		return;
24867c478bd9Sstevel@tonic-gate 	}
24877c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS);
24887c478bd9Sstevel@tonic-gate }
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate void
24917c478bd9Sstevel@tonic-gate vn_invalid(vnode_t *vp)
24927c478bd9Sstevel@tonic-gate {
24937c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
24947c478bd9Sstevel@tonic-gate 
2495ddfcde86Srsb 	if (vfsp == NULL ||
2496ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
24977c478bd9Sstevel@tonic-gate 		return;
24987c478bd9Sstevel@tonic-gate 	}
24997c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED);
25007c478bd9Sstevel@tonic-gate }
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate /* Vnode event notification */
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate int
2505da6c28aaSamw vnevent_support(vnode_t *vp, caller_context_t *ct)
25067c478bd9Sstevel@tonic-gate {
25077c478bd9Sstevel@tonic-gate 	if (vp == NULL)
25087c478bd9Sstevel@tonic-gate 		return (EINVAL);
25097c478bd9Sstevel@tonic-gate 
2510da6c28aaSamw 	return (VOP_VNEVENT(vp, VE_SUPPORT, NULL, NULL, ct));
25117c478bd9Sstevel@tonic-gate }
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate void
2514da6c28aaSamw vnevent_rename_src(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
25157c478bd9Sstevel@tonic-gate {
25167c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
25177c478bd9Sstevel@tonic-gate 		return;
25187c478bd9Sstevel@tonic-gate 	}
2519da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RENAME_SRC, dvp, name, ct);
25207c478bd9Sstevel@tonic-gate }
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate void
2523da6c28aaSamw vnevent_rename_dest(vnode_t *vp, vnode_t *dvp, char *name,
2524da6c28aaSamw     caller_context_t *ct)
25257c478bd9Sstevel@tonic-gate {
25267c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
25277c478bd9Sstevel@tonic-gate 		return;
25287c478bd9Sstevel@tonic-gate 	}
2529da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST, dvp, name, ct);
25307c478bd9Sstevel@tonic-gate }
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate void
2533da6c28aaSamw vnevent_rename_dest_dir(vnode_t *vp, caller_context_t *ct)
25347c478bd9Sstevel@tonic-gate {
25357c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
25367c478bd9Sstevel@tonic-gate 		return;
25377c478bd9Sstevel@tonic-gate 	}
2538da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST_DIR, NULL, NULL, ct);
25397c478bd9Sstevel@tonic-gate }
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate void
2542da6c28aaSamw vnevent_remove(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
25437c478bd9Sstevel@tonic-gate {
25447c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
25457c478bd9Sstevel@tonic-gate 		return;
25467c478bd9Sstevel@tonic-gate 	}
2547da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_REMOVE, dvp, name, ct);
2548df2381bfSpraks }
2549df2381bfSpraks 
2550df2381bfSpraks void
2551da6c28aaSamw vnevent_rmdir(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2552df2381bfSpraks {
2553df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2554df2381bfSpraks 		return;
2555df2381bfSpraks 	}
2556da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RMDIR, dvp, name, ct);
2557df2381bfSpraks }
2558df2381bfSpraks 
2559df2381bfSpraks void
2560da6c28aaSamw vnevent_create(vnode_t *vp, caller_context_t *ct)
2561df2381bfSpraks {
2562df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2563df2381bfSpraks 		return;
2564df2381bfSpraks 	}
2565da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_CREATE, NULL, NULL, ct);
2566df2381bfSpraks }
2567df2381bfSpraks 
2568df2381bfSpraks void
2569da6c28aaSamw vnevent_link(vnode_t *vp, caller_context_t *ct)
2570df2381bfSpraks {
2571df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2572df2381bfSpraks 		return;
2573df2381bfSpraks 	}
2574da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_LINK, NULL, NULL, ct);
2575df2381bfSpraks }
2576df2381bfSpraks 
2577df2381bfSpraks void
2578da6c28aaSamw vnevent_mountedover(vnode_t *vp, caller_context_t *ct)
2579df2381bfSpraks {
2580df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2581df2381bfSpraks 		return;
2582df2381bfSpraks 	}
2583da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_MOUNTEDOVER, NULL, NULL, ct);
25847c478bd9Sstevel@tonic-gate }
25857c478bd9Sstevel@tonic-gate 
2586*72102e74SBryan Cantrill void
2587*72102e74SBryan Cantrill vnevent_truncate(vnode_t *vp, caller_context_t *ct)
2588*72102e74SBryan Cantrill {
2589*72102e74SBryan Cantrill 	if (vp == NULL || vp->v_femhead == NULL) {
2590*72102e74SBryan Cantrill 		return;
2591*72102e74SBryan Cantrill 	}
2592*72102e74SBryan Cantrill 	(void) VOP_VNEVENT(vp, VE_TRUNCATE, NULL, NULL, ct);
2593*72102e74SBryan Cantrill }
2594*72102e74SBryan Cantrill 
25957c478bd9Sstevel@tonic-gate /*
25967c478bd9Sstevel@tonic-gate  * Vnode accessors.
25977c478bd9Sstevel@tonic-gate  */
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate int
26007c478bd9Sstevel@tonic-gate vn_is_readonly(vnode_t *vp)
26017c478bd9Sstevel@tonic-gate {
26027c478bd9Sstevel@tonic-gate 	return (vp->v_vfsp->vfs_flag & VFS_RDONLY);
26037c478bd9Sstevel@tonic-gate }
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate int
26067c478bd9Sstevel@tonic-gate vn_has_flocks(vnode_t *vp)
26077c478bd9Sstevel@tonic-gate {
26087c478bd9Sstevel@tonic-gate 	return (vp->v_filocks != NULL);
26097c478bd9Sstevel@tonic-gate }
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate int
26127c478bd9Sstevel@tonic-gate vn_has_mandatory_locks(vnode_t *vp, int mode)
26137c478bd9Sstevel@tonic-gate {
26147c478bd9Sstevel@tonic-gate 	return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode)));
26157c478bd9Sstevel@tonic-gate }
26167c478bd9Sstevel@tonic-gate 
26177c478bd9Sstevel@tonic-gate int
26187c478bd9Sstevel@tonic-gate vn_has_cached_data(vnode_t *vp)
26197c478bd9Sstevel@tonic-gate {
26207c478bd9Sstevel@tonic-gate 	return (vp->v_pages != NULL);
26217c478bd9Sstevel@tonic-gate }
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate /*
26247c478bd9Sstevel@tonic-gate  * Return 0 if the vnode in question shouldn't be permitted into a zone via
26257c478bd9Sstevel@tonic-gate  * zone_enter(2).
26267c478bd9Sstevel@tonic-gate  */
26277c478bd9Sstevel@tonic-gate int
26287c478bd9Sstevel@tonic-gate vn_can_change_zones(vnode_t *vp)
26297c478bd9Sstevel@tonic-gate {
26307c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
26317c478bd9Sstevel@tonic-gate 	int allow = 1;
26327c478bd9Sstevel@tonic-gate 	vnode_t *rvp;
26337c478bd9Sstevel@tonic-gate 
2634108322fbScarlsonj 	if (nfs_global_client_only != 0)
2635108322fbScarlsonj 		return (1);
2636108322fbScarlsonj 
26377c478bd9Sstevel@tonic-gate 	/*
26387c478bd9Sstevel@tonic-gate 	 * We always want to look at the underlying vnode if there is one.
26397c478bd9Sstevel@tonic-gate 	 */
2640da6c28aaSamw 	if (VOP_REALVP(vp, &rvp, NULL) != 0)
26417c478bd9Sstevel@tonic-gate 		rvp = vp;
26427c478bd9Sstevel@tonic-gate 	/*
26437c478bd9Sstevel@tonic-gate 	 * Some pseudo filesystems (including doorfs) don't actually register
26447c478bd9Sstevel@tonic-gate 	 * their vfsops_t, so the following may return NULL; we happily let
26457c478bd9Sstevel@tonic-gate 	 * such vnodes switch zones.
26467c478bd9Sstevel@tonic-gate 	 */
26477c478bd9Sstevel@tonic-gate 	vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp));
26487c478bd9Sstevel@tonic-gate 	if (vswp != NULL) {
26497c478bd9Sstevel@tonic-gate 		if (vswp->vsw_flag & VSW_NOTZONESAFE)
26507c478bd9Sstevel@tonic-gate 			allow = 0;
26517c478bd9Sstevel@tonic-gate 		vfs_unrefvfssw(vswp);
26527c478bd9Sstevel@tonic-gate 	}
26537c478bd9Sstevel@tonic-gate 	return (allow);
26547c478bd9Sstevel@tonic-gate }
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate /*
26577c478bd9Sstevel@tonic-gate  * Return nonzero if the vnode is a mount point, zero if not.
26587c478bd9Sstevel@tonic-gate  */
26597c478bd9Sstevel@tonic-gate int
26607c478bd9Sstevel@tonic-gate vn_ismntpt(vnode_t *vp)
26617c478bd9Sstevel@tonic-gate {
26627c478bd9Sstevel@tonic-gate 	return (vp->v_vfsmountedhere != NULL);
26637c478bd9Sstevel@tonic-gate }
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate /* Retrieve the vfs (if any) mounted on this vnode */
26667c478bd9Sstevel@tonic-gate vfs_t *
26677c478bd9Sstevel@tonic-gate vn_mountedvfs(vnode_t *vp)
26687c478bd9Sstevel@tonic-gate {
26697c478bd9Sstevel@tonic-gate 	return (vp->v_vfsmountedhere);
26707c478bd9Sstevel@tonic-gate }
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate /*
2673b5fca8f8Stomee  * Return nonzero if the vnode is referenced by the dnlc, zero if not.
2674b5fca8f8Stomee  */
2675b5fca8f8Stomee int
2676b5fca8f8Stomee vn_in_dnlc(vnode_t *vp)
2677b5fca8f8Stomee {
2678b5fca8f8Stomee 	return (vp->v_count_dnlc > 0);
2679b5fca8f8Stomee }
2680b5fca8f8Stomee 
2681b5fca8f8Stomee /*
2682da6c28aaSamw  * vn_has_other_opens() checks whether a particular file is opened by more than
2683da6c28aaSamw  * just the caller and whether the open is for read and/or write.
2684da6c28aaSamw  * This routine is for calling after the caller has already called VOP_OPEN()
2685da6c28aaSamw  * and the caller wishes to know if they are the only one with it open for
2686da6c28aaSamw  * the mode(s) specified.
2687da6c28aaSamw  *
2688da6c28aaSamw  * Vnode counts are only kept on regular files (v_type=VREG).
2689da6c28aaSamw  */
2690da6c28aaSamw int
2691da6c28aaSamw vn_has_other_opens(
2692da6c28aaSamw 	vnode_t *vp,
2693da6c28aaSamw 	v_mode_t mode)
2694da6c28aaSamw {
2695da6c28aaSamw 
2696da6c28aaSamw 	ASSERT(vp != NULL);
2697da6c28aaSamw 
2698da6c28aaSamw 	switch (mode) {
2699da6c28aaSamw 	case V_WRITE:
2700da6c28aaSamw 		if (vp->v_wrcnt > 1)
2701da6c28aaSamw 			return (V_TRUE);
2702da6c28aaSamw 		break;
2703da6c28aaSamw 	case V_RDORWR:
2704da6c28aaSamw 		if ((vp->v_rdcnt > 1) || (vp->v_wrcnt > 1))
2705da6c28aaSamw 			return (V_TRUE);
2706da6c28aaSamw 		break;
2707da6c28aaSamw 	case V_RDANDWR:
2708da6c28aaSamw 		if ((vp->v_rdcnt > 1) && (vp->v_wrcnt > 1))
2709da6c28aaSamw 			return (V_TRUE);
2710da6c28aaSamw 		break;
2711da6c28aaSamw 	case V_READ:
2712da6c28aaSamw 		if (vp->v_rdcnt > 1)
2713da6c28aaSamw 			return (V_TRUE);
2714da6c28aaSamw 		break;
2715da6c28aaSamw 	}
2716da6c28aaSamw 
2717da6c28aaSamw 	return (V_FALSE);
2718da6c28aaSamw }
2719da6c28aaSamw 
2720da6c28aaSamw /*
27217c478bd9Sstevel@tonic-gate  * vn_is_opened() checks whether a particular file is opened and
27227c478bd9Sstevel@tonic-gate  * whether the open is for read and/or write.
27237c478bd9Sstevel@tonic-gate  *
27247c478bd9Sstevel@tonic-gate  * Vnode counts are only kept on regular files (v_type=VREG).
27257c478bd9Sstevel@tonic-gate  */
27267c478bd9Sstevel@tonic-gate int
27277c478bd9Sstevel@tonic-gate vn_is_opened(
27287c478bd9Sstevel@tonic-gate 	vnode_t *vp,
27297c478bd9Sstevel@tonic-gate 	v_mode_t mode)
27307c478bd9Sstevel@tonic-gate {
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	switch (mode) {
27357c478bd9Sstevel@tonic-gate 	case V_WRITE:
27367c478bd9Sstevel@tonic-gate 		if (vp->v_wrcnt)
27377c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27387c478bd9Sstevel@tonic-gate 		break;
27397c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
27407c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt && vp->v_wrcnt)
27417c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27427c478bd9Sstevel@tonic-gate 		break;
27437c478bd9Sstevel@tonic-gate 	case V_RDORWR:
27447c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt || vp->v_wrcnt)
27457c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27467c478bd9Sstevel@tonic-gate 		break;
27477c478bd9Sstevel@tonic-gate 	case V_READ:
27487c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt)
27497c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27507c478bd9Sstevel@tonic-gate 		break;
27517c478bd9Sstevel@tonic-gate 	}
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 	return (V_FALSE);
27547c478bd9Sstevel@tonic-gate }
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate /*
27577c478bd9Sstevel@tonic-gate  * vn_is_mapped() checks whether a particular file is mapped and whether
27587c478bd9Sstevel@tonic-gate  * the file is mapped read and/or write.
27597c478bd9Sstevel@tonic-gate  */
27607c478bd9Sstevel@tonic-gate int
27617c478bd9Sstevel@tonic-gate vn_is_mapped(
27627c478bd9Sstevel@tonic-gate 	vnode_t *vp,
27637c478bd9Sstevel@tonic-gate 	v_mode_t mode)
27647c478bd9Sstevel@tonic-gate {
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate #if !defined(_LP64)
27697c478bd9Sstevel@tonic-gate 	switch (mode) {
27707c478bd9Sstevel@tonic-gate 	/*
27717c478bd9Sstevel@tonic-gate 	 * The atomic_add_64_nv functions force atomicity in the
27727c478bd9Sstevel@tonic-gate 	 * case of 32 bit architectures. Otherwise the 64 bit values
27737c478bd9Sstevel@tonic-gate 	 * require two fetches. The value of the fields may be
27747c478bd9Sstevel@tonic-gate 	 * (potentially) changed between the first fetch and the
27757c478bd9Sstevel@tonic-gate 	 * second
27767c478bd9Sstevel@tonic-gate 	 */
27777c478bd9Sstevel@tonic-gate 	case V_WRITE:
27787c478bd9Sstevel@tonic-gate 		if (atomic_add_64_nv((&(vp->v_mmap_write)), 0))
27797c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27807c478bd9Sstevel@tonic-gate 		break;
27817c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
27827c478bd9Sstevel@tonic-gate 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) &&
27837c478bd9Sstevel@tonic-gate 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
27847c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27857c478bd9Sstevel@tonic-gate 		break;
27867c478bd9Sstevel@tonic-gate 	case V_RDORWR:
27877c478bd9Sstevel@tonic-gate 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) ||
27887c478bd9Sstevel@tonic-gate 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
27897c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27907c478bd9Sstevel@tonic-gate 		break;
27917c478bd9Sstevel@tonic-gate 	case V_READ:
27927c478bd9Sstevel@tonic-gate 		if (atomic_add_64_nv((&(vp->v_mmap_read)), 0))
27937c478bd9Sstevel@tonic-gate 			return (V_TRUE);
27947c478bd9Sstevel@tonic-gate 		break;
27957c478bd9Sstevel@tonic-gate 	}
27967c478bd9Sstevel@tonic-gate #else
27977c478bd9Sstevel@tonic-gate 	switch (mode) {
27987c478bd9Sstevel@tonic-gate 	case V_WRITE:
27997c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_write)
28007c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28017c478bd9Sstevel@tonic-gate 		break;
28027c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
28037c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read && vp->v_mmap_write)
28047c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28057c478bd9Sstevel@tonic-gate 		break;
28067c478bd9Sstevel@tonic-gate 	case V_RDORWR:
28077c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read || vp->v_mmap_write)
28087c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28097c478bd9Sstevel@tonic-gate 		break;
28107c478bd9Sstevel@tonic-gate 	case V_READ:
28117c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read)
28127c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28137c478bd9Sstevel@tonic-gate 		break;
28147c478bd9Sstevel@tonic-gate 	}
28157c478bd9Sstevel@tonic-gate #endif
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 	return (V_FALSE);
28187c478bd9Sstevel@tonic-gate }
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate /*
28217c478bd9Sstevel@tonic-gate  * Set the operations vector for a vnode.
28227c478bd9Sstevel@tonic-gate  *
28237c478bd9Sstevel@tonic-gate  * FEM ensures that the v_femhead pointer is filled in before the
28247c478bd9Sstevel@tonic-gate  * v_op pointer is changed.  This means that if the v_femhead pointer
28257c478bd9Sstevel@tonic-gate  * is NULL, and the v_op field hasn't changed since before which checked
28267c478bd9Sstevel@tonic-gate  * the v_femhead pointer; then our update is ok - we are not racing with
28277c478bd9Sstevel@tonic-gate  * FEM.
28287c478bd9Sstevel@tonic-gate  */
28297c478bd9Sstevel@tonic-gate void
28307c478bd9Sstevel@tonic-gate vn_setops(vnode_t *vp, vnodeops_t *vnodeops)
28317c478bd9Sstevel@tonic-gate {
28327c478bd9Sstevel@tonic-gate 	vnodeops_t	*op;
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
28357c478bd9Sstevel@tonic-gate 	ASSERT(vnodeops != NULL);
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 	op = vp->v_op;
28387c478bd9Sstevel@tonic-gate 	membar_consumer();
28397c478bd9Sstevel@tonic-gate 	/*
28407c478bd9Sstevel@tonic-gate 	 * If vp->v_femhead == NULL, then we'll call casptr() to do the
28417c478bd9Sstevel@tonic-gate 	 * compare-and-swap on vp->v_op.  If either fails, then FEM is
28427c478bd9Sstevel@tonic-gate 	 * in effect on the vnode and we need to have FEM deal with it.
28437c478bd9Sstevel@tonic-gate 	 */
28447c478bd9Sstevel@tonic-gate 	if (vp->v_femhead != NULL || casptr(&vp->v_op, op, vnodeops) != op) {
28457c478bd9Sstevel@tonic-gate 		fem_setvnops(vp, vnodeops);
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate }
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate /*
28507c478bd9Sstevel@tonic-gate  * Retrieve the operations vector for a vnode
28517c478bd9Sstevel@tonic-gate  * As with vn_setops(above); make sure we aren't racing with FEM.
28527c478bd9Sstevel@tonic-gate  * FEM sets the v_op to a special, internal, vnodeops that wouldn't
28537c478bd9Sstevel@tonic-gate  * make sense to the callers of this routine.
28547c478bd9Sstevel@tonic-gate  */
28557c478bd9Sstevel@tonic-gate vnodeops_t *
28567c478bd9Sstevel@tonic-gate vn_getops(vnode_t *vp)
28577c478bd9Sstevel@tonic-gate {
28587c478bd9Sstevel@tonic-gate 	vnodeops_t	*op;
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	op = vp->v_op;
28637c478bd9Sstevel@tonic-gate 	membar_consumer();
28647c478bd9Sstevel@tonic-gate 	if (vp->v_femhead == NULL && op == vp->v_op) {
28657c478bd9Sstevel@tonic-gate 		return (op);
28667c478bd9Sstevel@tonic-gate 	} else {
28677c478bd9Sstevel@tonic-gate 		return (fem_getvnops(vp));
28687c478bd9Sstevel@tonic-gate 	}
28697c478bd9Sstevel@tonic-gate }
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate /*
28727c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the vnodeops matches that of the vnode.
28737c478bd9Sstevel@tonic-gate  * Returns zero (0) if not.
28747c478bd9Sstevel@tonic-gate  */
28757c478bd9Sstevel@tonic-gate int
28767c478bd9Sstevel@tonic-gate vn_matchops(vnode_t *vp, vnodeops_t *vnodeops)
28777c478bd9Sstevel@tonic-gate {
28787c478bd9Sstevel@tonic-gate 	return (vn_getops(vp) == vnodeops);
28797c478bd9Sstevel@tonic-gate }
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate /*
28827c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the specified operation matches the
28837c478bd9Sstevel@tonic-gate  * corresponding operation for that the vnode.
28847c478bd9Sstevel@tonic-gate  * Returns zero (0) if not.
28857c478bd9Sstevel@tonic-gate  */
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate #define	MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0))
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate int
28907c478bd9Sstevel@tonic-gate vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp)
28917c478bd9Sstevel@tonic-gate {
28927c478bd9Sstevel@tonic-gate 	const fs_operation_trans_def_t *otdp;
28937c478bd9Sstevel@tonic-gate 	fs_generic_func_p *loc = NULL;
28947c478bd9Sstevel@tonic-gate 	vnodeops_t	*vop = vn_getops(vp);
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 	ASSERT(vopname != NULL);
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	for (otdp = vn_ops_table; otdp->name != NULL; otdp++) {
28997c478bd9Sstevel@tonic-gate 		if (MATCHNAME(otdp->name, vopname)) {
2900b9c98e29Spf199842 			loc = (fs_generic_func_p *)
2901b9c98e29Spf199842 			    ((char *)(vop) + otdp->offset);
29027c478bd9Sstevel@tonic-gate 			break;
29037c478bd9Sstevel@tonic-gate 		}
29047c478bd9Sstevel@tonic-gate 	}
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 	return ((loc != NULL) && (*loc == funcp));
29077c478bd9Sstevel@tonic-gate }
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate /*
29107c478bd9Sstevel@tonic-gate  * fs_new_caller_id() needs to return a unique ID on a given local system.
29117c478bd9Sstevel@tonic-gate  * The IDs do not need to survive across reboots.  These are primarily
29127c478bd9Sstevel@tonic-gate  * used so that (FEM) monitors can detect particular callers (such as
29137c478bd9Sstevel@tonic-gate  * the NFS server) to a given vnode/vfs operation.
29147c478bd9Sstevel@tonic-gate  */
29157c478bd9Sstevel@tonic-gate u_longlong_t
29167c478bd9Sstevel@tonic-gate fs_new_caller_id()
29177c478bd9Sstevel@tonic-gate {
29187c478bd9Sstevel@tonic-gate 	static uint64_t next_caller_id = 0LL; /* First call returns 1 */
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate 	return ((u_longlong_t)atomic_add_64_nv(&next_caller_id, 1));
29217c478bd9Sstevel@tonic-gate }
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate /*
29247c478bd9Sstevel@tonic-gate  * Given a starting vnode and a path, updates the path in the target vnode in
29257c478bd9Sstevel@tonic-gate  * a safe manner.  If the vnode already has path information embedded, then the
2926ca2c3138Seschrock  * cached path is left untouched.
29277c478bd9Sstevel@tonic-gate  */
292848011479Ssn199410 
292948011479Ssn199410 size_t max_vnode_path = 4 * MAXPATHLEN;
293048011479Ssn199410 
29317c478bd9Sstevel@tonic-gate void
29327c478bd9Sstevel@tonic-gate vn_setpath(vnode_t *rootvp, struct vnode *startvp, struct vnode *vp,
29337c478bd9Sstevel@tonic-gate     const char *path, size_t plen)
29347c478bd9Sstevel@tonic-gate {
29357c478bd9Sstevel@tonic-gate 	char	*rpath;
29367c478bd9Sstevel@tonic-gate 	vnode_t	*base;
29377c478bd9Sstevel@tonic-gate 	size_t	rpathlen, rpathalloc;
29387c478bd9Sstevel@tonic-gate 	int	doslash = 1;
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	if (*path == '/') {
29417c478bd9Sstevel@tonic-gate 		base = rootvp;
29427c478bd9Sstevel@tonic-gate 		path++;
29437c478bd9Sstevel@tonic-gate 		plen--;
29447c478bd9Sstevel@tonic-gate 	} else {
29457c478bd9Sstevel@tonic-gate 		base = startvp;
29467c478bd9Sstevel@tonic-gate 	}
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate 	/*
29497c478bd9Sstevel@tonic-gate 	 * We cannot grab base->v_lock while we hold vp->v_lock because of
29507c478bd9Sstevel@tonic-gate 	 * the potential for deadlock.
29517c478bd9Sstevel@tonic-gate 	 */
29527c478bd9Sstevel@tonic-gate 	mutex_enter(&base->v_lock);
29537c478bd9Sstevel@tonic-gate 	if (base->v_path == NULL) {
29547c478bd9Sstevel@tonic-gate 		mutex_exit(&base->v_lock);
29557c478bd9Sstevel@tonic-gate 		return;
29567c478bd9Sstevel@tonic-gate 	}
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate 	rpathlen = strlen(base->v_path);
29597c478bd9Sstevel@tonic-gate 	rpathalloc = rpathlen + plen + 1;
29607c478bd9Sstevel@tonic-gate 	/* Avoid adding a slash if there's already one there */
29617c478bd9Sstevel@tonic-gate 	if (base->v_path[rpathlen-1] == '/')
29627c478bd9Sstevel@tonic-gate 		doslash = 0;
29637c478bd9Sstevel@tonic-gate 	else
29647c478bd9Sstevel@tonic-gate 		rpathalloc++;
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate 	/*
29677c478bd9Sstevel@tonic-gate 	 * We don't want to call kmem_alloc(KM_SLEEP) with kernel locks held,
29687c478bd9Sstevel@tonic-gate 	 * so we must do this dance.  If, by chance, something changes the path,
29697c478bd9Sstevel@tonic-gate 	 * just give up since there is no real harm.
29707c478bd9Sstevel@tonic-gate 	 */
29717c478bd9Sstevel@tonic-gate 	mutex_exit(&base->v_lock);
29727c478bd9Sstevel@tonic-gate 
297348011479Ssn199410 	/* Paths should stay within reason */
297448011479Ssn199410 	if (rpathalloc > max_vnode_path)
297548011479Ssn199410 		return;
297648011479Ssn199410 
29777c478bd9Sstevel@tonic-gate 	rpath = kmem_alloc(rpathalloc, KM_SLEEP);
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate 	mutex_enter(&base->v_lock);
29807c478bd9Sstevel@tonic-gate 	if (base->v_path == NULL || strlen(base->v_path) != rpathlen) {
29817c478bd9Sstevel@tonic-gate 		mutex_exit(&base->v_lock);
29827c478bd9Sstevel@tonic-gate 		kmem_free(rpath, rpathalloc);
29837c478bd9Sstevel@tonic-gate 		return;
29847c478bd9Sstevel@tonic-gate 	}
29857c478bd9Sstevel@tonic-gate 	bcopy(base->v_path, rpath, rpathlen);
29867c478bd9Sstevel@tonic-gate 	mutex_exit(&base->v_lock);
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate 	if (doslash)
29897c478bd9Sstevel@tonic-gate 		rpath[rpathlen++] = '/';
29907c478bd9Sstevel@tonic-gate 	bcopy(path, rpath + rpathlen, plen);
29917c478bd9Sstevel@tonic-gate 	rpath[rpathlen + plen] = '\0';
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
29947c478bd9Sstevel@tonic-gate 	if (vp->v_path != NULL) {
29957c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
29967c478bd9Sstevel@tonic-gate 		kmem_free(rpath, rpathalloc);
29977c478bd9Sstevel@tonic-gate 	} else {
29987c478bd9Sstevel@tonic-gate 		vp->v_path = rpath;
29997c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
30007c478bd9Sstevel@tonic-gate 	}
30017c478bd9Sstevel@tonic-gate }
30027c478bd9Sstevel@tonic-gate 
30037c478bd9Sstevel@tonic-gate /*
30047c478bd9Sstevel@tonic-gate  * Sets the path to the vnode to be the given string, regardless of current
30057c478bd9Sstevel@tonic-gate  * context.  The string must be a complete path from rootdir.  This is only used
30067c478bd9Sstevel@tonic-gate  * by fsop_root() for setting the path based on the mountpoint.
30077c478bd9Sstevel@tonic-gate  */
30087c478bd9Sstevel@tonic-gate void
30097c478bd9Sstevel@tonic-gate vn_setpath_str(struct vnode *vp, const char *str, size_t len)
30107c478bd9Sstevel@tonic-gate {
30117c478bd9Sstevel@tonic-gate 	char *buf = kmem_alloc(len + 1, KM_SLEEP);
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
30147c478bd9Sstevel@tonic-gate 	if (vp->v_path != NULL) {
30157c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
30167c478bd9Sstevel@tonic-gate 		kmem_free(buf, len + 1);
30177c478bd9Sstevel@tonic-gate 		return;
30187c478bd9Sstevel@tonic-gate 	}
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 	vp->v_path = buf;
30217c478bd9Sstevel@tonic-gate 	bcopy(str, vp->v_path, len);
30227c478bd9Sstevel@tonic-gate 	vp->v_path[len] = '\0';
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
30257c478bd9Sstevel@tonic-gate }
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate /*
302851ece835Seschrock  * Called from within filesystem's vop_rename() to handle renames once the
302951ece835Seschrock  * target vnode is available.
303051ece835Seschrock  */
303151ece835Seschrock void
303251ece835Seschrock vn_renamepath(vnode_t *dvp, vnode_t *vp, const char *nm, size_t len)
303351ece835Seschrock {
303451ece835Seschrock 	char *tmp;
303551ece835Seschrock 
303651ece835Seschrock 	mutex_enter(&vp->v_lock);
303751ece835Seschrock 	tmp = vp->v_path;
303851ece835Seschrock 	vp->v_path = NULL;
303951ece835Seschrock 	mutex_exit(&vp->v_lock);
304051ece835Seschrock 	vn_setpath(rootdir, dvp, vp, nm, len);
304151ece835Seschrock 	if (tmp != NULL)
304251ece835Seschrock 		kmem_free(tmp, strlen(tmp) + 1);
304351ece835Seschrock }
304451ece835Seschrock 
304551ece835Seschrock /*
30467c478bd9Sstevel@tonic-gate  * Similar to vn_setpath_str(), this function sets the path of the destination
30477c478bd9Sstevel@tonic-gate  * vnode to the be the same as the source vnode.
30487c478bd9Sstevel@tonic-gate  */
30497c478bd9Sstevel@tonic-gate void
30507c478bd9Sstevel@tonic-gate vn_copypath(struct vnode *src, struct vnode *dst)
30517c478bd9Sstevel@tonic-gate {
30527c478bd9Sstevel@tonic-gate 	char *buf;
30537c478bd9Sstevel@tonic-gate 	int alloc;
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 	mutex_enter(&src->v_lock);
30567c478bd9Sstevel@tonic-gate 	if (src->v_path == NULL) {
30577c478bd9Sstevel@tonic-gate 		mutex_exit(&src->v_lock);
30587c478bd9Sstevel@tonic-gate 		return;
30597c478bd9Sstevel@tonic-gate 	}
30607c478bd9Sstevel@tonic-gate 	alloc = strlen(src->v_path) + 1;
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 	/* avoid kmem_alloc() with lock held */
30637c478bd9Sstevel@tonic-gate 	mutex_exit(&src->v_lock);
30647c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(alloc, KM_SLEEP);
30657c478bd9Sstevel@tonic-gate 	mutex_enter(&src->v_lock);
30667c478bd9Sstevel@tonic-gate 	if (src->v_path == NULL || strlen(src->v_path) + 1 != alloc) {
30677c478bd9Sstevel@tonic-gate 		mutex_exit(&src->v_lock);
30687c478bd9Sstevel@tonic-gate 		kmem_free(buf, alloc);
30697c478bd9Sstevel@tonic-gate 		return;
30707c478bd9Sstevel@tonic-gate 	}
30717c478bd9Sstevel@tonic-gate 	bcopy(src->v_path, buf, alloc);
30727c478bd9Sstevel@tonic-gate 	mutex_exit(&src->v_lock);
30737c478bd9Sstevel@tonic-gate 
30747c478bd9Sstevel@tonic-gate 	mutex_enter(&dst->v_lock);
30757c478bd9Sstevel@tonic-gate 	if (dst->v_path != NULL) {
30767c478bd9Sstevel@tonic-gate 		mutex_exit(&dst->v_lock);
30777c478bd9Sstevel@tonic-gate 		kmem_free(buf, alloc);
30787c478bd9Sstevel@tonic-gate 		return;
30797c478bd9Sstevel@tonic-gate 	}
30807c478bd9Sstevel@tonic-gate 	dst->v_path = buf;
30817c478bd9Sstevel@tonic-gate 	mutex_exit(&dst->v_lock);
30827c478bd9Sstevel@tonic-gate }
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate /*
30857c478bd9Sstevel@tonic-gate  * XXX Private interface for segvn routines that handle vnode
30867c478bd9Sstevel@tonic-gate  * large page segments.
30877c478bd9Sstevel@tonic-gate  *
30887c478bd9Sstevel@tonic-gate  * return 1 if vp's file system VOP_PAGEIO() implementation
30897c478bd9Sstevel@tonic-gate  * can be safely used instead of VOP_GETPAGE() for handling
30907c478bd9Sstevel@tonic-gate  * pagefaults against regular non swap files. VOP_PAGEIO()
30917c478bd9Sstevel@tonic-gate  * interface is considered safe here if its implementation
30927c478bd9Sstevel@tonic-gate  * is very close to VOP_GETPAGE() implementation.
30937c478bd9Sstevel@tonic-gate  * e.g. It zero's out the part of the page beyond EOF. Doesn't
30947c478bd9Sstevel@tonic-gate  * panic if there're file holes but instead returns an error.
30957c478bd9Sstevel@tonic-gate  * Doesn't assume file won't be changed by user writes, etc.
30967c478bd9Sstevel@tonic-gate  *
30977c478bd9Sstevel@tonic-gate  * return 0 otherwise.
30987c478bd9Sstevel@tonic-gate  *
30997c478bd9Sstevel@tonic-gate  * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs.
31007c478bd9Sstevel@tonic-gate  */
31017c478bd9Sstevel@tonic-gate int
31027c478bd9Sstevel@tonic-gate vn_vmpss_usepageio(vnode_t *vp)
31037c478bd9Sstevel@tonic-gate {
31047c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
31057c478bd9Sstevel@tonic-gate 	char *fsname = vfssw[vfsp->vfs_fstype].vsw_name;
31067c478bd9Sstevel@tonic-gate 	char *pageio_ok_fss[] = {"ufs", "nfs", NULL};
31077c478bd9Sstevel@tonic-gate 	char **fsok = pageio_ok_fss;
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate 	if (fsname == NULL) {
31107c478bd9Sstevel@tonic-gate 		return (0);
31117c478bd9Sstevel@tonic-gate 	}
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 	for (; *fsok; fsok++) {
31147c478bd9Sstevel@tonic-gate 		if (strcmp(*fsok, fsname) == 0) {
31157c478bd9Sstevel@tonic-gate 			return (1);
31167c478bd9Sstevel@tonic-gate 		}
31177c478bd9Sstevel@tonic-gate 	}
31187c478bd9Sstevel@tonic-gate 	return (0);
31197c478bd9Sstevel@tonic-gate }
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate /* VOP_XXX() macros call the corresponding fop_xxx() function */
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate int
31247c478bd9Sstevel@tonic-gate fop_open(
31257c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
31267c478bd9Sstevel@tonic-gate 	int mode,
3127da6c28aaSamw 	cred_t *cr,
3128da6c28aaSamw 	caller_context_t *ct)
31297c478bd9Sstevel@tonic-gate {
31307c478bd9Sstevel@tonic-gate 	int ret;
31317c478bd9Sstevel@tonic-gate 	vnode_t *vp = *vpp;
31327c478bd9Sstevel@tonic-gate 
31337c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
31347c478bd9Sstevel@tonic-gate 	/*
31357c478bd9Sstevel@tonic-gate 	 * Adding to the vnode counts before calling open
31367c478bd9Sstevel@tonic-gate 	 * avoids the need for a mutex. It circumvents a race
31377c478bd9Sstevel@tonic-gate 	 * condition where a query made on the vnode counts results in a
31387c478bd9Sstevel@tonic-gate 	 * false negative. The inquirer goes away believing the file is
31397c478bd9Sstevel@tonic-gate 	 * not open when there is an open on the file already under way.
31407c478bd9Sstevel@tonic-gate 	 *
31417c478bd9Sstevel@tonic-gate 	 * The counts are meant to prevent NFS from granting a delegation
31427c478bd9Sstevel@tonic-gate 	 * when it would be dangerous to do so.
31437c478bd9Sstevel@tonic-gate 	 *
31447c478bd9Sstevel@tonic-gate 	 * The vnode counts are only kept on regular files
31457c478bd9Sstevel@tonic-gate 	 */
31467c478bd9Sstevel@tonic-gate 	if ((*vpp)->v_type == VREG) {
31477c478bd9Sstevel@tonic-gate 		if (mode & FREAD)
31487c478bd9Sstevel@tonic-gate 			atomic_add_32(&((*vpp)->v_rdcnt), 1);
31497c478bd9Sstevel@tonic-gate 		if (mode & FWRITE)
31507c478bd9Sstevel@tonic-gate 			atomic_add_32(&((*vpp)->v_wrcnt), 1);
31517c478bd9Sstevel@tonic-gate 	}
31527c478bd9Sstevel@tonic-gate 
3153f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3154f48205beScasper 
3155da6c28aaSamw 	ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr, ct);
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate 	if (ret) {
31587c478bd9Sstevel@tonic-gate 		/*
31597c478bd9Sstevel@tonic-gate 		 * Use the saved vp just in case the vnode ptr got trashed
31607c478bd9Sstevel@tonic-gate 		 * by the error.
31617c478bd9Sstevel@tonic-gate 		 */
31622bb1cb30Sbmc 		VOPSTATS_UPDATE(vp, open);
31637c478bd9Sstevel@tonic-gate 		if ((vp->v_type == VREG) && (mode & FREAD))
31647c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_rdcnt), -1);
31657c478bd9Sstevel@tonic-gate 		if ((vp->v_type == VREG) && (mode & FWRITE))
31667c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_wrcnt), -1);
31677c478bd9Sstevel@tonic-gate 	} else {
31687c478bd9Sstevel@tonic-gate 		/*
31697c478bd9Sstevel@tonic-gate 		 * Some filesystems will return a different vnode,
31707c478bd9Sstevel@tonic-gate 		 * but the same path was still used to open it.
31717c478bd9Sstevel@tonic-gate 		 * So if we do change the vnode and need to
31727c478bd9Sstevel@tonic-gate 		 * copy over the path, do so here, rather than special
31737c478bd9Sstevel@tonic-gate 		 * casing each filesystem. Adjust the vnode counts to
31747c478bd9Sstevel@tonic-gate 		 * reflect the vnode switch.
31757c478bd9Sstevel@tonic-gate 		 */
31762bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, open);
31777c478bd9Sstevel@tonic-gate 		if (*vpp != vp && *vpp != NULL) {
31787c478bd9Sstevel@tonic-gate 			vn_copypath(vp, *vpp);
31797c478bd9Sstevel@tonic-gate 			if (((*vpp)->v_type == VREG) && (mode & FREAD))
31807c478bd9Sstevel@tonic-gate 				atomic_add_32(&((*vpp)->v_rdcnt), 1);
31817c478bd9Sstevel@tonic-gate 			if ((vp->v_type == VREG) && (mode & FREAD))
31827c478bd9Sstevel@tonic-gate 				atomic_add_32(&(vp->v_rdcnt), -1);
31837c478bd9Sstevel@tonic-gate 			if (((*vpp)->v_type == VREG) && (mode & FWRITE))
31847c478bd9Sstevel@tonic-gate 				atomic_add_32(&((*vpp)->v_wrcnt), 1);
31857c478bd9Sstevel@tonic-gate 			if ((vp->v_type == VREG) && (mode & FWRITE))
31867c478bd9Sstevel@tonic-gate 				atomic_add_32(&(vp->v_wrcnt), -1);
31877c478bd9Sstevel@tonic-gate 		}
31887c478bd9Sstevel@tonic-gate 	}
31897c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
31907c478bd9Sstevel@tonic-gate 	return (ret);
31917c478bd9Sstevel@tonic-gate }
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate int
31947c478bd9Sstevel@tonic-gate fop_close(
31957c478bd9Sstevel@tonic-gate 	vnode_t *vp,
31967c478bd9Sstevel@tonic-gate 	int flag,
31977c478bd9Sstevel@tonic-gate 	int count,
31987c478bd9Sstevel@tonic-gate 	offset_t offset,
3199da6c28aaSamw 	cred_t *cr,
3200da6c28aaSamw 	caller_context_t *ct)
32017c478bd9Sstevel@tonic-gate {
32025a59a8b3Srsb 	int err;
32035a59a8b3Srsb 
3204f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3205f48205beScasper 
3206da6c28aaSamw 	err = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr, ct);
32072bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, close);
32087c478bd9Sstevel@tonic-gate 	/*
32097c478bd9Sstevel@tonic-gate 	 * Check passed in count to handle possible dups. Vnode counts are only
32107c478bd9Sstevel@tonic-gate 	 * kept on regular files
32117c478bd9Sstevel@tonic-gate 	 */
32127c478bd9Sstevel@tonic-gate 	if ((vp->v_type == VREG) && (count == 1))  {
32137c478bd9Sstevel@tonic-gate 		if (flag & FREAD) {
32147c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_rdcnt > 0);
32157c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_rdcnt), -1);
32167c478bd9Sstevel@tonic-gate 		}
32177c478bd9Sstevel@tonic-gate 		if (flag & FWRITE) {
32187c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_wrcnt > 0);
32197c478bd9Sstevel@tonic-gate 			atomic_add_32(&(vp->v_wrcnt), -1);
32207c478bd9Sstevel@tonic-gate 		}
32217c478bd9Sstevel@tonic-gate 	}
32225a59a8b3Srsb 	return (err);
32237c478bd9Sstevel@tonic-gate }
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate int
32267c478bd9Sstevel@tonic-gate fop_read(
32277c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32287c478bd9Sstevel@tonic-gate 	uio_t *uiop,
32297c478bd9Sstevel@tonic-gate 	int ioflag,
32307c478bd9Sstevel@tonic-gate 	cred_t *cr,
3231da6c28aaSamw 	caller_context_t *ct)
32327c478bd9Sstevel@tonic-gate {
32335a59a8b3Srsb 	int	err;
32345a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
32355a59a8b3Srsb 
3236f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3237f48205beScasper 
32385a59a8b3Srsb 	err = (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct);
32392bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, read,
32405a59a8b3Srsb 	    read_bytes, (resid_start - uiop->uio_resid));
32415a59a8b3Srsb 	return (err);
32427c478bd9Sstevel@tonic-gate }
32437c478bd9Sstevel@tonic-gate 
32447c478bd9Sstevel@tonic-gate int
32457c478bd9Sstevel@tonic-gate fop_write(
32467c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32477c478bd9Sstevel@tonic-gate 	uio_t *uiop,
32487c478bd9Sstevel@tonic-gate 	int ioflag,
32497c478bd9Sstevel@tonic-gate 	cred_t *cr,
3250da6c28aaSamw 	caller_context_t *ct)
32517c478bd9Sstevel@tonic-gate {
32525a59a8b3Srsb 	int	err;
32535a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
32545a59a8b3Srsb 
3255f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3256f48205beScasper 
32575a59a8b3Srsb 	err = (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct);
32582bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, write,
32595a59a8b3Srsb 	    write_bytes, (resid_start - uiop->uio_resid));
32605a59a8b3Srsb 	return (err);
32617c478bd9Sstevel@tonic-gate }
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate int
32647c478bd9Sstevel@tonic-gate fop_ioctl(
32657c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32667c478bd9Sstevel@tonic-gate 	int cmd,
32677c478bd9Sstevel@tonic-gate 	intptr_t arg,
32687c478bd9Sstevel@tonic-gate 	int flag,
32697c478bd9Sstevel@tonic-gate 	cred_t *cr,
3270da6c28aaSamw 	int *rvalp,
3271da6c28aaSamw 	caller_context_t *ct)
32727c478bd9Sstevel@tonic-gate {
32735a59a8b3Srsb 	int	err;
32745a59a8b3Srsb 
3275f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3276f48205beScasper 
3277da6c28aaSamw 	err = (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp, ct);
32782bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, ioctl);
32795a59a8b3Srsb 	return (err);
32807c478bd9Sstevel@tonic-gate }
32817c478bd9Sstevel@tonic-gate 
32827c478bd9Sstevel@tonic-gate int
32837c478bd9Sstevel@tonic-gate fop_setfl(
32847c478bd9Sstevel@tonic-gate 	vnode_t *vp,
32857c478bd9Sstevel@tonic-gate 	int oflags,
32867c478bd9Sstevel@tonic-gate 	int nflags,
3287da6c28aaSamw 	cred_t *cr,
3288da6c28aaSamw 	caller_context_t *ct)
32897c478bd9Sstevel@tonic-gate {
32905a59a8b3Srsb 	int	err;
32915a59a8b3Srsb 
3292f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3293f48205beScasper 
3294da6c28aaSamw 	err = (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr, ct);
32952bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setfl);
32965a59a8b3Srsb 	return (err);
32977c478bd9Sstevel@tonic-gate }
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate int
33007c478bd9Sstevel@tonic-gate fop_getattr(
33017c478bd9Sstevel@tonic-gate 	vnode_t *vp,
33027c478bd9Sstevel@tonic-gate 	vattr_t *vap,
33037c478bd9Sstevel@tonic-gate 	int flags,
3304da6c28aaSamw 	cred_t *cr,
3305da6c28aaSamw 	caller_context_t *ct)
33067c478bd9Sstevel@tonic-gate {
33075a59a8b3Srsb 	int	err;
33085a59a8b3Srsb 
3309f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3310f48205beScasper 
3311da6c28aaSamw 	/*
3312da6c28aaSamw 	 * If this file system doesn't understand the xvattr extensions
3313da6c28aaSamw 	 * then turn off the xvattr bit.
3314da6c28aaSamw 	 */
3315da6c28aaSamw 	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3316da6c28aaSamw 		vap->va_mask &= ~AT_XVATTR;
3317da6c28aaSamw 	}
3318da6c28aaSamw 
3319da6c28aaSamw 	/*
3320da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
3321da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
3322da6c28aaSamw 	 */
3323da6c28aaSamw 	if ((flags & ATTR_NOACLCHECK) &&
3324da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3325da6c28aaSamw 		return (EINVAL);
3326da6c28aaSamw 	}
3327da6c28aaSamw 	err = (*(vp)->v_op->vop_getattr)(vp, vap, flags, cr, ct);
33282bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getattr);
33295a59a8b3Srsb 	return (err);
33307c478bd9Sstevel@tonic-gate }
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate int
33337c478bd9Sstevel@tonic-gate fop_setattr(
33347c478bd9Sstevel@tonic-gate 	vnode_t *vp,
33357c478bd9Sstevel@tonic-gate 	vattr_t *vap,
33367c478bd9Sstevel@tonic-gate 	int flags,
33377c478bd9Sstevel@tonic-gate 	cred_t *cr,
33387c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
33397c478bd9Sstevel@tonic-gate {
33405a59a8b3Srsb 	int	err;
33415a59a8b3Srsb 
3342f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3343f48205beScasper 
3344da6c28aaSamw 	/*
3345da6c28aaSamw 	 * If this file system doesn't understand the xvattr extensions
3346da6c28aaSamw 	 * then turn off the xvattr bit.
3347da6c28aaSamw 	 */
3348da6c28aaSamw 	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3349da6c28aaSamw 		vap->va_mask &= ~AT_XVATTR;
3350da6c28aaSamw 	}
3351da6c28aaSamw 
3352da6c28aaSamw 	/*
3353da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
3354da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
3355da6c28aaSamw 	 */
3356da6c28aaSamw 	if ((flags & ATTR_NOACLCHECK) &&
3357da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3358da6c28aaSamw 		return (EINVAL);
3359da6c28aaSamw 	}
33605a59a8b3Srsb 	err = (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct);
33612bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setattr);
33625a59a8b3Srsb 	return (err);
33637c478bd9Sstevel@tonic-gate }
33647c478bd9Sstevel@tonic-gate 
33657c478bd9Sstevel@tonic-gate int
33667c478bd9Sstevel@tonic-gate fop_access(
33677c478bd9Sstevel@tonic-gate 	vnode_t *vp,
33687c478bd9Sstevel@tonic-gate 	int mode,
33697c478bd9Sstevel@tonic-gate 	int flags,
3370da6c28aaSamw 	cred_t *cr,
3371da6c28aaSamw 	caller_context_t *ct)
33727c478bd9Sstevel@tonic-gate {
33735a59a8b3Srsb 	int	err;
33745a59a8b3Srsb 
3375da6c28aaSamw 	if ((flags & V_ACE_MASK) &&
3376da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3377da6c28aaSamw 		return (EINVAL);
3378da6c28aaSamw 	}
3379da6c28aaSamw 
3380f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3381f48205beScasper 
3382da6c28aaSamw 	err = (*(vp)->v_op->vop_access)(vp, mode, flags, cr, ct);
33832bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, access);
33845a59a8b3Srsb 	return (err);
33857c478bd9Sstevel@tonic-gate }
33867c478bd9Sstevel@tonic-gate 
33877c478bd9Sstevel@tonic-gate int
33887c478bd9Sstevel@tonic-gate fop_lookup(
33897c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
33907c478bd9Sstevel@tonic-gate 	char *nm,
33917c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
33927c478bd9Sstevel@tonic-gate 	pathname_t *pnp,
33937c478bd9Sstevel@tonic-gate 	int flags,
33947c478bd9Sstevel@tonic-gate 	vnode_t *rdir,
3395da6c28aaSamw 	cred_t *cr,
3396da6c28aaSamw 	caller_context_t *ct,
3397da6c28aaSamw 	int *deflags,		/* Returned per-dirent flags */
3398da6c28aaSamw 	pathname_t *ppnp)	/* Returned case-preserved name in directory */
33997c478bd9Sstevel@tonic-gate {
3400ca2c3138Seschrock 	int ret;
3401ca2c3138Seschrock 
3402da6c28aaSamw 	/*
3403da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3404da6c28aaSamw 	 * and said access is requested, fail quickly.  It is required
3405da6c28aaSamw 	 * that if the vfs supports case-insensitive lookup, it also
3406da6c28aaSamw 	 * supports extended dirent flags.
3407da6c28aaSamw 	 */
3408da6c28aaSamw 	if (flags & FIGNORECASE &&
3409da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3410da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3411da6c28aaSamw 		return (EINVAL);
3412da6c28aaSamw 
3413f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3414f48205beScasper 
3415da6c28aaSamw 	if ((flags & LOOKUP_XATTR) && (flags & LOOKUP_HAVE_SYSATTR_DIR) == 0) {
3416da6c28aaSamw 		ret = xattr_dir_lookup(dvp, vpp, flags, cr);
3417da6c28aaSamw 	} else {
3418da6c28aaSamw 		ret = (*(dvp)->v_op->vop_lookup)
3419da6c28aaSamw 		    (dvp, nm, vpp, pnp, flags, rdir, cr, ct, deflags, ppnp);
3420da6c28aaSamw 	}
34215a59a8b3Srsb 	if (ret == 0 && *vpp) {
34222bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, lookup);
34235a59a8b3Srsb 		if ((*vpp)->v_path == NULL) {
3424ca2c3138Seschrock 			vn_setpath(rootdir, dvp, *vpp, nm, strlen(nm));
34255a59a8b3Srsb 		}
34265a59a8b3Srsb 	}
3427ca2c3138Seschrock 
3428ca2c3138Seschrock 	return (ret);
34297c478bd9Sstevel@tonic-gate }
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate int
34327c478bd9Sstevel@tonic-gate fop_create(
34337c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
34347c478bd9Sstevel@tonic-gate 	char *name,
34357c478bd9Sstevel@tonic-gate 	vattr_t *vap,
34367c478bd9Sstevel@tonic-gate 	vcexcl_t excl,
34377c478bd9Sstevel@tonic-gate 	int mode,
34387c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
34397c478bd9Sstevel@tonic-gate 	cred_t *cr,
3440da6c28aaSamw 	int flags,
3441da6c28aaSamw 	caller_context_t *ct,
3442da6c28aaSamw 	vsecattr_t *vsecp)	/* ACL to set during create */
34437c478bd9Sstevel@tonic-gate {
34447c478bd9Sstevel@tonic-gate 	int ret;
34457c478bd9Sstevel@tonic-gate 
3446da6c28aaSamw 	if (vsecp != NULL &&
3447da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3448da6c28aaSamw 		return (EINVAL);
3449da6c28aaSamw 	}
3450da6c28aaSamw 	/*
3451da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3452da6c28aaSamw 	 * and said access is requested, fail quickly.
3453da6c28aaSamw 	 */
3454da6c28aaSamw 	if (flags & FIGNORECASE &&
3455da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3456da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3457da6c28aaSamw 		return (EINVAL);
3458da6c28aaSamw 
3459f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3460f48205beScasper 
34617c478bd9Sstevel@tonic-gate 	ret = (*(dvp)->v_op->vop_create)
3462da6c28aaSamw 	    (dvp, name, vap, excl, mode, vpp, cr, flags, ct, vsecp);
34635a59a8b3Srsb 	if (ret == 0 && *vpp) {
34642bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, create);
34655a59a8b3Srsb 		if ((*vpp)->v_path == NULL) {
3466ca2c3138Seschrock 			vn_setpath(rootdir, dvp, *vpp, name, strlen(name));
34675a59a8b3Srsb 		}
34685a59a8b3Srsb 	}
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 	return (ret);
34717c478bd9Sstevel@tonic-gate }
34727c478bd9Sstevel@tonic-gate 
34737c478bd9Sstevel@tonic-gate int
34747c478bd9Sstevel@tonic-gate fop_remove(
34757c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
34767c478bd9Sstevel@tonic-gate 	char *nm,
3477da6c28aaSamw 	cred_t *cr,
3478da6c28aaSamw 	caller_context_t *ct,
3479da6c28aaSamw 	int flags)
34807c478bd9Sstevel@tonic-gate {
34815a59a8b3Srsb 	int	err;
34825a59a8b3Srsb 
3483da6c28aaSamw 	/*
3484da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3485da6c28aaSamw 	 * and said access is requested, fail quickly.
3486da6c28aaSamw 	 */
3487da6c28aaSamw 	if (flags & FIGNORECASE &&
3488da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3489da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3490da6c28aaSamw 		return (EINVAL);
3491da6c28aaSamw 
3492f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3493f48205beScasper 
3494da6c28aaSamw 	err = (*(dvp)->v_op->vop_remove)(dvp, nm, cr, ct, flags);
34952bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, remove);
34965a59a8b3Srsb 	return (err);
34977c478bd9Sstevel@tonic-gate }
34987c478bd9Sstevel@tonic-gate 
34997c478bd9Sstevel@tonic-gate int
35007c478bd9Sstevel@tonic-gate fop_link(
35017c478bd9Sstevel@tonic-gate 	vnode_t *tdvp,
35027c478bd9Sstevel@tonic-gate 	vnode_t *svp,
35037c478bd9Sstevel@tonic-gate 	char *tnm,
3504da6c28aaSamw 	cred_t *cr,
3505da6c28aaSamw 	caller_context_t *ct,
3506da6c28aaSamw 	int flags)
35077c478bd9Sstevel@tonic-gate {
35085a59a8b3Srsb 	int	err;
35095a59a8b3Srsb 
3510da6c28aaSamw 	/*
3511da6c28aaSamw 	 * If the target file system doesn't support case-insensitive access
3512da6c28aaSamw 	 * and said access is requested, fail quickly.
3513da6c28aaSamw 	 */
3514da6c28aaSamw 	if (flags & FIGNORECASE &&
3515da6c28aaSamw 	    (vfs_has_feature(tdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3516da6c28aaSamw 	    vfs_has_feature(tdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3517da6c28aaSamw 		return (EINVAL);
3518da6c28aaSamw 
3519f48205beScasper 	VOPXID_MAP_CR(tdvp, cr);
3520f48205beScasper 
3521da6c28aaSamw 	err = (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr, ct, flags);
35222bb1cb30Sbmc 	VOPSTATS_UPDATE(tdvp, link);
35235a59a8b3Srsb 	return (err);
35247c478bd9Sstevel@tonic-gate }
35257c478bd9Sstevel@tonic-gate 
35267c478bd9Sstevel@tonic-gate int
35277c478bd9Sstevel@tonic-gate fop_rename(
35287c478bd9Sstevel@tonic-gate 	vnode_t *sdvp,
35297c478bd9Sstevel@tonic-gate 	char *snm,
35307c478bd9Sstevel@tonic-gate 	vnode_t *tdvp,
35317c478bd9Sstevel@tonic-gate 	char *tnm,
3532da6c28aaSamw 	cred_t *cr,
3533da6c28aaSamw 	caller_context_t *ct,
3534da6c28aaSamw 	int flags)
35357c478bd9Sstevel@tonic-gate {
35365a59a8b3Srsb 	int	err;
35375a59a8b3Srsb 
3538da6c28aaSamw 	/*
3539da6c28aaSamw 	 * If the file system involved does not support
3540da6c28aaSamw 	 * case-insensitive access and said access is requested, fail
3541da6c28aaSamw 	 * quickly.
3542da6c28aaSamw 	 */
3543da6c28aaSamw 	if (flags & FIGNORECASE &&
3544da6c28aaSamw 	    ((vfs_has_feature(sdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3545da6c28aaSamw 	    vfs_has_feature(sdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0)))
3546da6c28aaSamw 		return (EINVAL);
3547da6c28aaSamw 
3548f48205beScasper 	VOPXID_MAP_CR(tdvp, cr);
3549f48205beScasper 
3550da6c28aaSamw 	err = (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr, ct, flags);
35512bb1cb30Sbmc 	VOPSTATS_UPDATE(sdvp, rename);
35525a59a8b3Srsb 	return (err);
35537c478bd9Sstevel@tonic-gate }
35547c478bd9Sstevel@tonic-gate 
35557c478bd9Sstevel@tonic-gate int
35567c478bd9Sstevel@tonic-gate fop_mkdir(
35577c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
35587c478bd9Sstevel@tonic-gate 	char *dirname,
35597c478bd9Sstevel@tonic-gate 	vattr_t *vap,
35607c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
3561da6c28aaSamw 	cred_t *cr,
3562da6c28aaSamw 	caller_context_t *ct,
3563da6c28aaSamw 	int flags,
3564da6c28aaSamw 	vsecattr_t *vsecp)	/* ACL to set during create */
35657c478bd9Sstevel@tonic-gate {
35667c478bd9Sstevel@tonic-gate 	int ret;
35677c478bd9Sstevel@tonic-gate 
3568da6c28aaSamw 	if (vsecp != NULL &&
3569da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3570da6c28aaSamw 		return (EINVAL);
3571da6c28aaSamw 	}
3572da6c28aaSamw 	/*
3573da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3574da6c28aaSamw 	 * and said access is requested, fail quickly.
3575da6c28aaSamw 	 */
3576da6c28aaSamw 	if (flags & FIGNORECASE &&
3577da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3578da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3579da6c28aaSamw 		return (EINVAL);
3580da6c28aaSamw 
3581f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3582f48205beScasper 
3583da6c28aaSamw 	ret = (*(dvp)->v_op->vop_mkdir)
3584da6c28aaSamw 	    (dvp, dirname, vap, vpp, cr, ct, flags, vsecp);
35855a59a8b3Srsb 	if (ret == 0 && *vpp) {
35862bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, mkdir);
35875a59a8b3Srsb 		if ((*vpp)->v_path == NULL) {
35885a59a8b3Srsb 			vn_setpath(rootdir, dvp, *vpp, dirname,
35895a59a8b3Srsb 			    strlen(dirname));
35905a59a8b3Srsb 		}
35915a59a8b3Srsb 	}
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate 	return (ret);
35947c478bd9Sstevel@tonic-gate }
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate int
35977c478bd9Sstevel@tonic-gate fop_rmdir(
35987c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
35997c478bd9Sstevel@tonic-gate 	char *nm,
36007c478bd9Sstevel@tonic-gate 	vnode_t *cdir,
3601da6c28aaSamw 	cred_t *cr,
3602da6c28aaSamw 	caller_context_t *ct,
3603da6c28aaSamw 	int flags)
36047c478bd9Sstevel@tonic-gate {
36055a59a8b3Srsb 	int	err;
36065a59a8b3Srsb 
3607da6c28aaSamw 	/*
3608da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3609da6c28aaSamw 	 * and said access is requested, fail quickly.
3610da6c28aaSamw 	 */
3611da6c28aaSamw 	if (flags & FIGNORECASE &&
3612da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3613da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3614da6c28aaSamw 		return (EINVAL);
3615da6c28aaSamw 
3616f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3617f48205beScasper 
3618da6c28aaSamw 	err = (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr, ct, flags);
36192bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, rmdir);
36205a59a8b3Srsb 	return (err);
36217c478bd9Sstevel@tonic-gate }
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate int
36247c478bd9Sstevel@tonic-gate fop_readdir(
36257c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36267c478bd9Sstevel@tonic-gate 	uio_t *uiop,
36277c478bd9Sstevel@tonic-gate 	cred_t *cr,
3628da6c28aaSamw 	int *eofp,
3629da6c28aaSamw 	caller_context_t *ct,
3630da6c28aaSamw 	int flags)
36317c478bd9Sstevel@tonic-gate {
36325a59a8b3Srsb 	int	err;
36335a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
36345a59a8b3Srsb 
3635da6c28aaSamw 	/*
3636da6c28aaSamw 	 * If this file system doesn't support retrieving directory
3637da6c28aaSamw 	 * entry flags and said access is requested, fail quickly.
3638da6c28aaSamw 	 */
3639da6c28aaSamw 	if (flags & V_RDDIR_ENTFLAGS &&
3640da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS) == 0)
3641da6c28aaSamw 		return (EINVAL);
3642da6c28aaSamw 
3643f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3644f48205beScasper 
3645da6c28aaSamw 	err = (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp, ct, flags);
36462bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, readdir,
36475a59a8b3Srsb 	    readdir_bytes, (resid_start - uiop->uio_resid));
36485a59a8b3Srsb 	return (err);
36497c478bd9Sstevel@tonic-gate }
36507c478bd9Sstevel@tonic-gate 
36517c478bd9Sstevel@tonic-gate int
36527c478bd9Sstevel@tonic-gate fop_symlink(
36537c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
36547c478bd9Sstevel@tonic-gate 	char *linkname,
36557c478bd9Sstevel@tonic-gate 	vattr_t *vap,
36567c478bd9Sstevel@tonic-gate 	char *target,
3657da6c28aaSamw 	cred_t *cr,
3658da6c28aaSamw 	caller_context_t *ct,
3659da6c28aaSamw 	int flags)
36607c478bd9Sstevel@tonic-gate {
36615a59a8b3Srsb 	int	err;
36627a286c47SDai Ngo 	xvattr_t xvattr;
36635a59a8b3Srsb 
3664da6c28aaSamw 	/*
3665da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3666da6c28aaSamw 	 * and said access is requested, fail quickly.
3667da6c28aaSamw 	 */
3668da6c28aaSamw 	if (flags & FIGNORECASE &&
3669da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3670da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3671da6c28aaSamw 		return (EINVAL);
3672da6c28aaSamw 
3673f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3674f48205beScasper 
36757a286c47SDai Ngo 	/* check for reparse point */
36767a286c47SDai Ngo 	if ((vfs_has_feature(dvp->v_vfsp, VFSFT_REPARSE)) &&
36777a286c47SDai Ngo 	    (strncmp(target, FS_REPARSE_TAG_STR,
36787a286c47SDai Ngo 	    strlen(FS_REPARSE_TAG_STR)) == 0)) {
36797a286c47SDai Ngo 		if (!fs_reparse_mark(target, vap, &xvattr))
36807a286c47SDai Ngo 			vap = (vattr_t *)&xvattr;
36817a286c47SDai Ngo 	}
36827a286c47SDai Ngo 
3683da6c28aaSamw 	err = (*(dvp)->v_op->vop_symlink)
3684da6c28aaSamw 	    (dvp, linkname, vap, target, cr, ct, flags);
36852bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, symlink);
36865a59a8b3Srsb 	return (err);
36877c478bd9Sstevel@tonic-gate }
36887c478bd9Sstevel@tonic-gate 
36897c478bd9Sstevel@tonic-gate int
36907c478bd9Sstevel@tonic-gate fop_readlink(
36917c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36927c478bd9Sstevel@tonic-gate 	uio_t *uiop,
3693da6c28aaSamw 	cred_t *cr,
3694da6c28aaSamw 	caller_context_t *ct)
36957c478bd9Sstevel@tonic-gate {
36965a59a8b3Srsb 	int	err;
36975a59a8b3Srsb 
3698f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3699f48205beScasper 
3700da6c28aaSamw 	err = (*(vp)->v_op->vop_readlink)(vp, uiop, cr, ct);
37012bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, readlink);
37025a59a8b3Srsb 	return (err);
37037c478bd9Sstevel@tonic-gate }
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate int
37067c478bd9Sstevel@tonic-gate fop_fsync(
37077c478bd9Sstevel@tonic-gate 	vnode_t *vp,
37087c478bd9Sstevel@tonic-gate 	int syncflag,
3709da6c28aaSamw 	cred_t *cr,
3710da6c28aaSamw 	caller_context_t *ct)
37117c478bd9Sstevel@tonic-gate {
37125a59a8b3Srsb 	int	err;
37135a59a8b3Srsb 
3714f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3715f48205beScasper 
3716da6c28aaSamw 	err = (*(vp)->v_op->vop_fsync)(vp, syncflag, cr, ct);
37172bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, fsync);
37185a59a8b3Srsb 	return (err);
37197c478bd9Sstevel@tonic-gate }
37207c478bd9Sstevel@tonic-gate 
37217c478bd9Sstevel@tonic-gate void
37227c478bd9Sstevel@tonic-gate fop_inactive(
37237c478bd9Sstevel@tonic-gate 	vnode_t *vp,
3724da6c28aaSamw 	cred_t *cr,
3725da6c28aaSamw 	caller_context_t *ct)
37267c478bd9Sstevel@tonic-gate {
37275a59a8b3Srsb 	/* Need to update stats before vop call since we may lose the vnode */
37282bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, inactive);
3729f48205beScasper 
3730f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3731f48205beScasper 
3732da6c28aaSamw 	(*(vp)->v_op->vop_inactive)(vp, cr, ct);
37337c478bd9Sstevel@tonic-gate }
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate int
37367c478bd9Sstevel@tonic-gate fop_fid(
37377c478bd9Sstevel@tonic-gate 	vnode_t *vp,
3738da6c28aaSamw 	fid_t *fidp,
3739da6c28aaSamw 	caller_context_t *ct)
37407c478bd9Sstevel@tonic-gate {
37415a59a8b3Srsb 	int	err;
37425a59a8b3Srsb 
3743da6c28aaSamw 	err = (*(vp)->v_op->vop_fid)(vp, fidp, ct);
37442bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, fid);
37455a59a8b3Srsb 	return (err);
37467c478bd9Sstevel@tonic-gate }
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate int
37497c478bd9Sstevel@tonic-gate fop_rwlock(
37507c478bd9Sstevel@tonic-gate 	vnode_t *vp,
37517c478bd9Sstevel@tonic-gate 	int write_lock,
37527c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
37537c478bd9Sstevel@tonic-gate {
37545a59a8b3Srsb 	int	ret;
37555a59a8b3Srsb 
37565a59a8b3Srsb 	ret = ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct));
37572bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, rwlock);
37585a59a8b3Srsb 	return (ret);
37597c478bd9Sstevel@tonic-gate }
37607c478bd9Sstevel@tonic-gate 
37617c478bd9Sstevel@tonic-gate void
37627c478bd9Sstevel@tonic-gate fop_rwunlock(
37637c478bd9Sstevel@tonic-gate 	vnode_t *vp,
37647c478bd9Sstevel@tonic-gate 	int write_lock,
37657c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
37667c478bd9Sstevel@tonic-gate {
37677c478bd9Sstevel@tonic-gate 	(*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct);
37682bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, rwunlock);
37697c478bd9Sstevel@tonic-gate }
37707c478bd9Sstevel@tonic-gate 
37717c478bd9Sstevel@tonic-gate int
37727c478bd9Sstevel@tonic-gate fop_seek(
37737c478bd9Sstevel@tonic-gate 	vnode_t *vp,
37747c478bd9Sstevel@tonic-gate 	offset_t ooff,
3775da6c28aaSamw 	offset_t *noffp,
3776da6c28aaSamw 	caller_context_t *ct)
37777c478bd9Sstevel@tonic-gate {
37785a59a8b3Srsb 	int	err;
37795a59a8b3Srsb 
3780da6c28aaSamw 	err = (*(vp)->v_op->vop_seek)(vp, ooff, noffp, ct);
37812bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, seek);
37825a59a8b3Srsb 	return (err);
37837c478bd9Sstevel@tonic-gate }
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate int
37867c478bd9Sstevel@tonic-gate fop_cmp(
37877c478bd9Sstevel@tonic-gate 	vnode_t *vp1,
3788da6c28aaSamw 	vnode_t *vp2,
3789da6c28aaSamw 	caller_context_t *ct)
37907c478bd9Sstevel@tonic-gate {
37915a59a8b3Srsb 	int	err;
37925a59a8b3Srsb 
3793da6c28aaSamw 	err = (*(vp1)->v_op->vop_cmp)(vp1, vp2, ct);
37942bb1cb30Sbmc 	VOPSTATS_UPDATE(vp1, cmp);
37955a59a8b3Srsb 	return (err);
37967c478bd9Sstevel@tonic-gate }
37977c478bd9Sstevel@tonic-gate 
37987c478bd9Sstevel@tonic-gate int
37997c478bd9Sstevel@tonic-gate fop_frlock(
38007c478bd9Sstevel@tonic-gate 	vnode_t *vp,
38017c478bd9Sstevel@tonic-gate 	int cmd,
38027c478bd9Sstevel@tonic-gate 	flock64_t *bfp,
38037c478bd9Sstevel@tonic-gate 	int flag,
38047c478bd9Sstevel@tonic-gate 	offset_t offset,
38057c478bd9Sstevel@tonic-gate 	struct flk_callback *flk_cbp,
3806da6c28aaSamw 	cred_t *cr,
3807da6c28aaSamw 	caller_context_t *ct)
38087c478bd9Sstevel@tonic-gate {
38095a59a8b3Srsb 	int	err;
38105a59a8b3Srsb 
3811f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3812f48205beScasper 
38135a59a8b3Srsb 	err = (*(vp)->v_op->vop_frlock)
3814da6c28aaSamw 	    (vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
38152bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, frlock);
38165a59a8b3Srsb 	return (err);
38177c478bd9Sstevel@tonic-gate }
38187c478bd9Sstevel@tonic-gate 
38197c478bd9Sstevel@tonic-gate int
38207c478bd9Sstevel@tonic-gate fop_space(
38217c478bd9Sstevel@tonic-gate 	vnode_t *vp,
38227c478bd9Sstevel@tonic-gate 	int cmd,
38237c478bd9Sstevel@tonic-gate 	flock64_t *bfp,
38247c478bd9Sstevel@tonic-gate 	int flag,
38257c478bd9Sstevel@tonic-gate 	offset_t offset,
38267c478bd9Sstevel@tonic-gate 	cred_t *cr,
38277c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
38287c478bd9Sstevel@tonic-gate {
38295a59a8b3Srsb 	int	err;
38305a59a8b3Srsb 
3831f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3832f48205beScasper 
38335a59a8b3Srsb 	err = (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct);
38342bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, space);
38355a59a8b3Srsb 	return (err);
38367c478bd9Sstevel@tonic-gate }
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate int
38397c478bd9Sstevel@tonic-gate fop_realvp(
38407c478bd9Sstevel@tonic-gate 	vnode_t *vp,
3841da6c28aaSamw 	vnode_t **vpp,
3842da6c28aaSamw 	caller_context_t *ct)
38437c478bd9Sstevel@tonic-gate {
38445a59a8b3Srsb 	int	err;
38455a59a8b3Srsb 
3846da6c28aaSamw 	err = (*(vp)->v_op->vop_realvp)(vp, vpp, ct);
38472bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, realvp);
38485a59a8b3Srsb 	return (err);
38497c478bd9Sstevel@tonic-gate }
38507c478bd9Sstevel@tonic-gate 
38517c478bd9Sstevel@tonic-gate int
38527c478bd9Sstevel@tonic-gate fop_getpage(
38537c478bd9Sstevel@tonic-gate 	vnode_t *vp,
38547c478bd9Sstevel@tonic-gate 	offset_t off,
38557c478bd9Sstevel@tonic-gate 	size_t len,
38567c478bd9Sstevel@tonic-gate 	uint_t *protp,
38577c478bd9Sstevel@tonic-gate 	page_t **plarr,
38587c478bd9Sstevel@tonic-gate 	size_t plsz,
38597c478bd9Sstevel@tonic-gate 	struct seg *seg,
38607c478bd9Sstevel@tonic-gate 	caddr_t addr,
38617c478bd9Sstevel@tonic-gate 	enum seg_rw rw,
3862da6c28aaSamw 	cred_t *cr,
3863da6c28aaSamw 	caller_context_t *ct)
38647c478bd9Sstevel@tonic-gate {
38655a59a8b3Srsb 	int	err;
38665a59a8b3Srsb 
3867f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3868f48205beScasper 
38695a59a8b3Srsb 	err = (*(vp)->v_op->vop_getpage)
3870da6c28aaSamw 	    (vp, off, len, protp, plarr, plsz, seg, addr, rw, cr, ct);
38712bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getpage);
38725a59a8b3Srsb 	return (err);
38737c478bd9Sstevel@tonic-gate }
38747c478bd9Sstevel@tonic-gate 
38757c478bd9Sstevel@tonic-gate int
38767c478bd9Sstevel@tonic-gate fop_putpage(
38777c478bd9Sstevel@tonic-gate 	vnode_t *vp,
38787c478bd9Sstevel@tonic-gate 	offset_t off,
38797c478bd9Sstevel@tonic-gate 	size_t len,
38807c478bd9Sstevel@tonic-gate 	int flags,
3881da6c28aaSamw 	cred_t *cr,
3882da6c28aaSamw 	caller_context_t *ct)
38837c478bd9Sstevel@tonic-gate {
38845a59a8b3Srsb 	int	err;
38855a59a8b3Srsb 
3886f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3887f48205beScasper 
3888da6c28aaSamw 	err = (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr, ct);
38892bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, putpage);
38905a59a8b3Srsb 	return (err);
38917c478bd9Sstevel@tonic-gate }
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate int
38947c478bd9Sstevel@tonic-gate fop_map(
38957c478bd9Sstevel@tonic-gate 	vnode_t *vp,
38967c478bd9Sstevel@tonic-gate 	offset_t off,
38977c478bd9Sstevel@tonic-gate 	struct as *as,
38987c478bd9Sstevel@tonic-gate 	caddr_t *addrp,
38997c478bd9Sstevel@tonic-gate 	size_t len,
39007c478bd9Sstevel@tonic-gate 	uchar_t prot,
39017c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
39027c478bd9Sstevel@tonic-gate 	uint_t flags,
3903da6c28aaSamw 	cred_t *cr,
3904da6c28aaSamw 	caller_context_t *ct)
39057c478bd9Sstevel@tonic-gate {
39065a59a8b3Srsb 	int	err;
39075a59a8b3Srsb 
3908f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3909f48205beScasper 
39105a59a8b3Srsb 	err = (*(vp)->v_op->vop_map)
3911da6c28aaSamw 	    (vp, off, as, addrp, len, prot, maxprot, flags, cr, ct);
39122bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, map);
39135a59a8b3Srsb 	return (err);
39147c478bd9Sstevel@tonic-gate }
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate int
39177c478bd9Sstevel@tonic-gate fop_addmap(
39187c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39197c478bd9Sstevel@tonic-gate 	offset_t off,
39207c478bd9Sstevel@tonic-gate 	struct as *as,
39217c478bd9Sstevel@tonic-gate 	caddr_t addr,
39227c478bd9Sstevel@tonic-gate 	size_t len,
39237c478bd9Sstevel@tonic-gate 	uchar_t prot,
39247c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
39257c478bd9Sstevel@tonic-gate 	uint_t flags,
3926da6c28aaSamw 	cred_t *cr,
3927da6c28aaSamw 	caller_context_t *ct)
39287c478bd9Sstevel@tonic-gate {
39297c478bd9Sstevel@tonic-gate 	int error;
39307c478bd9Sstevel@tonic-gate 	u_longlong_t delta;
39317c478bd9Sstevel@tonic-gate 
3932f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3933f48205beScasper 
39347c478bd9Sstevel@tonic-gate 	error = (*(vp)->v_op->vop_addmap)
3935da6c28aaSamw 	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
39367c478bd9Sstevel@tonic-gate 
39377c478bd9Sstevel@tonic-gate 	if ((!error) && (vp->v_type == VREG)) {
39387c478bd9Sstevel@tonic-gate 		delta = (u_longlong_t)btopr(len);
39397c478bd9Sstevel@tonic-gate 		/*
39407c478bd9Sstevel@tonic-gate 		 * If file is declared MAP_PRIVATE, it can't be written back
39417c478bd9Sstevel@tonic-gate 		 * even if open for write. Handle as read.
39427c478bd9Sstevel@tonic-gate 		 */
39437c478bd9Sstevel@tonic-gate 		if (flags & MAP_PRIVATE) {
39447c478bd9Sstevel@tonic-gate 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
39457c478bd9Sstevel@tonic-gate 			    (int64_t)delta);
39467c478bd9Sstevel@tonic-gate 		} else {
39477c478bd9Sstevel@tonic-gate 			/*
39487c478bd9Sstevel@tonic-gate 			 * atomic_add_64 forces the fetch of a 64 bit value to
39497c478bd9Sstevel@tonic-gate 			 * be atomic on 32 bit machines
39507c478bd9Sstevel@tonic-gate 			 */
39517c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_WRITE)
39527c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
39537c478bd9Sstevel@tonic-gate 				    (int64_t)delta);
39547c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_READ)
39557c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
39567c478bd9Sstevel@tonic-gate 				    (int64_t)delta);
39577c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_EXEC)
39587c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
39597c478bd9Sstevel@tonic-gate 				    (int64_t)delta);
39607c478bd9Sstevel@tonic-gate 		}
39617c478bd9Sstevel@tonic-gate 	}
39622bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, addmap);
39637c478bd9Sstevel@tonic-gate 	return (error);
39647c478bd9Sstevel@tonic-gate }
39657c478bd9Sstevel@tonic-gate 
39667c478bd9Sstevel@tonic-gate int
39677c478bd9Sstevel@tonic-gate fop_delmap(
39687c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39697c478bd9Sstevel@tonic-gate 	offset_t off,
39707c478bd9Sstevel@tonic-gate 	struct as *as,
39717c478bd9Sstevel@tonic-gate 	caddr_t addr,
39727c478bd9Sstevel@tonic-gate 	size_t len,
39737c478bd9Sstevel@tonic-gate 	uint_t prot,
39747c478bd9Sstevel@tonic-gate 	uint_t maxprot,
39757c478bd9Sstevel@tonic-gate 	uint_t flags,
3976da6c28aaSamw 	cred_t *cr,
3977da6c28aaSamw 	caller_context_t *ct)
39787c478bd9Sstevel@tonic-gate {
39797c478bd9Sstevel@tonic-gate 	int error;
39807c478bd9Sstevel@tonic-gate 	u_longlong_t delta;
3981f48205beScasper 
3982f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3983f48205beScasper 
39847c478bd9Sstevel@tonic-gate 	error = (*(vp)->v_op->vop_delmap)
3985da6c28aaSamw 	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
39867c478bd9Sstevel@tonic-gate 
39877c478bd9Sstevel@tonic-gate 	/*
39887c478bd9Sstevel@tonic-gate 	 * NFS calls into delmap twice, the first time
39897c478bd9Sstevel@tonic-gate 	 * it simply establishes a callback mechanism and returns EAGAIN
39907c478bd9Sstevel@tonic-gate 	 * while the real work is being done upon the second invocation.
39917c478bd9Sstevel@tonic-gate 	 * We have to detect this here and only decrement the counts upon
39927c478bd9Sstevel@tonic-gate 	 * the second delmap request.
39937c478bd9Sstevel@tonic-gate 	 */
39947c478bd9Sstevel@tonic-gate 	if ((error != EAGAIN) && (vp->v_type == VREG)) {
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate 		delta = (u_longlong_t)btopr(len);
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate 		if (flags & MAP_PRIVATE) {
39997c478bd9Sstevel@tonic-gate 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
40007c478bd9Sstevel@tonic-gate 			    (int64_t)(-delta));
40017c478bd9Sstevel@tonic-gate 		} else {
40027c478bd9Sstevel@tonic-gate 			/*
40037c478bd9Sstevel@tonic-gate 			 * atomic_add_64 forces the fetch of a 64 bit value
40047c478bd9Sstevel@tonic-gate 			 * to be atomic on 32 bit machines
40057c478bd9Sstevel@tonic-gate 			 */
40067c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_WRITE)
40077c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
40087c478bd9Sstevel@tonic-gate 				    (int64_t)(-delta));
40097c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_READ)
40107c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
40117c478bd9Sstevel@tonic-gate 				    (int64_t)(-delta));
40127c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_EXEC)
40137c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
40147c478bd9Sstevel@tonic-gate 				    (int64_t)(-delta));
40157c478bd9Sstevel@tonic-gate 		}
40167c478bd9Sstevel@tonic-gate 	}
40172bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, delmap);
40187c478bd9Sstevel@tonic-gate 	return (error);
40197c478bd9Sstevel@tonic-gate }
40207c478bd9Sstevel@tonic-gate 
40217c478bd9Sstevel@tonic-gate 
40227c478bd9Sstevel@tonic-gate int
40237c478bd9Sstevel@tonic-gate fop_poll(
40247c478bd9Sstevel@tonic-gate 	vnode_t *vp,
40257c478bd9Sstevel@tonic-gate 	short events,
40267c478bd9Sstevel@tonic-gate 	int anyyet,
40277c478bd9Sstevel@tonic-gate 	short *reventsp,
4028da6c28aaSamw 	struct pollhead **phpp,
4029da6c28aaSamw 	caller_context_t *ct)
40307c478bd9Sstevel@tonic-gate {
40315a59a8b3Srsb 	int	err;
40325a59a8b3Srsb 
4033da6c28aaSamw 	err = (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp, ct);
40342bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, poll);
40355a59a8b3Srsb 	return (err);
40367c478bd9Sstevel@tonic-gate }
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate int
40397c478bd9Sstevel@tonic-gate fop_dump(
40407c478bd9Sstevel@tonic-gate 	vnode_t *vp,
40417c478bd9Sstevel@tonic-gate 	caddr_t addr,
4042d7334e51Srm15945 	offset_t lbdn,
4043d7334e51Srm15945 	offset_t dblks,
4044da6c28aaSamw 	caller_context_t *ct)
40457c478bd9Sstevel@tonic-gate {
40465a59a8b3Srsb 	int	err;
40475a59a8b3Srsb 
4048d7334e51Srm15945 	/* ensure lbdn and dblks can be passed safely to bdev_dump */
4049d7334e51Srm15945 	if ((lbdn != (daddr_t)lbdn) || (dblks != (int)dblks))
4050d7334e51Srm15945 		return (EIO);
4051d7334e51Srm15945 
4052da6c28aaSamw 	err = (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks, ct);
40532bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dump);
40545a59a8b3Srsb 	return (err);
40557c478bd9Sstevel@tonic-gate }
40567c478bd9Sstevel@tonic-gate 
40577c478bd9Sstevel@tonic-gate int
40587c478bd9Sstevel@tonic-gate fop_pathconf(
40597c478bd9Sstevel@tonic-gate 	vnode_t *vp,
40607c478bd9Sstevel@tonic-gate 	int cmd,
40617c478bd9Sstevel@tonic-gate 	ulong_t *valp,
4062da6c28aaSamw 	cred_t *cr,
4063da6c28aaSamw 	caller_context_t *ct)
40647c478bd9Sstevel@tonic-gate {
40655a59a8b3Srsb 	int	err;
40665a59a8b3Srsb 
4067f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4068f48205beScasper 
4069da6c28aaSamw 	err = (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr, ct);
40702bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, pathconf);
40715a59a8b3Srsb 	return (err);
40727c478bd9Sstevel@tonic-gate }
40737c478bd9Sstevel@tonic-gate 
40747c478bd9Sstevel@tonic-gate int
40757c478bd9Sstevel@tonic-gate fop_pageio(
40767c478bd9Sstevel@tonic-gate 	vnode_t *vp,
40777c478bd9Sstevel@tonic-gate 	struct page *pp,
40787c478bd9Sstevel@tonic-gate 	u_offset_t io_off,
40797c478bd9Sstevel@tonic-gate 	size_t io_len,
40807c478bd9Sstevel@tonic-gate 	int flags,
4081da6c28aaSamw 	cred_t *cr,
4082da6c28aaSamw 	caller_context_t *ct)
40837c478bd9Sstevel@tonic-gate {
40845a59a8b3Srsb 	int	err;
40855a59a8b3Srsb 
4086f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4087f48205beScasper 
4088da6c28aaSamw 	err = (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr, ct);
40892bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, pageio);
40905a59a8b3Srsb 	return (err);
40917c478bd9Sstevel@tonic-gate }
40927c478bd9Sstevel@tonic-gate 
40937c478bd9Sstevel@tonic-gate int
40947c478bd9Sstevel@tonic-gate fop_dumpctl(
40957c478bd9Sstevel@tonic-gate 	vnode_t *vp,
40967c478bd9Sstevel@tonic-gate 	int action,
4097d7334e51Srm15945 	offset_t *blkp,
4098da6c28aaSamw 	caller_context_t *ct)
40997c478bd9Sstevel@tonic-gate {
41005a59a8b3Srsb 	int	err;
4101da6c28aaSamw 	err = (*(vp)->v_op->vop_dumpctl)(vp, action, blkp, ct);
41022bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dumpctl);
41035a59a8b3Srsb 	return (err);
41047c478bd9Sstevel@tonic-gate }
41057c478bd9Sstevel@tonic-gate 
41067c478bd9Sstevel@tonic-gate void
41077c478bd9Sstevel@tonic-gate fop_dispose(
41087c478bd9Sstevel@tonic-gate 	vnode_t *vp,
41097c478bd9Sstevel@tonic-gate 	page_t *pp,
41107c478bd9Sstevel@tonic-gate 	int flag,
41117c478bd9Sstevel@tonic-gate 	int dn,
4112da6c28aaSamw 	cred_t *cr,
4113da6c28aaSamw 	caller_context_t *ct)
41147c478bd9Sstevel@tonic-gate {
41155a59a8b3Srsb 	/* Must do stats first since it's possible to lose the vnode */
41162bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dispose);
4117f48205beScasper 
4118f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4119f48205beScasper 
4120da6c28aaSamw 	(*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr, ct);
41217c478bd9Sstevel@tonic-gate }
41227c478bd9Sstevel@tonic-gate 
41237c478bd9Sstevel@tonic-gate int
41247c478bd9Sstevel@tonic-gate fop_setsecattr(
41257c478bd9Sstevel@tonic-gate 	vnode_t *vp,
41267c478bd9Sstevel@tonic-gate 	vsecattr_t *vsap,
41277c478bd9Sstevel@tonic-gate 	int flag,
4128da6c28aaSamw 	cred_t *cr,
4129da6c28aaSamw 	caller_context_t *ct)
41307c478bd9Sstevel@tonic-gate {
41315a59a8b3Srsb 	int	err;
41325a59a8b3Srsb 
4133f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4134f48205beScasper 
4135da6c28aaSamw 	/*
4136da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
4137da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
4138da6c28aaSamw 	 */
4139da6c28aaSamw 	if ((flag & ATTR_NOACLCHECK) &&
4140da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4141da6c28aaSamw 		return (EINVAL);
4142da6c28aaSamw 	}
4143da6c28aaSamw 	err = (*(vp)->v_op->vop_setsecattr) (vp, vsap, flag, cr, ct);
41442bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setsecattr);
41455a59a8b3Srsb 	return (err);
41467c478bd9Sstevel@tonic-gate }
41477c478bd9Sstevel@tonic-gate 
41487c478bd9Sstevel@tonic-gate int
41497c478bd9Sstevel@tonic-gate fop_getsecattr(
41507c478bd9Sstevel@tonic-gate 	vnode_t *vp,
41517c478bd9Sstevel@tonic-gate 	vsecattr_t *vsap,
41527c478bd9Sstevel@tonic-gate 	int flag,
4153da6c28aaSamw 	cred_t *cr,
4154da6c28aaSamw 	caller_context_t *ct)
41557c478bd9Sstevel@tonic-gate {
41565a59a8b3Srsb 	int	err;
41575a59a8b3Srsb 
4158da6c28aaSamw 	/*
4159da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
4160da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
4161da6c28aaSamw 	 */
4162da6c28aaSamw 	if ((flag & ATTR_NOACLCHECK) &&
4163da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4164da6c28aaSamw 		return (EINVAL);
4165da6c28aaSamw 	}
4166da6c28aaSamw 
4167f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4168f48205beScasper 
4169da6c28aaSamw 	err = (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr, ct);
41702bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getsecattr);
41715a59a8b3Srsb 	return (err);
41727c478bd9Sstevel@tonic-gate }
41737c478bd9Sstevel@tonic-gate 
41747c478bd9Sstevel@tonic-gate int
41757c478bd9Sstevel@tonic-gate fop_shrlock(
41767c478bd9Sstevel@tonic-gate 	vnode_t *vp,
41777c478bd9Sstevel@tonic-gate 	int cmd,
41787c478bd9Sstevel@tonic-gate 	struct shrlock *shr,
41797c478bd9Sstevel@tonic-gate 	int flag,
4180da6c28aaSamw 	cred_t *cr,
4181da6c28aaSamw 	caller_context_t *ct)
41827c478bd9Sstevel@tonic-gate {
41835a59a8b3Srsb 	int	err;
41845a59a8b3Srsb 
4185f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4186f48205beScasper 
4187da6c28aaSamw 	err = (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr, ct);
41882bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, shrlock);
41895a59a8b3Srsb 	return (err);
41907c478bd9Sstevel@tonic-gate }
41917c478bd9Sstevel@tonic-gate 
41927c478bd9Sstevel@tonic-gate int
4193da6c28aaSamw fop_vnevent(vnode_t *vp, vnevent_t vnevent, vnode_t *dvp, char *fnm,
4194da6c28aaSamw     caller_context_t *ct)
41957c478bd9Sstevel@tonic-gate {
41965a59a8b3Srsb 	int	err;
41975a59a8b3Srsb 
4198da6c28aaSamw 	err = (*(vp)->v_op->vop_vnevent)(vp, vnevent, dvp, fnm, ct);
41992bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, vnevent);
42005a59a8b3Srsb 	return (err);
42017c478bd9Sstevel@tonic-gate }
42021b300de9Sjwahlig 
4203c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
4204c242f9a0Schunli zhang - Sun Microsystems - Irvine United States fop_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *uiop, cred_t *cr,
4205c242f9a0Schunli zhang - Sun Microsystems - Irvine United States     caller_context_t *ct)
4206c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
4207c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int err;
4208c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
4209c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (vfs_has_feature(vp->v_vfsp, VFSFT_ZEROCOPY_SUPPORTED) == 0)
4210c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		return (ENOTSUP);
4211c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	err = (*(vp)->v_op->vop_reqzcbuf)(vp, ioflag, uiop, cr, ct);
4212c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	VOPSTATS_UPDATE(vp, reqzcbuf);
4213c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (err);
4214c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
4215c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
4216c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
4217c242f9a0Schunli zhang - Sun Microsystems - Irvine United States fop_retzcbuf(vnode_t *vp, xuio_t *uiop, cred_t *cr, caller_context_t *ct)
4218c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
4219c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int err;
4220c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
4221c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (vfs_has_feature(vp->v_vfsp, VFSFT_ZEROCOPY_SUPPORTED) == 0)
4222c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		return (ENOTSUP);
4223c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	err = (*(vp)->v_op->vop_retzcbuf)(vp, uiop, cr, ct);
4224c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	VOPSTATS_UPDATE(vp, retzcbuf);
4225c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (err);
4226c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
4227c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
42281b300de9Sjwahlig /*
42291b300de9Sjwahlig  * Default destructor
42301b300de9Sjwahlig  *	Needed because NULL destructor means that the key is unused
42311b300de9Sjwahlig  */
42321b300de9Sjwahlig /* ARGSUSED */
42331b300de9Sjwahlig void
42341b300de9Sjwahlig vsd_defaultdestructor(void *value)
42351b300de9Sjwahlig {}
42361b300de9Sjwahlig 
42371b300de9Sjwahlig /*
42381b300de9Sjwahlig  * Create a key (index into per vnode array)
42391b300de9Sjwahlig  *	Locks out vsd_create, vsd_destroy, and vsd_free
42401b300de9Sjwahlig  *	May allocate memory with lock held
42411b300de9Sjwahlig  */
42421b300de9Sjwahlig void
42431b300de9Sjwahlig vsd_create(uint_t *keyp, void (*destructor)(void *))
42441b300de9Sjwahlig {
42451b300de9Sjwahlig 	int	i;
42461b300de9Sjwahlig 	uint_t	nkeys;
42471b300de9Sjwahlig 
42481b300de9Sjwahlig 	/*
42491b300de9Sjwahlig 	 * if key is allocated, do nothing
42501b300de9Sjwahlig 	 */
42511b300de9Sjwahlig 	mutex_enter(&vsd_lock);
42521b300de9Sjwahlig 	if (*keyp) {
42531b300de9Sjwahlig 		mutex_exit(&vsd_lock);
42541b300de9Sjwahlig 		return;
42551b300de9Sjwahlig 	}
42561b300de9Sjwahlig 	/*
42571b300de9Sjwahlig 	 * find an unused key
42581b300de9Sjwahlig 	 */
42591b300de9Sjwahlig 	if (destructor == NULL)
42601b300de9Sjwahlig 		destructor = vsd_defaultdestructor;
42611b300de9Sjwahlig 
42621b300de9Sjwahlig 	for (i = 0; i < vsd_nkeys; ++i)
42631b300de9Sjwahlig 		if (vsd_destructor[i] == NULL)
42641b300de9Sjwahlig 			break;
42651b300de9Sjwahlig 
42661b300de9Sjwahlig 	/*
42671b300de9Sjwahlig 	 * if no unused keys, increase the size of the destructor array
42681b300de9Sjwahlig 	 */
42691b300de9Sjwahlig 	if (i == vsd_nkeys) {
42701b300de9Sjwahlig 		if ((nkeys = (vsd_nkeys << 1)) == 0)
42711b300de9Sjwahlig 			nkeys = 1;
42721b300de9Sjwahlig 		vsd_destructor =
42731b300de9Sjwahlig 		    (void (**)(void *))vsd_realloc((void *)vsd_destructor,
42741b300de9Sjwahlig 		    (size_t)(vsd_nkeys * sizeof (void (*)(void *))),
42751b300de9Sjwahlig 		    (size_t)(nkeys * sizeof (void (*)(void *))));
42761b300de9Sjwahlig 		vsd_nkeys = nkeys;
42771b300de9Sjwahlig 	}
42781b300de9Sjwahlig 
42791b300de9Sjwahlig 	/*
42801b300de9Sjwahlig 	 * allocate the next available unused key
42811b300de9Sjwahlig 	 */
42821b300de9Sjwahlig 	vsd_destructor[i] = destructor;
42831b300de9Sjwahlig 	*keyp = i + 1;
42841b300de9Sjwahlig 
42851b300de9Sjwahlig 	/* create vsd_list, if it doesn't exist */
42861b300de9Sjwahlig 	if (vsd_list == NULL) {
42871b300de9Sjwahlig 		vsd_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
42881b300de9Sjwahlig 		list_create(vsd_list, sizeof (struct vsd_node),
42891b300de9Sjwahlig 		    offsetof(struct vsd_node, vs_nodes));
42901b300de9Sjwahlig 	}
42911b300de9Sjwahlig 
42921b300de9Sjwahlig 	mutex_exit(&vsd_lock);
42931b300de9Sjwahlig }
42941b300de9Sjwahlig 
42951b300de9Sjwahlig /*
42961b300de9Sjwahlig  * Destroy a key
42971b300de9Sjwahlig  *
42981b300de9Sjwahlig  * Assumes that the caller is preventing vsd_set and vsd_get
42991b300de9Sjwahlig  * Locks out vsd_create, vsd_destroy, and vsd_free
43001b300de9Sjwahlig  * May free memory with lock held
43011b300de9Sjwahlig  */
43021b300de9Sjwahlig void
43031b300de9Sjwahlig vsd_destroy(uint_t *keyp)
43041b300de9Sjwahlig {
43051b300de9Sjwahlig 	uint_t key;
43061b300de9Sjwahlig 	struct vsd_node *vsd;
43071b300de9Sjwahlig 
43081b300de9Sjwahlig 	/*
43091b300de9Sjwahlig 	 * protect the key namespace and our destructor lists
43101b300de9Sjwahlig 	 */
43111b300de9Sjwahlig 	mutex_enter(&vsd_lock);
43121b300de9Sjwahlig 	key = *keyp;
43131b300de9Sjwahlig 	*keyp = 0;
43141b300de9Sjwahlig 
43151b300de9Sjwahlig 	ASSERT(key <= vsd_nkeys);
43161b300de9Sjwahlig 
43171b300de9Sjwahlig 	/*
43181b300de9Sjwahlig 	 * if the key is valid
43191b300de9Sjwahlig 	 */
43201b300de9Sjwahlig 	if (key != 0) {
43211b300de9Sjwahlig 		uint_t k = key - 1;
43221b300de9Sjwahlig 		/*
43231b300de9Sjwahlig 		 * for every vnode with VSD, call key's destructor
43241b300de9Sjwahlig 		 */
43251b300de9Sjwahlig 		for (vsd = list_head(vsd_list); vsd != NULL;
43261b300de9Sjwahlig 		    vsd = list_next(vsd_list, vsd)) {
43271b300de9Sjwahlig 			/*
43281b300de9Sjwahlig 			 * no VSD for key in this vnode
43291b300de9Sjwahlig 			 */
43301b300de9Sjwahlig 			if (key > vsd->vs_nkeys)
43311b300de9Sjwahlig 				continue;
43321b300de9Sjwahlig 			/*
43331b300de9Sjwahlig 			 * call destructor for key
43341b300de9Sjwahlig 			 */
43351b300de9Sjwahlig 			if (vsd->vs_value[k] && vsd_destructor[k])
43361b300de9Sjwahlig 				(*vsd_destructor[k])(vsd->vs_value[k]);
43371b300de9Sjwahlig 			/*
43381b300de9Sjwahlig 			 * reset value for key
43391b300de9Sjwahlig 			 */
43401b300de9Sjwahlig 			vsd->vs_value[k] = NULL;
43411b300de9Sjwahlig 		}
43421b300de9Sjwahlig 		/*
43431b300de9Sjwahlig 		 * actually free the key (NULL destructor == unused)
43441b300de9Sjwahlig 		 */
43451b300de9Sjwahlig 		vsd_destructor[k] = NULL;
43461b300de9Sjwahlig 	}
43471b300de9Sjwahlig 
43481b300de9Sjwahlig 	mutex_exit(&vsd_lock);
43491b300de9Sjwahlig }
43501b300de9Sjwahlig 
43511b300de9Sjwahlig /*
43521b300de9Sjwahlig  * Quickly return the per vnode value that was stored with the specified key
43531b300de9Sjwahlig  * Assumes the caller is protecting key from vsd_create and vsd_destroy
4354d216dff5SRobert Mastors  * Assumes the caller is holding v_vsd_lock to protect the vsd.
43551b300de9Sjwahlig  */
43561b300de9Sjwahlig void *
43571b300de9Sjwahlig vsd_get(vnode_t *vp, uint_t key)
43581b300de9Sjwahlig {
43591b300de9Sjwahlig 	struct vsd_node *vsd;
43601b300de9Sjwahlig 
43611b300de9Sjwahlig 	ASSERT(vp != NULL);
4362d216dff5SRobert Mastors 	ASSERT(mutex_owned(&vp->v_vsd_lock));
43631b300de9Sjwahlig 
43641b300de9Sjwahlig 	vsd = vp->v_vsd;
43651b300de9Sjwahlig 
43661b300de9Sjwahlig 	if (key && vsd != NULL && key <= vsd->vs_nkeys)
43671b300de9Sjwahlig 		return (vsd->vs_value[key - 1]);
43681b300de9Sjwahlig 	return (NULL);
43691b300de9Sjwahlig }
43701b300de9Sjwahlig 
43711b300de9Sjwahlig /*
43721b300de9Sjwahlig  * Set a per vnode value indexed with the specified key
4373d216dff5SRobert Mastors  * Assumes the caller is holding v_vsd_lock to protect the vsd.
43741b300de9Sjwahlig  */
43751b300de9Sjwahlig int
43761b300de9Sjwahlig vsd_set(vnode_t *vp, uint_t key, void *value)
43771b300de9Sjwahlig {
4378d216dff5SRobert Mastors 	struct vsd_node *vsd;
4379d216dff5SRobert Mastors 
4380d216dff5SRobert Mastors 	ASSERT(vp != NULL);
4381d216dff5SRobert Mastors 	ASSERT(mutex_owned(&vp->v_vsd_lock));
43821b300de9Sjwahlig 
43831b300de9Sjwahlig 	if (key == 0)
43841b300de9Sjwahlig 		return (EINVAL);
4385d216dff5SRobert Mastors 
4386d216dff5SRobert Mastors 	vsd = vp->v_vsd;
43871b300de9Sjwahlig 	if (vsd == NULL)
43881b300de9Sjwahlig 		vsd = vp->v_vsd = kmem_zalloc(sizeof (*vsd), KM_SLEEP);
43891b300de9Sjwahlig 
43901b300de9Sjwahlig 	/*
43911b300de9Sjwahlig 	 * If the vsd was just allocated, vs_nkeys will be 0, so the following
43921b300de9Sjwahlig 	 * code won't happen and we will continue down and allocate space for
43931b300de9Sjwahlig 	 * the vs_value array.
43941b300de9Sjwahlig 	 * If the caller is replacing one value with another, then it is up
43951b300de9Sjwahlig 	 * to the caller to free/rele/destroy the previous value (if needed).
43961b300de9Sjwahlig 	 */
43971b300de9Sjwahlig 	if (key <= vsd->vs_nkeys) {
43981b300de9Sjwahlig 		vsd->vs_value[key - 1] = value;
43991b300de9Sjwahlig 		return (0);
44001b300de9Sjwahlig 	}
44011b300de9Sjwahlig 
44021b300de9Sjwahlig 	ASSERT(key <= vsd_nkeys);
44031b300de9Sjwahlig 
44041b300de9Sjwahlig 	if (vsd->vs_nkeys == 0) {
44051b300de9Sjwahlig 		mutex_enter(&vsd_lock);	/* lock out vsd_destroy() */
44061b300de9Sjwahlig 		/*
44071b300de9Sjwahlig 		 * Link onto list of all VSD nodes.
44081b300de9Sjwahlig 		 */
44091b300de9Sjwahlig 		list_insert_head(vsd_list, vsd);
44101b300de9Sjwahlig 		mutex_exit(&vsd_lock);
44111b300de9Sjwahlig 	}
44121b300de9Sjwahlig 
44131b300de9Sjwahlig 	/*
44141b300de9Sjwahlig 	 * Allocate vnode local storage and set the value for key
44151b300de9Sjwahlig 	 */
44161b300de9Sjwahlig 	vsd->vs_value = vsd_realloc(vsd->vs_value,
44171b300de9Sjwahlig 	    vsd->vs_nkeys * sizeof (void *),
44181b300de9Sjwahlig 	    key * sizeof (void *));
44191b300de9Sjwahlig 	vsd->vs_nkeys = key;
44201b300de9Sjwahlig 	vsd->vs_value[key - 1] = value;
44211b300de9Sjwahlig 
44221b300de9Sjwahlig 	return (0);
44231b300de9Sjwahlig }
44241b300de9Sjwahlig 
44251b300de9Sjwahlig /*
44261b300de9Sjwahlig  * Called from vn_free() to run the destructor function for each vsd
44271b300de9Sjwahlig  *	Locks out vsd_create and vsd_destroy
44281b300de9Sjwahlig  *	Assumes that the destructor *DOES NOT* use vsd
44291b300de9Sjwahlig  */
44301b300de9Sjwahlig void
44311b300de9Sjwahlig vsd_free(vnode_t *vp)
44321b300de9Sjwahlig {
44331b300de9Sjwahlig 	int i;
44341b300de9Sjwahlig 	struct vsd_node *vsd = vp->v_vsd;
44351b300de9Sjwahlig 
44361b300de9Sjwahlig 	if (vsd == NULL)
44371b300de9Sjwahlig 		return;
44381b300de9Sjwahlig 
44391b300de9Sjwahlig 	if (vsd->vs_nkeys == 0) {
44401b300de9Sjwahlig 		kmem_free(vsd, sizeof (*vsd));
44411b300de9Sjwahlig 		vp->v_vsd = NULL;
44421b300de9Sjwahlig 		return;
44431b300de9Sjwahlig 	}
44441b300de9Sjwahlig 
44451b300de9Sjwahlig 	/*
44461b300de9Sjwahlig 	 * lock out vsd_create and vsd_destroy, call
44471b300de9Sjwahlig 	 * the destructor, and mark the value as destroyed.
44481b300de9Sjwahlig 	 */
44491b300de9Sjwahlig 	mutex_enter(&vsd_lock);
44501b300de9Sjwahlig 
44511b300de9Sjwahlig 	for (i = 0; i < vsd->vs_nkeys; i++) {
44521b300de9Sjwahlig 		if (vsd->vs_value[i] && vsd_destructor[i])
44531b300de9Sjwahlig 			(*vsd_destructor[i])(vsd->vs_value[i]);
44541b300de9Sjwahlig 		vsd->vs_value[i] = NULL;
44551b300de9Sjwahlig 	}
44561b300de9Sjwahlig 
44571b300de9Sjwahlig 	/*
44581b300de9Sjwahlig 	 * remove from linked list of VSD nodes
44591b300de9Sjwahlig 	 */
44601b300de9Sjwahlig 	list_remove(vsd_list, vsd);
44611b300de9Sjwahlig 
44621b300de9Sjwahlig 	mutex_exit(&vsd_lock);
44631b300de9Sjwahlig 
44641b300de9Sjwahlig 	/*
44651b300de9Sjwahlig 	 * free up the VSD
44661b300de9Sjwahlig 	 */
44671b300de9Sjwahlig 	kmem_free(vsd->vs_value, vsd->vs_nkeys * sizeof (void *));
44681b300de9Sjwahlig 	kmem_free(vsd, sizeof (struct vsd_node));
44691b300de9Sjwahlig 	vp->v_vsd = NULL;
44701b300de9Sjwahlig }
44711b300de9Sjwahlig 
44721b300de9Sjwahlig /*
44731b300de9Sjwahlig  * realloc
44741b300de9Sjwahlig  */
44751b300de9Sjwahlig static void *
44761b300de9Sjwahlig vsd_realloc(void *old, size_t osize, size_t nsize)
44771b300de9Sjwahlig {
44781b300de9Sjwahlig 	void *new;
44791b300de9Sjwahlig 
44801b300de9Sjwahlig 	new = kmem_zalloc(nsize, KM_SLEEP);
44811b300de9Sjwahlig 	if (old) {
44821b300de9Sjwahlig 		bcopy(old, new, osize);
44831b300de9Sjwahlig 		kmem_free(old, osize);
44841b300de9Sjwahlig 	}
44851b300de9Sjwahlig 	return (new);
44861b300de9Sjwahlig }
44877a286c47SDai Ngo 
44887a286c47SDai Ngo /*
44897a286c47SDai Ngo  * Setup the extensible system attribute for creating a reparse point.
44907a286c47SDai Ngo  * The symlink data 'target' is validated for proper format of a reparse
44917a286c47SDai Ngo  * string and a check also made to make sure the symlink data does not
44927a286c47SDai Ngo  * point to an existing file.
44937a286c47SDai Ngo  *
44947a286c47SDai Ngo  * return 0 if ok else -1.
44957a286c47SDai Ngo  */
44967a286c47SDai Ngo static int
44977a286c47SDai Ngo fs_reparse_mark(char *target, vattr_t *vap, xvattr_t *xvattr)
44987a286c47SDai Ngo {
44997a286c47SDai Ngo 	xoptattr_t *xoap;
45007a286c47SDai Ngo 
45017a286c47SDai Ngo 	if ((!target) || (!vap) || (!xvattr))
45027a286c47SDai Ngo 		return (-1);
45037a286c47SDai Ngo 
45047a286c47SDai Ngo 	/* validate reparse string */
45057a286c47SDai Ngo 	if (reparse_validate((const char *)target))
45067a286c47SDai Ngo 		return (-1);
45077a286c47SDai Ngo 
45087a286c47SDai Ngo 	xva_init(xvattr);
45097a286c47SDai Ngo 	xvattr->xva_vattr = *vap;
45107a286c47SDai Ngo 	xvattr->xva_vattr.va_mask |= AT_XVATTR;
45117a286c47SDai Ngo 	xoap = xva_getxoptattr(xvattr);
45127a286c47SDai Ngo 	ASSERT(xoap);
45137a286c47SDai Ngo 	XVA_SET_REQ(xvattr, XAT_REPARSE);
45147a286c47SDai Ngo 	xoap->xoa_reparse = 1;
45157a286c47SDai Ngo 
45167a286c47SDai Ngo 	return (0);
45177a286c47SDai Ngo }
45182f172c55SRobert Thurlow 
45192f172c55SRobert Thurlow /*
45202f172c55SRobert Thurlow  * Function to check whether a symlink is a reparse point.
45212f172c55SRobert Thurlow  * Return B_TRUE if it is a reparse point, else return B_FALSE
45222f172c55SRobert Thurlow  */
45232f172c55SRobert Thurlow boolean_t
45242f172c55SRobert Thurlow vn_is_reparse(vnode_t *vp, cred_t *cr, caller_context_t *ct)
45252f172c55SRobert Thurlow {
45262f172c55SRobert Thurlow 	xvattr_t xvattr;
45272f172c55SRobert Thurlow 	xoptattr_t *xoap;
45282f172c55SRobert Thurlow 
45292f172c55SRobert Thurlow 	if ((vp->v_type != VLNK) ||
45302f172c55SRobert Thurlow 	    !(vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR)))
45312f172c55SRobert Thurlow 		return (B_FALSE);
45322f172c55SRobert Thurlow 
45332f172c55SRobert Thurlow 	xva_init(&xvattr);
45342f172c55SRobert Thurlow 	xoap = xva_getxoptattr(&xvattr);
45352f172c55SRobert Thurlow 	ASSERT(xoap);
45362f172c55SRobert Thurlow 	XVA_SET_REQ(&xvattr, XAT_REPARSE);
45372f172c55SRobert Thurlow 
45382f172c55SRobert Thurlow 	if (VOP_GETATTR(vp, &xvattr.xva_vattr, 0, cr, ct))
45392f172c55SRobert Thurlow 		return (B_FALSE);
45402f172c55SRobert Thurlow 
45412f172c55SRobert Thurlow 	if ((!(xvattr.xva_vattr.va_mask & AT_XVATTR)) ||
45422f172c55SRobert Thurlow 	    (!(XVA_ISSET_RTN(&xvattr, XAT_REPARSE))))
45432f172c55SRobert Thurlow 		return (B_FALSE);
45442f172c55SRobert Thurlow 
45452f172c55SRobert Thurlow 	return (xoap->xoa_reparse ? B_TRUE : B_FALSE);
45462f172c55SRobert Thurlow }
4547