]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_default.c
Factor out resource limit enforcement code in the ELF loader.
[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/kernel.h>
47 #include <sys/limits.h>
48 #include <sys/lock.h>
49 #include <sys/lockf.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/rwlock.h>
54 #include <sys/fcntl.h>
55 #include <sys/unistd.h>
56 #include <sys/vnode.h>
57 #include <sys/dirent.h>
58 #include <sys/poll.h>
59
60 #include <security/mac/mac_framework.h>
61
62 #include <vm/vm.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_extern.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_page.h>
68 #include <vm/vm_pager.h>
69 #include <vm/vnode_pager.h>
70
71 static int      vop_nolookup(struct vop_lookup_args *);
72 static int      vop_norename(struct vop_rename_args *);
73 static int      vop_nostrategy(struct vop_strategy_args *);
74 static int      get_next_dirent(struct vnode *vp, struct dirent **dpp,
75                                 char *dirbuf, int dirbuflen, off_t *off,
76                                 char **cpos, int *len, int *eofflag,
77                                 struct thread *td);
78 static int      dirent_exists(struct vnode *vp, const char *dirname,
79                               struct thread *td);
80
81 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
82
83 static int vop_stdis_text(struct vop_is_text_args *ap);
84 static int vop_stdset_text(struct vop_set_text_args *ap);
85 static int vop_stdunset_text(struct vop_unset_text_args *ap);
86 static int vop_stdget_writecount(struct vop_get_writecount_args *ap);
87 static int vop_stdadd_writecount(struct vop_add_writecount_args *ap);
88 static int vop_stdfdatasync(struct vop_fdatasync_args *ap);
89 static int vop_stdgetpages_async(struct vop_getpages_async_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_bmap =             vop_stdbmap,
115         .vop_close =            VOP_NULL,
116         .vop_fsync =            VOP_NULL,
117         .vop_fdatasync =        vop_stdfdatasync,
118         .vop_getpages =         vop_stdgetpages,
119         .vop_getpages_async =   vop_stdgetpages_async,
120         .vop_getwritemount =    vop_stdgetwritemount,
121         .vop_inactive =         VOP_NULL,
122         .vop_ioctl =            VOP_ENOTTY,
123         .vop_kqfilter =         vop_stdkqfilter,
124         .vop_islocked =         vop_stdislocked,
125         .vop_lock1 =            vop_stdlock,
126         .vop_lookup =           vop_nolookup,
127         .vop_open =             VOP_NULL,
128         .vop_pathconf =         VOP_EINVAL,
129         .vop_poll =             vop_nopoll,
130         .vop_putpages =         vop_stdputpages,
131         .vop_readlink =         VOP_EINVAL,
132         .vop_rename =           vop_norename,
133         .vop_revoke =           VOP_PANIC,
134         .vop_strategy =         vop_nostrategy,
135         .vop_unlock =           vop_stdunlock,
136         .vop_vptocnp =          vop_stdvptocnp,
137         .vop_vptofh =           vop_stdvptofh,
138         .vop_unp_bind =         vop_stdunp_bind,
139         .vop_unp_connect =      vop_stdunp_connect,
140         .vop_unp_detach =       vop_stdunp_detach,
141         .vop_is_text =          vop_stdis_text,
142         .vop_set_text =         vop_stdset_text,
143         .vop_unset_text =       vop_stdunset_text,
144         .vop_get_writecount =   vop_stdget_writecount,
145         .vop_add_writecount =   vop_stdadd_writecount,
146 };
147
148 /*
149  * Series of placeholder functions for various error returns for
150  * VOPs.
151  */
152
153 int
154 vop_eopnotsupp(struct vop_generic_args *ap)
155 {
156         /*
157         printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name);
158         */
159
160         return (EOPNOTSUPP);
161 }
162
163 int
164 vop_ebadf(struct vop_generic_args *ap)
165 {
166
167         return (EBADF);
168 }
169
170 int
171 vop_enotty(struct vop_generic_args *ap)
172 {
173
174         return (ENOTTY);
175 }
176
177 int
178 vop_einval(struct vop_generic_args *ap)
179 {
180
181         return (EINVAL);
182 }
183
184 int
185 vop_enoent(struct vop_generic_args *ap)
186 {
187
188         return (ENOENT);
189 }
190
191 int
192 vop_null(struct vop_generic_args *ap)
193 {
194
195         return (0);
196 }
197
198 /*
199  * Helper function to panic on some bad VOPs in some filesystems.
200  */
201 int
202 vop_panic(struct vop_generic_args *ap)
203 {
204
205         panic("filesystem goof: vop_panic[%s]", ap->a_desc->vdesc_name);
206 }
207
208 /*
209  * vop_std<something> and vop_no<something> are default functions for use by
210  * filesystems that need the "default reasonable" implementation for a
211  * particular operation.
212  *
213  * The documentation for the operations they implement exists (if it exists)
214  * in the VOP_<SOMETHING>(9) manpage (all uppercase).
215  */
216
217 /*
218  * Default vop for filesystems that do not support name lookup
219  */
220 static int
221 vop_nolookup(ap)
222         struct vop_lookup_args /* {
223                 struct vnode *a_dvp;
224                 struct vnode **a_vpp;
225                 struct componentname *a_cnp;
226         } */ *ap;
227 {
228
229         *ap->a_vpp = NULL;
230         return (ENOTDIR);
231 }
232
233 /*
234  * vop_norename:
235  *
236  * Handle unlock and reference counting for arguments of vop_rename
237  * for filesystems that do not implement rename operation.
238  */
239 static int
240 vop_norename(struct vop_rename_args *ap)
241 {
242
243         vop_rename_fail(ap);
244         return (EOPNOTSUPP);
245 }
246
247 /*
248  *      vop_nostrategy:
249  *
250  *      Strategy routine for VFS devices that have none.
251  *
252  *      BIO_ERROR and B_INVAL must be cleared prior to calling any strategy
253  *      routine.  Typically this is done for a BIO_READ strategy call.
254  *      Typically B_INVAL is assumed to already be clear prior to a write
255  *      and should not be cleared manually unless you just made the buffer
256  *      invalid.  BIO_ERROR should be cleared either way.
257  */
258
259 static int
260 vop_nostrategy (struct vop_strategy_args *ap)
261 {
262         printf("No strategy for buffer at %p\n", ap->a_bp);
263         vn_printf(ap->a_vp, "vnode ");
264         ap->a_bp->b_ioflags |= BIO_ERROR;
265         ap->a_bp->b_error = EOPNOTSUPP;
266         bufdone(ap->a_bp);
267         return (EOPNOTSUPP);
268 }
269
270 static int
271 get_next_dirent(struct vnode *vp, struct dirent **dpp, char *dirbuf,
272                 int dirbuflen, off_t *off, char **cpos, int *len,
273                 int *eofflag, struct thread *td)
274 {
275         int error, reclen;
276         struct uio uio;
277         struct iovec iov;
278         struct dirent *dp;
279
280         KASSERT(VOP_ISLOCKED(vp), ("vp %p is not locked", vp));
281         KASSERT(vp->v_type == VDIR, ("vp %p is not a directory", vp));
282
283         if (*len == 0) {
284                 iov.iov_base = dirbuf;
285                 iov.iov_len = dirbuflen;
286
287                 uio.uio_iov = &iov;
288                 uio.uio_iovcnt = 1;
289                 uio.uio_offset = *off;
290                 uio.uio_resid = dirbuflen;
291                 uio.uio_segflg = UIO_SYSSPACE;
292                 uio.uio_rw = UIO_READ;
293                 uio.uio_td = td;
294
295                 *eofflag = 0;
296
297 #ifdef MAC
298                 error = mac_vnode_check_readdir(td->td_ucred, vp);
299                 if (error == 0)
300 #endif
301                         error = VOP_READDIR(vp, &uio, td->td_ucred, eofflag,
302                                 NULL, NULL);
303                 if (error)
304                         return (error);
305
306                 *off = uio.uio_offset;
307
308                 *cpos = dirbuf;
309                 *len = (dirbuflen - uio.uio_resid);
310
311                 if (*len == 0)
312                         return (ENOENT);
313         }
314
315         dp = (struct dirent *)(*cpos);
316         reclen = dp->d_reclen;
317         *dpp = dp;
318
319         /* check for malformed directory.. */
320         if (reclen < DIRENT_MINSIZE)
321                 return (EINVAL);
322
323         *cpos += reclen;
324         *len -= reclen;
325
326         return (0);
327 }
328
329 /*
330  * Check if a named file exists in a given directory vnode.
331  */
332 static int
333 dirent_exists(struct vnode *vp, const char *dirname, struct thread *td)
334 {
335         char *dirbuf, *cpos;
336         int error, eofflag, dirbuflen, len, found;
337         off_t off;
338         struct dirent *dp;
339         struct vattr va;
340
341         KASSERT(VOP_ISLOCKED(vp), ("vp %p is not locked", vp));
342         KASSERT(vp->v_type == VDIR, ("vp %p is not a directory", vp));
343
344         found = 0;
345
346         error = VOP_GETATTR(vp, &va, td->td_ucred);
347         if (error)
348                 return (found);
349
350         dirbuflen = DEV_BSIZE;
351         if (dirbuflen < va.va_blocksize)
352                 dirbuflen = va.va_blocksize;
353         dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
354
355         off = 0;
356         len = 0;
357         do {
358                 error = get_next_dirent(vp, &dp, dirbuf, dirbuflen, &off,
359                                         &cpos, &len, &eofflag, td);
360                 if (error)
361                         goto out;
362
363                 if (dp->d_type != DT_WHT && dp->d_fileno != 0 &&
364                     strcmp(dp->d_name, dirname) == 0) {
365                         found = 1;
366                         goto out;
367                 }
368         } while (len > 0 || !eofflag);
369
370 out:
371         free(dirbuf, M_TEMP);
372         return (found);
373 }
374
375 int
376 vop_stdaccess(struct vop_access_args *ap)
377 {
378
379         KASSERT((ap->a_accmode & ~(VEXEC | VWRITE | VREAD | VADMIN |
380             VAPPEND)) == 0, ("invalid bit in accmode"));
381
382         return (VOP_ACCESSX(ap->a_vp, ap->a_accmode, ap->a_cred, ap->a_td));
383 }
384
385 int
386 vop_stdaccessx(struct vop_accessx_args *ap)
387 {
388         int error;
389         accmode_t accmode = ap->a_accmode;
390
391         error = vfs_unixify_accmode(&accmode);
392         if (error != 0)
393                 return (error);
394
395         if (accmode == 0)
396                 return (0);
397
398         return (VOP_ACCESS(ap->a_vp, accmode, ap->a_cred, ap->a_td));
399 }
400
401 /*
402  * Advisory record locking support
403  */
404 int
405 vop_stdadvlock(struct vop_advlock_args *ap)
406 {
407         struct vnode *vp;
408         struct vattr vattr;
409         int error;
410
411         vp = ap->a_vp;
412         if (ap->a_fl->l_whence == SEEK_END) {
413                 /*
414                  * The NFSv4 server must avoid doing a vn_lock() here, since it
415                  * can deadlock the nfsd threads, due to a LOR.  Fortunately
416                  * the NFSv4 server always uses SEEK_SET and this code is
417                  * only required for the SEEK_END case.
418                  */
419                 vn_lock(vp, LK_SHARED | LK_RETRY);
420                 error = VOP_GETATTR(vp, &vattr, curthread->td_ucred);
421                 VOP_UNLOCK(vp, 0);
422                 if (error)
423                         return (error);
424         } else
425                 vattr.va_size = 0;
426
427         return (lf_advlock(ap, &(vp->v_lockf), vattr.va_size));
428 }
429
430 int
431 vop_stdadvlockasync(struct vop_advlockasync_args *ap)
432 {
433         struct vnode *vp;
434         struct vattr vattr;
435         int error;
436
437         vp = ap->a_vp;
438         if (ap->a_fl->l_whence == SEEK_END) {
439                 /* The size argument is only needed for SEEK_END. */
440                 vn_lock(vp, LK_SHARED | LK_RETRY);
441                 error = VOP_GETATTR(vp, &vattr, curthread->td_ucred);
442                 VOP_UNLOCK(vp, 0);
443                 if (error)
444                         return (error);
445         } else
446                 vattr.va_size = 0;
447
448         return (lf_advlockasync(ap, &(vp->v_lockf), vattr.va_size));
449 }
450
451 int
452 vop_stdadvlockpurge(struct vop_advlockpurge_args *ap)
453 {
454         struct vnode *vp;
455
456         vp = ap->a_vp;
457         lf_purgelocks(vp, &vp->v_lockf);
458         return (0);
459 }
460
461 /*
462  * vop_stdpathconf:
463  *
464  * Standard implementation of POSIX pathconf, to get information about limits
465  * for a filesystem.
466  * Override per filesystem for the case where the filesystem has smaller
467  * limits.
468  */
469 int
470 vop_stdpathconf(ap)
471         struct vop_pathconf_args /* {
472         struct vnode *a_vp;
473         int a_name;
474         int *a_retval;
475         } */ *ap;
476 {
477
478         switch (ap->a_name) {
479                 case _PC_ASYNC_IO:
480                         *ap->a_retval = _POSIX_ASYNCHRONOUS_IO;
481                         return (0);
482                 case _PC_PATH_MAX:
483                         *ap->a_retval = PATH_MAX;
484                         return (0);
485                 case _PC_ACL_EXTENDED:
486                 case _PC_ACL_NFS4:
487                 case _PC_CAP_PRESENT:
488                 case _PC_INF_PRESENT:
489                 case _PC_MAC_PRESENT:
490                         *ap->a_retval = 0;
491                         return (0);
492                 default:
493                         return (EINVAL);
494         }
495         /* NOTREACHED */
496 }
497
498 /*
499  * Standard lock, unlock and islocked functions.
500  */
501 int
502 vop_stdlock(ap)
503         struct vop_lock1_args /* {
504                 struct vnode *a_vp;
505                 int a_flags;
506                 char *file;
507                 int line;
508         } */ *ap;
509 {
510         struct vnode *vp = ap->a_vp;
511         struct mtx *ilk;
512
513         ilk = VI_MTX(vp);
514         return (lockmgr_lock_fast_path(vp->v_vnlock, ap->a_flags,
515             &ilk->lock_object, ap->a_file, ap->a_line));
516 }
517
518 /* See above. */
519 int
520 vop_stdunlock(ap)
521         struct vop_unlock_args /* {
522                 struct vnode *a_vp;
523                 int a_flags;
524         } */ *ap;
525 {
526         struct vnode *vp = ap->a_vp;
527         struct mtx *ilk;
528
529         ilk = VI_MTX(vp);
530         return (lockmgr_unlock_fast_path(vp->v_vnlock, ap->a_flags,
531             &ilk->lock_object));
532 }
533
534 /* See above. */
535 int
536 vop_stdislocked(ap)
537         struct vop_islocked_args /* {
538                 struct vnode *a_vp;
539         } */ *ap;
540 {
541
542         return (lockstatus(ap->a_vp->v_vnlock));
543 }
544
545 /*
546  * Return true for select/poll.
547  */
548 int
549 vop_nopoll(ap)
550         struct vop_poll_args /* {
551                 struct vnode *a_vp;
552                 int  a_events;
553                 struct ucred *a_cred;
554                 struct thread *a_td;
555         } */ *ap;
556 {
557
558         return (poll_no_poll(ap->a_events));
559 }
560
561 /*
562  * Implement poll for local filesystems that support it.
563  */
564 int
565 vop_stdpoll(ap)
566         struct vop_poll_args /* {
567                 struct vnode *a_vp;
568                 int  a_events;
569                 struct ucred *a_cred;
570                 struct thread *a_td;
571         } */ *ap;
572 {
573         if (ap->a_events & ~POLLSTANDARD)
574                 return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
575         return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
576 }
577
578 /*
579  * Return our mount point, as we will take charge of the writes.
580  */
581 int
582 vop_stdgetwritemount(ap)
583         struct vop_getwritemount_args /* {
584                 struct vnode *a_vp;
585                 struct mount **a_mpp;
586         } */ *ap;
587 {
588         struct mount *mp;
589
590         /*
591          * XXX Since this is called unlocked we may be recycled while
592          * attempting to ref the mount.  If this is the case or mountpoint
593          * will be set to NULL.  We only have to prevent this call from
594          * returning with a ref to an incorrect mountpoint.  It is not
595          * harmful to return with a ref to our previous mountpoint.
596          */
597         mp = ap->a_vp->v_mount;
598         if (mp != NULL) {
599                 vfs_ref(mp);
600                 if (mp != ap->a_vp->v_mount) {
601                         vfs_rel(mp);
602                         mp = NULL;
603                 }
604         }
605         *(ap->a_mpp) = mp;
606         return (0);
607 }
608
609 /* XXX Needs good comment and VOP_BMAP(9) manpage */
610 int
611 vop_stdbmap(ap)
612         struct vop_bmap_args /* {
613                 struct vnode *a_vp;
614                 daddr_t  a_bn;
615                 struct bufobj **a_bop;
616                 daddr_t *a_bnp;
617                 int *a_runp;
618                 int *a_runb;
619         } */ *ap;
620 {
621
622         if (ap->a_bop != NULL)
623                 *ap->a_bop = &ap->a_vp->v_bufobj;
624         if (ap->a_bnp != NULL)
625                 *ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize);
626         if (ap->a_runp != NULL)
627                 *ap->a_runp = 0;
628         if (ap->a_runb != NULL)
629                 *ap->a_runb = 0;
630         return (0);
631 }
632
633 int
634 vop_stdfsync(ap)
635         struct vop_fsync_args /* {
636                 struct vnode *a_vp;
637                 int a_waitfor;
638                 struct thread *a_td;
639         } */ *ap;
640 {
641         struct vnode *vp;
642         struct buf *bp, *nbp;
643         struct bufobj *bo;
644         struct mount *mp;
645         int error, maxretry;
646
647         error = 0;
648         maxretry = 10000;     /* large, arbitrarily chosen */
649         vp = ap->a_vp;
650         mp = NULL;
651         if (vp->v_type == VCHR) {
652                 VI_LOCK(vp);
653                 mp = vp->v_rdev->si_mountpt;
654                 VI_UNLOCK(vp);
655         }
656         bo = &vp->v_bufobj;
657         BO_LOCK(bo);
658 loop1:
659         /*
660          * MARK/SCAN initialization to avoid infinite loops.
661          */
662         TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
663                 bp->b_vflags &= ~BV_SCANNED;
664                 bp->b_error = 0;
665         }
666
667         /*
668          * Flush all dirty buffers associated with a vnode.
669          */
670 loop2:
671         TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
672                 if ((bp->b_vflags & BV_SCANNED) != 0)
673                         continue;
674                 bp->b_vflags |= BV_SCANNED;
675                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
676                         if (ap->a_waitfor != MNT_WAIT)
677                                 continue;
678                         if (BUF_LOCK(bp,
679                             LK_EXCLUSIVE | LK_INTERLOCK | LK_SLEEPFAIL,
680                             BO_LOCKPTR(bo)) != 0) {
681                                 BO_LOCK(bo);
682                                 goto loop1;
683                         }
684                         BO_LOCK(bo);
685                 }
686                 BO_UNLOCK(bo);
687                 KASSERT(bp->b_bufobj == bo,
688                     ("bp %p wrong b_bufobj %p should be %p",
689                     bp, bp->b_bufobj, bo));
690                 if ((bp->b_flags & B_DELWRI) == 0)
691                         panic("fsync: not dirty");
692                 if ((vp->v_object != NULL) && (bp->b_flags & B_CLUSTEROK)) {
693                         vfs_bio_awrite(bp);
694                 } else {
695                         bremfree(bp);
696                         bawrite(bp);
697                 }
698                 if (maxretry < 1000)
699                         pause("dirty", hz < 1000 ? 1 : hz / 1000);
700                 BO_LOCK(bo);
701                 goto loop2;
702         }
703
704         /*
705          * If synchronous the caller expects us to completely resolve all
706          * dirty buffers in the system.  Wait for in-progress I/O to
707          * complete (which could include background bitmap writes), then
708          * retry if dirty blocks still exist.
709          */
710         if (ap->a_waitfor == MNT_WAIT) {
711                 bufobj_wwait(bo, 0, 0);
712                 if (bo->bo_dirty.bv_cnt > 0) {
713                         /*
714                          * If we are unable to write any of these buffers
715                          * then we fail now rather than trying endlessly
716                          * to write them out.
717                          */
718                         TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
719                                 if ((error = bp->b_error) != 0)
720                                         break;
721                         if ((mp != NULL && mp->mnt_secondary_writes > 0) ||
722                             (error == 0 && --maxretry >= 0))
723                                 goto loop1;
724                         if (error == 0)
725                                 error = EAGAIN;
726                 }
727         }
728         BO_UNLOCK(bo);
729         if (error != 0)
730                 vn_printf(vp, "fsync: giving up on dirty (error = %d) ", error);
731
732         return (error);
733 }
734
735 static int
736 vop_stdfdatasync(struct vop_fdatasync_args *ap)
737 {
738
739         return (VOP_FSYNC(ap->a_vp, MNT_WAIT, ap->a_td));
740 }
741
742 int
743 vop_stdfdatasync_buf(struct vop_fdatasync_args *ap)
744 {
745         struct vop_fsync_args apf;
746
747         apf.a_vp = ap->a_vp;
748         apf.a_waitfor = MNT_WAIT;
749         apf.a_td = ap->a_td;
750         return (vop_stdfsync(&apf));
751 }
752
753 /* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */
754 int
755 vop_stdgetpages(ap)
756         struct vop_getpages_args /* {
757                 struct vnode *a_vp;
758                 vm_page_t *a_m;
759                 int a_count;
760                 int *a_rbehind;
761                 int *a_rahead;
762         } */ *ap;
763 {
764
765         return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
766             ap->a_count, ap->a_rbehind, ap->a_rahead, NULL, NULL);
767 }
768
769 static int
770 vop_stdgetpages_async(struct vop_getpages_async_args *ap)
771 {
772         int error;
773
774         error = VOP_GETPAGES(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
775             ap->a_rahead);
776         ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
777         return (error);
778 }
779
780 int
781 vop_stdkqfilter(struct vop_kqfilter_args *ap)
782 {
783         return vfs_kqfilter(ap);
784 }
785
786 /* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */
787 int
788 vop_stdputpages(ap)
789         struct vop_putpages_args /* {
790                 struct vnode *a_vp;
791                 vm_page_t *a_m;
792                 int a_count;
793                 int a_sync;
794                 int *a_rtvals;
795         } */ *ap;
796 {
797
798         return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
799              ap->a_sync, ap->a_rtvals);
800 }
801
802 int
803 vop_stdvptofh(struct vop_vptofh_args *ap)
804 {
805         return (EOPNOTSUPP);
806 }
807
808 int
809 vop_stdvptocnp(struct vop_vptocnp_args *ap)
810 {
811         struct vnode *vp = ap->a_vp;
812         struct vnode **dvp = ap->a_vpp;
813         struct ucred *cred = ap->a_cred;
814         char *buf = ap->a_buf;
815         int *buflen = ap->a_buflen;
816         char *dirbuf, *cpos;
817         int i, error, eofflag, dirbuflen, flags, locked, len, covered;
818         off_t off;
819         ino_t fileno;
820         struct vattr va;
821         struct nameidata nd;
822         struct thread *td;
823         struct dirent *dp;
824         struct vnode *mvp;
825
826         i = *buflen;
827         error = 0;
828         covered = 0;
829         td = curthread;
830
831         if (vp->v_type != VDIR)
832                 return (ENOENT);
833
834         error = VOP_GETATTR(vp, &va, cred);
835         if (error)
836                 return (error);
837
838         VREF(vp);
839         locked = VOP_ISLOCKED(vp);
840         VOP_UNLOCK(vp, 0);
841         NDINIT_ATVP(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
842             "..", vp, td);
843         flags = FREAD;
844         error = vn_open_cred(&nd, &flags, 0, VN_OPEN_NOAUDIT, cred, NULL);
845         if (error) {
846                 vn_lock(vp, locked | LK_RETRY);
847                 return (error);
848         }
849         NDFREE(&nd, NDF_ONLY_PNBUF);
850
851         mvp = *dvp = nd.ni_vp;
852
853         if (vp->v_mount != (*dvp)->v_mount &&
854             ((*dvp)->v_vflag & VV_ROOT) &&
855             ((*dvp)->v_mount->mnt_flag & MNT_UNION)) {
856                 *dvp = (*dvp)->v_mount->mnt_vnodecovered;
857                 VREF(mvp);
858                 VOP_UNLOCK(mvp, 0);
859                 vn_close(mvp, FREAD, cred, td);
860                 VREF(*dvp);
861                 vn_lock(*dvp, LK_SHARED | LK_RETRY);
862                 covered = 1;
863         }
864
865         fileno = va.va_fileid;
866
867         dirbuflen = DEV_BSIZE;
868         if (dirbuflen < va.va_blocksize)
869                 dirbuflen = va.va_blocksize;
870         dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
871
872         if ((*dvp)->v_type != VDIR) {
873                 error = ENOENT;
874                 goto out;
875         }
876
877         off = 0;
878         len = 0;
879         do {
880                 /* call VOP_READDIR of parent */
881                 error = get_next_dirent(*dvp, &dp, dirbuf, dirbuflen, &off,
882                                         &cpos, &len, &eofflag, td);
883                 if (error)
884                         goto out;
885
886                 if ((dp->d_type != DT_WHT) &&
887                     (dp->d_fileno == fileno)) {
888                         if (covered) {
889                                 VOP_UNLOCK(*dvp, 0);
890                                 vn_lock(mvp, LK_SHARED | LK_RETRY);
891                                 if (dirent_exists(mvp, dp->d_name, td)) {
892                                         error = ENOENT;
893                                         VOP_UNLOCK(mvp, 0);
894                                         vn_lock(*dvp, LK_SHARED | LK_RETRY);
895                                         goto out;
896                                 }
897                                 VOP_UNLOCK(mvp, 0);
898                                 vn_lock(*dvp, LK_SHARED | LK_RETRY);
899                         }
900                         i -= dp->d_namlen;
901
902                         if (i < 0) {
903                                 error = ENOMEM;
904                                 goto out;
905                         }
906                         if (dp->d_namlen == 1 && dp->d_name[0] == '.') {
907                                 error = ENOENT;
908                         } else {
909                                 bcopy(dp->d_name, buf + i, dp->d_namlen);
910                                 error = 0;
911                         }
912                         goto out;
913                 }
914         } while (len > 0 || !eofflag);
915         error = ENOENT;
916
917 out:
918         free(dirbuf, M_TEMP);
919         if (!error) {
920                 *buflen = i;
921                 vref(*dvp);
922         }
923         if (covered) {
924                 vput(*dvp);
925                 vrele(mvp);
926         } else {
927                 VOP_UNLOCK(mvp, 0);
928                 vn_close(mvp, FREAD, cred, td);
929         }
930         vn_lock(vp, locked | LK_RETRY);
931         return (error);
932 }
933
934 int
935 vop_stdallocate(struct vop_allocate_args *ap)
936 {
937 #ifdef __notyet__
938         struct statfs *sfs;
939         off_t maxfilesize = 0;
940 #endif
941         struct iovec aiov;
942         struct vattr vattr, *vap;
943         struct uio auio;
944         off_t fsize, len, cur, offset;
945         uint8_t *buf;
946         struct thread *td;
947         struct vnode *vp;
948         size_t iosize;
949         int error;
950
951         buf = NULL;
952         error = 0;
953         td = curthread;
954         vap = &vattr;
955         vp = ap->a_vp;
956         len = *ap->a_len;
957         offset = *ap->a_offset;
958
959         error = VOP_GETATTR(vp, vap, td->td_ucred);
960         if (error != 0)
961                 goto out;
962         fsize = vap->va_size;
963         iosize = vap->va_blocksize;
964         if (iosize == 0)
965                 iosize = BLKDEV_IOSIZE;
966         if (iosize > MAXPHYS)
967                 iosize = MAXPHYS;
968         buf = malloc(iosize, M_TEMP, M_WAITOK);
969
970 #ifdef __notyet__
971         /*
972          * Check if the filesystem sets f_maxfilesize; if not use
973          * VOP_SETATTR to perform the check.
974          */
975         sfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
976         error = VFS_STATFS(vp->v_mount, sfs, td);
977         if (error == 0)
978                 maxfilesize = sfs->f_maxfilesize;
979         free(sfs, M_STATFS);
980         if (error != 0)
981                 goto out;
982         if (maxfilesize) {
983                 if (offset > maxfilesize || len > maxfilesize ||
984                     offset + len > maxfilesize) {
985                         error = EFBIG;
986                         goto out;
987                 }
988         } else
989 #endif
990         if (offset + len > vap->va_size) {
991                 /*
992                  * Test offset + len against the filesystem's maxfilesize.
993                  */
994                 VATTR_NULL(vap);
995                 vap->va_size = offset + len;
996                 error = VOP_SETATTR(vp, vap, td->td_ucred);
997                 if (error != 0)
998                         goto out;
999                 VATTR_NULL(vap);
1000                 vap->va_size = fsize;
1001                 error = VOP_SETATTR(vp, vap, td->td_ucred);
1002                 if (error != 0)
1003                         goto out;
1004         }
1005
1006         for (;;) {
1007                 /*
1008                  * Read and write back anything below the nominal file
1009                  * size.  There's currently no way outside the filesystem
1010                  * to know whether this area is sparse or not.
1011                  */
1012                 cur = iosize;
1013                 if ((offset % iosize) != 0)
1014                         cur -= (offset % iosize);
1015                 if (cur > len)
1016                         cur = len;
1017                 if (offset < fsize) {
1018                         aiov.iov_base = buf;
1019                         aiov.iov_len = cur;
1020                         auio.uio_iov = &aiov;
1021                         auio.uio_iovcnt = 1;
1022                         auio.uio_offset = offset;
1023                         auio.uio_resid = cur;
1024                         auio.uio_segflg = UIO_SYSSPACE;
1025                         auio.uio_rw = UIO_READ;
1026                         auio.uio_td = td;
1027                         error = VOP_READ(vp, &auio, 0, td->td_ucred);
1028                         if (error != 0)
1029                                 break;
1030                         if (auio.uio_resid > 0) {
1031                                 bzero(buf + cur - auio.uio_resid,
1032                                     auio.uio_resid);
1033                         }
1034                 } else {
1035                         bzero(buf, cur);
1036                 }
1037
1038                 aiov.iov_base = buf;
1039                 aiov.iov_len = cur;
1040                 auio.uio_iov = &aiov;
1041                 auio.uio_iovcnt = 1;
1042                 auio.uio_offset = offset;
1043                 auio.uio_resid = cur;
1044                 auio.uio_segflg = UIO_SYSSPACE;
1045                 auio.uio_rw = UIO_WRITE;
1046                 auio.uio_td = td;
1047
1048                 error = VOP_WRITE(vp, &auio, 0, td->td_ucred);
1049                 if (error != 0)
1050                         break;
1051
1052                 len -= cur;
1053                 offset += cur;
1054                 if (len == 0)
1055                         break;
1056                 if (should_yield())
1057                         break;
1058         }
1059
1060  out:
1061         *ap->a_len = len;
1062         *ap->a_offset = offset;
1063         free(buf, M_TEMP);
1064         return (error);
1065 }
1066
1067 int
1068 vop_stdadvise(struct vop_advise_args *ap)
1069 {
1070         struct vnode *vp;
1071         struct bufobj *bo;
1072         daddr_t startn, endn;
1073         off_t bstart, bend, start, end;
1074         int bsize, error;
1075
1076         vp = ap->a_vp;
1077         switch (ap->a_advice) {
1078         case POSIX_FADV_WILLNEED:
1079                 /*
1080                  * Do nothing for now.  Filesystems should provide a
1081                  * custom method which starts an asynchronous read of
1082                  * the requested region.
1083                  */
1084                 error = 0;
1085                 break;
1086         case POSIX_FADV_DONTNEED:
1087                 error = 0;
1088                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1089                 if (vp->v_iflag & VI_DOOMED) {
1090                         VOP_UNLOCK(vp, 0);
1091                         break;
1092                 }
1093
1094                 /*
1095                  * Round to block boundaries (and later possibly further to
1096                  * page boundaries).  Applications cannot reasonably be aware  
1097                  * of the boundaries, and the rounding must be to expand at
1098                  * both extremities to cover enough.  It still doesn't cover
1099                  * read-ahead.  For partial blocks, this gives unnecessary
1100                  * discarding of buffers but is efficient enough since the
1101                  * pages usually remain in VMIO for some time.
1102                  */
1103                 bsize = vp->v_bufobj.bo_bsize;
1104                 bstart = rounddown(ap->a_start, bsize);
1105                 bend = roundup(ap->a_end, bsize);
1106
1107                 /*
1108                  * Deactivate pages in the specified range from the backing VM
1109                  * object.  Pages that are resident in the buffer cache will
1110                  * remain wired until their corresponding buffers are released
1111                  * below.
1112                  */
1113                 if (vp->v_object != NULL) {
1114                         start = trunc_page(bstart);
1115                         end = round_page(bend);
1116                         VM_OBJECT_RLOCK(vp->v_object);
1117                         vm_object_page_noreuse(vp->v_object, OFF_TO_IDX(start),
1118                             OFF_TO_IDX(end));
1119                         VM_OBJECT_RUNLOCK(vp->v_object);
1120                 }
1121
1122                 bo = &vp->v_bufobj;
1123                 BO_RLOCK(bo);
1124                 startn = bstart / bsize;
1125                 endn = bend / bsize;
1126                 error = bnoreuselist(&bo->bo_clean, bo, startn, endn);
1127                 if (error == 0)
1128                         error = bnoreuselist(&bo->bo_dirty, bo, startn, endn);
1129                 BO_RUNLOCK(bo);
1130                 VOP_UNLOCK(vp, 0);
1131                 break;
1132         default:
1133                 error = EINVAL;
1134                 break;
1135         }
1136         return (error);
1137 }
1138
1139 int
1140 vop_stdunp_bind(struct vop_unp_bind_args *ap)
1141 {
1142
1143         ap->a_vp->v_unpcb = ap->a_unpcb;
1144         return (0);
1145 }
1146
1147 int
1148 vop_stdunp_connect(struct vop_unp_connect_args *ap)
1149 {
1150
1151         *ap->a_unpcb = ap->a_vp->v_unpcb;
1152         return (0);
1153 }
1154
1155 int
1156 vop_stdunp_detach(struct vop_unp_detach_args *ap)
1157 {
1158
1159         ap->a_vp->v_unpcb = NULL;
1160         return (0);
1161 }
1162
1163 static int
1164 vop_stdis_text(struct vop_is_text_args *ap)
1165 {
1166
1167         return ((ap->a_vp->v_vflag & VV_TEXT) != 0);
1168 }
1169
1170 static int
1171 vop_stdset_text(struct vop_set_text_args *ap)
1172 {
1173
1174         ap->a_vp->v_vflag |= VV_TEXT;
1175         return (0);
1176 }
1177
1178 static int
1179 vop_stdunset_text(struct vop_unset_text_args *ap)
1180 {
1181
1182         ap->a_vp->v_vflag &= ~VV_TEXT;
1183         return (0);
1184 }
1185
1186 static int
1187 vop_stdget_writecount(struct vop_get_writecount_args *ap)
1188 {
1189
1190         *ap->a_writecount = ap->a_vp->v_writecount;
1191         return (0);
1192 }
1193
1194 static int
1195 vop_stdadd_writecount(struct vop_add_writecount_args *ap)
1196 {
1197
1198         ap->a_vp->v_writecount += ap->a_inc;
1199         return (0);
1200 }
1201
1202 /*
1203  * vfs default ops
1204  * used to fill the vfs function table to get reasonable default return values.
1205  */
1206 int
1207 vfs_stdroot (mp, flags, vpp)
1208         struct mount *mp;
1209         int flags;
1210         struct vnode **vpp;
1211 {
1212
1213         return (EOPNOTSUPP);
1214 }
1215
1216 int
1217 vfs_stdstatfs (mp, sbp)
1218         struct mount *mp;
1219         struct statfs *sbp;
1220 {
1221
1222         return (EOPNOTSUPP);
1223 }
1224
1225 int
1226 vfs_stdquotactl (mp, cmds, uid, arg)
1227         struct mount *mp;
1228         int cmds;
1229         uid_t uid;
1230         void *arg;
1231 {
1232
1233         return (EOPNOTSUPP);
1234 }
1235
1236 int
1237 vfs_stdsync(mp, waitfor)
1238         struct mount *mp;
1239         int waitfor;
1240 {
1241         struct vnode *vp, *mvp;
1242         struct thread *td;
1243         int error, lockreq, allerror = 0;
1244
1245         td = curthread;
1246         lockreq = LK_EXCLUSIVE | LK_INTERLOCK;
1247         if (waitfor != MNT_WAIT)
1248                 lockreq |= LK_NOWAIT;
1249         /*
1250          * Force stale buffer cache information to be flushed.
1251          */
1252 loop:
1253         MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1254                 if (vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1255                         VI_UNLOCK(vp);
1256                         continue;
1257                 }
1258                 if ((error = vget(vp, lockreq, td)) != 0) {
1259                         if (error == ENOENT) {
1260                                 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1261                                 goto loop;
1262                         }
1263                         continue;
1264                 }
1265                 error = VOP_FSYNC(vp, waitfor, td);
1266                 if (error)
1267                         allerror = error;
1268                 vput(vp);
1269         }
1270         return (allerror);
1271 }
1272
1273 int
1274 vfs_stdnosync (mp, waitfor)
1275         struct mount *mp;
1276         int waitfor;
1277 {
1278
1279         return (0);
1280 }
1281
1282 int
1283 vfs_stdvget (mp, ino, flags, vpp)
1284         struct mount *mp;
1285         ino_t ino;
1286         int flags;
1287         struct vnode **vpp;
1288 {
1289
1290         return (EOPNOTSUPP);
1291 }
1292
1293 int
1294 vfs_stdfhtovp (mp, fhp, flags, vpp)
1295         struct mount *mp;
1296         struct fid *fhp;
1297         int flags;
1298         struct vnode **vpp;
1299 {
1300
1301         return (EOPNOTSUPP);
1302 }
1303
1304 int
1305 vfs_stdinit (vfsp)
1306         struct vfsconf *vfsp;
1307 {
1308
1309         return (0);
1310 }
1311
1312 int
1313 vfs_stduninit (vfsp)
1314         struct vfsconf *vfsp;
1315 {
1316
1317         return(0);
1318 }
1319
1320 int
1321 vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, attrname)
1322         struct mount *mp;
1323         int cmd;
1324         struct vnode *filename_vp;
1325         int attrnamespace;
1326         const char *attrname;
1327 {
1328
1329         if (filename_vp != NULL)
1330                 VOP_UNLOCK(filename_vp, 0);
1331         return (EOPNOTSUPP);
1332 }
1333
1334 int
1335 vfs_stdsysctl(mp, op, req)
1336         struct mount *mp;
1337         fsctlop_t op;
1338         struct sysctl_req *req;
1339 {
1340
1341         return (EOPNOTSUPP);
1342 }
1343
1344 static vop_bypass_t *
1345 bp_by_off(struct vop_vector *vop, struct vop_generic_args *a)
1346 {
1347
1348         return (*(vop_bypass_t **)((char *)vop + a->a_desc->vdesc_vop_offset));
1349 }
1350
1351 int
1352 vop_sigdefer(struct vop_vector *vop, struct vop_generic_args *a)
1353 {
1354         vop_bypass_t *bp;
1355         int prev_stops, rc;
1356
1357         for (; vop != NULL; vop = vop->vop_default) {
1358                 bp = bp_by_off(vop, a);
1359                 if (bp != NULL)
1360                         break;
1361
1362                 /*
1363                  * Bypass is not really supported.  It is done for
1364                  * fallback to unimplemented vops in the default
1365                  * vector.
1366                  */
1367                 bp = vop->vop_bypass;
1368                 if (bp != NULL)
1369                         break;
1370         }
1371         MPASS(bp != NULL);
1372
1373         prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
1374         rc = bp(a);
1375         sigallowstop(prev_stops);
1376         return (rc);
1377 }