BSD 4_4 release
[unix-history] / usr / src / sys / kern / vfs_subr.c
index f8e6b5a..d11e8d3 100644 (file)
@@ -1,10 +1,36 @@
 /*
 /*
- * 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.90 (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
  */
 
 /*
  */
 
 /*
@@ -24,6 +50,9 @@
 #include <sys/errno.h>
 #include <sys/malloc.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] = {
 #include <miscfs/specfs/specdev.h>
 
 enum vtype iftovt_tab[16] = {
@@ -190,6 +219,7 @@ void vattr_null(vap)
                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_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_vaflags = 0;
 }
 
 /*
 }
 
 /*
@@ -201,6 +231,7 @@ extern void vclean();
 long numvnodes;
 extern struct vattr va_null;
 
 long numvnodes;
 extern struct vattr va_null;
 
+
 /*
  * Return the next vnode from the free list.
  */
 /*
  * Return the next vnode from the free list.
  */
@@ -211,6 +242,7 @@ getnewvnode(tag, mp, vops, vpp)
        struct vnode **vpp;
 {
        register struct vnode *vp, *vq;
        struct vnode **vpp;
 {
        register struct vnode *vp, *vq;
+       int s;
 
        if ((vfreeh == NULL && numvnodes < 2 * desiredvnodes) ||
            numvnodes < desiredvnodes) {
 
        if ((vfreeh == NULL && numvnodes < 2 * desiredvnodes) ||
            numvnodes < desiredvnodes) {
@@ -236,22 +268,32 @@ getnewvnode(tag, mp, vops, vpp)
                vp->v_lease = NULL;
                if (vp->v_type != VBAD)
                        vgone(vp);
                vp->v_lease = NULL;
                if (vp->v_type != VBAD)
                        vgone(vp);
+#ifdef DIAGNOSTIC
                if (vp->v_data)
                        panic("cleaned vnode isn't");
                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.
  */
@@ -293,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");
@@ -310,11 +354,12 @@ vwakeup(bp)
  * Called with the underlying object locked.
  */
 int
  * Called with the underlying object locked.
  */
 int
-vinvalbuf(vp, flags, cred, p)
+vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
        register struct vnode *vp;
        int flags;
        struct ucred *cred;
        struct proc *p;
        register struct vnode *vp;
        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;
@@ -344,13 +389,26 @@ vinvalbuf(vp, flags, cred, p)
                        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);
+                       /*
+                        * 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;
+                       }
                        bp->b_flags |= B_INVAL;
                        brelse(bp);
                }
                        bp->b_flags |= B_INVAL;
                        brelse(bp);
                }
@@ -552,13 +610,11 @@ 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);
 }
 
-int bug_refs = 0;
-
 /*
  * Vnode reference, just increment the count
  */
 /*
  * Vnode reference, just increment the count
  */
@@ -566,11 +622,9 @@ 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++;
-       if (vp->v_type != VBLK && curproc)
-               curproc->p_spare[0]++;
-       if (bug_refs)
-               vprint("vref: ");
 }
 
 /*
 }
 
 /*
@@ -597,10 +651,6 @@ void vrele(vp)
                panic("vrele: null vp");
 #endif
        vp->v_usecount--;
                panic("vrele: null vp");
 #endif
        vp->v_usecount--;
-       if (vp->v_type != VBLK && curproc)
-               curproc->p_spare[0]--;
-       if (bug_refs)
-               vprint("vref: ");
        if (vp->v_usecount > 0)
                return;
 #ifdef DIAGNOSTIC
        if (vp->v_usecount > 0)
                return;
 #ifdef DIAGNOSTIC
@@ -649,7 +699,8 @@ void 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;
@@ -752,7 +803,7 @@ vclean(vp, flags)
         * Clean out any buffers associated with the vnode.
         */
        if (flags & DOCLOSE)
         * Clean out any buffers associated with the vnode.
         */
        if (flags & DOCLOSE)
-               vinvalbuf(vp, 1, NOCRED, NULL);
+               vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
        /*
         * Any other processes trying to obtain this lock must first
         * wait for VXLOCK to clear, then call the new lock operation.
        /*
         * Any other processes trying to obtain this lock must first
         * wait for VXLOCK to clear, then call the new lock operation.
@@ -931,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);
 }
 
 /*
 }
 
 /*
@@ -1026,14 +1077,13 @@ 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;
 {
        register struct mount *mp = rootfs;
        struct mount *omp;
@@ -1045,10 +1095,10 @@ kinfo_vnode(op, where, acopysize, arg, aneeded)
 #define VPTRSZ sizeof (struct vnode *)
 #define VNODESZ        sizeof (struct vnode)
        if (where == NULL) {
 #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 + *acopysize;
+       ewhere = where + *sizep;
                
        do {
                if (vfs_busy(mp)) {
                
        do {
                if (vfs_busy(mp)) {
@@ -1069,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;
                }
@@ -1081,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);
 }