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