+#define binshash(bp, dp) LIST_INSERT_HEAD(dp, bp, b_hash)
+#define bremhash(bp) LIST_REMOVE(bp, b_hash)
+
+/*
+ * Definitions for the buffer free lists.
+ */
+#define BQUEUES 4 /* number of free buffer queues */
+
+#define BQ_LOCKED 0 /* super-blocks &c */
+#define BQ_LRU 1 /* lru, useful buffers */
+#define BQ_AGE 2 /* rubbish */
+#define BQ_EMPTY 3 /* buffer headers with no memory */
+
+TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
+int needbuffer;
+
+/*
+ * Insq/Remq for the buffer free lists.
+ */
+#define binsheadfree(bp, dp) TAILQ_INSERT_HEAD(dp, bp, b_freelist)
+#define binstailfree(bp, dp) TAILQ_INSERT_TAIL(dp, bp, b_freelist)
+
+void
+bremfree(bp)
+ struct buf *bp;
+{
+ struct bqueues *dp = NULL;
+
+ /*
+ * We only calculate the head of the freelist when removing
+ * the last element of the list as that is the only time that
+ * it is needed (e.g. to reset the tail pointer).
+ *
+ * NB: This makes an assumption about how tailq's are implemented.
+ */
+ if (bp->b_freelist.tqe_next == NULL) {
+ for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
+ if (dp->tqh_last == &bp->b_freelist.tqe_next)
+ break;
+ if (dp == &bufqueues[BQUEUES])
+ panic("bremfree: lost tail");
+ }
+ TAILQ_REMOVE(dp, bp, b_freelist);
+}
+
+/*
+ * Initialize buffers and hash links for buffers.
+ */
+void
+bufinit()
+{
+ register struct buf *bp;
+ struct bqueues *dp;
+ register int i;
+ int base, residual;
+
+ for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
+ TAILQ_INIT(dp);
+ bufhashtbl = hashinit(nbuf, M_CACHE, &bufhash);
+ base = bufpages / nbuf;
+ residual = bufpages % nbuf;
+ for (i = 0; i < nbuf; i++) {
+ bp = &buf[i];
+ bzero((char *)bp, sizeof *bp);
+ bp->b_dev = NODEV;
+ bp->b_rcred = NOCRED;
+ bp->b_wcred = NOCRED;
+ bp->b_vnbufs.le_next = NOLIST;
+ bp->b_data = buffers + i * MAXBSIZE;
+ if (i < residual)
+ bp->b_bufsize = (base + 1) * CLBYTES;
+ else
+ bp->b_bufsize = base * CLBYTES;
+ bp->b_flags = B_INVAL;
+ dp = bp->b_bufsize ? &bufqueues[BQ_AGE] : &bufqueues[BQ_EMPTY];
+ binsheadfree(bp, dp);
+ binshash(bp, &invalhash);
+ }
+}
+
+/*
+ * Find the block in the buffer pool.
+ * If the buffer is not present, allocate a new buffer and load
+ * its contents according to the filesystem fill routine.
+ */
+bread(vp, blkno, size, cred, bpp)
+ struct vnode *vp;