xref: /csrg-svn/sys/kern/kern_physio.c (revision 39880)
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.12 (Berkeley) 01/04/90
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 	while (nbytes > 0) {
86 		bp->b_blkno = dblkno;
87 		if (bp->b_vp)
88 			brelvp(bp);
89 		VHOLD(vp);
90 		bp->b_vp = vp;
91 		bp->b_dev = vp->v_rdev;
92 		bp->b_bcount = nbytes;
93 		if ((bp->b_flags & B_READ) == 0)
94 			vp->v_numoutput++;
95 		minphys(bp);
96 		c = bp->b_bcount;
97 #ifdef TRACE
98 		trace(TR_SWAPIO, vp, bp->b_blkno);
99 #endif
100 		VOP_STRATEGY(bp);
101 		/* pageout daemon doesn't wait for pushed pages */
102 		if (flag & B_DIRTY) {
103 			if (c < nbytes)
104 				panic("big push");
105 			return (0);
106 		} else {
107 			s = splbio();
108 			while ((bp->b_flags & B_DONE) == 0)
109 				sleep((caddr_t)bp, PSWP);
110 			splx(s);
111 		}
112 		bp->b_un.b_addr += c;
113 		bp->b_flags &= ~B_DONE;
114 		if (bp->b_flags & B_ERROR) {
115 			if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
116 				panic("hard IO err in swap");
117 			swkill(p, "swap: read error from swap device");
118 			error = EIO;
119 		}
120 		nbytes -= c;
121 		dblkno += btodb(c);
122 	}
123 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
124 	freeswbuf(bp);
125 	return (error);
126 }
127 
128 /*
129  * Put a buffer on the clean list after I/O is done.
130  * Called from biodone.
131  */
132 swdone(bp)
133 	register struct buf *bp;
134 {
135 	register int s;
136 
137 	if (bp->b_flags & B_ERROR)
138 		panic("IO err in push");
139 	s = splbio();
140 	bp->av_forw = bclnlist;
141 	cnt.v_pgout++;
142 	cnt.v_pgpgout += bp->b_bcount / NBPG;
143 	bclnlist = bp;
144 	if (bswlist.b_flags & B_WANTED)
145 		wakeup((caddr_t)&proc[2]);
146 	splx(s);
147 }
148 
149 /*
150  * If rout == 0 then killed on swap error, else
151  * rout is the name of the routine where we ran out of
152  * swap space.
153  */
154 swkill(p, rout)
155 	struct proc *p;
156 	char *rout;
157 {
158 
159 	printf("pid %d: %s\n", p->p_pid, rout);
160 	uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout);
161 	/*
162 	 * To be sure no looping (e.g. in vmsched trying to
163 	 * swap out) mark process locked in core (as though
164 	 * done by user) after killing it so noone will try
165 	 * to swap it out.
166 	 */
167 	psignal(p, SIGKILL);
168 	p->p_flag |= SULOCK;
169 }
170 
171 /*
172  * Raw I/O. The arguments are
173  *	The strategy routine for the device
174  *	A buffer, which will either be a special buffer header owned
175  *	    exclusively by the device for this purpose, or NULL,
176  *	    indicating that we should use a swap buffer
177  *	The device number
178  *	Read/write flag
179  * Essentially all the work is computing physical addresses and
180  * validating them.
181  * If the user has the proper access privilidges, the process is
182  * marked 'delayed unlock' and the pages involved in the I/O are
183  * faulted and locked. After the completion of the I/O, the above pages
184  * are unlocked.
185  */
186 physio(strat, bp, dev, rw, mincnt, uio)
187 	int (*strat)();
188 	register struct buf *bp;
189 	dev_t dev;
190 	int rw;
191 	u_int (*mincnt)();
192 	struct uio *uio;
193 {
194 	register struct iovec *iov;
195 	register int requested, done;
196 	char *a;
197 	int s, allocbuf = 0, error = 0;
198 	struct buf *getswbuf();
199 
200 	if (bp == NULL) {
201 		allocbuf = 1;
202 		bp = getswbuf(PRIBIO+1);
203 	}
204 	for (; uio->uio_iovcnt; uio->uio_iov++, uio->uio_iovcnt--) {
205 		iov = uio->uio_iov;
206 		if (!useracc(iov->iov_base, (u_int)iov->iov_len,
207 		    rw == B_READ ? B_WRITE : B_READ)) {
208 			error = EFAULT;
209 			break;
210 		}
211 		if (!allocbuf) {	/* only if sharing caller's buffer */
212 			s = splbio();
213 			while (bp->b_flags&B_BUSY) {
214 				bp->b_flags |= B_WANTED;
215 				sleep((caddr_t)bp, PRIBIO+1);
216 			}
217 			splx(s);
218 		}
219 		bp->b_error = 0;
220 		bp->b_proc = u.u_procp;
221 		bp->b_un.b_addr = iov->iov_base;
222 		while (iov->iov_len > 0) {
223 			bp->b_flags = B_BUSY | B_PHYS | B_RAW | rw;
224 			bp->b_dev = dev;
225 			bp->b_blkno = btodb(uio->uio_offset);
226 			bp->b_bcount = iov->iov_len;
227 			(*mincnt)(bp);
228 			requested = bp->b_bcount;
229 			u.u_procp->p_flag |= SPHYSIO;
230 			vslock(a = bp->b_un.b_addr, requested);
231 			(*strat)(bp);
232 			s = splbio();
233 			while ((bp->b_flags & B_DONE) == 0)
234 				sleep((caddr_t)bp, PRIBIO);
235 			vsunlock(a, requested, rw);
236 			u.u_procp->p_flag &= ~SPHYSIO;
237 			if (bp->b_flags&B_WANTED)	/* rare */
238 				wakeup((caddr_t)bp);
239 			splx(s);
240 			done = bp->b_bcount - bp->b_resid;
241 			bp->b_un.b_addr += done;
242 			iov->iov_len -= done;
243 			uio->uio_resid -= done;
244 			uio->uio_offset += done;
245 			/* temp kludge for disk drives */
246 			if (done < requested || bp->b_flags & B_ERROR)
247 				break;
248 		}
249 		bp->b_flags &= ~(B_BUSY | B_WANTED | B_PHYS | B_RAW);
250 		error = biowait(bp);
251 		/* temp kludge for disk drives */
252 		if (done < requested || bp->b_flags & B_ERROR)
253 			break;
254 	}
255 	if (allocbuf)
256 		freeswbuf(bp);
257 	return (error);
258 }
259 
260 u_int
261 minphys(bp)
262 	struct buf *bp;
263 {
264 	if (bp->b_bcount > MAXPHYS)
265 		bp->b_bcount = MAXPHYS;
266 }
267 
268 static
269 struct buf *
270 getswbuf(prio)
271 	int prio;
272 {
273 	int s;
274 	struct buf *bp;
275 
276 	s = splbio();
277 	while (bswlist.av_forw == NULL) {
278 		bswlist.b_flags |= B_WANTED;
279 		sleep((caddr_t)&bswlist, prio);
280 	}
281 	bp = bswlist.av_forw;
282 	bswlist.av_forw = bp->av_forw;
283 	splx(s);
284 	return (bp);
285 }
286 
287 static
288 freeswbuf(bp)
289 	struct buf *bp;
290 {
291 	int s;
292 
293 	s = splbio();
294 	bp->av_forw = bswlist.av_forw;
295 	bswlist.av_forw = bp;
296 	if (bp->b_vp)
297 		brelvp(bp);
298 	if (bswlist.b_flags & B_WANTED) {
299 		bswlist.b_flags &= ~B_WANTED;
300 		wakeup((caddr_t)&bswlist);
301 		wakeup((caddr_t)&proc[2]);
302 	}
303 	splx(s);
304 }
305 
306 rawread(dev, uio)
307 	dev_t dev;
308 	struct uio *uio;
309 {
310 	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
311 	    dev, B_READ, minphys, uio));
312 }
313 
314 rawwrite(dev, uio)
315 	dev_t dev;
316 	struct uio *uio;
317 {
318 	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
319 	    dev, B_WRITE, minphys, uio));
320 }
321