xref: /csrg-svn/sys/kern/kern_physio.c (revision 38881)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)kern_physio.c	7.9 (Berkeley) 08/30/89
7  */
8 
9 #include "param.h"
10 #include "systm.h"
11 #include "user.h"
12 #include "buf.h"
13 #include "conf.h"
14 #include "proc.h"
15 #include "seg.h"
16 #include "vm.h"
17 #include "trace.h"
18 #include "map.h"
19 #include "vnode.h"
20 
21 #include "machine/pte.h"
22 
23 /*
24  * Swap IO headers -
25  * They contain the necessary information for the swap I/O.
26  * At any given time, a swap header can be in three
27  * different lists. When free it is in the free list,
28  * when allocated and the I/O queued, it is on the swap
29  * device list, and finally, if the operation was a dirty
30  * page push, when the I/O completes, it is inserted
31  * in a list of cleaned pages to be processed by the pageout daemon.
32  */
33 struct	buf *swbuf;
34 
35 /*
36  * swap I/O -
37  *
38  * If the flag indicates a dirty page push initiated
39  * by the pageout daemon, we map the page into the i th
40  * virtual page of process 2 (the daemon itself) where i is
41  * the index of the swap header that has been allocated.
42  * We simply initialize the header and queue the I/O but
43  * do not wait for completion. When the I/O completes,
44  * biodone() will link the header to a list of cleaned
45  * pages to be processed by the pageout daemon.
46  */
47 swap(p, dblkno, addr, nbytes, rdflg, flag, vp, pfcent)
48 	struct proc *p;
49 	swblk_t dblkno;
50 	caddr_t addr;
51 	int nbytes, rdflg, flag;
52 	struct vnode *vp;
53 	u_int pfcent;
54 {
55 	register struct buf *bp;
56 	register struct pte *dpte, *vpte;
57 	register u_int c;
58 	int p2dp, s, error = 0;
59 	struct buf *getswbuf();
60 	int swdone();
61 
62 	bp = getswbuf(PSWP+1);
63 	bp->b_flags = B_BUSY | B_PHYS | rdflg | flag;
64 	if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0)
65 		if (rdflg == B_READ)
66 			sum.v_pswpin += btoc(nbytes);
67 		else
68 			sum.v_pswpout += btoc(nbytes);
69 	bp->b_proc = p;
70 	if (flag & B_DIRTY) {
71 		p2dp = ((bp - swbuf) * CLSIZE) * KLMAX;
72 		dpte = dptopte(&proc[2], p2dp);
73 		vpte = vtopte(p, btop(addr));
74 		for (c = 0; c < nbytes; c += NBPG) {
75 			if (vpte->pg_pfnum == 0 || vpte->pg_fod)
76 				panic("swap bad pte");
77 			*dpte++ = *vpte++;
78 		}
79 		bp->b_un.b_addr = (caddr_t)ctob(dptov(&proc[2], p2dp));
80 		bp->b_flags |= B_CALL;
81 		bp->b_iodone = swdone;
82 		bp->b_pfcent = pfcent;
83 	} else
84 		bp->b_un.b_addr = addr;
85 	if (bp->b_vp)
86 		panic("swap: active vp");
87 	VREF(vp);
88 	bp->b_vp = vp;
89 	bp->b_dev = vp->v_rdev;
90 	while (nbytes > 0) {
91 		bp->b_blkno = dblkno;
92 		bp->b_bcount = nbytes;
93 		minphys(bp);
94 		c = bp->b_bcount;
95 #ifdef TRACE
96 		trace(TR_SWAPIO, vp, bp->b_blkno);
97 #endif
98 		VOP_STRATEGY(bp);
99 		/* pageout daemon doesn't wait for pushed pages */
100 		if (flag & B_DIRTY) {
101 			if (c < nbytes)
102 				panic("big push");
103 			return (0);
104 		} else {
105 			s = splbio();
106 			while ((bp->b_flags & B_DONE) == 0)
107 				sleep((caddr_t)bp, PSWP);
108 			splx(s);
109 		}
110 		bp->b_un.b_addr += c;
111 		bp->b_flags &= ~B_DONE;
112 		if (bp->b_flags & B_ERROR) {
113 			if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
114 				panic("hard IO err in swap");
115 			swkill(p, "swap: read error from swap device");
116 			error = EIO;
117 		}
118 		nbytes -= c;
119 		dblkno += btodb(c);
120 	}
121 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
122 	brelvp(bp);
123 	freeswbuf(bp);
124 	return (error);
125 }
126 
127 /*
128  * Put a buffer on the clean list after I/O is done.
129  * Called from biodone.
130  */
131 swdone(bp)
132 	register struct buf *bp;
133 {
134 	register int s;
135 
136 	if (bp->b_flags & B_ERROR)
137 		panic("IO err in push");
138 	s = splbio();
139 	bp->av_forw = bclnlist;
140 	cnt.v_pgout++;
141 	cnt.v_pgpgout += bp->b_bcount / NBPG;
142 	bclnlist = bp;
143 	if (bswlist.b_flags & B_WANTED)
144 		wakeup((caddr_t)&proc[2]);
145 	splx(s);
146 }
147 
148 /*
149  * If rout == 0 then killed on swap error, else
150  * rout is the name of the routine where we ran out of
151  * swap space.
152  */
153 swkill(p, rout)
154 	struct proc *p;
155 	char *rout;
156 {
157 
158 	printf("pid %d: %s\n", p->p_pid, rout);
159 	uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout);
160 	/*
161 	 * To be sure no looping (e.g. in vmsched trying to
162 	 * swap out) mark process locked in core (as though
163 	 * done by user) after killing it so noone will try
164 	 * to swap it out.
165 	 */
166 	psignal(p, SIGKILL);
167 	p->p_flag |= SULOCK;
168 }
169 
170 /*
171  * Raw I/O. The arguments are
172  *	The strategy routine for the device
173  *	A buffer, which will either be a special buffer header owned
174  *	    exclusively by the device for this purpose, or NULL,
175  *	    indicating that we should use a swap buffer
176  *	The device number
177  *	Read/write flag
178  * Essentially all the work is computing physical addresses and
179  * validating them.
180  * If the user has the proper access privilidges, the process is
181  * marked 'delayed unlock' and the pages involved in the I/O are
182  * faulted and locked. After the completion of the I/O, the above pages
183  * are unlocked.
184  */
185 physio(strat, bp, dev, rw, mincnt, uio)
186 	int (*strat)();
187 	register struct buf *bp;
188 	dev_t dev;
189 	int rw;
190 	u_int (*mincnt)();
191 	struct uio *uio;
192 {
193 	register struct iovec *iov;
194 	register int requested, done;
195 	char *a;
196 	int s, allocbuf = 0, error = 0;
197 	struct buf *getswbuf();
198 
199 	if (bp == NULL) {
200 		allocbuf = 1;
201 		bp = getswbuf(PRIBIO+1);
202 	}
203 	for (; uio->uio_iovcnt; uio->uio_iov++, uio->uio_iovcnt--) {
204 		iov = uio->uio_iov;
205 		if (!useracc(iov->iov_base, (u_int)iov->iov_len,
206 		    rw == B_READ ? B_WRITE : B_READ)) {
207 			error = EFAULT;
208 			break;
209 		}
210 		if (!allocbuf) {	/* only if sharing caller's buffer */
211 			s = splbio();
212 			while (bp->b_flags&B_BUSY) {
213 				bp->b_flags |= B_WANTED;
214 				sleep((caddr_t)bp, PRIBIO+1);
215 			}
216 			splx(s);
217 		}
218 		bp->b_error = 0;
219 		bp->b_proc = u.u_procp;
220 		bp->b_un.b_addr = iov->iov_base;
221 		while (iov->iov_len > 0) {
222 			bp->b_flags = B_BUSY | B_PHYS | B_RAW | rw;
223 			bp->b_dev = dev;
224 			bp->b_blkno = btodb(uio->uio_offset);
225 			bp->b_bcount = iov->iov_len;
226 			(*mincnt)(bp);
227 			requested = bp->b_bcount;
228 			u.u_procp->p_flag |= SPHYSIO;
229 			vslock(a = bp->b_un.b_addr, requested);
230 			(*strat)(bp);
231 			s = splbio();
232 			while ((bp->b_flags & B_DONE) == 0)
233 				sleep((caddr_t)bp, PRIBIO);
234 			vsunlock(a, requested, rw);
235 			u.u_procp->p_flag &= ~SPHYSIO;
236 			if (bp->b_flags&B_WANTED)	/* rare */
237 				wakeup((caddr_t)bp);
238 			splx(s);
239 			done = bp->b_bcount - bp->b_resid;
240 			bp->b_un.b_addr += done;
241 			iov->iov_len -= done;
242 			uio->uio_resid -= done;
243 			uio->uio_offset += done;
244 			/* temp kludge for disk drives */
245 			if (done < requested || bp->b_flags & B_ERROR)
246 				break;
247 		}
248 		bp->b_flags &= ~(B_BUSY | B_WANTED | B_PHYS | B_RAW);
249 		error = biowait(bp);
250 		/* temp kludge for disk drives */
251 		if (done < requested || bp->b_flags & B_ERROR)
252 			break;
253 	}
254 	if (allocbuf)
255 		freeswbuf(bp);
256 	return (error);
257 }
258 
259 u_int
260 minphys(bp)
261 	struct buf *bp;
262 {
263 	if (bp->b_bcount > MAXPHYS)
264 		bp->b_bcount = MAXPHYS;
265 }
266 
267 static
268 struct buf *
269 getswbuf(prio)
270 	int prio;
271 {
272 	int s;
273 	struct buf *bp;
274 
275 	s = splbio();
276 	while (bswlist.av_forw == NULL) {
277 		bswlist.b_flags |= B_WANTED;
278 		sleep((caddr_t)&bswlist, prio);
279 	}
280 	bp = bswlist.av_forw;
281 	bswlist.av_forw = bp->av_forw;
282 	splx(s);
283 	return (bp);
284 }
285 
286 static
287 freeswbuf(bp)
288 	struct buf *bp;
289 {
290 	int s;
291 
292 	s = splbio();
293 	bp->av_forw = bswlist.av_forw;
294 	bswlist.av_forw = bp;
295 	if (bswlist.b_flags & B_WANTED) {
296 		bswlist.b_flags &= ~B_WANTED;
297 		wakeup((caddr_t)&bswlist);
298 		wakeup((caddr_t)&proc[2]);
299 	}
300 	splx(s);
301 }
302 
303 rawread(dev, uio)
304 	dev_t dev;
305 	struct uio *uio;
306 {
307 	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
308 	    dev, B_READ, minphys, uio));
309 }
310 
311 rawwrite(dev, uio)
312 	dev_t dev;
313 	struct uio *uio;
314 {
315 	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
316 	    dev, B_WRITE, minphys, uio));
317 }
318