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