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