xref: /csrg-svn/sys/kern/vfs_cluster.c (revision 3099)
1*3099Swnj /*	vfs_cluster.c	4.16	03/09/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 
37*3099Swnj struct	buf bfreelist[BQUEUES];
38*3099Swnj struct	buf bswlist, *bclnlist;
39*3099Swnj 
4091Sbill #define	BUFHSZ	63
412325Swnj struct	bufhd bufhash[BUFHSZ];
422328Swnj #define	BUFHASH(dev, dblkno)	\
432328Swnj 		((struct buf *)&bufhash[((int)(dev)+(int)(dblkno)) % BUFHSZ])
4491Sbill 
4591Sbill /*
4691Sbill  * Initialize hash links for buffers.
4791Sbill  */
4891Sbill bhinit()
4991Sbill {
5091Sbill 	register int i;
512325Swnj 	register struct bufhd *bp;
5291Sbill 
532325Swnj 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
542325Swnj 		bp->b_forw = bp->b_back = (struct buf *)bp;
5591Sbill }
5691Sbill 
578Sbill /* #define	DISKMON	1 */
588Sbill 
598Sbill #ifdef	DISKMON
608Sbill struct {
618Sbill 	int	nbuf;
628Sbill 	long	nread;
638Sbill 	long	nreada;
648Sbill 	long	ncache;
658Sbill 	long	nwrite;
662771Swnj 	long	bufcount[64];
678Sbill } io_info;
688Sbill #endif
698Sbill 
708Sbill /*
718Sbill  * Swap IO headers -
728Sbill  * They contain the necessary information for the swap I/O.
738Sbill  * At any given time, a swap header can be in three
748Sbill  * different lists. When free it is in the free list,
758Sbill  * when allocated and the I/O queued, it is on the swap
768Sbill  * device list, and finally, if the operation was a dirty
778Sbill  * page push, when the I/O completes, it is inserted
788Sbill  * in a list of cleaned pages to be processed by the pageout daemon.
798Sbill  */
802771Swnj struct	buf *swbuf;
812771Swnj short	*swsize;		/* CAN WE JUST USE B_BCOUNT? */
822771Swnj int	*swpf;
838Sbill 
848Sbill 
852706Swnj #ifndef	UNFAST
868Sbill #define	notavail(bp) \
878Sbill { \
888Sbill 	int s = spl6(); \
898Sbill 	(bp)->av_back->av_forw = (bp)->av_forw; \
908Sbill 	(bp)->av_forw->av_back = (bp)->av_back; \
918Sbill 	(bp)->b_flags |= B_BUSY; \
928Sbill 	splx(s); \
938Sbill }
948Sbill #endif
958Sbill 
968Sbill /*
978Sbill  * Read in (if necessary) the block and return a buffer pointer.
988Sbill  */
998Sbill struct buf *
1008Sbill bread(dev, blkno)
1018Sbill dev_t dev;
1028Sbill daddr_t blkno;
1038Sbill {
1048Sbill 	register struct buf *bp;
1058Sbill 
1068Sbill 	bp = getblk(dev, blkno);
1078Sbill 	if (bp->b_flags&B_DONE) {
1082045Swnj #ifdef	EPAWNJ
1092045Swnj 		trace(TR_BREAD|TR_HIT, dev, blkno);
1102045Swnj #endif
1118Sbill #ifdef	DISKMON
1128Sbill 		io_info.ncache++;
1138Sbill #endif
1148Sbill 		return(bp);
1158Sbill 	}
1168Sbill 	bp->b_flags |= B_READ;
1178Sbill 	bp->b_bcount = BSIZE;
1188Sbill 	(*bdevsw[major(dev)].d_strategy)(bp);
1192045Swnj #ifdef	EPAWNJ
1202045Swnj 	trace(TR_BREAD|TR_MISS, dev, blkno);
1212045Swnj #endif
1228Sbill #ifdef	DISKMON
1238Sbill 	io_info.nread++;
1248Sbill #endif
1258Sbill 	u.u_vm.vm_inblk++;		/* pay for read */
1268Sbill 	iowait(bp);
1278Sbill 	return(bp);
1288Sbill }
1298Sbill 
1308Sbill /*
1318Sbill  * Read in the block, like bread, but also start I/O on the
1328Sbill  * read-ahead block (which is not allocated to the caller)
1338Sbill  */
1348Sbill struct buf *
1358Sbill breada(dev, blkno, rablkno)
1368Sbill dev_t dev;
1378Sbill daddr_t blkno, rablkno;
1388Sbill {
1398Sbill 	register struct buf *bp, *rabp;
1408Sbill 
1418Sbill 	bp = NULL;
1428Sbill 	if (!incore(dev, blkno)) {
1438Sbill 		bp = getblk(dev, blkno);
1448Sbill 		if ((bp->b_flags&B_DONE) == 0) {
1458Sbill 			bp->b_flags |= B_READ;
1468Sbill 			bp->b_bcount = BSIZE;
1478Sbill 			(*bdevsw[major(dev)].d_strategy)(bp);
1482045Swnj #ifdef	EPAWNJ
1492045Swnj 			trace(TR_BREAD|TR_MISS, dev, blkno);
1502045Swnj #endif
1518Sbill #ifdef	DISKMON
1528Sbill 			io_info.nread++;
1538Sbill #endif
1548Sbill 			u.u_vm.vm_inblk++;		/* pay for read */
1558Sbill 		}
1562045Swnj #ifdef	EPAWNJ
1572045Swnj 		else
1582045Swnj 			trace(TR_BREAD|TR_HIT, dev, blkno);
1592045Swnj #endif
1608Sbill 	}
1618Sbill 	if (rablkno && !incore(dev, rablkno)) {
1628Sbill 		rabp = getblk(dev, rablkno);
1632045Swnj 		if (rabp->b_flags & B_DONE) {
1648Sbill 			brelse(rabp);
1652045Swnj #ifdef	EPAWNJ
1662045Swnj 			trace(TR_BREAD|TR_HIT|TR_RA, dev, blkno);
1672045Swnj #endif
1682045Swnj 		} else {
1698Sbill 			rabp->b_flags |= B_READ|B_ASYNC;
1708Sbill 			rabp->b_bcount = BSIZE;
1718Sbill 			(*bdevsw[major(dev)].d_strategy)(rabp);
1722045Swnj #ifdef	EPAWNJ
1732045Swnj 			trace(TR_BREAD|TR_MISS|TR_RA, dev, rablock);
1742045Swnj #endif
1758Sbill #ifdef	DISKMON
1768Sbill 			io_info.nreada++;
1778Sbill #endif
1788Sbill 			u.u_vm.vm_inblk++;		/* pay in advance */
1798Sbill 		}
1808Sbill 	}
1818Sbill 	if(bp == NULL)
1828Sbill 		return(bread(dev, blkno));
1838Sbill 	iowait(bp);
1848Sbill 	return(bp);
1858Sbill }
1868Sbill 
1878Sbill /*
1888Sbill  * Write the buffer, waiting for completion.
1898Sbill  * Then release the buffer.
1908Sbill  */
1918Sbill bwrite(bp)
1928Sbill register struct buf *bp;
1938Sbill {
1948Sbill 	register flag;
1958Sbill 
1968Sbill 	flag = bp->b_flags;
1978Sbill 	bp->b_flags &= ~(B_READ | B_DONE | B_ERROR | B_DELWRI | B_AGE);
1988Sbill 	bp->b_bcount = BSIZE;
1998Sbill #ifdef	DISKMON
2008Sbill 	io_info.nwrite++;
2018Sbill #endif
2028Sbill 	if ((flag&B_DELWRI) == 0)
2038Sbill 		u.u_vm.vm_oublk++;		/* noone paid yet */
2042045Swnj #ifdef	EPAWNJ
2052045Swnj 	trace(TR_BWRITE, bp->b_dev, dbtofsb(bp->b_blkno));
2062045Swnj #endif
2078Sbill 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
2088Sbill 	if ((flag&B_ASYNC) == 0) {
2098Sbill 		iowait(bp);
2108Sbill 		brelse(bp);
2118Sbill 	} else if (flag & B_DELWRI)
2128Sbill 		bp->b_flags |= B_AGE;
2138Sbill 	else
2148Sbill 		geterror(bp);
2158Sbill }
2168Sbill 
2178Sbill /*
2188Sbill  * Release the buffer, marking it so that if it is grabbed
2198Sbill  * for another purpose it will be written out before being
2208Sbill  * given up (e.g. when writing a partial block where it is
2218Sbill  * assumed that another write for the same block will soon follow).
2228Sbill  * This can't be done for magtape, since writes must be done
2238Sbill  * in the same order as requested.
2248Sbill  */
2258Sbill bdwrite(bp)
2268Sbill register struct buf *bp;
2278Sbill {
2282403Skre 	register int flags;
2298Sbill 
2308Sbill 	if ((bp->b_flags&B_DELWRI) == 0)
2318Sbill 		u.u_vm.vm_oublk++;		/* noone paid yet */
2322403Skre 	flags = bdevsw[major(bp->b_dev)].d_flags;
2332403Skre 	if(flags & B_TAPE)
2348Sbill 		bawrite(bp);
2358Sbill 	else {
2368Sbill 		bp->b_flags |= B_DELWRI | B_DONE;
2378Sbill 		brelse(bp);
2388Sbill 	}
2398Sbill }
2408Sbill 
2418Sbill /*
2428Sbill  * Release the buffer, start I/O on it, but don't wait for completion.
2438Sbill  */
2448Sbill bawrite(bp)
2458Sbill register struct buf *bp;
2468Sbill {
2478Sbill 
2488Sbill 	bp->b_flags |= B_ASYNC;
2498Sbill 	bwrite(bp);
2508Sbill }
2518Sbill 
2528Sbill /*
2538Sbill  * release the buffer, with no I/O implied.
2548Sbill  */
2558Sbill brelse(bp)
2568Sbill register struct buf *bp;
2578Sbill {
2582325Swnj 	register struct buf *flist;
2598Sbill 	register s;
2608Sbill 
2618Sbill 	if (bp->b_flags&B_WANTED)
2628Sbill 		wakeup((caddr_t)bp);
2632325Swnj 	if (bfreelist[0].b_flags&B_WANTED) {
2642325Swnj 		bfreelist[0].b_flags &= ~B_WANTED;
2652325Swnj 		wakeup((caddr_t)bfreelist);
2668Sbill 	}
2672683Swnj 	if (bp->b_flags&B_ERROR)
2682683Swnj 		if (bp->b_flags & B_LOCKED)
2692683Swnj 			bp->b_flags &= ~B_ERROR;	/* try again later */
2702683Swnj 		else
2712683Swnj 			bp->b_dev = NODEV;  		/* no assoc */
2728Sbill 	s = spl6();
2732325Swnj 	if (bp->b_flags & (B_ERROR|B_INVAL)) {
2742325Swnj 		/* block has no info ... put at front of most free list */
2752325Swnj 		flist = &bfreelist[BQUEUES-1];
2762325Swnj 		flist->av_forw->av_back = bp;
2772325Swnj 		bp->av_forw = flist->av_forw;
2782325Swnj 		flist->av_forw = bp;
2792325Swnj 		bp->av_back = flist;
2808Sbill 	} else {
2812325Swnj 		if (bp->b_flags & B_LOCKED)
2822325Swnj 			flist = &bfreelist[BQ_LOCKED];
2832325Swnj 		else if (bp->b_flags & B_AGE)
2842325Swnj 			flist = &bfreelist[BQ_AGE];
2852325Swnj 		else
2862325Swnj 			flist = &bfreelist[BQ_LRU];
2872325Swnj 		flist->av_back->av_forw = bp;
2882325Swnj 		bp->av_back = flist->av_back;
2892325Swnj 		flist->av_back = bp;
2902325Swnj 		bp->av_forw = flist;
2918Sbill 	}
2928Sbill 	bp->b_flags &= ~(B_WANTED|B_BUSY|B_ASYNC|B_AGE);
2938Sbill 	splx(s);
2948Sbill }
2958Sbill 
2968Sbill /*
2978Sbill  * See if the block is associated with some buffer
2988Sbill  * (mainly to avoid getting hung up on a wait in breada)
2998Sbill  */
3008Sbill incore(dev, blkno)
3018Sbill dev_t dev;
3028Sbill daddr_t blkno;
3038Sbill {
3048Sbill 	register struct buf *bp;
3052325Swnj 	register struct buf *dp;
3068Sbill 	register int dblkno = fsbtodb(blkno);
3078Sbill 
3082328Swnj 	dp = BUFHASH(dev, dblkno);
3092325Swnj 	for (bp = dp->b_forw; bp != dp; bp = bp->b_forw)
3102325Swnj 		if (bp->b_blkno == dblkno && bp->b_dev == dev &&
3112325Swnj 		    !(bp->b_flags & B_INVAL))
31291Sbill 			return (1);
31391Sbill 	return (0);
3148Sbill }
3158Sbill 
3168Sbill struct buf *
3178Sbill baddr(dev, blkno)
3188Sbill dev_t dev;
3198Sbill daddr_t blkno;
3208Sbill {
3218Sbill 
3228Sbill 	if (incore(dev, blkno))
3238Sbill 		return (bread(dev, blkno));
3248Sbill 	return (0);
3258Sbill }
3268Sbill 
3278Sbill /*
3288Sbill  * Assign a buffer for the given block.  If the appropriate
3298Sbill  * block is already associated, return it; otherwise search
3308Sbill  * for the oldest non-busy buffer and reassign it.
3318Sbill  */
3328Sbill struct buf *
3338Sbill getblk(dev, blkno)
3348Sbill dev_t dev;
3358Sbill daddr_t blkno;
3368Sbill {
33791Sbill 	register struct buf *bp, *dp, *ep;
3382325Swnj 	register int dblkno = fsbtodb(blkno);
3392423Skre #ifdef	DISKMON
3402423Skre 	register int i;
3412423Skre #endif
3428Sbill 
3431831Sbill 	if ((unsigned)blkno >= 1 << (sizeof(int)*NBBY-PGSHIFT))
3441831Sbill 		blkno = 1 << ((sizeof(int)*NBBY-PGSHIFT) + 1);
3451831Sbill 	dblkno = fsbtodb(blkno);
3462325Swnj 	dp = BUFHASH(dev, dblkno);
3478Sbill     loop:
348124Sbill 	(void) spl0();
3492325Swnj 	for (bp = dp->b_forw; bp != dp; bp = bp->b_forw) {
3502325Swnj 		if (bp->b_blkno != dblkno || bp->b_dev != dev ||
3512325Swnj 		    bp->b_flags&B_INVAL)
3528Sbill 			continue;
353124Sbill 		(void) spl6();
3548Sbill 		if (bp->b_flags&B_BUSY) {
3558Sbill 			bp->b_flags |= B_WANTED;
3568Sbill 			sleep((caddr_t)bp, PRIBIO+1);
3578Sbill 			goto loop;
3588Sbill 		}
359124Sbill 		(void) spl0();
3608Sbill #ifdef	DISKMON
3618Sbill 		i = 0;
3628Sbill 		dp = bp->av_forw;
3632325Swnj 		while ((dp->b_flags & B_HEAD) == 0) {
3648Sbill 			i++;
3658Sbill 			dp = dp->av_forw;
3668Sbill 		}
3672771Swnj 		if (i<64)
3688Sbill 			io_info.bufcount[i]++;
3698Sbill #endif
3708Sbill 		notavail(bp);
3718Sbill 		bp->b_flags |= B_CACHE;
3728Sbill 		return(bp);
3738Sbill 	}
37491Sbill 	if (major(dev) >= nblkdev)
37591Sbill 		panic("blkdev");
376124Sbill 	(void) spl6();
3772325Swnj 	for (ep = &bfreelist[BQUEUES-1]; ep > bfreelist; ep--)
3782325Swnj 		if (ep->av_forw != ep)
3792325Swnj 			break;
3802325Swnj 	if (ep == bfreelist) {		/* no free blocks at all */
3812325Swnj 		ep->b_flags |= B_WANTED;
3822325Swnj 		sleep((caddr_t)ep, PRIBIO+1);
3838Sbill 		goto loop;
3848Sbill 	}
3851792Sbill 	(void) spl0();
3862325Swnj 	bp = ep->av_forw;
3878Sbill 	notavail(bp);
3888Sbill 	if (bp->b_flags & B_DELWRI) {
3898Sbill 		bp->b_flags |= B_ASYNC;
3908Sbill 		bwrite(bp);
3918Sbill 		goto loop;
3928Sbill 	}
3932045Swnj #ifdef EPAWNJ
3942045Swnj 	trace(TR_BRELSE, bp->b_dev, dbtofsb(bp->b_blkno));
3952045Swnj #endif
3968Sbill 	bp->b_flags = B_BUSY;
3978Sbill 	bp->b_back->b_forw = bp->b_forw;
3988Sbill 	bp->b_forw->b_back = bp->b_back;
3998Sbill 	bp->b_forw = dp->b_forw;
4008Sbill 	bp->b_back = dp;
4018Sbill 	dp->b_forw->b_back = bp;
4028Sbill 	dp->b_forw = bp;
4038Sbill 	bp->b_dev = dev;
4048Sbill 	bp->b_blkno = dblkno;
4058Sbill 	return(bp);
4068Sbill }
4078Sbill 
4088Sbill /*
4098Sbill  * get an empty block,
4108Sbill  * not assigned to any particular device
4118Sbill  */
4128Sbill struct buf *
4138Sbill geteblk()
4148Sbill {
415182Sbill 	register struct buf *bp, *dp;
4168Sbill 
4178Sbill loop:
418124Sbill 	(void) spl6();
4192325Swnj 	for (dp = &bfreelist[BQUEUES-1]; dp > bfreelist; dp--)
4202325Swnj 		if (dp->av_forw != dp)
4212325Swnj 			break;
4222325Swnj 	if (dp == bfreelist) {		/* no free blocks */
4232325Swnj 		dp->b_flags |= B_WANTED;
4242325Swnj 		sleep((caddr_t)dp, PRIBIO+1);
4252325Swnj 		goto loop;
4268Sbill 	}
427124Sbill 	(void) spl0();
4282325Swnj 	bp = dp->av_forw;
4298Sbill 	notavail(bp);
4308Sbill 	if (bp->b_flags & B_DELWRI) {
4318Sbill 		bp->b_flags |= B_ASYNC;
4328Sbill 		bwrite(bp);
4338Sbill 		goto loop;
4348Sbill 	}
4352045Swnj #ifdef EPAWNJ
4362325Swnj 	trace(TR_BRELSE, bp->b_dev, dbtofsb(bp->b_blkno));
4372045Swnj #endif
4382325Swnj 	bp->b_flags = B_BUSY|B_INVAL;
4398Sbill 	bp->b_back->b_forw = bp->b_forw;
4408Sbill 	bp->b_forw->b_back = bp->b_back;
4418Sbill 	bp->b_forw = dp->b_forw;
4428Sbill 	bp->b_back = dp;
4438Sbill 	dp->b_forw->b_back = bp;
4448Sbill 	dp->b_forw = bp;
4458Sbill 	bp->b_dev = (dev_t)NODEV;
4468Sbill 	return(bp);
4478Sbill }
4488Sbill 
4498Sbill /*
4508Sbill  * Wait for I/O completion on the buffer; return errors
4518Sbill  * to the user.
4528Sbill  */
4538Sbill iowait(bp)
4548Sbill register struct buf *bp;
4558Sbill {
4568Sbill 
457124Sbill 	(void) spl6();
4588Sbill 	while ((bp->b_flags&B_DONE)==0)
4598Sbill 		sleep((caddr_t)bp, PRIBIO);
460124Sbill 	(void) spl0();
4618Sbill 	geterror(bp);
4628Sbill }
4638Sbill 
4642706Swnj #ifdef UNFAST
4658Sbill /*
4668Sbill  * Unlink a buffer from the available list and mark it busy.
4678Sbill  * (internal interface)
4688Sbill  */
4698Sbill notavail(bp)
4708Sbill register struct buf *bp;
4718Sbill {
4728Sbill 	register s;
4738Sbill 
4748Sbill 	s = spl6();
4758Sbill 	bp->av_back->av_forw = bp->av_forw;
4768Sbill 	bp->av_forw->av_back = bp->av_back;
4778Sbill 	bp->b_flags |= B_BUSY;
4788Sbill 	splx(s);
4798Sbill }
4808Sbill #endif
4818Sbill 
4828Sbill /*
4838Sbill  * Mark I/O complete on a buffer. If the header
4848Sbill  * indicates a dirty page push completion, the
4858Sbill  * header is inserted into the ``cleaned'' list
4868Sbill  * to be processed by the pageout daemon. Otherwise
4878Sbill  * release it if I/O is asynchronous, and wake
4888Sbill  * up anyone waiting for it.
4898Sbill  */
4908Sbill iodone(bp)
4918Sbill register struct buf *bp;
4928Sbill {
4938Sbill 	register int s;
4948Sbill 
495420Sbill 	if (bp->b_flags & B_DONE)
496420Sbill 		panic("dup iodone");
4978Sbill 	bp->b_flags |= B_DONE;
4988Sbill 	if (bp->b_flags & B_DIRTY) {
4998Sbill 		if (bp->b_flags & B_ERROR)
5008Sbill 			panic("IO err in push");
5018Sbill 		s = spl6();
5028Sbill 		cnt.v_pgout++;
5038Sbill 		bp->av_forw = bclnlist;
5048Sbill 		bp->b_bcount = swsize[bp - swbuf];
5058Sbill 		bp->b_pfcent = swpf[bp - swbuf];
5068Sbill 		bclnlist = bp;
5078Sbill 		if (bswlist.b_flags & B_WANTED)
5088Sbill 			wakeup((caddr_t)&proc[2]);
5098Sbill 		splx(s);
510383Sbill 		return;
5118Sbill 	}
5128Sbill 	if (bp->b_flags&B_ASYNC)
5138Sbill 		brelse(bp);
5148Sbill 	else {
5158Sbill 		bp->b_flags &= ~B_WANTED;
5168Sbill 		wakeup((caddr_t)bp);
5178Sbill 	}
5188Sbill }
5198Sbill 
5208Sbill /*
5218Sbill  * Zero the core associated with a buffer.
5228Sbill  */
5238Sbill clrbuf(bp)
5248Sbill struct buf *bp;
5258Sbill {
5268Sbill 	register *p;
5278Sbill 	register c;
5288Sbill 
5298Sbill 	p = bp->b_un.b_words;
5308Sbill 	c = BSIZE/sizeof(int);
5318Sbill 	do
5328Sbill 		*p++ = 0;
5338Sbill 	while (--c);
5348Sbill 	bp->b_resid = 0;
5358Sbill }
5368Sbill 
5378Sbill /*
5388Sbill  * swap I/O -
5398Sbill  *
5408Sbill  * If the flag indicates a dirty page push initiated
5418Sbill  * by the pageout daemon, we map the page into the i th
5428Sbill  * virtual page of process 2 (the daemon itself) where i is
5438Sbill  * the index of the swap header that has been allocated.
5448Sbill  * We simply initialize the header and queue the I/O but
5458Sbill  * do not wait for completion. When the I/O completes,
5468Sbill  * iodone() will link the header to a list of cleaned
5478Sbill  * pages to be processed by the pageout daemon.
5488Sbill  */
5498Sbill swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent)
5508Sbill 	struct proc *p;
5518Sbill 	swblk_t dblkno;
5528Sbill 	caddr_t addr;
5538Sbill 	int flag, nbytes;
5548Sbill 	dev_t dev;
5558Sbill 	unsigned pfcent;
5568Sbill {
5578Sbill 	register struct buf *bp;
5588Sbill 	register int c;
5598Sbill 	int p2dp;
5608Sbill 	register struct pte *dpte, *vpte;
5618Sbill 
562124Sbill 	(void) spl6();
5638Sbill 	while (bswlist.av_forw == NULL) {
5648Sbill 		bswlist.b_flags |= B_WANTED;
5658Sbill 		sleep((caddr_t)&bswlist, PSWP+1);
5668Sbill 	}
5678Sbill 	bp = bswlist.av_forw;
5688Sbill 	bswlist.av_forw = bp->av_forw;
569124Sbill 	(void) spl0();
5708Sbill 
5718Sbill 	bp->b_flags = B_BUSY | B_PHYS | rdflg | flag;
5728Sbill 	if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0)
5738Sbill 		if (rdflg == B_READ)
5748Sbill 			sum.v_pswpin += btoc(nbytes);
5758Sbill 		else
5768Sbill 			sum.v_pswpout += btoc(nbytes);
5778Sbill 	bp->b_proc = p;
5788Sbill 	if (flag & B_DIRTY) {
5798Sbill 		p2dp = ((bp - swbuf) * CLSIZE) * KLMAX;
5808Sbill 		dpte = dptopte(&proc[2], p2dp);
5818Sbill 		vpte = vtopte(p, btop(addr));
5828Sbill 		for (c = 0; c < nbytes; c += NBPG) {
5838Sbill 			if (vpte->pg_pfnum == 0 || vpte->pg_fod)
5848Sbill 				panic("swap bad pte");
5858Sbill 			*dpte++ = *vpte++;
5868Sbill 		}
5878Sbill 		bp->b_un.b_addr = (caddr_t)ctob(p2dp);
5888Sbill 	} else
5898Sbill 		bp->b_un.b_addr = addr;
5908Sbill 	while (nbytes > 0) {
5918Sbill 		c = imin(ctob(120), nbytes);
5928Sbill 		bp->b_bcount = c;
5938Sbill 		bp->b_blkno = dblkno;
5948Sbill 		bp->b_dev = dev;
595718Sbill 		if (flag & B_DIRTY) {
596718Sbill 			swpf[bp - swbuf] = pfcent;
597718Sbill 			swsize[bp - swbuf] = nbytes;
598718Sbill 		}
5998Sbill 		(*bdevsw[major(dev)].d_strategy)(bp);
6008Sbill 		if (flag & B_DIRTY) {
6018Sbill 			if (c < nbytes)
6028Sbill 				panic("big push");
6038Sbill 			return;
6048Sbill 		}
605124Sbill 		(void) spl6();
6068Sbill 		while((bp->b_flags&B_DONE)==0)
6078Sbill 			sleep((caddr_t)bp, PSWP);
608124Sbill 		(void) spl0();
6098Sbill 		bp->b_un.b_addr += c;
6108Sbill 		bp->b_flags &= ~B_DONE;
6118Sbill 		if (bp->b_flags & B_ERROR) {
6128Sbill 			if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
6138Sbill 				panic("hard IO err in swap");
6148Sbill 			swkill(p, (char *)0);
6158Sbill 		}
6168Sbill 		nbytes -= c;
6178Sbill 		dblkno += btoc(c);
6188Sbill 	}
619124Sbill 	(void) spl6();
6208Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
6218Sbill 	bp->av_forw = bswlist.av_forw;
6228Sbill 	bswlist.av_forw = bp;
6238Sbill 	if (bswlist.b_flags & B_WANTED) {
6248Sbill 		bswlist.b_flags &= ~B_WANTED;
6258Sbill 		wakeup((caddr_t)&bswlist);
6268Sbill 		wakeup((caddr_t)&proc[2]);
6278Sbill 	}
628124Sbill 	(void) spl0();
6298Sbill }
6308Sbill 
6318Sbill /*
6328Sbill  * If rout == 0 then killed on swap error, else
6338Sbill  * rout is the name of the routine where we ran out of
6348Sbill  * swap space.
6358Sbill  */
6368Sbill swkill(p, rout)
6378Sbill 	struct proc *p;
6388Sbill 	char *rout;
6398Sbill {
6402922Swnj 	char *mesg;
6418Sbill 
6422922Swnj 	printf("pid %d: ", p->p_pid);
6438Sbill 	if (rout)
6442922Swnj 		printf(mesg = "killed due to no swap space\n");
6458Sbill 	else
6462922Swnj 		printf(mesg = "killed on swap error\n");
6472922Swnj 	uprintf("sorry, pid %d was %s", p->p_pid, mesg);
6488Sbill 	/*
6498Sbill 	 * To be sure no looping (e.g. in vmsched trying to
6508Sbill 	 * swap out) mark process locked in core (as though
6518Sbill 	 * done by user) after killing it so noone will try
6528Sbill 	 * to swap it out.
6538Sbill 	 */
654165Sbill 	psignal(p, SIGKILL);
6558Sbill 	p->p_flag |= SULOCK;
6568Sbill }
6578Sbill 
6588Sbill /*
6598Sbill  * make sure all write-behind blocks
6608Sbill  * on dev (or NODEV for all)
6618Sbill  * are flushed out.
6628Sbill  * (from umount and update)
6638Sbill  */
6648Sbill bflush(dev)
6658Sbill dev_t dev;
6668Sbill {
6678Sbill 	register struct buf *bp;
6682325Swnj 	register struct buf *flist;
6698Sbill 
6708Sbill loop:
671124Sbill 	(void) spl6();
6722325Swnj 	for (flist = bfreelist; flist < &bfreelist[BQUEUES]; flist++)
6732325Swnj 	for (bp = flist->av_forw; bp != flist; bp = bp->av_forw) {
6748Sbill 		if (bp->b_flags&B_DELWRI && (dev == NODEV||dev==bp->b_dev)) {
6758Sbill 			bp->b_flags |= B_ASYNC;
6768Sbill 			notavail(bp);
6778Sbill 			bwrite(bp);
6788Sbill 			goto loop;
6798Sbill 		}
6808Sbill 	}
681124Sbill 	(void) spl0();
6828Sbill }
6838Sbill 
6848Sbill /*
6858Sbill  * Raw I/O. The arguments are
6868Sbill  *	The strategy routine for the device
6878Sbill  *	A buffer, which will always be a special buffer
6888Sbill  *	  header owned exclusively by the device for this purpose
6898Sbill  *	The device number
6908Sbill  *	Read/write flag
6918Sbill  * Essentially all the work is computing physical addresses and
6928Sbill  * validating them.
6938Sbill  * If the user has the proper access privilidges, the process is
6948Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
6958Sbill  * faulted and locked. After the completion of the I/O, the above pages
6968Sbill  * are unlocked.
6978Sbill  */
6988Sbill physio(strat, bp, dev, rw, mincnt)
6998Sbill int (*strat)();
7008Sbill register struct buf *bp;
7018Sbill unsigned (*mincnt)();
7028Sbill {
7038Sbill 	register int c;
7048Sbill 	char *a;
7058Sbill 
7068Sbill 	if (useracc(u.u_base,u.u_count,rw==B_READ?B_WRITE:B_READ) == NULL) {
7078Sbill 		u.u_error = EFAULT;
7088Sbill 		return;
7098Sbill 	}
710124Sbill 	(void) spl6();
7118Sbill 	while (bp->b_flags&B_BUSY) {
7128Sbill 		bp->b_flags |= B_WANTED;
7138Sbill 		sleep((caddr_t)bp, PRIBIO+1);
7148Sbill 	}
7158Sbill 	bp->b_error = 0;
7168Sbill 	bp->b_proc = u.u_procp;
7178Sbill 	bp->b_un.b_addr = u.u_base;
7188Sbill 	while (u.u_count != 0 && bp->b_error==0) {
7198Sbill 		bp->b_flags = B_BUSY | B_PHYS | rw;
7208Sbill 		bp->b_dev = dev;
7218Sbill 		bp->b_blkno = u.u_offset >> PGSHIFT;
7228Sbill 		bp->b_bcount = u.u_count;
7238Sbill 		(*mincnt)(bp);
7248Sbill 		c = bp->b_bcount;
7258Sbill 		u.u_procp->p_flag |= SPHYSIO;
7268Sbill 		vslock(a = bp->b_un.b_addr, c);
7278Sbill 		(*strat)(bp);
728124Sbill 		(void) spl6();
7298Sbill 		while ((bp->b_flags&B_DONE) == 0)
7308Sbill 			sleep((caddr_t)bp, PRIBIO);
7318Sbill 		vsunlock(a, c, rw);
7328Sbill 		u.u_procp->p_flag &= ~SPHYSIO;
7338Sbill 		if (bp->b_flags&B_WANTED)
7348Sbill 			wakeup((caddr_t)bp);
735124Sbill 		(void) spl0();
7368Sbill 		bp->b_un.b_addr += c;
7378Sbill 		u.u_count -= c;
7388Sbill 		u.u_offset += c;
7398Sbill 	}
7408Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
7418Sbill 	u.u_count = bp->b_resid;
7428Sbill 	geterror(bp);
7438Sbill }
7448Sbill 
7458Sbill /*ARGSUSED*/
7468Sbill unsigned
7478Sbill minphys(bp)
7488Sbill struct buf *bp;
7498Sbill {
7508Sbill 
7518Sbill 	if (bp->b_bcount > 60 * 1024)
7528Sbill 		bp->b_bcount = 60 * 1024;
7538Sbill }
7548Sbill 
7558Sbill /*
7568Sbill  * Pick up the device's error number and pass it to the user;
7578Sbill  * if there is an error but the number is 0 set a generalized
7588Sbill  * code.  Actually the latter is always true because devices
7598Sbill  * don't yet return specific errors.
7608Sbill  */
7618Sbill geterror(bp)
7628Sbill register struct buf *bp;
7638Sbill {
7648Sbill 
7658Sbill 	if (bp->b_flags&B_ERROR)
7668Sbill 		if ((u.u_error = bp->b_error)==0)
7678Sbill 			u.u_error = EIO;
7688Sbill }
7692299Skre 
7702299Skre /*
7712299Skre  * Invalidate in core blocks belonging to closed or umounted filesystem
7722299Skre  *
7732299Skre  * This is not nicely done at all - the buffer ought to be removed from the
7742299Skre  * hash chains & have its dev/blkno fields clobbered, but unfortunately we
7752299Skre  * can't do that here, as it is quite possible that the block is still
7762299Skre  * being used for i/o. Eventually, all disc drivers should be forced to
7772299Skre  * have a close routine, which ought ensure that the queue is empty, then
7782299Skre  * properly flush the queues. Until that happy day, this suffices for
7792299Skre  * correctness.						... kre
7802299Skre  */
7812299Skre binval(dev)
7822299Skre dev_t dev;
7832299Skre {
7842361Skre 	register struct buf *bp;
7852361Skre 	register struct bufhd *hp;
7862361Skre #define dp ((struct buf *)hp)
7872299Skre 
7882361Skre 	for (hp = bufhash; hp < &bufhash[BUFHSZ]; hp++)
7892361Skre 		for (bp = dp->b_forw; bp != dp; bp = bp->b_forw)
7902361Skre 			if (bp->b_dev == dev)
7912361Skre 				bp->b_flags |= B_INVAL;
7922299Skre }
793