xref: /csrg-svn/sys/kern/vfs_bio.c (revision 68171)
149589Sbostic /*-
265771Sbostic  * Copyright (c) 1986, 1989, 1993
365771Sbostic  *	The Regents of the University of California.  All rights reserved.
465771Sbostic  * (c) UNIX System Laboratories, Inc.
565771Sbostic  * All or some portions of this file are derived from material licensed
665771Sbostic  * to the University of California by American Telephone and Telegraph
765771Sbostic  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
865771Sbostic  * the permission of UNIX System Laboratories, Inc.
923395Smckusick  *
1064437Sbostic  * This code is derived from software contributed to Berkeley by
1164437Sbostic  * Berkeley Software Design Inc.
1237736Smckusick  *
1364437Sbostic  * %sccs.include.redist.c%
1464437Sbostic  *
15*68171Scgd  *	@(#)vfs_bio.c	8.11 (Berkeley) 01/09/95
1623395Smckusick  */
178Sbill 
1851455Sbostic #include <sys/param.h>
1965256Smckusick #include <sys/systm.h>
2051455Sbostic #include <sys/proc.h>
2151455Sbostic #include <sys/buf.h>
2251455Sbostic #include <sys/vnode.h>
2351455Sbostic #include <sys/mount.h>
2451455Sbostic #include <sys/trace.h>
2559879Smckusick #include <sys/malloc.h>
2651455Sbostic #include <sys/resourcevar.h>
278Sbill 
2891Sbill /*
2956395Smckusick  * Definitions for the buffer hash lists.
3056395Smckusick  */
3156395Smckusick #define	BUFHASH(dvp, lbn)	\
3256395Smckusick 	(&bufhashtbl[((int)(dvp) / sizeof(*(dvp)) + (int)(lbn)) & bufhash])
3365256Smckusick LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash;
3456395Smckusick u_long	bufhash;
3556395Smckusick 
3656395Smckusick /*
3756395Smckusick  * Insq/Remq for the buffer hash lists.
3856395Smckusick  */
3965256Smckusick #define	binshash(bp, dp)	LIST_INSERT_HEAD(dp, bp, b_hash)
4065256Smckusick #define	bremhash(bp)		LIST_REMOVE(bp, b_hash)
4156395Smckusick 
4256395Smckusick /*
4356395Smckusick  * Definitions for the buffer free lists.
4456395Smckusick  */
4556395Smckusick #define	BQUEUES		4		/* number of free buffer queues */
4656395Smckusick 
4756395Smckusick #define	BQ_LOCKED	0		/* super-blocks &c */
4856395Smckusick #define	BQ_LRU		1		/* lru, useful buffers */
4956395Smckusick #define	BQ_AGE		2		/* rubbish */
5056395Smckusick #define	BQ_EMPTY	3		/* buffer headers with no memory */
5156395Smckusick 
5265256Smckusick TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
5356395Smckusick int needbuffer;
5456395Smckusick 
5556395Smckusick /*
5656395Smckusick  * Insq/Remq for the buffer free lists.
5756395Smckusick  */
5865256Smckusick #define	binsheadfree(bp, dp)	TAILQ_INSERT_HEAD(dp, bp, b_freelist)
5965256Smckusick #define	binstailfree(bp, dp)	TAILQ_INSERT_TAIL(dp, bp, b_freelist)
6056607Smckusick 
6156395Smckusick void
bremfree(bp)6256395Smckusick bremfree(bp)
6356395Smckusick 	struct buf *bp;
6456395Smckusick {
6565256Smckusick 	struct bqueues *dp = NULL;
6656395Smckusick 
6756607Smckusick 	/*
6856607Smckusick 	 * We only calculate the head of the freelist when removing
6956607Smckusick 	 * the last element of the list as that is the only time that
7056607Smckusick 	 * it is needed (e.g. to reset the tail pointer).
7165256Smckusick 	 *
7265256Smckusick 	 * NB: This makes an assumption about how tailq's are implemented.
7356607Smckusick 	 */
7465256Smckusick 	if (bp->b_freelist.tqe_next == NULL) {
7556395Smckusick 		for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
7665256Smckusick 			if (dp->tqh_last == &bp->b_freelist.tqe_next)
7756395Smckusick 				break;
7856395Smckusick 		if (dp == &bufqueues[BQUEUES])
7956395Smckusick 			panic("bremfree: lost tail");
8056395Smckusick 	}
8165256Smckusick 	TAILQ_REMOVE(dp, bp, b_freelist);
8256395Smckusick }
8356395Smckusick 
8456395Smckusick /*
8549280Skarels  * Initialize buffers and hash links for buffers.
8649280Skarels  */
8751455Sbostic void
bufinit()8849280Skarels bufinit()
8949280Skarels {
9056395Smckusick 	register struct buf *bp;
9165256Smckusick 	struct bqueues *dp;
9249280Skarels 	register int i;
9349280Skarels 	int base, residual;
9449280Skarels 
9556395Smckusick 	for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
9665256Smckusick 		TAILQ_INIT(dp);
9765256Smckusick 	bufhashtbl = hashinit(nbuf, M_CACHE, &bufhash);
9849280Skarels 	base = bufpages / nbuf;
9949280Skarels 	residual = bufpages % nbuf;
10049280Skarels 	for (i = 0; i < nbuf; i++) {
10149280Skarels 		bp = &buf[i];
10256395Smckusick 		bzero((char *)bp, sizeof *bp);
10349280Skarels 		bp->b_dev = NODEV;
10449280Skarels 		bp->b_rcred = NOCRED;
10549280Skarels 		bp->b_wcred = NOCRED;
10665552Smckusick 		bp->b_vnbufs.le_next = NOLIST;
10764536Sbostic 		bp->b_data = buffers + i * MAXBSIZE;
10849280Skarels 		if (i < residual)
10949280Skarels 			bp->b_bufsize = (base + 1) * CLBYTES;
11049280Skarels 		else
11149280Skarels 			bp->b_bufsize = base * CLBYTES;
11252413Storek 		bp->b_flags = B_INVAL;
11356395Smckusick 		dp = bp->b_bufsize ? &bufqueues[BQ_AGE] : &bufqueues[BQ_EMPTY];
11452413Storek 		binsheadfree(bp, dp);
11556395Smckusick 		binshash(bp, &invalhash);
11649280Skarels 	}
11749280Skarels }
11849280Skarels 
11949280Skarels /*
12046151Smckusick  * Find the block in the buffer pool.
12146151Smckusick  * If the buffer is not present, allocate a new buffer and load
12246151Smckusick  * its contents according to the filesystem fill routine.
1238Sbill  */
12438776Smckusick bread(vp, blkno, size, cred, bpp)
12537736Smckusick 	struct vnode *vp;
1266563Smckusic 	daddr_t blkno;
1276563Smckusic 	int size;
12838776Smckusick 	struct ucred *cred;
12937736Smckusick 	struct buf **bpp;
1308Sbill {
13147545Skarels 	struct proc *p = curproc;		/* XXX */
1328Sbill 	register struct buf *bp;
1338Sbill 
1348670S 	if (size == 0)
1358670S 		panic("bread: size 0");
13657797Smckusick 	*bpp = bp = getblk(vp, blkno, size, 0, 0);
13746151Smckusick 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
13840341Smckusick 		trace(TR_BREADHIT, pack(vp, size), blkno);
13937736Smckusick 		return (0);
1408Sbill 	}
1418Sbill 	bp->b_flags |= B_READ;
1428670S 	if (bp->b_bcount > bp->b_bufsize)
1438670S 		panic("bread");
14438776Smckusick 	if (bp->b_rcred == NOCRED && cred != NOCRED) {
14538776Smckusick 		crhold(cred);
14638776Smckusick 		bp->b_rcred = cred;
14738776Smckusick 	}
14837736Smckusick 	VOP_STRATEGY(bp);
14940341Smckusick 	trace(TR_BREADMISS, pack(vp, size), blkno);
15047545Skarels 	p->p_stats->p_ru.ru_inblock++;		/* pay for read */
15137736Smckusick 	return (biowait(bp));
1528Sbill }
1538Sbill 
1548Sbill /*
15552189Smckusick  * Operates like bread, but also starts I/O on the N specified
15652189Smckusick  * read-ahead blocks.
1578Sbill  */
15852189Smckusick breadn(vp, blkno, size, rablkno, rabsize, num, cred, bpp)
15937736Smckusick 	struct vnode *vp;
1607114Smckusick 	daddr_t blkno; int size;
16152189Smckusick 	daddr_t rablkno[]; int rabsize[];
16252189Smckusick 	int num;
16338776Smckusick 	struct ucred *cred;
16437736Smckusick 	struct buf **bpp;
1658Sbill {
16647545Skarels 	struct proc *p = curproc;		/* XXX */
1678Sbill 	register struct buf *bp, *rabp;
16852189Smckusick 	register int i;
1698Sbill 
1708Sbill 	bp = NULL;
1717015Smckusick 	/*
17246151Smckusick 	 * If the block is not memory resident,
17346151Smckusick 	 * allocate a buffer and start I/O.
1747015Smckusick 	 */
17537736Smckusick 	if (!incore(vp, blkno)) {
17657797Smckusick 		*bpp = bp = getblk(vp, blkno, size, 0, 0);
17746151Smckusick 		if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0) {
1788Sbill 			bp->b_flags |= B_READ;
1798670S 			if (bp->b_bcount > bp->b_bufsize)
18052189Smckusick 				panic("breadn");
18138776Smckusick 			if (bp->b_rcred == NOCRED && cred != NOCRED) {
18238776Smckusick 				crhold(cred);
18338776Smckusick 				bp->b_rcred = cred;
18438776Smckusick 			}
18537736Smckusick 			VOP_STRATEGY(bp);
18640341Smckusick 			trace(TR_BREADMISS, pack(vp, size), blkno);
18747545Skarels 			p->p_stats->p_ru.ru_inblock++;	/* pay for read */
18854342Smckusick 		} else {
18940341Smckusick 			trace(TR_BREADHIT, pack(vp, size), blkno);
19054342Smckusick 		}
1918Sbill 	}
1927015Smckusick 
1937015Smckusick 	/*
19452189Smckusick 	 * If there's read-ahead block(s), start I/O
19552189Smckusick 	 * on them also (as above).
1967015Smckusick 	 */
19752189Smckusick 	for (i = 0; i < num; i++) {
19852189Smckusick 		if (incore(vp, rablkno[i]))
19952189Smckusick 			continue;
20057797Smckusick 		rabp = getblk(vp, rablkno[i], rabsize[i], 0, 0);
20146151Smckusick 		if (rabp->b_flags & (B_DONE | B_DELWRI)) {
2028Sbill 			brelse(rabp);
20352189Smckusick 			trace(TR_BREADHITRA, pack(vp, rabsize[i]), rablkno[i]);
2042045Swnj 		} else {
20546151Smckusick 			rabp->b_flags |= B_ASYNC | B_READ;
2068670S 			if (rabp->b_bcount > rabp->b_bufsize)
2078670S 				panic("breadrabp");
20838880Smckusick 			if (rabp->b_rcred == NOCRED && cred != NOCRED) {
20938776Smckusick 				crhold(cred);
21038880Smckusick 				rabp->b_rcred = cred;
21138776Smckusick 			}
21237736Smckusick 			VOP_STRATEGY(rabp);
21352189Smckusick 			trace(TR_BREADMISSRA, pack(vp, rabsize[i]), rablkno[i]);
21447545Skarels 			p->p_stats->p_ru.ru_inblock++;	/* pay in advance */
2158Sbill 		}
2168Sbill 	}
2177015Smckusick 
2187015Smckusick 	/*
21946151Smckusick 	 * If block was memory resident, let bread get it.
22046151Smckusick 	 * If block was not memory resident, the read was
22146151Smckusick 	 * started above, so just wait for the read to complete.
2227015Smckusick 	 */
2237114Smckusick 	if (bp == NULL)
22438776Smckusick 		return (bread(vp, blkno, size, cred, bpp));
22537736Smckusick 	return (biowait(bp));
2268Sbill }
2278Sbill 
2288Sbill /*
22946151Smckusick  * Synchronous write.
23046151Smckusick  * Release buffer on completion.
2318Sbill  */
bwrite(bp)2328Sbill bwrite(bp)
2337015Smckusick 	register struct buf *bp;
2348Sbill {
23547545Skarels 	struct proc *p = curproc;		/* XXX */
23637736Smckusick 	register int flag;
23752413Storek 	int s, error = 0;
2388Sbill 
23965858Smckusick 	if ((bp->b_flags & B_ASYNC) == 0 &&
24065898Shibler 	    bp->b_vp && bp->b_vp->v_mount &&
24165898Shibler 	    (bp->b_vp->v_mount->mnt_flag & MNT_ASYNC)) {
24265858Smckusick 		bdwrite(bp);
24365858Smckusick 		return (0);
24465858Smckusick 	}
2458Sbill 	flag = bp->b_flags;
2469857Ssam 	bp->b_flags &= ~(B_READ | B_DONE | B_ERROR | B_DELWRI);
24749459Smckusick 	if (flag & B_ASYNC) {
24849459Smckusick 		if ((flag & B_DELWRI) == 0)
24949459Smckusick 			p->p_stats->p_ru.ru_oublock++;	/* no one paid yet */
25049459Smckusick 		else
25149459Smckusick 			reassignbuf(bp, bp->b_vp);
25249459Smckusick 	}
25340341Smckusick 	trace(TR_BWRITE, pack(bp->b_vp, bp->b_bcount), bp->b_lblkno);
2548670S 	if (bp->b_bcount > bp->b_bufsize)
2558670S 		panic("bwrite");
25640226Smckusick 	s = splbio();
25739882Smckusick 	bp->b_vp->v_numoutput++;
25857797Smckusick 	bp->b_flags |= B_WRITEINPROG;
25940226Smckusick 	splx(s);
26037736Smckusick 	VOP_STRATEGY(bp);
2617015Smckusick 
2627015Smckusick 	/*
26346151Smckusick 	 * If the write was synchronous, then await I/O completion.
2647015Smckusick 	 * If the write was "delayed", then we put the buffer on
26546151Smckusick 	 * the queue of blocks awaiting I/O completion status.
2667015Smckusick 	 */
26746151Smckusick 	if ((flag & B_ASYNC) == 0) {
26837736Smckusick 		error = biowait(bp);
26949459Smckusick 		if ((flag&B_DELWRI) == 0)
27049459Smckusick 			p->p_stats->p_ru.ru_oublock++;	/* no one paid yet */
27149459Smckusick 		else
27249459Smckusick 			reassignbuf(bp, bp->b_vp);
27357797Smckusick 		if (bp->b_flags & B_EINTR) {
27457797Smckusick 			bp->b_flags &= ~B_EINTR;
27557797Smckusick 			error = EINTR;
27657797Smckusick 		}
2778Sbill 		brelse(bp);
27837736Smckusick 	} else if (flag & B_DELWRI) {
27952413Storek 		s = splbio();
2808Sbill 		bp->b_flags |= B_AGE;
28152413Storek 		splx(s);
28237736Smckusick 	}
28337736Smckusick 	return (error);
2848Sbill }
2858Sbill 
28653578Sheideman int
vn_bwrite(ap)28753578Sheideman vn_bwrite(ap)
28853578Sheideman 	struct vop_bwrite_args *ap;
28953578Sheideman {
29065612Smckusick 
29156395Smckusick 	return (bwrite(ap->a_bp));
29253578Sheideman }
29353578Sheideman 
29453578Sheideman 
2958Sbill /*
29646151Smckusick  * Delayed write.
29746151Smckusick  *
29846151Smckusick  * The buffer is marked dirty, but is not queued for I/O.
29946151Smckusick  * This routine should be used when the buffer is expected
30046151Smckusick  * to be modified again soon, typically a small write that
30146151Smckusick  * partially fills a buffer.
30246151Smckusick  *
30346151Smckusick  * NB: magnetic tapes cannot be delayed; they must be
30446151Smckusick  * written in the order that the writes are requested.
3058Sbill  */
306*68171Scgd void
bdwrite(bp)3078Sbill bdwrite(bp)
3087015Smckusick 	register struct buf *bp;
3098Sbill {
31047545Skarels 	struct proc *p = curproc;		/* XXX */
3118Sbill 
31239882Smckusick 	if ((bp->b_flags & B_DELWRI) == 0) {
31339882Smckusick 		bp->b_flags |= B_DELWRI;
31439882Smckusick 		reassignbuf(bp, bp->b_vp);
31547545Skarels 		p->p_stats->p_ru.ru_oublock++;		/* no one paid yet */
31639882Smckusick 	}
31737736Smckusick 	/*
31839668Smckusick 	 * If this is a tape drive, the write must be initiated.
31937736Smckusick 	 */
32048360Smckusick 	if (VOP_IOCTL(bp->b_vp, 0, (caddr_t)B_TAPE, 0, NOCRED, p) == 0) {
3218Sbill 		bawrite(bp);
32239668Smckusick 	} else {
32346151Smckusick 		bp->b_flags |= (B_DONE | B_DELWRI);
3248Sbill 		brelse(bp);
3258Sbill 	}
3268Sbill }
3278Sbill 
3288Sbill /*
32946151Smckusick  * Asynchronous write.
33046151Smckusick  * Start I/O on a buffer, but do not wait for it to complete.
33146151Smckusick  * The buffer is released when the I/O completes.
3328Sbill  */
333*68171Scgd void
bawrite(bp)3348Sbill bawrite(bp)
3357015Smckusick 	register struct buf *bp;
3368Sbill {
3378Sbill 
33846151Smckusick 	/*
33946151Smckusick 	 * Setting the ASYNC flag causes bwrite to return
34046151Smckusick 	 * after starting the I/O.
34146151Smckusick 	 */
3428Sbill 	bp->b_flags |= B_ASYNC;
34357797Smckusick 	(void) VOP_BWRITE(bp);
3448Sbill }
3458Sbill 
3468Sbill /*
34746151Smckusick  * Release a buffer.
34846151Smckusick  * Even if the buffer is dirty, no I/O is started.
3498Sbill  */
350*68171Scgd void
brelse(bp)3518Sbill brelse(bp)
3527015Smckusick 	register struct buf *bp;
3538Sbill {
35465256Smckusick 	register struct bqueues *flist;
35546151Smckusick 	int s;
3568Sbill 
35740341Smckusick 	trace(TR_BRELSE, pack(bp->b_vp, bp->b_bufsize), bp->b_lblkno);
3587015Smckusick 	/*
35939668Smckusick 	 * If a process is waiting for the buffer, or
36039668Smckusick 	 * is waiting for a free buffer, awaken it.
3617015Smckusick 	 */
36246151Smckusick 	if (bp->b_flags & B_WANTED)
3638Sbill 		wakeup((caddr_t)bp);
36456395Smckusick 	if (needbuffer) {
36556395Smckusick 		needbuffer = 0;
36656395Smckusick 		wakeup((caddr_t)&needbuffer);
3678Sbill 	}
36839668Smckusick 	/*
36939668Smckusick 	 * Retry I/O for locked buffers rather than invalidating them.
37039668Smckusick 	 */
37152413Storek 	s = splbio();
37239668Smckusick 	if ((bp->b_flags & B_ERROR) && (bp->b_flags & B_LOCKED))
37339668Smckusick 		bp->b_flags &= ~B_ERROR;
37439668Smckusick 	/*
37539668Smckusick 	 * Disassociate buffers that are no longer valid.
37639668Smckusick 	 */
37746151Smckusick 	if (bp->b_flags & (B_NOCACHE | B_ERROR))
37837736Smckusick 		bp->b_flags |= B_INVAL;
37946151Smckusick 	if ((bp->b_bufsize <= 0) || (bp->b_flags & (B_ERROR | B_INVAL))) {
38039668Smckusick 		if (bp->b_vp)
38139668Smckusick 			brelvp(bp);
38239668Smckusick 		bp->b_flags &= ~B_DELWRI;
38337736Smckusick 	}
3847015Smckusick 	/*
3857015Smckusick 	 * Stick the buffer back on a free list.
3867015Smckusick 	 */
3878670S 	if (bp->b_bufsize <= 0) {
3888670S 		/* block has no buffer ... put at front of unused buffer list */
38956395Smckusick 		flist = &bufqueues[BQ_EMPTY];
3908670S 		binsheadfree(bp, flist);
39146151Smckusick 	} else if (bp->b_flags & (B_ERROR | B_INVAL)) {
3922325Swnj 		/* block has no info ... put at front of most free list */
39356395Smckusick 		flist = &bufqueues[BQ_AGE];
3947015Smckusick 		binsheadfree(bp, flist);
3958Sbill 	} else {
3962325Swnj 		if (bp->b_flags & B_LOCKED)
39756395Smckusick 			flist = &bufqueues[BQ_LOCKED];
3982325Swnj 		else if (bp->b_flags & B_AGE)
39956395Smckusick 			flist = &bufqueues[BQ_AGE];
4002325Swnj 		else
40156395Smckusick 			flist = &bufqueues[BQ_LRU];
4027015Smckusick 		binstailfree(bp, flist);
4038Sbill 	}
40446151Smckusick 	bp->b_flags &= ~(B_WANTED | B_BUSY | B_ASYNC | B_AGE | B_NOCACHE);
4058Sbill 	splx(s);
4068Sbill }
4078Sbill 
4088Sbill /*
40946151Smckusick  * Check to see if a block is currently memory resident.
4108Sbill  */
41157797Smckusick struct buf *
incore(vp,blkno)41237736Smckusick incore(vp, blkno)
41337736Smckusick 	struct vnode *vp;
4147015Smckusick 	daddr_t blkno;
4158Sbill {
4168Sbill 	register struct buf *bp;
4178Sbill 
41865256Smckusick 	for (bp = BUFHASH(vp, blkno)->lh_first; bp; bp = bp->b_hash.le_next)
41939668Smckusick 		if (bp->b_lblkno == blkno && bp->b_vp == vp &&
4207015Smckusick 		    (bp->b_flags & B_INVAL) == 0)
42157797Smckusick 			return (bp);
42257797Smckusick 	return (NULL);
4238Sbill }
4248Sbill 
42539668Smckusick /*
42646151Smckusick  * Check to see if a block is currently memory resident.
42746151Smckusick  * If it is resident, return it. If it is not resident,
42846151Smckusick  * allocate a new buffer and assign it to the block.
42939668Smckusick  */
4308Sbill struct buf *
getblk(vp,blkno,size,slpflag,slptimeo)43157797Smckusick getblk(vp, blkno, size, slpflag, slptimeo)
43237736Smckusick 	register struct vnode *vp;
4336563Smckusic 	daddr_t blkno;
43457797Smckusick 	int size, slpflag, slptimeo;
4358Sbill {
43656607Smckusick 	register struct buf *bp;
43765256Smckusick 	struct bufhashhdr *dp;
43857797Smckusick 	int s, error;
4398Sbill 
44025255Smckusick 	if (size > MAXBSIZE)
44125255Smckusick 		panic("getblk: size too big");
4427015Smckusick 	/*
44346151Smckusick 	 * Search the cache for the block. If the buffer is found,
44446151Smckusick 	 * but it is currently locked, the we must wait for it to
44546151Smckusick 	 * become available.
4467015Smckusick 	 */
44737736Smckusick 	dp = BUFHASH(vp, blkno);
4487015Smckusick loop:
44965256Smckusick 	for (bp = dp->lh_first; bp; bp = bp->b_hash.le_next) {
45057797Smckusick 		if (bp->b_lblkno != blkno || bp->b_vp != vp)
4518Sbill 			continue;
45226271Skarels 		s = splbio();
45346151Smckusick 		if (bp->b_flags & B_BUSY) {
4548Sbill 			bp->b_flags |= B_WANTED;
45557797Smckusick 			error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
45657797Smckusick 				"getblk", slptimeo);
4575424Swnj 			splx(s);
45857797Smckusick 			if (error)
45957797Smckusick 				return (NULL);
4608Sbill 			goto loop;
4618Sbill 		}
46257797Smckusick 		/*
46357797Smckusick 		 * The test for B_INVAL is moved down here, since there
46457797Smckusick 		 * are cases where B_INVAL is set before VOP_BWRITE() is
46557797Smckusick 		 * called and for NFS, the process cannot be allowed to
46657797Smckusick 		 * allocate a new buffer for the same block until the write
46757797Smckusick 		 * back to the server has been completed. (ie. B_BUSY clears)
46857797Smckusick 		 */
46957797Smckusick 		if (bp->b_flags & B_INVAL) {
47057797Smckusick 			splx(s);
47157797Smckusick 			continue;
47257797Smckusick 		}
47339882Smckusick 		bremfree(bp);
47439882Smckusick 		bp->b_flags |= B_BUSY;
4755424Swnj 		splx(s);
47632608Smckusick 		if (bp->b_bcount != size) {
47765996Spendry 			printf("getblk: stray size\n");
47839668Smckusick 			bp->b_flags |= B_INVAL;
47957797Smckusick 			VOP_BWRITE(bp);
48039668Smckusick 			goto loop;
48132608Smckusick 		}
4828Sbill 		bp->b_flags |= B_CACHE;
48326271Skarels 		return (bp);
4848Sbill 	}
48557797Smckusick 	/*
48657797Smckusick 	 * The loop back to the top when getnewbuf() fails is because
48757797Smckusick 	 * stateless filesystems like NFS have no node locks. Thus,
48857797Smckusick 	 * there is a slight chance that more than one process will
48957797Smckusick 	 * try and getnewbuf() for the same block concurrently when
49057797Smckusick 	 * the first sleeps in getnewbuf(). So after a sleep, go back
49157797Smckusick 	 * up to the top to check the hash lists again.
49257797Smckusick 	 */
49357797Smckusick 	if ((bp = getnewbuf(slpflag, slptimeo)) == 0)
49457797Smckusick 		goto loop;
4957015Smckusick 	bremhash(bp);
49639668Smckusick 	bgetvp(vp, bp);
49745116Smckusick 	bp->b_bcount = 0;
49839668Smckusick 	bp->b_lblkno = blkno;
4996563Smckusic 	bp->b_blkno = blkno;
5008670S 	bp->b_error = 0;
50137736Smckusick 	bp->b_resid = 0;
50237736Smckusick 	binshash(bp, dp);
50345116Smckusick 	allocbuf(bp, size);
50426271Skarels 	return (bp);
5058Sbill }
5068Sbill 
5078Sbill /*
50846151Smckusick  * Allocate a buffer.
50946151Smckusick  * The caller will assign it to a block.
5108Sbill  */
5118Sbill struct buf *
geteblk(size)5126563Smckusic geteblk(size)
5136563Smckusic 	int size;
5148Sbill {
51556395Smckusick 	register struct buf *bp;
5168Sbill 
51725255Smckusick 	if (size > MAXBSIZE)
51825255Smckusick 		panic("geteblk: size too big");
51957797Smckusick 	while ((bp = getnewbuf(0, 0)) == NULL)
52057797Smckusick 		/* void */;
5218670S 	bp->b_flags |= B_INVAL;
5227015Smckusick 	bremhash(bp);
52356395Smckusick 	binshash(bp, &invalhash);
52445116Smckusick 	bp->b_bcount = 0;
52537736Smckusick 	bp->b_error = 0;
52637736Smckusick 	bp->b_resid = 0;
52745116Smckusick 	allocbuf(bp, size);
52826271Skarels 	return (bp);
5298Sbill }
5308Sbill 
5318Sbill /*
53245116Smckusick  * Expand or contract the actual memory allocated to a buffer.
53346151Smckusick  * If no memory is available, release buffer and take error exit.
5346563Smckusic  */
allocbuf(tp,size)53545116Smckusick allocbuf(tp, size)
53645116Smckusick 	register struct buf *tp;
5376563Smckusic 	int size;
5386563Smckusic {
53945116Smckusick 	register struct buf *bp, *ep;
54045116Smckusick 	int sizealloc, take, s;
5416563Smckusic 
54245116Smckusick 	sizealloc = roundup(size, CLBYTES);
54345116Smckusick 	/*
54445116Smckusick 	 * Buffer size does not change
54545116Smckusick 	 */
54645116Smckusick 	if (sizealloc == tp->b_bufsize)
54745116Smckusick 		goto out;
54845116Smckusick 	/*
54945116Smckusick 	 * Buffer size is shrinking.
55045116Smckusick 	 * Place excess space in a buffer header taken from the
55145116Smckusick 	 * BQ_EMPTY buffer list and placed on the "most free" list.
55245116Smckusick 	 * If no extra buffer headers are available, leave the
55345116Smckusick 	 * extra space in the present buffer.
55445116Smckusick 	 */
55545116Smckusick 	if (sizealloc < tp->b_bufsize) {
55665256Smckusick 		if ((ep = bufqueues[BQ_EMPTY].tqh_first) == NULL)
55745116Smckusick 			goto out;
55845116Smckusick 		s = splbio();
55945116Smckusick 		bremfree(ep);
56045116Smckusick 		ep->b_flags |= B_BUSY;
56145116Smckusick 		splx(s);
56264536Sbostic 		pagemove((char *)tp->b_data + sizealloc, ep->b_data,
56345116Smckusick 		    (int)tp->b_bufsize - sizealloc);
56445116Smckusick 		ep->b_bufsize = tp->b_bufsize - sizealloc;
56545116Smckusick 		tp->b_bufsize = sizealloc;
56645116Smckusick 		ep->b_flags |= B_INVAL;
56745116Smckusick 		ep->b_bcount = 0;
56845116Smckusick 		brelse(ep);
56945116Smckusick 		goto out;
57045116Smckusick 	}
57145116Smckusick 	/*
57245116Smckusick 	 * More buffer space is needed. Get it out of buffers on
57345116Smckusick 	 * the "most free" list, placing the empty headers on the
57445116Smckusick 	 * BQ_EMPTY buffer header list.
57545116Smckusick 	 */
57645116Smckusick 	while (tp->b_bufsize < sizealloc) {
57745116Smckusick 		take = sizealloc - tp->b_bufsize;
57857797Smckusick 		while ((bp = getnewbuf(0, 0)) == NULL)
57957797Smckusick 			/* void */;
58045116Smckusick 		if (take >= bp->b_bufsize)
58145116Smckusick 			take = bp->b_bufsize;
58264536Sbostic 		pagemove(&((char *)bp->b_data)[bp->b_bufsize - take],
58364536Sbostic 		    &((char *)tp->b_data)[tp->b_bufsize], take);
58445116Smckusick 		tp->b_bufsize += take;
58545116Smckusick 		bp->b_bufsize = bp->b_bufsize - take;
58645116Smckusick 		if (bp->b_bcount > bp->b_bufsize)
58745116Smckusick 			bp->b_bcount = bp->b_bufsize;
58845116Smckusick 		if (bp->b_bufsize <= 0) {
58945116Smckusick 			bremhash(bp);
59056395Smckusick 			binshash(bp, &invalhash);
59146151Smckusick 			bp->b_dev = NODEV;
59245116Smckusick 			bp->b_error = 0;
59345116Smckusick 			bp->b_flags |= B_INVAL;
59445116Smckusick 		}
59545116Smckusick 		brelse(bp);
59645116Smckusick 	}
59745116Smckusick out:
59845116Smckusick 	tp->b_bcount = size;
59945116Smckusick 	return (1);
6008670S }
6018670S 
6028670S /*
6038670S  * Find a buffer which is available for use.
6048670S  * Select something from a free list.
6058670S  * Preference is to AGE list, then LRU list.
6068670S  */
6078670S struct buf *
getnewbuf(slpflag,slptimeo)60857797Smckusick getnewbuf(slpflag, slptimeo)
60957797Smckusick 	int slpflag, slptimeo;
6108670S {
61156395Smckusick 	register struct buf *bp;
61265256Smckusick 	register struct bqueues *dp;
61338776Smckusick 	register struct ucred *cred;
6148670S 	int s;
6158670S 
6168670S loop:
61726271Skarels 	s = splbio();
61859879Smckusick 	for (dp = &bufqueues[BQ_AGE]; dp > bufqueues; dp--)
61965256Smckusick 		if (dp->tqh_first)
62059879Smckusick 			break;
62156395Smckusick 	if (dp == bufqueues) {		/* no free blocks */
62256395Smckusick 		needbuffer = 1;
62357797Smckusick 		(void) tsleep((caddr_t)&needbuffer, slpflag | (PRIBIO + 1),
62457797Smckusick 			"getnewbuf", slptimeo);
62512170Ssam 		splx(s);
62657797Smckusick 		return (NULL);
6278670S 	}
62865256Smckusick 	bp = dp->tqh_first;
62939882Smckusick 	bremfree(bp);
63039882Smckusick 	bp->b_flags |= B_BUSY;
6318670S 	splx(s);
6328670S 	if (bp->b_flags & B_DELWRI) {
63338614Smckusick 		(void) bawrite(bp);
6348670S 		goto loop;
6358670S 	}
63640341Smckusick 	trace(TR_BRELSE, pack(bp->b_vp, bp->b_bufsize), bp->b_lblkno);
63739668Smckusick 	if (bp->b_vp)
63839668Smckusick 		brelvp(bp);
63938776Smckusick 	if (bp->b_rcred != NOCRED) {
64038776Smckusick 		cred = bp->b_rcred;
64138776Smckusick 		bp->b_rcred = NOCRED;
64238776Smckusick 		crfree(cred);
64338776Smckusick 	}
64438776Smckusick 	if (bp->b_wcred != NOCRED) {
64538776Smckusick 		cred = bp->b_wcred;
64638776Smckusick 		bp->b_wcred = NOCRED;
64738776Smckusick 		crfree(cred);
64838776Smckusick 	}
6498670S 	bp->b_flags = B_BUSY;
65046989Smckusick 	bp->b_dirtyoff = bp->b_dirtyend = 0;
65152189Smckusick 	bp->b_validoff = bp->b_validend = 0;
6528670S 	return (bp);
6538670S }
6548670S 
6558670S /*
65646151Smckusick  * Wait for I/O to complete.
65746151Smckusick  *
65846151Smckusick  * Extract and return any errors associated with the I/O.
65946151Smckusick  * If the error flag is set, but no specific error is
66046151Smckusick  * given, return EIO.
6618Sbill  */
biowait(bp)6627015Smckusick biowait(bp)
6636563Smckusic 	register struct buf *bp;
6648Sbill {
6655431Sroot 	int s;
6668Sbill 
66726271Skarels 	s = splbio();
66838776Smckusick 	while ((bp->b_flags & B_DONE) == 0)
6698Sbill 		sleep((caddr_t)bp, PRIBIO);
6705431Sroot 	splx(s);
67137736Smckusick 	if ((bp->b_flags & B_ERROR) == 0)
67237736Smckusick 		return (0);
67337736Smckusick 	if (bp->b_error)
67437736Smckusick 		return (bp->b_error);
67537736Smckusick 	return (EIO);
6768Sbill }
6778Sbill 
6788Sbill /*
67913128Ssam  * Mark I/O complete on a buffer.
68046151Smckusick  *
68146151Smckusick  * If a callback has been requested, e.g. the pageout
68246151Smckusick  * daemon, do so. Otherwise, awaken waiting processes.
6838Sbill  */
68451455Sbostic void
biodone(bp)6857015Smckusick biodone(bp)
6867015Smckusick 	register struct buf *bp;
6878Sbill {
6888Sbill 
689420Sbill 	if (bp->b_flags & B_DONE)
6907015Smckusick 		panic("dup biodone");
6918Sbill 	bp->b_flags |= B_DONE;
69249232Smckusick 	if ((bp->b_flags & B_READ) == 0)
69349232Smckusick 		vwakeup(bp);
6949763Ssam 	if (bp->b_flags & B_CALL) {
6959763Ssam 		bp->b_flags &= ~B_CALL;
6969763Ssam 		(*bp->b_iodone)(bp);
6979763Ssam 		return;
6989763Ssam 	}
69946151Smckusick 	if (bp->b_flags & B_ASYNC)
7008Sbill 		brelse(bp);
7018Sbill 	else {
7028Sbill 		bp->b_flags &= ~B_WANTED;
7038Sbill 		wakeup((caddr_t)bp);
7048Sbill 	}
7058Sbill }
70656356Smckusick 
70757035Smargo int
count_lock_queue()70857035Smargo count_lock_queue()
70957035Smargo {
71057035Smargo 	register struct buf *bp;
71157035Smargo 	register int ret;
71257035Smargo 
71365256Smckusick 	for (ret = 0, bp = (struct buf *)bufqueues[BQ_LOCKED].tqh_first;
71465256Smckusick 	    bp; bp = (struct buf *)bp->b_freelist.tqe_next)
71557035Smargo 		++ret;
71657035Smargo 	return(ret);
71757035Smargo }
71857035Smargo 
71956356Smckusick #ifdef DIAGNOSTIC
72056356Smckusick /*
72156356Smckusick  * Print out statistics on the current allocation of the buffer pool.
72256356Smckusick  * Can be enabled to print out on every ``sync'' by setting "syncprt"
72359879Smckusick  * in vfs_syscalls.c using sysctl.
72456356Smckusick  */
72556356Smckusick void
vfs_bufstats()72656356Smckusick vfs_bufstats()
72756356Smckusick {
72856356Smckusick 	int s, i, j, count;
72956395Smckusick 	register struct buf *bp;
73065256Smckusick 	register struct bqueues *dp;
73156356Smckusick 	int counts[MAXBSIZE/CLBYTES+1];
73256356Smckusick 	static char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE", "EMPTY" };
73356356Smckusick 
73456395Smckusick 	for (dp = bufqueues, i = 0; dp < &bufqueues[BQUEUES]; dp++, i++) {
73556356Smckusick 		count = 0;
73656356Smckusick 		for (j = 0; j <= MAXBSIZE/CLBYTES; j++)
73756356Smckusick 			counts[j] = 0;
73856356Smckusick 		s = splbio();
73965256Smckusick 		for (bp = dp->tqh_first; bp; bp = bp->b_freelist.tqe_next) {
74056356Smckusick 			counts[bp->b_bufsize/CLBYTES]++;
74156356Smckusick 			count++;
74256356Smckusick 		}
74356356Smckusick 		splx(s);
74456356Smckusick 		printf("%s: total-%d", bname[i], count);
74556356Smckusick 		for (j = 0; j <= MAXBSIZE/CLBYTES; j++)
74656356Smckusick 			if (counts[j] != 0)
74756356Smckusick 				printf(", %d-%d", j * CLBYTES, counts[j]);
74856356Smckusick 		printf("\n");
74956356Smckusick 	}
75056356Smckusick }
75156356Smckusick #endif /* DIAGNOSTIC */
752