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