xref: /illumos-gate/usr/src/uts/common/fs/vnode.c (revision 41a4986b)
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.
24edd58064SJerry Jelinek  * Copyright 2020 Joyent, Inc.
25*41a4986bSSpencer Evans-Cole  * Copyright 2022 Spencer Evans-Cole.
26b3286c38SKevin Crowe  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
27ade42b55SSebastien Roy  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
310333c437SToomas Soome /*	  All Rights Reserved	*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
357c478bd9Sstevel@tonic-gate  * The Regents of the University of California
367c478bd9Sstevel@tonic-gate  * All Rights Reserved
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
397c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
407c478bd9Sstevel@tonic-gate  * contributors.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/param.h>
457c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
467c478bd9Sstevel@tonic-gate #include <sys/errno.h>
477c478bd9Sstevel@tonic-gate #include <sys/cred.h>
487c478bd9Sstevel@tonic-gate #include <sys/user.h>
497c478bd9Sstevel@tonic-gate #include <sys/uio.h>
507c478bd9Sstevel@tonic-gate #include <sys/file.h>
517c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
527c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
53aa59c4cbSrsb #include <sys/vfs_opreg.h>
547c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
55edd58064SJerry Jelinek #include <sys/filio.h>
567c478bd9Sstevel@tonic-gate #include <sys/rwstlock.h>
577c478bd9Sstevel@tonic-gate #include <sys/fem.h>
587c478bd9Sstevel@tonic-gate #include <sys/stat.h>
597c478bd9Sstevel@tonic-gate #include <sys/mode.h>
607c478bd9Sstevel@tonic-gate #include <sys/conf.h>
617c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
627c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
637c478bd9Sstevel@tonic-gate #include <sys/systm.h>
647c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
657c478bd9Sstevel@tonic-gate #include <sys/debug.h>
667c478bd9Sstevel@tonic-gate #include <c2/audit.h>
677c478bd9Sstevel@tonic-gate #include <sys/acl.h>
687c478bd9Sstevel@tonic-gate #include <sys/nbmlock.h>
697c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
707c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
719d3574bfSNeil Perrin #include <sys/taskq.h>
727a286c47SDai Ngo #include <fs/fs_reparse.h>
73e2fc3408SPatrick Mooney #include <sys/time.h>
74e2fc3408SPatrick Mooney #include <sys/sdt.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* Determine if this vnode is a file that is read-only */
777c478bd9Sstevel@tonic-gate #define	ISROFILE(vp)	\
787c478bd9Sstevel@tonic-gate 	((vp)->v_type != VCHR && (vp)->v_type != VBLK && \
797c478bd9Sstevel@tonic-gate 	    (vp)->v_type != VFIFO && vn_is_readonly(vp))
807c478bd9Sstevel@tonic-gate 
81108322fbScarlsonj /* Tunable via /etc/system; used only by admin/install */
82108322fbScarlsonj int nfs_global_client_only;
83108322fbScarlsonj 
845a59a8b3Srsb /*
855a59a8b3Srsb  * Array of vopstats_t for per-FS-type vopstats.  This array has the same
865a59a8b3Srsb  * number of entries as and parallel to the vfssw table.  (Arguably, it could
875a59a8b3Srsb  * be part of the vfssw table.)  Once it's initialized, it's accessed using
885a59a8b3Srsb  * the same fstype index that is used to index into the vfssw table.
895a59a8b3Srsb  */
905a59a8b3Srsb vopstats_t **vopstats_fstype;
915a59a8b3Srsb 
925a59a8b3Srsb /* vopstats initialization template used for fast initialization via bcopy() */
935a59a8b3Srsb static vopstats_t *vs_templatep;
945a59a8b3Srsb 
955a59a8b3Srsb /* Kmem cache handle for vsk_anchor_t allocations */
965a59a8b3Srsb kmem_cache_t *vsk_anchor_cache;
975a59a8b3Srsb 
98df2381bfSpraks /* file events cleanup routine */
99df2381bfSpraks extern void free_fopdata(vnode_t *);
100df2381bfSpraks 
1015a59a8b3Srsb /*
1025a59a8b3Srsb  * Root of AVL tree for the kstats associated with vopstats.  Lock protects
1035a59a8b3Srsb  * updates to vsktat_tree.
1045a59a8b3Srsb  */
1055a59a8b3Srsb avl_tree_t	vskstat_tree;
1065a59a8b3Srsb kmutex_t	vskstat_tree_lock;
1075a59a8b3Srsb 
1085a59a8b3Srsb /* Global variable which enables/disables the vopstats collection */
1095a59a8b3Srsb int vopstats_enabled = 1;
1105a59a8b3Srsb 
111e2fc3408SPatrick Mooney /* Global used for empty/invalid v_path */
112e2fc3408SPatrick Mooney char *vn_vpath_empty = "";
113e2fc3408SPatrick Mooney 
1141b300de9Sjwahlig /*
1151b300de9Sjwahlig  * forward declarations for internal vnode specific data (vsd)
1161b300de9Sjwahlig  */
1171b300de9Sjwahlig static void *vsd_realloc(void *, size_t, size_t);
1181b300de9Sjwahlig 
1197a286c47SDai Ngo /*
1207a286c47SDai Ngo  * forward declarations for reparse point functions
1217a286c47SDai Ngo  */
1227a286c47SDai Ngo static int fs_reparse_mark(char *target, vattr_t *vap, xvattr_t *xvattr);
1237a286c47SDai Ngo 
1241b300de9Sjwahlig /*
1251b300de9Sjwahlig  * VSD -- VNODE SPECIFIC DATA
1261b300de9Sjwahlig  * The v_data pointer is typically used by a file system to store a
1271b300de9Sjwahlig  * pointer to the file system's private node (e.g. ufs inode, nfs rnode).
1281b300de9Sjwahlig  * However, there are times when additional project private data needs
1291b300de9Sjwahlig  * to be stored separately from the data (node) pointed to by v_data.
1301b300de9Sjwahlig  * This additional data could be stored by the file system itself or
1311b300de9Sjwahlig  * by a completely different kernel entity.  VSD provides a way for
1321b300de9Sjwahlig  * callers to obtain a key and store a pointer to private data associated
1331b300de9Sjwahlig  * with a vnode.
1341b300de9Sjwahlig  *
135d216dff5SRobert Mastors  * Callers are responsible for protecting the vsd by holding v_vsd_lock
1361b300de9Sjwahlig  * for calls to vsd_set() and vsd_get().
1371b300de9Sjwahlig  */
1381b300de9Sjwahlig 
1391b300de9Sjwahlig /*
1401b300de9Sjwahlig  * vsd_lock protects:
1411b300de9Sjwahlig  *   vsd_nkeys - creation and deletion of vsd keys
1421b300de9Sjwahlig  *   vsd_list - insertion and deletion of vsd_node in the vsd_list
1431b300de9Sjwahlig  *   vsd_destructor - adding and removing destructors to the list
1441b300de9Sjwahlig  */
1451b300de9Sjwahlig static kmutex_t		vsd_lock;
1461b300de9Sjwahlig static uint_t		vsd_nkeys;	 /* size of destructor array */
1471b300de9Sjwahlig /* list of vsd_node's */
1481b300de9Sjwahlig static list_t *vsd_list = NULL;
1491b300de9Sjwahlig /* per-key destructor funcs */
1500333c437SToomas Soome static void		(**vsd_destructor)(void *);
1511b300de9Sjwahlig 
1525a59a8b3Srsb /*
1535a59a8b3Srsb  * The following is the common set of actions needed to update the
1545a59a8b3Srsb  * vopstats structure from a vnode op.  Both VOPSTATS_UPDATE() and
1555a59a8b3Srsb  * VOPSTATS_UPDATE_IO() do almost the same thing, except for the
1565a59a8b3Srsb  * recording of the bytes transferred.  Since the code is similar
1575a59a8b3Srsb  * but small, it is nearly a duplicate.  Consequently any changes
1585a59a8b3Srsb  * to one may need to be reflected in the other.
1595a59a8b3Srsb  * Rundown of the variables:
1605a59a8b3Srsb  * vp - Pointer to the vnode
1615a59a8b3Srsb  * counter - Partial name structure member to update in vopstats for counts
1625a59a8b3Srsb  * bytecounter - Partial name structure member to update in vopstats for bytes
1635a59a8b3Srsb  * bytesval - Value to update in vopstats for bytes
1645a59a8b3Srsb  * fstype - Index into vsanchor_fstype[], same as index into vfssw[]
1655a59a8b3Srsb  * vsp - Pointer to vopstats structure (either in vfs or vsanchor_fstype[i])
1665a59a8b3Srsb  */
1675a59a8b3Srsb 
1685a59a8b3Srsb #define	VOPSTATS_UPDATE(vp, counter) {					\
1695a59a8b3Srsb 	vfs_t *vfsp = (vp)->v_vfsp;					\
170ddfcde86Srsb 	if (vfsp && vfsp->vfs_implp &&					\
171ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {	\
1725a59a8b3Srsb 		vopstats_t *vsp = &vfsp->vfs_vopstats;			\
1732bb1cb30Sbmc 		uint64_t *stataddr = &(vsp->n##counter.value.ui64);	\
1742bb1cb30Sbmc 		extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
1752bb1cb30Sbmc 		    size_t, uint64_t *);				\
1762bb1cb30Sbmc 		__dtrace_probe___fsinfo_##counter(vp, 0, stataddr);	\
1772bb1cb30Sbmc 		(*stataddr)++;						\
1785a59a8b3Srsb 		if ((vsp = vfsp->vfs_fstypevsp) != NULL) {		\
1792bb1cb30Sbmc 			vsp->n##counter.value.ui64++;			\
1805a59a8b3Srsb 		}							\
1815a59a8b3Srsb 	}								\
1825a59a8b3Srsb }
1835a59a8b3Srsb 
1845a59a8b3Srsb #define	VOPSTATS_UPDATE_IO(vp, counter, bytecounter, bytesval) {	\
1855a59a8b3Srsb 	vfs_t *vfsp = (vp)->v_vfsp;					\
186ddfcde86Srsb 	if (vfsp && vfsp->vfs_implp &&					\
187ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {	\
1885a59a8b3Srsb 		vopstats_t *vsp = &vfsp->vfs_vopstats;			\
1892bb1cb30Sbmc 		uint64_t *stataddr = &(vsp->n##counter.value.ui64);	\
1902bb1cb30Sbmc 		extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
1912bb1cb30Sbmc 		    size_t, uint64_t *);				\
1922bb1cb30Sbmc 		__dtrace_probe___fsinfo_##counter(vp, bytesval, stataddr); \
1932bb1cb30Sbmc 		(*stataddr)++;						\
1945a59a8b3Srsb 		vsp->bytecounter.value.ui64 += bytesval;		\
1955a59a8b3Srsb 		if ((vsp = vfsp->vfs_fstypevsp) != NULL) {		\
1962bb1cb30Sbmc 			vsp->n##counter.value.ui64++;			\
1975a59a8b3Srsb 			vsp->bytecounter.value.ui64 += bytesval;	\
1985a59a8b3Srsb 		}							\
1995a59a8b3Srsb 	}								\
2005a59a8b3Srsb }
2015a59a8b3Srsb 
202f48205beScasper /*
203f48205beScasper  * If the filesystem does not support XIDs map credential
204f48205beScasper  * If the vfsp is NULL, perhaps we should also map?
205f48205beScasper  */
206f48205beScasper #define	VOPXID_MAP_CR(vp, cr)	{					\
207f48205beScasper 	vfs_t *vfsp = (vp)->v_vfsp;					\
208f48205beScasper 	if (vfsp != NULL && (vfsp->vfs_flag & VFS_XID) == 0)		\
209f48205beScasper 		cr = crgetmapped(cr);					\
210f48205beScasper 	}
211f48205beScasper 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * Convert stat(2) formats to vnode types and vice versa.  (Knows about
2147c478bd9Sstevel@tonic-gate  * numerical order of S_IFMT and vnode types.)
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate enum vtype iftovt_tab[] = {
2177c478bd9Sstevel@tonic-gate 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
2187c478bd9Sstevel@tonic-gate 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
2197c478bd9Sstevel@tonic-gate };
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate ushort_t vttoif_tab[] = {
2227c478bd9Sstevel@tonic-gate 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO,
2237c478bd9Sstevel@tonic-gate 	S_IFDOOR, 0, S_IFSOCK, S_IFPORT, 0
2247c478bd9Sstevel@tonic-gate };
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * The system vnode cache.
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate kmem_cache_t *vn_cache;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * Vnode operations vector.
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static const fs_operation_trans_def_t vn_ops_table[] = {
2387c478bd9Sstevel@tonic-gate 	VOPNAME_OPEN, offsetof(struct vnodeops, vop_open),
2397c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	VOPNAME_CLOSE, offsetof(struct vnodeops, vop_close),
2427c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	VOPNAME_READ, offsetof(struct vnodeops, vop_read),
2457c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	VOPNAME_WRITE, offsetof(struct vnodeops, vop_write),
2487c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	VOPNAME_IOCTL, offsetof(struct vnodeops, vop_ioctl),
2517c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	VOPNAME_SETFL, offsetof(struct vnodeops, vop_setfl),
2547c478bd9Sstevel@tonic-gate 	    fs_setfl, fs_nosys,
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	VOPNAME_GETATTR, offsetof(struct vnodeops, vop_getattr),
2577c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	VOPNAME_SETATTR, offsetof(struct vnodeops, vop_setattr),
2607c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	VOPNAME_ACCESS, offsetof(struct vnodeops, vop_access),
2637c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	VOPNAME_LOOKUP, offsetof(struct vnodeops, vop_lookup),
2667c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	VOPNAME_CREATE, offsetof(struct vnodeops, vop_create),
2697c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	VOPNAME_REMOVE, offsetof(struct vnodeops, vop_remove),
2727c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	VOPNAME_LINK, offsetof(struct vnodeops, vop_link),
2757c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	VOPNAME_RENAME, offsetof(struct vnodeops, vop_rename),
2787c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	VOPNAME_MKDIR, offsetof(struct vnodeops, vop_mkdir),
2817c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	VOPNAME_RMDIR, offsetof(struct vnodeops, vop_rmdir),
2847c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	VOPNAME_READDIR, offsetof(struct vnodeops, vop_readdir),
2877c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	VOPNAME_SYMLINK, offsetof(struct vnodeops, vop_symlink),
2907c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	VOPNAME_READLINK, offsetof(struct vnodeops, vop_readlink),
2937c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	VOPNAME_FSYNC, offsetof(struct vnodeops, vop_fsync),
2967c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	VOPNAME_INACTIVE, offsetof(struct vnodeops, vop_inactive),
2997c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	VOPNAME_FID, offsetof(struct vnodeops, vop_fid),
3027c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	VOPNAME_RWLOCK, offsetof(struct vnodeops, vop_rwlock),
3057c478bd9Sstevel@tonic-gate 	    fs_rwlock, fs_rwlock,
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	VOPNAME_RWUNLOCK, offsetof(struct vnodeops, vop_rwunlock),
3080333c437SToomas Soome 	    (fs_generic_func_p)(uintptr_t)fs_rwunlock,
3090333c437SToomas Soome 	    (fs_generic_func_p)(uintptr_t)fs_rwunlock,	/* no errors allowed */
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	VOPNAME_SEEK, offsetof(struct vnodeops, vop_seek),
3127c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	VOPNAME_CMP, offsetof(struct vnodeops, vop_cmp),
3157c478bd9Sstevel@tonic-gate 	    fs_cmp, fs_cmp,		/* no errors allowed */
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	VOPNAME_FRLOCK, offsetof(struct vnodeops, vop_frlock),
3187c478bd9Sstevel@tonic-gate 	    fs_frlock, fs_nosys,
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	VOPNAME_SPACE, offsetof(struct vnodeops, vop_space),
3217c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	VOPNAME_REALVP, offsetof(struct vnodeops, vop_realvp),
3247c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	VOPNAME_GETPAGE, offsetof(struct vnodeops, vop_getpage),
3277c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	VOPNAME_PUTPAGE, offsetof(struct vnodeops, vop_putpage),
3307c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	VOPNAME_MAP, offsetof(struct vnodeops, vop_map),
3337c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_map,
3347c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_map,
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	VOPNAME_ADDMAP, offsetof(struct vnodeops, vop_addmap),
3377c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_addmap,
3387c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_nosys_addmap,
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	VOPNAME_DELMAP, offsetof(struct vnodeops, vop_delmap),
3417c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	VOPNAME_POLL, offsetof(struct vnodeops, vop_poll),
3447c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_poll, (fs_generic_func_p) fs_nosys_poll,
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	VOPNAME_DUMP, offsetof(struct vnodeops, vop_dump),
3477c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	VOPNAME_PATHCONF, offsetof(struct vnodeops, vop_pathconf),
3507c478bd9Sstevel@tonic-gate 	    fs_pathconf, fs_nosys,
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	VOPNAME_PAGEIO, offsetof(struct vnodeops, vop_pageio),
3537c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	VOPNAME_DUMPCTL, offsetof(struct vnodeops, vop_dumpctl),
3567c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	VOPNAME_DISPOSE, offsetof(struct vnodeops, vop_dispose),
3590333c437SToomas Soome 	    (fs_generic_func_p)(uintptr_t)fs_dispose,
3600333c437SToomas Soome 	    (fs_generic_func_p)(uintptr_t)fs_nodispose,
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	VOPNAME_SETSECATTR, offsetof(struct vnodeops, vop_setsecattr),
3637c478bd9Sstevel@tonic-gate 	    fs_nosys, fs_nosys,
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	VOPNAME_GETSECATTR, offsetof(struct vnodeops, vop_getsecattr),
3667c478bd9Sstevel@tonic-gate 	    fs_fab_acl, fs_nosys,
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	VOPNAME_SHRLOCK, offsetof(struct vnodeops, vop_shrlock),
3697c478bd9Sstevel@tonic-gate 	    fs_shrlock, fs_nosys,
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	VOPNAME_VNEVENT, offsetof(struct vnodeops, vop_vnevent),
3727c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_vnevent_nosupport,
3737c478bd9Sstevel@tonic-gate 	    (fs_generic_func_p) fs_vnevent_nosupport,
3747c478bd9Sstevel@tonic-gate 
375c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	VOPNAME_REQZCBUF, offsetof(struct vnodeops, vop_reqzcbuf),
376c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    fs_nosys, fs_nosys,
377c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
378c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	VOPNAME_RETZCBUF, offsetof(struct vnodeops, vop_retzcbuf),
379c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	    fs_nosys, fs_nosys,
380c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
3817c478bd9Sstevel@tonic-gate 	NULL, 0, NULL, NULL
3827c478bd9Sstevel@tonic-gate };
3837c478bd9Sstevel@tonic-gate 
384da6c28aaSamw /* Extensible attribute (xva) routines. */
385da6c28aaSamw 
386da6c28aaSamw /*
387da6c28aaSamw  * Zero out the structure, set the size of the requested/returned bitmaps,
388da6c28aaSamw  * set AT_XVATTR in the embedded vattr_t's va_mask, and set up the pointer
389da6c28aaSamw  * to the returned attributes array.
390da6c28aaSamw  */
391da6c28aaSamw void
xva_init(xvattr_t * xvap)392da6c28aaSamw xva_init(xvattr_t *xvap)
393da6c28aaSamw {
394da6c28aaSamw 	bzero(xvap, sizeof (xvattr_t));
395da6c28aaSamw 	xvap->xva_mapsize = XVA_MAPSIZE;
396da6c28aaSamw 	xvap->xva_magic = XVA_MAGIC;
397da6c28aaSamw 	xvap->xva_vattr.va_mask = AT_XVATTR;
398da6c28aaSamw 	xvap->xva_rtnattrmapp = &(xvap->xva_rtnattrmap)[0];
399da6c28aaSamw }
400da6c28aaSamw 
401da6c28aaSamw /*
402da6c28aaSamw  * If AT_XVATTR is set, returns a pointer to the embedded xoptattr_t
403da6c28aaSamw  * structure.  Otherwise, returns NULL.
404da6c28aaSamw  */
405da6c28aaSamw xoptattr_t *
xva_getxoptattr(xvattr_t * xvap)406da6c28aaSamw xva_getxoptattr(xvattr_t *xvap)
407da6c28aaSamw {
408da6c28aaSamw 	xoptattr_t *xoap = NULL;
409da6c28aaSamw 	if (xvap->xva_vattr.va_mask & AT_XVATTR)
410da6c28aaSamw 		xoap = &xvap->xva_xoptattrs;
411da6c28aaSamw 	return (xoap);
412da6c28aaSamw }
413da6c28aaSamw 
4145a59a8b3Srsb /*
4155a59a8b3Srsb  * Used by the AVL routines to compare two vsk_anchor_t structures in the tree.
4165a59a8b3Srsb  * We use the f_fsid reported by VFS_STATVFS() since we use that for the
4175a59a8b3Srsb  * kstat name.
4185a59a8b3Srsb  */
4195a59a8b3Srsb static int
vska_compar(const void * n1,const void * n2)4205a59a8b3Srsb vska_compar(const void *n1, const void *n2)
4215a59a8b3Srsb {
4225a59a8b3Srsb 	int ret;
4235a59a8b3Srsb 	ulong_t p1 = ((vsk_anchor_t *)n1)->vsk_fsid;
4245a59a8b3Srsb 	ulong_t p2 = ((vsk_anchor_t *)n2)->vsk_fsid;
4255a59a8b3Srsb 
4265a59a8b3Srsb 	if (p1 < p2) {
4275a59a8b3Srsb 		ret = -1;
4285a59a8b3Srsb 	} else if (p1 > p2) {
4295a59a8b3Srsb 		ret = 1;
4305a59a8b3Srsb 	} else {
4315a59a8b3Srsb 		ret = 0;
4325a59a8b3Srsb 	}
4335a59a8b3Srsb 
4345a59a8b3Srsb 	return (ret);
4355a59a8b3Srsb }
4365a59a8b3Srsb 
4375a59a8b3Srsb /*
4385a59a8b3Srsb  * Used to create a single template which will be bcopy()ed to a newly
4395a59a8b3Srsb  * allocated vsanchor_combo_t structure in new_vsanchor(), below.
4405a59a8b3Srsb  */
4415a59a8b3Srsb static vopstats_t *
create_vopstats_template()4425a59a8b3Srsb create_vopstats_template()
4435a59a8b3Srsb {
4445a59a8b3Srsb 	vopstats_t		*vsp;
4455a59a8b3Srsb 
4465a59a8b3Srsb 	vsp = kmem_alloc(sizeof (vopstats_t), KM_SLEEP);
4475a59a8b3Srsb 	bzero(vsp, sizeof (*vsp));	/* Start fresh */
4485a59a8b3Srsb 
4495a59a8b3Srsb 	/* VOP_OPEN */
4505a59a8b3Srsb 	kstat_named_init(&vsp->nopen, "nopen", KSTAT_DATA_UINT64);
4515a59a8b3Srsb 	/* VOP_CLOSE */
4525a59a8b3Srsb 	kstat_named_init(&vsp->nclose, "nclose", KSTAT_DATA_UINT64);
4535a59a8b3Srsb 	/* VOP_READ I/O */
4545a59a8b3Srsb 	kstat_named_init(&vsp->nread, "nread", KSTAT_DATA_UINT64);
4555a59a8b3Srsb 	kstat_named_init(&vsp->read_bytes, "read_bytes", KSTAT_DATA_UINT64);
4565a59a8b3Srsb 	/* VOP_WRITE I/O */
4575a59a8b3Srsb 	kstat_named_init(&vsp->nwrite, "nwrite", KSTAT_DATA_UINT64);
4585a59a8b3Srsb 	kstat_named_init(&vsp->write_bytes, "write_bytes", KSTAT_DATA_UINT64);
4595a59a8b3Srsb 	/* VOP_IOCTL */
4605a59a8b3Srsb 	kstat_named_init(&vsp->nioctl, "nioctl", KSTAT_DATA_UINT64);
4615a59a8b3Srsb 	/* VOP_SETFL */
4625a59a8b3Srsb 	kstat_named_init(&vsp->nsetfl, "nsetfl", KSTAT_DATA_UINT64);
4635a59a8b3Srsb 	/* VOP_GETATTR */
4645a59a8b3Srsb 	kstat_named_init(&vsp->ngetattr, "ngetattr", KSTAT_DATA_UINT64);
4655a59a8b3Srsb 	/* VOP_SETATTR */
4665a59a8b3Srsb 	kstat_named_init(&vsp->nsetattr, "nsetattr", KSTAT_DATA_UINT64);
4675a59a8b3Srsb 	/* VOP_ACCESS */
4685a59a8b3Srsb 	kstat_named_init(&vsp->naccess, "naccess", KSTAT_DATA_UINT64);
4695a59a8b3Srsb 	/* VOP_LOOKUP */
4705a59a8b3Srsb 	kstat_named_init(&vsp->nlookup, "nlookup", KSTAT_DATA_UINT64);
4715a59a8b3Srsb 	/* VOP_CREATE */
4725a59a8b3Srsb 	kstat_named_init(&vsp->ncreate, "ncreate", KSTAT_DATA_UINT64);
4735a59a8b3Srsb 	/* VOP_REMOVE */
4745a59a8b3Srsb 	kstat_named_init(&vsp->nremove, "nremove", KSTAT_DATA_UINT64);
4755a59a8b3Srsb 	/* VOP_LINK */
4765a59a8b3Srsb 	kstat_named_init(&vsp->nlink, "nlink", KSTAT_DATA_UINT64);
4775a59a8b3Srsb 	/* VOP_RENAME */
4785a59a8b3Srsb 	kstat_named_init(&vsp->nrename, "nrename", KSTAT_DATA_UINT64);
4795a59a8b3Srsb 	/* VOP_MKDIR */
4805a59a8b3Srsb 	kstat_named_init(&vsp->nmkdir, "nmkdir", KSTAT_DATA_UINT64);
4815a59a8b3Srsb 	/* VOP_RMDIR */
4825a59a8b3Srsb 	kstat_named_init(&vsp->nrmdir, "nrmdir", KSTAT_DATA_UINT64);
4835a59a8b3Srsb 	/* VOP_READDIR I/O */
4845a59a8b3Srsb 	kstat_named_init(&vsp->nreaddir, "nreaddir", KSTAT_DATA_UINT64);
4855a59a8b3Srsb 	kstat_named_init(&vsp->readdir_bytes, "readdir_bytes",
4865a59a8b3Srsb 	    KSTAT_DATA_UINT64);
4875a59a8b3Srsb 	/* VOP_SYMLINK */
4885a59a8b3Srsb 	kstat_named_init(&vsp->nsymlink, "nsymlink", KSTAT_DATA_UINT64);
4895a59a8b3Srsb 	/* VOP_READLINK */
4905a59a8b3Srsb 	kstat_named_init(&vsp->nreadlink, "nreadlink", KSTAT_DATA_UINT64);
4915a59a8b3Srsb 	/* VOP_FSYNC */
4925a59a8b3Srsb 	kstat_named_init(&vsp->nfsync, "nfsync", KSTAT_DATA_UINT64);
4935a59a8b3Srsb 	/* VOP_INACTIVE */
4945a59a8b3Srsb 	kstat_named_init(&vsp->ninactive, "ninactive", KSTAT_DATA_UINT64);
4955a59a8b3Srsb 	/* VOP_FID */
4965a59a8b3Srsb 	kstat_named_init(&vsp->nfid, "nfid", KSTAT_DATA_UINT64);
4975a59a8b3Srsb 	/* VOP_RWLOCK */
4985a59a8b3Srsb 	kstat_named_init(&vsp->nrwlock, "nrwlock", KSTAT_DATA_UINT64);
4995a59a8b3Srsb 	/* VOP_RWUNLOCK */
5005a59a8b3Srsb 	kstat_named_init(&vsp->nrwunlock, "nrwunlock", KSTAT_DATA_UINT64);
5015a59a8b3Srsb 	/* VOP_SEEK */
5025a59a8b3Srsb 	kstat_named_init(&vsp->nseek, "nseek", KSTAT_DATA_UINT64);
5035a59a8b3Srsb 	/* VOP_CMP */
5045a59a8b3Srsb 	kstat_named_init(&vsp->ncmp, "ncmp", KSTAT_DATA_UINT64);
5055a59a8b3Srsb 	/* VOP_FRLOCK */
5065a59a8b3Srsb 	kstat_named_init(&vsp->nfrlock, "nfrlock", KSTAT_DATA_UINT64);
5075a59a8b3Srsb 	/* VOP_SPACE */
5085a59a8b3Srsb 	kstat_named_init(&vsp->nspace, "nspace", KSTAT_DATA_UINT64);
5095a59a8b3Srsb 	/* VOP_REALVP */
5105a59a8b3Srsb 	kstat_named_init(&vsp->nrealvp, "nrealvp", KSTAT_DATA_UINT64);
5115a59a8b3Srsb 	/* VOP_GETPAGE */
5125a59a8b3Srsb 	kstat_named_init(&vsp->ngetpage, "ngetpage", KSTAT_DATA_UINT64);
5135a59a8b3Srsb 	/* VOP_PUTPAGE */
5145a59a8b3Srsb 	kstat_named_init(&vsp->nputpage, "nputpage", KSTAT_DATA_UINT64);
5155a59a8b3Srsb 	/* VOP_MAP */
5165a59a8b3Srsb 	kstat_named_init(&vsp->nmap, "nmap", KSTAT_DATA_UINT64);
5175a59a8b3Srsb 	/* VOP_ADDMAP */
5185a59a8b3Srsb 	kstat_named_init(&vsp->naddmap, "naddmap", KSTAT_DATA_UINT64);
5195a59a8b3Srsb 	/* VOP_DELMAP */
5205a59a8b3Srsb 	kstat_named_init(&vsp->ndelmap, "ndelmap", KSTAT_DATA_UINT64);
5215a59a8b3Srsb 	/* VOP_POLL */
5225a59a8b3Srsb 	kstat_named_init(&vsp->npoll, "npoll", KSTAT_DATA_UINT64);
5235a59a8b3Srsb 	/* VOP_DUMP */
5245a59a8b3Srsb 	kstat_named_init(&vsp->ndump, "ndump", KSTAT_DATA_UINT64);
5255a59a8b3Srsb 	/* VOP_PATHCONF */
5265a59a8b3Srsb 	kstat_named_init(&vsp->npathconf, "npathconf", KSTAT_DATA_UINT64);
5275a59a8b3Srsb 	/* VOP_PAGEIO */
5285a59a8b3Srsb 	kstat_named_init(&vsp->npageio, "npageio", KSTAT_DATA_UINT64);
5295a59a8b3Srsb 	/* VOP_DUMPCTL */
5305a59a8b3Srsb 	kstat_named_init(&vsp->ndumpctl, "ndumpctl", KSTAT_DATA_UINT64);
5315a59a8b3Srsb 	/* VOP_DISPOSE */
5325a59a8b3Srsb 	kstat_named_init(&vsp->ndispose, "ndispose", KSTAT_DATA_UINT64);
5335a59a8b3Srsb 	/* VOP_SETSECATTR */
5345a59a8b3Srsb 	kstat_named_init(&vsp->nsetsecattr, "nsetsecattr", KSTAT_DATA_UINT64);
5355a59a8b3Srsb 	/* VOP_GETSECATTR */
5365a59a8b3Srsb 	kstat_named_init(&vsp->ngetsecattr, "ngetsecattr", KSTAT_DATA_UINT64);
5375a59a8b3Srsb 	/* VOP_SHRLOCK */
5385a59a8b3Srsb 	kstat_named_init(&vsp->nshrlock, "nshrlock", KSTAT_DATA_UINT64);
5395a59a8b3Srsb 	/* VOP_VNEVENT */
5405a59a8b3Srsb 	kstat_named_init(&vsp->nvnevent, "nvnevent", KSTAT_DATA_UINT64);
541c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	/* VOP_REQZCBUF */
542c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kstat_named_init(&vsp->nreqzcbuf, "nreqzcbuf", KSTAT_DATA_UINT64);
543c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	/* VOP_RETZCBUF */
544c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	kstat_named_init(&vsp->nretzcbuf, "nretzcbuf", KSTAT_DATA_UINT64);
5455a59a8b3Srsb 
5465a59a8b3Srsb 	return (vsp);
5475a59a8b3Srsb }
5485a59a8b3Srsb 
5495a59a8b3Srsb /*
5505a59a8b3Srsb  * Creates a kstat structure associated with a vopstats structure.
5515a59a8b3Srsb  */
5525a59a8b3Srsb kstat_t *
new_vskstat(char * ksname,vopstats_t * vsp)5535a59a8b3Srsb new_vskstat(char *ksname, vopstats_t *vsp)
5545a59a8b3Srsb {
5555a59a8b3Srsb 	kstat_t		*ksp;
5565a59a8b3Srsb 
5575a59a8b3Srsb 	if (!vopstats_enabled) {
5585a59a8b3Srsb 		return (NULL);
5595a59a8b3Srsb 	}
5605a59a8b3Srsb 
5615a59a8b3Srsb 	ksp = kstat_create("unix", 0, ksname, "misc", KSTAT_TYPE_NAMED,
5625a59a8b3Srsb 	    sizeof (vopstats_t)/sizeof (kstat_named_t),
5635a59a8b3Srsb 	    KSTAT_FLAG_VIRTUAL|KSTAT_FLAG_WRITABLE);
5645a59a8b3Srsb 	if (ksp) {
5655a59a8b3Srsb 		ksp->ks_data = vsp;
5665a59a8b3Srsb 		kstat_install(ksp);
5675a59a8b3Srsb 	}
5685a59a8b3Srsb 
5695a59a8b3Srsb 	return (ksp);
5705a59a8b3Srsb }
5715a59a8b3Srsb 
5725a59a8b3Srsb /*
5735a59a8b3Srsb  * Called from vfsinit() to initialize the support mechanisms for vopstats
5745a59a8b3Srsb  */
5755a59a8b3Srsb void
vopstats_startup()5765a59a8b3Srsb vopstats_startup()
5775a59a8b3Srsb {
5785a59a8b3Srsb 	if (!vopstats_enabled)
5795a59a8b3Srsb 		return;
5805a59a8b3Srsb 
5815a59a8b3Srsb 	/*
5825a59a8b3Srsb 	 * Creates the AVL tree which holds per-vfs vopstat anchors.  This
5835a59a8b3Srsb 	 * is necessary since we need to check if a kstat exists before we
5845a59a8b3Srsb 	 * attempt to create it.  Also, initialize its lock.
5855a59a8b3Srsb 	 */
5865a59a8b3Srsb 	avl_create(&vskstat_tree, vska_compar, sizeof (vsk_anchor_t),
5875a59a8b3Srsb 	    offsetof(vsk_anchor_t, vsk_node));
5885a59a8b3Srsb 	mutex_init(&vskstat_tree_lock, NULL, MUTEX_DEFAULT, NULL);
5895a59a8b3Srsb 
5905a59a8b3Srsb 	vsk_anchor_cache = kmem_cache_create("vsk_anchor_cache",
5915a59a8b3Srsb 	    sizeof (vsk_anchor_t), sizeof (uintptr_t), NULL, NULL, NULL,
5925a59a8b3Srsb 	    NULL, NULL, 0);
5935a59a8b3Srsb 
5945a59a8b3Srsb 	/*
5955a59a8b3Srsb 	 * Set up the array of pointers for the vopstats-by-FS-type.
5965a59a8b3Srsb 	 * The entries will be allocated/initialized as each file system
5975a59a8b3Srsb 	 * goes through modload/mod_installfs.
5985a59a8b3Srsb 	 */
5995a59a8b3Srsb 	vopstats_fstype = (vopstats_t **)kmem_zalloc(
6005a59a8b3Srsb 	    (sizeof (vopstats_t *) * nfstype), KM_SLEEP);
6015a59a8b3Srsb 
6025a59a8b3Srsb 	/* Set up the global vopstats initialization template */
6035a59a8b3Srsb 	vs_templatep = create_vopstats_template();
6045a59a8b3Srsb }
6055a59a8b3Srsb 
6065a59a8b3Srsb /*
6075a59a8b3Srsb  * We need to have the all of the counters zeroed.
6085a59a8b3Srsb  * The initialization of the vopstats_t includes on the order of
6095a59a8b3Srsb  * 50 calls to kstat_named_init().  Rather that do that on every call,
6105a59a8b3Srsb  * we do it once in a template (vs_templatep) then bcopy it over.
6115a59a8b3Srsb  */
6125a59a8b3Srsb void
initialize_vopstats(vopstats_t * vsp)6135a59a8b3Srsb initialize_vopstats(vopstats_t *vsp)
6145a59a8b3Srsb {
6155a59a8b3Srsb 	if (vsp == NULL)
6165a59a8b3Srsb 		return;
6175a59a8b3Srsb 
6185a59a8b3Srsb 	bcopy(vs_templatep, vsp, sizeof (vopstats_t));
6195a59a8b3Srsb }
6205a59a8b3Srsb 
6215a59a8b3Srsb /*
62282c7f3c4Srsb  * If possible, determine which vopstats by fstype to use and
62382c7f3c4Srsb  * return a pointer to the caller.
6245a59a8b3Srsb  */
62582c7f3c4Srsb vopstats_t *
get_fstype_vopstats(vfs_t * vfsp,struct vfssw * vswp)62682c7f3c4Srsb get_fstype_vopstats(vfs_t *vfsp, struct vfssw *vswp)
6275a59a8b3Srsb {
62882c7f3c4Srsb 	int		fstype = 0;	/* Index into vfssw[] */
62982c7f3c4Srsb 	vopstats_t	*vsp = NULL;
6305a59a8b3Srsb 
6315a59a8b3Srsb 	if (vfsp == NULL || (vfsp->vfs_flag & VFS_STATS) == 0 ||
6325a59a8b3Srsb 	    !vopstats_enabled)
63382c7f3c4Srsb 		return (NULL);
6345a59a8b3Srsb 	/*
6355a59a8b3Srsb 	 * Set up the fstype.  We go to so much trouble because all versions
6365a59a8b3Srsb 	 * of NFS use the same fstype in their vfs even though they have
6375a59a8b3Srsb 	 * distinct entries in the vfssw[] table.
63882c7f3c4Srsb 	 * NOTE: A special vfs (e.g., EIO_vfs) may not have an entry.
6395a59a8b3Srsb 	 */
64082c7f3c4Srsb 	if (vswp) {
64182c7f3c4Srsb 		fstype = vswp - vfssw;	/* Gets us the index */
6425a59a8b3Srsb 	} else {
6435a59a8b3Srsb 		fstype = vfsp->vfs_fstype;
6445a59a8b3Srsb 	}
6455a59a8b3Srsb 
6465a59a8b3Srsb 	/*
6475a59a8b3Srsb 	 * Point to the per-fstype vopstats. The only valid values are
6485a59a8b3Srsb 	 * non-zero positive values less than the number of vfssw[] table
6495a59a8b3Srsb 	 * entries.
6505a59a8b3Srsb 	 */
6515a59a8b3Srsb 	if (fstype > 0 && fstype < nfstype) {
65282c7f3c4Srsb 		vsp = vopstats_fstype[fstype];
6535a59a8b3Srsb 	}
6545a59a8b3Srsb 
65582c7f3c4Srsb 	return (vsp);
65682c7f3c4Srsb }
65782c7f3c4Srsb 
65882c7f3c4Srsb /*
65982c7f3c4Srsb  * Generate a kstat name, create the kstat structure, and allocate a
66082c7f3c4Srsb  * vsk_anchor_t to hold it together.  Return the pointer to the vsk_anchor_t
66182c7f3c4Srsb  * to the caller.  This must only be called from a mount.
66282c7f3c4Srsb  */
66382c7f3c4Srsb vsk_anchor_t *
get_vskstat_anchor(vfs_t * vfsp)66482c7f3c4Srsb get_vskstat_anchor(vfs_t *vfsp)
66582c7f3c4Srsb {
66682c7f3c4Srsb 	char		kstatstr[KSTAT_STRLEN]; /* kstat name for vopstats */
66782c7f3c4Srsb 	statvfs64_t	statvfsbuf;		/* Needed to find f_fsid */
66882c7f3c4Srsb 	vsk_anchor_t	*vskp = NULL;		/* vfs <--> kstat anchor */
66982c7f3c4Srsb 	kstat_t		*ksp;			/* Ptr to new kstat */
67082c7f3c4Srsb 	avl_index_t	where;			/* Location in the AVL tree */
67182c7f3c4Srsb 
672ddfcde86Srsb 	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
673ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
67482c7f3c4Srsb 		return (NULL);
67582c7f3c4Srsb 
6765a59a8b3Srsb 	/* Need to get the fsid to build a kstat name */
6775a59a8b3Srsb 	if (VFS_STATVFS(vfsp, &statvfsbuf) == 0) {
6785a59a8b3Srsb 		/* Create a name for our kstats based on fsid */
6795a59a8b3Srsb 		(void) snprintf(kstatstr, KSTAT_STRLEN, "%s%lx",
6805a59a8b3Srsb 		    VOPSTATS_STR, statvfsbuf.f_fsid);
6815a59a8b3Srsb 
6825a59a8b3Srsb 		/* Allocate and initialize the vsk_anchor_t */
6835a59a8b3Srsb 		vskp = kmem_cache_alloc(vsk_anchor_cache, KM_SLEEP);
6845a59a8b3Srsb 		bzero(vskp, sizeof (*vskp));
6855a59a8b3Srsb 		vskp->vsk_fsid = statvfsbuf.f_fsid;
6865a59a8b3Srsb 
6875a59a8b3Srsb 		mutex_enter(&vskstat_tree_lock);
6885a59a8b3Srsb 		if (avl_find(&vskstat_tree, vskp, &where) == NULL) {
6895a59a8b3Srsb 			avl_insert(&vskstat_tree, vskp, where);
6905a59a8b3Srsb 			mutex_exit(&vskstat_tree_lock);
6915a59a8b3Srsb 
6925a59a8b3Srsb 			/*
6935a59a8b3Srsb 			 * Now that we've got the anchor in the AVL
6945a59a8b3Srsb 			 * tree, we can create the kstat.
6955a59a8b3Srsb 			 */
6965a59a8b3Srsb 			ksp = new_vskstat(kstatstr, &vfsp->vfs_vopstats);
6975a59a8b3Srsb 			if (ksp) {
6985a59a8b3Srsb 				vskp->vsk_ksp = ksp;
6995a59a8b3Srsb 			}
7005a59a8b3Srsb 		} else {
7015a59a8b3Srsb 			/* Oops, found one! Release memory and lock. */
7025a59a8b3Srsb 			mutex_exit(&vskstat_tree_lock);
7035a59a8b3Srsb 			kmem_cache_free(vsk_anchor_cache, vskp);
70482c7f3c4Srsb 			vskp = NULL;
7055a59a8b3Srsb 		}
7065a59a8b3Srsb 	}
70782c7f3c4Srsb 	return (vskp);
7085a59a8b3Srsb }
7095a59a8b3Srsb 
7105a59a8b3Srsb /*
7115a59a8b3Srsb  * We're in the process of tearing down the vfs and need to cleanup
7125a59a8b3Srsb  * the data structures associated with the vopstats. Must only be called
7135a59a8b3Srsb  * from dounmount().
7145a59a8b3Srsb  */
7155a59a8b3Srsb void
teardown_vopstats(vfs_t * vfsp)7165a59a8b3Srsb teardown_vopstats(vfs_t *vfsp)
7175a59a8b3Srsb {
7185a59a8b3Srsb 	vsk_anchor_t	*vskap;
7195a59a8b3Srsb 	avl_index_t	where;
7205a59a8b3Srsb 
721ddfcde86Srsb 	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
722ddfcde86Srsb 	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
7235a59a8b3Srsb 		return;
7245a59a8b3Srsb 
7255a59a8b3Srsb 	/* This is a safe check since VFS_STATS must be set (see above) */
7265a59a8b3Srsb 	if ((vskap = vfsp->vfs_vskap) == NULL)
7275a59a8b3Srsb 		return;
7285a59a8b3Srsb 
7295a59a8b3Srsb 	/* Whack the pointer right away */
7305a59a8b3Srsb 	vfsp->vfs_vskap = NULL;
7315a59a8b3Srsb 
7325a59a8b3Srsb 	/* Lock the tree, remove the node, and delete the kstat */
7335a59a8b3Srsb 	mutex_enter(&vskstat_tree_lock);
7345a59a8b3Srsb 	if (avl_find(&vskstat_tree, vskap, &where)) {
7355a59a8b3Srsb 		avl_remove(&vskstat_tree, vskap);
7365a59a8b3Srsb 	}
7375a59a8b3Srsb 
7385a59a8b3Srsb 	if (vskap->vsk_ksp) {
7395a59a8b3Srsb 		kstat_delete(vskap->vsk_ksp);
7405a59a8b3Srsb 	}
7415a59a8b3Srsb 	mutex_exit(&vskstat_tree_lock);
7425a59a8b3Srsb 
7435a59a8b3Srsb 	kmem_cache_free(vsk_anchor_cache, vskap);
7445a59a8b3Srsb }
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate /*
7477c478bd9Sstevel@tonic-gate  * Read or write a vnode.  Called from kernel code.
7487c478bd9Sstevel@tonic-gate  */
7497c478bd9Sstevel@tonic-gate int
vn_rdwr(enum uio_rw rw,struct vnode * vp,caddr_t base,ssize_t len,offset_t offset,enum uio_seg seg,int ioflag,rlim64_t ulimit,cred_t * cr,ssize_t * residp)7507c478bd9Sstevel@tonic-gate vn_rdwr(
7517c478bd9Sstevel@tonic-gate 	enum uio_rw rw,
7527c478bd9Sstevel@tonic-gate 	struct vnode *vp,
7537c478bd9Sstevel@tonic-gate 	caddr_t base,
7547c478bd9Sstevel@tonic-gate 	ssize_t len,
7557c478bd9Sstevel@tonic-gate 	offset_t offset,
7567c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
7577c478bd9Sstevel@tonic-gate 	int ioflag,
7587c478bd9Sstevel@tonic-gate 	rlim64_t ulimit,	/* meaningful only if rw is UIO_WRITE */
7597c478bd9Sstevel@tonic-gate 	cred_t *cr,
7607c478bd9Sstevel@tonic-gate 	ssize_t *residp)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	struct uio uio;
7637c478bd9Sstevel@tonic-gate 	struct iovec iov;
7647c478bd9Sstevel@tonic-gate 	int error;
7657c478bd9Sstevel@tonic-gate 	int in_crit = 0;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (rw == UIO_WRITE && ISROFILE(vp))
7687c478bd9Sstevel@tonic-gate 		return (EROFS);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (len < 0)
7717c478bd9Sstevel@tonic-gate 		return (EIO);
7727c478bd9Sstevel@tonic-gate 
773f48205beScasper 	VOPXID_MAP_CR(vp, cr);
774f48205beScasper 
7757c478bd9Sstevel@tonic-gate 	iov.iov_base = base;
7767c478bd9Sstevel@tonic-gate 	iov.iov_len = len;
7777c478bd9Sstevel@tonic-gate 	uio.uio_iov = &iov;
7787c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
7797c478bd9Sstevel@tonic-gate 	uio.uio_loffset = offset;
7807c478bd9Sstevel@tonic-gate 	uio.uio_segflg = (short)seg;
7817c478bd9Sstevel@tonic-gate 	uio.uio_resid = len;
7827c478bd9Sstevel@tonic-gate 	uio.uio_llimit = ulimit;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	/*
7857c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
7867c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
7877c478bd9Sstevel@tonic-gate 	 */
7887c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
7897c478bd9Sstevel@tonic-gate 		int svmand;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
7927c478bd9Sstevel@tonic-gate 		in_crit = 1;
7937c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, cr, &svmand);
7947c478bd9Sstevel@tonic-gate 		if (error != 0)
7957c478bd9Sstevel@tonic-gate 			goto done;
7967c478bd9Sstevel@tonic-gate 		if (nbl_conflict(vp, rw == UIO_WRITE ? NBL_WRITE : NBL_READ,
797da6c28aaSamw 		    uio.uio_offset, uio.uio_resid, svmand, NULL)) {
7987c478bd9Sstevel@tonic-gate 			error = EACCES;
7997c478bd9Sstevel@tonic-gate 			goto done;
8007c478bd9Sstevel@tonic-gate 		}
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp,
804b9c98e29Spf 	    rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
8057c478bd9Sstevel@tonic-gate 	if (rw == UIO_WRITE) {
8067c478bd9Sstevel@tonic-gate 		uio.uio_fmode = FWRITE;
8077c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_DEFAULT;
8087c478bd9Sstevel@tonic-gate 		error = VOP_WRITE(vp, &uio, ioflag, cr, NULL);
8097c478bd9Sstevel@tonic-gate 	} else {
8107c478bd9Sstevel@tonic-gate 		uio.uio_fmode = FREAD;
8117c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_CACHED;
8127c478bd9Sstevel@tonic-gate 		error = VOP_READ(vp, &uio, ioflag, cr, NULL);
8137c478bd9Sstevel@tonic-gate 	}
814da6c28aaSamw 	VOP_RWUNLOCK(vp,
815da6c28aaSamw 	    rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
8167c478bd9Sstevel@tonic-gate 	if (residp)
8177c478bd9Sstevel@tonic-gate 		*residp = uio.uio_resid;
8187c478bd9Sstevel@tonic-gate 	else if (uio.uio_resid)
8197c478bd9Sstevel@tonic-gate 		error = EIO;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate done:
8227c478bd9Sstevel@tonic-gate 	if (in_crit)
8237c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
8247c478bd9Sstevel@tonic-gate 	return (error);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate /*
8287c478bd9Sstevel@tonic-gate  * Release a vnode.  Call VOP_INACTIVE on last reference or
8297c478bd9Sstevel@tonic-gate  * decrement reference count.
8307c478bd9Sstevel@tonic-gate  *
8317c478bd9Sstevel@tonic-gate  * To avoid race conditions, the v_count is left at 1 for
8327c478bd9Sstevel@tonic-gate  * the call to VOP_INACTIVE. This prevents another thread
8337c478bd9Sstevel@tonic-gate  * from reclaiming and releasing the vnode *before* the
8347c478bd9Sstevel@tonic-gate  * VOP_INACTIVE routine has a chance to destroy the vnode.
8357c478bd9Sstevel@tonic-gate  * We can't have more than 1 thread calling VOP_INACTIVE
8367c478bd9Sstevel@tonic-gate  * on a vnode.
8377c478bd9Sstevel@tonic-gate  */
8387c478bd9Sstevel@tonic-gate void
vn_rele(vnode_t * vp)8397c478bd9Sstevel@tonic-gate vn_rele(vnode_t *vp)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
8427c478bd9Sstevel@tonic-gate 	if (vp->v_count == 1) {
8437c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
844da6c28aaSamw 		VOP_INACTIVE(vp, CRED(), NULL);
845b5fca8f8Stomee 		return;
846b5fca8f8Stomee 	}
847*41a4986bSSpencer Evans-Cole 	VERIFY(vp->v_count > 0);
848ade42b55SSebastien Roy 	VN_RELE_LOCKED(vp);
849b5fca8f8Stomee 	mutex_exit(&vp->v_lock);
850b5fca8f8Stomee }
851b5fca8f8Stomee 
852b5fca8f8Stomee /*
853b5fca8f8Stomee  * Release a vnode referenced by the DNLC. Multiple DNLC references are treated
854b5fca8f8Stomee  * as a single reference, so v_count is not decremented until the last DNLC hold
855b5fca8f8Stomee  * is released. This makes it possible to distinguish vnodes that are referenced
856b5fca8f8Stomee  * only by the DNLC.
857b5fca8f8Stomee  */
858b5fca8f8Stomee void
vn_rele_dnlc(vnode_t * vp)859b5fca8f8Stomee vn_rele_dnlc(vnode_t *vp)
860b5fca8f8Stomee {
861b5fca8f8Stomee 	mutex_enter(&vp->v_lock);
862*41a4986bSSpencer Evans-Cole 	VERIFY((vp->v_count > 0) && (vp->v_count_dnlc > 0));
863b5fca8f8Stomee 	if (--vp->v_count_dnlc == 0) {
864b5fca8f8Stomee 		if (vp->v_count == 1) {
865b5fca8f8Stomee 			mutex_exit(&vp->v_lock);
866b5fca8f8Stomee 			VOP_INACTIVE(vp, CRED(), NULL);
867b5fca8f8Stomee 			return;
868b5fca8f8Stomee 		}
869ade42b55SSebastien Roy 		VN_RELE_LOCKED(vp);
8707c478bd9Sstevel@tonic-gate 	}
871b5fca8f8Stomee 	mutex_exit(&vp->v_lock);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate  * Like vn_rele() except that it clears v_stream under v_lock.
876ade42b55SSebastien Roy  * This is used by sockfs when it dismantles the association between
877ade42b55SSebastien Roy  * the sockfs node and the vnode in the underlying file system.
8787c478bd9Sstevel@tonic-gate  * v_lock has to be held to prevent a thread coming through the lookupname
8797c478bd9Sstevel@tonic-gate  * path from accessing a stream head that is going away.
8807c478bd9Sstevel@tonic-gate  */
8817c478bd9Sstevel@tonic-gate void
vn_rele_stream(vnode_t * vp)8827c478bd9Sstevel@tonic-gate vn_rele_stream(vnode_t *vp)
8837c478bd9Sstevel@tonic-gate {
8847c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
8857c478bd9Sstevel@tonic-gate 	vp->v_stream = NULL;
8867c478bd9Sstevel@tonic-gate 	if (vp->v_count == 1) {
8877c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
888da6c28aaSamw 		VOP_INACTIVE(vp, CRED(), NULL);
889b5fca8f8Stomee 		return;
8907c478bd9Sstevel@tonic-gate 	}
891*41a4986bSSpencer Evans-Cole 	VERIFY(vp->v_count > 0);
892ade42b55SSebastien Roy 	VN_RELE_LOCKED(vp);
893b5fca8f8Stomee 	mutex_exit(&vp->v_lock);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8969d3574bfSNeil Perrin static void
vn_rele_inactive(vnode_t * vp)8979d3574bfSNeil Perrin vn_rele_inactive(vnode_t *vp)
8989d3574bfSNeil Perrin {
8999d3574bfSNeil Perrin 	VOP_INACTIVE(vp, CRED(), NULL);
9009d3574bfSNeil Perrin }
9019d3574bfSNeil Perrin 
9029d3574bfSNeil Perrin /*
9039d3574bfSNeil Perrin  * Like vn_rele() except if we are going to call VOP_INACTIVE() then do it
9049d3574bfSNeil Perrin  * asynchronously using a taskq. This can avoid deadlocks caused by re-entering
9059d3574bfSNeil Perrin  * the file system as a result of releasing the vnode. Note, file systems
9069d3574bfSNeil Perrin  * already have to handle the race where the vnode is incremented before the
9079d3574bfSNeil Perrin  * inactive routine is called and does its locking.
9089d3574bfSNeil Perrin  *
9099d3574bfSNeil Perrin  * Warning: Excessive use of this routine can lead to performance problems.
9109d3574bfSNeil Perrin  * This is because taskqs throttle back allocation if too many are created.
9119d3574bfSNeil Perrin  */
9129d3574bfSNeil Perrin void
vn_rele_async(vnode_t * vp,taskq_t * taskq)9139d3574bfSNeil Perrin vn_rele_async(vnode_t *vp, taskq_t *taskq)
9149d3574bfSNeil Perrin {
9159d3574bfSNeil Perrin 	mutex_enter(&vp->v_lock);
9169d3574bfSNeil Perrin 	if (vp->v_count == 1) {
9179d3574bfSNeil Perrin 		mutex_exit(&vp->v_lock);
9189d3574bfSNeil Perrin 		VERIFY(taskq_dispatch(taskq, (task_func_t *)vn_rele_inactive,
919fc8ae2ecSToomas Soome 		    vp, TQ_SLEEP) != TASKQID_INVALID);
9209d3574bfSNeil Perrin 		return;
9219d3574bfSNeil Perrin 	}
922*41a4986bSSpencer Evans-Cole 	VERIFY(vp->v_count > 0);
923ade42b55SSebastien Roy 	VN_RELE_LOCKED(vp);
9249d3574bfSNeil Perrin 	mutex_exit(&vp->v_lock);
9259d3574bfSNeil Perrin }
9269d3574bfSNeil Perrin 
9277c478bd9Sstevel@tonic-gate int
vn_open(char * pnamep,enum uio_seg seg,int filemode,int createmode,struct vnode ** vpp,enum create crwhy,mode_t umask)9287c478bd9Sstevel@tonic-gate vn_open(
9297c478bd9Sstevel@tonic-gate 	char *pnamep,
9307c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
9317c478bd9Sstevel@tonic-gate 	int filemode,
9327c478bd9Sstevel@tonic-gate 	int createmode,
9337c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
9347c478bd9Sstevel@tonic-gate 	enum create crwhy,
9357c478bd9Sstevel@tonic-gate 	mode_t umask)
9367c478bd9Sstevel@tonic-gate {
937da6c28aaSamw 	return (vn_openat(pnamep, seg, filemode, createmode, vpp, crwhy,
938da6c28aaSamw 	    umask, NULL, -1));
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate /*
9437c478bd9Sstevel@tonic-gate  * Open/create a vnode.
9447c478bd9Sstevel@tonic-gate  * This may be callable by the kernel, the only known use
9457c478bd9Sstevel@tonic-gate  * of user context being that the current user credentials
9467c478bd9Sstevel@tonic-gate  * are used for permissions.  crwhy is defined iff filemode & FCREAT.
9477c478bd9Sstevel@tonic-gate  */
9487c478bd9Sstevel@tonic-gate int
vn_openat(char * pnamep,enum uio_seg seg,int filemode,int createmode,struct vnode ** vpp,enum create crwhy,mode_t umask,struct vnode * startvp,int fd)9497c478bd9Sstevel@tonic-gate vn_openat(
9507c478bd9Sstevel@tonic-gate 	char *pnamep,
9517c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
9527c478bd9Sstevel@tonic-gate 	int filemode,
9537c478bd9Sstevel@tonic-gate 	int createmode,
9547c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
9557c478bd9Sstevel@tonic-gate 	enum create crwhy,
9567c478bd9Sstevel@tonic-gate 	mode_t umask,
957da6c28aaSamw 	struct vnode *startvp,
958da6c28aaSamw 	int fd)
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate 	struct vnode *vp;
9617c478bd9Sstevel@tonic-gate 	int mode;
962da6c28aaSamw 	int accessflags;
9637c478bd9Sstevel@tonic-gate 	int error;
9647c478bd9Sstevel@tonic-gate 	int in_crit = 0;
965da6c28aaSamw 	int open_done = 0;
966da6c28aaSamw 	int shrlock_done = 0;
9677c478bd9Sstevel@tonic-gate 	struct vattr vattr;
9687c478bd9Sstevel@tonic-gate 	enum symfollow follow;
969dd29fa4aSprabahar 	int estale_retry = 0;
970da6c28aaSamw 	struct shrlock shr;
971da6c28aaSamw 	struct shr_locowner shr_own;
97269c811abSRobert Mustacchi 	boolean_t create;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	mode = 0;
975da6c28aaSamw 	accessflags = 0;
9767c478bd9Sstevel@tonic-gate 	if (filemode & FREAD)
9777c478bd9Sstevel@tonic-gate 		mode |= VREAD;
9787c478bd9Sstevel@tonic-gate 	if (filemode & (FWRITE|FTRUNC))
9797c478bd9Sstevel@tonic-gate 		mode |= VWRITE;
980794f0adbSRoger A. Faulkner 	if (filemode & (FSEARCH|FEXEC|FXATTRDIROPEN))
981da6c28aaSamw 		mode |= VEXEC;
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	/* symlink interpretation */
9847c478bd9Sstevel@tonic-gate 	if (filemode & FNOFOLLOW)
9857c478bd9Sstevel@tonic-gate 		follow = NO_FOLLOW;
9867c478bd9Sstevel@tonic-gate 	else
9877c478bd9Sstevel@tonic-gate 		follow = FOLLOW;
9887c478bd9Sstevel@tonic-gate 
989da6c28aaSamw 	if (filemode & FAPPEND)
990da6c28aaSamw 		accessflags |= V_APPEND;
991da6c28aaSamw 
99269c811abSRobert Mustacchi 	/*
99369c811abSRobert Mustacchi 	 * We need to handle the case of FCREAT | FDIRECTORY and the case of
99469c811abSRobert Mustacchi 	 * FEXCL. If all three are specified, then we always fail because we
99569c811abSRobert Mustacchi 	 * cannot create a directory through this interface and FEXCL says we
99669c811abSRobert Mustacchi 	 * need to fail the request if we can't create it. If, however, only
99769c811abSRobert Mustacchi 	 * FCREAT | FDIRECTORY are specified, then we can treat this as the case
99869c811abSRobert Mustacchi 	 * of opening a file that already exists. If it exists, we can do
99969c811abSRobert Mustacchi 	 * something and if not, we fail. Effectively FCREAT | FDIRECTORY is
100069c811abSRobert Mustacchi 	 * treated as FDIRECTORY.
100169c811abSRobert Mustacchi 	 */
100269c811abSRobert Mustacchi 	if ((filemode & (FCREAT | FDIRECTORY | FEXCL)) ==
100369c811abSRobert Mustacchi 	    (FCREAT | FDIRECTORY | FEXCL)) {
100469c811abSRobert Mustacchi 		return (EINVAL);
100569c811abSRobert Mustacchi 	}
100669c811abSRobert Mustacchi 
100769c811abSRobert Mustacchi 	if ((filemode & (FCREAT | FDIRECTORY)) == (FCREAT | FDIRECTORY)) {
100869c811abSRobert Mustacchi 		create = B_FALSE;
100969c811abSRobert Mustacchi 	} else if ((filemode & FCREAT) != 0) {
101069c811abSRobert Mustacchi 		create = B_TRUE;
101169c811abSRobert Mustacchi 	} else {
101269c811abSRobert Mustacchi 		create = B_FALSE;
101369c811abSRobert Mustacchi 	}
101469c811abSRobert Mustacchi 
10157c478bd9Sstevel@tonic-gate top:
101669c811abSRobert Mustacchi 	if (create) {
10177c478bd9Sstevel@tonic-gate 		enum vcexcl excl;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 		/*
10207c478bd9Sstevel@tonic-gate 		 * Wish to create a file.
10217c478bd9Sstevel@tonic-gate 		 */
10227c478bd9Sstevel@tonic-gate 		vattr.va_type = VREG;
10237c478bd9Sstevel@tonic-gate 		vattr.va_mode = createmode;
10247c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_TYPE|AT_MODE;
10257c478bd9Sstevel@tonic-gate 		if (filemode & FTRUNC) {
10267c478bd9Sstevel@tonic-gate 			vattr.va_size = 0;
10277c478bd9Sstevel@tonic-gate 			vattr.va_mask |= AT_SIZE;
10287c478bd9Sstevel@tonic-gate 		}
10297c478bd9Sstevel@tonic-gate 		if (filemode & FEXCL)
10307c478bd9Sstevel@tonic-gate 			excl = EXCL;
10317c478bd9Sstevel@tonic-gate 		else
10327c478bd9Sstevel@tonic-gate 			excl = NONEXCL;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 		if (error =
10357c478bd9Sstevel@tonic-gate 		    vn_createat(pnamep, seg, &vattr, excl, mode, &vp, crwhy,
10361b300de9Sjwahlig 		    (filemode & ~(FTRUNC|FEXCL)), umask, startvp))
10377c478bd9Sstevel@tonic-gate 			return (error);
10387c478bd9Sstevel@tonic-gate 	} else {
10397c478bd9Sstevel@tonic-gate 		/*
10407c478bd9Sstevel@tonic-gate 		 * Wish to open a file.  Just look it up.
10417c478bd9Sstevel@tonic-gate 		 */
10427c478bd9Sstevel@tonic-gate 		if (error = lookupnameat(pnamep, seg, follow,
10437c478bd9Sstevel@tonic-gate 		    NULLVPP, &vp, startvp)) {
1044dd29fa4aSprabahar 			if ((error == ESTALE) &&
1045dd29fa4aSprabahar 			    fs_need_estale_retry(estale_retry++))
10467c478bd9Sstevel@tonic-gate 				goto top;
10477c478bd9Sstevel@tonic-gate 			return (error);
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 		/*
10517c478bd9Sstevel@tonic-gate 		 * Get the attributes to check whether file is large.
10527c478bd9Sstevel@tonic-gate 		 * We do this only if the FOFFMAX flag is not set and
10537c478bd9Sstevel@tonic-gate 		 * only for regular files.
10547c478bd9Sstevel@tonic-gate 		 */
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 		if (!(filemode & FOFFMAX) && (vp->v_type == VREG)) {
10577c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
1058da6c28aaSamw 			if ((error = VOP_GETATTR(vp, &vattr, 0,
1059da6c28aaSamw 			    CRED(), NULL))) {
10607c478bd9Sstevel@tonic-gate 				goto out;
10617c478bd9Sstevel@tonic-gate 			}
10627c478bd9Sstevel@tonic-gate 			if (vattr.va_size > (u_offset_t)MAXOFF32_T) {
10637c478bd9Sstevel@tonic-gate 				/*
10647c478bd9Sstevel@tonic-gate 				 * Large File API - regular open fails
10657c478bd9Sstevel@tonic-gate 				 * if FOFFMAX flag is set in file mode
10667c478bd9Sstevel@tonic-gate 				 */
10677c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
10687c478bd9Sstevel@tonic-gate 				goto out;
10697c478bd9Sstevel@tonic-gate 			}
10707c478bd9Sstevel@tonic-gate 		}
10717c478bd9Sstevel@tonic-gate 		/*
10727c478bd9Sstevel@tonic-gate 		 * Can't write directories, active texts, or
10737c478bd9Sstevel@tonic-gate 		 * read-only filesystems.  Can't truncate files
10747c478bd9Sstevel@tonic-gate 		 * on which mandatory locking is in effect.
10757c478bd9Sstevel@tonic-gate 		 */
10767c478bd9Sstevel@tonic-gate 		if (filemode & (FWRITE|FTRUNC)) {
10777c478bd9Sstevel@tonic-gate 			/*
10787c478bd9Sstevel@tonic-gate 			 * Allow writable directory if VDIROPEN flag is set.
10797c478bd9Sstevel@tonic-gate 			 */
10807c478bd9Sstevel@tonic-gate 			if (vp->v_type == VDIR && !(vp->v_flag & VDIROPEN)) {
10817c478bd9Sstevel@tonic-gate 				error = EISDIR;
10827c478bd9Sstevel@tonic-gate 				goto out;
10837c478bd9Sstevel@tonic-gate 			}
10847c478bd9Sstevel@tonic-gate 			if (ISROFILE(vp)) {
10857c478bd9Sstevel@tonic-gate 				error = EROFS;
10867c478bd9Sstevel@tonic-gate 				goto out;
10877c478bd9Sstevel@tonic-gate 			}
10887c478bd9Sstevel@tonic-gate 			/*
1089da6c28aaSamw 			 * Can't truncate files on which
1090da6c28aaSamw 			 * sysv mandatory locking is in effect.
10917c478bd9Sstevel@tonic-gate 			 */
10927c478bd9Sstevel@tonic-gate 			if (filemode & FTRUNC) {
10937c478bd9Sstevel@tonic-gate 				vnode_t *rvp;
10947c478bd9Sstevel@tonic-gate 
1095da6c28aaSamw 				if (VOP_REALVP(vp, &rvp, NULL) != 0)
10967c478bd9Sstevel@tonic-gate 					rvp = vp;
1097da6c28aaSamw 				if (rvp->v_filocks != NULL) {
10987c478bd9Sstevel@tonic-gate 					vattr.va_mask = AT_MODE;
1099da6c28aaSamw 					if ((error = VOP_GETATTR(vp,
1100da6c28aaSamw 					    &vattr, 0, CRED(), NULL)) == 0 &&
1101da6c28aaSamw 					    MANDLOCK(vp, vattr.va_mode))
11027c478bd9Sstevel@tonic-gate 						error = EAGAIN;
11037c478bd9Sstevel@tonic-gate 				}
11047c478bd9Sstevel@tonic-gate 			}
11057c478bd9Sstevel@tonic-gate 			if (error)
11067c478bd9Sstevel@tonic-gate 				goto out;
11077c478bd9Sstevel@tonic-gate 		}
11087c478bd9Sstevel@tonic-gate 		/*
11097c478bd9Sstevel@tonic-gate 		 * Check permissions.
11107c478bd9Sstevel@tonic-gate 		 */
1111da6c28aaSamw 		if (error = VOP_ACCESS(vp, mode, accessflags, CRED(), NULL))
11127c478bd9Sstevel@tonic-gate 			goto out;
111369c811abSRobert Mustacchi 
1114794f0adbSRoger A. Faulkner 		/*
111569c811abSRobert Mustacchi 		 * Require FSEARCH and FDIRECTORY to return a directory. Require
111669c811abSRobert Mustacchi 		 * FEXEC to return a regular file.
1117794f0adbSRoger A. Faulkner 		 */
111869c811abSRobert Mustacchi 		if ((filemode & (FSEARCH|FDIRECTORY)) != 0 &&
111969c811abSRobert Mustacchi 		    vp->v_type != VDIR) {
1120794f0adbSRoger A. Faulkner 			error = ENOTDIR;
1121794f0adbSRoger A. Faulkner 			goto out;
1122794f0adbSRoger A. Faulkner 		}
1123794f0adbSRoger A. Faulkner 		if ((filemode & FEXEC) && vp->v_type != VREG) {
1124794f0adbSRoger A. Faulkner 			error = ENOEXEC;	/* XXX: error code? */
1125794f0adbSRoger A. Faulkner 			goto out;
1126794f0adbSRoger A. Faulkner 		}
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/*
11307c478bd9Sstevel@tonic-gate 	 * Do remaining checks for FNOFOLLOW and FNOLINKS.
11317c478bd9Sstevel@tonic-gate 	 */
11327c478bd9Sstevel@tonic-gate 	if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) {
11339acbbeafSnn 		error = ELOOP;
11347c478bd9Sstevel@tonic-gate 		goto out;
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 	if (filemode & FNOLINKS) {
11377c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_NLINK;
1138da6c28aaSamw 		if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))) {
11397c478bd9Sstevel@tonic-gate 			goto out;
11407c478bd9Sstevel@tonic-gate 		}
11417c478bd9Sstevel@tonic-gate 		if (vattr.va_nlink != 1) {
11427c478bd9Sstevel@tonic-gate 			error = EMLINK;
11437c478bd9Sstevel@tonic-gate 			goto out;
11447c478bd9Sstevel@tonic-gate 		}
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	/*
11487c478bd9Sstevel@tonic-gate 	 * Opening a socket corresponding to the AF_UNIX pathname
11497c478bd9Sstevel@tonic-gate 	 * in the filesystem name space is not supported.
11507c478bd9Sstevel@tonic-gate 	 * However, VSOCK nodes in namefs are supported in order
11517c478bd9Sstevel@tonic-gate 	 * to make fattach work for sockets.
11527c478bd9Sstevel@tonic-gate 	 *
11537c478bd9Sstevel@tonic-gate 	 * XXX This uses VOP_REALVP to distinguish between
11547c478bd9Sstevel@tonic-gate 	 * an unopened namefs node (where VOP_REALVP returns a
11557c478bd9Sstevel@tonic-gate 	 * different VSOCK vnode) and a VSOCK created by vn_create
11567c478bd9Sstevel@tonic-gate 	 * in some file system (where VOP_REALVP would never return
11577c478bd9Sstevel@tonic-gate 	 * a different vnode).
11587c478bd9Sstevel@tonic-gate 	 */
11597c478bd9Sstevel@tonic-gate 	if (vp->v_type == VSOCK) {
11607c478bd9Sstevel@tonic-gate 		struct vnode *nvp;
11617c478bd9Sstevel@tonic-gate 
1162da6c28aaSamw 		error = VOP_REALVP(vp, &nvp, NULL);
11637c478bd9Sstevel@tonic-gate 		if (error != 0 || nvp == NULL || nvp == vp ||
11647c478bd9Sstevel@tonic-gate 		    nvp->v_type != VSOCK) {
11657c478bd9Sstevel@tonic-gate 			error = EOPNOTSUPP;
11667c478bd9Sstevel@tonic-gate 			goto out;
11677c478bd9Sstevel@tonic-gate 		}
11687c478bd9Sstevel@tonic-gate 	}
1169da6c28aaSamw 
1170da6c28aaSamw 	if ((vp->v_type == VREG) && nbl_need_check(vp)) {
1171da6c28aaSamw 		/* get share reservation */
1172da6c28aaSamw 		shr.s_access = 0;
1173da6c28aaSamw 		if (filemode & FWRITE)
1174da6c28aaSamw 			shr.s_access |= F_WRACC;
1175da6c28aaSamw 		if (filemode & FREAD)
1176da6c28aaSamw 			shr.s_access |= F_RDACC;
1177da6c28aaSamw 		shr.s_deny = 0;
1178da6c28aaSamw 		shr.s_sysid = 0;
1179da6c28aaSamw 		shr.s_pid = ttoproc(curthread)->p_pid;
1180da6c28aaSamw 		shr_own.sl_pid = shr.s_pid;
1181da6c28aaSamw 		shr_own.sl_id = fd;
1182da6c28aaSamw 		shr.s_own_len = sizeof (shr_own);
1183da6c28aaSamw 		shr.s_owner = (caddr_t)&shr_own;
1184da6c28aaSamw 		error = VOP_SHRLOCK(vp, F_SHARE_NBMAND, &shr, filemode, CRED(),
1185da6c28aaSamw 		    NULL);
1186da6c28aaSamw 		if (error)
1187da6c28aaSamw 			goto out;
1188da6c28aaSamw 		shrlock_done = 1;
1189da6c28aaSamw 
1190da6c28aaSamw 		/* nbmand conflict check if truncating file */
1191da6c28aaSamw 		if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
1192da6c28aaSamw 			nbl_start_crit(vp, RW_READER);
1193da6c28aaSamw 			in_crit = 1;
1194da6c28aaSamw 
1195da6c28aaSamw 			vattr.va_mask = AT_SIZE;
1196da6c28aaSamw 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
1197da6c28aaSamw 				goto out;
1198da6c28aaSamw 			if (nbl_conflict(vp, NBL_WRITE, 0, vattr.va_size, 0,
1199da6c28aaSamw 			    NULL)) {
1200da6c28aaSamw 				error = EACCES;
1201da6c28aaSamw 				goto out;
1202da6c28aaSamw 			}
1203da6c28aaSamw 		}
1204da6c28aaSamw 	}
1205da6c28aaSamw 
12067c478bd9Sstevel@tonic-gate 	/*
12077c478bd9Sstevel@tonic-gate 	 * Do opening protocol.
12087c478bd9Sstevel@tonic-gate 	 */
1209da6c28aaSamw 	error = VOP_OPEN(&vp, filemode, CRED(), NULL);
1210da6c28aaSamw 	if (error)
1211da6c28aaSamw 		goto out;
1212da6c28aaSamw 	open_done = 1;
1213da6c28aaSamw 
12147c478bd9Sstevel@tonic-gate 	/*
12157c478bd9Sstevel@tonic-gate 	 * Truncate if required.
12167c478bd9Sstevel@tonic-gate 	 */
1217da6c28aaSamw 	if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
12187c478bd9Sstevel@tonic-gate 		vattr.va_size = 0;
12197c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_SIZE;
12207c478bd9Sstevel@tonic-gate 		if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0)
1221da6c28aaSamw 			goto out;
12227c478bd9Sstevel@tonic-gate 	}
1223edd58064SJerry Jelinek 
1224edd58064SJerry Jelinek 	/*
1225edd58064SJerry Jelinek 	 * Turn on directio, if requested.
1226edd58064SJerry Jelinek 	 */
1227edd58064SJerry Jelinek 	if (filemode & FDIRECT) {
1228edd58064SJerry Jelinek 		if ((error = VOP_IOCTL(vp, _FIODIRECTIO, DIRECTIO_ON, 0,
1229edd58064SJerry Jelinek 		    CRED(), NULL, NULL)) != 0) {
1230edd58064SJerry Jelinek 			/*
1231edd58064SJerry Jelinek 			 * On Linux, O_DIRECT returns EINVAL when the file
1232edd58064SJerry Jelinek 			 * system does not support directio, so we'll do the
1233edd58064SJerry Jelinek 			 * same.
1234edd58064SJerry Jelinek 			 */
1235edd58064SJerry Jelinek 			error = EINVAL;
1236edd58064SJerry Jelinek 			goto out;
1237edd58064SJerry Jelinek 		}
1238edd58064SJerry Jelinek 	}
12397c478bd9Sstevel@tonic-gate out:
12407c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	if (in_crit) {
12437c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
12447c478bd9Sstevel@tonic-gate 		in_crit = 0;
12457c478bd9Sstevel@tonic-gate 	}
12467c478bd9Sstevel@tonic-gate 	if (error) {
1247da6c28aaSamw 		if (open_done) {
1248da6c28aaSamw 			(void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED(),
1249da6c28aaSamw 			    NULL);
1250da6c28aaSamw 			open_done = 0;
1251da6c28aaSamw 			shrlock_done = 0;
1252da6c28aaSamw 		}
1253da6c28aaSamw 		if (shrlock_done) {
1254da6c28aaSamw 			(void) VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, CRED(),
1255da6c28aaSamw 			    NULL);
1256da6c28aaSamw 			shrlock_done = 0;
1257da6c28aaSamw 		}
1258da6c28aaSamw 
12597c478bd9Sstevel@tonic-gate 		/*
12607c478bd9Sstevel@tonic-gate 		 * The following clause was added to handle a problem
12617c478bd9Sstevel@tonic-gate 		 * with NFS consistency.  It is possible that a lookup
12627c478bd9Sstevel@tonic-gate 		 * of the file to be opened succeeded, but the file
12637c478bd9Sstevel@tonic-gate 		 * itself doesn't actually exist on the server.  This
12647c478bd9Sstevel@tonic-gate 		 * is chiefly due to the DNLC containing an entry for
12657c478bd9Sstevel@tonic-gate 		 * the file which has been removed on the server.  In
12667c478bd9Sstevel@tonic-gate 		 * this case, we just start over.  If there was some
12677c478bd9Sstevel@tonic-gate 		 * other cause for the ESTALE error, then the lookup
12687c478bd9Sstevel@tonic-gate 		 * of the file will fail and the error will be returned
12697c478bd9Sstevel@tonic-gate 		 * above instead of looping around from here.
12707c478bd9Sstevel@tonic-gate 		 */
12717c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1272dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
12737c478bd9Sstevel@tonic-gate 			goto top;
12747c478bd9Sstevel@tonic-gate 	} else
12757c478bd9Sstevel@tonic-gate 		*vpp = vp;
12767c478bd9Sstevel@tonic-gate 	return (error);
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate 
1279da6c28aaSamw /*
1280da6c28aaSamw  * The following two accessor functions are for the NFSv4 server.  Since there
1281da6c28aaSamw  * is no VOP_OPEN_UP/DOWNGRADE we need a way for the NFS server to keep the
1282da6c28aaSamw  * vnode open counts correct when a client "upgrades" an open or does an
1283da6c28aaSamw  * open_downgrade.  In NFS, an upgrade or downgrade can not only change the
1284da6c28aaSamw  * open mode (add or subtract read or write), but also change the share/deny
1285da6c28aaSamw  * modes.  However, share reservations are not integrated with OPEN, yet, so
1286da6c28aaSamw  * we need to handle each separately.  These functions are cleaner than having
1287da6c28aaSamw  * the NFS server manipulate the counts directly, however, nobody else should
1288da6c28aaSamw  * use these functions.
1289da6c28aaSamw  */
1290da6c28aaSamw void
vn_open_upgrade(vnode_t * vp,int filemode)1291da6c28aaSamw vn_open_upgrade(
1292da6c28aaSamw 	vnode_t *vp,
1293da6c28aaSamw 	int filemode)
1294da6c28aaSamw {
1295da6c28aaSamw 	ASSERT(vp->v_type == VREG);
1296da6c28aaSamw 
1297da6c28aaSamw 	if (filemode & FREAD)
12981a5e258fSJosef 'Jeff' Sipek 		atomic_inc_32(&vp->v_rdcnt);
1299da6c28aaSamw 	if (filemode & FWRITE)
13001a5e258fSJosef 'Jeff' Sipek 		atomic_inc_32(&vp->v_wrcnt);
1301da6c28aaSamw 
1302da6c28aaSamw }
1303da6c28aaSamw 
1304da6c28aaSamw void
vn_open_downgrade(vnode_t * vp,int filemode)1305da6c28aaSamw vn_open_downgrade(
1306da6c28aaSamw 	vnode_t *vp,
1307da6c28aaSamw 	int filemode)
1308da6c28aaSamw {
1309da6c28aaSamw 	ASSERT(vp->v_type == VREG);
1310da6c28aaSamw 
1311da6c28aaSamw 	if (filemode & FREAD) {
1312da6c28aaSamw 		ASSERT(vp->v_rdcnt > 0);
13131a5e258fSJosef 'Jeff' Sipek 		atomic_dec_32(&vp->v_rdcnt);
1314da6c28aaSamw 	}
1315da6c28aaSamw 	if (filemode & FWRITE) {
1316da6c28aaSamw 		ASSERT(vp->v_wrcnt > 0);
13171a5e258fSJosef 'Jeff' Sipek 		atomic_dec_32(&vp->v_wrcnt);
1318da6c28aaSamw 	}
1319da6c28aaSamw 
1320da6c28aaSamw }
1321da6c28aaSamw 
13227c478bd9Sstevel@tonic-gate int
vn_create(char * pnamep,enum uio_seg seg,struct vattr * vap,enum vcexcl excl,int mode,struct vnode ** vpp,enum create why,int flag,mode_t umask)13237c478bd9Sstevel@tonic-gate vn_create(
13247c478bd9Sstevel@tonic-gate 	char *pnamep,
13257c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
13267c478bd9Sstevel@tonic-gate 	struct vattr *vap,
13277c478bd9Sstevel@tonic-gate 	enum vcexcl excl,
13287c478bd9Sstevel@tonic-gate 	int mode,
13297c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
13307c478bd9Sstevel@tonic-gate 	enum create why,
13317c478bd9Sstevel@tonic-gate 	int flag,
13327c478bd9Sstevel@tonic-gate 	mode_t umask)
13337c478bd9Sstevel@tonic-gate {
1334da6c28aaSamw 	return (vn_createat(pnamep, seg, vap, excl, mode, vpp, why, flag,
1335da6c28aaSamw 	    umask, NULL));
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate /*
13397c478bd9Sstevel@tonic-gate  * Create a vnode (makenode).
13407c478bd9Sstevel@tonic-gate  */
13417c478bd9Sstevel@tonic-gate int
vn_createat(char * pnamep,enum uio_seg seg,struct vattr * vap,enum vcexcl excl,int mode,struct vnode ** vpp,enum create why,int flag,mode_t umask,struct vnode * startvp)13427c478bd9Sstevel@tonic-gate vn_createat(
13437c478bd9Sstevel@tonic-gate 	char *pnamep,
13447c478bd9Sstevel@tonic-gate 	enum uio_seg seg,
13457c478bd9Sstevel@tonic-gate 	struct vattr *vap,
13467c478bd9Sstevel@tonic-gate 	enum vcexcl excl,
13477c478bd9Sstevel@tonic-gate 	int mode,
13487c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
13497c478bd9Sstevel@tonic-gate 	enum create why,
13507c478bd9Sstevel@tonic-gate 	int flag,
13517c478bd9Sstevel@tonic-gate 	mode_t umask,
13527c478bd9Sstevel@tonic-gate 	struct vnode *startvp)
13537c478bd9Sstevel@tonic-gate {
13547c478bd9Sstevel@tonic-gate 	struct vnode *dvp;	/* ptr to parent dir vnode */
13557c478bd9Sstevel@tonic-gate 	struct vnode *vp = NULL;
13567c478bd9Sstevel@tonic-gate 	struct pathname pn;
13577c478bd9Sstevel@tonic-gate 	int error;
13587c478bd9Sstevel@tonic-gate 	int in_crit = 0;
13597c478bd9Sstevel@tonic-gate 	struct vattr vattr;
13607c478bd9Sstevel@tonic-gate 	enum symfollow follow;
1361dd29fa4aSprabahar 	int estale_retry = 0;
1362005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	/* symlink interpretation */
13677c478bd9Sstevel@tonic-gate 	if ((flag & FNOFOLLOW) || excl == EXCL)
13687c478bd9Sstevel@tonic-gate 		follow = NO_FOLLOW;
13697c478bd9Sstevel@tonic-gate 	else
13707c478bd9Sstevel@tonic-gate 		follow = FOLLOW;
13717c478bd9Sstevel@tonic-gate 	flag &= ~(FNOFOLLOW|FNOLINKS);
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate top:
13747c478bd9Sstevel@tonic-gate 	/*
13757c478bd9Sstevel@tonic-gate 	 * Lookup directory.
13767c478bd9Sstevel@tonic-gate 	 * If new object is a file, call lower level to create it.
13777c478bd9Sstevel@tonic-gate 	 * Note that it is up to the lower level to enforce exclusive
13787c478bd9Sstevel@tonic-gate 	 * creation, if the file is already there.
13797c478bd9Sstevel@tonic-gate 	 * This allows the lower level to do whatever
13807c478bd9Sstevel@tonic-gate 	 * locking or protocol that is needed to prevent races.
13817c478bd9Sstevel@tonic-gate 	 * If the new object is directory call lower level to make
13827c478bd9Sstevel@tonic-gate 	 * the new directory, with "." and "..".
13837c478bd9Sstevel@tonic-gate 	 */
13847c478bd9Sstevel@tonic-gate 	if (error = pn_get(pnamep, seg, &pn))
13857c478bd9Sstevel@tonic-gate 		return (error);
1386005d3febSMarek Pospisil 	if (auditing)
13877c478bd9Sstevel@tonic-gate 		audit_vncreate_start();
13887c478bd9Sstevel@tonic-gate 	dvp = NULL;
13897c478bd9Sstevel@tonic-gate 	*vpp = NULL;
13907c478bd9Sstevel@tonic-gate 	/*
13917c478bd9Sstevel@tonic-gate 	 * lookup will find the parent directory for the vnode.
13927c478bd9Sstevel@tonic-gate 	 * When it is done the pn holds the name of the entry
13937c478bd9Sstevel@tonic-gate 	 * in the directory.
13947c478bd9Sstevel@tonic-gate 	 * If this is a non-exclusive create we also find the node itself.
13957c478bd9Sstevel@tonic-gate 	 */
13967c478bd9Sstevel@tonic-gate 	error = lookuppnat(&pn, NULL, follow, &dvp,
13977c478bd9Sstevel@tonic-gate 	    (excl == EXCL) ? NULLVPP : vpp, startvp);
13987c478bd9Sstevel@tonic-gate 	if (error) {
13997c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1400dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
14017c478bd9Sstevel@tonic-gate 			goto top;
14027c478bd9Sstevel@tonic-gate 		if (why == CRMKDIR && error == EINVAL)
14037c478bd9Sstevel@tonic-gate 			error = EEXIST;		/* SVID */
14047c478bd9Sstevel@tonic-gate 		return (error);
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	if (why != CRMKNOD)
14087c478bd9Sstevel@tonic-gate 		vap->va_mode &= ~VSVTX;
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	/*
14117c478bd9Sstevel@tonic-gate 	 * If default ACLs are defined for the directory don't apply the
14127c478bd9Sstevel@tonic-gate 	 * umask if umask is passed.
14137c478bd9Sstevel@tonic-gate 	 */
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	if (umask) {
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 		vsecattr_t vsec;
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 		vsec.vsa_aclcnt = 0;
14207c478bd9Sstevel@tonic-gate 		vsec.vsa_aclentp = NULL;
14217c478bd9Sstevel@tonic-gate 		vsec.vsa_dfaclcnt = 0;
14227c478bd9Sstevel@tonic-gate 		vsec.vsa_dfaclentp = NULL;
14237c478bd9Sstevel@tonic-gate 		vsec.vsa_mask = VSA_DFACLCNT;
1424da6c28aaSamw 		error = VOP_GETSECATTR(dvp, &vsec, 0, CRED(), NULL);
1425fa9e4066Sahrens 		/*
1426fa9e4066Sahrens 		 * If error is ENOSYS then treat it as no error
1427fa9e4066Sahrens 		 * Don't want to force all file systems to support
1428fa9e4066Sahrens 		 * aclent_t style of ACL's.
1429fa9e4066Sahrens 		 */
1430fa9e4066Sahrens 		if (error == ENOSYS)
1431fa9e4066Sahrens 			error = 0;
1432fa9e4066Sahrens 		if (error) {
14337c478bd9Sstevel@tonic-gate 			if (*vpp != NULL)
14347c478bd9Sstevel@tonic-gate 				VN_RELE(*vpp);
14357c478bd9Sstevel@tonic-gate 			goto out;
1436fa9e4066Sahrens 		} else {
1437fa9e4066Sahrens 			/*
1438fa9e4066Sahrens 			 * Apply the umask if no default ACLs.
1439fa9e4066Sahrens 			 */
1440fa9e4066Sahrens 			if (vsec.vsa_dfaclcnt == 0)
1441fa9e4066Sahrens 				vap->va_mode &= ~umask;
14427c478bd9Sstevel@tonic-gate 
1443fa9e4066Sahrens 			/*
1444fa9e4066Sahrens 			 * VOP_GETSECATTR() may have allocated memory for
1445fa9e4066Sahrens 			 * ACLs we didn't request, so double-check and
1446fa9e4066Sahrens 			 * free it if necessary.
1447fa9e4066Sahrens 			 */
1448fa9e4066Sahrens 			if (vsec.vsa_aclcnt && vsec.vsa_aclentp != NULL)
1449fa9e4066Sahrens 				kmem_free((caddr_t)vsec.vsa_aclentp,
1450fa9e4066Sahrens 				    vsec.vsa_aclcnt * sizeof (aclent_t));
1451fa9e4066Sahrens 			if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp != NULL)
1452fa9e4066Sahrens 				kmem_free((caddr_t)vsec.vsa_dfaclentp,
1453fa9e4066Sahrens 				    vsec.vsa_dfaclcnt * sizeof (aclent_t));
1454fa9e4066Sahrens 		}
14557c478bd9Sstevel@tonic-gate 	}
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	/*
14587c478bd9Sstevel@tonic-gate 	 * In general we want to generate EROFS if the file system is
14597c478bd9Sstevel@tonic-gate 	 * readonly.  However, POSIX (IEEE Std. 1003.1) section 5.3.1
14607c478bd9Sstevel@tonic-gate 	 * documents the open system call, and it says that O_CREAT has no
14617c478bd9Sstevel@tonic-gate 	 * effect if the file already exists.  Bug 1119649 states
14627c478bd9Sstevel@tonic-gate 	 * that open(path, O_CREAT, ...) fails when attempting to open an
14637c478bd9Sstevel@tonic-gate 	 * existing file on a read only file system.  Thus, the first part
14647c478bd9Sstevel@tonic-gate 	 * of the following if statement has 3 checks:
14657c478bd9Sstevel@tonic-gate 	 *	if the file exists &&
14667c478bd9Sstevel@tonic-gate 	 *		it is being open with write access &&
14677c478bd9Sstevel@tonic-gate 	 *		the file system is read only
14687c478bd9Sstevel@tonic-gate 	 *	then generate EROFS
14697c478bd9Sstevel@tonic-gate 	 */
14707c478bd9Sstevel@tonic-gate 	if ((*vpp != NULL && (mode & VWRITE) && ISROFILE(*vpp)) ||
14717c478bd9Sstevel@tonic-gate 	    (*vpp == NULL && dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
14727c478bd9Sstevel@tonic-gate 		if (*vpp)
14737c478bd9Sstevel@tonic-gate 			VN_RELE(*vpp);
14747c478bd9Sstevel@tonic-gate 		error = EROFS;
14757c478bd9Sstevel@tonic-gate 	} else if (excl == NONEXCL && *vpp != NULL) {
14767c478bd9Sstevel@tonic-gate 		vnode_t *rvp;
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 		/*
14797c478bd9Sstevel@tonic-gate 		 * File already exists.  If a mandatory lock has been
14807c478bd9Sstevel@tonic-gate 		 * applied, return error.
14817c478bd9Sstevel@tonic-gate 		 */
14827c478bd9Sstevel@tonic-gate 		vp = *vpp;
1483da6c28aaSamw 		if (VOP_REALVP(vp, &rvp, NULL) != 0)
14847c478bd9Sstevel@tonic-gate 			rvp = vp;
14857c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_SIZE) && nbl_need_check(vp)) {
14867c478bd9Sstevel@tonic-gate 			nbl_start_crit(vp, RW_READER);
14877c478bd9Sstevel@tonic-gate 			in_crit = 1;
14887c478bd9Sstevel@tonic-gate 		}
14897c478bd9Sstevel@tonic-gate 		if (rvp->v_filocks != NULL || rvp->v_shrlocks != NULL) {
14907c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_MODE|AT_SIZE;
1491da6c28aaSamw 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL)) {
14927c478bd9Sstevel@tonic-gate 				goto out;
14937c478bd9Sstevel@tonic-gate 			}
14947c478bd9Sstevel@tonic-gate 			if (MANDLOCK(vp, vattr.va_mode)) {
14957c478bd9Sstevel@tonic-gate 				error = EAGAIN;
14967c478bd9Sstevel@tonic-gate 				goto out;
14977c478bd9Sstevel@tonic-gate 			}
14987c478bd9Sstevel@tonic-gate 			/*
14997c478bd9Sstevel@tonic-gate 			 * File cannot be truncated if non-blocking mandatory
15007c478bd9Sstevel@tonic-gate 			 * locks are currently on the file.
15017c478bd9Sstevel@tonic-gate 			 */
15027c478bd9Sstevel@tonic-gate 			if ((vap->va_mask & AT_SIZE) && in_crit) {
15037c478bd9Sstevel@tonic-gate 				u_offset_t offset;
15047c478bd9Sstevel@tonic-gate 				ssize_t length;
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 				offset = vap->va_size > vattr.va_size ?
1507b9c98e29Spf 				    vattr.va_size : vap->va_size;
15087c478bd9Sstevel@tonic-gate 				length = vap->va_size > vattr.va_size ?
1509b9c98e29Spf 				    vap->va_size - vattr.va_size :
1510b9c98e29Spf 				    vattr.va_size - vap->va_size;
15117c478bd9Sstevel@tonic-gate 				if (nbl_conflict(vp, NBL_WRITE, offset,
1512da6c28aaSamw 				    length, 0, NULL)) {
15137c478bd9Sstevel@tonic-gate 					error = EACCES;
15147c478bd9Sstevel@tonic-gate 					goto out;
15157c478bd9Sstevel@tonic-gate 				}
15167c478bd9Sstevel@tonic-gate 			}
15177c478bd9Sstevel@tonic-gate 		}
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 		/*
15207c478bd9Sstevel@tonic-gate 		 * If the file is the root of a VFS, we've crossed a
15217c478bd9Sstevel@tonic-gate 		 * mount point and the "containing" directory that we
15227c478bd9Sstevel@tonic-gate 		 * acquired above (dvp) is irrelevant because it's in
15237c478bd9Sstevel@tonic-gate 		 * a different file system.  We apply VOP_CREATE to the
15247c478bd9Sstevel@tonic-gate 		 * target itself instead of to the containing directory
15257c478bd9Sstevel@tonic-gate 		 * and supply a null path name to indicate (conventionally)
15267c478bd9Sstevel@tonic-gate 		 * the node itself as the "component" of interest.
15277c478bd9Sstevel@tonic-gate 		 *
1528b3286c38SKevin Crowe 		 * The call to VOP_CREATE() is necessary to ensure
1529b3286c38SKevin Crowe 		 * that the appropriate permission checks are made,
1530b3286c38SKevin Crowe 		 * i.e. EISDIR, EACCES, etc.  We already know that vpp
1531b3286c38SKevin Crowe 		 * exists since we are in the else condition where this
1532b3286c38SKevin Crowe 		 * was checked.
15337c478bd9Sstevel@tonic-gate 		 */
15347c478bd9Sstevel@tonic-gate 		if (vp->v_flag & VROOT) {
15357c478bd9Sstevel@tonic-gate 			ASSERT(why != CRMKDIR);
1536da6c28aaSamw 			error = VOP_CREATE(vp, "", vap, excl, mode, vpp,
1537da6c28aaSamw 			    CRED(), flag, NULL, NULL);
15387c478bd9Sstevel@tonic-gate 			/*
1539b3286c38SKevin Crowe 			 * If the create succeeded, it will have created a
1540b3286c38SKevin Crowe 			 * new reference on a new vnode (*vpp) in the child
1541b3286c38SKevin Crowe 			 * file system, so we want to drop our reference on
1542b3286c38SKevin Crowe 			 * the old (vp) upon exit.
15437c478bd9Sstevel@tonic-gate 			 */
15447c478bd9Sstevel@tonic-gate 			goto out;
15457c478bd9Sstevel@tonic-gate 		}
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 		/*
15487c478bd9Sstevel@tonic-gate 		 * Large File API - non-large open (FOFFMAX flag not set)
15497c478bd9Sstevel@tonic-gate 		 * of regular file fails if the file size exceeds MAXOFF32_T.
15507c478bd9Sstevel@tonic-gate 		 */
15517c478bd9Sstevel@tonic-gate 		if (why != CRMKDIR &&
15527c478bd9Sstevel@tonic-gate 		    !(flag & FOFFMAX) &&
15537c478bd9Sstevel@tonic-gate 		    (vp->v_type == VREG)) {
15547c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
1555da6c28aaSamw 			if ((error = VOP_GETATTR(vp, &vattr, 0,
1556da6c28aaSamw 			    CRED(), NULL))) {
15577c478bd9Sstevel@tonic-gate 				goto out;
15587c478bd9Sstevel@tonic-gate 			}
15597c478bd9Sstevel@tonic-gate 			if ((vattr.va_size > (u_offset_t)MAXOFF32_T)) {
15607c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
15617c478bd9Sstevel@tonic-gate 				goto out;
15627c478bd9Sstevel@tonic-gate 			}
15637c478bd9Sstevel@tonic-gate 		}
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	if (error == 0) {
15677c478bd9Sstevel@tonic-gate 		/*
15687c478bd9Sstevel@tonic-gate 		 * Call mkdir() if specified, otherwise create().
15697c478bd9Sstevel@tonic-gate 		 */
15707c478bd9Sstevel@tonic-gate 		int must_be_dir = pn_fixslash(&pn);	/* trailing '/'? */
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 		if (why == CRMKDIR)
1573da6c28aaSamw 			/*
1574da6c28aaSamw 			 * N.B., if vn_createat() ever requests
1575da6c28aaSamw 			 * case-insensitive behavior then it will need
1576da6c28aaSamw 			 * to be passed to VOP_MKDIR().  VOP_CREATE()
1577da6c28aaSamw 			 * will already get it via "flag"
1578da6c28aaSamw 			 */
1579da6c28aaSamw 			error = VOP_MKDIR(dvp, pn.pn_path, vap, vpp, CRED(),
1580da6c28aaSamw 			    NULL, 0, NULL);
15817c478bd9Sstevel@tonic-gate 		else if (!must_be_dir)
15827c478bd9Sstevel@tonic-gate 			error = VOP_CREATE(dvp, pn.pn_path, vap,
1583da6c28aaSamw 			    excl, mode, vpp, CRED(), flag, NULL, NULL);
15847c478bd9Sstevel@tonic-gate 		else
15857c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
15867c478bd9Sstevel@tonic-gate 	}
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate out:
15897c478bd9Sstevel@tonic-gate 
1590005d3febSMarek Pospisil 	if (auditing)
15917c478bd9Sstevel@tonic-gate 		audit_vncreate_finish(*vpp, error);
15927c478bd9Sstevel@tonic-gate 	if (in_crit) {
15937c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
15947c478bd9Sstevel@tonic-gate 		in_crit = 0;
15957c478bd9Sstevel@tonic-gate 	}
15967c478bd9Sstevel@tonic-gate 	if (vp != NULL) {
15977c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
15987c478bd9Sstevel@tonic-gate 		vp = NULL;
15997c478bd9Sstevel@tonic-gate 	}
16007c478bd9Sstevel@tonic-gate 	pn_free(&pn);
16017c478bd9Sstevel@tonic-gate 	VN_RELE(dvp);
16027c478bd9Sstevel@tonic-gate 	/*
16037c478bd9Sstevel@tonic-gate 	 * The following clause was added to handle a problem
16047c478bd9Sstevel@tonic-gate 	 * with NFS consistency.  It is possible that a lookup
16057c478bd9Sstevel@tonic-gate 	 * of the file to be created succeeded, but the file
16067c478bd9Sstevel@tonic-gate 	 * itself doesn't actually exist on the server.  This
16077c478bd9Sstevel@tonic-gate 	 * is chiefly due to the DNLC containing an entry for
16087c478bd9Sstevel@tonic-gate 	 * the file which has been removed on the server.  In
16097c478bd9Sstevel@tonic-gate 	 * this case, we just start over.  If there was some
16107c478bd9Sstevel@tonic-gate 	 * other cause for the ESTALE error, then the lookup
16117c478bd9Sstevel@tonic-gate 	 * of the file will fail and the error will be returned
16127c478bd9Sstevel@tonic-gate 	 * above instead of looping around from here.
16137c478bd9Sstevel@tonic-gate 	 */
1614dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
16157c478bd9Sstevel@tonic-gate 		goto top;
16167c478bd9Sstevel@tonic-gate 	return (error);
16177c478bd9Sstevel@tonic-gate }
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate int
vn_link(char * from,char * to,enum uio_seg seg)16207c478bd9Sstevel@tonic-gate vn_link(char *from, char *to, enum uio_seg seg)
1621794f0adbSRoger A. Faulkner {
1622794f0adbSRoger A. Faulkner 	return (vn_linkat(NULL, from, NO_FOLLOW, NULL, to, seg));
1623794f0adbSRoger A. Faulkner }
1624794f0adbSRoger A. Faulkner 
1625794f0adbSRoger A. Faulkner int
vn_linkat(vnode_t * fstartvp,char * from,enum symfollow follow,vnode_t * tstartvp,char * to,enum uio_seg seg)1626794f0adbSRoger A. Faulkner vn_linkat(vnode_t *fstartvp, char *from, enum symfollow follow,
1627794f0adbSRoger A. Faulkner     vnode_t *tstartvp, char *to, enum uio_seg seg)
16287c478bd9Sstevel@tonic-gate {
16297c478bd9Sstevel@tonic-gate 	struct vnode *fvp;		/* from vnode ptr */
16307c478bd9Sstevel@tonic-gate 	struct vnode *tdvp;		/* to directory vnode ptr */
16317c478bd9Sstevel@tonic-gate 	struct pathname pn;
16327c478bd9Sstevel@tonic-gate 	int error;
16337c478bd9Sstevel@tonic-gate 	struct vattr vattr;
16347c478bd9Sstevel@tonic-gate 	dev_t fsid;
1635dd29fa4aSprabahar 	int estale_retry = 0;
1636794f0adbSRoger A. Faulkner 	uint32_t auditing = AU_AUDITING();
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate top:
16397c478bd9Sstevel@tonic-gate 	fvp = tdvp = NULL;
16407c478bd9Sstevel@tonic-gate 	if (error = pn_get(to, seg, &pn))
16417c478bd9Sstevel@tonic-gate 		return (error);
1642794f0adbSRoger A. Faulkner 	if (auditing && fstartvp != NULL)
1643794f0adbSRoger A. Faulkner 		audit_setfsat_path(1);
1644794f0adbSRoger A. Faulkner 	if (error = lookupnameat(from, seg, follow, NULLVPP, &fvp, fstartvp))
16457c478bd9Sstevel@tonic-gate 		goto out;
1646794f0adbSRoger A. Faulkner 	if (auditing && tstartvp != NULL)
1647794f0adbSRoger A. Faulkner 		audit_setfsat_path(3);
1648794f0adbSRoger A. Faulkner 	if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &tdvp, NULLVPP, tstartvp))
16497c478bd9Sstevel@tonic-gate 		goto out;
16507c478bd9Sstevel@tonic-gate 	/*
16517c478bd9Sstevel@tonic-gate 	 * Make sure both source vnode and target directory vnode are
16527c478bd9Sstevel@tonic-gate 	 * in the same vfs and that it is writeable.
16537c478bd9Sstevel@tonic-gate 	 */
16547c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_FSID;
1655da6c28aaSamw 	if (error = VOP_GETATTR(fvp, &vattr, 0, CRED(), NULL))
16567c478bd9Sstevel@tonic-gate 		goto out;
16577c478bd9Sstevel@tonic-gate 	fsid = vattr.va_fsid;
16587c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_FSID;
1659da6c28aaSamw 	if (error = VOP_GETATTR(tdvp, &vattr, 0, CRED(), NULL))
16607c478bd9Sstevel@tonic-gate 		goto out;
16617c478bd9Sstevel@tonic-gate 	if (fsid != vattr.va_fsid) {
16627c478bd9Sstevel@tonic-gate 		error = EXDEV;
16637c478bd9Sstevel@tonic-gate 		goto out;
16647c478bd9Sstevel@tonic-gate 	}
16657c478bd9Sstevel@tonic-gate 	if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) {
16667c478bd9Sstevel@tonic-gate 		error = EROFS;
16677c478bd9Sstevel@tonic-gate 		goto out;
16687c478bd9Sstevel@tonic-gate 	}
16697c478bd9Sstevel@tonic-gate 	/*
16707c478bd9Sstevel@tonic-gate 	 * Do the link.
16717c478bd9Sstevel@tonic-gate 	 */
16727c478bd9Sstevel@tonic-gate 	(void) pn_fixslash(&pn);
1673da6c28aaSamw 	error = VOP_LINK(tdvp, fvp, pn.pn_path, CRED(), NULL, 0);
16747c478bd9Sstevel@tonic-gate out:
16757c478bd9Sstevel@tonic-gate 	pn_free(&pn);
16767c478bd9Sstevel@tonic-gate 	if (fvp)
16777c478bd9Sstevel@tonic-gate 		VN_RELE(fvp);
16787c478bd9Sstevel@tonic-gate 	if (tdvp)
16797c478bd9Sstevel@tonic-gate 		VN_RELE(tdvp);
1680dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
16817c478bd9Sstevel@tonic-gate 		goto top;
16827c478bd9Sstevel@tonic-gate 	return (error);
16837c478bd9Sstevel@tonic-gate }
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate int
vn_rename(char * from,char * to,enum uio_seg seg)16867c478bd9Sstevel@tonic-gate vn_rename(char *from, char *to, enum uio_seg seg)
16877c478bd9Sstevel@tonic-gate {
16887c478bd9Sstevel@tonic-gate 	return (vn_renameat(NULL, from, NULL, to, seg));
16897c478bd9Sstevel@tonic-gate }
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate int
vn_renameat(vnode_t * fdvp,char * fname,vnode_t * tdvp,char * tname,enum uio_seg seg)16927c478bd9Sstevel@tonic-gate vn_renameat(vnode_t *fdvp, char *fname, vnode_t *tdvp,
1693b3286c38SKevin Crowe     char *tname, enum uio_seg seg)
16947c478bd9Sstevel@tonic-gate {
16957c478bd9Sstevel@tonic-gate 	int error;
16967c478bd9Sstevel@tonic-gate 	struct vattr vattr;
16977c478bd9Sstevel@tonic-gate 	struct pathname fpn;		/* from pathname */
16987c478bd9Sstevel@tonic-gate 	struct pathname tpn;		/* to pathname */
16997c478bd9Sstevel@tonic-gate 	dev_t fsid;
1700da6c28aaSamw 	int in_crit_src, in_crit_targ;
17017c478bd9Sstevel@tonic-gate 	vnode_t *fromvp, *fvp;
1702da6c28aaSamw 	vnode_t *tovp, *targvp;
1703dd29fa4aSprabahar 	int estale_retry = 0;
1704005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate top:
1707da6c28aaSamw 	fvp = fromvp = tovp = targvp = NULL;
1708da6c28aaSamw 	in_crit_src = in_crit_targ = 0;
17097c478bd9Sstevel@tonic-gate 	/*
17107c478bd9Sstevel@tonic-gate 	 * Get to and from pathnames.
17117c478bd9Sstevel@tonic-gate 	 */
17127c478bd9Sstevel@tonic-gate 	if (error = pn_get(fname, seg, &fpn))
17137c478bd9Sstevel@tonic-gate 		return (error);
17147c478bd9Sstevel@tonic-gate 	if (error = pn_get(tname, seg, &tpn)) {
17157c478bd9Sstevel@tonic-gate 		pn_free(&fpn);
17167c478bd9Sstevel@tonic-gate 		return (error);
17177c478bd9Sstevel@tonic-gate 	}
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	/*
17207c478bd9Sstevel@tonic-gate 	 * First we need to resolve the correct directories
17217c478bd9Sstevel@tonic-gate 	 * The passed in directories may only be a starting point,
17227c478bd9Sstevel@tonic-gate 	 * but we need the real directories the file(s) live in.
17237c478bd9Sstevel@tonic-gate 	 * For example the fname may be something like usr/lib/sparc
17247c478bd9Sstevel@tonic-gate 	 * and we were passed in the / directory, but we need to
17257c478bd9Sstevel@tonic-gate 	 * use the lib directory for the rename.
17267c478bd9Sstevel@tonic-gate 	 */
17277c478bd9Sstevel@tonic-gate 
1728794f0adbSRoger A. Faulkner 	if (auditing && fdvp != NULL)
17297c478bd9Sstevel@tonic-gate 		audit_setfsat_path(1);
17307c478bd9Sstevel@tonic-gate 	/*
17317c478bd9Sstevel@tonic-gate 	 * Lookup to and from directories.
17327c478bd9Sstevel@tonic-gate 	 */
17337c478bd9Sstevel@tonic-gate 	if (error = lookuppnat(&fpn, NULL, NO_FOLLOW, &fromvp, &fvp, fdvp)) {
17347c478bd9Sstevel@tonic-gate 		goto out;
17357c478bd9Sstevel@tonic-gate 	}
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	/*
17387c478bd9Sstevel@tonic-gate 	 * Make sure there is an entry.
17397c478bd9Sstevel@tonic-gate 	 */
17407c478bd9Sstevel@tonic-gate 	if (fvp == NULL) {
17417c478bd9Sstevel@tonic-gate 		error = ENOENT;
17427c478bd9Sstevel@tonic-gate 		goto out;
17437c478bd9Sstevel@tonic-gate 	}
17447c478bd9Sstevel@tonic-gate 
1745794f0adbSRoger A. Faulkner 	if (auditing && tdvp != NULL)
17467c478bd9Sstevel@tonic-gate 		audit_setfsat_path(3);
1747da6c28aaSamw 	if (error = lookuppnat(&tpn, NULL, NO_FOLLOW, &tovp, &targvp, tdvp)) {
17487c478bd9Sstevel@tonic-gate 		goto out;
17497c478bd9Sstevel@tonic-gate 	}
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	/*
17527c478bd9Sstevel@tonic-gate 	 * Make sure both the from vnode directory and the to directory
17537c478bd9Sstevel@tonic-gate 	 * are in the same vfs and the to directory is writable.
17547c478bd9Sstevel@tonic-gate 	 * We check fsid's, not vfs pointers, so loopback fs works.
17557c478bd9Sstevel@tonic-gate 	 */
17567c478bd9Sstevel@tonic-gate 	if (fromvp != tovp) {
17577c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_FSID;
1758da6c28aaSamw 		if (error = VOP_GETATTR(fromvp, &vattr, 0, CRED(), NULL))
17597c478bd9Sstevel@tonic-gate 			goto out;
17607c478bd9Sstevel@tonic-gate 		fsid = vattr.va_fsid;
17617c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_FSID;
1762da6c28aaSamw 		if (error = VOP_GETATTR(tovp, &vattr, 0, CRED(), NULL))
17637c478bd9Sstevel@tonic-gate 			goto out;
17647c478bd9Sstevel@tonic-gate 		if (fsid != vattr.va_fsid) {
17657c478bd9Sstevel@tonic-gate 			error = EXDEV;
17667c478bd9Sstevel@tonic-gate 			goto out;
17677c478bd9Sstevel@tonic-gate 		}
17687c478bd9Sstevel@tonic-gate 	}
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	if (tovp->v_vfsp->vfs_flag & VFS_RDONLY) {
17717c478bd9Sstevel@tonic-gate 		error = EROFS;
17727c478bd9Sstevel@tonic-gate 		goto out;
17737c478bd9Sstevel@tonic-gate 	}
17747c478bd9Sstevel@tonic-gate 
1775cd00b393SGordon Ross 	/*
1776cd00b393SGordon Ross 	 * Make sure "from" vp is not a mount point.
1777cd00b393SGordon Ross 	 * Note, lookup did traverse() already, so
1778cd00b393SGordon Ross 	 * we'll be looking at the mounted FS root.
1779cd00b393SGordon Ross 	 * (but allow files like mnttab)
1780cd00b393SGordon Ross 	 */
1781cd00b393SGordon Ross 	if ((fvp->v_flag & VROOT) != 0 && fvp->v_type == VDIR) {
1782cd00b393SGordon Ross 		error = EBUSY;
1783cd00b393SGordon Ross 		goto out;
1784cd00b393SGordon Ross 	}
1785cd00b393SGordon Ross 
1786da6c28aaSamw 	if (targvp && (fvp != targvp)) {
1787da6c28aaSamw 		nbl_start_crit(targvp, RW_READER);
1788da6c28aaSamw 		in_crit_targ = 1;
1789da6c28aaSamw 		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
1790da6c28aaSamw 			error = EACCES;
1791da6c28aaSamw 			goto out;
1792da6c28aaSamw 		}
1793da6c28aaSamw 	}
1794da6c28aaSamw 
17957c478bd9Sstevel@tonic-gate 	if (nbl_need_check(fvp)) {
17967c478bd9Sstevel@tonic-gate 		nbl_start_crit(fvp, RW_READER);
1797da6c28aaSamw 		in_crit_src = 1;
1798da6c28aaSamw 		if (nbl_conflict(fvp, NBL_RENAME, 0, 0, 0, NULL)) {
17997c478bd9Sstevel@tonic-gate 			error = EACCES;
18007c478bd9Sstevel@tonic-gate 			goto out;
18017c478bd9Sstevel@tonic-gate 		}
18027c478bd9Sstevel@tonic-gate 	}
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	/*
18057c478bd9Sstevel@tonic-gate 	 * Do the rename.
18067c478bd9Sstevel@tonic-gate 	 */
18077c478bd9Sstevel@tonic-gate 	(void) pn_fixslash(&tpn);
1808da6c28aaSamw 	error = VOP_RENAME(fromvp, fpn.pn_path, tovp, tpn.pn_path, CRED(),
1809da6c28aaSamw 	    NULL, 0);
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate out:
18127c478bd9Sstevel@tonic-gate 	pn_free(&fpn);
18137c478bd9Sstevel@tonic-gate 	pn_free(&tpn);
1814da6c28aaSamw 	if (in_crit_src)
18157c478bd9Sstevel@tonic-gate 		nbl_end_crit(fvp);
1816da6c28aaSamw 	if (in_crit_targ)
1817da6c28aaSamw 		nbl_end_crit(targvp);
18187c478bd9Sstevel@tonic-gate 	if (fromvp)
18197c478bd9Sstevel@tonic-gate 		VN_RELE(fromvp);
18207c478bd9Sstevel@tonic-gate 	if (tovp)
18217c478bd9Sstevel@tonic-gate 		VN_RELE(tovp);
1822da6c28aaSamw 	if (targvp)
1823da6c28aaSamw 		VN_RELE(targvp);
18247c478bd9Sstevel@tonic-gate 	if (fvp)
18257c478bd9Sstevel@tonic-gate 		VN_RELE(fvp);
1826dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
18277c478bd9Sstevel@tonic-gate 		goto top;
18287c478bd9Sstevel@tonic-gate 	return (error);
18297c478bd9Sstevel@tonic-gate }
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate /*
18327c478bd9Sstevel@tonic-gate  * Remove a file or directory.
18337c478bd9Sstevel@tonic-gate  */
18347c478bd9Sstevel@tonic-gate int
vn_remove(char * fnamep,enum uio_seg seg,enum rm dirflag)18357c478bd9Sstevel@tonic-gate vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
18367c478bd9Sstevel@tonic-gate {
18377c478bd9Sstevel@tonic-gate 	return (vn_removeat(NULL, fnamep, seg, dirflag));
18387c478bd9Sstevel@tonic-gate }
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate int
vn_removeat(vnode_t * startvp,char * fnamep,enum uio_seg seg,enum rm dirflag)18417c478bd9Sstevel@tonic-gate vn_removeat(vnode_t *startvp, char *fnamep, enum uio_seg seg, enum rm dirflag)
18427c478bd9Sstevel@tonic-gate {
18437c478bd9Sstevel@tonic-gate 	struct vnode *vp;		/* entry vnode */
18447c478bd9Sstevel@tonic-gate 	struct vnode *dvp;		/* ptr to parent dir vnode */
18457c478bd9Sstevel@tonic-gate 	struct vnode *coveredvp;
18467c478bd9Sstevel@tonic-gate 	struct pathname pn;		/* name of entry */
18477c478bd9Sstevel@tonic-gate 	enum vtype vtype;
18487c478bd9Sstevel@tonic-gate 	int error;
18497c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
18507c478bd9Sstevel@tonic-gate 	struct vfs *dvfsp;	/* ptr to parent dir vfs */
18517c478bd9Sstevel@tonic-gate 	int in_crit = 0;
1852dd29fa4aSprabahar 	int estale_retry = 0;
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate top:
18557c478bd9Sstevel@tonic-gate 	if (error = pn_get(fnamep, seg, &pn))
18567c478bd9Sstevel@tonic-gate 		return (error);
18577c478bd9Sstevel@tonic-gate 	dvp = vp = NULL;
18587c478bd9Sstevel@tonic-gate 	if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &dvp, &vp, startvp)) {
18597c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1860dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
18617c478bd9Sstevel@tonic-gate 			goto top;
18627c478bd9Sstevel@tonic-gate 		return (error);
18637c478bd9Sstevel@tonic-gate 	}
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	/*
18667c478bd9Sstevel@tonic-gate 	 * Make sure there is an entry.
18677c478bd9Sstevel@tonic-gate 	 */
18687c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
18697c478bd9Sstevel@tonic-gate 		error = ENOENT;
18707c478bd9Sstevel@tonic-gate 		goto out;
18717c478bd9Sstevel@tonic-gate 	}
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	vfsp = vp->v_vfsp;
18747c478bd9Sstevel@tonic-gate 	dvfsp = dvp->v_vfsp;
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	/*
18777c478bd9Sstevel@tonic-gate 	 * If the named file is the root of a mounted filesystem, fail,
18787c478bd9Sstevel@tonic-gate 	 * unless it's marked unlinkable.  In that case, unmount the
18797c478bd9Sstevel@tonic-gate 	 * filesystem and proceed to unlink the covered vnode.  (If the
18807c478bd9Sstevel@tonic-gate 	 * covered vnode is a directory, use rmdir instead of unlink,
18817c478bd9Sstevel@tonic-gate 	 * to avoid file system corruption.)
18827c478bd9Sstevel@tonic-gate 	 */
18837c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VROOT) {
1884b9c98e29Spf 		if ((vfsp->vfs_flag & VFS_UNLINKABLE) == 0) {
1885b9c98e29Spf 			error = EBUSY;
1886b9c98e29Spf 			goto out;
1887b9c98e29Spf 		}
1888b9c98e29Spf 
1889b9c98e29Spf 		/*
1890b9c98e29Spf 		 * Namefs specific code starts here.
1891b9c98e29Spf 		 */
1892b9c98e29Spf 
1893b9c98e29Spf 		if (dirflag == RMDIRECTORY) {
18947c478bd9Sstevel@tonic-gate 			/*
1895b9c98e29Spf 			 * User called rmdir(2) on a file that has
1896b9c98e29Spf 			 * been namefs mounted on top of.  Since
1897b9c98e29Spf 			 * namefs doesn't allow directories to
1898b9c98e29Spf 			 * be mounted on other files we know
1899b9c98e29Spf 			 * vp is not of type VDIR so fail to operation.
19007c478bd9Sstevel@tonic-gate 			 */
1901b9c98e29Spf 			error = ENOTDIR;
1902b9c98e29Spf 			goto out;
1903b9c98e29Spf 		}
1904b9c98e29Spf 
1905b9c98e29Spf 		/*
1906b9c98e29Spf 		 * If VROOT is still set after grabbing vp->v_lock,
1907b9c98e29Spf 		 * noone has finished nm_unmount so far and coveredvp
1908b9c98e29Spf 		 * is valid.
1909b9c98e29Spf 		 * If we manage to grab vn_vfswlock(coveredvp) before releasing
1910b9c98e29Spf 		 * vp->v_lock, any race window is eliminated.
1911b9c98e29Spf 		 */
1912b9c98e29Spf 
1913b9c98e29Spf 		mutex_enter(&vp->v_lock);
1914b9c98e29Spf 		if ((vp->v_flag & VROOT) == 0) {
1915b9c98e29Spf 			/* Someone beat us to the unmount */
1916b9c98e29Spf 			mutex_exit(&vp->v_lock);
19177c478bd9Sstevel@tonic-gate 			error = EBUSY;
1918b9c98e29Spf 			goto out;
1919b9c98e29Spf 		}
1920b9c98e29Spf 		vfsp = vp->v_vfsp;
1921b9c98e29Spf 		coveredvp = vfsp->vfs_vnodecovered;
1922b9c98e29Spf 		ASSERT(coveredvp);
1923b9c98e29Spf 		/*
1924b9c98e29Spf 		 * Note: Implementation of vn_vfswlock shows that ordering of
1925b9c98e29Spf 		 * v_lock / vn_vfswlock is not an issue here.
1926b9c98e29Spf 		 */
1927b9c98e29Spf 		error = vn_vfswlock(coveredvp);
1928b9c98e29Spf 		mutex_exit(&vp->v_lock);
1929b9c98e29Spf 
1930b9c98e29Spf 		if (error)
1931b9c98e29Spf 			goto out;
1932b9c98e29Spf 
1933b9c98e29Spf 		VN_HOLD(coveredvp);
1934b9c98e29Spf 		VN_RELE(vp);
1935b9c98e29Spf 		error = dounmount(vfsp, 0, CRED());
1936b9c98e29Spf 
1937b9c98e29Spf 		/*
1938b9c98e29Spf 		 * Unmounted the namefs file system; now get
1939b9c98e29Spf 		 * the object it was mounted over.
1940b9c98e29Spf 		 */
1941b9c98e29Spf 		vp = coveredvp;
1942b9c98e29Spf 		/*
1943b9c98e29Spf 		 * If namefs was mounted over a directory, then
1944b9c98e29Spf 		 * we want to use rmdir() instead of unlink().
1945b9c98e29Spf 		 */
1946b9c98e29Spf 		if (vp->v_type == VDIR)
1947b9c98e29Spf 			dirflag = RMDIRECTORY;
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 		if (error)
19507c478bd9Sstevel@tonic-gate 			goto out;
19517c478bd9Sstevel@tonic-gate 	}
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	/*
19547c478bd9Sstevel@tonic-gate 	 * Make sure filesystem is writeable.
19557c478bd9Sstevel@tonic-gate 	 * We check the parent directory's vfs in case this is an lofs vnode.
19567c478bd9Sstevel@tonic-gate 	 */
19577c478bd9Sstevel@tonic-gate 	if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) {
19587c478bd9Sstevel@tonic-gate 		error = EROFS;
19597c478bd9Sstevel@tonic-gate 		goto out;
19607c478bd9Sstevel@tonic-gate 	}
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	vtype = vp->v_type;
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	/*
19657c478bd9Sstevel@tonic-gate 	 * If there is the possibility of an nbmand share reservation, make
19667c478bd9Sstevel@tonic-gate 	 * sure it's okay to remove the file.  Keep a reference to the
19677c478bd9Sstevel@tonic-gate 	 * vnode, so that we can exit the nbl critical region after
19687c478bd9Sstevel@tonic-gate 	 * calling VOP_REMOVE.
19697c478bd9Sstevel@tonic-gate 	 * If there is no possibility of an nbmand share reservation,
19707c478bd9Sstevel@tonic-gate 	 * release the vnode reference now.  Filesystems like NFS may
19717c478bd9Sstevel@tonic-gate 	 * behave differently if there is an extra reference, so get rid of
19727c478bd9Sstevel@tonic-gate 	 * this one.  Fortunately, we can't have nbmand mounts on NFS
19737c478bd9Sstevel@tonic-gate 	 * filesystems.
19747c478bd9Sstevel@tonic-gate 	 */
19757c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
19767c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
19777c478bd9Sstevel@tonic-gate 		in_crit = 1;
1978da6c28aaSamw 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
19797c478bd9Sstevel@tonic-gate 			error = EACCES;
19807c478bd9Sstevel@tonic-gate 			goto out;
19817c478bd9Sstevel@tonic-gate 		}
19827c478bd9Sstevel@tonic-gate 	} else {
19837c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
19847c478bd9Sstevel@tonic-gate 		vp = NULL;
19857c478bd9Sstevel@tonic-gate 	}
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	if (dirflag == RMDIRECTORY) {
19887c478bd9Sstevel@tonic-gate 		/*
19897c478bd9Sstevel@tonic-gate 		 * Caller is using rmdir(2), which can only be applied to
19907c478bd9Sstevel@tonic-gate 		 * directories.
19917c478bd9Sstevel@tonic-gate 		 */
19927c478bd9Sstevel@tonic-gate 		if (vtype != VDIR) {
19937c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
19947c478bd9Sstevel@tonic-gate 		} else {
19957c478bd9Sstevel@tonic-gate 			vnode_t *cwd;
19967c478bd9Sstevel@tonic-gate 			proc_t *pp = curproc;
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
19997c478bd9Sstevel@tonic-gate 			cwd = PTOU(pp)->u_cdir;
20007c478bd9Sstevel@tonic-gate 			VN_HOLD(cwd);
20017c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
2002da6c28aaSamw 			error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED(),
2003da6c28aaSamw 			    NULL, 0);
20047c478bd9Sstevel@tonic-gate 			VN_RELE(cwd);
20057c478bd9Sstevel@tonic-gate 		}
20067c478bd9Sstevel@tonic-gate 	} else {
20077c478bd9Sstevel@tonic-gate 		/*
20087c478bd9Sstevel@tonic-gate 		 * Unlink(2) can be applied to anything.
20097c478bd9Sstevel@tonic-gate 		 */
2010da6c28aaSamw 		error = VOP_REMOVE(dvp, pn.pn_path, CRED(), NULL, 0);
20117c478bd9Sstevel@tonic-gate 	}
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate out:
20147c478bd9Sstevel@tonic-gate 	pn_free(&pn);
20157c478bd9Sstevel@tonic-gate 	if (in_crit) {
20167c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
20177c478bd9Sstevel@tonic-gate 		in_crit = 0;
20187c478bd9Sstevel@tonic-gate 	}
20197c478bd9Sstevel@tonic-gate 	if (vp != NULL)
20207c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
20217c478bd9Sstevel@tonic-gate 	if (dvp != NULL)
20227c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
2023dd29fa4aSprabahar 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
20247c478bd9Sstevel@tonic-gate 		goto top;
20257c478bd9Sstevel@tonic-gate 	return (error);
20267c478bd9Sstevel@tonic-gate }
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate /*
20297c478bd9Sstevel@tonic-gate  * Utility function to compare equality of vnodes.
20307c478bd9Sstevel@tonic-gate  * Compare the underlying real vnodes, if there are underlying vnodes.
20317c478bd9Sstevel@tonic-gate  * This is a more thorough comparison than the VN_CMP() macro provides.
20327c478bd9Sstevel@tonic-gate  */
20337c478bd9Sstevel@tonic-gate int
vn_compare(vnode_t * vp1,vnode_t * vp2)20347c478bd9Sstevel@tonic-gate vn_compare(vnode_t *vp1, vnode_t *vp2)
20357c478bd9Sstevel@tonic-gate {
20367c478bd9Sstevel@tonic-gate 	vnode_t *realvp;
20377c478bd9Sstevel@tonic-gate 
2038da6c28aaSamw 	if (vp1 != NULL && VOP_REALVP(vp1, &realvp, NULL) == 0)
20397c478bd9Sstevel@tonic-gate 		vp1 = realvp;
2040da6c28aaSamw 	if (vp2 != NULL && VOP_REALVP(vp2, &realvp, NULL) == 0)
20417c478bd9Sstevel@tonic-gate 		vp2 = realvp;
20427c478bd9Sstevel@tonic-gate 	return (VN_CMP(vp1, vp2));
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate /*
20467c478bd9Sstevel@tonic-gate  * The number of locks to hash into.  This value must be a power
20477c478bd9Sstevel@tonic-gate  * of 2 minus 1 and should probably also be prime.
20487c478bd9Sstevel@tonic-gate  */
20497c478bd9Sstevel@tonic-gate #define	NUM_BUCKETS	1023
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate struct  vn_vfslocks_bucket {
20527c478bd9Sstevel@tonic-gate 	kmutex_t vb_lock;
20537c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vb_list;
20547c478bd9Sstevel@tonic-gate 	char pad[64 - sizeof (kmutex_t) - sizeof (void *)];
20557c478bd9Sstevel@tonic-gate };
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate /*
20587c478bd9Sstevel@tonic-gate  * Total number of buckets will be NUM_BUCKETS + 1 .
20597c478bd9Sstevel@tonic-gate  */
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate #pragma	align	64(vn_vfslocks_buckets)
20627c478bd9Sstevel@tonic-gate static	struct vn_vfslocks_bucket	vn_vfslocks_buckets[NUM_BUCKETS + 1];
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate #define	VN_VFSLOCKS_SHIFT	9
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate #define	VN_VFSLOCKS_HASH(vfsvpptr)	\
20677c478bd9Sstevel@tonic-gate 	((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS)
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate /*
20707c478bd9Sstevel@tonic-gate  * vn_vfslocks_getlock() uses an HASH scheme to generate
20717c478bd9Sstevel@tonic-gate  * rwstlock using vfs/vnode pointer passed to it.
20727c478bd9Sstevel@tonic-gate  *
20737c478bd9Sstevel@tonic-gate  * vn_vfslocks_rele() releases a reference in the
20747c478bd9Sstevel@tonic-gate  * HASH table which allows the entry allocated by
20757c478bd9Sstevel@tonic-gate  * vn_vfslocks_getlock() to be freed at a later
20767c478bd9Sstevel@tonic-gate  * stage when the refcount drops to zero.
20777c478bd9Sstevel@tonic-gate  */
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate vn_vfslocks_entry_t *
vn_vfslocks_getlock(void * vfsvpptr)20807c478bd9Sstevel@tonic-gate vn_vfslocks_getlock(void *vfsvpptr)
20817c478bd9Sstevel@tonic-gate {
20827c478bd9Sstevel@tonic-gate 	struct vn_vfslocks_bucket *bp;
20837c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vep;
20847c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *tvep;
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	ASSERT(vfsvpptr != NULL);
20877c478bd9Sstevel@tonic-gate 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)];
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
20907c478bd9Sstevel@tonic-gate 	for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
20917c478bd9Sstevel@tonic-gate 		if (vep->ve_vpvfs == vfsvpptr) {
20927c478bd9Sstevel@tonic-gate 			vep->ve_refcnt++;
20937c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->vb_lock);
20947c478bd9Sstevel@tonic-gate 			return (vep);
20957c478bd9Sstevel@tonic-gate 		}
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
20987c478bd9Sstevel@tonic-gate 	vep = kmem_alloc(sizeof (*vep), KM_SLEEP);
20997c478bd9Sstevel@tonic-gate 	rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL);
21007c478bd9Sstevel@tonic-gate 	vep->ve_vpvfs = (char *)vfsvpptr;
21017c478bd9Sstevel@tonic-gate 	vep->ve_refcnt = 1;
21027c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
21037c478bd9Sstevel@tonic-gate 	for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) {
21047c478bd9Sstevel@tonic-gate 		if (tvep->ve_vpvfs == vfsvpptr) {
21057c478bd9Sstevel@tonic-gate 			tvep->ve_refcnt++;
21067c478bd9Sstevel@tonic-gate 			mutex_exit(&bp->vb_lock);
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 			/*
21097c478bd9Sstevel@tonic-gate 			 * There is already an entry in the hash
21107c478bd9Sstevel@tonic-gate 			 * destroy what we just allocated.
21117c478bd9Sstevel@tonic-gate 			 */
21127c478bd9Sstevel@tonic-gate 			rwst_destroy(&vep->ve_lock);
21137c478bd9Sstevel@tonic-gate 			kmem_free(vep, sizeof (*vep));
21147c478bd9Sstevel@tonic-gate 			return (tvep);
21157c478bd9Sstevel@tonic-gate 		}
21167c478bd9Sstevel@tonic-gate 	}
21177c478bd9Sstevel@tonic-gate 	vep->ve_next = bp->vb_list;
21187c478bd9Sstevel@tonic-gate 	bp->vb_list = vep;
21197c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
21207c478bd9Sstevel@tonic-gate 	return (vep);
21217c478bd9Sstevel@tonic-gate }
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate void
vn_vfslocks_rele(vn_vfslocks_entry_t * vepent)21247c478bd9Sstevel@tonic-gate vn_vfslocks_rele(vn_vfslocks_entry_t *vepent)
21257c478bd9Sstevel@tonic-gate {
21267c478bd9Sstevel@tonic-gate 	struct vn_vfslocks_bucket *bp;
21277c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vep;
21287c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *pvep;
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	ASSERT(vepent != NULL);
21317c478bd9Sstevel@tonic-gate 	ASSERT(vepent->ve_vpvfs != NULL);
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)];
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	mutex_enter(&bp->vb_lock);
21367c478bd9Sstevel@tonic-gate 	vepent->ve_refcnt--;
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	if ((int32_t)vepent->ve_refcnt < 0)
21397c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative");
21407c478bd9Sstevel@tonic-gate 
2141c6f039c7SToomas Soome 	pvep = NULL;
21427c478bd9Sstevel@tonic-gate 	if (vepent->ve_refcnt == 0) {
21437c478bd9Sstevel@tonic-gate 		for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
21447c478bd9Sstevel@tonic-gate 			if (vep->ve_vpvfs == vepent->ve_vpvfs) {
2145c6f039c7SToomas Soome 				if (pvep == NULL)
21467c478bd9Sstevel@tonic-gate 					bp->vb_list = vep->ve_next;
21477c478bd9Sstevel@tonic-gate 				else {
21487c478bd9Sstevel@tonic-gate 					pvep->ve_next = vep->ve_next;
21497c478bd9Sstevel@tonic-gate 				}
21507c478bd9Sstevel@tonic-gate 				mutex_exit(&bp->vb_lock);
21517c478bd9Sstevel@tonic-gate 				rwst_destroy(&vep->ve_lock);
21527c478bd9Sstevel@tonic-gate 				kmem_free(vep, sizeof (*vep));
21537c478bd9Sstevel@tonic-gate 				return;
21547c478bd9Sstevel@tonic-gate 			}
21557c478bd9Sstevel@tonic-gate 			pvep = vep;
21567c478bd9Sstevel@tonic-gate 		}
21577c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found");
21587c478bd9Sstevel@tonic-gate 	}
21597c478bd9Sstevel@tonic-gate 	mutex_exit(&bp->vb_lock);
21607c478bd9Sstevel@tonic-gate }
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate /*
21637c478bd9Sstevel@tonic-gate  * vn_vfswlock_wait is used to implement a lock which is logically a writers
21647c478bd9Sstevel@tonic-gate  * lock protecting the v_vfsmountedhere field.
21657c478bd9Sstevel@tonic-gate  * vn_vfswlock_wait has been modified to be similar to vn_vfswlock,
21667c478bd9Sstevel@tonic-gate  * except that it blocks to acquire the lock VVFSLOCK.
21677c478bd9Sstevel@tonic-gate  *
21687c478bd9Sstevel@tonic-gate  * traverse() and routines re-implementing part of traverse (e.g. autofs)
21697c478bd9Sstevel@tonic-gate  * need to hold this lock. mount(), vn_rename(), vn_remove() and so on
21707c478bd9Sstevel@tonic-gate  * need the non-blocking version of the writers lock i.e. vn_vfswlock
21717c478bd9Sstevel@tonic-gate  */
21727c478bd9Sstevel@tonic-gate int
vn_vfswlock_wait(vnode_t * vp)21737c478bd9Sstevel@tonic-gate vn_vfswlock_wait(vnode_t *vp)
21747c478bd9Sstevel@tonic-gate {
21757c478bd9Sstevel@tonic-gate 	int retval;
21767c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
21777c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
21807c478bd9Sstevel@tonic-gate 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER);
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 	if (retval == EINTR) {
21837c478bd9Sstevel@tonic-gate 		vn_vfslocks_rele(vpvfsentry);
21847c478bd9Sstevel@tonic-gate 		return (EINTR);
21857c478bd9Sstevel@tonic-gate 	}
21867c478bd9Sstevel@tonic-gate 	return (retval);
21877c478bd9Sstevel@tonic-gate }
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate int
vn_vfsrlock_wait(vnode_t * vp)21907c478bd9Sstevel@tonic-gate vn_vfsrlock_wait(vnode_t *vp)
21917c478bd9Sstevel@tonic-gate {
21927c478bd9Sstevel@tonic-gate 	int retval;
21937c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
21947c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
21977c478bd9Sstevel@tonic-gate 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER);
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	if (retval == EINTR) {
22007c478bd9Sstevel@tonic-gate 		vn_vfslocks_rele(vpvfsentry);
22017c478bd9Sstevel@tonic-gate 		return (EINTR);
22027c478bd9Sstevel@tonic-gate 	}
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 	return (retval);
22057c478bd9Sstevel@tonic-gate }
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate /*
22097c478bd9Sstevel@tonic-gate  * vn_vfswlock is used to implement a lock which is logically a writers lock
22107c478bd9Sstevel@tonic-gate  * protecting the v_vfsmountedhere field.
22117c478bd9Sstevel@tonic-gate  */
22127c478bd9Sstevel@tonic-gate int
vn_vfswlock(vnode_t * vp)22137c478bd9Sstevel@tonic-gate vn_vfswlock(vnode_t *vp)
22147c478bd9Sstevel@tonic-gate {
22157c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	/*
22187c478bd9Sstevel@tonic-gate 	 * If vp is NULL then somebody is trying to lock the covered vnode
22197c478bd9Sstevel@tonic-gate 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
22207c478bd9Sstevel@tonic-gate 	 * only happen when unmounting /.  Since that operation will fail
22217c478bd9Sstevel@tonic-gate 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
22227c478bd9Sstevel@tonic-gate 	 */
22237c478bd9Sstevel@tonic-gate 	if (vp == NULL)
22247c478bd9Sstevel@tonic-gate 		return (EBUSY);
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
22297c478bd9Sstevel@tonic-gate 		return (0);
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
22327c478bd9Sstevel@tonic-gate 	return (EBUSY);
22337c478bd9Sstevel@tonic-gate }
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate int
vn_vfsrlock(vnode_t * vp)22367c478bd9Sstevel@tonic-gate vn_vfsrlock(vnode_t *vp)
22377c478bd9Sstevel@tonic-gate {
22387c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 	/*
22417c478bd9Sstevel@tonic-gate 	 * If vp is NULL then somebody is trying to lock the covered vnode
22427c478bd9Sstevel@tonic-gate 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
22437c478bd9Sstevel@tonic-gate 	 * only happen when unmounting /.  Since that operation will fail
22447c478bd9Sstevel@tonic-gate 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
22457c478bd9Sstevel@tonic-gate 	 */
22467c478bd9Sstevel@tonic-gate 	if (vp == NULL)
22477c478bd9Sstevel@tonic-gate 		return (EBUSY);
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
22527c478bd9Sstevel@tonic-gate 		return (0);
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
22557c478bd9Sstevel@tonic-gate 	return (EBUSY);
22567c478bd9Sstevel@tonic-gate }
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate void
vn_vfsunlock(vnode_t * vp)22597c478bd9Sstevel@tonic-gate vn_vfsunlock(vnode_t *vp)
22607c478bd9Sstevel@tonic-gate {
22617c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 	/*
22647c478bd9Sstevel@tonic-gate 	 * ve_refcnt needs to be decremented twice.
22657c478bd9Sstevel@tonic-gate 	 * 1. To release refernce after a call to vn_vfslocks_getlock()
22667c478bd9Sstevel@tonic-gate 	 * 2. To release the reference from the locking routines like
22677c478bd9Sstevel@tonic-gate 	 *    vn_vfsrlock/vn_vfswlock etc,.
22687c478bd9Sstevel@tonic-gate 	 */
22697c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
22707c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	rwst_exit(&vpvfsentry->ve_lock);
22737c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
22747c478bd9Sstevel@tonic-gate }
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate int
vn_vfswlock_held(vnode_t * vp)22777c478bd9Sstevel@tonic-gate vn_vfswlock_held(vnode_t *vp)
22787c478bd9Sstevel@tonic-gate {
22797c478bd9Sstevel@tonic-gate 	int held;
22807c478bd9Sstevel@tonic-gate 	vn_vfslocks_entry_t *vpvfsentry;
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 	vpvfsentry = vn_vfslocks_getlock(vp);
22857c478bd9Sstevel@tonic-gate 	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 	vn_vfslocks_rele(vpvfsentry);
22887c478bd9Sstevel@tonic-gate 	return (held);
22897c478bd9Sstevel@tonic-gate }
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate int
vn_make_ops(const char * name,const fs_operation_def_t * templ,vnodeops_t ** actual)22937c478bd9Sstevel@tonic-gate vn_make_ops(
22947c478bd9Sstevel@tonic-gate 	const char *name,			/* Name of file system */
22957c478bd9Sstevel@tonic-gate 	const fs_operation_def_t *templ,	/* Operation specification */
22967c478bd9Sstevel@tonic-gate 	vnodeops_t **actual)			/* Return the vnodeops */
22977c478bd9Sstevel@tonic-gate {
22987c478bd9Sstevel@tonic-gate 	int unused_ops;
22997c478bd9Sstevel@tonic-gate 	int error;
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	*actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP);
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	(*actual)->vnop_name = name;
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ);
23067c478bd9Sstevel@tonic-gate 	if (error) {
23077c478bd9Sstevel@tonic-gate 		kmem_free(*actual, sizeof (vnodeops_t));
23087c478bd9Sstevel@tonic-gate 	}
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate #if DEBUG
23117c478bd9Sstevel@tonic-gate 	if (unused_ops != 0)
23127c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied "
23137c478bd9Sstevel@tonic-gate 		    "but not used", name, unused_ops);
23147c478bd9Sstevel@tonic-gate #endif
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 	return (error);
23177c478bd9Sstevel@tonic-gate }
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate /*
23207c478bd9Sstevel@tonic-gate  * Free the vnodeops created as a result of vn_make_ops()
23217c478bd9Sstevel@tonic-gate  */
23227c478bd9Sstevel@tonic-gate void
vn_freevnodeops(vnodeops_t * vnops)23237c478bd9Sstevel@tonic-gate vn_freevnodeops(vnodeops_t *vnops)
23247c478bd9Sstevel@tonic-gate {
23257c478bd9Sstevel@tonic-gate 	kmem_free(vnops, sizeof (vnodeops_t));
23267c478bd9Sstevel@tonic-gate }
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate /*
23297c478bd9Sstevel@tonic-gate  * Vnode cache.
23307c478bd9Sstevel@tonic-gate  */
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate /* ARGSUSED */
23337c478bd9Sstevel@tonic-gate static int
vn_cache_constructor(void * buf,void * cdrarg,int kmflags)23347c478bd9Sstevel@tonic-gate vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
23357c478bd9Sstevel@tonic-gate {
23367c478bd9Sstevel@tonic-gate 	struct vnode *vp;
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 	vp = buf;
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 	mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
2341d216dff5SRobert Mastors 	mutex_init(&vp->v_vsd_lock, NULL, MUTEX_DEFAULT, NULL);
23427c478bd9Sstevel@tonic-gate 	cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL);
23437c478bd9Sstevel@tonic-gate 	rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL);
23447c478bd9Sstevel@tonic-gate 	vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2345e2fc3408SPatrick Mooney 	vp->v_path = vn_vpath_empty;
2346e2fc3408SPatrick Mooney 	vp->v_path_stamp = 0;
23477c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
23481b300de9Sjwahlig 	vp->v_vsd = NULL;
2349df2381bfSpraks 	vp->v_fopdata = NULL;
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 	return (0);
23527c478bd9Sstevel@tonic-gate }
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate /* ARGSUSED */
23557c478bd9Sstevel@tonic-gate static void
vn_cache_destructor(void * buf,void * cdrarg)23567c478bd9Sstevel@tonic-gate vn_cache_destructor(void *buf, void *cdrarg)
23577c478bd9Sstevel@tonic-gate {
23587c478bd9Sstevel@tonic-gate 	struct vnode *vp;
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate 	vp = buf;
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	rw_destroy(&vp->v_nbllock);
23637c478bd9Sstevel@tonic-gate 	cv_destroy(&vp->v_cv);
2364d216dff5SRobert Mastors 	mutex_destroy(&vp->v_vsd_lock);
23657c478bd9Sstevel@tonic-gate 	mutex_destroy(&vp->v_lock);
23667c478bd9Sstevel@tonic-gate }
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate void
vn_create_cache(void)23697c478bd9Sstevel@tonic-gate vn_create_cache(void)
23707c478bd9Sstevel@tonic-gate {
2371cb15d5d9SPeter Rival 	/* LINTED */
2372cb15d5d9SPeter Rival 	ASSERT((1 << VNODE_ALIGN_LOG2) ==
2373cb15d5d9SPeter Rival 	    P2ROUNDUP(sizeof (struct vnode), VNODE_ALIGN));
2374cb15d5d9SPeter Rival 	vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode),
2375cb15d5d9SPeter Rival 	    VNODE_ALIGN, vn_cache_constructor, vn_cache_destructor, NULL, NULL,
23767c478bd9Sstevel@tonic-gate 	    NULL, 0);
23777c478bd9Sstevel@tonic-gate }
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate void
vn_destroy_cache(void)23807c478bd9Sstevel@tonic-gate vn_destroy_cache(void)
23817c478bd9Sstevel@tonic-gate {
23827c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(vn_cache);
23837c478bd9Sstevel@tonic-gate }
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate /*
23867c478bd9Sstevel@tonic-gate  * Used by file systems when fs-specific nodes (e.g., ufs inodes) are
23877c478bd9Sstevel@tonic-gate  * cached by the file system and vnodes remain associated.
23887c478bd9Sstevel@tonic-gate  */
23897c478bd9Sstevel@tonic-gate void
vn_recycle(vnode_t * vp)23907c478bd9Sstevel@tonic-gate vn_recycle(vnode_t *vp)
23917c478bd9Sstevel@tonic-gate {
23927c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_pages == NULL);
2393e2fc3408SPatrick Mooney 	VERIFY(vp->v_path != NULL);
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	/*
23967c478bd9Sstevel@tonic-gate 	 * XXX - This really belongs in vn_reinit(), but we have some issues
23977c478bd9Sstevel@tonic-gate 	 * with the counts.  Best to have it here for clean initialization.
23987c478bd9Sstevel@tonic-gate 	 */
23997c478bd9Sstevel@tonic-gate 	vp->v_rdcnt = 0;
24007c478bd9Sstevel@tonic-gate 	vp->v_wrcnt = 0;
24017c478bd9Sstevel@tonic-gate 	vp->v_mmap_read = 0;
24027c478bd9Sstevel@tonic-gate 	vp->v_mmap_write = 0;
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	/*
24057c478bd9Sstevel@tonic-gate 	 * If FEM was in use, make sure everything gets cleaned up
24067c478bd9Sstevel@tonic-gate 	 * NOTE: vp->v_femhead is initialized to NULL in the vnode
24077c478bd9Sstevel@tonic-gate 	 * constructor.
24087c478bd9Sstevel@tonic-gate 	 */
24097c478bd9Sstevel@tonic-gate 	if (vp->v_femhead) {
24107c478bd9Sstevel@tonic-gate 		/* XXX - There should be a free_femhead() that does all this */
24117c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_femhead->femh_list == NULL);
24127c478bd9Sstevel@tonic-gate 		mutex_destroy(&vp->v_femhead->femh_lock);
24137c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
24147c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;
24157c478bd9Sstevel@tonic-gate 	}
2416e2fc3408SPatrick Mooney 	if (vp->v_path != vn_vpath_empty) {
24177c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2418e2fc3408SPatrick Mooney 		vp->v_path = vn_vpath_empty;
24197c478bd9Sstevel@tonic-gate 	}
2420e2fc3408SPatrick Mooney 	vp->v_path_stamp = 0;
2421df2381bfSpraks 
2422df2381bfSpraks 	if (vp->v_fopdata != NULL) {
2423df2381bfSpraks 		free_fopdata(vp);
2424df2381bfSpraks 	}
24257c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
24261b300de9Sjwahlig 	vsd_free(vp);
24277c478bd9Sstevel@tonic-gate }
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate /*
24307c478bd9Sstevel@tonic-gate  * Used to reset the vnode fields including those that are directly accessible
24317c478bd9Sstevel@tonic-gate  * as well as those which require an accessor function.
24327c478bd9Sstevel@tonic-gate  *
24337c478bd9Sstevel@tonic-gate  * Does not initialize:
2434d216dff5SRobert Mastors  *	synchronization objects: v_lock, v_vsd_lock, v_nbllock, v_cv
24357c478bd9Sstevel@tonic-gate  *	v_data (since FS-nodes and vnodes point to each other and should
24367c478bd9Sstevel@tonic-gate  *		be updated simultaneously)
24377c478bd9Sstevel@tonic-gate  *	v_op (in case someone needs to make a VOP call on this object)
24387c478bd9Sstevel@tonic-gate  */
24397c478bd9Sstevel@tonic-gate void
vn_reinit(vnode_t * vp)24407c478bd9Sstevel@tonic-gate vn_reinit(vnode_t *vp)
24417c478bd9Sstevel@tonic-gate {
24427c478bd9Sstevel@tonic-gate 	vp->v_count = 1;
2443b5fca8f8Stomee 	vp->v_count_dnlc = 0;
24447c478bd9Sstevel@tonic-gate 	vp->v_vfsp = NULL;
24457c478bd9Sstevel@tonic-gate 	vp->v_stream = NULL;
24467c478bd9Sstevel@tonic-gate 	vp->v_vfsmountedhere = NULL;
24477c478bd9Sstevel@tonic-gate 	vp->v_flag = 0;
24487c478bd9Sstevel@tonic-gate 	vp->v_type = VNON;
24497c478bd9Sstevel@tonic-gate 	vp->v_rdev = NODEV;
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 	vp->v_filocks = NULL;
24527c478bd9Sstevel@tonic-gate 	vp->v_shrlocks = NULL;
24537c478bd9Sstevel@tonic-gate 	vp->v_pages = NULL;
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 	vp->v_locality = NULL;
2456da6c28aaSamw 	vp->v_xattrdir = NULL;
24577c478bd9Sstevel@tonic-gate 
2458e2fc3408SPatrick Mooney 	/*
2459e2fc3408SPatrick Mooney 	 * In a few specific instances, vn_reinit() is used to initialize
2460e2fc3408SPatrick Mooney 	 * locally defined vnode_t instances.  Lacking the construction offered
2461e2fc3408SPatrick Mooney 	 * by vn_alloc(), these vnodes require v_path initialization.
2462e2fc3408SPatrick Mooney 	 */
2463e2fc3408SPatrick Mooney 	if (vp->v_path == NULL) {
2464e2fc3408SPatrick Mooney 		vp->v_path = vn_vpath_empty;
2465e2fc3408SPatrick Mooney 	}
2466e2fc3408SPatrick Mooney 
24677c478bd9Sstevel@tonic-gate 	/* Handles v_femhead, v_path, and the r/w/map counts */
24687c478bd9Sstevel@tonic-gate 	vn_recycle(vp);
24697c478bd9Sstevel@tonic-gate }
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate vnode_t *
vn_alloc(int kmflag)24727c478bd9Sstevel@tonic-gate vn_alloc(int kmflag)
24737c478bd9Sstevel@tonic-gate {
24747c478bd9Sstevel@tonic-gate 	vnode_t *vp;
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 	vp = kmem_cache_alloc(vn_cache, kmflag);
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 	if (vp != NULL) {
24797c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2480df2381bfSpraks 		vp->v_fopdata = NULL;
24817c478bd9Sstevel@tonic-gate 		vn_reinit(vp);
24827c478bd9Sstevel@tonic-gate 	}
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	return (vp);
24857c478bd9Sstevel@tonic-gate }
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate void
vn_free(vnode_t * vp)24887c478bd9Sstevel@tonic-gate vn_free(vnode_t *vp)
24897c478bd9Sstevel@tonic-gate {
2490da6c28aaSamw 	ASSERT(vp->v_shrlocks == NULL);
2491da6c28aaSamw 	ASSERT(vp->v_filocks == NULL);
2492da6c28aaSamw 
24937c478bd9Sstevel@tonic-gate 	/*
24947c478bd9Sstevel@tonic-gate 	 * Some file systems call vn_free() with v_count of zero,
24957c478bd9Sstevel@tonic-gate 	 * some with v_count of 1.  In any case, the value should
24967c478bd9Sstevel@tonic-gate 	 * never be anything else.
24977c478bd9Sstevel@tonic-gate 	 */
24987c478bd9Sstevel@tonic-gate 	ASSERT((vp->v_count == 0) || (vp->v_count == 1));
2499b5fca8f8Stomee 	ASSERT(vp->v_count_dnlc == 0);
2500e2fc3408SPatrick Mooney 	VERIFY(vp->v_path != NULL);
2501e2fc3408SPatrick Mooney 	if (vp->v_path != vn_vpath_empty) {
25027c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2503e2fc3408SPatrick Mooney 		vp->v_path = vn_vpath_empty;
25047c478bd9Sstevel@tonic-gate 	}
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate 	/* If FEM was in use, make sure everything gets cleaned up */
25077c478bd9Sstevel@tonic-gate 	if (vp->v_femhead) {
25087c478bd9Sstevel@tonic-gate 		/* XXX - There should be a free_femhead() that does all this */
25097c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_femhead->femh_list == NULL);
25107c478bd9Sstevel@tonic-gate 		mutex_destroy(&vp->v_femhead->femh_lock);
25117c478bd9Sstevel@tonic-gate 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
25127c478bd9Sstevel@tonic-gate 		vp->v_femhead = NULL;
25137c478bd9Sstevel@tonic-gate 	}
2514df2381bfSpraks 
2515df2381bfSpraks 	if (vp->v_fopdata != NULL) {
2516df2381bfSpraks 		free_fopdata(vp);
2517df2381bfSpraks 	}
25187c478bd9Sstevel@tonic-gate 	vp->v_mpssdata = NULL;
25191b300de9Sjwahlig 	vsd_free(vp);
25207c478bd9Sstevel@tonic-gate 	kmem_cache_free(vn_cache, vp);
25217c478bd9Sstevel@tonic-gate }
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate /*
25247c478bd9Sstevel@tonic-gate  * vnode status changes, should define better states than 1, 0.
25257c478bd9Sstevel@tonic-gate  */
25267c478bd9Sstevel@tonic-gate void
vn_reclaim(vnode_t * vp)25277c478bd9Sstevel@tonic-gate vn_reclaim(vnode_t *vp)
25287c478bd9Sstevel@tonic-gate {
25297c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
25307c478bd9Sstevel@tonic-gate 
2531ddfcde86Srsb 	if (vfsp == NULL ||
2532ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
25337c478bd9Sstevel@tonic-gate 		return;
25347c478bd9Sstevel@tonic-gate 	}
25357c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED);
25367c478bd9Sstevel@tonic-gate }
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate void
vn_idle(vnode_t * vp)25397c478bd9Sstevel@tonic-gate vn_idle(vnode_t *vp)
25407c478bd9Sstevel@tonic-gate {
25417c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
25427c478bd9Sstevel@tonic-gate 
2543ddfcde86Srsb 	if (vfsp == NULL ||
2544ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
25457c478bd9Sstevel@tonic-gate 		return;
25467c478bd9Sstevel@tonic-gate 	}
25477c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_IDLED);
25487c478bd9Sstevel@tonic-gate }
25497c478bd9Sstevel@tonic-gate void
vn_exists(vnode_t * vp)25507c478bd9Sstevel@tonic-gate vn_exists(vnode_t *vp)
25517c478bd9Sstevel@tonic-gate {
25527c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
25537c478bd9Sstevel@tonic-gate 
2554ddfcde86Srsb 	if (vfsp == NULL ||
2555ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
25567c478bd9Sstevel@tonic-gate 		return;
25577c478bd9Sstevel@tonic-gate 	}
25587c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS);
25597c478bd9Sstevel@tonic-gate }
25607c478bd9Sstevel@tonic-gate 
25617c478bd9Sstevel@tonic-gate void
vn_invalid(vnode_t * vp)25627c478bd9Sstevel@tonic-gate vn_invalid(vnode_t *vp)
25637c478bd9Sstevel@tonic-gate {
25647c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
25657c478bd9Sstevel@tonic-gate 
2566ddfcde86Srsb 	if (vfsp == NULL ||
2567ddfcde86Srsb 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
25687c478bd9Sstevel@tonic-gate 		return;
25697c478bd9Sstevel@tonic-gate 	}
25707c478bd9Sstevel@tonic-gate 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED);
25717c478bd9Sstevel@tonic-gate }
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate /* Vnode event notification */
25747c478bd9Sstevel@tonic-gate 
25757c478bd9Sstevel@tonic-gate int
vnevent_support(vnode_t * vp,caller_context_t * ct)2576da6c28aaSamw vnevent_support(vnode_t *vp, caller_context_t *ct)
25777c478bd9Sstevel@tonic-gate {
25787c478bd9Sstevel@tonic-gate 	if (vp == NULL)
25797c478bd9Sstevel@tonic-gate 		return (EINVAL);
25807c478bd9Sstevel@tonic-gate 
2581da6c28aaSamw 	return (VOP_VNEVENT(vp, VE_SUPPORT, NULL, NULL, ct));
2582df2381bfSpraks }
2583df2381bfSpraks 
2584df2381bfSpraks void
vnevent_rename_src(vnode_t * vp,vnode_t * dvp,char * name,caller_context_t * ct)2585da6c28aaSamw vnevent_rename_src(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2586df2381bfSpraks {
2587df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2588df2381bfSpraks 		return;
2589df2381bfSpraks 	}
2590da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RENAME_SRC, dvp, name, ct);
2591df2381bfSpraks }
2592df2381bfSpraks 
2593df2381bfSpraks void
vnevent_rename_dest(vnode_t * vp,vnode_t * dvp,char * name,caller_context_t * ct)2594da6c28aaSamw vnevent_rename_dest(vnode_t *vp, vnode_t *dvp, char *name,
2595da6c28aaSamw     caller_context_t *ct)
2596df2381bfSpraks {
2597df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2598df2381bfSpraks 		return;
2599df2381bfSpraks 	}
2600da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST, dvp, name, ct);
2601df2381bfSpraks }
2602df2381bfSpraks 
2603df2381bfSpraks void
vnevent_rename_dest_dir(vnode_t * vp,caller_context_t * ct)2604da6c28aaSamw vnevent_rename_dest_dir(vnode_t *vp, caller_context_t *ct)
2605df2381bfSpraks {
2606df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2607df2381bfSpraks 		return;
2608df2381bfSpraks 	}
2609da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST_DIR, NULL, NULL, ct);
2610df2381bfSpraks }
2611df2381bfSpraks 
2612df2381bfSpraks void
vnevent_remove(vnode_t * vp,vnode_t * dvp,char * name,caller_context_t * ct)2613da6c28aaSamw vnevent_remove(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2614df2381bfSpraks {
2615df2381bfSpraks 	if (vp == NULL || vp->v_femhead == NULL) {
2616df2381bfSpraks 		return;
2617df2381bfSpraks 	}
2618da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_REMOVE, dvp, name, ct);
26197c478bd9Sstevel@tonic-gate }
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate void
vnevent_rmdir(vnode_t * vp,vnode_t * dvp,char * name,caller_context_t * ct)2622da6c28aaSamw vnevent_rmdir(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
26237c478bd9Sstevel@tonic-gate {
26247c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
26257c478bd9Sstevel@tonic-gate 		return;
26267c478bd9Sstevel@tonic-gate 	}
2627da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_RMDIR, dvp, name, ct);
26287c478bd9Sstevel@tonic-gate }
26297c478bd9Sstevel@tonic-gate 
263054207fd2SJerry Jelinek void
vnevent_pre_rename_src(vnode_t * vp,vnode_t * dvp,char * name,caller_context_t * ct)263154207fd2SJerry Jelinek vnevent_pre_rename_src(vnode_t *vp, vnode_t *dvp, char *name,
263254207fd2SJerry Jelinek     caller_context_t *ct)
263354207fd2SJerry Jelinek {
263454207fd2SJerry Jelinek 	if (vp == NULL || vp->v_femhead == NULL) {
263554207fd2SJerry Jelinek 		return;
263654207fd2SJerry Jelinek 	}
263754207fd2SJerry Jelinek 	(void) VOP_VNEVENT(vp, VE_PRE_RENAME_SRC, dvp, name, ct);
263854207fd2SJerry Jelinek }
263954207fd2SJerry Jelinek 
264054207fd2SJerry Jelinek void
vnevent_pre_rename_dest(vnode_t * vp,vnode_t * dvp,char * name,caller_context_t * ct)264154207fd2SJerry Jelinek vnevent_pre_rename_dest(vnode_t *vp, vnode_t *dvp, char *name,
264254207fd2SJerry Jelinek     caller_context_t *ct)
264354207fd2SJerry Jelinek {
264454207fd2SJerry Jelinek 	if (vp == NULL || vp->v_femhead == NULL) {
264554207fd2SJerry Jelinek 		return;
264654207fd2SJerry Jelinek 	}
264754207fd2SJerry Jelinek 	(void) VOP_VNEVENT(vp, VE_PRE_RENAME_DEST, dvp, name, ct);
264854207fd2SJerry Jelinek }
264954207fd2SJerry Jelinek 
265054207fd2SJerry Jelinek void
vnevent_pre_rename_dest_dir(vnode_t * vp,vnode_t * nvp,char * name,caller_context_t * ct)265154207fd2SJerry Jelinek vnevent_pre_rename_dest_dir(vnode_t *vp, vnode_t *nvp, char *name,
265254207fd2SJerry Jelinek     caller_context_t *ct)
265354207fd2SJerry Jelinek {
265454207fd2SJerry Jelinek 	if (vp == NULL || vp->v_femhead == NULL) {
265554207fd2SJerry Jelinek 		return;
265654207fd2SJerry Jelinek 	}
265754207fd2SJerry Jelinek 	(void) VOP_VNEVENT(vp, VE_PRE_RENAME_DEST_DIR, nvp, name, ct);
265854207fd2SJerry Jelinek }
265954207fd2SJerry Jelinek 
26607c478bd9Sstevel@tonic-gate void
vnevent_create(vnode_t * vp,caller_context_t * ct)2661da6c28aaSamw vnevent_create(vnode_t *vp, caller_context_t *ct)
26627c478bd9Sstevel@tonic-gate {
26637c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
26647c478bd9Sstevel@tonic-gate 		return;
26657c478bd9Sstevel@tonic-gate 	}
2666da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_CREATE, NULL, NULL, ct);
26677c478bd9Sstevel@tonic-gate }
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate void
vnevent_link(vnode_t * vp,caller_context_t * ct)2670da6c28aaSamw vnevent_link(vnode_t *vp, caller_context_t *ct)
26717c478bd9Sstevel@tonic-gate {
26727c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
26737c478bd9Sstevel@tonic-gate 		return;
26747c478bd9Sstevel@tonic-gate 	}
2675da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_LINK, NULL, NULL, ct);
26767c478bd9Sstevel@tonic-gate }
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate void
vnevent_mountedover(vnode_t * vp,caller_context_t * ct)2679da6c28aaSamw vnevent_mountedover(vnode_t *vp, caller_context_t *ct)
26807c478bd9Sstevel@tonic-gate {
26817c478bd9Sstevel@tonic-gate 	if (vp == NULL || vp->v_femhead == NULL) {
26827c478bd9Sstevel@tonic-gate 		return;
26837c478bd9Sstevel@tonic-gate 	}
2684da6c28aaSamw 	(void) VOP_VNEVENT(vp, VE_MOUNTEDOVER, NULL, NULL, ct);
26857c478bd9Sstevel@tonic-gate }
26867c478bd9Sstevel@tonic-gate 
268772102e74SBryan Cantrill void
vnevent_truncate(vnode_t * vp,caller_context_t * ct)268872102e74SBryan Cantrill vnevent_truncate(vnode_t *vp, caller_context_t *ct)
268972102e74SBryan Cantrill {
269072102e74SBryan Cantrill 	if (vp == NULL || vp->v_femhead == NULL) {
269172102e74SBryan Cantrill 		return;
269272102e74SBryan Cantrill 	}
269372102e74SBryan Cantrill 	(void) VOP_VNEVENT(vp, VE_TRUNCATE, NULL, NULL, ct);
269472102e74SBryan Cantrill }
269572102e74SBryan Cantrill 
26967c478bd9Sstevel@tonic-gate /*
26977c478bd9Sstevel@tonic-gate  * Vnode accessors.
26987c478bd9Sstevel@tonic-gate  */
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate int
vn_is_readonly(vnode_t * vp)27017c478bd9Sstevel@tonic-gate vn_is_readonly(vnode_t *vp)
27027c478bd9Sstevel@tonic-gate {
27037c478bd9Sstevel@tonic-gate 	return (vp->v_vfsp->vfs_flag & VFS_RDONLY);
27047c478bd9Sstevel@tonic-gate }
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate int
vn_has_flocks(vnode_t * vp)27077c478bd9Sstevel@tonic-gate vn_has_flocks(vnode_t *vp)
27087c478bd9Sstevel@tonic-gate {
27097c478bd9Sstevel@tonic-gate 	return (vp->v_filocks != NULL);
27107c478bd9Sstevel@tonic-gate }
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate int
vn_has_mandatory_locks(vnode_t * vp,int mode)27137c478bd9Sstevel@tonic-gate vn_has_mandatory_locks(vnode_t *vp, int mode)
27147c478bd9Sstevel@tonic-gate {
27157c478bd9Sstevel@tonic-gate 	return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode)));
27167c478bd9Sstevel@tonic-gate }
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate int
vn_has_cached_data(vnode_t * vp)27197c478bd9Sstevel@tonic-gate vn_has_cached_data(vnode_t *vp)
27207c478bd9Sstevel@tonic-gate {
27217c478bd9Sstevel@tonic-gate 	return (vp->v_pages != NULL);
27227c478bd9Sstevel@tonic-gate }
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate /*
27257c478bd9Sstevel@tonic-gate  * Return 0 if the vnode in question shouldn't be permitted into a zone via
27267c478bd9Sstevel@tonic-gate  * zone_enter(2).
27277c478bd9Sstevel@tonic-gate  */
27287c478bd9Sstevel@tonic-gate int
vn_can_change_zones(vnode_t * vp)27297c478bd9Sstevel@tonic-gate vn_can_change_zones(vnode_t *vp)
27307c478bd9Sstevel@tonic-gate {
27317c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
27327c478bd9Sstevel@tonic-gate 	int allow = 1;
27337c478bd9Sstevel@tonic-gate 	vnode_t *rvp;
27347c478bd9Sstevel@tonic-gate 
2735108322fbScarlsonj 	if (nfs_global_client_only != 0)
2736108322fbScarlsonj 		return (1);
2737108322fbScarlsonj 
27387c478bd9Sstevel@tonic-gate 	/*
27397c478bd9Sstevel@tonic-gate 	 * We always want to look at the underlying vnode if there is one.
27407c478bd9Sstevel@tonic-gate 	 */
2741da6c28aaSamw 	if (VOP_REALVP(vp, &rvp, NULL) != 0)
27427c478bd9Sstevel@tonic-gate 		rvp = vp;
27437c478bd9Sstevel@tonic-gate 	/*
27447c478bd9Sstevel@tonic-gate 	 * Some pseudo filesystems (including doorfs) don't actually register
27457c478bd9Sstevel@tonic-gate 	 * their vfsops_t, so the following may return NULL; we happily let
27467c478bd9Sstevel@tonic-gate 	 * such vnodes switch zones.
27477c478bd9Sstevel@tonic-gate 	 */
27487c478bd9Sstevel@tonic-gate 	vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp));
27497c478bd9Sstevel@tonic-gate 	if (vswp != NULL) {
27507c478bd9Sstevel@tonic-gate 		if (vswp->vsw_flag & VSW_NOTZONESAFE)
27517c478bd9Sstevel@tonic-gate 			allow = 0;
27527c478bd9Sstevel@tonic-gate 		vfs_unrefvfssw(vswp);
27537c478bd9Sstevel@tonic-gate 	}
27547c478bd9Sstevel@tonic-gate 	return (allow);
27557c478bd9Sstevel@tonic-gate }
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate /*
27587c478bd9Sstevel@tonic-gate  * Return nonzero if the vnode is a mount point, zero if not.
27597c478bd9Sstevel@tonic-gate  */
27607c478bd9Sstevel@tonic-gate int
vn_ismntpt(vnode_t * vp)27617c478bd9Sstevel@tonic-gate vn_ismntpt(vnode_t *vp)
27627c478bd9Sstevel@tonic-gate {
27637c478bd9Sstevel@tonic-gate 	return (vp->v_vfsmountedhere != NULL);
27647c478bd9Sstevel@tonic-gate }
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate /* Retrieve the vfs (if any) mounted on this vnode */
27677c478bd9Sstevel@tonic-gate vfs_t *
vn_mountedvfs(vnode_t * vp)27687c478bd9Sstevel@tonic-gate vn_mountedvfs(vnode_t *vp)
27697c478bd9Sstevel@tonic-gate {
27707c478bd9Sstevel@tonic-gate 	return (vp->v_vfsmountedhere);
27717c478bd9Sstevel@tonic-gate }
27727c478bd9Sstevel@tonic-gate 
2773b5fca8f8Stomee /*
2774b5fca8f8Stomee  * Return nonzero if the vnode is referenced by the dnlc, zero if not.
2775b5fca8f8Stomee  */
2776b5fca8f8Stomee int
vn_in_dnlc(vnode_t * vp)2777b5fca8f8Stomee vn_in_dnlc(vnode_t *vp)
2778b5fca8f8Stomee {
2779b5fca8f8Stomee 	return (vp->v_count_dnlc > 0);
2780b5fca8f8Stomee }
2781b5fca8f8Stomee 
2782da6c28aaSamw /*
2783da6c28aaSamw  * vn_has_other_opens() checks whether a particular file is opened by more than
2784da6c28aaSamw  * just the caller and whether the open is for read and/or write.
2785da6c28aaSamw  * This routine is for calling after the caller has already called VOP_OPEN()
2786da6c28aaSamw  * and the caller wishes to know if they are the only one with it open for
2787da6c28aaSamw  * the mode(s) specified.
2788da6c28aaSamw  *
2789da6c28aaSamw  * Vnode counts are only kept on regular files (v_type=VREG).
2790da6c28aaSamw  */
2791da6c28aaSamw int
vn_has_other_opens(vnode_t * vp,v_mode_t mode)2792da6c28aaSamw vn_has_other_opens(
2793da6c28aaSamw 	vnode_t *vp,
2794da6c28aaSamw 	v_mode_t mode)
2795da6c28aaSamw {
2796da6c28aaSamw 
2797da6c28aaSamw 	ASSERT(vp != NULL);
2798da6c28aaSamw 
2799da6c28aaSamw 	switch (mode) {
2800da6c28aaSamw 	case V_WRITE:
2801da6c28aaSamw 		if (vp->v_wrcnt > 1)
2802da6c28aaSamw 			return (V_TRUE);
2803da6c28aaSamw 		break;
2804da6c28aaSamw 	case V_RDORWR:
2805da6c28aaSamw 		if ((vp->v_rdcnt > 1) || (vp->v_wrcnt > 1))
2806da6c28aaSamw 			return (V_TRUE);
2807da6c28aaSamw 		break;
2808da6c28aaSamw 	case V_RDANDWR:
2809da6c28aaSamw 		if ((vp->v_rdcnt > 1) && (vp->v_wrcnt > 1))
2810da6c28aaSamw 			return (V_TRUE);
2811da6c28aaSamw 		break;
2812da6c28aaSamw 	case V_READ:
2813da6c28aaSamw 		if (vp->v_rdcnt > 1)
2814da6c28aaSamw 			return (V_TRUE);
2815da6c28aaSamw 		break;
2816da6c28aaSamw 	}
2817da6c28aaSamw 
2818da6c28aaSamw 	return (V_FALSE);
2819da6c28aaSamw }
2820da6c28aaSamw 
28217c478bd9Sstevel@tonic-gate /*
28227c478bd9Sstevel@tonic-gate  * vn_is_opened() checks whether a particular file is opened and
28237c478bd9Sstevel@tonic-gate  * whether the open is for read and/or write.
28247c478bd9Sstevel@tonic-gate  *
28257c478bd9Sstevel@tonic-gate  * Vnode counts are only kept on regular files (v_type=VREG).
28267c478bd9Sstevel@tonic-gate  */
28277c478bd9Sstevel@tonic-gate int
vn_is_opened(vnode_t * vp,v_mode_t mode)28287c478bd9Sstevel@tonic-gate vn_is_opened(
28297c478bd9Sstevel@tonic-gate 	vnode_t *vp,
28307c478bd9Sstevel@tonic-gate 	v_mode_t mode)
28317c478bd9Sstevel@tonic-gate {
28327c478bd9Sstevel@tonic-gate 
28337c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	switch (mode) {
28367c478bd9Sstevel@tonic-gate 	case V_WRITE:
28377c478bd9Sstevel@tonic-gate 		if (vp->v_wrcnt)
28387c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28397c478bd9Sstevel@tonic-gate 		break;
28407c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
28417c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt && vp->v_wrcnt)
28427c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28437c478bd9Sstevel@tonic-gate 		break;
28447c478bd9Sstevel@tonic-gate 	case V_RDORWR:
28457c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt || vp->v_wrcnt)
28467c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28477c478bd9Sstevel@tonic-gate 		break;
28487c478bd9Sstevel@tonic-gate 	case V_READ:
28497c478bd9Sstevel@tonic-gate 		if (vp->v_rdcnt)
28507c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28517c478bd9Sstevel@tonic-gate 		break;
28527c478bd9Sstevel@tonic-gate 	}
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 	return (V_FALSE);
28557c478bd9Sstevel@tonic-gate }
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate /*
28587c478bd9Sstevel@tonic-gate  * vn_is_mapped() checks whether a particular file is mapped and whether
28597c478bd9Sstevel@tonic-gate  * the file is mapped read and/or write.
28607c478bd9Sstevel@tonic-gate  */
28617c478bd9Sstevel@tonic-gate int
vn_is_mapped(vnode_t * vp,v_mode_t mode)28627c478bd9Sstevel@tonic-gate vn_is_mapped(
28637c478bd9Sstevel@tonic-gate 	vnode_t *vp,
28647c478bd9Sstevel@tonic-gate 	v_mode_t mode)
28657c478bd9Sstevel@tonic-gate {
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate #if !defined(_LP64)
28707c478bd9Sstevel@tonic-gate 	switch (mode) {
28717c478bd9Sstevel@tonic-gate 	/*
28727c478bd9Sstevel@tonic-gate 	 * The atomic_add_64_nv functions force atomicity in the
28737c478bd9Sstevel@tonic-gate 	 * case of 32 bit architectures. Otherwise the 64 bit values
28747c478bd9Sstevel@tonic-gate 	 * require two fetches. The value of the fields may be
28757c478bd9Sstevel@tonic-gate 	 * (potentially) changed between the first fetch and the
28767c478bd9Sstevel@tonic-gate 	 * second
28777c478bd9Sstevel@tonic-gate 	 */
28787c478bd9Sstevel@tonic-gate 	case V_WRITE:
28797c478bd9Sstevel@tonic-gate 		if (atomic_add_64_nv((&(vp->v_mmap_write)), 0))
28807c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28817c478bd9Sstevel@tonic-gate 		break;
28827c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
28837c478bd9Sstevel@tonic-gate 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) &&
28847c478bd9Sstevel@tonic-gate 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
28857c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28867c478bd9Sstevel@tonic-gate 		break;
28877c478bd9Sstevel@tonic-gate 	case V_RDORWR:
28887c478bd9Sstevel@tonic-gate 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) ||
28897c478bd9Sstevel@tonic-gate 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
28907c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28917c478bd9Sstevel@tonic-gate 		break;
28927c478bd9Sstevel@tonic-gate 	case V_READ:
28937c478bd9Sstevel@tonic-gate 		if (atomic_add_64_nv((&(vp->v_mmap_read)), 0))
28947c478bd9Sstevel@tonic-gate 			return (V_TRUE);
28957c478bd9Sstevel@tonic-gate 		break;
28967c478bd9Sstevel@tonic-gate 	}
28977c478bd9Sstevel@tonic-gate #else
28987c478bd9Sstevel@tonic-gate 	switch (mode) {
28997c478bd9Sstevel@tonic-gate 	case V_WRITE:
29007c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_write)
29017c478bd9Sstevel@tonic-gate 			return (V_TRUE);
29027c478bd9Sstevel@tonic-gate 		break;
29037c478bd9Sstevel@tonic-gate 	case V_RDANDWR:
29047c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read && vp->v_mmap_write)
29057c478bd9Sstevel@tonic-gate 			return (V_TRUE);
29067c478bd9Sstevel@tonic-gate 		break;
29077c478bd9Sstevel@tonic-gate 	case V_RDORWR:
29087c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read || vp->v_mmap_write)
29097c478bd9Sstevel@tonic-gate 			return (V_TRUE);
29107c478bd9Sstevel@tonic-gate 		break;
29117c478bd9Sstevel@tonic-gate 	case V_READ:
29127c478bd9Sstevel@tonic-gate 		if (vp->v_mmap_read)
29137c478bd9Sstevel@tonic-gate 			return (V_TRUE);
29147c478bd9Sstevel@tonic-gate 		break;
29157c478bd9Sstevel@tonic-gate 	}
29167c478bd9Sstevel@tonic-gate #endif
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	return (V_FALSE);
29197c478bd9Sstevel@tonic-gate }
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate /*
29227c478bd9Sstevel@tonic-gate  * Set the operations vector for a vnode.
29237c478bd9Sstevel@tonic-gate  *
29247c478bd9Sstevel@tonic-gate  * FEM ensures that the v_femhead pointer is filled in before the
29257c478bd9Sstevel@tonic-gate  * v_op pointer is changed.  This means that if the v_femhead pointer
29267c478bd9Sstevel@tonic-gate  * is NULL, and the v_op field hasn't changed since before which checked
29277c478bd9Sstevel@tonic-gate  * the v_femhead pointer; then our update is ok - we are not racing with
29287c478bd9Sstevel@tonic-gate  * FEM.
29297c478bd9Sstevel@tonic-gate  */
29307c478bd9Sstevel@tonic-gate void
vn_setops(vnode_t * vp,vnodeops_t * vnodeops)29317c478bd9Sstevel@tonic-gate vn_setops(vnode_t *vp, vnodeops_t *vnodeops)
29327c478bd9Sstevel@tonic-gate {
29337c478bd9Sstevel@tonic-gate 	vnodeops_t	*op;
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
29367c478bd9Sstevel@tonic-gate 	ASSERT(vnodeops != NULL);
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	op = vp->v_op;
29397c478bd9Sstevel@tonic-gate 	membar_consumer();
29407c478bd9Sstevel@tonic-gate 	/*
294175d94465SJosef 'Jeff' Sipek 	 * If vp->v_femhead == NULL, then we'll call atomic_cas_ptr() to do
294275d94465SJosef 'Jeff' Sipek 	 * the compare-and-swap on vp->v_op.  If either fails, then FEM is
29437c478bd9Sstevel@tonic-gate 	 * in effect on the vnode and we need to have FEM deal with it.
29447c478bd9Sstevel@tonic-gate 	 */
294575d94465SJosef 'Jeff' Sipek 	if (vp->v_femhead != NULL || atomic_cas_ptr(&vp->v_op, op, vnodeops) !=
294675d94465SJosef 'Jeff' Sipek 	    op) {
29477c478bd9Sstevel@tonic-gate 		fem_setvnops(vp, vnodeops);
29487c478bd9Sstevel@tonic-gate 	}
29497c478bd9Sstevel@tonic-gate }
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate /*
29527c478bd9Sstevel@tonic-gate  * Retrieve the operations vector for a vnode
29537c478bd9Sstevel@tonic-gate  * As with vn_setops(above); make sure we aren't racing with FEM.
29547c478bd9Sstevel@tonic-gate  * FEM sets the v_op to a special, internal, vnodeops that wouldn't
29557c478bd9Sstevel@tonic-gate  * make sense to the callers of this routine.
29567c478bd9Sstevel@tonic-gate  */
29577c478bd9Sstevel@tonic-gate vnodeops_t *
vn_getops(vnode_t * vp)29587c478bd9Sstevel@tonic-gate vn_getops(vnode_t *vp)
29597c478bd9Sstevel@tonic-gate {
29607c478bd9Sstevel@tonic-gate 	vnodeops_t	*op;
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate 	op = vp->v_op;
29657c478bd9Sstevel@tonic-gate 	membar_consumer();
29667c478bd9Sstevel@tonic-gate 	if (vp->v_femhead == NULL && op == vp->v_op) {
29677c478bd9Sstevel@tonic-gate 		return (op);
29687c478bd9Sstevel@tonic-gate 	} else {
29697c478bd9Sstevel@tonic-gate 		return (fem_getvnops(vp));
29707c478bd9Sstevel@tonic-gate 	}
29717c478bd9Sstevel@tonic-gate }
29727c478bd9Sstevel@tonic-gate 
29737c478bd9Sstevel@tonic-gate /*
29747c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the vnodeops matches that of the vnode.
29757c478bd9Sstevel@tonic-gate  * Returns zero (0) if not.
29767c478bd9Sstevel@tonic-gate  */
29777c478bd9Sstevel@tonic-gate int
vn_matchops(vnode_t * vp,vnodeops_t * vnodeops)29787c478bd9Sstevel@tonic-gate vn_matchops(vnode_t *vp, vnodeops_t *vnodeops)
29797c478bd9Sstevel@tonic-gate {
29807c478bd9Sstevel@tonic-gate 	return (vn_getops(vp) == vnodeops);
29817c478bd9Sstevel@tonic-gate }
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate /*
29847c478bd9Sstevel@tonic-gate  * Returns non-zero (1) if the specified operation matches the
29857c478bd9Sstevel@tonic-gate  * corresponding operation for that the vnode.
29867c478bd9Sstevel@tonic-gate  * Returns zero (0) if not.
29877c478bd9Sstevel@tonic-gate  */
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate #define	MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0))
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate int
vn_matchopval(vnode_t * vp,char * vopname,fs_generic_func_p funcp)29927c478bd9Sstevel@tonic-gate vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp)
29937c478bd9Sstevel@tonic-gate {
29947c478bd9Sstevel@tonic-gate 	const fs_operation_trans_def_t *otdp;
29957c478bd9Sstevel@tonic-gate 	fs_generic_func_p *loc = NULL;
29967c478bd9Sstevel@tonic-gate 	vnodeops_t	*vop = vn_getops(vp);
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 	ASSERT(vopname != NULL);
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 	for (otdp = vn_ops_table; otdp->name != NULL; otdp++) {
30017c478bd9Sstevel@tonic-gate 		if (MATCHNAME(otdp->name, vopname)) {
3002b9c98e29Spf 			loc = (fs_generic_func_p *)
3003b9c98e29Spf 			    ((char *)(vop) + otdp->offset);
30047c478bd9Sstevel@tonic-gate 			break;
30057c478bd9Sstevel@tonic-gate 		}
30067c478bd9Sstevel@tonic-gate 	}
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 	return ((loc != NULL) && (*loc == funcp));
30097c478bd9Sstevel@tonic-gate }
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate /*
30127c478bd9Sstevel@tonic-gate  * fs_new_caller_id() needs to return a unique ID on a given local system.
30137c478bd9Sstevel@tonic-gate  * The IDs do not need to survive across reboots.  These are primarily
30147c478bd9Sstevel@tonic-gate  * used so that (FEM) monitors can detect particular callers (such as
30157c478bd9Sstevel@tonic-gate  * the NFS server) to a given vnode/vfs operation.
30167c478bd9Sstevel@tonic-gate  */
30177c478bd9Sstevel@tonic-gate u_longlong_t
fs_new_caller_id()30187c478bd9Sstevel@tonic-gate fs_new_caller_id()
30197c478bd9Sstevel@tonic-gate {
30207c478bd9Sstevel@tonic-gate 	static uint64_t next_caller_id = 0LL; /* First call returns 1 */
30217c478bd9Sstevel@tonic-gate 
30221a5e258fSJosef 'Jeff' Sipek 	return ((u_longlong_t)atomic_inc_64_nv(&next_caller_id));
30237c478bd9Sstevel@tonic-gate }
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate /*
3026e2fc3408SPatrick Mooney  * The value stored in v_path is relative to rootdir, located in the global
3027e2fc3408SPatrick Mooney  * zone.  Zones or chroot environments which reside deeper inside the VFS
3028e2fc3408SPatrick Mooney  * hierarchy will have a relative view of MAXPATHLEN since they are unaware of
3029e2fc3408SPatrick Mooney  * what lies below their perceived root.  In order to keep v_path usable for
3030e2fc3408SPatrick Mooney  * these child environments, its allocations are allowed to exceed MAXPATHLEN.
3031e2fc3408SPatrick Mooney  *
3032e2fc3408SPatrick Mooney  * An upper bound of max_vnode_path is placed upon v_path allocations to
3033e2fc3408SPatrick Mooney  * prevent the system from going too wild at the behest of pathological
3034e2fc3408SPatrick Mooney  * behavior from the operator.
30357c478bd9Sstevel@tonic-gate  */
303648011479Ssn size_t max_vnode_path = 4 * MAXPATHLEN;
303748011479Ssn 
3038e2fc3408SPatrick Mooney 
30397c478bd9Sstevel@tonic-gate void
vn_clearpath(vnode_t * vp,hrtime_t compare_stamp)3040e2fc3408SPatrick Mooney vn_clearpath(vnode_t *vp, hrtime_t compare_stamp)
3041e2fc3408SPatrick Mooney {
3042e2fc3408SPatrick Mooney 	char *buf;
30437c478bd9Sstevel@tonic-gate 
3044e2fc3408SPatrick Mooney 	mutex_enter(&vp->v_lock);
30457c478bd9Sstevel@tonic-gate 	/*
3046e2fc3408SPatrick Mooney 	 * If the snapshot of v_path_stamp passed in via compare_stamp does not
3047e2fc3408SPatrick Mooney 	 * match the present value on the vnode, it indicates that subsequent
3048e2fc3408SPatrick Mooney 	 * changes have occurred.  The v_path value is not cleared in this case
3049e2fc3408SPatrick Mooney 	 * since the new value may be valid.
30507c478bd9Sstevel@tonic-gate 	 */
3051e2fc3408SPatrick Mooney 	if (compare_stamp != 0 && vp->v_path_stamp != compare_stamp) {
3052e2fc3408SPatrick Mooney 		mutex_exit(&vp->v_lock);
30537c478bd9Sstevel@tonic-gate 		return;
30547c478bd9Sstevel@tonic-gate 	}
3055e2fc3408SPatrick Mooney 	buf = vp->v_path;
3056e2fc3408SPatrick Mooney 	vp->v_path = vn_vpath_empty;
3057e2fc3408SPatrick Mooney 	vp->v_path_stamp = 0;
3058e2fc3408SPatrick Mooney 	mutex_exit(&vp->v_lock);
3059e2fc3408SPatrick Mooney 	if (buf != vn_vpath_empty) {
3060e2fc3408SPatrick Mooney 		kmem_free(buf, strlen(buf) + 1);
3061e2fc3408SPatrick Mooney 	}
3062e2fc3408SPatrick Mooney }
30637c478bd9Sstevel@tonic-gate 
3064e2fc3408SPatrick Mooney static void
vn_setpath_common(vnode_t * pvp,vnode_t * vp,const char * name,size_t len,boolean_t is_rename)3065e2fc3408SPatrick Mooney vn_setpath_common(vnode_t *pvp, vnode_t *vp, const char *name, size_t len,
3066e2fc3408SPatrick Mooney     boolean_t is_rename)
3067e2fc3408SPatrick Mooney {
3068e2fc3408SPatrick Mooney 	char *buf, *oldbuf;
3069e2fc3408SPatrick Mooney 	hrtime_t pstamp;
3070e2fc3408SPatrick Mooney 	size_t baselen, buflen = 0;
3071e2fc3408SPatrick Mooney 
3072e2fc3408SPatrick Mooney 	/* Handle the vn_setpath_str case. */
3073e2fc3408SPatrick Mooney 	if (pvp == NULL) {
3074e2fc3408SPatrick Mooney 		if (len + 1 > max_vnode_path) {
3075e2fc3408SPatrick Mooney 			DTRACE_PROBE4(vn__setpath__too__long, vnode_t *, pvp,
3076e2fc3408SPatrick Mooney 			    vnode_t *, vp, char *, name, size_t, len + 1);
3077e2fc3408SPatrick Mooney 			return;
3078e2fc3408SPatrick Mooney 		}
3079e2fc3408SPatrick Mooney 		buf = kmem_alloc(len + 1, KM_SLEEP);
3080e2fc3408SPatrick Mooney 		bcopy(name, buf, len);
3081e2fc3408SPatrick Mooney 		buf[len] = '\0';
30827c478bd9Sstevel@tonic-gate 
3083e2fc3408SPatrick Mooney 		mutex_enter(&vp->v_lock);
3084e2fc3408SPatrick Mooney 		oldbuf = vp->v_path;
3085e2fc3408SPatrick Mooney 		vp->v_path = buf;
3086e2fc3408SPatrick Mooney 		vp->v_path_stamp = gethrtime();
3087e2fc3408SPatrick Mooney 		mutex_exit(&vp->v_lock);
3088e2fc3408SPatrick Mooney 		if (oldbuf != vn_vpath_empty) {
3089e2fc3408SPatrick Mooney 			kmem_free(oldbuf, strlen(oldbuf) + 1);
3090e2fc3408SPatrick Mooney 		}
3091e2fc3408SPatrick Mooney 		return;
3092e2fc3408SPatrick Mooney 	}
3093e2fc3408SPatrick Mooney 
3094e2fc3408SPatrick Mooney 	/* Take snapshot of parent dir */
3095e2fc3408SPatrick Mooney 	mutex_enter(&pvp->v_lock);
30967c478bd9Sstevel@tonic-gate 
3097e2fc3408SPatrick Mooney 	if ((pvp->v_flag & VTRAVERSE) != 0) {
3098e2fc3408SPatrick Mooney 		/*
3099e2fc3408SPatrick Mooney 		 * When the parent vnode has VTRAVERSE set in its flags, normal
3100e2fc3408SPatrick Mooney 		 * assumptions about v_path calculation no longer apply.  The
3101e2fc3408SPatrick Mooney 		 * primary situation where this occurs is via the VFS tricks
3102e2fc3408SPatrick Mooney 		 * which procfs plays in order to allow /proc/PID/(root|cwd) to
3103e2fc3408SPatrick Mooney 		 * yield meaningful results.
3104e2fc3408SPatrick Mooney 		 *
3105e2fc3408SPatrick Mooney 		 * When this flag is set, v_path on the child must not be
3106e2fc3408SPatrick Mooney 		 * updated since the calculated value is likely to be
3107e2fc3408SPatrick Mooney 		 * incorrect, given the current context.
3108e2fc3408SPatrick Mooney 		 */
3109e2fc3408SPatrick Mooney 		mutex_exit(&pvp->v_lock);
311048011479Ssn 		return;
3111e2fc3408SPatrick Mooney 	}
3112e2fc3408SPatrick Mooney 
3113e2fc3408SPatrick Mooney retrybuf:
3114e2fc3408SPatrick Mooney 	if (pvp->v_path == vn_vpath_empty) {
3115e2fc3408SPatrick Mooney 		/*
3116e2fc3408SPatrick Mooney 		 * Without v_path from the parent directory, generating a child
3117e2fc3408SPatrick Mooney 		 * path from the name is impossible.
3118e2fc3408SPatrick Mooney 		 */
3119e2fc3408SPatrick Mooney 		if (len > 0) {
3120e2fc3408SPatrick Mooney 			pstamp = pvp->v_path_stamp;
3121e2fc3408SPatrick Mooney 			mutex_exit(&pvp->v_lock);
3122e2fc3408SPatrick Mooney 			vn_clearpath(vp, pstamp);
3123e2fc3408SPatrick Mooney 			return;
3124e2fc3408SPatrick Mooney 		}
312548011479Ssn 
3126e2fc3408SPatrick Mooney 		/*
3127e2fc3408SPatrick Mooney 		 * The only feasible case here is where a NUL lookup is being
3128e2fc3408SPatrick Mooney 		 * performed on rootdir prior to its v_path being populated.
3129e2fc3408SPatrick Mooney 		 */
3130e2fc3408SPatrick Mooney 		ASSERT(pvp->v_path_stamp == 0);
3131e2fc3408SPatrick Mooney 		baselen = 0;
3132e2fc3408SPatrick Mooney 		pstamp = 0;
3133e2fc3408SPatrick Mooney 	} else {
3134e2fc3408SPatrick Mooney 		pstamp = pvp->v_path_stamp;
3135e2fc3408SPatrick Mooney 		baselen = strlen(pvp->v_path);
3136e2fc3408SPatrick Mooney 		/* ignore a trailing slash if present */
3137e2fc3408SPatrick Mooney 		if (pvp->v_path[baselen - 1] == '/') {
3138e2fc3408SPatrick Mooney 			/* This should only the be case for rootdir */
3139e2fc3408SPatrick Mooney 			ASSERT(baselen == 1 && pvp == rootdir);
3140e2fc3408SPatrick Mooney 			baselen--;
3141e2fc3408SPatrick Mooney 		}
3142e2fc3408SPatrick Mooney 	}
3143e2fc3408SPatrick Mooney 	mutex_exit(&pvp->v_lock);
31447c478bd9Sstevel@tonic-gate 
3145e2fc3408SPatrick Mooney 	if (buflen != 0) {
3146e2fc3408SPatrick Mooney 		/* Free the existing (mis-sized) buffer in case of retry */
3147e2fc3408SPatrick Mooney 		kmem_free(buf, buflen);
3148e2fc3408SPatrick Mooney 	}
3149e2fc3408SPatrick Mooney 	/* base, '/', name and trailing NUL */
3150e2fc3408SPatrick Mooney 	buflen = baselen + len + 2;
3151e2fc3408SPatrick Mooney 	if (buflen > max_vnode_path) {
3152e2fc3408SPatrick Mooney 		DTRACE_PROBE4(vn__setpath_too__long, vnode_t *, pvp,
3153e2fc3408SPatrick Mooney 		    vnode_t *, vp, char *, name, size_t, buflen);
31547c478bd9Sstevel@tonic-gate 		return;
31557c478bd9Sstevel@tonic-gate 	}
3156e2fc3408SPatrick Mooney 	buf = kmem_alloc(buflen, KM_SLEEP);
3157e2fc3408SPatrick Mooney 
3158e2fc3408SPatrick Mooney 	mutex_enter(&pvp->v_lock);
3159e2fc3408SPatrick Mooney 	if (pvp->v_path_stamp != pstamp) {
3160e2fc3408SPatrick Mooney 		size_t vlen;
3161e2fc3408SPatrick Mooney 
3162e2fc3408SPatrick Mooney 		/*
3163e2fc3408SPatrick Mooney 		 * Since v_path_stamp changed on the parent, it is likely that
3164e2fc3408SPatrick Mooney 		 * v_path has been altered as well.  If the length does not
3165e2fc3408SPatrick Mooney 		 * exactly match what was previously measured, the buffer
3166e2fc3408SPatrick Mooney 		 * allocation must be repeated for proper sizing.
3167e2fc3408SPatrick Mooney 		 */
3168e2fc3408SPatrick Mooney 		if (pvp->v_path == vn_vpath_empty) {
3169e2fc3408SPatrick Mooney 			/* Give up if parent lack v_path */
3170e2fc3408SPatrick Mooney 			mutex_exit(&pvp->v_lock);
3171e2fc3408SPatrick Mooney 			kmem_free(buf, buflen);
3172e2fc3408SPatrick Mooney 			return;
3173e2fc3408SPatrick Mooney 		}
3174e2fc3408SPatrick Mooney 		vlen = strlen(pvp->v_path);
3175e2fc3408SPatrick Mooney 		if (pvp->v_path[vlen - 1] == '/') {
3176e2fc3408SPatrick Mooney 			vlen--;
3177e2fc3408SPatrick Mooney 		}
3178e2fc3408SPatrick Mooney 		if (vlen != baselen) {
3179e2fc3408SPatrick Mooney 			goto retrybuf;
3180e2fc3408SPatrick Mooney 		}
3181e2fc3408SPatrick Mooney 	}
3182e2fc3408SPatrick Mooney 	bcopy(pvp->v_path, buf, baselen);
3183e2fc3408SPatrick Mooney 	mutex_exit(&pvp->v_lock);
31847c478bd9Sstevel@tonic-gate 
3185e2fc3408SPatrick Mooney 	buf[baselen] = '/';
3186e2fc3408SPatrick Mooney 	baselen++;
3187e2fc3408SPatrick Mooney 	bcopy(name, &buf[baselen], len + 1);
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
3190e2fc3408SPatrick Mooney 	if (vp->v_path_stamp == 0) {
3191e2fc3408SPatrick Mooney 		/* never-visited vnode can inherit stamp from parent */
3192e2fc3408SPatrick Mooney 		ASSERT(vp->v_path == vn_vpath_empty);
3193e2fc3408SPatrick Mooney 		vp->v_path_stamp = pstamp;
3194e2fc3408SPatrick Mooney 		vp->v_path = buf;
3195e2fc3408SPatrick Mooney 		mutex_exit(&vp->v_lock);
3196e2fc3408SPatrick Mooney 	} else if (vp->v_path_stamp < pstamp || is_rename) {
3197e2fc3408SPatrick Mooney 		/*
3198e2fc3408SPatrick Mooney 		 * Install the updated path and stamp, ensuring that the v_path
3199e2fc3408SPatrick Mooney 		 * pointer is valid at all times for dtrace.
3200e2fc3408SPatrick Mooney 		 */
3201e2fc3408SPatrick Mooney 		oldbuf = vp->v_path;
3202e2fc3408SPatrick Mooney 		vp->v_path = buf;
3203e2fc3408SPatrick Mooney 		vp->v_path_stamp = gethrtime();
32047c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
3205e2fc3408SPatrick Mooney 		kmem_free(oldbuf, strlen(oldbuf) + 1);
32067c478bd9Sstevel@tonic-gate 	} else {
3207e2fc3408SPatrick Mooney 		/*
3208e2fc3408SPatrick Mooney 		 * If the timestamp matches or is greater, it means another
3209e2fc3408SPatrick Mooney 		 * thread performed the update first while locks were dropped
3210e2fc3408SPatrick Mooney 		 * here to make the allocation.  We defer to the newer value.
3211e2fc3408SPatrick Mooney 		 */
32127c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
3213e2fc3408SPatrick Mooney 		kmem_free(buf, buflen);
32147c478bd9Sstevel@tonic-gate 	}
3215e2fc3408SPatrick Mooney 	ASSERT(MUTEX_NOT_HELD(&vp->v_lock));
32167c478bd9Sstevel@tonic-gate }
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate void
vn_updatepath(vnode_t * pvp,vnode_t * vp,const char * name)3219e2fc3408SPatrick Mooney vn_updatepath(vnode_t *pvp, vnode_t *vp, const char *name)
32207c478bd9Sstevel@tonic-gate {
3221e2fc3408SPatrick Mooney 	size_t len;
32227c478bd9Sstevel@tonic-gate 
3223e2fc3408SPatrick Mooney 	/*
3224e2fc3408SPatrick Mooney 	 * If the parent is older or empty, there's nothing further to do.
3225e2fc3408SPatrick Mooney 	 */
3226e2fc3408SPatrick Mooney 	if (pvp->v_path == vn_vpath_empty ||
3227e2fc3408SPatrick Mooney 	    pvp->v_path_stamp <= vp->v_path_stamp) {
32287c478bd9Sstevel@tonic-gate 		return;
32297c478bd9Sstevel@tonic-gate 	}
32307c478bd9Sstevel@tonic-gate 
3231e2fc3408SPatrick Mooney 	/*
3232e2fc3408SPatrick Mooney 	 * Given the lack of appropriate context, meaningful updates to v_path
3233e2fc3408SPatrick Mooney 	 * cannot be made for during lookups for the '.' or '..' entries.
3234e2fc3408SPatrick Mooney 	 */
3235e2fc3408SPatrick Mooney 	len = strlen(name);
3236e2fc3408SPatrick Mooney 	if (len == 0 || (len == 1 && name[0] == '.') ||
3237e2fc3408SPatrick Mooney 	    (len == 2 && name[0] == '.' && name[1] == '.')) {
3238e2fc3408SPatrick Mooney 		return;
3239e2fc3408SPatrick Mooney 	}
32407c478bd9Sstevel@tonic-gate 
3241e2fc3408SPatrick Mooney 	vn_setpath_common(pvp, vp, name, len, B_FALSE);
3242e2fc3408SPatrick Mooney }
3243e2fc3408SPatrick Mooney 
3244e2fc3408SPatrick Mooney /*
3245e2fc3408SPatrick Mooney  * Given a starting vnode and a path, updates the path in the target vnode in
3246e2fc3408SPatrick Mooney  * a safe manner.  If the vnode already has path information embedded, then the
3247e2fc3408SPatrick Mooney  * cached path is left untouched.
3248e2fc3408SPatrick Mooney  */
3249e2fc3408SPatrick Mooney /* ARGSUSED */
3250e2fc3408SPatrick Mooney void
vn_setpath(vnode_t * rootvp,vnode_t * pvp,vnode_t * vp,const char * name,size_t len)3251e2fc3408SPatrick Mooney vn_setpath(vnode_t *rootvp, vnode_t *pvp, vnode_t *vp, const char *name,
3252e2fc3408SPatrick Mooney     size_t len)
3253e2fc3408SPatrick Mooney {
3254e2fc3408SPatrick Mooney 	vn_setpath_common(pvp, vp, name, len, B_FALSE);
3255e2fc3408SPatrick Mooney }
3256e2fc3408SPatrick Mooney 
3257e2fc3408SPatrick Mooney /*
3258e2fc3408SPatrick Mooney  * Sets the path to the vnode to be the given string, regardless of current
3259e2fc3408SPatrick Mooney  * context.  The string must be a complete path from rootdir.  This is only used
3260e2fc3408SPatrick Mooney  * by fsop_root() for setting the path based on the mountpoint.
3261e2fc3408SPatrick Mooney  */
3262e2fc3408SPatrick Mooney void
vn_setpath_str(vnode_t * vp,const char * str,size_t len)3263e2fc3408SPatrick Mooney vn_setpath_str(vnode_t *vp, const char *str, size_t len)
3264e2fc3408SPatrick Mooney {
3265e2fc3408SPatrick Mooney 	vn_setpath_common(NULL, vp, str, len, B_FALSE);
32667c478bd9Sstevel@tonic-gate }
32677c478bd9Sstevel@tonic-gate 
326851ece835Seschrock /*
326951ece835Seschrock  * Called from within filesystem's vop_rename() to handle renames once the
327051ece835Seschrock  * target vnode is available.
327151ece835Seschrock  */
327251ece835Seschrock void
vn_renamepath(vnode_t * pvp,vnode_t * vp,const char * name,size_t len)3273e2fc3408SPatrick Mooney vn_renamepath(vnode_t *pvp, vnode_t *vp, const char *name, size_t len)
327451ece835Seschrock {
3275e2fc3408SPatrick Mooney 	vn_setpath_common(pvp, vp, name, len, B_TRUE);
327651ece835Seschrock }
327751ece835Seschrock 
32787c478bd9Sstevel@tonic-gate /*
32797c478bd9Sstevel@tonic-gate  * Similar to vn_setpath_str(), this function sets the path of the destination
32807c478bd9Sstevel@tonic-gate  * vnode to the be the same as the source vnode.
32817c478bd9Sstevel@tonic-gate  */
32827c478bd9Sstevel@tonic-gate void
vn_copypath(struct vnode * src,struct vnode * dst)32837c478bd9Sstevel@tonic-gate vn_copypath(struct vnode *src, struct vnode *dst)
32847c478bd9Sstevel@tonic-gate {
32857c478bd9Sstevel@tonic-gate 	char *buf;
3286e2fc3408SPatrick Mooney 	hrtime_t stamp;
3287e2fc3408SPatrick Mooney 	size_t buflen;
32887c478bd9Sstevel@tonic-gate 
32897c478bd9Sstevel@tonic-gate 	mutex_enter(&src->v_lock);
3290e2fc3408SPatrick Mooney 	if (src->v_path == vn_vpath_empty) {
32917c478bd9Sstevel@tonic-gate 		mutex_exit(&src->v_lock);
32927c478bd9Sstevel@tonic-gate 		return;
32937c478bd9Sstevel@tonic-gate 	}
3294e2fc3408SPatrick Mooney 	buflen = strlen(src->v_path) + 1;
32957c478bd9Sstevel@tonic-gate 	mutex_exit(&src->v_lock);
3296e2fc3408SPatrick Mooney 
3297e2fc3408SPatrick Mooney 	buf = kmem_alloc(buflen, KM_SLEEP);
3298e2fc3408SPatrick Mooney 
32997c478bd9Sstevel@tonic-gate 	mutex_enter(&src->v_lock);
3300e2fc3408SPatrick Mooney 	if (src->v_path == vn_vpath_empty ||
3301e2fc3408SPatrick Mooney 	    strlen(src->v_path) + 1 != buflen) {
33027c478bd9Sstevel@tonic-gate 		mutex_exit(&src->v_lock);
3303e2fc3408SPatrick Mooney 		kmem_free(buf, buflen);
33047c478bd9Sstevel@tonic-gate 		return;
33057c478bd9Sstevel@tonic-gate 	}
3306e2fc3408SPatrick Mooney 	bcopy(src->v_path, buf, buflen);
3307e2fc3408SPatrick Mooney 	stamp = src->v_path_stamp;
33087c478bd9Sstevel@tonic-gate 	mutex_exit(&src->v_lock);
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	mutex_enter(&dst->v_lock);
3311e2fc3408SPatrick Mooney 	if (dst->v_path != vn_vpath_empty) {
33127c478bd9Sstevel@tonic-gate 		mutex_exit(&dst->v_lock);
3313e2fc3408SPatrick Mooney 		kmem_free(buf, buflen);
33147c478bd9Sstevel@tonic-gate 		return;
33157c478bd9Sstevel@tonic-gate 	}
33167c478bd9Sstevel@tonic-gate 	dst->v_path = buf;
3317e2fc3408SPatrick Mooney 	dst->v_path_stamp = stamp;
33187c478bd9Sstevel@tonic-gate 	mutex_exit(&dst->v_lock);
33197c478bd9Sstevel@tonic-gate }
33207c478bd9Sstevel@tonic-gate 
3321e2fc3408SPatrick Mooney 
33227c478bd9Sstevel@tonic-gate /*
33237c478bd9Sstevel@tonic-gate  * XXX Private interface for segvn routines that handle vnode
33247c478bd9Sstevel@tonic-gate  * large page segments.
33257c478bd9Sstevel@tonic-gate  *
33267c478bd9Sstevel@tonic-gate  * return 1 if vp's file system VOP_PAGEIO() implementation
33277c478bd9Sstevel@tonic-gate  * can be safely used instead of VOP_GETPAGE() for handling
33287c478bd9Sstevel@tonic-gate  * pagefaults against regular non swap files. VOP_PAGEIO()
33297c478bd9Sstevel@tonic-gate  * interface is considered safe here if its implementation
33307c478bd9Sstevel@tonic-gate  * is very close to VOP_GETPAGE() implementation.
33317c478bd9Sstevel@tonic-gate  * e.g. It zero's out the part of the page beyond EOF. Doesn't
33327c478bd9Sstevel@tonic-gate  * panic if there're file holes but instead returns an error.
33337c478bd9Sstevel@tonic-gate  * Doesn't assume file won't be changed by user writes, etc.
33347c478bd9Sstevel@tonic-gate  *
33357c478bd9Sstevel@tonic-gate  * return 0 otherwise.
33367c478bd9Sstevel@tonic-gate  *
33377c478bd9Sstevel@tonic-gate  * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs.
33387c478bd9Sstevel@tonic-gate  */
33397c478bd9Sstevel@tonic-gate int
vn_vmpss_usepageio(vnode_t * vp)33407c478bd9Sstevel@tonic-gate vn_vmpss_usepageio(vnode_t *vp)
33417c478bd9Sstevel@tonic-gate {
33427c478bd9Sstevel@tonic-gate 	vfs_t   *vfsp = vp->v_vfsp;
33437c478bd9Sstevel@tonic-gate 	char *fsname = vfssw[vfsp->vfs_fstype].vsw_name;
33447c478bd9Sstevel@tonic-gate 	char *pageio_ok_fss[] = {"ufs", "nfs", NULL};
33457c478bd9Sstevel@tonic-gate 	char **fsok = pageio_ok_fss;
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate 	if (fsname == NULL) {
33487c478bd9Sstevel@tonic-gate 		return (0);
33497c478bd9Sstevel@tonic-gate 	}
33507c478bd9Sstevel@tonic-gate 
33517c478bd9Sstevel@tonic-gate 	for (; *fsok; fsok++) {
33527c478bd9Sstevel@tonic-gate 		if (strcmp(*fsok, fsname) == 0) {
33537c478bd9Sstevel@tonic-gate 			return (1);
33547c478bd9Sstevel@tonic-gate 		}
33557c478bd9Sstevel@tonic-gate 	}
33567c478bd9Sstevel@tonic-gate 	return (0);
33577c478bd9Sstevel@tonic-gate }
33587c478bd9Sstevel@tonic-gate 
33597c478bd9Sstevel@tonic-gate /* VOP_XXX() macros call the corresponding fop_xxx() function */
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate int
fop_open(vnode_t ** vpp,int mode,cred_t * cr,caller_context_t * ct)33627c478bd9Sstevel@tonic-gate fop_open(
33637c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
33647c478bd9Sstevel@tonic-gate 	int mode,
3365da6c28aaSamw 	cred_t *cr,
3366da6c28aaSamw 	caller_context_t *ct)
33677c478bd9Sstevel@tonic-gate {
33687c478bd9Sstevel@tonic-gate 	int ret;
33697c478bd9Sstevel@tonic-gate 	vnode_t *vp = *vpp;
33707c478bd9Sstevel@tonic-gate 
33717c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
33727c478bd9Sstevel@tonic-gate 	/*
33737c478bd9Sstevel@tonic-gate 	 * Adding to the vnode counts before calling open
33747c478bd9Sstevel@tonic-gate 	 * avoids the need for a mutex. It circumvents a race
33757c478bd9Sstevel@tonic-gate 	 * condition where a query made on the vnode counts results in a
33767c478bd9Sstevel@tonic-gate 	 * false negative. The inquirer goes away believing the file is
33777c478bd9Sstevel@tonic-gate 	 * not open when there is an open on the file already under way.
33787c478bd9Sstevel@tonic-gate 	 *
33797c478bd9Sstevel@tonic-gate 	 * The counts are meant to prevent NFS from granting a delegation
33807c478bd9Sstevel@tonic-gate 	 * when it would be dangerous to do so.
33817c478bd9Sstevel@tonic-gate 	 *
33827c478bd9Sstevel@tonic-gate 	 * The vnode counts are only kept on regular files
33837c478bd9Sstevel@tonic-gate 	 */
33847c478bd9Sstevel@tonic-gate 	if ((*vpp)->v_type == VREG) {
33857c478bd9Sstevel@tonic-gate 		if (mode & FREAD)
33861a5e258fSJosef 'Jeff' Sipek 			atomic_inc_32(&(*vpp)->v_rdcnt);
33877c478bd9Sstevel@tonic-gate 		if (mode & FWRITE)
33881a5e258fSJosef 'Jeff' Sipek 			atomic_inc_32(&(*vpp)->v_wrcnt);
33897c478bd9Sstevel@tonic-gate 	}
33907c478bd9Sstevel@tonic-gate 
3391f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3392f48205beScasper 
3393da6c28aaSamw 	ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr, ct);
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	if (ret) {
33967c478bd9Sstevel@tonic-gate 		/*
33977c478bd9Sstevel@tonic-gate 		 * Use the saved vp just in case the vnode ptr got trashed
33987c478bd9Sstevel@tonic-gate 		 * by the error.
33997c478bd9Sstevel@tonic-gate 		 */
34002bb1cb30Sbmc 		VOPSTATS_UPDATE(vp, open);
34017c478bd9Sstevel@tonic-gate 		if ((vp->v_type == VREG) && (mode & FREAD))
34021a5e258fSJosef 'Jeff' Sipek 			atomic_dec_32(&vp->v_rdcnt);
34037c478bd9Sstevel@tonic-gate 		if ((vp->v_type == VREG) && (mode & FWRITE))
34041a5e258fSJosef 'Jeff' Sipek 			atomic_dec_32(&vp->v_wrcnt);
34057c478bd9Sstevel@tonic-gate 	} else {
34067c478bd9Sstevel@tonic-gate 		/*
34077c478bd9Sstevel@tonic-gate 		 * Some filesystems will return a different vnode,
34087c478bd9Sstevel@tonic-gate 		 * but the same path was still used to open it.
34097c478bd9Sstevel@tonic-gate 		 * So if we do change the vnode and need to
34107c478bd9Sstevel@tonic-gate 		 * copy over the path, do so here, rather than special
34117c478bd9Sstevel@tonic-gate 		 * casing each filesystem. Adjust the vnode counts to
34127c478bd9Sstevel@tonic-gate 		 * reflect the vnode switch.
34137c478bd9Sstevel@tonic-gate 		 */
34142bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, open);
3415f19903c6SJohn Levon 		if (*vpp != vp) {
3416ca2c3138Seschrock 			vn_copypath(vp, *vpp);
3417ca2c3138Seschrock 			if (((*vpp)->v_type == VREG) && (mode & FREAD))
34181a5e258fSJosef 'Jeff' Sipek 				atomic_inc_32(&(*vpp)->v_rdcnt);
3419ca2c3138Seschrock 			if ((vp->v_type == VREG) && (mode & FREAD))
34201a5e258fSJosef 'Jeff' Sipek 				atomic_dec_32(&vp->v_rdcnt);
3421ca2c3138Seschrock 			if (((*vpp)->v_type == VREG) && (mode & FWRITE))
34221a5e258fSJosef 'Jeff' Sipek 				atomic_inc_32(&(*vpp)->v_wrcnt);
3423ca2c3138Seschrock 			if ((vp->v_type == VREG) && (mode & FWRITE))
34241a5e258fSJosef 'Jeff' Sipek 				atomic_dec_32(&vp->v_wrcnt);
34257c478bd9Sstevel@tonic-gate 		}
34267c478bd9Sstevel@tonic-gate 	}
34277c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
34287c478bd9Sstevel@tonic-gate 	return (ret);
34297c478bd9Sstevel@tonic-gate }
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate int
fop_close(vnode_t * vp,int flag,int count,offset_t offset,cred_t * cr,caller_context_t * ct)34327c478bd9Sstevel@tonic-gate fop_close(
34337c478bd9Sstevel@tonic-gate 	vnode_t *vp,
34347c478bd9Sstevel@tonic-gate 	int flag,
34357c478bd9Sstevel@tonic-gate 	int count,
34367c478bd9Sstevel@tonic-gate 	offset_t offset,
3437da6c28aaSamw 	cred_t *cr,
3438da6c28aaSamw 	caller_context_t *ct)
34397c478bd9Sstevel@tonic-gate {
34405a59a8b3Srsb 	int err;
34415a59a8b3Srsb 
3442f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3443f48205beScasper 
3444da6c28aaSamw 	err = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr, ct);
34452bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, close);
34467c478bd9Sstevel@tonic-gate 	/*
34477c478bd9Sstevel@tonic-gate 	 * Check passed in count to handle possible dups. Vnode counts are only
34487c478bd9Sstevel@tonic-gate 	 * kept on regular files
34497c478bd9Sstevel@tonic-gate 	 */
34507c478bd9Sstevel@tonic-gate 	if ((vp->v_type == VREG) && (count == 1))  {
34517c478bd9Sstevel@tonic-gate 		if (flag & FREAD) {
34527c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_rdcnt > 0);
34531a5e258fSJosef 'Jeff' Sipek 			atomic_dec_32(&vp->v_rdcnt);
34547c478bd9Sstevel@tonic-gate 		}
34557c478bd9Sstevel@tonic-gate 		if (flag & FWRITE) {
34567c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_wrcnt > 0);
34571a5e258fSJosef 'Jeff' Sipek 			atomic_dec_32(&vp->v_wrcnt);
34587c478bd9Sstevel@tonic-gate 		}
34597c478bd9Sstevel@tonic-gate 	}
34605a59a8b3Srsb 	return (err);
34617c478bd9Sstevel@tonic-gate }
34627c478bd9Sstevel@tonic-gate 
34637c478bd9Sstevel@tonic-gate int
fop_read(vnode_t * vp,uio_t * uiop,int ioflag,cred_t * cr,caller_context_t * ct)34647c478bd9Sstevel@tonic-gate fop_read(
34657c478bd9Sstevel@tonic-gate 	vnode_t *vp,
34667c478bd9Sstevel@tonic-gate 	uio_t *uiop,
34677c478bd9Sstevel@tonic-gate 	int ioflag,
34687c478bd9Sstevel@tonic-gate 	cred_t *cr,
3469da6c28aaSamw 	caller_context_t *ct)
34707c478bd9Sstevel@tonic-gate {
34715a59a8b3Srsb 	int	err;
34725a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
34735a59a8b3Srsb 
3474f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3475f48205beScasper 
34765a59a8b3Srsb 	err = (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct);
34772bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, read,
34785a59a8b3Srsb 	    read_bytes, (resid_start - uiop->uio_resid));
34795a59a8b3Srsb 	return (err);
34807c478bd9Sstevel@tonic-gate }
34817c478bd9Sstevel@tonic-gate 
34827c478bd9Sstevel@tonic-gate int
fop_write(vnode_t * vp,uio_t * uiop,int ioflag,cred_t * cr,caller_context_t * ct)34837c478bd9Sstevel@tonic-gate fop_write(
34847c478bd9Sstevel@tonic-gate 	vnode_t *vp,
34857c478bd9Sstevel@tonic-gate 	uio_t *uiop,
34867c478bd9Sstevel@tonic-gate 	int ioflag,
34877c478bd9Sstevel@tonic-gate 	cred_t *cr,
3488da6c28aaSamw 	caller_context_t *ct)
34897c478bd9Sstevel@tonic-gate {
34905a59a8b3Srsb 	int	err;
34915a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
34925a59a8b3Srsb 
3493f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3494f48205beScasper 
34955a59a8b3Srsb 	err = (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct);
34962bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, write,
34975a59a8b3Srsb 	    write_bytes, (resid_start - uiop->uio_resid));
34985a59a8b3Srsb 	return (err);
34997c478bd9Sstevel@tonic-gate }
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate int
fop_ioctl(vnode_t * vp,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp,caller_context_t * ct)35027c478bd9Sstevel@tonic-gate fop_ioctl(
35037c478bd9Sstevel@tonic-gate 	vnode_t *vp,
35047c478bd9Sstevel@tonic-gate 	int cmd,
35057c478bd9Sstevel@tonic-gate 	intptr_t arg,
35067c478bd9Sstevel@tonic-gate 	int flag,
35077c478bd9Sstevel@tonic-gate 	cred_t *cr,
3508da6c28aaSamw 	int *rvalp,
3509da6c28aaSamw 	caller_context_t *ct)
35107c478bd9Sstevel@tonic-gate {
35115a59a8b3Srsb 	int	err;
35125a59a8b3Srsb 
3513f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3514f48205beScasper 
3515da6c28aaSamw 	err = (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp, ct);
35162bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, ioctl);
35175a59a8b3Srsb 	return (err);
35187c478bd9Sstevel@tonic-gate }
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate int
fop_setfl(vnode_t * vp,int oflags,int nflags,cred_t * cr,caller_context_t * ct)35217c478bd9Sstevel@tonic-gate fop_setfl(
35227c478bd9Sstevel@tonic-gate 	vnode_t *vp,
35237c478bd9Sstevel@tonic-gate 	int oflags,
35247c478bd9Sstevel@tonic-gate 	int nflags,
3525da6c28aaSamw 	cred_t *cr,
3526da6c28aaSamw 	caller_context_t *ct)
35277c478bd9Sstevel@tonic-gate {
35285a59a8b3Srsb 	int	err;
35295a59a8b3Srsb 
3530f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3531f48205beScasper 
3532da6c28aaSamw 	err = (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr, ct);
35332bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setfl);
35345a59a8b3Srsb 	return (err);
35357c478bd9Sstevel@tonic-gate }
35367c478bd9Sstevel@tonic-gate 
35377c478bd9Sstevel@tonic-gate int
fop_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)35387c478bd9Sstevel@tonic-gate fop_getattr(
35397c478bd9Sstevel@tonic-gate 	vnode_t *vp,
35407c478bd9Sstevel@tonic-gate 	vattr_t *vap,
35417c478bd9Sstevel@tonic-gate 	int flags,
3542da6c28aaSamw 	cred_t *cr,
3543da6c28aaSamw 	caller_context_t *ct)
35447c478bd9Sstevel@tonic-gate {
35455a59a8b3Srsb 	int	err;
35465a59a8b3Srsb 
3547f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3548f48205beScasper 
3549da6c28aaSamw 	/*
3550da6c28aaSamw 	 * If this file system doesn't understand the xvattr extensions
3551da6c28aaSamw 	 * then turn off the xvattr bit.
3552da6c28aaSamw 	 */
3553da6c28aaSamw 	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3554da6c28aaSamw 		vap->va_mask &= ~AT_XVATTR;
3555da6c28aaSamw 	}
3556da6c28aaSamw 
3557da6c28aaSamw 	/*
3558da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
3559da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
3560da6c28aaSamw 	 */
3561da6c28aaSamw 	if ((flags & ATTR_NOACLCHECK) &&
3562da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3563da6c28aaSamw 		return (EINVAL);
3564da6c28aaSamw 	}
3565da6c28aaSamw 	err = (*(vp)->v_op->vop_getattr)(vp, vap, flags, cr, ct);
35662bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getattr);
35675a59a8b3Srsb 	return (err);
35687c478bd9Sstevel@tonic-gate }
35697c478bd9Sstevel@tonic-gate 
35707c478bd9Sstevel@tonic-gate int
fop_setattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)35717c478bd9Sstevel@tonic-gate fop_setattr(
35727c478bd9Sstevel@tonic-gate 	vnode_t *vp,
35737c478bd9Sstevel@tonic-gate 	vattr_t *vap,
35747c478bd9Sstevel@tonic-gate 	int flags,
35757c478bd9Sstevel@tonic-gate 	cred_t *cr,
35767c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
35777c478bd9Sstevel@tonic-gate {
35785a59a8b3Srsb 	int	err;
35795a59a8b3Srsb 
3580f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3581f48205beScasper 
3582da6c28aaSamw 	/*
3583da6c28aaSamw 	 * If this file system doesn't understand the xvattr extensions
3584da6c28aaSamw 	 * then turn off the xvattr bit.
3585da6c28aaSamw 	 */
3586da6c28aaSamw 	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3587da6c28aaSamw 		vap->va_mask &= ~AT_XVATTR;
3588da6c28aaSamw 	}
3589da6c28aaSamw 
3590da6c28aaSamw 	/*
3591da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
3592da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
3593da6c28aaSamw 	 */
3594da6c28aaSamw 	if ((flags & ATTR_NOACLCHECK) &&
3595da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3596da6c28aaSamw 		return (EINVAL);
3597da6c28aaSamw 	}
35985a59a8b3Srsb 	err = (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct);
35992bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setattr);
36005a59a8b3Srsb 	return (err);
36017c478bd9Sstevel@tonic-gate }
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate int
fop_access(vnode_t * vp,int mode,int flags,cred_t * cr,caller_context_t * ct)36047c478bd9Sstevel@tonic-gate fop_access(
36057c478bd9Sstevel@tonic-gate 	vnode_t *vp,
36067c478bd9Sstevel@tonic-gate 	int mode,
36077c478bd9Sstevel@tonic-gate 	int flags,
3608da6c28aaSamw 	cred_t *cr,
3609da6c28aaSamw 	caller_context_t *ct)
36107c478bd9Sstevel@tonic-gate {
36115a59a8b3Srsb 	int	err;
36125a59a8b3Srsb 
3613da6c28aaSamw 	if ((flags & V_ACE_MASK) &&
3614da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3615da6c28aaSamw 		return (EINVAL);
3616da6c28aaSamw 	}
3617da6c28aaSamw 
3618f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3619f48205beScasper 
3620da6c28aaSamw 	err = (*(vp)->v_op->vop_access)(vp, mode, flags, cr, ct);
36212bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, access);
36225a59a8b3Srsb 	return (err);
36237c478bd9Sstevel@tonic-gate }
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate int
fop_lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,pathname_t * pnp,int flags,vnode_t * rdir,cred_t * cr,caller_context_t * ct,int * deflags,pathname_t * ppnp)36267c478bd9Sstevel@tonic-gate fop_lookup(
36277c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
36287c478bd9Sstevel@tonic-gate 	char *nm,
36297c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
36307c478bd9Sstevel@tonic-gate 	pathname_t *pnp,
36317c478bd9Sstevel@tonic-gate 	int flags,
36327c478bd9Sstevel@tonic-gate 	vnode_t *rdir,
3633da6c28aaSamw 	cred_t *cr,
3634da6c28aaSamw 	caller_context_t *ct,
3635da6c28aaSamw 	int *deflags,		/* Returned per-dirent flags */
3636da6c28aaSamw 	pathname_t *ppnp)	/* Returned case-preserved name in directory */
36377c478bd9Sstevel@tonic-gate {
3638ca2c3138Seschrock 	int ret;
3639ca2c3138Seschrock 
3640da6c28aaSamw 	/*
3641da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3642da6c28aaSamw 	 * and said access is requested, fail quickly.  It is required
3643da6c28aaSamw 	 * that if the vfs supports case-insensitive lookup, it also
3644da6c28aaSamw 	 * supports extended dirent flags.
3645da6c28aaSamw 	 */
3646da6c28aaSamw 	if (flags & FIGNORECASE &&
3647da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3648da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3649da6c28aaSamw 		return (EINVAL);
3650da6c28aaSamw 
3651f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3652f48205beScasper 
3653da6c28aaSamw 	if ((flags & LOOKUP_XATTR) && (flags & LOOKUP_HAVE_SYSATTR_DIR) == 0) {
3654da6c28aaSamw 		ret = xattr_dir_lookup(dvp, vpp, flags, cr);
3655da6c28aaSamw 	} else {
3656da6c28aaSamw 		ret = (*(dvp)->v_op->vop_lookup)
3657da6c28aaSamw 		    (dvp, nm, vpp, pnp, flags, rdir, cr, ct, deflags, ppnp);
3658da6c28aaSamw 	}
36595a59a8b3Srsb 	if (ret == 0 && *vpp) {
36602bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, lookup);
3661e2fc3408SPatrick Mooney 		vn_updatepath(dvp, *vpp, nm);
36625a59a8b3Srsb 	}
3663ca2c3138Seschrock 
3664ca2c3138Seschrock 	return (ret);
36657c478bd9Sstevel@tonic-gate }
36667c478bd9Sstevel@tonic-gate 
36677c478bd9Sstevel@tonic-gate int
fop_create(vnode_t * dvp,char * name,vattr_t * vap,vcexcl_t excl,int mode,vnode_t ** vpp,cred_t * cr,int flags,caller_context_t * ct,vsecattr_t * vsecp)36687c478bd9Sstevel@tonic-gate fop_create(
36697c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
36707c478bd9Sstevel@tonic-gate 	char *name,
36717c478bd9Sstevel@tonic-gate 	vattr_t *vap,
36727c478bd9Sstevel@tonic-gate 	vcexcl_t excl,
36737c478bd9Sstevel@tonic-gate 	int mode,
36747c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
36757c478bd9Sstevel@tonic-gate 	cred_t *cr,
3676da6c28aaSamw 	int flags,
3677da6c28aaSamw 	caller_context_t *ct,
3678da6c28aaSamw 	vsecattr_t *vsecp)	/* ACL to set during create */
36797c478bd9Sstevel@tonic-gate {
36807c478bd9Sstevel@tonic-gate 	int ret;
36817c478bd9Sstevel@tonic-gate 
3682da6c28aaSamw 	if (vsecp != NULL &&
3683da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3684da6c28aaSamw 		return (EINVAL);
3685da6c28aaSamw 	}
3686da6c28aaSamw 	/*
3687da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3688da6c28aaSamw 	 * and said access is requested, fail quickly.
3689da6c28aaSamw 	 */
3690da6c28aaSamw 	if (flags & FIGNORECASE &&
3691da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3692da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3693da6c28aaSamw 		return (EINVAL);
3694da6c28aaSamw 
3695f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3696f48205beScasper 
36977c478bd9Sstevel@tonic-gate 	ret = (*(dvp)->v_op->vop_create)
3698da6c28aaSamw 	    (dvp, name, vap, excl, mode, vpp, cr, flags, ct, vsecp);
36995a59a8b3Srsb 	if (ret == 0 && *vpp) {
37002bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, create);
3701e2fc3408SPatrick Mooney 		vn_updatepath(dvp, *vpp, name);
37025a59a8b3Srsb 	}
37037c478bd9Sstevel@tonic-gate 
37047c478bd9Sstevel@tonic-gate 	return (ret);
37057c478bd9Sstevel@tonic-gate }
37067c478bd9Sstevel@tonic-gate 
37077c478bd9Sstevel@tonic-gate int
fop_remove(vnode_t * dvp,char * nm,cred_t * cr,caller_context_t * ct,int flags)37087c478bd9Sstevel@tonic-gate fop_remove(
37097c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
37107c478bd9Sstevel@tonic-gate 	char *nm,
3711da6c28aaSamw 	cred_t *cr,
3712da6c28aaSamw 	caller_context_t *ct,
3713da6c28aaSamw 	int flags)
37147c478bd9Sstevel@tonic-gate {
37155a59a8b3Srsb 	int	err;
37165a59a8b3Srsb 
3717da6c28aaSamw 	/*
3718da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3719da6c28aaSamw 	 * and said access is requested, fail quickly.
3720da6c28aaSamw 	 */
3721da6c28aaSamw 	if (flags & FIGNORECASE &&
3722da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3723da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3724da6c28aaSamw 		return (EINVAL);
3725da6c28aaSamw 
3726f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3727f48205beScasper 
3728da6c28aaSamw 	err = (*(dvp)->v_op->vop_remove)(dvp, nm, cr, ct, flags);
37292bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, remove);
37305a59a8b3Srsb 	return (err);
37317c478bd9Sstevel@tonic-gate }
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate int
fop_link(vnode_t * tdvp,vnode_t * svp,char * tnm,cred_t * cr,caller_context_t * ct,int flags)37347c478bd9Sstevel@tonic-gate fop_link(
37357c478bd9Sstevel@tonic-gate 	vnode_t *tdvp,
37367c478bd9Sstevel@tonic-gate 	vnode_t *svp,
37377c478bd9Sstevel@tonic-gate 	char *tnm,
3738da6c28aaSamw 	cred_t *cr,
3739da6c28aaSamw 	caller_context_t *ct,
3740da6c28aaSamw 	int flags)
37417c478bd9Sstevel@tonic-gate {
37425a59a8b3Srsb 	int	err;
37435a59a8b3Srsb 
3744da6c28aaSamw 	/*
3745da6c28aaSamw 	 * If the target file system doesn't support case-insensitive access
3746da6c28aaSamw 	 * and said access is requested, fail quickly.
3747da6c28aaSamw 	 */
3748da6c28aaSamw 	if (flags & FIGNORECASE &&
3749da6c28aaSamw 	    (vfs_has_feature(tdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3750da6c28aaSamw 	    vfs_has_feature(tdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3751da6c28aaSamw 		return (EINVAL);
3752da6c28aaSamw 
3753f48205beScasper 	VOPXID_MAP_CR(tdvp, cr);
3754f48205beScasper 
3755da6c28aaSamw 	err = (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr, ct, flags);
37562bb1cb30Sbmc 	VOPSTATS_UPDATE(tdvp, link);
37575a59a8b3Srsb 	return (err);
37587c478bd9Sstevel@tonic-gate }
37597c478bd9Sstevel@tonic-gate 
37607c478bd9Sstevel@tonic-gate int
fop_rename(vnode_t * sdvp,char * snm,vnode_t * tdvp,char * tnm,cred_t * cr,caller_context_t * ct,int flags)37617c478bd9Sstevel@tonic-gate fop_rename(
37627c478bd9Sstevel@tonic-gate 	vnode_t *sdvp,
37637c478bd9Sstevel@tonic-gate 	char *snm,
37647c478bd9Sstevel@tonic-gate 	vnode_t *tdvp,
37657c478bd9Sstevel@tonic-gate 	char *tnm,
3766da6c28aaSamw 	cred_t *cr,
3767da6c28aaSamw 	caller_context_t *ct,
3768da6c28aaSamw 	int flags)
37697c478bd9Sstevel@tonic-gate {
37705a59a8b3Srsb 	int	err;
37715a59a8b3Srsb 
3772da6c28aaSamw 	/*
3773da6c28aaSamw 	 * If the file system involved does not support
3774da6c28aaSamw 	 * case-insensitive access and said access is requested, fail
3775da6c28aaSamw 	 * quickly.
3776da6c28aaSamw 	 */
3777da6c28aaSamw 	if (flags & FIGNORECASE &&
3778da6c28aaSamw 	    ((vfs_has_feature(sdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3779da6c28aaSamw 	    vfs_has_feature(sdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0)))
3780da6c28aaSamw 		return (EINVAL);
3781da6c28aaSamw 
3782f48205beScasper 	VOPXID_MAP_CR(tdvp, cr);
3783f48205beScasper 
3784da6c28aaSamw 	err = (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr, ct, flags);
37852bb1cb30Sbmc 	VOPSTATS_UPDATE(sdvp, rename);
37865a59a8b3Srsb 	return (err);
37877c478bd9Sstevel@tonic-gate }
37887c478bd9Sstevel@tonic-gate 
37897c478bd9Sstevel@tonic-gate int
fop_mkdir(vnode_t * dvp,char * dirname,vattr_t * vap,vnode_t ** vpp,cred_t * cr,caller_context_t * ct,int flags,vsecattr_t * vsecp)37907c478bd9Sstevel@tonic-gate fop_mkdir(
37917c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
37927c478bd9Sstevel@tonic-gate 	char *dirname,
37937c478bd9Sstevel@tonic-gate 	vattr_t *vap,
37947c478bd9Sstevel@tonic-gate 	vnode_t **vpp,
3795da6c28aaSamw 	cred_t *cr,
3796da6c28aaSamw 	caller_context_t *ct,
3797da6c28aaSamw 	int flags,
3798da6c28aaSamw 	vsecattr_t *vsecp)	/* ACL to set during create */
37997c478bd9Sstevel@tonic-gate {
38007c478bd9Sstevel@tonic-gate 	int ret;
38017c478bd9Sstevel@tonic-gate 
3802da6c28aaSamw 	if (vsecp != NULL &&
3803da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3804da6c28aaSamw 		return (EINVAL);
3805da6c28aaSamw 	}
3806da6c28aaSamw 	/*
3807da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3808da6c28aaSamw 	 * and said access is requested, fail quickly.
3809da6c28aaSamw 	 */
3810da6c28aaSamw 	if (flags & FIGNORECASE &&
3811da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3812da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3813da6c28aaSamw 		return (EINVAL);
3814da6c28aaSamw 
3815f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3816f48205beScasper 
3817da6c28aaSamw 	ret = (*(dvp)->v_op->vop_mkdir)
3818da6c28aaSamw 	    (dvp, dirname, vap, vpp, cr, ct, flags, vsecp);
38195a59a8b3Srsb 	if (ret == 0 && *vpp) {
38202bb1cb30Sbmc 		VOPSTATS_UPDATE(*vpp, mkdir);
3821e2fc3408SPatrick Mooney 		vn_updatepath(dvp, *vpp, dirname);
38225a59a8b3Srsb 	}
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate 	return (ret);
38257c478bd9Sstevel@tonic-gate }
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate int
fop_rmdir(vnode_t * dvp,char * nm,vnode_t * cdir,cred_t * cr,caller_context_t * ct,int flags)38287c478bd9Sstevel@tonic-gate fop_rmdir(
38297c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
38307c478bd9Sstevel@tonic-gate 	char *nm,
38317c478bd9Sstevel@tonic-gate 	vnode_t *cdir,
3832da6c28aaSamw 	cred_t *cr,
3833da6c28aaSamw 	caller_context_t *ct,
3834da6c28aaSamw 	int flags)
38357c478bd9Sstevel@tonic-gate {
38365a59a8b3Srsb 	int	err;
38375a59a8b3Srsb 
3838da6c28aaSamw 	/*
3839da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3840da6c28aaSamw 	 * and said access is requested, fail quickly.
3841da6c28aaSamw 	 */
3842da6c28aaSamw 	if (flags & FIGNORECASE &&
3843da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3844da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3845da6c28aaSamw 		return (EINVAL);
3846da6c28aaSamw 
3847f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3848f48205beScasper 
3849da6c28aaSamw 	err = (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr, ct, flags);
38502bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, rmdir);
38515a59a8b3Srsb 	return (err);
38527c478bd9Sstevel@tonic-gate }
38537c478bd9Sstevel@tonic-gate 
38547c478bd9Sstevel@tonic-gate int
fop_readdir(vnode_t * vp,uio_t * uiop,cred_t * cr,int * eofp,caller_context_t * ct,int flags)38557c478bd9Sstevel@tonic-gate fop_readdir(
38567c478bd9Sstevel@tonic-gate 	vnode_t *vp,
38577c478bd9Sstevel@tonic-gate 	uio_t *uiop,
38587c478bd9Sstevel@tonic-gate 	cred_t *cr,
3859da6c28aaSamw 	int *eofp,
3860da6c28aaSamw 	caller_context_t *ct,
3861da6c28aaSamw 	int flags)
38627c478bd9Sstevel@tonic-gate {
38635a59a8b3Srsb 	int	err;
38645a59a8b3Srsb 	ssize_t	resid_start = uiop->uio_resid;
38655a59a8b3Srsb 
3866da6c28aaSamw 	/*
3867da6c28aaSamw 	 * If this file system doesn't support retrieving directory
3868da6c28aaSamw 	 * entry flags and said access is requested, fail quickly.
3869da6c28aaSamw 	 */
3870da6c28aaSamw 	if (flags & V_RDDIR_ENTFLAGS &&
3871da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS) == 0)
3872da6c28aaSamw 		return (EINVAL);
3873da6c28aaSamw 
3874f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3875f48205beScasper 
3876da6c28aaSamw 	err = (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp, ct, flags);
38772bb1cb30Sbmc 	VOPSTATS_UPDATE_IO(vp, readdir,
38785a59a8b3Srsb 	    readdir_bytes, (resid_start - uiop->uio_resid));
38795a59a8b3Srsb 	return (err);
38807c478bd9Sstevel@tonic-gate }
38817c478bd9Sstevel@tonic-gate 
38827c478bd9Sstevel@tonic-gate int
fop_symlink(vnode_t * dvp,char * linkname,vattr_t * vap,char * target,cred_t * cr,caller_context_t * ct,int flags)38837c478bd9Sstevel@tonic-gate fop_symlink(
38847c478bd9Sstevel@tonic-gate 	vnode_t *dvp,
38857c478bd9Sstevel@tonic-gate 	char *linkname,
38867c478bd9Sstevel@tonic-gate 	vattr_t *vap,
38877c478bd9Sstevel@tonic-gate 	char *target,
3888da6c28aaSamw 	cred_t *cr,
3889da6c28aaSamw 	caller_context_t *ct,
3890da6c28aaSamw 	int flags)
38917c478bd9Sstevel@tonic-gate {
38925a59a8b3Srsb 	int	err;
38937a286c47SDai Ngo 	xvattr_t xvattr;
38945a59a8b3Srsb 
3895da6c28aaSamw 	/*
3896da6c28aaSamw 	 * If this file system doesn't support case-insensitive access
3897da6c28aaSamw 	 * and said access is requested, fail quickly.
3898da6c28aaSamw 	 */
3899da6c28aaSamw 	if (flags & FIGNORECASE &&
3900da6c28aaSamw 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3901da6c28aaSamw 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3902da6c28aaSamw 		return (EINVAL);
3903da6c28aaSamw 
3904f48205beScasper 	VOPXID_MAP_CR(dvp, cr);
3905f48205beScasper 
39067a286c47SDai Ngo 	/* check for reparse point */
39077a286c47SDai Ngo 	if ((vfs_has_feature(dvp->v_vfsp, VFSFT_REPARSE)) &&
39087a286c47SDai Ngo 	    (strncmp(target, FS_REPARSE_TAG_STR,
39097a286c47SDai Ngo 	    strlen(FS_REPARSE_TAG_STR)) == 0)) {
39107a286c47SDai Ngo 		if (!fs_reparse_mark(target, vap, &xvattr))
39117a286c47SDai Ngo 			vap = (vattr_t *)&xvattr;
39127a286c47SDai Ngo 	}
39137a286c47SDai Ngo 
3914da6c28aaSamw 	err = (*(dvp)->v_op->vop_symlink)
3915da6c28aaSamw 	    (dvp, linkname, vap, target, cr, ct, flags);
39162bb1cb30Sbmc 	VOPSTATS_UPDATE(dvp, symlink);
39175a59a8b3Srsb 	return (err);
39187c478bd9Sstevel@tonic-gate }
39197c478bd9Sstevel@tonic-gate 
39207c478bd9Sstevel@tonic-gate int
fop_readlink(vnode_t * vp,uio_t * uiop,cred_t * cr,caller_context_t * ct)39217c478bd9Sstevel@tonic-gate fop_readlink(
39227c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39237c478bd9Sstevel@tonic-gate 	uio_t *uiop,
3924da6c28aaSamw 	cred_t *cr,
3925da6c28aaSamw 	caller_context_t *ct)
39267c478bd9Sstevel@tonic-gate {
39275a59a8b3Srsb 	int	err;
39285a59a8b3Srsb 
3929f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3930f48205beScasper 
3931da6c28aaSamw 	err = (*(vp)->v_op->vop_readlink)(vp, uiop, cr, ct);
39322bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, readlink);
39335a59a8b3Srsb 	return (err);
39347c478bd9Sstevel@tonic-gate }
39357c478bd9Sstevel@tonic-gate 
39367c478bd9Sstevel@tonic-gate int
fop_fsync(vnode_t * vp,int syncflag,cred_t * cr,caller_context_t * ct)39377c478bd9Sstevel@tonic-gate fop_fsync(
39387c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39397c478bd9Sstevel@tonic-gate 	int syncflag,
3940da6c28aaSamw 	cred_t *cr,
3941da6c28aaSamw 	caller_context_t *ct)
39427c478bd9Sstevel@tonic-gate {
39435a59a8b3Srsb 	int	err;
39445a59a8b3Srsb 
3945f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3946f48205beScasper 
3947da6c28aaSamw 	err = (*(vp)->v_op->vop_fsync)(vp, syncflag, cr, ct);
39482bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, fsync);
39495a59a8b3Srsb 	return (err);
39507c478bd9Sstevel@tonic-gate }
39517c478bd9Sstevel@tonic-gate 
39527c478bd9Sstevel@tonic-gate void
fop_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)39537c478bd9Sstevel@tonic-gate fop_inactive(
39547c478bd9Sstevel@tonic-gate 	vnode_t *vp,
3955da6c28aaSamw 	cred_t *cr,
3956da6c28aaSamw 	caller_context_t *ct)
39577c478bd9Sstevel@tonic-gate {
39585a59a8b3Srsb 	/* Need to update stats before vop call since we may lose the vnode */
39592bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, inactive);
3960f48205beScasper 
3961f48205beScasper 	VOPXID_MAP_CR(vp, cr);
3962f48205beScasper 
3963da6c28aaSamw 	(*(vp)->v_op->vop_inactive)(vp, cr, ct);
39647c478bd9Sstevel@tonic-gate }
39657c478bd9Sstevel@tonic-gate 
39667c478bd9Sstevel@tonic-gate int
fop_fid(vnode_t * vp,fid_t * fidp,caller_context_t * ct)39677c478bd9Sstevel@tonic-gate fop_fid(
39687c478bd9Sstevel@tonic-gate 	vnode_t *vp,
3969da6c28aaSamw 	fid_t *fidp,
3970da6c28aaSamw 	caller_context_t *ct)
39717c478bd9Sstevel@tonic-gate {
39725a59a8b3Srsb 	int	err;
39735a59a8b3Srsb 
3974da6c28aaSamw 	err = (*(vp)->v_op->vop_fid)(vp, fidp, ct);
39752bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, fid);
39765a59a8b3Srsb 	return (err);
39777c478bd9Sstevel@tonic-gate }
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate int
fop_rwlock(vnode_t * vp,int write_lock,caller_context_t * ct)39807c478bd9Sstevel@tonic-gate fop_rwlock(
39817c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39827c478bd9Sstevel@tonic-gate 	int write_lock,
39837c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
39847c478bd9Sstevel@tonic-gate {
39855a59a8b3Srsb 	int	ret;
39865a59a8b3Srsb 
39875a59a8b3Srsb 	ret = ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct));
39882bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, rwlock);
39895a59a8b3Srsb 	return (ret);
39907c478bd9Sstevel@tonic-gate }
39917c478bd9Sstevel@tonic-gate 
39927c478bd9Sstevel@tonic-gate void
fop_rwunlock(vnode_t * vp,int write_lock,caller_context_t * ct)39937c478bd9Sstevel@tonic-gate fop_rwunlock(
39947c478bd9Sstevel@tonic-gate 	vnode_t *vp,
39957c478bd9Sstevel@tonic-gate 	int write_lock,
39967c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
39977c478bd9Sstevel@tonic-gate {
39987c478bd9Sstevel@tonic-gate 	(*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct);
39992bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, rwunlock);
40007c478bd9Sstevel@tonic-gate }
40017c478bd9Sstevel@tonic-gate 
40027c478bd9Sstevel@tonic-gate int
fop_seek(vnode_t * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)40037c478bd9Sstevel@tonic-gate fop_seek(
40047c478bd9Sstevel@tonic-gate 	vnode_t *vp,
40057c478bd9Sstevel@tonic-gate 	offset_t ooff,
4006da6c28aaSamw 	offset_t *noffp,
4007da6c28aaSamw 	caller_context_t *ct)
40087c478bd9Sstevel@tonic-gate {
40095a59a8b3Srsb 	int	err;
40105a59a8b3Srsb 
4011da6c28aaSamw 	err = (*(vp)->v_op->vop_seek)(vp, ooff, noffp, ct);
40122bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, seek);
40135a59a8b3Srsb 	return (err);
40147c478bd9Sstevel@tonic-gate }
40157c478bd9Sstevel@tonic-gate 
40167c478bd9Sstevel@tonic-gate int
fop_cmp(vnode_t * vp1,vnode_t * vp2,caller_context_t * ct)40177c478bd9Sstevel@tonic-gate fop_cmp(
40187c478bd9Sstevel@tonic-gate 	vnode_t *vp1,
4019da6c28aaSamw 	vnode_t *vp2,
4020da6c28aaSamw 	caller_context_t *ct)
40217c478bd9Sstevel@tonic-gate {
40225a59a8b3Srsb 	int	err;
40235a59a8b3Srsb 
4024da6c28aaSamw 	err = (*(vp1)->v_op->vop_cmp)(vp1, vp2, ct);
40252bb1cb30Sbmc 	VOPSTATS_UPDATE(vp1, cmp);
40265a59a8b3Srsb 	return (err);
40277c478bd9Sstevel@tonic-gate }
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate int
fop_frlock(vnode_t * vp,int cmd,flock64_t * bfp,int flag,offset_t offset,struct flk_callback * flk_cbp,cred_t * cr,caller_context_t * ct)40307c478bd9Sstevel@tonic-gate fop_frlock(
40317c478bd9Sstevel@tonic-gate 	vnode_t *vp,
40327c478bd9Sstevel@tonic-gate 	int cmd,
40337c478bd9Sstevel@tonic-gate 	flock64_t *bfp,
40347c478bd9Sstevel@tonic-gate 	int flag,
40357c478bd9Sstevel@tonic-gate 	offset_t offset,
40367c478bd9Sstevel@tonic-gate 	struct flk_callback *flk_cbp,
4037da6c28aaSamw 	cred_t *cr,
4038da6c28aaSamw 	caller_context_t *ct)
40397c478bd9Sstevel@tonic-gate {
40405a59a8b3Srsb 	int	err;
40415a59a8b3Srsb 
4042f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4043f48205beScasper 
40445a59a8b3Srsb 	err = (*(vp)->v_op->vop_frlock)
4045da6c28aaSamw 	    (vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
40462bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, frlock);
40475a59a8b3Srsb 	return (err);
40487c478bd9Sstevel@tonic-gate }
40497c478bd9Sstevel@tonic-gate 
40507c478bd9Sstevel@tonic-gate int
fop_space(vnode_t * vp,int cmd,flock64_t * bfp,int flag,offset_t offset,cred_t * cr,caller_context_t * ct)40517c478bd9Sstevel@tonic-gate fop_space(
40527c478bd9Sstevel@tonic-gate 	vnode_t *vp,
40537c478bd9Sstevel@tonic-gate 	int cmd,
40547c478bd9Sstevel@tonic-gate 	flock64_t *bfp,
40557c478bd9Sstevel@tonic-gate 	int flag,
40567c478bd9Sstevel@tonic-gate 	offset_t offset,
40577c478bd9Sstevel@tonic-gate 	cred_t *cr,
40587c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
40597c478bd9Sstevel@tonic-gate {
40605a59a8b3Srsb 	int	err;
40615a59a8b3Srsb 
4062f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4063f48205beScasper 
40645a59a8b3Srsb 	err = (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct);
40652bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, space);
40665a59a8b3Srsb 	return (err);
40677c478bd9Sstevel@tonic-gate }
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate int
fop_realvp(vnode_t * vp,vnode_t ** vpp,caller_context_t * ct)40707c478bd9Sstevel@tonic-gate fop_realvp(
40717c478bd9Sstevel@tonic-gate 	vnode_t *vp,
4072da6c28aaSamw 	vnode_t **vpp,
4073da6c28aaSamw 	caller_context_t *ct)
40747c478bd9Sstevel@tonic-gate {
40755a59a8b3Srsb 	int	err;
40765a59a8b3Srsb 
4077da6c28aaSamw 	err = (*(vp)->v_op->vop_realvp)(vp, vpp, ct);
40782bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, realvp);
40795a59a8b3Srsb 	return (err);
40807c478bd9Sstevel@tonic-gate }
40817c478bd9Sstevel@tonic-gate 
40827c478bd9Sstevel@tonic-gate int
fop_getpage(vnode_t * vp,offset_t off,size_t len,uint_t * protp,page_t ** plarr,size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,cred_t * cr,caller_context_t * ct)40837c478bd9Sstevel@tonic-gate fop_getpage(
40847c478bd9Sstevel@tonic-gate 	vnode_t *vp,
40857c478bd9Sstevel@tonic-gate 	offset_t off,
40867c478bd9Sstevel@tonic-gate 	size_t len,
40877c478bd9Sstevel@tonic-gate 	uint_t *protp,
40887c478bd9Sstevel@tonic-gate 	page_t **plarr,
40897c478bd9Sstevel@tonic-gate 	size_t plsz,
40907c478bd9Sstevel@tonic-gate 	struct seg *seg,
40917c478bd9Sstevel@tonic-gate 	caddr_t addr,
40927c478bd9Sstevel@tonic-gate 	enum seg_rw rw,
4093da6c28aaSamw 	cred_t *cr,
4094da6c28aaSamw 	caller_context_t *ct)
40957c478bd9Sstevel@tonic-gate {
40965a59a8b3Srsb 	int	err;
40975a59a8b3Srsb 
4098f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4099f48205beScasper 
41005a59a8b3Srsb 	err = (*(vp)->v_op->vop_getpage)
4101da6c28aaSamw 	    (vp, off, len, protp, plarr, plsz, seg, addr, rw, cr, ct);
41022bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getpage);
41035a59a8b3Srsb 	return (err);
41047c478bd9Sstevel@tonic-gate }
41057c478bd9Sstevel@tonic-gate 
41067c478bd9Sstevel@tonic-gate int
fop_putpage(vnode_t * vp,offset_t off,size_t len,int flags,cred_t * cr,caller_context_t * ct)41077c478bd9Sstevel@tonic-gate fop_putpage(
41087c478bd9Sstevel@tonic-gate 	vnode_t *vp,
41097c478bd9Sstevel@tonic-gate 	offset_t off,
41107c478bd9Sstevel@tonic-gate 	size_t len,
41117c478bd9Sstevel@tonic-gate 	int flags,
4112da6c28aaSamw 	cred_t *cr,
4113da6c28aaSamw 	caller_context_t *ct)
41147c478bd9Sstevel@tonic-gate {
41155a59a8b3Srsb 	int	err;
41165a59a8b3Srsb 
4117f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4118f48205beScasper 
4119da6c28aaSamw 	err = (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr, ct);
41202bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, putpage);
41215a59a8b3Srsb 	return (err);
41227c478bd9Sstevel@tonic-gate }
41237c478bd9Sstevel@tonic-gate 
41247c478bd9Sstevel@tonic-gate int
fop_map(vnode_t * vp,offset_t off,struct as * as,caddr_t * addrp,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)41257c478bd9Sstevel@tonic-gate fop_map(
41267c478bd9Sstevel@tonic-gate 	vnode_t *vp,
41277c478bd9Sstevel@tonic-gate 	offset_t off,
41287c478bd9Sstevel@tonic-gate 	struct as *as,
41297c478bd9Sstevel@tonic-gate 	caddr_t *addrp,
41307c478bd9Sstevel@tonic-gate 	size_t len,
41317c478bd9Sstevel@tonic-gate 	uchar_t prot,
41327c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
41337c478bd9Sstevel@tonic-gate 	uint_t flags,
4134da6c28aaSamw 	cred_t *cr,
4135da6c28aaSamw 	caller_context_t *ct)
41367c478bd9Sstevel@tonic-gate {
41375a59a8b3Srsb 	int	err;
41385a59a8b3Srsb 
4139f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4140f48205beScasper 
41415a59a8b3Srsb 	err = (*(vp)->v_op->vop_map)
4142da6c28aaSamw 	    (vp, off, as, addrp, len, prot, maxprot, flags, cr, ct);
41432bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, map);
41445a59a8b3Srsb 	return (err);
41457c478bd9Sstevel@tonic-gate }
41467c478bd9Sstevel@tonic-gate 
41477c478bd9Sstevel@tonic-gate int
fop_addmap(vnode_t * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)41487c478bd9Sstevel@tonic-gate fop_addmap(
41497c478bd9Sstevel@tonic-gate 	vnode_t *vp,
41507c478bd9Sstevel@tonic-gate 	offset_t off,
41517c478bd9Sstevel@tonic-gate 	struct as *as,
41527c478bd9Sstevel@tonic-gate 	caddr_t addr,
41537c478bd9Sstevel@tonic-gate 	size_t len,
41547c478bd9Sstevel@tonic-gate 	uchar_t prot,
41557c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
41567c478bd9Sstevel@tonic-gate 	uint_t flags,
4157da6c28aaSamw 	cred_t *cr,
4158da6c28aaSamw 	caller_context_t *ct)
41597c478bd9Sstevel@tonic-gate {
41607c478bd9Sstevel@tonic-gate 	int error;
41617c478bd9Sstevel@tonic-gate 	u_longlong_t delta;
41627c478bd9Sstevel@tonic-gate 
4163f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4164f48205beScasper 
41657c478bd9Sstevel@tonic-gate 	error = (*(vp)->v_op->vop_addmap)
4166da6c28aaSamw 	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
41677c478bd9Sstevel@tonic-gate 
41687c478bd9Sstevel@tonic-gate 	if ((!error) && (vp->v_type == VREG)) {
41697c478bd9Sstevel@tonic-gate 		delta = (u_longlong_t)btopr(len);
41707c478bd9Sstevel@tonic-gate 		/*
41717c478bd9Sstevel@tonic-gate 		 * If file is declared MAP_PRIVATE, it can't be written back
41727c478bd9Sstevel@tonic-gate 		 * even if open for write. Handle as read.
41737c478bd9Sstevel@tonic-gate 		 */
41747c478bd9Sstevel@tonic-gate 		if (flags & MAP_PRIVATE) {
41757c478bd9Sstevel@tonic-gate 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4176b9c98e29Spf 			    (int64_t)delta);
41777c478bd9Sstevel@tonic-gate 		} else {
41787c478bd9Sstevel@tonic-gate 			/*
41797c478bd9Sstevel@tonic-gate 			 * atomic_add_64 forces the fetch of a 64 bit value to
41807c478bd9Sstevel@tonic-gate 			 * be atomic on 32 bit machines
41817c478bd9Sstevel@tonic-gate 			 */
41827c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_WRITE)
41837c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
4184b9c98e29Spf 				    (int64_t)delta);
41857c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_READ)
41867c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4187b9c98e29Spf 				    (int64_t)delta);
41887c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_EXEC)
41897c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4190b9c98e29Spf 				    (int64_t)delta);
41917c478bd9Sstevel@tonic-gate 		}
41927c478bd9Sstevel@tonic-gate 	}
41932bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, addmap);
41947c478bd9Sstevel@tonic-gate 	return (error);
41957c478bd9Sstevel@tonic-gate }
41967c478bd9Sstevel@tonic-gate 
41977c478bd9Sstevel@tonic-gate int
fop_delmap(vnode_t * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uint_t prot,uint_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)41987c478bd9Sstevel@tonic-gate fop_delmap(
41997c478bd9Sstevel@tonic-gate 	vnode_t *vp,
42007c478bd9Sstevel@tonic-gate 	offset_t off,
42017c478bd9Sstevel@tonic-gate 	struct as *as,
42027c478bd9Sstevel@tonic-gate 	caddr_t addr,
42037c478bd9Sstevel@tonic-gate 	size_t len,
42047c478bd9Sstevel@tonic-gate 	uint_t prot,
42057c478bd9Sstevel@tonic-gate 	uint_t maxprot,
42067c478bd9Sstevel@tonic-gate 	uint_t flags,
4207da6c28aaSamw 	cred_t *cr,
4208da6c28aaSamw 	caller_context_t *ct)
42097c478bd9Sstevel@tonic-gate {
42107c478bd9Sstevel@tonic-gate 	int error;
42117c478bd9Sstevel@tonic-gate 	u_longlong_t delta;
4212f48205beScasper 
4213f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4214f48205beScasper 
42157c478bd9Sstevel@tonic-gate 	error = (*(vp)->v_op->vop_delmap)
4216da6c28aaSamw 	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
42177c478bd9Sstevel@tonic-gate 
42187c478bd9Sstevel@tonic-gate 	/*
42197c478bd9Sstevel@tonic-gate 	 * NFS calls into delmap twice, the first time
42207c478bd9Sstevel@tonic-gate 	 * it simply establishes a callback mechanism and returns EAGAIN
42217c478bd9Sstevel@tonic-gate 	 * while the real work is being done upon the second invocation.
42227c478bd9Sstevel@tonic-gate 	 * We have to detect this here and only decrement the counts upon
42237c478bd9Sstevel@tonic-gate 	 * the second delmap request.
42247c478bd9Sstevel@tonic-gate 	 */
42257c478bd9Sstevel@tonic-gate 	if ((error != EAGAIN) && (vp->v_type == VREG)) {
42267c478bd9Sstevel@tonic-gate 
42277c478bd9Sstevel@tonic-gate 		delta = (u_longlong_t)btopr(len);
42287c478bd9Sstevel@tonic-gate 
42297c478bd9Sstevel@tonic-gate 		if (flags & MAP_PRIVATE) {
42307c478bd9Sstevel@tonic-gate 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4231b9c98e29Spf 			    (int64_t)(-delta));
42327c478bd9Sstevel@tonic-gate 		} else {
42337c478bd9Sstevel@tonic-gate 			/*
42347c478bd9Sstevel@tonic-gate 			 * atomic_add_64 forces the fetch of a 64 bit value
42357c478bd9Sstevel@tonic-gate 			 * to be atomic on 32 bit machines
42367c478bd9Sstevel@tonic-gate 			 */
42377c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_WRITE)
42387c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
4239b9c98e29Spf 				    (int64_t)(-delta));
42407c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_READ)
42417c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4242b9c98e29Spf 				    (int64_t)(-delta));
42437c478bd9Sstevel@tonic-gate 			if (maxprot & PROT_EXEC)
42447c478bd9Sstevel@tonic-gate 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4245b9c98e29Spf 				    (int64_t)(-delta));
42467c478bd9Sstevel@tonic-gate 		}
42477c478bd9Sstevel@tonic-gate 	}
42482bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, delmap);
42497c478bd9Sstevel@tonic-gate 	return (error);
42507c478bd9Sstevel@tonic-gate }
42517c478bd9Sstevel@tonic-gate 
42527c478bd9Sstevel@tonic-gate 
42537c478bd9Sstevel@tonic-gate int
fop_poll(vnode_t * vp,short events,int anyyet,short * reventsp,struct pollhead ** phpp,caller_context_t * ct)42547c478bd9Sstevel@tonic-gate fop_poll(
42557c478bd9Sstevel@tonic-gate 	vnode_t *vp,
42567c478bd9Sstevel@tonic-gate 	short events,
42577c478bd9Sstevel@tonic-gate 	int anyyet,
42587c478bd9Sstevel@tonic-gate 	short *reventsp,
4259da6c28aaSamw 	struct pollhead **phpp,
4260da6c28aaSamw 	caller_context_t *ct)
42617c478bd9Sstevel@tonic-gate {
42625a59a8b3Srsb 	int	err;
42635a59a8b3Srsb 
4264da6c28aaSamw 	err = (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp, ct);
42652bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, poll);
42665a59a8b3Srsb 	return (err);
42677c478bd9Sstevel@tonic-gate }
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate int
fop_dump(vnode_t * vp,caddr_t addr,offset_t lbdn,offset_t dblks,caller_context_t * ct)42707c478bd9Sstevel@tonic-gate fop_dump(
42717c478bd9Sstevel@tonic-gate 	vnode_t *vp,
42727c478bd9Sstevel@tonic-gate 	caddr_t addr,
4273d7334e51Srm 	offset_t lbdn,
4274d7334e51Srm 	offset_t dblks,
4275da6c28aaSamw 	caller_context_t *ct)
42767c478bd9Sstevel@tonic-gate {
42775a59a8b3Srsb 	int	err;
42785a59a8b3Srsb 
4279d7334e51Srm 	/* ensure lbdn and dblks can be passed safely to bdev_dump */
4280d7334e51Srm 	if ((lbdn != (daddr_t)lbdn) || (dblks != (int)dblks))
4281d7334e51Srm 		return (EIO);
4282d7334e51Srm 
4283da6c28aaSamw 	err = (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks, ct);
42842bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dump);
42855a59a8b3Srsb 	return (err);
42867c478bd9Sstevel@tonic-gate }
42877c478bd9Sstevel@tonic-gate 
42887c478bd9Sstevel@tonic-gate int
fop_pathconf(vnode_t * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)42897c478bd9Sstevel@tonic-gate fop_pathconf(
42907c478bd9Sstevel@tonic-gate 	vnode_t *vp,
42917c478bd9Sstevel@tonic-gate 	int cmd,
42927c478bd9Sstevel@tonic-gate 	ulong_t *valp,
4293da6c28aaSamw 	cred_t *cr,
4294da6c28aaSamw 	caller_context_t *ct)
42957c478bd9Sstevel@tonic-gate {
42965a59a8b3Srsb 	int	err;
42975a59a8b3Srsb 
4298f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4299f48205beScasper 
4300da6c28aaSamw 	err = (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr, ct);
43012bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, pathconf);
43025a59a8b3Srsb 	return (err);
43037c478bd9Sstevel@tonic-gate }
43047c478bd9Sstevel@tonic-gate 
43057c478bd9Sstevel@tonic-gate int
fop_pageio(vnode_t * vp,struct page * pp,u_offset_t io_off,size_t io_len,int flags,cred_t * cr,caller_context_t * ct)43067c478bd9Sstevel@tonic-gate fop_pageio(
43077c478bd9Sstevel@tonic-gate 	vnode_t *vp,
43087c478bd9Sstevel@tonic-gate 	struct page *pp,
43097c478bd9Sstevel@tonic-gate 	u_offset_t io_off,
43107c478bd9Sstevel@tonic-gate 	size_t io_len,
43117c478bd9Sstevel@tonic-gate 	int flags,
4312da6c28aaSamw 	cred_t *cr,
4313da6c28aaSamw 	caller_context_t *ct)
43147c478bd9Sstevel@tonic-gate {
43155a59a8b3Srsb 	int	err;
43165a59a8b3Srsb 
4317f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4318f48205beScasper 
4319da6c28aaSamw 	err = (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr, ct);
43202bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, pageio);
43215a59a8b3Srsb 	return (err);
43227c478bd9Sstevel@tonic-gate }
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate int
fop_dumpctl(vnode_t * vp,int action,offset_t * blkp,caller_context_t * ct)43257c478bd9Sstevel@tonic-gate fop_dumpctl(
43267c478bd9Sstevel@tonic-gate 	vnode_t *vp,
43277c478bd9Sstevel@tonic-gate 	int action,
4328d7334e51Srm 	offset_t *blkp,
4329da6c28aaSamw 	caller_context_t *ct)
43307c478bd9Sstevel@tonic-gate {
43315a59a8b3Srsb 	int	err;
4332da6c28aaSamw 	err = (*(vp)->v_op->vop_dumpctl)(vp, action, blkp, ct);
43332bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dumpctl);
43345a59a8b3Srsb 	return (err);
43357c478bd9Sstevel@tonic-gate }
43367c478bd9Sstevel@tonic-gate 
43377c478bd9Sstevel@tonic-gate void
fop_dispose(vnode_t * vp,page_t * pp,int flag,int dn,cred_t * cr,caller_context_t * ct)43387c478bd9Sstevel@tonic-gate fop_dispose(
43397c478bd9Sstevel@tonic-gate 	vnode_t *vp,
43407c478bd9Sstevel@tonic-gate 	page_t *pp,
43417c478bd9Sstevel@tonic-gate 	int flag,
43427c478bd9Sstevel@tonic-gate 	int dn,
4343da6c28aaSamw 	cred_t *cr,
4344da6c28aaSamw 	caller_context_t *ct)
43457c478bd9Sstevel@tonic-gate {
43465a59a8b3Srsb 	/* Must do stats first since it's possible to lose the vnode */
43472bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, dispose);
4348f48205beScasper 
4349f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4350f48205beScasper 
4351da6c28aaSamw 	(*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr, ct);
43527c478bd9Sstevel@tonic-gate }
43537c478bd9Sstevel@tonic-gate 
43547c478bd9Sstevel@tonic-gate int
fop_setsecattr(vnode_t * vp,vsecattr_t * vsap,int flag,cred_t * cr,caller_context_t * ct)43557c478bd9Sstevel@tonic-gate fop_setsecattr(
43567c478bd9Sstevel@tonic-gate 	vnode_t *vp,
43577c478bd9Sstevel@tonic-gate 	vsecattr_t *vsap,
43587c478bd9Sstevel@tonic-gate 	int flag,
4359da6c28aaSamw 	cred_t *cr,
4360da6c28aaSamw 	caller_context_t *ct)
43617c478bd9Sstevel@tonic-gate {
43625a59a8b3Srsb 	int	err;
43635a59a8b3Srsb 
4364f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4365f48205beScasper 
4366da6c28aaSamw 	/*
4367da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
4368da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
4369da6c28aaSamw 	 */
4370da6c28aaSamw 	if ((flag & ATTR_NOACLCHECK) &&
4371da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4372da6c28aaSamw 		return (EINVAL);
4373da6c28aaSamw 	}
4374da6c28aaSamw 	err = (*(vp)->v_op->vop_setsecattr) (vp, vsap, flag, cr, ct);
43752bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, setsecattr);
43765a59a8b3Srsb 	return (err);
43777c478bd9Sstevel@tonic-gate }
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate int
fop_getsecattr(vnode_t * vp,vsecattr_t * vsap,int flag,cred_t * cr,caller_context_t * ct)43807c478bd9Sstevel@tonic-gate fop_getsecattr(
43817c478bd9Sstevel@tonic-gate 	vnode_t *vp,
43827c478bd9Sstevel@tonic-gate 	vsecattr_t *vsap,
43837c478bd9Sstevel@tonic-gate 	int flag,
4384da6c28aaSamw 	cred_t *cr,
4385da6c28aaSamw 	caller_context_t *ct)
43867c478bd9Sstevel@tonic-gate {
43875a59a8b3Srsb 	int	err;
43885a59a8b3Srsb 
4389da6c28aaSamw 	/*
4390da6c28aaSamw 	 * We're only allowed to skip the ACL check iff we used a 32 bit
4391da6c28aaSamw 	 * ACE mask with VOP_ACCESS() to determine permissions.
4392da6c28aaSamw 	 */
4393da6c28aaSamw 	if ((flag & ATTR_NOACLCHECK) &&
4394da6c28aaSamw 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4395da6c28aaSamw 		return (EINVAL);
4396da6c28aaSamw 	}
4397da6c28aaSamw 
4398f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4399f48205beScasper 
4400da6c28aaSamw 	err = (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr, ct);
44012bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, getsecattr);
44025a59a8b3Srsb 	return (err);
44037c478bd9Sstevel@tonic-gate }
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate int
fop_shrlock(vnode_t * vp,int cmd,struct shrlock * shr,int flag,cred_t * cr,caller_context_t * ct)44067c478bd9Sstevel@tonic-gate fop_shrlock(
44077c478bd9Sstevel@tonic-gate 	vnode_t *vp,
44087c478bd9Sstevel@tonic-gate 	int cmd,
44097c478bd9Sstevel@tonic-gate 	struct shrlock *shr,
44107c478bd9Sstevel@tonic-gate 	int flag,
4411da6c28aaSamw 	cred_t *cr,
4412da6c28aaSamw 	caller_context_t *ct)
44137c478bd9Sstevel@tonic-gate {
44145a59a8b3Srsb 	int	err;
44155a59a8b3Srsb 
4416f48205beScasper 	VOPXID_MAP_CR(vp, cr);
4417f48205beScasper 
4418da6c28aaSamw 	err = (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr, ct);
44192bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, shrlock);
44205a59a8b3Srsb 	return (err);
44217c478bd9Sstevel@tonic-gate }
44227c478bd9Sstevel@tonic-gate 
44237c478bd9Sstevel@tonic-gate int
fop_vnevent(vnode_t * vp,vnevent_t vnevent,vnode_t * dvp,char * fnm,caller_context_t * ct)4424da6c28aaSamw fop_vnevent(vnode_t *vp, vnevent_t vnevent, vnode_t *dvp, char *fnm,
4425da6c28aaSamw     caller_context_t *ct)
44267c478bd9Sstevel@tonic-gate {
44275a59a8b3Srsb 	int	err;
44285a59a8b3Srsb 
4429da6c28aaSamw 	err = (*(vp)->v_op->vop_vnevent)(vp, vnevent, dvp, fnm, ct);
44302bb1cb30Sbmc 	VOPSTATS_UPDATE(vp, vnevent);
44315a59a8b3Srsb 	return (err);
44327c478bd9Sstevel@tonic-gate }
44331b300de9Sjwahlig 
4434c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
fop_reqzcbuf(vnode_t * vp,enum uio_rw ioflag,xuio_t * uiop,cred_t * cr,caller_context_t * ct)4435c242f9a0Schunli zhang - Sun Microsystems - Irvine United States fop_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *uiop, cred_t *cr,
4436c242f9a0Schunli zhang - Sun Microsystems - Irvine United States     caller_context_t *ct)
4437c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
4438c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int err;
4439c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
4440c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (vfs_has_feature(vp->v_vfsp, VFSFT_ZEROCOPY_SUPPORTED) == 0)
4441c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		return (ENOTSUP);
4442c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	err = (*(vp)->v_op->vop_reqzcbuf)(vp, ioflag, uiop, cr, ct);
4443c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	VOPSTATS_UPDATE(vp, reqzcbuf);
4444c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (err);
4445c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
4446c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
4447c242f9a0Schunli zhang - Sun Microsystems - Irvine United States int
fop_retzcbuf(vnode_t * vp,xuio_t * uiop,cred_t * cr,caller_context_t * ct)4448c242f9a0Schunli zhang - Sun Microsystems - Irvine United States fop_retzcbuf(vnode_t *vp, xuio_t *uiop, cred_t *cr, caller_context_t *ct)
4449c242f9a0Schunli zhang - Sun Microsystems - Irvine United States {
4450c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int err;
4451c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
4452c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (vfs_has_feature(vp->v_vfsp, VFSFT_ZEROCOPY_SUPPORTED) == 0)
4453c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		return (ENOTSUP);
4454c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	err = (*(vp)->v_op->vop_retzcbuf)(vp, uiop, cr, ct);
4455c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	VOPSTATS_UPDATE(vp, retzcbuf);
4456c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	return (err);
4457c242f9a0Schunli zhang - Sun Microsystems - Irvine United States }
4458c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
44591b300de9Sjwahlig /*
44601b300de9Sjwahlig  * Default destructor
44611b300de9Sjwahlig  *	Needed because NULL destructor means that the key is unused
44621b300de9Sjwahlig  */
44631b300de9Sjwahlig /* ARGSUSED */
44641b300de9Sjwahlig void
vsd_defaultdestructor(void * value)44651b300de9Sjwahlig vsd_defaultdestructor(void *value)
44661b300de9Sjwahlig {}
44671b300de9Sjwahlig 
44681b300de9Sjwahlig /*
44691b300de9Sjwahlig  * Create a key (index into per vnode array)
44701b300de9Sjwahlig  *	Locks out vsd_create, vsd_destroy, and vsd_free
44711b300de9Sjwahlig  *	May allocate memory with lock held
44721b300de9Sjwahlig  */
44731b300de9Sjwahlig void
vsd_create(uint_t * keyp,void (* destructor)(void *))44741b300de9Sjwahlig vsd_create(uint_t *keyp, void (*destructor)(void *))
44751b300de9Sjwahlig {
44761b300de9Sjwahlig 	int	i;
44771b300de9Sjwahlig 	uint_t	nkeys;
44781b300de9Sjwahlig 
44791b300de9Sjwahlig 	/*
44801b300de9Sjwahlig 	 * if key is allocated, do nothing
44811b300de9Sjwahlig 	 */
44821b300de9Sjwahlig 	mutex_enter(&vsd_lock);
44831b300de9Sjwahlig 	if (*keyp) {
44841b300de9Sjwahlig 		mutex_exit(&vsd_lock);
44851b300de9Sjwahlig 		return;
44861b300de9Sjwahlig 	}
44871b300de9Sjwahlig 	/*
44881b300de9Sjwahlig 	 * find an unused key
44891b300de9Sjwahlig 	 */
44901b300de9Sjwahlig 	if (destructor == NULL)
44911b300de9Sjwahlig 		destructor = vsd_defaultdestructor;
44921b300de9Sjwahlig 
44931b300de9Sjwahlig 	for (i = 0; i < vsd_nkeys; ++i)
44941b300de9Sjwahlig 		if (vsd_destructor[i] == NULL)
44951b300de9Sjwahlig 			break;
44961b300de9Sjwahlig 
44971b300de9Sjwahlig 	/*
44981b300de9Sjwahlig 	 * if no unused keys, increase the size of the destructor array
44991b300de9Sjwahlig 	 */
45001b300de9Sjwahlig 	if (i == vsd_nkeys) {
45011b300de9Sjwahlig 		if ((nkeys = (vsd_nkeys << 1)) == 0)
45021b300de9Sjwahlig 			nkeys = 1;
45031b300de9Sjwahlig 		vsd_destructor =
45041b300de9Sjwahlig 		    (void (**)(void *))vsd_realloc((void *)vsd_destructor,
45051b300de9Sjwahlig 		    (size_t)(vsd_nkeys * sizeof (void (*)(void *))),
45061b300de9Sjwahlig 		    (size_t)(nkeys * sizeof (void (*)(void *))));
45071b300de9Sjwahlig 		vsd_nkeys = nkeys;
45081b300de9Sjwahlig 	}
45091b300de9Sjwahlig 
45101b300de9Sjwahlig 	/*
45111b300de9Sjwahlig 	 * allocate the next available unused key
45121b300de9Sjwahlig 	 */
45131b300de9Sjwahlig 	vsd_destructor[i] = destructor;
45141b300de9Sjwahlig 	*keyp = i + 1;
45151b300de9Sjwahlig 
45161b300de9Sjwahlig 	/* create vsd_list, if it doesn't exist */
45171b300de9Sjwahlig 	if (vsd_list == NULL) {
45181b300de9Sjwahlig 		vsd_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
45191b300de9Sjwahlig 		list_create(vsd_list, sizeof (struct vsd_node),
45201b300de9Sjwahlig 		    offsetof(struct vsd_node, vs_nodes));
45211b300de9Sjwahlig 	}
45221b300de9Sjwahlig 
45231b300de9Sjwahlig 	mutex_exit(&vsd_lock);
45241b300de9Sjwahlig }
45251b300de9Sjwahlig 
45261b300de9Sjwahlig /*
45271b300de9Sjwahlig  * Destroy a key
45281b300de9Sjwahlig  *
45291b300de9Sjwahlig  * Assumes that the caller is preventing vsd_set and vsd_get
45301b300de9Sjwahlig  * Locks out vsd_create, vsd_destroy, and vsd_free
45311b300de9Sjwahlig  * May free memory with lock held
45321b300de9Sjwahlig  */
45331b300de9Sjwahlig void
vsd_destroy(uint_t * keyp)45341b300de9Sjwahlig vsd_destroy(uint_t *keyp)
45351b300de9Sjwahlig {
45361b300de9Sjwahlig 	uint_t key;
45371b300de9Sjwahlig 	struct vsd_node *vsd;
45381b300de9Sjwahlig 
45391b300de9Sjwahlig 	/*
45401b300de9Sjwahlig 	 * protect the key namespace and our destructor lists
45411b300de9Sjwahlig 	 */
45421b300de9Sjwahlig 	mutex_enter(&vsd_lock);
45431b300de9Sjwahlig 	key = *keyp;
45441b300de9Sjwahlig 	*keyp = 0;
45451b300de9Sjwahlig 
45461b300de9Sjwahlig 	ASSERT(key <= vsd_nkeys);
45471b300de9Sjwahlig 
45481b300de9Sjwahlig 	/*
45491b300de9Sjwahlig 	 * if the key is valid
45501b300de9Sjwahlig 	 */
45511b300de9Sjwahlig 	if (key != 0) {
45521b300de9Sjwahlig 		uint_t k = key - 1;
45531b300de9Sjwahlig 		/*
45541b300de9Sjwahlig 		 * for every vnode with VSD, call key's destructor
45551b300de9Sjwahlig 		 */
45561b300de9Sjwahlig 		for (vsd = list_head(vsd_list); vsd != NULL;
45571b300de9Sjwahlig 		    vsd = list_next(vsd_list, vsd)) {
45581b300de9Sjwahlig 			/*
45591b300de9Sjwahlig 			 * no VSD for key in this vnode
45601b300de9Sjwahlig 			 */
45611b300de9Sjwahlig 			if (key > vsd->vs_nkeys)
45621b300de9Sjwahlig 				continue;
45631b300de9Sjwahlig 			/*
45641b300de9Sjwahlig 			 * call destructor for key
45651b300de9Sjwahlig 			 */
45661b300de9Sjwahlig 			if (vsd->vs_value[k] && vsd_destructor[k])
45671b300de9Sjwahlig 				(*vsd_destructor[k])(vsd->vs_value[k]);
45681b300de9Sjwahlig 			/*
45691b300de9Sjwahlig 			 * reset value for key
45701b300de9Sjwahlig 			 */
45711b300de9Sjwahlig 			vsd->vs_value[k] = NULL;
45721b300de9Sjwahlig 		}
45731b300de9Sjwahlig 		/*
45741b300de9Sjwahlig 		 * actually free the key (NULL destructor == unused)
45751b300de9Sjwahlig 		 */
45761b300de9Sjwahlig 		vsd_destructor[k] = NULL;
45771b300de9Sjwahlig 	}
45781b300de9Sjwahlig 
45791b300de9Sjwahlig 	mutex_exit(&vsd_lock);
45801b300de9Sjwahlig }
45811b300de9Sjwahlig 
45821b300de9Sjwahlig /*
45831b300de9Sjwahlig  * Quickly return the per vnode value that was stored with the specified key
45841b300de9Sjwahlig  * Assumes the caller is protecting key from vsd_create and vsd_destroy
4585d216dff5SRobert Mastors  * Assumes the caller is holding v_vsd_lock to protect the vsd.
45861b300de9Sjwahlig  */
45871b300de9Sjwahlig void *
vsd_get(vnode_t * vp,uint_t key)45881b300de9Sjwahlig vsd_get(vnode_t *vp, uint_t key)
45891b300de9Sjwahlig {
45901b300de9Sjwahlig 	struct vsd_node *vsd;
45911b300de9Sjwahlig 
45921b300de9Sjwahlig 	ASSERT(vp != NULL);
4593d216dff5SRobert Mastors 	ASSERT(mutex_owned(&vp->v_vsd_lock));
45941b300de9Sjwahlig 
45951b300de9Sjwahlig 	vsd = vp->v_vsd;
45961b300de9Sjwahlig 
45971b300de9Sjwahlig 	if (key && vsd != NULL && key <= vsd->vs_nkeys)
45981b300de9Sjwahlig 		return (vsd->vs_value[key - 1]);
45991b300de9Sjwahlig 	return (NULL);
46001b300de9Sjwahlig }
46011b300de9Sjwahlig 
46021b300de9Sjwahlig /*
46031b300de9Sjwahlig  * Set a per vnode value indexed with the specified key
4604d216dff5SRobert Mastors  * Assumes the caller is holding v_vsd_lock to protect the vsd.
46051b300de9Sjwahlig  */
46061b300de9Sjwahlig int
vsd_set(vnode_t * vp,uint_t key,void * value)46071b300de9Sjwahlig vsd_set(vnode_t *vp, uint_t key, void *value)
46081b300de9Sjwahlig {
4609d216dff5SRobert Mastors 	struct vsd_node *vsd;
4610d216dff5SRobert Mastors 
4611d216dff5SRobert Mastors 	ASSERT(vp != NULL);
4612d216dff5SRobert Mastors 	ASSERT(mutex_owned(&vp->v_vsd_lock));
46131b300de9Sjwahlig 
46141b300de9Sjwahlig 	if (key == 0)
46151b300de9Sjwahlig 		return (EINVAL);
4616d216dff5SRobert Mastors 
4617d216dff5SRobert Mastors 	vsd = vp->v_vsd;
46181b300de9Sjwahlig 	if (vsd == NULL)
46191b300de9Sjwahlig 		vsd = vp->v_vsd = kmem_zalloc(sizeof (*vsd), KM_SLEEP);
46201b300de9Sjwahlig 
46211b300de9Sjwahlig 	/*
46221b300de9Sjwahlig 	 * If the vsd was just allocated, vs_nkeys will be 0, so the following
46231b300de9Sjwahlig 	 * code won't happen and we will continue down and allocate space for
46241b300de9Sjwahlig 	 * the vs_value array.
46251b300de9Sjwahlig 	 * If the caller is replacing one value with another, then it is up
46261b300de9Sjwahlig 	 * to the caller to free/rele/destroy the previous value (if needed).
46271b300de9Sjwahlig 	 */
46281b300de9Sjwahlig 	if (key <= vsd->vs_nkeys) {
46291b300de9Sjwahlig 		vsd->vs_value[key - 1] = value;
46301b300de9Sjwahlig 		return (0);
46311b300de9Sjwahlig 	}
46321b300de9Sjwahlig 
46331b300de9Sjwahlig 	ASSERT(key <= vsd_nkeys);
46341b300de9Sjwahlig 
46351b300de9Sjwahlig 	if (vsd->vs_nkeys == 0) {
46361b300de9Sjwahlig 		mutex_enter(&vsd_lock);	/* lock out vsd_destroy() */
46371b300de9Sjwahlig 		/*
46381b300de9Sjwahlig 		 * Link onto list of all VSD nodes.
46391b300de9Sjwahlig 		 */
46401b300de9Sjwahlig 		list_insert_head(vsd_list, vsd);
46411b300de9Sjwahlig 		mutex_exit(&vsd_lock);
46421b300de9Sjwahlig 	}
46431b300de9Sjwahlig 
46441b300de9Sjwahlig 	/*
46451b300de9Sjwahlig 	 * Allocate vnode local storage and set the value for key
46461b300de9Sjwahlig 	 */
46471b300de9Sjwahlig 	vsd->vs_value = vsd_realloc(vsd->vs_value,
46481b300de9Sjwahlig 	    vsd->vs_nkeys * sizeof (void *),
46491b300de9Sjwahlig 	    key * sizeof (void *));
46501b300de9Sjwahlig 	vsd->vs_nkeys = key;
46511b300de9Sjwahlig 	vsd->vs_value[key - 1] = value;
46521b300de9Sjwahlig 
46531b300de9Sjwahlig 	return (0);
46541b300de9Sjwahlig }
46551b300de9Sjwahlig 
46561b300de9Sjwahlig /*
46571b300de9Sjwahlig  * Called from vn_free() to run the destructor function for each vsd
46581b300de9Sjwahlig  *	Locks out vsd_create and vsd_destroy
46591b300de9Sjwahlig  *	Assumes that the destructor *DOES NOT* use vsd
46601b300de9Sjwahlig  */
46611b300de9Sjwahlig void
vsd_free(vnode_t * vp)46621b300de9Sjwahlig vsd_free(vnode_t *vp)
46631b300de9Sjwahlig {
46641b300de9Sjwahlig 	int i;
46651b300de9Sjwahlig 	struct vsd_node *vsd = vp->v_vsd;
46661b300de9Sjwahlig 
46671b300de9Sjwahlig 	if (vsd == NULL)
46681b300de9Sjwahlig 		return;
46691b300de9Sjwahlig 
46701b300de9Sjwahlig 	if (vsd->vs_nkeys == 0) {
46711b300de9Sjwahlig 		kmem_free(vsd, sizeof (*vsd));
46721b300de9Sjwahlig 		vp->v_vsd = NULL;
46731b300de9Sjwahlig 		return;
46741b300de9Sjwahlig 	}
46751b300de9Sjwahlig 
46761b300de9Sjwahlig 	/*
46771b300de9Sjwahlig 	 * lock out vsd_create and vsd_destroy, call
46781b300de9Sjwahlig 	 * the destructor, and mark the value as destroyed.
46791b300de9Sjwahlig 	 */
46801b300de9Sjwahlig 	mutex_enter(&vsd_lock);
46811b300de9Sjwahlig 
46821b300de9Sjwahlig 	for (i = 0; i < vsd->vs_nkeys; i++) {
46831b300de9Sjwahlig 		if (vsd->vs_value[i] && vsd_destructor[i])
46841b300de9Sjwahlig 			(*vsd_destructor[i])(vsd->vs_value[i]);
46851b300de9Sjwahlig 		vsd->vs_value[i] = NULL;
46861b300de9Sjwahlig 	}
46871b300de9Sjwahlig 
46881b300de9Sjwahlig 	/*
46891b300de9Sjwahlig 	 * remove from linked list of VSD nodes
46901b300de9Sjwahlig 	 */
46911b300de9Sjwahlig 	list_remove(vsd_list, vsd);
46921b300de9Sjwahlig 
46931b300de9Sjwahlig 	mutex_exit(&vsd_lock);
46941b300de9Sjwahlig 
46951b300de9Sjwahlig 	/*
46961b300de9Sjwahlig 	 * free up the VSD
46971b300de9Sjwahlig 	 */
46981b300de9Sjwahlig 	kmem_free(vsd->vs_value, vsd->vs_nkeys * sizeof (void *));
46991b300de9Sjwahlig 	kmem_free(vsd, sizeof (struct vsd_node));
47001b300de9Sjwahlig 	vp->v_vsd = NULL;
47011b300de9Sjwahlig }
47021b300de9Sjwahlig 
47031b300de9Sjwahlig /*
47041b300de9Sjwahlig  * realloc
47051b300de9Sjwahlig  */
47061b300de9Sjwahlig static void *
vsd_realloc(void * old,size_t osize,size_t nsize)47071b300de9Sjwahlig vsd_realloc(void *old, size_t osize, size_t nsize)
47081b300de9Sjwahlig {
47091b300de9Sjwahlig 	void *new;
47101b300de9Sjwahlig 
47111b300de9Sjwahlig 	new = kmem_zalloc(nsize, KM_SLEEP);
47121b300de9Sjwahlig 	if (old) {
47131b300de9Sjwahlig 		bcopy(old, new, osize);
47141b300de9Sjwahlig 		kmem_free(old, osize);
47151b300de9Sjwahlig 	}
47161b300de9Sjwahlig 	return (new);
47171b300de9Sjwahlig }
47187a286c47SDai Ngo 
47197a286c47SDai Ngo /*
47207a286c47SDai Ngo  * Setup the extensible system attribute for creating a reparse point.
47217a286c47SDai Ngo  * The symlink data 'target' is validated for proper format of a reparse
47227a286c47SDai Ngo  * string and a check also made to make sure the symlink data does not
47237a286c47SDai Ngo  * point to an existing file.
47247a286c47SDai Ngo  *
47257a286c47SDai Ngo  * return 0 if ok else -1.
47267a286c47SDai Ngo  */
47277a286c47SDai Ngo static int
fs_reparse_mark(char * target,vattr_t * vap,xvattr_t * xvattr)47287a286c47SDai Ngo fs_reparse_mark(char *target, vattr_t *vap, xvattr_t *xvattr)
47297a286c47SDai Ngo {
47307a286c47SDai Ngo 	xoptattr_t *xoap;
47317a286c47SDai Ngo 
47327a286c47SDai Ngo 	if ((!target) || (!vap) || (!xvattr))
47337a286c47SDai Ngo 		return (-1);
47347a286c47SDai Ngo 
47357a286c47SDai Ngo 	/* validate reparse string */
47367a286c47SDai Ngo 	if (reparse_validate((const char *)target))
47377a286c47SDai Ngo 		return (-1);
47387a286c47SDai Ngo 
47397a286c47SDai Ngo 	xva_init(xvattr);
47407a286c47SDai Ngo 	xvattr->xva_vattr = *vap;
47417a286c47SDai Ngo 	xvattr->xva_vattr.va_mask |= AT_XVATTR;
47427a286c47SDai Ngo 	xoap = xva_getxoptattr(xvattr);
47437a286c47SDai Ngo 	ASSERT(xoap);
47447a286c47SDai Ngo 	XVA_SET_REQ(xvattr, XAT_REPARSE);
47457a286c47SDai Ngo 	xoap->xoa_reparse = 1;
47467a286c47SDai Ngo 
47477a286c47SDai Ngo 	return (0);
47487a286c47SDai Ngo }
47492f172c55SRobert Thurlow 
47502f172c55SRobert Thurlow /*
47512f172c55SRobert Thurlow  * Function to check whether a symlink is a reparse point.
47522f172c55SRobert Thurlow  * Return B_TRUE if it is a reparse point, else return B_FALSE
47532f172c55SRobert Thurlow  */
47542f172c55SRobert Thurlow boolean_t
vn_is_reparse(vnode_t * vp,cred_t * cr,caller_context_t * ct)47552f172c55SRobert Thurlow vn_is_reparse(vnode_t *vp, cred_t *cr, caller_context_t *ct)
47562f172c55SRobert Thurlow {
47572f172c55SRobert Thurlow 	xvattr_t xvattr;
47582f172c55SRobert Thurlow 	xoptattr_t *xoap;
47592f172c55SRobert Thurlow 
47602f172c55SRobert Thurlow 	if ((vp->v_type != VLNK) ||
47612f172c55SRobert Thurlow 	    !(vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR)))
47622f172c55SRobert Thurlow 		return (B_FALSE);
47632f172c55SRobert Thurlow 
47642f172c55SRobert Thurlow 	xva_init(&xvattr);
47652f172c55SRobert Thurlow 	xoap = xva_getxoptattr(&xvattr);
47662f172c55SRobert Thurlow 	ASSERT(xoap);
47672f172c55SRobert Thurlow 	XVA_SET_REQ(&xvattr, XAT_REPARSE);
47682f172c55SRobert Thurlow 
47692f172c55SRobert Thurlow 	if (VOP_GETATTR(vp, &xvattr.xva_vattr, 0, cr, ct))
47702f172c55SRobert Thurlow 		return (B_FALSE);
47712f172c55SRobert Thurlow 
47722f172c55SRobert Thurlow 	if ((!(xvattr.xva_vattr.va_mask & AT_XVATTR)) ||
47732f172c55SRobert Thurlow 	    (!(XVA_ISSET_RTN(&xvattr, XAT_REPARSE))))
47742f172c55SRobert Thurlow 		return (B_FALSE);
47752f172c55SRobert Thurlow 
47762f172c55SRobert Thurlow 	return (xoap->xoa_reparse ? B_TRUE : B_FALSE);
47772f172c55SRobert Thurlow }
4778