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