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