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