xref: /csrg-svn/sys/kern/vfs_cluster.c (revision 2325)
1*2325Swnj /*	vfs_cluster.c	4.6	01/31/81	*/
28Sbill 
38Sbill #include "../h/param.h"
48Sbill #include "../h/systm.h"
58Sbill #include "../h/dir.h"
68Sbill #include "../h/user.h"
78Sbill #include "../h/buf.h"
88Sbill #include "../h/conf.h"
98Sbill #include "../h/proc.h"
108Sbill #include "../h/seg.h"
118Sbill #include "../h/pte.h"
128Sbill #include "../h/vm.h"
132045Swnj #include "../h/trace.h"
148Sbill 
1591Sbill /*
1691Sbill  * The following several routines allocate and free
1791Sbill  * buffers with various side effects.  In general the
1891Sbill  * arguments to an allocate routine are a device and
1991Sbill  * a block number, and the value is a pointer to
2091Sbill  * to the buffer header; the buffer is marked "busy"
2191Sbill  * so that no one else can touch it.  If the block was
2291Sbill  * already in core, no I/O need be done; if it is
2391Sbill  * already busy, the process waits until it becomes free.
2491Sbill  * The following routines allocate a buffer:
2591Sbill  *	getblk
2691Sbill  *	bread
2791Sbill  *	breada
2891Sbill  *	baddr	(if it is incore)
2991Sbill  * Eventually the buffer must be released, possibly with the
3091Sbill  * side effect of writing it out, by using one of
3191Sbill  *	bwrite
3291Sbill  *	bdwrite
3391Sbill  *	bawrite
3491Sbill  *	brelse
3591Sbill  */
3691Sbill 
3791Sbill #define	BUFHSZ	63
38*2325Swnj struct	bufhd bufhash[BUFHSZ];
39*2325Swnj #define	BUFHASH(dev,blkno)	\
40*2325Swnj 		((struct buf *)&bufhash[((int)dev+(int)blkno) % BUFHSZ])
4191Sbill 
4291Sbill /*
4391Sbill  * Initialize hash links for buffers.
4491Sbill  */
4591Sbill bhinit()
4691Sbill {
4791Sbill 	register int i;
48*2325Swnj 	register struct bufhd *bp;
4991Sbill 
50*2325Swnj 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
51*2325Swnj 		bp->b_forw = bp->b_back = (struct buf *)bp;
5291Sbill }
5391Sbill 
548Sbill /* #define	DISKMON	1 */
558Sbill 
568Sbill #ifdef	DISKMON
578Sbill struct {
588Sbill 	int	nbuf;
598Sbill 	long	nread;
608Sbill 	long	nreada;
618Sbill 	long	ncache;
628Sbill 	long	nwrite;
638Sbill 	long	bufcount[NBUF];
648Sbill } io_info;
658Sbill #endif
668Sbill 
678Sbill /*
688Sbill  * Swap IO headers -
698Sbill  * They contain the necessary information for the swap I/O.
708Sbill  * At any given time, a swap header can be in three
718Sbill  * different lists. When free it is in the free list,
728Sbill  * when allocated and the I/O queued, it is on the swap
738Sbill  * device list, and finally, if the operation was a dirty
748Sbill  * page push, when the I/O completes, it is inserted
758Sbill  * in a list of cleaned pages to be processed by the pageout daemon.
768Sbill  */
778Sbill struct	buf swbuf[NSWBUF];
788Sbill short	swsize[NSWBUF];		/* CAN WE JUST USE B_BCOUNT? */
798Sbill int	swpf[NSWBUF];
808Sbill 
818Sbill 
828Sbill #ifdef	FASTVAX
838Sbill #define	notavail(bp) \
848Sbill { \
858Sbill 	int s = spl6(); \
868Sbill 	(bp)->av_back->av_forw = (bp)->av_forw; \
878Sbill 	(bp)->av_forw->av_back = (bp)->av_back; \
888Sbill 	(bp)->b_flags |= B_BUSY; \
898Sbill 	splx(s); \
908Sbill }
918Sbill #endif
928Sbill 
938Sbill /*
948Sbill  * Read in (if necessary) the block and return a buffer pointer.
958Sbill  */
968Sbill struct buf *
978Sbill bread(dev, blkno)
988Sbill dev_t dev;
998Sbill daddr_t blkno;
1008Sbill {
1018Sbill 	register struct buf *bp;
1028Sbill 
1038Sbill 	bp = getblk(dev, blkno);
1048Sbill 	if (bp->b_flags&B_DONE) {
1052045Swnj #ifdef	EPAWNJ
1062045Swnj 		trace(TR_BREAD|TR_HIT, dev, blkno);
1072045Swnj #endif
1088Sbill #ifdef	DISKMON
1098Sbill 		io_info.ncache++;
1108Sbill #endif
1118Sbill 		return(bp);
1128Sbill 	}
1138Sbill 	bp->b_flags |= B_READ;
1148Sbill 	bp->b_bcount = BSIZE;
1158Sbill 	(*bdevsw[major(dev)].d_strategy)(bp);
1162045Swnj #ifdef	EPAWNJ
1172045Swnj 	trace(TR_BREAD|TR_MISS, dev, blkno);
1182045Swnj #endif
1198Sbill #ifdef	DISKMON
1208Sbill 	io_info.nread++;
1218Sbill #endif
1228Sbill 	u.u_vm.vm_inblk++;		/* pay for read */
1238Sbill 	iowait(bp);
1248Sbill 	return(bp);
1258Sbill }
1268Sbill 
1278Sbill /*
1288Sbill  * Read in the block, like bread, but also start I/O on the
1298Sbill  * read-ahead block (which is not allocated to the caller)
1308Sbill  */
1318Sbill struct buf *
1328Sbill breada(dev, blkno, rablkno)
1338Sbill dev_t dev;
1348Sbill daddr_t blkno, rablkno;
1358Sbill {
1368Sbill 	register struct buf *bp, *rabp;
1378Sbill 
1388Sbill 	bp = NULL;
1398Sbill 	if (!incore(dev, blkno)) {
1408Sbill 		bp = getblk(dev, blkno);
1418Sbill 		if ((bp->b_flags&B_DONE) == 0) {
1428Sbill 			bp->b_flags |= B_READ;
1438Sbill 			bp->b_bcount = BSIZE;
1448Sbill 			(*bdevsw[major(dev)].d_strategy)(bp);
1452045Swnj #ifdef	EPAWNJ
1462045Swnj 			trace(TR_BREAD|TR_MISS, dev, blkno);
1472045Swnj #endif
1488Sbill #ifdef	DISKMON
1498Sbill 			io_info.nread++;
1508Sbill #endif
1518Sbill 			u.u_vm.vm_inblk++;		/* pay for read */
1528Sbill 		}
1532045Swnj #ifdef	EPAWNJ
1542045Swnj 		else
1552045Swnj 			trace(TR_BREAD|TR_HIT, dev, blkno);
1562045Swnj #endif
1578Sbill 	}
1588Sbill 	if (rablkno && !incore(dev, rablkno)) {
1598Sbill 		rabp = getblk(dev, rablkno);
1602045Swnj 		if (rabp->b_flags & B_DONE) {
1618Sbill 			brelse(rabp);
1622045Swnj #ifdef	EPAWNJ
1632045Swnj 			trace(TR_BREAD|TR_HIT|TR_RA, dev, blkno);
1642045Swnj #endif
1652045Swnj 		} else {
1668Sbill 			rabp->b_flags |= B_READ|B_ASYNC;
1678Sbill 			rabp->b_bcount = BSIZE;
1688Sbill 			(*bdevsw[major(dev)].d_strategy)(rabp);
1692045Swnj #ifdef	EPAWNJ
1702045Swnj 			trace(TR_BREAD|TR_MISS|TR_RA, dev, rablock);
1712045Swnj #endif
1728Sbill #ifdef	DISKMON
1738Sbill 			io_info.nreada++;
1748Sbill #endif
1758Sbill 			u.u_vm.vm_inblk++;		/* pay in advance */
1768Sbill 		}
1778Sbill 	}
1788Sbill 	if(bp == NULL)
1798Sbill 		return(bread(dev, blkno));
1808Sbill 	iowait(bp);
1818Sbill 	return(bp);
1828Sbill }
1838Sbill 
1848Sbill /*
1858Sbill  * Write the buffer, waiting for completion.
1868Sbill  * Then release the buffer.
1878Sbill  */
1888Sbill bwrite(bp)
1898Sbill register struct buf *bp;
1908Sbill {
1918Sbill 	register flag;
1928Sbill 
1938Sbill 	flag = bp->b_flags;
1948Sbill 	bp->b_flags &= ~(B_READ | B_DONE | B_ERROR | B_DELWRI | B_AGE);
1958Sbill 	bp->b_bcount = BSIZE;
1968Sbill #ifdef	DISKMON
1978Sbill 	io_info.nwrite++;
1988Sbill #endif
1998Sbill 	if ((flag&B_DELWRI) == 0)
2008Sbill 		u.u_vm.vm_oublk++;		/* noone paid yet */
2012045Swnj #ifdef	EPAWNJ
2022045Swnj 	trace(TR_BWRITE, bp->b_dev, dbtofsb(bp->b_blkno));
2032045Swnj #endif
2048Sbill 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
2058Sbill 	if ((flag&B_ASYNC) == 0) {
2068Sbill 		iowait(bp);
2078Sbill 		brelse(bp);
2088Sbill 	} else if (flag & B_DELWRI)
2098Sbill 		bp->b_flags |= B_AGE;
2108Sbill 	else
2118Sbill 		geterror(bp);
2128Sbill }
2138Sbill 
2148Sbill /*
2158Sbill  * Release the buffer, marking it so that if it is grabbed
2168Sbill  * for another purpose it will be written out before being
2178Sbill  * given up (e.g. when writing a partial block where it is
2188Sbill  * assumed that another write for the same block will soon follow).
2198Sbill  * This can't be done for magtape, since writes must be done
2208Sbill  * in the same order as requested.
2218Sbill  */
2228Sbill bdwrite(bp)
2238Sbill register struct buf *bp;
2248Sbill {
2258Sbill 	register struct buf *dp;
2268Sbill 
2278Sbill 	if ((bp->b_flags&B_DELWRI) == 0)
2288Sbill 		u.u_vm.vm_oublk++;		/* noone paid yet */
2298Sbill 	dp = bdevsw[major(bp->b_dev)].d_tab;
2308Sbill 	if(dp->b_flags & B_TAPE)
2318Sbill 		bawrite(bp);
2328Sbill 	else {
2338Sbill 		bp->b_flags |= B_DELWRI | B_DONE;
2348Sbill 		brelse(bp);
2358Sbill 	}
2368Sbill }
2378Sbill 
2388Sbill /*
2398Sbill  * Release the buffer, start I/O on it, but don't wait for completion.
2408Sbill  */
2418Sbill bawrite(bp)
2428Sbill register struct buf *bp;
2438Sbill {
2448Sbill 
2458Sbill 	bp->b_flags |= B_ASYNC;
2468Sbill 	bwrite(bp);
2478Sbill }
2488Sbill 
2498Sbill /*
2508Sbill  * release the buffer, with no I/O implied.
2518Sbill  */
2528Sbill brelse(bp)
2538Sbill register struct buf *bp;
2548Sbill {
255*2325Swnj 	register struct buf *flist;
2568Sbill 	register s;
2578Sbill 
2588Sbill 	if (bp->b_flags&B_WANTED)
2598Sbill 		wakeup((caddr_t)bp);
260*2325Swnj 	if (bfreelist[0].b_flags&B_WANTED) {
261*2325Swnj 		bfreelist[0].b_flags &= ~B_WANTED;
262*2325Swnj 		wakeup((caddr_t)bfreelist);
2638Sbill 	}
264*2325Swnj 	if ((bp->b_flags&B_ERROR) && bp->b_dev != NODEV)
2658Sbill 		bp->b_dev = NODEV;  /* no assoc. on error */
2668Sbill 	s = spl6();
267*2325Swnj 	if (bp->b_flags & (B_ERROR|B_INVAL)) {
268*2325Swnj 		/* block has no info ... put at front of most free list */
269*2325Swnj 		flist = &bfreelist[BQUEUES-1];
270*2325Swnj 		flist->av_forw->av_back = bp;
271*2325Swnj 		bp->av_forw = flist->av_forw;
272*2325Swnj 		flist->av_forw = bp;
273*2325Swnj 		bp->av_back = flist;
2748Sbill 	} else {
275*2325Swnj 		if (bp->b_flags & B_LOCKED)
276*2325Swnj 			flist = &bfreelist[BQ_LOCKED];
277*2325Swnj 		else if (bp->b_flags & B_AGE)
278*2325Swnj 			flist = &bfreelist[BQ_AGE];
279*2325Swnj 		else
280*2325Swnj 			flist = &bfreelist[BQ_LRU];
281*2325Swnj 		flist->av_back->av_forw = bp;
282*2325Swnj 		bp->av_back = flist->av_back;
283*2325Swnj 		flist->av_back = bp;
284*2325Swnj 		bp->av_forw = flist;
2858Sbill 	}
2868Sbill 	bp->b_flags &= ~(B_WANTED|B_BUSY|B_ASYNC|B_AGE);
2878Sbill 	splx(s);
2888Sbill }
2898Sbill 
2908Sbill /*
2918Sbill  * See if the block is associated with some buffer
2928Sbill  * (mainly to avoid getting hung up on a wait in breada)
2938Sbill  */
2948Sbill incore(dev, blkno)
2958Sbill dev_t dev;
2968Sbill daddr_t blkno;
2978Sbill {
2988Sbill 	register struct buf *bp;
299*2325Swnj 	register struct buf *dp;
3008Sbill 	register int dblkno = fsbtodb(blkno);
3018Sbill 
302*2325Swnj 	dp = BUFHASH(dev, blkno);
303*2325Swnj 	for (bp = dp->b_forw; bp != dp; bp = bp->b_forw)
304*2325Swnj 		if (bp->b_blkno == dblkno && bp->b_dev == dev &&
305*2325Swnj 		    !(bp->b_flags & B_INVAL))
30691Sbill 			return (1);
30791Sbill 	return (0);
3088Sbill }
3098Sbill 
3108Sbill struct buf *
3118Sbill baddr(dev, blkno)
3128Sbill dev_t dev;
3138Sbill daddr_t blkno;
3148Sbill {
3158Sbill 
3168Sbill 	if (incore(dev, blkno))
3178Sbill 		return (bread(dev, blkno));
3188Sbill 	return (0);
3198Sbill }
3208Sbill 
3218Sbill /*
3228Sbill  * Assign a buffer for the given block.  If the appropriate
3238Sbill  * block is already associated, return it; otherwise search
3248Sbill  * for the oldest non-busy buffer and reassign it.
3258Sbill  */
3268Sbill struct buf *
3278Sbill getblk(dev, blkno)
3288Sbill dev_t dev;
3298Sbill daddr_t blkno;
3308Sbill {
33191Sbill 	register struct buf *bp, *dp, *ep;
332*2325Swnj 	register int i, x;
333*2325Swnj 	register int dblkno = fsbtodb(blkno);
3348Sbill 
3351831Sbill 	if ((unsigned)blkno >= 1 << (sizeof(int)*NBBY-PGSHIFT))
3361831Sbill 		blkno = 1 << ((sizeof(int)*NBBY-PGSHIFT) + 1);
3371831Sbill 	dblkno = fsbtodb(blkno);
338*2325Swnj 	dp = BUFHASH(dev, dblkno);
3398Sbill     loop:
340124Sbill 	(void) spl0();
341*2325Swnj 	for (bp = dp->b_forw; bp != dp; bp = bp->b_forw) {
342*2325Swnj 		if (bp->b_blkno != dblkno || bp->b_dev != dev ||
343*2325Swnj 		    bp->b_flags&B_INVAL)
3448Sbill 			continue;
345124Sbill 		(void) spl6();
3468Sbill 		if (bp->b_flags&B_BUSY) {
3478Sbill 			bp->b_flags |= B_WANTED;
3488Sbill 			sleep((caddr_t)bp, PRIBIO+1);
3498Sbill 			goto loop;
3508Sbill 		}
351124Sbill 		(void) spl0();
3528Sbill #ifdef	DISKMON
3538Sbill 		i = 0;
3548Sbill 		dp = bp->av_forw;
355*2325Swnj 		while ((dp->b_flags & B_HEAD) == 0) {
3568Sbill 			i++;
3578Sbill 			dp = dp->av_forw;
3588Sbill 		}
3598Sbill 		if (i<NBUF)
3608Sbill 			io_info.bufcount[i]++;
3618Sbill #endif
3628Sbill 		notavail(bp);
3638Sbill 		bp->b_flags |= B_CACHE;
3648Sbill 		return(bp);
3658Sbill 	}
36691Sbill 	if (major(dev) >= nblkdev)
36791Sbill 		panic("blkdev");
368124Sbill 	(void) spl6();
369*2325Swnj 	for (ep = &bfreelist[BQUEUES-1]; ep > bfreelist; ep--)
370*2325Swnj 		if (ep->av_forw != ep)
371*2325Swnj 			break;
372*2325Swnj 	if (ep == bfreelist) {		/* no free blocks at all */
373*2325Swnj 		ep->b_flags |= B_WANTED;
374*2325Swnj 		sleep((caddr_t)ep, PRIBIO+1);
3758Sbill 		goto loop;
3768Sbill 	}
3771792Sbill 	(void) spl0();
378*2325Swnj 	bp = ep->av_forw;
3798Sbill 	notavail(bp);
3808Sbill 	if (bp->b_flags & B_DELWRI) {
3818Sbill 		bp->b_flags |= B_ASYNC;
3828Sbill 		bwrite(bp);
3838Sbill 		goto loop;
3848Sbill 	}
3852045Swnj #ifdef EPAWNJ
3862045Swnj 	trace(TR_BRELSE, bp->b_dev, dbtofsb(bp->b_blkno));
3872045Swnj #endif
3888Sbill 	bp->b_flags = B_BUSY;
3898Sbill 	bp->b_back->b_forw = bp->b_forw;
3908Sbill 	bp->b_forw->b_back = bp->b_back;
3918Sbill 	bp->b_forw = dp->b_forw;
3928Sbill 	bp->b_back = dp;
3938Sbill 	dp->b_forw->b_back = bp;
3948Sbill 	dp->b_forw = bp;
3958Sbill 	bp->b_dev = dev;
3968Sbill 	bp->b_blkno = dblkno;
3978Sbill 	return(bp);
3988Sbill }
3998Sbill 
4008Sbill /*
4018Sbill  * get an empty block,
4028Sbill  * not assigned to any particular device
4038Sbill  */
4048Sbill struct buf *
4058Sbill geteblk()
4068Sbill {
407182Sbill 	register struct buf *bp, *dp;
4088Sbill 
4098Sbill loop:
410124Sbill 	(void) spl6();
411*2325Swnj 	for (dp = &bfreelist[BQUEUES-1]; dp > bfreelist; dp--)
412*2325Swnj 		if (dp->av_forw != dp)
413*2325Swnj 			break;
414*2325Swnj 	if (dp == bfreelist) {		/* no free blocks */
415*2325Swnj 		dp->b_flags |= B_WANTED;
416*2325Swnj 		sleep((caddr_t)dp, PRIBIO+1);
417*2325Swnj 		goto loop;
4188Sbill 	}
419124Sbill 	(void) spl0();
420*2325Swnj 	bp = dp->av_forw;
4218Sbill 	notavail(bp);
4228Sbill 	if (bp->b_flags & B_DELWRI) {
4238Sbill 		bp->b_flags |= B_ASYNC;
4248Sbill 		bwrite(bp);
4258Sbill 		goto loop;
4268Sbill 	}
4272045Swnj #ifdef EPAWNJ
428*2325Swnj 	trace(TR_BRELSE, bp->b_dev, dbtofsb(bp->b_blkno));
4292045Swnj #endif
430*2325Swnj 	bp->b_flags = B_BUSY|B_INVAL;
4318Sbill 	bp->b_back->b_forw = bp->b_forw;
4328Sbill 	bp->b_forw->b_back = bp->b_back;
4338Sbill 	bp->b_forw = dp->b_forw;
4348Sbill 	bp->b_back = dp;
4358Sbill 	dp->b_forw->b_back = bp;
4368Sbill 	dp->b_forw = bp;
4378Sbill 	bp->b_dev = (dev_t)NODEV;
43891Sbill 	bp->b_hlink = -1;
4398Sbill 	return(bp);
4408Sbill }
4418Sbill 
4428Sbill /*
4438Sbill  * Wait for I/O completion on the buffer; return errors
4448Sbill  * to the user.
4458Sbill  */
4468Sbill iowait(bp)
4478Sbill register struct buf *bp;
4488Sbill {
4498Sbill 
450124Sbill 	(void) spl6();
4518Sbill 	while ((bp->b_flags&B_DONE)==0)
4528Sbill 		sleep((caddr_t)bp, PRIBIO);
453124Sbill 	(void) spl0();
4548Sbill 	geterror(bp);
4558Sbill }
4568Sbill 
4578Sbill #ifndef FASTVAX
4588Sbill /*
4598Sbill  * Unlink a buffer from the available list and mark it busy.
4608Sbill  * (internal interface)
4618Sbill  */
4628Sbill notavail(bp)
4638Sbill register struct buf *bp;
4648Sbill {
4658Sbill 	register s;
4668Sbill 
4678Sbill 	s = spl6();
4688Sbill 	bp->av_back->av_forw = bp->av_forw;
4698Sbill 	bp->av_forw->av_back = bp->av_back;
4708Sbill 	bp->b_flags |= B_BUSY;
4718Sbill 	splx(s);
4728Sbill }
4738Sbill #endif
4748Sbill 
4758Sbill /*
4768Sbill  * Mark I/O complete on a buffer. If the header
4778Sbill  * indicates a dirty page push completion, the
4788Sbill  * header is inserted into the ``cleaned'' list
4798Sbill  * to be processed by the pageout daemon. Otherwise
4808Sbill  * release it if I/O is asynchronous, and wake
4818Sbill  * up anyone waiting for it.
4828Sbill  */
4838Sbill iodone(bp)
4848Sbill register struct buf *bp;
4858Sbill {
4868Sbill 	register int s;
4878Sbill 
488420Sbill 	if (bp->b_flags & B_DONE)
489420Sbill 		panic("dup iodone");
4908Sbill 	bp->b_flags |= B_DONE;
4918Sbill 	if (bp->b_flags & B_DIRTY) {
4928Sbill 		if (bp->b_flags & B_ERROR)
4938Sbill 			panic("IO err in push");
4948Sbill 		s = spl6();
4958Sbill 		cnt.v_pgout++;
4968Sbill 		bp->av_forw = bclnlist;
4978Sbill 		bp->b_bcount = swsize[bp - swbuf];
4988Sbill 		bp->b_pfcent = swpf[bp - swbuf];
4998Sbill 		bclnlist = bp;
5008Sbill 		if (bswlist.b_flags & B_WANTED)
5018Sbill 			wakeup((caddr_t)&proc[2]);
5028Sbill 		splx(s);
503383Sbill 		return;
5048Sbill 	}
5058Sbill 	if (bp->b_flags&B_ASYNC)
5068Sbill 		brelse(bp);
5078Sbill 	else {
5088Sbill 		bp->b_flags &= ~B_WANTED;
5098Sbill 		wakeup((caddr_t)bp);
5108Sbill 	}
5118Sbill }
5128Sbill 
5138Sbill /*
5148Sbill  * Zero the core associated with a buffer.
5158Sbill  */
5168Sbill clrbuf(bp)
5178Sbill struct buf *bp;
5188Sbill {
5198Sbill 	register *p;
5208Sbill 	register c;
5218Sbill 
5228Sbill 	p = bp->b_un.b_words;
5238Sbill 	c = BSIZE/sizeof(int);
5248Sbill 	do
5258Sbill 		*p++ = 0;
5268Sbill 	while (--c);
5278Sbill 	bp->b_resid = 0;
5288Sbill }
5298Sbill 
5308Sbill /*
5318Sbill  * swap I/O -
5328Sbill  *
5338Sbill  * If the flag indicates a dirty page push initiated
5348Sbill  * by the pageout daemon, we map the page into the i th
5358Sbill  * virtual page of process 2 (the daemon itself) where i is
5368Sbill  * the index of the swap header that has been allocated.
5378Sbill  * We simply initialize the header and queue the I/O but
5388Sbill  * do not wait for completion. When the I/O completes,
5398Sbill  * iodone() will link the header to a list of cleaned
5408Sbill  * pages to be processed by the pageout daemon.
5418Sbill  */
5428Sbill swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent)
5438Sbill 	struct proc *p;
5448Sbill 	swblk_t dblkno;
5458Sbill 	caddr_t addr;
5468Sbill 	int flag, nbytes;
5478Sbill 	dev_t dev;
5488Sbill 	unsigned pfcent;
5498Sbill {
5508Sbill 	register struct buf *bp;
5518Sbill 	register int c;
5528Sbill 	int p2dp;
5538Sbill 	register struct pte *dpte, *vpte;
5548Sbill 
555124Sbill 	(void) spl6();
5568Sbill 	while (bswlist.av_forw == NULL) {
5578Sbill 		bswlist.b_flags |= B_WANTED;
5588Sbill 		sleep((caddr_t)&bswlist, PSWP+1);
5598Sbill 	}
5608Sbill 	bp = bswlist.av_forw;
5618Sbill 	bswlist.av_forw = bp->av_forw;
562124Sbill 	(void) spl0();
5638Sbill 
5648Sbill 	bp->b_flags = B_BUSY | B_PHYS | rdflg | flag;
5658Sbill 	if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0)
5668Sbill 		if (rdflg == B_READ)
5678Sbill 			sum.v_pswpin += btoc(nbytes);
5688Sbill 		else
5698Sbill 			sum.v_pswpout += btoc(nbytes);
5708Sbill 	bp->b_proc = p;
5718Sbill 	if (flag & B_DIRTY) {
5728Sbill 		p2dp = ((bp - swbuf) * CLSIZE) * KLMAX;
5738Sbill 		dpte = dptopte(&proc[2], p2dp);
5748Sbill 		vpte = vtopte(p, btop(addr));
5758Sbill 		for (c = 0; c < nbytes; c += NBPG) {
5768Sbill 			if (vpte->pg_pfnum == 0 || vpte->pg_fod)
5778Sbill 				panic("swap bad pte");
5788Sbill 			*dpte++ = *vpte++;
5798Sbill 		}
5808Sbill 		bp->b_un.b_addr = (caddr_t)ctob(p2dp);
5818Sbill 	} else
5828Sbill 		bp->b_un.b_addr = addr;
5838Sbill 	while (nbytes > 0) {
5848Sbill 		c = imin(ctob(120), nbytes);
5858Sbill 		bp->b_bcount = c;
5868Sbill 		bp->b_blkno = dblkno;
5878Sbill 		bp->b_dev = dev;
588718Sbill 		if (flag & B_DIRTY) {
589718Sbill 			swpf[bp - swbuf] = pfcent;
590718Sbill 			swsize[bp - swbuf] = nbytes;
591718Sbill 		}
5928Sbill 		(*bdevsw[major(dev)].d_strategy)(bp);
5938Sbill 		if (flag & B_DIRTY) {
5948Sbill 			if (c < nbytes)
5958Sbill 				panic("big push");
5968Sbill 			return;
5978Sbill 		}
598124Sbill 		(void) spl6();
5998Sbill 		while((bp->b_flags&B_DONE)==0)
6008Sbill 			sleep((caddr_t)bp, PSWP);
601124Sbill 		(void) spl0();
6028Sbill 		bp->b_un.b_addr += c;
6038Sbill 		bp->b_flags &= ~B_DONE;
6048Sbill 		if (bp->b_flags & B_ERROR) {
6058Sbill 			if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
6068Sbill 				panic("hard IO err in swap");
6078Sbill 			swkill(p, (char *)0);
6088Sbill 		}
6098Sbill 		nbytes -= c;
6108Sbill 		dblkno += btoc(c);
6118Sbill 	}
612124Sbill 	(void) spl6();
6138Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
6148Sbill 	bp->av_forw = bswlist.av_forw;
6158Sbill 	bswlist.av_forw = bp;
6168Sbill 	if (bswlist.b_flags & B_WANTED) {
6178Sbill 		bswlist.b_flags &= ~B_WANTED;
6188Sbill 		wakeup((caddr_t)&bswlist);
6198Sbill 		wakeup((caddr_t)&proc[2]);
6208Sbill 	}
621124Sbill 	(void) spl0();
6228Sbill }
6238Sbill 
6248Sbill /*
6258Sbill  * If rout == 0 then killed on swap error, else
6268Sbill  * rout is the name of the routine where we ran out of
6278Sbill  * swap space.
6288Sbill  */
6298Sbill swkill(p, rout)
6308Sbill 	struct proc *p;
6318Sbill 	char *rout;
6328Sbill {
6338Sbill 
6348Sbill 	printf("%d: ", p->p_pid);
6358Sbill 	if (rout)
6368Sbill 		printf("out of swap space in %s\n", rout);
6378Sbill 	else
6388Sbill 		printf("killed on swap error\n");
6398Sbill 	/*
6408Sbill 	 * To be sure no looping (e.g. in vmsched trying to
6418Sbill 	 * swap out) mark process locked in core (as though
6428Sbill 	 * done by user) after killing it so noone will try
6438Sbill 	 * to swap it out.
6448Sbill 	 */
645165Sbill 	psignal(p, SIGKILL);
6468Sbill 	p->p_flag |= SULOCK;
6478Sbill }
6488Sbill 
6498Sbill /*
6508Sbill  * make sure all write-behind blocks
6518Sbill  * on dev (or NODEV for all)
6528Sbill  * are flushed out.
6538Sbill  * (from umount and update)
6548Sbill  */
6558Sbill bflush(dev)
6568Sbill dev_t dev;
6578Sbill {
6588Sbill 	register struct buf *bp;
659*2325Swnj 	register struct buf *flist;
6608Sbill 
6618Sbill loop:
662124Sbill 	(void) spl6();
663*2325Swnj 	for (flist = bfreelist; flist < &bfreelist[BQUEUES]; flist++)
664*2325Swnj 	for (bp = flist->av_forw; bp != flist; bp = bp->av_forw) {
6658Sbill 		if (bp->b_flags&B_DELWRI && (dev == NODEV||dev==bp->b_dev)) {
6668Sbill 			bp->b_flags |= B_ASYNC;
6678Sbill 			notavail(bp);
6688Sbill 			bwrite(bp);
6698Sbill 			goto loop;
6708Sbill 		}
6718Sbill 	}
672124Sbill 	(void) spl0();
6738Sbill }
6748Sbill 
6758Sbill /*
6768Sbill  * Raw I/O. The arguments are
6778Sbill  *	The strategy routine for the device
6788Sbill  *	A buffer, which will always be a special buffer
6798Sbill  *	  header owned exclusively by the device for this purpose
6808Sbill  *	The device number
6818Sbill  *	Read/write flag
6828Sbill  * Essentially all the work is computing physical addresses and
6838Sbill  * validating them.
6848Sbill  * If the user has the proper access privilidges, the process is
6858Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
6868Sbill  * faulted and locked. After the completion of the I/O, the above pages
6878Sbill  * are unlocked.
6888Sbill  */
6898Sbill physio(strat, bp, dev, rw, mincnt)
6908Sbill int (*strat)();
6918Sbill register struct buf *bp;
6928Sbill unsigned (*mincnt)();
6938Sbill {
6948Sbill 	register int c;
6958Sbill 	char *a;
6968Sbill 
6978Sbill 	if (useracc(u.u_base,u.u_count,rw==B_READ?B_WRITE:B_READ) == NULL) {
6988Sbill 		u.u_error = EFAULT;
6998Sbill 		return;
7008Sbill 	}
701124Sbill 	(void) spl6();
7028Sbill 	while (bp->b_flags&B_BUSY) {
7038Sbill 		bp->b_flags |= B_WANTED;
7048Sbill 		sleep((caddr_t)bp, PRIBIO+1);
7058Sbill 	}
7068Sbill 	bp->b_error = 0;
7078Sbill 	bp->b_proc = u.u_procp;
7088Sbill 	bp->b_un.b_addr = u.u_base;
7098Sbill 	while (u.u_count != 0 && bp->b_error==0) {
7108Sbill 		bp->b_flags = B_BUSY | B_PHYS | rw;
7118Sbill 		bp->b_dev = dev;
7128Sbill 		bp->b_blkno = u.u_offset >> PGSHIFT;
7138Sbill 		bp->b_bcount = u.u_count;
7148Sbill 		(*mincnt)(bp);
7158Sbill 		c = bp->b_bcount;
7168Sbill 		u.u_procp->p_flag |= SPHYSIO;
7178Sbill 		vslock(a = bp->b_un.b_addr, c);
7188Sbill 		(*strat)(bp);
719124Sbill 		(void) spl6();
7208Sbill 		while ((bp->b_flags&B_DONE) == 0)
7218Sbill 			sleep((caddr_t)bp, PRIBIO);
7228Sbill 		vsunlock(a, c, rw);
7238Sbill 		u.u_procp->p_flag &= ~SPHYSIO;
7248Sbill 		if (bp->b_flags&B_WANTED)
7258Sbill 			wakeup((caddr_t)bp);
726124Sbill 		(void) spl0();
7278Sbill 		bp->b_un.b_addr += c;
7288Sbill 		u.u_count -= c;
7298Sbill 		u.u_offset += c;
7308Sbill 	}
7318Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
7328Sbill 	u.u_count = bp->b_resid;
7338Sbill 	geterror(bp);
7348Sbill }
7358Sbill 
7368Sbill /*ARGSUSED*/
7378Sbill unsigned
7388Sbill minphys(bp)
7398Sbill struct buf *bp;
7408Sbill {
7418Sbill 
7428Sbill 	if (bp->b_bcount > 60 * 1024)
7438Sbill 		bp->b_bcount = 60 * 1024;
7448Sbill }
7458Sbill 
7468Sbill /*
7478Sbill  * Pick up the device's error number and pass it to the user;
7488Sbill  * if there is an error but the number is 0 set a generalized
7498Sbill  * code.  Actually the latter is always true because devices
7508Sbill  * don't yet return specific errors.
7518Sbill  */
7528Sbill geterror(bp)
7538Sbill register struct buf *bp;
7548Sbill {
7558Sbill 
7568Sbill 	if (bp->b_flags&B_ERROR)
7578Sbill 		if ((u.u_error = bp->b_error)==0)
7588Sbill 			u.u_error = EIO;
7598Sbill }
7602299Skre 
7612299Skre /*
7622299Skre  * Invalidate in core blocks belonging to closed or umounted filesystem
7632299Skre  *
7642299Skre  * This is not nicely done at all - the buffer ought to be removed from the
7652299Skre  * hash chains & have its dev/blkno fields clobbered, but unfortunately we
7662299Skre  * can't do that here, as it is quite possible that the block is still
7672299Skre  * being used for i/o. Eventually, all disc drivers should be forced to
7682299Skre  * have a close routine, which ought ensure that the queue is empty, then
7692299Skre  * properly flush the queues. Until that happy day, this suffices for
7702299Skre  * correctness.						... kre
7712299Skre  */
7722299Skre binval(dev)
7732299Skre dev_t dev;
7742299Skre {
7752299Skre 	register struct buf *bp, *dp;
7762299Skre 
7772299Skre 	dp = bdevsw[major(dev)].d_tab;
7782299Skre 	for (bp = dp->b_forw; bp != dp; bp = bp->b_forw)
7792299Skre 		if (bp->b_dev == dev)
7802299Skre 			bp->b_flags |= B_INVAL;
7812299Skre }
782