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