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