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