xref: /csrg-svn/sys/kern/kern_physio.c (revision 10400)
1*10400Ssam /*	kern_physio.c	4.38	83/01/17	*/
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"
157724Swnj #include "../h/uio.h"
168Sbill 
1791Sbill /*
188Sbill  * Swap IO headers -
198Sbill  * They contain the necessary information for the swap I/O.
208Sbill  * At any given time, a swap header can be in three
218Sbill  * different lists. When free it is in the free list,
228Sbill  * when allocated and the I/O queued, it is on the swap
238Sbill  * device list, and finally, if the operation was a dirty
248Sbill  * page push, when the I/O completes, it is inserted
258Sbill  * in a list of cleaned pages to be processed by the pageout daemon.
268Sbill  */
272771Swnj struct	buf *swbuf;
282771Swnj short	*swsize;		/* CAN WE JUST USE B_BCOUNT? */
292771Swnj int	*swpf;
308Sbill 
318Sbill /*
328Sbill  * swap I/O -
338Sbill  *
348Sbill  * If the flag indicates a dirty page push initiated
358Sbill  * by the pageout daemon, we map the page into the i th
368Sbill  * virtual page of process 2 (the daemon itself) where i is
378Sbill  * the index of the swap header that has been allocated.
388Sbill  * We simply initialize the header and queue the I/O but
398Sbill  * do not wait for completion. When the I/O completes,
408Sbill  * iodone() will link the header to a list of cleaned
418Sbill  * pages to be processed by the pageout daemon.
428Sbill  */
438Sbill swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent)
448Sbill 	struct proc *p;
458Sbill 	swblk_t dblkno;
468Sbill 	caddr_t addr;
478674S 	int nbytes, rdflg, flag;
488Sbill 	dev_t dev;
498674S 	u_int pfcent;
508Sbill {
518Sbill 	register struct buf *bp;
528962Sroot 	register u_int c;
538Sbill 	int p2dp;
548Sbill 	register struct pte *dpte, *vpte;
555431Sroot 	int s;
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 		}
828Sbill 		bp->b_un.b_addr = (caddr_t)ctob(p2dp);
838Sbill 	} else
848Sbill 		bp->b_un.b_addr = addr;
858Sbill 	while (nbytes > 0) {
868962Sroot 		bp->b_bcount = nbytes;
878962Sroot 		minphys(bp);
888962Sroot 		c = bp->b_bcount;
898Sbill 		bp->b_blkno = dblkno;
908Sbill 		bp->b_dev = dev;
91718Sbill 		if (flag & B_DIRTY) {
92718Sbill 			swpf[bp - swbuf] = pfcent;
93718Sbill 			swsize[bp - swbuf] = nbytes;
94718Sbill 		}
954033Swnj #ifdef TRACE
964033Swnj 		trace(TR_SWAPIO, dev, bp->b_blkno);
974033Swnj #endif
989011Sroot 		physstrat(bp, bdevsw[major(dev)].d_strategy, PSWP);
998Sbill 		if (flag & B_DIRTY) {
1008Sbill 			if (c < nbytes)
1018Sbill 				panic("big push");
1028Sbill 			return;
1038Sbill 		}
1048Sbill 		bp->b_un.b_addr += c;
1058Sbill 		bp->b_flags &= ~B_DONE;
1068Sbill 		if (bp->b_flags & B_ERROR) {
1078Sbill 			if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
1088Sbill 				panic("hard IO err in swap");
1098Sbill 			swkill(p, (char *)0);
1108Sbill 		}
1118Sbill 		nbytes -= c;
1128962Sroot 		dblkno += c / DEV_BSIZE;
1138Sbill 	}
1145431Sroot 	s = spl6();
1158Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
1168Sbill 	bp->av_forw = bswlist.av_forw;
1178Sbill 	bswlist.av_forw = bp;
1188Sbill 	if (bswlist.b_flags & B_WANTED) {
1198Sbill 		bswlist.b_flags &= ~B_WANTED;
1208Sbill 		wakeup((caddr_t)&bswlist);
1218Sbill 		wakeup((caddr_t)&proc[2]);
1228Sbill 	}
1235431Sroot 	splx(s);
1248Sbill }
1258Sbill 
1268Sbill /*
1278Sbill  * If rout == 0 then killed on swap error, else
1288Sbill  * rout is the name of the routine where we ran out of
1298Sbill  * swap space.
1308Sbill  */
1318Sbill swkill(p, rout)
1328Sbill 	struct proc *p;
1338Sbill 	char *rout;
1348Sbill {
1352922Swnj 	char *mesg;
1368Sbill 
1372922Swnj 	printf("pid %d: ", p->p_pid);
1388Sbill 	if (rout)
1392922Swnj 		printf(mesg = "killed due to no swap space\n");
1408Sbill 	else
1412922Swnj 		printf(mesg = "killed on swap error\n");
1422922Swnj 	uprintf("sorry, pid %d was %s", p->p_pid, mesg);
1438Sbill 	/*
1448Sbill 	 * To be sure no looping (e.g. in vmsched trying to
1458Sbill 	 * swap out) mark process locked in core (as though
1468Sbill 	 * done by user) after killing it so noone will try
1478Sbill 	 * to swap it out.
1488Sbill 	 */
149165Sbill 	psignal(p, SIGKILL);
1508Sbill 	p->p_flag |= SULOCK;
1518Sbill }
1528Sbill 
1538Sbill /*
1548Sbill  * Raw I/O. The arguments are
1558Sbill  *	The strategy routine for the device
1568Sbill  *	A buffer, which will always be a special buffer
1578Sbill  *	  header owned exclusively by the device for this purpose
1588Sbill  *	The device number
1598Sbill  *	Read/write flag
1608Sbill  * Essentially all the work is computing physical addresses and
1618Sbill  * validating them.
1628Sbill  * If the user has the proper access privilidges, the process is
1638Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
1648Sbill  * faulted and locked. After the completion of the I/O, the above pages
1658Sbill  * are unlocked.
1668Sbill  */
1677724Swnj physio(strat, bp, dev, rw, mincnt, uio)
1687724Swnj 	int (*strat)();
1697724Swnj 	register struct buf *bp;
1707724Swnj 	dev_t dev;
1717724Swnj 	int rw;
1727724Swnj 	unsigned (*mincnt)();
1737724Swnj 	struct uio *uio;
1748Sbill {
1757830Sroot 	register struct iovec *iov = uio->uio_iov;
1768Sbill 	register int c;
1778Sbill 	char *a;
1787724Swnj 	int s, error = 0;
1798Sbill 
1807724Swnj nextiov:
1817830Sroot 	if (uio->uio_iovcnt == 0)
1827724Swnj 		return (0);
1837830Sroot 	if (useracc(iov->iov_base,(u_int)iov->iov_len,rw==B_READ?B_WRITE:B_READ) == NULL)
1847724Swnj 		return (EFAULT);
1855431Sroot 	s = spl6();
1868Sbill 	while (bp->b_flags&B_BUSY) {
1878Sbill 		bp->b_flags |= B_WANTED;
1888Sbill 		sleep((caddr_t)bp, PRIBIO+1);
1898Sbill 	}
1906319Swnj 	splx(s);
1918Sbill 	bp->b_error = 0;
1928Sbill 	bp->b_proc = u.u_procp;
1937724Swnj 	bp->b_un.b_addr = iov->iov_base;
1947724Swnj 	while (iov->iov_len > 0) {
1958Sbill 		bp->b_flags = B_BUSY | B_PHYS | rw;
1968Sbill 		bp->b_dev = dev;
1978962Sroot 		bp->b_blkno = uio->uio_offset / DEV_BSIZE;
1987724Swnj 		bp->b_bcount = iov->iov_len;
1998Sbill 		(*mincnt)(bp);
2008Sbill 		c = bp->b_bcount;
2018Sbill 		u.u_procp->p_flag |= SPHYSIO;
2028Sbill 		vslock(a = bp->b_un.b_addr, c);
2038962Sroot 		physstrat(bp, strat, PRIBIO);
204124Sbill 		(void) spl6();
2058Sbill 		vsunlock(a, c, rw);
2068Sbill 		u.u_procp->p_flag &= ~SPHYSIO;
2078Sbill 		if (bp->b_flags&B_WANTED)
2088Sbill 			wakeup((caddr_t)bp);
2095431Sroot 		splx(s);
2107724Swnj 		c -= bp->b_resid;
2118Sbill 		bp->b_un.b_addr += c;
2127724Swnj 		iov->iov_len -= c;
2137724Swnj 		uio->uio_resid -= c;
2147724Swnj 		uio->uio_offset += c;
2159766Ssam 		/* temp kludge for tape drives */
216*10400Ssam 		if (bp->b_resid || (bp->b_flags&B_ERROR))
2173667Swnj 			break;
2188Sbill 	}
2198Sbill 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
2207724Swnj 	error = geterror(bp);
2219766Ssam 	/* temp kludge for tape drives */
2229766Ssam 	if (bp->b_resid || error)
2237724Swnj 		return (error);
2247724Swnj 	uio->uio_iov++;
2257724Swnj 	uio->uio_iovcnt--;
2267724Swnj 	goto nextiov;
2278Sbill }
2288Sbill 
229*10400Ssam #define	MAXPHYS	(63 * 1024)
230*10400Ssam 
231*10400Ssam /* network disk brain damage */
232*10400Ssam #include "nd.h"
233*10400Ssam #if NND > 0
234*10400Ssam #define	MAXPHYS	(32 * 1024)
235*10400Ssam #endif
236*10400Ssam 
2378Sbill unsigned
2388Sbill minphys(bp)
2397724Swnj 	struct buf *bp;
2408Sbill {
2418Sbill 
242*10400Ssam 	if (bp->b_bcount > MAXPHYS)
243*10400Ssam 		bp->b_bcount = MAXPHYS;
2448Sbill }
2458Sbill 
246