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