Home | History | Annotate | Line # | Download | only in kern
vfs_bio.c revision 1.77
      1  1.77     lukem /*	$NetBSD: vfs_bio.c,v 1.77 2001/11/12 15:25:35 lukem Exp $	*/
      2  1.31       cgd 
      3  1.31       cgd /*-
      4  1.31       cgd  * Copyright (c) 1994 Christopher G. Demetriou
      5  1.31       cgd  * Copyright (c) 1982, 1986, 1989, 1993
      6  1.31       cgd  *	The Regents of the University of California.  All rights reserved.
      7  1.31       cgd  * (c) UNIX System Laboratories, Inc.
      8  1.31       cgd  * All or some portions of this file are derived from material licensed
      9  1.31       cgd  * to the University of California by American Telephone and Telegraph
     10  1.31       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     11  1.31       cgd  * the permission of UNIX System Laboratories, Inc.
     12  1.31       cgd  *
     13  1.31       cgd  * Redistribution and use in source and binary forms, with or without
     14  1.31       cgd  * modification, are permitted provided that the following conditions
     15  1.31       cgd  * are met:
     16  1.31       cgd  * 1. Redistributions of source code must retain the above copyright
     17  1.31       cgd  *    notice, this list of conditions and the following disclaimer.
     18  1.31       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.31       cgd  *    notice, this list of conditions and the following disclaimer in the
     20  1.31       cgd  *    documentation and/or other materials provided with the distribution.
     21  1.31       cgd  * 3. All advertising materials mentioning features or use of this software
     22  1.31       cgd  *    must display the following acknowledgement:
     23  1.31       cgd  *	This product includes software developed by the University of
     24  1.31       cgd  *	California, Berkeley and its contributors.
     25  1.31       cgd  * 4. Neither the name of the University nor the names of its contributors
     26  1.31       cgd  *    may be used to endorse or promote products derived from this software
     27  1.31       cgd  *    without specific prior written permission.
     28  1.31       cgd  *
     29  1.31       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  1.31       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  1.31       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  1.31       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  1.31       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  1.31       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  1.31       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  1.31       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  1.31       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  1.31       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  1.31       cgd  * SUCH DAMAGE.
     40  1.31       cgd  *
     41  1.31       cgd  *	@(#)vfs_bio.c	8.6 (Berkeley) 1/11/94
     42  1.31       cgd  */
     43  1.31       cgd 
     44  1.31       cgd /*
     45  1.31       cgd  * Some references:
     46  1.31       cgd  *	Bach: The Design of the UNIX Operating System (Prentice Hall, 1986)
     47  1.31       cgd  *	Leffler, et al.: The Design and Implementation of the 4.3BSD
     48  1.31       cgd  *		UNIX Operating System (Addison Welley, 1989)
     49  1.31       cgd  */
     50  1.77     lukem 
     51  1.77     lukem #include <sys/cdefs.h>
     52  1.77     lukem __KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.77 2001/11/12 15:25:35 lukem Exp $");
     53  1.31       cgd 
     54  1.31       cgd #include <sys/param.h>
     55  1.31       cgd #include <sys/systm.h>
     56  1.31       cgd #include <sys/proc.h>
     57  1.31       cgd #include <sys/buf.h>
     58  1.31       cgd #include <sys/vnode.h>
     59  1.31       cgd #include <sys/mount.h>
     60  1.31       cgd #include <sys/malloc.h>
     61  1.31       cgd #include <sys/resourcevar.h>
     62  1.35   mycroft #include <sys/conf.h>
     63  1.40  christos 
     64  1.73       chs #include <uvm/uvm.h>
     65  1.71   thorpej 
     66  1.59      fvdl #include <miscfs/specfs/specdev.h>
     67  1.59      fvdl 
     68  1.31       cgd /* Macros to clear/set/test flags. */
     69  1.31       cgd #define	SET(t, f)	(t) |= (f)
     70  1.31       cgd #define	CLR(t, f)	(t) &= ~(f)
     71  1.31       cgd #define	ISSET(t, f)	((t) & (f))
     72  1.31       cgd 
     73  1.31       cgd /*
     74  1.31       cgd  * Definitions for the buffer hash lists.
     75  1.31       cgd  */
     76  1.31       cgd #define	BUFHASH(dvp, lbn)	\
     77  1.73       chs 	(&bufhashtbl[(((long)(dvp) >> 8) + (int)(lbn)) & bufhash])
     78  1.31       cgd LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash;
     79  1.31       cgd u_long	bufhash;
     80  1.59      fvdl struct bio_ops bioops;	/* I/O operation notification */
     81  1.31       cgd 
     82  1.31       cgd /*
     83  1.31       cgd  * Insq/Remq for the buffer hash lists.
     84  1.31       cgd  */
     85  1.31       cgd #define	binshash(bp, dp)	LIST_INSERT_HEAD(dp, bp, b_hash)
     86  1.31       cgd #define	bremhash(bp)		LIST_REMOVE(bp, b_hash)
     87  1.31       cgd 
     88  1.31       cgd /*
     89  1.31       cgd  * Definitions for the buffer free lists.
     90  1.31       cgd  */
     91  1.31       cgd #define	BQUEUES		4		/* number of free buffer queues */
     92  1.31       cgd 
     93  1.31       cgd #define	BQ_LOCKED	0		/* super-blocks &c */
     94  1.31       cgd #define	BQ_LRU		1		/* lru, useful buffers */
     95  1.31       cgd #define	BQ_AGE		2		/* rubbish */
     96  1.31       cgd #define	BQ_EMPTY	3		/* buffer headers with no memory */
     97  1.31       cgd 
     98  1.31       cgd TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
     99  1.31       cgd int needbuffer;
    100  1.31       cgd 
    101  1.31       cgd /*
    102  1.65   thorpej  * Buffer pool for I/O buffers.
    103  1.65   thorpej  */
    104  1.65   thorpej struct pool bufpool;
    105  1.65   thorpej 
    106  1.65   thorpej /*
    107  1.31       cgd  * Insq/Remq for the buffer free lists.
    108  1.31       cgd  */
    109  1.31       cgd #define	binsheadfree(bp, dp)	TAILQ_INSERT_HEAD(dp, bp, b_freelist)
    110  1.31       cgd #define	binstailfree(bp, dp)	TAILQ_INSERT_TAIL(dp, bp, b_freelist)
    111  1.31       cgd 
    112  1.40  christos static __inline struct buf *bio_doread __P((struct vnode *, daddr_t, int,
    113  1.40  christos 					    struct ucred *, int));
    114  1.40  christos int count_lock_queue __P((void));
    115  1.40  christos 
    116  1.31       cgd void
    117  1.31       cgd bremfree(bp)
    118  1.31       cgd 	struct buf *bp;
    119  1.31       cgd {
    120  1.60      fvdl 	int s = splbio();
    121  1.60      fvdl 
    122  1.31       cgd 	struct bqueues *dp = NULL;
    123  1.31       cgd 
    124  1.31       cgd 	/*
    125  1.31       cgd 	 * We only calculate the head of the freelist when removing
    126  1.31       cgd 	 * the last element of the list as that is the only time that
    127  1.31       cgd 	 * it is needed (e.g. to reset the tail pointer).
    128  1.31       cgd 	 *
    129  1.31       cgd 	 * NB: This makes an assumption about how tailq's are implemented.
    130  1.31       cgd 	 */
    131  1.31       cgd 	if (bp->b_freelist.tqe_next == NULL) {
    132  1.31       cgd 		for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
    133  1.31       cgd 			if (dp->tqh_last == &bp->b_freelist.tqe_next)
    134  1.31       cgd 				break;
    135  1.31       cgd 		if (dp == &bufqueues[BQUEUES])
    136  1.31       cgd 			panic("bremfree: lost tail");
    137  1.31       cgd 	}
    138  1.31       cgd 	TAILQ_REMOVE(dp, bp, b_freelist);
    139  1.60      fvdl 	splx(s);
    140  1.31       cgd }
    141  1.31       cgd 
    142  1.31       cgd /*
    143  1.31       cgd  * Initialize buffers and hash links for buffers.
    144  1.31       cgd  */
    145  1.31       cgd void
    146  1.31       cgd bufinit()
    147  1.31       cgd {
    148  1.66  augustss 	struct buf *bp;
    149  1.31       cgd 	struct bqueues *dp;
    150  1.66  augustss 	int i;
    151  1.31       cgd 	int base, residual;
    152  1.65   thorpej 
    153  1.65   thorpej 	/*
    154  1.65   thorpej 	 * Initialize the buffer pool.  This pool is used for buffers
    155  1.65   thorpej 	 * which are strictly I/O control blocks, not buffer cache
    156  1.65   thorpej 	 * buffers.
    157  1.65   thorpej 	 */
    158  1.65   thorpej 	pool_init(&bufpool, sizeof(struct buf), 0, 0, 0, "bufpl", 0,
    159  1.65   thorpej 	    NULL, NULL, M_DEVBUF);
    160  1.31       cgd 
    161  1.31       cgd 	for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
    162  1.31       cgd 		TAILQ_INIT(dp);
    163  1.70        ad 	bufhashtbl = hashinit(nbuf, HASH_LIST, M_CACHE, M_WAITOK, &bufhash);
    164  1.31       cgd 	base = bufpages / nbuf;
    165  1.31       cgd 	residual = bufpages % nbuf;
    166  1.31       cgd 	for (i = 0; i < nbuf; i++) {
    167  1.31       cgd 		bp = &buf[i];
    168  1.55     perry 		memset((char *)bp, 0, sizeof(*bp));
    169  1.31       cgd 		bp->b_dev = NODEV;
    170  1.31       cgd 		bp->b_vnbufs.le_next = NOLIST;
    171  1.59      fvdl 		LIST_INIT(&bp->b_dep);
    172  1.31       cgd 		bp->b_data = buffers + i * MAXBSIZE;
    173  1.31       cgd 		if (i < residual)
    174  1.71   thorpej 			bp->b_bufsize = (base + 1) * PAGE_SIZE;
    175  1.31       cgd 		else
    176  1.71   thorpej 			bp->b_bufsize = base * PAGE_SIZE;
    177  1.31       cgd 		bp->b_flags = B_INVAL;
    178  1.31       cgd 		dp = bp->b_bufsize ? &bufqueues[BQ_AGE] : &bufqueues[BQ_EMPTY];
    179  1.31       cgd 		binsheadfree(bp, dp);
    180  1.31       cgd 		binshash(bp, &invalhash);
    181  1.31       cgd 	}
    182  1.31       cgd }
    183  1.31       cgd 
    184  1.40  christos static __inline struct buf *
    185  1.34   mycroft bio_doread(vp, blkno, size, cred, async)
    186  1.31       cgd 	struct vnode *vp;
    187  1.31       cgd 	daddr_t blkno;
    188  1.31       cgd 	int size;
    189  1.31       cgd 	struct ucred *cred;
    190  1.34   mycroft 	int async;
    191  1.31       cgd {
    192  1.66  augustss 	struct buf *bp;
    193  1.49       cgd 	struct proc *p = (curproc != NULL ? curproc : &proc0);	/* XXX */
    194  1.31       cgd 
    195  1.34   mycroft 	bp = getblk(vp, blkno, size, 0, 0);
    196  1.31       cgd 
    197  1.31       cgd 	/*
    198  1.34   mycroft 	 * If buffer does not have data valid, start a read.
    199  1.31       cgd 	 * Note that if buffer is B_INVAL, getblk() won't return it.
    200  1.31       cgd 	 * Therefore, it's valid if it's I/O has completed or been delayed.
    201  1.31       cgd 	 */
    202  1.34   mycroft 	if (!ISSET(bp->b_flags, (B_DONE | B_DELWRI))) {
    203  1.73       chs 		/* Start I/O for the buffer. */
    204  1.34   mycroft 		SET(bp->b_flags, B_READ | async);
    205  1.34   mycroft 		VOP_STRATEGY(bp);
    206  1.31       cgd 
    207  1.34   mycroft 		/* Pay for the read. */
    208  1.49       cgd 		p->p_stats->p_ru.ru_inblock++;
    209  1.34   mycroft 	} else if (async) {
    210  1.34   mycroft 		brelse(bp);
    211  1.31       cgd 	}
    212  1.31       cgd 
    213  1.34   mycroft 	return (bp);
    214  1.34   mycroft }
    215  1.34   mycroft 
    216  1.34   mycroft /*
    217  1.34   mycroft  * Read a disk block.
    218  1.34   mycroft  * This algorithm described in Bach (p.54).
    219  1.34   mycroft  */
    220  1.40  christos int
    221  1.34   mycroft bread(vp, blkno, size, cred, bpp)
    222  1.34   mycroft 	struct vnode *vp;
    223  1.34   mycroft 	daddr_t blkno;
    224  1.34   mycroft 	int size;
    225  1.34   mycroft 	struct ucred *cred;
    226  1.34   mycroft 	struct buf **bpp;
    227  1.34   mycroft {
    228  1.66  augustss 	struct buf *bp;
    229  1.34   mycroft 
    230  1.34   mycroft 	/* Get buffer for block. */
    231  1.34   mycroft 	bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
    232  1.31       cgd 
    233  1.51        pk 	/*
    234  1.51        pk 	 * Delayed write buffers are found in the cache and have
    235  1.51        pk 	 * valid contents. Also, B_ERROR is not set, otherwise
    236  1.51        pk 	 * getblk() would not have returned them.
    237  1.51        pk 	 */
    238  1.57   mycroft 	if (ISSET(bp->b_flags, B_DONE|B_DELWRI))
    239  1.51        pk 		return (0);
    240  1.51        pk 
    241  1.51        pk 	/*
    242  1.51        pk 	 * Otherwise, we had to start a read for it; wait until
    243  1.51        pk 	 * it's valid and return the result.
    244  1.51        pk 	 */
    245  1.31       cgd 	return (biowait(bp));
    246  1.31       cgd }
    247  1.31       cgd 
    248  1.31       cgd /*
    249  1.31       cgd  * Read-ahead multiple disk blocks. The first is sync, the rest async.
    250  1.31       cgd  * Trivial modification to the breada algorithm presented in Bach (p.55).
    251  1.31       cgd  */
    252  1.40  christos int
    253  1.31       cgd breadn(vp, blkno, size, rablks, rasizes, nrablks, cred, bpp)
    254  1.31       cgd 	struct vnode *vp;
    255  1.31       cgd 	daddr_t blkno; int size;
    256  1.31       cgd 	daddr_t rablks[]; int rasizes[];
    257  1.31       cgd 	int nrablks;
    258  1.31       cgd 	struct ucred *cred;
    259  1.31       cgd 	struct buf **bpp;
    260  1.31       cgd {
    261  1.66  augustss 	struct buf *bp;
    262  1.31       cgd 	int i;
    263  1.31       cgd 
    264  1.34   mycroft 	bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
    265  1.31       cgd 
    266  1.31       cgd 	/*
    267  1.31       cgd 	 * For each of the read-ahead blocks, start a read, if necessary.
    268  1.31       cgd 	 */
    269  1.31       cgd 	for (i = 0; i < nrablks; i++) {
    270  1.31       cgd 		/* If it's in the cache, just go on to next one. */
    271  1.31       cgd 		if (incore(vp, rablks[i]))
    272  1.31       cgd 			continue;
    273  1.31       cgd 
    274  1.31       cgd 		/* Get a buffer for the read-ahead block */
    275  1.34   mycroft 		(void) bio_doread(vp, rablks[i], rasizes[i], cred, B_ASYNC);
    276  1.31       cgd 	}
    277  1.31       cgd 
    278  1.51        pk 	/*
    279  1.51        pk 	 * Delayed write buffers are found in the cache and have
    280  1.51        pk 	 * valid contents. Also, B_ERROR is not set, otherwise
    281  1.51        pk 	 * getblk() would not have returned them.
    282  1.51        pk 	 */
    283  1.57   mycroft 	if (ISSET(bp->b_flags, B_DONE|B_DELWRI))
    284  1.57   mycroft 		return (0);
    285  1.51        pk 
    286  1.51        pk 	/*
    287  1.51        pk 	 * Otherwise, we had to start a read for it; wait until
    288  1.51        pk 	 * it's valid and return the result.
    289  1.51        pk 	 */
    290  1.31       cgd 	return (biowait(bp));
    291  1.31       cgd }
    292  1.31       cgd 
    293  1.31       cgd /*
    294  1.31       cgd  * Read with single-block read-ahead.  Defined in Bach (p.55), but
    295  1.31       cgd  * implemented as a call to breadn().
    296  1.31       cgd  * XXX for compatibility with old file systems.
    297  1.31       cgd  */
    298  1.40  christos int
    299  1.31       cgd breada(vp, blkno, size, rablkno, rabsize, cred, bpp)
    300  1.31       cgd 	struct vnode *vp;
    301  1.31       cgd 	daddr_t blkno; int size;
    302  1.31       cgd 	daddr_t rablkno; int rabsize;
    303  1.31       cgd 	struct ucred *cred;
    304  1.31       cgd 	struct buf **bpp;
    305  1.31       cgd {
    306  1.34   mycroft 
    307  1.31       cgd 	return (breadn(vp, blkno, size, &rablkno, &rabsize, 1, cred, bpp));
    308  1.31       cgd }
    309  1.31       cgd 
    310  1.31       cgd /*
    311  1.31       cgd  * Block write.  Described in Bach (p.56)
    312  1.31       cgd  */
    313  1.40  christos int
    314  1.31       cgd bwrite(bp)
    315  1.31       cgd 	struct buf *bp;
    316  1.31       cgd {
    317  1.44        pk 	int rv, sync, wasdelayed, s;
    318  1.49       cgd 	struct proc *p = (curproc != NULL ? curproc : &proc0);	/* XXX */
    319  1.59      fvdl 	struct vnode *vp;
    320  1.59      fvdl 	struct mount *mp;
    321  1.31       cgd 
    322  1.76       chs 	vp = bp->b_vp;
    323  1.76       chs 	if (vp != NULL) {
    324  1.76       chs 		if (vp->v_type == VBLK)
    325  1.76       chs 			mp = vp->v_specmountpoint;
    326  1.76       chs 		else
    327  1.76       chs 			mp = vp->v_mount;
    328  1.76       chs 	} else {
    329  1.76       chs 		mp = NULL;
    330  1.76       chs 	}
    331  1.76       chs 
    332  1.38       cgd 	/*
    333  1.38       cgd 	 * Remember buffer type, to switch on it later.  If the write was
    334  1.38       cgd 	 * synchronous, but the file system was mounted with MNT_ASYNC,
    335  1.38       cgd 	 * convert it to a delayed write.
    336  1.38       cgd 	 * XXX note that this relies on delayed tape writes being converted
    337  1.38       cgd 	 * to async, not sync writes (which is safe, but ugly).
    338  1.38       cgd 	 */
    339  1.31       cgd 	sync = !ISSET(bp->b_flags, B_ASYNC);
    340  1.76       chs 	if (sync && mp != NULL && ISSET(mp->mnt_flag, MNT_ASYNC)) {
    341  1.37       cgd 		bdwrite(bp);
    342  1.37       cgd 		return (0);
    343  1.37       cgd 	}
    344  1.46   mycroft 
    345  1.59      fvdl 	/*
    346  1.59      fvdl 	 * Collect statistics on synchronous and asynchronous writes.
    347  1.59      fvdl 	 * Writes to block devices are charged to their associated
    348  1.59      fvdl 	 * filesystem (if any).
    349  1.59      fvdl 	 */
    350  1.76       chs 	if (mp != NULL) {
    351  1.76       chs 		if (sync)
    352  1.76       chs 			mp->mnt_stat.f_syncwrites++;
    353  1.59      fvdl 		else
    354  1.76       chs 			mp->mnt_stat.f_asyncwrites++;
    355  1.59      fvdl 	}
    356  1.59      fvdl 
    357  1.31       cgd 	wasdelayed = ISSET(bp->b_flags, B_DELWRI);
    358  1.31       cgd 
    359  1.44        pk 	s = splbio();
    360  1.46   mycroft 
    361  1.60      fvdl 	CLR(bp->b_flags, (B_READ | B_DONE | B_ERROR | B_DELWRI));
    362  1.60      fvdl 
    363  1.46   mycroft 	/*
    364  1.46   mycroft 	 * Pay for the I/O operation and make sure the buf is on the correct
    365  1.46   mycroft 	 * vnode queue.
    366  1.46   mycroft 	 */
    367  1.46   mycroft 	if (wasdelayed)
    368  1.46   mycroft 		reassignbuf(bp, bp->b_vp);
    369  1.46   mycroft 	else
    370  1.49       cgd 		p->p_stats->p_ru.ru_oublock++;
    371  1.32   mycroft 
    372  1.31       cgd 	/* Initiate disk write.  Make sure the appropriate party is charged. */
    373  1.44        pk 	bp->b_vp->v_numoutput++;
    374  1.44        pk 	splx(s);
    375  1.46   mycroft 
    376  1.31       cgd 	VOP_STRATEGY(bp);
    377  1.31       cgd 
    378  1.34   mycroft 	if (sync) {
    379  1.46   mycroft 		/* If I/O was synchronous, wait for it to complete. */
    380  1.31       cgd 		rv = biowait(bp);
    381  1.31       cgd 
    382  1.34   mycroft 		/* Release the buffer. */
    383  1.31       cgd 		brelse(bp);
    384  1.34   mycroft 
    385  1.34   mycroft 		return (rv);
    386  1.34   mycroft 	} else {
    387  1.34   mycroft 		return (0);
    388  1.31       cgd 	}
    389  1.31       cgd }
    390  1.31       cgd 
    391  1.31       cgd int
    392  1.40  christos vn_bwrite(v)
    393  1.40  christos 	void *v;
    394  1.31       cgd {
    395  1.40  christos 	struct vop_bwrite_args *ap = v;
    396  1.34   mycroft 
    397  1.31       cgd 	return (bwrite(ap->a_bp));
    398  1.31       cgd }
    399  1.31       cgd 
    400  1.31       cgd /*
    401  1.31       cgd  * Delayed write.
    402  1.31       cgd  *
    403  1.31       cgd  * The buffer is marked dirty, but is not queued for I/O.
    404  1.31       cgd  * This routine should be used when the buffer is expected
    405  1.31       cgd  * to be modified again soon, typically a small write that
    406  1.31       cgd  * partially fills a buffer.
    407  1.31       cgd  *
    408  1.31       cgd  * NB: magnetic tapes cannot be delayed; they must be
    409  1.31       cgd  * written in the order that the writes are requested.
    410  1.31       cgd  *
    411  1.31       cgd  * Described in Leffler, et al. (pp. 208-213).
    412  1.31       cgd  */
    413  1.31       cgd void
    414  1.31       cgd bdwrite(bp)
    415  1.31       cgd 	struct buf *bp;
    416  1.31       cgd {
    417  1.60      fvdl 	struct proc *p = (curproc != NULL ? curproc : &proc0);	/* XXX */
    418  1.45        pk 	int s;
    419  1.31       cgd 
    420  1.46   mycroft 	/* If this is a tape block, write the block now. */
    421  1.52        pk 	/* XXX NOTE: the memory filesystem usurpes major device */
    422  1.52        pk 	/* XXX       number 255, which is a bad idea.		*/
    423  1.52        pk 	if (bp->b_dev != NODEV &&
    424  1.52        pk 	    major(bp->b_dev) != 255 &&	/* XXX - MFS buffers! */
    425  1.52        pk 	    bdevsw[major(bp->b_dev)].d_type == D_TAPE) {
    426  1.46   mycroft 		bawrite(bp);
    427  1.46   mycroft 		return;
    428  1.46   mycroft 	}
    429  1.46   mycroft 
    430  1.31       cgd 	/*
    431  1.31       cgd 	 * If the block hasn't been seen before:
    432  1.31       cgd 	 *	(1) Mark it as having been seen,
    433  1.45        pk 	 *	(2) Charge for the write,
    434  1.45        pk 	 *	(3) Make sure it's on its vnode's correct block list.
    435  1.31       cgd 	 */
    436  1.60      fvdl 	s = splbio();
    437  1.60      fvdl 
    438  1.31       cgd 	if (!ISSET(bp->b_flags, B_DELWRI)) {
    439  1.31       cgd 		SET(bp->b_flags, B_DELWRI);
    440  1.49       cgd 		p->p_stats->p_ru.ru_oublock++;
    441  1.31       cgd 		reassignbuf(bp, bp->b_vp);
    442  1.31       cgd 	}
    443  1.31       cgd 
    444  1.31       cgd 	/* Otherwise, the "write" is done, so mark and release the buffer. */
    445  1.57   mycroft 	CLR(bp->b_flags, B_NEEDCOMMIT|B_DONE);
    446  1.60      fvdl 	splx(s);
    447  1.60      fvdl 
    448  1.31       cgd 	brelse(bp);
    449  1.31       cgd }
    450  1.31       cgd 
    451  1.31       cgd /*
    452  1.31       cgd  * Asynchronous block write; just an asynchronous bwrite().
    453  1.31       cgd  */
    454  1.31       cgd void
    455  1.31       cgd bawrite(bp)
    456  1.31       cgd 	struct buf *bp;
    457  1.31       cgd {
    458  1.31       cgd 
    459  1.31       cgd 	SET(bp->b_flags, B_ASYNC);
    460  1.31       cgd 	VOP_BWRITE(bp);
    461  1.31       cgd }
    462  1.31       cgd 
    463  1.31       cgd /*
    464  1.63   thorpej  * Ordered block write; asynchronous, but I/O will occur in order queued.
    465  1.63   thorpej  */
    466  1.63   thorpej void
    467  1.63   thorpej bowrite(bp)
    468  1.63   thorpej 	struct buf *bp;
    469  1.63   thorpej {
    470  1.63   thorpej 
    471  1.63   thorpej 	SET(bp->b_flags, B_ASYNC | B_ORDERED);
    472  1.63   thorpej 	VOP_BWRITE(bp);
    473  1.63   thorpej }
    474  1.63   thorpej 
    475  1.63   thorpej /*
    476  1.59      fvdl  * Same as first half of bdwrite, mark buffer dirty, but do not release it.
    477  1.59      fvdl  */
    478  1.59      fvdl void
    479  1.59      fvdl bdirty(bp)
    480  1.59      fvdl 	struct buf *bp;
    481  1.59      fvdl {
    482  1.59      fvdl 	struct proc *p = (curproc != NULL ? curproc : &proc0);	/* XXX */
    483  1.59      fvdl 	int s;
    484  1.59      fvdl 
    485  1.60      fvdl 	s = splbio();
    486  1.61      fvdl 
    487  1.61      fvdl 	CLR(bp->b_flags, B_AGE);
    488  1.60      fvdl 
    489  1.59      fvdl 	if (!ISSET(bp->b_flags, B_DELWRI)) {
    490  1.59      fvdl 		SET(bp->b_flags, B_DELWRI);
    491  1.59      fvdl 		p->p_stats->p_ru.ru_oublock++;
    492  1.59      fvdl 		reassignbuf(bp, bp->b_vp);
    493  1.59      fvdl 	}
    494  1.60      fvdl 
    495  1.60      fvdl 	splx(s);
    496  1.59      fvdl }
    497  1.59      fvdl 
    498  1.59      fvdl /*
    499  1.31       cgd  * Release a buffer on to the free lists.
    500  1.31       cgd  * Described in Bach (p. 46).
    501  1.31       cgd  */
    502  1.31       cgd void
    503  1.31       cgd brelse(bp)
    504  1.31       cgd 	struct buf *bp;
    505  1.31       cgd {
    506  1.31       cgd 	struct bqueues *bufq;
    507  1.31       cgd 	int s;
    508  1.31       cgd 
    509  1.73       chs 	KASSERT(ISSET(bp->b_flags, B_BUSY));
    510  1.73       chs 
    511  1.31       cgd 	/* Wake up any processes waiting for any buffer to become free. */
    512  1.31       cgd 	if (needbuffer) {
    513  1.31       cgd 		needbuffer = 0;
    514  1.31       cgd 		wakeup(&needbuffer);
    515  1.31       cgd 	}
    516  1.31       cgd 
    517  1.60      fvdl 	/* Block disk interrupts. */
    518  1.60      fvdl 	s = splbio();
    519  1.60      fvdl 
    520  1.31       cgd 	/* Wake up any proceeses waiting for _this_ buffer to become free. */
    521  1.31       cgd 	if (ISSET(bp->b_flags, B_WANTED)) {
    522  1.57   mycroft 		CLR(bp->b_flags, B_WANTED|B_AGE);
    523  1.31       cgd 		wakeup(bp);
    524  1.31       cgd 	}
    525  1.31       cgd 
    526  1.31       cgd 	/*
    527  1.31       cgd 	 * Determine which queue the buffer should be on, then put it there.
    528  1.31       cgd 	 */
    529  1.31       cgd 
    530  1.31       cgd 	/* If it's locked, don't report an error; try again later. */
    531  1.31       cgd 	if (ISSET(bp->b_flags, (B_LOCKED|B_ERROR)) == (B_LOCKED|B_ERROR))
    532  1.31       cgd 		CLR(bp->b_flags, B_ERROR);
    533  1.31       cgd 
    534  1.31       cgd 	/* If it's not cacheable, or an error, mark it invalid. */
    535  1.31       cgd 	if (ISSET(bp->b_flags, (B_NOCACHE|B_ERROR)))
    536  1.31       cgd 		SET(bp->b_flags, B_INVAL);
    537  1.31       cgd 
    538  1.50   mycroft 	if (ISSET(bp->b_flags, B_VFLUSH)) {
    539  1.50   mycroft 		/*
    540  1.50   mycroft 		 * This is a delayed write buffer that was just flushed to
    541  1.50   mycroft 		 * disk.  It is still on the LRU queue.  If it's become
    542  1.50   mycroft 		 * invalid, then we need to move it to a different queue;
    543  1.50   mycroft 		 * otherwise leave it in its current position.
    544  1.50   mycroft 		 */
    545  1.50   mycroft 		CLR(bp->b_flags, B_VFLUSH);
    546  1.50   mycroft 		if (!ISSET(bp->b_flags, B_ERROR|B_INVAL|B_LOCKED|B_AGE))
    547  1.50   mycroft 			goto already_queued;
    548  1.50   mycroft 		else
    549  1.50   mycroft 			bremfree(bp);
    550  1.50   mycroft 	}
    551  1.50   mycroft 
    552  1.31       cgd 	if ((bp->b_bufsize <= 0) || ISSET(bp->b_flags, B_INVAL)) {
    553  1.31       cgd 		/*
    554  1.31       cgd 		 * If it's invalid or empty, dissociate it from its vnode
    555  1.31       cgd 		 * and put on the head of the appropriate queue.
    556  1.31       cgd 		 */
    557  1.59      fvdl 		if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_deallocate)
    558  1.59      fvdl 			(*bioops.io_deallocate)(bp);
    559  1.59      fvdl 		CLR(bp->b_flags, B_DONE|B_DELWRI);
    560  1.59      fvdl 		if (bp->b_vp) {
    561  1.59      fvdl 			reassignbuf(bp, bp->b_vp);
    562  1.31       cgd 			brelvp(bp);
    563  1.59      fvdl 		}
    564  1.31       cgd 		if (bp->b_bufsize <= 0)
    565  1.31       cgd 			/* no data */
    566  1.31       cgd 			bufq = &bufqueues[BQ_EMPTY];
    567  1.31       cgd 		else
    568  1.31       cgd 			/* invalid data */
    569  1.31       cgd 			bufq = &bufqueues[BQ_AGE];
    570  1.31       cgd 		binsheadfree(bp, bufq);
    571  1.31       cgd 	} else {
    572  1.31       cgd 		/*
    573  1.31       cgd 		 * It has valid data.  Put it on the end of the appropriate
    574  1.31       cgd 		 * queue, so that it'll stick around for as long as possible.
    575  1.67      fvdl 		 * If buf is AGE, but has dependencies, must put it on last
    576  1.67      fvdl 		 * bufqueue to be scanned, ie LRU. This protects against the
    577  1.67      fvdl 		 * livelock where BQ_AGE only has buffers with dependencies,
    578  1.67      fvdl 		 * and we thus never get to the dependent buffers in BQ_LRU.
    579  1.31       cgd 		 */
    580  1.31       cgd 		if (ISSET(bp->b_flags, B_LOCKED))
    581  1.31       cgd 			/* locked in core */
    582  1.31       cgd 			bufq = &bufqueues[BQ_LOCKED];
    583  1.67      fvdl 		else if (!ISSET(bp->b_flags, B_AGE))
    584  1.31       cgd 			/* valid data */
    585  1.31       cgd 			bufq = &bufqueues[BQ_LRU];
    586  1.67      fvdl 		else {
    587  1.67      fvdl 			/* stale but valid data */
    588  1.67      fvdl 			int has_deps;
    589  1.67      fvdl 
    590  1.67      fvdl 			if (LIST_FIRST(&bp->b_dep) != NULL &&
    591  1.67      fvdl 			    bioops.io_countdeps)
    592  1.67      fvdl 				has_deps = (*bioops.io_countdeps)(bp, 0);
    593  1.67      fvdl 			else
    594  1.67      fvdl 				has_deps = 0;
    595  1.67      fvdl 			bufq = has_deps ? &bufqueues[BQ_LRU] :
    596  1.67      fvdl 			    &bufqueues[BQ_AGE];
    597  1.67      fvdl 		}
    598  1.31       cgd 		binstailfree(bp, bufq);
    599  1.31       cgd 	}
    600  1.31       cgd 
    601  1.50   mycroft already_queued:
    602  1.31       cgd 	/* Unlock the buffer. */
    603  1.63   thorpej 	CLR(bp->b_flags, B_AGE|B_ASYNC|B_BUSY|B_NOCACHE|B_ORDERED);
    604  1.73       chs 	SET(bp->b_flags, B_CACHE);
    605  1.31       cgd 
    606  1.31       cgd 	/* Allow disk interrupts. */
    607  1.31       cgd 	splx(s);
    608  1.31       cgd }
    609  1.31       cgd 
    610  1.31       cgd /*
    611  1.31       cgd  * Determine if a block is in the cache.
    612  1.31       cgd  * Just look on what would be its hash chain.  If it's there, return
    613  1.31       cgd  * a pointer to it, unless it's marked invalid.  If it's marked invalid,
    614  1.31       cgd  * we normally don't return the buffer, unless the caller explicitly
    615  1.31       cgd  * wants us to.
    616  1.31       cgd  */
    617  1.31       cgd struct buf *
    618  1.31       cgd incore(vp, blkno)
    619  1.31       cgd 	struct vnode *vp;
    620  1.31       cgd 	daddr_t blkno;
    621  1.31       cgd {
    622  1.31       cgd 	struct buf *bp;
    623  1.31       cgd 
    624  1.31       cgd 	bp = BUFHASH(vp, blkno)->lh_first;
    625  1.31       cgd 
    626  1.31       cgd 	/* Search hash chain */
    627  1.31       cgd 	for (; bp != NULL; bp = bp->b_hash.le_next) {
    628  1.31       cgd 		if (bp->b_lblkno == blkno && bp->b_vp == vp &&
    629  1.31       cgd 		    !ISSET(bp->b_flags, B_INVAL))
    630  1.31       cgd 		return (bp);
    631  1.31       cgd 	}
    632  1.31       cgd 
    633  1.73       chs 	return (NULL);
    634  1.31       cgd }
    635  1.31       cgd 
    636  1.31       cgd /*
    637  1.31       cgd  * Get a block of requested size that is associated with
    638  1.31       cgd  * a given vnode and block offset. If it is found in the
    639  1.31       cgd  * block cache, mark it as having been found, make it busy
    640  1.31       cgd  * and return it. Otherwise, return an empty block of the
    641  1.31       cgd  * correct size. It is up to the caller to insure that the
    642  1.31       cgd  * cached blocks be of the correct size.
    643  1.31       cgd  */
    644  1.31       cgd struct buf *
    645  1.31       cgd getblk(vp, blkno, size, slpflag, slptimeo)
    646  1.66  augustss 	struct vnode *vp;
    647  1.31       cgd 	daddr_t blkno;
    648  1.31       cgd 	int size, slpflag, slptimeo;
    649  1.31       cgd {
    650  1.31       cgd 	struct buf *bp;
    651  1.31       cgd 	int s, err;
    652  1.31       cgd 
    653  1.39       cgd start:
    654  1.73       chs 	bp = incore(vp, blkno);
    655  1.73       chs 	if (bp != NULL) {
    656  1.39       cgd 		s = splbio();
    657  1.31       cgd 		if (ISSET(bp->b_flags, B_BUSY)) {
    658  1.73       chs 			if (curproc == uvm.pagedaemon_proc) {
    659  1.73       chs 				splx(s);
    660  1.73       chs 				return NULL;
    661  1.73       chs 			}
    662  1.31       cgd 			SET(bp->b_flags, B_WANTED);
    663  1.31       cgd 			err = tsleep(bp, slpflag | (PRIBIO + 1), "getblk",
    664  1.73       chs 				     slptimeo);
    665  1.31       cgd 			splx(s);
    666  1.31       cgd 			if (err)
    667  1.31       cgd 				return (NULL);
    668  1.31       cgd 			goto start;
    669  1.31       cgd 		}
    670  1.57   mycroft #ifdef DIAGNOSTIC
    671  1.73       chs 		if (ISSET(bp->b_flags, B_DONE|B_DELWRI) && bp->b_bcount < size)
    672  1.73       chs 			panic("getblk: block size invariant failed");
    673  1.57   mycroft #endif
    674  1.73       chs 		SET(bp->b_flags, B_BUSY);
    675  1.73       chs 		bremfree(bp);
    676  1.39       cgd 		splx(s);
    677  1.73       chs 	} else {
    678  1.31       cgd 		if ((bp = getnewbuf(slpflag, slptimeo)) == NULL)
    679  1.31       cgd 			goto start;
    680  1.73       chs 
    681  1.73       chs 		binshash(bp, BUFHASH(vp, blkno));
    682  1.64   thorpej 		bp->b_blkno = bp->b_lblkno = bp->b_rawblkno = blkno;
    683  1.31       cgd 		s = splbio();
    684  1.31       cgd 		bgetvp(vp, bp);
    685  1.31       cgd 		splx(s);
    686  1.31       cgd 	}
    687  1.39       cgd 	allocbuf(bp, size);
    688  1.31       cgd 	return (bp);
    689  1.31       cgd }
    690  1.31       cgd 
    691  1.31       cgd /*
    692  1.31       cgd  * Get an empty, disassociated buffer of given size.
    693  1.31       cgd  */
    694  1.31       cgd struct buf *
    695  1.31       cgd geteblk(size)
    696  1.31       cgd 	int size;
    697  1.31       cgd {
    698  1.31       cgd 	struct buf *bp;
    699  1.31       cgd 
    700  1.31       cgd 	while ((bp = getnewbuf(0, 0)) == 0)
    701  1.31       cgd 		;
    702  1.31       cgd 	SET(bp->b_flags, B_INVAL);
    703  1.31       cgd 	binshash(bp, &invalhash);
    704  1.31       cgd 	allocbuf(bp, size);
    705  1.31       cgd 	return (bp);
    706  1.31       cgd }
    707  1.31       cgd 
    708  1.31       cgd /*
    709  1.31       cgd  * Expand or contract the actual memory allocated to a buffer.
    710  1.31       cgd  *
    711  1.31       cgd  * If the buffer shrinks, data is lost, so it's up to the
    712  1.31       cgd  * caller to have written it out *first*; this routine will not
    713  1.31       cgd  * start a write.  If the buffer grows, it's the callers
    714  1.31       cgd  * responsibility to fill out the buffer's additional contents.
    715  1.31       cgd  */
    716  1.40  christos void
    717  1.31       cgd allocbuf(bp, size)
    718  1.31       cgd 	struct buf *bp;
    719  1.31       cgd 	int size;
    720  1.31       cgd {
    721  1.73       chs 	struct buf *nbp;
    722  1.73       chs 	vsize_t desired_size;
    723  1.73       chs 	int s;
    724  1.31       cgd 
    725  1.69       chs 	desired_size = round_page((vsize_t)size);
    726  1.31       cgd 	if (desired_size > MAXBSIZE)
    727  1.31       cgd 		panic("allocbuf: buffer larger than MAXBSIZE requested");
    728  1.31       cgd 
    729  1.31       cgd 	if (bp->b_bufsize == desired_size)
    730  1.31       cgd 		goto out;
    731  1.31       cgd 
    732  1.31       cgd 	/*
    733  1.31       cgd 	 * If the buffer is smaller than the desired size, we need to snarf
    734  1.31       cgd 	 * it from other buffers.  Get buffers (via getnewbuf()), and
    735  1.31       cgd 	 * steal their pages.
    736  1.31       cgd 	 */
    737  1.31       cgd 	while (bp->b_bufsize < desired_size) {
    738  1.31       cgd 		int amt;
    739  1.31       cgd 
    740  1.31       cgd 		/* find a buffer */
    741  1.31       cgd 		while ((nbp = getnewbuf(0, 0)) == NULL)
    742  1.31       cgd 			;
    743  1.73       chs 
    744  1.34   mycroft 		SET(nbp->b_flags, B_INVAL);
    745  1.34   mycroft 		binshash(nbp, &invalhash);
    746  1.31       cgd 
    747  1.31       cgd 		/* and steal its pages, up to the amount we need */
    748  1.31       cgd 		amt = min(nbp->b_bufsize, (desired_size - bp->b_bufsize));
    749  1.31       cgd 		pagemove((nbp->b_data + nbp->b_bufsize - amt),
    750  1.40  christos 			 bp->b_data + bp->b_bufsize, amt);
    751  1.31       cgd 		bp->b_bufsize += amt;
    752  1.31       cgd 		nbp->b_bufsize -= amt;
    753  1.31       cgd 
    754  1.31       cgd 		/* reduce transfer count if we stole some data */
    755  1.31       cgd 		if (nbp->b_bcount > nbp->b_bufsize)
    756  1.31       cgd 			nbp->b_bcount = nbp->b_bufsize;
    757  1.31       cgd 
    758  1.31       cgd #ifdef DIAGNOSTIC
    759  1.31       cgd 		if (nbp->b_bufsize < 0)
    760  1.31       cgd 			panic("allocbuf: negative bufsize");
    761  1.31       cgd #endif
    762  1.34   mycroft 
    763  1.31       cgd 		brelse(nbp);
    764  1.31       cgd 	}
    765  1.31       cgd 
    766  1.31       cgd 	/*
    767  1.31       cgd 	 * If we want a buffer smaller than the current size,
    768  1.31       cgd 	 * shrink this buffer.  Grab a buf head from the EMPTY queue,
    769  1.31       cgd 	 * move a page onto it, and put it on front of the AGE queue.
    770  1.31       cgd 	 * If there are no free buffer headers, leave the buffer alone.
    771  1.31       cgd 	 */
    772  1.31       cgd 	if (bp->b_bufsize > desired_size) {
    773  1.31       cgd 		s = splbio();
    774  1.31       cgd 		if ((nbp = bufqueues[BQ_EMPTY].tqh_first) == NULL) {
    775  1.31       cgd 			/* No free buffer head */
    776  1.31       cgd 			splx(s);
    777  1.31       cgd 			goto out;
    778  1.31       cgd 		}
    779  1.31       cgd 		bremfree(nbp);
    780  1.31       cgd 		SET(nbp->b_flags, B_BUSY);
    781  1.31       cgd 		splx(s);
    782  1.31       cgd 
    783  1.31       cgd 		/* move the page to it and note this change */
    784  1.31       cgd 		pagemove(bp->b_data + desired_size,
    785  1.31       cgd 		    nbp->b_data, bp->b_bufsize - desired_size);
    786  1.31       cgd 		nbp->b_bufsize = bp->b_bufsize - desired_size;
    787  1.31       cgd 		bp->b_bufsize = desired_size;
    788  1.31       cgd 		nbp->b_bcount = 0;
    789  1.31       cgd 		SET(nbp->b_flags, B_INVAL);
    790  1.31       cgd 
    791  1.31       cgd 		/* release the newly-filled buffer and leave */
    792  1.31       cgd 		brelse(nbp);
    793  1.31       cgd 	}
    794  1.31       cgd 
    795  1.31       cgd out:
    796  1.31       cgd 	bp->b_bcount = size;
    797  1.31       cgd }
    798  1.31       cgd 
    799  1.31       cgd /*
    800  1.31       cgd  * Find a buffer which is available for use.
    801  1.31       cgd  * Select something from a free list.
    802  1.31       cgd  * Preference is to AGE list, then LRU list.
    803  1.31       cgd  */
    804  1.31       cgd struct buf *
    805  1.31       cgd getnewbuf(slpflag, slptimeo)
    806  1.31       cgd 	int slpflag, slptimeo;
    807  1.31       cgd {
    808  1.66  augustss 	struct buf *bp;
    809  1.31       cgd 	int s;
    810  1.31       cgd 
    811  1.31       cgd start:
    812  1.31       cgd 	s = splbio();
    813  1.31       cgd 	if ((bp = bufqueues[BQ_AGE].tqh_first) != NULL ||
    814  1.31       cgd 	    (bp = bufqueues[BQ_LRU].tqh_first) != NULL) {
    815  1.31       cgd 		bremfree(bp);
    816  1.31       cgd 	} else {
    817  1.31       cgd 		/* wait for a free buffer of any kind */
    818  1.31       cgd 		needbuffer = 1;
    819  1.31       cgd 		tsleep(&needbuffer, slpflag|(PRIBIO+1), "getnewbuf", slptimeo);
    820  1.31       cgd 		splx(s);
    821  1.73       chs 		return (NULL);
    822  1.31       cgd 	}
    823  1.31       cgd 
    824  1.50   mycroft 	if (ISSET(bp->b_flags, B_VFLUSH)) {
    825  1.50   mycroft 		/*
    826  1.50   mycroft 		 * This is a delayed write buffer being flushed to disk.  Make
    827  1.50   mycroft 		 * sure it gets aged out of the queue when it's finished, and
    828  1.50   mycroft 		 * leave it off the LRU queue.
    829  1.50   mycroft 		 */
    830  1.50   mycroft 		CLR(bp->b_flags, B_VFLUSH);
    831  1.50   mycroft 		SET(bp->b_flags, B_AGE);
    832  1.50   mycroft 		splx(s);
    833  1.50   mycroft 		goto start;
    834  1.50   mycroft 	}
    835  1.50   mycroft 
    836  1.31       cgd 	/* Buffer is no longer on free lists. */
    837  1.31       cgd 	SET(bp->b_flags, B_BUSY);
    838  1.31       cgd 
    839  1.75       chs 	/*
    840  1.75       chs 	 * If buffer was a delayed write, start it and return NULL
    841  1.75       chs 	 * (since we might sleep while starting the write).
    842  1.75       chs 	 */
    843  1.31       cgd 	if (ISSET(bp->b_flags, B_DELWRI)) {
    844  1.39       cgd 		splx(s);
    845  1.50   mycroft 		/*
    846  1.50   mycroft 		 * This buffer has gone through the LRU, so make sure it gets
    847  1.50   mycroft 		 * reused ASAP.
    848  1.50   mycroft 		 */
    849  1.50   mycroft 		SET(bp->b_flags, B_AGE);
    850  1.50   mycroft 		bawrite(bp);
    851  1.75       chs 		return (NULL);
    852  1.31       cgd 	}
    853  1.31       cgd 
    854  1.31       cgd 	/* disassociate us from our vnode, if we had one... */
    855  1.31       cgd 	if (bp->b_vp)
    856  1.31       cgd 		brelvp(bp);
    857  1.31       cgd 	splx(s);
    858  1.31       cgd 
    859  1.59      fvdl 	if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_deallocate)
    860  1.59      fvdl 		(*bioops.io_deallocate)(bp);
    861  1.59      fvdl 
    862  1.31       cgd 	/* clear out various other fields */
    863  1.31       cgd 	bp->b_flags = B_BUSY;
    864  1.31       cgd 	bp->b_dev = NODEV;
    865  1.64   thorpej 	bp->b_blkno = bp->b_lblkno = bp->b_rawblkno = 0;
    866  1.31       cgd 	bp->b_iodone = 0;
    867  1.31       cgd 	bp->b_error = 0;
    868  1.31       cgd 	bp->b_resid = 0;
    869  1.31       cgd 	bp->b_bcount = 0;
    870  1.31       cgd 
    871  1.34   mycroft 	bremhash(bp);
    872  1.31       cgd 	return (bp);
    873  1.31       cgd }
    874  1.31       cgd 
    875  1.31       cgd /*
    876  1.31       cgd  * Wait for operations on the buffer to complete.
    877  1.31       cgd  * When they do, extract and return the I/O's error value.
    878  1.31       cgd  */
    879  1.31       cgd int
    880  1.31       cgd biowait(bp)
    881  1.31       cgd 	struct buf *bp;
    882  1.31       cgd {
    883  1.31       cgd 	int s;
    884  1.59      fvdl 
    885  1.31       cgd 	s = splbio();
    886  1.31       cgd 	while (!ISSET(bp->b_flags, B_DONE))
    887  1.31       cgd 		tsleep(bp, PRIBIO + 1, "biowait", 0);
    888  1.31       cgd 	splx(s);
    889  1.31       cgd 
    890  1.31       cgd 	/* check for interruption of I/O (e.g. via NFS), then errors. */
    891  1.31       cgd 	if (ISSET(bp->b_flags, B_EINTR)) {
    892  1.31       cgd 		CLR(bp->b_flags, B_EINTR);
    893  1.31       cgd 		return (EINTR);
    894  1.31       cgd 	} else if (ISSET(bp->b_flags, B_ERROR))
    895  1.31       cgd 		return (bp->b_error ? bp->b_error : EIO);
    896  1.31       cgd 	else
    897  1.31       cgd 		return (0);
    898  1.31       cgd }
    899  1.31       cgd 
    900  1.31       cgd /*
    901  1.31       cgd  * Mark I/O complete on a buffer.
    902  1.31       cgd  *
    903  1.31       cgd  * If a callback has been requested, e.g. the pageout
    904  1.31       cgd  * daemon, do so. Otherwise, awaken waiting processes.
    905  1.31       cgd  *
    906  1.31       cgd  * [ Leffler, et al., says on p.247:
    907  1.31       cgd  *	"This routine wakes up the blocked process, frees the buffer
    908  1.31       cgd  *	for an asynchronous write, or, for a request by the pagedaemon
    909  1.31       cgd  *	process, invokes a procedure specified in the buffer structure" ]
    910  1.31       cgd  *
    911  1.31       cgd  * In real life, the pagedaemon (or other system processes) wants
    912  1.31       cgd  * to do async stuff to, and doesn't want the buffer brelse()'d.
    913  1.31       cgd  * (for swap pager, that puts swap buffers on the free lists (!!!),
    914  1.31       cgd  * for the vn device, that puts malloc'd buffers on the free lists!)
    915  1.31       cgd  */
    916  1.31       cgd void
    917  1.31       cgd biodone(bp)
    918  1.31       cgd 	struct buf *bp;
    919  1.31       cgd {
    920  1.60      fvdl 	int s = splbio();
    921  1.60      fvdl 
    922  1.31       cgd 	if (ISSET(bp->b_flags, B_DONE))
    923  1.31       cgd 		panic("biodone already");
    924  1.31       cgd 	SET(bp->b_flags, B_DONE);		/* note that it's done */
    925  1.31       cgd 
    926  1.59      fvdl 	if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_complete)
    927  1.59      fvdl 		(*bioops.io_complete)(bp);
    928  1.59      fvdl 
    929  1.31       cgd 	if (!ISSET(bp->b_flags, B_READ))	/* wake up reader */
    930  1.31       cgd 		vwakeup(bp);
    931  1.31       cgd 
    932  1.31       cgd 	if (ISSET(bp->b_flags, B_CALL)) {	/* if necessary, call out */
    933  1.31       cgd 		CLR(bp->b_flags, B_CALL);	/* but note callout done */
    934  1.31       cgd 		(*bp->b_iodone)(bp);
    935  1.59      fvdl 	} else {
    936  1.59      fvdl 		if (ISSET(bp->b_flags, B_ASYNC))	/* if async, release */
    937  1.59      fvdl 			brelse(bp);
    938  1.59      fvdl 		else {				/* or just wakeup the buffer */
    939  1.59      fvdl 			CLR(bp->b_flags, B_WANTED);
    940  1.59      fvdl 			wakeup(bp);
    941  1.59      fvdl 		}
    942  1.31       cgd 	}
    943  1.60      fvdl 
    944  1.60      fvdl 	splx(s);
    945  1.31       cgd }
    946  1.31       cgd 
    947  1.31       cgd /*
    948  1.31       cgd  * Return a count of buffers on the "locked" queue.
    949  1.31       cgd  */
    950  1.31       cgd int
    951  1.31       cgd count_lock_queue()
    952  1.31       cgd {
    953  1.66  augustss 	struct buf *bp;
    954  1.66  augustss 	int n = 0;
    955  1.31       cgd 
    956  1.31       cgd 	for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
    957  1.31       cgd 	    bp = bp->b_freelist.tqe_next)
    958  1.31       cgd 		n++;
    959  1.31       cgd 	return (n);
    960  1.31       cgd }
    961  1.31       cgd 
    962  1.36       cgd #ifdef DEBUG
    963  1.31       cgd /*
    964  1.31       cgd  * Print out statistics on the current allocation of the buffer pool.
    965  1.31       cgd  * Can be enabled to print out on every ``sync'' by setting "syncprt"
    966  1.31       cgd  * in vfs_syscalls.c using sysctl.
    967  1.31       cgd  */
    968  1.31       cgd void
    969  1.31       cgd vfs_bufstats()
    970  1.31       cgd {
    971  1.31       cgd 	int s, i, j, count;
    972  1.66  augustss 	struct buf *bp;
    973  1.66  augustss 	struct bqueues *dp;
    974  1.72    simonb 	int counts[(MAXBSIZE / PAGE_SIZE) + 1];
    975  1.31       cgd 	static char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE", "EMPTY" };
    976  1.71   thorpej 
    977  1.31       cgd 	for (dp = bufqueues, i = 0; dp < &bufqueues[BQUEUES]; dp++, i++) {
    978  1.31       cgd 		count = 0;
    979  1.71   thorpej 		for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
    980  1.31       cgd 			counts[j] = 0;
    981  1.31       cgd 		s = splbio();
    982  1.31       cgd 		for (bp = dp->tqh_first; bp; bp = bp->b_freelist.tqe_next) {
    983  1.71   thorpej 			counts[bp->b_bufsize/PAGE_SIZE]++;
    984  1.31       cgd 			count++;
    985  1.31       cgd 		}
    986  1.31       cgd 		splx(s);
    987  1.48  christos 		printf("%s: total-%d", bname[i], count);
    988  1.71   thorpej 		for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
    989  1.31       cgd 			if (counts[j] != 0)
    990  1.71   thorpej 				printf(", %d-%d", j * PAGE_SIZE, counts[j]);
    991  1.48  christos 		printf("\n");
    992  1.31       cgd 	}
    993  1.31       cgd }
    994  1.36       cgd #endif /* DEBUG */
    995