xref: /freebsd/sys/kern/vfs_default.c (revision aef815e7873b006bd040ac1690425709635e32e7)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed
8  * to Berkeley by John Heidemann of the UCLA Ficus project.
9  *
10  * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/bio.h>
43 #include <sys/buf.h>
44 #include <sys/conf.h>
45 #include <sys/event.h>
46 #include <sys/filio.h>
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
49 #include <sys/lock.h>
50 #include <sys/lockf.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/namei.h>
54 #include <sys/rwlock.h>
55 #include <sys/fcntl.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
58 #include <sys/dirent.h>
59 #include <sys/poll.h>
60 #include <sys/stat.h>
61 #include <security/audit/audit.h>
62 #include <sys/priv.h>
63 
64 #include <security/mac/mac_framework.h>
65 
66 #include <vm/vm.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_extern.h>
69 #include <vm/pmap.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_pager.h>
73 #include <vm/vnode_pager.h>
74 
75 static int	vop_nolookup(struct vop_lookup_args *);
76 static int	vop_norename(struct vop_rename_args *);
77 static int	vop_nostrategy(struct vop_strategy_args *);
78 static int	get_next_dirent(struct vnode *vp, struct dirent **dpp,
79 				char *dirbuf, int dirbuflen, off_t *off,
80 				char **cpos, int *len, int *eofflag,
81 				struct thread *td);
82 static int	dirent_exists(struct vnode *vp, const char *dirname,
83 			      struct thread *td);
84 
85 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
86 
87 static int vop_stdis_text(struct vop_is_text_args *ap);
88 static int vop_stdunset_text(struct vop_unset_text_args *ap);
89 static int vop_stdadd_writecount(struct vop_add_writecount_args *ap);
90 static int vop_stdcopy_file_range(struct vop_copy_file_range_args *ap);
91 static int vop_stdfdatasync(struct vop_fdatasync_args *ap);
92 static int vop_stdgetpages_async(struct vop_getpages_async_args *ap);
93 static int vop_stdread_pgcache(struct vop_read_pgcache_args *ap);
94 static int vop_stdstat(struct vop_stat_args *ap);
95 static int vop_stdvput_pair(struct vop_vput_pair_args *ap);
96 static int vop_stddeallocate(struct vop_deallocate_args *ap);
97 
98 /*
99  * This vnode table stores what we want to do if the filesystem doesn't
100  * implement a particular VOP.
101  *
102  * If there is no specific entry here, we will return EOPNOTSUPP.
103  *
104  * Note that every filesystem has to implement either vop_access
105  * or vop_accessx; failing to do so will result in immediate crash
106  * due to stack overflow, as vop_stdaccess() calls vop_stdaccessx(),
107  * which calls vop_stdaccess() etc.
108  */
109 
110 struct vop_vector default_vnodeops = {
111 	.vop_default =		NULL,
112 	.vop_bypass =		VOP_EOPNOTSUPP,
113 
114 	.vop_access =		vop_stdaccess,
115 	.vop_accessx =		vop_stdaccessx,
116 	.vop_advise =		vop_stdadvise,
117 	.vop_advlock =		vop_stdadvlock,
118 	.vop_advlockasync =	vop_stdadvlockasync,
119 	.vop_advlockpurge =	vop_stdadvlockpurge,
120 	.vop_allocate =		vop_stdallocate,
121 	.vop_deallocate =	vop_stddeallocate,
122 	.vop_bmap =		vop_stdbmap,
123 	.vop_close =		VOP_NULL,
124 	.vop_fsync =		VOP_NULL,
125 	.vop_stat =		vop_stdstat,
126 	.vop_fdatasync =	vop_stdfdatasync,
127 	.vop_getpages =		vop_stdgetpages,
128 	.vop_getpages_async =	vop_stdgetpages_async,
129 	.vop_getwritemount = 	vop_stdgetwritemount,
130 	.vop_inactive =		VOP_NULL,
131 	.vop_need_inactive =	vop_stdneed_inactive,
132 	.vop_ioctl =		vop_stdioctl,
133 	.vop_kqfilter =		vop_stdkqfilter,
134 	.vop_islocked =		vop_stdislocked,
135 	.vop_lock1 =		vop_stdlock,
136 	.vop_lookup =		vop_nolookup,
137 	.vop_open =		VOP_NULL,
138 	.vop_pathconf =		VOP_EINVAL,
139 	.vop_poll =		vop_nopoll,
140 	.vop_putpages =		vop_stdputpages,
141 	.vop_readlink =		VOP_EINVAL,
142 	.vop_read_pgcache =	vop_stdread_pgcache,
143 	.vop_rename =		vop_norename,
144 	.vop_revoke =		VOP_PANIC,
145 	.vop_strategy =		vop_nostrategy,
146 	.vop_unlock =		vop_stdunlock,
147 	.vop_vptocnp =		vop_stdvptocnp,
148 	.vop_vptofh =		vop_stdvptofh,
149 	.vop_unp_bind =		vop_stdunp_bind,
150 	.vop_unp_connect =	vop_stdunp_connect,
151 	.vop_unp_detach =	vop_stdunp_detach,
152 	.vop_is_text =		vop_stdis_text,
153 	.vop_set_text =		vop_stdset_text,
154 	.vop_unset_text =	vop_stdunset_text,
155 	.vop_add_writecount =	vop_stdadd_writecount,
156 	.vop_copy_file_range =	vop_stdcopy_file_range,
157 	.vop_vput_pair =	vop_stdvput_pair,
158 };
159 VFS_VOP_VECTOR_REGISTER(default_vnodeops);
160 
161 /*
162  * Series of placeholder functions for various error returns for
163  * VOPs.
164  */
165 
166 int
167 vop_eopnotsupp(struct vop_generic_args *ap)
168 {
169 	/*
170 	printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name);
171 	*/
172 
173 	return (EOPNOTSUPP);
174 }
175 
176 int
177 vop_ebadf(struct vop_generic_args *ap)
178 {
179 
180 	return (EBADF);
181 }
182 
183 int
184 vop_enotty(struct vop_generic_args *ap)
185 {
186 
187 	return (ENOTTY);
188 }
189 
190 int
191 vop_einval(struct vop_generic_args *ap)
192 {
193 
194 	return (EINVAL);
195 }
196 
197 int
198 vop_enoent(struct vop_generic_args *ap)
199 {
200 
201 	return (ENOENT);
202 }
203 
204 int
205 vop_eagain(struct vop_generic_args *ap)
206 {
207 
208 	return (EAGAIN);
209 }
210 
211 int
212 vop_null(struct vop_generic_args *ap)
213 {
214 
215 	return (0);
216 }
217 
218 /*
219  * Helper function to panic on some bad VOPs in some filesystems.
220  */
221 int
222 vop_panic(struct vop_generic_args *ap)
223 {
224 
225 	panic("filesystem goof: vop_panic[%s]", ap->a_desc->vdesc_name);
226 }
227 
228 /*
229  * vop_std<something> and vop_no<something> are default functions for use by
230  * filesystems that need the "default reasonable" implementation for a
231  * particular operation.
232  *
233  * The documentation for the operations they implement exists (if it exists)
234  * in the VOP_<SOMETHING>(9) manpage (all uppercase).
235  */
236 
237 /*
238  * Default vop for filesystems that do not support name lookup
239  */
240 static int
241 vop_nolookup(ap)
242 	struct vop_lookup_args /* {
243 		struct vnode *a_dvp;
244 		struct vnode **a_vpp;
245 		struct componentname *a_cnp;
246 	} */ *ap;
247 {
248 
249 	*ap->a_vpp = NULL;
250 	return (ENOTDIR);
251 }
252 
253 /*
254  * vop_norename:
255  *
256  * Handle unlock and reference counting for arguments of vop_rename
257  * for filesystems that do not implement rename operation.
258  */
259 static int
260 vop_norename(struct vop_rename_args *ap)
261 {
262 
263 	vop_rename_fail(ap);
264 	return (EOPNOTSUPP);
265 }
266 
267 /*
268  *	vop_nostrategy:
269  *
270  *	Strategy routine for VFS devices that have none.
271  *
272  *	BIO_ERROR and B_INVAL must be cleared prior to calling any strategy
273  *	routine.  Typically this is done for a BIO_READ strategy call.
274  *	Typically B_INVAL is assumed to already be clear prior to a write
275  *	and should not be cleared manually unless you just made the buffer
276  *	invalid.  BIO_ERROR should be cleared either way.
277  */
278 
279 static int
280 vop_nostrategy (struct vop_strategy_args *ap)
281 {
282 	printf("No strategy for buffer at %p\n", ap->a_bp);
283 	vn_printf(ap->a_vp, "vnode ");
284 	ap->a_bp->b_ioflags |= BIO_ERROR;
285 	ap->a_bp->b_error = EOPNOTSUPP;
286 	bufdone(ap->a_bp);
287 	return (EOPNOTSUPP);
288 }
289 
290 static int
291 get_next_dirent(struct vnode *vp, struct dirent **dpp, char *dirbuf,
292 		int dirbuflen, off_t *off, char **cpos, int *len,
293 		int *eofflag, struct thread *td)
294 {
295 	int error, reclen;
296 	struct uio uio;
297 	struct iovec iov;
298 	struct dirent *dp;
299 
300 	KASSERT(VOP_ISLOCKED(vp), ("vp %p is not locked", vp));
301 	KASSERT(vp->v_type == VDIR, ("vp %p is not a directory", vp));
302 
303 	if (*len == 0) {
304 		iov.iov_base = dirbuf;
305 		iov.iov_len = dirbuflen;
306 
307 		uio.uio_iov = &iov;
308 		uio.uio_iovcnt = 1;
309 		uio.uio_offset = *off;
310 		uio.uio_resid = dirbuflen;
311 		uio.uio_segflg = UIO_SYSSPACE;
312 		uio.uio_rw = UIO_READ;
313 		uio.uio_td = td;
314 
315 		*eofflag = 0;
316 
317 #ifdef MAC
318 		error = mac_vnode_check_readdir(td->td_ucred, vp);
319 		if (error == 0)
320 #endif
321 			error = VOP_READDIR(vp, &uio, td->td_ucred, eofflag,
322 		    		NULL, NULL);
323 		if (error)
324 			return (error);
325 
326 		*off = uio.uio_offset;
327 
328 		*cpos = dirbuf;
329 		*len = (dirbuflen - uio.uio_resid);
330 
331 		if (*len == 0)
332 			return (ENOENT);
333 	}
334 
335 	dp = (struct dirent *)(*cpos);
336 	reclen = dp->d_reclen;
337 	*dpp = dp;
338 
339 	/* check for malformed directory.. */
340 	if (reclen < DIRENT_MINSIZE)
341 		return (EINVAL);
342 
343 	*cpos += reclen;
344 	*len -= reclen;
345 
346 	return (0);
347 }
348 
349 /*
350  * Check if a named file exists in a given directory vnode.
351  */
352 static int
353 dirent_exists(struct vnode *vp, const char *dirname, struct thread *td)
354 {
355 	char *dirbuf, *cpos;
356 	int error, eofflag, dirbuflen, len, found;
357 	off_t off;
358 	struct dirent *dp;
359 	struct vattr va;
360 
361 	KASSERT(VOP_ISLOCKED(vp), ("vp %p is not locked", vp));
362 	KASSERT(vp->v_type == VDIR, ("vp %p is not a directory", vp));
363 
364 	found = 0;
365 
366 	error = VOP_GETATTR(vp, &va, td->td_ucred);
367 	if (error)
368 		return (found);
369 
370 	dirbuflen = DEV_BSIZE;
371 	if (dirbuflen < va.va_blocksize)
372 		dirbuflen = va.va_blocksize;
373 	dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
374 
375 	off = 0;
376 	len = 0;
377 	do {
378 		error = get_next_dirent(vp, &dp, dirbuf, dirbuflen, &off,
379 					&cpos, &len, &eofflag, td);
380 		if (error)
381 			goto out;
382 
383 		if (dp->d_type != DT_WHT && dp->d_fileno != 0 &&
384 		    strcmp(dp->d_name, dirname) == 0) {
385 			found = 1;
386 			goto out;
387 		}
388 	} while (len > 0 || !eofflag);
389 
390 out:
391 	free(dirbuf, M_TEMP);
392 	return (found);
393 }
394 
395 int
396 vop_stdaccess(struct vop_access_args *ap)
397 {
398 
399 	KASSERT((ap->a_accmode & ~(VEXEC | VWRITE | VREAD | VADMIN |
400 	    VAPPEND)) == 0, ("invalid bit in accmode"));
401 
402 	return (VOP_ACCESSX(ap->a_vp, ap->a_accmode, ap->a_cred, ap->a_td));
403 }
404 
405 int
406 vop_stdaccessx(struct vop_accessx_args *ap)
407 {
408 	int error;
409 	accmode_t accmode = ap->a_accmode;
410 
411 	error = vfs_unixify_accmode(&accmode);
412 	if (error != 0)
413 		return (error);
414 
415 	if (accmode == 0)
416 		return (0);
417 
418 	return (VOP_ACCESS(ap->a_vp, accmode, ap->a_cred, ap->a_td));
419 }
420 
421 /*
422  * Advisory record locking support
423  */
424 int
425 vop_stdadvlock(struct vop_advlock_args *ap)
426 {
427 	struct vnode *vp;
428 	struct mount *mp;
429 	struct vattr vattr;
430 	int error;
431 
432 	vp = ap->a_vp;
433 
434 	/*
435 	 * Provide atomicity of open(O_CREAT | O_EXCL | O_EXLOCK) for
436 	 * local filesystems.  See vn_open_cred() for reciprocal part.
437 	 */
438 	mp = vp->v_mount;
439 	if (mp != NULL && (mp->mnt_flag & MNT_LOCAL) != 0 &&
440 	    ap->a_op == F_SETLK && (ap->a_flags & F_FIRSTOPEN) == 0) {
441 		VI_LOCK(vp);
442 		while ((vp->v_iflag & VI_FOPENING) != 0)
443 			msleep(vp, VI_MTX(vp), PLOCK, "lockfo", 0);
444 		VI_UNLOCK(vp);
445 	}
446 
447 	if (ap->a_fl->l_whence == SEEK_END) {
448 		/*
449 		 * The NFSv4 server must avoid doing a vn_lock() here, since it
450 		 * can deadlock the nfsd threads, due to a LOR.  Fortunately
451 		 * the NFSv4 server always uses SEEK_SET and this code is
452 		 * only required for the SEEK_END case.
453 		 */
454 		vn_lock(vp, LK_SHARED | LK_RETRY);
455 		error = VOP_GETATTR(vp, &vattr, curthread->td_ucred);
456 		VOP_UNLOCK(vp);
457 		if (error)
458 			return (error);
459 	} else
460 		vattr.va_size = 0;
461 
462 	return (lf_advlock(ap, &(vp->v_lockf), vattr.va_size));
463 }
464 
465 int
466 vop_stdadvlockasync(struct vop_advlockasync_args *ap)
467 {
468 	struct vnode *vp;
469 	struct vattr vattr;
470 	int error;
471 
472 	vp = ap->a_vp;
473 	if (ap->a_fl->l_whence == SEEK_END) {
474 		/* The size argument is only needed for SEEK_END. */
475 		vn_lock(vp, LK_SHARED | LK_RETRY);
476 		error = VOP_GETATTR(vp, &vattr, curthread->td_ucred);
477 		VOP_UNLOCK(vp);
478 		if (error)
479 			return (error);
480 	} else
481 		vattr.va_size = 0;
482 
483 	return (lf_advlockasync(ap, &(vp->v_lockf), vattr.va_size));
484 }
485 
486 int
487 vop_stdadvlockpurge(struct vop_advlockpurge_args *ap)
488 {
489 	struct vnode *vp;
490 
491 	vp = ap->a_vp;
492 	lf_purgelocks(vp, &vp->v_lockf);
493 	return (0);
494 }
495 
496 /*
497  * vop_stdpathconf:
498  *
499  * Standard implementation of POSIX pathconf, to get information about limits
500  * for a filesystem.
501  * Override per filesystem for the case where the filesystem has smaller
502  * limits.
503  */
504 int
505 vop_stdpathconf(ap)
506 	struct vop_pathconf_args /* {
507 	struct vnode *a_vp;
508 	int a_name;
509 	int *a_retval;
510 	} */ *ap;
511 {
512 
513 	switch (ap->a_name) {
514 		case _PC_ASYNC_IO:
515 			*ap->a_retval = _POSIX_ASYNCHRONOUS_IO;
516 			return (0);
517 		case _PC_PATH_MAX:
518 			*ap->a_retval = PATH_MAX;
519 			return (0);
520 		case _PC_ACL_EXTENDED:
521 		case _PC_ACL_NFS4:
522 		case _PC_CAP_PRESENT:
523 		case _PC_DEALLOC_PRESENT:
524 		case _PC_INF_PRESENT:
525 		case _PC_MAC_PRESENT:
526 			*ap->a_retval = 0;
527 			return (0);
528 		default:
529 			return (EINVAL);
530 	}
531 	/* NOTREACHED */
532 }
533 
534 /*
535  * Standard lock, unlock and islocked functions.
536  */
537 int
538 vop_stdlock(ap)
539 	struct vop_lock1_args /* {
540 		struct vnode *a_vp;
541 		int a_flags;
542 		char *file;
543 		int line;
544 	} */ *ap;
545 {
546 	struct vnode *vp = ap->a_vp;
547 	struct mtx *ilk;
548 
549 	ilk = VI_MTX(vp);
550 	return (lockmgr_lock_flags(vp->v_vnlock, ap->a_flags,
551 	    &ilk->lock_object, ap->a_file, ap->a_line));
552 }
553 
554 /* See above. */
555 int
556 vop_stdunlock(ap)
557 	struct vop_unlock_args /* {
558 		struct vnode *a_vp;
559 	} */ *ap;
560 {
561 	struct vnode *vp = ap->a_vp;
562 
563 	return (lockmgr_unlock(vp->v_vnlock));
564 }
565 
566 /* See above. */
567 int
568 vop_stdislocked(ap)
569 	struct vop_islocked_args /* {
570 		struct vnode *a_vp;
571 	} */ *ap;
572 {
573 
574 	return (lockstatus(ap->a_vp->v_vnlock));
575 }
576 
577 /*
578  * Variants of the above set.
579  *
580  * Differences are:
581  * - shared locking disablement is not supported
582  * - v_vnlock pointer is not honored
583  */
584 int
585 vop_lock(ap)
586 	struct vop_lock1_args /* {
587 		struct vnode *a_vp;
588 		int a_flags;
589 		char *file;
590 		int line;
591 	} */ *ap;
592 {
593 	struct vnode *vp = ap->a_vp;
594 	int flags = ap->a_flags;
595 	struct mtx *ilk;
596 
597 	MPASS(vp->v_vnlock == &vp->v_lock);
598 
599 	if (__predict_false((flags & ~(LK_TYPE_MASK | LK_NODDLKTREAT | LK_RETRY)) != 0))
600 		goto other;
601 
602 	switch (flags & LK_TYPE_MASK) {
603 	case LK_SHARED:
604 		return (lockmgr_slock(&vp->v_lock, flags, ap->a_file, ap->a_line));
605 	case LK_EXCLUSIVE:
606 		return (lockmgr_xlock(&vp->v_lock, flags, ap->a_file, ap->a_line));
607 	}
608 other:
609 	ilk = VI_MTX(vp);
610 	return (lockmgr_lock_flags(&vp->v_lock, flags,
611 	    &ilk->lock_object, ap->a_file, ap->a_line));
612 }
613 
614 int
615 vop_unlock(ap)
616 	struct vop_unlock_args /* {
617 		struct vnode *a_vp;
618 	} */ *ap;
619 {
620 	struct vnode *vp = ap->a_vp;
621 
622 	MPASS(vp->v_vnlock == &vp->v_lock);
623 
624 	return (lockmgr_unlock(&vp->v_lock));
625 }
626 
627 int
628 vop_islocked(ap)
629 	struct vop_islocked_args /* {
630 		struct vnode *a_vp;
631 	} */ *ap;
632 {
633 	struct vnode *vp = ap->a_vp;
634 
635 	MPASS(vp->v_vnlock == &vp->v_lock);
636 
637 	return (lockstatus(&vp->v_lock));
638 }
639 
640 /*
641  * Return true for select/poll.
642  */
643 int
644 vop_nopoll(ap)
645 	struct vop_poll_args /* {
646 		struct vnode *a_vp;
647 		int  a_events;
648 		struct ucred *a_cred;
649 		struct thread *a_td;
650 	} */ *ap;
651 {
652 
653 	if (ap->a_events & ~POLLSTANDARD)
654 		return (POLLNVAL);
655 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
656 }
657 
658 /*
659  * Implement poll for local filesystems that support it.
660  */
661 int
662 vop_stdpoll(ap)
663 	struct vop_poll_args /* {
664 		struct vnode *a_vp;
665 		int  a_events;
666 		struct ucred *a_cred;
667 		struct thread *a_td;
668 	} */ *ap;
669 {
670 	if (ap->a_events & ~POLLSTANDARD)
671 		return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
672 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
673 }
674 
675 /*
676  * Return our mount point, as we will take charge of the writes.
677  */
678 int
679 vop_stdgetwritemount(ap)
680 	struct vop_getwritemount_args /* {
681 		struct vnode *a_vp;
682 		struct mount **a_mpp;
683 	} */ *ap;
684 {
685 	struct mount *mp;
686 	struct vnode *vp;
687 
688 	/*
689 	 * Note that having a reference does not prevent forced unmount from
690 	 * setting ->v_mount to NULL after the lock gets released. This is of
691 	 * no consequence for typical consumers (most notably vn_start_write)
692 	 * since in this case the vnode is VIRF_DOOMED. Unmount might have
693 	 * progressed far enough that its completion is only delayed by the
694 	 * reference obtained here. The consumer only needs to concern itself
695 	 * with releasing it.
696 	 */
697 	vp = ap->a_vp;
698 	mp = vfs_ref_from_vp(vp);
699 	*(ap->a_mpp) = mp;
700 	return (0);
701 }
702 
703 /*
704  * If the file system doesn't implement VOP_BMAP, then return sensible defaults:
705  * - Return the vnode's bufobj instead of any underlying device's bufobj
706  * - Calculate the physical block number as if there were equal size
707  *   consecutive blocks, but
708  * - Report no contiguous runs of blocks.
709  */
710 int
711 vop_stdbmap(ap)
712 	struct vop_bmap_args /* {
713 		struct vnode *a_vp;
714 		daddr_t  a_bn;
715 		struct bufobj **a_bop;
716 		daddr_t *a_bnp;
717 		int *a_runp;
718 		int *a_runb;
719 	} */ *ap;
720 {
721 
722 	if (ap->a_bop != NULL)
723 		*ap->a_bop = &ap->a_vp->v_bufobj;
724 	if (ap->a_bnp != NULL)
725 		*ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize);
726 	if (ap->a_runp != NULL)
727 		*ap->a_runp = 0;
728 	if (ap->a_runb != NULL)
729 		*ap->a_runb = 0;
730 	return (0);
731 }
732 
733 int
734 vop_stdfsync(ap)
735 	struct vop_fsync_args /* {
736 		struct vnode *a_vp;
737 		int a_waitfor;
738 		struct thread *a_td;
739 	} */ *ap;
740 {
741 
742 	return (vn_fsync_buf(ap->a_vp, ap->a_waitfor));
743 }
744 
745 static int
746 vop_stdfdatasync(struct vop_fdatasync_args *ap)
747 {
748 
749 	return (VOP_FSYNC(ap->a_vp, MNT_WAIT, ap->a_td));
750 }
751 
752 int
753 vop_stdfdatasync_buf(struct vop_fdatasync_args *ap)
754 {
755 
756 	return (vn_fsync_buf(ap->a_vp, MNT_WAIT));
757 }
758 
759 /* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */
760 int
761 vop_stdgetpages(ap)
762 	struct vop_getpages_args /* {
763 		struct vnode *a_vp;
764 		vm_page_t *a_m;
765 		int a_count;
766 		int *a_rbehind;
767 		int *a_rahead;
768 	} */ *ap;
769 {
770 
771 	return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
772 	    ap->a_count, ap->a_rbehind, ap->a_rahead, NULL, NULL);
773 }
774 
775 static int
776 vop_stdgetpages_async(struct vop_getpages_async_args *ap)
777 {
778 	int error;
779 
780 	error = VOP_GETPAGES(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
781 	    ap->a_rahead);
782 	if (ap->a_iodone != NULL)
783 		ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
784 	return (error);
785 }
786 
787 int
788 vop_stdkqfilter(struct vop_kqfilter_args *ap)
789 {
790 	return vfs_kqfilter(ap);
791 }
792 
793 /* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */
794 int
795 vop_stdputpages(ap)
796 	struct vop_putpages_args /* {
797 		struct vnode *a_vp;
798 		vm_page_t *a_m;
799 		int a_count;
800 		int a_sync;
801 		int *a_rtvals;
802 	} */ *ap;
803 {
804 
805 	return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
806 	     ap->a_sync, ap->a_rtvals);
807 }
808 
809 int
810 vop_stdvptofh(struct vop_vptofh_args *ap)
811 {
812 	return (EOPNOTSUPP);
813 }
814 
815 int
816 vop_stdvptocnp(struct vop_vptocnp_args *ap)
817 {
818 	struct vnode *vp = ap->a_vp;
819 	struct vnode **dvp = ap->a_vpp;
820 	struct ucred *cred;
821 	char *buf = ap->a_buf;
822 	size_t *buflen = ap->a_buflen;
823 	char *dirbuf, *cpos;
824 	int i, error, eofflag, dirbuflen, flags, locked, len, covered;
825 	off_t off;
826 	ino_t fileno;
827 	struct vattr va;
828 	struct nameidata nd;
829 	struct thread *td;
830 	struct dirent *dp;
831 	struct vnode *mvp;
832 
833 	i = *buflen;
834 	error = 0;
835 	covered = 0;
836 	td = curthread;
837 	cred = td->td_ucred;
838 
839 	if (vp->v_type != VDIR)
840 		return (ENOENT);
841 
842 	error = VOP_GETATTR(vp, &va, cred);
843 	if (error)
844 		return (error);
845 
846 	VREF(vp);
847 	locked = VOP_ISLOCKED(vp);
848 	VOP_UNLOCK(vp);
849 	NDINIT_ATVP(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
850 	    "..", vp, td);
851 	flags = FREAD;
852 	error = vn_open_cred(&nd, &flags, 0, VN_OPEN_NOAUDIT, cred, NULL);
853 	if (error) {
854 		vn_lock(vp, locked | LK_RETRY);
855 		return (error);
856 	}
857 	NDFREE(&nd, NDF_ONLY_PNBUF);
858 
859 	mvp = *dvp = nd.ni_vp;
860 
861 	if (vp->v_mount != (*dvp)->v_mount &&
862 	    ((*dvp)->v_vflag & VV_ROOT) &&
863 	    ((*dvp)->v_mount->mnt_flag & MNT_UNION)) {
864 		*dvp = (*dvp)->v_mount->mnt_vnodecovered;
865 		VREF(mvp);
866 		VOP_UNLOCK(mvp);
867 		vn_close(mvp, FREAD, cred, td);
868 		VREF(*dvp);
869 		vn_lock(*dvp, LK_SHARED | LK_RETRY);
870 		covered = 1;
871 	}
872 
873 	fileno = va.va_fileid;
874 
875 	dirbuflen = DEV_BSIZE;
876 	if (dirbuflen < va.va_blocksize)
877 		dirbuflen = va.va_blocksize;
878 	dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
879 
880 	if ((*dvp)->v_type != VDIR) {
881 		error = ENOENT;
882 		goto out;
883 	}
884 
885 	off = 0;
886 	len = 0;
887 	do {
888 		/* call VOP_READDIR of parent */
889 		error = get_next_dirent(*dvp, &dp, dirbuf, dirbuflen, &off,
890 					&cpos, &len, &eofflag, td);
891 		if (error)
892 			goto out;
893 
894 		if ((dp->d_type != DT_WHT) &&
895 		    (dp->d_fileno == fileno)) {
896 			if (covered) {
897 				VOP_UNLOCK(*dvp);
898 				vn_lock(mvp, LK_SHARED | LK_RETRY);
899 				if (dirent_exists(mvp, dp->d_name, td)) {
900 					error = ENOENT;
901 					VOP_UNLOCK(mvp);
902 					vn_lock(*dvp, LK_SHARED | LK_RETRY);
903 					goto out;
904 				}
905 				VOP_UNLOCK(mvp);
906 				vn_lock(*dvp, LK_SHARED | LK_RETRY);
907 			}
908 			i -= dp->d_namlen;
909 
910 			if (i < 0) {
911 				error = ENOMEM;
912 				goto out;
913 			}
914 			if (dp->d_namlen == 1 && dp->d_name[0] == '.') {
915 				error = ENOENT;
916 			} else {
917 				bcopy(dp->d_name, buf + i, dp->d_namlen);
918 				error = 0;
919 			}
920 			goto out;
921 		}
922 	} while (len > 0 || !eofflag);
923 	error = ENOENT;
924 
925 out:
926 	free(dirbuf, M_TEMP);
927 	if (!error) {
928 		*buflen = i;
929 		vref(*dvp);
930 	}
931 	if (covered) {
932 		vput(*dvp);
933 		vrele(mvp);
934 	} else {
935 		VOP_UNLOCK(mvp);
936 		vn_close(mvp, FREAD, cred, td);
937 	}
938 	vn_lock(vp, locked | LK_RETRY);
939 	return (error);
940 }
941 
942 int
943 vop_stdallocate(struct vop_allocate_args *ap)
944 {
945 #ifdef __notyet__
946 	struct statfs *sfs;
947 	off_t maxfilesize = 0;
948 #endif
949 	struct iovec aiov;
950 	struct vattr vattr, *vap;
951 	struct uio auio;
952 	off_t fsize, len, cur, offset;
953 	uint8_t *buf;
954 	struct thread *td;
955 	struct vnode *vp;
956 	size_t iosize;
957 	int error;
958 
959 	buf = NULL;
960 	error = 0;
961 	td = curthread;
962 	vap = &vattr;
963 	vp = ap->a_vp;
964 	len = *ap->a_len;
965 	offset = *ap->a_offset;
966 
967 	error = VOP_GETATTR(vp, vap, td->td_ucred);
968 	if (error != 0)
969 		goto out;
970 	fsize = vap->va_size;
971 	iosize = vap->va_blocksize;
972 	if (iosize == 0)
973 		iosize = BLKDEV_IOSIZE;
974 	if (iosize > maxphys)
975 		iosize = maxphys;
976 	buf = malloc(iosize, M_TEMP, M_WAITOK);
977 
978 #ifdef __notyet__
979 	/*
980 	 * Check if the filesystem sets f_maxfilesize; if not use
981 	 * VOP_SETATTR to perform the check.
982 	 */
983 	sfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
984 	error = VFS_STATFS(vp->v_mount, sfs, td);
985 	if (error == 0)
986 		maxfilesize = sfs->f_maxfilesize;
987 	free(sfs, M_STATFS);
988 	if (error != 0)
989 		goto out;
990 	if (maxfilesize) {
991 		if (offset > maxfilesize || len > maxfilesize ||
992 		    offset + len > maxfilesize) {
993 			error = EFBIG;
994 			goto out;
995 		}
996 	} else
997 #endif
998 	if (offset + len > vap->va_size) {
999 		/*
1000 		 * Test offset + len against the filesystem's maxfilesize.
1001 		 */
1002 		VATTR_NULL(vap);
1003 		vap->va_size = offset + len;
1004 		error = VOP_SETATTR(vp, vap, td->td_ucred);
1005 		if (error != 0)
1006 			goto out;
1007 		VATTR_NULL(vap);
1008 		vap->va_size = fsize;
1009 		error = VOP_SETATTR(vp, vap, td->td_ucred);
1010 		if (error != 0)
1011 			goto out;
1012 	}
1013 
1014 	for (;;) {
1015 		/*
1016 		 * Read and write back anything below the nominal file
1017 		 * size.  There's currently no way outside the filesystem
1018 		 * to know whether this area is sparse or not.
1019 		 */
1020 		cur = iosize;
1021 		if ((offset % iosize) != 0)
1022 			cur -= (offset % iosize);
1023 		if (cur > len)
1024 			cur = len;
1025 		if (offset < fsize) {
1026 			aiov.iov_base = buf;
1027 			aiov.iov_len = cur;
1028 			auio.uio_iov = &aiov;
1029 			auio.uio_iovcnt = 1;
1030 			auio.uio_offset = offset;
1031 			auio.uio_resid = cur;
1032 			auio.uio_segflg = UIO_SYSSPACE;
1033 			auio.uio_rw = UIO_READ;
1034 			auio.uio_td = td;
1035 			error = VOP_READ(vp, &auio, 0, td->td_ucred);
1036 			if (error != 0)
1037 				break;
1038 			if (auio.uio_resid > 0) {
1039 				bzero(buf + cur - auio.uio_resid,
1040 				    auio.uio_resid);
1041 			}
1042 		} else {
1043 			bzero(buf, cur);
1044 		}
1045 
1046 		aiov.iov_base = buf;
1047 		aiov.iov_len = cur;
1048 		auio.uio_iov = &aiov;
1049 		auio.uio_iovcnt = 1;
1050 		auio.uio_offset = offset;
1051 		auio.uio_resid = cur;
1052 		auio.uio_segflg = UIO_SYSSPACE;
1053 		auio.uio_rw = UIO_WRITE;
1054 		auio.uio_td = td;
1055 
1056 		error = VOP_WRITE(vp, &auio, 0, td->td_ucred);
1057 		if (error != 0)
1058 			break;
1059 
1060 		len -= cur;
1061 		offset += cur;
1062 		if (len == 0)
1063 			break;
1064 		if (should_yield())
1065 			break;
1066 	}
1067 
1068  out:
1069 	*ap->a_len = len;
1070 	*ap->a_offset = offset;
1071 	free(buf, M_TEMP);
1072 	return (error);
1073 }
1074 
1075 static int
1076 vp_zerofill(struct vnode *vp, struct vattr *vap, off_t *offsetp, off_t *lenp,
1077     int ioflag, struct ucred *cred)
1078 {
1079 	int iosize;
1080 	int error = 0;
1081 	struct iovec aiov;
1082 	struct uio auio;
1083 	struct thread *td;
1084 	off_t offset, len;
1085 
1086 	iosize = vap->va_blocksize;
1087 	td = curthread;
1088 	offset = *offsetp;
1089 	len = *lenp;
1090 
1091 	if (iosize == 0)
1092 		iosize = BLKDEV_IOSIZE;
1093 	/* If va_blocksize is 512 bytes, iosize will be 4 kilobytes */
1094 	iosize = min(iosize * 8, ZERO_REGION_SIZE);
1095 
1096 	while (len > 0) {
1097 		int xfersize = iosize;
1098 		if (offset % iosize != 0)
1099 			xfersize -= offset % iosize;
1100 		if (xfersize > len)
1101 			xfersize = len;
1102 
1103 		aiov.iov_base = __DECONST(void *, zero_region);
1104 		aiov.iov_len = xfersize;
1105 		auio.uio_iov = &aiov;
1106 		auio.uio_iovcnt = 1;
1107 		auio.uio_offset = offset;
1108 		auio.uio_resid = xfersize;
1109 		auio.uio_segflg = UIO_SYSSPACE;
1110 		auio.uio_rw = UIO_WRITE;
1111 		auio.uio_td = td;
1112 
1113 		error = VOP_WRITE(vp, &auio, ioflag, cred);
1114 		if (error != 0) {
1115 			len -= xfersize - auio.uio_resid;
1116 			offset += xfersize - auio.uio_resid;
1117 			break;
1118 		}
1119 
1120 		len -= xfersize;
1121 		offset += xfersize;
1122 	}
1123 
1124 	*offsetp = offset;
1125 	*lenp = len;
1126 	return (error);
1127 }
1128 
1129 static int
1130 vop_stddeallocate(struct vop_deallocate_args *ap)
1131 {
1132 	struct vnode *vp;
1133 	off_t offset, len;
1134 	struct ucred *cred;
1135 	int error;
1136 	struct vattr va;
1137 	off_t noff, xfersize, rem;
1138 
1139 	vp = ap->a_vp;
1140 	offset = *ap->a_offset;
1141 	cred = ap->a_cred;
1142 
1143 	error = VOP_GETATTR(vp, &va, cred);
1144 	if (error)
1145 		return (error);
1146 
1147 	len = omin((off_t)va.va_size - offset, *ap->a_len);
1148 	while (len > 0) {
1149 		noff = offset;
1150 		error = vn_bmap_seekhole_locked(vp, FIOSEEKDATA, &noff, cred);
1151 		if (error) {
1152 			if (error != ENXIO)
1153 				/* XXX: Is it okay to fallback further? */
1154 				goto out;
1155 
1156 			/*
1157 			 * No more data region to be filled
1158 			 */
1159 			len = 0;
1160 			error = 0;
1161 			break;
1162 		}
1163 		KASSERT(noff >= offset, ("FIOSEEKDATA going backward"));
1164 		if (noff != offset) {
1165 			xfersize = omin(noff - offset, len);
1166 			len -= xfersize;
1167 			offset += xfersize;
1168 			if (len == 0)
1169 				break;
1170 		}
1171 		error = vn_bmap_seekhole_locked(vp, FIOSEEKHOLE, &noff, cred);
1172 		if (error)
1173 			goto out;
1174 
1175 		/* Fill zeroes */
1176 		xfersize = rem = omin(noff - offset, len);
1177 		error = vp_zerofill(vp, &va, &offset, &rem, ap->a_ioflag, cred);
1178 		if (error) {
1179 			len -= xfersize - rem;
1180 			goto out;
1181 		}
1182 
1183 		len -= xfersize;
1184 		if (should_yield())
1185 			break;
1186 	}
1187 	/* Handle the case when offset is beyond EOF */
1188 	if (len < 0) {
1189 		offset += len;
1190 		len = 0;
1191 	}
1192 out:
1193 	*ap->a_offset = offset;
1194 	*ap->a_len = len;
1195 	return (error);
1196 }
1197 
1198 int
1199 vop_stdadvise(struct vop_advise_args *ap)
1200 {
1201 	struct vnode *vp;
1202 	struct bufobj *bo;
1203 	daddr_t startn, endn;
1204 	off_t bstart, bend, start, end;
1205 	int bsize, error;
1206 
1207 	vp = ap->a_vp;
1208 	switch (ap->a_advice) {
1209 	case POSIX_FADV_WILLNEED:
1210 		/*
1211 		 * Do nothing for now.  Filesystems should provide a
1212 		 * custom method which starts an asynchronous read of
1213 		 * the requested region.
1214 		 */
1215 		error = 0;
1216 		break;
1217 	case POSIX_FADV_DONTNEED:
1218 		error = 0;
1219 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1220 		if (VN_IS_DOOMED(vp)) {
1221 			VOP_UNLOCK(vp);
1222 			break;
1223 		}
1224 
1225 		/*
1226 		 * Round to block boundaries (and later possibly further to
1227 		 * page boundaries).  Applications cannot reasonably be aware
1228 		 * of the boundaries, and the rounding must be to expand at
1229 		 * both extremities to cover enough.  It still doesn't cover
1230 		 * read-ahead.  For partial blocks, this gives unnecessary
1231 		 * discarding of buffers but is efficient enough since the
1232 		 * pages usually remain in VMIO for some time.
1233 		 */
1234 		bsize = vp->v_bufobj.bo_bsize;
1235 		bstart = rounddown(ap->a_start, bsize);
1236 		bend = roundup(ap->a_end, bsize);
1237 
1238 		/*
1239 		 * Deactivate pages in the specified range from the backing VM
1240 		 * object.  Pages that are resident in the buffer cache will
1241 		 * remain wired until their corresponding buffers are released
1242 		 * below.
1243 		 */
1244 		if (vp->v_object != NULL) {
1245 			start = trunc_page(bstart);
1246 			end = round_page(bend);
1247 			VM_OBJECT_RLOCK(vp->v_object);
1248 			vm_object_page_noreuse(vp->v_object, OFF_TO_IDX(start),
1249 			    OFF_TO_IDX(end));
1250 			VM_OBJECT_RUNLOCK(vp->v_object);
1251 		}
1252 
1253 		bo = &vp->v_bufobj;
1254 		BO_RLOCK(bo);
1255 		startn = bstart / bsize;
1256 		endn = bend / bsize;
1257 		error = bnoreuselist(&bo->bo_clean, bo, startn, endn);
1258 		if (error == 0)
1259 			error = bnoreuselist(&bo->bo_dirty, bo, startn, endn);
1260 		BO_RUNLOCK(bo);
1261 		VOP_UNLOCK(vp);
1262 		break;
1263 	default:
1264 		error = EINVAL;
1265 		break;
1266 	}
1267 	return (error);
1268 }
1269 
1270 int
1271 vop_stdunp_bind(struct vop_unp_bind_args *ap)
1272 {
1273 
1274 	ap->a_vp->v_unpcb = ap->a_unpcb;
1275 	return (0);
1276 }
1277 
1278 int
1279 vop_stdunp_connect(struct vop_unp_connect_args *ap)
1280 {
1281 
1282 	*ap->a_unpcb = ap->a_vp->v_unpcb;
1283 	return (0);
1284 }
1285 
1286 int
1287 vop_stdunp_detach(struct vop_unp_detach_args *ap)
1288 {
1289 
1290 	ap->a_vp->v_unpcb = NULL;
1291 	return (0);
1292 }
1293 
1294 static int
1295 vop_stdis_text(struct vop_is_text_args *ap)
1296 {
1297 
1298 	return (ap->a_vp->v_writecount < 0);
1299 }
1300 
1301 int
1302 vop_stdset_text(struct vop_set_text_args *ap)
1303 {
1304 	struct vnode *vp;
1305 	struct mount *mp;
1306 	int error, n;
1307 
1308 	vp = ap->a_vp;
1309 
1310 	/*
1311 	 * Avoid the interlock if execs are already present.
1312 	 */
1313 	n = atomic_load_int(&vp->v_writecount);
1314 	for (;;) {
1315 		if (n > -1) {
1316 			break;
1317 		}
1318 		if (atomic_fcmpset_int(&vp->v_writecount, &n, n - 1)) {
1319 			return (0);
1320 		}
1321 	}
1322 
1323 	VI_LOCK(vp);
1324 	if (vp->v_writecount > 0) {
1325 		error = ETXTBSY;
1326 	} else {
1327 		/*
1328 		 * If requested by fs, keep a use reference to the
1329 		 * vnode until the last text reference is released.
1330 		 */
1331 		mp = vp->v_mount;
1332 		if (mp != NULL && (mp->mnt_kern_flag & MNTK_TEXT_REFS) != 0 &&
1333 		    vp->v_writecount == 0) {
1334 			VNPASS((vp->v_iflag & VI_TEXT_REF) == 0, vp);
1335 			vp->v_iflag |= VI_TEXT_REF;
1336 			vrefl(vp);
1337 		}
1338 
1339 		atomic_subtract_int(&vp->v_writecount, 1);
1340 		error = 0;
1341 	}
1342 	VI_UNLOCK(vp);
1343 	return (error);
1344 }
1345 
1346 static int
1347 vop_stdunset_text(struct vop_unset_text_args *ap)
1348 {
1349 	struct vnode *vp;
1350 	int error, n;
1351 	bool last;
1352 
1353 	vp = ap->a_vp;
1354 
1355 	/*
1356 	 * Avoid the interlock if this is not the last exec.
1357 	 */
1358 	n = atomic_load_int(&vp->v_writecount);
1359 	for (;;) {
1360 		if (n >= -1) {
1361 			break;
1362 		}
1363 		if (atomic_fcmpset_int(&vp->v_writecount, &n, n + 1)) {
1364 			return (0);
1365 		}
1366 	}
1367 
1368 	last = false;
1369 	VI_LOCK(vp);
1370 	if (vp->v_writecount < 0) {
1371 		if ((vp->v_iflag & VI_TEXT_REF) != 0 &&
1372 		    vp->v_writecount == -1) {
1373 			last = true;
1374 			vp->v_iflag &= ~VI_TEXT_REF;
1375 		}
1376 		atomic_add_int(&vp->v_writecount, 1);
1377 		error = 0;
1378 	} else {
1379 		error = EINVAL;
1380 	}
1381 	VI_UNLOCK(vp);
1382 	if (last)
1383 		vunref(vp);
1384 	return (error);
1385 }
1386 
1387 static int
1388 vop_stdadd_writecount(struct vop_add_writecount_args *ap)
1389 {
1390 	struct vnode *vp;
1391 	struct mount *mp;
1392 	int error;
1393 
1394 	vp = ap->a_vp;
1395 	VI_LOCK_FLAGS(vp, MTX_DUPOK);
1396 	if (vp->v_writecount < 0) {
1397 		error = ETXTBSY;
1398 	} else {
1399 		VNASSERT(vp->v_writecount + ap->a_inc >= 0, vp,
1400 		    ("neg writecount increment %d", ap->a_inc));
1401 		if (vp->v_writecount == 0) {
1402 			mp = vp->v_mount;
1403 			if (mp != NULL && (mp->mnt_kern_flag & MNTK_NOMSYNC) == 0)
1404 				vlazy(vp);
1405 		}
1406 		vp->v_writecount += ap->a_inc;
1407 		error = 0;
1408 	}
1409 	VI_UNLOCK(vp);
1410 	return (error);
1411 }
1412 
1413 int
1414 vop_stdneed_inactive(struct vop_need_inactive_args *ap)
1415 {
1416 
1417 	return (1);
1418 }
1419 
1420 int
1421 vop_stdioctl(struct vop_ioctl_args *ap)
1422 {
1423 	struct vnode *vp;
1424 	struct vattr va;
1425 	off_t *offp;
1426 	int error;
1427 
1428 	switch (ap->a_command) {
1429 	case FIOSEEKDATA:
1430 	case FIOSEEKHOLE:
1431 		vp = ap->a_vp;
1432 		error = vn_lock(vp, LK_SHARED);
1433 		if (error != 0)
1434 			return (EBADF);
1435 		if (vp->v_type == VREG)
1436 			error = VOP_GETATTR(vp, &va, ap->a_cred);
1437 		else
1438 			error = ENOTTY;
1439 		if (error == 0) {
1440 			offp = ap->a_data;
1441 			if (*offp < 0 || *offp >= va.va_size)
1442 				error = ENXIO;
1443 			else if (ap->a_command == FIOSEEKHOLE)
1444 				*offp = va.va_size;
1445 		}
1446 		VOP_UNLOCK(vp);
1447 		break;
1448 	default:
1449 		error = ENOTTY;
1450 		break;
1451 	}
1452 	return (error);
1453 }
1454 
1455 /*
1456  * vfs default ops
1457  * used to fill the vfs function table to get reasonable default return values.
1458  */
1459 int
1460 vfs_stdroot (mp, flags, vpp)
1461 	struct mount *mp;
1462 	int flags;
1463 	struct vnode **vpp;
1464 {
1465 
1466 	return (EOPNOTSUPP);
1467 }
1468 
1469 int
1470 vfs_stdstatfs (mp, sbp)
1471 	struct mount *mp;
1472 	struct statfs *sbp;
1473 {
1474 
1475 	return (EOPNOTSUPP);
1476 }
1477 
1478 int
1479 vfs_stdquotactl (mp, cmds, uid, arg, mp_busy)
1480 	struct mount *mp;
1481 	int cmds;
1482 	uid_t uid;
1483 	void *arg;
1484 	bool *mp_busy;
1485 {
1486 	return (EOPNOTSUPP);
1487 }
1488 
1489 int
1490 vfs_stdsync(mp, waitfor)
1491 	struct mount *mp;
1492 	int waitfor;
1493 {
1494 	struct vnode *vp, *mvp;
1495 	struct thread *td;
1496 	int error, lockreq, allerror = 0;
1497 
1498 	td = curthread;
1499 	lockreq = LK_EXCLUSIVE | LK_INTERLOCK;
1500 	if (waitfor != MNT_WAIT)
1501 		lockreq |= LK_NOWAIT;
1502 	/*
1503 	 * Force stale buffer cache information to be flushed.
1504 	 */
1505 loop:
1506 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1507 		if (vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1508 			VI_UNLOCK(vp);
1509 			continue;
1510 		}
1511 		if ((error = vget(vp, lockreq)) != 0) {
1512 			if (error == ENOENT) {
1513 				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1514 				goto loop;
1515 			}
1516 			continue;
1517 		}
1518 		error = VOP_FSYNC(vp, waitfor, td);
1519 		if (error)
1520 			allerror = error;
1521 		vput(vp);
1522 	}
1523 	return (allerror);
1524 }
1525 
1526 int
1527 vfs_stdnosync (mp, waitfor)
1528 	struct mount *mp;
1529 	int waitfor;
1530 {
1531 
1532 	return (0);
1533 }
1534 
1535 static int
1536 vop_stdcopy_file_range(struct vop_copy_file_range_args *ap)
1537 {
1538 	int error;
1539 
1540 	error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
1541 	    ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags, ap->a_incred,
1542 	    ap->a_outcred, ap->a_fsizetd);
1543 	return (error);
1544 }
1545 
1546 int
1547 vfs_stdvget (mp, ino, flags, vpp)
1548 	struct mount *mp;
1549 	ino_t ino;
1550 	int flags;
1551 	struct vnode **vpp;
1552 {
1553 
1554 	return (EOPNOTSUPP);
1555 }
1556 
1557 int
1558 vfs_stdfhtovp (mp, fhp, flags, vpp)
1559 	struct mount *mp;
1560 	struct fid *fhp;
1561 	int flags;
1562 	struct vnode **vpp;
1563 {
1564 
1565 	return (EOPNOTSUPP);
1566 }
1567 
1568 int
1569 vfs_stdinit (vfsp)
1570 	struct vfsconf *vfsp;
1571 {
1572 
1573 	return (0);
1574 }
1575 
1576 int
1577 vfs_stduninit (vfsp)
1578 	struct vfsconf *vfsp;
1579 {
1580 
1581 	return(0);
1582 }
1583 
1584 int
1585 vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, attrname)
1586 	struct mount *mp;
1587 	int cmd;
1588 	struct vnode *filename_vp;
1589 	int attrnamespace;
1590 	const char *attrname;
1591 {
1592 
1593 	if (filename_vp != NULL)
1594 		VOP_UNLOCK(filename_vp);
1595 	return (EOPNOTSUPP);
1596 }
1597 
1598 int
1599 vfs_stdsysctl(mp, op, req)
1600 	struct mount *mp;
1601 	fsctlop_t op;
1602 	struct sysctl_req *req;
1603 {
1604 
1605 	return (EOPNOTSUPP);
1606 }
1607 
1608 static vop_bypass_t *
1609 bp_by_off(struct vop_vector *vop, struct vop_generic_args *a)
1610 {
1611 
1612 	return (*(vop_bypass_t **)((char *)vop + a->a_desc->vdesc_vop_offset));
1613 }
1614 
1615 int
1616 vop_sigdefer(struct vop_vector *vop, struct vop_generic_args *a)
1617 {
1618 	vop_bypass_t *bp;
1619 	int prev_stops, rc;
1620 
1621 	bp = bp_by_off(vop, a);
1622 	MPASS(bp != NULL);
1623 
1624 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
1625 	rc = bp(a);
1626 	sigallowstop(prev_stops);
1627 	return (rc);
1628 }
1629 
1630 static int
1631 vop_stdstat(struct vop_stat_args *a)
1632 {
1633 	struct vattr vattr;
1634 	struct vattr *vap;
1635 	struct vnode *vp;
1636 	struct stat *sb;
1637 	int error;
1638 	u_short mode;
1639 
1640 	vp = a->a_vp;
1641 	sb = a->a_sb;
1642 
1643 	error = vop_stat_helper_pre(a);
1644 	if (error != 0)
1645 		return (error);
1646 
1647 	vap = &vattr;
1648 
1649 	/*
1650 	 * Initialize defaults for new and unusual fields, so that file
1651 	 * systems which don't support these fields don't need to know
1652 	 * about them.
1653 	 */
1654 	vap->va_birthtime.tv_sec = -1;
1655 	vap->va_birthtime.tv_nsec = 0;
1656 	vap->va_fsid = VNOVAL;
1657 	vap->va_gen = 0;
1658 	vap->va_rdev = NODEV;
1659 
1660 	error = VOP_GETATTR(vp, vap, a->a_active_cred);
1661 	if (error)
1662 		goto out;
1663 
1664 	/*
1665 	 * Zero the spare stat fields
1666 	 */
1667 	bzero(sb, sizeof *sb);
1668 
1669 	/*
1670 	 * Copy from vattr table
1671 	 */
1672 	if (vap->va_fsid != VNOVAL)
1673 		sb->st_dev = vap->va_fsid;
1674 	else
1675 		sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1676 	sb->st_ino = vap->va_fileid;
1677 	mode = vap->va_mode;
1678 	switch (vap->va_type) {
1679 	case VREG:
1680 		mode |= S_IFREG;
1681 		break;
1682 	case VDIR:
1683 		mode |= S_IFDIR;
1684 		break;
1685 	case VBLK:
1686 		mode |= S_IFBLK;
1687 		break;
1688 	case VCHR:
1689 		mode |= S_IFCHR;
1690 		break;
1691 	case VLNK:
1692 		mode |= S_IFLNK;
1693 		break;
1694 	case VSOCK:
1695 		mode |= S_IFSOCK;
1696 		break;
1697 	case VFIFO:
1698 		mode |= S_IFIFO;
1699 		break;
1700 	default:
1701 		error = EBADF;
1702 		goto out;
1703 	}
1704 	sb->st_mode = mode;
1705 	sb->st_nlink = vap->va_nlink;
1706 	sb->st_uid = vap->va_uid;
1707 	sb->st_gid = vap->va_gid;
1708 	sb->st_rdev = vap->va_rdev;
1709 	if (vap->va_size > OFF_MAX) {
1710 		error = EOVERFLOW;
1711 		goto out;
1712 	}
1713 	sb->st_size = vap->va_size;
1714 	sb->st_atim.tv_sec = vap->va_atime.tv_sec;
1715 	sb->st_atim.tv_nsec = vap->va_atime.tv_nsec;
1716 	sb->st_mtim.tv_sec = vap->va_mtime.tv_sec;
1717 	sb->st_mtim.tv_nsec = vap->va_mtime.tv_nsec;
1718 	sb->st_ctim.tv_sec = vap->va_ctime.tv_sec;
1719 	sb->st_ctim.tv_nsec = vap->va_ctime.tv_nsec;
1720 	sb->st_birthtim.tv_sec = vap->va_birthtime.tv_sec;
1721 	sb->st_birthtim.tv_nsec = vap->va_birthtime.tv_nsec;
1722 
1723 	/*
1724 	 * According to www.opengroup.org, the meaning of st_blksize is
1725 	 *   "a filesystem-specific preferred I/O block size for this
1726 	 *    object.  In some filesystem types, this may vary from file
1727 	 *    to file"
1728 	 * Use minimum/default of PAGE_SIZE (e.g. for VCHR).
1729 	 */
1730 
1731 	sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize);
1732 	sb->st_flags = vap->va_flags;
1733 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1734 	sb->st_gen = vap->va_gen;
1735 out:
1736 	return (vop_stat_helper_post(a, error));
1737 }
1738 
1739 static int
1740 vop_stdread_pgcache(struct vop_read_pgcache_args *ap __unused)
1741 {
1742 	return (EJUSTRETURN);
1743 }
1744 
1745 static int
1746 vop_stdvput_pair(struct vop_vput_pair_args *ap)
1747 {
1748 	struct vnode *dvp, *vp, **vpp;
1749 
1750 	dvp = ap->a_dvp;
1751 	vpp = ap->a_vpp;
1752 	vput(dvp);
1753 	if (vpp != NULL && ap->a_unlock_vp && (vp = *vpp) != NULL)
1754 		vput(vp);
1755 	return (0);
1756 }
1757