xref: /titanic_41/usr/src/uts/common/fs/vnode.c (revision 4a6822d07d6d3f9ffe6907ef5f10d11dcadd75c6)
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) == 0) {
1569 			error = EBUSY;
1570 			goto out;
1571 		}
1572 
1573 		/*
1574 		 * Namefs specific code starts here.
1575 		 */
1576 
1577 		if (dirflag == RMDIRECTORY) {
1578 			/*
1579 			 * User called rmdir(2) on a file that has
1580 			 * been namefs mounted on top of.  Since
1581 			 * namefs doesn't allow directories to
1582 			 * be mounted on other files we know
1583 			 * vp is not of type VDIR so fail to operation.
1584 			 */
1585 			error = ENOTDIR;
1586 			goto out;
1587 		}
1588 
1589 		/*
1590 		 * If VROOT is still set after grabbing vp->v_lock,
1591 		 * noone has finished nm_unmount so far and coveredvp
1592 		 * is valid.
1593 		 * If we manage to grab vn_vfswlock(coveredvp) before releasing
1594 		 * vp->v_lock, any race window is eliminated.
1595 		 */
1596 
1597 		mutex_enter(&vp->v_lock);
1598 		if ((vp->v_flag & VROOT) == 0) {
1599 			/* Someone beat us to the unmount */
1600 			mutex_exit(&vp->v_lock);
1601 			error = EBUSY;
1602 			goto out;
1603 		}
1604 		vfsp = vp->v_vfsp;
1605 		coveredvp = vfsp->vfs_vnodecovered;
1606 		ASSERT(coveredvp);
1607 		/*
1608 		 * Note: Implementation of vn_vfswlock shows that ordering of
1609 		 * v_lock / vn_vfswlock is not an issue here.
1610 		 */
1611 		error = vn_vfswlock(coveredvp);
1612 		mutex_exit(&vp->v_lock);
1613 
1614 		if (error)
1615 			goto out;
1616 
1617 		VN_HOLD(coveredvp);
1618 		VN_RELE(vp);
1619 		error = dounmount(vfsp, 0, CRED());
1620 
1621 		/*
1622 		 * Unmounted the namefs file system; now get
1623 		 * the object it was mounted over.
1624 		 */
1625 		vp = coveredvp;
1626 		/*
1627 		 * If namefs was mounted over a directory, then
1628 		 * we want to use rmdir() instead of unlink().
1629 		 */
1630 		if (vp->v_type == VDIR)
1631 			dirflag = RMDIRECTORY;
1632 
1633 		if (error)
1634 			goto out;
1635 	}
1636 
1637 	/*
1638 	 * Make sure filesystem is writeable.
1639 	 * We check the parent directory's vfs in case this is an lofs vnode.
1640 	 */
1641 	if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) {
1642 		error = EROFS;
1643 		goto out;
1644 	}
1645 
1646 	vtype = vp->v_type;
1647 
1648 	/*
1649 	 * If there is the possibility of an nbmand share reservation, make
1650 	 * sure it's okay to remove the file.  Keep a reference to the
1651 	 * vnode, so that we can exit the nbl critical region after
1652 	 * calling VOP_REMOVE.
1653 	 * If there is no possibility of an nbmand share reservation,
1654 	 * release the vnode reference now.  Filesystems like NFS may
1655 	 * behave differently if there is an extra reference, so get rid of
1656 	 * this one.  Fortunately, we can't have nbmand mounts on NFS
1657 	 * filesystems.
1658 	 */
1659 	if (nbl_need_check(vp)) {
1660 		nbl_start_crit(vp, RW_READER);
1661 		in_crit = 1;
1662 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0)) {
1663 			error = EACCES;
1664 			goto out;
1665 		}
1666 	} else {
1667 		VN_RELE(vp);
1668 		vp = NULL;
1669 	}
1670 
1671 	if (dirflag == RMDIRECTORY) {
1672 		/*
1673 		 * Caller is using rmdir(2), which can only be applied to
1674 		 * directories.
1675 		 */
1676 		if (vtype != VDIR) {
1677 			error = ENOTDIR;
1678 		} else {
1679 			vnode_t *cwd;
1680 			proc_t *pp = curproc;
1681 
1682 			mutex_enter(&pp->p_lock);
1683 			cwd = PTOU(pp)->u_cdir;
1684 			VN_HOLD(cwd);
1685 			mutex_exit(&pp->p_lock);
1686 			error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED());
1687 			VN_RELE(cwd);
1688 		}
1689 	} else {
1690 		/*
1691 		 * Unlink(2) can be applied to anything.
1692 		 */
1693 		error = VOP_REMOVE(dvp, pn.pn_path, CRED());
1694 	}
1695 
1696 out:
1697 	pn_free(&pn);
1698 	if (in_crit) {
1699 		nbl_end_crit(vp);
1700 		in_crit = 0;
1701 	}
1702 	if (vp != NULL)
1703 		VN_RELE(vp);
1704 	if (dvp != NULL)
1705 		VN_RELE(dvp);
1706 	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1707 		goto top;
1708 	return (error);
1709 }
1710 
1711 /*
1712  * Utility function to compare equality of vnodes.
1713  * Compare the underlying real vnodes, if there are underlying vnodes.
1714  * This is a more thorough comparison than the VN_CMP() macro provides.
1715  */
1716 int
1717 vn_compare(vnode_t *vp1, vnode_t *vp2)
1718 {
1719 	vnode_t *realvp;
1720 
1721 	if (vp1 != NULL && VOP_REALVP(vp1, &realvp) == 0)
1722 		vp1 = realvp;
1723 	if (vp2 != NULL && VOP_REALVP(vp2, &realvp) == 0)
1724 		vp2 = realvp;
1725 	return (VN_CMP(vp1, vp2));
1726 }
1727 
1728 /*
1729  * The number of locks to hash into.  This value must be a power
1730  * of 2 minus 1 and should probably also be prime.
1731  */
1732 #define	NUM_BUCKETS	1023
1733 
1734 struct  vn_vfslocks_bucket {
1735 	kmutex_t vb_lock;
1736 	vn_vfslocks_entry_t *vb_list;
1737 	char pad[64 - sizeof (kmutex_t) - sizeof (void *)];
1738 };
1739 
1740 /*
1741  * Total number of buckets will be NUM_BUCKETS + 1 .
1742  */
1743 
1744 #pragma	align	64(vn_vfslocks_buckets)
1745 static	struct vn_vfslocks_bucket	vn_vfslocks_buckets[NUM_BUCKETS + 1];
1746 
1747 #define	VN_VFSLOCKS_SHIFT	9
1748 
1749 #define	VN_VFSLOCKS_HASH(vfsvpptr)	\
1750 	((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS)
1751 
1752 /*
1753  * vn_vfslocks_getlock() uses an HASH scheme to generate
1754  * rwstlock using vfs/vnode pointer passed to it.
1755  *
1756  * vn_vfslocks_rele() releases a reference in the
1757  * HASH table which allows the entry allocated by
1758  * vn_vfslocks_getlock() to be freed at a later
1759  * stage when the refcount drops to zero.
1760  */
1761 
1762 vn_vfslocks_entry_t *
1763 vn_vfslocks_getlock(void *vfsvpptr)
1764 {
1765 	struct vn_vfslocks_bucket *bp;
1766 	vn_vfslocks_entry_t *vep;
1767 	vn_vfslocks_entry_t *tvep;
1768 
1769 	ASSERT(vfsvpptr != NULL);
1770 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)];
1771 
1772 	mutex_enter(&bp->vb_lock);
1773 	for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
1774 		if (vep->ve_vpvfs == vfsvpptr) {
1775 			vep->ve_refcnt++;
1776 			mutex_exit(&bp->vb_lock);
1777 			return (vep);
1778 		}
1779 	}
1780 	mutex_exit(&bp->vb_lock);
1781 	vep = kmem_alloc(sizeof (*vep), KM_SLEEP);
1782 	rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL);
1783 	vep->ve_vpvfs = (char *)vfsvpptr;
1784 	vep->ve_refcnt = 1;
1785 	mutex_enter(&bp->vb_lock);
1786 	for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) {
1787 		if (tvep->ve_vpvfs == vfsvpptr) {
1788 			tvep->ve_refcnt++;
1789 			mutex_exit(&bp->vb_lock);
1790 
1791 			/*
1792 			 * There is already an entry in the hash
1793 			 * destroy what we just allocated.
1794 			 */
1795 			rwst_destroy(&vep->ve_lock);
1796 			kmem_free(vep, sizeof (*vep));
1797 			return (tvep);
1798 		}
1799 	}
1800 	vep->ve_next = bp->vb_list;
1801 	bp->vb_list = vep;
1802 	mutex_exit(&bp->vb_lock);
1803 	return (vep);
1804 }
1805 
1806 void
1807 vn_vfslocks_rele(vn_vfslocks_entry_t *vepent)
1808 {
1809 	struct vn_vfslocks_bucket *bp;
1810 	vn_vfslocks_entry_t *vep;
1811 	vn_vfslocks_entry_t *pvep;
1812 
1813 	ASSERT(vepent != NULL);
1814 	ASSERT(vepent->ve_vpvfs != NULL);
1815 
1816 	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)];
1817 
1818 	mutex_enter(&bp->vb_lock);
1819 	vepent->ve_refcnt--;
1820 
1821 	if ((int32_t)vepent->ve_refcnt < 0)
1822 		cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative");
1823 
1824 	if (vepent->ve_refcnt == 0) {
1825 		for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
1826 			if (vep->ve_vpvfs == vepent->ve_vpvfs) {
1827 				if (bp->vb_list == vep)
1828 					bp->vb_list = vep->ve_next;
1829 				else {
1830 					/* LINTED */
1831 					pvep->ve_next = vep->ve_next;
1832 				}
1833 				mutex_exit(&bp->vb_lock);
1834 				rwst_destroy(&vep->ve_lock);
1835 				kmem_free(vep, sizeof (*vep));
1836 				return;
1837 			}
1838 			pvep = vep;
1839 		}
1840 		cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found");
1841 	}
1842 	mutex_exit(&bp->vb_lock);
1843 }
1844 
1845 /*
1846  * vn_vfswlock_wait is used to implement a lock which is logically a writers
1847  * lock protecting the v_vfsmountedhere field.
1848  * vn_vfswlock_wait has been modified to be similar to vn_vfswlock,
1849  * except that it blocks to acquire the lock VVFSLOCK.
1850  *
1851  * traverse() and routines re-implementing part of traverse (e.g. autofs)
1852  * need to hold this lock. mount(), vn_rename(), vn_remove() and so on
1853  * need the non-blocking version of the writers lock i.e. vn_vfswlock
1854  */
1855 int
1856 vn_vfswlock_wait(vnode_t *vp)
1857 {
1858 	int retval;
1859 	vn_vfslocks_entry_t *vpvfsentry;
1860 	ASSERT(vp != NULL);
1861 
1862 	vpvfsentry = vn_vfslocks_getlock(vp);
1863 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER);
1864 
1865 	if (retval == EINTR) {
1866 		vn_vfslocks_rele(vpvfsentry);
1867 		return (EINTR);
1868 	}
1869 	return (retval);
1870 }
1871 
1872 int
1873 vn_vfsrlock_wait(vnode_t *vp)
1874 {
1875 	int retval;
1876 	vn_vfslocks_entry_t *vpvfsentry;
1877 	ASSERT(vp != NULL);
1878 
1879 	vpvfsentry = vn_vfslocks_getlock(vp);
1880 	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER);
1881 
1882 	if (retval == EINTR) {
1883 		vn_vfslocks_rele(vpvfsentry);
1884 		return (EINTR);
1885 	}
1886 
1887 	return (retval);
1888 }
1889 
1890 
1891 /*
1892  * vn_vfswlock is used to implement a lock which is logically a writers lock
1893  * protecting the v_vfsmountedhere field.
1894  */
1895 int
1896 vn_vfswlock(vnode_t *vp)
1897 {
1898 	vn_vfslocks_entry_t *vpvfsentry;
1899 
1900 	/*
1901 	 * If vp is NULL then somebody is trying to lock the covered vnode
1902 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
1903 	 * only happen when unmounting /.  Since that operation will fail
1904 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
1905 	 */
1906 	if (vp == NULL)
1907 		return (EBUSY);
1908 
1909 	vpvfsentry = vn_vfslocks_getlock(vp);
1910 
1911 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
1912 		return (0);
1913 
1914 	vn_vfslocks_rele(vpvfsentry);
1915 	return (EBUSY);
1916 }
1917 
1918 int
1919 vn_vfsrlock(vnode_t *vp)
1920 {
1921 	vn_vfslocks_entry_t *vpvfsentry;
1922 
1923 	/*
1924 	 * If vp is NULL then somebody is trying to lock the covered vnode
1925 	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
1926 	 * only happen when unmounting /.  Since that operation will fail
1927 	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
1928 	 */
1929 	if (vp == NULL)
1930 		return (EBUSY);
1931 
1932 	vpvfsentry = vn_vfslocks_getlock(vp);
1933 
1934 	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
1935 		return (0);
1936 
1937 	vn_vfslocks_rele(vpvfsentry);
1938 	return (EBUSY);
1939 }
1940 
1941 void
1942 vn_vfsunlock(vnode_t *vp)
1943 {
1944 	vn_vfslocks_entry_t *vpvfsentry;
1945 
1946 	/*
1947 	 * ve_refcnt needs to be decremented twice.
1948 	 * 1. To release refernce after a call to vn_vfslocks_getlock()
1949 	 * 2. To release the reference from the locking routines like
1950 	 *    vn_vfsrlock/vn_vfswlock etc,.
1951 	 */
1952 	vpvfsentry = vn_vfslocks_getlock(vp);
1953 	vn_vfslocks_rele(vpvfsentry);
1954 
1955 	rwst_exit(&vpvfsentry->ve_lock);
1956 	vn_vfslocks_rele(vpvfsentry);
1957 }
1958 
1959 int
1960 vn_vfswlock_held(vnode_t *vp)
1961 {
1962 	int held;
1963 	vn_vfslocks_entry_t *vpvfsentry;
1964 
1965 	ASSERT(vp != NULL);
1966 
1967 	vpvfsentry = vn_vfslocks_getlock(vp);
1968 	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
1969 
1970 	vn_vfslocks_rele(vpvfsentry);
1971 	return (held);
1972 }
1973 
1974 
1975 int
1976 vn_make_ops(
1977 	const char *name,			/* Name of file system */
1978 	const fs_operation_def_t *templ,	/* Operation specification */
1979 	vnodeops_t **actual)			/* Return the vnodeops */
1980 {
1981 	int unused_ops;
1982 	int error;
1983 
1984 	*actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP);
1985 
1986 	(*actual)->vnop_name = name;
1987 
1988 	error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ);
1989 	if (error) {
1990 		kmem_free(*actual, sizeof (vnodeops_t));
1991 	}
1992 
1993 #if DEBUG
1994 	if (unused_ops != 0)
1995 		cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied "
1996 		    "but not used", name, unused_ops);
1997 #endif
1998 
1999 	return (error);
2000 }
2001 
2002 /*
2003  * Free the vnodeops created as a result of vn_make_ops()
2004  */
2005 void
2006 vn_freevnodeops(vnodeops_t *vnops)
2007 {
2008 	kmem_free(vnops, sizeof (vnodeops_t));
2009 }
2010 
2011 /*
2012  * Vnode cache.
2013  */
2014 
2015 /* ARGSUSED */
2016 static int
2017 vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
2018 {
2019 	struct vnode *vp;
2020 
2021 	vp = buf;
2022 
2023 	mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
2024 	cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL);
2025 	rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL);
2026 	rw_init(&vp->v_mslock, NULL, RW_DEFAULT, NULL);
2027 	vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2028 	vp->v_path = NULL;
2029 	vp->v_mpssdata = NULL;
2030 	vp->v_fopdata = NULL;
2031 
2032 	return (0);
2033 }
2034 
2035 /* ARGSUSED */
2036 static void
2037 vn_cache_destructor(void *buf, void *cdrarg)
2038 {
2039 	struct vnode *vp;
2040 
2041 	vp = buf;
2042 
2043 	rw_destroy(&vp->v_mslock);
2044 	rw_destroy(&vp->v_nbllock);
2045 	cv_destroy(&vp->v_cv);
2046 	mutex_destroy(&vp->v_lock);
2047 }
2048 
2049 void
2050 vn_create_cache(void)
2051 {
2052 	vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode), 64,
2053 	    vn_cache_constructor, vn_cache_destructor, NULL, NULL,
2054 	    NULL, 0);
2055 }
2056 
2057 void
2058 vn_destroy_cache(void)
2059 {
2060 	kmem_cache_destroy(vn_cache);
2061 }
2062 
2063 /*
2064  * Used by file systems when fs-specific nodes (e.g., ufs inodes) are
2065  * cached by the file system and vnodes remain associated.
2066  */
2067 void
2068 vn_recycle(vnode_t *vp)
2069 {
2070 	ASSERT(vp->v_pages == NULL);
2071 
2072 	/*
2073 	 * XXX - This really belongs in vn_reinit(), but we have some issues
2074 	 * with the counts.  Best to have it here for clean initialization.
2075 	 */
2076 	vp->v_rdcnt = 0;
2077 	vp->v_wrcnt = 0;
2078 	vp->v_mmap_read = 0;
2079 	vp->v_mmap_write = 0;
2080 
2081 	/*
2082 	 * If FEM was in use, make sure everything gets cleaned up
2083 	 * NOTE: vp->v_femhead is initialized to NULL in the vnode
2084 	 * constructor.
2085 	 */
2086 	if (vp->v_femhead) {
2087 		/* XXX - There should be a free_femhead() that does all this */
2088 		ASSERT(vp->v_femhead->femh_list == NULL);
2089 		mutex_destroy(&vp->v_femhead->femh_lock);
2090 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2091 		vp->v_femhead = NULL;
2092 	}
2093 	if (vp->v_path) {
2094 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2095 		vp->v_path = NULL;
2096 	}
2097 
2098 	if (vp->v_fopdata != NULL) {
2099 		free_fopdata(vp);
2100 	}
2101 	vp->v_mpssdata = NULL;
2102 }
2103 
2104 /*
2105  * Used to reset the vnode fields including those that are directly accessible
2106  * as well as those which require an accessor function.
2107  *
2108  * Does not initialize:
2109  *	synchronization objects: v_lock, v_nbllock, v_cv
2110  *	v_data (since FS-nodes and vnodes point to each other and should
2111  *		be updated simultaneously)
2112  *	v_op (in case someone needs to make a VOP call on this object)
2113  */
2114 void
2115 vn_reinit(vnode_t *vp)
2116 {
2117 	vp->v_count = 1;
2118 	vp->v_vfsp = NULL;
2119 	vp->v_stream = NULL;
2120 	vp->v_vfsmountedhere = NULL;
2121 	vp->v_flag = 0;
2122 	vp->v_type = VNON;
2123 	vp->v_rdev = NODEV;
2124 
2125 	vp->v_filocks = NULL;
2126 	vp->v_shrlocks = NULL;
2127 	vp->v_pages = NULL;
2128 	vp->v_npages = 0;
2129 	vp->v_msnpages = 0;
2130 	vp->v_scanfront = NULL;
2131 	vp->v_scanback = NULL;
2132 
2133 	vp->v_locality = NULL;
2134 	vp->v_scantime = 0;
2135 	vp->v_mset = 0;
2136 	vp->v_msflags = 0;
2137 	vp->v_msnext = NULL;
2138 	vp->v_msprev = NULL;
2139 
2140 	/* Handles v_femhead, v_path, and the r/w/map counts */
2141 	vn_recycle(vp);
2142 }
2143 
2144 vnode_t *
2145 vn_alloc(int kmflag)
2146 {
2147 	vnode_t *vp;
2148 
2149 	vp = kmem_cache_alloc(vn_cache, kmflag);
2150 
2151 	if (vp != NULL) {
2152 		vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
2153 		vp->v_fopdata = NULL;
2154 		vn_reinit(vp);
2155 	}
2156 
2157 	return (vp);
2158 }
2159 
2160 void
2161 vn_free(vnode_t *vp)
2162 {
2163 	/*
2164 	 * Some file systems call vn_free() with v_count of zero,
2165 	 * some with v_count of 1.  In any case, the value should
2166 	 * never be anything else.
2167 	 */
2168 	ASSERT((vp->v_count == 0) || (vp->v_count == 1));
2169 	if (vp->v_path != NULL) {
2170 		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2171 		vp->v_path = NULL;
2172 	}
2173 
2174 	/* If FEM was in use, make sure everything gets cleaned up */
2175 	if (vp->v_femhead) {
2176 		/* XXX - There should be a free_femhead() that does all this */
2177 		ASSERT(vp->v_femhead->femh_list == NULL);
2178 		mutex_destroy(&vp->v_femhead->femh_lock);
2179 		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2180 		vp->v_femhead = NULL;
2181 	}
2182 
2183 	if (vp->v_fopdata != NULL) {
2184 		free_fopdata(vp);
2185 	}
2186 	vp->v_mpssdata = NULL;
2187 	kmem_cache_free(vn_cache, vp);
2188 }
2189 
2190 /*
2191  * vnode status changes, should define better states than 1, 0.
2192  */
2193 void
2194 vn_reclaim(vnode_t *vp)
2195 {
2196 	vfs_t   *vfsp = vp->v_vfsp;
2197 
2198 	if (vfsp == NULL ||
2199 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2200 		return;
2201 	}
2202 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED);
2203 }
2204 
2205 void
2206 vn_idle(vnode_t *vp)
2207 {
2208 	vfs_t   *vfsp = vp->v_vfsp;
2209 
2210 	if (vfsp == NULL ||
2211 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2212 		return;
2213 	}
2214 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_IDLED);
2215 }
2216 void
2217 vn_exists(vnode_t *vp)
2218 {
2219 	vfs_t   *vfsp = vp->v_vfsp;
2220 
2221 	if (vfsp == NULL ||
2222 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2223 		return;
2224 	}
2225 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS);
2226 }
2227 
2228 void
2229 vn_invalid(vnode_t *vp)
2230 {
2231 	vfs_t   *vfsp = vp->v_vfsp;
2232 
2233 	if (vfsp == NULL ||
2234 	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2235 		return;
2236 	}
2237 	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED);
2238 }
2239 
2240 /* Vnode event notification */
2241 
2242 int
2243 vnevent_support(vnode_t *vp)
2244 {
2245 	if (vp == NULL)
2246 		return (EINVAL);
2247 
2248 	return (VOP_VNEVENT(vp, VE_SUPPORT, NULL, NULL));
2249 }
2250 
2251 void
2252 vnevent_rename_src(vnode_t *vp, vnode_t *dvp, char *name)
2253 {
2254 	if (vp == NULL || vp->v_femhead == NULL) {
2255 		return;
2256 	}
2257 	(void) VOP_VNEVENT(vp, VE_RENAME_SRC, dvp, name);
2258 }
2259 
2260 void
2261 vnevent_rename_dest(vnode_t *vp, vnode_t *dvp, char *name)
2262 {
2263 	if (vp == NULL || vp->v_femhead == NULL) {
2264 		return;
2265 	}
2266 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST, dvp, name);
2267 }
2268 
2269 void
2270 vnevent_rename_dest_dir(vnode_t *vp)
2271 {
2272 	if (vp == NULL || vp->v_femhead == NULL) {
2273 		return;
2274 	}
2275 	(void) VOP_VNEVENT(vp, VE_RENAME_DEST_DIR, NULL, NULL);
2276 }
2277 
2278 void
2279 vnevent_remove(vnode_t *vp, vnode_t *dvp, char *name)
2280 {
2281 	if (vp == NULL || vp->v_femhead == NULL) {
2282 		return;
2283 	}
2284 	(void) VOP_VNEVENT(vp, VE_REMOVE, dvp, name);
2285 }
2286 
2287 void
2288 vnevent_rmdir(vnode_t *vp, vnode_t *dvp, char *name)
2289 {
2290 	if (vp == NULL || vp->v_femhead == NULL) {
2291 		return;
2292 	}
2293 	(void) VOP_VNEVENT(vp, VE_RMDIR, dvp, name);
2294 }
2295 
2296 void
2297 vnevent_create(vnode_t *vp)
2298 {
2299 	if (vp == NULL || vp->v_femhead == NULL) {
2300 		return;
2301 	}
2302 	(void) VOP_VNEVENT(vp, VE_CREATE, NULL, NULL);
2303 }
2304 
2305 void
2306 vnevent_link(vnode_t *vp)
2307 {
2308 	if (vp == NULL || vp->v_femhead == NULL) {
2309 		return;
2310 	}
2311 	(void) VOP_VNEVENT(vp, VE_LINK, NULL, NULL);
2312 }
2313 
2314 void
2315 vnevent_mountedover(vnode_t *vp)
2316 {
2317 	if (vp == NULL || vp->v_femhead == NULL) {
2318 		return;
2319 	}
2320 	(void) VOP_VNEVENT(vp, VE_MOUNTEDOVER, NULL, NULL);
2321 }
2322 
2323 /*
2324  * Vnode accessors.
2325  */
2326 
2327 int
2328 vn_is_readonly(vnode_t *vp)
2329 {
2330 	return (vp->v_vfsp->vfs_flag & VFS_RDONLY);
2331 }
2332 
2333 int
2334 vn_has_flocks(vnode_t *vp)
2335 {
2336 	return (vp->v_filocks != NULL);
2337 }
2338 
2339 int
2340 vn_has_mandatory_locks(vnode_t *vp, int mode)
2341 {
2342 	return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode)));
2343 }
2344 
2345 int
2346 vn_has_cached_data(vnode_t *vp)
2347 {
2348 	return (vp->v_pages != NULL);
2349 }
2350 
2351 /*
2352  * Return 0 if the vnode in question shouldn't be permitted into a zone via
2353  * zone_enter(2).
2354  */
2355 int
2356 vn_can_change_zones(vnode_t *vp)
2357 {
2358 	struct vfssw *vswp;
2359 	int allow = 1;
2360 	vnode_t *rvp;
2361 
2362 	if (nfs_global_client_only != 0)
2363 		return (1);
2364 
2365 	/*
2366 	 * We always want to look at the underlying vnode if there is one.
2367 	 */
2368 	if (VOP_REALVP(vp, &rvp) != 0)
2369 		rvp = vp;
2370 	/*
2371 	 * Some pseudo filesystems (including doorfs) don't actually register
2372 	 * their vfsops_t, so the following may return NULL; we happily let
2373 	 * such vnodes switch zones.
2374 	 */
2375 	vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp));
2376 	if (vswp != NULL) {
2377 		if (vswp->vsw_flag & VSW_NOTZONESAFE)
2378 			allow = 0;
2379 		vfs_unrefvfssw(vswp);
2380 	}
2381 	return (allow);
2382 }
2383 
2384 /*
2385  * Return nonzero if the vnode is a mount point, zero if not.
2386  */
2387 int
2388 vn_ismntpt(vnode_t *vp)
2389 {
2390 	return (vp->v_vfsmountedhere != NULL);
2391 }
2392 
2393 /* Retrieve the vfs (if any) mounted on this vnode */
2394 vfs_t *
2395 vn_mountedvfs(vnode_t *vp)
2396 {
2397 	return (vp->v_vfsmountedhere);
2398 }
2399 
2400 /*
2401  * vn_is_opened() checks whether a particular file is opened and
2402  * whether the open is for read and/or write.
2403  *
2404  * Vnode counts are only kept on regular files (v_type=VREG).
2405  */
2406 int
2407 vn_is_opened(
2408 	vnode_t *vp,
2409 	v_mode_t mode)
2410 {
2411 
2412 	ASSERT(vp != NULL);
2413 
2414 	switch (mode) {
2415 	case V_WRITE:
2416 		if (vp->v_wrcnt)
2417 			return (V_TRUE);
2418 		break;
2419 	case V_RDANDWR:
2420 		if (vp->v_rdcnt && vp->v_wrcnt)
2421 			return (V_TRUE);
2422 		break;
2423 	case V_RDORWR:
2424 		if (vp->v_rdcnt || vp->v_wrcnt)
2425 			return (V_TRUE);
2426 		break;
2427 	case V_READ:
2428 		if (vp->v_rdcnt)
2429 			return (V_TRUE);
2430 		break;
2431 	}
2432 
2433 	return (V_FALSE);
2434 }
2435 
2436 /*
2437  * vn_is_mapped() checks whether a particular file is mapped and whether
2438  * the file is mapped read and/or write.
2439  */
2440 int
2441 vn_is_mapped(
2442 	vnode_t *vp,
2443 	v_mode_t mode)
2444 {
2445 
2446 	ASSERT(vp != NULL);
2447 
2448 #if !defined(_LP64)
2449 	switch (mode) {
2450 	/*
2451 	 * The atomic_add_64_nv functions force atomicity in the
2452 	 * case of 32 bit architectures. Otherwise the 64 bit values
2453 	 * require two fetches. The value of the fields may be
2454 	 * (potentially) changed between the first fetch and the
2455 	 * second
2456 	 */
2457 	case V_WRITE:
2458 		if (atomic_add_64_nv((&(vp->v_mmap_write)), 0))
2459 			return (V_TRUE);
2460 		break;
2461 	case V_RDANDWR:
2462 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) &&
2463 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2464 			return (V_TRUE);
2465 		break;
2466 	case V_RDORWR:
2467 		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) ||
2468 		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2469 			return (V_TRUE);
2470 		break;
2471 	case V_READ:
2472 		if (atomic_add_64_nv((&(vp->v_mmap_read)), 0))
2473 			return (V_TRUE);
2474 		break;
2475 	}
2476 #else
2477 	switch (mode) {
2478 	case V_WRITE:
2479 		if (vp->v_mmap_write)
2480 			return (V_TRUE);
2481 		break;
2482 	case V_RDANDWR:
2483 		if (vp->v_mmap_read && vp->v_mmap_write)
2484 			return (V_TRUE);
2485 		break;
2486 	case V_RDORWR:
2487 		if (vp->v_mmap_read || vp->v_mmap_write)
2488 			return (V_TRUE);
2489 		break;
2490 	case V_READ:
2491 		if (vp->v_mmap_read)
2492 			return (V_TRUE);
2493 		break;
2494 	}
2495 #endif
2496 
2497 	return (V_FALSE);
2498 }
2499 
2500 /*
2501  * Set the operations vector for a vnode.
2502  *
2503  * FEM ensures that the v_femhead pointer is filled in before the
2504  * v_op pointer is changed.  This means that if the v_femhead pointer
2505  * is NULL, and the v_op field hasn't changed since before which checked
2506  * the v_femhead pointer; then our update is ok - we are not racing with
2507  * FEM.
2508  */
2509 void
2510 vn_setops(vnode_t *vp, vnodeops_t *vnodeops)
2511 {
2512 	vnodeops_t	*op;
2513 
2514 	ASSERT(vp != NULL);
2515 	ASSERT(vnodeops != NULL);
2516 
2517 	op = vp->v_op;
2518 	membar_consumer();
2519 	/*
2520 	 * If vp->v_femhead == NULL, then we'll call casptr() to do the
2521 	 * compare-and-swap on vp->v_op.  If either fails, then FEM is
2522 	 * in effect on the vnode and we need to have FEM deal with it.
2523 	 */
2524 	if (vp->v_femhead != NULL || casptr(&vp->v_op, op, vnodeops) != op) {
2525 		fem_setvnops(vp, vnodeops);
2526 	}
2527 }
2528 
2529 /*
2530  * Retrieve the operations vector for a vnode
2531  * As with vn_setops(above); make sure we aren't racing with FEM.
2532  * FEM sets the v_op to a special, internal, vnodeops that wouldn't
2533  * make sense to the callers of this routine.
2534  */
2535 vnodeops_t *
2536 vn_getops(vnode_t *vp)
2537 {
2538 	vnodeops_t	*op;
2539 
2540 	ASSERT(vp != NULL);
2541 
2542 	op = vp->v_op;
2543 	membar_consumer();
2544 	if (vp->v_femhead == NULL && op == vp->v_op) {
2545 		return (op);
2546 	} else {
2547 		return (fem_getvnops(vp));
2548 	}
2549 }
2550 
2551 /*
2552  * Returns non-zero (1) if the vnodeops matches that of the vnode.
2553  * Returns zero (0) if not.
2554  */
2555 int
2556 vn_matchops(vnode_t *vp, vnodeops_t *vnodeops)
2557 {
2558 	return (vn_getops(vp) == vnodeops);
2559 }
2560 
2561 /*
2562  * Returns non-zero (1) if the specified operation matches the
2563  * corresponding operation for that the vnode.
2564  * Returns zero (0) if not.
2565  */
2566 
2567 #define	MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0))
2568 
2569 int
2570 vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp)
2571 {
2572 	const fs_operation_trans_def_t *otdp;
2573 	fs_generic_func_p *loc = NULL;
2574 	vnodeops_t	*vop = vn_getops(vp);
2575 
2576 	ASSERT(vopname != NULL);
2577 
2578 	for (otdp = vn_ops_table; otdp->name != NULL; otdp++) {
2579 		if (MATCHNAME(otdp->name, vopname)) {
2580 			loc = (fs_generic_func_p *)
2581 			    ((char *)(vop) + otdp->offset);
2582 			break;
2583 		}
2584 	}
2585 
2586 	return ((loc != NULL) && (*loc == funcp));
2587 }
2588 
2589 /*
2590  * fs_new_caller_id() needs to return a unique ID on a given local system.
2591  * The IDs do not need to survive across reboots.  These are primarily
2592  * used so that (FEM) monitors can detect particular callers (such as
2593  * the NFS server) to a given vnode/vfs operation.
2594  */
2595 u_longlong_t
2596 fs_new_caller_id()
2597 {
2598 	static uint64_t next_caller_id = 0LL; /* First call returns 1 */
2599 
2600 	return ((u_longlong_t)atomic_add_64_nv(&next_caller_id, 1));
2601 }
2602 
2603 /*
2604  * Given a starting vnode and a path, updates the path in the target vnode in
2605  * a safe manner.  If the vnode already has path information embedded, then the
2606  * cached path is left untouched.
2607  */
2608 
2609 size_t max_vnode_path = 4 * MAXPATHLEN;
2610 
2611 void
2612 vn_setpath(vnode_t *rootvp, struct vnode *startvp, struct vnode *vp,
2613     const char *path, size_t plen)
2614 {
2615 	char	*rpath;
2616 	vnode_t	*base;
2617 	size_t	rpathlen, rpathalloc;
2618 	int	doslash = 1;
2619 
2620 	if (*path == '/') {
2621 		base = rootvp;
2622 		path++;
2623 		plen--;
2624 	} else {
2625 		base = startvp;
2626 	}
2627 
2628 	/*
2629 	 * We cannot grab base->v_lock while we hold vp->v_lock because of
2630 	 * the potential for deadlock.
2631 	 */
2632 	mutex_enter(&base->v_lock);
2633 	if (base->v_path == NULL) {
2634 		mutex_exit(&base->v_lock);
2635 		return;
2636 	}
2637 
2638 	rpathlen = strlen(base->v_path);
2639 	rpathalloc = rpathlen + plen + 1;
2640 	/* Avoid adding a slash if there's already one there */
2641 	if (base->v_path[rpathlen-1] == '/')
2642 		doslash = 0;
2643 	else
2644 		rpathalloc++;
2645 
2646 	/*
2647 	 * We don't want to call kmem_alloc(KM_SLEEP) with kernel locks held,
2648 	 * so we must do this dance.  If, by chance, something changes the path,
2649 	 * just give up since there is no real harm.
2650 	 */
2651 	mutex_exit(&base->v_lock);
2652 
2653 	/* Paths should stay within reason */
2654 	if (rpathalloc > max_vnode_path)
2655 		return;
2656 
2657 	rpath = kmem_alloc(rpathalloc, KM_SLEEP);
2658 
2659 	mutex_enter(&base->v_lock);
2660 	if (base->v_path == NULL || strlen(base->v_path) != rpathlen) {
2661 		mutex_exit(&base->v_lock);
2662 		kmem_free(rpath, rpathalloc);
2663 		return;
2664 	}
2665 	bcopy(base->v_path, rpath, rpathlen);
2666 	mutex_exit(&base->v_lock);
2667 
2668 	if (doslash)
2669 		rpath[rpathlen++] = '/';
2670 	bcopy(path, rpath + rpathlen, plen);
2671 	rpath[rpathlen + plen] = '\0';
2672 
2673 	mutex_enter(&vp->v_lock);
2674 	if (vp->v_path != NULL) {
2675 		mutex_exit(&vp->v_lock);
2676 		kmem_free(rpath, rpathalloc);
2677 	} else {
2678 		vp->v_path = rpath;
2679 		mutex_exit(&vp->v_lock);
2680 	}
2681 }
2682 
2683 /*
2684  * Sets the path to the vnode to be the given string, regardless of current
2685  * context.  The string must be a complete path from rootdir.  This is only used
2686  * by fsop_root() for setting the path based on the mountpoint.
2687  */
2688 void
2689 vn_setpath_str(struct vnode *vp, const char *str, size_t len)
2690 {
2691 	char *buf = kmem_alloc(len + 1, KM_SLEEP);
2692 
2693 	mutex_enter(&vp->v_lock);
2694 	if (vp->v_path != NULL) {
2695 		mutex_exit(&vp->v_lock);
2696 		kmem_free(buf, len + 1);
2697 		return;
2698 	}
2699 
2700 	vp->v_path = buf;
2701 	bcopy(str, vp->v_path, len);
2702 	vp->v_path[len] = '\0';
2703 
2704 	mutex_exit(&vp->v_lock);
2705 }
2706 
2707 /*
2708  * Similar to vn_setpath_str(), this function sets the path of the destination
2709  * vnode to the be the same as the source vnode.
2710  */
2711 void
2712 vn_copypath(struct vnode *src, struct vnode *dst)
2713 {
2714 	char *buf;
2715 	int alloc;
2716 
2717 	mutex_enter(&src->v_lock);
2718 	if (src->v_path == NULL) {
2719 		mutex_exit(&src->v_lock);
2720 		return;
2721 	}
2722 	alloc = strlen(src->v_path) + 1;
2723 
2724 	/* avoid kmem_alloc() with lock held */
2725 	mutex_exit(&src->v_lock);
2726 	buf = kmem_alloc(alloc, KM_SLEEP);
2727 	mutex_enter(&src->v_lock);
2728 	if (src->v_path == NULL || strlen(src->v_path) + 1 != alloc) {
2729 		mutex_exit(&src->v_lock);
2730 		kmem_free(buf, alloc);
2731 		return;
2732 	}
2733 	bcopy(src->v_path, buf, alloc);
2734 	mutex_exit(&src->v_lock);
2735 
2736 	mutex_enter(&dst->v_lock);
2737 	if (dst->v_path != NULL) {
2738 		mutex_exit(&dst->v_lock);
2739 		kmem_free(buf, alloc);
2740 		return;
2741 	}
2742 	dst->v_path = buf;
2743 	mutex_exit(&dst->v_lock);
2744 }
2745 
2746 /*
2747  * XXX Private interface for segvn routines that handle vnode
2748  * large page segments.
2749  *
2750  * return 1 if vp's file system VOP_PAGEIO() implementation
2751  * can be safely used instead of VOP_GETPAGE() for handling
2752  * pagefaults against regular non swap files. VOP_PAGEIO()
2753  * interface is considered safe here if its implementation
2754  * is very close to VOP_GETPAGE() implementation.
2755  * e.g. It zero's out the part of the page beyond EOF. Doesn't
2756  * panic if there're file holes but instead returns an error.
2757  * Doesn't assume file won't be changed by user writes, etc.
2758  *
2759  * return 0 otherwise.
2760  *
2761  * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs.
2762  */
2763 int
2764 vn_vmpss_usepageio(vnode_t *vp)
2765 {
2766 	vfs_t   *vfsp = vp->v_vfsp;
2767 	char *fsname = vfssw[vfsp->vfs_fstype].vsw_name;
2768 	char *pageio_ok_fss[] = {"ufs", "nfs", NULL};
2769 	char **fsok = pageio_ok_fss;
2770 
2771 	if (fsname == NULL) {
2772 		return (0);
2773 	}
2774 
2775 	for (; *fsok; fsok++) {
2776 		if (strcmp(*fsok, fsname) == 0) {
2777 			return (1);
2778 		}
2779 	}
2780 	return (0);
2781 }
2782 
2783 /* VOP_XXX() macros call the corresponding fop_xxx() function */
2784 
2785 int
2786 fop_open(
2787 	vnode_t **vpp,
2788 	int mode,
2789 	cred_t *cr)
2790 {
2791 	int ret;
2792 	vnode_t *vp = *vpp;
2793 
2794 	VN_HOLD(vp);
2795 	/*
2796 	 * Adding to the vnode counts before calling open
2797 	 * avoids the need for a mutex. It circumvents a race
2798 	 * condition where a query made on the vnode counts results in a
2799 	 * false negative. The inquirer goes away believing the file is
2800 	 * not open when there is an open on the file already under way.
2801 	 *
2802 	 * The counts are meant to prevent NFS from granting a delegation
2803 	 * when it would be dangerous to do so.
2804 	 *
2805 	 * The vnode counts are only kept on regular files
2806 	 */
2807 	if ((*vpp)->v_type == VREG) {
2808 		if (mode & FREAD)
2809 			atomic_add_32(&((*vpp)->v_rdcnt), 1);
2810 		if (mode & FWRITE)
2811 			atomic_add_32(&((*vpp)->v_wrcnt), 1);
2812 	}
2813 
2814 	VOPXID_MAP_CR(vp, cr);
2815 
2816 	ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr);
2817 
2818 	if (ret) {
2819 		/*
2820 		 * Use the saved vp just in case the vnode ptr got trashed
2821 		 * by the error.
2822 		 */
2823 		VOPSTATS_UPDATE(vp, open);
2824 		if ((vp->v_type == VREG) && (mode & FREAD))
2825 			atomic_add_32(&(vp->v_rdcnt), -1);
2826 		if ((vp->v_type == VREG) && (mode & FWRITE))
2827 			atomic_add_32(&(vp->v_wrcnt), -1);
2828 	} else {
2829 		/*
2830 		 * Some filesystems will return a different vnode,
2831 		 * but the same path was still used to open it.
2832 		 * So if we do change the vnode and need to
2833 		 * copy over the path, do so here, rather than special
2834 		 * casing each filesystem. Adjust the vnode counts to
2835 		 * reflect the vnode switch.
2836 		 */
2837 		VOPSTATS_UPDATE(*vpp, open);
2838 		if (*vpp != vp && *vpp != NULL) {
2839 			vn_copypath(vp, *vpp);
2840 			if (((*vpp)->v_type == VREG) && (mode & FREAD))
2841 				atomic_add_32(&((*vpp)->v_rdcnt), 1);
2842 			if ((vp->v_type == VREG) && (mode & FREAD))
2843 				atomic_add_32(&(vp->v_rdcnt), -1);
2844 			if (((*vpp)->v_type == VREG) && (mode & FWRITE))
2845 				atomic_add_32(&((*vpp)->v_wrcnt), 1);
2846 			if ((vp->v_type == VREG) && (mode & FWRITE))
2847 				atomic_add_32(&(vp->v_wrcnt), -1);
2848 		}
2849 	}
2850 	VN_RELE(vp);
2851 	return (ret);
2852 }
2853 
2854 int
2855 fop_close(
2856 	vnode_t *vp,
2857 	int flag,
2858 	int count,
2859 	offset_t offset,
2860 	cred_t *cr)
2861 {
2862 	int err;
2863 
2864 	VOPXID_MAP_CR(vp, cr);
2865 
2866 	err = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr);
2867 	VOPSTATS_UPDATE(vp, close);
2868 	/*
2869 	 * Check passed in count to handle possible dups. Vnode counts are only
2870 	 * kept on regular files
2871 	 */
2872 	if ((vp->v_type == VREG) && (count == 1))  {
2873 		if (flag & FREAD) {
2874 			ASSERT(vp->v_rdcnt > 0);
2875 			atomic_add_32(&(vp->v_rdcnt), -1);
2876 		}
2877 		if (flag & FWRITE) {
2878 			ASSERT(vp->v_wrcnt > 0);
2879 			atomic_add_32(&(vp->v_wrcnt), -1);
2880 		}
2881 	}
2882 	return (err);
2883 }
2884 
2885 int
2886 fop_read(
2887 	vnode_t *vp,
2888 	uio_t *uiop,
2889 	int ioflag,
2890 	cred_t *cr,
2891 	struct caller_context *ct)
2892 {
2893 	int	err;
2894 	ssize_t	resid_start = uiop->uio_resid;
2895 
2896 	VOPXID_MAP_CR(vp, cr);
2897 
2898 	err = (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct);
2899 	VOPSTATS_UPDATE_IO(vp, read,
2900 	    read_bytes, (resid_start - uiop->uio_resid));
2901 	return (err);
2902 }
2903 
2904 int
2905 fop_write(
2906 	vnode_t *vp,
2907 	uio_t *uiop,
2908 	int ioflag,
2909 	cred_t *cr,
2910 	struct caller_context *ct)
2911 {
2912 	int	err;
2913 	ssize_t	resid_start = uiop->uio_resid;
2914 
2915 	VOPXID_MAP_CR(vp, cr);
2916 
2917 	err = (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct);
2918 	VOPSTATS_UPDATE_IO(vp, write,
2919 	    write_bytes, (resid_start - uiop->uio_resid));
2920 	return (err);
2921 }
2922 
2923 int
2924 fop_ioctl(
2925 	vnode_t *vp,
2926 	int cmd,
2927 	intptr_t arg,
2928 	int flag,
2929 	cred_t *cr,
2930 	int *rvalp)
2931 {
2932 	int	err;
2933 
2934 	VOPXID_MAP_CR(vp, cr);
2935 
2936 	err = (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp);
2937 	VOPSTATS_UPDATE(vp, ioctl);
2938 	return (err);
2939 }
2940 
2941 int
2942 fop_setfl(
2943 	vnode_t *vp,
2944 	int oflags,
2945 	int nflags,
2946 	cred_t *cr)
2947 {
2948 	int	err;
2949 
2950 	VOPXID_MAP_CR(vp, cr);
2951 
2952 	err = (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr);
2953 	VOPSTATS_UPDATE(vp, setfl);
2954 	return (err);
2955 }
2956 
2957 int
2958 fop_getattr(
2959 	vnode_t *vp,
2960 	vattr_t *vap,
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_getattr)(vp, vap, flags, cr);
2969 	VOPSTATS_UPDATE(vp, getattr);
2970 	return (err);
2971 }
2972 
2973 int
2974 fop_setattr(
2975 	vnode_t *vp,
2976 	vattr_t *vap,
2977 	int flags,
2978 	cred_t *cr,
2979 	caller_context_t *ct)
2980 {
2981 	int	err;
2982 
2983 	VOPXID_MAP_CR(vp, cr);
2984 
2985 	err = (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct);
2986 	VOPSTATS_UPDATE(vp, setattr);
2987 	return (err);
2988 }
2989 
2990 int
2991 fop_access(
2992 	vnode_t *vp,
2993 	int mode,
2994 	int flags,
2995 	cred_t *cr)
2996 {
2997 	int	err;
2998 
2999 	VOPXID_MAP_CR(vp, cr);
3000 
3001 	err = (*(vp)->v_op->vop_access)(vp, mode, flags, cr);
3002 	VOPSTATS_UPDATE(vp, access);
3003 	return (err);
3004 }
3005 
3006 int
3007 fop_lookup(
3008 	vnode_t *dvp,
3009 	char *nm,
3010 	vnode_t **vpp,
3011 	pathname_t *pnp,
3012 	int flags,
3013 	vnode_t *rdir,
3014 	cred_t *cr)
3015 {
3016 	int ret;
3017 
3018 	VOPXID_MAP_CR(dvp, cr);
3019 
3020 	ret = (*(dvp)->v_op->vop_lookup)(dvp, nm, vpp, pnp, flags, rdir, cr);
3021 	if (ret == 0 && *vpp) {
3022 		VOPSTATS_UPDATE(*vpp, lookup);
3023 		if ((*vpp)->v_path == NULL) {
3024 			vn_setpath(rootdir, dvp, *vpp, nm, strlen(nm));
3025 		}
3026 	}
3027 
3028 	return (ret);
3029 }
3030 
3031 int
3032 fop_create(
3033 	vnode_t *dvp,
3034 	char *name,
3035 	vattr_t *vap,
3036 	vcexcl_t excl,
3037 	int mode,
3038 	vnode_t **vpp,
3039 	cred_t *cr,
3040 	int flag)
3041 {
3042 	int ret;
3043 
3044 	VOPXID_MAP_CR(dvp, cr);
3045 
3046 	ret = (*(dvp)->v_op->vop_create)
3047 	    (dvp, name, vap, excl, mode, vpp, cr, flag);
3048 	if (ret == 0 && *vpp) {
3049 		VOPSTATS_UPDATE(*vpp, create);
3050 		if ((*vpp)->v_path == NULL) {
3051 			vn_setpath(rootdir, dvp, *vpp, name, strlen(name));
3052 		}
3053 	}
3054 
3055 	return (ret);
3056 }
3057 
3058 int
3059 fop_remove(
3060 	vnode_t *dvp,
3061 	char *nm,
3062 	cred_t *cr)
3063 {
3064 	int	err;
3065 
3066 	VOPXID_MAP_CR(dvp, cr);
3067 
3068 	err = (*(dvp)->v_op->vop_remove)(dvp, nm, cr);
3069 	VOPSTATS_UPDATE(dvp, remove);
3070 	return (err);
3071 }
3072 
3073 int
3074 fop_link(
3075 	vnode_t *tdvp,
3076 	vnode_t *svp,
3077 	char *tnm,
3078 	cred_t *cr)
3079 {
3080 	int	err;
3081 
3082 	VOPXID_MAP_CR(tdvp, cr);
3083 
3084 	err = (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr);
3085 	VOPSTATS_UPDATE(tdvp, link);
3086 	return (err);
3087 }
3088 
3089 int
3090 fop_rename(
3091 	vnode_t *sdvp,
3092 	char *snm,
3093 	vnode_t *tdvp,
3094 	char *tnm,
3095 	cred_t *cr)
3096 {
3097 	int	err;
3098 
3099 	VOPXID_MAP_CR(tdvp, cr);
3100 
3101 	err = (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr);
3102 	VOPSTATS_UPDATE(sdvp, rename);
3103 	return (err);
3104 }
3105 
3106 int
3107 fop_mkdir(
3108 	vnode_t *dvp,
3109 	char *dirname,
3110 	vattr_t *vap,
3111 	vnode_t **vpp,
3112 	cred_t *cr)
3113 {
3114 	int ret;
3115 
3116 	VOPXID_MAP_CR(dvp, cr);
3117 
3118 	ret = (*(dvp)->v_op->vop_mkdir)(dvp, dirname, vap, vpp, cr);
3119 	if (ret == 0 && *vpp) {
3120 		VOPSTATS_UPDATE(*vpp, mkdir);
3121 		if ((*vpp)->v_path == NULL) {
3122 			vn_setpath(rootdir, dvp, *vpp, dirname,
3123 			    strlen(dirname));
3124 		}
3125 	}
3126 
3127 	return (ret);
3128 }
3129 
3130 int
3131 fop_rmdir(
3132 	vnode_t *dvp,
3133 	char *nm,
3134 	vnode_t *cdir,
3135 	cred_t *cr)
3136 {
3137 	int	err;
3138 
3139 	VOPXID_MAP_CR(dvp, cr);
3140 
3141 	err = (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr);
3142 	VOPSTATS_UPDATE(dvp, rmdir);
3143 	return (err);
3144 }
3145 
3146 int
3147 fop_readdir(
3148 	vnode_t *vp,
3149 	uio_t *uiop,
3150 	cred_t *cr,
3151 	int *eofp)
3152 {
3153 	int	err;
3154 	ssize_t	resid_start = uiop->uio_resid;
3155 
3156 	VOPXID_MAP_CR(vp, cr);
3157 
3158 	err = (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp);
3159 	VOPSTATS_UPDATE_IO(vp, readdir,
3160 	    readdir_bytes, (resid_start - uiop->uio_resid));
3161 	return (err);
3162 }
3163 
3164 int
3165 fop_symlink(
3166 	vnode_t *dvp,
3167 	char *linkname,
3168 	vattr_t *vap,
3169 	char *target,
3170 	cred_t *cr)
3171 {
3172 	int	err;
3173 
3174 	VOPXID_MAP_CR(dvp, cr);
3175 
3176 	err = (*(dvp)->v_op->vop_symlink) (dvp, linkname, vap, target, cr);
3177 	VOPSTATS_UPDATE(dvp, symlink);
3178 	return (err);
3179 }
3180 
3181 int
3182 fop_readlink(
3183 	vnode_t *vp,
3184 	uio_t *uiop,
3185 	cred_t *cr)
3186 {
3187 	int	err;
3188 
3189 	VOPXID_MAP_CR(vp, cr);
3190 
3191 	err = (*(vp)->v_op->vop_readlink)(vp, uiop, cr);
3192 	VOPSTATS_UPDATE(vp, readlink);
3193 	return (err);
3194 }
3195 
3196 int
3197 fop_fsync(
3198 	vnode_t *vp,
3199 	int syncflag,
3200 	cred_t *cr)
3201 {
3202 	int	err;
3203 
3204 	VOPXID_MAP_CR(vp, cr);
3205 
3206 	err = (*(vp)->v_op->vop_fsync)(vp, syncflag, cr);
3207 	VOPSTATS_UPDATE(vp, fsync);
3208 	return (err);
3209 }
3210 
3211 void
3212 fop_inactive(
3213 	vnode_t *vp,
3214 	cred_t *cr)
3215 {
3216 	/* Need to update stats before vop call since we may lose the vnode */
3217 	VOPSTATS_UPDATE(vp, inactive);
3218 
3219 	VOPXID_MAP_CR(vp, cr);
3220 
3221 	(*(vp)->v_op->vop_inactive)(vp, cr);
3222 }
3223 
3224 int
3225 fop_fid(
3226 	vnode_t *vp,
3227 	fid_t *fidp)
3228 {
3229 	int	err;
3230 
3231 	err = (*(vp)->v_op->vop_fid)(vp, fidp);
3232 	VOPSTATS_UPDATE(vp, fid);
3233 	return (err);
3234 }
3235 
3236 int
3237 fop_rwlock(
3238 	vnode_t *vp,
3239 	int write_lock,
3240 	caller_context_t *ct)
3241 {
3242 	int	ret;
3243 
3244 	ret = ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct));
3245 	VOPSTATS_UPDATE(vp, rwlock);
3246 	return (ret);
3247 }
3248 
3249 void
3250 fop_rwunlock(
3251 	vnode_t *vp,
3252 	int write_lock,
3253 	caller_context_t *ct)
3254 {
3255 	(*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct);
3256 	VOPSTATS_UPDATE(vp, rwunlock);
3257 }
3258 
3259 int
3260 fop_seek(
3261 	vnode_t *vp,
3262 	offset_t ooff,
3263 	offset_t *noffp)
3264 {
3265 	int	err;
3266 
3267 	err = (*(vp)->v_op->vop_seek)(vp, ooff, noffp);
3268 	VOPSTATS_UPDATE(vp, seek);
3269 	return (err);
3270 }
3271 
3272 int
3273 fop_cmp(
3274 	vnode_t *vp1,
3275 	vnode_t *vp2)
3276 {
3277 	int	err;
3278 
3279 	err = (*(vp1)->v_op->vop_cmp)(vp1, vp2);
3280 	VOPSTATS_UPDATE(vp1, cmp);
3281 	return (err);
3282 }
3283 
3284 int
3285 fop_frlock(
3286 	vnode_t *vp,
3287 	int cmd,
3288 	flock64_t *bfp,
3289 	int flag,
3290 	offset_t offset,
3291 	struct flk_callback *flk_cbp,
3292 	cred_t *cr)
3293 {
3294 	int	err;
3295 
3296 	VOPXID_MAP_CR(vp, cr);
3297 
3298 	err = (*(vp)->v_op->vop_frlock)
3299 	    (vp, cmd, bfp, flag, offset, flk_cbp, cr);
3300 	VOPSTATS_UPDATE(vp, frlock);
3301 	return (err);
3302 }
3303 
3304 int
3305 fop_space(
3306 	vnode_t *vp,
3307 	int cmd,
3308 	flock64_t *bfp,
3309 	int flag,
3310 	offset_t offset,
3311 	cred_t *cr,
3312 	caller_context_t *ct)
3313 {
3314 	int	err;
3315 
3316 	VOPXID_MAP_CR(vp, cr);
3317 
3318 	err = (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct);
3319 	VOPSTATS_UPDATE(vp, space);
3320 	return (err);
3321 }
3322 
3323 int
3324 fop_realvp(
3325 	vnode_t *vp,
3326 	vnode_t **vpp)
3327 {
3328 	int	err;
3329 
3330 	err = (*(vp)->v_op->vop_realvp)(vp, vpp);
3331 	VOPSTATS_UPDATE(vp, realvp);
3332 	return (err);
3333 }
3334 
3335 int
3336 fop_getpage(
3337 	vnode_t *vp,
3338 	offset_t off,
3339 	size_t len,
3340 	uint_t *protp,
3341 	page_t **plarr,
3342 	size_t plsz,
3343 	struct seg *seg,
3344 	caddr_t addr,
3345 	enum seg_rw rw,
3346 	cred_t *cr)
3347 {
3348 	int	err;
3349 
3350 	VOPXID_MAP_CR(vp, cr);
3351 
3352 	err = (*(vp)->v_op->vop_getpage)
3353 	    (vp, off, len, protp, plarr, plsz, seg, addr, rw, cr);
3354 	VOPSTATS_UPDATE(vp, getpage);
3355 	return (err);
3356 }
3357 
3358 int
3359 fop_putpage(
3360 	vnode_t *vp,
3361 	offset_t off,
3362 	size_t len,
3363 	int flags,
3364 	cred_t *cr)
3365 {
3366 	int	err;
3367 
3368 	VOPXID_MAP_CR(vp, cr);
3369 
3370 	err = (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr);
3371 	VOPSTATS_UPDATE(vp, putpage);
3372 	return (err);
3373 }
3374 
3375 int
3376 fop_map(
3377 	vnode_t *vp,
3378 	offset_t off,
3379 	struct as *as,
3380 	caddr_t *addrp,
3381 	size_t len,
3382 	uchar_t prot,
3383 	uchar_t maxprot,
3384 	uint_t flags,
3385 	cred_t *cr)
3386 {
3387 	int	err;
3388 
3389 	VOPXID_MAP_CR(vp, cr);
3390 
3391 	err = (*(vp)->v_op->vop_map)
3392 	    (vp, off, as, addrp, len, prot, maxprot, flags, cr);
3393 	VOPSTATS_UPDATE(vp, map);
3394 	return (err);
3395 }
3396 
3397 int
3398 fop_addmap(
3399 	vnode_t *vp,
3400 	offset_t off,
3401 	struct as *as,
3402 	caddr_t addr,
3403 	size_t len,
3404 	uchar_t prot,
3405 	uchar_t maxprot,
3406 	uint_t flags,
3407 	cred_t *cr)
3408 {
3409 	int error;
3410 	u_longlong_t delta;
3411 
3412 	VOPXID_MAP_CR(vp, cr);
3413 
3414 	error = (*(vp)->v_op->vop_addmap)
3415 	    (vp, off, as, addr, len, prot, maxprot, flags, cr);
3416 
3417 	if ((!error) && (vp->v_type == VREG)) {
3418 		delta = (u_longlong_t)btopr(len);
3419 		/*
3420 		 * If file is declared MAP_PRIVATE, it can't be written back
3421 		 * even if open for write. Handle as read.
3422 		 */
3423 		if (flags & MAP_PRIVATE) {
3424 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3425 			    (int64_t)delta);
3426 		} else {
3427 			/*
3428 			 * atomic_add_64 forces the fetch of a 64 bit value to
3429 			 * be atomic on 32 bit machines
3430 			 */
3431 			if (maxprot & PROT_WRITE)
3432 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
3433 				    (int64_t)delta);
3434 			if (maxprot & PROT_READ)
3435 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3436 				    (int64_t)delta);
3437 			if (maxprot & PROT_EXEC)
3438 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3439 				    (int64_t)delta);
3440 		}
3441 	}
3442 	VOPSTATS_UPDATE(vp, addmap);
3443 	return (error);
3444 }
3445 
3446 int
3447 fop_delmap(
3448 	vnode_t *vp,
3449 	offset_t off,
3450 	struct as *as,
3451 	caddr_t addr,
3452 	size_t len,
3453 	uint_t prot,
3454 	uint_t maxprot,
3455 	uint_t flags,
3456 	cred_t *cr)
3457 {
3458 	int error;
3459 	u_longlong_t delta;
3460 
3461 	VOPXID_MAP_CR(vp, cr);
3462 
3463 	error = (*(vp)->v_op->vop_delmap)
3464 	    (vp, off, as, addr, len, prot, maxprot, flags, cr);
3465 
3466 	/*
3467 	 * NFS calls into delmap twice, the first time
3468 	 * it simply establishes a callback mechanism and returns EAGAIN
3469 	 * while the real work is being done upon the second invocation.
3470 	 * We have to detect this here and only decrement the counts upon
3471 	 * the second delmap request.
3472 	 */
3473 	if ((error != EAGAIN) && (vp->v_type == VREG)) {
3474 
3475 		delta = (u_longlong_t)btopr(len);
3476 
3477 		if (flags & MAP_PRIVATE) {
3478 			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3479 			    (int64_t)(-delta));
3480 		} else {
3481 			/*
3482 			 * atomic_add_64 forces the fetch of a 64 bit value
3483 			 * to be atomic on 32 bit machines
3484 			 */
3485 			if (maxprot & PROT_WRITE)
3486 				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
3487 				    (int64_t)(-delta));
3488 			if (maxprot & PROT_READ)
3489 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3490 				    (int64_t)(-delta));
3491 			if (maxprot & PROT_EXEC)
3492 				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
3493 				    (int64_t)(-delta));
3494 		}
3495 	}
3496 	VOPSTATS_UPDATE(vp, delmap);
3497 	return (error);
3498 }
3499 
3500 
3501 int
3502 fop_poll(
3503 	vnode_t *vp,
3504 	short events,
3505 	int anyyet,
3506 	short *reventsp,
3507 	struct pollhead **phpp)
3508 {
3509 	int	err;
3510 
3511 	err = (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp);
3512 	VOPSTATS_UPDATE(vp, poll);
3513 	return (err);
3514 }
3515 
3516 int
3517 fop_dump(
3518 	vnode_t *vp,
3519 	caddr_t addr,
3520 	int lbdn,
3521 	int dblks)
3522 {
3523 	int	err;
3524 
3525 	err = (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks);
3526 	VOPSTATS_UPDATE(vp, dump);
3527 	return (err);
3528 }
3529 
3530 int
3531 fop_pathconf(
3532 	vnode_t *vp,
3533 	int cmd,
3534 	ulong_t *valp,
3535 	cred_t *cr)
3536 {
3537 	int	err;
3538 
3539 	VOPXID_MAP_CR(vp, cr);
3540 
3541 	err = (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr);
3542 	VOPSTATS_UPDATE(vp, pathconf);
3543 	return (err);
3544 }
3545 
3546 int
3547 fop_pageio(
3548 	vnode_t *vp,
3549 	struct page *pp,
3550 	u_offset_t io_off,
3551 	size_t io_len,
3552 	int flags,
3553 	cred_t *cr)
3554 {
3555 	int	err;
3556 
3557 	VOPXID_MAP_CR(vp, cr);
3558 
3559 	err = (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr);
3560 	VOPSTATS_UPDATE(vp, pageio);
3561 	return (err);
3562 }
3563 
3564 int
3565 fop_dumpctl(
3566 	vnode_t *vp,
3567 	int action,
3568 	int *blkp)
3569 {
3570 	int	err;
3571 	err = (*(vp)->v_op->vop_dumpctl)(vp, action, blkp);
3572 	VOPSTATS_UPDATE(vp, dumpctl);
3573 	return (err);
3574 }
3575 
3576 void
3577 fop_dispose(
3578 	vnode_t *vp,
3579 	page_t *pp,
3580 	int flag,
3581 	int dn,
3582 	cred_t *cr)
3583 {
3584 	/* Must do stats first since it's possible to lose the vnode */
3585 	VOPSTATS_UPDATE(vp, dispose);
3586 
3587 	VOPXID_MAP_CR(vp, cr);
3588 
3589 	(*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr);
3590 }
3591 
3592 int
3593 fop_setsecattr(
3594 	vnode_t *vp,
3595 	vsecattr_t *vsap,
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_setsecattr) (vp, vsap, flag, cr);
3604 	VOPSTATS_UPDATE(vp, setsecattr);
3605 	return (err);
3606 }
3607 
3608 int
3609 fop_getsecattr(
3610 	vnode_t *vp,
3611 	vsecattr_t *vsap,
3612 	int flag,
3613 	cred_t *cr)
3614 {
3615 	int	err;
3616 
3617 	VOPXID_MAP_CR(vp, cr);
3618 
3619 	err = (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr);
3620 	VOPSTATS_UPDATE(vp, getsecattr);
3621 	return (err);
3622 }
3623 
3624 int
3625 fop_shrlock(
3626 	vnode_t *vp,
3627 	int cmd,
3628 	struct shrlock *shr,
3629 	int flag,
3630 	cred_t *cr)
3631 {
3632 	int	err;
3633 
3634 	VOPXID_MAP_CR(vp, cr);
3635 
3636 	err = (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr);
3637 	VOPSTATS_UPDATE(vp, shrlock);
3638 	return (err);
3639 }
3640 
3641 int
3642 fop_vnevent(vnode_t *vp, vnevent_t vnevent, vnode_t *dvp, char *fnm)
3643 {
3644 	int	err;
3645 
3646 	err = (*(vp)->v_op->vop_vnevent)(vp, vnevent, dvp, fnm);
3647 	VOPSTATS_UPDATE(vp, vnevent);
3648 	return (err);
3649 }
3650