xref: /csrg-svn/sys/kern/kern_physio.c (revision 12491)
1*12491Ssam /*	kern_physio.c	4.40	83/05/18	*/
28Sbill 
39766Ssam #include "../machine/pte.h"
49766Ssam 
58Sbill #include "../h/param.h"
68Sbill #include "../h/systm.h"
78Sbill #include "../h/dir.h"
88Sbill #include "../h/user.h"
98Sbill #include "../h/buf.h"
108Sbill #include "../h/conf.h"
118Sbill #include "../h/proc.h"
128Sbill #include "../h/seg.h"
138Sbill #include "../h/vm.h"
142045Swnj #include "../h/trace.h"
15*12491Ssam #include "../h/map.h"
167724Swnj #include "../h/uio.h"
178Sbill 
1891Sbill /*
198Sbill  * Swap IO headers -
208Sbill  * They contain the necessary information for the swap I/O.
218Sbill  * At any given time, a swap header can be in three
228Sbill  * different lists. When free it is in the free list,
238Sbill  * when allocated and the I/O queued, it is on the swap
248Sbill  * device list, and finally, if the operation was a dirty
258Sbill  * page push, when the I/O completes, it is inserted
268Sbill  * in a list of cleaned pages to be processed by the pageout daemon.
278Sbill  */
282771Swnj struct	buf *swbuf;
298Sbill 
308Sbill /*
318Sbill  * swap I/O -
328Sbill  *
338Sbill  * If the flag indicates a dirty page push initiated
348Sbill  * by the pageout daemon, we map the page into the i th
358Sbill  * virtual page of process 2 (the daemon itself) where i is
368Sbill  * the index of the swap header that has been allocated.
378Sbill  * We simply initialize the header and queue the I/O but
388Sbill  * do not wait for completion. When the I/O completes,
398Sbill  * iodone() will link the header to a list of cleaned
408Sbill  * pages to be processed by the pageout daemon.
418Sbill  */
428Sbill swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent)
438Sbill 	struct proc *p;
448Sbill 	swblk_t dblkno;
458Sbill 	caddr_t addr;
468674S 	int nbytes, rdflg, flag;
478Sbill 	dev_t dev;
488674S 	u_int pfcent;
498Sbill {
508Sbill 	register struct buf *bp;
518962Sroot 	register u_int c;
528Sbill 	int p2dp;
538Sbill 	register struct pte *dpte, *vpte;
545431Sroot 	int s;
55*12491Ssam 	extern swdone();
568Sbill 
575431Sroot 	s = spl6();
588Sbill 	while (bswlist.av_forw == NULL) {
598Sbill 		bswlist.b_flags |= B_WANTED;
608Sbill 		sleep((caddr_t)&bswlist, PSWP+1);
618Sbill 	}
628Sbill 	bp = bswlist.av_forw;
638Sbill 	bswlist.av_forw = bp->av_forw;
645431Sroot 	splx(s);
658Sbill 
668Sbill 	bp->b_flags = B_BUSY | B_PHYS | rdflg | flag;
678Sbill 	if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0)
688Sbill 		if (rdflg == B_READ)
698Sbill 			sum.v_pswpin += btoc(nbytes);
708Sbill 		else
718Sbill 			sum.v_pswpout += btoc(nbytes);
728Sbill 	bp->b_proc = p;
738Sbill 	if (flag & B_DIRTY) {
748Sbill 		p2dp = ((bp - swbuf) * CLSIZE) * KLMAX;
758Sbill 		dpte = dptopte(&proc[2], p2dp);
768Sbill 		vpte = vtopte(p, btop(addr));
778Sbill 		for (c = 0; c < nbytes; c += NBPG) {
788Sbill 			if (vpte->pg_pfnum == 0 || vpte->pg_fod)
798Sbill 				panic("swap bad pte");
808Sbill 			*dpte++ = *vpte++;
818Sbill 		}
82*12491Ssam 		bp->b_un.b_addr = (caddr_t)ctob(dptov(&proc[2], p2dp));
83*12491Ssam 		bp->b_flags |= B_CALL;
84*12491Ssam 		bp->b_iodone = swdone;
85*12491Ssam 		bp->b_pfcent = pfcent;
868Sbill 	} else
878Sbill 		bp->b_un.b_addr = addr;
888Sbill 	while (nbytes > 0) {
898962Sroot 		bp->b_bcount = nbytes;
908962Sroot 		minphys(bp);
918962Sroot 		c = bp->b_bcount;
928Sbill 		bp->b_blkno = dblkno;
938Sbill 		bp->b_dev = dev;
944033Swnj #ifdef TRACE
954033Swnj 		trace(TR_SWAPIO, dev, bp->b_blkno);
964033Swnj #endif
979011Sroot 		physstrat(bp, bdevsw[major(dev)].d_strategy, PSWP);
988Sbill 		if (flag & B_DIRTY) {
998Sbill 			if (c < nbytes)
1008Sbill 				panic("big push");
1018Sbill 			return;
1028Sbill 		}
1038Sbill 		bp->b_un.b_addr += c;
1048Sbill 		bp->b_flags &= ~B_DONE;
1058Sbill 		if (bp->b_flags & B_ERROR) {
1068Sbill 			if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
1078Sbill 				panic("hard IO err in swap");
1088Sbill 			swkill(p, (char *)0);
1098Sbill 		}
1108Sbill 		nbytes -= c;
1118962Sroot 		dblkno += c / DEV_BSIZE;
1128Sbill 	}
1135431Sroot 	s = spl6();
1148Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
1158Sbill 	bp->av_forw = bswlist.av_forw;
1168Sbill 	bswlist.av_forw = bp;
1178Sbill 	if (bswlist.b_flags & B_WANTED) {
1188Sbill 		bswlist.b_flags &= ~B_WANTED;
1198Sbill 		wakeup((caddr_t)&bswlist);
1208Sbill 		wakeup((caddr_t)&proc[2]);
1218Sbill 	}
1225431Sroot 	splx(s);
1238Sbill }
1248Sbill 
1258Sbill /*
126*12491Ssam  * Put a buffer on the clean list after I/O is done.
127*12491Ssam  * Called from biodone.
128*12491Ssam  */
129*12491Ssam swdone(bp)
130*12491Ssam 	register struct buf *bp;
131*12491Ssam {
132*12491Ssam 	register int s;
133*12491Ssam 
134*12491Ssam 	if (bp->b_flags & B_ERROR)
135*12491Ssam 		panic("IO err in push");
136*12491Ssam 	s = spl6();
137*12491Ssam 	bp->av_forw = bclnlist;
138*12491Ssam 	cnt.v_pgout++;
139*12491Ssam 	cnt.v_pgpgout += bp->b_bcount / NBPG;
140*12491Ssam 	bclnlist = bp;
141*12491Ssam 	if (bswlist.b_flags & B_WANTED)
142*12491Ssam 		wakeup((caddr_t)&proc[2]);
143*12491Ssam 	splx(s);
144*12491Ssam }
145*12491Ssam 
146*12491Ssam /*
1478Sbill  * If rout == 0 then killed on swap error, else
1488Sbill  * rout is the name of the routine where we ran out of
1498Sbill  * swap space.
1508Sbill  */
1518Sbill swkill(p, rout)
1528Sbill 	struct proc *p;
1538Sbill 	char *rout;
1548Sbill {
1552922Swnj 	char *mesg;
1568Sbill 
1572922Swnj 	printf("pid %d: ", p->p_pid);
1588Sbill 	if (rout)
1592922Swnj 		printf(mesg = "killed due to no swap space\n");
1608Sbill 	else
1612922Swnj 		printf(mesg = "killed on swap error\n");
1622922Swnj 	uprintf("sorry, pid %d was %s", p->p_pid, mesg);
1638Sbill 	/*
1648Sbill 	 * To be sure no looping (e.g. in vmsched trying to
1658Sbill 	 * swap out) mark process locked in core (as though
1668Sbill 	 * done by user) after killing it so noone will try
1678Sbill 	 * to swap it out.
1688Sbill 	 */
169165Sbill 	psignal(p, SIGKILL);
1708Sbill 	p->p_flag |= SULOCK;
1718Sbill }
1728Sbill 
1738Sbill /*
1748Sbill  * Raw I/O. The arguments are
1758Sbill  *	The strategy routine for the device
1768Sbill  *	A buffer, which will always be a special buffer
1778Sbill  *	  header owned exclusively by the device for this purpose
1788Sbill  *	The device number
1798Sbill  *	Read/write flag
1808Sbill  * Essentially all the work is computing physical addresses and
1818Sbill  * validating them.
1828Sbill  * If the user has the proper access privilidges, the process is
1838Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
1848Sbill  * faulted and locked. After the completion of the I/O, the above pages
1858Sbill  * are unlocked.
1868Sbill  */
1877724Swnj physio(strat, bp, dev, rw, mincnt, uio)
1887724Swnj 	int (*strat)();
1897724Swnj 	register struct buf *bp;
1907724Swnj 	dev_t dev;
1917724Swnj 	int rw;
1927724Swnj 	unsigned (*mincnt)();
1937724Swnj 	struct uio *uio;
1948Sbill {
1957830Sroot 	register struct iovec *iov = uio->uio_iov;
1968Sbill 	register int c;
1978Sbill 	char *a;
1987724Swnj 	int s, error = 0;
1998Sbill 
2007724Swnj nextiov:
2017830Sroot 	if (uio->uio_iovcnt == 0)
2027724Swnj 		return (0);
2037830Sroot 	if (useracc(iov->iov_base,(u_int)iov->iov_len,rw==B_READ?B_WRITE:B_READ) == NULL)
2047724Swnj 		return (EFAULT);
2055431Sroot 	s = spl6();
2068Sbill 	while (bp->b_flags&B_BUSY) {
2078Sbill 		bp->b_flags |= B_WANTED;
2088Sbill 		sleep((caddr_t)bp, PRIBIO+1);
2098Sbill 	}
2106319Swnj 	splx(s);
2118Sbill 	bp->b_error = 0;
2128Sbill 	bp->b_proc = u.u_procp;
2137724Swnj 	bp->b_un.b_addr = iov->iov_base;
2147724Swnj 	while (iov->iov_len > 0) {
2158Sbill 		bp->b_flags = B_BUSY | B_PHYS | rw;
2168Sbill 		bp->b_dev = dev;
2178962Sroot 		bp->b_blkno = uio->uio_offset / DEV_BSIZE;
2187724Swnj 		bp->b_bcount = iov->iov_len;
2198Sbill 		(*mincnt)(bp);
2208Sbill 		c = bp->b_bcount;
2218Sbill 		u.u_procp->p_flag |= SPHYSIO;
2228Sbill 		vslock(a = bp->b_un.b_addr, c);
2238962Sroot 		physstrat(bp, strat, PRIBIO);
224124Sbill 		(void) spl6();
2258Sbill 		vsunlock(a, c, rw);
2268Sbill 		u.u_procp->p_flag &= ~SPHYSIO;
2278Sbill 		if (bp->b_flags&B_WANTED)
2288Sbill 			wakeup((caddr_t)bp);
2295431Sroot 		splx(s);
2307724Swnj 		c -= bp->b_resid;
2318Sbill 		bp->b_un.b_addr += c;
2327724Swnj 		iov->iov_len -= c;
2337724Swnj 		uio->uio_resid -= c;
2347724Swnj 		uio->uio_offset += c;
2359766Ssam 		/* temp kludge for tape drives */
23610400Ssam 		if (bp->b_resid || (bp->b_flags&B_ERROR))
2373667Swnj 			break;
2388Sbill 	}
2398Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
2407724Swnj 	error = geterror(bp);
2419766Ssam 	/* temp kludge for tape drives */
2429766Ssam 	if (bp->b_resid || error)
2437724Swnj 		return (error);
2447724Swnj 	uio->uio_iov++;
2457724Swnj 	uio->uio_iovcnt--;
2467724Swnj 	goto nextiov;
2478Sbill }
2488Sbill 
24910400Ssam #define	MAXPHYS	(63 * 1024)
25010400Ssam 
25110400Ssam /* network disk brain damage */
25210400Ssam #include "nd.h"
25310400Ssam #if NND > 0
25410430Ssam #undef MAXPHYS
25510400Ssam #define	MAXPHYS	(32 * 1024)
25610400Ssam #endif
25710400Ssam 
2588Sbill unsigned
2598Sbill minphys(bp)
2607724Swnj 	struct buf *bp;
2618Sbill {
2628Sbill 
26310400Ssam 	if (bp->b_bcount > MAXPHYS)
26410400Ssam 		bp->b_bcount = MAXPHYS;
2658Sbill }
2668Sbill 
267