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