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