]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_default.c
MFV r328220: 8677 Open-Context Channel Programs
[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                 default:
486                         return (EINVAL);
487         }
488         /* NOTREACHED */
489 }
490
491 /*
492  * Standard lock, unlock and islocked functions.
493  */
494 int
495 vop_stdlock(ap)
496         struct vop_lock1_args /* {
497                 struct vnode *a_vp;
498                 int a_flags;
499                 char *file;
500                 int line;
501         } */ *ap;
502 {
503         struct vnode *vp = ap->a_vp;
504         struct mtx *ilk;
505
506         ilk = VI_MTX(vp);
507         return (lockmgr_lock_fast_path(vp->v_vnlock, ap->a_flags,
508             (ilk != NULL) ? &ilk->lock_object : NULL, ap->a_file, ap->a_line));
509 }
510
511 /* See above. */
512 int
513 vop_stdunlock(ap)
514         struct vop_unlock_args /* {
515                 struct vnode *a_vp;
516                 int a_flags;
517         } */ *ap;
518 {
519         struct vnode *vp = ap->a_vp;
520         struct mtx *ilk;
521
522         ilk = VI_MTX(vp);
523         return (lockmgr_unlock_fast_path(vp->v_vnlock, ap->a_flags,
524             (ilk != NULL) ? &ilk->lock_object : NULL));
525 }
526
527 /* See above. */
528 int
529 vop_stdislocked(ap)
530         struct vop_islocked_args /* {
531                 struct vnode *a_vp;
532         } */ *ap;
533 {
534
535         return (lockstatus(ap->a_vp->v_vnlock));
536 }
537
538 /*
539  * Return true for select/poll.
540  */
541 int
542 vop_nopoll(ap)
543         struct vop_poll_args /* {
544                 struct vnode *a_vp;
545                 int  a_events;
546                 struct ucred *a_cred;
547                 struct thread *a_td;
548         } */ *ap;
549 {
550
551         return (poll_no_poll(ap->a_events));
552 }
553
554 /*
555  * Implement poll for local filesystems that support it.
556  */
557 int
558 vop_stdpoll(ap)
559         struct vop_poll_args /* {
560                 struct vnode *a_vp;
561                 int  a_events;
562                 struct ucred *a_cred;
563                 struct thread *a_td;
564         } */ *ap;
565 {
566         if (ap->a_events & ~POLLSTANDARD)
567                 return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
568         return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
569 }
570
571 /*
572  * Return our mount point, as we will take charge of the writes.
573  */
574 int
575 vop_stdgetwritemount(ap)
576         struct vop_getwritemount_args /* {
577                 struct vnode *a_vp;
578                 struct mount **a_mpp;
579         } */ *ap;
580 {
581         struct mount *mp;
582
583         /*
584          * XXX Since this is called unlocked we may be recycled while
585          * attempting to ref the mount.  If this is the case or mountpoint
586          * will be set to NULL.  We only have to prevent this call from
587          * returning with a ref to an incorrect mountpoint.  It is not
588          * harmful to return with a ref to our previous mountpoint.
589          */
590         mp = ap->a_vp->v_mount;
591         if (mp != NULL) {
592                 vfs_ref(mp);
593                 if (mp != ap->a_vp->v_mount) {
594                         vfs_rel(mp);
595                         mp = NULL;
596                 }
597         }
598         *(ap->a_mpp) = mp;
599         return (0);
600 }
601
602 /* XXX Needs good comment and VOP_BMAP(9) manpage */
603 int
604 vop_stdbmap(ap)
605         struct vop_bmap_args /* {
606                 struct vnode *a_vp;
607                 daddr_t  a_bn;
608                 struct bufobj **a_bop;
609                 daddr_t *a_bnp;
610                 int *a_runp;
611                 int *a_runb;
612         } */ *ap;
613 {
614
615         if (ap->a_bop != NULL)
616                 *ap->a_bop = &ap->a_vp->v_bufobj;
617         if (ap->a_bnp != NULL)
618                 *ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize);
619         if (ap->a_runp != NULL)
620                 *ap->a_runp = 0;
621         if (ap->a_runb != NULL)
622                 *ap->a_runb = 0;
623         return (0);
624 }
625
626 int
627 vop_stdfsync(ap)
628         struct vop_fsync_args /* {
629                 struct vnode *a_vp;
630                 int a_waitfor;
631                 struct thread *a_td;
632         } */ *ap;
633 {
634         struct vnode *vp = ap->a_vp;
635         struct buf *bp;
636         struct bufobj *bo;
637         struct buf *nbp;
638         int error = 0;
639         int maxretry = 1000;     /* large, arbitrarily chosen */
640
641         bo = &vp->v_bufobj;
642         BO_LOCK(bo);
643 loop1:
644         /*
645          * MARK/SCAN initialization to avoid infinite loops.
646          */
647         TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
648                 bp->b_vflags &= ~BV_SCANNED;
649                 bp->b_error = 0;
650         }
651
652         /*
653          * Flush all dirty buffers associated with a vnode.
654          */
655 loop2:
656         TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
657                 if ((bp->b_vflags & BV_SCANNED) != 0)
658                         continue;
659                 bp->b_vflags |= BV_SCANNED;
660                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
661                         if (ap->a_waitfor != MNT_WAIT)
662                                 continue;
663                         if (BUF_LOCK(bp,
664                             LK_EXCLUSIVE | LK_INTERLOCK | LK_SLEEPFAIL,
665                             BO_LOCKPTR(bo)) != 0) {
666                                 BO_LOCK(bo);
667                                 goto loop1;
668                         }
669                         BO_LOCK(bo);
670                 }
671                 BO_UNLOCK(bo);
672                 KASSERT(bp->b_bufobj == bo,
673                     ("bp %p wrong b_bufobj %p should be %p",
674                     bp, bp->b_bufobj, bo));
675                 if ((bp->b_flags & B_DELWRI) == 0)
676                         panic("fsync: not dirty");
677                 if ((vp->v_object != NULL) && (bp->b_flags & B_CLUSTEROK)) {
678                         vfs_bio_awrite(bp);
679                 } else {
680                         bremfree(bp);
681                         bawrite(bp);
682                 }
683                 BO_LOCK(bo);
684                 goto loop2;
685         }
686
687         /*
688          * If synchronous the caller expects us to completely resolve all
689          * dirty buffers in the system.  Wait for in-progress I/O to
690          * complete (which could include background bitmap writes), then
691          * retry if dirty blocks still exist.
692          */
693         if (ap->a_waitfor == MNT_WAIT) {
694                 bufobj_wwait(bo, 0, 0);
695                 if (bo->bo_dirty.bv_cnt > 0) {
696                         /*
697                          * If we are unable to write any of these buffers
698                          * then we fail now rather than trying endlessly
699                          * to write them out.
700                          */
701                         TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
702                                 if ((error = bp->b_error) != 0)
703                                         break;
704                         if (error == 0 && --maxretry >= 0)
705                                 goto loop1;
706                         error = EAGAIN;
707                 }
708         }
709         BO_UNLOCK(bo);
710         if (error == EAGAIN)
711                 vn_printf(vp, "fsync: giving up on dirty ");
712
713         return (error);
714 }
715
716 static int
717 vop_stdfdatasync(struct vop_fdatasync_args *ap)
718 {
719
720         return (VOP_FSYNC(ap->a_vp, MNT_WAIT, ap->a_td));
721 }
722
723 int
724 vop_stdfdatasync_buf(struct vop_fdatasync_args *ap)
725 {
726         struct vop_fsync_args apf;
727
728         apf.a_vp = ap->a_vp;
729         apf.a_waitfor = MNT_WAIT;
730         apf.a_td = ap->a_td;
731         return (vop_stdfsync(&apf));
732 }
733
734 /* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */
735 int
736 vop_stdgetpages(ap)
737         struct vop_getpages_args /* {
738                 struct vnode *a_vp;
739                 vm_page_t *a_m;
740                 int a_count;
741                 int *a_rbehind;
742                 int *a_rahead;
743         } */ *ap;
744 {
745
746         return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
747             ap->a_count, ap->a_rbehind, ap->a_rahead, NULL, NULL);
748 }
749
750 static int
751 vop_stdgetpages_async(struct vop_getpages_async_args *ap)
752 {
753         int error;
754
755         error = VOP_GETPAGES(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
756             ap->a_rahead);
757         ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
758         return (error);
759 }
760
761 int
762 vop_stdkqfilter(struct vop_kqfilter_args *ap)
763 {
764         return vfs_kqfilter(ap);
765 }
766
767 /* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */
768 int
769 vop_stdputpages(ap)
770         struct vop_putpages_args /* {
771                 struct vnode *a_vp;
772                 vm_page_t *a_m;
773                 int a_count;
774                 int a_sync;
775                 int *a_rtvals;
776         } */ *ap;
777 {
778
779         return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
780              ap->a_sync, ap->a_rtvals);
781 }
782
783 int
784 vop_stdvptofh(struct vop_vptofh_args *ap)
785 {
786         return (EOPNOTSUPP);
787 }
788
789 int
790 vop_stdvptocnp(struct vop_vptocnp_args *ap)
791 {
792         struct vnode *vp = ap->a_vp;
793         struct vnode **dvp = ap->a_vpp;
794         struct ucred *cred = ap->a_cred;
795         char *buf = ap->a_buf;
796         int *buflen = ap->a_buflen;
797         char *dirbuf, *cpos;
798         int i, error, eofflag, dirbuflen, flags, locked, len, covered;
799         off_t off;
800         ino_t fileno;
801         struct vattr va;
802         struct nameidata nd;
803         struct thread *td;
804         struct dirent *dp;
805         struct vnode *mvp;
806
807         i = *buflen;
808         error = 0;
809         covered = 0;
810         td = curthread;
811
812         if (vp->v_type != VDIR)
813                 return (ENOENT);
814
815         error = VOP_GETATTR(vp, &va, cred);
816         if (error)
817                 return (error);
818
819         VREF(vp);
820         locked = VOP_ISLOCKED(vp);
821         VOP_UNLOCK(vp, 0);
822         NDINIT_ATVP(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
823             "..", vp, td);
824         flags = FREAD;
825         error = vn_open_cred(&nd, &flags, 0, VN_OPEN_NOAUDIT, cred, NULL);
826         if (error) {
827                 vn_lock(vp, locked | LK_RETRY);
828                 return (error);
829         }
830         NDFREE(&nd, NDF_ONLY_PNBUF);
831
832         mvp = *dvp = nd.ni_vp;
833
834         if (vp->v_mount != (*dvp)->v_mount &&
835             ((*dvp)->v_vflag & VV_ROOT) &&
836             ((*dvp)->v_mount->mnt_flag & MNT_UNION)) {
837                 *dvp = (*dvp)->v_mount->mnt_vnodecovered;
838                 VREF(mvp);
839                 VOP_UNLOCK(mvp, 0);
840                 vn_close(mvp, FREAD, cred, td);
841                 VREF(*dvp);
842                 vn_lock(*dvp, LK_SHARED | LK_RETRY);
843                 covered = 1;
844         }
845
846         fileno = va.va_fileid;
847
848         dirbuflen = DEV_BSIZE;
849         if (dirbuflen < va.va_blocksize)
850                 dirbuflen = va.va_blocksize;
851         dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
852
853         if ((*dvp)->v_type != VDIR) {
854                 error = ENOENT;
855                 goto out;
856         }
857
858         off = 0;
859         len = 0;
860         do {
861                 /* call VOP_READDIR of parent */
862                 error = get_next_dirent(*dvp, &dp, dirbuf, dirbuflen, &off,
863                                         &cpos, &len, &eofflag, td);
864                 if (error)
865                         goto out;
866
867                 if ((dp->d_type != DT_WHT) &&
868                     (dp->d_fileno == fileno)) {
869                         if (covered) {
870                                 VOP_UNLOCK(*dvp, 0);
871                                 vn_lock(mvp, LK_SHARED | LK_RETRY);
872                                 if (dirent_exists(mvp, dp->d_name, td)) {
873                                         error = ENOENT;
874                                         VOP_UNLOCK(mvp, 0);
875                                         vn_lock(*dvp, LK_SHARED | LK_RETRY);
876                                         goto out;
877                                 }
878                                 VOP_UNLOCK(mvp, 0);
879                                 vn_lock(*dvp, LK_SHARED | LK_RETRY);
880                         }
881                         i -= dp->d_namlen;
882
883                         if (i < 0) {
884                                 error = ENOMEM;
885                                 goto out;
886                         }
887                         if (dp->d_namlen == 1 && dp->d_name[0] == '.') {
888                                 error = ENOENT;
889                         } else {
890                                 bcopy(dp->d_name, buf + i, dp->d_namlen);
891                                 error = 0;
892                         }
893                         goto out;
894                 }
895         } while (len > 0 || !eofflag);
896         error = ENOENT;
897
898 out:
899         free(dirbuf, M_TEMP);
900         if (!error) {
901                 *buflen = i;
902                 vref(*dvp);
903         }
904         if (covered) {
905                 vput(*dvp);
906                 vrele(mvp);
907         } else {
908                 VOP_UNLOCK(mvp, 0);
909                 vn_close(mvp, FREAD, cred, td);
910         }
911         vn_lock(vp, locked | LK_RETRY);
912         return (error);
913 }
914
915 int
916 vop_stdallocate(struct vop_allocate_args *ap)
917 {
918 #ifdef __notyet__
919         struct statfs *sfs;
920         off_t maxfilesize = 0;
921 #endif
922         struct iovec aiov;
923         struct vattr vattr, *vap;
924         struct uio auio;
925         off_t fsize, len, cur, offset;
926         uint8_t *buf;
927         struct thread *td;
928         struct vnode *vp;
929         size_t iosize;
930         int error;
931
932         buf = NULL;
933         error = 0;
934         td = curthread;
935         vap = &vattr;
936         vp = ap->a_vp;
937         len = *ap->a_len;
938         offset = *ap->a_offset;
939
940         error = VOP_GETATTR(vp, vap, td->td_ucred);
941         if (error != 0)
942                 goto out;
943         fsize = vap->va_size;
944         iosize = vap->va_blocksize;
945         if (iosize == 0)
946                 iosize = BLKDEV_IOSIZE;
947         if (iosize > MAXPHYS)
948                 iosize = MAXPHYS;
949         buf = malloc(iosize, M_TEMP, M_WAITOK);
950
951 #ifdef __notyet__
952         /*
953          * Check if the filesystem sets f_maxfilesize; if not use
954          * VOP_SETATTR to perform the check.
955          */
956         sfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
957         error = VFS_STATFS(vp->v_mount, sfs, td);
958         if (error == 0)
959                 maxfilesize = sfs->f_maxfilesize;
960         free(sfs, M_STATFS);
961         if (error != 0)
962                 goto out;
963         if (maxfilesize) {
964                 if (offset > maxfilesize || len > maxfilesize ||
965                     offset + len > maxfilesize) {
966                         error = EFBIG;
967                         goto out;
968                 }
969         } else
970 #endif
971         if (offset + len > vap->va_size) {
972                 /*
973                  * Test offset + len against the filesystem's maxfilesize.
974                  */
975                 VATTR_NULL(vap);
976                 vap->va_size = offset + len;
977                 error = VOP_SETATTR(vp, vap, td->td_ucred);
978                 if (error != 0)
979                         goto out;
980                 VATTR_NULL(vap);
981                 vap->va_size = fsize;
982                 error = VOP_SETATTR(vp, vap, td->td_ucred);
983                 if (error != 0)
984                         goto out;
985         }
986
987         for (;;) {
988                 /*
989                  * Read and write back anything below the nominal file
990                  * size.  There's currently no way outside the filesystem
991                  * to know whether this area is sparse or not.
992                  */
993                 cur = iosize;
994                 if ((offset % iosize) != 0)
995                         cur -= (offset % iosize);
996                 if (cur > len)
997                         cur = len;
998                 if (offset < fsize) {
999                         aiov.iov_base = buf;
1000                         aiov.iov_len = cur;
1001                         auio.uio_iov = &aiov;
1002                         auio.uio_iovcnt = 1;
1003                         auio.uio_offset = offset;
1004                         auio.uio_resid = cur;
1005                         auio.uio_segflg = UIO_SYSSPACE;
1006                         auio.uio_rw = UIO_READ;
1007                         auio.uio_td = td;
1008                         error = VOP_READ(vp, &auio, 0, td->td_ucred);
1009                         if (error != 0)
1010                                 break;
1011                         if (auio.uio_resid > 0) {
1012                                 bzero(buf + cur - auio.uio_resid,
1013                                     auio.uio_resid);
1014                         }
1015                 } else {
1016                         bzero(buf, cur);
1017                 }
1018
1019                 aiov.iov_base = buf;
1020                 aiov.iov_len = cur;
1021                 auio.uio_iov = &aiov;
1022                 auio.uio_iovcnt = 1;
1023                 auio.uio_offset = offset;
1024                 auio.uio_resid = cur;
1025                 auio.uio_segflg = UIO_SYSSPACE;
1026                 auio.uio_rw = UIO_WRITE;
1027                 auio.uio_td = td;
1028
1029                 error = VOP_WRITE(vp, &auio, 0, td->td_ucred);
1030                 if (error != 0)
1031                         break;
1032
1033                 len -= cur;
1034                 offset += cur;
1035                 if (len == 0)
1036                         break;
1037                 if (should_yield())
1038                         break;
1039         }
1040
1041  out:
1042         *ap->a_len = len;
1043         *ap->a_offset = offset;
1044         free(buf, M_TEMP);
1045         return (error);
1046 }
1047
1048 int
1049 vop_stdadvise(struct vop_advise_args *ap)
1050 {
1051         struct vnode *vp;
1052         struct bufobj *bo;
1053         daddr_t startn, endn;
1054         off_t start, end;
1055         int bsize, error;
1056
1057         vp = ap->a_vp;
1058         switch (ap->a_advice) {
1059         case POSIX_FADV_WILLNEED:
1060                 /*
1061                  * Do nothing for now.  Filesystems should provide a
1062                  * custom method which starts an asynchronous read of
1063                  * the requested region.
1064                  */
1065                 error = 0;
1066                 break;
1067         case POSIX_FADV_DONTNEED:
1068                 error = 0;
1069                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1070                 if (vp->v_iflag & VI_DOOMED) {
1071                         VOP_UNLOCK(vp, 0);
1072                         break;
1073                 }
1074
1075                 /*
1076                  * Deactivate pages in the specified range from the backing VM
1077                  * object.  Pages that are resident in the buffer cache will
1078                  * remain wired until their corresponding buffers are released
1079                  * below.
1080                  */
1081                 if (vp->v_object != NULL) {
1082                         start = trunc_page(ap->a_start);
1083                         end = round_page(ap->a_end);
1084                         VM_OBJECT_RLOCK(vp->v_object);
1085                         vm_object_page_noreuse(vp->v_object, OFF_TO_IDX(start),
1086                             OFF_TO_IDX(end));
1087                         VM_OBJECT_RUNLOCK(vp->v_object);
1088                 }
1089
1090                 bo = &vp->v_bufobj;
1091                 BO_RLOCK(bo);
1092                 bsize = vp->v_bufobj.bo_bsize;
1093                 startn = ap->a_start / bsize;
1094                 endn = ap->a_end / bsize;
1095                 error = bnoreuselist(&bo->bo_clean, bo, startn, endn);
1096                 if (error == 0)
1097                         error = bnoreuselist(&bo->bo_dirty, bo, startn, endn);
1098                 BO_RUNLOCK(bo);
1099                 VOP_UNLOCK(vp, 0);
1100                 break;
1101         default:
1102                 error = EINVAL;
1103                 break;
1104         }
1105         return (error);
1106 }
1107
1108 int
1109 vop_stdunp_bind(struct vop_unp_bind_args *ap)
1110 {
1111
1112         ap->a_vp->v_unpcb = ap->a_unpcb;
1113         return (0);
1114 }
1115
1116 int
1117 vop_stdunp_connect(struct vop_unp_connect_args *ap)
1118 {
1119
1120         *ap->a_unpcb = ap->a_vp->v_unpcb;
1121         return (0);
1122 }
1123
1124 int
1125 vop_stdunp_detach(struct vop_unp_detach_args *ap)
1126 {
1127
1128         ap->a_vp->v_unpcb = NULL;
1129         return (0);
1130 }
1131
1132 static int
1133 vop_stdis_text(struct vop_is_text_args *ap)
1134 {
1135
1136         return ((ap->a_vp->v_vflag & VV_TEXT) != 0);
1137 }
1138
1139 static int
1140 vop_stdset_text(struct vop_set_text_args *ap)
1141 {
1142
1143         ap->a_vp->v_vflag |= VV_TEXT;
1144         return (0);
1145 }
1146
1147 static int
1148 vop_stdunset_text(struct vop_unset_text_args *ap)
1149 {
1150
1151         ap->a_vp->v_vflag &= ~VV_TEXT;
1152         return (0);
1153 }
1154
1155 static int
1156 vop_stdget_writecount(struct vop_get_writecount_args *ap)
1157 {
1158
1159         *ap->a_writecount = ap->a_vp->v_writecount;
1160         return (0);
1161 }
1162
1163 static int
1164 vop_stdadd_writecount(struct vop_add_writecount_args *ap)
1165 {
1166
1167         ap->a_vp->v_writecount += ap->a_inc;
1168         return (0);
1169 }
1170
1171 /*
1172  * vfs default ops
1173  * used to fill the vfs function table to get reasonable default return values.
1174  */
1175 int
1176 vfs_stdroot (mp, flags, vpp)
1177         struct mount *mp;
1178         int flags;
1179         struct vnode **vpp;
1180 {
1181
1182         return (EOPNOTSUPP);
1183 }
1184
1185 int
1186 vfs_stdstatfs (mp, sbp)
1187         struct mount *mp;
1188         struct statfs *sbp;
1189 {
1190
1191         return (EOPNOTSUPP);
1192 }
1193
1194 int
1195 vfs_stdquotactl (mp, cmds, uid, arg)
1196         struct mount *mp;
1197         int cmds;
1198         uid_t uid;
1199         void *arg;
1200 {
1201
1202         return (EOPNOTSUPP);
1203 }
1204
1205 int
1206 vfs_stdsync(mp, waitfor)
1207         struct mount *mp;
1208         int waitfor;
1209 {
1210         struct vnode *vp, *mvp;
1211         struct thread *td;
1212         int error, lockreq, allerror = 0;
1213
1214         td = curthread;
1215         lockreq = LK_EXCLUSIVE | LK_INTERLOCK;
1216         if (waitfor != MNT_WAIT)
1217                 lockreq |= LK_NOWAIT;
1218         /*
1219          * Force stale buffer cache information to be flushed.
1220          */
1221 loop:
1222         MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1223                 if (vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1224                         VI_UNLOCK(vp);
1225                         continue;
1226                 }
1227                 if ((error = vget(vp, lockreq, td)) != 0) {
1228                         if (error == ENOENT) {
1229                                 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1230                                 goto loop;
1231                         }
1232                         continue;
1233                 }
1234                 error = VOP_FSYNC(vp, waitfor, td);
1235                 if (error)
1236                         allerror = error;
1237                 vput(vp);
1238         }
1239         return (allerror);
1240 }
1241
1242 int
1243 vfs_stdnosync (mp, waitfor)
1244         struct mount *mp;
1245         int waitfor;
1246 {
1247
1248         return (0);
1249 }
1250
1251 int
1252 vfs_stdvget (mp, ino, flags, vpp)
1253         struct mount *mp;
1254         ino_t ino;
1255         int flags;
1256         struct vnode **vpp;
1257 {
1258
1259         return (EOPNOTSUPP);
1260 }
1261
1262 int
1263 vfs_stdfhtovp (mp, fhp, flags, vpp)
1264         struct mount *mp;
1265         struct fid *fhp;
1266         int flags;
1267         struct vnode **vpp;
1268 {
1269
1270         return (EOPNOTSUPP);
1271 }
1272
1273 int
1274 vfs_stdinit (vfsp)
1275         struct vfsconf *vfsp;
1276 {
1277
1278         return (0);
1279 }
1280
1281 int
1282 vfs_stduninit (vfsp)
1283         struct vfsconf *vfsp;
1284 {
1285
1286         return(0);
1287 }
1288
1289 int
1290 vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, attrname)
1291         struct mount *mp;
1292         int cmd;
1293         struct vnode *filename_vp;
1294         int attrnamespace;
1295         const char *attrname;
1296 {
1297
1298         if (filename_vp != NULL)
1299                 VOP_UNLOCK(filename_vp, 0);
1300         return (EOPNOTSUPP);
1301 }
1302
1303 int
1304 vfs_stdsysctl(mp, op, req)
1305         struct mount *mp;
1306         fsctlop_t op;
1307         struct sysctl_req *req;
1308 {
1309
1310         return (EOPNOTSUPP);
1311 }
1312
1313 /* end of vfs default ops */