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