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