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