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