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