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