xref: /titanic_52/usr/src/uts/common/fs/vnode.c (revision f498645a3eecf2ddd304b4ea9c7f1b4c155ff79e)
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 	int estale_retry = 0;
812 
813 	mode = 0;
814 	if (filemode & FREAD)
815 		mode |= VREAD;
816 	if (filemode & (FWRITE|FTRUNC))
817 		mode |= VWRITE;
818 
819 	/* symlink interpretation */
820 	if (filemode & FNOFOLLOW)
821 		follow = NO_FOLLOW;
822 	else
823 		follow = FOLLOW;
824 
825 top:
826 	if (filemode & FCREAT) {
827 		enum vcexcl excl;
828 
829 		/*
830 		 * Wish to create a file.
831 		 */
832 		vattr.va_type = VREG;
833 		vattr.va_mode = createmode;
834 		vattr.va_mask = AT_TYPE|AT_MODE;
835 		if (filemode & FTRUNC) {
836 			vattr.va_size = 0;
837 			vattr.va_mask |= AT_SIZE;
838 		}
839 		if (filemode & FEXCL)
840 			excl = EXCL;
841 		else
842 			excl = NONEXCL;
843 
844 		if (error =
845 		    vn_createat(pnamep, seg, &vattr, excl, mode, &vp, crwhy,
846 					(filemode & ~(FTRUNC|FEXCL)),
847 						umask, startvp))
848 			return (error);
849 	} else {
850 		/*
851 		 * Wish to open a file.  Just look it up.
852 		 */
853 		if (error = lookupnameat(pnamep, seg, follow,
854 		    NULLVPP, &vp, startvp)) {
855 			if ((error == ESTALE) &&
856 			    fs_need_estale_retry(estale_retry++))
857 				goto top;
858 			return (error);
859 		}
860 
861 		/*
862 		 * Get the attributes to check whether file is large.
863 		 * We do this only if the FOFFMAX flag is not set and
864 		 * only for regular files.
865 		 */
866 
867 		if (!(filemode & FOFFMAX) && (vp->v_type == VREG)) {
868 			vattr.va_mask = AT_SIZE;
869 			if ((error = VOP_GETATTR(vp, &vattr, 0, CRED()))) {
870 				goto out;
871 			}
872 			if (vattr.va_size > (u_offset_t)MAXOFF32_T) {
873 				/*
874 				 * Large File API - regular open fails
875 				 * if FOFFMAX flag is set in file mode
876 				 */
877 				error = EOVERFLOW;
878 				goto out;
879 			}
880 		}
881 		/*
882 		 * Can't write directories, active texts, or
883 		 * read-only filesystems.  Can't truncate files
884 		 * on which mandatory locking is in effect.
885 		 */
886 		if (filemode & (FWRITE|FTRUNC)) {
887 			/*
888 			 * Allow writable directory if VDIROPEN flag is set.
889 			 */
890 			if (vp->v_type == VDIR && !(vp->v_flag & VDIROPEN)) {
891 				error = EISDIR;
892 				goto out;
893 			}
894 			if (ISROFILE(vp)) {
895 				error = EROFS;
896 				goto out;
897 			}
898 			/*
899 			 * Can't truncate files on which mandatory locking
900 			 * or non-blocking mandatory locking is in effect.
901 			 */
902 			if (filemode & FTRUNC) {
903 				vnode_t *rvp;
904 
905 				if (VOP_REALVP(vp, &rvp) != 0)
906 					rvp = vp;
907 				if (nbl_need_check(vp)) {
908 					nbl_start_crit(vp, RW_READER);
909 					in_crit = 1;
910 					vattr.va_mask = AT_MODE|AT_SIZE;
911 					if ((error = VOP_GETATTR(vp, &vattr, 0,
912 					    CRED())) == 0) {
913 						if (rvp->v_filocks != NULL)
914 							if (MANDLOCK(vp,
915 							    vattr.va_mode))
916 								error = EAGAIN;
917 						if (!error) {
918 							if (nbl_conflict(vp,
919 							    NBL_WRITE, 0,
920 							    vattr.va_size, 0))
921 								error = EACCES;
922 						}
923 					}
924 				} else if (rvp->v_filocks != NULL) {
925 					vattr.va_mask = AT_MODE;
926 					if ((error = VOP_GETATTR(vp, &vattr,
927 					    0, CRED())) == 0 && MANDLOCK(vp,
928 					    vattr.va_mode))
929 						error = EAGAIN;
930 				}
931 			}
932 			if (error)
933 				goto out;
934 		}
935 		/*
936 		 * Check permissions.
937 		 */
938 		if (error = VOP_ACCESS(vp, mode, 0, CRED()))
939 			goto out;
940 	}
941 
942 	/*
943 	 * Do remaining checks for FNOFOLLOW and FNOLINKS.
944 	 */
945 	if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) {
946 		error = EINVAL;
947 		goto out;
948 	}
949 	if (filemode & FNOLINKS) {
950 		vattr.va_mask = AT_NLINK;
951 		if ((error = VOP_GETATTR(vp, &vattr, 0, CRED()))) {
952 			goto out;
953 		}
954 		if (vattr.va_nlink != 1) {
955 			error = EMLINK;
956 			goto out;
957 		}
958 	}
959 
960 	/*
961 	 * Opening a socket corresponding to the AF_UNIX pathname
962 	 * in the filesystem name space is not supported.
963 	 * However, VSOCK nodes in namefs are supported in order
964 	 * to make fattach work for sockets.
965 	 *
966 	 * XXX This uses VOP_REALVP to distinguish between
967 	 * an unopened namefs node (where VOP_REALVP returns a
968 	 * different VSOCK vnode) and a VSOCK created by vn_create
969 	 * in some file system (where VOP_REALVP would never return
970 	 * a different vnode).
971 	 */
972 	if (vp->v_type == VSOCK) {
973 		struct vnode *nvp;
974 
975 		error = VOP_REALVP(vp, &nvp);
976 		if (error != 0 || nvp == NULL || nvp == vp ||
977 		    nvp->v_type != VSOCK) {
978 			error = EOPNOTSUPP;
979 			goto out;
980 		}
981 	}
982 	/*
983 	 * Do opening protocol.
984 	 */
985 	error = VOP_OPEN(&vp, filemode, CRED());
986 	/*
987 	 * Truncate if required.
988 	 */
989 	if (error == 0 && (filemode & FTRUNC) && !(filemode & FCREAT)) {
990 		vattr.va_size = 0;
991 		vattr.va_mask = AT_SIZE;
992 		if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0)
993 			(void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED());
994 	}
995 out:
996 	ASSERT(vp->v_count > 0);
997 
998 	if (in_crit) {
999 		nbl_end_crit(vp);
1000 		in_crit = 0;
1001 	}
1002 	if (error) {
1003 		/*
1004 		 * The following clause was added to handle a problem
1005 		 * with NFS consistency.  It is possible that a lookup
1006 		 * of the file to be opened succeeded, but the file
1007 		 * itself doesn't actually exist on the server.  This
1008 		 * is chiefly due to the DNLC containing an entry for
1009 		 * the file which has been removed on the server.  In
1010 		 * this case, we just start over.  If there was some
1011 		 * other cause for the ESTALE error, then the lookup
1012 		 * of the file will fail and the error will be returned
1013 		 * above instead of looping around from here.
1014 		 */
1015 		VN_RELE(vp);
1016 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1017 			goto top;
1018 	} else
1019 		*vpp = vp;
1020 	return (error);
1021 }
1022 
1023 int
1024 vn_create(
1025 	char *pnamep,
1026 	enum uio_seg seg,
1027 	struct vattr *vap,
1028 	enum vcexcl excl,
1029 	int mode,
1030 	struct vnode **vpp,
1031 	enum create why,
1032 	int flag,
1033 	mode_t umask)
1034 {
1035 	return (vn_createat(pnamep, seg, vap, excl, mode, vpp,
1036 			why, flag, umask, NULL));
1037 }
1038 
1039 /*
1040  * Create a vnode (makenode).
1041  */
1042 int
1043 vn_createat(
1044 	char *pnamep,
1045 	enum uio_seg seg,
1046 	struct vattr *vap,
1047 	enum vcexcl excl,
1048 	int mode,
1049 	struct vnode **vpp,
1050 	enum create why,
1051 	int flag,
1052 	mode_t umask,
1053 	struct vnode *startvp)
1054 {
1055 	struct vnode *dvp;	/* ptr to parent dir vnode */
1056 	struct vnode *vp = NULL;
1057 	struct pathname pn;
1058 	int error;
1059 	int in_crit = 0;
1060 	struct vattr vattr;
1061 	enum symfollow follow;
1062 	int estale_retry = 0;
1063 
1064 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
1065 
1066 	/* symlink interpretation */
1067 	if ((flag & FNOFOLLOW) || excl == EXCL)
1068 		follow = NO_FOLLOW;
1069 	else
1070 		follow = FOLLOW;
1071 	flag &= ~(FNOFOLLOW|FNOLINKS);
1072 
1073 top:
1074 	/*
1075 	 * Lookup directory.
1076 	 * If new object is a file, call lower level to create it.
1077 	 * Note that it is up to the lower level to enforce exclusive
1078 	 * creation, if the file is already there.
1079 	 * This allows the lower level to do whatever
1080 	 * locking or protocol that is needed to prevent races.
1081 	 * If the new object is directory call lower level to make
1082 	 * the new directory, with "." and "..".
1083 	 */
1084 	if (error = pn_get(pnamep, seg, &pn))
1085 		return (error);
1086 #ifdef  C2_AUDIT
1087 	if (audit_active)
1088 		audit_vncreate_start();
1089 #endif /* C2_AUDIT */
1090 	dvp = NULL;
1091 	*vpp = NULL;
1092 	/*
1093 	 * lookup will find the parent directory for the vnode.
1094 	 * When it is done the pn holds the name of the entry
1095 	 * in the directory.
1096 	 * If this is a non-exclusive create we also find the node itself.
1097 	 */
1098 	error = lookuppnat(&pn, NULL, follow, &dvp,
1099 	    (excl == EXCL) ? NULLVPP : vpp, startvp);
1100 	if (error) {
1101 		pn_free(&pn);
1102 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1103 			goto top;
1104 		if (why == CRMKDIR && error == EINVAL)
1105 			error = EEXIST;		/* SVID */
1106 		return (error);
1107 	}
1108 
1109 	if (why != CRMKNOD)
1110 		vap->va_mode &= ~VSVTX;
1111 
1112 	/*
1113 	 * If default ACLs are defined for the directory don't apply the
1114 	 * umask if umask is passed.
1115 	 */
1116 
1117 	if (umask) {
1118 
1119 		vsecattr_t vsec;
1120 
1121 		vsec.vsa_aclcnt = 0;
1122 		vsec.vsa_aclentp = NULL;
1123 		vsec.vsa_dfaclcnt = 0;
1124 		vsec.vsa_dfaclentp = NULL;
1125 		vsec.vsa_mask = VSA_DFACLCNT;
1126 		error =  VOP_GETSECATTR(dvp, &vsec, 0, CRED());
1127 		/*
1128 		 * If error is ENOSYS then treat it as no error
1129 		 * Don't want to force all file systems to support
1130 		 * aclent_t style of ACL's.
1131 		 */
1132 		if (error == ENOSYS)
1133 			error = 0;
1134 		if (error) {
1135 			if (*vpp != NULL)
1136 				VN_RELE(*vpp);
1137 			goto out;
1138 		} else {
1139 			/*
1140 			 * Apply the umask if no default ACLs.
1141 			 */
1142 			if (vsec.vsa_dfaclcnt == 0)
1143 				vap->va_mode &= ~umask;
1144 
1145 			/*
1146 			 * VOP_GETSECATTR() may have allocated memory for
1147 			 * ACLs we didn't request, so double-check and
1148 			 * free it if necessary.
1149 			 */
1150 			if (vsec.vsa_aclcnt && vsec.vsa_aclentp != NULL)
1151 				kmem_free((caddr_t)vsec.vsa_aclentp,
1152 				    vsec.vsa_aclcnt * sizeof (aclent_t));
1153 			if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp != NULL)
1154 				kmem_free((caddr_t)vsec.vsa_dfaclentp,
1155 				    vsec.vsa_dfaclcnt * sizeof (aclent_t));
1156 		}
1157 	}
1158 
1159 	/*
1160 	 * In general we want to generate EROFS if the file system is
1161 	 * readonly.  However, POSIX (IEEE Std. 1003.1) section 5.3.1
1162 	 * documents the open system call, and it says that O_CREAT has no
1163 	 * effect if the file already exists.  Bug 1119649 states
1164 	 * that open(path, O_CREAT, ...) fails when attempting to open an
1165 	 * existing file on a read only file system.  Thus, the first part
1166 	 * of the following if statement has 3 checks:
1167 	 *	if the file exists &&
1168 	 *		it is being open with write access &&
1169 	 *		the file system is read only
1170 	 *	then generate EROFS
1171 	 */
1172 	if ((*vpp != NULL && (mode & VWRITE) && ISROFILE(*vpp)) ||
1173 	    (*vpp == NULL && dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
1174 		if (*vpp)
1175 			VN_RELE(*vpp);
1176 		error = EROFS;
1177 	} else if (excl == NONEXCL && *vpp != NULL) {
1178 		vnode_t *rvp;
1179 
1180 		/*
1181 		 * File already exists.  If a mandatory lock has been
1182 		 * applied, return error.
1183 		 */
1184 		vp = *vpp;
1185 		if (VOP_REALVP(vp, &rvp) != 0)
1186 			rvp = vp;
1187 		if ((vap->va_mask & AT_SIZE) && nbl_need_check(vp)) {
1188 			nbl_start_crit(vp, RW_READER);
1189 			in_crit = 1;
1190 		}
1191 		if (rvp->v_filocks != NULL || rvp->v_shrlocks != NULL) {
1192 			vattr.va_mask = AT_MODE|AT_SIZE;
1193 			if (error = VOP_GETATTR(vp, &vattr, 0, CRED())) {
1194 				goto out;
1195 			}
1196 			if (MANDLOCK(vp, vattr.va_mode)) {
1197 				error = EAGAIN;
1198 				goto out;
1199 			}
1200 			/*
1201 			 * File cannot be truncated if non-blocking mandatory
1202 			 * locks are currently on the file.
1203 			 */
1204 			if ((vap->va_mask & AT_SIZE) && in_crit) {
1205 				u_offset_t offset;
1206 				ssize_t length;
1207 
1208 				offset = vap->va_size > vattr.va_size ?
1209 						vattr.va_size : vap->va_size;
1210 				length = vap->va_size > vattr.va_size ?
1211 						vap->va_size - vattr.va_size :
1212 						vattr.va_size - vap->va_size;
1213 				if (nbl_conflict(vp, NBL_WRITE, offset,
1214 						length, 0)) {
1215 					error = EACCES;
1216 					goto out;
1217 				}
1218 			}
1219 		}
1220 
1221 		/*
1222 		 * If the file is the root of a VFS, we've crossed a
1223 		 * mount point and the "containing" directory that we
1224 		 * acquired above (dvp) is irrelevant because it's in
1225 		 * a different file system.  We apply VOP_CREATE to the
1226 		 * target itself instead of to the containing directory
1227 		 * and supply a null path name to indicate (conventionally)
1228 		 * the node itself as the "component" of interest.
1229 		 *
1230 		 * The intercession of the file system is necessary to
1231 		 * ensure that the appropriate permission checks are
1232 		 * done.
1233 		 */
1234 		if (vp->v_flag & VROOT) {
1235 			ASSERT(why != CRMKDIR);
1236 			error =
1237 			    VOP_CREATE(vp, "", vap, excl, mode, vpp, CRED(),
1238 				    flag);
1239 			/*
1240 			 * If the create succeeded, it will have created
1241 			 * a new reference to the vnode.  Give up the
1242 			 * original reference.  The assertion should not
1243 			 * get triggered because NBMAND locks only apply to
1244 			 * VREG files.  And if in_crit is non-zero for some
1245 			 * reason, detect that here, rather than when we
1246 			 * deference a null vp.
1247 			 */
1248 			ASSERT(in_crit == 0);
1249 			VN_RELE(vp);
1250 			vp = NULL;
1251 			goto out;
1252 		}
1253 
1254 		/*
1255 		 * Large File API - non-large open (FOFFMAX flag not set)
1256 		 * of regular file fails if the file size exceeds MAXOFF32_T.
1257 		 */
1258 		if (why != CRMKDIR &&
1259 		    !(flag & FOFFMAX) &&
1260 		    (vp->v_type == VREG)) {
1261 			vattr.va_mask = AT_SIZE;
1262 			if ((error = VOP_GETATTR(vp, &vattr, 0, CRED()))) {
1263 				goto out;
1264 			}
1265 			if ((vattr.va_size > (u_offset_t)MAXOFF32_T)) {
1266 				error = EOVERFLOW;
1267 				goto out;
1268 			}
1269 		}
1270 	}
1271 
1272 	if (error == 0) {
1273 		/*
1274 		 * Call mkdir() if specified, otherwise create().
1275 		 */
1276 		int must_be_dir = pn_fixslash(&pn);	/* trailing '/'? */
1277 
1278 		if (why == CRMKDIR)
1279 			error = VOP_MKDIR(dvp, pn.pn_path, vap, vpp, CRED());
1280 		else if (!must_be_dir)
1281 			error = VOP_CREATE(dvp, pn.pn_path, vap,
1282 			    excl, mode, vpp, CRED(), flag);
1283 		else
1284 			error = ENOTDIR;
1285 	}
1286 
1287 out:
1288 
1289 #ifdef C2_AUDIT
1290 	if (audit_active)
1291 		audit_vncreate_finish(*vpp, error);
1292 #endif  /* C2_AUDIT */
1293 	if (in_crit) {
1294 		nbl_end_crit(vp);
1295 		in_crit = 0;
1296 	}
1297 	if (vp != NULL) {
1298 		VN_RELE(vp);
1299 		vp = NULL;
1300 	}
1301 	pn_free(&pn);
1302 	VN_RELE(dvp);
1303 	/*
1304 	 * The following clause was added to handle a problem
1305 	 * with NFS consistency.  It is possible that a lookup
1306 	 * of the file to be created succeeded, but the file
1307 	 * itself doesn't actually exist on the server.  This
1308 	 * is chiefly due to the DNLC containing an entry for
1309 	 * the file which has been removed on the server.  In
1310 	 * this case, we just start over.  If there was some
1311 	 * other cause for the ESTALE error, then the lookup
1312 	 * of the file will fail and the error will be returned
1313 	 * above instead of looping around from here.
1314 	 */
1315 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1316 		goto top;
1317 	return (error);
1318 }
1319 
1320 int
1321 vn_link(char *from, char *to, enum uio_seg seg)
1322 {
1323 	struct vnode *fvp;		/* from vnode ptr */
1324 	struct vnode *tdvp;		/* to directory vnode ptr */
1325 	struct pathname pn;
1326 	int error;
1327 	struct vattr vattr;
1328 	dev_t fsid;
1329 	int estale_retry = 0;
1330 
1331 top:
1332 	fvp = tdvp = NULL;
1333 	if (error = pn_get(to, seg, &pn))
1334 		return (error);
1335 	if (error = lookupname(from, seg, NO_FOLLOW, NULLVPP, &fvp))
1336 		goto out;
1337 	if (error = lookuppn(&pn, NULL, NO_FOLLOW, &tdvp, NULLVPP))
1338 		goto out;
1339 	/*
1340 	 * Make sure both source vnode and target directory vnode are
1341 	 * in the same vfs and that it is writeable.
1342 	 */
1343 	vattr.va_mask = AT_FSID;
1344 	if (error = VOP_GETATTR(fvp, &vattr, 0, CRED()))
1345 		goto out;
1346 	fsid = vattr.va_fsid;
1347 	vattr.va_mask = AT_FSID;
1348 	if (error = VOP_GETATTR(tdvp, &vattr, 0, CRED()))
1349 		goto out;
1350 	if (fsid != vattr.va_fsid) {
1351 		error = EXDEV;
1352 		goto out;
1353 	}
1354 	if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) {
1355 		error = EROFS;
1356 		goto out;
1357 	}
1358 	/*
1359 	 * Do the link.
1360 	 */
1361 	(void) pn_fixslash(&pn);
1362 	error = VOP_LINK(tdvp, fvp, pn.pn_path, CRED());
1363 out:
1364 	pn_free(&pn);
1365 	if (fvp)
1366 		VN_RELE(fvp);
1367 	if (tdvp)
1368 		VN_RELE(tdvp);
1369 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1370 		goto top;
1371 	return (error);
1372 }
1373 
1374 int
1375 vn_rename(char *from, char *to, enum uio_seg seg)
1376 {
1377 	return (vn_renameat(NULL, from, NULL, to, seg));
1378 }
1379 
1380 int
1381 vn_renameat(vnode_t *fdvp, char *fname, vnode_t *tdvp,
1382 		char *tname, enum uio_seg seg)
1383 {
1384 	int error;
1385 	struct vattr vattr;
1386 	struct pathname fpn;		/* from pathname */
1387 	struct pathname tpn;		/* to pathname */
1388 	dev_t fsid;
1389 	int in_crit = 0;
1390 	vnode_t *fromvp, *fvp;
1391 	vnode_t *tovp;
1392 	int estale_retry = 0;
1393 
1394 top:
1395 	fvp = fromvp = tovp = NULL;
1396 	/*
1397 	 * Get to and from pathnames.
1398 	 */
1399 	if (error = pn_get(fname, seg, &fpn))
1400 		return (error);
1401 	if (error = pn_get(tname, seg, &tpn)) {
1402 		pn_free(&fpn);
1403 		return (error);
1404 	}
1405 
1406 	/*
1407 	 * First we need to resolve the correct directories
1408 	 * The passed in directories may only be a starting point,
1409 	 * but we need the real directories the file(s) live in.
1410 	 * For example the fname may be something like usr/lib/sparc
1411 	 * and we were passed in the / directory, but we need to
1412 	 * use the lib directory for the rename.
1413 	 */
1414 
1415 #ifdef  C2_AUDIT
1416 	if (audit_active)
1417 		audit_setfsat_path(1);
1418 #endif /* C2_AUDIT */
1419 	/*
1420 	 * Lookup to and from directories.
1421 	 */
1422 	if (error = lookuppnat(&fpn, NULL, NO_FOLLOW, &fromvp, &fvp, fdvp)) {
1423 		goto out;
1424 	}
1425 
1426 	/*
1427 	 * Make sure there is an entry.
1428 	 */
1429 	if (fvp == NULL) {
1430 		error = ENOENT;
1431 		goto out;
1432 	}
1433 
1434 #ifdef  C2_AUDIT
1435 	if (audit_active)
1436 		audit_setfsat_path(3);
1437 #endif /* C2_AUDIT */
1438 	if (error = lookuppnat(&tpn, NULL, NO_FOLLOW, &tovp, NULLVPP, tdvp)) {
1439 		goto out;
1440 	}
1441 
1442 	/*
1443 	 * Make sure both the from vnode directory and the to directory
1444 	 * are in the same vfs and the to directory is writable.
1445 	 * We check fsid's, not vfs pointers, so loopback fs works.
1446 	 */
1447 	if (fromvp != tovp) {
1448 		vattr.va_mask = AT_FSID;
1449 		if (error = VOP_GETATTR(fromvp, &vattr, 0, CRED()))
1450 			goto out;
1451 		fsid = vattr.va_fsid;
1452 		vattr.va_mask = AT_FSID;
1453 		if (error = VOP_GETATTR(tovp, &vattr, 0, CRED()))
1454 			goto out;
1455 		if (fsid != vattr.va_fsid) {
1456 			error = EXDEV;
1457 			goto out;
1458 		}
1459 	}
1460 
1461 	if (tovp->v_vfsp->vfs_flag & VFS_RDONLY) {
1462 		error = EROFS;
1463 		goto out;
1464 	}
1465 
1466 	if (nbl_need_check(fvp)) {
1467 		nbl_start_crit(fvp, RW_READER);
1468 		in_crit = 1;
1469 		if (nbl_conflict(fvp, NBL_RENAME, 0, 0, 0)) {
1470 			error = EACCES;
1471 			goto out;
1472 		}
1473 	}
1474 
1475 	/*
1476 	 * Do the rename.
1477 	 */
1478 	(void) pn_fixslash(&tpn);
1479 	error = VOP_RENAME(fromvp, fpn.pn_path, tovp, tpn.pn_path, CRED());
1480 
1481 out:
1482 	pn_free(&fpn);
1483 	pn_free(&tpn);
1484 	if (in_crit) {
1485 		nbl_end_crit(fvp);
1486 		in_crit = 0;
1487 	}
1488 	if (fromvp)
1489 		VN_RELE(fromvp);
1490 	if (tovp)
1491 		VN_RELE(tovp);
1492 	if (fvp)
1493 		VN_RELE(fvp);
1494 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1495 		goto top;
1496 	return (error);
1497 }
1498 
1499 /*
1500  * Remove a file or directory.
1501  */
1502 int
1503 vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
1504 {
1505 	return (vn_removeat(NULL, fnamep, seg, dirflag));
1506 }
1507 
1508 int
1509 vn_removeat(vnode_t *startvp, char *fnamep, enum uio_seg seg, enum rm dirflag)
1510 {
1511 	struct vnode *vp;		/* entry vnode */
1512 	struct vnode *dvp;		/* ptr to parent dir vnode */
1513 	struct vnode *coveredvp;
1514 	struct pathname pn;		/* name of entry */
1515 	enum vtype vtype;
1516 	int error;
1517 	struct vfs *vfsp;
1518 	struct vfs *dvfsp;	/* ptr to parent dir vfs */
1519 	int in_crit = 0;
1520 	int estale_retry = 0;
1521 
1522 top:
1523 	if (error = pn_get(fnamep, seg, &pn))
1524 		return (error);
1525 	dvp = vp = NULL;
1526 	if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &dvp, &vp, startvp)) {
1527 		pn_free(&pn);
1528 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1529 			goto top;
1530 		return (error);
1531 	}
1532 
1533 	/*
1534 	 * Make sure there is an entry.
1535 	 */
1536 	if (vp == NULL) {
1537 		error = ENOENT;
1538 		goto out;
1539 	}
1540 
1541 	vfsp = vp->v_vfsp;
1542 	dvfsp = dvp->v_vfsp;
1543 
1544 	/*
1545 	 * If the named file is the root of a mounted filesystem, fail,
1546 	 * unless it's marked unlinkable.  In that case, unmount the
1547 	 * filesystem and proceed to unlink the covered vnode.  (If the
1548 	 * covered vnode is a directory, use rmdir instead of unlink,
1549 	 * to avoid file system corruption.)
1550 	 */
1551 	if (vp->v_flag & VROOT) {
1552 		if (vfsp->vfs_flag & VFS_UNLINKABLE) {
1553 			if (dirflag == RMDIRECTORY) {
1554 				/*
1555 				 * User called rmdir(2) on a file that has
1556 				 * been namefs mounted on top of.  Since
1557 				 * namefs doesn't allow directories to
1558 				 * be mounted on other files we know
1559 				 * vp is not of type VDIR so fail to operation.
1560 				 */
1561 				error = ENOTDIR;
1562 				goto out;
1563 			}
1564 			coveredvp = vfsp->vfs_vnodecovered;
1565 			VN_HOLD(coveredvp);
1566 			VN_RELE(vp);
1567 			vp = NULL;
1568 			if ((error = vn_vfswlock(coveredvp)) == 0)
1569 				error = dounmount(vfsp, 0, CRED());
1570 			/*
1571 			 * Unmounted the namefs file system; now get
1572 			 * the object it was mounted over.
1573 			 */
1574 			vp = coveredvp;
1575 			/*
1576 			 * If namefs was mounted over a directory, then
1577 			 * we want to use rmdir() instead of unlink().
1578 			 */
1579 			if (vp->v_type == VDIR)
1580 				dirflag = RMDIRECTORY;
1581 		} else
1582 			error = EBUSY;
1583 
1584 		if (error)
1585 			goto out;
1586 	}
1587 
1588 	/*
1589 	 * Make sure filesystem is writeable.
1590 	 * We check the parent directory's vfs in case this is an lofs vnode.
1591 	 */
1592 	if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) {
1593 		error = EROFS;
1594 		goto out;
1595 	}
1596 
1597 	vtype = vp->v_type;
1598 
1599 	/*
1600 	 * If there is the possibility of an nbmand share reservation, make
1601 	 * sure it's okay to remove the file.  Keep a reference to the
1602 	 * vnode, so that we can exit the nbl critical region after
1603 	 * calling VOP_REMOVE.
1604 	 * If there is no possibility of an nbmand share reservation,
1605 	 * release the vnode reference now.  Filesystems like NFS may
1606 	 * behave differently if there is an extra reference, so get rid of
1607 	 * this one.  Fortunately, we can't have nbmand mounts on NFS
1608 	 * filesystems.
1609 	 */
1610 	if (nbl_need_check(vp)) {
1611 		nbl_start_crit(vp, RW_READER);
1612 		in_crit = 1;
1613 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0)) {
1614 			error = EACCES;
1615 			goto out;
1616 		}
1617 	} else {
1618 		VN_RELE(vp);
1619 		vp = NULL;
1620 	}
1621 
1622 	if (dirflag == RMDIRECTORY) {
1623 		/*
1624 		 * Caller is using rmdir(2), which can only be applied to
1625 		 * directories.
1626 		 */
1627 		if (vtype != VDIR) {
1628 			error = ENOTDIR;
1629 		} else {
1630 			vnode_t *cwd;
1631 			proc_t *pp = curproc;
1632 
1633 			mutex_enter(&pp->p_lock);
1634 			cwd = PTOU(pp)->u_cdir;
1635 			VN_HOLD(cwd);
1636 			mutex_exit(&pp->p_lock);
1637 			error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED());
1638 			VN_RELE(cwd);
1639 		}
1640 	} else {
1641 		/*
1642 		 * Unlink(2) can be applied to anything.
1643 		 */
1644 		error = VOP_REMOVE(dvp, pn.pn_path, CRED());
1645 	}
1646 
1647 out:
1648 	pn_free(&pn);
1649 	if (in_crit) {
1650 		nbl_end_crit(vp);
1651 		in_crit = 0;
1652 	}
1653 	if (vp != NULL)
1654 		VN_RELE(vp);
1655 	if (dvp != NULL)
1656 		VN_RELE(dvp);
1657 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1658 		goto top;
1659 	return (error);
1660 }
1661 
1662 /*
1663  * Utility function to compare equality of vnodes.
1664  * Compare the underlying real vnodes, if there are underlying vnodes.
1665  * This is a more thorough comparison than the VN_CMP() macro provides.
1666  */
1667 int
1668 vn_compare(vnode_t *vp1, vnode_t *vp2)
1669 {
1670 	vnode_t *realvp;
1671 
1672 	if (vp1 != NULL && VOP_REALVP(vp1, &realvp) == 0)
1673 		vp1 = realvp;
1674 	if (vp2 != NULL && VOP_REALVP(vp2, &realvp) == 0)
1675 		vp2 = realvp;
1676 	return (VN_CMP(vp1, vp2));
1677 }
1678 
1679 /*
1680  * The number of locks to hash into.  This value must be a power
1681  * of 2 minus 1 and should probably also be prime.
1682  */
1683 #define	NUM_BUCKETS	1023
1684 
1685 struct  vn_vfslocks_bucket {
1686 	kmutex_t vb_lock;
1687 	vn_vfslocks_entry_t *vb_list;
1688 	char pad[64 - sizeof (kmutex_t) - sizeof (void *)];
1689 };
1690 
1691 /*
1692  * Total number of buckets will be NUM_BUCKETS + 1 .
1693  */
1694 
1695 #pragma	align	64(vn_vfslocks_buckets)
1696 static	struct vn_vfslocks_bucket	vn_vfslocks_buckets[NUM_BUCKETS + 1];
1697 
1698 #define	VN_VFSLOCKS_SHIFT	9
1699 
1700 #define	VN_VFSLOCKS_HASH(vfsvpptr)	\
1701 	((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS)
1702 
1703 /*
1704  * vn_vfslocks_getlock() uses an HASH scheme to generate
1705  * rwstlock using vfs/vnode pointer passed to it.
1706  *
1707  * vn_vfslocks_rele() releases a reference in the
1708  * HASH table which allows the entry allocated by
1709  * vn_vfslocks_getlock() to be freed at a later
1710  * stage when the refcount drops to zero.
1711  */
1712 
1713 vn_vfslocks_entry_t *
1714 vn_vfslocks_getlock(void *vfsvpptr)
1715 {
1716 	struct vn_vfslocks_bucket *bp;
1717 	vn_vfslocks_entry_t *vep;
1718 	vn_vfslocks_entry_t *tvep;
1719 
1720 	ASSERT(vfsvpptr != NULL);
1721 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)];
1722 
1723 	mutex_enter(&bp->vb_lock);
1724 	for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
1725 		if (vep->ve_vpvfs == vfsvpptr) {
1726 			vep->ve_refcnt++;
1727 			mutex_exit(&bp->vb_lock);
1728 			return (vep);
1729 		}
1730 	}
1731 	mutex_exit(&bp->vb_lock);
1732 	vep = kmem_alloc(sizeof (*vep), KM_SLEEP);
1733 	rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL);
1734 	vep->ve_vpvfs = (char *)vfsvpptr;
1735 	vep->ve_refcnt = 1;
1736 	mutex_enter(&bp->vb_lock);
1737 	for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) {
1738 		if (tvep->ve_vpvfs == vfsvpptr) {
1739 			tvep->ve_refcnt++;
1740 			mutex_exit(&bp->vb_lock);
1741 
1742 			/*
1743 			 * There is already an entry in the hash
1744 			 * destroy what we just allocated.
1745 			 */
1746 			rwst_destroy(&vep->ve_lock);
1747 			kmem_free(vep, sizeof (*vep));
1748 			return (tvep);
1749 		}
1750 	}
1751 	vep->ve_next = bp->vb_list;
1752 	bp->vb_list = vep;
1753 	mutex_exit(&bp->vb_lock);
1754 	return (vep);
1755 }
1756 
1757 void
1758 vn_vfslocks_rele(vn_vfslocks_entry_t *vepent)
1759 {
1760 	struct vn_vfslocks_bucket *bp;
1761 	vn_vfslocks_entry_t *vep;
1762 	vn_vfslocks_entry_t *pvep;
1763 
1764 	ASSERT(vepent != NULL);
1765 	ASSERT(vepent->ve_vpvfs != NULL);
1766 
1767 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)];
1768 
1769 	mutex_enter(&bp->vb_lock);
1770 	vepent->ve_refcnt--;
1771 
1772 	if ((int32_t)vepent->ve_refcnt < 0)
1773 		cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative");
1774 
1775 	if (vepent->ve_refcnt == 0) {
1776 		for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
1777 			if (vep->ve_vpvfs == vepent->ve_vpvfs) {
1778 				if (bp->vb_list == vep)
1779 					bp->vb_list = vep->ve_next;
1780 				else {
1781 					/* LINTED */
1782 					pvep->ve_next = vep->ve_next;
1783 				}
1784 				mutex_exit(&bp->vb_lock);
1785 				rwst_destroy(&vep->ve_lock);
1786 				kmem_free(vep, sizeof (*vep));
1787 				return;
1788 			}
1789 			pvep = vep;
1790 		}
1791 		cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found");
1792 	}
1793 	mutex_exit(&bp->vb_lock);
1794 }
1795 
1796 /*
1797  * vn_vfswlock_wait is used to implement a lock which is logically a writers
1798  * lock protecting the v_vfsmountedhere field.
1799  * vn_vfswlock_wait has been modified to be similar to vn_vfswlock,
1800  * except that it blocks to acquire the lock VVFSLOCK.
1801  *
1802  * traverse() and routines re-implementing part of traverse (e.g. autofs)
1803  * need to hold this lock. mount(), vn_rename(), vn_remove() and so on
1804  * need the non-blocking version of the writers lock i.e. vn_vfswlock
1805  */
1806 int
1807 vn_vfswlock_wait(vnode_t *vp)
1808 {
1809 	int retval;
1810 	vn_vfslocks_entry_t *vpvfsentry;
1811 	ASSERT(vp != NULL);
1812 
1813 	vpvfsentry = vn_vfslocks_getlock(vp);
1814 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER);
1815 
1816 	if (retval == EINTR) {
1817 		vn_vfslocks_rele(vpvfsentry);
1818 		return (EINTR);
1819 	}
1820 	return (retval);
1821 }
1822 
1823 int
1824 vn_vfsrlock_wait(vnode_t *vp)
1825 {
1826 	int retval;
1827 	vn_vfslocks_entry_t *vpvfsentry;
1828 	ASSERT(vp != NULL);
1829 
1830 	vpvfsentry = vn_vfslocks_getlock(vp);
1831 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER);
1832 
1833 	if (retval == EINTR) {
1834 		vn_vfslocks_rele(vpvfsentry);
1835 		return (EINTR);
1836 	}
1837 
1838 	return (retval);
1839 }
1840 
1841 
1842 /*
1843  * vn_vfswlock is used to implement a lock which is logically a writers lock
1844  * protecting the v_vfsmountedhere field.
1845  */
1846 int
1847 vn_vfswlock(vnode_t *vp)
1848 {
1849 	vn_vfslocks_entry_t *vpvfsentry;
1850 
1851 	/*
1852 	 * If vp is NULL then somebody is trying to lock the covered vnode
1853 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
1854 	 * only happen when unmounting /.  Since that operation will fail
1855 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
1856 	 */
1857 	if (vp == NULL)
1858 		return (EBUSY);
1859 
1860 	vpvfsentry = vn_vfslocks_getlock(vp);
1861 
1862 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
1863 		return (0);
1864 
1865 	vn_vfslocks_rele(vpvfsentry);
1866 	return (EBUSY);
1867 }
1868 
1869 int
1870 vn_vfsrlock(vnode_t *vp)
1871 {
1872 	vn_vfslocks_entry_t *vpvfsentry;
1873 
1874 	/*
1875 	 * If vp is NULL then somebody is trying to lock the covered vnode
1876 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
1877 	 * only happen when unmounting /.  Since that operation will fail
1878 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
1879 	 */
1880 	if (vp == NULL)
1881 		return (EBUSY);
1882 
1883 	vpvfsentry = vn_vfslocks_getlock(vp);
1884 
1885 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
1886 		return (0);
1887 
1888 	vn_vfslocks_rele(vpvfsentry);
1889 	return (EBUSY);
1890 }
1891 
1892 void
1893 vn_vfsunlock(vnode_t *vp)
1894 {
1895 	vn_vfslocks_entry_t *vpvfsentry;
1896 
1897 	/*
1898 	 * ve_refcnt needs to be decremented twice.
1899 	 * 1. To release refernce after a call to vn_vfslocks_getlock()
1900 	 * 2. To release the reference from the locking routines like
1901 	 *    vn_vfsrlock/vn_vfswlock etc,.
1902 	 */
1903 	vpvfsentry = vn_vfslocks_getlock(vp);
1904 	vn_vfslocks_rele(vpvfsentry);
1905 
1906 	rwst_exit(&vpvfsentry->ve_lock);
1907 	vn_vfslocks_rele(vpvfsentry);
1908 }
1909 
1910 int
1911 vn_vfswlock_held(vnode_t *vp)
1912 {
1913 	int held;
1914 	vn_vfslocks_entry_t *vpvfsentry;
1915 
1916 	ASSERT(vp != NULL);
1917 
1918 	vpvfsentry = vn_vfslocks_getlock(vp);
1919 	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
1920 
1921 	vn_vfslocks_rele(vpvfsentry);
1922 	return (held);
1923 }
1924 
1925 
1926 int
1927 vn_make_ops(
1928 	const char *name,			/* Name of file system */
1929 	const fs_operation_def_t *templ,	/* Operation specification */
1930 	vnodeops_t **actual)			/* Return the vnodeops */
1931 {
1932 	int unused_ops;
1933 	int error;
1934 
1935 	*actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP);
1936 
1937 	(*actual)->vnop_name = name;
1938 
1939 	error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ);
1940 	if (error) {
1941 		kmem_free(*actual, sizeof (vnodeops_t));
1942 	}
1943 
1944 #if DEBUG
1945 	if (unused_ops != 0)
1946 		cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied "
1947 		    "but not used", name, unused_ops);
1948 #endif
1949 
1950 	return (error);
1951 }
1952 
1953 /*
1954  * Free the vnodeops created as a result of vn_make_ops()
1955  */
1956 void
1957 vn_freevnodeops(vnodeops_t *vnops)
1958 {
1959 	kmem_free(vnops, sizeof (vnodeops_t));
1960 }
1961 
1962 /*
1963  * Vnode cache.
1964  */
1965 
1966 /* ARGSUSED */
1967 static int
1968 vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
1969 {
1970 	struct vnode *vp;
1971 
1972 	vp = buf;
1973 
1974 	mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
1975 	cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL);
1976 	rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL);
1977 	rw_init(&vp->v_mslock, NULL, RW_DEFAULT, NULL);
1978 
1979 	vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
1980 	vp->v_path = NULL;
1981 	vp->v_mpssdata = NULL;
1982 
1983 	return (0);
1984 }
1985 
1986 /* ARGSUSED */
1987 static void
1988 vn_cache_destructor(void *buf, void *cdrarg)
1989 {
1990 	struct vnode *vp;
1991 
1992 	vp = buf;
1993 
1994 	rw_destroy(&vp->v_mslock);
1995 	rw_destroy(&vp->v_nbllock);
1996 	cv_destroy(&vp->v_cv);
1997 	mutex_destroy(&vp->v_lock);
1998 }
1999 
2000 void
2001 vn_create_cache(void)
2002 {
2003 	vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode), 64,
2004 	    vn_cache_constructor, vn_cache_destructor, NULL, NULL,
2005 	    NULL, 0);
2006 }
2007 
2008 void
2009 vn_destroy_cache(void)
2010 {
2011 	kmem_cache_destroy(vn_cache);
2012 }
2013 
2014 /*
2015  * Used by file systems when fs-specific nodes (e.g., ufs inodes) are
2016  * cached by the file system and vnodes remain associated.
2017  */
2018 void
2019 vn_recycle(vnode_t *vp)
2020 {
2021 	ASSERT(vp->v_pages == NULL);
2022 
2023 	/*
2024 	 * XXX - This really belongs in vn_reinit(), but we have some issues
2025 	 * with the counts.  Best to have it here for clean initialization.
2026 	 */
2027 	vp->v_rdcnt = 0;
2028 	vp->v_wrcnt = 0;
2029 	vp->v_mmap_read = 0;
2030 	vp->v_mmap_write = 0;
2031 
2032 	/*
2033 	 * If FEM was in use, make sure everything gets cleaned up
2034 	 * NOTE: vp->v_femhead is initialized to NULL in the vnode
2035 	 * constructor.
2036 	 */
2037 	if (vp->v_femhead) {
2038 		/* XXX - There should be a free_femhead() that does all this */
2039 		ASSERT(vp->v_femhead->femh_list == NULL);
2040 		mutex_destroy(&vp->v_femhead->femh_lock);
2041 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2042 		vp->v_femhead = NULL;
2043 	}
2044 	if (vp->v_path) {
2045 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2046 		vp->v_path = NULL;
2047 	}
2048 	vp->v_mpssdata = NULL;
2049 }
2050 
2051 /*
2052  * Used to reset the vnode fields including those that are directly accessible
2053  * as well as those which require an accessor function.
2054  *
2055  * Does not initialize:
2056  *	synchronization objects: v_lock, v_nbllock, v_cv
2057  *	v_data (since FS-nodes and vnodes point to each other and should
2058  *		be updated simultaneously)
2059  *	v_op (in case someone needs to make a VOP call on this object)
2060  */
2061 void
2062 vn_reinit(vnode_t *vp)
2063 {
2064 	vp->v_count = 1;
2065 	vp->v_vfsp = NULL;
2066 	vp->v_stream = NULL;
2067 	vp->v_vfsmountedhere = NULL;
2068 	vp->v_flag = 0;
2069 	vp->v_type = VNON;
2070 	vp->v_rdev = NODEV;
2071 
2072 	vp->v_filocks = NULL;
2073 	vp->v_shrlocks = NULL;
2074 	vp->v_pages = NULL;
2075 	vp->v_npages = 0;
2076 	vp->v_msnpages = 0;
2077 	vp->v_scanfront = NULL;
2078 	vp->v_scanback = NULL;
2079 
2080 	vp->v_locality = NULL;
2081 	vp->v_scantime = 0;
2082 	vp->v_mset = 0;
2083 	vp->v_msflags = 0;
2084 	vp->v_msnext = NULL;
2085 	vp->v_msprev = NULL;
2086 
2087 	/* Handles v_femhead, v_path, and the r/w/map counts */
2088 	vn_recycle(vp);
2089 }
2090 
2091 vnode_t *
2092 vn_alloc(int kmflag)
2093 {
2094 	vnode_t *vp;
2095 
2096 	vp = kmem_cache_alloc(vn_cache, kmflag);
2097 
2098 	if (vp != NULL) {
2099 		vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2100 		vn_reinit(vp);
2101 	}
2102 
2103 	return (vp);
2104 }
2105 
2106 void
2107 vn_free(vnode_t *vp)
2108 {
2109 	/*
2110 	 * Some file systems call vn_free() with v_count of zero,
2111 	 * some with v_count of 1.  In any case, the value should
2112 	 * never be anything else.
2113 	 */
2114 	ASSERT((vp->v_count == 0) || (vp->v_count == 1));
2115 	if (vp->v_path != NULL) {
2116 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2117 		vp->v_path = NULL;
2118 	}
2119 
2120 	/* If FEM was in use, make sure everything gets cleaned up */
2121 	if (vp->v_femhead) {
2122 		/* XXX - There should be a free_femhead() that does all this */
2123 		ASSERT(vp->v_femhead->femh_list == NULL);
2124 		mutex_destroy(&vp->v_femhead->femh_lock);
2125 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2126 		vp->v_femhead = NULL;
2127 	}
2128 	vp->v_mpssdata = NULL;
2129 	kmem_cache_free(vn_cache, vp);
2130 }
2131 
2132 /*
2133  * vnode status changes, should define better states than 1, 0.
2134  */
2135 void
2136 vn_reclaim(vnode_t *vp)
2137 {
2138 	vfs_t   *vfsp = vp->v_vfsp;
2139 
2140 	if (vfsp == NULL ||
2141 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2142 		return;
2143 	}
2144 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED);
2145 }
2146 
2147 void
2148 vn_idle(vnode_t *vp)
2149 {
2150 	vfs_t   *vfsp = vp->v_vfsp;
2151 
2152 	if (vfsp == NULL ||
2153 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2154 		return;
2155 	}
2156 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_IDLED);
2157 }
2158 void
2159 vn_exists(vnode_t *vp)
2160 {
2161 	vfs_t   *vfsp = vp->v_vfsp;
2162 
2163 	if (vfsp == NULL ||
2164 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2165 		return;
2166 	}
2167 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS);
2168 }
2169 
2170 void
2171 vn_invalid(vnode_t *vp)
2172 {
2173 	vfs_t   *vfsp = vp->v_vfsp;
2174 
2175 	if (vfsp == NULL ||
2176 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2177 		return;
2178 	}
2179 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED);
2180 }
2181 
2182 /* Vnode event notification */
2183 
2184 int
2185 vnevent_support(vnode_t *vp)
2186 {
2187 	if (vp == NULL)
2188 		return (EINVAL);
2189 
2190 	return (VOP_VNEVENT(vp, VE_SUPPORT));
2191 }
2192 
2193 void
2194 vnevent_rename_src(vnode_t *vp)
2195 {
2196 	if (vp == NULL || vp->v_femhead == NULL) {
2197 		return;
2198 	}
2199 	(void) VOP_VNEVENT(vp, VE_RENAME_SRC);
2200 }
2201 
2202 void
2203 vnevent_rename_dest(vnode_t *vp)
2204 {
2205 	if (vp == NULL || vp->v_femhead == NULL) {
2206 		return;
2207 	}
2208 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST);
2209 }
2210 
2211 void
2212 vnevent_remove(vnode_t *vp)
2213 {
2214 	if (vp == NULL || vp->v_femhead == NULL) {
2215 		return;
2216 	}
2217 	(void) VOP_VNEVENT(vp, VE_REMOVE);
2218 }
2219 
2220 void
2221 vnevent_rmdir(vnode_t *vp)
2222 {
2223 	if (vp == NULL || vp->v_femhead == NULL) {
2224 		return;
2225 	}
2226 	(void) VOP_VNEVENT(vp, VE_RMDIR);
2227 }
2228 
2229 /*
2230  * Vnode accessors.
2231  */
2232 
2233 int
2234 vn_is_readonly(vnode_t *vp)
2235 {
2236 	return (vp->v_vfsp->vfs_flag & VFS_RDONLY);
2237 }
2238 
2239 int
2240 vn_has_flocks(vnode_t *vp)
2241 {
2242 	return (vp->v_filocks != NULL);
2243 }
2244 
2245 int
2246 vn_has_mandatory_locks(vnode_t *vp, int mode)
2247 {
2248 	return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode)));
2249 }
2250 
2251 int
2252 vn_has_cached_data(vnode_t *vp)
2253 {
2254 	return (vp->v_pages != NULL);
2255 }
2256 
2257 /*
2258  * Return 0 if the vnode in question shouldn't be permitted into a zone via
2259  * zone_enter(2).
2260  */
2261 int
2262 vn_can_change_zones(vnode_t *vp)
2263 {
2264 	struct vfssw *vswp;
2265 	int allow = 1;
2266 	vnode_t *rvp;
2267 
2268 	if (nfs_global_client_only != 0)
2269 		return (1);
2270 
2271 	/*
2272 	 * We always want to look at the underlying vnode if there is one.
2273 	 */
2274 	if (VOP_REALVP(vp, &rvp) != 0)
2275 		rvp = vp;
2276 	/*
2277 	 * Some pseudo filesystems (including doorfs) don't actually register
2278 	 * their vfsops_t, so the following may return NULL; we happily let
2279 	 * such vnodes switch zones.
2280 	 */
2281 	vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp));
2282 	if (vswp != NULL) {
2283 		if (vswp->vsw_flag & VSW_NOTZONESAFE)
2284 			allow = 0;
2285 		vfs_unrefvfssw(vswp);
2286 	}
2287 	return (allow);
2288 }
2289 
2290 /*
2291  * Return nonzero if the vnode is a mount point, zero if not.
2292  */
2293 int
2294 vn_ismntpt(vnode_t *vp)
2295 {
2296 	return (vp->v_vfsmountedhere != NULL);
2297 }
2298 
2299 /* Retrieve the vfs (if any) mounted on this vnode */
2300 vfs_t *
2301 vn_mountedvfs(vnode_t *vp)
2302 {
2303 	return (vp->v_vfsmountedhere);
2304 }
2305 
2306 /*
2307  * vn_is_opened() checks whether a particular file is opened and
2308  * whether the open is for read and/or write.
2309  *
2310  * Vnode counts are only kept on regular files (v_type=VREG).
2311  */
2312 int
2313 vn_is_opened(
2314 	vnode_t *vp,
2315 	v_mode_t mode)
2316 {
2317 
2318 	ASSERT(vp != NULL);
2319 
2320 	switch (mode) {
2321 	case V_WRITE:
2322 		if (vp->v_wrcnt)
2323 			return (V_TRUE);
2324 		break;
2325 	case V_RDANDWR:
2326 		if (vp->v_rdcnt && vp->v_wrcnt)
2327 			return (V_TRUE);
2328 		break;
2329 	case V_RDORWR:
2330 		if (vp->v_rdcnt || vp->v_wrcnt)
2331 			return (V_TRUE);
2332 		break;
2333 	case V_READ:
2334 		if (vp->v_rdcnt)
2335 			return (V_TRUE);
2336 		break;
2337 	}
2338 
2339 	return (V_FALSE);
2340 }
2341 
2342 /*
2343  * vn_is_mapped() checks whether a particular file is mapped and whether
2344  * the file is mapped read and/or write.
2345  */
2346 int
2347 vn_is_mapped(
2348 	vnode_t *vp,
2349 	v_mode_t mode)
2350 {
2351 
2352 	ASSERT(vp != NULL);
2353 
2354 #if !defined(_LP64)
2355 	switch (mode) {
2356 	/*
2357 	 * The atomic_add_64_nv functions force atomicity in the
2358 	 * case of 32 bit architectures. Otherwise the 64 bit values
2359 	 * require two fetches. The value of the fields may be
2360 	 * (potentially) changed between the first fetch and the
2361 	 * second
2362 	 */
2363 	case V_WRITE:
2364 		if (atomic_add_64_nv((&(vp->v_mmap_write)), 0))
2365 			return (V_TRUE);
2366 		break;
2367 	case V_RDANDWR:
2368 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) &&
2369 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2370 			return (V_TRUE);
2371 		break;
2372 	case V_RDORWR:
2373 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) ||
2374 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2375 			return (V_TRUE);
2376 		break;
2377 	case V_READ:
2378 		if (atomic_add_64_nv((&(vp->v_mmap_read)), 0))
2379 			return (V_TRUE);
2380 		break;
2381 	}
2382 #else
2383 	switch (mode) {
2384 	case V_WRITE:
2385 		if (vp->v_mmap_write)
2386 			return (V_TRUE);
2387 		break;
2388 	case V_RDANDWR:
2389 		if (vp->v_mmap_read && vp->v_mmap_write)
2390 			return (V_TRUE);
2391 		break;
2392 	case V_RDORWR:
2393 		if (vp->v_mmap_read || vp->v_mmap_write)
2394 			return (V_TRUE);
2395 		break;
2396 	case V_READ:
2397 		if (vp->v_mmap_read)
2398 			return (V_TRUE);
2399 		break;
2400 	}
2401 #endif
2402 
2403 	return (V_FALSE);
2404 }
2405 
2406 /*
2407  * Set the operations vector for a vnode.
2408  *
2409  * FEM ensures that the v_femhead pointer is filled in before the
2410  * v_op pointer is changed.  This means that if the v_femhead pointer
2411  * is NULL, and the v_op field hasn't changed since before which checked
2412  * the v_femhead pointer; then our update is ok - we are not racing with
2413  * FEM.
2414  */
2415 void
2416 vn_setops(vnode_t *vp, vnodeops_t *vnodeops)
2417 {
2418 	vnodeops_t	*op;
2419 
2420 	ASSERT(vp != NULL);
2421 	ASSERT(vnodeops != NULL);
2422 
2423 	op = vp->v_op;
2424 	membar_consumer();
2425 	/*
2426 	 * If vp->v_femhead == NULL, then we'll call casptr() to do the
2427 	 * compare-and-swap on vp->v_op.  If either fails, then FEM is
2428 	 * in effect on the vnode and we need to have FEM deal with it.
2429 	 */
2430 	if (vp->v_femhead != NULL || casptr(&vp->v_op, op, vnodeops) != op) {
2431 		fem_setvnops(vp, vnodeops);
2432 	}
2433 }
2434 
2435 /*
2436  * Retrieve the operations vector for a vnode
2437  * As with vn_setops(above); make sure we aren't racing with FEM.
2438  * FEM sets the v_op to a special, internal, vnodeops that wouldn't
2439  * make sense to the callers of this routine.
2440  */
2441 vnodeops_t *
2442 vn_getops(vnode_t *vp)
2443 {
2444 	vnodeops_t	*op;
2445 
2446 	ASSERT(vp != NULL);
2447 
2448 	op = vp->v_op;
2449 	membar_consumer();
2450 	if (vp->v_femhead == NULL && op == vp->v_op) {
2451 		return (op);
2452 	} else {
2453 		return (fem_getvnops(vp));
2454 	}
2455 }
2456 
2457 /*
2458  * Returns non-zero (1) if the vnodeops matches that of the vnode.
2459  * Returns zero (0) if not.
2460  */
2461 int
2462 vn_matchops(vnode_t *vp, vnodeops_t *vnodeops)
2463 {
2464 	return (vn_getops(vp) == vnodeops);
2465 }
2466 
2467 /*
2468  * Returns non-zero (1) if the specified operation matches the
2469  * corresponding operation for that the vnode.
2470  * Returns zero (0) if not.
2471  */
2472 
2473 #define	MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0))
2474 
2475 int
2476 vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp)
2477 {
2478 	const fs_operation_trans_def_t *otdp;
2479 	fs_generic_func_p *loc = NULL;
2480 	vnodeops_t	*vop = vn_getops(vp);
2481 
2482 	ASSERT(vopname != NULL);
2483 
2484 	for (otdp = vn_ops_table; otdp->name != NULL; otdp++) {
2485 		if (MATCHNAME(otdp->name, vopname)) {
2486 			loc = (fs_generic_func_p *)((char *)(vop)
2487 							+ otdp->offset);
2488 			break;
2489 		}
2490 	}
2491 
2492 	return ((loc != NULL) && (*loc == funcp));
2493 }
2494 
2495 /*
2496  * fs_new_caller_id() needs to return a unique ID on a given local system.
2497  * The IDs do not need to survive across reboots.  These are primarily
2498  * used so that (FEM) monitors can detect particular callers (such as
2499  * the NFS server) to a given vnode/vfs operation.
2500  */
2501 u_longlong_t
2502 fs_new_caller_id()
2503 {
2504 	static uint64_t next_caller_id = 0LL; /* First call returns 1 */
2505 
2506 	return ((u_longlong_t)atomic_add_64_nv(&next_caller_id, 1));
2507 }
2508 
2509 /*
2510  * Given a starting vnode and a path, updates the path in the target vnode in
2511  * a safe manner.  If the vnode already has path information embedded, then the
2512  * cached path is left untouched.
2513  */
2514 void
2515 vn_setpath(vnode_t *rootvp, struct vnode *startvp, struct vnode *vp,
2516     const char *path, size_t plen)
2517 {
2518 	char	*rpath;
2519 	vnode_t	*base;
2520 	size_t	rpathlen, rpathalloc;
2521 	int	doslash = 1;
2522 
2523 	if (*path == '/') {
2524 		base = rootvp;
2525 		path++;
2526 		plen--;
2527 	} else {
2528 		base = startvp;
2529 	}
2530 
2531 	/*
2532 	 * We cannot grab base->v_lock while we hold vp->v_lock because of
2533 	 * the potential for deadlock.
2534 	 */
2535 	mutex_enter(&base->v_lock);
2536 	if (base->v_path == NULL) {
2537 		mutex_exit(&base->v_lock);
2538 		return;
2539 	}
2540 
2541 	rpathlen = strlen(base->v_path);
2542 	rpathalloc = rpathlen + plen + 1;
2543 	/* Avoid adding a slash if there's already one there */
2544 	if (base->v_path[rpathlen-1] == '/')
2545 		doslash = 0;
2546 	else
2547 		rpathalloc++;
2548 
2549 	/*
2550 	 * We don't want to call kmem_alloc(KM_SLEEP) with kernel locks held,
2551 	 * so we must do this dance.  If, by chance, something changes the path,
2552 	 * just give up since there is no real harm.
2553 	 */
2554 	mutex_exit(&base->v_lock);
2555 
2556 	rpath = kmem_alloc(rpathalloc, KM_SLEEP);
2557 
2558 	mutex_enter(&base->v_lock);
2559 	if (base->v_path == NULL || strlen(base->v_path) != rpathlen) {
2560 		mutex_exit(&base->v_lock);
2561 		kmem_free(rpath, rpathalloc);
2562 		return;
2563 	}
2564 	bcopy(base->v_path, rpath, rpathlen);
2565 	mutex_exit(&base->v_lock);
2566 
2567 	if (doslash)
2568 		rpath[rpathlen++] = '/';
2569 	bcopy(path, rpath + rpathlen, plen);
2570 	rpath[rpathlen + plen] = '\0';
2571 
2572 	mutex_enter(&vp->v_lock);
2573 	if (vp->v_path != NULL) {
2574 		mutex_exit(&vp->v_lock);
2575 		kmem_free(rpath, rpathalloc);
2576 	} else {
2577 		vp->v_path = rpath;
2578 		mutex_exit(&vp->v_lock);
2579 	}
2580 }
2581 
2582 /*
2583  * Sets the path to the vnode to be the given string, regardless of current
2584  * context.  The string must be a complete path from rootdir.  This is only used
2585  * by fsop_root() for setting the path based on the mountpoint.
2586  */
2587 void
2588 vn_setpath_str(struct vnode *vp, const char *str, size_t len)
2589 {
2590 	char *buf = kmem_alloc(len + 1, KM_SLEEP);
2591 
2592 	mutex_enter(&vp->v_lock);
2593 	if (vp->v_path != NULL) {
2594 		mutex_exit(&vp->v_lock);
2595 		kmem_free(buf, len + 1);
2596 		return;
2597 	}
2598 
2599 	vp->v_path = buf;
2600 	bcopy(str, vp->v_path, len);
2601 	vp->v_path[len] = '\0';
2602 
2603 	mutex_exit(&vp->v_lock);
2604 }
2605 
2606 /*
2607  * Similar to vn_setpath_str(), this function sets the path of the destination
2608  * vnode to the be the same as the source vnode.
2609  */
2610 void
2611 vn_copypath(struct vnode *src, struct vnode *dst)
2612 {
2613 	char *buf;
2614 	int alloc;
2615 
2616 	mutex_enter(&src->v_lock);
2617 	if (src->v_path == NULL) {
2618 		mutex_exit(&src->v_lock);
2619 		return;
2620 	}
2621 	alloc = strlen(src->v_path) + 1;
2622 
2623 	/* avoid kmem_alloc() with lock held */
2624 	mutex_exit(&src->v_lock);
2625 	buf = kmem_alloc(alloc, KM_SLEEP);
2626 	mutex_enter(&src->v_lock);
2627 	if (src->v_path == NULL || strlen(src->v_path) + 1 != alloc) {
2628 		mutex_exit(&src->v_lock);
2629 		kmem_free(buf, alloc);
2630 		return;
2631 	}
2632 	bcopy(src->v_path, buf, alloc);
2633 	mutex_exit(&src->v_lock);
2634 
2635 	mutex_enter(&dst->v_lock);
2636 	if (dst->v_path != NULL) {
2637 		mutex_exit(&dst->v_lock);
2638 		kmem_free(buf, alloc);
2639 		return;
2640 	}
2641 	dst->v_path = buf;
2642 	mutex_exit(&dst->v_lock);
2643 }
2644 
2645 /*
2646  * XXX Private interface for segvn routines that handle vnode
2647  * large page segments.
2648  *
2649  * return 1 if vp's file system VOP_PAGEIO() implementation
2650  * can be safely used instead of VOP_GETPAGE() for handling
2651  * pagefaults against regular non swap files. VOP_PAGEIO()
2652  * interface is considered safe here if its implementation
2653  * is very close to VOP_GETPAGE() implementation.
2654  * e.g. It zero's out the part of the page beyond EOF. Doesn't
2655  * panic if there're file holes but instead returns an error.
2656  * Doesn't assume file won't be changed by user writes, etc.
2657  *
2658  * return 0 otherwise.
2659  *
2660  * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs.
2661  */
2662 int
2663 vn_vmpss_usepageio(vnode_t *vp)
2664 {
2665 	vfs_t   *vfsp = vp->v_vfsp;
2666 	char *fsname = vfssw[vfsp->vfs_fstype].vsw_name;
2667 	char *pageio_ok_fss[] = {"ufs", "nfs", NULL};
2668 	char **fsok = pageio_ok_fss;
2669 
2670 	if (fsname == NULL) {
2671 		return (0);
2672 	}
2673 
2674 	for (; *fsok; fsok++) {
2675 		if (strcmp(*fsok, fsname) == 0) {
2676 			return (1);
2677 		}
2678 	}
2679 	return (0);
2680 }
2681 
2682 /* VOP_XXX() macros call the corresponding fop_xxx() function */
2683 
2684 int
2685 fop_open(
2686 	vnode_t **vpp,
2687 	int mode,
2688 	cred_t *cr)
2689 {
2690 	int ret;
2691 	vnode_t *vp = *vpp;
2692 
2693 	VN_HOLD(vp);
2694 	/*
2695 	 * Adding to the vnode counts before calling open
2696 	 * avoids the need for a mutex. It circumvents a race
2697 	 * condition where a query made on the vnode counts results in a
2698 	 * false negative. The inquirer goes away believing the file is
2699 	 * not open when there is an open on the file already under way.
2700 	 *
2701 	 * The counts are meant to prevent NFS from granting a delegation
2702 	 * when it would be dangerous to do so.
2703 	 *
2704 	 * The vnode counts are only kept on regular files
2705 	 */
2706 	if ((*vpp)->v_type == VREG) {
2707 		if (mode & FREAD)
2708 			atomic_add_32(&((*vpp)->v_rdcnt), 1);
2709 		if (mode & FWRITE)
2710 			atomic_add_32(&((*vpp)->v_wrcnt), 1);
2711 	}
2712 
2713 	ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr);
2714 
2715 	if (ret) {
2716 		/*
2717 		 * Use the saved vp just in case the vnode ptr got trashed
2718 		 * by the error.
2719 		 */
2720 		VOPSTATS_UPDATE(vp, open);
2721 		if ((vp->v_type == VREG) && (mode & FREAD))
2722 			atomic_add_32(&(vp->v_rdcnt), -1);
2723 		if ((vp->v_type == VREG) && (mode & FWRITE))
2724 			atomic_add_32(&(vp->v_wrcnt), -1);
2725 	} else {
2726 		/*
2727 		 * Some filesystems will return a different vnode,
2728 		 * but the same path was still used to open it.
2729 		 * So if we do change the vnode and need to
2730 		 * copy over the path, do so here, rather than special
2731 		 * casing each filesystem. Adjust the vnode counts to
2732 		 * reflect the vnode switch.
2733 		 */
2734 		VOPSTATS_UPDATE(*vpp, open);
2735 		if (*vpp != vp && *vpp != NULL) {
2736 			vn_copypath(vp, *vpp);
2737 			if (((*vpp)->v_type == VREG) && (mode & FREAD))
2738 				atomic_add_32(&((*vpp)->v_rdcnt), 1);
2739 			if ((vp->v_type == VREG) && (mode & FREAD))
2740 				atomic_add_32(&(vp->v_rdcnt), -1);
2741 			if (((*vpp)->v_type == VREG) && (mode & FWRITE))
2742 				atomic_add_32(&((*vpp)->v_wrcnt), 1);
2743 			if ((vp->v_type == VREG) && (mode & FWRITE))
2744 				atomic_add_32(&(vp->v_wrcnt), -1);
2745 		}
2746 	}
2747 	VN_RELE(vp);
2748 	return (ret);
2749 }
2750 
2751 int
2752 fop_close(
2753 	vnode_t *vp,
2754 	int flag,
2755 	int count,
2756 	offset_t offset,
2757 	cred_t *cr)
2758 {
2759 	int err;
2760 
2761 	err = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr);
2762 	VOPSTATS_UPDATE(vp, close);
2763 	/*
2764 	 * Check passed in count to handle possible dups. Vnode counts are only
2765 	 * kept on regular files
2766 	 */
2767 	if ((vp->v_type == VREG) && (count == 1))  {
2768 		if (flag & FREAD) {
2769 			ASSERT(vp->v_rdcnt > 0);
2770 			atomic_add_32(&(vp->v_rdcnt), -1);
2771 		}
2772 		if (flag & FWRITE) {
2773 			ASSERT(vp->v_wrcnt > 0);
2774 			atomic_add_32(&(vp->v_wrcnt), -1);
2775 		}
2776 	}
2777 	return (err);
2778 }
2779 
2780 int
2781 fop_read(
2782 	vnode_t *vp,
2783 	uio_t *uiop,
2784 	int ioflag,
2785 	cred_t *cr,
2786 	struct caller_context *ct)
2787 {
2788 	int	err;
2789 	ssize_t	resid_start = uiop->uio_resid;
2790 
2791 	err = (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct);
2792 	VOPSTATS_UPDATE_IO(vp, read,
2793 	    read_bytes, (resid_start - uiop->uio_resid));
2794 	return (err);
2795 }
2796 
2797 int
2798 fop_write(
2799 	vnode_t *vp,
2800 	uio_t *uiop,
2801 	int ioflag,
2802 	cred_t *cr,
2803 	struct caller_context *ct)
2804 {
2805 	int	err;
2806 	ssize_t	resid_start = uiop->uio_resid;
2807 
2808 	err = (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct);
2809 	VOPSTATS_UPDATE_IO(vp, write,
2810 	    write_bytes, (resid_start - uiop->uio_resid));
2811 	return (err);
2812 }
2813 
2814 int
2815 fop_ioctl(
2816 	vnode_t *vp,
2817 	int cmd,
2818 	intptr_t arg,
2819 	int flag,
2820 	cred_t *cr,
2821 	int *rvalp)
2822 {
2823 	int	err;
2824 
2825 	err = (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp);
2826 	VOPSTATS_UPDATE(vp, ioctl);
2827 	return (err);
2828 }
2829 
2830 int
2831 fop_setfl(
2832 	vnode_t *vp,
2833 	int oflags,
2834 	int nflags,
2835 	cred_t *cr)
2836 {
2837 	int	err;
2838 
2839 	err = (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr);
2840 	VOPSTATS_UPDATE(vp, setfl);
2841 	return (err);
2842 }
2843 
2844 int
2845 fop_getattr(
2846 	vnode_t *vp,
2847 	vattr_t *vap,
2848 	int flags,
2849 	cred_t *cr)
2850 {
2851 	int	err;
2852 
2853 	err = (*(vp)->v_op->vop_getattr)(vp, vap, flags, cr);
2854 	VOPSTATS_UPDATE(vp, getattr);
2855 	return (err);
2856 }
2857 
2858 int
2859 fop_setattr(
2860 	vnode_t *vp,
2861 	vattr_t *vap,
2862 	int flags,
2863 	cred_t *cr,
2864 	caller_context_t *ct)
2865 {
2866 	int	err;
2867 
2868 	err = (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct);
2869 	VOPSTATS_UPDATE(vp, setattr);
2870 	return (err);
2871 }
2872 
2873 int
2874 fop_access(
2875 	vnode_t *vp,
2876 	int mode,
2877 	int flags,
2878 	cred_t *cr)
2879 {
2880 	int	err;
2881 
2882 	err = (*(vp)->v_op->vop_access)(vp, mode, flags, cr);
2883 	VOPSTATS_UPDATE(vp, access);
2884 	return (err);
2885 }
2886 
2887 int
2888 fop_lookup(
2889 	vnode_t *dvp,
2890 	char *nm,
2891 	vnode_t **vpp,
2892 	pathname_t *pnp,
2893 	int flags,
2894 	vnode_t *rdir,
2895 	cred_t *cr)
2896 {
2897 	int ret;
2898 
2899 	ret = (*(dvp)->v_op->vop_lookup)(dvp, nm, vpp, pnp, flags, rdir, cr);
2900 	if (ret == 0 && *vpp) {
2901 		VOPSTATS_UPDATE(*vpp, lookup);
2902 		if ((*vpp)->v_path == NULL) {
2903 			vn_setpath(rootdir, dvp, *vpp, nm, strlen(nm));
2904 		}
2905 	}
2906 
2907 	return (ret);
2908 }
2909 
2910 int
2911 fop_create(
2912 	vnode_t *dvp,
2913 	char *name,
2914 	vattr_t *vap,
2915 	vcexcl_t excl,
2916 	int mode,
2917 	vnode_t **vpp,
2918 	cred_t *cr,
2919 	int flag)
2920 {
2921 	int ret;
2922 
2923 	ret = (*(dvp)->v_op->vop_create)
2924 				(dvp, name, vap, excl, mode, vpp, cr, flag);
2925 	if (ret == 0 && *vpp) {
2926 		VOPSTATS_UPDATE(*vpp, create);
2927 		if ((*vpp)->v_path == NULL) {
2928 			vn_setpath(rootdir, dvp, *vpp, name, strlen(name));
2929 		}
2930 	}
2931 
2932 	return (ret);
2933 }
2934 
2935 int
2936 fop_remove(
2937 	vnode_t *dvp,
2938 	char *nm,
2939 	cred_t *cr)
2940 {
2941 	int	err;
2942 
2943 	err = (*(dvp)->v_op->vop_remove)(dvp, nm, cr);
2944 	VOPSTATS_UPDATE(dvp, remove);
2945 	return (err);
2946 }
2947 
2948 int
2949 fop_link(
2950 	vnode_t *tdvp,
2951 	vnode_t *svp,
2952 	char *tnm,
2953 	cred_t *cr)
2954 {
2955 	int	err;
2956 
2957 	err = (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr);
2958 	VOPSTATS_UPDATE(tdvp, link);
2959 	return (err);
2960 }
2961 
2962 int
2963 fop_rename(
2964 	vnode_t *sdvp,
2965 	char *snm,
2966 	vnode_t *tdvp,
2967 	char *tnm,
2968 	cred_t *cr)
2969 {
2970 	int	err;
2971 
2972 	err = (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr);
2973 	VOPSTATS_UPDATE(sdvp, rename);
2974 	return (err);
2975 }
2976 
2977 int
2978 fop_mkdir(
2979 	vnode_t *dvp,
2980 	char *dirname,
2981 	vattr_t *vap,
2982 	vnode_t **vpp,
2983 	cred_t *cr)
2984 {
2985 	int ret;
2986 
2987 	ret = (*(dvp)->v_op->vop_mkdir)(dvp, dirname, vap, vpp, cr);
2988 	if (ret == 0 && *vpp) {
2989 		VOPSTATS_UPDATE(*vpp, mkdir);
2990 		if ((*vpp)->v_path == NULL) {
2991 			vn_setpath(rootdir, dvp, *vpp, dirname,
2992 			    strlen(dirname));
2993 		}
2994 	}
2995 
2996 	return (ret);
2997 }
2998 
2999 int
3000 fop_rmdir(
3001 	vnode_t *dvp,
3002 	char *nm,
3003 	vnode_t *cdir,
3004 	cred_t *cr)
3005 {
3006 	int	err;
3007 
3008 	err = (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr);
3009 	VOPSTATS_UPDATE(dvp, rmdir);
3010 	return (err);
3011 }
3012 
3013 int
3014 fop_readdir(
3015 	vnode_t *vp,
3016 	uio_t *uiop,
3017 	cred_t *cr,
3018 	int *eofp)
3019 {
3020 	int	err;
3021 	ssize_t	resid_start = uiop->uio_resid;
3022 
3023 	err = (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp);
3024 	VOPSTATS_UPDATE_IO(vp, readdir,
3025 	    readdir_bytes, (resid_start - uiop->uio_resid));
3026 	return (err);
3027 }
3028 
3029 int
3030 fop_symlink(
3031 	vnode_t *dvp,
3032 	char *linkname,
3033 	vattr_t *vap,
3034 	char *target,
3035 	cred_t *cr)
3036 {
3037 	int	err;
3038 
3039 	err = (*(dvp)->v_op->vop_symlink) (dvp, linkname, vap, target, cr);
3040 	VOPSTATS_UPDATE(dvp, symlink);
3041 	return (err);
3042 }
3043 
3044 int
3045 fop_readlink(
3046 	vnode_t *vp,
3047 	uio_t *uiop,
3048 	cred_t *cr)
3049 {
3050 	int	err;
3051 
3052 	err = (*(vp)->v_op->vop_readlink)(vp, uiop, cr);
3053 	VOPSTATS_UPDATE(vp, readlink);
3054 	return (err);
3055 }
3056 
3057 int
3058 fop_fsync(
3059 	vnode_t *vp,
3060 	int syncflag,
3061 	cred_t *cr)
3062 {
3063 	int	err;
3064 
3065 	err = (*(vp)->v_op->vop_fsync)(vp, syncflag, cr);
3066 	VOPSTATS_UPDATE(vp, fsync);
3067 	return (err);
3068 }
3069 
3070 void
3071 fop_inactive(
3072 	vnode_t *vp,
3073 	cred_t *cr)
3074 {
3075 	/* Need to update stats before vop call since we may lose the vnode */
3076 	VOPSTATS_UPDATE(vp, inactive);
3077 	(*(vp)->v_op->vop_inactive)(vp, cr);
3078 }
3079 
3080 int
3081 fop_fid(
3082 	vnode_t *vp,
3083 	fid_t *fidp)
3084 {
3085 	int	err;
3086 
3087 	err = (*(vp)->v_op->vop_fid)(vp, fidp);
3088 	VOPSTATS_UPDATE(vp, fid);
3089 	return (err);
3090 }
3091 
3092 int
3093 fop_rwlock(
3094 	vnode_t *vp,
3095 	int write_lock,
3096 	caller_context_t *ct)
3097 {
3098 	int	ret;
3099 
3100 	ret = ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct));
3101 	VOPSTATS_UPDATE(vp, rwlock);
3102 	return (ret);
3103 }
3104 
3105 void
3106 fop_rwunlock(
3107 	vnode_t *vp,
3108 	int write_lock,
3109 	caller_context_t *ct)
3110 {
3111 	(*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct);
3112 	VOPSTATS_UPDATE(vp, rwunlock);
3113 }
3114 
3115 int
3116 fop_seek(
3117 	vnode_t *vp,
3118 	offset_t ooff,
3119 	offset_t *noffp)
3120 {
3121 	int	err;
3122 
3123 	err = (*(vp)->v_op->vop_seek)(vp, ooff, noffp);
3124 	VOPSTATS_UPDATE(vp, seek);
3125 	return (err);
3126 }
3127 
3128 int
3129 fop_cmp(
3130 	vnode_t *vp1,
3131 	vnode_t *vp2)
3132 {
3133 	int	err;
3134 
3135 	err = (*(vp1)->v_op->vop_cmp)(vp1, vp2);
3136 	VOPSTATS_UPDATE(vp1, cmp);
3137 	return (err);
3138 }
3139 
3140 int
3141 fop_frlock(
3142 	vnode_t *vp,
3143 	int cmd,
3144 	flock64_t *bfp,
3145 	int flag,
3146 	offset_t offset,
3147 	struct flk_callback *flk_cbp,
3148 	cred_t *cr)
3149 {
3150 	int	err;
3151 
3152 	err = (*(vp)->v_op->vop_frlock)
3153 				(vp, cmd, bfp, flag, offset, flk_cbp, cr);
3154 	VOPSTATS_UPDATE(vp, frlock);
3155 	return (err);
3156 }
3157 
3158 int
3159 fop_space(
3160 	vnode_t *vp,
3161 	int cmd,
3162 	flock64_t *bfp,
3163 	int flag,
3164 	offset_t offset,
3165 	cred_t *cr,
3166 	caller_context_t *ct)
3167 {
3168 	int	err;
3169 
3170 	err = (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct);
3171 	VOPSTATS_UPDATE(vp, space);
3172 	return (err);
3173 }
3174 
3175 int
3176 fop_realvp(
3177 	vnode_t *vp,
3178 	vnode_t **vpp)
3179 {
3180 	int	err;
3181 
3182 	err = (*(vp)->v_op->vop_realvp)(vp, vpp);
3183 	VOPSTATS_UPDATE(vp, realvp);
3184 	return (err);
3185 }
3186 
3187 int
3188 fop_getpage(
3189 	vnode_t *vp,
3190 	offset_t off,
3191 	size_t len,
3192 	uint_t *protp,
3193 	page_t **plarr,
3194 	size_t plsz,
3195 	struct seg *seg,
3196 	caddr_t addr,
3197 	enum seg_rw rw,
3198 	cred_t *cr)
3199 {
3200 	int	err;
3201 
3202 	err = (*(vp)->v_op->vop_getpage)
3203 			(vp, off, len, protp, plarr, plsz, seg, addr, rw, cr);
3204 	VOPSTATS_UPDATE(vp, getpage);
3205 	return (err);
3206 }
3207 
3208 int
3209 fop_putpage(
3210 	vnode_t *vp,
3211 	offset_t off,
3212 	size_t len,
3213 	int flags,
3214 	cred_t *cr)
3215 {
3216 	int	err;
3217 
3218 	err = (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr);
3219 	VOPSTATS_UPDATE(vp, putpage);
3220 	return (err);
3221 }
3222 
3223 int
3224 fop_map(
3225 	vnode_t *vp,
3226 	offset_t off,
3227 	struct as *as,
3228 	caddr_t *addrp,
3229 	size_t len,
3230 	uchar_t prot,
3231 	uchar_t maxprot,
3232 	uint_t flags,
3233 	cred_t *cr)
3234 {
3235 	int	err;
3236 
3237 	err = (*(vp)->v_op->vop_map)
3238 			(vp, off, as, addrp, len, prot, maxprot, flags, cr);
3239 	VOPSTATS_UPDATE(vp, map);
3240 	return (err);
3241 }
3242 
3243 int
3244 fop_addmap(
3245 	vnode_t *vp,
3246 	offset_t off,
3247 	struct as *as,
3248 	caddr_t addr,
3249 	size_t len,
3250 	uchar_t prot,
3251 	uchar_t maxprot,
3252 	uint_t flags,
3253 	cred_t *cr)
3254 {
3255 	int error;
3256 	u_longlong_t delta;
3257 
3258 	error = (*(vp)->v_op->vop_addmap)
3259 			(vp, off, as, addr, len, prot, maxprot, flags, cr);
3260 
3261 	if ((!error) && (vp->v_type == VREG)) {
3262 		delta = (u_longlong_t)btopr(len);
3263 		/*
3264 		 * If file is declared MAP_PRIVATE, it can't be written back
3265 		 * even if open for write. Handle as read.
3266 		 */
3267 		if (flags & MAP_PRIVATE) {
3268 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3269 				(int64_t)delta);
3270 		} else {
3271 			/*
3272 			 * atomic_add_64 forces the fetch of a 64 bit value to
3273 			 * be atomic on 32 bit machines
3274 			 */
3275 			if (maxprot & PROT_WRITE)
3276 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
3277 					(int64_t)delta);
3278 			if (maxprot & PROT_READ)
3279 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3280 					(int64_t)delta);
3281 			if (maxprot & PROT_EXEC)
3282 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3283 					(int64_t)delta);
3284 		}
3285 	}
3286 	VOPSTATS_UPDATE(vp, addmap);
3287 	return (error);
3288 }
3289 
3290 int
3291 fop_delmap(
3292 	vnode_t *vp,
3293 	offset_t off,
3294 	struct as *as,
3295 	caddr_t addr,
3296 	size_t len,
3297 	uint_t prot,
3298 	uint_t maxprot,
3299 	uint_t flags,
3300 	cred_t *cr)
3301 {
3302 	int error;
3303 	u_longlong_t delta;
3304 	error = (*(vp)->v_op->vop_delmap)
3305 		(vp, off, as, addr, len, prot, maxprot, flags, cr);
3306 
3307 	/*
3308 	 * NFS calls into delmap twice, the first time
3309 	 * it simply establishes a callback mechanism and returns EAGAIN
3310 	 * while the real work is being done upon the second invocation.
3311 	 * We have to detect this here and only decrement the counts upon
3312 	 * the second delmap request.
3313 	 */
3314 	if ((error != EAGAIN) && (vp->v_type == VREG)) {
3315 
3316 		delta = (u_longlong_t)btopr(len);
3317 
3318 		if (flags & MAP_PRIVATE) {
3319 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3320 				(int64_t)(-delta));
3321 		} else {
3322 			/*
3323 			 * atomic_add_64 forces the fetch of a 64 bit value
3324 			 * to be atomic on 32 bit machines
3325 			 */
3326 			if (maxprot & PROT_WRITE)
3327 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
3328 					(int64_t)(-delta));
3329 			if (maxprot & PROT_READ)
3330 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3331 					(int64_t)(-delta));
3332 			if (maxprot & PROT_EXEC)
3333 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3334 					(int64_t)(-delta));
3335 		}
3336 	}
3337 	VOPSTATS_UPDATE(vp, delmap);
3338 	return (error);
3339 }
3340 
3341 
3342 int
3343 fop_poll(
3344 	vnode_t *vp,
3345 	short events,
3346 	int anyyet,
3347 	short *reventsp,
3348 	struct pollhead **phpp)
3349 {
3350 	int	err;
3351 
3352 	err = (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp);
3353 	VOPSTATS_UPDATE(vp, poll);
3354 	return (err);
3355 }
3356 
3357 int
3358 fop_dump(
3359 	vnode_t *vp,
3360 	caddr_t addr,
3361 	int lbdn,
3362 	int dblks)
3363 {
3364 	int	err;
3365 
3366 	err = (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks);
3367 	VOPSTATS_UPDATE(vp, dump);
3368 	return (err);
3369 }
3370 
3371 int
3372 fop_pathconf(
3373 	vnode_t *vp,
3374 	int cmd,
3375 	ulong_t *valp,
3376 	cred_t *cr)
3377 {
3378 	int	err;
3379 
3380 	err = (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr);
3381 	VOPSTATS_UPDATE(vp, pathconf);
3382 	return (err);
3383 }
3384 
3385 int
3386 fop_pageio(
3387 	vnode_t *vp,
3388 	struct page *pp,
3389 	u_offset_t io_off,
3390 	size_t io_len,
3391 	int flags,
3392 	cred_t *cr)
3393 {
3394 	int	err;
3395 
3396 	err = (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr);
3397 	VOPSTATS_UPDATE(vp, pageio);
3398 	return (err);
3399 }
3400 
3401 int
3402 fop_dumpctl(
3403 	vnode_t *vp,
3404 	int action,
3405 	int *blkp)
3406 {
3407 	int	err;
3408 	err = (*(vp)->v_op->vop_dumpctl)(vp, action, blkp);
3409 	VOPSTATS_UPDATE(vp, dumpctl);
3410 	return (err);
3411 }
3412 
3413 void
3414 fop_dispose(
3415 	vnode_t *vp,
3416 	page_t *pp,
3417 	int flag,
3418 	int dn,
3419 	cred_t *cr)
3420 {
3421 	/* Must do stats first since it's possible to lose the vnode */
3422 	VOPSTATS_UPDATE(vp, dispose);
3423 	(*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr);
3424 }
3425 
3426 int
3427 fop_setsecattr(
3428 	vnode_t *vp,
3429 	vsecattr_t *vsap,
3430 	int flag,
3431 	cred_t *cr)
3432 {
3433 	int	err;
3434 
3435 	err = (*(vp)->v_op->vop_setsecattr) (vp, vsap, flag, cr);
3436 	VOPSTATS_UPDATE(vp, setsecattr);
3437 	return (err);
3438 }
3439 
3440 int
3441 fop_getsecattr(
3442 	vnode_t *vp,
3443 	vsecattr_t *vsap,
3444 	int flag,
3445 	cred_t *cr)
3446 {
3447 	int	err;
3448 
3449 	err = (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr);
3450 	VOPSTATS_UPDATE(vp, getsecattr);
3451 	return (err);
3452 }
3453 
3454 int
3455 fop_shrlock(
3456 	vnode_t *vp,
3457 	int cmd,
3458 	struct shrlock *shr,
3459 	int flag,
3460 	cred_t *cr)
3461 {
3462 	int	err;
3463 
3464 	err = (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr);
3465 	VOPSTATS_UPDATE(vp, shrlock);
3466 	return (err);
3467 }
3468 
3469 int
3470 fop_vnevent(vnode_t *vp, vnevent_t vnevent)
3471 {
3472 	int	err;
3473 
3474 	err = (*(vp)->v_op->vop_vnevent)(vp, vnevent);
3475 	VOPSTATS_UPDATE(vp, vnevent);
3476 	return (err);
3477 }
3478