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 case _PC_CASE_INSENSITIVE:
462 *ap->a_retval = 0;
463 return (0);
464 default:
465 return (EINVAL);
466 }
467 /* NOTREACHED */
468 }
469
470 /*
471 * Standard lock, unlock and islocked functions.
472 */
473 int
vop_stdlock(struct vop_lock1_args * ap)474 vop_stdlock(struct vop_lock1_args *ap)
475 {
476 struct vnode *vp = ap->a_vp;
477 struct mtx *ilk;
478
479 ilk = VI_MTX(vp);
480 return (lockmgr_lock_flags(vp->v_vnlock, ap->a_flags,
481 &ilk->lock_object, ap->a_file, ap->a_line));
482 }
483
484 /* See above. */
485 int
vop_stdunlock(struct vop_unlock_args * ap)486 vop_stdunlock(struct vop_unlock_args *ap)
487 {
488 struct vnode *vp = ap->a_vp;
489
490 return (lockmgr_unlock(vp->v_vnlock));
491 }
492
493 /* See above. */
494 int
vop_stdislocked(struct vop_islocked_args * ap)495 vop_stdislocked(struct vop_islocked_args *ap)
496 {
497
498 return (lockstatus(ap->a_vp->v_vnlock));
499 }
500
501 /*
502 * Variants of the above set.
503 *
504 * Differences are:
505 * - shared locking disablement is not supported
506 * - v_vnlock pointer is not honored
507 */
508 int
vop_lock(struct vop_lock1_args * ap)509 vop_lock(struct vop_lock1_args *ap)
510 {
511 struct vnode *vp = ap->a_vp;
512 int flags = ap->a_flags;
513 struct mtx *ilk;
514
515 MPASS(vp->v_vnlock == &vp->v_lock);
516
517 if (__predict_false((flags & ~(LK_TYPE_MASK | LK_NODDLKTREAT | LK_RETRY)) != 0))
518 goto other;
519
520 switch (flags & LK_TYPE_MASK) {
521 case LK_SHARED:
522 return (lockmgr_slock(&vp->v_lock, flags, ap->a_file, ap->a_line));
523 case LK_EXCLUSIVE:
524 return (lockmgr_xlock(&vp->v_lock, flags, ap->a_file, ap->a_line));
525 }
526 other:
527 ilk = VI_MTX(vp);
528 return (lockmgr_lock_flags(&vp->v_lock, flags,
529 &ilk->lock_object, ap->a_file, ap->a_line));
530 }
531
532 int
vop_unlock(struct vop_unlock_args * ap)533 vop_unlock(struct vop_unlock_args *ap)
534 {
535 struct vnode *vp = ap->a_vp;
536
537 MPASS(vp->v_vnlock == &vp->v_lock);
538
539 return (lockmgr_unlock(&vp->v_lock));
540 }
541
542 int
vop_islocked(struct vop_islocked_args * ap)543 vop_islocked(struct vop_islocked_args *ap)
544 {
545 struct vnode *vp = ap->a_vp;
546
547 MPASS(vp->v_vnlock == &vp->v_lock);
548
549 return (lockstatus(&vp->v_lock));
550 }
551
552 /*
553 * Return true for select/poll.
554 */
555 int
vop_nopoll(struct vop_poll_args * ap)556 vop_nopoll(struct vop_poll_args *ap)
557 {
558
559 if (ap->a_events & ~POLLSTANDARD)
560 return (POLLNVAL);
561 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
562 }
563
564 /*
565 * Implement poll for local filesystems that support it.
566 */
567 int
vop_stdpoll(struct vop_poll_args * ap)568 vop_stdpoll(struct vop_poll_args *ap)
569 {
570 if (ap->a_events & ~POLLSTANDARD)
571 return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
572 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
573 }
574
575 /*
576 * Return our mount point, as we will take charge of the writes.
577 */
578 int
vop_stdgetwritemount(struct vop_getwritemount_args * ap)579 vop_stdgetwritemount(struct vop_getwritemount_args *ap)
580 {
581 struct mount *mp;
582 struct vnode *vp;
583
584 /*
585 * Note that having a reference does not prevent forced unmount from
586 * setting ->v_mount to NULL after the lock gets released. This is of
587 * no consequence for typical consumers (most notably vn_start_write)
588 * since in this case the vnode is VIRF_DOOMED. Unmount might have
589 * progressed far enough that its completion is only delayed by the
590 * reference obtained here. The consumer only needs to concern itself
591 * with releasing it.
592 */
593 vp = ap->a_vp;
594 mp = vfs_ref_from_vp(vp);
595 *(ap->a_mpp) = mp;
596 return (0);
597 }
598
599 /*
600 * If the file system doesn't implement VOP_BMAP, then return sensible defaults:
601 * - Return the vnode's bufobj instead of any underlying device's bufobj
602 * - Calculate the physical block number as if there were equal size
603 * consecutive blocks, but
604 * - Report no contiguous runs of blocks.
605 */
606 int
vop_stdbmap(struct vop_bmap_args * ap)607 vop_stdbmap(struct vop_bmap_args *ap)
608 {
609
610 if (ap->a_bop != NULL)
611 *ap->a_bop = &ap->a_vp->v_bufobj;
612 if (ap->a_bnp != NULL)
613 *ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize);
614 if (ap->a_runp != NULL)
615 *ap->a_runp = 0;
616 if (ap->a_runb != NULL)
617 *ap->a_runb = 0;
618 return (0);
619 }
620
621 int
vop_stdfsync(struct vop_fsync_args * ap)622 vop_stdfsync(struct vop_fsync_args *ap)
623 {
624
625 return (vn_fsync_buf(ap->a_vp, ap->a_waitfor));
626 }
627
628 static int
vop_stdfdatasync(struct vop_fdatasync_args * ap)629 vop_stdfdatasync(struct vop_fdatasync_args *ap)
630 {
631
632 return (VOP_FSYNC(ap->a_vp, MNT_WAIT, ap->a_td));
633 }
634
635 int
vop_stdfdatasync_buf(struct vop_fdatasync_args * ap)636 vop_stdfdatasync_buf(struct vop_fdatasync_args *ap)
637 {
638
639 return (vn_fsync_buf(ap->a_vp, MNT_WAIT));
640 }
641
642 /* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */
643 int
vop_stdgetpages(struct vop_getpages_args * ap)644 vop_stdgetpages(struct vop_getpages_args *ap)
645 {
646
647 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
648 ap->a_count, ap->a_rbehind, ap->a_rahead, NULL, NULL);
649 }
650
651 static int
vop_stdgetpages_async(struct vop_getpages_async_args * ap)652 vop_stdgetpages_async(struct vop_getpages_async_args *ap)
653 {
654 int error;
655
656 error = VOP_GETPAGES(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
657 ap->a_rahead);
658 if (ap->a_iodone != NULL)
659 ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
660 return (error);
661 }
662
663 int
vop_stdkqfilter(struct vop_kqfilter_args * ap)664 vop_stdkqfilter(struct vop_kqfilter_args *ap)
665 {
666 return vfs_kqfilter(ap);
667 }
668
669 /* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */
670 int
vop_stdputpages(struct vop_putpages_args * ap)671 vop_stdputpages(struct vop_putpages_args *ap)
672 {
673
674 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
675 ap->a_sync, ap->a_rtvals);
676 }
677
678 int
vop_stdvptofh(struct vop_vptofh_args * ap)679 vop_stdvptofh(struct vop_vptofh_args *ap)
680 {
681 return (EOPNOTSUPP);
682 }
683
684 int
vop_stdvptocnp(struct vop_vptocnp_args * ap)685 vop_stdvptocnp(struct vop_vptocnp_args *ap)
686 {
687 struct vnode *const vp = ap->a_vp;
688 struct vnode **const dvp = ap->a_vpp;
689 char *buf = ap->a_buf;
690 size_t *buflen = ap->a_buflen;
691 char *dirbuf;
692 int i = *buflen;
693 int error = 0, covered = 0;
694 int eofflag, flags, locked;
695 size_t dirbuflen, len;
696 off_t off;
697 ino_t fileno;
698 struct vattr va;
699 struct nameidata nd;
700 struct thread *const td = curthread;
701 struct ucred *const cred = td->td_ucred;
702 struct dirent *dp;
703 struct vnode *mvp;
704
705 if (vp->v_type != VDIR)
706 return (ENOENT);
707
708 error = VOP_GETATTR(vp, &va, cred);
709 if (error)
710 return (error);
711
712 vref(vp);
713 locked = VOP_ISLOCKED(vp);
714 VOP_UNLOCK(vp);
715 NDINIT_ATVP(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
716 "..", vp);
717 flags = FREAD;
718 error = vn_open_cred(&nd, &flags, 0, VN_OPEN_NOAUDIT, cred, NULL);
719 if (error) {
720 vn_lock(vp, locked | LK_RETRY);
721 return (error);
722 }
723 NDFREE_PNBUF(&nd);
724
725 mvp = *dvp = nd.ni_vp;
726
727 if (vp->v_mount != (*dvp)->v_mount &&
728 ((*dvp)->v_vflag & VV_ROOT) &&
729 ((*dvp)->v_mount->mnt_flag & MNT_UNION)) {
730 *dvp = (*dvp)->v_mount->mnt_vnodecovered;
731 vref(mvp);
732 VOP_UNLOCK(mvp);
733 vn_close(mvp, FREAD, cred, td);
734 vref(*dvp);
735 vn_lock(*dvp, LK_SHARED | LK_RETRY);
736 covered = 1;
737 }
738
739 fileno = va.va_fileid;
740
741 dirbuflen = MAX(DEV_BSIZE, GENERIC_MAXDIRSIZ);
742 if (dirbuflen < va.va_blocksize)
743 dirbuflen = va.va_blocksize;
744 dirbuf = malloc(dirbuflen, M_TEMP, M_WAITOK);
745
746 if ((*dvp)->v_type != VDIR) {
747 error = ENOENT;
748 goto out;
749 }
750
751 len = 0;
752 off = 0;
753 eofflag = 0;
754
755 for (;;) {
756 /* call VOP_READDIR of parent */
757 error = vn_dir_next_dirent(*dvp, td,
758 dirbuf, dirbuflen, &dp, &len, &off, &eofflag);
759 if (error != 0)
760 goto out;
761
762 if (len == 0) {
763 error = ENOENT;
764 goto out;
765 }
766
767 if ((dp->d_type != DT_WHT) &&
768 (dp->d_fileno == fileno)) {
769 if (covered) {
770 VOP_UNLOCK(*dvp);
771 vn_lock(mvp, LK_SHARED | LK_RETRY);
772 if (dirent_exists(mvp, dp->d_name, td) == 0) {
773 error = ENOENT;
774 VOP_UNLOCK(mvp);
775 vn_lock(*dvp, LK_SHARED | LK_RETRY);
776 goto out;
777 }
778 VOP_UNLOCK(mvp);
779 vn_lock(*dvp, LK_SHARED | LK_RETRY);
780 }
781 i -= dp->d_namlen;
782
783 if (i < 0) {
784 error = ENOMEM;
785 goto out;
786 }
787 if (dp->d_namlen == 1 && dp->d_name[0] == '.') {
788 error = ENOENT;
789 } else {
790 bcopy(dp->d_name, buf + i, dp->d_namlen);
791 error = 0;
792 }
793 goto out;
794 }
795 }
796
797 out:
798 free(dirbuf, M_TEMP);
799 if (!error) {
800 *buflen = i;
801 vref(*dvp);
802 }
803 if (covered) {
804 vput(*dvp);
805 vrele(mvp);
806 } else {
807 VOP_UNLOCK(mvp);
808 vn_close(mvp, FREAD, cred, td);
809 }
810 vn_lock(vp, locked | LK_RETRY);
811 return (error);
812 }
813
814 int
vop_stdallocate(struct vop_allocate_args * ap)815 vop_stdallocate(struct vop_allocate_args *ap)
816 {
817 #ifdef __notyet__
818 struct statfs *sfs;
819 off_t maxfilesize = 0;
820 #endif
821 struct iovec aiov;
822 struct vattr vattr, *vap;
823 struct uio auio;
824 off_t fsize, len, cur, offset;
825 uint8_t *buf;
826 struct thread *td;
827 struct vnode *vp;
828 size_t iosize;
829 int error;
830
831 buf = NULL;
832 error = 0;
833 td = curthread;
834 vap = &vattr;
835 vp = ap->a_vp;
836 len = *ap->a_len;
837 offset = *ap->a_offset;
838
839 error = VOP_GETATTR(vp, vap, ap->a_cred);
840 if (error != 0)
841 goto out;
842 fsize = vap->va_size;
843 iosize = vap->va_blocksize;
844 if (iosize == 0)
845 iosize = BLKDEV_IOSIZE;
846 if (iosize > maxphys)
847 iosize = maxphys;
848 buf = malloc(iosize, M_TEMP, M_WAITOK);
849
850 #ifdef __notyet__
851 /*
852 * Check if the filesystem sets f_maxfilesize; if not use
853 * VOP_SETATTR to perform the check.
854 */
855 sfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
856 error = VFS_STATFS(vp->v_mount, sfs, td);
857 if (error == 0)
858 maxfilesize = sfs->f_maxfilesize;
859 free(sfs, M_STATFS);
860 if (error != 0)
861 goto out;
862 if (maxfilesize) {
863 if (offset > maxfilesize || len > maxfilesize ||
864 offset + len > maxfilesize) {
865 error = EFBIG;
866 goto out;
867 }
868 } else
869 #endif
870 if (offset + len > vap->va_size) {
871 /*
872 * Test offset + len against the filesystem's maxfilesize.
873 */
874 VATTR_NULL(vap);
875 vap->va_size = offset + len;
876 error = VOP_SETATTR(vp, vap, ap->a_cred);
877 if (error != 0)
878 goto out;
879 VATTR_NULL(vap);
880 vap->va_size = fsize;
881 error = VOP_SETATTR(vp, vap, ap->a_cred);
882 if (error != 0)
883 goto out;
884 }
885
886 for (;;) {
887 /*
888 * Read and write back anything below the nominal file
889 * size. There's currently no way outside the filesystem
890 * to know whether this area is sparse or not.
891 */
892 cur = iosize;
893 if ((offset % iosize) != 0)
894 cur -= (offset % iosize);
895 if (cur > len)
896 cur = len;
897 if (offset < fsize) {
898 aiov.iov_base = buf;
899 aiov.iov_len = cur;
900 auio.uio_iov = &aiov;
901 auio.uio_iovcnt = 1;
902 auio.uio_offset = offset;
903 auio.uio_resid = cur;
904 auio.uio_segflg = UIO_SYSSPACE;
905 auio.uio_rw = UIO_READ;
906 auio.uio_td = td;
907 error = VOP_READ(vp, &auio, ap->a_ioflag, ap->a_cred);
908 if (error != 0)
909 break;
910 if (auio.uio_resid > 0) {
911 bzero(buf + cur - auio.uio_resid,
912 auio.uio_resid);
913 }
914 } else {
915 bzero(buf, cur);
916 }
917
918 aiov.iov_base = buf;
919 aiov.iov_len = cur;
920 auio.uio_iov = &aiov;
921 auio.uio_iovcnt = 1;
922 auio.uio_offset = offset;
923 auio.uio_resid = cur;
924 auio.uio_segflg = UIO_SYSSPACE;
925 auio.uio_rw = UIO_WRITE;
926 auio.uio_td = td;
927
928 error = VOP_WRITE(vp, &auio, ap->a_ioflag, ap->a_cred);
929 if (error != 0)
930 break;
931
932 len -= cur;
933 offset += cur;
934 if (len == 0)
935 break;
936 if (should_yield())
937 break;
938 }
939
940 out:
941 *ap->a_len = len;
942 *ap->a_offset = offset;
943 free(buf, M_TEMP);
944 return (error);
945 }
946
947 static int
vp_zerofill(struct vnode * vp,struct vattr * vap,off_t * offsetp,off_t * lenp,int ioflag,struct ucred * cred)948 vp_zerofill(struct vnode *vp, struct vattr *vap, off_t *offsetp, off_t *lenp,
949 int ioflag, struct ucred *cred)
950 {
951 int iosize;
952 int error = 0;
953 struct iovec aiov;
954 struct uio auio;
955 struct thread *td;
956 off_t offset, len;
957
958 iosize = vap->va_blocksize;
959 td = curthread;
960 offset = *offsetp;
961 len = *lenp;
962
963 if (iosize == 0)
964 iosize = BLKDEV_IOSIZE;
965 /* If va_blocksize is 512 bytes, iosize will be 4 kilobytes */
966 iosize = min(iosize * 8, ZERO_REGION_SIZE);
967
968 while (len > 0) {
969 int xfersize = iosize;
970 if (offset % iosize != 0)
971 xfersize -= offset % iosize;
972 if (xfersize > len)
973 xfersize = len;
974
975 aiov.iov_base = __DECONST(void *, zero_region);
976 aiov.iov_len = xfersize;
977 auio.uio_iov = &aiov;
978 auio.uio_iovcnt = 1;
979 auio.uio_offset = offset;
980 auio.uio_resid = xfersize;
981 auio.uio_segflg = UIO_SYSSPACE;
982 auio.uio_rw = UIO_WRITE;
983 auio.uio_td = td;
984
985 error = VOP_WRITE(vp, &auio, ioflag, cred);
986 if (error != 0) {
987 len -= xfersize - auio.uio_resid;
988 offset += xfersize - auio.uio_resid;
989 break;
990 }
991
992 len -= xfersize;
993 offset += xfersize;
994 }
995
996 *offsetp = offset;
997 *lenp = len;
998 return (error);
999 }
1000
1001 int
vop_stddeallocate(struct vop_deallocate_args * ap)1002 vop_stddeallocate(struct vop_deallocate_args *ap)
1003 {
1004 struct vnode *vp;
1005 off_t offset, len;
1006 struct ucred *cred;
1007 int error;
1008 struct vattr va;
1009 off_t noff, xfersize, rem;
1010
1011 vp = ap->a_vp;
1012 offset = *ap->a_offset;
1013 cred = ap->a_cred;
1014
1015 error = VOP_GETATTR(vp, &va, cred);
1016 if (error)
1017 return (error);
1018
1019 len = omin((off_t)va.va_size - offset, *ap->a_len);
1020 while (len > 0) {
1021 noff = offset;
1022 error = vn_bmap_seekhole_locked(vp, FIOSEEKDATA, &noff, cred);
1023 if (error) {
1024 if (error != ENXIO)
1025 /* XXX: Is it okay to fallback further? */
1026 goto out;
1027
1028 /*
1029 * No more data region to be filled
1030 */
1031 offset += len;
1032 len = 0;
1033 error = 0;
1034 break;
1035 }
1036 KASSERT(noff >= offset, ("FIOSEEKDATA going backward"));
1037 if (noff != offset) {
1038 xfersize = omin(noff - offset, len);
1039 len -= xfersize;
1040 offset += xfersize;
1041 if (len == 0)
1042 break;
1043 }
1044 error = vn_bmap_seekhole_locked(vp, FIOSEEKHOLE, &noff, cred);
1045 if (error)
1046 goto out;
1047
1048 /* Fill zeroes */
1049 xfersize = rem = omin(noff - offset, len);
1050 error = vp_zerofill(vp, &va, &offset, &rem, ap->a_ioflag, cred);
1051 if (error) {
1052 len -= xfersize - rem;
1053 goto out;
1054 }
1055
1056 len -= xfersize;
1057 if (should_yield())
1058 break;
1059 }
1060 /* Handle the case when offset is beyond EOF */
1061 if (len < 0)
1062 len = 0;
1063 out:
1064 *ap->a_offset = offset;
1065 *ap->a_len = len;
1066 return (error);
1067 }
1068
1069 int
vop_stdadvise(struct vop_advise_args * ap)1070 vop_stdadvise(struct vop_advise_args *ap)
1071 {
1072 struct vnode *vp;
1073 struct bufobj *bo;
1074 uintmax_t bstart, bend;
1075 daddr_t startn, endn;
1076 int bsize, error;
1077
1078 vp = ap->a_vp;
1079 switch (ap->a_advice) {
1080 case POSIX_FADV_WILLNEED:
1081 /*
1082 * Do nothing for now. Filesystems should provide a
1083 * custom method which starts an asynchronous read of
1084 * the requested region.
1085 */
1086 error = 0;
1087 break;
1088 case POSIX_FADV_DONTNEED:
1089 error = 0;
1090 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1091 if (VN_IS_DOOMED(vp)) {
1092 VOP_UNLOCK(vp);
1093 break;
1094 }
1095
1096 /*
1097 * Round to block boundaries (and later possibly further to
1098 * page boundaries). Applications cannot reasonably be aware
1099 * of the boundaries, and the rounding must be to expand at
1100 * both extremities to cover enough. It still doesn't cover
1101 * read-ahead. For partial blocks, this gives unnecessary
1102 * discarding of buffers but is efficient enough since the
1103 * pages usually remain in VMIO for some time.
1104 */
1105 bsize = vp->v_bufobj.bo_bsize;
1106 bstart = rounddown(ap->a_start, bsize);
1107 bend = ap->a_end;
1108 bend = roundup(bend, bsize);
1109
1110 /*
1111 * Deactivate pages in the specified range from the backing VM
1112 * object. Pages that are resident in the buffer cache will
1113 * remain wired until their corresponding buffers are released
1114 * below.
1115 */
1116 if (vp->v_object != NULL) {
1117 VM_OBJECT_RLOCK(vp->v_object);
1118 vm_object_page_noreuse(vp->v_object,
1119 OFF_TO_IDX(trunc_page(bstart)),
1120 OFF_TO_IDX(round_page(bend)));
1121 VM_OBJECT_RUNLOCK(vp->v_object);
1122 }
1123
1124 bo = &vp->v_bufobj;
1125 startn = bstart / bsize;
1126 endn = bend / bsize;
1127 BO_RLOCK(bo);
1128 error = bnoreuselist(&bo->bo_clean, bo, startn, endn);
1129 if (error == 0)
1130 error = bnoreuselist(&bo->bo_dirty, bo, startn, endn);
1131 BO_RUNLOCK(bo);
1132 VOP_UNLOCK(vp);
1133 break;
1134 default:
1135 error = EINVAL;
1136 break;
1137 }
1138 return (error);
1139 }
1140
1141 int
vop_stdunp_bind(struct vop_unp_bind_args * ap)1142 vop_stdunp_bind(struct vop_unp_bind_args *ap)
1143 {
1144
1145 ap->a_vp->v_unpcb = ap->a_unpcb;
1146 return (0);
1147 }
1148
1149 int
vop_stdunp_connect(struct vop_unp_connect_args * ap)1150 vop_stdunp_connect(struct vop_unp_connect_args *ap)
1151 {
1152
1153 *ap->a_unpcb = ap->a_vp->v_unpcb;
1154 return (0);
1155 }
1156
1157 int
vop_stdunp_detach(struct vop_unp_detach_args * ap)1158 vop_stdunp_detach(struct vop_unp_detach_args *ap)
1159 {
1160
1161 ap->a_vp->v_unpcb = NULL;
1162 return (0);
1163 }
1164
1165 static int
vop_stdis_text(struct vop_is_text_args * ap)1166 vop_stdis_text(struct vop_is_text_args *ap)
1167 {
1168
1169 return ((int)atomic_load_int(&ap->a_vp->v_writecount) < 0);
1170 }
1171
1172 int
vop_stdset_text(struct vop_set_text_args * ap)1173 vop_stdset_text(struct vop_set_text_args *ap)
1174 {
1175 struct vnode *vp;
1176 int n;
1177 bool gotref;
1178
1179 vp = ap->a_vp;
1180
1181 n = atomic_load_int(&vp->v_writecount);
1182 for (;;) {
1183 if (__predict_false(n > 0)) {
1184 return (ETXTBSY);
1185 }
1186
1187 /*
1188 * Transition point, we may need to grab a reference on the vnode.
1189 *
1190 * Take the ref early As a safety measure against bogus calls
1191 * to vop_stdunset_text.
1192 */
1193 if (n == 0) {
1194 gotref = false;
1195 if ((vn_irflag_read(vp) & VIRF_TEXT_REF) != 0) {
1196 vref(vp);
1197 gotref = true;
1198 }
1199 if (atomic_fcmpset_int(&vp->v_writecount, &n, -1)) {
1200 return (0);
1201 }
1202 if (gotref) {
1203 vunref(vp);
1204 }
1205 continue;
1206 }
1207
1208 MPASS(n < 0);
1209 if (atomic_fcmpset_int(&vp->v_writecount, &n, n - 1)) {
1210 return (0);
1211 }
1212 }
1213 __assert_unreachable();
1214 }
1215
1216 static int
vop_stdunset_text(struct vop_unset_text_args * ap)1217 vop_stdunset_text(struct vop_unset_text_args *ap)
1218 {
1219 struct vnode *vp;
1220 int n;
1221
1222 vp = ap->a_vp;
1223
1224 n = atomic_load_int(&vp->v_writecount);
1225 for (;;) {
1226 if (__predict_false(n >= 0)) {
1227 return (EINVAL);
1228 }
1229
1230 /*
1231 * Transition point, we may need to release a reference on the vnode.
1232 */
1233 if (n == -1) {
1234 if (atomic_fcmpset_int(&vp->v_writecount, &n, 0)) {
1235 if ((vn_irflag_read(vp) & VIRF_TEXT_REF) != 0) {
1236 vunref(vp);
1237 }
1238 return (0);
1239 }
1240 continue;
1241 }
1242
1243 MPASS(n < -1);
1244 if (atomic_fcmpset_int(&vp->v_writecount, &n, n + 1)) {
1245 return (0);
1246 }
1247 }
1248 __assert_unreachable();
1249 }
1250
1251 static __always_inline int
vop_stdadd_writecount_impl(struct vop_add_writecount_args * ap,bool handle_msync)1252 vop_stdadd_writecount_impl(struct vop_add_writecount_args *ap, bool handle_msync)
1253 {
1254 struct vnode *vp;
1255 struct mount *mp __diagused;
1256 int n;
1257
1258 vp = ap->a_vp;
1259
1260 #ifdef INVARIANTS
1261 mp = vp->v_mount;
1262 if (mp != NULL) {
1263 if (handle_msync) {
1264 VNPASS((mp->mnt_kern_flag & MNTK_NOMSYNC) == 0, vp);
1265 } else {
1266 VNPASS((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0, vp);
1267 }
1268 }
1269 #endif
1270
1271 n = atomic_load_int(&vp->v_writecount);
1272 for (;;) {
1273 if (__predict_false(n < 0)) {
1274 return (ETXTBSY);
1275 }
1276
1277 VNASSERT(n + ap->a_inc >= 0, vp,
1278 ("neg writecount increment %d + %d = %d", n, ap->a_inc,
1279 n + ap->a_inc));
1280 if (n == 0) {
1281 if (handle_msync) {
1282 vlazy(vp);
1283 }
1284 }
1285
1286 if (atomic_fcmpset_int(&vp->v_writecount, &n, n + ap->a_inc)) {
1287 return (0);
1288 }
1289 }
1290 __assert_unreachable();
1291 }
1292
1293 int
vop_stdadd_writecount(struct vop_add_writecount_args * ap)1294 vop_stdadd_writecount(struct vop_add_writecount_args *ap)
1295 {
1296
1297 return (vop_stdadd_writecount_impl(ap, true));
1298 }
1299
1300 int
vop_stdadd_writecount_nomsync(struct vop_add_writecount_args * ap)1301 vop_stdadd_writecount_nomsync(struct vop_add_writecount_args *ap)
1302 {
1303
1304 return (vop_stdadd_writecount_impl(ap, false));
1305 }
1306
1307 int
vop_stdneed_inactive(struct vop_need_inactive_args * ap)1308 vop_stdneed_inactive(struct vop_need_inactive_args *ap)
1309 {
1310
1311 return (1);
1312 }
1313
1314 int
vop_stdinotify(struct vop_inotify_args * ap)1315 vop_stdinotify(struct vop_inotify_args *ap)
1316 {
1317 vn_inotify(ap->a_vp, ap->a_dvp, ap->a_cnp, ap->a_event, ap->a_cookie);
1318 return (0);
1319 }
1320
1321 int
vop_stdinotify_add_watch(struct vop_inotify_add_watch_args * ap)1322 vop_stdinotify_add_watch(struct vop_inotify_add_watch_args *ap)
1323 {
1324 return (vn_inotify_add_watch(ap->a_vp, ap->a_sc, ap->a_mask,
1325 ap->a_wdp, ap->a_td));
1326 }
1327
1328 int
vop_stdioctl(struct vop_ioctl_args * ap)1329 vop_stdioctl(struct vop_ioctl_args *ap)
1330 {
1331 struct vnode *vp;
1332 struct vattr va;
1333 off_t *offp;
1334 int error;
1335
1336 switch (ap->a_command) {
1337 case FIOSEEKDATA:
1338 case FIOSEEKHOLE:
1339 vp = ap->a_vp;
1340 error = vn_lock(vp, LK_SHARED);
1341 if (error != 0)
1342 return (EBADF);
1343 if (vp->v_type == VREG)
1344 error = VOP_GETATTR(vp, &va, ap->a_cred);
1345 else
1346 error = ENOTTY;
1347 if (error == 0) {
1348 offp = ap->a_data;
1349 if (*offp < 0 || *offp >= va.va_size)
1350 error = ENXIO;
1351 else if (ap->a_command == FIOSEEKHOLE)
1352 *offp = va.va_size;
1353 }
1354 VOP_UNLOCK(vp);
1355 break;
1356 default:
1357 error = ENOTTY;
1358 break;
1359 }
1360 return (error);
1361 }
1362
1363 /*
1364 * vfs default ops
1365 * used to fill the vfs function table to get reasonable default return values.
1366 */
1367 int
vfs_stdroot(struct mount * mp,int flags,struct vnode ** vpp)1368 vfs_stdroot(struct mount *mp, int flags, struct vnode **vpp)
1369 {
1370
1371 return (EOPNOTSUPP);
1372 }
1373
1374 int
vfs_stdstatfs(struct mount * mp,struct statfs * sbp)1375 vfs_stdstatfs(struct mount *mp, struct statfs *sbp)
1376 {
1377
1378 return (EOPNOTSUPP);
1379 }
1380
1381 int
vfs_stdquotactl(struct mount * mp,int cmds,uid_t uid,void * arg,bool * mp_busy)1382 vfs_stdquotactl(struct mount *mp, int cmds, uid_t uid, void *arg, bool *mp_busy)
1383 {
1384 return (EOPNOTSUPP);
1385 }
1386
1387 int
vfs_stdsync(struct mount * mp,int waitfor)1388 vfs_stdsync(struct mount *mp, int waitfor)
1389 {
1390 struct vnode *vp, *mvp;
1391 struct thread *td;
1392 int error, lockreq, allerror = 0;
1393
1394 td = curthread;
1395 lockreq = LK_EXCLUSIVE | LK_INTERLOCK;
1396 if (waitfor != MNT_WAIT)
1397 lockreq |= LK_NOWAIT;
1398 /*
1399 * Force stale buffer cache information to be flushed.
1400 */
1401 loop:
1402 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1403 if (vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1404 VI_UNLOCK(vp);
1405 continue;
1406 }
1407 if ((error = vget(vp, lockreq)) != 0) {
1408 if (error == ENOENT) {
1409 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1410 goto loop;
1411 }
1412 continue;
1413 }
1414 error = VOP_FSYNC(vp, waitfor, td);
1415 if (error)
1416 allerror = error;
1417 vput(vp);
1418 }
1419 return (allerror);
1420 }
1421
1422 int
vfs_stdnosync(struct mount * mp,int waitfor)1423 vfs_stdnosync(struct mount *mp, int waitfor)
1424 {
1425
1426 return (0);
1427 }
1428
1429 static int
vop_stdcopy_file_range(struct vop_copy_file_range_args * ap)1430 vop_stdcopy_file_range(struct vop_copy_file_range_args *ap)
1431 {
1432 int error;
1433
1434 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
1435 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags, ap->a_incred,
1436 ap->a_outcred, ap->a_fsizetd);
1437 return (error);
1438 }
1439
1440 int
vfs_stdvget(struct mount * mp,ino_t ino,int flags,struct vnode ** vpp)1441 vfs_stdvget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
1442 {
1443
1444 return (EOPNOTSUPP);
1445 }
1446
1447 int
vfs_stdfhtovp(struct mount * mp,struct fid * fhp,int flags,struct vnode ** vpp)1448 vfs_stdfhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1449 {
1450
1451 return (EOPNOTSUPP);
1452 }
1453
1454 int
vfs_stdinit(struct vfsconf * vfsp)1455 vfs_stdinit(struct vfsconf *vfsp)
1456 {
1457
1458 return (0);
1459 }
1460
1461 int
vfs_stduninit(struct vfsconf * vfsp)1462 vfs_stduninit(struct vfsconf *vfsp)
1463 {
1464
1465 return(0);
1466 }
1467
1468 int
vfs_stdextattrctl(struct mount * mp,int cmd,struct vnode * filename_vp,int attrnamespace,const char * attrname)1469 vfs_stdextattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1470 int attrnamespace, const char *attrname)
1471 {
1472
1473 if (filename_vp != NULL)
1474 VOP_UNLOCK(filename_vp);
1475 return (EOPNOTSUPP);
1476 }
1477
1478 int
vfs_stdsysctl(struct mount * mp,fsctlop_t op,struct sysctl_req * req)1479 vfs_stdsysctl(struct mount *mp, fsctlop_t op, struct sysctl_req *req)
1480 {
1481
1482 return (EOPNOTSUPP);
1483 }
1484
1485 static vop_bypass_t *
bp_by_off(struct vop_vector * vop,struct vop_generic_args * a)1486 bp_by_off(struct vop_vector *vop, struct vop_generic_args *a)
1487 {
1488
1489 return (*(vop_bypass_t **)((char *)vop + a->a_desc->vdesc_vop_offset));
1490 }
1491
1492 int
vop_sigdefer(struct vop_vector * vop,struct vop_generic_args * a)1493 vop_sigdefer(struct vop_vector *vop, struct vop_generic_args *a)
1494 {
1495 vop_bypass_t *bp;
1496 int prev_stops, rc;
1497
1498 bp = bp_by_off(vop, a);
1499 MPASS(bp != NULL);
1500
1501 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
1502 rc = bp(a);
1503 sigallowstop(prev_stops);
1504 return (rc);
1505 }
1506
1507 static int
vop_stdstat(struct vop_stat_args * a)1508 vop_stdstat(struct vop_stat_args *a)
1509 {
1510 struct vattr vattr;
1511 struct vattr *vap;
1512 struct vnode *vp;
1513 struct stat *sb;
1514 int error;
1515 u_short mode;
1516
1517 vp = a->a_vp;
1518 sb = a->a_sb;
1519
1520 error = vop_stat_helper_pre(a);
1521 if (error != 0)
1522 return (error);
1523
1524 vap = &vattr;
1525
1526 /*
1527 * Initialize defaults for new and unusual fields, so that file
1528 * systems which don't support these fields don't need to know
1529 * about them.
1530 */
1531 vap->va_birthtime.tv_sec = -1;
1532 vap->va_birthtime.tv_nsec = 0;
1533 vap->va_fsid = VNOVAL;
1534 vap->va_gen = 0;
1535 vap->va_rdev = NODEV;
1536 vap->va_filerev = 0;
1537 vap->va_bsdflags = 0;
1538
1539 error = VOP_GETATTR(vp, vap, a->a_active_cred);
1540 if (error)
1541 goto out;
1542
1543 /*
1544 * Zero the spare stat fields
1545 */
1546 bzero(sb, sizeof *sb);
1547
1548 /*
1549 * Copy from vattr table
1550 */
1551 if (vap->va_fsid != VNOVAL)
1552 sb->st_dev = vap->va_fsid;
1553 else
1554 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1555 sb->st_ino = vap->va_fileid;
1556 mode = vap->va_mode;
1557 switch (vap->va_type) {
1558 case VREG:
1559 mode |= S_IFREG;
1560 break;
1561 case VDIR:
1562 mode |= S_IFDIR;
1563 break;
1564 case VBLK:
1565 mode |= S_IFBLK;
1566 break;
1567 case VCHR:
1568 mode |= S_IFCHR;
1569 break;
1570 case VLNK:
1571 mode |= S_IFLNK;
1572 break;
1573 case VSOCK:
1574 mode |= S_IFSOCK;
1575 break;
1576 case VFIFO:
1577 mode |= S_IFIFO;
1578 break;
1579 default:
1580 error = EBADF;
1581 goto out;
1582 }
1583 sb->st_mode = mode;
1584 sb->st_nlink = vap->va_nlink;
1585 sb->st_uid = vap->va_uid;
1586 sb->st_gid = vap->va_gid;
1587 sb->st_rdev = vap->va_rdev;
1588 if (vap->va_size > OFF_MAX) {
1589 error = EOVERFLOW;
1590 goto out;
1591 }
1592 sb->st_size = vap->va_size;
1593 sb->st_atim.tv_sec = vap->va_atime.tv_sec;
1594 sb->st_atim.tv_nsec = vap->va_atime.tv_nsec;
1595 sb->st_mtim.tv_sec = vap->va_mtime.tv_sec;
1596 sb->st_mtim.tv_nsec = vap->va_mtime.tv_nsec;
1597 sb->st_ctim.tv_sec = vap->va_ctime.tv_sec;
1598 sb->st_ctim.tv_nsec = vap->va_ctime.tv_nsec;
1599 sb->st_birthtim.tv_sec = vap->va_birthtime.tv_sec;
1600 sb->st_birthtim.tv_nsec = vap->va_birthtime.tv_nsec;
1601
1602 /*
1603 * According to www.opengroup.org, the meaning of st_blksize is
1604 * "a filesystem-specific preferred I/O block size for this
1605 * object. In some filesystem types, this may vary from file
1606 * to file"
1607 * Use minimum/default of PAGE_SIZE (e.g. for VCHR).
1608 */
1609
1610 sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize);
1611 sb->st_flags = vap->va_flags;
1612 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1613 sb->st_gen = vap->va_gen;
1614 sb->st_filerev = vap->va_filerev;
1615 sb->st_bsdflags = vap->va_bsdflags;
1616 out:
1617 return (vop_stat_helper_post(a, error));
1618 }
1619
1620 static int
vop_stdread_pgcache(struct vop_read_pgcache_args * ap __unused)1621 vop_stdread_pgcache(struct vop_read_pgcache_args *ap __unused)
1622 {
1623 return (EJUSTRETURN);
1624 }
1625
1626 static int
vop_stdvput_pair(struct vop_vput_pair_args * ap)1627 vop_stdvput_pair(struct vop_vput_pair_args *ap)
1628 {
1629 struct vnode *dvp, *vp, **vpp;
1630
1631 dvp = ap->a_dvp;
1632 vpp = ap->a_vpp;
1633 vput(dvp);
1634 if (vpp != NULL && ap->a_unlock_vp && (vp = *vpp) != NULL)
1635 vput(vp);
1636 return (0);
1637 }
1638
1639 static int
vop_stdgetlowvnode(struct vop_getlowvnode_args * ap)1640 vop_stdgetlowvnode(struct vop_getlowvnode_args *ap)
1641 {
1642 vref(ap->a_vp);
1643 *ap->a_vplp = ap->a_vp;
1644 return (0);
1645 }
1646