2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed
6 * to Berkeley by John Heidemann of the UCLA Ficus project.
8 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/limits.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/unistd.h>
50 #include <sys/vnode.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_extern.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_pager.h>
60 #include <vm/vnode_pager.h>
62 static int vop_nolookup(struct vop_lookup_args *);
63 static int vop_nostrategy(struct vop_strategy_args *);
66 * This vnode table stores what we want to do if the filesystem doesn't
67 * implement a particular VOP.
69 * If there is no specific entry here, we will return EOPNOTSUPP.
73 vop_t **default_vnodeop_p;
74 static struct vnodeopv_entry_desc default_vnodeop_entries[] = {
75 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
76 { &vop_advlock_desc, (vop_t *) vop_einval },
77 { &vop_bmap_desc, (vop_t *) vop_stdbmap },
78 { &vop_close_desc, (vop_t *) vop_null },
79 { &vop_createvobject_desc, (vop_t *) vop_stdcreatevobject },
80 { &vop_destroyvobject_desc, (vop_t *) vop_stddestroyvobject },
81 { &vop_fsync_desc, (vop_t *) vop_null },
82 { &vop_getpages_desc, (vop_t *) vop_stdgetpages },
83 { &vop_getvobject_desc, (vop_t *) vop_stdgetvobject },
84 { &vop_inactive_desc, (vop_t *) vop_stdinactive },
85 { &vop_ioctl_desc, (vop_t *) vop_enotty },
86 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
87 { &vop_lease_desc, (vop_t *) vop_null },
88 { &vop_lock_desc, (vop_t *) vop_stdlock },
89 { &vop_lookup_desc, (vop_t *) vop_nolookup },
90 { &vop_open_desc, (vop_t *) vop_null },
91 { &vop_pathconf_desc, (vop_t *) vop_einval },
92 { &vop_poll_desc, (vop_t *) vop_nopoll },
93 { &vop_putpages_desc, (vop_t *) vop_stdputpages },
94 { &vop_readlink_desc, (vop_t *) vop_einval },
95 { &vop_revoke_desc, (vop_t *) vop_revoke },
96 { &vop_specstrategy_desc, (vop_t *) vop_panic },
97 { &vop_strategy_desc, (vop_t *) vop_nostrategy },
98 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
102 static struct vnodeopv_desc default_vnodeop_opv_desc =
103 { &default_vnodeop_p, default_vnodeop_entries };
105 VNODEOP_SET(default_vnodeop_opv_desc);
108 * Series of placeholder functions for various error returns for
113 vop_eopnotsupp(struct vop_generic_args *ap)
116 printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name);
123 vop_ebadf(struct vop_generic_args *ap)
130 vop_enotty(struct vop_generic_args *ap)
137 vop_einval(struct vop_generic_args *ap)
144 vop_null(struct vop_generic_args *ap)
151 * Used to make a defined VOP fall back to the default VOP.
154 vop_defaultop(struct vop_generic_args *ap)
157 return (VOCALL(default_vnodeop_p, ap->a_desc->vdesc_offset, ap));
161 * Helper function to panic on some bad VOPs in some filesystems.
164 vop_panic(struct vop_generic_args *ap)
167 panic("filesystem goof: vop_panic[%s]", ap->a_desc->vdesc_name);
171 * vop_std<something> and vop_no<something> are default functions for use by
172 * filesystems that need the "default reasonable" implementation for a
173 * particular operation.
175 * The documentation for the operations they implement exists (if it exists)
176 * in the VOP_<SOMETHING>(9) manpage (all uppercase).
180 * Default vop for filesystems that do not support name lookup
184 struct vop_lookup_args /* {
186 struct vnode **a_vpp;
187 struct componentname *a_cnp;
198 * Strategy routine for VFS devices that have none.
200 * BIO_ERROR and B_INVAL must be cleared prior to calling any strategy
201 * routine. Typically this is done for a BIO_READ strategy call.
202 * Typically B_INVAL is assumed to already be clear prior to a write
203 * and should not be cleared manually unless you just made the buffer
204 * invalid. BIO_ERROR should be cleared either way.
208 vop_nostrategy (struct vop_strategy_args *ap)
210 KASSERT(ap->a_vp == ap->a_bp->b_vp, ("%s(%p != %p)",
211 __func__, ap->a_vp, ap->a_bp->b_vp));
212 printf("No strategy for buffer at %p\n", ap->a_bp);
213 vprint("vnode", ap->a_vp);
214 vprint("device vnode", ap->a_bp->b_vp);
215 ap->a_bp->b_ioflags |= BIO_ERROR;
216 ap->a_bp->b_error = EOPNOTSUPP;
224 * Standard implementation of POSIX pathconf, to get information about limits
226 * Override per filesystem for the case where the filesystem has smaller
231 struct vop_pathconf_args /* {
238 switch (ap->a_name) {
240 *ap->a_retval = LINK_MAX;
243 *ap->a_retval = MAX_CANON;
246 *ap->a_retval = MAX_INPUT;
249 *ap->a_retval = PIPE_BUF;
251 case _PC_CHOWN_RESTRICTED:
255 *ap->a_retval = _POSIX_VDISABLE;
264 * Standard lock, unlock and islocked functions.
268 struct vop_lock_args /* {
274 struct vnode *vp = ap->a_vp;
277 return (lockmgr(vp->v_vnlock, ap->a_flags, VI_MTX(vp), ap->a_td));
279 return (debuglockmgr(vp->v_vnlock, ap->a_flags, VI_MTX(vp),
280 ap->a_td, "vop_stdlock", vp->filename, vp->line));
287 struct vop_unlock_args /* {
293 struct vnode *vp = ap->a_vp;
295 return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE, VI_MTX(vp),
302 struct vop_islocked_args /* {
308 return (lockstatus(ap->a_vp->v_vnlock, ap->a_td));
311 /* Mark the vnode inactive */
314 struct vop_inactive_args /* {
320 VOP_UNLOCK(ap->a_vp, 0, ap->a_td);
325 * Return true for select/poll.
329 struct vop_poll_args /* {
332 struct ucred *a_cred;
337 * Return true for read/write. If the user asked for something
338 * special, return POLLNVAL, so that clients have a way of
339 * determining reliably whether or not the extended
340 * functionality is present without hard-coding knowledge
341 * of specific filesystem implementations.
342 * Stay in sync with kern_conf.c::no_poll().
344 if (ap->a_events & ~POLLSTANDARD)
347 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
351 * Implement poll for local filesystems that support it.
355 struct vop_poll_args /* {
358 struct ucred *a_cred;
362 if (ap->a_events & ~POLLSTANDARD)
363 return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events));
364 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
368 * Return our mount point, as we will take charge of the writes.
371 vop_stdgetwritemount(ap)
372 struct vop_getwritemount_args /* {
374 struct mount **a_mpp;
378 *(ap->a_mpp) = ap->a_vp->v_mount;
382 /* Create the VM system backing object for this vnode */
384 vop_stdcreatevobject(ap)
385 struct vop_createvobject_args /* {
391 struct vnode *vp = ap->a_vp;
392 struct ucred *cred = ap->a_cred;
393 struct thread *td = ap->a_td;
400 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
404 if ((object = vp->v_object) == NULL) {
405 if (vp->v_type == VREG || vp->v_type == VDIR) {
406 if ((error = VOP_GETATTR(vp, &vat, cred, td)) != 0)
408 object = vnode_pager_alloc(vp, vat.va_size, 0, 0);
409 } else if (vn_isdisk(vp, NULL)) {
411 * This simply allocates the biggest object possible
412 * for a disk vnode. This should be fixed, but doesn't
413 * cause any problems (yet).
415 object = vnode_pager_alloc(vp, IDX_TO_OFF(INT_MAX), 0, 0);
420 * Dereference the reference we just created. This assumes
421 * that the object is associated with the vp.
423 VM_OBJECT_LOCK(object);
425 VM_OBJECT_UNLOCK(object);
428 VM_OBJECT_LOCK(object);
429 if (object->flags & OBJ_DEAD) {
430 VOP_UNLOCK(vp, 0, td);
431 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM,
433 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
436 VM_OBJECT_UNLOCK(object);
439 KASSERT(vp->v_object != NULL, ("vfs_object_create: NULL object"));
440 vp->v_vflag |= VV_OBJBUF;
446 /* Destroy the VM system object associated with this vnode */
448 vop_stddestroyvobject(ap)
449 struct vop_destroyvobject_args /* {
453 struct vnode *vp = ap->a_vp;
454 vm_object_t obj = vp->v_object;
461 if (obj->ref_count == 0) {
463 * vclean() may be called twice. The first time
464 * removes the primary reference to the object,
465 * the second time goes one further and is a
466 * special-case to terminate the object.
468 * don't double-terminate the object
470 if ((obj->flags & OBJ_DEAD) == 0)
471 vm_object_terminate(obj);
473 VM_OBJECT_UNLOCK(obj);
476 * Woe to the process that tries to page now :-).
478 vm_pager_deallocate(obj);
479 VM_OBJECT_UNLOCK(obj);
485 * Return the underlying VM object. This routine may be called with or
486 * without the vnode interlock held. If called without, the returned
487 * object is not guarenteed to be valid. The syncer typically gets the
488 * object without holding the interlock in order to quickly test whether
489 * it might be dirty before going heavy-weight. vm_object's use zalloc
490 * and thus stable-storage, so this is safe.
493 vop_stdgetvobject(ap)
494 struct vop_getvobject_args /* {
496 struct vm_object **objpp;
499 struct vnode *vp = ap->a_vp;
500 struct vm_object **objpp = ap->a_objpp;
503 *objpp = vp->v_object;
504 return (vp->v_object ? 0 : EINVAL);
507 /* XXX Needs good comment and VOP_BMAP(9) manpage */
510 struct vop_bmap_args /* {
513 struct vnode **a_vpp;
520 if (ap->a_vpp != NULL)
521 *ap->a_vpp = ap->a_vp;
522 if (ap->a_bnp != NULL)
523 *ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize);
524 if (ap->a_runp != NULL)
526 if (ap->a_runb != NULL)
533 struct vop_fsync_args /* {
535 struct ucred *a_cred;
540 struct vnode *vp = ap->a_vp;
544 int maxretry = 100; /* large, arbitrarily chosen */
549 * MARK/SCAN initialization to avoid infinite loops.
552 TAILQ_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
553 bp->b_vflags &= ~BV_SCANNED;
559 * Flush all dirty buffers associated with a block device.
563 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp != NULL; bp = nbp) {
564 nbp = TAILQ_NEXT(bp, b_vnbufs);
565 if ((bp->b_vflags & BV_SCANNED) != 0)
567 bp->b_vflags |= BV_SCANNED;
568 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
571 if ((bp->b_flags & B_DELWRI) == 0)
572 panic("fsync: not dirty");
573 if ((vp->v_vflag & VV_OBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
586 * If synchronous the caller expects us to completely resolve all
587 * dirty buffers in the system. Wait for in-progress I/O to
588 * complete (which could include background bitmap writes), then
589 * retry if dirty blocks still exist.
591 if (ap->a_waitfor == MNT_WAIT) {
592 while (vp->v_numoutput) {
593 vp->v_iflag |= VI_BWAIT;
594 msleep((caddr_t)&vp->v_numoutput, VI_MTX(vp),
595 PRIBIO + 1, "fsync", 0);
597 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
599 * If we are unable to write any of these buffers
600 * then we fail now rather than trying endlessly
603 TAILQ_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs)
604 if ((error = bp->b_error) == 0)
606 if (error == 0 && --maxretry >= 0) {
610 vprint("fsync: giving up on dirty", vp);
620 /* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */
623 struct vop_getpages_args /* {
628 vm_ooffset_t a_offset;
632 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
633 ap->a_count, ap->a_reqpage);
636 /* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */
639 struct vop_putpages_args /* {
645 vm_ooffset_t a_offset;
649 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
650 ap->a_sync, ap->a_rtvals);
655 * used to fill the vfs function table to get reasonable default return values.
658 vfs_stdroot (mp, vpp, td)
668 vfs_stdstatfs (mp, sbp, td)
678 vfs_stdvptofh (vp, fhp)
687 vfs_stdstart (mp, flags, td)
697 vfs_stdquotactl (mp, cmds, uid, arg, td)
709 vfs_stdsync(mp, waitfor, cred, td)
715 struct vnode *vp, *nvp;
716 int error, lockreq, allerror = 0;
718 lockreq = LK_EXCLUSIVE | LK_INTERLOCK;
719 if (waitfor != MNT_WAIT)
720 lockreq |= LK_NOWAIT;
722 * Force stale buffer cache information to be flushed.
726 MNT_VNODE_FOREACH(vp, mp, nvp) {
729 if (TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
735 if ((error = vget(vp, lockreq, td)) != 0) {
741 error = VOP_FSYNC(vp, cred, waitfor, td);
745 VOP_UNLOCK(vp, 0, td);
754 vfs_stdnosync (mp, waitfor, cred, td)
765 vfs_stdvget (mp, ino, flags, vpp)
776 vfs_stdfhtovp (mp, fhp, vpp)
787 struct vfsconf *vfsp;
795 struct vfsconf *vfsp;
802 vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, attrname, td)
805 struct vnode *filename_vp;
807 const char *attrname;
811 if (filename_vp != NULL)
812 VOP_UNLOCK(filename_vp, 0, td);
817 vfs_stdsysctl(mp, op, req)
820 struct sysctl_req *req;
826 /* end of vfs default ops */