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