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