1 /* $OpenBSD: kern_physio.c,v 1.21 2003/07/21 22:44:50 tedu Exp $ */ 2 /* $NetBSD: kern_physio.c,v 1.28 1997/05/19 10:43:28 pk Exp $ */ 3 4 /*- 5 * Copyright (c) 1994 Christopher G. Demetriou 6 * Copyright (c) 1982, 1986, 1990, 1993 7 * The Regents of the University of California. All rights reserved. 8 * (c) UNIX System Laboratories, Inc. 9 * All or some portions of this file are derived from material licensed 10 * to the University of California by American Telephone and Telegraph 11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 12 * the permission of UNIX System Laboratories, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_physio.c 8.1 (Berkeley) 6/10/93 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/buf.h> 44 #include <sys/conf.h> 45 #include <sys/proc.h> 46 #include <sys/pool.h> 47 48 #include <uvm/uvm_extern.h> 49 50 /* 51 * The routines implemented in this file are described in: 52 * Leffler, et al.: The Design and Implementation of the 4.3BSD 53 * UNIX Operating System (Addison Welley, 1989) 54 * on pages 231-233. 55 * 56 * The routines "getphysbuf" and "putphysbuf" steal and return a swap 57 * buffer. Leffler, et al., says that swap buffers are used to do the 58 * I/O, so raw I/O requests don't have to be single-threaded. 59 */ 60 61 struct buf *getphysbuf(void); 62 void putphysbuf(struct buf *bp); 63 64 /* 65 * Do "physical I/O" on behalf of a user. "Physical I/O" is I/O directly 66 * from the raw device to user buffers, and bypasses the buffer cache. 67 * 68 * Comments in brackets are from Leffler, et al.'s pseudo-code implementation. 69 */ 70 int 71 physio(strategy, bp, dev, flags, minphys, uio) 72 void (*strategy)(struct buf *); 73 struct buf *bp; 74 dev_t dev; 75 int flags; 76 void (*minphys)(struct buf *); 77 struct uio *uio; 78 { 79 struct iovec *iovp; 80 struct proc *p = curproc; 81 int error, done, i, nobuf, s, todo; 82 83 error = 0; 84 flags &= B_READ | B_WRITE; 85 86 /* Make sure we have a buffer, creating one if necessary. */ 87 if ((nobuf = (bp == NULL)) != 0) 88 bp = getphysbuf(); 89 90 /* [raise the processor priority level to splbio;] */ 91 s = splbio(); 92 93 /* [while the buffer is marked busy] */ 94 while (bp->b_flags & B_BUSY) { 95 /* [mark the buffer wanted] */ 96 bp->b_flags |= B_WANTED; 97 /* [wait until the buffer is available] */ 98 tsleep(bp, PRIBIO+1, "physbuf", 0); 99 } 100 101 /* Mark it busy, so nobody else will use it. */ 102 bp->b_flags |= B_BUSY; 103 104 /* [lower the priority level] */ 105 splx(s); 106 107 /* [set up the fixed part of the buffer for a transfer] */ 108 bp->b_dev = dev; 109 bp->b_error = 0; 110 bp->b_proc = p; 111 LIST_INIT(&bp->b_dep); 112 113 /* 114 * [while there are data to transfer and no I/O error] 115 * Note that I/O errors are handled with a 'goto' at the bottom 116 * of the 'while' loop. 117 */ 118 for (i = 0; i < uio->uio_iovcnt; i++) { 119 iovp = &uio->uio_iov[i]; 120 while (iovp->iov_len > 0) { 121 /* 122 * [mark the buffer busy for physical I/O] 123 * (i.e. set B_PHYS (because it's an I/O to user 124 * memory, and B_RAW, because B_RAW is to be 125 * "Set by physio for raw transfers.", in addition 126 * to the "busy" and read/write flag.) 127 */ 128 bp->b_flags = B_BUSY | B_PHYS | B_RAW | flags; 129 130 /* [set up the buffer for a maximum-sized transfer] */ 131 bp->b_blkno = btodb(uio->uio_offset); 132 bp->b_bcount = iovp->iov_len; 133 bp->b_data = iovp->iov_base; 134 135 /* 136 * [call minphys to bound the tranfer size] 137 * and remember the amount of data to transfer, 138 * for later comparison. 139 */ 140 (*minphys)(bp); 141 todo = bp->b_bcount; 142 #ifdef DIAGNOSTIC 143 if (todo < 0) 144 panic("todo < 0; minphys broken"); 145 if (todo > MAXPHYS) 146 panic("todo > MAXPHYS; minphys broken"); 147 #endif 148 149 /* 150 * [lock the part of the user address space involved 151 * in the transfer] 152 * Beware vmapbuf(); it clobbers b_data and 153 * saves it in b_saveaddr. However, vunmapbuf() 154 * restores it. 155 */ 156 PHOLD(p); 157 error = uvm_vslock(p, bp->b_data, todo, 158 (flags & B_READ) ? 159 VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ); 160 if (error) { 161 bp->b_flags |= B_ERROR; 162 bp->b_error = error; 163 goto after_unlock; 164 } 165 vmapbuf(bp, todo); 166 167 /* [call strategy to start the transfer] */ 168 (*strategy)(bp); 169 170 /* 171 * Note that the raise/wait/lower/get error 172 * steps below would be done by biowait(), but 173 * we want to unlock the address space before 174 * we lower the priority. 175 * 176 * [raise the priority level to splbio] 177 */ 178 s = splbio(); 179 180 /* [wait for the transfer to complete] */ 181 while ((bp->b_flags & B_DONE) == 0) 182 tsleep(bp, PRIBIO + 1, "physio", 0); 183 184 /* Mark it busy again, so nobody else will use it. */ 185 bp->b_flags |= B_BUSY; 186 187 /* [lower the priority level] */ 188 splx(s); 189 190 /* 191 * [unlock the part of the address space previously 192 * locked] 193 */ 194 vunmapbuf(bp, todo); 195 uvm_vsunlock(p, bp->b_data, todo); 196 after_unlock: 197 PRELE(p); 198 199 /* remember error value (save a splbio/splx pair) */ 200 if (bp->b_flags & B_ERROR) 201 error = (bp->b_error ? bp->b_error : EIO); 202 203 /* 204 * [deduct the transfer size from the total number 205 * of data to transfer] 206 */ 207 done = bp->b_bcount - bp->b_resid; 208 #ifdef DIAGNOSTIC 209 if (done < 0) 210 panic("done < 0; strategy broken"); 211 if (done > todo) 212 panic("done > todo; strategy broken"); 213 #endif 214 iovp->iov_len -= done; 215 iovp->iov_base = (caddr_t)iovp->iov_base + done; 216 uio->uio_offset += done; 217 uio->uio_resid -= done; 218 219 /* 220 * Now, check for an error. 221 * Also, handle weird end-of-disk semantics. 222 */ 223 if (error || done < todo) 224 goto done; 225 } 226 } 227 228 done: 229 /* 230 * [clean up the state of the buffer] 231 * Remember if somebody wants it, so we can wake them up below. 232 * Also, if we had to steal it, give it back. 233 */ 234 s = splbio(); 235 bp->b_flags &= ~(B_BUSY | B_PHYS | B_RAW); 236 if (nobuf) 237 putphysbuf(bp); 238 else { 239 /* 240 * [if another process is waiting for the raw I/O buffer, 241 * wake up processes waiting to do physical I/O; 242 */ 243 if (bp->b_flags & B_WANTED) { 244 bp->b_flags &= ~B_WANTED; 245 wakeup(bp); 246 } 247 } 248 splx(s); 249 250 return (error); 251 } 252 253 /* 254 * Get a swap buffer structure, for use in physical I/O. 255 * Mostly taken from /sys/vm/swap_pager.c, except that it no longer 256 * records buffer list-empty conditions, and sleeps at PRIBIO + 1, 257 * rather than PSWP + 1 (and on a different wchan). 258 */ 259 struct buf * 260 getphysbuf() 261 { 262 struct buf *bp; 263 264 bp = pool_get(&bufpool, PR_WAITOK); 265 bzero(bp, sizeof(*bp)); 266 267 /* XXXCDC: are the following two lines necessary? */ 268 bp->b_vnbufs.le_next = NOLIST; 269 270 return (bp); 271 } 272 273 /* 274 * Get rid of a swap buffer structure which has been used in physical I/O. 275 * Mostly taken from /sys/vm/swap_pager.c, except that it now uses 276 * wakeup() rather than the VM-internal thread_wakeup(), and that the caller 277 * must mask disk interrupts, rather than putphysbuf() itself. 278 */ 279 void 280 putphysbuf(bp) 281 struct buf *bp; 282 { 283 /* XXXCDC: is this necesary? */ 284 if (bp->b_vp) 285 brelvp(bp); 286 287 #ifdef DIAGNOSTIC 288 if (bp->b_flags & B_WANTED) 289 panic("putphysbuf: private buf B_WANTED"); 290 #endif 291 pool_put(&bufpool, bp); 292 } 293 294 /* 295 * Leffler, et al., says on p. 231: 296 * "The minphys() routine is called by physio() to adjust the 297 * size of each I/O transfer before the latter is passed to 298 * the strategy routine..." 299 * 300 * so, just adjust the buffer's count accounting to MAXPHYS here, 301 * and return the new count; 302 */ 303 void 304 minphys(bp) 305 struct buf *bp; 306 { 307 308 if (bp->b_bcount > MAXPHYS) 309 bp->b_bcount = MAXPHYS; 310 } 311