Commit | Line | Data |
---|---|---|
e1e57888 | 1 | /* kern_physio.c 4.10 %G% */ |
663dbc72 BJ |
2 | |
3 | #include "../h/param.h" | |
4 | #include "../h/systm.h" | |
5 | #include "../h/dir.h" | |
6 | #include "../h/user.h" | |
7 | #include "../h/buf.h" | |
8 | #include "../h/conf.h" | |
9 | #include "../h/proc.h" | |
10 | #include "../h/seg.h" | |
11 | #include "../h/pte.h" | |
12 | #include "../h/vm.h" | |
973ecc4f | 13 | #include "../h/trace.h" |
663dbc72 | 14 | |
5603d07d BJ |
15 | /* |
16 | * The following several routines allocate and free | |
17 | * buffers with various side effects. In general the | |
18 | * arguments to an allocate routine are a device and | |
19 | * a block number, and the value is a pointer to | |
20 | * to the buffer header; the buffer is marked "busy" | |
21 | * so that no one else can touch it. If the block was | |
22 | * already in core, no I/O need be done; if it is | |
23 | * already busy, the process waits until it becomes free. | |
24 | * The following routines allocate a buffer: | |
25 | * getblk | |
26 | * bread | |
27 | * breada | |
28 | * baddr (if it is incore) | |
29 | * Eventually the buffer must be released, possibly with the | |
30 | * side effect of writing it out, by using one of | |
31 | * bwrite | |
32 | * bdwrite | |
33 | * bawrite | |
34 | * brelse | |
35 | */ | |
36 | ||
37 | #define BUFHSZ 63 | |
46387ee3 | 38 | struct bufhd bufhash[BUFHSZ]; |
337ed2cc BJ |
39 | #define BUFHASH(dev, dblkno) \ |
40 | ((struct buf *)&bufhash[((int)(dev)+(int)(dblkno)) % BUFHSZ]) | |
5603d07d BJ |
41 | |
42 | /* | |
43 | * Initialize hash links for buffers. | |
44 | */ | |
45 | bhinit() | |
46 | { | |
47 | register int i; | |
46387ee3 | 48 | register struct bufhd *bp; |
5603d07d | 49 | |
46387ee3 BJ |
50 | for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) |
51 | bp->b_forw = bp->b_back = (struct buf *)bp; | |
5603d07d BJ |
52 | } |
53 | ||
663dbc72 BJ |
54 | /* #define DISKMON 1 */ |
55 | ||
56 | #ifdef DISKMON | |
57 | struct { | |
58 | int nbuf; | |
59 | long nread; | |
60 | long nreada; | |
61 | long ncache; | |
62 | long nwrite; | |
63 | long bufcount[NBUF]; | |
64 | } io_info; | |
65 | #endif | |
66 | ||
67 | /* | |
68 | * Swap IO headers - | |
69 | * They contain the necessary information for the swap I/O. | |
70 | * At any given time, a swap header can be in three | |
71 | * different lists. When free it is in the free list, | |
72 | * when allocated and the I/O queued, it is on the swap | |
73 | * device list, and finally, if the operation was a dirty | |
74 | * page push, when the I/O completes, it is inserted | |
75 | * in a list of cleaned pages to be processed by the pageout daemon. | |
76 | */ | |
77 | struct buf swbuf[NSWBUF]; | |
78 | short swsize[NSWBUF]; /* CAN WE JUST USE B_BCOUNT? */ | |
79 | int swpf[NSWBUF]; | |
80 | ||
663dbc72 BJ |
81 | |
82 | #ifdef FASTVAX | |
83 | #define notavail(bp) \ | |
84 | { \ | |
85 | int s = spl6(); \ | |
86 | (bp)->av_back->av_forw = (bp)->av_forw; \ | |
87 | (bp)->av_forw->av_back = (bp)->av_back; \ | |
88 | (bp)->b_flags |= B_BUSY; \ | |
89 | splx(s); \ | |
90 | } | |
91 | #endif | |
92 | ||
93 | /* | |
94 | * Read in (if necessary) the block and return a buffer pointer. | |
95 | */ | |
96 | struct buf * | |
97 | bread(dev, blkno) | |
98 | dev_t dev; | |
99 | daddr_t blkno; | |
100 | { | |
101 | register struct buf *bp; | |
102 | ||
103 | bp = getblk(dev, blkno); | |
104 | if (bp->b_flags&B_DONE) { | |
973ecc4f BJ |
105 | #ifdef EPAWNJ |
106 | trace(TR_BREAD|TR_HIT, dev, blkno); | |
107 | #endif | |
663dbc72 BJ |
108 | #ifdef DISKMON |
109 | io_info.ncache++; | |
110 | #endif | |
111 | return(bp); | |
112 | } | |
113 | bp->b_flags |= B_READ; | |
114 | bp->b_bcount = BSIZE; | |
115 | (*bdevsw[major(dev)].d_strategy)(bp); | |
973ecc4f BJ |
116 | #ifdef EPAWNJ |
117 | trace(TR_BREAD|TR_MISS, dev, blkno); | |
118 | #endif | |
663dbc72 BJ |
119 | #ifdef DISKMON |
120 | io_info.nread++; | |
121 | #endif | |
122 | u.u_vm.vm_inblk++; /* pay for read */ | |
123 | iowait(bp); | |
124 | return(bp); | |
125 | } | |
126 | ||
127 | /* | |
128 | * Read in the block, like bread, but also start I/O on the | |
129 | * read-ahead block (which is not allocated to the caller) | |
130 | */ | |
131 | struct buf * | |
132 | breada(dev, blkno, rablkno) | |
133 | dev_t dev; | |
134 | daddr_t blkno, rablkno; | |
135 | { | |
136 | register struct buf *bp, *rabp; | |
137 | ||
138 | bp = NULL; | |
139 | if (!incore(dev, blkno)) { | |
140 | bp = getblk(dev, blkno); | |
141 | if ((bp->b_flags&B_DONE) == 0) { | |
142 | bp->b_flags |= B_READ; | |
143 | bp->b_bcount = BSIZE; | |
144 | (*bdevsw[major(dev)].d_strategy)(bp); | |
973ecc4f BJ |
145 | #ifdef EPAWNJ |
146 | trace(TR_BREAD|TR_MISS, dev, blkno); | |
147 | #endif | |
663dbc72 BJ |
148 | #ifdef DISKMON |
149 | io_info.nread++; | |
150 | #endif | |
151 | u.u_vm.vm_inblk++; /* pay for read */ | |
152 | } | |
973ecc4f BJ |
153 | #ifdef EPAWNJ |
154 | else | |
155 | trace(TR_BREAD|TR_HIT, dev, blkno); | |
156 | #endif | |
663dbc72 BJ |
157 | } |
158 | if (rablkno && !incore(dev, rablkno)) { | |
159 | rabp = getblk(dev, rablkno); | |
973ecc4f | 160 | if (rabp->b_flags & B_DONE) { |
663dbc72 | 161 | brelse(rabp); |
973ecc4f BJ |
162 | #ifdef EPAWNJ |
163 | trace(TR_BREAD|TR_HIT|TR_RA, dev, blkno); | |
164 | #endif | |
165 | } else { | |
663dbc72 BJ |
166 | rabp->b_flags |= B_READ|B_ASYNC; |
167 | rabp->b_bcount = BSIZE; | |
168 | (*bdevsw[major(dev)].d_strategy)(rabp); | |
973ecc4f BJ |
169 | #ifdef EPAWNJ |
170 | trace(TR_BREAD|TR_MISS|TR_RA, dev, rablock); | |
171 | #endif | |
663dbc72 BJ |
172 | #ifdef DISKMON |
173 | io_info.nreada++; | |
174 | #endif | |
175 | u.u_vm.vm_inblk++; /* pay in advance */ | |
176 | } | |
177 | } | |
178 | if(bp == NULL) | |
179 | return(bread(dev, blkno)); | |
180 | iowait(bp); | |
181 | return(bp); | |
182 | } | |
183 | ||
184 | /* | |
185 | * Write the buffer, waiting for completion. | |
186 | * Then release the buffer. | |
187 | */ | |
188 | bwrite(bp) | |
189 | register struct buf *bp; | |
190 | { | |
191 | register flag; | |
192 | ||
193 | flag = bp->b_flags; | |
194 | bp->b_flags &= ~(B_READ | B_DONE | B_ERROR | B_DELWRI | B_AGE); | |
195 | bp->b_bcount = BSIZE; | |
196 | #ifdef DISKMON | |
197 | io_info.nwrite++; | |
198 | #endif | |
199 | if ((flag&B_DELWRI) == 0) | |
200 | u.u_vm.vm_oublk++; /* noone paid yet */ | |
973ecc4f BJ |
201 | #ifdef EPAWNJ |
202 | trace(TR_BWRITE, bp->b_dev, dbtofsb(bp->b_blkno)); | |
203 | #endif | |
663dbc72 BJ |
204 | (*bdevsw[major(bp->b_dev)].d_strategy)(bp); |
205 | if ((flag&B_ASYNC) == 0) { | |
206 | iowait(bp); | |
207 | brelse(bp); | |
208 | } else if (flag & B_DELWRI) | |
209 | bp->b_flags |= B_AGE; | |
210 | else | |
211 | geterror(bp); | |
212 | } | |
213 | ||
214 | /* | |
215 | * Release the buffer, marking it so that if it is grabbed | |
216 | * for another purpose it will be written out before being | |
217 | * given up (e.g. when writing a partial block where it is | |
218 | * assumed that another write for the same block will soon follow). | |
219 | * This can't be done for magtape, since writes must be done | |
220 | * in the same order as requested. | |
221 | */ | |
222 | bdwrite(bp) | |
223 | register struct buf *bp; | |
224 | { | |
e1e57888 | 225 | register int flags; |
663dbc72 BJ |
226 | |
227 | if ((bp->b_flags&B_DELWRI) == 0) | |
228 | u.u_vm.vm_oublk++; /* noone paid yet */ | |
e1e57888 RE |
229 | flags = bdevsw[major(bp->b_dev)].d_flags; |
230 | if(flags & B_TAPE) | |
663dbc72 BJ |
231 | bawrite(bp); |
232 | else { | |
233 | bp->b_flags |= B_DELWRI | B_DONE; | |
234 | brelse(bp); | |
235 | } | |
236 | } | |
237 | ||
238 | /* | |
239 | * Release the buffer, start I/O on it, but don't wait for completion. | |
240 | */ | |
241 | bawrite(bp) | |
242 | register struct buf *bp; | |
243 | { | |
244 | ||
245 | bp->b_flags |= B_ASYNC; | |
246 | bwrite(bp); | |
247 | } | |
248 | ||
249 | /* | |
250 | * release the buffer, with no I/O implied. | |
251 | */ | |
252 | brelse(bp) | |
253 | register struct buf *bp; | |
254 | { | |
46387ee3 | 255 | register struct buf *flist; |
663dbc72 BJ |
256 | register s; |
257 | ||
258 | if (bp->b_flags&B_WANTED) | |
259 | wakeup((caddr_t)bp); | |
46387ee3 BJ |
260 | if (bfreelist[0].b_flags&B_WANTED) { |
261 | bfreelist[0].b_flags &= ~B_WANTED; | |
262 | wakeup((caddr_t)bfreelist); | |
663dbc72 | 263 | } |
46387ee3 | 264 | if ((bp->b_flags&B_ERROR) && bp->b_dev != NODEV) |
663dbc72 BJ |
265 | bp->b_dev = NODEV; /* no assoc. on error */ |
266 | s = spl6(); | |
46387ee3 BJ |
267 | if (bp->b_flags & (B_ERROR|B_INVAL)) { |
268 | /* block has no info ... put at front of most free list */ | |
269 | flist = &bfreelist[BQUEUES-1]; | |
270 | flist->av_forw->av_back = bp; | |
271 | bp->av_forw = flist->av_forw; | |
272 | flist->av_forw = bp; | |
273 | bp->av_back = flist; | |
663dbc72 | 274 | } else { |
46387ee3 BJ |
275 | if (bp->b_flags & B_LOCKED) |
276 | flist = &bfreelist[BQ_LOCKED]; | |
277 | else if (bp->b_flags & B_AGE) | |
278 | flist = &bfreelist[BQ_AGE]; | |
279 | else | |
280 | flist = &bfreelist[BQ_LRU]; | |
281 | flist->av_back->av_forw = bp; | |
282 | bp->av_back = flist->av_back; | |
283 | flist->av_back = bp; | |
284 | bp->av_forw = flist; | |
663dbc72 BJ |
285 | } |
286 | bp->b_flags &= ~(B_WANTED|B_BUSY|B_ASYNC|B_AGE); | |
287 | splx(s); | |
288 | } | |
289 | ||
290 | /* | |
291 | * See if the block is associated with some buffer | |
292 | * (mainly to avoid getting hung up on a wait in breada) | |
293 | */ | |
294 | incore(dev, blkno) | |
295 | dev_t dev; | |
296 | daddr_t blkno; | |
297 | { | |
298 | register struct buf *bp; | |
46387ee3 | 299 | register struct buf *dp; |
663dbc72 BJ |
300 | register int dblkno = fsbtodb(blkno); |
301 | ||
337ed2cc | 302 | dp = BUFHASH(dev, dblkno); |
46387ee3 BJ |
303 | for (bp = dp->b_forw; bp != dp; bp = bp->b_forw) |
304 | if (bp->b_blkno == dblkno && bp->b_dev == dev && | |
305 | !(bp->b_flags & B_INVAL)) | |
5603d07d | 306 | return (1); |
5603d07d | 307 | return (0); |
663dbc72 BJ |
308 | } |
309 | ||
310 | struct buf * | |
311 | baddr(dev, blkno) | |
312 | dev_t dev; | |
313 | daddr_t blkno; | |
314 | { | |
315 | ||
316 | if (incore(dev, blkno)) | |
317 | return (bread(dev, blkno)); | |
318 | return (0); | |
319 | } | |
320 | ||
321 | /* | |
322 | * Assign a buffer for the given block. If the appropriate | |
323 | * block is already associated, return it; otherwise search | |
324 | * for the oldest non-busy buffer and reassign it. | |
325 | */ | |
326 | struct buf * | |
327 | getblk(dev, blkno) | |
328 | dev_t dev; | |
329 | daddr_t blkno; | |
330 | { | |
5603d07d | 331 | register struct buf *bp, *dp, *ep; |
46387ee3 BJ |
332 | register int i, x; |
333 | register int dblkno = fsbtodb(blkno); | |
663dbc72 | 334 | |
01659974 BJ |
335 | if ((unsigned)blkno >= 1 << (sizeof(int)*NBBY-PGSHIFT)) |
336 | blkno = 1 << ((sizeof(int)*NBBY-PGSHIFT) + 1); | |
337 | dblkno = fsbtodb(blkno); | |
46387ee3 | 338 | dp = BUFHASH(dev, dblkno); |
663dbc72 | 339 | loop: |
81263dba | 340 | (void) spl0(); |
46387ee3 BJ |
341 | for (bp = dp->b_forw; bp != dp; bp = bp->b_forw) { |
342 | if (bp->b_blkno != dblkno || bp->b_dev != dev || | |
343 | bp->b_flags&B_INVAL) | |
663dbc72 | 344 | continue; |
81263dba | 345 | (void) spl6(); |
663dbc72 BJ |
346 | if (bp->b_flags&B_BUSY) { |
347 | bp->b_flags |= B_WANTED; | |
348 | sleep((caddr_t)bp, PRIBIO+1); | |
349 | goto loop; | |
350 | } | |
81263dba | 351 | (void) spl0(); |
663dbc72 BJ |
352 | #ifdef DISKMON |
353 | i = 0; | |
354 | dp = bp->av_forw; | |
46387ee3 | 355 | while ((dp->b_flags & B_HEAD) == 0) { |
663dbc72 BJ |
356 | i++; |
357 | dp = dp->av_forw; | |
358 | } | |
359 | if (i<NBUF) | |
360 | io_info.bufcount[i]++; | |
361 | #endif | |
362 | notavail(bp); | |
363 | bp->b_flags |= B_CACHE; | |
364 | return(bp); | |
365 | } | |
5603d07d BJ |
366 | if (major(dev) >= nblkdev) |
367 | panic("blkdev"); | |
81263dba | 368 | (void) spl6(); |
46387ee3 BJ |
369 | for (ep = &bfreelist[BQUEUES-1]; ep > bfreelist; ep--) |
370 | if (ep->av_forw != ep) | |
371 | break; | |
372 | if (ep == bfreelist) { /* no free blocks at all */ | |
373 | ep->b_flags |= B_WANTED; | |
374 | sleep((caddr_t)ep, PRIBIO+1); | |
663dbc72 BJ |
375 | goto loop; |
376 | } | |
283cac0a | 377 | (void) spl0(); |
46387ee3 | 378 | bp = ep->av_forw; |
663dbc72 BJ |
379 | notavail(bp); |
380 | if (bp->b_flags & B_DELWRI) { | |
381 | bp->b_flags |= B_ASYNC; | |
382 | bwrite(bp); | |
383 | goto loop; | |
384 | } | |
973ecc4f BJ |
385 | #ifdef EPAWNJ |
386 | trace(TR_BRELSE, bp->b_dev, dbtofsb(bp->b_blkno)); | |
387 | #endif | |
663dbc72 BJ |
388 | bp->b_flags = B_BUSY; |
389 | bp->b_back->b_forw = bp->b_forw; | |
390 | bp->b_forw->b_back = bp->b_back; | |
391 | bp->b_forw = dp->b_forw; | |
392 | bp->b_back = dp; | |
393 | dp->b_forw->b_back = bp; | |
394 | dp->b_forw = bp; | |
395 | bp->b_dev = dev; | |
396 | bp->b_blkno = dblkno; | |
397 | return(bp); | |
398 | } | |
399 | ||
400 | /* | |
401 | * get an empty block, | |
402 | * not assigned to any particular device | |
403 | */ | |
404 | struct buf * | |
405 | geteblk() | |
406 | { | |
436518b9 | 407 | register struct buf *bp, *dp; |
663dbc72 BJ |
408 | |
409 | loop: | |
81263dba | 410 | (void) spl6(); |
46387ee3 BJ |
411 | for (dp = &bfreelist[BQUEUES-1]; dp > bfreelist; dp--) |
412 | if (dp->av_forw != dp) | |
413 | break; | |
414 | if (dp == bfreelist) { /* no free blocks */ | |
415 | dp->b_flags |= B_WANTED; | |
416 | sleep((caddr_t)dp, PRIBIO+1); | |
417 | goto loop; | |
663dbc72 | 418 | } |
81263dba | 419 | (void) spl0(); |
46387ee3 | 420 | bp = dp->av_forw; |
663dbc72 BJ |
421 | notavail(bp); |
422 | if (bp->b_flags & B_DELWRI) { | |
423 | bp->b_flags |= B_ASYNC; | |
424 | bwrite(bp); | |
425 | goto loop; | |
426 | } | |
973ecc4f | 427 | #ifdef EPAWNJ |
46387ee3 | 428 | trace(TR_BRELSE, bp->b_dev, dbtofsb(bp->b_blkno)); |
973ecc4f | 429 | #endif |
46387ee3 | 430 | bp->b_flags = B_BUSY|B_INVAL; |
663dbc72 BJ |
431 | bp->b_back->b_forw = bp->b_forw; |
432 | bp->b_forw->b_back = bp->b_back; | |
433 | bp->b_forw = dp->b_forw; | |
434 | bp->b_back = dp; | |
435 | dp->b_forw->b_back = bp; | |
436 | dp->b_forw = bp; | |
437 | bp->b_dev = (dev_t)NODEV; | |
438 | return(bp); | |
439 | } | |
440 | ||
441 | /* | |
442 | * Wait for I/O completion on the buffer; return errors | |
443 | * to the user. | |
444 | */ | |
445 | iowait(bp) | |
446 | register struct buf *bp; | |
447 | { | |
448 | ||
81263dba | 449 | (void) spl6(); |
663dbc72 BJ |
450 | while ((bp->b_flags&B_DONE)==0) |
451 | sleep((caddr_t)bp, PRIBIO); | |
81263dba | 452 | (void) spl0(); |
663dbc72 BJ |
453 | geterror(bp); |
454 | } | |
455 | ||
456 | #ifndef FASTVAX | |
457 | /* | |
458 | * Unlink a buffer from the available list and mark it busy. | |
459 | * (internal interface) | |
460 | */ | |
461 | notavail(bp) | |
462 | register struct buf *bp; | |
463 | { | |
464 | register s; | |
465 | ||
466 | s = spl6(); | |
467 | bp->av_back->av_forw = bp->av_forw; | |
468 | bp->av_forw->av_back = bp->av_back; | |
469 | bp->b_flags |= B_BUSY; | |
470 | splx(s); | |
471 | } | |
472 | #endif | |
473 | ||
474 | /* | |
475 | * Mark I/O complete on a buffer. If the header | |
476 | * indicates a dirty page push completion, the | |
477 | * header is inserted into the ``cleaned'' list | |
478 | * to be processed by the pageout daemon. Otherwise | |
479 | * release it if I/O is asynchronous, and wake | |
480 | * up anyone waiting for it. | |
481 | */ | |
482 | iodone(bp) | |
483 | register struct buf *bp; | |
484 | { | |
485 | register int s; | |
486 | ||
80e7c811 BJ |
487 | if (bp->b_flags & B_DONE) |
488 | panic("dup iodone"); | |
663dbc72 BJ |
489 | bp->b_flags |= B_DONE; |
490 | if (bp->b_flags & B_DIRTY) { | |
491 | if (bp->b_flags & B_ERROR) | |
492 | panic("IO err in push"); | |
493 | s = spl6(); | |
494 | cnt.v_pgout++; | |
495 | bp->av_forw = bclnlist; | |
496 | bp->b_bcount = swsize[bp - swbuf]; | |
497 | bp->b_pfcent = swpf[bp - swbuf]; | |
498 | bclnlist = bp; | |
499 | if (bswlist.b_flags & B_WANTED) | |
500 | wakeup((caddr_t)&proc[2]); | |
501 | splx(s); | |
a3ee1d55 | 502 | return; |
663dbc72 BJ |
503 | } |
504 | if (bp->b_flags&B_ASYNC) | |
505 | brelse(bp); | |
506 | else { | |
507 | bp->b_flags &= ~B_WANTED; | |
508 | wakeup((caddr_t)bp); | |
509 | } | |
510 | } | |
511 | ||
512 | /* | |
513 | * Zero the core associated with a buffer. | |
514 | */ | |
515 | clrbuf(bp) | |
516 | struct buf *bp; | |
517 | { | |
518 | register *p; | |
519 | register c; | |
520 | ||
521 | p = bp->b_un.b_words; | |
522 | c = BSIZE/sizeof(int); | |
523 | do | |
524 | *p++ = 0; | |
525 | while (--c); | |
526 | bp->b_resid = 0; | |
527 | } | |
528 | ||
529 | /* | |
530 | * swap I/O - | |
531 | * | |
532 | * If the flag indicates a dirty page push initiated | |
533 | * by the pageout daemon, we map the page into the i th | |
534 | * virtual page of process 2 (the daemon itself) where i is | |
535 | * the index of the swap header that has been allocated. | |
536 | * We simply initialize the header and queue the I/O but | |
537 | * do not wait for completion. When the I/O completes, | |
538 | * iodone() will link the header to a list of cleaned | |
539 | * pages to be processed by the pageout daemon. | |
540 | */ | |
541 | swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent) | |
542 | struct proc *p; | |
543 | swblk_t dblkno; | |
544 | caddr_t addr; | |
545 | int flag, nbytes; | |
546 | dev_t dev; | |
547 | unsigned pfcent; | |
548 | { | |
549 | register struct buf *bp; | |
550 | register int c; | |
551 | int p2dp; | |
552 | register struct pte *dpte, *vpte; | |
553 | ||
81263dba | 554 | (void) spl6(); |
663dbc72 BJ |
555 | while (bswlist.av_forw == NULL) { |
556 | bswlist.b_flags |= B_WANTED; | |
557 | sleep((caddr_t)&bswlist, PSWP+1); | |
558 | } | |
559 | bp = bswlist.av_forw; | |
560 | bswlist.av_forw = bp->av_forw; | |
81263dba | 561 | (void) spl0(); |
663dbc72 BJ |
562 | |
563 | bp->b_flags = B_BUSY | B_PHYS | rdflg | flag; | |
564 | if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0) | |
565 | if (rdflg == B_READ) | |
566 | sum.v_pswpin += btoc(nbytes); | |
567 | else | |
568 | sum.v_pswpout += btoc(nbytes); | |
569 | bp->b_proc = p; | |
570 | if (flag & B_DIRTY) { | |
571 | p2dp = ((bp - swbuf) * CLSIZE) * KLMAX; | |
572 | dpte = dptopte(&proc[2], p2dp); | |
573 | vpte = vtopte(p, btop(addr)); | |
574 | for (c = 0; c < nbytes; c += NBPG) { | |
575 | if (vpte->pg_pfnum == 0 || vpte->pg_fod) | |
576 | panic("swap bad pte"); | |
577 | *dpte++ = *vpte++; | |
578 | } | |
579 | bp->b_un.b_addr = (caddr_t)ctob(p2dp); | |
580 | } else | |
581 | bp->b_un.b_addr = addr; | |
582 | while (nbytes > 0) { | |
583 | c = imin(ctob(120), nbytes); | |
584 | bp->b_bcount = c; | |
585 | bp->b_blkno = dblkno; | |
586 | bp->b_dev = dev; | |
d2f87136 BJ |
587 | if (flag & B_DIRTY) { |
588 | swpf[bp - swbuf] = pfcent; | |
589 | swsize[bp - swbuf] = nbytes; | |
590 | } | |
663dbc72 BJ |
591 | (*bdevsw[major(dev)].d_strategy)(bp); |
592 | if (flag & B_DIRTY) { | |
593 | if (c < nbytes) | |
594 | panic("big push"); | |
663dbc72 BJ |
595 | return; |
596 | } | |
81263dba | 597 | (void) spl6(); |
663dbc72 BJ |
598 | while((bp->b_flags&B_DONE)==0) |
599 | sleep((caddr_t)bp, PSWP); | |
81263dba | 600 | (void) spl0(); |
663dbc72 BJ |
601 | bp->b_un.b_addr += c; |
602 | bp->b_flags &= ~B_DONE; | |
603 | if (bp->b_flags & B_ERROR) { | |
604 | if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE) | |
605 | panic("hard IO err in swap"); | |
606 | swkill(p, (char *)0); | |
607 | } | |
608 | nbytes -= c; | |
609 | dblkno += btoc(c); | |
610 | } | |
81263dba | 611 | (void) spl6(); |
663dbc72 BJ |
612 | bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); |
613 | bp->av_forw = bswlist.av_forw; | |
614 | bswlist.av_forw = bp; | |
615 | if (bswlist.b_flags & B_WANTED) { | |
616 | bswlist.b_flags &= ~B_WANTED; | |
617 | wakeup((caddr_t)&bswlist); | |
618 | wakeup((caddr_t)&proc[2]); | |
619 | } | |
81263dba | 620 | (void) spl0(); |
663dbc72 BJ |
621 | } |
622 | ||
623 | /* | |
624 | * If rout == 0 then killed on swap error, else | |
625 | * rout is the name of the routine where we ran out of | |
626 | * swap space. | |
627 | */ | |
628 | swkill(p, rout) | |
629 | struct proc *p; | |
630 | char *rout; | |
631 | { | |
632 | ||
633 | printf("%d: ", p->p_pid); | |
634 | if (rout) | |
635 | printf("out of swap space in %s\n", rout); | |
636 | else | |
637 | printf("killed on swap error\n"); | |
638 | /* | |
639 | * To be sure no looping (e.g. in vmsched trying to | |
640 | * swap out) mark process locked in core (as though | |
641 | * done by user) after killing it so noone will try | |
642 | * to swap it out. | |
643 | */ | |
a30d2e97 | 644 | psignal(p, SIGKILL); |
663dbc72 BJ |
645 | p->p_flag |= SULOCK; |
646 | } | |
647 | ||
648 | /* | |
649 | * make sure all write-behind blocks | |
650 | * on dev (or NODEV for all) | |
651 | * are flushed out. | |
652 | * (from umount and update) | |
653 | */ | |
654 | bflush(dev) | |
655 | dev_t dev; | |
656 | { | |
657 | register struct buf *bp; | |
46387ee3 | 658 | register struct buf *flist; |
663dbc72 BJ |
659 | |
660 | loop: | |
81263dba | 661 | (void) spl6(); |
46387ee3 BJ |
662 | for (flist = bfreelist; flist < &bfreelist[BQUEUES]; flist++) |
663 | for (bp = flist->av_forw; bp != flist; bp = bp->av_forw) { | |
663dbc72 BJ |
664 | if (bp->b_flags&B_DELWRI && (dev == NODEV||dev==bp->b_dev)) { |
665 | bp->b_flags |= B_ASYNC; | |
666 | notavail(bp); | |
667 | bwrite(bp); | |
668 | goto loop; | |
669 | } | |
670 | } | |
81263dba | 671 | (void) spl0(); |
663dbc72 BJ |
672 | } |
673 | ||
674 | /* | |
675 | * Raw I/O. The arguments are | |
676 | * The strategy routine for the device | |
677 | * A buffer, which will always be a special buffer | |
678 | * header owned exclusively by the device for this purpose | |
679 | * The device number | |
680 | * Read/write flag | |
681 | * Essentially all the work is computing physical addresses and | |
682 | * validating them. | |
683 | * If the user has the proper access privilidges, the process is | |
684 | * marked 'delayed unlock' and the pages involved in the I/O are | |
685 | * faulted and locked. After the completion of the I/O, the above pages | |
686 | * are unlocked. | |
687 | */ | |
688 | physio(strat, bp, dev, rw, mincnt) | |
689 | int (*strat)(); | |
690 | register struct buf *bp; | |
691 | unsigned (*mincnt)(); | |
692 | { | |
693 | register int c; | |
694 | char *a; | |
695 | ||
696 | if (useracc(u.u_base,u.u_count,rw==B_READ?B_WRITE:B_READ) == NULL) { | |
697 | u.u_error = EFAULT; | |
698 | return; | |
699 | } | |
81263dba | 700 | (void) spl6(); |
663dbc72 BJ |
701 | while (bp->b_flags&B_BUSY) { |
702 | bp->b_flags |= B_WANTED; | |
703 | sleep((caddr_t)bp, PRIBIO+1); | |
704 | } | |
705 | bp->b_error = 0; | |
706 | bp->b_proc = u.u_procp; | |
707 | bp->b_un.b_addr = u.u_base; | |
708 | while (u.u_count != 0 && bp->b_error==0) { | |
709 | bp->b_flags = B_BUSY | B_PHYS | rw; | |
710 | bp->b_dev = dev; | |
711 | bp->b_blkno = u.u_offset >> PGSHIFT; | |
712 | bp->b_bcount = u.u_count; | |
713 | (*mincnt)(bp); | |
714 | c = bp->b_bcount; | |
715 | u.u_procp->p_flag |= SPHYSIO; | |
716 | vslock(a = bp->b_un.b_addr, c); | |
717 | (*strat)(bp); | |
81263dba | 718 | (void) spl6(); |
663dbc72 BJ |
719 | while ((bp->b_flags&B_DONE) == 0) |
720 | sleep((caddr_t)bp, PRIBIO); | |
721 | vsunlock(a, c, rw); | |
722 | u.u_procp->p_flag &= ~SPHYSIO; | |
723 | if (bp->b_flags&B_WANTED) | |
724 | wakeup((caddr_t)bp); | |
81263dba | 725 | (void) spl0(); |
663dbc72 BJ |
726 | bp->b_un.b_addr += c; |
727 | u.u_count -= c; | |
728 | u.u_offset += c; | |
729 | } | |
730 | bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS); | |
731 | u.u_count = bp->b_resid; | |
732 | geterror(bp); | |
733 | } | |
734 | ||
735 | /*ARGSUSED*/ | |
736 | unsigned | |
737 | minphys(bp) | |
738 | struct buf *bp; | |
739 | { | |
740 | ||
741 | if (bp->b_bcount > 60 * 1024) | |
742 | bp->b_bcount = 60 * 1024; | |
743 | } | |
744 | ||
745 | /* | |
746 | * Pick up the device's error number and pass it to the user; | |
747 | * if there is an error but the number is 0 set a generalized | |
748 | * code. Actually the latter is always true because devices | |
749 | * don't yet return specific errors. | |
750 | */ | |
751 | geterror(bp) | |
752 | register struct buf *bp; | |
753 | { | |
754 | ||
755 | if (bp->b_flags&B_ERROR) | |
756 | if ((u.u_error = bp->b_error)==0) | |
757 | u.u_error = EIO; | |
758 | } | |
7b8b5a01 RE |
759 | |
760 | /* | |
761 | * Invalidate in core blocks belonging to closed or umounted filesystem | |
762 | * | |
763 | * This is not nicely done at all - the buffer ought to be removed from the | |
764 | * hash chains & have its dev/blkno fields clobbered, but unfortunately we | |
765 | * can't do that here, as it is quite possible that the block is still | |
766 | * being used for i/o. Eventually, all disc drivers should be forced to | |
767 | * have a close routine, which ought ensure that the queue is empty, then | |
768 | * properly flush the queues. Until that happy day, this suffices for | |
769 | * correctness. ... kre | |
770 | */ | |
771 | binval(dev) | |
772 | dev_t dev; | |
773 | { | |
634ebdbe RE |
774 | register struct buf *bp; |
775 | register struct bufhd *hp; | |
776 | #define dp ((struct buf *)hp) | |
7b8b5a01 | 777 | |
634ebdbe RE |
778 | for (hp = bufhash; hp < &bufhash[BUFHSZ]; hp++) |
779 | for (bp = dp->b_forw; bp != dp; bp = bp->b_forw) | |
780 | if (bp->b_dev == dev) | |
781 | bp->b_flags |= B_INVAL; | |
7b8b5a01 | 782 | } |