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