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