BSD 4_4 release
[unix-history] / usr / src / sys / kern / vfs_subr.c
index bc25070..d11e8d3 100644 (file)
@@ -1,27 +1,74 @@
 /*
 /*
- * Copyright (c) 1989 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1989, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *     @(#)vfs_subr.c  7.60 (Berkeley) %G%
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)vfs_subr.c  8.1 (Berkeley) 6/10/93
  */
 
 /*
  * External virtual filesystem routines
  */
 
  */
 
 /*
  * External virtual filesystem routines
  */
 
-#include "param.h"
-#include "proc.h"
-#include "mount.h"
-#include "time.h"
-#include "vnode.h"
-#include "specdev.h"
-#include "namei.h"
-#include "ucred.h"
-#include "buf.h"
-#include "errno.h"
-#include "malloc.h"
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/proc.h>
+#include <sys/mount.h>
+#include <sys/time.h>
+#include <sys/vnode.h>
+#include <sys/stat.h>
+#include <sys/namei.h>
+#include <sys/ucred.h>
+#include <sys/buf.h>
+#include <sys/errno.h>
+#include <sys/malloc.h>
+
+#include <vm/vm.h>
+#include <sys/sysctl.h>
+
+#include <miscfs/specfs/specdev.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,
+};
+
+/*
+ * Insq/Remq for the vnode usage lists.
+ */
+#define        bufinsvn(bp, dp)        list_enter_head(dp, bp, struct buf *, b_vnbufs)
+#define        bufremvn(bp)            list_remove(bp, struct buf *, b_vnbufs)
 
 /*
  * Remove a mount point from the list of mounted filesystems.
 
 /*
  * Remove a mount point from the list of mounted filesystems.
@@ -129,6 +176,33 @@ getvfs(fsid)
        return ((struct mount *)0);
 }
 
        return ((struct mount *)0);
 }
 
+/*
+ * Get a new unique fsid
+ */
+void
+getnewfsid(mp, mtype)
+       struct mount *mp;
+       int mtype;
+{
+static u_short xxxfs_mntid;
+
+       fsid_t tfsid;
+
+       mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + 11, 0);  /* XXX */
+       mp->mnt_stat.f_fsid.val[1] = mtype;
+       if (xxxfs_mntid == 0)
+               ++xxxfs_mntid;
+       tfsid.val[0] = makedev(nblkdev, xxxfs_mntid);
+       tfsid.val[1] = mtype;
+       if (rootfs) {
+               while (getvfs(&tfsid)) {
+                       tfsid.val[0]++;
+                       xxxfs_mntid++;
+               }
+       }
+       mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
+}
+
 /*
  * Set vnode attributes to VNOVAL
  */
 /*
  * Set vnode attributes to VNOVAL
  */
@@ -137,46 +211,26 @@ void vattr_null(vap)
 {
 
        vap->va_type = VNON;
 {
 
        vap->va_type = VNON;
+       vap->va_size = vap->va_bytes = VNOVAL;
        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_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_fsid = vap->va_fileid =
+               vap->va_blocksize = vap->va_rdev =
+               vap->va_atime.ts_sec = vap->va_atime.ts_nsec =
+               vap->va_mtime.ts_sec = vap->va_mtime.ts_nsec =
+               vap->va_ctime.ts_sec = vap->va_ctime.ts_nsec =
                vap->va_flags = vap->va_gen = VNOVAL;
                vap->va_flags = vap->va_gen = VNOVAL;
+       vap->va_vaflags = 0;
 }
 
 /*
  * 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;
+struct vnode *vfreeh, **vfreet = &vfreeh;
+extern int (**dead_vnodeop_p)();
 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;
+extern struct vattr va_null;
 
 
-       /*
-        * 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)();
-       }
-}
 
 /*
  * Return the next vnode from the free list.
 
 /*
  * Return the next vnode from the free list.
@@ -184,12 +238,14 @@ vfsinit()
 getnewvnode(tag, mp, vops, vpp)
        enum vtagtype tag;
        struct mount *mp;
 getnewvnode(tag, mp, vops, vpp)
        enum vtagtype tag;
        struct mount *mp;
-       struct vnodeops *vops;
+       int (**vops)();
        struct vnode **vpp;
 {
        register struct vnode *vp, *vq;
        struct vnode **vpp;
 {
        register struct vnode *vp, *vq;
+       int s;
 
 
-       if (numvnodes < desiredvnodes) {
+       if ((vfreeh == NULL && numvnodes < 2 * desiredvnodes) ||
+           numvnodes < desiredvnodes) {
                vp = (struct vnode *)malloc((u_long)sizeof *vp,
                    M_VNODE, M_WAITOK);
                bzero((char *)vp, sizeof *vp);
                vp = (struct vnode *)malloc((u_long)sizeof *vp,
                    M_VNODE, M_WAITOK);
                bzero((char *)vp, sizeof *vp);
@@ -209,22 +265,35 @@ 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);
+#ifdef DIAGNOSTIC
+               if (vp->v_data)
+                       panic("cleaned vnode isn't");
+               s = splbio();
+               if (vp->v_numoutput)
+                       panic("Clean vnode has pending I/O's");
+               splx(s);
+#endif
                vp->v_flag = 0;
                vp->v_lastr = 0;
                vp->v_flag = 0;
                vp->v_lastr = 0;
+               vp->v_lastw = 0;
+               vp->v_lasta = 0;
+               vp->v_cstart = 0;
+               vp->v_clen = 0;
                vp->v_socket = 0;
        }
                vp->v_socket = 0;
        }
+       vp->v_ralen = 1;
        vp->v_type = VNON;
        cache_purge(vp);
        vp->v_tag = tag;
        vp->v_op = vops;
        insmntque(vp, mp);
        vp->v_type = VNON;
        cache_purge(vp);
        vp->v_tag = tag;
        vp->v_op = vops;
        insmntque(vp, mp);
-       VREF(vp);
+       vp->v_usecount++;
        *vpp = vp;
        return (0);
 }
        *vpp = vp;
        return (0);
 }
-
 /*
  * Move a vnode from one mount queue to another.
  */
 /*
  * Move a vnode from one mount queue to another.
  */
@@ -258,80 +327,6 @@ insmntque(vp, mp)
        mp->mnt_mounth = vp;
 }
 
        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.
  */
 /*
  * Update outstanding I/O count and do wakeup if requested.
  */
@@ -340,9 +335,11 @@ vwakeup(bp)
 {
        register struct vnode *vp;
 
 {
        register struct vnode *vp;
 
-       bp->b_dirtyoff = bp->b_dirtyend = 0;
+       bp->b_flags &= ~B_WRITEINPROG;
        if (vp = bp->b_vp) {
                vp->v_numoutput--;
        if (vp = bp->b_vp) {
                vp->v_numoutput--;
+               if (vp->v_numoutput < 0)
+                       panic("vwakeup: neg numoutput");
                if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
                        if (vp->v_numoutput < 0)
                                panic("vwakeup: neg numoutput");
                if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
                        if (vp->v_numoutput < 0)
                                panic("vwakeup: neg numoutput");
@@ -352,81 +349,74 @@ vwakeup(bp)
        }
 }
 
        }
 }
 
-/*
- * 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.
  */
 /*
  * Flush out and invalidate all buffers associated with a vnode.
  * Called with the underlying object locked.
  */
-vinvalbuf(vp, save)
+int
+vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
        register struct vnode *vp;
        register struct vnode *vp;
-       int save;
+       int flags;
+       struct ucred *cred;
+       struct proc *p;
+       int slpflag, slptimeo;
 {
        register struct buf *bp;
        struct buf *nbp, *blist;
 {
        register struct buf *bp;
        struct buf *nbp, *blist;
-       int s, dirty = 0;
+       int s, error;
 
 
+       if (flags & V_SAVE) {
+               if (error = VOP_FSYNC(vp, cred, MNT_WAIT, p))
+                       return (error);
+               if (vp->v_dirtyblkhd.le_next != NULL)
+                       panic("vinvalbuf: dirty bufs");
+       }
        for (;;) {
        for (;;) {
-               if (blist = vp->v_dirtyblkhd)
-                       /* void */;
-               else if (blist = vp->v_cleanblkhd)
-                       /* void */;
-               else
+               if ((blist = vp->v_cleanblkhd.le_next) && flags & V_SAVEMETA)
+                       while (blist && blist->b_lblkno < 0)
+                               blist = blist->b_vnbufs.qe_next;
+               if (!blist && (blist = vp->v_dirtyblkhd.le_next) && 
+                   (flags & V_SAVEMETA))
+                       while (blist && blist->b_lblkno < 0)
+                               blist = blist->b_vnbufs.qe_next;
+               if (!blist)
                        break;
                        break;
+
                for (bp = blist; bp; bp = nbp) {
                for (bp = blist; bp; bp = nbp) {
-                       nbp = bp->b_blockf;
+                       nbp = bp->b_vnbufs.qe_next;
+                       if (flags & V_SAVEMETA && bp->b_lblkno < 0)
+                               continue;
                        s = splbio();
                        if (bp->b_flags & B_BUSY) {
                                bp->b_flags |= B_WANTED;
                        s = splbio();
                        if (bp->b_flags & B_BUSY) {
                                bp->b_flags |= B_WANTED;
-                               sleep((caddr_t)bp, PRIBIO + 1);
+                               error = tsleep((caddr_t)bp,
+                                       slpflag | (PRIBIO + 1), "vinvalbuf",
+                                       slptimeo);
                                splx(s);
                                splx(s);
+                               if (error)
+                                       return (error);
                                break;
                        }
                        bremfree(bp);
                        bp->b_flags |= B_BUSY;
                        splx(s);
                                break;
                        }
                        bremfree(bp);
                        bp->b_flags |= B_BUSY;
                        splx(s);
-                       if (save && (bp->b_flags & B_DELWRI)) {
-                               dirty++;
-                               (void) bwrite(bp);
+                       /*
+                        * XXX Since there are no node locks for NFS, I believe
+                        * there is a slight chance that a delayed write will
+                        * occur while sleeping just above, so check for it.
+                        */
+                       if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) {
+                               (void) VOP_BWRITE(bp);
                                break;
                        }
                                break;
                        }
-                       if (bp->b_vp != vp)
-                               reassignbuf(bp, bp->b_vp);
-                       else
-                               bp->b_flags |= B_INVAL;
+                       bp->b_flags |= B_INVAL;
                        brelse(bp);
                }
        }
                        brelse(bp);
                }
        }
-       if (vp->v_dirtyblkhd || vp->v_cleanblkhd)
+       if (!(flags & V_SAVEMETA) &&
+           (vp->v_dirtyblkhd.le_next || vp->v_cleanblkhd.le_next))
                panic("vinvalbuf: flush failed");
                panic("vinvalbuf: flush failed");
-       return (dirty);
+       return (0);
 }
 
 /*
 }
 
 /*
@@ -437,7 +427,6 @@ bgetvp(vp, bp)
        register struct buf *bp;
 {
        register struct vnode *vq;
        register struct buf *bp;
 {
        register struct vnode *vq;
-       register struct buf *bq;
 
        if (bp->b_vp)
                panic("bgetvp: not free");
 
        if (bp->b_vp)
                panic("bgetvp: not free");
@@ -450,11 +439,7 @@ bgetvp(vp, bp)
        /*
         * Insert onto list for new vnode.
         */
        /*
         * 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;
+       bufinsvn(bp, &vp->v_cleanblkhd);
 }
 
 /*
 }
 
 /*
@@ -463,7 +448,6 @@ bgetvp(vp, bp)
 brelvp(bp)
        register struct buf *bp;
 {
 brelvp(bp)
        register struct buf *bp;
 {
-       struct buf *bq;
        struct vnode *vp;
 
        if (bp->b_vp == (struct vnode *) 0)
        struct vnode *vp;
 
        if (bp->b_vp == (struct vnode *) 0)
@@ -471,13 +455,8 @@ brelvp(bp)
        /*
         * Delete from old vnode list, if on one.
         */
        /*
         * 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;
-       }
+       if (bp->b_vnbufs.qe_next != NOLIST)
+               bufremvn(bp);
        vp = bp->b_vp;
        bp->b_vp = (struct vnode *) 0;
        HOLDRELE(vp);
        vp = bp->b_vp;
        bp->b_vp = (struct vnode *) 0;
        HOLDRELE(vp);
@@ -492,18 +471,17 @@ reassignbuf(bp, newvp)
        register struct buf *bp;
        register struct vnode *newvp;
 {
        register struct buf *bp;
        register struct vnode *newvp;
 {
-       register struct buf *bq, **listheadp;
+       register struct list_entry *listheadp;
 
 
-       if (newvp == NULL)
-               panic("reassignbuf: NULL");
+       if (newvp == NULL) {
+               printf("reassignbuf: NULL");
+               return;
+       }
        /*
         * Delete from old vnode list, if on one.
         */
        /*
         * 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 (bp->b_vnbufs.qe_next != NOLIST)
+               bufremvn(bp);
        /*
         * If dirty, put on list of dirty buffers;
         * otherwise insert onto list of clean buffers.
        /*
         * If dirty, put on list of dirty buffers;
         * otherwise insert onto list of clean buffers.
@@ -512,11 +490,7 @@ reassignbuf(bp, newvp)
                listheadp = &newvp->v_dirtyblkhd;
        else
                listheadp = &newvp->v_cleanblkhd;
                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;
+       bufinsvn(bp, listheadp);
 }
 
 /*
 }
 
 /*
@@ -534,7 +508,7 @@ bdevvp(dev, vpp)
 
        if (dev == NODEV)
                return (0);
 
        if (dev == NODEV)
                return (0);
-       error = getnewvnode(VT_NON, (struct mount *)0, &spec_vnodeops, &nvp);
+       error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
        if (error) {
                *vpp = 0;
                return (error);
        if (error) {
                *vpp = 0;
                return (error);
@@ -636,7 +610,7 @@ vget(vp)
                vp->v_freef = NULL;
                vp->v_freeb = NULL;
        }
                vp->v_freef = NULL;
                vp->v_freeb = NULL;
        }
-       VREF(vp);
+       vp->v_usecount++;
        VOP_LOCK(vp);
        return (0);
 }
        VOP_LOCK(vp);
        return (0);
 }
@@ -648,6 +622,8 @@ void vref(vp)
        struct vnode *vp;
 {
 
        struct vnode *vp;
 {
 
+       if (vp->v_usecount <= 0)
+               panic("vref used where vget required");
        vp->v_usecount++;
 }
 
        vp->v_usecount++;
 }
 
@@ -657,6 +633,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);
 }
@@ -668,7 +645,6 @@ 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)
 
 #ifdef DIAGNOSTIC
        if (vp == NULL)
@@ -683,28 +659,20 @@ void vrele(vp)
                panic("vrele: ref cnt");
        }
 #endif
                panic("vrele: ref cnt");
        }
 #endif
-       if (vfreeh == NULLVP) {
-               /*
-                * insert into empty list
-                */
-               vfreeh = vp;
-               vp->v_freeb = &vfreeh;
-       } else {
-               /*
-                * insert at tail of list
-                */
-               *vfreet = vp;
-               vp->v_freeb = vfreet;
-       }
+       /*
+        * insert at tail of LRU list
+        */
+       *vfreet = vp;
+       vp->v_freeb = vfreet;
        vp->v_freef = NULL;
        vfreet = &vp->v_freef;
        vp->v_freef = NULL;
        vfreet = &vp->v_freef;
-       VOP_INACTIVE(vp, p);
+       VOP_INACTIVE(vp);
 }
 
 /*
  * 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;
 {
 
@@ -714,7 +682,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;
 {
 
@@ -731,7 +699,8 @@ holdrele(vp)
  * system error). If MNT_FORCE is specified, detach any active vnodes
  * that are found.
  */
  * system error). If MNT_FORCE is specified, detach any active vnodes
  * that are found.
  */
-int busyprt = 0;       /* patch to print out busy vnodes */
+int busyprt = 0;       /* print out busy vnodes */
+struct ctldebug debug1 = { "busyprt", &busyprt };
 
 vflush(mp, skipvp, flags)
        struct mount *mp;
 
 vflush(mp, skipvp, flags)
        struct mount *mp;
@@ -758,6 +727,13 @@ loop:
                 */
                if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM))
                        continue;
                 */
                if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM))
                        continue;
+               /*
+                * If WRITECLOSE is set, only flush out regular file
+                * vnodes open for writing.
+                */
+               if ((flags & WRITECLOSE) &&
+                   (vp->v_writecount == 0 || vp->v_type != VREG))
+                       continue;
                /*
                 * With v_usecount == 0, all we need to do is clear
                 * out the vnode data structures and we are done.
                /*
                 * With v_usecount == 0, all we need to do is clear
                 * out the vnode data structures and we are done.
@@ -767,6 +743,7 @@ loop:
                        continue;
                }
                /*
                        continue;
                }
                /*
+                * If FORCECLOSE is set, forcibly close the vnode.
                 * For block or character devices, revert to an
                 * anonymous device. For all other files, just kill them.
                 */
                 * For block or character devices, revert to an
                 * anonymous device. For all other files, just kill them.
                 */
@@ -775,7 +752,7 @@ loop:
                                vgone(vp);
                        } else {
                                vclean(vp, 0);
                                vgone(vp);
                        } else {
                                vclean(vp, 0);
-                               vp->v_op = &spec_vnodeops;
+                               vp->v_op = spec_vnodeop_p;
                                insmntque(vp, (struct mount *)0);
                        }
                        continue;
                                insmntque(vp, (struct mount *)0);
                        }
                        continue;
@@ -792,13 +769,12 @@ loop:
 /*
  * Disassociate the underlying file system from a vnode.
  */
 /*
  * Disassociate the underlying file system from a vnode.
  */
-void vclean(vp, flags)
+void
+vclean(vp, flags)
        register struct vnode *vp;
        int flags;
 {
        register struct vnode *vp;
        int flags;
 {
-       struct vnodeops *origops;
        int active;
        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.
@@ -808,6 +784,14 @@ void vclean(vp, flags)
         */
        if (active = vp->v_usecount)
                VREF(vp);
         */
        if (active = vp->v_usecount)
                VREF(vp);
+       /*
+        * Even if the count is zero, the VOP_INACTIVE routine may still
+        * have the object locked while it cleans it out. The VOP_LOCK
+        * ensures that the VOP_INACTIVE routine is done with its work.
+        * For active vnodes, it ensures that no other activity can
+        * occur while the underlying object is being cleaned out.
+        */
+       VOP_LOCK(vp);
        /*
         * Prevent the vnode from being recycled or
         * brought into use while we clean it out.
        /*
         * Prevent the vnode from being recycled or
         * brought into use while we clean it out.
@@ -816,42 +800,37 @@ void vclean(vp, flags)
                panic("vclean: deadlock");
        vp->v_flag |= VXLOCK;
        /*
                panic("vclean: deadlock");
        vp->v_flag |= VXLOCK;
        /*
-        * Even if the count is zero, the VOP_INACTIVE routine may still
-        * have the object locked while it cleans it out. The VOP_LOCK
-        * ensures that the VOP_INACTIVE routine is done with its work.
-        * For active vnodes, it ensures that no other activity can
-        * occur while the buffer list is being cleaned out.
+        * Clean out any buffers associated with the vnode.
         */
         */
-       VOP_LOCK(vp);
        if (flags & DOCLOSE)
        if (flags & DOCLOSE)
-               vinvalbuf(vp, 1);
+               vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
        /*
        /*
-        * Prevent any further operations on the vnode from
-        * being passed through to the old file system.
+        * Any other processes trying to obtain this lock must first
+        * wait for VXLOCK to clear, then call the new lock operation.
         */
         */
-       origops = vp->v_op;
-       vp->v_op = &dead_vnodeops;
-       vp->v_tag = VT_NON;
+       VOP_UNLOCK(vp);
        /*
        /*
-        * If purging an active vnode, it must be unlocked, closed,
-        * and deactivated before being reclaimed.
+        * If purging an active vnode, it must be closed and
+        * deactivated before being reclaimed.
         */
         */
-       (*(origops->vop_unlock))(vp);
        if (active) {
                if (flags & DOCLOSE)
        if (active) {
                if (flags & DOCLOSE)
-                       (*(origops->vop_close))(vp, IO_NDELAY, NOCRED, p);
-               (*(origops->vop_inactive))(vp, p);
+                       VOP_CLOSE(vp, IO_NDELAY, NOCRED, NULL);
+               VOP_INACTIVE(vp);
        }
        /*
         * Reclaim the vnode.
         */
        }
        /*
         * Reclaim the vnode.
         */
-       if ((*(origops->vop_reclaim))(vp))
+       if (VOP_RECLAIM(vp))
                panic("vclean: cannot reclaim");
        if (active)
                vrele(vp);
                panic("vclean: cannot reclaim");
        if (active)
                vrele(vp);
+
        /*
        /*
-        * Done with purge, notify sleepers in vget of the grim news.
+        * Done with purge, notify sleepers of the grim news.
         */
         */
+       vp->v_op = dead_vnodeop_p;
+       vp->v_tag = VT_NON;
        vp->v_flag &= ~VXLOCK;
        if (vp->v_flag & VXWANT) {
                vp->v_flag &= ~VXWANT;
        vp->v_flag &= ~VXLOCK;
        if (vp->v_flag & VXWANT) {
                vp->v_flag &= ~VXWANT;
@@ -911,7 +890,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,
@@ -935,6 +913,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.
@@ -953,17 +932,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;
                }
@@ -971,9 +951,10 @@ void vgone(vp)
                vp->v_specinfo = NULL;
        }
        /*
                vp->v_specinfo = NULL;
        }
        /*
-        * If it is on the freelist, move it to the head of the list.
+        * If it is on the freelist and not already at the head,
+        * move it to the head of the list.
         */
         */
-       if (vp->v_freeb) {
+       if (vp->v_freeb && vfreeh != vp) {
                if (vq = vp->v_freef)
                        vq->v_freeb = vp->v_freeb;
                else
                if (vq = vp->v_freef)
                        vq->v_freeb = vp->v_freeb;
                else
@@ -1001,9 +982,9 @@ vfinddev(dev, type, vpp)
                if (dev != vp->v_rdev || type != vp->v_type)
                        continue;
                *vpp = vp;
                if (dev != vp->v_rdev || type != vp->v_type)
                        continue;
                *vpp = vp;
-               return (0);
+               return (1);
        }
        }
-       return (1);
+       return (0);
 }
 
 /*
 }
 
 /*
@@ -1096,28 +1077,28 @@ int kinfo_vdebug = 1;
 int kinfo_vgetfailed;
 #define KINFO_VNODESLOP        10
 /*
 int kinfo_vgetfailed;
 #define KINFO_VNODESLOP        10
 /*
- * Dump vnode list (via kinfo).
+ * Dump vnode list (via sysctl).
  * Copyout address of vnode followed by vnode.
  */
 /* ARGSUSED */
  * Copyout address of vnode followed by vnode.
  */
 /* ARGSUSED */
-kinfo_vnode(op, where, acopysize, arg, aneeded)
-       int op;
+sysctl_vnode(where, sizep)
        char *where;
        char *where;
-       int *acopysize, arg, *aneeded;
+       size_t *sizep;
 {
        register struct mount *mp = rootfs;
        struct mount *omp;
        struct vnode *vp;
        register char *bp = where, *savebp;
 {
        register struct mount *mp = rootfs;
        struct mount *omp;
        struct vnode *vp;
        register char *bp = where, *savebp;
-       char *ewhere = where + *acopysize;
+       char *ewhere;
        int error;
 
 #define VPTRSZ sizeof (struct vnode *)
 #define VNODESZ        sizeof (struct vnode)
        if (where == NULL) {
        int error;
 
 #define VPTRSZ sizeof (struct vnode *)
 #define VNODESZ        sizeof (struct vnode)
        if (where == NULL) {
-               *aneeded = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
+               *sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
                return (0);
        }
                return (0);
        }
+       ewhere = where + *sizep;
                
        do {
                if (vfs_busy(mp)) {
                
        do {
                if (vfs_busy(mp)) {
@@ -1138,10 +1119,12 @@ again:
                                bp = savebp;
                                goto again;
                        }
                                bp = savebp;
                                goto again;
                        }
-                       if ((bp + VPTRSZ + VNODESZ <= ewhere) && 
-                           ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
-                            (error = copyout((caddr_t)vp, bp + VPTRSZ, 
-                             VNODESZ))))
+                       if (bp + VPTRSZ + VNODESZ > ewhere) {
+                               *sizep = bp - where;
+                               return (ENOMEM);
+                       }
+                       if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
+                          (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ)))
                                return (error);
                        bp += VPTRSZ + VNODESZ;
                }
                                return (error);
                        bp += VPTRSZ + VNODESZ;
                }
@@ -1150,10 +1133,6 @@ again:
                vfs_unbusy(omp);
        } while (mp != rootfs);
 
                vfs_unbusy(omp);
        } while (mp != rootfs);
 
-       *aneeded = bp - where;
-       if (bp > ewhere)
-               *acopysize = ewhere - where;
-       else
-               *acopysize = bp - where;
+       *sizep = bp - where;
        return (0);
 }
        return (0);
 }