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