xref: /csrg-svn/sys/kern/vfs_cluster.c (revision 5424)
1*5424Swnj /*	vfs_cluster.c	4.24	82/01/17	*/
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 
373099Swnj struct	buf bfreelist[BQUEUES];
383099Swnj struct	buf bswlist, *bclnlist;
393099Swnj 
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) {
1083199Swnj #ifdef	TRACE
1093199Swnj 		trace(TR_BREADHIT, 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);
1193199Swnj #ifdef	TRACE
1203199Swnj 	trace(TR_BREADMISS, 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);
1483199Swnj #ifdef	TRACE
1493199Swnj 			trace(TR_BREADMISS, dev, blkno);
1502045Swnj #endif
1518Sbill #ifdef	DISKMON
1528Sbill 			io_info.nread++;
1538Sbill #endif
1548Sbill 			u.u_vm.vm_inblk++;		/* pay for read */
1558Sbill 		}
1563199Swnj #ifdef	TRACE
1572045Swnj 		else
1583199Swnj 			trace(TR_BREADHIT, 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);
1653199Swnj #ifdef	TRACE
1663199Swnj 			trace(TR_BREADHITRA, 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);
1723199Swnj #ifdef	TRACE
1733199Swnj 			trace(TR_BREADMISSRA, 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 */
2043199Swnj #ifdef	TRACE
2054033Swnj 	trace(TR_BWRITE, bp->b_dev, 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.
331*5424Swnj  *
332*5424Swnj  * We use splx here because this routine may be called
333*5424Swnj  * on the interrupt stack during a dump, and we don't
334*5424Swnj  * want to lower the ipl back to 0.
3358Sbill  */
3368Sbill struct buf *
3378Sbill getblk(dev, blkno)
3388Sbill dev_t dev;
3398Sbill daddr_t blkno;
3408Sbill {
34191Sbill 	register struct buf *bp, *dp, *ep;
3422325Swnj 	register int dblkno = fsbtodb(blkno);
3432423Skre #ifdef	DISKMON
3442423Skre 	register int i;
3452423Skre #endif
346*5424Swnj 	int s;
3478Sbill 
3481831Sbill 	if ((unsigned)blkno >= 1 << (sizeof(int)*NBBY-PGSHIFT))
3491831Sbill 		blkno = 1 << ((sizeof(int)*NBBY-PGSHIFT) + 1);
3501831Sbill 	dblkno = fsbtodb(blkno);
3512325Swnj 	dp = BUFHASH(dev, dblkno);
3528Sbill     loop:
3532325Swnj 	for (bp = dp->b_forw; bp != dp; bp = bp->b_forw) {
3542325Swnj 		if (bp->b_blkno != dblkno || bp->b_dev != dev ||
3552325Swnj 		    bp->b_flags&B_INVAL)
3568Sbill 			continue;
357*5424Swnj 		s = spl6();
3588Sbill 		if (bp->b_flags&B_BUSY) {
3598Sbill 			bp->b_flags |= B_WANTED;
3608Sbill 			sleep((caddr_t)bp, PRIBIO+1);
361*5424Swnj 			splx(s);
3628Sbill 			goto loop;
3638Sbill 		}
364*5424Swnj 		splx(s);
3658Sbill #ifdef	DISKMON
3668Sbill 		i = 0;
3678Sbill 		dp = bp->av_forw;
3682325Swnj 		while ((dp->b_flags & B_HEAD) == 0) {
3698Sbill 			i++;
3708Sbill 			dp = dp->av_forw;
3718Sbill 		}
3722771Swnj 		if (i<64)
3738Sbill 			io_info.bufcount[i]++;
3748Sbill #endif
3758Sbill 		notavail(bp);
3768Sbill 		bp->b_flags |= B_CACHE;
3778Sbill 		return(bp);
3788Sbill 	}
37991Sbill 	if (major(dev) >= nblkdev)
38091Sbill 		panic("blkdev");
381*5424Swnj 	s = spl6();
3822325Swnj 	for (ep = &bfreelist[BQUEUES-1]; ep > bfreelist; ep--)
3832325Swnj 		if (ep->av_forw != ep)
3842325Swnj 			break;
3852325Swnj 	if (ep == bfreelist) {		/* no free blocks at all */
3862325Swnj 		ep->b_flags |= B_WANTED;
3872325Swnj 		sleep((caddr_t)ep, PRIBIO+1);
388*5424Swnj 		splx(s);
3898Sbill 		goto loop;
3908Sbill 	}
391*5424Swnj 	splx(s);
3922325Swnj 	bp = ep->av_forw;
3938Sbill 	notavail(bp);
3948Sbill 	if (bp->b_flags & B_DELWRI) {
3958Sbill 		bp->b_flags |= B_ASYNC;
3968Sbill 		bwrite(bp);
3978Sbill 		goto loop;
3988Sbill 	}
3993199Swnj #ifdef TRACE
4004033Swnj 	trace(TR_BRELSE, bp->b_dev, bp->b_blkno);
4012045Swnj #endif
4028Sbill 	bp->b_flags = B_BUSY;
4038Sbill 	bp->b_back->b_forw = bp->b_forw;
4048Sbill 	bp->b_forw->b_back = bp->b_back;
4058Sbill 	bp->b_forw = dp->b_forw;
4068Sbill 	bp->b_back = dp;
4078Sbill 	dp->b_forw->b_back = bp;
4088Sbill 	dp->b_forw = bp;
4098Sbill 	bp->b_dev = dev;
4108Sbill 	bp->b_blkno = dblkno;
4118Sbill 	return(bp);
4128Sbill }
4138Sbill 
4148Sbill /*
4158Sbill  * get an empty block,
4168Sbill  * not assigned to any particular device
4178Sbill  */
4188Sbill struct buf *
4198Sbill geteblk()
4208Sbill {
421182Sbill 	register struct buf *bp, *dp;
4228Sbill 
4238Sbill loop:
424124Sbill 	(void) spl6();
4252325Swnj 	for (dp = &bfreelist[BQUEUES-1]; dp > bfreelist; dp--)
4262325Swnj 		if (dp->av_forw != dp)
4272325Swnj 			break;
4282325Swnj 	if (dp == bfreelist) {		/* no free blocks */
4292325Swnj 		dp->b_flags |= B_WANTED;
4302325Swnj 		sleep((caddr_t)dp, PRIBIO+1);
4312325Swnj 		goto loop;
4328Sbill 	}
433124Sbill 	(void) spl0();
4342325Swnj 	bp = dp->av_forw;
4358Sbill 	notavail(bp);
4368Sbill 	if (bp->b_flags & B_DELWRI) {
4378Sbill 		bp->b_flags |= B_ASYNC;
4388Sbill 		bwrite(bp);
4398Sbill 		goto loop;
4408Sbill 	}
4413199Swnj #ifdef TRACE
4424033Swnj 	trace(TR_BRELSE, bp->b_dev, bp->b_blkno);
4432045Swnj #endif
4442325Swnj 	bp->b_flags = B_BUSY|B_INVAL;
4458Sbill 	bp->b_back->b_forw = bp->b_forw;
4468Sbill 	bp->b_forw->b_back = bp->b_back;
4478Sbill 	bp->b_forw = dp->b_forw;
4488Sbill 	bp->b_back = dp;
4498Sbill 	dp->b_forw->b_back = bp;
4508Sbill 	dp->b_forw = bp;
4518Sbill 	bp->b_dev = (dev_t)NODEV;
4528Sbill 	return(bp);
4538Sbill }
4548Sbill 
4558Sbill /*
4568Sbill  * Wait for I/O completion on the buffer; return errors
4578Sbill  * to the user.
4588Sbill  */
4598Sbill iowait(bp)
4608Sbill register struct buf *bp;
4618Sbill {
4628Sbill 
463124Sbill 	(void) spl6();
4648Sbill 	while ((bp->b_flags&B_DONE)==0)
4658Sbill 		sleep((caddr_t)bp, PRIBIO);
466124Sbill 	(void) spl0();
4678Sbill 	geterror(bp);
4688Sbill }
4698Sbill 
4702706Swnj #ifdef UNFAST
4718Sbill /*
4728Sbill  * Unlink a buffer from the available list and mark it busy.
4738Sbill  * (internal interface)
4748Sbill  */
4758Sbill notavail(bp)
4768Sbill register struct buf *bp;
4778Sbill {
4788Sbill 	register s;
4798Sbill 
4808Sbill 	s = spl6();
4818Sbill 	bp->av_back->av_forw = bp->av_forw;
4828Sbill 	bp->av_forw->av_back = bp->av_back;
4838Sbill 	bp->b_flags |= B_BUSY;
4848Sbill 	splx(s);
4858Sbill }
4868Sbill #endif
4878Sbill 
4888Sbill /*
4898Sbill  * Mark I/O complete on a buffer. If the header
4908Sbill  * indicates a dirty page push completion, the
4918Sbill  * header is inserted into the ``cleaned'' list
4928Sbill  * to be processed by the pageout daemon. Otherwise
4938Sbill  * release it if I/O is asynchronous, and wake
4948Sbill  * up anyone waiting for it.
4958Sbill  */
4968Sbill iodone(bp)
4978Sbill register struct buf *bp;
4988Sbill {
4998Sbill 	register int s;
5008Sbill 
501420Sbill 	if (bp->b_flags & B_DONE)
502420Sbill 		panic("dup iodone");
5038Sbill 	bp->b_flags |= B_DONE;
5048Sbill 	if (bp->b_flags & B_DIRTY) {
5058Sbill 		if (bp->b_flags & B_ERROR)
5068Sbill 			panic("IO err in push");
5078Sbill 		s = spl6();
5088Sbill 		bp->av_forw = bclnlist;
5098Sbill 		bp->b_bcount = swsize[bp - swbuf];
5108Sbill 		bp->b_pfcent = swpf[bp - swbuf];
5113601Swnj 		cnt.v_pgout++;
5123601Swnj 		cnt.v_pgpgout += bp->b_bcount / NBPG;
5138Sbill 		bclnlist = bp;
5148Sbill 		if (bswlist.b_flags & B_WANTED)
5158Sbill 			wakeup((caddr_t)&proc[2]);
5168Sbill 		splx(s);
517383Sbill 		return;
5188Sbill 	}
5198Sbill 	if (bp->b_flags&B_ASYNC)
5208Sbill 		brelse(bp);
5218Sbill 	else {
5228Sbill 		bp->b_flags &= ~B_WANTED;
5238Sbill 		wakeup((caddr_t)bp);
5248Sbill 	}
5258Sbill }
5268Sbill 
5278Sbill /*
5288Sbill  * Zero the core associated with a buffer.
5298Sbill  */
5308Sbill clrbuf(bp)
5318Sbill struct buf *bp;
5328Sbill {
5338Sbill 	register *p;
5348Sbill 	register c;
5358Sbill 
5368Sbill 	p = bp->b_un.b_words;
5378Sbill 	c = BSIZE/sizeof(int);
5388Sbill 	do
5398Sbill 		*p++ = 0;
5408Sbill 	while (--c);
5418Sbill 	bp->b_resid = 0;
5428Sbill }
5438Sbill 
5448Sbill /*
5458Sbill  * swap I/O -
5468Sbill  *
5478Sbill  * If the flag indicates a dirty page push initiated
5488Sbill  * by the pageout daemon, we map the page into the i th
5498Sbill  * virtual page of process 2 (the daemon itself) where i is
5508Sbill  * the index of the swap header that has been allocated.
5518Sbill  * We simply initialize the header and queue the I/O but
5528Sbill  * do not wait for completion. When the I/O completes,
5538Sbill  * iodone() will link the header to a list of cleaned
5548Sbill  * pages to be processed by the pageout daemon.
5558Sbill  */
5568Sbill swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent)
5578Sbill 	struct proc *p;
5588Sbill 	swblk_t dblkno;
5598Sbill 	caddr_t addr;
5608Sbill 	int flag, nbytes;
5618Sbill 	dev_t dev;
5628Sbill 	unsigned pfcent;
5638Sbill {
5648Sbill 	register struct buf *bp;
5658Sbill 	register int c;
5668Sbill 	int p2dp;
5678Sbill 	register struct pte *dpte, *vpte;
5688Sbill 
569124Sbill 	(void) spl6();
5708Sbill 	while (bswlist.av_forw == NULL) {
5718Sbill 		bswlist.b_flags |= B_WANTED;
5728Sbill 		sleep((caddr_t)&bswlist, PSWP+1);
5738Sbill 	}
5748Sbill 	bp = bswlist.av_forw;
5758Sbill 	bswlist.av_forw = bp->av_forw;
576124Sbill 	(void) spl0();
5778Sbill 
5788Sbill 	bp->b_flags = B_BUSY | B_PHYS | rdflg | flag;
5798Sbill 	if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0)
5808Sbill 		if (rdflg == B_READ)
5818Sbill 			sum.v_pswpin += btoc(nbytes);
5828Sbill 		else
5838Sbill 			sum.v_pswpout += btoc(nbytes);
5848Sbill 	bp->b_proc = p;
5858Sbill 	if (flag & B_DIRTY) {
5868Sbill 		p2dp = ((bp - swbuf) * CLSIZE) * KLMAX;
5878Sbill 		dpte = dptopte(&proc[2], p2dp);
5888Sbill 		vpte = vtopte(p, btop(addr));
5898Sbill 		for (c = 0; c < nbytes; c += NBPG) {
5908Sbill 			if (vpte->pg_pfnum == 0 || vpte->pg_fod)
5918Sbill 				panic("swap bad pte");
5928Sbill 			*dpte++ = *vpte++;
5938Sbill 		}
5948Sbill 		bp->b_un.b_addr = (caddr_t)ctob(p2dp);
5958Sbill 	} else
5968Sbill 		bp->b_un.b_addr = addr;
5978Sbill 	while (nbytes > 0) {
5988Sbill 		c = imin(ctob(120), nbytes);
5998Sbill 		bp->b_bcount = c;
6008Sbill 		bp->b_blkno = dblkno;
6018Sbill 		bp->b_dev = dev;
602718Sbill 		if (flag & B_DIRTY) {
603718Sbill 			swpf[bp - swbuf] = pfcent;
604718Sbill 			swsize[bp - swbuf] = nbytes;
605718Sbill 		}
6064033Swnj #ifdef TRACE
6074033Swnj 		trace(TR_SWAPIO, dev, bp->b_blkno);
6084033Swnj #endif
6098Sbill 		(*bdevsw[major(dev)].d_strategy)(bp);
6108Sbill 		if (flag & B_DIRTY) {
6118Sbill 			if (c < nbytes)
6128Sbill 				panic("big push");
6138Sbill 			return;
6148Sbill 		}
615124Sbill 		(void) spl6();
6168Sbill 		while((bp->b_flags&B_DONE)==0)
6178Sbill 			sleep((caddr_t)bp, PSWP);
618124Sbill 		(void) spl0();
6198Sbill 		bp->b_un.b_addr += c;
6208Sbill 		bp->b_flags &= ~B_DONE;
6218Sbill 		if (bp->b_flags & B_ERROR) {
6228Sbill 			if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
6238Sbill 				panic("hard IO err in swap");
6248Sbill 			swkill(p, (char *)0);
6258Sbill 		}
6268Sbill 		nbytes -= c;
6278Sbill 		dblkno += btoc(c);
6288Sbill 	}
629124Sbill 	(void) spl6();
6308Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
6318Sbill 	bp->av_forw = bswlist.av_forw;
6328Sbill 	bswlist.av_forw = bp;
6338Sbill 	if (bswlist.b_flags & B_WANTED) {
6348Sbill 		bswlist.b_flags &= ~B_WANTED;
6358Sbill 		wakeup((caddr_t)&bswlist);
6368Sbill 		wakeup((caddr_t)&proc[2]);
6378Sbill 	}
638124Sbill 	(void) spl0();
6398Sbill }
6408Sbill 
6418Sbill /*
6428Sbill  * If rout == 0 then killed on swap error, else
6438Sbill  * rout is the name of the routine where we ran out of
6448Sbill  * swap space.
6458Sbill  */
6468Sbill swkill(p, rout)
6478Sbill 	struct proc *p;
6488Sbill 	char *rout;
6498Sbill {
6502922Swnj 	char *mesg;
6518Sbill 
6522922Swnj 	printf("pid %d: ", p->p_pid);
6538Sbill 	if (rout)
6542922Swnj 		printf(mesg = "killed due to no swap space\n");
6558Sbill 	else
6562922Swnj 		printf(mesg = "killed on swap error\n");
6572922Swnj 	uprintf("sorry, pid %d was %s", p->p_pid, mesg);
6588Sbill 	/*
6598Sbill 	 * To be sure no looping (e.g. in vmsched trying to
6608Sbill 	 * swap out) mark process locked in core (as though
6618Sbill 	 * done by user) after killing it so noone will try
6628Sbill 	 * to swap it out.
6638Sbill 	 */
664165Sbill 	psignal(p, SIGKILL);
6658Sbill 	p->p_flag |= SULOCK;
6668Sbill }
6678Sbill 
6688Sbill /*
6698Sbill  * make sure all write-behind blocks
6708Sbill  * on dev (or NODEV for all)
6718Sbill  * are flushed out.
6728Sbill  * (from umount and update)
6738Sbill  */
6748Sbill bflush(dev)
6758Sbill dev_t dev;
6768Sbill {
6778Sbill 	register struct buf *bp;
6782325Swnj 	register struct buf *flist;
6798Sbill 
6808Sbill loop:
681124Sbill 	(void) spl6();
6822325Swnj 	for (flist = bfreelist; flist < &bfreelist[BQUEUES]; flist++)
6832325Swnj 	for (bp = flist->av_forw; bp != flist; bp = bp->av_forw) {
6848Sbill 		if (bp->b_flags&B_DELWRI && (dev == NODEV||dev==bp->b_dev)) {
6858Sbill 			bp->b_flags |= B_ASYNC;
6868Sbill 			notavail(bp);
6878Sbill 			bwrite(bp);
6888Sbill 			goto loop;
6898Sbill 		}
6908Sbill 	}
691124Sbill 	(void) spl0();
6928Sbill }
6938Sbill 
6948Sbill /*
6958Sbill  * Raw I/O. The arguments are
6968Sbill  *	The strategy routine for the device
6978Sbill  *	A buffer, which will always be a special buffer
6988Sbill  *	  header owned exclusively by the device for this purpose
6998Sbill  *	The device number
7008Sbill  *	Read/write flag
7018Sbill  * Essentially all the work is computing physical addresses and
7028Sbill  * validating them.
7038Sbill  * If the user has the proper access privilidges, the process is
7048Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
7058Sbill  * faulted and locked. After the completion of the I/O, the above pages
7068Sbill  * are unlocked.
7078Sbill  */
7088Sbill physio(strat, bp, dev, rw, mincnt)
7098Sbill int (*strat)();
7108Sbill register struct buf *bp;
7118Sbill unsigned (*mincnt)();
7128Sbill {
7138Sbill 	register int c;
7148Sbill 	char *a;
7158Sbill 
7168Sbill 	if (useracc(u.u_base,u.u_count,rw==B_READ?B_WRITE:B_READ) == NULL) {
7178Sbill 		u.u_error = EFAULT;
7188Sbill 		return;
7198Sbill 	}
720124Sbill 	(void) spl6();
7218Sbill 	while (bp->b_flags&B_BUSY) {
7228Sbill 		bp->b_flags |= B_WANTED;
7238Sbill 		sleep((caddr_t)bp, PRIBIO+1);
7248Sbill 	}
7258Sbill 	bp->b_error = 0;
7268Sbill 	bp->b_proc = u.u_procp;
7278Sbill 	bp->b_un.b_addr = u.u_base;
7283667Swnj 	while (u.u_count != 0) {
7298Sbill 		bp->b_flags = B_BUSY | B_PHYS | rw;
7308Sbill 		bp->b_dev = dev;
7318Sbill 		bp->b_blkno = u.u_offset >> PGSHIFT;
7328Sbill 		bp->b_bcount = u.u_count;
7338Sbill 		(*mincnt)(bp);
7348Sbill 		c = bp->b_bcount;
7358Sbill 		u.u_procp->p_flag |= SPHYSIO;
7368Sbill 		vslock(a = bp->b_un.b_addr, c);
7378Sbill 		(*strat)(bp);
738124Sbill 		(void) spl6();
7398Sbill 		while ((bp->b_flags&B_DONE) == 0)
7408Sbill 			sleep((caddr_t)bp, PRIBIO);
7418Sbill 		vsunlock(a, c, rw);
7428Sbill 		u.u_procp->p_flag &= ~SPHYSIO;
7438Sbill 		if (bp->b_flags&B_WANTED)
7448Sbill 			wakeup((caddr_t)bp);
745124Sbill 		(void) spl0();
7468Sbill 		bp->b_un.b_addr += c;
7478Sbill 		u.u_count -= c;
7488Sbill 		u.u_offset += c;
7493667Swnj 		if (bp->b_flags&B_ERROR)
7503667Swnj 			break;
7518Sbill 	}
7528Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
7538Sbill 	u.u_count = bp->b_resid;
7548Sbill 	geterror(bp);
7558Sbill }
7568Sbill 
7578Sbill /*ARGSUSED*/
7588Sbill unsigned
7598Sbill minphys(bp)
7608Sbill struct buf *bp;
7618Sbill {
7628Sbill 
7638Sbill 	if (bp->b_bcount > 60 * 1024)
7648Sbill 		bp->b_bcount = 60 * 1024;
7658Sbill }
7668Sbill 
7678Sbill /*
7688Sbill  * Pick up the device's error number and pass it to the user;
7698Sbill  * if there is an error but the number is 0 set a generalized
7708Sbill  * code.  Actually the latter is always true because devices
7718Sbill  * don't yet return specific errors.
7728Sbill  */
7738Sbill geterror(bp)
7748Sbill register struct buf *bp;
7758Sbill {
7768Sbill 
7778Sbill 	if (bp->b_flags&B_ERROR)
7788Sbill 		if ((u.u_error = bp->b_error)==0)
7798Sbill 			u.u_error = EIO;
7808Sbill }
7812299Skre 
7822299Skre /*
7832299Skre  * Invalidate in core blocks belonging to closed or umounted filesystem
7842299Skre  *
7852299Skre  * This is not nicely done at all - the buffer ought to be removed from the
7862299Skre  * hash chains & have its dev/blkno fields clobbered, but unfortunately we
7872299Skre  * can't do that here, as it is quite possible that the block is still
7882299Skre  * being used for i/o. Eventually, all disc drivers should be forced to
7892299Skre  * have a close routine, which ought ensure that the queue is empty, then
7902299Skre  * properly flush the queues. Until that happy day, this suffices for
7912299Skre  * correctness.						... kre
7922299Skre  */
7932299Skre binval(dev)
7942299Skre dev_t dev;
7952299Skre {
7962361Skre 	register struct buf *bp;
7972361Skre 	register struct bufhd *hp;
7982361Skre #define dp ((struct buf *)hp)
7992299Skre 
8002361Skre 	for (hp = bufhash; hp < &bufhash[BUFHSZ]; hp++)
8012361Skre 		for (bp = dp->b_forw; bp != dp; bp = bp->b_forw)
8022361Skre 			if (bp->b_dev == dev)
8032361Skre 				bp->b_flags |= B_INVAL;
8042299Skre }
805