vfs initialization code moved to vfs_init.c
[unix-history] / usr / src / sys / kern / vfs_subr.c
index ae73b2b..5346b35 100644 (file)
@@ -4,22 +4,34 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)vfs_subr.c  7.50 (Berkeley) %G%
+ *     @(#)vfs_subr.c  7.73 (Berkeley) %G%
  */
 
 /*
  * External virtual filesystem routines
  */
 
  */
 
 /*
  * External virtual filesystem routines
  */
 
-#include "param.h"
-#include "mount.h"
-#include "time.h"
-#include "vnode.h"
-#include "specdev.h"
-#include "namei.h"
-#include "ucred.h"
-#include "errno.h"
-#include "malloc.h"
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/mount.h>
+#include <sys/time.h>
+#include <sys/vnode.h>
+#include <sys/stat.h>
+#include <sys/specdev.h>
+#include <sys/namei.h>
+#include <sys/ucred.h>
+#include <sys/buf.h>
+#include <sys/errno.h>
+#include <sys/malloc.h>
+
+enum vtype iftovt_tab[16] = {
+       VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
+       VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
+};
+int    vttoif_tab[9] = {
+       0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
+       S_IFSOCK, S_IFIFO, S_IFMT,
+};
 
 /*
  * Remove a mount point from the list of mounted filesystems.
 
 /*
  * Remove a mount point from the list of mounted filesystems.
@@ -135,83 +147,28 @@ void vattr_null(vap)
 {
 
        vap->va_type = VNON;
 {
 
        vap->va_type = VNON;
+       vap->va_size = vap->va_bytes = VNOVAL;
+#ifdef _NOQUAD
+       vap->va_size_rsv = vap->va_bytes_rsv = VNOVAL;
+#endif
        vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid =
        vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid =
-               vap->va_fsid = vap->va_fileid = vap->va_size =
-               vap->va_size_rsv = vap->va_blocksize = vap->va_rdev =
-               vap->va_bytes = vap->va_bytes_rsv =
+               vap->va_fsid = vap->va_fileid =
+               vap->va_blocksize = vap->va_rdev =
                vap->va_atime.tv_sec = vap->va_atime.tv_usec =
                vap->va_mtime.tv_sec = vap->va_mtime.tv_usec =
                vap->va_ctime.tv_sec = vap->va_ctime.tv_usec =
                vap->va_flags = vap->va_gen = VNOVAL;
 }
 
                vap->va_atime.tv_sec = vap->va_atime.tv_usec =
                vap->va_mtime.tv_sec = vap->va_mtime.tv_usec =
                vap->va_ctime.tv_sec = vap->va_ctime.tv_usec =
                vap->va_flags = vap->va_gen = VNOVAL;
 }
 
-/*
- * Initialize a nameidata structure
- */
-ndinit(ndp)
-       register struct nameidata *ndp;
-{
-
-       bzero((caddr_t)ndp, sizeof(struct nameidata));
-       ndp->ni_iov = &ndp->ni_nd.nd_iovec;
-       ndp->ni_iovcnt = 1;
-       ndp->ni_base = (caddr_t)&ndp->ni_dent;
-       ndp->ni_rw = UIO_WRITE;
-       ndp->ni_uioseg = UIO_SYSSPACE;
-}
-
-/*
- * Duplicate a nameidata structure
- */
-nddup(ndp, newndp)
-       register struct nameidata *ndp, *newndp;
-{
-
-       ndinit(newndp);
-       newndp->ni_cred = ndp->ni_cred;
-       crhold(newndp->ni_cred);
-}
-
-/*
- * Release a nameidata structure
- */
-ndrele(ndp)
-       register struct nameidata *ndp;
-{
-
-       crfree(ndp->ni_cred);
-}
-
 /*
  * Routines having to do with the management of the vnode table.
  */
 /*
  * Routines having to do with the management of the vnode table.
  */
-struct vnode *vfreeh, **vfreet;
-extern struct vnodeops dead_vnodeops, spec_vnodeops;
+extern struct vnode *vfreeh, **vfreet;
+extern struct vnodeops dead_vnodeops;
+extern struct vnodeops spec_vnodeops;
 extern void vclean();
 long numvnodes;
 extern void vclean();
 long numvnodes;
-struct vattr va_null;
-
-/*
- * Initialize the vnode structures and initialize each file system type.
- */
-vfsinit()
-{
-       struct vfsops **vfsp;
-
-       /*
-        * Initialize the vnode name cache
-        */
-       nchinit();
-       /*
-        * Initialize each file system type.
-        */
-       vattr_null(&va_null);
-       for (vfsp = &vfssw[0]; vfsp <= &vfssw[MOUNT_MAXTYPE]; vfsp++) {
-               if (*vfsp == NULL)
-                       continue;
-               (*(*vfsp)->vfs_init)();
-       }
-}
+extern struct vattr va_null;
 
 /*
  * Return the next vnode from the free list.
 
 /*
  * Return the next vnode from the free list.
@@ -244,8 +201,11 @@ getnewvnode(tag, mp, vops, vpp)
                vfreeh = vq;
                vp->v_freef = NULL;
                vp->v_freeb = NULL;
                vfreeh = vq;
                vp->v_freef = NULL;
                vp->v_freeb = NULL;
+               vp->v_lease = NULL;
                if (vp->v_type != VBAD)
                        vgone(vp);
                if (vp->v_type != VBAD)
                        vgone(vp);
+               if (vp->v_data)
+                       panic("cleaned vnode isn't");
                vp->v_flag = 0;
                vp->v_lastr = 0;
                vp->v_socket = 0;
                vp->v_flag = 0;
                vp->v_lastr = 0;
                vp->v_socket = 0;
@@ -267,7 +227,7 @@ insmntque(vp, mp)
        register struct vnode *vp;
        register struct mount *mp;
 {
        register struct vnode *vp;
        register struct mount *mp;
 {
-       struct vnode *vq;
+       register struct vnode *vq;
 
        /*
         * Delete from old mount point vnode list, if on one.
 
        /*
         * Delete from old mount point vnode list, if on one.
@@ -286,18 +246,276 @@ insmntque(vp, mp)
                vp->v_mountb = NULL;
                return;
        }
                vp->v_mountb = NULL;
                return;
        }
-       if (mp->mnt_mounth) {
-               vp->v_mountf = mp->mnt_mounth;
-               vp->v_mountb = &mp->mnt_mounth;
-               mp->mnt_mounth->v_mountb = &vp->v_mountf;
-               mp->mnt_mounth = vp;
-       } else {
-               mp->mnt_mounth = vp;
-               vp->v_mountb = &mp->mnt_mounth;
-               vp->v_mountf = NULL;
+       if (vq = mp->mnt_mounth)
+               vq->v_mountb = &vp->v_mountf;
+       vp->v_mountf = vq;
+       vp->v_mountb = &mp->mnt_mounth;
+       mp->mnt_mounth = vp;
+}
+
+/*
+ * Make sure all write-behind blocks associated
+ * with mount point are flushed out (from sync).
+ */
+mntflushbuf(mountp, flags)
+       struct mount *mountp;
+       int flags;
+{
+       register struct vnode *vp;
+
+       if ((mountp->mnt_flag & MNT_MPBUSY) == 0)
+               panic("mntflushbuf: not busy");
+loop:
+       for (vp = mountp->mnt_mounth; vp; vp = vp->v_mountf) {
+               if (VOP_ISLOCKED(vp))
+                       continue;
+               if (vget(vp))
+                       goto loop;
+               vflushbuf(vp, flags);
+               vput(vp);
+               if (vp->v_mount != mountp)
+                       goto loop;
+       }
+}
+
+/*
+ * Flush all dirty buffers associated with a vnode.
+ */
+vflushbuf(vp, flags)
+       register struct vnode *vp;
+       int flags;
+{
+       register struct buf *bp;
+       struct buf *nbp;
+       int s;
+
+loop:
+       s = splbio();
+       for (bp = vp->v_dirtyblkhd; bp; bp = nbp) {
+               nbp = bp->b_blockf;
+               if ((bp->b_flags & B_BUSY))
+                       continue;
+               if ((bp->b_flags & B_DELWRI) == 0)
+                       panic("vflushbuf: not dirty");
+               bremfree(bp);
+               bp->b_flags |= B_BUSY;
+               splx(s);
+               /*
+                * Wait for I/O associated with indirect blocks to complete,
+                * since there is no way to quickly wait for them below.
+                * NB: This is really specific to ufs, but is done here
+                * as it is easier and quicker.
+                */
+               if (bp->b_vp == vp || (flags & B_SYNC) == 0)
+                       (void) bawrite(bp);
+               else
+                       (void) bwrite(bp);
+               goto loop;
+       }
+       splx(s);
+       if ((flags & B_SYNC) == 0)
+               return;
+       s = splbio();
+       while (vp->v_numoutput) {
+               vp->v_flag |= VBWAIT;
+               sleep((caddr_t)&vp->v_numoutput, PRIBIO + 1);
+       }
+       splx(s);
+       if (vp->v_dirtyblkhd) {
+               vprint("vflushbuf: dirty", vp);
+               goto loop;
        }
 }
 
        }
 }
 
+/*
+ * Update outstanding I/O count and do wakeup if requested.
+ */
+vwakeup(bp)
+       register struct buf *bp;
+{
+       register struct vnode *vp;
+
+       bp->b_dirtyoff = bp->b_dirtyend = 0;
+       if (vp = bp->b_vp) {
+               vp->v_numoutput--;
+               if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
+                       if (vp->v_numoutput < 0)
+                               panic("vwakeup: neg numoutput");
+                       vp->v_flag &= ~VBWAIT;
+                       wakeup((caddr_t)&vp->v_numoutput);
+               }
+       }
+}
+
+/*
+ * Invalidate in core blocks belonging to closed or umounted filesystem
+ *
+ * Go through the list of vnodes associated with the file system;
+ * for each vnode invalidate any buffers that it holds. Normally
+ * this routine is preceeded by a bflush call, so that on a quiescent
+ * filesystem there will be no dirty buffers when we are done. Binval
+ * returns the count of dirty buffers when it is finished.
+ */
+mntinvalbuf(mountp)
+       struct mount *mountp;
+{
+       register struct vnode *vp;
+       int dirty = 0;
+
+       if ((mountp->mnt_flag & MNT_MPBUSY) == 0)
+               panic("mntinvalbuf: not busy");
+loop:
+       for (vp = mountp->mnt_mounth; vp; vp = vp->v_mountf) {
+               if (vget(vp))
+                       goto loop;
+               dirty += vinvalbuf(vp, 1);
+               vput(vp);
+               if (vp->v_mount != mountp)
+                       goto loop;
+       }
+       return (dirty);
+}
+
+/*
+ * Flush out and invalidate all buffers associated with a vnode.
+ * Called with the underlying object locked.
+ */
+vinvalbuf(vp, save)
+       register struct vnode *vp;
+       int save;
+{
+       register struct buf *bp;
+       struct buf *nbp, *blist;
+       int s, dirty = 0;
+
+       for (;;) {
+               if (blist = vp->v_dirtyblkhd)
+                       /* void */;
+               else if (blist = vp->v_cleanblkhd)
+                       /* void */;
+               else
+                       break;
+               for (bp = blist; bp; bp = nbp) {
+                       nbp = bp->b_blockf;
+                       s = splbio();
+                       if (bp->b_flags & B_BUSY) {
+                               bp->b_flags |= B_WANTED;
+                               sleep((caddr_t)bp, PRIBIO + 1);
+                               splx(s);
+                               break;
+                       }
+                       bremfree(bp);
+                       bp->b_flags |= B_BUSY;
+                       splx(s);
+                       if (save && (bp->b_flags & B_DELWRI)) {
+                               dirty++;
+                               (void) VOP_BWRITE(bp);
+                               break;
+                       }
+                       if (bp->b_vp != vp)
+                               reassignbuf(bp, bp->b_vp);
+                       else
+                               bp->b_flags |= B_INVAL;
+                       brelse(bp);
+               }
+       }
+       if (vp->v_dirtyblkhd || vp->v_cleanblkhd)
+               panic("vinvalbuf: flush failed");
+       return (dirty);
+}
+
+/*
+ * Associate a buffer with a vnode.
+ */
+bgetvp(vp, bp)
+       register struct vnode *vp;
+       register struct buf *bp;
+{
+       register struct vnode *vq;
+       register struct buf *bq;
+
+       if (bp->b_vp)
+               panic("bgetvp: not free");
+       VHOLD(vp);
+       bp->b_vp = vp;
+       if (vp->v_type == VBLK || vp->v_type == VCHR)
+               bp->b_dev = vp->v_rdev;
+       else
+               bp->b_dev = NODEV;
+       /*
+        * Insert onto list for new vnode.
+        */
+       if (bq = vp->v_cleanblkhd)
+               bq->b_blockb = &bp->b_blockf;
+       bp->b_blockf = bq;
+       bp->b_blockb = &vp->v_cleanblkhd;
+       vp->v_cleanblkhd = bp;
+}
+
+/*
+ * Disassociate a buffer from a vnode.
+ */
+brelvp(bp)
+       register struct buf *bp;
+{
+       struct buf *bq;
+       struct vnode *vp;
+
+       if (bp->b_vp == (struct vnode *) 0)
+               panic("brelvp: NULL");
+       /*
+        * Delete from old vnode list, if on one.
+        */
+       if (bp->b_blockb) {
+               if (bq = bp->b_blockf)
+                       bq->b_blockb = bp->b_blockb;
+               *bp->b_blockb = bq;
+               bp->b_blockf = NULL;
+               bp->b_blockb = NULL;
+       }
+       vp = bp->b_vp;
+       bp->b_vp = (struct vnode *) 0;
+       HOLDRELE(vp);
+}
+
+/*
+ * Reassign a buffer from one vnode to another.
+ * Used to assign file specific control information
+ * (indirect blocks) to the vnode to which they belong.
+ */
+reassignbuf(bp, newvp)
+       register struct buf *bp;
+       register struct vnode *newvp;
+{
+       register struct buf *bq, **listheadp;
+
+       if (newvp == NULL) {
+               printf("reassignbuf: NULL");
+               return;
+       }
+       /*
+        * Delete from old vnode list, if on one.
+        */
+       if (bp->b_blockb) {
+               if (bq = bp->b_blockf)
+                       bq->b_blockb = bp->b_blockb;
+               *bp->b_blockb = bq;
+       }
+       /*
+        * If dirty, put on list of dirty buffers;
+        * otherwise insert onto list of clean buffers.
+        */
+       if (bp->b_flags & B_DELWRI)
+               listheadp = &newvp->v_dirtyblkhd;
+       else
+               listheadp = &newvp->v_cleanblkhd;
+       if (bq = *listheadp)
+               bq->b_blockb = &bp->b_blockf;
+       bp->b_blockf = bq;
+       bp->b_blockb = listheadp;
+       *listheadp = bp;
+}
+
 /*
  * Create a vnode for a block device.
  * Used for root filesystem, argdev, and swap areas.
 /*
  * Create a vnode for a block device.
  * Used for root filesystem, argdev, and swap areas.
@@ -311,6 +529,8 @@ bdevvp(dev, vpp)
        struct vnode *nvp;
        int error;
 
        struct vnode *nvp;
        int error;
 
+       if (dev == NODEV)
+               return (0);
        error = getnewvnode(VT_NON, (struct mount *)0, &spec_vnodeops, &nvp);
        if (error) {
                *vpp = 0;
        error = getnewvnode(VT_NON, (struct mount *)0, &spec_vnodeops, &nvp);
        if (error) {
                *vpp = 0;
@@ -418,6 +638,8 @@ vget(vp)
        return (0);
 }
 
        return (0);
 }
 
+int bug_refs = 0;
+
 /*
  * Vnode reference, just increment the count
  */
 /*
  * Vnode reference, just increment the count
  */
@@ -426,6 +648,10 @@ void vref(vp)
 {
 
        vp->v_usecount++;
 {
 
        vp->v_usecount++;
+       if (vp->v_type != VBLK && curproc)
+               curproc->p_spare[0]++;
+       if (bug_refs)
+               vprint("vref: ");
 }
 
 /*
 }
 
 /*
@@ -434,6 +660,7 @@ void vref(vp)
 void vput(vp)
        register struct vnode *vp;
 {
 void vput(vp)
        register struct vnode *vp;
 {
+
        VOP_UNLOCK(vp);
        vrele(vp);
 }
        VOP_UNLOCK(vp);
        vrele(vp);
 }
@@ -445,14 +672,25 @@ void vput(vp)
 void vrele(vp)
        register struct vnode *vp;
 {
 void vrele(vp)
        register struct vnode *vp;
 {
+       struct proc *p = curproc;               /* XXX */
 
 
+#ifdef DIAGNOSTIC
        if (vp == NULL)
                panic("vrele: null vp");
        if (vp == NULL)
                panic("vrele: null vp");
+#endif
        vp->v_usecount--;
        vp->v_usecount--;
-       if (vp->v_usecount < 0)
-               vprint("vrele: bad ref count", vp);
+       if (vp->v_type != VBLK && curproc)
+               curproc->p_spare[0]--;
+       if (bug_refs)
+               vprint("vref: ");
        if (vp->v_usecount > 0)
                return;
        if (vp->v_usecount > 0)
                return;
+#ifdef DIAGNOSTIC
+       if (vp->v_usecount != 0 || vp->v_writecount != 0) {
+               vprint("vrele: bad ref count", vp);
+               panic("vrele: ref cnt");
+       }
+#endif
        if (vfreeh == NULLVP) {
                /*
                 * insert into empty list
        if (vfreeh == NULLVP) {
                /*
                 * insert into empty list
@@ -468,13 +706,13 @@ void vrele(vp)
        }
        vp->v_freef = NULL;
        vfreet = &vp->v_freef;
        }
        vp->v_freef = NULL;
        vfreet = &vp->v_freef;
-       VOP_INACTIVE(vp);
+       VOP_INACTIVE(vp, p);
 }
 
 /*
  * Page or buffer structure gets a reference.
  */
 }
 
 /*
  * Page or buffer structure gets a reference.
  */
-vhold(vp)
+void vhold(vp)
        register struct vnode *vp;
 {
 
        register struct vnode *vp;
 {
 
@@ -484,7 +722,7 @@ vhold(vp)
 /*
  * Page or buffer structure frees a reference.
  */
 /*
  * Page or buffer structure frees a reference.
  */
-holdrele(vp)
+void holdrele(vp)
        register struct vnode *vp;
 {
 
        register struct vnode *vp;
 {
 
@@ -568,6 +806,7 @@ void vclean(vp, flags)
 {
        struct vnodeops *origops;
        int active;
 {
        struct vnodeops *origops;
        int active;
+       struct proc *p = curproc;       /* XXX */
 
        /*
         * Check to see if the vnode is in use.
 
        /*
         * Check to see if the vnode is in use.
@@ -605,16 +844,16 @@ void vclean(vp, flags)
         * If purging an active vnode, it must be unlocked, closed,
         * and deactivated before being reclaimed.
         */
         * If purging an active vnode, it must be unlocked, closed,
         * and deactivated before being reclaimed.
         */
-       (*(origops->vn_unlock))(vp);
+       (*(origops->vop_unlock))(vp);
        if (active) {
                if (flags & DOCLOSE)
        if (active) {
                if (flags & DOCLOSE)
-                       (*(origops->vn_close))(vp, 0, NOCRED);
-               (*(origops->vn_inactive))(vp);
+                       (*(origops->vop_close))(vp, IO_NDELAY, NOCRED, p);
+               (*(origops->vop_inactive))(vp, p);
        }
        /*
         * Reclaim the vnode.
         */
        }
        /*
         * Reclaim the vnode.
         */
-       if ((*(origops->vn_reclaim))(vp))
+       if ((*(origops->vop_reclaim))(vp))
                panic("vclean: cannot reclaim");
        if (active)
                vrele(vp);
                panic("vclean: cannot reclaim");
        if (active)
                vrele(vp);
@@ -680,7 +919,6 @@ void vgone(vp)
 {
        register struct vnode *vq;
        struct vnode *vx;
 {
        register struct vnode *vq;
        struct vnode *vx;
-       long count;
 
        /*
         * If a vgone (or vclean) is already in progress,
 
        /*
         * If a vgone (or vclean) is already in progress,
@@ -704,6 +942,7 @@ void vgone(vp)
                *vp->v_mountb = vq;
                vp->v_mountf = NULL;
                vp->v_mountb = NULL;
                *vp->v_mountb = vq;
                vp->v_mountf = NULL;
                vp->v_mountb = NULL;
+               vp->v_mount = NULL;
        }
        /*
         * If special device, remove it from special device alias list.
        }
        /*
         * If special device, remove it from special device alias list.
@@ -722,17 +961,18 @@ void vgone(vp)
                                panic("missing bdev");
                }
                if (vp->v_flag & VALIASED) {
                                panic("missing bdev");
                }
                if (vp->v_flag & VALIASED) {
-                       count = 0;
+                       vx = NULL;
                        for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
                                if (vq->v_rdev != vp->v_rdev ||
                                    vq->v_type != vp->v_type)
                                        continue;
                        for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
                                if (vq->v_rdev != vp->v_rdev ||
                                    vq->v_type != vp->v_type)
                                        continue;
-                               count++;
+                               if (vx)
+                                       break;
                                vx = vq;
                        }
                                vx = vq;
                        }
-                       if (count == 0)
+                       if (vx == NULL)
                                panic("missing alias");
                                panic("missing alias");
-                       if (count == 1)
+                       if (vq == NULL)
                                vx->v_flag &= ~VALIASED;
                        vp->v_flag &= ~VALIASED;
                }
                                vx->v_flag &= ~VALIASED;
                        vp->v_flag &= ~VALIASED;
                }
@@ -816,8 +1056,9 @@ vprint(label, vp)
 
        if (label != NULL)
                printf("%s: ", label);
 
        if (label != NULL)
                printf("%s: ", label);
-       printf("type %s, usecount %d, refcount %d,", typename[vp->v_type],
-               vp->v_usecount, vp->v_holdcnt);
+       printf("type %s, usecount %d, writecount %d, refcount %d,",
+               typename[vp->v_type], vp->v_usecount, vp->v_writecount,
+               vp->v_holdcnt);
        buf[0] = '\0';
        if (vp->v_flag & VROOT)
                strcat(buf, "|VROOT");
        buf[0] = '\0';
        if (vp->v_flag & VROOT)
                strcat(buf, "|VROOT");
@@ -839,6 +1080,27 @@ vprint(label, vp)
        VOP_PRINT(vp);
 }
 
        VOP_PRINT(vp);
 }
 
+#ifdef DEBUG
+/*
+ * List all of the locked vnodes in the system.
+ * Called when debugging the kernel.
+ */
+printlockedvnodes()
+{
+       register struct mount *mp;
+       register struct vnode *vp;
+
+       printf("Locked vnodes\n");
+       mp = rootfs;
+       do {
+               for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf)
+                       if (VOP_ISLOCKED(vp))
+                               vprint((char *)0, vp);
+               mp = mp->mnt_next;
+       } while (mp != rootfs);
+}
+#endif
+
 int kinfo_vdebug = 1;
 int kinfo_vgetfailed;
 #define KINFO_VNODESLOP        10
 int kinfo_vdebug = 1;
 int kinfo_vgetfailed;
 #define KINFO_VNODESLOP        10