xref: /titanic_41/usr/src/uts/common/fs/vnode.c (revision 54c529d43d4363891fd7381edde56d2cafbff593)
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 	VERIFY(vp->v_count > 0);
818 	mutex_enter(&vp->v_lock);
819 	if (vp->v_count == 1) {
820 		mutex_exit(&vp->v_lock);
821 		VOP_INACTIVE(vp, CRED(), NULL);
822 		return;
823 	}
824 	vp->v_count--;
825 	mutex_exit(&vp->v_lock);
826 }
827 
828 /*
829  * Release a vnode referenced by the DNLC. Multiple DNLC references are treated
830  * as a single reference, so v_count is not decremented until the last DNLC hold
831  * is released. This makes it possible to distinguish vnodes that are referenced
832  * only by the DNLC.
833  */
834 void
835 vn_rele_dnlc(vnode_t *vp)
836 {
837 	VERIFY((vp->v_count > 0) && (vp->v_count_dnlc > 0));
838 	mutex_enter(&vp->v_lock);
839 	if (--vp->v_count_dnlc == 0) {
840 		if (vp->v_count == 1) {
841 			mutex_exit(&vp->v_lock);
842 			VOP_INACTIVE(vp, CRED(), NULL);
843 			return;
844 		}
845 		vp->v_count--;
846 	}
847 	mutex_exit(&vp->v_lock);
848 }
849 
850 /*
851  * Like vn_rele() except that it clears v_stream under v_lock.
852  * This is used by sockfs when it dismantels the association between
853  * the sockfs node and the vnode in the underlaying file system.
854  * v_lock has to be held to prevent a thread coming through the lookupname
855  * path from accessing a stream head that is going away.
856  */
857 void
858 vn_rele_stream(vnode_t *vp)
859 {
860 	VERIFY(vp->v_count > 0);
861 	mutex_enter(&vp->v_lock);
862 	vp->v_stream = NULL;
863 	if (vp->v_count == 1) {
864 		mutex_exit(&vp->v_lock);
865 		VOP_INACTIVE(vp, CRED(), NULL);
866 		return;
867 	}
868 	vp->v_count--;
869 	mutex_exit(&vp->v_lock);
870 }
871 
872 int
873 vn_open(
874 	char *pnamep,
875 	enum uio_seg seg,
876 	int filemode,
877 	int createmode,
878 	struct vnode **vpp,
879 	enum create crwhy,
880 	mode_t umask)
881 {
882 	return (vn_openat(pnamep, seg, filemode, createmode, vpp, crwhy,
883 	    umask, NULL, -1));
884 }
885 
886 
887 /*
888  * Open/create a vnode.
889  * This may be callable by the kernel, the only known use
890  * of user context being that the current user credentials
891  * are used for permissions.  crwhy is defined iff filemode & FCREAT.
892  */
893 int
894 vn_openat(
895 	char *pnamep,
896 	enum uio_seg seg,
897 	int filemode,
898 	int createmode,
899 	struct vnode **vpp,
900 	enum create crwhy,
901 	mode_t umask,
902 	struct vnode *startvp,
903 	int fd)
904 {
905 	struct vnode *vp;
906 	int mode;
907 	int accessflags;
908 	int error;
909 	int in_crit = 0;
910 	int open_done = 0;
911 	int shrlock_done = 0;
912 	struct vattr vattr;
913 	enum symfollow follow;
914 	int estale_retry = 0;
915 	struct shrlock shr;
916 	struct shr_locowner shr_own;
917 
918 	mode = 0;
919 	accessflags = 0;
920 	if (filemode & FREAD)
921 		mode |= VREAD;
922 	if (filemode & (FWRITE|FTRUNC))
923 		mode |= VWRITE;
924 	if (filemode & FXATTRDIROPEN)
925 		mode |= VEXEC;
926 
927 	/* symlink interpretation */
928 	if (filemode & FNOFOLLOW)
929 		follow = NO_FOLLOW;
930 	else
931 		follow = FOLLOW;
932 
933 	if (filemode & FAPPEND)
934 		accessflags |= V_APPEND;
935 
936 top:
937 	if (filemode & FCREAT) {
938 		enum vcexcl excl;
939 
940 		/*
941 		 * Wish to create a file.
942 		 */
943 		vattr.va_type = VREG;
944 		vattr.va_mode = createmode;
945 		vattr.va_mask = AT_TYPE|AT_MODE;
946 		if (filemode & FTRUNC) {
947 			vattr.va_size = 0;
948 			vattr.va_mask |= AT_SIZE;
949 		}
950 		if (filemode & FEXCL)
951 			excl = EXCL;
952 		else
953 			excl = NONEXCL;
954 
955 		if (error =
956 		    vn_createat(pnamep, seg, &vattr, excl, mode, &vp, crwhy,
957 		    (filemode & ~(FTRUNC|FEXCL)), umask, startvp))
958 			return (error);
959 	} else {
960 		/*
961 		 * Wish to open a file.  Just look it up.
962 		 */
963 		if (error = lookupnameat(pnamep, seg, follow,
964 		    NULLVPP, &vp, startvp)) {
965 			if ((error == ESTALE) &&
966 			    fs_need_estale_retry(estale_retry++))
967 				goto top;
968 			return (error);
969 		}
970 
971 		/*
972 		 * Get the attributes to check whether file is large.
973 		 * We do this only if the FOFFMAX flag is not set and
974 		 * only for regular files.
975 		 */
976 
977 		if (!(filemode & FOFFMAX) && (vp->v_type == VREG)) {
978 			vattr.va_mask = AT_SIZE;
979 			if ((error = VOP_GETATTR(vp, &vattr, 0,
980 			    CRED(), NULL))) {
981 				goto out;
982 			}
983 			if (vattr.va_size > (u_offset_t)MAXOFF32_T) {
984 				/*
985 				 * Large File API - regular open fails
986 				 * if FOFFMAX flag is set in file mode
987 				 */
988 				error = EOVERFLOW;
989 				goto out;
990 			}
991 		}
992 		/*
993 		 * Can't write directories, active texts, or
994 		 * read-only filesystems.  Can't truncate files
995 		 * on which mandatory locking is in effect.
996 		 */
997 		if (filemode & (FWRITE|FTRUNC)) {
998 			/*
999 			 * Allow writable directory if VDIROPEN flag is set.
1000 			 */
1001 			if (vp->v_type == VDIR && !(vp->v_flag & VDIROPEN)) {
1002 				error = EISDIR;
1003 				goto out;
1004 			}
1005 			if (ISROFILE(vp)) {
1006 				error = EROFS;
1007 				goto out;
1008 			}
1009 			/*
1010 			 * Can't truncate files on which
1011 			 * sysv mandatory locking is in effect.
1012 			 */
1013 			if (filemode & FTRUNC) {
1014 				vnode_t *rvp;
1015 
1016 				if (VOP_REALVP(vp, &rvp, NULL) != 0)
1017 					rvp = vp;
1018 				if (rvp->v_filocks != NULL) {
1019 					vattr.va_mask = AT_MODE;
1020 					if ((error = VOP_GETATTR(vp,
1021 					    &vattr, 0, CRED(), NULL)) == 0 &&
1022 					    MANDLOCK(vp, vattr.va_mode))
1023 						error = EAGAIN;
1024 				}
1025 			}
1026 			if (error)
1027 				goto out;
1028 		}
1029 		/*
1030 		 * Check permissions.
1031 		 */
1032 		if (error = VOP_ACCESS(vp, mode, accessflags, CRED(), NULL))
1033 			goto out;
1034 	}
1035 
1036 	/*
1037 	 * Do remaining checks for FNOFOLLOW and FNOLINKS.
1038 	 */
1039 	if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) {
1040 		error = ELOOP;
1041 		goto out;
1042 	}
1043 	if (filemode & FNOLINKS) {
1044 		vattr.va_mask = AT_NLINK;
1045 		if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))) {
1046 			goto out;
1047 		}
1048 		if (vattr.va_nlink != 1) {
1049 			error = EMLINK;
1050 			goto out;
1051 		}
1052 	}
1053 
1054 	/*
1055 	 * Opening a socket corresponding to the AF_UNIX pathname
1056 	 * in the filesystem name space is not supported.
1057 	 * However, VSOCK nodes in namefs are supported in order
1058 	 * to make fattach work for sockets.
1059 	 *
1060 	 * XXX This uses VOP_REALVP to distinguish between
1061 	 * an unopened namefs node (where VOP_REALVP returns a
1062 	 * different VSOCK vnode) and a VSOCK created by vn_create
1063 	 * in some file system (where VOP_REALVP would never return
1064 	 * a different vnode).
1065 	 */
1066 	if (vp->v_type == VSOCK) {
1067 		struct vnode *nvp;
1068 
1069 		error = VOP_REALVP(vp, &nvp, NULL);
1070 		if (error != 0 || nvp == NULL || nvp == vp ||
1071 		    nvp->v_type != VSOCK) {
1072 			error = EOPNOTSUPP;
1073 			goto out;
1074 		}
1075 	}
1076 
1077 	if ((vp->v_type == VREG) && nbl_need_check(vp)) {
1078 		/* get share reservation */
1079 		shr.s_access = 0;
1080 		if (filemode & FWRITE)
1081 			shr.s_access |= F_WRACC;
1082 		if (filemode & FREAD)
1083 			shr.s_access |= F_RDACC;
1084 		shr.s_deny = 0;
1085 		shr.s_sysid = 0;
1086 		shr.s_pid = ttoproc(curthread)->p_pid;
1087 		shr_own.sl_pid = shr.s_pid;
1088 		shr_own.sl_id = fd;
1089 		shr.s_own_len = sizeof (shr_own);
1090 		shr.s_owner = (caddr_t)&shr_own;
1091 		error = VOP_SHRLOCK(vp, F_SHARE_NBMAND, &shr, filemode, CRED(),
1092 		    NULL);
1093 		if (error)
1094 			goto out;
1095 		shrlock_done = 1;
1096 
1097 		/* nbmand conflict check if truncating file */
1098 		if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
1099 			nbl_start_crit(vp, RW_READER);
1100 			in_crit = 1;
1101 
1102 			vattr.va_mask = AT_SIZE;
1103 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
1104 				goto out;
1105 			if (nbl_conflict(vp, NBL_WRITE, 0, vattr.va_size, 0,
1106 			    NULL)) {
1107 				error = EACCES;
1108 				goto out;
1109 			}
1110 		}
1111 	}
1112 
1113 	/*
1114 	 * Do opening protocol.
1115 	 */
1116 	error = VOP_OPEN(&vp, filemode, CRED(), NULL);
1117 	if (error)
1118 		goto out;
1119 	open_done = 1;
1120 
1121 	/*
1122 	 * Truncate if required.
1123 	 */
1124 	if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
1125 		vattr.va_size = 0;
1126 		vattr.va_mask = AT_SIZE;
1127 		if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0)
1128 			goto out;
1129 	}
1130 out:
1131 	ASSERT(vp->v_count > 0);
1132 
1133 	if (in_crit) {
1134 		nbl_end_crit(vp);
1135 		in_crit = 0;
1136 	}
1137 	if (error) {
1138 		if (open_done) {
1139 			(void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED(),
1140 			    NULL);
1141 			open_done = 0;
1142 			shrlock_done = 0;
1143 		}
1144 		if (shrlock_done) {
1145 			(void) VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, CRED(),
1146 			    NULL);
1147 			shrlock_done = 0;
1148 		}
1149 
1150 		/*
1151 		 * The following clause was added to handle a problem
1152 		 * with NFS consistency.  It is possible that a lookup
1153 		 * of the file to be opened succeeded, but the file
1154 		 * itself doesn't actually exist on the server.  This
1155 		 * is chiefly due to the DNLC containing an entry for
1156 		 * the file which has been removed on the server.  In
1157 		 * this case, we just start over.  If there was some
1158 		 * other cause for the ESTALE error, then the lookup
1159 		 * of the file will fail and the error will be returned
1160 		 * above instead of looping around from here.
1161 		 */
1162 		VN_RELE(vp);
1163 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1164 			goto top;
1165 	} else
1166 		*vpp = vp;
1167 	return (error);
1168 }
1169 
1170 /*
1171  * The following two accessor functions are for the NFSv4 server.  Since there
1172  * is no VOP_OPEN_UP/DOWNGRADE we need a way for the NFS server to keep the
1173  * vnode open counts correct when a client "upgrades" an open or does an
1174  * open_downgrade.  In NFS, an upgrade or downgrade can not only change the
1175  * open mode (add or subtract read or write), but also change the share/deny
1176  * modes.  However, share reservations are not integrated with OPEN, yet, so
1177  * we need to handle each separately.  These functions are cleaner than having
1178  * the NFS server manipulate the counts directly, however, nobody else should
1179  * use these functions.
1180  */
1181 void
1182 vn_open_upgrade(
1183 	vnode_t *vp,
1184 	int filemode)
1185 {
1186 	ASSERT(vp->v_type == VREG);
1187 
1188 	if (filemode & FREAD)
1189 		atomic_add_32(&(vp->v_rdcnt), 1);
1190 	if (filemode & FWRITE)
1191 		atomic_add_32(&(vp->v_wrcnt), 1);
1192 
1193 }
1194 
1195 void
1196 vn_open_downgrade(
1197 	vnode_t *vp,
1198 	int filemode)
1199 {
1200 	ASSERT(vp->v_type == VREG);
1201 
1202 	if (filemode & FREAD) {
1203 		ASSERT(vp->v_rdcnt > 0);
1204 		atomic_add_32(&(vp->v_rdcnt), -1);
1205 	}
1206 	if (filemode & FWRITE) {
1207 		ASSERT(vp->v_wrcnt > 0);
1208 		atomic_add_32(&(vp->v_wrcnt), -1);
1209 	}
1210 
1211 }
1212 
1213 int
1214 vn_create(
1215 	char *pnamep,
1216 	enum uio_seg seg,
1217 	struct vattr *vap,
1218 	enum vcexcl excl,
1219 	int mode,
1220 	struct vnode **vpp,
1221 	enum create why,
1222 	int flag,
1223 	mode_t umask)
1224 {
1225 	return (vn_createat(pnamep, seg, vap, excl, mode, vpp, why, flag,
1226 	    umask, NULL));
1227 }
1228 
1229 /*
1230  * Create a vnode (makenode).
1231  */
1232 int
1233 vn_createat(
1234 	char *pnamep,
1235 	enum uio_seg seg,
1236 	struct vattr *vap,
1237 	enum vcexcl excl,
1238 	int mode,
1239 	struct vnode **vpp,
1240 	enum create why,
1241 	int flag,
1242 	mode_t umask,
1243 	struct vnode *startvp)
1244 {
1245 	struct vnode *dvp;	/* ptr to parent dir vnode */
1246 	struct vnode *vp = NULL;
1247 	struct pathname pn;
1248 	int error;
1249 	int in_crit = 0;
1250 	struct vattr vattr;
1251 	enum symfollow follow;
1252 	int estale_retry = 0;
1253 
1254 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
1255 
1256 	/* symlink interpretation */
1257 	if ((flag & FNOFOLLOW) || excl == EXCL)
1258 		follow = NO_FOLLOW;
1259 	else
1260 		follow = FOLLOW;
1261 	flag &= ~(FNOFOLLOW|FNOLINKS);
1262 
1263 top:
1264 	/*
1265 	 * Lookup directory.
1266 	 * If new object is a file, call lower level to create it.
1267 	 * Note that it is up to the lower level to enforce exclusive
1268 	 * creation, if the file is already there.
1269 	 * This allows the lower level to do whatever
1270 	 * locking or protocol that is needed to prevent races.
1271 	 * If the new object is directory call lower level to make
1272 	 * the new directory, with "." and "..".
1273 	 */
1274 	if (error = pn_get(pnamep, seg, &pn))
1275 		return (error);
1276 	if (audit_active)
1277 		audit_vncreate_start();
1278 	dvp = NULL;
1279 	*vpp = NULL;
1280 	/*
1281 	 * lookup will find the parent directory for the vnode.
1282 	 * When it is done the pn holds the name of the entry
1283 	 * in the directory.
1284 	 * If this is a non-exclusive create we also find the node itself.
1285 	 */
1286 	error = lookuppnat(&pn, NULL, follow, &dvp,
1287 	    (excl == EXCL) ? NULLVPP : vpp, startvp);
1288 	if (error) {
1289 		pn_free(&pn);
1290 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1291 			goto top;
1292 		if (why == CRMKDIR && error == EINVAL)
1293 			error = EEXIST;		/* SVID */
1294 		return (error);
1295 	}
1296 
1297 	if (why != CRMKNOD)
1298 		vap->va_mode &= ~VSVTX;
1299 
1300 	/*
1301 	 * If default ACLs are defined for the directory don't apply the
1302 	 * umask if umask is passed.
1303 	 */
1304 
1305 	if (umask) {
1306 
1307 		vsecattr_t vsec;
1308 
1309 		vsec.vsa_aclcnt = 0;
1310 		vsec.vsa_aclentp = NULL;
1311 		vsec.vsa_dfaclcnt = 0;
1312 		vsec.vsa_dfaclentp = NULL;
1313 		vsec.vsa_mask = VSA_DFACLCNT;
1314 		error = VOP_GETSECATTR(dvp, &vsec, 0, CRED(), NULL);
1315 		/*
1316 		 * If error is ENOSYS then treat it as no error
1317 		 * Don't want to force all file systems to support
1318 		 * aclent_t style of ACL's.
1319 		 */
1320 		if (error == ENOSYS)
1321 			error = 0;
1322 		if (error) {
1323 			if (*vpp != NULL)
1324 				VN_RELE(*vpp);
1325 			goto out;
1326 		} else {
1327 			/*
1328 			 * Apply the umask if no default ACLs.
1329 			 */
1330 			if (vsec.vsa_dfaclcnt == 0)
1331 				vap->va_mode &= ~umask;
1332 
1333 			/*
1334 			 * VOP_GETSECATTR() may have allocated memory for
1335 			 * ACLs we didn't request, so double-check and
1336 			 * free it if necessary.
1337 			 */
1338 			if (vsec.vsa_aclcnt && vsec.vsa_aclentp != NULL)
1339 				kmem_free((caddr_t)vsec.vsa_aclentp,
1340 				    vsec.vsa_aclcnt * sizeof (aclent_t));
1341 			if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp != NULL)
1342 				kmem_free((caddr_t)vsec.vsa_dfaclentp,
1343 				    vsec.vsa_dfaclcnt * sizeof (aclent_t));
1344 		}
1345 	}
1346 
1347 	/*
1348 	 * In general we want to generate EROFS if the file system is
1349 	 * readonly.  However, POSIX (IEEE Std. 1003.1) section 5.3.1
1350 	 * documents the open system call, and it says that O_CREAT has no
1351 	 * effect if the file already exists.  Bug 1119649 states
1352 	 * that open(path, O_CREAT, ...) fails when attempting to open an
1353 	 * existing file on a read only file system.  Thus, the first part
1354 	 * of the following if statement has 3 checks:
1355 	 *	if the file exists &&
1356 	 *		it is being open with write access &&
1357 	 *		the file system is read only
1358 	 *	then generate EROFS
1359 	 */
1360 	if ((*vpp != NULL && (mode & VWRITE) && ISROFILE(*vpp)) ||
1361 	    (*vpp == NULL && dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
1362 		if (*vpp)
1363 			VN_RELE(*vpp);
1364 		error = EROFS;
1365 	} else if (excl == NONEXCL && *vpp != NULL) {
1366 		vnode_t *rvp;
1367 
1368 		/*
1369 		 * File already exists.  If a mandatory lock has been
1370 		 * applied, return error.
1371 		 */
1372 		vp = *vpp;
1373 		if (VOP_REALVP(vp, &rvp, NULL) != 0)
1374 			rvp = vp;
1375 		if ((vap->va_mask & AT_SIZE) && nbl_need_check(vp)) {
1376 			nbl_start_crit(vp, RW_READER);
1377 			in_crit = 1;
1378 		}
1379 		if (rvp->v_filocks != NULL || rvp->v_shrlocks != NULL) {
1380 			vattr.va_mask = AT_MODE|AT_SIZE;
1381 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL)) {
1382 				goto out;
1383 			}
1384 			if (MANDLOCK(vp, vattr.va_mode)) {
1385 				error = EAGAIN;
1386 				goto out;
1387 			}
1388 			/*
1389 			 * File cannot be truncated if non-blocking mandatory
1390 			 * locks are currently on the file.
1391 			 */
1392 			if ((vap->va_mask & AT_SIZE) && in_crit) {
1393 				u_offset_t offset;
1394 				ssize_t length;
1395 
1396 				offset = vap->va_size > vattr.va_size ?
1397 				    vattr.va_size : vap->va_size;
1398 				length = vap->va_size > vattr.va_size ?
1399 				    vap->va_size - vattr.va_size :
1400 				    vattr.va_size - vap->va_size;
1401 				if (nbl_conflict(vp, NBL_WRITE, offset,
1402 				    length, 0, NULL)) {
1403 					error = EACCES;
1404 					goto out;
1405 				}
1406 			}
1407 		}
1408 
1409 		/*
1410 		 * If the file is the root of a VFS, we've crossed a
1411 		 * mount point and the "containing" directory that we
1412 		 * acquired above (dvp) is irrelevant because it's in
1413 		 * a different file system.  We apply VOP_CREATE to the
1414 		 * target itself instead of to the containing directory
1415 		 * and supply a null path name to indicate (conventionally)
1416 		 * the node itself as the "component" of interest.
1417 		 *
1418 		 * The intercession of the file system is necessary to
1419 		 * ensure that the appropriate permission checks are
1420 		 * done.
1421 		 */
1422 		if (vp->v_flag & VROOT) {
1423 			ASSERT(why != CRMKDIR);
1424 			error = VOP_CREATE(vp, "", vap, excl, mode, vpp,
1425 			    CRED(), flag, NULL, NULL);
1426 			/*
1427 			 * If the create succeeded, it will have created
1428 			 * a new reference to the vnode.  Give up the
1429 			 * original reference.  The assertion should not
1430 			 * get triggered because NBMAND locks only apply to
1431 			 * VREG files.  And if in_crit is non-zero for some
1432 			 * reason, detect that here, rather than when we
1433 			 * deference a null vp.
1434 			 */
1435 			ASSERT(in_crit == 0);
1436 			VN_RELE(vp);
1437 			vp = NULL;
1438 			goto out;
1439 		}
1440 
1441 		/*
1442 		 * Large File API - non-large open (FOFFMAX flag not set)
1443 		 * of regular file fails if the file size exceeds MAXOFF32_T.
1444 		 */
1445 		if (why != CRMKDIR &&
1446 		    !(flag & FOFFMAX) &&
1447 		    (vp->v_type == VREG)) {
1448 			vattr.va_mask = AT_SIZE;
1449 			if ((error = VOP_GETATTR(vp, &vattr, 0,
1450 			    CRED(), NULL))) {
1451 				goto out;
1452 			}
1453 			if ((vattr.va_size > (u_offset_t)MAXOFF32_T)) {
1454 				error = EOVERFLOW;
1455 				goto out;
1456 			}
1457 		}
1458 	}
1459 
1460 	if (error == 0) {
1461 		/*
1462 		 * Call mkdir() if specified, otherwise create().
1463 		 */
1464 		int must_be_dir = pn_fixslash(&pn);	/* trailing '/'? */
1465 
1466 		if (why == CRMKDIR)
1467 			/*
1468 			 * N.B., if vn_createat() ever requests
1469 			 * case-insensitive behavior then it will need
1470 			 * to be passed to VOP_MKDIR().  VOP_CREATE()
1471 			 * will already get it via "flag"
1472 			 */
1473 			error = VOP_MKDIR(dvp, pn.pn_path, vap, vpp, CRED(),
1474 			    NULL, 0, NULL);
1475 		else if (!must_be_dir)
1476 			error = VOP_CREATE(dvp, pn.pn_path, vap,
1477 			    excl, mode, vpp, CRED(), flag, NULL, NULL);
1478 		else
1479 			error = ENOTDIR;
1480 	}
1481 
1482 out:
1483 
1484 	if (audit_active)
1485 		audit_vncreate_finish(*vpp, error);
1486 	if (in_crit) {
1487 		nbl_end_crit(vp);
1488 		in_crit = 0;
1489 	}
1490 	if (vp != NULL) {
1491 		VN_RELE(vp);
1492 		vp = NULL;
1493 	}
1494 	pn_free(&pn);
1495 	VN_RELE(dvp);
1496 	/*
1497 	 * The following clause was added to handle a problem
1498 	 * with NFS consistency.  It is possible that a lookup
1499 	 * of the file to be created succeeded, but the file
1500 	 * itself doesn't actually exist on the server.  This
1501 	 * is chiefly due to the DNLC containing an entry for
1502 	 * the file which has been removed on the server.  In
1503 	 * this case, we just start over.  If there was some
1504 	 * other cause for the ESTALE error, then the lookup
1505 	 * of the file will fail and the error will be returned
1506 	 * above instead of looping around from here.
1507 	 */
1508 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1509 		goto top;
1510 	return (error);
1511 }
1512 
1513 int
1514 vn_link(char *from, char *to, enum uio_seg seg)
1515 {
1516 	struct vnode *fvp;		/* from vnode ptr */
1517 	struct vnode *tdvp;		/* to directory vnode ptr */
1518 	struct pathname pn;
1519 	int error;
1520 	struct vattr vattr;
1521 	dev_t fsid;
1522 	int estale_retry = 0;
1523 
1524 top:
1525 	fvp = tdvp = NULL;
1526 	if (error = pn_get(to, seg, &pn))
1527 		return (error);
1528 	if (error = lookupname(from, seg, NO_FOLLOW, NULLVPP, &fvp))
1529 		goto out;
1530 	if (error = lookuppn(&pn, NULL, NO_FOLLOW, &tdvp, NULLVPP))
1531 		goto out;
1532 	/*
1533 	 * Make sure both source vnode and target directory vnode are
1534 	 * in the same vfs and that it is writeable.
1535 	 */
1536 	vattr.va_mask = AT_FSID;
1537 	if (error = VOP_GETATTR(fvp, &vattr, 0, CRED(), NULL))
1538 		goto out;
1539 	fsid = vattr.va_fsid;
1540 	vattr.va_mask = AT_FSID;
1541 	if (error = VOP_GETATTR(tdvp, &vattr, 0, CRED(), NULL))
1542 		goto out;
1543 	if (fsid != vattr.va_fsid) {
1544 		error = EXDEV;
1545 		goto out;
1546 	}
1547 	if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) {
1548 		error = EROFS;
1549 		goto out;
1550 	}
1551 	/*
1552 	 * Do the link.
1553 	 */
1554 	(void) pn_fixslash(&pn);
1555 	error = VOP_LINK(tdvp, fvp, pn.pn_path, CRED(), NULL, 0);
1556 out:
1557 	pn_free(&pn);
1558 	if (fvp)
1559 		VN_RELE(fvp);
1560 	if (tdvp)
1561 		VN_RELE(tdvp);
1562 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1563 		goto top;
1564 	return (error);
1565 }
1566 
1567 int
1568 vn_rename(char *from, char *to, enum uio_seg seg)
1569 {
1570 	return (vn_renameat(NULL, from, NULL, to, seg));
1571 }
1572 
1573 int
1574 vn_renameat(vnode_t *fdvp, char *fname, vnode_t *tdvp,
1575 		char *tname, enum uio_seg seg)
1576 {
1577 	int error;
1578 	struct vattr vattr;
1579 	struct pathname fpn;		/* from pathname */
1580 	struct pathname tpn;		/* to pathname */
1581 	dev_t fsid;
1582 	int in_crit_src, in_crit_targ;
1583 	vnode_t *fromvp, *fvp;
1584 	vnode_t *tovp, *targvp;
1585 	int estale_retry = 0;
1586 
1587 top:
1588 	fvp = fromvp = tovp = targvp = NULL;
1589 	in_crit_src = in_crit_targ = 0;
1590 	/*
1591 	 * Get to and from pathnames.
1592 	 */
1593 	if (error = pn_get(fname, seg, &fpn))
1594 		return (error);
1595 	if (error = pn_get(tname, seg, &tpn)) {
1596 		pn_free(&fpn);
1597 		return (error);
1598 	}
1599 
1600 	/*
1601 	 * First we need to resolve the correct directories
1602 	 * The passed in directories may only be a starting point,
1603 	 * but we need the real directories the file(s) live in.
1604 	 * For example the fname may be something like usr/lib/sparc
1605 	 * and we were passed in the / directory, but we need to
1606 	 * use the lib directory for the rename.
1607 	 */
1608 
1609 	if (audit_active)
1610 		audit_setfsat_path(1);
1611 	/*
1612 	 * Lookup to and from directories.
1613 	 */
1614 	if (error = lookuppnat(&fpn, NULL, NO_FOLLOW, &fromvp, &fvp, fdvp)) {
1615 		goto out;
1616 	}
1617 
1618 	/*
1619 	 * Make sure there is an entry.
1620 	 */
1621 	if (fvp == NULL) {
1622 		error = ENOENT;
1623 		goto out;
1624 	}
1625 
1626 	if (audit_active)
1627 		audit_setfsat_path(3);
1628 	if (error = lookuppnat(&tpn, NULL, NO_FOLLOW, &tovp, &targvp, tdvp)) {
1629 		goto out;
1630 	}
1631 
1632 	/*
1633 	 * Make sure both the from vnode directory and the to directory
1634 	 * are in the same vfs and the to directory is writable.
1635 	 * We check fsid's, not vfs pointers, so loopback fs works.
1636 	 */
1637 	if (fromvp != tovp) {
1638 		vattr.va_mask = AT_FSID;
1639 		if (error = VOP_GETATTR(fromvp, &vattr, 0, CRED(), NULL))
1640 			goto out;
1641 		fsid = vattr.va_fsid;
1642 		vattr.va_mask = AT_FSID;
1643 		if (error = VOP_GETATTR(tovp, &vattr, 0, CRED(), NULL))
1644 			goto out;
1645 		if (fsid != vattr.va_fsid) {
1646 			error = EXDEV;
1647 			goto out;
1648 		}
1649 	}
1650 
1651 	if (tovp->v_vfsp->vfs_flag & VFS_RDONLY) {
1652 		error = EROFS;
1653 		goto out;
1654 	}
1655 
1656 	if (targvp && (fvp != targvp)) {
1657 		nbl_start_crit(targvp, RW_READER);
1658 		in_crit_targ = 1;
1659 		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
1660 			error = EACCES;
1661 			goto out;
1662 		}
1663 	}
1664 
1665 	if (nbl_need_check(fvp)) {
1666 		nbl_start_crit(fvp, RW_READER);
1667 		in_crit_src = 1;
1668 		if (nbl_conflict(fvp, NBL_RENAME, 0, 0, 0, NULL)) {
1669 			error = EACCES;
1670 			goto out;
1671 		}
1672 	}
1673 
1674 	/*
1675 	 * Do the rename.
1676 	 */
1677 	(void) pn_fixslash(&tpn);
1678 	error = VOP_RENAME(fromvp, fpn.pn_path, tovp, tpn.pn_path, CRED(),
1679 	    NULL, 0);
1680 
1681 out:
1682 	pn_free(&fpn);
1683 	pn_free(&tpn);
1684 	if (in_crit_src)
1685 		nbl_end_crit(fvp);
1686 	if (in_crit_targ)
1687 		nbl_end_crit(targvp);
1688 	if (fromvp)
1689 		VN_RELE(fromvp);
1690 	if (tovp)
1691 		VN_RELE(tovp);
1692 	if (targvp)
1693 		VN_RELE(targvp);
1694 	if (fvp)
1695 		VN_RELE(fvp);
1696 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1697 		goto top;
1698 	return (error);
1699 }
1700 
1701 /*
1702  * Remove a file or directory.
1703  */
1704 int
1705 vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
1706 {
1707 	return (vn_removeat(NULL, fnamep, seg, dirflag));
1708 }
1709 
1710 int
1711 vn_removeat(vnode_t *startvp, char *fnamep, enum uio_seg seg, enum rm dirflag)
1712 {
1713 	struct vnode *vp;		/* entry vnode */
1714 	struct vnode *dvp;		/* ptr to parent dir vnode */
1715 	struct vnode *coveredvp;
1716 	struct pathname pn;		/* name of entry */
1717 	enum vtype vtype;
1718 	int error;
1719 	struct vfs *vfsp;
1720 	struct vfs *dvfsp;	/* ptr to parent dir vfs */
1721 	int in_crit = 0;
1722 	int estale_retry = 0;
1723 
1724 top:
1725 	if (error = pn_get(fnamep, seg, &pn))
1726 		return (error);
1727 	dvp = vp = NULL;
1728 	if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &dvp, &vp, startvp)) {
1729 		pn_free(&pn);
1730 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1731 			goto top;
1732 		return (error);
1733 	}
1734 
1735 	/*
1736 	 * Make sure there is an entry.
1737 	 */
1738 	if (vp == NULL) {
1739 		error = ENOENT;
1740 		goto out;
1741 	}
1742 
1743 	vfsp = vp->v_vfsp;
1744 	dvfsp = dvp->v_vfsp;
1745 
1746 	/*
1747 	 * If the named file is the root of a mounted filesystem, fail,
1748 	 * unless it's marked unlinkable.  In that case, unmount the
1749 	 * filesystem and proceed to unlink the covered vnode.  (If the
1750 	 * covered vnode is a directory, use rmdir instead of unlink,
1751 	 * to avoid file system corruption.)
1752 	 */
1753 	if (vp->v_flag & VROOT) {
1754 		if ((vfsp->vfs_flag & VFS_UNLINKABLE) == 0) {
1755 			error = EBUSY;
1756 			goto out;
1757 		}
1758 
1759 		/*
1760 		 * Namefs specific code starts here.
1761 		 */
1762 
1763 		if (dirflag == RMDIRECTORY) {
1764 			/*
1765 			 * User called rmdir(2) on a file that has
1766 			 * been namefs mounted on top of.  Since
1767 			 * namefs doesn't allow directories to
1768 			 * be mounted on other files we know
1769 			 * vp is not of type VDIR so fail to operation.
1770 			 */
1771 			error = ENOTDIR;
1772 			goto out;
1773 		}
1774 
1775 		/*
1776 		 * If VROOT is still set after grabbing vp->v_lock,
1777 		 * noone has finished nm_unmount so far and coveredvp
1778 		 * is valid.
1779 		 * If we manage to grab vn_vfswlock(coveredvp) before releasing
1780 		 * vp->v_lock, any race window is eliminated.
1781 		 */
1782 
1783 		mutex_enter(&vp->v_lock);
1784 		if ((vp->v_flag & VROOT) == 0) {
1785 			/* Someone beat us to the unmount */
1786 			mutex_exit(&vp->v_lock);
1787 			error = EBUSY;
1788 			goto out;
1789 		}
1790 		vfsp = vp->v_vfsp;
1791 		coveredvp = vfsp->vfs_vnodecovered;
1792 		ASSERT(coveredvp);
1793 		/*
1794 		 * Note: Implementation of vn_vfswlock shows that ordering of
1795 		 * v_lock / vn_vfswlock is not an issue here.
1796 		 */
1797 		error = vn_vfswlock(coveredvp);
1798 		mutex_exit(&vp->v_lock);
1799 
1800 		if (error)
1801 			goto out;
1802 
1803 		VN_HOLD(coveredvp);
1804 		VN_RELE(vp);
1805 		error = dounmount(vfsp, 0, CRED());
1806 
1807 		/*
1808 		 * Unmounted the namefs file system; now get
1809 		 * the object it was mounted over.
1810 		 */
1811 		vp = coveredvp;
1812 		/*
1813 		 * If namefs was mounted over a directory, then
1814 		 * we want to use rmdir() instead of unlink().
1815 		 */
1816 		if (vp->v_type == VDIR)
1817 			dirflag = RMDIRECTORY;
1818 
1819 		if (error)
1820 			goto out;
1821 	}
1822 
1823 	/*
1824 	 * Make sure filesystem is writeable.
1825 	 * We check the parent directory's vfs in case this is an lofs vnode.
1826 	 */
1827 	if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) {
1828 		error = EROFS;
1829 		goto out;
1830 	}
1831 
1832 	vtype = vp->v_type;
1833 
1834 	/*
1835 	 * If there is the possibility of an nbmand share reservation, make
1836 	 * sure it's okay to remove the file.  Keep a reference to the
1837 	 * vnode, so that we can exit the nbl critical region after
1838 	 * calling VOP_REMOVE.
1839 	 * If there is no possibility of an nbmand share reservation,
1840 	 * release the vnode reference now.  Filesystems like NFS may
1841 	 * behave differently if there is an extra reference, so get rid of
1842 	 * this one.  Fortunately, we can't have nbmand mounts on NFS
1843 	 * filesystems.
1844 	 */
1845 	if (nbl_need_check(vp)) {
1846 		nbl_start_crit(vp, RW_READER);
1847 		in_crit = 1;
1848 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
1849 			error = EACCES;
1850 			goto out;
1851 		}
1852 	} else {
1853 		VN_RELE(vp);
1854 		vp = NULL;
1855 	}
1856 
1857 	if (dirflag == RMDIRECTORY) {
1858 		/*
1859 		 * Caller is using rmdir(2), which can only be applied to
1860 		 * directories.
1861 		 */
1862 		if (vtype != VDIR) {
1863 			error = ENOTDIR;
1864 		} else {
1865 			vnode_t *cwd;
1866 			proc_t *pp = curproc;
1867 
1868 			mutex_enter(&pp->p_lock);
1869 			cwd = PTOU(pp)->u_cdir;
1870 			VN_HOLD(cwd);
1871 			mutex_exit(&pp->p_lock);
1872 			error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED(),
1873 			    NULL, 0);
1874 			VN_RELE(cwd);
1875 		}
1876 	} else {
1877 		/*
1878 		 * Unlink(2) can be applied to anything.
1879 		 */
1880 		error = VOP_REMOVE(dvp, pn.pn_path, CRED(), NULL, 0);
1881 	}
1882 
1883 out:
1884 	pn_free(&pn);
1885 	if (in_crit) {
1886 		nbl_end_crit(vp);
1887 		in_crit = 0;
1888 	}
1889 	if (vp != NULL)
1890 		VN_RELE(vp);
1891 	if (dvp != NULL)
1892 		VN_RELE(dvp);
1893 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1894 		goto top;
1895 	return (error);
1896 }
1897 
1898 /*
1899  * Utility function to compare equality of vnodes.
1900  * Compare the underlying real vnodes, if there are underlying vnodes.
1901  * This is a more thorough comparison than the VN_CMP() macro provides.
1902  */
1903 int
1904 vn_compare(vnode_t *vp1, vnode_t *vp2)
1905 {
1906 	vnode_t *realvp;
1907 
1908 	if (vp1 != NULL && VOP_REALVP(vp1, &realvp, NULL) == 0)
1909 		vp1 = realvp;
1910 	if (vp2 != NULL && VOP_REALVP(vp2, &realvp, NULL) == 0)
1911 		vp2 = realvp;
1912 	return (VN_CMP(vp1, vp2));
1913 }
1914 
1915 /*
1916  * The number of locks to hash into.  This value must be a power
1917  * of 2 minus 1 and should probably also be prime.
1918  */
1919 #define	NUM_BUCKETS	1023
1920 
1921 struct  vn_vfslocks_bucket {
1922 	kmutex_t vb_lock;
1923 	vn_vfslocks_entry_t *vb_list;
1924 	char pad[64 - sizeof (kmutex_t) - sizeof (void *)];
1925 };
1926 
1927 /*
1928  * Total number of buckets will be NUM_BUCKETS + 1 .
1929  */
1930 
1931 #pragma	align	64(vn_vfslocks_buckets)
1932 static	struct vn_vfslocks_bucket	vn_vfslocks_buckets[NUM_BUCKETS + 1];
1933 
1934 #define	VN_VFSLOCKS_SHIFT	9
1935 
1936 #define	VN_VFSLOCKS_HASH(vfsvpptr)	\
1937 	((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS)
1938 
1939 /*
1940  * vn_vfslocks_getlock() uses an HASH scheme to generate
1941  * rwstlock using vfs/vnode pointer passed to it.
1942  *
1943  * vn_vfslocks_rele() releases a reference in the
1944  * HASH table which allows the entry allocated by
1945  * vn_vfslocks_getlock() to be freed at a later
1946  * stage when the refcount drops to zero.
1947  */
1948 
1949 vn_vfslocks_entry_t *
1950 vn_vfslocks_getlock(void *vfsvpptr)
1951 {
1952 	struct vn_vfslocks_bucket *bp;
1953 	vn_vfslocks_entry_t *vep;
1954 	vn_vfslocks_entry_t *tvep;
1955 
1956 	ASSERT(vfsvpptr != NULL);
1957 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)];
1958 
1959 	mutex_enter(&bp->vb_lock);
1960 	for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
1961 		if (vep->ve_vpvfs == vfsvpptr) {
1962 			vep->ve_refcnt++;
1963 			mutex_exit(&bp->vb_lock);
1964 			return (vep);
1965 		}
1966 	}
1967 	mutex_exit(&bp->vb_lock);
1968 	vep = kmem_alloc(sizeof (*vep), KM_SLEEP);
1969 	rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL);
1970 	vep->ve_vpvfs = (char *)vfsvpptr;
1971 	vep->ve_refcnt = 1;
1972 	mutex_enter(&bp->vb_lock);
1973 	for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) {
1974 		if (tvep->ve_vpvfs == vfsvpptr) {
1975 			tvep->ve_refcnt++;
1976 			mutex_exit(&bp->vb_lock);
1977 
1978 			/*
1979 			 * There is already an entry in the hash
1980 			 * destroy what we just allocated.
1981 			 */
1982 			rwst_destroy(&vep->ve_lock);
1983 			kmem_free(vep, sizeof (*vep));
1984 			return (tvep);
1985 		}
1986 	}
1987 	vep->ve_next = bp->vb_list;
1988 	bp->vb_list = vep;
1989 	mutex_exit(&bp->vb_lock);
1990 	return (vep);
1991 }
1992 
1993 void
1994 vn_vfslocks_rele(vn_vfslocks_entry_t *vepent)
1995 {
1996 	struct vn_vfslocks_bucket *bp;
1997 	vn_vfslocks_entry_t *vep;
1998 	vn_vfslocks_entry_t *pvep;
1999 
2000 	ASSERT(vepent != NULL);
2001 	ASSERT(vepent->ve_vpvfs != NULL);
2002 
2003 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)];
2004 
2005 	mutex_enter(&bp->vb_lock);
2006 	vepent->ve_refcnt--;
2007 
2008 	if ((int32_t)vepent->ve_refcnt < 0)
2009 		cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative");
2010 
2011 	if (vepent->ve_refcnt == 0) {
2012 		for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
2013 			if (vep->ve_vpvfs == vepent->ve_vpvfs) {
2014 				if (bp->vb_list == vep)
2015 					bp->vb_list = vep->ve_next;
2016 				else {
2017 					/* LINTED */
2018 					pvep->ve_next = vep->ve_next;
2019 				}
2020 				mutex_exit(&bp->vb_lock);
2021 				rwst_destroy(&vep->ve_lock);
2022 				kmem_free(vep, sizeof (*vep));
2023 				return;
2024 			}
2025 			pvep = vep;
2026 		}
2027 		cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found");
2028 	}
2029 	mutex_exit(&bp->vb_lock);
2030 }
2031 
2032 /*
2033  * vn_vfswlock_wait is used to implement a lock which is logically a writers
2034  * lock protecting the v_vfsmountedhere field.
2035  * vn_vfswlock_wait has been modified to be similar to vn_vfswlock,
2036  * except that it blocks to acquire the lock VVFSLOCK.
2037  *
2038  * traverse() and routines re-implementing part of traverse (e.g. autofs)
2039  * need to hold this lock. mount(), vn_rename(), vn_remove() and so on
2040  * need the non-blocking version of the writers lock i.e. vn_vfswlock
2041  */
2042 int
2043 vn_vfswlock_wait(vnode_t *vp)
2044 {
2045 	int retval;
2046 	vn_vfslocks_entry_t *vpvfsentry;
2047 	ASSERT(vp != NULL);
2048 
2049 	vpvfsentry = vn_vfslocks_getlock(vp);
2050 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER);
2051 
2052 	if (retval == EINTR) {
2053 		vn_vfslocks_rele(vpvfsentry);
2054 		return (EINTR);
2055 	}
2056 	return (retval);
2057 }
2058 
2059 int
2060 vn_vfsrlock_wait(vnode_t *vp)
2061 {
2062 	int retval;
2063 	vn_vfslocks_entry_t *vpvfsentry;
2064 	ASSERT(vp != NULL);
2065 
2066 	vpvfsentry = vn_vfslocks_getlock(vp);
2067 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER);
2068 
2069 	if (retval == EINTR) {
2070 		vn_vfslocks_rele(vpvfsentry);
2071 		return (EINTR);
2072 	}
2073 
2074 	return (retval);
2075 }
2076 
2077 
2078 /*
2079  * vn_vfswlock is used to implement a lock which is logically a writers lock
2080  * protecting the v_vfsmountedhere field.
2081  */
2082 int
2083 vn_vfswlock(vnode_t *vp)
2084 {
2085 	vn_vfslocks_entry_t *vpvfsentry;
2086 
2087 	/*
2088 	 * If vp is NULL then somebody is trying to lock the covered vnode
2089 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
2090 	 * only happen when unmounting /.  Since that operation will fail
2091 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
2092 	 */
2093 	if (vp == NULL)
2094 		return (EBUSY);
2095 
2096 	vpvfsentry = vn_vfslocks_getlock(vp);
2097 
2098 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
2099 		return (0);
2100 
2101 	vn_vfslocks_rele(vpvfsentry);
2102 	return (EBUSY);
2103 }
2104 
2105 int
2106 vn_vfsrlock(vnode_t *vp)
2107 {
2108 	vn_vfslocks_entry_t *vpvfsentry;
2109 
2110 	/*
2111 	 * If vp is NULL then somebody is trying to lock the covered vnode
2112 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
2113 	 * only happen when unmounting /.  Since that operation will fail
2114 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
2115 	 */
2116 	if (vp == NULL)
2117 		return (EBUSY);
2118 
2119 	vpvfsentry = vn_vfslocks_getlock(vp);
2120 
2121 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
2122 		return (0);
2123 
2124 	vn_vfslocks_rele(vpvfsentry);
2125 	return (EBUSY);
2126 }
2127 
2128 void
2129 vn_vfsunlock(vnode_t *vp)
2130 {
2131 	vn_vfslocks_entry_t *vpvfsentry;
2132 
2133 	/*
2134 	 * ve_refcnt needs to be decremented twice.
2135 	 * 1. To release refernce after a call to vn_vfslocks_getlock()
2136 	 * 2. To release the reference from the locking routines like
2137 	 *    vn_vfsrlock/vn_vfswlock etc,.
2138 	 */
2139 	vpvfsentry = vn_vfslocks_getlock(vp);
2140 	vn_vfslocks_rele(vpvfsentry);
2141 
2142 	rwst_exit(&vpvfsentry->ve_lock);
2143 	vn_vfslocks_rele(vpvfsentry);
2144 }
2145 
2146 int
2147 vn_vfswlock_held(vnode_t *vp)
2148 {
2149 	int held;
2150 	vn_vfslocks_entry_t *vpvfsentry;
2151 
2152 	ASSERT(vp != NULL);
2153 
2154 	vpvfsentry = vn_vfslocks_getlock(vp);
2155 	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
2156 
2157 	vn_vfslocks_rele(vpvfsentry);
2158 	return (held);
2159 }
2160 
2161 
2162 int
2163 vn_make_ops(
2164 	const char *name,			/* Name of file system */
2165 	const fs_operation_def_t *templ,	/* Operation specification */
2166 	vnodeops_t **actual)			/* Return the vnodeops */
2167 {
2168 	int unused_ops;
2169 	int error;
2170 
2171 	*actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP);
2172 
2173 	(*actual)->vnop_name = name;
2174 
2175 	error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ);
2176 	if (error) {
2177 		kmem_free(*actual, sizeof (vnodeops_t));
2178 	}
2179 
2180 #if DEBUG
2181 	if (unused_ops != 0)
2182 		cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied "
2183 		    "but not used", name, unused_ops);
2184 #endif
2185 
2186 	return (error);
2187 }
2188 
2189 /*
2190  * Free the vnodeops created as a result of vn_make_ops()
2191  */
2192 void
2193 vn_freevnodeops(vnodeops_t *vnops)
2194 {
2195 	kmem_free(vnops, sizeof (vnodeops_t));
2196 }
2197 
2198 /*
2199  * Vnode cache.
2200  */
2201 
2202 /* ARGSUSED */
2203 static int
2204 vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
2205 {
2206 	struct vnode *vp;
2207 
2208 	vp = buf;
2209 
2210 	mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
2211 	cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL);
2212 	rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL);
2213 	vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2214 	vp->v_path = NULL;
2215 	vp->v_mpssdata = NULL;
2216 	vp->v_vsd = NULL;
2217 	vp->v_fopdata = NULL;
2218 
2219 	return (0);
2220 }
2221 
2222 /* ARGSUSED */
2223 static void
2224 vn_cache_destructor(void *buf, void *cdrarg)
2225 {
2226 	struct vnode *vp;
2227 
2228 	vp = buf;
2229 
2230 	rw_destroy(&vp->v_nbllock);
2231 	cv_destroy(&vp->v_cv);
2232 	mutex_destroy(&vp->v_lock);
2233 }
2234 
2235 void
2236 vn_create_cache(void)
2237 {
2238 	vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode), 64,
2239 	    vn_cache_constructor, vn_cache_destructor, NULL, NULL,
2240 	    NULL, 0);
2241 }
2242 
2243 void
2244 vn_destroy_cache(void)
2245 {
2246 	kmem_cache_destroy(vn_cache);
2247 }
2248 
2249 /*
2250  * Used by file systems when fs-specific nodes (e.g., ufs inodes) are
2251  * cached by the file system and vnodes remain associated.
2252  */
2253 void
2254 vn_recycle(vnode_t *vp)
2255 {
2256 	ASSERT(vp->v_pages == NULL);
2257 
2258 	/*
2259 	 * XXX - This really belongs in vn_reinit(), but we have some issues
2260 	 * with the counts.  Best to have it here for clean initialization.
2261 	 */
2262 	vp->v_rdcnt = 0;
2263 	vp->v_wrcnt = 0;
2264 	vp->v_mmap_read = 0;
2265 	vp->v_mmap_write = 0;
2266 
2267 	/*
2268 	 * If FEM was in use, make sure everything gets cleaned up
2269 	 * NOTE: vp->v_femhead is initialized to NULL in the vnode
2270 	 * constructor.
2271 	 */
2272 	if (vp->v_femhead) {
2273 		/* XXX - There should be a free_femhead() that does all this */
2274 		ASSERT(vp->v_femhead->femh_list == NULL);
2275 		mutex_destroy(&vp->v_femhead->femh_lock);
2276 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2277 		vp->v_femhead = NULL;
2278 	}
2279 	if (vp->v_path) {
2280 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2281 		vp->v_path = NULL;
2282 	}
2283 
2284 	if (vp->v_fopdata != NULL) {
2285 		free_fopdata(vp);
2286 	}
2287 	vp->v_mpssdata = NULL;
2288 	vsd_free(vp);
2289 }
2290 
2291 /*
2292  * Used to reset the vnode fields including those that are directly accessible
2293  * as well as those which require an accessor function.
2294  *
2295  * Does not initialize:
2296  *	synchronization objects: v_lock, v_nbllock, v_cv
2297  *	v_data (since FS-nodes and vnodes point to each other and should
2298  *		be updated simultaneously)
2299  *	v_op (in case someone needs to make a VOP call on this object)
2300  */
2301 void
2302 vn_reinit(vnode_t *vp)
2303 {
2304 	vp->v_count = 1;
2305 	vp->v_count_dnlc = 0;
2306 	vp->v_vfsp = NULL;
2307 	vp->v_stream = NULL;
2308 	vp->v_vfsmountedhere = NULL;
2309 	vp->v_flag = 0;
2310 	vp->v_type = VNON;
2311 	vp->v_rdev = NODEV;
2312 
2313 	vp->v_filocks = NULL;
2314 	vp->v_shrlocks = NULL;
2315 	vp->v_pages = NULL;
2316 
2317 	vp->v_locality = NULL;
2318 	vp->v_xattrdir = NULL;
2319 
2320 	/* Handles v_femhead, v_path, and the r/w/map counts */
2321 	vn_recycle(vp);
2322 }
2323 
2324 vnode_t *
2325 vn_alloc(int kmflag)
2326 {
2327 	vnode_t *vp;
2328 
2329 	vp = kmem_cache_alloc(vn_cache, kmflag);
2330 
2331 	if (vp != NULL) {
2332 		vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2333 		vp->v_fopdata = NULL;
2334 		vn_reinit(vp);
2335 	}
2336 
2337 	return (vp);
2338 }
2339 
2340 void
2341 vn_free(vnode_t *vp)
2342 {
2343 	ASSERT(vp->v_shrlocks == NULL);
2344 	ASSERT(vp->v_filocks == NULL);
2345 
2346 	/*
2347 	 * Some file systems call vn_free() with v_count of zero,
2348 	 * some with v_count of 1.  In any case, the value should
2349 	 * never be anything else.
2350 	 */
2351 	ASSERT((vp->v_count == 0) || (vp->v_count == 1));
2352 	ASSERT(vp->v_count_dnlc == 0);
2353 	if (vp->v_path != NULL) {
2354 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2355 		vp->v_path = NULL;
2356 	}
2357 
2358 	/* If FEM was in use, make sure everything gets cleaned up */
2359 	if (vp->v_femhead) {
2360 		/* XXX - There should be a free_femhead() that does all this */
2361 		ASSERT(vp->v_femhead->femh_list == NULL);
2362 		mutex_destroy(&vp->v_femhead->femh_lock);
2363 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2364 		vp->v_femhead = NULL;
2365 	}
2366 
2367 	if (vp->v_fopdata != NULL) {
2368 		free_fopdata(vp);
2369 	}
2370 	vp->v_mpssdata = NULL;
2371 	vsd_free(vp);
2372 	kmem_cache_free(vn_cache, vp);
2373 }
2374 
2375 /*
2376  * vnode status changes, should define better states than 1, 0.
2377  */
2378 void
2379 vn_reclaim(vnode_t *vp)
2380 {
2381 	vfs_t   *vfsp = vp->v_vfsp;
2382 
2383 	if (vfsp == NULL ||
2384 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2385 		return;
2386 	}
2387 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED);
2388 }
2389 
2390 void
2391 vn_idle(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_IDLED);
2400 }
2401 void
2402 vn_exists(vnode_t *vp)
2403 {
2404 	vfs_t   *vfsp = vp->v_vfsp;
2405 
2406 	if (vfsp == NULL ||
2407 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2408 		return;
2409 	}
2410 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS);
2411 }
2412 
2413 void
2414 vn_invalid(vnode_t *vp)
2415 {
2416 	vfs_t   *vfsp = vp->v_vfsp;
2417 
2418 	if (vfsp == NULL ||
2419 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2420 		return;
2421 	}
2422 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED);
2423 }
2424 
2425 /* Vnode event notification */
2426 
2427 int
2428 vnevent_support(vnode_t *vp, caller_context_t *ct)
2429 {
2430 	if (vp == NULL)
2431 		return (EINVAL);
2432 
2433 	return (VOP_VNEVENT(vp, VE_SUPPORT, NULL, NULL, ct));
2434 }
2435 
2436 void
2437 vnevent_rename_src(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2438 {
2439 	if (vp == NULL || vp->v_femhead == NULL) {
2440 		return;
2441 	}
2442 	(void) VOP_VNEVENT(vp, VE_RENAME_SRC, dvp, name, ct);
2443 }
2444 
2445 void
2446 vnevent_rename_dest(vnode_t *vp, vnode_t *dvp, char *name,
2447     caller_context_t *ct)
2448 {
2449 	if (vp == NULL || vp->v_femhead == NULL) {
2450 		return;
2451 	}
2452 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST, dvp, name, ct);
2453 }
2454 
2455 void
2456 vnevent_rename_dest_dir(vnode_t *vp, caller_context_t *ct)
2457 {
2458 	if (vp == NULL || vp->v_femhead == NULL) {
2459 		return;
2460 	}
2461 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST_DIR, NULL, NULL, ct);
2462 }
2463 
2464 void
2465 vnevent_remove(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2466 {
2467 	if (vp == NULL || vp->v_femhead == NULL) {
2468 		return;
2469 	}
2470 	(void) VOP_VNEVENT(vp, VE_REMOVE, dvp, name, ct);
2471 }
2472 
2473 void
2474 vnevent_rmdir(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2475 {
2476 	if (vp == NULL || vp->v_femhead == NULL) {
2477 		return;
2478 	}
2479 	(void) VOP_VNEVENT(vp, VE_RMDIR, dvp, name, ct);
2480 }
2481 
2482 void
2483 vnevent_create(vnode_t *vp, caller_context_t *ct)
2484 {
2485 	if (vp == NULL || vp->v_femhead == NULL) {
2486 		return;
2487 	}
2488 	(void) VOP_VNEVENT(vp, VE_CREATE, NULL, NULL, ct);
2489 }
2490 
2491 void
2492 vnevent_link(vnode_t *vp, caller_context_t *ct)
2493 {
2494 	if (vp == NULL || vp->v_femhead == NULL) {
2495 		return;
2496 	}
2497 	(void) VOP_VNEVENT(vp, VE_LINK, NULL, NULL, ct);
2498 }
2499 
2500 void
2501 vnevent_mountedover(vnode_t *vp, caller_context_t *ct)
2502 {
2503 	if (vp == NULL || vp->v_femhead == NULL) {
2504 		return;
2505 	}
2506 	(void) VOP_VNEVENT(vp, VE_MOUNTEDOVER, NULL, NULL, ct);
2507 }
2508 
2509 /*
2510  * Vnode accessors.
2511  */
2512 
2513 int
2514 vn_is_readonly(vnode_t *vp)
2515 {
2516 	return (vp->v_vfsp->vfs_flag & VFS_RDONLY);
2517 }
2518 
2519 int
2520 vn_has_flocks(vnode_t *vp)
2521 {
2522 	return (vp->v_filocks != NULL);
2523 }
2524 
2525 int
2526 vn_has_mandatory_locks(vnode_t *vp, int mode)
2527 {
2528 	return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode)));
2529 }
2530 
2531 int
2532 vn_has_cached_data(vnode_t *vp)
2533 {
2534 	return (vp->v_pages != NULL);
2535 }
2536 
2537 /*
2538  * Return 0 if the vnode in question shouldn't be permitted into a zone via
2539  * zone_enter(2).
2540  */
2541 int
2542 vn_can_change_zones(vnode_t *vp)
2543 {
2544 	struct vfssw *vswp;
2545 	int allow = 1;
2546 	vnode_t *rvp;
2547 
2548 	if (nfs_global_client_only != 0)
2549 		return (1);
2550 
2551 	/*
2552 	 * We always want to look at the underlying vnode if there is one.
2553 	 */
2554 	if (VOP_REALVP(vp, &rvp, NULL) != 0)
2555 		rvp = vp;
2556 	/*
2557 	 * Some pseudo filesystems (including doorfs) don't actually register
2558 	 * their vfsops_t, so the following may return NULL; we happily let
2559 	 * such vnodes switch zones.
2560 	 */
2561 	vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp));
2562 	if (vswp != NULL) {
2563 		if (vswp->vsw_flag & VSW_NOTZONESAFE)
2564 			allow = 0;
2565 		vfs_unrefvfssw(vswp);
2566 	}
2567 	return (allow);
2568 }
2569 
2570 /*
2571  * Return nonzero if the vnode is a mount point, zero if not.
2572  */
2573 int
2574 vn_ismntpt(vnode_t *vp)
2575 {
2576 	return (vp->v_vfsmountedhere != NULL);
2577 }
2578 
2579 /* Retrieve the vfs (if any) mounted on this vnode */
2580 vfs_t *
2581 vn_mountedvfs(vnode_t *vp)
2582 {
2583 	return (vp->v_vfsmountedhere);
2584 }
2585 
2586 /*
2587  * Return nonzero if the vnode is referenced by the dnlc, zero if not.
2588  */
2589 int
2590 vn_in_dnlc(vnode_t *vp)
2591 {
2592 	return (vp->v_count_dnlc > 0);
2593 }
2594 
2595 /*
2596  * vn_has_other_opens() checks whether a particular file is opened by more than
2597  * just the caller and whether the open is for read and/or write.
2598  * This routine is for calling after the caller has already called VOP_OPEN()
2599  * and the caller wishes to know if they are the only one with it open for
2600  * the mode(s) specified.
2601  *
2602  * Vnode counts are only kept on regular files (v_type=VREG).
2603  */
2604 int
2605 vn_has_other_opens(
2606 	vnode_t *vp,
2607 	v_mode_t mode)
2608 {
2609 
2610 	ASSERT(vp != NULL);
2611 
2612 	switch (mode) {
2613 	case V_WRITE:
2614 		if (vp->v_wrcnt > 1)
2615 			return (V_TRUE);
2616 		break;
2617 	case V_RDORWR:
2618 		if ((vp->v_rdcnt > 1) || (vp->v_wrcnt > 1))
2619 			return (V_TRUE);
2620 		break;
2621 	case V_RDANDWR:
2622 		if ((vp->v_rdcnt > 1) && (vp->v_wrcnt > 1))
2623 			return (V_TRUE);
2624 		break;
2625 	case V_READ:
2626 		if (vp->v_rdcnt > 1)
2627 			return (V_TRUE);
2628 		break;
2629 	}
2630 
2631 	return (V_FALSE);
2632 }
2633 
2634 /*
2635  * vn_is_opened() checks whether a particular file is opened and
2636  * whether the open is for read and/or write.
2637  *
2638  * Vnode counts are only kept on regular files (v_type=VREG).
2639  */
2640 int
2641 vn_is_opened(
2642 	vnode_t *vp,
2643 	v_mode_t mode)
2644 {
2645 
2646 	ASSERT(vp != NULL);
2647 
2648 	switch (mode) {
2649 	case V_WRITE:
2650 		if (vp->v_wrcnt)
2651 			return (V_TRUE);
2652 		break;
2653 	case V_RDANDWR:
2654 		if (vp->v_rdcnt && vp->v_wrcnt)
2655 			return (V_TRUE);
2656 		break;
2657 	case V_RDORWR:
2658 		if (vp->v_rdcnt || vp->v_wrcnt)
2659 			return (V_TRUE);
2660 		break;
2661 	case V_READ:
2662 		if (vp->v_rdcnt)
2663 			return (V_TRUE);
2664 		break;
2665 	}
2666 
2667 	return (V_FALSE);
2668 }
2669 
2670 /*
2671  * vn_is_mapped() checks whether a particular file is mapped and whether
2672  * the file is mapped read and/or write.
2673  */
2674 int
2675 vn_is_mapped(
2676 	vnode_t *vp,
2677 	v_mode_t mode)
2678 {
2679 
2680 	ASSERT(vp != NULL);
2681 
2682 #if !defined(_LP64)
2683 	switch (mode) {
2684 	/*
2685 	 * The atomic_add_64_nv functions force atomicity in the
2686 	 * case of 32 bit architectures. Otherwise the 64 bit values
2687 	 * require two fetches. The value of the fields may be
2688 	 * (potentially) changed between the first fetch and the
2689 	 * second
2690 	 */
2691 	case V_WRITE:
2692 		if (atomic_add_64_nv((&(vp->v_mmap_write)), 0))
2693 			return (V_TRUE);
2694 		break;
2695 	case V_RDANDWR:
2696 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) &&
2697 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2698 			return (V_TRUE);
2699 		break;
2700 	case V_RDORWR:
2701 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) ||
2702 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2703 			return (V_TRUE);
2704 		break;
2705 	case V_READ:
2706 		if (atomic_add_64_nv((&(vp->v_mmap_read)), 0))
2707 			return (V_TRUE);
2708 		break;
2709 	}
2710 #else
2711 	switch (mode) {
2712 	case V_WRITE:
2713 		if (vp->v_mmap_write)
2714 			return (V_TRUE);
2715 		break;
2716 	case V_RDANDWR:
2717 		if (vp->v_mmap_read && vp->v_mmap_write)
2718 			return (V_TRUE);
2719 		break;
2720 	case V_RDORWR:
2721 		if (vp->v_mmap_read || vp->v_mmap_write)
2722 			return (V_TRUE);
2723 		break;
2724 	case V_READ:
2725 		if (vp->v_mmap_read)
2726 			return (V_TRUE);
2727 		break;
2728 	}
2729 #endif
2730 
2731 	return (V_FALSE);
2732 }
2733 
2734 /*
2735  * Set the operations vector for a vnode.
2736  *
2737  * FEM ensures that the v_femhead pointer is filled in before the
2738  * v_op pointer is changed.  This means that if the v_femhead pointer
2739  * is NULL, and the v_op field hasn't changed since before which checked
2740  * the v_femhead pointer; then our update is ok - we are not racing with
2741  * FEM.
2742  */
2743 void
2744 vn_setops(vnode_t *vp, vnodeops_t *vnodeops)
2745 {
2746 	vnodeops_t	*op;
2747 
2748 	ASSERT(vp != NULL);
2749 	ASSERT(vnodeops != NULL);
2750 
2751 	op = vp->v_op;
2752 	membar_consumer();
2753 	/*
2754 	 * If vp->v_femhead == NULL, then we'll call casptr() to do the
2755 	 * compare-and-swap on vp->v_op.  If either fails, then FEM is
2756 	 * in effect on the vnode and we need to have FEM deal with it.
2757 	 */
2758 	if (vp->v_femhead != NULL || casptr(&vp->v_op, op, vnodeops) != op) {
2759 		fem_setvnops(vp, vnodeops);
2760 	}
2761 }
2762 
2763 /*
2764  * Retrieve the operations vector for a vnode
2765  * As with vn_setops(above); make sure we aren't racing with FEM.
2766  * FEM sets the v_op to a special, internal, vnodeops that wouldn't
2767  * make sense to the callers of this routine.
2768  */
2769 vnodeops_t *
2770 vn_getops(vnode_t *vp)
2771 {
2772 	vnodeops_t	*op;
2773 
2774 	ASSERT(vp != NULL);
2775 
2776 	op = vp->v_op;
2777 	membar_consumer();
2778 	if (vp->v_femhead == NULL && op == vp->v_op) {
2779 		return (op);
2780 	} else {
2781 		return (fem_getvnops(vp));
2782 	}
2783 }
2784 
2785 /*
2786  * Returns non-zero (1) if the vnodeops matches that of the vnode.
2787  * Returns zero (0) if not.
2788  */
2789 int
2790 vn_matchops(vnode_t *vp, vnodeops_t *vnodeops)
2791 {
2792 	return (vn_getops(vp) == vnodeops);
2793 }
2794 
2795 /*
2796  * Returns non-zero (1) if the specified operation matches the
2797  * corresponding operation for that the vnode.
2798  * Returns zero (0) if not.
2799  */
2800 
2801 #define	MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0))
2802 
2803 int
2804 vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp)
2805 {
2806 	const fs_operation_trans_def_t *otdp;
2807 	fs_generic_func_p *loc = NULL;
2808 	vnodeops_t	*vop = vn_getops(vp);
2809 
2810 	ASSERT(vopname != NULL);
2811 
2812 	for (otdp = vn_ops_table; otdp->name != NULL; otdp++) {
2813 		if (MATCHNAME(otdp->name, vopname)) {
2814 			loc = (fs_generic_func_p *)
2815 			    ((char *)(vop) + otdp->offset);
2816 			break;
2817 		}
2818 	}
2819 
2820 	return ((loc != NULL) && (*loc == funcp));
2821 }
2822 
2823 /*
2824  * fs_new_caller_id() needs to return a unique ID on a given local system.
2825  * The IDs do not need to survive across reboots.  These are primarily
2826  * used so that (FEM) monitors can detect particular callers (such as
2827  * the NFS server) to a given vnode/vfs operation.
2828  */
2829 u_longlong_t
2830 fs_new_caller_id()
2831 {
2832 	static uint64_t next_caller_id = 0LL; /* First call returns 1 */
2833 
2834 	return ((u_longlong_t)atomic_add_64_nv(&next_caller_id, 1));
2835 }
2836 
2837 /*
2838  * Given a starting vnode and a path, updates the path in the target vnode in
2839  * a safe manner.  If the vnode already has path information embedded, then the
2840  * cached path is left untouched.
2841  */
2842 
2843 size_t max_vnode_path = 4 * MAXPATHLEN;
2844 
2845 void
2846 vn_setpath(vnode_t *rootvp, struct vnode *startvp, struct vnode *vp,
2847     const char *path, size_t plen)
2848 {
2849 	char	*rpath;
2850 	vnode_t	*base;
2851 	size_t	rpathlen, rpathalloc;
2852 	int	doslash = 1;
2853 
2854 	if (*path == '/') {
2855 		base = rootvp;
2856 		path++;
2857 		plen--;
2858 	} else {
2859 		base = startvp;
2860 	}
2861 
2862 	/*
2863 	 * We cannot grab base->v_lock while we hold vp->v_lock because of
2864 	 * the potential for deadlock.
2865 	 */
2866 	mutex_enter(&base->v_lock);
2867 	if (base->v_path == NULL) {
2868 		mutex_exit(&base->v_lock);
2869 		return;
2870 	}
2871 
2872 	rpathlen = strlen(base->v_path);
2873 	rpathalloc = rpathlen + plen + 1;
2874 	/* Avoid adding a slash if there's already one there */
2875 	if (base->v_path[rpathlen-1] == '/')
2876 		doslash = 0;
2877 	else
2878 		rpathalloc++;
2879 
2880 	/*
2881 	 * We don't want to call kmem_alloc(KM_SLEEP) with kernel locks held,
2882 	 * so we must do this dance.  If, by chance, something changes the path,
2883 	 * just give up since there is no real harm.
2884 	 */
2885 	mutex_exit(&base->v_lock);
2886 
2887 	/* Paths should stay within reason */
2888 	if (rpathalloc > max_vnode_path)
2889 		return;
2890 
2891 	rpath = kmem_alloc(rpathalloc, KM_SLEEP);
2892 
2893 	mutex_enter(&base->v_lock);
2894 	if (base->v_path == NULL || strlen(base->v_path) != rpathlen) {
2895 		mutex_exit(&base->v_lock);
2896 		kmem_free(rpath, rpathalloc);
2897 		return;
2898 	}
2899 	bcopy(base->v_path, rpath, rpathlen);
2900 	mutex_exit(&base->v_lock);
2901 
2902 	if (doslash)
2903 		rpath[rpathlen++] = '/';
2904 	bcopy(path, rpath + rpathlen, plen);
2905 	rpath[rpathlen + plen] = '\0';
2906 
2907 	mutex_enter(&vp->v_lock);
2908 	if (vp->v_path != NULL) {
2909 		mutex_exit(&vp->v_lock);
2910 		kmem_free(rpath, rpathalloc);
2911 	} else {
2912 		vp->v_path = rpath;
2913 		mutex_exit(&vp->v_lock);
2914 	}
2915 }
2916 
2917 /*
2918  * Sets the path to the vnode to be the given string, regardless of current
2919  * context.  The string must be a complete path from rootdir.  This is only used
2920  * by fsop_root() for setting the path based on the mountpoint.
2921  */
2922 void
2923 vn_setpath_str(struct vnode *vp, const char *str, size_t len)
2924 {
2925 	char *buf = kmem_alloc(len + 1, KM_SLEEP);
2926 
2927 	mutex_enter(&vp->v_lock);
2928 	if (vp->v_path != NULL) {
2929 		mutex_exit(&vp->v_lock);
2930 		kmem_free(buf, len + 1);
2931 		return;
2932 	}
2933 
2934 	vp->v_path = buf;
2935 	bcopy(str, vp->v_path, len);
2936 	vp->v_path[len] = '\0';
2937 
2938 	mutex_exit(&vp->v_lock);
2939 }
2940 
2941 /*
2942  * Called from within filesystem's vop_rename() to handle renames once the
2943  * target vnode is available.
2944  */
2945 void
2946 vn_renamepath(vnode_t *dvp, vnode_t *vp, const char *nm, size_t len)
2947 {
2948 	char *tmp;
2949 
2950 	mutex_enter(&vp->v_lock);
2951 	tmp = vp->v_path;
2952 	vp->v_path = NULL;
2953 	mutex_exit(&vp->v_lock);
2954 	vn_setpath(rootdir, dvp, vp, nm, len);
2955 	if (tmp != NULL)
2956 		kmem_free(tmp, strlen(tmp) + 1);
2957 }
2958 
2959 /*
2960  * Similar to vn_setpath_str(), this function sets the path of the destination
2961  * vnode to the be the same as the source vnode.
2962  */
2963 void
2964 vn_copypath(struct vnode *src, struct vnode *dst)
2965 {
2966 	char *buf;
2967 	int alloc;
2968 
2969 	mutex_enter(&src->v_lock);
2970 	if (src->v_path == NULL) {
2971 		mutex_exit(&src->v_lock);
2972 		return;
2973 	}
2974 	alloc = strlen(src->v_path) + 1;
2975 
2976 	/* avoid kmem_alloc() with lock held */
2977 	mutex_exit(&src->v_lock);
2978 	buf = kmem_alloc(alloc, KM_SLEEP);
2979 	mutex_enter(&src->v_lock);
2980 	if (src->v_path == NULL || strlen(src->v_path) + 1 != alloc) {
2981 		mutex_exit(&src->v_lock);
2982 		kmem_free(buf, alloc);
2983 		return;
2984 	}
2985 	bcopy(src->v_path, buf, alloc);
2986 	mutex_exit(&src->v_lock);
2987 
2988 	mutex_enter(&dst->v_lock);
2989 	if (dst->v_path != NULL) {
2990 		mutex_exit(&dst->v_lock);
2991 		kmem_free(buf, alloc);
2992 		return;
2993 	}
2994 	dst->v_path = buf;
2995 	mutex_exit(&dst->v_lock);
2996 }
2997 
2998 /*
2999  * XXX Private interface for segvn routines that handle vnode
3000  * large page segments.
3001  *
3002  * return 1 if vp's file system VOP_PAGEIO() implementation
3003  * can be safely used instead of VOP_GETPAGE() for handling
3004  * pagefaults against regular non swap files. VOP_PAGEIO()
3005  * interface is considered safe here if its implementation
3006  * is very close to VOP_GETPAGE() implementation.
3007  * e.g. It zero's out the part of the page beyond EOF. Doesn't
3008  * panic if there're file holes but instead returns an error.
3009  * Doesn't assume file won't be changed by user writes, etc.
3010  *
3011  * return 0 otherwise.
3012  *
3013  * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs.
3014  */
3015 int
3016 vn_vmpss_usepageio(vnode_t *vp)
3017 {
3018 	vfs_t   *vfsp = vp->v_vfsp;
3019 	char *fsname = vfssw[vfsp->vfs_fstype].vsw_name;
3020 	char *pageio_ok_fss[] = {"ufs", "nfs", NULL};
3021 	char **fsok = pageio_ok_fss;
3022 
3023 	if (fsname == NULL) {
3024 		return (0);
3025 	}
3026 
3027 	for (; *fsok; fsok++) {
3028 		if (strcmp(*fsok, fsname) == 0) {
3029 			return (1);
3030 		}
3031 	}
3032 	return (0);
3033 }
3034 
3035 /* VOP_XXX() macros call the corresponding fop_xxx() function */
3036 
3037 int
3038 fop_open(
3039 	vnode_t **vpp,
3040 	int mode,
3041 	cred_t *cr,
3042 	caller_context_t *ct)
3043 {
3044 	int ret;
3045 	vnode_t *vp = *vpp;
3046 
3047 	VN_HOLD(vp);
3048 	/*
3049 	 * Adding to the vnode counts before calling open
3050 	 * avoids the need for a mutex. It circumvents a race
3051 	 * condition where a query made on the vnode counts results in a
3052 	 * false negative. The inquirer goes away believing the file is
3053 	 * not open when there is an open on the file already under way.
3054 	 *
3055 	 * The counts are meant to prevent NFS from granting a delegation
3056 	 * when it would be dangerous to do so.
3057 	 *
3058 	 * The vnode counts are only kept on regular files
3059 	 */
3060 	if ((*vpp)->v_type == VREG) {
3061 		if (mode & FREAD)
3062 			atomic_add_32(&((*vpp)->v_rdcnt), 1);
3063 		if (mode & FWRITE)
3064 			atomic_add_32(&((*vpp)->v_wrcnt), 1);
3065 	}
3066 
3067 	VOPXID_MAP_CR(vp, cr);
3068 
3069 	ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr, ct);
3070 
3071 	if (ret) {
3072 		/*
3073 		 * Use the saved vp just in case the vnode ptr got trashed
3074 		 * by the error.
3075 		 */
3076 		VOPSTATS_UPDATE(vp, open);
3077 		if ((vp->v_type == VREG) && (mode & FREAD))
3078 			atomic_add_32(&(vp->v_rdcnt), -1);
3079 		if ((vp->v_type == VREG) && (mode & FWRITE))
3080 			atomic_add_32(&(vp->v_wrcnt), -1);
3081 	} else {
3082 		/*
3083 		 * Some filesystems will return a different vnode,
3084 		 * but the same path was still used to open it.
3085 		 * So if we do change the vnode and need to
3086 		 * copy over the path, do so here, rather than special
3087 		 * casing each filesystem. Adjust the vnode counts to
3088 		 * reflect the vnode switch.
3089 		 */
3090 		VOPSTATS_UPDATE(*vpp, open);
3091 		if (*vpp != vp && *vpp != NULL) {
3092 			vn_copypath(vp, *vpp);
3093 			if (((*vpp)->v_type == VREG) && (mode & FREAD))
3094 				atomic_add_32(&((*vpp)->v_rdcnt), 1);
3095 			if ((vp->v_type == VREG) && (mode & FREAD))
3096 				atomic_add_32(&(vp->v_rdcnt), -1);
3097 			if (((*vpp)->v_type == VREG) && (mode & FWRITE))
3098 				atomic_add_32(&((*vpp)->v_wrcnt), 1);
3099 			if ((vp->v_type == VREG) && (mode & FWRITE))
3100 				atomic_add_32(&(vp->v_wrcnt), -1);
3101 		}
3102 	}
3103 	VN_RELE(vp);
3104 	return (ret);
3105 }
3106 
3107 int
3108 fop_close(
3109 	vnode_t *vp,
3110 	int flag,
3111 	int count,
3112 	offset_t offset,
3113 	cred_t *cr,
3114 	caller_context_t *ct)
3115 {
3116 	int err;
3117 
3118 	VOPXID_MAP_CR(vp, cr);
3119 
3120 	err = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr, ct);
3121 	VOPSTATS_UPDATE(vp, close);
3122 	/*
3123 	 * Check passed in count to handle possible dups. Vnode counts are only
3124 	 * kept on regular files
3125 	 */
3126 	if ((vp->v_type == VREG) && (count == 1))  {
3127 		if (flag & FREAD) {
3128 			ASSERT(vp->v_rdcnt > 0);
3129 			atomic_add_32(&(vp->v_rdcnt), -1);
3130 		}
3131 		if (flag & FWRITE) {
3132 			ASSERT(vp->v_wrcnt > 0);
3133 			atomic_add_32(&(vp->v_wrcnt), -1);
3134 		}
3135 	}
3136 	return (err);
3137 }
3138 
3139 int
3140 fop_read(
3141 	vnode_t *vp,
3142 	uio_t *uiop,
3143 	int ioflag,
3144 	cred_t *cr,
3145 	caller_context_t *ct)
3146 {
3147 	int	err;
3148 	ssize_t	resid_start = uiop->uio_resid;
3149 
3150 	VOPXID_MAP_CR(vp, cr);
3151 
3152 	err = (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct);
3153 	VOPSTATS_UPDATE_IO(vp, read,
3154 	    read_bytes, (resid_start - uiop->uio_resid));
3155 	return (err);
3156 }
3157 
3158 int
3159 fop_write(
3160 	vnode_t *vp,
3161 	uio_t *uiop,
3162 	int ioflag,
3163 	cred_t *cr,
3164 	caller_context_t *ct)
3165 {
3166 	int	err;
3167 	ssize_t	resid_start = uiop->uio_resid;
3168 
3169 	VOPXID_MAP_CR(vp, cr);
3170 
3171 	err = (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct);
3172 	VOPSTATS_UPDATE_IO(vp, write,
3173 	    write_bytes, (resid_start - uiop->uio_resid));
3174 	return (err);
3175 }
3176 
3177 int
3178 fop_ioctl(
3179 	vnode_t *vp,
3180 	int cmd,
3181 	intptr_t arg,
3182 	int flag,
3183 	cred_t *cr,
3184 	int *rvalp,
3185 	caller_context_t *ct)
3186 {
3187 	int	err;
3188 
3189 	VOPXID_MAP_CR(vp, cr);
3190 
3191 	err = (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp, ct);
3192 	VOPSTATS_UPDATE(vp, ioctl);
3193 	return (err);
3194 }
3195 
3196 int
3197 fop_setfl(
3198 	vnode_t *vp,
3199 	int oflags,
3200 	int nflags,
3201 	cred_t *cr,
3202 	caller_context_t *ct)
3203 {
3204 	int	err;
3205 
3206 	VOPXID_MAP_CR(vp, cr);
3207 
3208 	err = (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr, ct);
3209 	VOPSTATS_UPDATE(vp, setfl);
3210 	return (err);
3211 }
3212 
3213 int
3214 fop_getattr(
3215 	vnode_t *vp,
3216 	vattr_t *vap,
3217 	int flags,
3218 	cred_t *cr,
3219 	caller_context_t *ct)
3220 {
3221 	int	err;
3222 
3223 	VOPXID_MAP_CR(vp, cr);
3224 
3225 	/*
3226 	 * If this file system doesn't understand the xvattr extensions
3227 	 * then turn off the xvattr bit.
3228 	 */
3229 	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3230 		vap->va_mask &= ~AT_XVATTR;
3231 	}
3232 
3233 	/*
3234 	 * We're only allowed to skip the ACL check iff we used a 32 bit
3235 	 * ACE mask with VOP_ACCESS() to determine permissions.
3236 	 */
3237 	if ((flags & ATTR_NOACLCHECK) &&
3238 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3239 		return (EINVAL);
3240 	}
3241 	err = (*(vp)->v_op->vop_getattr)(vp, vap, flags, cr, ct);
3242 	VOPSTATS_UPDATE(vp, getattr);
3243 	return (err);
3244 }
3245 
3246 int
3247 fop_setattr(
3248 	vnode_t *vp,
3249 	vattr_t *vap,
3250 	int flags,
3251 	cred_t *cr,
3252 	caller_context_t *ct)
3253 {
3254 	int	err;
3255 
3256 	VOPXID_MAP_CR(vp, cr);
3257 
3258 	/*
3259 	 * If this file system doesn't understand the xvattr extensions
3260 	 * then turn off the xvattr bit.
3261 	 */
3262 	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3263 		vap->va_mask &= ~AT_XVATTR;
3264 	}
3265 
3266 	/*
3267 	 * We're only allowed to skip the ACL check iff we used a 32 bit
3268 	 * ACE mask with VOP_ACCESS() to determine permissions.
3269 	 */
3270 	if ((flags & ATTR_NOACLCHECK) &&
3271 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3272 		return (EINVAL);
3273 	}
3274 	err = (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct);
3275 	VOPSTATS_UPDATE(vp, setattr);
3276 	return (err);
3277 }
3278 
3279 int
3280 fop_access(
3281 	vnode_t *vp,
3282 	int mode,
3283 	int flags,
3284 	cred_t *cr,
3285 	caller_context_t *ct)
3286 {
3287 	int	err;
3288 
3289 	if ((flags & V_ACE_MASK) &&
3290 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3291 		return (EINVAL);
3292 	}
3293 
3294 	VOPXID_MAP_CR(vp, cr);
3295 
3296 	err = (*(vp)->v_op->vop_access)(vp, mode, flags, cr, ct);
3297 	VOPSTATS_UPDATE(vp, access);
3298 	return (err);
3299 }
3300 
3301 int
3302 fop_lookup(
3303 	vnode_t *dvp,
3304 	char *nm,
3305 	vnode_t **vpp,
3306 	pathname_t *pnp,
3307 	int flags,
3308 	vnode_t *rdir,
3309 	cred_t *cr,
3310 	caller_context_t *ct,
3311 	int *deflags,		/* Returned per-dirent flags */
3312 	pathname_t *ppnp)	/* Returned case-preserved name in directory */
3313 {
3314 	int ret;
3315 
3316 	/*
3317 	 * If this file system doesn't support case-insensitive access
3318 	 * and said access is requested, fail quickly.  It is required
3319 	 * that if the vfs supports case-insensitive lookup, it also
3320 	 * supports extended dirent flags.
3321 	 */
3322 	if (flags & FIGNORECASE &&
3323 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3324 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3325 		return (EINVAL);
3326 
3327 	VOPXID_MAP_CR(dvp, cr);
3328 
3329 	if ((flags & LOOKUP_XATTR) && (flags & LOOKUP_HAVE_SYSATTR_DIR) == 0) {
3330 		ret = xattr_dir_lookup(dvp, vpp, flags, cr);
3331 	} else {
3332 		ret = (*(dvp)->v_op->vop_lookup)
3333 		    (dvp, nm, vpp, pnp, flags, rdir, cr, ct, deflags, ppnp);
3334 	}
3335 	if (ret == 0 && *vpp) {
3336 		VOPSTATS_UPDATE(*vpp, lookup);
3337 		if ((*vpp)->v_path == NULL) {
3338 			vn_setpath(rootdir, dvp, *vpp, nm, strlen(nm));
3339 		}
3340 	}
3341 
3342 	return (ret);
3343 }
3344 
3345 int
3346 fop_create(
3347 	vnode_t *dvp,
3348 	char *name,
3349 	vattr_t *vap,
3350 	vcexcl_t excl,
3351 	int mode,
3352 	vnode_t **vpp,
3353 	cred_t *cr,
3354 	int flags,
3355 	caller_context_t *ct,
3356 	vsecattr_t *vsecp)	/* ACL to set during create */
3357 {
3358 	int ret;
3359 
3360 	if (vsecp != NULL &&
3361 	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3362 		return (EINVAL);
3363 	}
3364 	/*
3365 	 * If this file system doesn't support case-insensitive access
3366 	 * and said access is requested, fail quickly.
3367 	 */
3368 	if (flags & FIGNORECASE &&
3369 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3370 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3371 		return (EINVAL);
3372 
3373 	VOPXID_MAP_CR(dvp, cr);
3374 
3375 	ret = (*(dvp)->v_op->vop_create)
3376 	    (dvp, name, vap, excl, mode, vpp, cr, flags, ct, vsecp);
3377 	if (ret == 0 && *vpp) {
3378 		VOPSTATS_UPDATE(*vpp, create);
3379 		if ((*vpp)->v_path == NULL) {
3380 			vn_setpath(rootdir, dvp, *vpp, name, strlen(name));
3381 		}
3382 	}
3383 
3384 	return (ret);
3385 }
3386 
3387 int
3388 fop_remove(
3389 	vnode_t *dvp,
3390 	char *nm,
3391 	cred_t *cr,
3392 	caller_context_t *ct,
3393 	int flags)
3394 {
3395 	int	err;
3396 
3397 	/*
3398 	 * If this file system doesn't support case-insensitive access
3399 	 * and said access is requested, fail quickly.
3400 	 */
3401 	if (flags & FIGNORECASE &&
3402 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3403 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3404 		return (EINVAL);
3405 
3406 	VOPXID_MAP_CR(dvp, cr);
3407 
3408 	err = (*(dvp)->v_op->vop_remove)(dvp, nm, cr, ct, flags);
3409 	VOPSTATS_UPDATE(dvp, remove);
3410 	return (err);
3411 }
3412 
3413 int
3414 fop_link(
3415 	vnode_t *tdvp,
3416 	vnode_t *svp,
3417 	char *tnm,
3418 	cred_t *cr,
3419 	caller_context_t *ct,
3420 	int flags)
3421 {
3422 	int	err;
3423 
3424 	/*
3425 	 * If the target file system doesn't support case-insensitive access
3426 	 * and said access is requested, fail quickly.
3427 	 */
3428 	if (flags & FIGNORECASE &&
3429 	    (vfs_has_feature(tdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3430 	    vfs_has_feature(tdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3431 		return (EINVAL);
3432 
3433 	VOPXID_MAP_CR(tdvp, cr);
3434 
3435 	err = (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr, ct, flags);
3436 	VOPSTATS_UPDATE(tdvp, link);
3437 	return (err);
3438 }
3439 
3440 int
3441 fop_rename(
3442 	vnode_t *sdvp,
3443 	char *snm,
3444 	vnode_t *tdvp,
3445 	char *tnm,
3446 	cred_t *cr,
3447 	caller_context_t *ct,
3448 	int flags)
3449 {
3450 	int	err;
3451 
3452 	/*
3453 	 * If the file system involved does not support
3454 	 * case-insensitive access and said access is requested, fail
3455 	 * quickly.
3456 	 */
3457 	if (flags & FIGNORECASE &&
3458 	    ((vfs_has_feature(sdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3459 	    vfs_has_feature(sdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0)))
3460 		return (EINVAL);
3461 
3462 	VOPXID_MAP_CR(tdvp, cr);
3463 
3464 	err = (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr, ct, flags);
3465 	VOPSTATS_UPDATE(sdvp, rename);
3466 	return (err);
3467 }
3468 
3469 int
3470 fop_mkdir(
3471 	vnode_t *dvp,
3472 	char *dirname,
3473 	vattr_t *vap,
3474 	vnode_t **vpp,
3475 	cred_t *cr,
3476 	caller_context_t *ct,
3477 	int flags,
3478 	vsecattr_t *vsecp)	/* ACL to set during create */
3479 {
3480 	int ret;
3481 
3482 	if (vsecp != NULL &&
3483 	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3484 		return (EINVAL);
3485 	}
3486 	/*
3487 	 * If this file system doesn't support case-insensitive access
3488 	 * and said access is requested, fail quickly.
3489 	 */
3490 	if (flags & FIGNORECASE &&
3491 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3492 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3493 		return (EINVAL);
3494 
3495 	VOPXID_MAP_CR(dvp, cr);
3496 
3497 	ret = (*(dvp)->v_op->vop_mkdir)
3498 	    (dvp, dirname, vap, vpp, cr, ct, flags, vsecp);
3499 	if (ret == 0 && *vpp) {
3500 		VOPSTATS_UPDATE(*vpp, mkdir);
3501 		if ((*vpp)->v_path == NULL) {
3502 			vn_setpath(rootdir, dvp, *vpp, dirname,
3503 			    strlen(dirname));
3504 		}
3505 	}
3506 
3507 	return (ret);
3508 }
3509 
3510 int
3511 fop_rmdir(
3512 	vnode_t *dvp,
3513 	char *nm,
3514 	vnode_t *cdir,
3515 	cred_t *cr,
3516 	caller_context_t *ct,
3517 	int flags)
3518 {
3519 	int	err;
3520 
3521 	/*
3522 	 * If this file system doesn't support case-insensitive access
3523 	 * and said access is requested, fail quickly.
3524 	 */
3525 	if (flags & FIGNORECASE &&
3526 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3527 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3528 		return (EINVAL);
3529 
3530 	VOPXID_MAP_CR(dvp, cr);
3531 
3532 	err = (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr, ct, flags);
3533 	VOPSTATS_UPDATE(dvp, rmdir);
3534 	return (err);
3535 }
3536 
3537 int
3538 fop_readdir(
3539 	vnode_t *vp,
3540 	uio_t *uiop,
3541 	cred_t *cr,
3542 	int *eofp,
3543 	caller_context_t *ct,
3544 	int flags)
3545 {
3546 	int	err;
3547 	ssize_t	resid_start = uiop->uio_resid;
3548 
3549 	/*
3550 	 * If this file system doesn't support retrieving directory
3551 	 * entry flags and said access is requested, fail quickly.
3552 	 */
3553 	if (flags & V_RDDIR_ENTFLAGS &&
3554 	    vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS) == 0)
3555 		return (EINVAL);
3556 
3557 	VOPXID_MAP_CR(vp, cr);
3558 
3559 	err = (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp, ct, flags);
3560 	VOPSTATS_UPDATE_IO(vp, readdir,
3561 	    readdir_bytes, (resid_start - uiop->uio_resid));
3562 	return (err);
3563 }
3564 
3565 int
3566 fop_symlink(
3567 	vnode_t *dvp,
3568 	char *linkname,
3569 	vattr_t *vap,
3570 	char *target,
3571 	cred_t *cr,
3572 	caller_context_t *ct,
3573 	int flags)
3574 {
3575 	int	err;
3576 
3577 	/*
3578 	 * If this file system doesn't support case-insensitive access
3579 	 * and said access is requested, fail quickly.
3580 	 */
3581 	if (flags & FIGNORECASE &&
3582 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3583 	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3584 		return (EINVAL);
3585 
3586 	VOPXID_MAP_CR(dvp, cr);
3587 
3588 	err = (*(dvp)->v_op->vop_symlink)
3589 	    (dvp, linkname, vap, target, cr, ct, flags);
3590 	VOPSTATS_UPDATE(dvp, symlink);
3591 	return (err);
3592 }
3593 
3594 int
3595 fop_readlink(
3596 	vnode_t *vp,
3597 	uio_t *uiop,
3598 	cred_t *cr,
3599 	caller_context_t *ct)
3600 {
3601 	int	err;
3602 
3603 	VOPXID_MAP_CR(vp, cr);
3604 
3605 	err = (*(vp)->v_op->vop_readlink)(vp, uiop, cr, ct);
3606 	VOPSTATS_UPDATE(vp, readlink);
3607 	return (err);
3608 }
3609 
3610 int
3611 fop_fsync(
3612 	vnode_t *vp,
3613 	int syncflag,
3614 	cred_t *cr,
3615 	caller_context_t *ct)
3616 {
3617 	int	err;
3618 
3619 	VOPXID_MAP_CR(vp, cr);
3620 
3621 	err = (*(vp)->v_op->vop_fsync)(vp, syncflag, cr, ct);
3622 	VOPSTATS_UPDATE(vp, fsync);
3623 	return (err);
3624 }
3625 
3626 void
3627 fop_inactive(
3628 	vnode_t *vp,
3629 	cred_t *cr,
3630 	caller_context_t *ct)
3631 {
3632 	/* Need to update stats before vop call since we may lose the vnode */
3633 	VOPSTATS_UPDATE(vp, inactive);
3634 
3635 	VOPXID_MAP_CR(vp, cr);
3636 
3637 	(*(vp)->v_op->vop_inactive)(vp, cr, ct);
3638 }
3639 
3640 int
3641 fop_fid(
3642 	vnode_t *vp,
3643 	fid_t *fidp,
3644 	caller_context_t *ct)
3645 {
3646 	int	err;
3647 
3648 	err = (*(vp)->v_op->vop_fid)(vp, fidp, ct);
3649 	VOPSTATS_UPDATE(vp, fid);
3650 	return (err);
3651 }
3652 
3653 int
3654 fop_rwlock(
3655 	vnode_t *vp,
3656 	int write_lock,
3657 	caller_context_t *ct)
3658 {
3659 	int	ret;
3660 
3661 	ret = ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct));
3662 	VOPSTATS_UPDATE(vp, rwlock);
3663 	return (ret);
3664 }
3665 
3666 void
3667 fop_rwunlock(
3668 	vnode_t *vp,
3669 	int write_lock,
3670 	caller_context_t *ct)
3671 {
3672 	(*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct);
3673 	VOPSTATS_UPDATE(vp, rwunlock);
3674 }
3675 
3676 int
3677 fop_seek(
3678 	vnode_t *vp,
3679 	offset_t ooff,
3680 	offset_t *noffp,
3681 	caller_context_t *ct)
3682 {
3683 	int	err;
3684 
3685 	err = (*(vp)->v_op->vop_seek)(vp, ooff, noffp, ct);
3686 	VOPSTATS_UPDATE(vp, seek);
3687 	return (err);
3688 }
3689 
3690 int
3691 fop_cmp(
3692 	vnode_t *vp1,
3693 	vnode_t *vp2,
3694 	caller_context_t *ct)
3695 {
3696 	int	err;
3697 
3698 	err = (*(vp1)->v_op->vop_cmp)(vp1, vp2, ct);
3699 	VOPSTATS_UPDATE(vp1, cmp);
3700 	return (err);
3701 }
3702 
3703 int
3704 fop_frlock(
3705 	vnode_t *vp,
3706 	int cmd,
3707 	flock64_t *bfp,
3708 	int flag,
3709 	offset_t offset,
3710 	struct flk_callback *flk_cbp,
3711 	cred_t *cr,
3712 	caller_context_t *ct)
3713 {
3714 	int	err;
3715 
3716 	VOPXID_MAP_CR(vp, cr);
3717 
3718 	err = (*(vp)->v_op->vop_frlock)
3719 	    (vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
3720 	VOPSTATS_UPDATE(vp, frlock);
3721 	return (err);
3722 }
3723 
3724 int
3725 fop_space(
3726 	vnode_t *vp,
3727 	int cmd,
3728 	flock64_t *bfp,
3729 	int flag,
3730 	offset_t offset,
3731 	cred_t *cr,
3732 	caller_context_t *ct)
3733 {
3734 	int	err;
3735 
3736 	VOPXID_MAP_CR(vp, cr);
3737 
3738 	err = (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct);
3739 	VOPSTATS_UPDATE(vp, space);
3740 	return (err);
3741 }
3742 
3743 int
3744 fop_realvp(
3745 	vnode_t *vp,
3746 	vnode_t **vpp,
3747 	caller_context_t *ct)
3748 {
3749 	int	err;
3750 
3751 	err = (*(vp)->v_op->vop_realvp)(vp, vpp, ct);
3752 	VOPSTATS_UPDATE(vp, realvp);
3753 	return (err);
3754 }
3755 
3756 int
3757 fop_getpage(
3758 	vnode_t *vp,
3759 	offset_t off,
3760 	size_t len,
3761 	uint_t *protp,
3762 	page_t **plarr,
3763 	size_t plsz,
3764 	struct seg *seg,
3765 	caddr_t addr,
3766 	enum seg_rw rw,
3767 	cred_t *cr,
3768 	caller_context_t *ct)
3769 {
3770 	int	err;
3771 
3772 	VOPXID_MAP_CR(vp, cr);
3773 
3774 	err = (*(vp)->v_op->vop_getpage)
3775 	    (vp, off, len, protp, plarr, plsz, seg, addr, rw, cr, ct);
3776 	VOPSTATS_UPDATE(vp, getpage);
3777 	return (err);
3778 }
3779 
3780 int
3781 fop_putpage(
3782 	vnode_t *vp,
3783 	offset_t off,
3784 	size_t len,
3785 	int flags,
3786 	cred_t *cr,
3787 	caller_context_t *ct)
3788 {
3789 	int	err;
3790 
3791 	VOPXID_MAP_CR(vp, cr);
3792 
3793 	err = (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr, ct);
3794 	VOPSTATS_UPDATE(vp, putpage);
3795 	return (err);
3796 }
3797 
3798 int
3799 fop_map(
3800 	vnode_t *vp,
3801 	offset_t off,
3802 	struct as *as,
3803 	caddr_t *addrp,
3804 	size_t len,
3805 	uchar_t prot,
3806 	uchar_t maxprot,
3807 	uint_t flags,
3808 	cred_t *cr,
3809 	caller_context_t *ct)
3810 {
3811 	int	err;
3812 
3813 	VOPXID_MAP_CR(vp, cr);
3814 
3815 	err = (*(vp)->v_op->vop_map)
3816 	    (vp, off, as, addrp, len, prot, maxprot, flags, cr, ct);
3817 	VOPSTATS_UPDATE(vp, map);
3818 	return (err);
3819 }
3820 
3821 int
3822 fop_addmap(
3823 	vnode_t *vp,
3824 	offset_t off,
3825 	struct as *as,
3826 	caddr_t addr,
3827 	size_t len,
3828 	uchar_t prot,
3829 	uchar_t maxprot,
3830 	uint_t flags,
3831 	cred_t *cr,
3832 	caller_context_t *ct)
3833 {
3834 	int error;
3835 	u_longlong_t delta;
3836 
3837 	VOPXID_MAP_CR(vp, cr);
3838 
3839 	error = (*(vp)->v_op->vop_addmap)
3840 	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
3841 
3842 	if ((!error) && (vp->v_type == VREG)) {
3843 		delta = (u_longlong_t)btopr(len);
3844 		/*
3845 		 * If file is declared MAP_PRIVATE, it can't be written back
3846 		 * even if open for write. Handle as read.
3847 		 */
3848 		if (flags & MAP_PRIVATE) {
3849 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3850 			    (int64_t)delta);
3851 		} else {
3852 			/*
3853 			 * atomic_add_64 forces the fetch of a 64 bit value to
3854 			 * be atomic on 32 bit machines
3855 			 */
3856 			if (maxprot & PROT_WRITE)
3857 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
3858 				    (int64_t)delta);
3859 			if (maxprot & PROT_READ)
3860 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3861 				    (int64_t)delta);
3862 			if (maxprot & PROT_EXEC)
3863 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3864 				    (int64_t)delta);
3865 		}
3866 	}
3867 	VOPSTATS_UPDATE(vp, addmap);
3868 	return (error);
3869 }
3870 
3871 int
3872 fop_delmap(
3873 	vnode_t *vp,
3874 	offset_t off,
3875 	struct as *as,
3876 	caddr_t addr,
3877 	size_t len,
3878 	uint_t prot,
3879 	uint_t maxprot,
3880 	uint_t flags,
3881 	cred_t *cr,
3882 	caller_context_t *ct)
3883 {
3884 	int error;
3885 	u_longlong_t delta;
3886 
3887 	VOPXID_MAP_CR(vp, cr);
3888 
3889 	error = (*(vp)->v_op->vop_delmap)
3890 	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
3891 
3892 	/*
3893 	 * NFS calls into delmap twice, the first time
3894 	 * it simply establishes a callback mechanism and returns EAGAIN
3895 	 * while the real work is being done upon the second invocation.
3896 	 * We have to detect this here and only decrement the counts upon
3897 	 * the second delmap request.
3898 	 */
3899 	if ((error != EAGAIN) && (vp->v_type == VREG)) {
3900 
3901 		delta = (u_longlong_t)btopr(len);
3902 
3903 		if (flags & MAP_PRIVATE) {
3904 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3905 			    (int64_t)(-delta));
3906 		} else {
3907 			/*
3908 			 * atomic_add_64 forces the fetch of a 64 bit value
3909 			 * to be atomic on 32 bit machines
3910 			 */
3911 			if (maxprot & PROT_WRITE)
3912 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
3913 				    (int64_t)(-delta));
3914 			if (maxprot & PROT_READ)
3915 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3916 				    (int64_t)(-delta));
3917 			if (maxprot & PROT_EXEC)
3918 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3919 				    (int64_t)(-delta));
3920 		}
3921 	}
3922 	VOPSTATS_UPDATE(vp, delmap);
3923 	return (error);
3924 }
3925 
3926 
3927 int
3928 fop_poll(
3929 	vnode_t *vp,
3930 	short events,
3931 	int anyyet,
3932 	short *reventsp,
3933 	struct pollhead **phpp,
3934 	caller_context_t *ct)
3935 {
3936 	int	err;
3937 
3938 	err = (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp, ct);
3939 	VOPSTATS_UPDATE(vp, poll);
3940 	return (err);
3941 }
3942 
3943 int
3944 fop_dump(
3945 	vnode_t *vp,
3946 	caddr_t addr,
3947 	offset_t lbdn,
3948 	offset_t dblks,
3949 	caller_context_t *ct)
3950 {
3951 	int	err;
3952 
3953 	/* ensure lbdn and dblks can be passed safely to bdev_dump */
3954 	if ((lbdn != (daddr_t)lbdn) || (dblks != (int)dblks))
3955 		return (EIO);
3956 
3957 	err = (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks, ct);
3958 	VOPSTATS_UPDATE(vp, dump);
3959 	return (err);
3960 }
3961 
3962 int
3963 fop_pathconf(
3964 	vnode_t *vp,
3965 	int cmd,
3966 	ulong_t *valp,
3967 	cred_t *cr,
3968 	caller_context_t *ct)
3969 {
3970 	int	err;
3971 
3972 	VOPXID_MAP_CR(vp, cr);
3973 
3974 	err = (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr, ct);
3975 	VOPSTATS_UPDATE(vp, pathconf);
3976 	return (err);
3977 }
3978 
3979 int
3980 fop_pageio(
3981 	vnode_t *vp,
3982 	struct page *pp,
3983 	u_offset_t io_off,
3984 	size_t io_len,
3985 	int flags,
3986 	cred_t *cr,
3987 	caller_context_t *ct)
3988 {
3989 	int	err;
3990 
3991 	VOPXID_MAP_CR(vp, cr);
3992 
3993 	err = (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr, ct);
3994 	VOPSTATS_UPDATE(vp, pageio);
3995 	return (err);
3996 }
3997 
3998 int
3999 fop_dumpctl(
4000 	vnode_t *vp,
4001 	int action,
4002 	offset_t *blkp,
4003 	caller_context_t *ct)
4004 {
4005 	int	err;
4006 	err = (*(vp)->v_op->vop_dumpctl)(vp, action, blkp, ct);
4007 	VOPSTATS_UPDATE(vp, dumpctl);
4008 	return (err);
4009 }
4010 
4011 void
4012 fop_dispose(
4013 	vnode_t *vp,
4014 	page_t *pp,
4015 	int flag,
4016 	int dn,
4017 	cred_t *cr,
4018 	caller_context_t *ct)
4019 {
4020 	/* Must do stats first since it's possible to lose the vnode */
4021 	VOPSTATS_UPDATE(vp, dispose);
4022 
4023 	VOPXID_MAP_CR(vp, cr);
4024 
4025 	(*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr, ct);
4026 }
4027 
4028 int
4029 fop_setsecattr(
4030 	vnode_t *vp,
4031 	vsecattr_t *vsap,
4032 	int flag,
4033 	cred_t *cr,
4034 	caller_context_t *ct)
4035 {
4036 	int	err;
4037 
4038 	VOPXID_MAP_CR(vp, cr);
4039 
4040 	/*
4041 	 * We're only allowed to skip the ACL check iff we used a 32 bit
4042 	 * ACE mask with VOP_ACCESS() to determine permissions.
4043 	 */
4044 	if ((flag & ATTR_NOACLCHECK) &&
4045 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4046 		return (EINVAL);
4047 	}
4048 	err = (*(vp)->v_op->vop_setsecattr) (vp, vsap, flag, cr, ct);
4049 	VOPSTATS_UPDATE(vp, setsecattr);
4050 	return (err);
4051 }
4052 
4053 int
4054 fop_getsecattr(
4055 	vnode_t *vp,
4056 	vsecattr_t *vsap,
4057 	int flag,
4058 	cred_t *cr,
4059 	caller_context_t *ct)
4060 {
4061 	int	err;
4062 
4063 	/*
4064 	 * We're only allowed to skip the ACL check iff we used a 32 bit
4065 	 * ACE mask with VOP_ACCESS() to determine permissions.
4066 	 */
4067 	if ((flag & ATTR_NOACLCHECK) &&
4068 	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4069 		return (EINVAL);
4070 	}
4071 
4072 	VOPXID_MAP_CR(vp, cr);
4073 
4074 	err = (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr, ct);
4075 	VOPSTATS_UPDATE(vp, getsecattr);
4076 	return (err);
4077 }
4078 
4079 int
4080 fop_shrlock(
4081 	vnode_t *vp,
4082 	int cmd,
4083 	struct shrlock *shr,
4084 	int flag,
4085 	cred_t *cr,
4086 	caller_context_t *ct)
4087 {
4088 	int	err;
4089 
4090 	VOPXID_MAP_CR(vp, cr);
4091 
4092 	err = (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr, ct);
4093 	VOPSTATS_UPDATE(vp, shrlock);
4094 	return (err);
4095 }
4096 
4097 int
4098 fop_vnevent(vnode_t *vp, vnevent_t vnevent, vnode_t *dvp, char *fnm,
4099     caller_context_t *ct)
4100 {
4101 	int	err;
4102 
4103 	err = (*(vp)->v_op->vop_vnevent)(vp, vnevent, dvp, fnm, ct);
4104 	VOPSTATS_UPDATE(vp, vnevent);
4105 	return (err);
4106 }
4107 
4108 /*
4109  * Default destructor
4110  *	Needed because NULL destructor means that the key is unused
4111  */
4112 /* ARGSUSED */
4113 void
4114 vsd_defaultdestructor(void *value)
4115 {}
4116 
4117 /*
4118  * Create a key (index into per vnode array)
4119  *	Locks out vsd_create, vsd_destroy, and vsd_free
4120  *	May allocate memory with lock held
4121  */
4122 void
4123 vsd_create(uint_t *keyp, void (*destructor)(void *))
4124 {
4125 	int	i;
4126 	uint_t	nkeys;
4127 
4128 	/*
4129 	 * if key is allocated, do nothing
4130 	 */
4131 	mutex_enter(&vsd_lock);
4132 	if (*keyp) {
4133 		mutex_exit(&vsd_lock);
4134 		return;
4135 	}
4136 	/*
4137 	 * find an unused key
4138 	 */
4139 	if (destructor == NULL)
4140 		destructor = vsd_defaultdestructor;
4141 
4142 	for (i = 0; i < vsd_nkeys; ++i)
4143 		if (vsd_destructor[i] == NULL)
4144 			break;
4145 
4146 	/*
4147 	 * if no unused keys, increase the size of the destructor array
4148 	 */
4149 	if (i == vsd_nkeys) {
4150 		if ((nkeys = (vsd_nkeys << 1)) == 0)
4151 			nkeys = 1;
4152 		vsd_destructor =
4153 		    (void (**)(void *))vsd_realloc((void *)vsd_destructor,
4154 		    (size_t)(vsd_nkeys * sizeof (void (*)(void *))),
4155 		    (size_t)(nkeys * sizeof (void (*)(void *))));
4156 		vsd_nkeys = nkeys;
4157 	}
4158 
4159 	/*
4160 	 * allocate the next available unused key
4161 	 */
4162 	vsd_destructor[i] = destructor;
4163 	*keyp = i + 1;
4164 
4165 	/* create vsd_list, if it doesn't exist */
4166 	if (vsd_list == NULL) {
4167 		vsd_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
4168 		list_create(vsd_list, sizeof (struct vsd_node),
4169 		    offsetof(struct vsd_node, vs_nodes));
4170 	}
4171 
4172 	mutex_exit(&vsd_lock);
4173 }
4174 
4175 /*
4176  * Destroy a key
4177  *
4178  * Assumes that the caller is preventing vsd_set and vsd_get
4179  * Locks out vsd_create, vsd_destroy, and vsd_free
4180  * May free memory with lock held
4181  */
4182 void
4183 vsd_destroy(uint_t *keyp)
4184 {
4185 	uint_t key;
4186 	struct vsd_node *vsd;
4187 
4188 	/*
4189 	 * protect the key namespace and our destructor lists
4190 	 */
4191 	mutex_enter(&vsd_lock);
4192 	key = *keyp;
4193 	*keyp = 0;
4194 
4195 	ASSERT(key <= vsd_nkeys);
4196 
4197 	/*
4198 	 * if the key is valid
4199 	 */
4200 	if (key != 0) {
4201 		uint_t k = key - 1;
4202 		/*
4203 		 * for every vnode with VSD, call key's destructor
4204 		 */
4205 		for (vsd = list_head(vsd_list); vsd != NULL;
4206 		    vsd = list_next(vsd_list, vsd)) {
4207 			/*
4208 			 * no VSD for key in this vnode
4209 			 */
4210 			if (key > vsd->vs_nkeys)
4211 				continue;
4212 			/*
4213 			 * call destructor for key
4214 			 */
4215 			if (vsd->vs_value[k] && vsd_destructor[k])
4216 				(*vsd_destructor[k])(vsd->vs_value[k]);
4217 			/*
4218 			 * reset value for key
4219 			 */
4220 			vsd->vs_value[k] = NULL;
4221 		}
4222 		/*
4223 		 * actually free the key (NULL destructor == unused)
4224 		 */
4225 		vsd_destructor[k] = NULL;
4226 	}
4227 
4228 	mutex_exit(&vsd_lock);
4229 }
4230 
4231 /*
4232  * Quickly return the per vnode value that was stored with the specified key
4233  * Assumes the caller is protecting key from vsd_create and vsd_destroy
4234  * Assumes the caller is holding v_lock to protect the vsd.
4235  */
4236 void *
4237 vsd_get(vnode_t *vp, uint_t key)
4238 {
4239 	struct vsd_node *vsd;
4240 
4241 	/*
4242 	 * The caller needs to pass a valid vnode.
4243 	 */
4244 	ASSERT(vp != NULL);
4245 	if (vp == NULL)
4246 		return (NULL);
4247 
4248 	vsd = vp->v_vsd;
4249 
4250 	if (key && vsd != NULL && key <= vsd->vs_nkeys)
4251 		return (vsd->vs_value[key - 1]);
4252 	return (NULL);
4253 }
4254 
4255 /*
4256  * Set a per vnode value indexed with the specified key
4257  * Assumes the caller is holding v_lock to protect the vsd.
4258  */
4259 int
4260 vsd_set(vnode_t *vp, uint_t key, void *value)
4261 {
4262 	struct vsd_node *vsd = vp->v_vsd;
4263 
4264 	if (key == 0)
4265 		return (EINVAL);
4266 	if (vsd == NULL)
4267 		vsd = vp->v_vsd = kmem_zalloc(sizeof (*vsd), KM_SLEEP);
4268 
4269 	/*
4270 	 * If the vsd was just allocated, vs_nkeys will be 0, so the following
4271 	 * code won't happen and we will continue down and allocate space for
4272 	 * the vs_value array.
4273 	 * If the caller is replacing one value with another, then it is up
4274 	 * to the caller to free/rele/destroy the previous value (if needed).
4275 	 */
4276 	if (key <= vsd->vs_nkeys) {
4277 		vsd->vs_value[key - 1] = value;
4278 		return (0);
4279 	}
4280 
4281 	ASSERT(key <= vsd_nkeys);
4282 
4283 	if (vsd->vs_nkeys == 0) {
4284 		mutex_enter(&vsd_lock);	/* lock out vsd_destroy() */
4285 		/*
4286 		 * Link onto list of all VSD nodes.
4287 		 */
4288 		list_insert_head(vsd_list, vsd);
4289 		mutex_exit(&vsd_lock);
4290 	}
4291 
4292 	/*
4293 	 * Allocate vnode local storage and set the value for key
4294 	 */
4295 	vsd->vs_value = vsd_realloc(vsd->vs_value,
4296 	    vsd->vs_nkeys * sizeof (void *),
4297 	    key * sizeof (void *));
4298 	vsd->vs_nkeys = key;
4299 	vsd->vs_value[key - 1] = value;
4300 
4301 	return (0);
4302 }
4303 
4304 /*
4305  * Called from vn_free() to run the destructor function for each vsd
4306  *	Locks out vsd_create and vsd_destroy
4307  *	Assumes that the destructor *DOES NOT* use vsd
4308  */
4309 void
4310 vsd_free(vnode_t *vp)
4311 {
4312 	int i;
4313 	struct vsd_node *vsd = vp->v_vsd;
4314 
4315 	if (vsd == NULL)
4316 		return;
4317 
4318 	if (vsd->vs_nkeys == 0) {
4319 		kmem_free(vsd, sizeof (*vsd));
4320 		vp->v_vsd = NULL;
4321 		return;
4322 	}
4323 
4324 	/*
4325 	 * lock out vsd_create and vsd_destroy, call
4326 	 * the destructor, and mark the value as destroyed.
4327 	 */
4328 	mutex_enter(&vsd_lock);
4329 
4330 	for (i = 0; i < vsd->vs_nkeys; i++) {
4331 		if (vsd->vs_value[i] && vsd_destructor[i])
4332 			(*vsd_destructor[i])(vsd->vs_value[i]);
4333 		vsd->vs_value[i] = NULL;
4334 	}
4335 
4336 	/*
4337 	 * remove from linked list of VSD nodes
4338 	 */
4339 	list_remove(vsd_list, vsd);
4340 
4341 	mutex_exit(&vsd_lock);
4342 
4343 	/*
4344 	 * free up the VSD
4345 	 */
4346 	kmem_free(vsd->vs_value, vsd->vs_nkeys * sizeof (void *));
4347 	kmem_free(vsd, sizeof (struct vsd_node));
4348 	vp->v_vsd = NULL;
4349 }
4350 
4351 /*
4352  * realloc
4353  */
4354 static void *
4355 vsd_realloc(void *old, size_t osize, size_t nsize)
4356 {
4357 	void *new;
4358 
4359 	new = kmem_zalloc(nsize, KM_SLEEP);
4360 	if (old) {
4361 		bcopy(old, new, osize);
4362 		kmem_free(old, osize);
4363 	}
4364 	return (new);
4365 }
4366