xref: /csrg-svn/sys/kern/vfs_cluster.c (revision 2423)
1*2423Skre /*	vfs_cluster.c	4.11	02/15/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
382325Swnj struct	bufhd bufhash[BUFHSZ];
392328Swnj #define	BUFHASH(dev, dblkno)	\
402328Swnj 		((struct buf *)&bufhash[((int)(dev)+(int)(dblkno)) % BUFHSZ])
4191Sbill 
4291Sbill /*
4391Sbill  * Initialize hash links for buffers.
4491Sbill  */
4591Sbill bhinit()
4691Sbill {
4791Sbill 	register int i;
482325Swnj 	register struct bufhd *bp;
4991Sbill 
502325Swnj 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
512325Swnj 		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 {
2252403Skre 	register int flags;
2268Sbill 
2278Sbill 	if ((bp->b_flags&B_DELWRI) == 0)
2288Sbill 		u.u_vm.vm_oublk++;		/* noone paid yet */
2292403Skre 	flags = bdevsw[major(bp->b_dev)].d_flags;
2302403Skre 	if(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 {
2552325Swnj 	register struct buf *flist;
2568Sbill 	register s;
2578Sbill 
2588Sbill 	if (bp->b_flags&B_WANTED)
2598Sbill 		wakeup((caddr_t)bp);
2602325Swnj 	if (bfreelist[0].b_flags&B_WANTED) {
2612325Swnj 		bfreelist[0].b_flags &= ~B_WANTED;
2622325Swnj 		wakeup((caddr_t)bfreelist);
2638Sbill 	}
2642325Swnj 	if ((bp->b_flags&B_ERROR) && bp->b_dev != NODEV)
2658Sbill 		bp->b_dev = NODEV;  /* no assoc. on error */
2668Sbill 	s = spl6();
2672325Swnj 	if (bp->b_flags & (B_ERROR|B_INVAL)) {
2682325Swnj 		/* block has no info ... put at front of most free list */
2692325Swnj 		flist = &bfreelist[BQUEUES-1];
2702325Swnj 		flist->av_forw->av_back = bp;
2712325Swnj 		bp->av_forw = flist->av_forw;
2722325Swnj 		flist->av_forw = bp;
2732325Swnj 		bp->av_back = flist;
2748Sbill 	} else {
2752325Swnj 		if (bp->b_flags & B_LOCKED)
2762325Swnj 			flist = &bfreelist[BQ_LOCKED];
2772325Swnj 		else if (bp->b_flags & B_AGE)
2782325Swnj 			flist = &bfreelist[BQ_AGE];
2792325Swnj 		else
2802325Swnj 			flist = &bfreelist[BQ_LRU];
2812325Swnj 		flist->av_back->av_forw = bp;
2822325Swnj 		bp->av_back = flist->av_back;
2832325Swnj 		flist->av_back = bp;
2842325Swnj 		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;
2992325Swnj 	register struct buf *dp;
3008Sbill 	register int dblkno = fsbtodb(blkno);
3018Sbill 
3022328Swnj 	dp = BUFHASH(dev, dblkno);
3032325Swnj 	for (bp = dp->b_forw; bp != dp; bp = bp->b_forw)
3042325Swnj 		if (bp->b_blkno == dblkno && bp->b_dev == dev &&
3052325Swnj 		    !(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;
3322325Swnj 	register int dblkno = fsbtodb(blkno);
333*2423Skre #ifdef	DISKMON
334*2423Skre 	register int i;
335*2423Skre #endif
3368Sbill 
3371831Sbill 	if ((unsigned)blkno >= 1 << (sizeof(int)*NBBY-PGSHIFT))
3381831Sbill 		blkno = 1 << ((sizeof(int)*NBBY-PGSHIFT) + 1);
3391831Sbill 	dblkno = fsbtodb(blkno);
3402325Swnj 	dp = BUFHASH(dev, dblkno);
3418Sbill     loop:
342124Sbill 	(void) spl0();
3432325Swnj 	for (bp = dp->b_forw; bp != dp; bp = bp->b_forw) {
3442325Swnj 		if (bp->b_blkno != dblkno || bp->b_dev != dev ||
3452325Swnj 		    bp->b_flags&B_INVAL)
3468Sbill 			continue;
347124Sbill 		(void) spl6();
3488Sbill 		if (bp->b_flags&B_BUSY) {
3498Sbill 			bp->b_flags |= B_WANTED;
3508Sbill 			sleep((caddr_t)bp, PRIBIO+1);
3518Sbill 			goto loop;
3528Sbill 		}
353124Sbill 		(void) spl0();
3548Sbill #ifdef	DISKMON
3558Sbill 		i = 0;
3568Sbill 		dp = bp->av_forw;
3572325Swnj 		while ((dp->b_flags & B_HEAD) == 0) {
3588Sbill 			i++;
3598Sbill 			dp = dp->av_forw;
3608Sbill 		}
3618Sbill 		if (i<NBUF)
3628Sbill 			io_info.bufcount[i]++;
3638Sbill #endif
3648Sbill 		notavail(bp);
3658Sbill 		bp->b_flags |= B_CACHE;
3668Sbill 		return(bp);
3678Sbill 	}
36891Sbill 	if (major(dev) >= nblkdev)
36991Sbill 		panic("blkdev");
370124Sbill 	(void) spl6();
3712325Swnj 	for (ep = &bfreelist[BQUEUES-1]; ep > bfreelist; ep--)
3722325Swnj 		if (ep->av_forw != ep)
3732325Swnj 			break;
3742325Swnj 	if (ep == bfreelist) {		/* no free blocks at all */
3752325Swnj 		ep->b_flags |= B_WANTED;
3762325Swnj 		sleep((caddr_t)ep, PRIBIO+1);
3778Sbill 		goto loop;
3788Sbill 	}
3791792Sbill 	(void) spl0();
3802325Swnj 	bp = ep->av_forw;
3818Sbill 	notavail(bp);
3828Sbill 	if (bp->b_flags & B_DELWRI) {
3838Sbill 		bp->b_flags |= B_ASYNC;
3848Sbill 		bwrite(bp);
3858Sbill 		goto loop;
3868Sbill 	}
3872045Swnj #ifdef EPAWNJ
3882045Swnj 	trace(TR_BRELSE, bp->b_dev, dbtofsb(bp->b_blkno));
3892045Swnj #endif
3908Sbill 	bp->b_flags = B_BUSY;
3918Sbill 	bp->b_back->b_forw = bp->b_forw;
3928Sbill 	bp->b_forw->b_back = bp->b_back;
3938Sbill 	bp->b_forw = dp->b_forw;
3948Sbill 	bp->b_back = dp;
3958Sbill 	dp->b_forw->b_back = bp;
3968Sbill 	dp->b_forw = bp;
3978Sbill 	bp->b_dev = dev;
3988Sbill 	bp->b_blkno = dblkno;
3998Sbill 	return(bp);
4008Sbill }
4018Sbill 
4028Sbill /*
4038Sbill  * get an empty block,
4048Sbill  * not assigned to any particular device
4058Sbill  */
4068Sbill struct buf *
4078Sbill geteblk()
4088Sbill {
409182Sbill 	register struct buf *bp, *dp;
4108Sbill 
4118Sbill loop:
412124Sbill 	(void) spl6();
4132325Swnj 	for (dp = &bfreelist[BQUEUES-1]; dp > bfreelist; dp--)
4142325Swnj 		if (dp->av_forw != dp)
4152325Swnj 			break;
4162325Swnj 	if (dp == bfreelist) {		/* no free blocks */
4172325Swnj 		dp->b_flags |= B_WANTED;
4182325Swnj 		sleep((caddr_t)dp, PRIBIO+1);
4192325Swnj 		goto loop;
4208Sbill 	}
421124Sbill 	(void) spl0();
4222325Swnj 	bp = dp->av_forw;
4238Sbill 	notavail(bp);
4248Sbill 	if (bp->b_flags & B_DELWRI) {
4258Sbill 		bp->b_flags |= B_ASYNC;
4268Sbill 		bwrite(bp);
4278Sbill 		goto loop;
4288Sbill 	}
4292045Swnj #ifdef EPAWNJ
4302325Swnj 	trace(TR_BRELSE, bp->b_dev, dbtofsb(bp->b_blkno));
4312045Swnj #endif
4322325Swnj 	bp->b_flags = B_BUSY|B_INVAL;
4338Sbill 	bp->b_back->b_forw = bp->b_forw;
4348Sbill 	bp->b_forw->b_back = bp->b_back;
4358Sbill 	bp->b_forw = dp->b_forw;
4368Sbill 	bp->b_back = dp;
4378Sbill 	dp->b_forw->b_back = bp;
4388Sbill 	dp->b_forw = bp;
4398Sbill 	bp->b_dev = (dev_t)NODEV;
4408Sbill 	return(bp);
4418Sbill }
4428Sbill 
4438Sbill /*
4448Sbill  * Wait for I/O completion on the buffer; return errors
4458Sbill  * to the user.
4468Sbill  */
4478Sbill iowait(bp)
4488Sbill register struct buf *bp;
4498Sbill {
4508Sbill 
451124Sbill 	(void) spl6();
4528Sbill 	while ((bp->b_flags&B_DONE)==0)
4538Sbill 		sleep((caddr_t)bp, PRIBIO);
454124Sbill 	(void) spl0();
4558Sbill 	geterror(bp);
4568Sbill }
4578Sbill 
4588Sbill #ifndef FASTVAX
4598Sbill /*
4608Sbill  * Unlink a buffer from the available list and mark it busy.
4618Sbill  * (internal interface)
4628Sbill  */
4638Sbill notavail(bp)
4648Sbill register struct buf *bp;
4658Sbill {
4668Sbill 	register s;
4678Sbill 
4688Sbill 	s = spl6();
4698Sbill 	bp->av_back->av_forw = bp->av_forw;
4708Sbill 	bp->av_forw->av_back = bp->av_back;
4718Sbill 	bp->b_flags |= B_BUSY;
4728Sbill 	splx(s);
4738Sbill }
4748Sbill #endif
4758Sbill 
4768Sbill /*
4778Sbill  * Mark I/O complete on a buffer. If the header
4788Sbill  * indicates a dirty page push completion, the
4798Sbill  * header is inserted into the ``cleaned'' list
4808Sbill  * to be processed by the pageout daemon. Otherwise
4818Sbill  * release it if I/O is asynchronous, and wake
4828Sbill  * up anyone waiting for it.
4838Sbill  */
4848Sbill iodone(bp)
4858Sbill register struct buf *bp;
4868Sbill {
4878Sbill 	register int s;
4888Sbill 
489420Sbill 	if (bp->b_flags & B_DONE)
490420Sbill 		panic("dup iodone");
4918Sbill 	bp->b_flags |= B_DONE;
4928Sbill 	if (bp->b_flags & B_DIRTY) {
4938Sbill 		if (bp->b_flags & B_ERROR)
4948Sbill 			panic("IO err in push");
4958Sbill 		s = spl6();
4968Sbill 		cnt.v_pgout++;
4978Sbill 		bp->av_forw = bclnlist;
4988Sbill 		bp->b_bcount = swsize[bp - swbuf];
4998Sbill 		bp->b_pfcent = swpf[bp - swbuf];
5008Sbill 		bclnlist = bp;
5018Sbill 		if (bswlist.b_flags & B_WANTED)
5028Sbill 			wakeup((caddr_t)&proc[2]);
5038Sbill 		splx(s);
504383Sbill 		return;
5058Sbill 	}
5068Sbill 	if (bp->b_flags&B_ASYNC)
5078Sbill 		brelse(bp);
5088Sbill 	else {
5098Sbill 		bp->b_flags &= ~B_WANTED;
5108Sbill 		wakeup((caddr_t)bp);
5118Sbill 	}
5128Sbill }
5138Sbill 
5148Sbill /*
5158Sbill  * Zero the core associated with a buffer.
5168Sbill  */
5178Sbill clrbuf(bp)
5188Sbill struct buf *bp;
5198Sbill {
5208Sbill 	register *p;
5218Sbill 	register c;
5228Sbill 
5238Sbill 	p = bp->b_un.b_words;
5248Sbill 	c = BSIZE/sizeof(int);
5258Sbill 	do
5268Sbill 		*p++ = 0;
5278Sbill 	while (--c);
5288Sbill 	bp->b_resid = 0;
5298Sbill }
5308Sbill 
5318Sbill /*
5328Sbill  * swap I/O -
5338Sbill  *
5348Sbill  * If the flag indicates a dirty page push initiated
5358Sbill  * by the pageout daemon, we map the page into the i th
5368Sbill  * virtual page of process 2 (the daemon itself) where i is
5378Sbill  * the index of the swap header that has been allocated.
5388Sbill  * We simply initialize the header and queue the I/O but
5398Sbill  * do not wait for completion. When the I/O completes,
5408Sbill  * iodone() will link the header to a list of cleaned
5418Sbill  * pages to be processed by the pageout daemon.
5428Sbill  */
5438Sbill swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent)
5448Sbill 	struct proc *p;
5458Sbill 	swblk_t dblkno;
5468Sbill 	caddr_t addr;
5478Sbill 	int flag, nbytes;
5488Sbill 	dev_t dev;
5498Sbill 	unsigned pfcent;
5508Sbill {
5518Sbill 	register struct buf *bp;
5528Sbill 	register int c;
5538Sbill 	int p2dp;
5548Sbill 	register struct pte *dpte, *vpte;
5558Sbill 
556124Sbill 	(void) spl6();
5578Sbill 	while (bswlist.av_forw == NULL) {
5588Sbill 		bswlist.b_flags |= B_WANTED;
5598Sbill 		sleep((caddr_t)&bswlist, PSWP+1);
5608Sbill 	}
5618Sbill 	bp = bswlist.av_forw;
5628Sbill 	bswlist.av_forw = bp->av_forw;
563124Sbill 	(void) spl0();
5648Sbill 
5658Sbill 	bp->b_flags = B_BUSY | B_PHYS | rdflg | flag;
5668Sbill 	if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0)
5678Sbill 		if (rdflg == B_READ)
5688Sbill 			sum.v_pswpin += btoc(nbytes);
5698Sbill 		else
5708Sbill 			sum.v_pswpout += btoc(nbytes);
5718Sbill 	bp->b_proc = p;
5728Sbill 	if (flag & B_DIRTY) {
5738Sbill 		p2dp = ((bp - swbuf) * CLSIZE) * KLMAX;
5748Sbill 		dpte = dptopte(&proc[2], p2dp);
5758Sbill 		vpte = vtopte(p, btop(addr));
5768Sbill 		for (c = 0; c < nbytes; c += NBPG) {
5778Sbill 			if (vpte->pg_pfnum == 0 || vpte->pg_fod)
5788Sbill 				panic("swap bad pte");
5798Sbill 			*dpte++ = *vpte++;
5808Sbill 		}
5818Sbill 		bp->b_un.b_addr = (caddr_t)ctob(p2dp);
5828Sbill 	} else
5838Sbill 		bp->b_un.b_addr = addr;
5848Sbill 	while (nbytes > 0) {
5858Sbill 		c = imin(ctob(120), nbytes);
5868Sbill 		bp->b_bcount = c;
5878Sbill 		bp->b_blkno = dblkno;
5888Sbill 		bp->b_dev = dev;
589718Sbill 		if (flag & B_DIRTY) {
590718Sbill 			swpf[bp - swbuf] = pfcent;
591718Sbill 			swsize[bp - swbuf] = nbytes;
592718Sbill 		}
5938Sbill 		(*bdevsw[major(dev)].d_strategy)(bp);
5948Sbill 		if (flag & B_DIRTY) {
5958Sbill 			if (c < nbytes)
5968Sbill 				panic("big push");
5978Sbill 			return;
5988Sbill 		}
599124Sbill 		(void) spl6();
6008Sbill 		while((bp->b_flags&B_DONE)==0)
6018Sbill 			sleep((caddr_t)bp, PSWP);
602124Sbill 		(void) spl0();
6038Sbill 		bp->b_un.b_addr += c;
6048Sbill 		bp->b_flags &= ~B_DONE;
6058Sbill 		if (bp->b_flags & B_ERROR) {
6068Sbill 			if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
6078Sbill 				panic("hard IO err in swap");
6088Sbill 			swkill(p, (char *)0);
6098Sbill 		}
6108Sbill 		nbytes -= c;
6118Sbill 		dblkno += btoc(c);
6128Sbill 	}
613124Sbill 	(void) spl6();
6148Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
6158Sbill 	bp->av_forw = bswlist.av_forw;
6168Sbill 	bswlist.av_forw = bp;
6178Sbill 	if (bswlist.b_flags & B_WANTED) {
6188Sbill 		bswlist.b_flags &= ~B_WANTED;
6198Sbill 		wakeup((caddr_t)&bswlist);
6208Sbill 		wakeup((caddr_t)&proc[2]);
6218Sbill 	}
622124Sbill 	(void) spl0();
6238Sbill }
6248Sbill 
6258Sbill /*
6268Sbill  * If rout == 0 then killed on swap error, else
6278Sbill  * rout is the name of the routine where we ran out of
6288Sbill  * swap space.
6298Sbill  */
6308Sbill swkill(p, rout)
6318Sbill 	struct proc *p;
6328Sbill 	char *rout;
6338Sbill {
6348Sbill 
6358Sbill 	printf("%d: ", p->p_pid);
6368Sbill 	if (rout)
6378Sbill 		printf("out of swap space in %s\n", rout);
6388Sbill 	else
6398Sbill 		printf("killed on swap error\n");
6408Sbill 	/*
6418Sbill 	 * To be sure no looping (e.g. in vmsched trying to
6428Sbill 	 * swap out) mark process locked in core (as though
6438Sbill 	 * done by user) after killing it so noone will try
6448Sbill 	 * to swap it out.
6458Sbill 	 */
646165Sbill 	psignal(p, SIGKILL);
6478Sbill 	p->p_flag |= SULOCK;
6488Sbill }
6498Sbill 
6508Sbill /*
6518Sbill  * make sure all write-behind blocks
6528Sbill  * on dev (or NODEV for all)
6538Sbill  * are flushed out.
6548Sbill  * (from umount and update)
6558Sbill  */
6568Sbill bflush(dev)
6578Sbill dev_t dev;
6588Sbill {
6598Sbill 	register struct buf *bp;
6602325Swnj 	register struct buf *flist;
6618Sbill 
6628Sbill loop:
663124Sbill 	(void) spl6();
6642325Swnj 	for (flist = bfreelist; flist < &bfreelist[BQUEUES]; flist++)
6652325Swnj 	for (bp = flist->av_forw; bp != flist; bp = bp->av_forw) {
6668Sbill 		if (bp->b_flags&B_DELWRI && (dev == NODEV||dev==bp->b_dev)) {
6678Sbill 			bp->b_flags |= B_ASYNC;
6688Sbill 			notavail(bp);
6698Sbill 			bwrite(bp);
6708Sbill 			goto loop;
6718Sbill 		}
6728Sbill 	}
673124Sbill 	(void) spl0();
6748Sbill }
6758Sbill 
6768Sbill /*
6778Sbill  * Raw I/O. The arguments are
6788Sbill  *	The strategy routine for the device
6798Sbill  *	A buffer, which will always be a special buffer
6808Sbill  *	  header owned exclusively by the device for this purpose
6818Sbill  *	The device number
6828Sbill  *	Read/write flag
6838Sbill  * Essentially all the work is computing physical addresses and
6848Sbill  * validating them.
6858Sbill  * If the user has the proper access privilidges, the process is
6868Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
6878Sbill  * faulted and locked. After the completion of the I/O, the above pages
6888Sbill  * are unlocked.
6898Sbill  */
6908Sbill physio(strat, bp, dev, rw, mincnt)
6918Sbill int (*strat)();
6928Sbill register struct buf *bp;
6938Sbill unsigned (*mincnt)();
6948Sbill {
6958Sbill 	register int c;
6968Sbill 	char *a;
6978Sbill 
6988Sbill 	if (useracc(u.u_base,u.u_count,rw==B_READ?B_WRITE:B_READ) == NULL) {
6998Sbill 		u.u_error = EFAULT;
7008Sbill 		return;
7018Sbill 	}
702124Sbill 	(void) spl6();
7038Sbill 	while (bp->b_flags&B_BUSY) {
7048Sbill 		bp->b_flags |= B_WANTED;
7058Sbill 		sleep((caddr_t)bp, PRIBIO+1);
7068Sbill 	}
7078Sbill 	bp->b_error = 0;
7088Sbill 	bp->b_proc = u.u_procp;
7098Sbill 	bp->b_un.b_addr = u.u_base;
7108Sbill 	while (u.u_count != 0 && bp->b_error==0) {
7118Sbill 		bp->b_flags = B_BUSY | B_PHYS | rw;
7128Sbill 		bp->b_dev = dev;
7138Sbill 		bp->b_blkno = u.u_offset >> PGSHIFT;
7148Sbill 		bp->b_bcount = u.u_count;
7158Sbill 		(*mincnt)(bp);
7168Sbill 		c = bp->b_bcount;
7178Sbill 		u.u_procp->p_flag |= SPHYSIO;
7188Sbill 		vslock(a = bp->b_un.b_addr, c);
7198Sbill 		(*strat)(bp);
720124Sbill 		(void) spl6();
7218Sbill 		while ((bp->b_flags&B_DONE) == 0)
7228Sbill 			sleep((caddr_t)bp, PRIBIO);
7238Sbill 		vsunlock(a, c, rw);
7248Sbill 		u.u_procp->p_flag &= ~SPHYSIO;
7258Sbill 		if (bp->b_flags&B_WANTED)
7268Sbill 			wakeup((caddr_t)bp);
727124Sbill 		(void) spl0();
7288Sbill 		bp->b_un.b_addr += c;
7298Sbill 		u.u_count -= c;
7308Sbill 		u.u_offset += c;
7318Sbill 	}
7328Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
7338Sbill 	u.u_count = bp->b_resid;
7348Sbill 	geterror(bp);
7358Sbill }
7368Sbill 
7378Sbill /*ARGSUSED*/
7388Sbill unsigned
7398Sbill minphys(bp)
7408Sbill struct buf *bp;
7418Sbill {
7428Sbill 
7438Sbill 	if (bp->b_bcount > 60 * 1024)
7448Sbill 		bp->b_bcount = 60 * 1024;
7458Sbill }
7468Sbill 
7478Sbill /*
7488Sbill  * Pick up the device's error number and pass it to the user;
7498Sbill  * if there is an error but the number is 0 set a generalized
7508Sbill  * code.  Actually the latter is always true because devices
7518Sbill  * don't yet return specific errors.
7528Sbill  */
7538Sbill geterror(bp)
7548Sbill register struct buf *bp;
7558Sbill {
7568Sbill 
7578Sbill 	if (bp->b_flags&B_ERROR)
7588Sbill 		if ((u.u_error = bp->b_error)==0)
7598Sbill 			u.u_error = EIO;
7608Sbill }
7612299Skre 
7622299Skre /*
7632299Skre  * Invalidate in core blocks belonging to closed or umounted filesystem
7642299Skre  *
7652299Skre  * This is not nicely done at all - the buffer ought to be removed from the
7662299Skre  * hash chains & have its dev/blkno fields clobbered, but unfortunately we
7672299Skre  * can't do that here, as it is quite possible that the block is still
7682299Skre  * being used for i/o. Eventually, all disc drivers should be forced to
7692299Skre  * have a close routine, which ought ensure that the queue is empty, then
7702299Skre  * properly flush the queues. Until that happy day, this suffices for
7712299Skre  * correctness.						... kre
7722299Skre  */
7732299Skre binval(dev)
7742299Skre dev_t dev;
7752299Skre {
7762361Skre 	register struct buf *bp;
7772361Skre 	register struct bufhd *hp;
7782361Skre #define dp ((struct buf *)hp)
7792299Skre 
7802361Skre 	for (hp = bufhash; hp < &bufhash[BUFHSZ]; hp++)
7812361Skre 		for (bp = dp->b_forw; bp != dp; bp = bp->b_forw)
7822361Skre 			if (bp->b_dev == dev)
7832361Skre 				bp->b_flags |= B_INVAL;
7842299Skre }
785