]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/vfs_default.c
MFC 220791,220793,220846,221836,226364:
[FreeBSD/stable/8.git] / sys / kern / vfs_default.c
1 /*-
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed
6  * to Berkeley by John Heidemann of the UCLA Ficus project.
7  *
8  * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bio.h>
41 #include <sys/buf.h>
42 #include <sys/conf.h>
43 #include <sys/event.h>
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
46 #include <sys/lock.h>
47 #include <sys/lockf.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mutex.h>
51 #include <sys/namei.h>
52 #include <sys/fcntl.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <sys/dirent.h>
56 #include <sys/poll.h>
57
58 #include <security/mac/mac_framework.h>
59
60 #include <vm/vm.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_extern.h>
63 #include <vm/pmap.h>
64 #include <vm/vm_map.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_pager.h>
67 #include <vm/vnode_pager.h>
68
69 static int      vop_nolookup(struct vop_lookup_args *);
70 static int      vop_norename(struct vop_rename_args *);
71 static int      vop_nostrategy(struct vop_strategy_args *);
72 static int      get_next_dirent(struct vnode *vp, struct dirent **dpp,
73                                 char *dirbuf, int dirbuflen, off_t *off,
74                                 char **cpos, int *len, int *eofflag,
75                                 struct thread *td);
76 static int      dirent_exists(struct vnode *vp, const char *dirname,
77                               struct thread *td);
78
79 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
80
81 /*
82  * This vnode table stores what we want to do if the filesystem doesn't
83  * implement a particular VOP.
84  *
85  * If there is no specific entry here, we will return EOPNOTSUPP.
86  *
87  * Note that every filesystem has to implement either vop_access
88  * or vop_accessx; failing to do so will result in immediate crash
89  * due to stack overflow, as vop_stdaccess() calls vop_stdaccessx(),
90  * which calls vop_stdaccess() etc.
91  */
92
93 struct vop_vector default_vnodeops = {
94         .vop_default =          NULL,
95         .vop_bypass =           VOP_EOPNOTSUPP,
96
97         .vop_access =           vop_stdaccess,
98         .vop_accessx =          vop_stdaccessx,
99         .vop_advlock =          vop_stdadvlock,
100         .vop_advlockasync =     vop_stdadvlockasync,
101         .vop_allocate =         vop_stdallocate,
102         .vop_bmap =             vop_stdbmap,
103         .vop_close =            VOP_NULL,
104         .vop_fsync =            VOP_NULL,
105         .vop_getpages =         vop_stdgetpages,
106         .vop_getwritemount =    vop_stdgetwritemount,
107         .vop_inactive =         VOP_NULL,
108         .vop_ioctl =            VOP_ENOTTY,
109         .vop_kqfilter =         vop_stdkqfilter,
110         .vop_islocked =         vop_stdislocked,
111         .vop_lock1 =            vop_stdlock,
112         .vop_lookup =           vop_nolookup,
113         .vop_open =             VOP_NULL,
114         .vop_pathconf =         VOP_EINVAL,
115         .vop_poll =             vop_nopoll,
116         .vop_putpages =         vop_stdputpages,
117         .vop_readlink =         VOP_EINVAL,
118         .vop_rename =           vop_norename,
119         .vop_revoke =           VOP_PANIC,
120         .vop_strategy =         vop_nostrategy,
121         .vop_unlock =           vop_stdunlock,
122         .vop_vptocnp =          vop_stdvptocnp,
123         .vop_vptofh =           vop_stdvptofh,
124 };
125
126 /*
127  * Series of placeholder functions for various error returns for
128  * VOPs.
129  */
130
131 int
132 vop_eopnotsupp(struct vop_generic_args *ap)
133 {
134         /*
135         printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name);
136         */
137
138         return (EOPNOTSUPP);
139 }
140
141 int
142 vop_ebadf(struct vop_generic_args *ap)
143 {
144
145         return (EBADF);
146 }
147
148 int
149 vop_enotty(struct vop_generic_args *ap)
150 {
151
152         return (ENOTTY);
153 }
154
155 int
156 vop_einval(struct vop_generic_args *ap)
157 {
158
159         return (EINVAL);
160 }
161
162 int
163 vop_enoent(struct vop_generic_args *ap)
164 {
165
166         return (ENOENT);
167 }
168
169 int
170 vop_null(struct vop_generic_args *ap)
171 {
172
173         return (0);
174 }
175
176 /*
177  * Helper function to panic on some bad VOPs in some filesystems.
178  */
179 int
180 vop_panic(struct vop_generic_args *ap)
181 {
182
183         panic("filesystem goof: vop_panic[%s]", ap->a_desc->vdesc_name);
184 }
185
186 /*
187  * vop_std<something> and vop_no<something> are default functions for use by
188  * filesystems that need the "default reasonable" implementation for a
189  * particular operation.
190  *
191  * The documentation for the operations they implement exists (if it exists)
192  * in the VOP_<SOMETHING>(9) manpage (all uppercase).
193  */
194
195 /*
196  * Default vop for filesystems that do not support name lookup
197  */
198 static int
199 vop_nolookup(ap)
200         struct vop_lookup_args /* {
201                 struct vnode *a_dvp;
202                 struct vnode **a_vpp;
203                 struct componentname *a_cnp;
204         } */ *ap;
205 {
206
207         *ap->a_vpp = NULL;
208         return (ENOTDIR);
209 }
210
211 /*
212  * vop_norename:
213  *
214  * Handle unlock and reference counting for arguments of vop_rename
215  * for filesystems that do not implement rename operation.
216  */
217 static int
218 vop_norename(struct vop_rename_args *ap)
219 {
220
221         vop_rename_fail(ap);
222         return (EOPNOTSUPP);
223 }
224
225 /*
226  *      vop_nostrategy:
227  *
228  *      Strategy routine for VFS devices that have none.
229  *
230  *      BIO_ERROR and B_INVAL must be cleared prior to calling any strategy
231  *      routine.  Typically this is done for a BIO_READ strategy call.
232  *      Typically B_INVAL is assumed to already be clear prior to a write
233  *      and should not be cleared manually unless you just made the buffer
234  *      invalid.  BIO_ERROR should be cleared either way.
235  */
236
237 static int
238 vop_nostrategy (struct vop_strategy_args *ap)
239 {
240         printf("No strategy for buffer at %p\n", ap->a_bp);
241         vprint("vnode", ap->a_vp);
242         ap->a_bp->b_ioflags |= BIO_ERROR;
243         ap->a_bp->b_error = EOPNOTSUPP;
244         bufdone(ap->a_bp);
245         return (EOPNOTSUPP);
246 }
247
248 static int
249 get_next_dirent(struct vnode *vp, struct dirent **dpp, char *dirbuf,
250                 int dirbuflen, off_t *off, char **cpos, int *len,
251                 int *eofflag, struct thread *td)
252 {
253         int error, reclen;
254         struct uio uio;
255         struct iovec iov;
256         struct dirent *dp;
257
258         KASSERT(VOP_ISLOCKED(vp), ("vp %p is not locked", vp));
259         KASSERT(vp->v_type == VDIR, ("vp %p is not a directory", vp));
260
261         if (*len == 0) {
262                 iov.iov_base = dirbuf;
263                 iov.iov_len = dirbuflen;
264
265                 uio.uio_iov = &iov;
266                 uio.uio_iovcnt = 1;
267                 uio.uio_offset = *off;
268                 uio.uio_resid = dirbuflen;
269                 uio.uio_segflg = UIO_SYSSPACE;
270                 uio.uio_rw = UIO_READ;
271                 uio.uio_td = td;
272
273                 *eofflag = 0;
274
275 #ifdef MAC
276                 error = mac_vnode_check_readdir(td->td_ucred, vp);
277                 if (error == 0)
278 #endif
279                         error = VOP_READDIR(vp, &uio, td->td_ucred, eofflag,
280                                 NULL, NULL);
281                 if (error)
282                         return (error);
283
284                 *off = uio.uio_offset;
285
286                 *cpos = dirbuf;
287                 *len = (dirbuflen - uio.uio_resid);
288
289                 if (*len == 0)
290                         return (ENOENT);
291         }
292
293         dp = (struct dirent *)(*cpos);
294         reclen = dp->d_reclen;
295         *dpp = dp;
296
297         /* check for malformed directory.. */
298         if (reclen < DIRENT_MINSIZE)
299                 return (EINVAL);
300
301         *cpos += reclen;
302         *len -= reclen;
303
304         return (0);
305 }
306
307 /*
308  * Check if a named file exists in a given directory vnode.
309  */
310 static int
311 dirent_exists(struct vnode *vp, const char *dirname, struct thread *td)
312 {
313         char *dirbuf, *cpos;
314         int error, eofflag, dirbuflen, len, found;
315         off_t off;
316         struct dirent *dp;
317         struct vattr va;
318
319         KASSERT(VOP_ISLOCKED(vp), ("vp %p is not locked", vp));
320         KASSERT(vp->v_type == VDIR, ("vp %p is not a directory", vp));
321
322         found = 0;
323
324         error = VOP_GETATTR(vp, &va, td->td_ucred);
325         if (error)
326                 return (found);
327
328         dirbuflen = DEV_BSIZE;
329         if (dirbuflen < va.va_blocksize)
330                 dirbuflen = va.va_blocksize;
331         dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
332
333         off = 0;
334         len = 0;
335         do {
336                 error = get_next_dirent(vp, &dp, dirbuf, dirbuflen, &off,
337                                         &cpos, &len, &eofflag, td);
338                 if (error)
339                         goto out;
340
341                 if ((dp->d_type != DT_WHT) &&
342                     !strcmp(dp->d_name, dirname)) {
343                         found = 1;
344                         goto out;
345                 }
346         } while (len > 0 || !eofflag);
347
348 out:
349         free(dirbuf, M_TEMP);
350         return (found);
351 }
352
353 int
354 vop_stdaccess(struct vop_access_args *ap)
355 {
356
357         KASSERT((ap->a_accmode & ~(VEXEC | VWRITE | VREAD | VADMIN |
358             VAPPEND)) == 0, ("invalid bit in accmode"));
359
360         return (VOP_ACCESSX(ap->a_vp, ap->a_accmode, ap->a_cred, ap->a_td));
361 }
362
363 int
364 vop_stdaccessx(struct vop_accessx_args *ap)
365 {
366         int error;
367         accmode_t accmode = ap->a_accmode;
368
369         error = vfs_unixify_accmode(&accmode);
370         if (error != 0)
371                 return (error);
372
373         if (accmode == 0)
374                 return (0);
375
376         return (VOP_ACCESS(ap->a_vp, accmode, ap->a_cred, ap->a_td));
377 }
378
379 /*
380  * Advisory record locking support
381  */
382 int
383 vop_stdadvlock(struct vop_advlock_args *ap)
384 {
385         struct vnode *vp;
386         struct ucred *cred;
387         struct vattr vattr;
388         int error;
389
390         vp = ap->a_vp;
391         cred = curthread->td_ucred;
392         vn_lock(vp, LK_SHARED | LK_RETRY);
393         error = VOP_GETATTR(vp, &vattr, cred);
394         VOP_UNLOCK(vp, 0);
395         if (error)
396                 return (error);
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 ucred *cred;
406         struct vattr vattr;
407         int error;
408
409         vp = ap->a_vp;
410         cred = curthread->td_ucred;
411         vn_lock(vp, LK_SHARED | LK_RETRY);
412         error = VOP_GETATTR(vp, &vattr, cred);
413         VOP_UNLOCK(vp, 0);
414         if (error)
415                 return (error);
416
417         return (lf_advlockasync(ap, &(vp->v_lockf), vattr.va_size));
418 }
419
420 /*
421  * vop_stdpathconf:
422  *
423  * Standard implementation of POSIX pathconf, to get information about limits
424  * for a filesystem.
425  * Override per filesystem for the case where the filesystem has smaller
426  * limits.
427  */
428 int
429 vop_stdpathconf(ap)
430         struct vop_pathconf_args /* {
431         struct vnode *a_vp;
432         int a_name;
433         int *a_retval;
434         } */ *ap;
435 {
436
437         switch (ap->a_name) {
438                 case _PC_NAME_MAX:
439                         *ap->a_retval = NAME_MAX;
440                         return (0);
441                 case _PC_PATH_MAX:
442                         *ap->a_retval = PATH_MAX;
443                         return (0);
444                 case _PC_LINK_MAX:
445                         *ap->a_retval = LINK_MAX;
446                         return (0);
447                 case _PC_MAX_CANON:
448                         *ap->a_retval = MAX_CANON;
449                         return (0);
450                 case _PC_MAX_INPUT:
451                         *ap->a_retval = MAX_INPUT;
452                         return (0);
453                 case _PC_PIPE_BUF:
454                         *ap->a_retval = PIPE_BUF;
455                         return (0);
456                 case _PC_CHOWN_RESTRICTED:
457                         *ap->a_retval = 1;
458                         return (0);
459                 case _PC_VDISABLE:
460                         *ap->a_retval = _POSIX_VDISABLE;
461                         return (0);
462                 default:
463                         return (EINVAL);
464         }
465         /* NOTREACHED */
466 }
467
468 /*
469  * Standard lock, unlock and islocked functions.
470  */
471 int
472 vop_stdlock(ap)
473         struct vop_lock1_args /* {
474                 struct vnode *a_vp;
475                 int a_flags;
476                 char *file;
477                 int line;
478         } */ *ap;
479 {
480         struct vnode *vp = ap->a_vp;
481
482         return (_lockmgr_args(vp->v_vnlock, ap->a_flags, VI_MTX(vp),
483             LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, ap->a_file,
484             ap->a_line));
485 }
486
487 /* See above. */
488 int
489 vop_stdunlock(ap)
490         struct vop_unlock_args /* {
491                 struct vnode *a_vp;
492                 int a_flags;
493         } */ *ap;
494 {
495         struct vnode *vp = ap->a_vp;
496
497         return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE, VI_MTX(vp)));
498 }
499
500 /* See above. */
501 int
502 vop_stdislocked(ap)
503         struct vop_islocked_args /* {
504                 struct vnode *a_vp;
505         } */ *ap;
506 {
507
508         return (lockstatus(ap->a_vp->v_vnlock));
509 }
510
511 /*
512  * Return true for select/poll.
513  */
514 int
515 vop_nopoll(ap)
516         struct vop_poll_args /* {
517                 struct vnode *a_vp;
518                 int  a_events;
519                 struct ucred *a_cred;
520                 struct thread *a_td;
521         } */ *ap;
522 {
523
524         return (poll_no_poll(ap->a_events));
525 }
526
527 /*
528  * Implement poll for local filesystems that support it.
529  */
530 int
531 vop_stdpoll(ap)
532         struct vop_poll_args /* {
533                 struct vnode *a_vp;
534                 int  a_events;
535                 struct ucred *a_cred;
536                 struct thread *a_td;
537         } */ *ap;
538 {
539         if (ap->a_events & ~POLLSTANDARD)
540                 return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
541         return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
542 }
543
544 /*
545  * Return our mount point, as we will take charge of the writes.
546  */
547 int
548 vop_stdgetwritemount(ap)
549         struct vop_getwritemount_args /* {
550                 struct vnode *a_vp;
551                 struct mount **a_mpp;
552         } */ *ap;
553 {
554         struct mount *mp;
555
556         /*
557          * XXX Since this is called unlocked we may be recycled while
558          * attempting to ref the mount.  If this is the case or mountpoint
559          * will be set to NULL.  We only have to prevent this call from
560          * returning with a ref to an incorrect mountpoint.  It is not
561          * harmful to return with a ref to our previous mountpoint.
562          */
563         mp = ap->a_vp->v_mount;
564         if (mp != NULL) {
565                 vfs_ref(mp);
566                 if (mp != ap->a_vp->v_mount) {
567                         vfs_rel(mp);
568                         mp = NULL;
569                 }
570         }
571         *(ap->a_mpp) = mp;
572         return (0);
573 }
574
575 /* XXX Needs good comment and VOP_BMAP(9) manpage */
576 int
577 vop_stdbmap(ap)
578         struct vop_bmap_args /* {
579                 struct vnode *a_vp;
580                 daddr_t  a_bn;
581                 struct bufobj **a_bop;
582                 daddr_t *a_bnp;
583                 int *a_runp;
584                 int *a_runb;
585         } */ *ap;
586 {
587
588         if (ap->a_bop != NULL)
589                 *ap->a_bop = &ap->a_vp->v_bufobj;
590         if (ap->a_bnp != NULL)
591                 *ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize);
592         if (ap->a_runp != NULL)
593                 *ap->a_runp = 0;
594         if (ap->a_runb != NULL)
595                 *ap->a_runb = 0;
596         return (0);
597 }
598
599 int
600 vop_stdfsync(ap)
601         struct vop_fsync_args /* {
602                 struct vnode *a_vp;
603                 struct ucred *a_cred;
604                 int a_waitfor;
605                 struct thread *a_td;
606         } */ *ap;
607 {
608         struct vnode *vp = ap->a_vp;
609         struct buf *bp;
610         struct bufobj *bo;
611         struct buf *nbp;
612         int error = 0;
613         int maxretry = 1000;     /* large, arbitrarily chosen */
614
615         bo = &vp->v_bufobj;
616         BO_LOCK(bo);
617 loop1:
618         /*
619          * MARK/SCAN initialization to avoid infinite loops.
620          */
621         TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
622                 bp->b_vflags &= ~BV_SCANNED;
623                 bp->b_error = 0;
624         }
625
626         /*
627          * Flush all dirty buffers associated with a vnode.
628          */
629 loop2:
630         TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
631                 if ((bp->b_vflags & BV_SCANNED) != 0)
632                         continue;
633                 bp->b_vflags |= BV_SCANNED;
634                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
635                         continue;
636                 BO_UNLOCK(bo);
637                 KASSERT(bp->b_bufobj == bo,
638                     ("bp %p wrong b_bufobj %p should be %p",
639                     bp, bp->b_bufobj, bo));
640                 if ((bp->b_flags & B_DELWRI) == 0)
641                         panic("fsync: not dirty");
642                 if ((vp->v_object != NULL) && (bp->b_flags & B_CLUSTEROK)) {
643                         vfs_bio_awrite(bp);
644                 } else {
645                         bremfree(bp);
646                         bawrite(bp);
647                 }
648                 BO_LOCK(bo);
649                 goto loop2;
650         }
651
652         /*
653          * If synchronous the caller expects us to completely resolve all
654          * dirty buffers in the system.  Wait for in-progress I/O to
655          * complete (which could include background bitmap writes), then
656          * retry if dirty blocks still exist.
657          */
658         if (ap->a_waitfor == MNT_WAIT) {
659                 bufobj_wwait(bo, 0, 0);
660                 if (bo->bo_dirty.bv_cnt > 0) {
661                         /*
662                          * If we are unable to write any of these buffers
663                          * then we fail now rather than trying endlessly
664                          * to write them out.
665                          */
666                         TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
667                                 if ((error = bp->b_error) == 0)
668                                         continue;
669                         if (error == 0 && --maxretry >= 0)
670                                 goto loop1;
671                         error = EAGAIN;
672                 }
673         }
674         BO_UNLOCK(bo);
675         if (error == EAGAIN)
676                 vprint("fsync: giving up on dirty", vp);
677
678         return (error);
679 }
680
681 /* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */
682 int
683 vop_stdgetpages(ap)
684         struct vop_getpages_args /* {
685                 struct vnode *a_vp;
686                 vm_page_t *a_m;
687                 int a_count;
688                 int a_reqpage;
689                 vm_ooffset_t a_offset;
690         } */ *ap;
691 {
692
693         return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
694             ap->a_count, ap->a_reqpage);
695 }
696
697 int
698 vop_stdkqfilter(struct vop_kqfilter_args *ap)
699 {
700         return vfs_kqfilter(ap);
701 }
702
703 /* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */
704 int
705 vop_stdputpages(ap)
706         struct vop_putpages_args /* {
707                 struct vnode *a_vp;
708                 vm_page_t *a_m;
709                 int a_count;
710                 int a_sync;
711                 int *a_rtvals;
712                 vm_ooffset_t a_offset;
713         } */ *ap;
714 {
715
716         return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
717              ap->a_sync, ap->a_rtvals);
718 }
719
720 int
721 vop_stdvptofh(struct vop_vptofh_args *ap)
722 {
723         return (EOPNOTSUPP);
724 }
725
726 int
727 vop_stdvptocnp(struct vop_vptocnp_args *ap)
728 {
729         struct vnode *vp = ap->a_vp;
730         struct vnode **dvp = ap->a_vpp;
731         struct ucred *cred = ap->a_cred;
732         char *buf = ap->a_buf;
733         int *buflen = ap->a_buflen;
734         char *dirbuf, *cpos;
735         int i, error, eofflag, dirbuflen, flags, locked, len, covered;
736         off_t off;
737         ino_t fileno;
738         struct vattr va;
739         struct nameidata nd;
740         struct thread *td;
741         struct dirent *dp;
742         struct vnode *mvp;
743
744         i = *buflen;
745         error = 0;
746         covered = 0;
747         td = curthread;
748
749         if (vp->v_type != VDIR)
750                 return (ENOENT);
751
752         error = VOP_GETATTR(vp, &va, cred);
753         if (error)
754                 return (error);
755
756         VREF(vp);
757         locked = VOP_ISLOCKED(vp);
758         VOP_UNLOCK(vp, 0);
759         NDINIT_ATVP(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
760             "..", vp, td);
761         flags = FREAD;
762         error = vn_open_cred(&nd, &flags, 0, VN_OPEN_NOAUDIT, cred, NULL);
763         if (error) {
764                 vn_lock(vp, locked | LK_RETRY);
765                 return (error);
766         }
767         NDFREE(&nd, NDF_ONLY_PNBUF);
768
769         mvp = *dvp = nd.ni_vp;
770
771         if (vp->v_mount != (*dvp)->v_mount &&
772             ((*dvp)->v_vflag & VV_ROOT) &&
773             ((*dvp)->v_mount->mnt_flag & MNT_UNION)) {
774                 *dvp = (*dvp)->v_mount->mnt_vnodecovered;
775                 VREF(mvp);
776                 VOP_UNLOCK(mvp, 0);
777                 vn_close(mvp, FREAD, cred, td);
778                 VREF(*dvp);
779                 vn_lock(*dvp, LK_EXCLUSIVE | LK_RETRY);
780                 covered = 1;
781         }
782
783         fileno = va.va_fileid;
784
785         dirbuflen = DEV_BSIZE;
786         if (dirbuflen < va.va_blocksize)
787                 dirbuflen = va.va_blocksize;
788         dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
789
790         if ((*dvp)->v_type != VDIR) {
791                 error = ENOENT;
792                 goto out;
793         }
794
795         off = 0;
796         len = 0;
797         do {
798                 /* call VOP_READDIR of parent */
799                 error = get_next_dirent(*dvp, &dp, dirbuf, dirbuflen, &off,
800                                         &cpos, &len, &eofflag, td);
801                 if (error)
802                         goto out;
803
804                 if ((dp->d_type != DT_WHT) &&
805                     (dp->d_fileno == fileno)) {
806                         if (covered) {
807                                 VOP_UNLOCK(*dvp, 0);
808                                 vn_lock(mvp, LK_EXCLUSIVE | LK_RETRY);
809                                 if (dirent_exists(mvp, dp->d_name, td)) {
810                                         error = ENOENT;
811                                         VOP_UNLOCK(mvp, 0);
812                                         vn_lock(*dvp, LK_EXCLUSIVE | LK_RETRY);
813                                         goto out;
814                                 }
815                                 VOP_UNLOCK(mvp, 0);
816                                 vn_lock(*dvp, LK_EXCLUSIVE | LK_RETRY);
817                         }
818                         i -= dp->d_namlen;
819
820                         if (i < 0) {
821                                 error = ENOMEM;
822                                 goto out;
823                         }
824                         bcopy(dp->d_name, buf + i, dp->d_namlen);
825                         error = 0;
826                         goto out;
827                 }
828         } while (len > 0 || !eofflag);
829         error = ENOENT;
830
831 out:
832         free(dirbuf, M_TEMP);
833         if (!error) {
834                 *buflen = i;
835                 vhold(*dvp);
836         }
837         if (covered) {
838                 vput(*dvp);
839                 vrele(mvp);
840         } else {
841                 VOP_UNLOCK(mvp, 0);
842                 vn_close(mvp, FREAD, cred, td);
843         }
844         vn_lock(vp, locked | LK_RETRY);
845         return (error);
846 }
847
848 int
849 vop_stdallocate(struct vop_allocate_args *ap)
850 {
851 #ifdef __notyet__
852         struct statfs sfs;
853 #endif
854         struct iovec aiov;
855         struct vattr vattr, *vap;
856         struct uio auio;
857         off_t fsize, len, cur, offset;
858         uint8_t *buf;
859         struct thread *td;
860         struct vnode *vp;
861         size_t iosize;
862         int error;
863
864         buf = NULL;
865         error = 0;
866         td = curthread;
867         vap = &vattr;
868         vp = ap->a_vp;
869         len = *ap->a_len;
870         offset = *ap->a_offset;
871
872         error = VOP_GETATTR(vp, vap, td->td_ucred);
873         if (error != 0)
874                 goto out;
875         fsize = vap->va_size;
876         iosize = vap->va_blocksize;
877         if (iosize == 0)
878                 iosize = BLKDEV_IOSIZE;
879         if (iosize > MAXPHYS)
880                 iosize = MAXPHYS;
881         buf = malloc(iosize, M_TEMP, M_WAITOK);
882
883 #ifdef __notyet__
884         /*
885          * Check if the filesystem sets f_maxfilesize; if not use
886          * VOP_SETATTR to perform the check.
887          */
888         error = VFS_STATFS(vp->v_mount, &sfs, td);
889         if (error != 0)
890                 goto out;
891         if (sfs.f_maxfilesize) {
892                 if (offset > sfs.f_maxfilesize || len > sfs.f_maxfilesize ||
893                     offset + len > sfs.f_maxfilesize) {
894                         error = EFBIG;
895                         goto out;
896                 }
897         } else
898 #endif
899         if (offset + len > vap->va_size) {
900                 /*
901                  * Test offset + len against the filesystem's maxfilesize.
902                  */
903                 VATTR_NULL(vap);
904                 vap->va_size = offset + len;
905                 error = VOP_SETATTR(vp, vap, td->td_ucred);
906                 if (error != 0)
907                         goto out;
908                 VATTR_NULL(vap);
909                 vap->va_size = fsize;
910                 error = VOP_SETATTR(vp, vap, td->td_ucred);
911                 if (error != 0)
912                         goto out;
913         }
914
915         for (;;) {
916                 /*
917                  * Read and write back anything below the nominal file
918                  * size.  There's currently no way outside the filesystem
919                  * to know whether this area is sparse or not.
920                  */
921                 cur = iosize;
922                 if ((offset % iosize) != 0)
923                         cur -= (offset % iosize);
924                 if (cur > len)
925                         cur = len;
926                 if (offset < fsize) {
927                         aiov.iov_base = buf;
928                         aiov.iov_len = cur;
929                         auio.uio_iov = &aiov;
930                         auio.uio_iovcnt = 1;
931                         auio.uio_offset = offset;
932                         auio.uio_resid = cur;
933                         auio.uio_segflg = UIO_SYSSPACE;
934                         auio.uio_rw = UIO_READ;
935                         auio.uio_td = td;
936                         error = VOP_READ(vp, &auio, 0, td->td_ucred);
937                         if (error != 0)
938                                 break;
939                         if (auio.uio_resid > 0) {
940                                 bzero(buf + cur - auio.uio_resid,
941                                     auio.uio_resid);
942                         }
943                 } else {
944                         bzero(buf, cur);
945                 }
946
947                 aiov.iov_base = buf;
948                 aiov.iov_len = cur;
949                 auio.uio_iov = &aiov;
950                 auio.uio_iovcnt = 1;
951                 auio.uio_offset = offset;
952                 auio.uio_resid = cur;
953                 auio.uio_segflg = UIO_SYSSPACE;
954                 auio.uio_rw = UIO_WRITE;
955                 auio.uio_td = td;
956
957                 error = VOP_WRITE(vp, &auio, 0, td->td_ucred);
958                 if (error != 0)
959                         break;
960
961                 len -= cur;
962                 offset += cur;
963                 if (len == 0)
964                         break;
965                 if (should_yield())
966                         break;
967         }
968
969  out:
970         *ap->a_len = len;
971         *ap->a_offset = offset;
972         free(buf, M_TEMP);
973         return (error);
974 }
975
976 /*
977  * vfs default ops
978  * used to fill the vfs function table to get reasonable default return values.
979  */
980 int
981 vfs_stdroot (mp, flags, vpp)
982         struct mount *mp;
983         int flags;
984         struct vnode **vpp;
985 {
986
987         return (EOPNOTSUPP);
988 }
989
990 int
991 vfs_stdstatfs (mp, sbp)
992         struct mount *mp;
993         struct statfs *sbp;
994 {
995
996         return (EOPNOTSUPP);
997 }
998
999 int
1000 vfs_stdquotactl (mp, cmds, uid, arg)
1001         struct mount *mp;
1002         int cmds;
1003         uid_t uid;
1004         void *arg;
1005 {
1006
1007         return (EOPNOTSUPP);
1008 }
1009
1010 int
1011 vfs_stdsync(mp, waitfor)
1012         struct mount *mp;
1013         int waitfor;
1014 {
1015         struct vnode *vp, *mvp;
1016         struct thread *td;
1017         int error, lockreq, allerror = 0;
1018
1019         td = curthread;
1020         lockreq = LK_EXCLUSIVE | LK_INTERLOCK;
1021         if (waitfor != MNT_WAIT)
1022                 lockreq |= LK_NOWAIT;
1023         /*
1024          * Force stale buffer cache information to be flushed.
1025          */
1026         MNT_ILOCK(mp);
1027 loop:
1028         MNT_VNODE_FOREACH(vp, mp, mvp) {
1029                 /* bv_cnt is an acceptable race here. */
1030                 if (vp->v_bufobj.bo_dirty.bv_cnt == 0)
1031                         continue;
1032                 VI_LOCK(vp);
1033                 MNT_IUNLOCK(mp);
1034                 if ((error = vget(vp, lockreq, td)) != 0) {
1035                         MNT_ILOCK(mp);
1036                         if (error == ENOENT) {
1037                                 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1038                                 goto loop;
1039                         }
1040                         continue;
1041                 }
1042                 error = VOP_FSYNC(vp, waitfor, td);
1043                 if (error)
1044                         allerror = error;
1045
1046                 /* Do not turn this into vput.  td is not always curthread. */
1047                 VOP_UNLOCK(vp, 0);
1048                 vrele(vp);
1049                 MNT_ILOCK(mp);
1050         }
1051         MNT_IUNLOCK(mp);
1052         return (allerror);
1053 }
1054
1055 int
1056 vfs_stdnosync (mp, waitfor)
1057         struct mount *mp;
1058         int waitfor;
1059 {
1060
1061         return (0);
1062 }
1063
1064 int
1065 vfs_stdvget (mp, ino, flags, vpp)
1066         struct mount *mp;
1067         ino_t ino;
1068         int flags;
1069         struct vnode **vpp;
1070 {
1071
1072         return (EOPNOTSUPP);
1073 }
1074
1075 int
1076 vfs_stdfhtovp (mp, fhp, vpp)
1077         struct mount *mp;
1078         struct fid *fhp;
1079         struct vnode **vpp;
1080 {
1081
1082         return (EOPNOTSUPP);
1083 }
1084
1085 int
1086 vfs_stdinit (vfsp)
1087         struct vfsconf *vfsp;
1088 {
1089
1090         return (0);
1091 }
1092
1093 int
1094 vfs_stduninit (vfsp)
1095         struct vfsconf *vfsp;
1096 {
1097
1098         return(0);
1099 }
1100
1101 int
1102 vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, attrname)
1103         struct mount *mp;
1104         int cmd;
1105         struct vnode *filename_vp;
1106         int attrnamespace;
1107         const char *attrname;
1108 {
1109
1110         if (filename_vp != NULL)
1111                 VOP_UNLOCK(filename_vp, 0);
1112         return (EOPNOTSUPP);
1113 }
1114
1115 int
1116 vfs_stdsysctl(mp, op, req)
1117         struct mount *mp;
1118         fsctlop_t op;
1119         struct sysctl_req *req;
1120 {
1121
1122         return (EOPNOTSUPP);
1123 }
1124
1125 /* end of vfs default ops */