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