BUF/BIO cleanup 2/99:
authorHiten Pandya <hmp@dragonflybsd.org>
Wed, 3 Aug 2005 04:59:53 +0000 (04:59 +0000)
committerHiten Pandya <hmp@dragonflybsd.org>
Wed, 3 Aug 2005 04:59:53 +0000 (04:59 +0000)
Localise buffer queue information into kern/vfs_bio.c, it should not be
messed with outside of the named file.  Convert the QUEUE_* #defines
into enum bufq_type, prefix the names with 'B'.  The change to initpbuf()
is acceptable since they are a hack anyway, not to mention that

Move vfs_bufstats() from kern/vfs_syscalls.c into kern/vfs_bio.c since
that's where it should really belong, atleast till its use is cleaned.

Move bufqueues extern from sys/buf.h into kern/vfs_bio.c as it shouldn't
be messed with by anything else.  It was only sitting in sys/buf.h
because of vfs_bufstats().

Note the change to initpbuf() is acceptable since they are a hack anyway,
not to mention that the said function and friends should probably reside
in kern/vfs_bio.c.

sys/kern/vfs_bio.c
sys/kern/vfs_syscalls.c
sys/sys/buf.h
sys/vm/vm_pager.c

index edc6c04..271549d 100644 (file)
@@ -12,7 +12,7 @@
  *             John S. Dyson.
  *
  * $FreeBSD: src/sys/kern/vfs_bio.c,v 1.242.2.20 2003/05/28 18:38:10 alc Exp $
- * $DragonFly: src/sys/kern/vfs_bio.c,v 1.38 2005/07/28 18:15:09 dillon Exp $
+ * $DragonFly: src/sys/kern/vfs_bio.c,v 1.39 2005/08/03 04:59:53 hmp Exp $
  */
 
 /*
 #include <sys/thread2.h>
 #include <vm/vm_page2.h>
 
+/*
+ * Buffer queues.
+ */
+#define BUFFER_QUEUES  6
+enum bufq_type {
+       BQUEUE_NONE,            /* not on any queue */
+       BQUEUE_LOCKED,          /* locked buffers */
+       BQUEUE_CLEAN,           /* non-B_DELWRI buffers */
+       BQUEUE_DIRTY,           /* B_DELWRI buffers */
+       BQUEUE_EMPTYKVA,        /* empty buffer headers with KVA assignment */
+       BQUEUE_EMPTY            /* empty buffer headers */
+};
+TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES];
+
 static MALLOC_DEFINE(M_BIOBUF, "BIO buffer", "BIO buffer");
 
 struct bio_ops bioops;         /* I/O operation notification */
@@ -168,7 +182,6 @@ SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
 static int bufhashmask;
 static int bufhashshift;
 static LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash;
-struct bqueues bufqueues[BUFFER_QUEUES] = { { 0 } };
 char *buf_wmesg = BUF_WMESG;
 
 extern int vm_swap_size;
@@ -411,12 +424,12 @@ bufinit(void)
                bzero(bp, sizeof *bp);
                bp->b_flags = B_INVAL;  /* we're just an empty header */
                bp->b_dev = NODEV;
-               bp->b_qindex = QUEUE_EMPTY;
+               bp->b_qindex = BQUEUE_EMPTY;
                bp->b_xflags = 0;
                xio_init(&bp->b_xio);
                LIST_INIT(&bp->b_dep);
                BUF_LOCKINIT(bp);
-               TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
+               TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_EMPTY], bp, b_freelist);
                LIST_INSERT_HEAD(&invalhash, bp, b_hash);
        }
 
@@ -532,11 +545,11 @@ bremfree(struct buf * bp)
        crit_enter();
        old_qindex = bp->b_qindex;
 
-       if (bp->b_qindex != QUEUE_NONE) {
+       if (bp->b_qindex != BQUEUE_NONE) {
                KASSERT(BUF_REFCNTNB(bp) == 1, 
                                ("bremfree: bp %p not locked",bp));
                TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
-               bp->b_qindex = QUEUE_NONE;
+               bp->b_qindex = BQUEUE_NONE;
        } else {
                if (BUF_REFCNTNB(bp) <= 1)
                        panic("bremfree: removing a buffer not on a queue");
@@ -549,10 +562,10 @@ bremfree(struct buf * bp)
         */
        if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
                switch(old_qindex) {
-               case QUEUE_DIRTY:
-               case QUEUE_CLEAN:
-               case QUEUE_EMPTY:
-               case QUEUE_EMPTYKVA:
+               case BQUEUE_DIRTY:
+               case BQUEUE_CLEAN:
+               case BQUEUE_EMPTY:
+               case BQUEUE_EMPTYKVA:
                        --numfreebuffers;
                        break;
                default:
@@ -914,12 +927,12 @@ bdwrite(struct buf *bp)
  *     count.
  *
  *     Must be called from a critical section.
- *     The buffer must be on QUEUE_NONE.
+ *     The buffer must be on BQUEUE_NONE.
  */
 void
 bdirty(struct buf *bp)
 {
-       KASSERT(bp->b_qindex == QUEUE_NONE, ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
+       KASSERT(bp->b_qindex == BQUEUE_NONE, ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
        bp->b_flags &= ~(B_READ|B_RELBUF);
 
        if ((bp->b_flags & B_DELWRI) == 0) {
@@ -940,7 +953,7 @@ bdirty(struct buf *bp)
  *     
  *     Must be called from a critical section.
  *
- *     The buffer is typically on QUEUE_NONE but there is one case in 
+ *     The buffer is typically on BQUEUE_NONE but there is one case in 
  *     brelse() that calls this function after placing the buffer on
  *     a different queue.
  */
@@ -1246,7 +1259,7 @@ brelse(struct buf * bp)
                }
        }
                        
-       if (bp->b_qindex != QUEUE_NONE)
+       if (bp->b_qindex != BQUEUE_NONE)
                panic("brelse: free buffer onto another queue???");
        if (BUF_REFCNTNB(bp) > 1) {
                /* Temporary panic to verify exclusive locking */
@@ -1276,9 +1289,9 @@ brelse(struct buf * bp)
                if (bp->b_xflags & BX_BKGRDINPROG)
                        panic("losing buffer 1");
                if (bp->b_kvasize) {
-                       bp->b_qindex = QUEUE_EMPTYKVA;
+                       bp->b_qindex = BQUEUE_EMPTYKVA;
                } else {
-                       bp->b_qindex = QUEUE_EMPTY;
+                       bp->b_qindex = BQUEUE_EMPTY;
                }
                TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
                LIST_REMOVE(bp, b_hash);
@@ -1294,8 +1307,8 @@ brelse(struct buf * bp)
                bp->b_xflags &= ~BX_BKGRDWRITE;
                if (bp->b_xflags & BX_BKGRDINPROG)
                        panic("losing buffer 2");
-               bp->b_qindex = QUEUE_CLEAN;
-               TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
+               bp->b_qindex = BQUEUE_CLEAN;
+               TAILQ_INSERT_HEAD(&bufqueues[BQUEUE_CLEAN], bp, b_freelist);
                LIST_REMOVE(bp, b_hash);
                LIST_INSERT_HEAD(&invalhash, bp, b_hash);
                bp->b_dev = NODEV;
@@ -1303,8 +1316,8 @@ brelse(struct buf * bp)
                /*
                 * Buffers that are locked.
                 */
-               bp->b_qindex = QUEUE_LOCKED;
-               TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LOCKED], bp, b_freelist);
+               bp->b_qindex = BQUEUE_LOCKED;
+               TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_LOCKED], bp, b_freelist);
        } else {
                /*
                 * Remaining buffers.  These buffers are still associated with
@@ -1312,20 +1325,20 @@ brelse(struct buf * bp)
                 */
                switch(bp->b_flags & (B_DELWRI|B_AGE)) {
                case B_DELWRI | B_AGE:
-                   bp->b_qindex = QUEUE_DIRTY;
-                   TAILQ_INSERT_HEAD(&bufqueues[QUEUE_DIRTY], bp, b_freelist);
+                   bp->b_qindex = BQUEUE_DIRTY;
+                   TAILQ_INSERT_HEAD(&bufqueues[BQUEUE_DIRTY], bp, b_freelist);
                    break;
                case B_DELWRI:
-                   bp->b_qindex = QUEUE_DIRTY;
-                   TAILQ_INSERT_TAIL(&bufqueues[QUEUE_DIRTY], bp, b_freelist);
+                   bp->b_qindex = BQUEUE_DIRTY;
+                   TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_DIRTY], bp, b_freelist);
                    break;
                case B_AGE:
-                   bp->b_qindex = QUEUE_CLEAN;
-                   TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
+                   bp->b_qindex = BQUEUE_CLEAN;
+                   TAILQ_INSERT_HEAD(&bufqueues[BQUEUE_CLEAN], bp, b_freelist);
                    break;
                default:
-                   bp->b_qindex = QUEUE_CLEAN;
-                   TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
+                   bp->b_qindex = BQUEUE_CLEAN;
+                   TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_CLEAN], bp, b_freelist);
                    break;
                }
        }
@@ -1377,7 +1390,7 @@ bqrelse(struct buf * bp)
 
        KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)), ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
 
-       if (bp->b_qindex != QUEUE_NONE)
+       if (bp->b_qindex != BQUEUE_NONE)
                panic("bqrelse: free buffer onto another queue???");
        if (BUF_REFCNTNB(bp) > 1) {
                /* do not release to free list */
@@ -1388,12 +1401,12 @@ bqrelse(struct buf * bp)
        }
        if (bp->b_flags & B_LOCKED) {
                bp->b_flags &= ~B_ERROR;
-               bp->b_qindex = QUEUE_LOCKED;
-               TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LOCKED], bp, b_freelist);
+               bp->b_qindex = BQUEUE_LOCKED;
+               TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_LOCKED], bp, b_freelist);
                /* buffers with stale but valid contents */
        } else if (bp->b_flags & B_DELWRI) {
-               bp->b_qindex = QUEUE_DIRTY;
-               TAILQ_INSERT_TAIL(&bufqueues[QUEUE_DIRTY], bp, b_freelist);
+               bp->b_qindex = BQUEUE_DIRTY;
+               TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_DIRTY], bp, b_freelist);
        } else if (vm_page_count_severe()) {
                /*
                 * We are too low on memory, we have to try to free the
@@ -1404,8 +1417,8 @@ bqrelse(struct buf * bp)
                brelse(bp);
                return;
        } else {
-               bp->b_qindex = QUEUE_CLEAN;
-               TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
+               bp->b_qindex = BQUEUE_CLEAN;
+               TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_CLEAN], bp, b_freelist);
        }
 
        if ((bp->b_flags & B_LOCKED) == 0 &&
@@ -1641,8 +1654,8 @@ restart:
         * However, there are a number of cases (defragging, reusing, ...)
         * where we cannot backup.
         */
-       nqindex = QUEUE_EMPTYKVA;
-       nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]);
+       nqindex = BQUEUE_EMPTYKVA;
+       nbp = TAILQ_FIRST(&bufqueues[BQUEUE_EMPTYKVA]);
 
        if (nbp == NULL) {
                /*
@@ -1652,8 +1665,8 @@ restart:
                 * skip this step so we can allocate a new buffer.
                 */
                if (defrag || bufspace >= lobufspace) {
-                       nqindex = QUEUE_CLEAN;
-                       nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]);
+                       nqindex = BQUEUE_CLEAN;
+                       nbp = TAILQ_FIRST(&bufqueues[BQUEUE_CLEAN]);
                }
 
                /*
@@ -1664,8 +1677,8 @@ restart:
                 */
                if (nbp == NULL && defrag == 0 &&
                    bufspace + maxsize < hibufspace) {
-                       nqindex = QUEUE_EMPTY;
-                       nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
+                       nqindex = BQUEUE_EMPTY;
+                       nbp = TAILQ_FIRST(&bufqueues[BQUEUE_EMPTY]);
                }
        }
 
@@ -1683,17 +1696,17 @@ restart:
                 */
                if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) {
                        switch(qindex) {
-                       case QUEUE_EMPTY:
-                               nqindex = QUEUE_EMPTYKVA;
-                               if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA])))
+                       case BQUEUE_EMPTY:
+                               nqindex = BQUEUE_EMPTYKVA;
+                               if ((nbp = TAILQ_FIRST(&bufqueues[BQUEUE_EMPTYKVA])))
                                        break;
                                /* fall through */
-                       case QUEUE_EMPTYKVA:
-                               nqindex = QUEUE_CLEAN;
-                               if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN])))
+                       case BQUEUE_EMPTYKVA:
+                               nqindex = BQUEUE_CLEAN;
+                               if ((nbp = TAILQ_FIRST(&bufqueues[BQUEUE_CLEAN])))
                                        break;
                                /* fall through */
-                       case QUEUE_CLEAN:
+                       case BQUEUE_CLEAN:
                                /*
                                 * nbp is NULL. 
                                 */
@@ -1726,7 +1739,7 @@ restart:
 
                /*
                 * Start freeing the bp.  This is somewhat involved.  nbp
-                * remains valid only for QUEUE_EMPTY[KVA] bp's.  Buffers
+                * remains valid only for BQUEUE_EMPTY[KVA] bp's.  Buffers
                 * on the clean list must be disassociated from their 
                 * current vnode.  Buffers on the empty[kva] lists have
                 * already been disassociated.
@@ -1736,7 +1749,7 @@ restart:
                        panic("getnewbuf: locked buf");
                bremfree(bp);
 
-               if (qindex == QUEUE_CLEAN) {
+               if (qindex == BQUEUE_CLEAN) {
                        if (bp->b_flags & B_VMIO) {
                                bp->b_flags &= ~B_ASYNC;
                                vfs_vmio_release(bp);
@@ -1984,7 +1997,7 @@ flushbufqueues(void)
        struct buf *bp;
        int r = 0;
 
-       bp = TAILQ_FIRST(&bufqueues[QUEUE_DIRTY]);
+       bp = TAILQ_FIRST(&bufqueues[BQUEUE_DIRTY]);
 
        while (bp) {
                KASSERT((bp->b_flags & B_DELWRI), ("unexpected clean buffer %p", bp));
@@ -2002,12 +2015,12 @@ flushbufqueues(void)
                            bioops.io_countdeps &&
                            (bp->b_flags & B_DEFERRED) == 0 &&
                            (*bioops.io_countdeps)(bp, 0)) {
-                               TAILQ_REMOVE(&bufqueues[QUEUE_DIRTY],
+                               TAILQ_REMOVE(&bufqueues[BQUEUE_DIRTY],
                                    bp, b_freelist);
-                               TAILQ_INSERT_TAIL(&bufqueues[QUEUE_DIRTY],
+                               TAILQ_INSERT_TAIL(&bufqueues[BQUEUE_DIRTY],
                                    bp, b_freelist);
                                bp->b_flags |= B_DEFERRED;
-                               bp = TAILQ_FIRST(&bufqueues[QUEUE_DIRTY]);
+                               bp = TAILQ_FIRST(&bufqueues[BQUEUE_DIRTY]);
                                continue;
                        }
                        vfs_bio_awrite(bp);
@@ -3471,6 +3484,39 @@ vunmapbuf(struct buf *bp)
        bp->b_data = bp->b_saveaddr;
 }
 
+/*
+ * print out statistics from the current status of the buffer pool
+ * this can be toggeled by the system control option debug.syncprt
+ */
+#ifdef DEBUG
+void
+vfs_bufstats(void)
+{
+        int i, j, count;
+        struct buf *bp;
+        struct bqueues *dp;
+        int counts[(MAXBSIZE / PAGE_SIZE) + 1];
+        static char *bname[3] = { "LOCKED", "LRU", "AGE" };
+
+        for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
+                count = 0;
+                for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
+                        counts[j] = 0;
+               crit_enter();
+                TAILQ_FOREACH(bp, dp, b_freelist) {
+                        counts[bp->b_bufsize/PAGE_SIZE]++;
+                        count++;
+                }
+               crit_exit();
+                printf("%s: total-%d", bname[i], count);
+                for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
+                        if (counts[j] != 0)
+                                printf(", %d-%d", j * PAGE_SIZE, counts[j]);
+                printf("\n");
+        }
+}
+#endif
+
 #include "opt_ddb.h"
 #ifdef DDB
 #include <ddb/ddb.h>
index 54c5bac..c37a347 100644 (file)
@@ -37,7 +37,7 @@
  *
  *     @(#)vfs_syscalls.c      8.13 (Berkeley) 4/15/94
  * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
- * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.65 2005/07/23 23:26:50 joerg Exp $
+ * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.66 2005/08/03 04:59:53 hmp Exp $
  */
 
 #include <sys/param.h>
@@ -3522,39 +3522,6 @@ extattr_delete_file(struct extattr_delete_file_args *uap)
        return(error);
 }
 
-/*
- * print out statistics from the current status of the buffer pool
- * this can be toggeled by the system control option debug.syncprt
- */
-#ifdef DEBUG
-void
-vfs_bufstats(void)
-{
-        int i, j, count;
-        struct buf *bp;
-        struct bqueues *dp;
-        int counts[(MAXBSIZE / PAGE_SIZE) + 1];
-        static char *bname[3] = { "LOCKED", "LRU", "AGE" };
-
-        for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
-                count = 0;
-                for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
-                        counts[j] = 0;
-               crit_enter();
-                TAILQ_FOREACH(bp, dp, b_freelist) {
-                        counts[bp->b_bufsize/PAGE_SIZE]++;
-                        count++;
-                }
-               crit_exit();
-                printf("%s: total-%d", bname[i], count);
-                for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
-                        if (counts[j] != 0)
-                                printf(", %d-%d", j * PAGE_SIZE, counts[j]);
-                printf("\n");
-        }
-}
-#endif
-
 static int
 chroot_visible_mnt(struct mount *mp, struct proc *p)
 {
index 83f4388..43f43a7 100644 (file)
@@ -37,7 +37,7 @@
  *
  *     @(#)buf.h       8.9 (Berkeley) 3/30/95
  * $FreeBSD: src/sys/sys/buf.h,v 1.88.2.10 2003/01/25 19:02:23 dillon Exp $
- * $DragonFly: src/sys/sys/buf.h,v 1.12 2005/04/15 19:08:13 dillon Exp $
+ * $DragonFly: src/sys/sys/buf.h,v 1.13 2005/08/03 04:59:53 hmp Exp $
  */
 
 #ifndef _SYS_BUF_H_
@@ -299,18 +299,6 @@ struct cluster_save {
        struct buf **bs_children;       /* List of associated buffers. */
 };
 
-/*
- * Definitions for the buffer free lists.
- */
-#define BUFFER_QUEUES  6       /* number of free buffer queues */
-
-#define QUEUE_NONE     0       /* on no queue */
-#define QUEUE_LOCKED   1       /* locked buffers */
-#define QUEUE_CLEAN    2       /* non-B_DELWRI buffers */
-#define QUEUE_DIRTY    3       /* B_DELWRI buffers */
-#define QUEUE_EMPTYKVA 4       /* empty buffer headers w/KVA assignment */
-#define QUEUE_EMPTY    5       /* empty buffer headers */
-
 /*
  * Zero out the buffer's data area.
  */
@@ -343,7 +331,6 @@ extern int  bufpages;               /* Number of memory pages in the buffer pool. */
 extern struct  buf *swbuf;             /* Swap I/O buffer headers. */
 extern int     nswbuf;                 /* Number of swap I/O buffer headers. */
 extern TAILQ_HEAD(swqueue, buf) bswlist;
-extern TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES];
 
 struct uio;
 
index eb16429..d6d7613 100644 (file)
@@ -62,7 +62,7 @@
  * rights to redistribute these changes.
  *
  * $FreeBSD: src/sys/vm/vm_pager.c,v 1.54.2.2 2001/11/18 07:11:00 dillon Exp $
- * $DragonFly: src/sys/vm/vm_pager.c,v 1.12 2005/06/02 20:57:21 swildner Exp $
+ * $DragonFly: src/sys/vm/vm_pager.c,v 1.13 2005/08/03 04:59:53 hmp Exp $
  */
 
 /*
@@ -307,7 +307,7 @@ vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
 static void
 initpbuf(struct buf *bp)
 {
-       bp->b_qindex = QUEUE_NONE;
+       bp->b_qindex = 0; /* BQUEUE_NONE */
        bp->b_data = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva;
        bp->b_kvabase = bp->b_data;
        bp->b_kvasize = MAXPHYS;