xref: /titanic_50/usr/src/uts/common/fs/vnode.c (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/t_lock.h>
45 #include <sys/errno.h>
46 #include <sys/cred.h>
47 #include <sys/user.h>
48 #include <sys/uio.h>
49 #include <sys/file.h>
50 #include <sys/pathname.h>
51 #include <sys/vfs.h>
52 #include <sys/vfs_opreg.h>
53 #include <sys/vnode.h>
54 #include <sys/rwstlock.h>
55 #include <sys/fem.h>
56 #include <sys/stat.h>
57 #include <sys/mode.h>
58 #include <sys/conf.h>
59 #include <sys/sysmacros.h>
60 #include <sys/cmn_err.h>
61 #include <sys/systm.h>
62 #include <sys/kmem.h>
63 #include <sys/debug.h>
64 #include <c2/audit.h>
65 #include <sys/acl.h>
66 #include <sys/nbmlock.h>
67 #include <sys/fcntl.h>
68 #include <fs/fs_subr.h>
69 
70 /* Determine if this vnode is a file that is read-only */
71 #define	ISROFILE(vp)	\
72 	((vp)->v_type != VCHR && (vp)->v_type != VBLK && \
73 	    (vp)->v_type != VFIFO && vn_is_readonly(vp))
74 
75 /* Tunable via /etc/system; used only by admin/install */
76 int nfs_global_client_only;
77 
78 /*
79  * Array of vopstats_t for per-FS-type vopstats.  This array has the same
80  * number of entries as and parallel to the vfssw table.  (Arguably, it could
81  * be part of the vfssw table.)  Once it's initialized, it's accessed using
82  * the same fstype index that is used to index into the vfssw table.
83  */
84 vopstats_t **vopstats_fstype;
85 
86 /* vopstats initialization template used for fast initialization via bcopy() */
87 static vopstats_t *vs_templatep;
88 
89 /* Kmem cache handle for vsk_anchor_t allocations */
90 kmem_cache_t *vsk_anchor_cache;
91 
92 /* file events cleanup routine */
93 extern void free_fopdata(vnode_t *);
94 
95 /*
96  * Root of AVL tree for the kstats associated with vopstats.  Lock protects
97  * updates to vsktat_tree.
98  */
99 avl_tree_t	vskstat_tree;
100 kmutex_t	vskstat_tree_lock;
101 
102 /* Global variable which enables/disables the vopstats collection */
103 int vopstats_enabled = 1;
104 
105 /*
106  * forward declarations for internal vnode specific data (vsd)
107  */
108 static void *vsd_realloc(void *, size_t, size_t);
109 
110 /*
111  * VSD -- VNODE SPECIFIC DATA
112  * The v_data pointer is typically used by a file system to store a
113  * pointer to the file system's private node (e.g. ufs inode, nfs rnode).
114  * However, there are times when additional project private data needs
115  * to be stored separately from the data (node) pointed to by v_data.
116  * This additional data could be stored by the file system itself or
117  * by a completely different kernel entity.  VSD provides a way for
118  * callers to obtain a key and store a pointer to private data associated
119  * with a vnode.
120  *
121  * Callers are responsible for protecting the vsd by holding v_lock
122  * for calls to vsd_set() and vsd_get().
123  */
124 
125 /*
126  * vsd_lock protects:
127  *   vsd_nkeys - creation and deletion of vsd keys
128  *   vsd_list - insertion and deletion of vsd_node in the vsd_list
129  *   vsd_destructor - adding and removing destructors to the list
130  */
131 static kmutex_t		vsd_lock;
132 static uint_t		vsd_nkeys;	 /* size of destructor array */
133 /* list of vsd_node's */
134 static list_t *vsd_list = NULL;
135 /* per-key destructor funcs */
136 static void 		(**vsd_destructor)(void *);
137 
138 /*
139  * The following is the common set of actions needed to update the
140  * vopstats structure from a vnode op.  Both VOPSTATS_UPDATE() and
141  * VOPSTATS_UPDATE_IO() do almost the same thing, except for the
142  * recording of the bytes transferred.  Since the code is similar
143  * but small, it is nearly a duplicate.  Consequently any changes
144  * to one may need to be reflected in the other.
145  * Rundown of the variables:
146  * vp - Pointer to the vnode
147  * counter - Partial name structure member to update in vopstats for counts
148  * bytecounter - Partial name structure member to update in vopstats for bytes
149  * bytesval - Value to update in vopstats for bytes
150  * fstype - Index into vsanchor_fstype[], same as index into vfssw[]
151  * vsp - Pointer to vopstats structure (either in vfs or vsanchor_fstype[i])
152  */
153 
154 #define	VOPSTATS_UPDATE(vp, counter) {					\
155 	vfs_t *vfsp = (vp)->v_vfsp;					\
156 	if (vfsp && vfsp->vfs_implp &&					\
157 	    (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {	\
158 		vopstats_t *vsp = &vfsp->vfs_vopstats;			\
159 		uint64_t *stataddr = &(vsp->n##counter.value.ui64);	\
160 		extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
161 		    size_t, uint64_t *);				\
162 		__dtrace_probe___fsinfo_##counter(vp, 0, stataddr);	\
163 		(*stataddr)++;						\
164 		if ((vsp = vfsp->vfs_fstypevsp) != NULL) {		\
165 			vsp->n##counter.value.ui64++;			\
166 		}							\
167 	}								\
168 }
169 
170 #define	VOPSTATS_UPDATE_IO(vp, counter, bytecounter, bytesval) {	\
171 	vfs_t *vfsp = (vp)->v_vfsp;					\
172 	if (vfsp && vfsp->vfs_implp &&					\
173 	    (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {	\
174 		vopstats_t *vsp = &vfsp->vfs_vopstats;			\
175 		uint64_t *stataddr = &(vsp->n##counter.value.ui64);	\
176 		extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
177 		    size_t, uint64_t *);				\
178 		__dtrace_probe___fsinfo_##counter(vp, bytesval, stataddr); \
179 		(*stataddr)++;						\
180 		vsp->bytecounter.value.ui64 += bytesval;		\
181 		if ((vsp = vfsp->vfs_fstypevsp) != NULL) {		\
182 			vsp->n##counter.value.ui64++;			\
183 			vsp->bytecounter.value.ui64 += bytesval;	\
184 		}							\
185 	}								\
186 }
187 
188 /*
189  * If the filesystem does not support XIDs map credential
190  * If the vfsp is NULL, perhaps we should also map?
191  */
192 #define	VOPXID_MAP_CR(vp, cr)	{					\
193 	vfs_t *vfsp = (vp)->v_vfsp;					\
194 	if (vfsp != NULL && (vfsp->vfs_flag & VFS_XID) == 0)		\
195 		cr = crgetmapped(cr);					\
196 	}
197 
198 /*
199  * Convert stat(2) formats to vnode types and vice versa.  (Knows about
200  * numerical order of S_IFMT and vnode types.)
201  */
202 enum vtype iftovt_tab[] = {
203 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
204 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
205 };
206 
207 ushort_t vttoif_tab[] = {
208 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO,
209 	S_IFDOOR, 0, S_IFSOCK, S_IFPORT, 0
210 };
211 
212 /*
213  * The system vnode cache.
214  */
215 
216 kmem_cache_t *vn_cache;
217 
218 
219 /*
220  * Vnode operations vector.
221  */
222 
223 static const fs_operation_trans_def_t vn_ops_table[] = {
224 	VOPNAME_OPEN, offsetof(struct vnodeops, vop_open),
225 	    fs_nosys, fs_nosys,
226 
227 	VOPNAME_CLOSE, offsetof(struct vnodeops, vop_close),
228 	    fs_nosys, fs_nosys,
229 
230 	VOPNAME_READ, offsetof(struct vnodeops, vop_read),
231 	    fs_nosys, fs_nosys,
232 
233 	VOPNAME_WRITE, offsetof(struct vnodeops, vop_write),
234 	    fs_nosys, fs_nosys,
235 
236 	VOPNAME_IOCTL, offsetof(struct vnodeops, vop_ioctl),
237 	    fs_nosys, fs_nosys,
238 
239 	VOPNAME_SETFL, offsetof(struct vnodeops, vop_setfl),
240 	    fs_setfl, fs_nosys,
241 
242 	VOPNAME_GETATTR, offsetof(struct vnodeops, vop_getattr),
243 	    fs_nosys, fs_nosys,
244 
245 	VOPNAME_SETATTR, offsetof(struct vnodeops, vop_setattr),
246 	    fs_nosys, fs_nosys,
247 
248 	VOPNAME_ACCESS, offsetof(struct vnodeops, vop_access),
249 	    fs_nosys, fs_nosys,
250 
251 	VOPNAME_LOOKUP, offsetof(struct vnodeops, vop_lookup),
252 	    fs_nosys, fs_nosys,
253 
254 	VOPNAME_CREATE, offsetof(struct vnodeops, vop_create),
255 	    fs_nosys, fs_nosys,
256 
257 	VOPNAME_REMOVE, offsetof(struct vnodeops, vop_remove),
258 	    fs_nosys, fs_nosys,
259 
260 	VOPNAME_LINK, offsetof(struct vnodeops, vop_link),
261 	    fs_nosys, fs_nosys,
262 
263 	VOPNAME_RENAME, offsetof(struct vnodeops, vop_rename),
264 	    fs_nosys, fs_nosys,
265 
266 	VOPNAME_MKDIR, offsetof(struct vnodeops, vop_mkdir),
267 	    fs_nosys, fs_nosys,
268 
269 	VOPNAME_RMDIR, offsetof(struct vnodeops, vop_rmdir),
270 	    fs_nosys, fs_nosys,
271 
272 	VOPNAME_READDIR, offsetof(struct vnodeops, vop_readdir),
273 	    fs_nosys, fs_nosys,
274 
275 	VOPNAME_SYMLINK, offsetof(struct vnodeops, vop_symlink),
276 	    fs_nosys, fs_nosys,
277 
278 	VOPNAME_READLINK, offsetof(struct vnodeops, vop_readlink),
279 	    fs_nosys, fs_nosys,
280 
281 	VOPNAME_FSYNC, offsetof(struct vnodeops, vop_fsync),
282 	    fs_nosys, fs_nosys,
283 
284 	VOPNAME_INACTIVE, offsetof(struct vnodeops, vop_inactive),
285 	    fs_nosys, fs_nosys,
286 
287 	VOPNAME_FID, offsetof(struct vnodeops, vop_fid),
288 	    fs_nosys, fs_nosys,
289 
290 	VOPNAME_RWLOCK, offsetof(struct vnodeops, vop_rwlock),
291 	    fs_rwlock, fs_rwlock,
292 
293 	VOPNAME_RWUNLOCK, offsetof(struct vnodeops, vop_rwunlock),
294 	    (fs_generic_func_p) fs_rwunlock,
295 	    (fs_generic_func_p) fs_rwunlock,	/* no errors allowed */
296 
297 	VOPNAME_SEEK, offsetof(struct vnodeops, vop_seek),
298 	    fs_nosys, fs_nosys,
299 
300 	VOPNAME_CMP, offsetof(struct vnodeops, vop_cmp),
301 	    fs_cmp, fs_cmp,		/* no errors allowed */
302 
303 	VOPNAME_FRLOCK, offsetof(struct vnodeops, vop_frlock),
304 	    fs_frlock, fs_nosys,
305 
306 	VOPNAME_SPACE, offsetof(struct vnodeops, vop_space),
307 	    fs_nosys, fs_nosys,
308 
309 	VOPNAME_REALVP, offsetof(struct vnodeops, vop_realvp),
310 	    fs_nosys, fs_nosys,
311 
312 	VOPNAME_GETPAGE, offsetof(struct vnodeops, vop_getpage),
313 	    fs_nosys, fs_nosys,
314 
315 	VOPNAME_PUTPAGE, offsetof(struct vnodeops, vop_putpage),
316 	    fs_nosys, fs_nosys,
317 
318 	VOPNAME_MAP, offsetof(struct vnodeops, vop_map),
319 	    (fs_generic_func_p) fs_nosys_map,
320 	    (fs_generic_func_p) fs_nosys_map,
321 
322 	VOPNAME_ADDMAP, offsetof(struct vnodeops, vop_addmap),
323 	    (fs_generic_func_p) fs_nosys_addmap,
324 	    (fs_generic_func_p) fs_nosys_addmap,
325 
326 	VOPNAME_DELMAP, offsetof(struct vnodeops, vop_delmap),
327 	    fs_nosys, fs_nosys,
328 
329 	VOPNAME_POLL, offsetof(struct vnodeops, vop_poll),
330 	    (fs_generic_func_p) fs_poll, (fs_generic_func_p) fs_nosys_poll,
331 
332 	VOPNAME_DUMP, offsetof(struct vnodeops, vop_dump),
333 	    fs_nosys, fs_nosys,
334 
335 	VOPNAME_PATHCONF, offsetof(struct vnodeops, vop_pathconf),
336 	    fs_pathconf, fs_nosys,
337 
338 	VOPNAME_PAGEIO, offsetof(struct vnodeops, vop_pageio),
339 	    fs_nosys, fs_nosys,
340 
341 	VOPNAME_DUMPCTL, offsetof(struct vnodeops, vop_dumpctl),
342 	    fs_nosys, fs_nosys,
343 
344 	VOPNAME_DISPOSE, offsetof(struct vnodeops, vop_dispose),
345 	    (fs_generic_func_p) fs_dispose,
346 	    (fs_generic_func_p) fs_nodispose,
347 
348 	VOPNAME_SETSECATTR, offsetof(struct vnodeops, vop_setsecattr),
349 	    fs_nosys, fs_nosys,
350 
351 	VOPNAME_GETSECATTR, offsetof(struct vnodeops, vop_getsecattr),
352 	    fs_fab_acl, fs_nosys,
353 
354 	VOPNAME_SHRLOCK, offsetof(struct vnodeops, vop_shrlock),
355 	    fs_shrlock, fs_nosys,
356 
357 	VOPNAME_VNEVENT, offsetof(struct vnodeops, vop_vnevent),
358 	    (fs_generic_func_p) fs_vnevent_nosupport,
359 	    (fs_generic_func_p) fs_vnevent_nosupport,
360 
361 	NULL, 0, NULL, NULL
362 };
363 
364 /* Extensible attribute (xva) routines. */
365 
366 /*
367  * Zero out the structure, set the size of the requested/returned bitmaps,
368  * set AT_XVATTR in the embedded vattr_t's va_mask, and set up the pointer
369  * to the returned attributes array.
370  */
371 void
372 xva_init(xvattr_t *xvap)
373 {
374 	bzero(xvap, sizeof (xvattr_t));
375 	xvap->xva_mapsize = XVA_MAPSIZE;
376 	xvap->xva_magic = XVA_MAGIC;
377 	xvap->xva_vattr.va_mask = AT_XVATTR;
378 	xvap->xva_rtnattrmapp = &(xvap->xva_rtnattrmap)[0];
379 }
380 
381 /*
382  * If AT_XVATTR is set, returns a pointer to the embedded xoptattr_t
383  * structure.  Otherwise, returns NULL.
384  */
385 xoptattr_t *
386 xva_getxoptattr(xvattr_t *xvap)
387 {
388 	xoptattr_t *xoap = NULL;
389 	if (xvap->xva_vattr.va_mask & AT_XVATTR)
390 		xoap = &xvap->xva_xoptattrs;
391 	return (xoap);
392 }
393 
394 /*
395  * Used by the AVL routines to compare two vsk_anchor_t structures in the tree.
396  * We use the f_fsid reported by VFS_STATVFS() since we use that for the
397  * kstat name.
398  */
399 static int
400 vska_compar(const void *n1, const void *n2)
401 {
402 	int ret;
403 	ulong_t p1 = ((vsk_anchor_t *)n1)->vsk_fsid;
404 	ulong_t p2 = ((vsk_anchor_t *)n2)->vsk_fsid;
405 
406 	if (p1 < p2) {
407 		ret = -1;
408 	} else if (p1 > p2) {
409 		ret = 1;
410 	} else {
411 		ret = 0;
412 	}
413 
414 	return (ret);
415 }
416 
417 /*
418  * Used to create a single template which will be bcopy()ed to a newly
419  * allocated vsanchor_combo_t structure in new_vsanchor(), below.
420  */
421 static vopstats_t *
422 create_vopstats_template()
423 {
424 	vopstats_t		*vsp;
425 
426 	vsp = kmem_alloc(sizeof (vopstats_t), KM_SLEEP);
427 	bzero(vsp, sizeof (*vsp));	/* Start fresh */
428 
429 	/* VOP_OPEN */
430 	kstat_named_init(&vsp->nopen, "nopen", KSTAT_DATA_UINT64);
431 	/* VOP_CLOSE */
432 	kstat_named_init(&vsp->nclose, "nclose", KSTAT_DATA_UINT64);
433 	/* VOP_READ I/O */
434 	kstat_named_init(&vsp->nread, "nread", KSTAT_DATA_UINT64);
435 	kstat_named_init(&vsp->read_bytes, "read_bytes", KSTAT_DATA_UINT64);
436 	/* VOP_WRITE I/O */
437 	kstat_named_init(&vsp->nwrite, "nwrite", KSTAT_DATA_UINT64);
438 	kstat_named_init(&vsp->write_bytes, "write_bytes", KSTAT_DATA_UINT64);
439 	/* VOP_IOCTL */
440 	kstat_named_init(&vsp->nioctl, "nioctl", KSTAT_DATA_UINT64);
441 	/* VOP_SETFL */
442 	kstat_named_init(&vsp->nsetfl, "nsetfl", KSTAT_DATA_UINT64);
443 	/* VOP_GETATTR */
444 	kstat_named_init(&vsp->ngetattr, "ngetattr", KSTAT_DATA_UINT64);
445 	/* VOP_SETATTR */
446 	kstat_named_init(&vsp->nsetattr, "nsetattr", KSTAT_DATA_UINT64);
447 	/* VOP_ACCESS */
448 	kstat_named_init(&vsp->naccess, "naccess", KSTAT_DATA_UINT64);
449 	/* VOP_LOOKUP */
450 	kstat_named_init(&vsp->nlookup, "nlookup", KSTAT_DATA_UINT64);
451 	/* VOP_CREATE */
452 	kstat_named_init(&vsp->ncreate, "ncreate", KSTAT_DATA_UINT64);
453 	/* VOP_REMOVE */
454 	kstat_named_init(&vsp->nremove, "nremove", KSTAT_DATA_UINT64);
455 	/* VOP_LINK */
456 	kstat_named_init(&vsp->nlink, "nlink", KSTAT_DATA_UINT64);
457 	/* VOP_RENAME */
458 	kstat_named_init(&vsp->nrename, "nrename", KSTAT_DATA_UINT64);
459 	/* VOP_MKDIR */
460 	kstat_named_init(&vsp->nmkdir, "nmkdir", KSTAT_DATA_UINT64);
461 	/* VOP_RMDIR */
462 	kstat_named_init(&vsp->nrmdir, "nrmdir", KSTAT_DATA_UINT64);
463 	/* VOP_READDIR I/O */
464 	kstat_named_init(&vsp->nreaddir, "nreaddir", KSTAT_DATA_UINT64);
465 	kstat_named_init(&vsp->readdir_bytes, "readdir_bytes",
466 	    KSTAT_DATA_UINT64);
467 	/* VOP_SYMLINK */
468 	kstat_named_init(&vsp->nsymlink, "nsymlink", KSTAT_DATA_UINT64);
469 	/* VOP_READLINK */
470 	kstat_named_init(&vsp->nreadlink, "nreadlink", KSTAT_DATA_UINT64);
471 	/* VOP_FSYNC */
472 	kstat_named_init(&vsp->nfsync, "nfsync", KSTAT_DATA_UINT64);
473 	/* VOP_INACTIVE */
474 	kstat_named_init(&vsp->ninactive, "ninactive", KSTAT_DATA_UINT64);
475 	/* VOP_FID */
476 	kstat_named_init(&vsp->nfid, "nfid", KSTAT_DATA_UINT64);
477 	/* VOP_RWLOCK */
478 	kstat_named_init(&vsp->nrwlock, "nrwlock", KSTAT_DATA_UINT64);
479 	/* VOP_RWUNLOCK */
480 	kstat_named_init(&vsp->nrwunlock, "nrwunlock", KSTAT_DATA_UINT64);
481 	/* VOP_SEEK */
482 	kstat_named_init(&vsp->nseek, "nseek", KSTAT_DATA_UINT64);
483 	/* VOP_CMP */
484 	kstat_named_init(&vsp->ncmp, "ncmp", KSTAT_DATA_UINT64);
485 	/* VOP_FRLOCK */
486 	kstat_named_init(&vsp->nfrlock, "nfrlock", KSTAT_DATA_UINT64);
487 	/* VOP_SPACE */
488 	kstat_named_init(&vsp->nspace, "nspace", KSTAT_DATA_UINT64);
489 	/* VOP_REALVP */
490 	kstat_named_init(&vsp->nrealvp, "nrealvp", KSTAT_DATA_UINT64);
491 	/* VOP_GETPAGE */
492 	kstat_named_init(&vsp->ngetpage, "ngetpage", KSTAT_DATA_UINT64);
493 	/* VOP_PUTPAGE */
494 	kstat_named_init(&vsp->nputpage, "nputpage", KSTAT_DATA_UINT64);
495 	/* VOP_MAP */
496 	kstat_named_init(&vsp->nmap, "nmap", KSTAT_DATA_UINT64);
497 	/* VOP_ADDMAP */
498 	kstat_named_init(&vsp->naddmap, "naddmap", KSTAT_DATA_UINT64);
499 	/* VOP_DELMAP */
500 	kstat_named_init(&vsp->ndelmap, "ndelmap", KSTAT_DATA_UINT64);
501 	/* VOP_POLL */
502 	kstat_named_init(&vsp->npoll, "npoll", KSTAT_DATA_UINT64);
503 	/* VOP_DUMP */
504 	kstat_named_init(&vsp->ndump, "ndump", KSTAT_DATA_UINT64);
505 	/* VOP_PATHCONF */
506 	kstat_named_init(&vsp->npathconf, "npathconf", KSTAT_DATA_UINT64);
507 	/* VOP_PAGEIO */
508 	kstat_named_init(&vsp->npageio, "npageio", KSTAT_DATA_UINT64);
509 	/* VOP_DUMPCTL */
510 	kstat_named_init(&vsp->ndumpctl, "ndumpctl", KSTAT_DATA_UINT64);
511 	/* VOP_DISPOSE */
512 	kstat_named_init(&vsp->ndispose, "ndispose", KSTAT_DATA_UINT64);
513 	/* VOP_SETSECATTR */
514 	kstat_named_init(&vsp->nsetsecattr, "nsetsecattr", KSTAT_DATA_UINT64);
515 	/* VOP_GETSECATTR */
516 	kstat_named_init(&vsp->ngetsecattr, "ngetsecattr", KSTAT_DATA_UINT64);
517 	/* VOP_SHRLOCK */
518 	kstat_named_init(&vsp->nshrlock, "nshrlock", KSTAT_DATA_UINT64);
519 	/* VOP_VNEVENT */
520 	kstat_named_init(&vsp->nvnevent, "nvnevent", KSTAT_DATA_UINT64);
521 
522 	return (vsp);
523 }
524 
525 /*
526  * Creates a kstat structure associated with a vopstats structure.
527  */
528 kstat_t *
529 new_vskstat(char *ksname, vopstats_t *vsp)
530 {
531 	kstat_t		*ksp;
532 
533 	if (!vopstats_enabled) {
534 		return (NULL);
535 	}
536 
537 	ksp = kstat_create("unix", 0, ksname, "misc", KSTAT_TYPE_NAMED,
538 	    sizeof (vopstats_t)/sizeof (kstat_named_t),
539 	    KSTAT_FLAG_VIRTUAL|KSTAT_FLAG_WRITABLE);
540 	if (ksp) {
541 		ksp->ks_data = vsp;
542 		kstat_install(ksp);
543 	}
544 
545 	return (ksp);
546 }
547 
548 /*
549  * Called from vfsinit() to initialize the support mechanisms for vopstats
550  */
551 void
552 vopstats_startup()
553 {
554 	if (!vopstats_enabled)
555 		return;
556 
557 	/*
558 	 * Creates the AVL tree which holds per-vfs vopstat anchors.  This
559 	 * is necessary since we need to check if a kstat exists before we
560 	 * attempt to create it.  Also, initialize its lock.
561 	 */
562 	avl_create(&vskstat_tree, vska_compar, sizeof (vsk_anchor_t),
563 	    offsetof(vsk_anchor_t, vsk_node));
564 	mutex_init(&vskstat_tree_lock, NULL, MUTEX_DEFAULT, NULL);
565 
566 	vsk_anchor_cache = kmem_cache_create("vsk_anchor_cache",
567 	    sizeof (vsk_anchor_t), sizeof (uintptr_t), NULL, NULL, NULL,
568 	    NULL, NULL, 0);
569 
570 	/*
571 	 * Set up the array of pointers for the vopstats-by-FS-type.
572 	 * The entries will be allocated/initialized as each file system
573 	 * goes through modload/mod_installfs.
574 	 */
575 	vopstats_fstype = (vopstats_t **)kmem_zalloc(
576 	    (sizeof (vopstats_t *) * nfstype), KM_SLEEP);
577 
578 	/* Set up the global vopstats initialization template */
579 	vs_templatep = create_vopstats_template();
580 }
581 
582 /*
583  * We need to have the all of the counters zeroed.
584  * The initialization of the vopstats_t includes on the order of
585  * 50 calls to kstat_named_init().  Rather that do that on every call,
586  * we do it once in a template (vs_templatep) then bcopy it over.
587  */
588 void
589 initialize_vopstats(vopstats_t *vsp)
590 {
591 	if (vsp == NULL)
592 		return;
593 
594 	bcopy(vs_templatep, vsp, sizeof (vopstats_t));
595 }
596 
597 /*
598  * If possible, determine which vopstats by fstype to use and
599  * return a pointer to the caller.
600  */
601 vopstats_t *
602 get_fstype_vopstats(vfs_t *vfsp, struct vfssw *vswp)
603 {
604 	int		fstype = 0;	/* Index into vfssw[] */
605 	vopstats_t	*vsp = NULL;
606 
607 	if (vfsp == NULL || (vfsp->vfs_flag & VFS_STATS) == 0 ||
608 	    !vopstats_enabled)
609 		return (NULL);
610 	/*
611 	 * Set up the fstype.  We go to so much trouble because all versions
612 	 * of NFS use the same fstype in their vfs even though they have
613 	 * distinct entries in the vfssw[] table.
614 	 * NOTE: A special vfs (e.g., EIO_vfs) may not have an entry.
615 	 */
616 	if (vswp) {
617 		fstype = vswp - vfssw;	/* Gets us the index */
618 	} else {
619 		fstype = vfsp->vfs_fstype;
620 	}
621 
622 	/*
623 	 * Point to the per-fstype vopstats. The only valid values are
624 	 * non-zero positive values less than the number of vfssw[] table
625 	 * entries.
626 	 */
627 	if (fstype > 0 && fstype < nfstype) {
628 		vsp = vopstats_fstype[fstype];
629 	}
630 
631 	return (vsp);
632 }
633 
634 /*
635  * Generate a kstat name, create the kstat structure, and allocate a
636  * vsk_anchor_t to hold it together.  Return the pointer to the vsk_anchor_t
637  * to the caller.  This must only be called from a mount.
638  */
639 vsk_anchor_t *
640 get_vskstat_anchor(vfs_t *vfsp)
641 {
642 	char		kstatstr[KSTAT_STRLEN]; /* kstat name for vopstats */
643 	statvfs64_t	statvfsbuf;		/* Needed to find f_fsid */
644 	vsk_anchor_t	*vskp = NULL;		/* vfs <--> kstat anchor */
645 	kstat_t		*ksp;			/* Ptr to new kstat */
646 	avl_index_t	where;			/* Location in the AVL tree */
647 
648 	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
649 	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
650 		return (NULL);
651 
652 	/* Need to get the fsid to build a kstat name */
653 	if (VFS_STATVFS(vfsp, &statvfsbuf) == 0) {
654 		/* Create a name for our kstats based on fsid */
655 		(void) snprintf(kstatstr, KSTAT_STRLEN, "%s%lx",
656 		    VOPSTATS_STR, statvfsbuf.f_fsid);
657 
658 		/* Allocate and initialize the vsk_anchor_t */
659 		vskp = kmem_cache_alloc(vsk_anchor_cache, KM_SLEEP);
660 		bzero(vskp, sizeof (*vskp));
661 		vskp->vsk_fsid = statvfsbuf.f_fsid;
662 
663 		mutex_enter(&vskstat_tree_lock);
664 		if (avl_find(&vskstat_tree, vskp, &where) == NULL) {
665 			avl_insert(&vskstat_tree, vskp, where);
666 			mutex_exit(&vskstat_tree_lock);
667 
668 			/*
669 			 * Now that we've got the anchor in the AVL
670 			 * tree, we can create the kstat.
671 			 */
672 			ksp = new_vskstat(kstatstr, &vfsp->vfs_vopstats);
673 			if (ksp) {
674 				vskp->vsk_ksp = ksp;
675 			}
676 		} else {
677 			/* Oops, found one! Release memory and lock. */
678 			mutex_exit(&vskstat_tree_lock);
679 			kmem_cache_free(vsk_anchor_cache, vskp);
680 			vskp = NULL;
681 		}
682 	}
683 	return (vskp);
684 }
685 
686 /*
687  * We're in the process of tearing down the vfs and need to cleanup
688  * the data structures associated with the vopstats. Must only be called
689  * from dounmount().
690  */
691 void
692 teardown_vopstats(vfs_t *vfsp)
693 {
694 	vsk_anchor_t	*vskap;
695 	avl_index_t	where;
696 
697 	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
698 	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
699 		return;
700 
701 	/* This is a safe check since VFS_STATS must be set (see above) */
702 	if ((vskap = vfsp->vfs_vskap) == NULL)
703 		return;
704 
705 	/* Whack the pointer right away */
706 	vfsp->vfs_vskap = NULL;
707 
708 	/* Lock the tree, remove the node, and delete the kstat */
709 	mutex_enter(&vskstat_tree_lock);
710 	if (avl_find(&vskstat_tree, vskap, &where)) {
711 		avl_remove(&vskstat_tree, vskap);
712 	}
713 
714 	if (vskap->vsk_ksp) {
715 		kstat_delete(vskap->vsk_ksp);
716 	}
717 	mutex_exit(&vskstat_tree_lock);
718 
719 	kmem_cache_free(vsk_anchor_cache, vskap);
720 }
721 
722 /*
723  * Read or write a vnode.  Called from kernel code.
724  */
725 int
726 vn_rdwr(
727 	enum uio_rw rw,
728 	struct vnode *vp,
729 	caddr_t base,
730 	ssize_t len,
731 	offset_t offset,
732 	enum uio_seg seg,
733 	int ioflag,
734 	rlim64_t ulimit,	/* meaningful only if rw is UIO_WRITE */
735 	cred_t *cr,
736 	ssize_t *residp)
737 {
738 	struct uio uio;
739 	struct iovec iov;
740 	int error;
741 	int in_crit = 0;
742 
743 	if (rw == UIO_WRITE && ISROFILE(vp))
744 		return (EROFS);
745 
746 	if (len < 0)
747 		return (EIO);
748 
749 	VOPXID_MAP_CR(vp, cr);
750 
751 	iov.iov_base = base;
752 	iov.iov_len = len;
753 	uio.uio_iov = &iov;
754 	uio.uio_iovcnt = 1;
755 	uio.uio_loffset = offset;
756 	uio.uio_segflg = (short)seg;
757 	uio.uio_resid = len;
758 	uio.uio_llimit = ulimit;
759 
760 	/*
761 	 * We have to enter the critical region before calling VOP_RWLOCK
762 	 * to avoid a deadlock with ufs.
763 	 */
764 	if (nbl_need_check(vp)) {
765 		int svmand;
766 
767 		nbl_start_crit(vp, RW_READER);
768 		in_crit = 1;
769 		error = nbl_svmand(vp, cr, &svmand);
770 		if (error != 0)
771 			goto done;
772 		if (nbl_conflict(vp, rw == UIO_WRITE ? NBL_WRITE : NBL_READ,
773 		    uio.uio_offset, uio.uio_resid, svmand, NULL)) {
774 			error = EACCES;
775 			goto done;
776 		}
777 	}
778 
779 	(void) VOP_RWLOCK(vp,
780 	    rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
781 	if (rw == UIO_WRITE) {
782 		uio.uio_fmode = FWRITE;
783 		uio.uio_extflg = UIO_COPY_DEFAULT;
784 		error = VOP_WRITE(vp, &uio, ioflag, cr, NULL);
785 	} else {
786 		uio.uio_fmode = FREAD;
787 		uio.uio_extflg = UIO_COPY_CACHED;
788 		error = VOP_READ(vp, &uio, ioflag, cr, NULL);
789 	}
790 	VOP_RWUNLOCK(vp,
791 	    rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
792 	if (residp)
793 		*residp = uio.uio_resid;
794 	else if (uio.uio_resid)
795 		error = EIO;
796 
797 done:
798 	if (in_crit)
799 		nbl_end_crit(vp);
800 	return (error);
801 }
802 
803 /*
804  * Release a vnode.  Call VOP_INACTIVE on last reference or
805  * decrement reference count.
806  *
807  * To avoid race conditions, the v_count is left at 1 for
808  * the call to VOP_INACTIVE. This prevents another thread
809  * from reclaiming and releasing the vnode *before* the
810  * VOP_INACTIVE routine has a chance to destroy the vnode.
811  * We can't have more than 1 thread calling VOP_INACTIVE
812  * on a vnode.
813  */
814 void
815 vn_rele(vnode_t *vp)
816 {
817 	if (vp->v_count == 0)
818 		cmn_err(CE_PANIC, "vn_rele: vnode ref count 0");
819 	mutex_enter(&vp->v_lock);
820 	if (vp->v_count == 1) {
821 		mutex_exit(&vp->v_lock);
822 		VOP_INACTIVE(vp, CRED(), NULL);
823 	} else {
824 		vp->v_count--;
825 		mutex_exit(&vp->v_lock);
826 	}
827 }
828 
829 /*
830  * Like vn_rele() except that it clears v_stream under v_lock.
831  * This is used by sockfs when it dismantels the association between
832  * the sockfs node and the vnode in the underlaying file system.
833  * v_lock has to be held to prevent a thread coming through the lookupname
834  * path from accessing a stream head that is going away.
835  */
836 void
837 vn_rele_stream(vnode_t *vp)
838 {
839 	if (vp->v_count == 0)
840 		cmn_err(CE_PANIC, "vn_rele: vnode ref count 0");
841 	mutex_enter(&vp->v_lock);
842 	vp->v_stream = NULL;
843 	if (vp->v_count == 1) {
844 		mutex_exit(&vp->v_lock);
845 		VOP_INACTIVE(vp, CRED(), NULL);
846 	} else {
847 		vp->v_count--;
848 		mutex_exit(&vp->v_lock);
849 	}
850 }
851 
852 int
853 vn_open(
854 	char *pnamep,
855 	enum uio_seg seg,
856 	int filemode,
857 	int createmode,
858 	struct vnode **vpp,
859 	enum create crwhy,
860 	mode_t umask)
861 {
862 	return (vn_openat(pnamep, seg, filemode, createmode, vpp, crwhy,
863 	    umask, NULL, -1));
864 }
865 
866 
867 /*
868  * Open/create a vnode.
869  * This may be callable by the kernel, the only known use
870  * of user context being that the current user credentials
871  * are used for permissions.  crwhy is defined iff filemode & FCREAT.
872  */
873 int
874 vn_openat(
875 	char *pnamep,
876 	enum uio_seg seg,
877 	int filemode,
878 	int createmode,
879 	struct vnode **vpp,
880 	enum create crwhy,
881 	mode_t umask,
882 	struct vnode *startvp,
883 	int fd)
884 {
885 	struct vnode *vp;
886 	int mode;
887 	int accessflags;
888 	int error;
889 	int in_crit = 0;
890 	int open_done = 0;
891 	int shrlock_done = 0;
892 	struct vattr vattr;
893 	enum symfollow follow;
894 	int estale_retry = 0;
895 	struct shrlock shr;
896 	struct shr_locowner shr_own;
897 
898 	mode = 0;
899 	accessflags = 0;
900 	if (filemode & FREAD)
901 		mode |= VREAD;
902 	if (filemode & (FWRITE|FTRUNC))
903 		mode |= VWRITE;
904 	if (filemode & FXATTRDIROPEN)
905 		mode |= VEXEC;
906 
907 	/* symlink interpretation */
908 	if (filemode & FNOFOLLOW)
909 		follow = NO_FOLLOW;
910 	else
911 		follow = FOLLOW;
912 
913 	if (filemode & FAPPEND)
914 		accessflags |= V_APPEND;
915 
916 top:
917 	if (filemode & FCREAT) {
918 		enum vcexcl excl;
919 
920 		/*
921 		 * Wish to create a file.
922 		 */
923 		vattr.va_type = VREG;
924 		vattr.va_mode = createmode;
925 		vattr.va_mask = AT_TYPE|AT_MODE;
926 		if (filemode & FTRUNC) {
927 			vattr.va_size = 0;
928 			vattr.va_mask |= AT_SIZE;
929 		}
930 		if (filemode & FEXCL)
931 			excl = EXCL;
932 		else
933 			excl = NONEXCL;
934 
935 		if (error =
936 		    vn_createat(pnamep, seg, &vattr, excl, mode, &vp, crwhy,
937 		    (filemode & ~(FTRUNC|FEXCL)), umask, startvp))
938 			return (error);
939 	} else {
940 		/*
941 		 * Wish to open a file.  Just look it up.
942 		 */
943 		if (error = lookupnameat(pnamep, seg, follow,
944 		    NULLVPP, &vp, startvp)) {
945 			if ((error == ESTALE) &&
946 			    fs_need_estale_retry(estale_retry++))
947 				goto top;
948 			return (error);
949 		}
950 
951 		/*
952 		 * Get the attributes to check whether file is large.
953 		 * We do this only if the FOFFMAX flag is not set and
954 		 * only for regular files.
955 		 */
956 
957 		if (!(filemode & FOFFMAX) && (vp->v_type == VREG)) {
958 			vattr.va_mask = AT_SIZE;
959 			if ((error = VOP_GETATTR(vp, &vattr, 0,
960 			    CRED(), NULL))) {
961 				goto out;
962 			}
963 			if (vattr.va_size > (u_offset_t)MAXOFF32_T) {
964 				/*
965 				 * Large File API - regular open fails
966 				 * if FOFFMAX flag is set in file mode
967 				 */
968 				error = EOVERFLOW;
969 				goto out;
970 			}
971 		}
972 		/*
973 		 * Can't write directories, active texts, or
974 		 * read-only filesystems.  Can't truncate files
975 		 * on which mandatory locking is in effect.
976 		 */
977 		if (filemode & (FWRITE|FTRUNC)) {
978 			/*
979 			 * Allow writable directory if VDIROPEN flag is set.
980 			 */
981 			if (vp->v_type == VDIR && !(vp->v_flag & VDIROPEN)) {
982 				error = EISDIR;
983 				goto out;
984 			}
985 			if (ISROFILE(vp)) {
986 				error = EROFS;
987 				goto out;
988 			}
989 			/*
990 			 * Can't truncate files on which
991 			 * sysv mandatory locking is in effect.
992 			 */
993 			if (filemode & FTRUNC) {
994 				vnode_t *rvp;
995 
996 				if (VOP_REALVP(vp, &rvp, NULL) != 0)
997 					rvp = vp;
998 				if (rvp->v_filocks != NULL) {
999 					vattr.va_mask = AT_MODE;
1000 					if ((error = VOP_GETATTR(vp,
1001 					    &vattr, 0, CRED(), NULL)) == 0 &&
1002 					    MANDLOCK(vp, vattr.va_mode))
1003 						error = EAGAIN;
1004 				}
1005 			}
1006 			if (error)
1007 				goto out;
1008 		}
1009 		/*
1010 		 * Check permissions.
1011 		 */
1012 		if (error = VOP_ACCESS(vp, mode, accessflags, CRED(), NULL))
1013 			goto out;
1014 	}
1015 
1016 	/*
1017 	 * Do remaining checks for FNOFOLLOW and FNOLINKS.
1018 	 */
1019 	if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) {
1020 		error = ELOOP;
1021 		goto out;
1022 	}
1023 	if (filemode & FNOLINKS) {
1024 		vattr.va_mask = AT_NLINK;
1025 		if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))) {
1026 			goto out;
1027 		}
1028 		if (vattr.va_nlink != 1) {
1029 			error = EMLINK;
1030 			goto out;
1031 		}
1032 	}
1033 
1034 	/*
1035 	 * Opening a socket corresponding to the AF_UNIX pathname
1036 	 * in the filesystem name space is not supported.
1037 	 * However, VSOCK nodes in namefs are supported in order
1038 	 * to make fattach work for sockets.
1039 	 *
1040 	 * XXX This uses VOP_REALVP to distinguish between
1041 	 * an unopened namefs node (where VOP_REALVP returns a
1042 	 * different VSOCK vnode) and a VSOCK created by vn_create
1043 	 * in some file system (where VOP_REALVP would never return
1044 	 * a different vnode).
1045 	 */
1046 	if (vp->v_type == VSOCK) {
1047 		struct vnode *nvp;
1048 
1049 		error = VOP_REALVP(vp, &nvp, NULL);
1050 		if (error != 0 || nvp == NULL || nvp == vp ||
1051 		    nvp->v_type != VSOCK) {
1052 			error = EOPNOTSUPP;
1053 			goto out;
1054 		}
1055 	}
1056 
1057 	if ((vp->v_type == VREG) && nbl_need_check(vp)) {
1058 		/* get share reservation */
1059 		shr.s_access = 0;
1060 		if (filemode & FWRITE)
1061 			shr.s_access |= F_WRACC;
1062 		if (filemode & FREAD)
1063 			shr.s_access |= F_RDACC;
1064 		shr.s_deny = 0;
1065 		shr.s_sysid = 0;
1066 		shr.s_pid = ttoproc(curthread)->p_pid;
1067 		shr_own.sl_pid = shr.s_pid;
1068 		shr_own.sl_id = fd;
1069 		shr.s_own_len = sizeof (shr_own);
1070 		shr.s_owner = (caddr_t)&shr_own;
1071 		error = VOP_SHRLOCK(vp, F_SHARE_NBMAND, &shr, filemode, CRED(),
1072 		    NULL);
1073 		if (error)
1074 			goto out;
1075 		shrlock_done = 1;
1076 
1077 		/* nbmand conflict check if truncating file */
1078 		if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
1079 			nbl_start_crit(vp, RW_READER);
1080 			in_crit = 1;
1081 
1082 			vattr.va_mask = AT_SIZE;
1083 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
1084 				goto out;
1085 			if (nbl_conflict(vp, NBL_WRITE, 0, vattr.va_size, 0,
1086 			    NULL)) {
1087 				error = EACCES;
1088 				goto out;
1089 			}
1090 		}
1091 	}
1092 
1093 	/*
1094 	 * Do opening protocol.
1095 	 */
1096 	error = VOP_OPEN(&vp, filemode, CRED(), NULL);
1097 	if (error)
1098 		goto out;
1099 	open_done = 1;
1100 
1101 	/*
1102 	 * Truncate if required.
1103 	 */
1104 	if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
1105 		vattr.va_size = 0;
1106 		vattr.va_mask = AT_SIZE;
1107 		if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0)
1108 			goto out;
1109 	}
1110 out:
1111 	ASSERT(vp->v_count > 0);
1112 
1113 	if (in_crit) {
1114 		nbl_end_crit(vp);
1115 		in_crit = 0;
1116 	}
1117 	if (error) {
1118 		if (open_done) {
1119 			(void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED(),
1120 			    NULL);
1121 			open_done = 0;
1122 			shrlock_done = 0;
1123 		}
1124 		if (shrlock_done) {
1125 			(void) VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, CRED(),
1126 			    NULL);
1127 			shrlock_done = 0;
1128 		}
1129 
1130 		/*
1131 		 * The following clause was added to handle a problem
1132 		 * with NFS consistency.  It is possible that a lookup
1133 		 * of the file to be opened succeeded, but the file
1134 		 * itself doesn't actually exist on the server.  This
1135 		 * is chiefly due to the DNLC containing an entry for
1136 		 * the file which has been removed on the server.  In
1137 		 * this case, we just start over.  If there was some
1138 		 * other cause for the ESTALE error, then the lookup
1139 		 * of the file will fail and the error will be returned
1140 		 * above instead of looping around from here.
1141 		 */
1142 		VN_RELE(vp);
1143 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1144 			goto top;
1145 	} else
1146 		*vpp = vp;
1147 	return (error);
1148 }
1149 
1150 /*
1151  * The following two accessor functions are for the NFSv4 server.  Since there
1152  * is no VOP_OPEN_UP/DOWNGRADE we need a way for the NFS server to keep the
1153  * vnode open counts correct when a client "upgrades" an open or does an
1154  * open_downgrade.  In NFS, an upgrade or downgrade can not only change the
1155  * open mode (add or subtract read or write), but also change the share/deny
1156  * modes.  However, share reservations are not integrated with OPEN, yet, so
1157  * we need to handle each separately.  These functions are cleaner than having
1158  * the NFS server manipulate the counts directly, however, nobody else should
1159  * use these functions.
1160  */
1161 void
1162 vn_open_upgrade(
1163 	vnode_t *vp,
1164 	int filemode)
1165 {
1166 	ASSERT(vp->v_type == VREG);
1167 
1168 	if (filemode & FREAD)
1169 		atomic_add_32(&(vp->v_rdcnt), 1);
1170 	if (filemode & FWRITE)
1171 		atomic_add_32(&(vp->v_wrcnt), 1);
1172 
1173 }
1174 
1175 void
1176 vn_open_downgrade(
1177 	vnode_t *vp,
1178 	int filemode)
1179 {
1180 	ASSERT(vp->v_type == VREG);
1181 
1182 	if (filemode & FREAD) {
1183 		ASSERT(vp->v_rdcnt > 0);
1184 		atomic_add_32(&(vp->v_rdcnt), -1);
1185 	}
1186 	if (filemode & FWRITE) {
1187 		ASSERT(vp->v_wrcnt > 0);
1188 		atomic_add_32(&(vp->v_wrcnt), -1);
1189 	}
1190 
1191 }
1192 
1193 int
1194 vn_create(
1195 	char *pnamep,
1196 	enum uio_seg seg,
1197 	struct vattr *vap,
1198 	enum vcexcl excl,
1199 	int mode,
1200 	struct vnode **vpp,
1201 	enum create why,
1202 	int flag,
1203 	mode_t umask)
1204 {
1205 	return (vn_createat(pnamep, seg, vap, excl, mode, vpp, why, flag,
1206 	    umask, NULL));
1207 }
1208 
1209 /*
1210  * Create a vnode (makenode).
1211  */
1212 int
1213 vn_createat(
1214 	char *pnamep,
1215 	enum uio_seg seg,
1216 	struct vattr *vap,
1217 	enum vcexcl excl,
1218 	int mode,
1219 	struct vnode **vpp,
1220 	enum create why,
1221 	int flag,
1222 	mode_t umask,
1223 	struct vnode *startvp)
1224 {
1225 	struct vnode *dvp;	/* ptr to parent dir vnode */
1226 	struct vnode *vp = NULL;
1227 	struct pathname pn;
1228 	int error;
1229 	int in_crit = 0;
1230 	struct vattr vattr;
1231 	enum symfollow follow;
1232 	int estale_retry = 0;
1233 
1234 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
1235 
1236 	/* symlink interpretation */
1237 	if ((flag & FNOFOLLOW) || excl == EXCL)
1238 		follow = NO_FOLLOW;
1239 	else
1240 		follow = FOLLOW;
1241 	flag &= ~(FNOFOLLOW|FNOLINKS);
1242 
1243 top:
1244 	/*
1245 	 * Lookup directory.
1246 	 * If new object is a file, call lower level to create it.
1247 	 * Note that it is up to the lower level to enforce exclusive
1248 	 * creation, if the file is already there.
1249 	 * This allows the lower level to do whatever
1250 	 * locking or protocol that is needed to prevent races.
1251 	 * If the new object is directory call lower level to make
1252 	 * the new directory, with "." and "..".
1253 	 */
1254 	if (error = pn_get(pnamep, seg, &pn))
1255 		return (error);
1256 	if (audit_active)
1257 		audit_vncreate_start();
1258 	dvp = NULL;
1259 	*vpp = NULL;
1260 	/*
1261 	 * lookup will find the parent directory for the vnode.
1262 	 * When it is done the pn holds the name of the entry
1263 	 * in the directory.
1264 	 * If this is a non-exclusive create we also find the node itself.
1265 	 */
1266 	error = lookuppnat(&pn, NULL, follow, &dvp,
1267 	    (excl == EXCL) ? NULLVPP : vpp, startvp);
1268 	if (error) {
1269 		pn_free(&pn);
1270 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1271 			goto top;
1272 		if (why == CRMKDIR && error == EINVAL)
1273 			error = EEXIST;		/* SVID */
1274 		return (error);
1275 	}
1276 
1277 	if (why != CRMKNOD)
1278 		vap->va_mode &= ~VSVTX;
1279 
1280 	/*
1281 	 * If default ACLs are defined for the directory don't apply the
1282 	 * umask if umask is passed.
1283 	 */
1284 
1285 	if (umask) {
1286 
1287 		vsecattr_t vsec;
1288 
1289 		vsec.vsa_aclcnt = 0;
1290 		vsec.vsa_aclentp = NULL;
1291 		vsec.vsa_dfaclcnt = 0;
1292 		vsec.vsa_dfaclentp = NULL;
1293 		vsec.vsa_mask = VSA_DFACLCNT;
1294 		error = VOP_GETSECATTR(dvp, &vsec, 0, CRED(), NULL);
1295 		/*
1296 		 * If error is ENOSYS then treat it as no error
1297 		 * Don't want to force all file systems to support
1298 		 * aclent_t style of ACL's.
1299 		 */
1300 		if (error == ENOSYS)
1301 			error = 0;
1302 		if (error) {
1303 			if (*vpp != NULL)
1304 				VN_RELE(*vpp);
1305 			goto out;
1306 		} else {
1307 			/*
1308 			 * Apply the umask if no default ACLs.
1309 			 */
1310 			if (vsec.vsa_dfaclcnt == 0)
1311 				vap->va_mode &= ~umask;
1312 
1313 			/*
1314 			 * VOP_GETSECATTR() may have allocated memory for
1315 			 * ACLs we didn't request, so double-check and
1316 			 * free it if necessary.
1317 			 */
1318 			if (vsec.vsa_aclcnt && vsec.vsa_aclentp != NULL)
1319 				kmem_free((caddr_t)vsec.vsa_aclentp,
1320 				    vsec.vsa_aclcnt * sizeof (aclent_t));
1321 			if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp != NULL)
1322 				kmem_free((caddr_t)vsec.vsa_dfaclentp,
1323 				    vsec.vsa_dfaclcnt * sizeof (aclent_t));
1324 		}
1325 	}
1326 
1327 	/*
1328 	 * In general we want to generate EROFS if the file system is
1329 	 * readonly.  However, POSIX (IEEE Std. 1003.1) section 5.3.1
1330 	 * documents the open system call, and it says that O_CREAT has no
1331 	 * effect if the file already exists.  Bug 1119649 states
1332 	 * that open(path, O_CREAT, ...) fails when attempting to open an
1333 	 * existing file on a read only file system.  Thus, the first part
1334 	 * of the following if statement has 3 checks:
1335 	 *	if the file exists &&
1336 	 *		it is being open with write access &&
1337 	 *		the file system is read only
1338 	 *	then generate EROFS
1339 	 */
1340 	if ((*vpp != NULL && (mode & VWRITE) && ISROFILE(*vpp)) ||
1341 	    (*vpp == NULL && dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
1342 		if (*vpp)
1343 			VN_RELE(*vpp);
1344 		error = EROFS;
1345 	} else if (excl == NONEXCL && *vpp != NULL) {
1346 		vnode_t *rvp;
1347 
1348 		/*
1349 		 * File already exists.  If a mandatory lock has been
1350 		 * applied, return error.
1351 		 */
1352 		vp = *vpp;
1353 		if (VOP_REALVP(vp, &rvp, NULL) != 0)
1354 			rvp = vp;
1355 		if ((vap->va_mask & AT_SIZE) && nbl_need_check(vp)) {
1356 			nbl_start_crit(vp, RW_READER);
1357 			in_crit = 1;
1358 		}
1359 		if (rvp->v_filocks != NULL || rvp->v_shrlocks != NULL) {
1360 			vattr.va_mask = AT_MODE|AT_SIZE;
1361 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL)) {
1362 				goto out;
1363 			}
1364 			if (MANDLOCK(vp, vattr.va_mode)) {
1365 				error = EAGAIN;
1366 				goto out;
1367 			}
1368 			/*
1369 			 * File cannot be truncated if non-blocking mandatory
1370 			 * locks are currently on the file.
1371 			 */
1372 			if ((vap->va_mask & AT_SIZE) && in_crit) {
1373 				u_offset_t offset;
1374 				ssize_t length;
1375 
1376 				offset = vap->va_size > vattr.va_size ?
1377 				    vattr.va_size : vap->va_size;
1378 				length = vap->va_size > vattr.va_size ?
1379 				    vap->va_size - vattr.va_size :
1380 				    vattr.va_size - vap->va_size;
1381 				if (nbl_conflict(vp, NBL_WRITE, offset,
1382 				    length, 0, NULL)) {
1383 					error = EACCES;
1384 					goto out;
1385 				}
1386 			}
1387 		}
1388 
1389 		/*
1390 		 * If the file is the root of a VFS, we've crossed a
1391 		 * mount point and the "containing" directory that we
1392 		 * acquired above (dvp) is irrelevant because it's in
1393 		 * a different file system.  We apply VOP_CREATE to the
1394 		 * target itself instead of to the containing directory
1395 		 * and supply a null path name to indicate (conventionally)
1396 		 * the node itself as the "component" of interest.
1397 		 *
1398 		 * The intercession of the file system is necessary to
1399 		 * ensure that the appropriate permission checks are
1400 		 * done.
1401 		 */
1402 		if (vp->v_flag & VROOT) {
1403 			ASSERT(why != CRMKDIR);
1404 			error = VOP_CREATE(vp, "", vap, excl, mode, vpp,
1405 			    CRED(), flag, NULL, NULL);
1406 			/*
1407 			 * If the create succeeded, it will have created
1408 			 * a new reference to the vnode.  Give up the
1409 			 * original reference.  The assertion should not
1410 			 * get triggered because NBMAND locks only apply to
1411 			 * VREG files.  And if in_crit is non-zero for some
1412 			 * reason, detect that here, rather than when we
1413 			 * deference a null vp.
1414 			 */
1415 			ASSERT(in_crit == 0);
1416 			VN_RELE(vp);
1417 			vp = NULL;
1418 			goto out;
1419 		}
1420 
1421 		/*
1422 		 * Large File API - non-large open (FOFFMAX flag not set)
1423 		 * of regular file fails if the file size exceeds MAXOFF32_T.
1424 		 */
1425 		if (why != CRMKDIR &&
1426 		    !(flag & FOFFMAX) &&
1427 		    (vp->v_type == VREG)) {
1428 			vattr.va_mask = AT_SIZE;
1429 			if ((error = VOP_GETATTR(vp, &vattr, 0,
1430 			    CRED(), NULL))) {
1431 				goto out;
1432 			}
1433 			if ((vattr.va_size > (u_offset_t)MAXOFF32_T)) {
1434 				error = EOVERFLOW;
1435 				goto out;
1436 			}
1437 		}
1438 	}
1439 
1440 	if (error == 0) {
1441 		/*
1442 		 * Call mkdir() if specified, otherwise create().
1443 		 */
1444 		int must_be_dir = pn_fixslash(&pn);	/* trailing '/'? */
1445 
1446 		if (why == CRMKDIR)
1447 			/*
1448 			 * N.B., if vn_createat() ever requests
1449 			 * case-insensitive behavior then it will need
1450 			 * to be passed to VOP_MKDIR().  VOP_CREATE()
1451 			 * will already get it via "flag"
1452 			 */
1453 			error = VOP_MKDIR(dvp, pn.pn_path, vap, vpp, CRED(),
1454 			    NULL, 0, NULL);
1455 		else if (!must_be_dir)
1456 			error = VOP_CREATE(dvp, pn.pn_path, vap,
1457 			    excl, mode, vpp, CRED(), flag, NULL, NULL);
1458 		else
1459 			error = ENOTDIR;
1460 	}
1461 
1462 out:
1463 
1464 	if (audit_active)
1465 		audit_vncreate_finish(*vpp, error);
1466 	if (in_crit) {
1467 		nbl_end_crit(vp);
1468 		in_crit = 0;
1469 	}
1470 	if (vp != NULL) {
1471 		VN_RELE(vp);
1472 		vp = NULL;
1473 	}
1474 	pn_free(&pn);
1475 	VN_RELE(dvp);
1476 	/*
1477 	 * The following clause was added to handle a problem
1478 	 * with NFS consistency.  It is possible that a lookup
1479 	 * of the file to be created succeeded, but the file
1480 	 * itself doesn't actually exist on the server.  This
1481 	 * is chiefly due to the DNLC containing an entry for
1482 	 * the file which has been removed on the server.  In
1483 	 * this case, we just start over.  If there was some
1484 	 * other cause for the ESTALE error, then the lookup
1485 	 * of the file will fail and the error will be returned
1486 	 * above instead of looping around from here.
1487 	 */
1488 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1489 		goto top;
1490 	return (error);
1491 }
1492 
1493 int
1494 vn_link(char *from, char *to, enum uio_seg seg)
1495 {
1496 	struct vnode *fvp;		/* from vnode ptr */
1497 	struct vnode *tdvp;		/* to directory vnode ptr */
1498 	struct pathname pn;
1499 	int error;
1500 	struct vattr vattr;
1501 	dev_t fsid;
1502 	int estale_retry = 0;
1503 
1504 top:
1505 	fvp = tdvp = NULL;
1506 	if (error = pn_get(to, seg, &pn))
1507 		return (error);
1508 	if (error = lookupname(from, seg, NO_FOLLOW, NULLVPP, &fvp))
1509 		goto out;
1510 	if (error = lookuppn(&pn, NULL, NO_FOLLOW, &tdvp, NULLVPP))
1511 		goto out;
1512 	/*
1513 	 * Make sure both source vnode and target directory vnode are
1514 	 * in the same vfs and that it is writeable.
1515 	 */
1516 	vattr.va_mask = AT_FSID;
1517 	if (error = VOP_GETATTR(fvp, &vattr, 0, CRED(), NULL))
1518 		goto out;
1519 	fsid = vattr.va_fsid;
1520 	vattr.va_mask = AT_FSID;
1521 	if (error = VOP_GETATTR(tdvp, &vattr, 0, CRED(), NULL))
1522 		goto out;
1523 	if (fsid != vattr.va_fsid) {
1524 		error = EXDEV;
1525 		goto out;
1526 	}
1527 	if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) {
1528 		error = EROFS;
1529 		goto out;
1530 	}
1531 	/*
1532 	 * Do the link.
1533 	 */
1534 	(void) pn_fixslash(&pn);
1535 	error = VOP_LINK(tdvp, fvp, pn.pn_path, CRED(), NULL, 0);
1536 out:
1537 	pn_free(&pn);
1538 	if (fvp)
1539 		VN_RELE(fvp);
1540 	if (tdvp)
1541 		VN_RELE(tdvp);
1542 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1543 		goto top;
1544 	return (error);
1545 }
1546 
1547 int
1548 vn_rename(char *from, char *to, enum uio_seg seg)
1549 {
1550 	return (vn_renameat(NULL, from, NULL, to, seg));
1551 }
1552 
1553 int
1554 vn_renameat(vnode_t *fdvp, char *fname, vnode_t *tdvp,
1555 		char *tname, enum uio_seg seg)
1556 {
1557 	int error;
1558 	struct vattr vattr;
1559 	struct pathname fpn;		/* from pathname */
1560 	struct pathname tpn;		/* to pathname */
1561 	dev_t fsid;
1562 	int in_crit_src, in_crit_targ;
1563 	vnode_t *fromvp, *fvp;
1564 	vnode_t *tovp, *targvp;
1565 	int estale_retry = 0;
1566 
1567 top:
1568 	fvp = fromvp = tovp = targvp = NULL;
1569 	in_crit_src = in_crit_targ = 0;
1570 	/*
1571 	 * Get to and from pathnames.
1572 	 */
1573 	if (error = pn_get(fname, seg, &fpn))
1574 		return (error);
1575 	if (error = pn_get(tname, seg, &tpn)) {
1576 		pn_free(&fpn);
1577 		return (error);
1578 	}
1579 
1580 	/*
1581 	 * First we need to resolve the correct directories
1582 	 * The passed in directories may only be a starting point,
1583 	 * but we need the real directories the file(s) live in.
1584 	 * For example the fname may be something like usr/lib/sparc
1585 	 * and we were passed in the / directory, but we need to
1586 	 * use the lib directory for the rename.
1587 	 */
1588 
1589 	if (audit_active)
1590 		audit_setfsat_path(1);
1591 	/*
1592 	 * Lookup to and from directories.
1593 	 */
1594 	if (error = lookuppnat(&fpn, NULL, NO_FOLLOW, &fromvp, &fvp, fdvp)) {
1595 		goto out;
1596 	}
1597 
1598 	/*
1599 	 * Make sure there is an entry.
1600 	 */
1601 	if (fvp == NULL) {
1602 		error = ENOENT;
1603 		goto out;
1604 	}
1605 
1606 	if (audit_active)
1607 		audit_setfsat_path(3);
1608 	if (error = lookuppnat(&tpn, NULL, NO_FOLLOW, &tovp, &targvp, tdvp)) {
1609 		goto out;
1610 	}
1611 
1612 	/*
1613 	 * Make sure both the from vnode directory and the to directory
1614 	 * are in the same vfs and the to directory is writable.
1615 	 * We check fsid's, not vfs pointers, so loopback fs works.
1616 	 */
1617 	if (fromvp != tovp) {
1618 		vattr.va_mask = AT_FSID;
1619 		if (error = VOP_GETATTR(fromvp, &vattr, 0, CRED(), NULL))
1620 			goto out;
1621 		fsid = vattr.va_fsid;
1622 		vattr.va_mask = AT_FSID;
1623 		if (error = VOP_GETATTR(tovp, &vattr, 0, CRED(), NULL))
1624 			goto out;
1625 		if (fsid != vattr.va_fsid) {
1626 			error = EXDEV;
1627 			goto out;
1628 		}
1629 	}
1630 
1631 	if (tovp->v_vfsp->vfs_flag & VFS_RDONLY) {
1632 		error = EROFS;
1633 		goto out;
1634 	}
1635 
1636 	if (targvp && (fvp != targvp)) {
1637 		nbl_start_crit(targvp, RW_READER);
1638 		in_crit_targ = 1;
1639 		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
1640 			error = EACCES;
1641 			goto out;
1642 		}
1643 	}
1644 
1645 	if (nbl_need_check(fvp)) {
1646 		nbl_start_crit(fvp, RW_READER);
1647 		in_crit_src = 1;
1648 		if (nbl_conflict(fvp, NBL_RENAME, 0, 0, 0, NULL)) {
1649 			error = EACCES;
1650 			goto out;
1651 		}
1652 	}
1653 
1654 	/*
1655 	 * Do the rename.
1656 	 */
1657 	(void) pn_fixslash(&tpn);
1658 	error = VOP_RENAME(fromvp, fpn.pn_path, tovp, tpn.pn_path, CRED(),
1659 	    NULL, 0);
1660 
1661 out:
1662 	pn_free(&fpn);
1663 	pn_free(&tpn);
1664 	if (in_crit_src)
1665 		nbl_end_crit(fvp);
1666 	if (in_crit_targ)
1667 		nbl_end_crit(targvp);
1668 	if (fromvp)
1669 		VN_RELE(fromvp);
1670 	if (tovp)
1671 		VN_RELE(tovp);
1672 	if (targvp)
1673 		VN_RELE(targvp);
1674 	if (fvp)
1675 		VN_RELE(fvp);
1676 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1677 		goto top;
1678 	return (error);
1679 }
1680 
1681 /*
1682  * Remove a file or directory.
1683  */
1684 int
1685 vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
1686 {
1687 	return (vn_removeat(NULL, fnamep, seg, dirflag));
1688 }
1689 
1690 int
1691 vn_removeat(vnode_t *startvp, char *fnamep, enum uio_seg seg, enum rm dirflag)
1692 {
1693 	struct vnode *vp;		/* entry vnode */
1694 	struct vnode *dvp;		/* ptr to parent dir vnode */
1695 	struct vnode *coveredvp;
1696 	struct pathname pn;		/* name of entry */
1697 	enum vtype vtype;
1698 	int error;
1699 	struct vfs *vfsp;
1700 	struct vfs *dvfsp;	/* ptr to parent dir vfs */
1701 	int in_crit = 0;
1702 	int estale_retry = 0;
1703 
1704 top:
1705 	if (error = pn_get(fnamep, seg, &pn))
1706 		return (error);
1707 	dvp = vp = NULL;
1708 	if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &dvp, &vp, startvp)) {
1709 		pn_free(&pn);
1710 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1711 			goto top;
1712 		return (error);
1713 	}
1714 
1715 	/*
1716 	 * Make sure there is an entry.
1717 	 */
1718 	if (vp == NULL) {
1719 		error = ENOENT;
1720 		goto out;
1721 	}
1722 
1723 	vfsp = vp->v_vfsp;
1724 	dvfsp = dvp->v_vfsp;
1725 
1726 	/*
1727 	 * If the named file is the root of a mounted filesystem, fail,
1728 	 * unless it's marked unlinkable.  In that case, unmount the
1729 	 * filesystem and proceed to unlink the covered vnode.  (If the
1730 	 * covered vnode is a directory, use rmdir instead of unlink,
1731 	 * to avoid file system corruption.)
1732 	 */
1733 	if (vp->v_flag & VROOT) {
1734 		if ((vfsp->vfs_flag & VFS_UNLINKABLE) == 0) {
1735 			error = EBUSY;
1736 			goto out;
1737 		}
1738 
1739 		/*
1740 		 * Namefs specific code starts here.
1741 		 */
1742 
1743 		if (dirflag == RMDIRECTORY) {
1744 			/*
1745 			 * User called rmdir(2) on a file that has
1746 			 * been namefs mounted on top of.  Since
1747 			 * namefs doesn't allow directories to
1748 			 * be mounted on other files we know
1749 			 * vp is not of type VDIR so fail to operation.
1750 			 */
1751 			error = ENOTDIR;
1752 			goto out;
1753 		}
1754 
1755 		/*
1756 		 * If VROOT is still set after grabbing vp->v_lock,
1757 		 * noone has finished nm_unmount so far and coveredvp
1758 		 * is valid.
1759 		 * If we manage to grab vn_vfswlock(coveredvp) before releasing
1760 		 * vp->v_lock, any race window is eliminated.
1761 		 */
1762 
1763 		mutex_enter(&vp->v_lock);
1764 		if ((vp->v_flag & VROOT) == 0) {
1765 			/* Someone beat us to the unmount */
1766 			mutex_exit(&vp->v_lock);
1767 			error = EBUSY;
1768 			goto out;
1769 		}
1770 		vfsp = vp->v_vfsp;
1771 		coveredvp = vfsp->vfs_vnodecovered;
1772 		ASSERT(coveredvp);
1773 		/*
1774 		 * Note: Implementation of vn_vfswlock shows that ordering of
1775 		 * v_lock / vn_vfswlock is not an issue here.
1776 		 */
1777 		error = vn_vfswlock(coveredvp);
1778 		mutex_exit(&vp->v_lock);
1779 
1780 		if (error)
1781 			goto out;
1782 
1783 		VN_HOLD(coveredvp);
1784 		VN_RELE(vp);
1785 		error = dounmount(vfsp, 0, CRED());
1786 
1787 		/*
1788 		 * Unmounted the namefs file system; now get
1789 		 * the object it was mounted over.
1790 		 */
1791 		vp = coveredvp;
1792 		/*
1793 		 * If namefs was mounted over a directory, then
1794 		 * we want to use rmdir() instead of unlink().
1795 		 */
1796 		if (vp->v_type == VDIR)
1797 			dirflag = RMDIRECTORY;
1798 
1799 		if (error)
1800 			goto out;
1801 	}
1802 
1803 	/*
1804 	 * Make sure filesystem is writeable.
1805 	 * We check the parent directory's vfs in case this is an lofs vnode.
1806 	 */
1807 	if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) {
1808 		error = EROFS;
1809 		goto out;
1810 	}
1811 
1812 	vtype = vp->v_type;
1813 
1814 	/*
1815 	 * If there is the possibility of an nbmand share reservation, make
1816 	 * sure it's okay to remove the file.  Keep a reference to the
1817 	 * vnode, so that we can exit the nbl critical region after
1818 	 * calling VOP_REMOVE.
1819 	 * If there is no possibility of an nbmand share reservation,
1820 	 * release the vnode reference now.  Filesystems like NFS may
1821 	 * behave differently if there is an extra reference, so get rid of
1822 	 * this one.  Fortunately, we can't have nbmand mounts on NFS
1823 	 * filesystems.
1824 	 */
1825 	if (nbl_need_check(vp)) {
1826 		nbl_start_crit(vp, RW_READER);
1827 		in_crit = 1;
1828 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
1829 			error = EACCES;
1830 			goto out;
1831 		}
1832 	} else {
1833 		VN_RELE(vp);
1834 		vp = NULL;
1835 	}
1836 
1837 	if (dirflag == RMDIRECTORY) {
1838 		/*
1839 		 * Caller is using rmdir(2), which can only be applied to
1840 		 * directories.
1841 		 */
1842 		if (vtype != VDIR) {
1843 			error = ENOTDIR;
1844 		} else {
1845 			vnode_t *cwd;
1846 			proc_t *pp = curproc;
1847 
1848 			mutex_enter(&pp->p_lock);
1849 			cwd = PTOU(pp)->u_cdir;
1850 			VN_HOLD(cwd);
1851 			mutex_exit(&pp->p_lock);
1852 			error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED(),
1853 			    NULL, 0);
1854 			VN_RELE(cwd);
1855 		}
1856 	} else {
1857 		/*
1858 		 * Unlink(2) can be applied to anything.
1859 		 */
1860 		error = VOP_REMOVE(dvp, pn.pn_path, CRED(), NULL, 0);
1861 	}
1862 
1863 out:
1864 	pn_free(&pn);
1865 	if (in_crit) {
1866 		nbl_end_crit(vp);
1867 		in_crit = 0;
1868 	}
1869 	if (vp != NULL)
1870 		VN_RELE(vp);
1871 	if (dvp != NULL)
1872 		VN_RELE(dvp);
1873 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1874 		goto top;
1875 	return (error);
1876 }
1877 
1878 /*
1879  * Utility function to compare equality of vnodes.
1880  * Compare the underlying real vnodes, if there are underlying vnodes.
1881  * This is a more thorough comparison than the VN_CMP() macro provides.
1882  */
1883 int
1884 vn_compare(vnode_t *vp1, vnode_t *vp2)
1885 {
1886 	vnode_t *realvp;
1887 
1888 	if (vp1 != NULL && VOP_REALVP(vp1, &realvp, NULL) == 0)
1889 		vp1 = realvp;
1890 	if (vp2 != NULL && VOP_REALVP(vp2, &realvp, NULL) == 0)
1891 		vp2 = realvp;
1892 	return (VN_CMP(vp1, vp2));
1893 }
1894 
1895 /*
1896  * The number of locks to hash into.  This value must be a power
1897  * of 2 minus 1 and should probably also be prime.
1898  */
1899 #define	NUM_BUCKETS	1023
1900 
1901 struct  vn_vfslocks_bucket {
1902 	kmutex_t vb_lock;
1903 	vn_vfslocks_entry_t *vb_list;
1904 	char pad[64 - sizeof (kmutex_t) - sizeof (void *)];
1905 };
1906 
1907 /*
1908  * Total number of buckets will be NUM_BUCKETS + 1 .
1909  */
1910 
1911 #pragma	align	64(vn_vfslocks_buckets)
1912 static	struct vn_vfslocks_bucket	vn_vfslocks_buckets[NUM_BUCKETS + 1];
1913 
1914 #define	VN_VFSLOCKS_SHIFT	9
1915 
1916 #define	VN_VFSLOCKS_HASH(vfsvpptr)	\
1917 	((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS)
1918 
1919 /*
1920  * vn_vfslocks_getlock() uses an HASH scheme to generate
1921  * rwstlock using vfs/vnode pointer passed to it.
1922  *
1923  * vn_vfslocks_rele() releases a reference in the
1924  * HASH table which allows the entry allocated by
1925  * vn_vfslocks_getlock() to be freed at a later
1926  * stage when the refcount drops to zero.
1927  */
1928 
1929 vn_vfslocks_entry_t *
1930 vn_vfslocks_getlock(void *vfsvpptr)
1931 {
1932 	struct vn_vfslocks_bucket *bp;
1933 	vn_vfslocks_entry_t *vep;
1934 	vn_vfslocks_entry_t *tvep;
1935 
1936 	ASSERT(vfsvpptr != NULL);
1937 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)];
1938 
1939 	mutex_enter(&bp->vb_lock);
1940 	for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
1941 		if (vep->ve_vpvfs == vfsvpptr) {
1942 			vep->ve_refcnt++;
1943 			mutex_exit(&bp->vb_lock);
1944 			return (vep);
1945 		}
1946 	}
1947 	mutex_exit(&bp->vb_lock);
1948 	vep = kmem_alloc(sizeof (*vep), KM_SLEEP);
1949 	rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL);
1950 	vep->ve_vpvfs = (char *)vfsvpptr;
1951 	vep->ve_refcnt = 1;
1952 	mutex_enter(&bp->vb_lock);
1953 	for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) {
1954 		if (tvep->ve_vpvfs == vfsvpptr) {
1955 			tvep->ve_refcnt++;
1956 			mutex_exit(&bp->vb_lock);
1957 
1958 			/*
1959 			 * There is already an entry in the hash
1960 			 * destroy what we just allocated.
1961 			 */
1962 			rwst_destroy(&vep->ve_lock);
1963 			kmem_free(vep, sizeof (*vep));
1964 			return (tvep);
1965 		}
1966 	}
1967 	vep->ve_next = bp->vb_list;
1968 	bp->vb_list = vep;
1969 	mutex_exit(&bp->vb_lock);
1970 	return (vep);
1971 }
1972 
1973 void
1974 vn_vfslocks_rele(vn_vfslocks_entry_t *vepent)
1975 {
1976 	struct vn_vfslocks_bucket *bp;
1977 	vn_vfslocks_entry_t *vep;
1978 	vn_vfslocks_entry_t *pvep;
1979 
1980 	ASSERT(vepent != NULL);
1981 	ASSERT(vepent->ve_vpvfs != NULL);
1982 
1983 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)];
1984 
1985 	mutex_enter(&bp->vb_lock);
1986 	vepent->ve_refcnt--;
1987 
1988 	if ((int32_t)vepent->ve_refcnt < 0)
1989 		cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative");
1990 
1991 	if (vepent->ve_refcnt == 0) {
1992 		for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
1993 			if (vep->ve_vpvfs == vepent->ve_vpvfs) {
1994 				if (bp->vb_list == vep)
1995 					bp->vb_list = vep->ve_next;
1996 				else {
1997 					/* LINTED */
1998 					pvep->ve_next = vep->ve_next;
1999 				}
2000 				mutex_exit(&bp->vb_lock);
2001 				rwst_destroy(&vep->ve_lock);
2002 				kmem_free(vep, sizeof (*vep));
2003 				return;
2004 			}
2005 			pvep = vep;
2006 		}
2007 		cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found");
2008 	}
2009 	mutex_exit(&bp->vb_lock);
2010 }
2011 
2012 /*
2013  * vn_vfswlock_wait is used to implement a lock which is logically a writers
2014  * lock protecting the v_vfsmountedhere field.
2015  * vn_vfswlock_wait has been modified to be similar to vn_vfswlock,
2016  * except that it blocks to acquire the lock VVFSLOCK.
2017  *
2018  * traverse() and routines re-implementing part of traverse (e.g. autofs)
2019  * need to hold this lock. mount(), vn_rename(), vn_remove() and so on
2020  * need the non-blocking version of the writers lock i.e. vn_vfswlock
2021  */
2022 int
2023 vn_vfswlock_wait(vnode_t *vp)
2024 {
2025 	int retval;
2026 	vn_vfslocks_entry_t *vpvfsentry;
2027 	ASSERT(vp != NULL);
2028 
2029 	vpvfsentry = vn_vfslocks_getlock(vp);
2030 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER);
2031 
2032 	if (retval == EINTR) {
2033 		vn_vfslocks_rele(vpvfsentry);
2034 		return (EINTR);
2035 	}
2036 	return (retval);
2037 }
2038 
2039 int
2040 vn_vfsrlock_wait(vnode_t *vp)
2041 {
2042 	int retval;
2043 	vn_vfslocks_entry_t *vpvfsentry;
2044 	ASSERT(vp != NULL);
2045 
2046 	vpvfsentry = vn_vfslocks_getlock(vp);
2047 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER);
2048 
2049 	if (retval == EINTR) {
2050 		vn_vfslocks_rele(vpvfsentry);
2051 		return (EINTR);
2052 	}
2053 
2054 	return (retval);
2055 }
2056 
2057 
2058 /*
2059  * vn_vfswlock is used to implement a lock which is logically a writers lock
2060  * protecting the v_vfsmountedhere field.
2061  */
2062 int
2063 vn_vfswlock(vnode_t *vp)
2064 {
2065 	vn_vfslocks_entry_t *vpvfsentry;
2066 
2067 	/*
2068 	 * If vp is NULL then somebody is trying to lock the covered vnode
2069 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
2070 	 * only happen when unmounting /.  Since that operation will fail
2071 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
2072 	 */
2073 	if (vp == NULL)
2074 		return (EBUSY);
2075 
2076 	vpvfsentry = vn_vfslocks_getlock(vp);
2077 
2078 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
2079 		return (0);
2080 
2081 	vn_vfslocks_rele(vpvfsentry);
2082 	return (EBUSY);
2083 }
2084 
2085 int
2086 vn_vfsrlock(vnode_t *vp)
2087 {
2088 	vn_vfslocks_entry_t *vpvfsentry;
2089 
2090 	/*
2091 	 * If vp is NULL then somebody is trying to lock the covered vnode
2092 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
2093 	 * only happen when unmounting /.  Since that operation will fail
2094 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
2095 	 */
2096 	if (vp == NULL)
2097 		return (EBUSY);
2098 
2099 	vpvfsentry = vn_vfslocks_getlock(vp);
2100 
2101 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
2102 		return (0);
2103 
2104 	vn_vfslocks_rele(vpvfsentry);
2105 	return (EBUSY);
2106 }
2107 
2108 void
2109 vn_vfsunlock(vnode_t *vp)
2110 {
2111 	vn_vfslocks_entry_t *vpvfsentry;
2112 
2113 	/*
2114 	 * ve_refcnt needs to be decremented twice.
2115 	 * 1. To release refernce after a call to vn_vfslocks_getlock()
2116 	 * 2. To release the reference from the locking routines like
2117 	 *    vn_vfsrlock/vn_vfswlock etc,.
2118 	 */
2119 	vpvfsentry = vn_vfslocks_getlock(vp);
2120 	vn_vfslocks_rele(vpvfsentry);
2121 
2122 	rwst_exit(&vpvfsentry->ve_lock);
2123 	vn_vfslocks_rele(vpvfsentry);
2124 }
2125 
2126 int
2127 vn_vfswlock_held(vnode_t *vp)
2128 {
2129 	int held;
2130 	vn_vfslocks_entry_t *vpvfsentry;
2131 
2132 	ASSERT(vp != NULL);
2133 
2134 	vpvfsentry = vn_vfslocks_getlock(vp);
2135 	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
2136 
2137 	vn_vfslocks_rele(vpvfsentry);
2138 	return (held);
2139 }
2140 
2141 
2142 int
2143 vn_make_ops(
2144 	const char *name,			/* Name of file system */
2145 	const fs_operation_def_t *templ,	/* Operation specification */
2146 	vnodeops_t **actual)			/* Return the vnodeops */
2147 {
2148 	int unused_ops;
2149 	int error;
2150 
2151 	*actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP);
2152 
2153 	(*actual)->vnop_name = name;
2154 
2155 	error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ);
2156 	if (error) {
2157 		kmem_free(*actual, sizeof (vnodeops_t));
2158 	}
2159 
2160 #if DEBUG
2161 	if (unused_ops != 0)
2162 		cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied "
2163 		    "but not used", name, unused_ops);
2164 #endif
2165 
2166 	return (error);
2167 }
2168 
2169 /*
2170  * Free the vnodeops created as a result of vn_make_ops()
2171  */
2172 void
2173 vn_freevnodeops(vnodeops_t *vnops)
2174 {
2175 	kmem_free(vnops, sizeof (vnodeops_t));
2176 }
2177 
2178 /*
2179  * Vnode cache.
2180  */
2181 
2182 /* ARGSUSED */
2183 static int
2184 vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
2185 {
2186 	struct vnode *vp;
2187 
2188 	vp = buf;
2189 
2190 	mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
2191 	cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL);
2192 	rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL);
2193 	rw_init(&vp->v_mslock, NULL, RW_DEFAULT, NULL);
2194 	vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2195 	vp->v_path = NULL;
2196 	vp->v_mpssdata = NULL;
2197 	vp->v_vsd = NULL;
2198 	vp->v_fopdata = NULL;
2199 
2200 	return (0);
2201 }
2202 
2203 /* ARGSUSED */
2204 static void
2205 vn_cache_destructor(void *buf, void *cdrarg)
2206 {
2207 	struct vnode *vp;
2208 
2209 	vp = buf;
2210 
2211 	rw_destroy(&vp->v_mslock);
2212 	rw_destroy(&vp->v_nbllock);
2213 	cv_destroy(&vp->v_cv);
2214 	mutex_destroy(&vp->v_lock);
2215 }
2216 
2217 void
2218 vn_create_cache(void)
2219 {
2220 	vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode), 64,
2221 	    vn_cache_constructor, vn_cache_destructor, NULL, NULL,
2222 	    NULL, 0);
2223 }
2224 
2225 void
2226 vn_destroy_cache(void)
2227 {
2228 	kmem_cache_destroy(vn_cache);
2229 }
2230 
2231 /*
2232  * Used by file systems when fs-specific nodes (e.g., ufs inodes) are
2233  * cached by the file system and vnodes remain associated.
2234  */
2235 void
2236 vn_recycle(vnode_t *vp)
2237 {
2238 	ASSERT(vp->v_pages == NULL);
2239 
2240 	/*
2241 	 * XXX - This really belongs in vn_reinit(), but we have some issues
2242 	 * with the counts.  Best to have it here for clean initialization.
2243 	 */
2244 	vp->v_rdcnt = 0;
2245 	vp->v_wrcnt = 0;
2246 	vp->v_mmap_read = 0;
2247 	vp->v_mmap_write = 0;
2248 
2249 	/*
2250 	 * If FEM was in use, make sure everything gets cleaned up
2251 	 * NOTE: vp->v_femhead is initialized to NULL in the vnode
2252 	 * constructor.
2253 	 */
2254 	if (vp->v_femhead) {
2255 		/* XXX - There should be a free_femhead() that does all this */
2256 		ASSERT(vp->v_femhead->femh_list == NULL);
2257 		mutex_destroy(&vp->v_femhead->femh_lock);
2258 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2259 		vp->v_femhead = NULL;
2260 	}
2261 	if (vp->v_path) {
2262 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2263 		vp->v_path = NULL;
2264 	}
2265 
2266 	if (vp->v_fopdata != NULL) {
2267 		free_fopdata(vp);
2268 	}
2269 	vp->v_mpssdata = NULL;
2270 	vsd_free(vp);
2271 }
2272 
2273 /*
2274  * Used to reset the vnode fields including those that are directly accessible
2275  * as well as those which require an accessor function.
2276  *
2277  * Does not initialize:
2278  *	synchronization objects: v_lock, v_nbllock, v_cv
2279  *	v_data (since FS-nodes and vnodes point to each other and should
2280  *		be updated simultaneously)
2281  *	v_op (in case someone needs to make a VOP call on this object)
2282  */
2283 void
2284 vn_reinit(vnode_t *vp)
2285 {
2286 	vp->v_count = 1;
2287 	vp->v_vfsp = NULL;
2288 	vp->v_stream = NULL;
2289 	vp->v_vfsmountedhere = NULL;
2290 	vp->v_flag = 0;
2291 	vp->v_type = VNON;
2292 	vp->v_rdev = NODEV;
2293 
2294 	vp->v_filocks = NULL;
2295 	vp->v_shrlocks = NULL;
2296 	vp->v_pages = NULL;
2297 	vp->v_npages = 0;
2298 	vp->v_msnpages = 0;
2299 	vp->v_scanfront = NULL;
2300 	vp->v_scanback = NULL;
2301 
2302 	vp->v_locality = NULL;
2303 	vp->v_scantime = 0;
2304 	vp->v_mset = 0;
2305 	vp->v_msflags = 0;
2306 	vp->v_msnext = NULL;
2307 	vp->v_msprev = NULL;
2308 	vp->v_xattrdir = NULL;
2309 
2310 	/* Handles v_femhead, v_path, and the r/w/map counts */
2311 	vn_recycle(vp);
2312 }
2313 
2314 vnode_t *
2315 vn_alloc(int kmflag)
2316 {
2317 	vnode_t *vp;
2318 
2319 	vp = kmem_cache_alloc(vn_cache, kmflag);
2320 
2321 	if (vp != NULL) {
2322 		vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2323 		vp->v_fopdata = NULL;
2324 		vn_reinit(vp);
2325 	}
2326 
2327 	return (vp);
2328 }
2329 
2330 void
2331 vn_free(vnode_t *vp)
2332 {
2333 	ASSERT(vp->v_shrlocks == NULL);
2334 	ASSERT(vp->v_filocks == NULL);
2335 
2336 	/*
2337 	 * Some file systems call vn_free() with v_count of zero,
2338 	 * some with v_count of 1.  In any case, the value should
2339 	 * never be anything else.
2340 	 */
2341 	ASSERT((vp->v_count == 0) || (vp->v_count == 1));
2342 	if (vp->v_path != NULL) {
2343 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2344 		vp->v_path = NULL;
2345 	}
2346 
2347 	/* If FEM was in use, make sure everything gets cleaned up */
2348 	if (vp->v_femhead) {
2349 		/* XXX - There should be a free_femhead() that does all this */
2350 		ASSERT(vp->v_femhead->femh_list == NULL);
2351 		mutex_destroy(&vp->v_femhead->femh_lock);
2352 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2353 		vp->v_femhead = NULL;
2354 	}
2355 
2356 	if (vp->v_fopdata != NULL) {
2357 		free_fopdata(vp);
2358 	}
2359 	vp->v_mpssdata = NULL;
2360 	vsd_free(vp);
2361 	kmem_cache_free(vn_cache, vp);
2362 }
2363 
2364 /*
2365  * vnode status changes, should define better states than 1, 0.
2366  */
2367 void
2368 vn_reclaim(vnode_t *vp)
2369 {
2370 	vfs_t   *vfsp = vp->v_vfsp;
2371 
2372 	if (vfsp == NULL ||
2373 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2374 		return;
2375 	}
2376 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED);
2377 }
2378 
2379 void
2380 vn_idle(vnode_t *vp)
2381 {
2382 	vfs_t   *vfsp = vp->v_vfsp;
2383 
2384 	if (vfsp == NULL ||
2385 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2386 		return;
2387 	}
2388 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_IDLED);
2389 }
2390 void
2391 vn_exists(vnode_t *vp)
2392 {
2393 	vfs_t   *vfsp = vp->v_vfsp;
2394 
2395 	if (vfsp == NULL ||
2396 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2397 		return;
2398 	}
2399 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS);
2400 }
2401 
2402 void
2403 vn_invalid(vnode_t *vp)
2404 {
2405 	vfs_t   *vfsp = vp->v_vfsp;
2406 
2407 	if (vfsp == NULL ||
2408 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2409 		return;
2410 	}
2411 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED);
2412 }
2413 
2414 /* Vnode event notification */
2415 
2416 int
2417 vnevent_support(vnode_t *vp, caller_context_t *ct)
2418 {
2419 	if (vp == NULL)
2420 		return (EINVAL);
2421 
2422 	return (VOP_VNEVENT(vp, VE_SUPPORT, NULL, NULL, ct));
2423 }
2424 
2425 void
2426 vnevent_rename_src(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2427 {
2428 	if (vp == NULL || vp->v_femhead == NULL) {
2429 		return;
2430 	}
2431 	(void) VOP_VNEVENT(vp, VE_RENAME_SRC, dvp, name, ct);
2432 }
2433 
2434 void
2435 vnevent_rename_dest(vnode_t *vp, vnode_t *dvp, char *name,
2436     caller_context_t *ct)
2437 {
2438 	if (vp == NULL || vp->v_femhead == NULL) {
2439 		return;
2440 	}
2441 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST, dvp, name, ct);
2442 }
2443 
2444 void
2445 vnevent_rename_dest_dir(vnode_t *vp, caller_context_t *ct)
2446 {
2447 	if (vp == NULL || vp->v_femhead == NULL) {
2448 		return;
2449 	}
2450 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST_DIR, NULL, NULL, ct);
2451 }
2452 
2453 void
2454 vnevent_remove(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2455 {
2456 	if (vp == NULL || vp->v_femhead == NULL) {
2457 		return;
2458 	}
2459 	(void) VOP_VNEVENT(vp, VE_REMOVE, dvp, name, ct);
2460 }
2461 
2462 void
2463 vnevent_rmdir(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2464 {
2465 	if (vp == NULL || vp->v_femhead == NULL) {
2466 		return;
2467 	}
2468 	(void) VOP_VNEVENT(vp, VE_RMDIR, dvp, name, ct);
2469 }
2470 
2471 void
2472 vnevent_create(vnode_t *vp, caller_context_t *ct)
2473 {
2474 	if (vp == NULL || vp->v_femhead == NULL) {
2475 		return;
2476 	}
2477 	(void) VOP_VNEVENT(vp, VE_CREATE, NULL, NULL, ct);
2478 }
2479 
2480 void
2481 vnevent_link(vnode_t *vp, caller_context_t *ct)
2482 {
2483 	if (vp == NULL || vp->v_femhead == NULL) {
2484 		return;
2485 	}
2486 	(void) VOP_VNEVENT(vp, VE_LINK, NULL, NULL, ct);
2487 }
2488 
2489 void
2490 vnevent_mountedover(vnode_t *vp, caller_context_t *ct)
2491 {
2492 	if (vp == NULL || vp->v_femhead == NULL) {
2493 		return;
2494 	}
2495 	(void) VOP_VNEVENT(vp, VE_MOUNTEDOVER, NULL, NULL, ct);
2496 }
2497 
2498 /*
2499  * Vnode accessors.
2500  */
2501 
2502 int
2503 vn_is_readonly(vnode_t *vp)
2504 {
2505 	return (vp->v_vfsp->vfs_flag & VFS_RDONLY);
2506 }
2507 
2508 int
2509 vn_has_flocks(vnode_t *vp)
2510 {
2511 	return (vp->v_filocks != NULL);
2512 }
2513 
2514 int
2515 vn_has_mandatory_locks(vnode_t *vp, int mode)
2516 {
2517 	return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode)));
2518 }
2519 
2520 int
2521 vn_has_cached_data(vnode_t *vp)
2522 {
2523 	return (vp->v_pages != NULL);
2524 }
2525 
2526 /*
2527  * Return 0 if the vnode in question shouldn't be permitted into a zone via
2528  * zone_enter(2).
2529  */
2530 int
2531 vn_can_change_zones(vnode_t *vp)
2532 {
2533 	struct vfssw *vswp;
2534 	int allow = 1;
2535 	vnode_t *rvp;
2536 
2537 	if (nfs_global_client_only != 0)
2538 		return (1);
2539 
2540 	/*
2541 	 * We always want to look at the underlying vnode if there is one.
2542 	 */
2543 	if (VOP_REALVP(vp, &rvp, NULL) != 0)
2544 		rvp = vp;
2545 	/*
2546 	 * Some pseudo filesystems (including doorfs) don't actually register
2547 	 * their vfsops_t, so the following may return NULL; we happily let
2548 	 * such vnodes switch zones.
2549 	 */
2550 	vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp));
2551 	if (vswp != NULL) {
2552 		if (vswp->vsw_flag & VSW_NOTZONESAFE)
2553 			allow = 0;
2554 		vfs_unrefvfssw(vswp);
2555 	}
2556 	return (allow);
2557 }
2558 
2559 /*
2560  * Return nonzero if the vnode is a mount point, zero if not.
2561  */
2562 int
2563 vn_ismntpt(vnode_t *vp)
2564 {
2565 	return (vp->v_vfsmountedhere != NULL);
2566 }
2567 
2568 /* Retrieve the vfs (if any) mounted on this vnode */
2569 vfs_t *
2570 vn_mountedvfs(vnode_t *vp)
2571 {
2572 	return (vp->v_vfsmountedhere);
2573 }
2574 
2575 /*
2576  * vn_has_other_opens() checks whether a particular file is opened by more than
2577  * just the caller and whether the open is for read and/or write.
2578  * This routine is for calling after the caller has already called VOP_OPEN()
2579  * and the caller wishes to know if they are the only one with it open for
2580  * the mode(s) specified.
2581  *
2582  * Vnode counts are only kept on regular files (v_type=VREG).
2583  */
2584 int
2585 vn_has_other_opens(
2586 	vnode_t *vp,
2587 	v_mode_t mode)
2588 {
2589 
2590 	ASSERT(vp != NULL);
2591 
2592 	switch (mode) {
2593 	case V_WRITE:
2594 		if (vp->v_wrcnt > 1)
2595 			return (V_TRUE);
2596 		break;
2597 	case V_RDORWR:
2598 		if ((vp->v_rdcnt > 1) || (vp->v_wrcnt > 1))
2599 			return (V_TRUE);
2600 		break;
2601 	case V_RDANDWR:
2602 		if ((vp->v_rdcnt > 1) && (vp->v_wrcnt > 1))
2603 			return (V_TRUE);
2604 		break;
2605 	case V_READ:
2606 		if (vp->v_rdcnt > 1)
2607 			return (V_TRUE);
2608 		break;
2609 	}
2610 
2611 	return (V_FALSE);
2612 }
2613 
2614 /*
2615  * vn_is_opened() checks whether a particular file is opened and
2616  * whether the open is for read and/or write.
2617  *
2618  * Vnode counts are only kept on regular files (v_type=VREG).
2619  */
2620 int
2621 vn_is_opened(
2622 	vnode_t *vp,
2623 	v_mode_t mode)
2624 {
2625 
2626 	ASSERT(vp != NULL);
2627 
2628 	switch (mode) {
2629 	case V_WRITE:
2630 		if (vp->v_wrcnt)
2631 			return (V_TRUE);
2632 		break;
2633 	case V_RDANDWR:
2634 		if (vp->v_rdcnt && vp->v_wrcnt)
2635 			return (V_TRUE);
2636 		break;
2637 	case V_RDORWR:
2638 		if (vp->v_rdcnt || vp->v_wrcnt)
2639 			return (V_TRUE);
2640 		break;
2641 	case V_READ:
2642 		if (vp->v_rdcnt)
2643 			return (V_TRUE);
2644 		break;
2645 	}
2646 
2647 	return (V_FALSE);
2648 }
2649 
2650 /*
2651  * vn_is_mapped() checks whether a particular file is mapped and whether
2652  * the file is mapped read and/or write.
2653  */
2654 int
2655 vn_is_mapped(
2656 	vnode_t *vp,
2657 	v_mode_t mode)
2658 {
2659 
2660 	ASSERT(vp != NULL);
2661 
2662 #if !defined(_LP64)
2663 	switch (mode) {
2664 	/*
2665 	 * The atomic_add_64_nv functions force atomicity in the
2666 	 * case of 32 bit architectures. Otherwise the 64 bit values
2667 	 * require two fetches. The value of the fields may be
2668 	 * (potentially) changed between the first fetch and the
2669 	 * second
2670 	 */
2671 	case V_WRITE:
2672 		if (atomic_add_64_nv((&(vp->v_mmap_write)), 0))
2673 			return (V_TRUE);
2674 		break;
2675 	case V_RDANDWR:
2676 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) &&
2677 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2678 			return (V_TRUE);
2679 		break;
2680 	case V_RDORWR:
2681 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) ||
2682 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2683 			return (V_TRUE);
2684 		break;
2685 	case V_READ:
2686 		if (atomic_add_64_nv((&(vp->v_mmap_read)), 0))
2687 			return (V_TRUE);
2688 		break;
2689 	}
2690 #else
2691 	switch (mode) {
2692 	case V_WRITE:
2693 		if (vp->v_mmap_write)
2694 			return (V_TRUE);
2695 		break;
2696 	case V_RDANDWR:
2697 		if (vp->v_mmap_read && vp->v_mmap_write)
2698 			return (V_TRUE);
2699 		break;
2700 	case V_RDORWR:
2701 		if (vp->v_mmap_read || vp->v_mmap_write)
2702 			return (V_TRUE);
2703 		break;
2704 	case V_READ:
2705 		if (vp->v_mmap_read)
2706 			return (V_TRUE);
2707 		break;
2708 	}
2709 #endif
2710 
2711 	return (V_FALSE);
2712 }
2713 
2714 /*
2715  * Set the operations vector for a vnode.
2716  *
2717  * FEM ensures that the v_femhead pointer is filled in before the
2718  * v_op pointer is changed.  This means that if the v_femhead pointer
2719  * is NULL, and the v_op field hasn't changed since before which checked
2720  * the v_femhead pointer; then our update is ok - we are not racing with
2721  * FEM.
2722  */
2723 void
2724 vn_setops(vnode_t *vp, vnodeops_t *vnodeops)
2725 {
2726 	vnodeops_t	*op;
2727 
2728 	ASSERT(vp != NULL);
2729 	ASSERT(vnodeops != NULL);
2730 
2731 	op = vp->v_op;
2732 	membar_consumer();
2733 	/*
2734 	 * If vp->v_femhead == NULL, then we'll call casptr() to do the
2735 	 * compare-and-swap on vp->v_op.  If either fails, then FEM is
2736 	 * in effect on the vnode and we need to have FEM deal with it.
2737 	 */
2738 	if (vp->v_femhead != NULL || casptr(&vp->v_op, op, vnodeops) != op) {
2739 		fem_setvnops(vp, vnodeops);
2740 	}
2741 }
2742 
2743 /*
2744  * Retrieve the operations vector for a vnode
2745  * As with vn_setops(above); make sure we aren't racing with FEM.
2746  * FEM sets the v_op to a special, internal, vnodeops that wouldn't
2747  * make sense to the callers of this routine.
2748  */
2749 vnodeops_t *
2750 vn_getops(vnode_t *vp)
2751 {
2752 	vnodeops_t	*op;
2753 
2754 	ASSERT(vp != NULL);
2755 
2756 	op = vp->v_op;
2757 	membar_consumer();
2758 	if (vp->v_femhead == NULL && op == vp->v_op) {
2759 		return (op);
2760 	} else {
2761 		return (fem_getvnops(vp));
2762 	}
2763 }
2764 
2765 /*
2766  * Returns non-zero (1) if the vnodeops matches that of the vnode.
2767  * Returns zero (0) if not.
2768  */
2769 int
2770 vn_matchops(vnode_t *vp, vnodeops_t *vnodeops)
2771 {
2772 	return (vn_getops(vp) == vnodeops);
2773 }
2774 
2775 /*
2776  * Returns non-zero (1) if the specified operation matches the
2777  * corresponding operation for that the vnode.
2778  * Returns zero (0) if not.
2779  */
2780 
2781 #define	MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0))
2782 
2783 int
2784 vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp)
2785 {
2786 	const fs_operation_trans_def_t *otdp;
2787 	fs_generic_func_p *loc = NULL;
2788 	vnodeops_t	*vop = vn_getops(vp);
2789 
2790 	ASSERT(vopname != NULL);
2791 
2792 	for (otdp = vn_ops_table; otdp->name != NULL; otdp++) {
2793 		if (MATCHNAME(otdp->name, vopname)) {
2794 			loc = (fs_generic_func_p *)
2795 			    ((char *)(vop) + otdp->offset);
2796 			break;
2797 		}
2798 	}
2799 
2800 	return ((loc != NULL) && (*loc == funcp));
2801 }
2802 
2803 /*
2804  * fs_new_caller_id() needs to return a unique ID on a given local system.
2805  * The IDs do not need to survive across reboots.  These are primarily
2806  * used so that (FEM) monitors can detect particular callers (such as
2807  * the NFS server) to a given vnode/vfs operation.
2808  */
2809 u_longlong_t
2810 fs_new_caller_id()
2811 {
2812 	static uint64_t next_caller_id = 0LL; /* First call returns 1 */
2813 
2814 	return ((u_longlong_t)atomic_add_64_nv(&next_caller_id, 1));
2815 }
2816 
2817 /*
2818  * Given a starting vnode and a path, updates the path in the target vnode in
2819  * a safe manner.  If the vnode already has path information embedded, then the
2820  * cached path is left untouched.
2821  */
2822 
2823 size_t max_vnode_path = 4 * MAXPATHLEN;
2824 
2825 void
2826 vn_setpath(vnode_t *rootvp, struct vnode *startvp, struct vnode *vp,
2827     const char *path, size_t plen)
2828 {
2829 	char	*rpath;
2830 	vnode_t	*base;
2831 	size_t	rpathlen, rpathalloc;
2832 	int	doslash = 1;
2833 
2834 	if (*path == '/') {
2835 		base = rootvp;
2836 		path++;
2837 		plen--;
2838 	} else {
2839 		base = startvp;
2840 	}
2841 
2842 	/*
2843 	 * We cannot grab base->v_lock while we hold vp->v_lock because of
2844 	 * the potential for deadlock.
2845 	 */
2846 	mutex_enter(&base->v_lock);
2847 	if (base->v_path == NULL) {
2848 		mutex_exit(&base->v_lock);
2849 		return;
2850 	}
2851 
2852 	rpathlen = strlen(base->v_path);
2853 	rpathalloc = rpathlen + plen + 1;
2854 	/* Avoid adding a slash if there's already one there */
2855 	if (base->v_path[rpathlen-1] == '/')
2856 		doslash = 0;
2857 	else
2858 		rpathalloc++;
2859 
2860 	/*
2861 	 * We don't want to call kmem_alloc(KM_SLEEP) with kernel locks held,
2862 	 * so we must do this dance.  If, by chance, something changes the path,
2863 	 * just give up since there is no real harm.
2864 	 */
2865 	mutex_exit(&base->v_lock);
2866 
2867 	/* Paths should stay within reason */
2868 	if (rpathalloc > max_vnode_path)
2869 		return;
2870 
2871 	rpath = kmem_alloc(rpathalloc, KM_SLEEP);
2872 
2873 	mutex_enter(&base->v_lock);
2874 	if (base->v_path == NULL || strlen(base->v_path) != rpathlen) {
2875 		mutex_exit(&base->v_lock);
2876 		kmem_free(rpath, rpathalloc);
2877 		return;
2878 	}
2879 	bcopy(base->v_path, rpath, rpathlen);
2880 	mutex_exit(&base->v_lock);
2881 
2882 	if (doslash)
2883 		rpath[rpathlen++] = '/';
2884 	bcopy(path, rpath + rpathlen, plen);
2885 	rpath[rpathlen + plen] = '\0';
2886 
2887 	mutex_enter(&vp->v_lock);
2888 	if (vp->v_path != NULL) {
2889 		mutex_exit(&vp->v_lock);
2890 		kmem_free(rpath, rpathalloc);
2891 	} else {
2892 		vp->v_path = rpath;
2893 		mutex_exit(&vp->v_lock);
2894 	}
2895 }
2896 
2897 /*
2898  * Sets the path to the vnode to be the given string, regardless of current
2899  * context.  The string must be a complete path from rootdir.  This is only used
2900  * by fsop_root() for setting the path based on the mountpoint.
2901  */
2902 void
2903 vn_setpath_str(struct vnode *vp, const char *str, size_t len)
2904 {
2905 	char *buf = kmem_alloc(len + 1, KM_SLEEP);
2906 
2907 	mutex_enter(&vp->v_lock);
2908 	if (vp->v_path != NULL) {
2909 		mutex_exit(&vp->v_lock);
2910 		kmem_free(buf, len + 1);
2911 		return;
2912 	}
2913 
2914 	vp->v_path = buf;
2915 	bcopy(str, vp->v_path, len);
2916 	vp->v_path[len] = '\0';
2917 
2918 	mutex_exit(&vp->v_lock);
2919 }
2920 
2921 /*
2922  * Similar to vn_setpath_str(), this function sets the path of the destination
2923  * vnode to the be the same as the source vnode.
2924  */
2925 void
2926 vn_copypath(struct vnode *src, struct vnode *dst)
2927 {
2928 	char *buf;
2929 	int alloc;
2930 
2931 	mutex_enter(&src->v_lock);
2932 	if (src->v_path == NULL) {
2933 		mutex_exit(&src->v_lock);
2934 		return;
2935 	}
2936 	alloc = strlen(src->v_path) + 1;
2937 
2938 	/* avoid kmem_alloc() with lock held */
2939 	mutex_exit(&src->v_lock);
2940 	buf = kmem_alloc(alloc, KM_SLEEP);
2941 	mutex_enter(&src->v_lock);
2942 	if (src->v_path == NULL || strlen(src->v_path) + 1 != alloc) {
2943 		mutex_exit(&src->v_lock);
2944 		kmem_free(buf, alloc);
2945 		return;
2946 	}
2947 	bcopy(src->v_path, buf, alloc);
2948 	mutex_exit(&src->v_lock);
2949 
2950 	mutex_enter(&dst->v_lock);
2951 	if (dst->v_path != NULL) {
2952 		mutex_exit(&dst->v_lock);
2953 		kmem_free(buf, alloc);
2954 		return;
2955 	}
2956 	dst->v_path = buf;
2957 	mutex_exit(&dst->v_lock);
2958 }
2959 
2960 /*
2961  * XXX Private interface for segvn routines that handle vnode
2962  * large page segments.
2963  *
2964  * return 1 if vp's file system VOP_PAGEIO() implementation
2965  * can be safely used instead of VOP_GETPAGE() for handling
2966  * pagefaults against regular non swap files. VOP_PAGEIO()
2967  * interface is considered safe here if its implementation
2968  * is very close to VOP_GETPAGE() implementation.
2969  * e.g. It zero's out the part of the page beyond EOF. Doesn't
2970  * panic if there're file holes but instead returns an error.
2971  * Doesn't assume file won't be changed by user writes, etc.
2972  *
2973  * return 0 otherwise.
2974  *
2975  * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs.
2976  */
2977 int
2978 vn_vmpss_usepageio(vnode_t *vp)
2979 {
2980 	vfs_t   *vfsp = vp->v_vfsp;
2981 	char *fsname = vfssw[vfsp->vfs_fstype].vsw_name;
2982 	char *pageio_ok_fss[] = {"ufs", "nfs", NULL};
2983 	char **fsok = pageio_ok_fss;
2984 
2985 	if (fsname == NULL) {
2986 		return (0);
2987 	}
2988 
2989 	for (; *fsok; fsok++) {
2990 		if (strcmp(*fsok, fsname) == 0) {
2991 			return (1);
2992 		}
2993 	}
2994 	return (0);
2995 }
2996 
2997 /* VOP_XXX() macros call the corresponding fop_xxx() function */
2998 
2999 int
3000 fop_open(
3001 	vnode_t **vpp,
3002 	int mode,
3003 	cred_t *cr,
3004 	caller_context_t *ct)
3005 {
3006 	int ret;
3007 	vnode_t *vp = *vpp;
3008 
3009 	VN_HOLD(vp);
3010 	/*
3011 	 * Adding to the vnode counts before calling open
3012 	 * avoids the need for a mutex. It circumvents a race
3013 	 * condition where a query made on the vnode counts results in a
3014 	 * false negative. The inquirer goes away believing the file is
3015 	 * not open when there is an open on the file already under way.
3016 	 *
3017 	 * The counts are meant to prevent NFS from granting a delegation
3018 	 * when it would be dangerous to do so.
3019 	 *
3020 	 * The vnode counts are only kept on regular files
3021 	 */
3022 	if ((*vpp)->v_type == VREG) {
3023 		if (mode & FREAD)
3024 			atomic_add_32(&((*vpp)->v_rdcnt), 1);
3025 		if (mode & FWRITE)
3026 			atomic_add_32(&((*vpp)->v_wrcnt), 1);
3027 	}
3028 
3029 	VOPXID_MAP_CR(vp, cr);
3030 
3031 	ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr, ct);
3032 
3033 	if (ret) {
3034 		/*
3035 		 * Use the saved vp just in case the vnode ptr got trashed
3036 		 * by the error.
3037 		 */
3038 		VOPSTATS_UPDATE(vp, open);
3039 		if ((vp->v_type == VREG) && (mode & FREAD))
3040 			atomic_add_32(&(vp->v_rdcnt), -1);
3041 		if ((vp->v_type == VREG) && (mode & FWRITE))
3042 			atomic_add_32(&(vp->v_wrcnt), -1);
3043 	} else {
3044 		/*
3045 		 * Some filesystems will return a different vnode,
3046 		 * but the same path was still used to open it.
3047 		 * So if we do change the vnode and need to
3048 		 * copy over the path, do so here, rather than special
3049 		 * casing each filesystem. Adjust the vnode counts to
3050 		 * reflect the vnode switch.
3051 		 */
3052 		VOPSTATS_UPDATE(*vpp, open);
3053 		if (*vpp != vp && *vpp != NULL) {
3054 			vn_copypath(vp, *vpp);
3055 			if (((*vpp)->v_type == VREG) && (mode & FREAD))
3056 				atomic_add_32(&((*vpp)->v_rdcnt), 1);
3057 			if ((vp->v_type == VREG) && (mode & FREAD))
3058 				atomic_add_32(&(vp->v_rdcnt), -1);
3059 			if (((*vpp)->v_type == VREG) && (mode & FWRITE))
3060 				atomic_add_32(&((*vpp)->v_wrcnt), 1);
3061 			if ((vp->v_type == VREG) && (mode & FWRITE))
3062 				atomic_add_32(&(vp->v_wrcnt), -1);
3063 		}
3064 	}
3065 	VN_RELE(vp);
3066 	return (ret);
3067 }
3068 
3069 int
3070 fop_close(
3071 	vnode_t *vp,
3072 	int flag,
3073 	int count,
3074 	offset_t offset,
3075 	cred_t *cr,
3076 	caller_context_t *ct)
3077 {
3078 	int err;
3079 
3080 	VOPXID_MAP_CR(vp, cr);
3081 
3082 	err = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr, ct);
3083 	VOPSTATS_UPDATE(vp, close);
3084 	/*
3085 	 * Check passed in count to handle possible dups. Vnode counts are only
3086 	 * kept on regular files
3087 	 */
3088 	if ((vp->v_type == VREG) && (count == 1))  {
3089 		if (flag & FREAD) {
3090 			ASSERT(vp->v_rdcnt > 0);
3091 			atomic_add_32(&(vp->v_rdcnt), -1);
3092 		}
3093 		if (flag & FWRITE) {
3094 			ASSERT(vp->v_wrcnt > 0);
3095 			atomic_add_32(&(vp->v_wrcnt), -1);
3096 		}
3097 	}
3098 	return (err);
3099 }
3100 
3101 int
3102 fop_read(
3103 	vnode_t *vp,
3104 	uio_t *uiop,
3105 	int ioflag,
3106 	cred_t *cr,
3107 	caller_context_t *ct)
3108 {
3109 	int	err;
3110 	ssize_t	resid_start = uiop->uio_resid;
3111 
3112 	VOPXID_MAP_CR(vp, cr);
3113 
3114 	err = (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct);
3115 	VOPSTATS_UPDATE_IO(vp, read,
3116 	    read_bytes, (resid_start - uiop->uio_resid));
3117 	return (err);
3118 }
3119 
3120 int
3121 fop_write(
3122 	vnode_t *vp,
3123 	uio_t *uiop,
3124 	int ioflag,
3125 	cred_t *cr,
3126 	caller_context_t *ct)
3127 {
3128 	int	err;
3129 	ssize_t	resid_start = uiop->uio_resid;
3130 
3131 	VOPXID_MAP_CR(vp, cr);
3132 
3133 	err = (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct);
3134 	VOPSTATS_UPDATE_IO(vp, write,
3135 	    write_bytes, (resid_start - uiop->uio_resid));
3136 	return (err);
3137 }
3138 
3139 int
3140 fop_ioctl(
3141 	vnode_t *vp,
3142 	int cmd,
3143 	intptr_t arg,
3144 	int flag,
3145 	cred_t *cr,
3146 	int *rvalp,
3147 	caller_context_t *ct)
3148 {
3149 	int	err;
3150 
3151 	VOPXID_MAP_CR(vp, cr);
3152 
3153 	err = (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp, ct);
3154 	VOPSTATS_UPDATE(vp, ioctl);
3155 	return (err);
3156 }
3157 
3158 int
3159 fop_setfl(
3160 	vnode_t *vp,
3161 	int oflags,
3162 	int nflags,
3163 	cred_t *cr,
3164 	caller_context_t *ct)
3165 {
3166 	int	err;
3167 
3168 	VOPXID_MAP_CR(vp, cr);
3169 
3170 	err = (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr, ct);
3171 	VOPSTATS_UPDATE(vp, setfl);
3172 	return (err);
3173 }
3174 
3175 int
3176 fop_getattr(
3177 	vnode_t *vp,
3178 	vattr_t *vap,
3179 	int flags,
3180 	cred_t *cr,
3181 	caller_context_t *ct)
3182 {
3183 	int	err;
3184 
3185 	VOPXID_MAP_CR(vp, cr);
3186 
3187 	/*
3188 	 * If this file system doesn't understand the xvattr extensions
3189 	 * then turn off the xvattr bit.
3190 	 */
3191 	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3192 		vap->va_mask &= ~AT_XVATTR;
3193 	}
3194 
3195 	/*
3196 	 * We're only allowed to skip the ACL check iff we used a 32 bit
3197 	 * ACE mask with VOP_ACCESS() to determine permissions.
3198 	 */
3199 	if ((flags & ATTR_NOACLCHECK) &&
3200 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3201 		return (EINVAL);
3202 	}
3203 	err = (*(vp)->v_op->vop_getattr)(vp, vap, flags, cr, ct);
3204 	VOPSTATS_UPDATE(vp, getattr);
3205 	return (err);
3206 }
3207 
3208 int
3209 fop_setattr(
3210 	vnode_t *vp,
3211 	vattr_t *vap,
3212 	int flags,
3213 	cred_t *cr,
3214 	caller_context_t *ct)
3215 {
3216 	int	err;
3217 
3218 	VOPXID_MAP_CR(vp, cr);
3219 
3220 	/*
3221 	 * If this file system doesn't understand the xvattr extensions
3222 	 * then turn off the xvattr bit.
3223 	 */
3224 	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3225 		vap->va_mask &= ~AT_XVATTR;
3226 	}
3227 
3228 	/*
3229 	 * We're only allowed to skip the ACL check iff we used a 32 bit
3230 	 * ACE mask with VOP_ACCESS() to determine permissions.
3231 	 */
3232 	if ((flags & ATTR_NOACLCHECK) &&
3233 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3234 		return (EINVAL);
3235 	}
3236 	err = (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct);
3237 	VOPSTATS_UPDATE(vp, setattr);
3238 	return (err);
3239 }
3240 
3241 int
3242 fop_access(
3243 	vnode_t *vp,
3244 	int mode,
3245 	int flags,
3246 	cred_t *cr,
3247 	caller_context_t *ct)
3248 {
3249 	int	err;
3250 
3251 	if ((flags & V_ACE_MASK) &&
3252 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3253 		return (EINVAL);
3254 	}
3255 
3256 	VOPXID_MAP_CR(vp, cr);
3257 
3258 	err = (*(vp)->v_op->vop_access)(vp, mode, flags, cr, ct);
3259 	VOPSTATS_UPDATE(vp, access);
3260 	return (err);
3261 }
3262 
3263 int
3264 fop_lookup(
3265 	vnode_t *dvp,
3266 	char *nm,
3267 	vnode_t **vpp,
3268 	pathname_t *pnp,
3269 	int flags,
3270 	vnode_t *rdir,
3271 	cred_t *cr,
3272 	caller_context_t *ct,
3273 	int *deflags,		/* Returned per-dirent flags */
3274 	pathname_t *ppnp)	/* Returned case-preserved name in directory */
3275 {
3276 	int ret;
3277 
3278 	/*
3279 	 * If this file system doesn't support case-insensitive access
3280 	 * and said access is requested, fail quickly.  It is required
3281 	 * that if the vfs supports case-insensitive lookup, it also
3282 	 * supports extended dirent flags.
3283 	 */
3284 	if (flags & FIGNORECASE &&
3285 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3286 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3287 		return (EINVAL);
3288 
3289 	VOPXID_MAP_CR(dvp, cr);
3290 
3291 	if ((flags & LOOKUP_XATTR) && (flags & LOOKUP_HAVE_SYSATTR_DIR) == 0) {
3292 		ret = xattr_dir_lookup(dvp, vpp, flags, cr);
3293 	} else {
3294 		ret = (*(dvp)->v_op->vop_lookup)
3295 		    (dvp, nm, vpp, pnp, flags, rdir, cr, ct, deflags, ppnp);
3296 	}
3297 	if (ret == 0 && *vpp) {
3298 		VOPSTATS_UPDATE(*vpp, lookup);
3299 		if ((*vpp)->v_path == NULL) {
3300 			vn_setpath(rootdir, dvp, *vpp, nm, strlen(nm));
3301 		}
3302 	}
3303 
3304 	return (ret);
3305 }
3306 
3307 int
3308 fop_create(
3309 	vnode_t *dvp,
3310 	char *name,
3311 	vattr_t *vap,
3312 	vcexcl_t excl,
3313 	int mode,
3314 	vnode_t **vpp,
3315 	cred_t *cr,
3316 	int flags,
3317 	caller_context_t *ct,
3318 	vsecattr_t *vsecp)	/* ACL to set during create */
3319 {
3320 	int ret;
3321 
3322 	if (vsecp != NULL &&
3323 	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3324 		return (EINVAL);
3325 	}
3326 	/*
3327 	 * If this file system doesn't support case-insensitive access
3328 	 * and said access is requested, fail quickly.
3329 	 */
3330 	if (flags & FIGNORECASE &&
3331 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3332 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3333 		return (EINVAL);
3334 
3335 	VOPXID_MAP_CR(dvp, cr);
3336 
3337 	ret = (*(dvp)->v_op->vop_create)
3338 	    (dvp, name, vap, excl, mode, vpp, cr, flags, ct, vsecp);
3339 	if (ret == 0 && *vpp) {
3340 		VOPSTATS_UPDATE(*vpp, create);
3341 		if ((*vpp)->v_path == NULL) {
3342 			vn_setpath(rootdir, dvp, *vpp, name, strlen(name));
3343 		}
3344 	}
3345 
3346 	return (ret);
3347 }
3348 
3349 int
3350 fop_remove(
3351 	vnode_t *dvp,
3352 	char *nm,
3353 	cred_t *cr,
3354 	caller_context_t *ct,
3355 	int flags)
3356 {
3357 	int	err;
3358 
3359 	/*
3360 	 * If this file system doesn't support case-insensitive access
3361 	 * and said access is requested, fail quickly.
3362 	 */
3363 	if (flags & FIGNORECASE &&
3364 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3365 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3366 		return (EINVAL);
3367 
3368 	VOPXID_MAP_CR(dvp, cr);
3369 
3370 	err = (*(dvp)->v_op->vop_remove)(dvp, nm, cr, ct, flags);
3371 	VOPSTATS_UPDATE(dvp, remove);
3372 	return (err);
3373 }
3374 
3375 int
3376 fop_link(
3377 	vnode_t *tdvp,
3378 	vnode_t *svp,
3379 	char *tnm,
3380 	cred_t *cr,
3381 	caller_context_t *ct,
3382 	int flags)
3383 {
3384 	int	err;
3385 
3386 	/*
3387 	 * If the target file system doesn't support case-insensitive access
3388 	 * and said access is requested, fail quickly.
3389 	 */
3390 	if (flags & FIGNORECASE &&
3391 	    (vfs_has_feature(tdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3392 	    vfs_has_feature(tdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3393 		return (EINVAL);
3394 
3395 	VOPXID_MAP_CR(tdvp, cr);
3396 
3397 	err = (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr, ct, flags);
3398 	VOPSTATS_UPDATE(tdvp, link);
3399 	return (err);
3400 }
3401 
3402 int
3403 fop_rename(
3404 	vnode_t *sdvp,
3405 	char *snm,
3406 	vnode_t *tdvp,
3407 	char *tnm,
3408 	cred_t *cr,
3409 	caller_context_t *ct,
3410 	int flags)
3411 {
3412 	int	err;
3413 
3414 	/*
3415 	 * If the file system involved does not support
3416 	 * case-insensitive access and said access is requested, fail
3417 	 * quickly.
3418 	 */
3419 	if (flags & FIGNORECASE &&
3420 	    ((vfs_has_feature(sdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3421 	    vfs_has_feature(sdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0)))
3422 		return (EINVAL);
3423 
3424 	VOPXID_MAP_CR(tdvp, cr);
3425 
3426 	err = (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr, ct, flags);
3427 	VOPSTATS_UPDATE(sdvp, rename);
3428 	return (err);
3429 }
3430 
3431 int
3432 fop_mkdir(
3433 	vnode_t *dvp,
3434 	char *dirname,
3435 	vattr_t *vap,
3436 	vnode_t **vpp,
3437 	cred_t *cr,
3438 	caller_context_t *ct,
3439 	int flags,
3440 	vsecattr_t *vsecp)	/* ACL to set during create */
3441 {
3442 	int ret;
3443 
3444 	if (vsecp != NULL &&
3445 	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3446 		return (EINVAL);
3447 	}
3448 	/*
3449 	 * If this file system doesn't support case-insensitive access
3450 	 * and said access is requested, fail quickly.
3451 	 */
3452 	if (flags & FIGNORECASE &&
3453 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3454 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3455 		return (EINVAL);
3456 
3457 	VOPXID_MAP_CR(dvp, cr);
3458 
3459 	ret = (*(dvp)->v_op->vop_mkdir)
3460 	    (dvp, dirname, vap, vpp, cr, ct, flags, vsecp);
3461 	if (ret == 0 && *vpp) {
3462 		VOPSTATS_UPDATE(*vpp, mkdir);
3463 		if ((*vpp)->v_path == NULL) {
3464 			vn_setpath(rootdir, dvp, *vpp, dirname,
3465 			    strlen(dirname));
3466 		}
3467 	}
3468 
3469 	return (ret);
3470 }
3471 
3472 int
3473 fop_rmdir(
3474 	vnode_t *dvp,
3475 	char *nm,
3476 	vnode_t *cdir,
3477 	cred_t *cr,
3478 	caller_context_t *ct,
3479 	int flags)
3480 {
3481 	int	err;
3482 
3483 	/*
3484 	 * If this file system doesn't support case-insensitive access
3485 	 * and said access is requested, fail quickly.
3486 	 */
3487 	if (flags & FIGNORECASE &&
3488 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3489 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3490 		return (EINVAL);
3491 
3492 	VOPXID_MAP_CR(dvp, cr);
3493 
3494 	err = (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr, ct, flags);
3495 	VOPSTATS_UPDATE(dvp, rmdir);
3496 	return (err);
3497 }
3498 
3499 int
3500 fop_readdir(
3501 	vnode_t *vp,
3502 	uio_t *uiop,
3503 	cred_t *cr,
3504 	int *eofp,
3505 	caller_context_t *ct,
3506 	int flags)
3507 {
3508 	int	err;
3509 	ssize_t	resid_start = uiop->uio_resid;
3510 
3511 	/*
3512 	 * If this file system doesn't support retrieving directory
3513 	 * entry flags and said access is requested, fail quickly.
3514 	 */
3515 	if (flags & V_RDDIR_ENTFLAGS &&
3516 	    vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS) == 0)
3517 		return (EINVAL);
3518 
3519 	VOPXID_MAP_CR(vp, cr);
3520 
3521 	err = (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp, ct, flags);
3522 	VOPSTATS_UPDATE_IO(vp, readdir,
3523 	    readdir_bytes, (resid_start - uiop->uio_resid));
3524 	return (err);
3525 }
3526 
3527 int
3528 fop_symlink(
3529 	vnode_t *dvp,
3530 	char *linkname,
3531 	vattr_t *vap,
3532 	char *target,
3533 	cred_t *cr,
3534 	caller_context_t *ct,
3535 	int flags)
3536 {
3537 	int	err;
3538 
3539 	/*
3540 	 * If this file system doesn't support case-insensitive access
3541 	 * and said access is requested, fail quickly.
3542 	 */
3543 	if (flags & FIGNORECASE &&
3544 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3545 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3546 		return (EINVAL);
3547 
3548 	VOPXID_MAP_CR(dvp, cr);
3549 
3550 	err = (*(dvp)->v_op->vop_symlink)
3551 	    (dvp, linkname, vap, target, cr, ct, flags);
3552 	VOPSTATS_UPDATE(dvp, symlink);
3553 	return (err);
3554 }
3555 
3556 int
3557 fop_readlink(
3558 	vnode_t *vp,
3559 	uio_t *uiop,
3560 	cred_t *cr,
3561 	caller_context_t *ct)
3562 {
3563 	int	err;
3564 
3565 	VOPXID_MAP_CR(vp, cr);
3566 
3567 	err = (*(vp)->v_op->vop_readlink)(vp, uiop, cr, ct);
3568 	VOPSTATS_UPDATE(vp, readlink);
3569 	return (err);
3570 }
3571 
3572 int
3573 fop_fsync(
3574 	vnode_t *vp,
3575 	int syncflag,
3576 	cred_t *cr,
3577 	caller_context_t *ct)
3578 {
3579 	int	err;
3580 
3581 	VOPXID_MAP_CR(vp, cr);
3582 
3583 	err = (*(vp)->v_op->vop_fsync)(vp, syncflag, cr, ct);
3584 	VOPSTATS_UPDATE(vp, fsync);
3585 	return (err);
3586 }
3587 
3588 void
3589 fop_inactive(
3590 	vnode_t *vp,
3591 	cred_t *cr,
3592 	caller_context_t *ct)
3593 {
3594 	/* Need to update stats before vop call since we may lose the vnode */
3595 	VOPSTATS_UPDATE(vp, inactive);
3596 
3597 	VOPXID_MAP_CR(vp, cr);
3598 
3599 	(*(vp)->v_op->vop_inactive)(vp, cr, ct);
3600 }
3601 
3602 int
3603 fop_fid(
3604 	vnode_t *vp,
3605 	fid_t *fidp,
3606 	caller_context_t *ct)
3607 {
3608 	int	err;
3609 
3610 	err = (*(vp)->v_op->vop_fid)(vp, fidp, ct);
3611 	VOPSTATS_UPDATE(vp, fid);
3612 	return (err);
3613 }
3614 
3615 int
3616 fop_rwlock(
3617 	vnode_t *vp,
3618 	int write_lock,
3619 	caller_context_t *ct)
3620 {
3621 	int	ret;
3622 
3623 	ret = ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct));
3624 	VOPSTATS_UPDATE(vp, rwlock);
3625 	return (ret);
3626 }
3627 
3628 void
3629 fop_rwunlock(
3630 	vnode_t *vp,
3631 	int write_lock,
3632 	caller_context_t *ct)
3633 {
3634 	(*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct);
3635 	VOPSTATS_UPDATE(vp, rwunlock);
3636 }
3637 
3638 int
3639 fop_seek(
3640 	vnode_t *vp,
3641 	offset_t ooff,
3642 	offset_t *noffp,
3643 	caller_context_t *ct)
3644 {
3645 	int	err;
3646 
3647 	err = (*(vp)->v_op->vop_seek)(vp, ooff, noffp, ct);
3648 	VOPSTATS_UPDATE(vp, seek);
3649 	return (err);
3650 }
3651 
3652 int
3653 fop_cmp(
3654 	vnode_t *vp1,
3655 	vnode_t *vp2,
3656 	caller_context_t *ct)
3657 {
3658 	int	err;
3659 
3660 	err = (*(vp1)->v_op->vop_cmp)(vp1, vp2, ct);
3661 	VOPSTATS_UPDATE(vp1, cmp);
3662 	return (err);
3663 }
3664 
3665 int
3666 fop_frlock(
3667 	vnode_t *vp,
3668 	int cmd,
3669 	flock64_t *bfp,
3670 	int flag,
3671 	offset_t offset,
3672 	struct flk_callback *flk_cbp,
3673 	cred_t *cr,
3674 	caller_context_t *ct)
3675 {
3676 	int	err;
3677 
3678 	VOPXID_MAP_CR(vp, cr);
3679 
3680 	err = (*(vp)->v_op->vop_frlock)
3681 	    (vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
3682 	VOPSTATS_UPDATE(vp, frlock);
3683 	return (err);
3684 }
3685 
3686 int
3687 fop_space(
3688 	vnode_t *vp,
3689 	int cmd,
3690 	flock64_t *bfp,
3691 	int flag,
3692 	offset_t offset,
3693 	cred_t *cr,
3694 	caller_context_t *ct)
3695 {
3696 	int	err;
3697 
3698 	VOPXID_MAP_CR(vp, cr);
3699 
3700 	err = (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct);
3701 	VOPSTATS_UPDATE(vp, space);
3702 	return (err);
3703 }
3704 
3705 int
3706 fop_realvp(
3707 	vnode_t *vp,
3708 	vnode_t **vpp,
3709 	caller_context_t *ct)
3710 {
3711 	int	err;
3712 
3713 	err = (*(vp)->v_op->vop_realvp)(vp, vpp, ct);
3714 	VOPSTATS_UPDATE(vp, realvp);
3715 	return (err);
3716 }
3717 
3718 int
3719 fop_getpage(
3720 	vnode_t *vp,
3721 	offset_t off,
3722 	size_t len,
3723 	uint_t *protp,
3724 	page_t **plarr,
3725 	size_t plsz,
3726 	struct seg *seg,
3727 	caddr_t addr,
3728 	enum seg_rw rw,
3729 	cred_t *cr,
3730 	caller_context_t *ct)
3731 {
3732 	int	err;
3733 
3734 	VOPXID_MAP_CR(vp, cr);
3735 
3736 	err = (*(vp)->v_op->vop_getpage)
3737 	    (vp, off, len, protp, plarr, plsz, seg, addr, rw, cr, ct);
3738 	VOPSTATS_UPDATE(vp, getpage);
3739 	return (err);
3740 }
3741 
3742 int
3743 fop_putpage(
3744 	vnode_t *vp,
3745 	offset_t off,
3746 	size_t len,
3747 	int flags,
3748 	cred_t *cr,
3749 	caller_context_t *ct)
3750 {
3751 	int	err;
3752 
3753 	VOPXID_MAP_CR(vp, cr);
3754 
3755 	err = (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr, ct);
3756 	VOPSTATS_UPDATE(vp, putpage);
3757 	return (err);
3758 }
3759 
3760 int
3761 fop_map(
3762 	vnode_t *vp,
3763 	offset_t off,
3764 	struct as *as,
3765 	caddr_t *addrp,
3766 	size_t len,
3767 	uchar_t prot,
3768 	uchar_t maxprot,
3769 	uint_t flags,
3770 	cred_t *cr,
3771 	caller_context_t *ct)
3772 {
3773 	int	err;
3774 
3775 	VOPXID_MAP_CR(vp, cr);
3776 
3777 	err = (*(vp)->v_op->vop_map)
3778 	    (vp, off, as, addrp, len, prot, maxprot, flags, cr, ct);
3779 	VOPSTATS_UPDATE(vp, map);
3780 	return (err);
3781 }
3782 
3783 int
3784 fop_addmap(
3785 	vnode_t *vp,
3786 	offset_t off,
3787 	struct as *as,
3788 	caddr_t addr,
3789 	size_t len,
3790 	uchar_t prot,
3791 	uchar_t maxprot,
3792 	uint_t flags,
3793 	cred_t *cr,
3794 	caller_context_t *ct)
3795 {
3796 	int error;
3797 	u_longlong_t delta;
3798 
3799 	VOPXID_MAP_CR(vp, cr);
3800 
3801 	error = (*(vp)->v_op->vop_addmap)
3802 	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
3803 
3804 	if ((!error) && (vp->v_type == VREG)) {
3805 		delta = (u_longlong_t)btopr(len);
3806 		/*
3807 		 * If file is declared MAP_PRIVATE, it can't be written back
3808 		 * even if open for write. Handle as read.
3809 		 */
3810 		if (flags & MAP_PRIVATE) {
3811 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3812 			    (int64_t)delta);
3813 		} else {
3814 			/*
3815 			 * atomic_add_64 forces the fetch of a 64 bit value to
3816 			 * be atomic on 32 bit machines
3817 			 */
3818 			if (maxprot & PROT_WRITE)
3819 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
3820 				    (int64_t)delta);
3821 			if (maxprot & PROT_READ)
3822 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3823 				    (int64_t)delta);
3824 			if (maxprot & PROT_EXEC)
3825 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3826 				    (int64_t)delta);
3827 		}
3828 	}
3829 	VOPSTATS_UPDATE(vp, addmap);
3830 	return (error);
3831 }
3832 
3833 int
3834 fop_delmap(
3835 	vnode_t *vp,
3836 	offset_t off,
3837 	struct as *as,
3838 	caddr_t addr,
3839 	size_t len,
3840 	uint_t prot,
3841 	uint_t maxprot,
3842 	uint_t flags,
3843 	cred_t *cr,
3844 	caller_context_t *ct)
3845 {
3846 	int error;
3847 	u_longlong_t delta;
3848 
3849 	VOPXID_MAP_CR(vp, cr);
3850 
3851 	error = (*(vp)->v_op->vop_delmap)
3852 	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
3853 
3854 	/*
3855 	 * NFS calls into delmap twice, the first time
3856 	 * it simply establishes a callback mechanism and returns EAGAIN
3857 	 * while the real work is being done upon the second invocation.
3858 	 * We have to detect this here and only decrement the counts upon
3859 	 * the second delmap request.
3860 	 */
3861 	if ((error != EAGAIN) && (vp->v_type == VREG)) {
3862 
3863 		delta = (u_longlong_t)btopr(len);
3864 
3865 		if (flags & MAP_PRIVATE) {
3866 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3867 			    (int64_t)(-delta));
3868 		} else {
3869 			/*
3870 			 * atomic_add_64 forces the fetch of a 64 bit value
3871 			 * to be atomic on 32 bit machines
3872 			 */
3873 			if (maxprot & PROT_WRITE)
3874 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
3875 				    (int64_t)(-delta));
3876 			if (maxprot & PROT_READ)
3877 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3878 				    (int64_t)(-delta));
3879 			if (maxprot & PROT_EXEC)
3880 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3881 				    (int64_t)(-delta));
3882 		}
3883 	}
3884 	VOPSTATS_UPDATE(vp, delmap);
3885 	return (error);
3886 }
3887 
3888 
3889 int
3890 fop_poll(
3891 	vnode_t *vp,
3892 	short events,
3893 	int anyyet,
3894 	short *reventsp,
3895 	struct pollhead **phpp,
3896 	caller_context_t *ct)
3897 {
3898 	int	err;
3899 
3900 	err = (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp, ct);
3901 	VOPSTATS_UPDATE(vp, poll);
3902 	return (err);
3903 }
3904 
3905 int
3906 fop_dump(
3907 	vnode_t *vp,
3908 	caddr_t addr,
3909 	offset_t lbdn,
3910 	offset_t dblks,
3911 	caller_context_t *ct)
3912 {
3913 	int	err;
3914 
3915 	/* ensure lbdn and dblks can be passed safely to bdev_dump */
3916 	if ((lbdn != (daddr_t)lbdn) || (dblks != (int)dblks))
3917 		return (EIO);
3918 
3919 	err = (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks, ct);
3920 	VOPSTATS_UPDATE(vp, dump);
3921 	return (err);
3922 }
3923 
3924 int
3925 fop_pathconf(
3926 	vnode_t *vp,
3927 	int cmd,
3928 	ulong_t *valp,
3929 	cred_t *cr,
3930 	caller_context_t *ct)
3931 {
3932 	int	err;
3933 
3934 	VOPXID_MAP_CR(vp, cr);
3935 
3936 	err = (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr, ct);
3937 	VOPSTATS_UPDATE(vp, pathconf);
3938 	return (err);
3939 }
3940 
3941 int
3942 fop_pageio(
3943 	vnode_t *vp,
3944 	struct page *pp,
3945 	u_offset_t io_off,
3946 	size_t io_len,
3947 	int flags,
3948 	cred_t *cr,
3949 	caller_context_t *ct)
3950 {
3951 	int	err;
3952 
3953 	VOPXID_MAP_CR(vp, cr);
3954 
3955 	err = (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr, ct);
3956 	VOPSTATS_UPDATE(vp, pageio);
3957 	return (err);
3958 }
3959 
3960 int
3961 fop_dumpctl(
3962 	vnode_t *vp,
3963 	int action,
3964 	offset_t *blkp,
3965 	caller_context_t *ct)
3966 {
3967 	int	err;
3968 	err = (*(vp)->v_op->vop_dumpctl)(vp, action, blkp, ct);
3969 	VOPSTATS_UPDATE(vp, dumpctl);
3970 	return (err);
3971 }
3972 
3973 void
3974 fop_dispose(
3975 	vnode_t *vp,
3976 	page_t *pp,
3977 	int flag,
3978 	int dn,
3979 	cred_t *cr,
3980 	caller_context_t *ct)
3981 {
3982 	/* Must do stats first since it's possible to lose the vnode */
3983 	VOPSTATS_UPDATE(vp, dispose);
3984 
3985 	VOPXID_MAP_CR(vp, cr);
3986 
3987 	(*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr, ct);
3988 }
3989 
3990 int
3991 fop_setsecattr(
3992 	vnode_t *vp,
3993 	vsecattr_t *vsap,
3994 	int flag,
3995 	cred_t *cr,
3996 	caller_context_t *ct)
3997 {
3998 	int	err;
3999 
4000 	VOPXID_MAP_CR(vp, cr);
4001 
4002 	/*
4003 	 * We're only allowed to skip the ACL check iff we used a 32 bit
4004 	 * ACE mask with VOP_ACCESS() to determine permissions.
4005 	 */
4006 	if ((flag & ATTR_NOACLCHECK) &&
4007 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4008 		return (EINVAL);
4009 	}
4010 	err = (*(vp)->v_op->vop_setsecattr) (vp, vsap, flag, cr, ct);
4011 	VOPSTATS_UPDATE(vp, setsecattr);
4012 	return (err);
4013 }
4014 
4015 int
4016 fop_getsecattr(
4017 	vnode_t *vp,
4018 	vsecattr_t *vsap,
4019 	int flag,
4020 	cred_t *cr,
4021 	caller_context_t *ct)
4022 {
4023 	int	err;
4024 
4025 	/*
4026 	 * We're only allowed to skip the ACL check iff we used a 32 bit
4027 	 * ACE mask with VOP_ACCESS() to determine permissions.
4028 	 */
4029 	if ((flag & ATTR_NOACLCHECK) &&
4030 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4031 		return (EINVAL);
4032 	}
4033 
4034 	VOPXID_MAP_CR(vp, cr);
4035 
4036 	err = (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr, ct);
4037 	VOPSTATS_UPDATE(vp, getsecattr);
4038 	return (err);
4039 }
4040 
4041 int
4042 fop_shrlock(
4043 	vnode_t *vp,
4044 	int cmd,
4045 	struct shrlock *shr,
4046 	int flag,
4047 	cred_t *cr,
4048 	caller_context_t *ct)
4049 {
4050 	int	err;
4051 
4052 	VOPXID_MAP_CR(vp, cr);
4053 
4054 	err = (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr, ct);
4055 	VOPSTATS_UPDATE(vp, shrlock);
4056 	return (err);
4057 }
4058 
4059 int
4060 fop_vnevent(vnode_t *vp, vnevent_t vnevent, vnode_t *dvp, char *fnm,
4061     caller_context_t *ct)
4062 {
4063 	int	err;
4064 
4065 	err = (*(vp)->v_op->vop_vnevent)(vp, vnevent, dvp, fnm, ct);
4066 	VOPSTATS_UPDATE(vp, vnevent);
4067 	return (err);
4068 }
4069 
4070 /*
4071  * Default destructor
4072  *	Needed because NULL destructor means that the key is unused
4073  */
4074 /* ARGSUSED */
4075 void
4076 vsd_defaultdestructor(void *value)
4077 {}
4078 
4079 /*
4080  * Create a key (index into per vnode array)
4081  *	Locks out vsd_create, vsd_destroy, and vsd_free
4082  *	May allocate memory with lock held
4083  */
4084 void
4085 vsd_create(uint_t *keyp, void (*destructor)(void *))
4086 {
4087 	int	i;
4088 	uint_t	nkeys;
4089 
4090 	/*
4091 	 * if key is allocated, do nothing
4092 	 */
4093 	mutex_enter(&vsd_lock);
4094 	if (*keyp) {
4095 		mutex_exit(&vsd_lock);
4096 		return;
4097 	}
4098 	/*
4099 	 * find an unused key
4100 	 */
4101 	if (destructor == NULL)
4102 		destructor = vsd_defaultdestructor;
4103 
4104 	for (i = 0; i < vsd_nkeys; ++i)
4105 		if (vsd_destructor[i] == NULL)
4106 			break;
4107 
4108 	/*
4109 	 * if no unused keys, increase the size of the destructor array
4110 	 */
4111 	if (i == vsd_nkeys) {
4112 		if ((nkeys = (vsd_nkeys << 1)) == 0)
4113 			nkeys = 1;
4114 		vsd_destructor =
4115 		    (void (**)(void *))vsd_realloc((void *)vsd_destructor,
4116 		    (size_t)(vsd_nkeys * sizeof (void (*)(void *))),
4117 		    (size_t)(nkeys * sizeof (void (*)(void *))));
4118 		vsd_nkeys = nkeys;
4119 	}
4120 
4121 	/*
4122 	 * allocate the next available unused key
4123 	 */
4124 	vsd_destructor[i] = destructor;
4125 	*keyp = i + 1;
4126 
4127 	/* create vsd_list, if it doesn't exist */
4128 	if (vsd_list == NULL) {
4129 		vsd_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
4130 		list_create(vsd_list, sizeof (struct vsd_node),
4131 		    offsetof(struct vsd_node, vs_nodes));
4132 	}
4133 
4134 	mutex_exit(&vsd_lock);
4135 }
4136 
4137 /*
4138  * Destroy a key
4139  *
4140  * Assumes that the caller is preventing vsd_set and vsd_get
4141  * Locks out vsd_create, vsd_destroy, and vsd_free
4142  * May free memory with lock held
4143  */
4144 void
4145 vsd_destroy(uint_t *keyp)
4146 {
4147 	uint_t key;
4148 	struct vsd_node *vsd;
4149 
4150 	/*
4151 	 * protect the key namespace and our destructor lists
4152 	 */
4153 	mutex_enter(&vsd_lock);
4154 	key = *keyp;
4155 	*keyp = 0;
4156 
4157 	ASSERT(key <= vsd_nkeys);
4158 
4159 	/*
4160 	 * if the key is valid
4161 	 */
4162 	if (key != 0) {
4163 		uint_t k = key - 1;
4164 		/*
4165 		 * for every vnode with VSD, call key's destructor
4166 		 */
4167 		for (vsd = list_head(vsd_list); vsd != NULL;
4168 		    vsd = list_next(vsd_list, vsd)) {
4169 			/*
4170 			 * no VSD for key in this vnode
4171 			 */
4172 			if (key > vsd->vs_nkeys)
4173 				continue;
4174 			/*
4175 			 * call destructor for key
4176 			 */
4177 			if (vsd->vs_value[k] && vsd_destructor[k])
4178 				(*vsd_destructor[k])(vsd->vs_value[k]);
4179 			/*
4180 			 * reset value for key
4181 			 */
4182 			vsd->vs_value[k] = NULL;
4183 		}
4184 		/*
4185 		 * actually free the key (NULL destructor == unused)
4186 		 */
4187 		vsd_destructor[k] = NULL;
4188 	}
4189 
4190 	mutex_exit(&vsd_lock);
4191 }
4192 
4193 /*
4194  * Quickly return the per vnode value that was stored with the specified key
4195  * Assumes the caller is protecting key from vsd_create and vsd_destroy
4196  * Assumes the caller is holding v_lock to protect the vsd.
4197  */
4198 void *
4199 vsd_get(vnode_t *vp, uint_t key)
4200 {
4201 	struct vsd_node *vsd;
4202 
4203 	/*
4204 	 * The caller needs to pass a valid vnode.
4205 	 */
4206 	ASSERT(vp != NULL);
4207 	if (vp == NULL)
4208 		return (NULL);
4209 
4210 	vsd = vp->v_vsd;
4211 
4212 	if (key && vsd != NULL && key <= vsd->vs_nkeys)
4213 		return (vsd->vs_value[key - 1]);
4214 	return (NULL);
4215 }
4216 
4217 /*
4218  * Set a per vnode value indexed with the specified key
4219  * Assumes the caller is holding v_lock to protect the vsd.
4220  */
4221 int
4222 vsd_set(vnode_t *vp, uint_t key, void *value)
4223 {
4224 	struct vsd_node *vsd = vp->v_vsd;
4225 
4226 	if (key == 0)
4227 		return (EINVAL);
4228 	if (vsd == NULL)
4229 		vsd = vp->v_vsd = kmem_zalloc(sizeof (*vsd), KM_SLEEP);
4230 
4231 	/*
4232 	 * If the vsd was just allocated, vs_nkeys will be 0, so the following
4233 	 * code won't happen and we will continue down and allocate space for
4234 	 * the vs_value array.
4235 	 * If the caller is replacing one value with another, then it is up
4236 	 * to the caller to free/rele/destroy the previous value (if needed).
4237 	 */
4238 	if (key <= vsd->vs_nkeys) {
4239 		vsd->vs_value[key - 1] = value;
4240 		return (0);
4241 	}
4242 
4243 	ASSERT(key <= vsd_nkeys);
4244 
4245 	if (vsd->vs_nkeys == 0) {
4246 		mutex_enter(&vsd_lock);	/* lock out vsd_destroy() */
4247 		/*
4248 		 * Link onto list of all VSD nodes.
4249 		 */
4250 		list_insert_head(vsd_list, vsd);
4251 		mutex_exit(&vsd_lock);
4252 	}
4253 
4254 	/*
4255 	 * Allocate vnode local storage and set the value for key
4256 	 */
4257 	vsd->vs_value = vsd_realloc(vsd->vs_value,
4258 	    vsd->vs_nkeys * sizeof (void *),
4259 	    key * sizeof (void *));
4260 	vsd->vs_nkeys = key;
4261 	vsd->vs_value[key - 1] = value;
4262 
4263 	return (0);
4264 }
4265 
4266 /*
4267  * Called from vn_free() to run the destructor function for each vsd
4268  *	Locks out vsd_create and vsd_destroy
4269  *	Assumes that the destructor *DOES NOT* use vsd
4270  */
4271 void
4272 vsd_free(vnode_t *vp)
4273 {
4274 	int i;
4275 	struct vsd_node *vsd = vp->v_vsd;
4276 
4277 	if (vsd == NULL)
4278 		return;
4279 
4280 	if (vsd->vs_nkeys == 0) {
4281 		kmem_free(vsd, sizeof (*vsd));
4282 		vp->v_vsd = NULL;
4283 		return;
4284 	}
4285 
4286 	/*
4287 	 * lock out vsd_create and vsd_destroy, call
4288 	 * the destructor, and mark the value as destroyed.
4289 	 */
4290 	mutex_enter(&vsd_lock);
4291 
4292 	for (i = 0; i < vsd->vs_nkeys; i++) {
4293 		if (vsd->vs_value[i] && vsd_destructor[i])
4294 			(*vsd_destructor[i])(vsd->vs_value[i]);
4295 		vsd->vs_value[i] = NULL;
4296 	}
4297 
4298 	/*
4299 	 * remove from linked list of VSD nodes
4300 	 */
4301 	list_remove(vsd_list, vsd);
4302 
4303 	mutex_exit(&vsd_lock);
4304 
4305 	/*
4306 	 * free up the VSD
4307 	 */
4308 	kmem_free(vsd->vs_value, vsd->vs_nkeys * sizeof (void *));
4309 	kmem_free(vsd, sizeof (struct vsd_node));
4310 	vp->v_vsd = NULL;
4311 }
4312 
4313 /*
4314  * realloc
4315  */
4316 static void *
4317 vsd_realloc(void *old, size_t osize, size_t nsize)
4318 {
4319 	void *new;
4320 
4321 	new = kmem_zalloc(nsize, KM_SLEEP);
4322 	if (old) {
4323 		bcopy(old, new, osize);
4324 		kmem_free(old, osize);
4325 	}
4326 	return (new);
4327 }
4328