1 /* $OpenBSD: kern_physio.c,v 1.13 2001/06/27 04:49:44 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/malloc.h> 51 52 #include <vm/vm.h> 53 54 #include <uvm/uvm_extern.h> 55 56 /* 57 * The routines implemented in this file are described in: 58 * Leffler, et al.: The Design and Implementation of the 4.3BSD 59 * UNIX Operating System (Addison Welley, 1989) 60 * on pages 231-233. 61 * 62 * The routines "getphysbuf" and "putphysbuf" steal and return a swap 63 * buffer. Leffler, et al., says that swap buffers are used to do the 64 * I/O, so raw I/O requests don't have to be single-threaded. 65 */ 66 67 struct buf *getphysbuf __P((void)); 68 void putphysbuf __P((struct buf *bp)); 69 70 /* 71 * Do "physical I/O" on behalf of a user. "Physical I/O" is I/O directly 72 * from the raw device to user buffers, and bypasses the buffer cache. 73 * 74 * Comments in brackets are from Leffler, et al.'s pseudo-code implementation. 75 */ 76 int 77 physio(strategy, bp, dev, flags, minphys, uio) 78 void (*strategy) __P((struct buf *)); 79 struct buf *bp; 80 dev_t dev; 81 int flags; 82 void (*minphys) __P((struct buf *)); 83 struct uio *uio; 84 { 85 struct iovec *iovp; 86 struct proc *p = curproc; 87 int error, done, i, nobuf, s, todo; 88 89 error = 0; 90 flags &= B_READ | B_WRITE; 91 92 /* 93 * [check user read/write access to the data buffer] 94 * 95 * Check each iov one by one. Note that we know if we're reading or 96 * writing, so we ignore the uio's rw parameter. Also note that if 97 * we're doing a read, that's a *write* to user-space. 98 */ 99 if (uio->uio_segflg == UIO_USERSPACE) 100 for (i = 0; i < uio->uio_iovcnt; i++) 101 /* XXX - obsolete now that vslock can error? */ 102 if (!uvm_useracc(uio->uio_iov[i].iov_base, 103 uio->uio_iov[i].iov_len, 104 (flags == B_READ) ? B_WRITE : B_READ)) 105 return (EFAULT); 106 107 /* Make sure we have a buffer, creating one if necessary. */ 108 if ((nobuf = (bp == NULL)) != 0) 109 bp = getphysbuf(); 110 111 /* [raise the processor priority level to splbio;] */ 112 s = splbio(); 113 114 /* [while the buffer is marked busy] */ 115 while (bp->b_flags & B_BUSY) { 116 /* [mark the buffer wanted] */ 117 bp->b_flags |= B_WANTED; 118 /* [wait until the buffer is available] */ 119 tsleep((caddr_t)bp, PRIBIO+1, "physbuf", 0); 120 } 121 122 /* Mark it busy, so nobody else will use it. */ 123 bp->b_flags |= B_BUSY; 124 125 /* [lower the priority level] */ 126 splx(s); 127 128 /* [set up the fixed part of the buffer for a transfer] */ 129 bp->b_dev = dev; 130 bp->b_error = 0; 131 bp->b_proc = p; 132 LIST_INIT(&bp->b_dep); 133 134 /* 135 * [while there are data to transfer and no I/O error] 136 * Note that I/O errors are handled with a 'goto' at the bottom 137 * of the 'while' loop. 138 */ 139 for (i = 0; i < uio->uio_iovcnt; i++) { 140 iovp = &uio->uio_iov[i]; 141 while (iovp->iov_len > 0) { 142 /* 143 * [mark the buffer busy for physical I/O] 144 * (i.e. set B_PHYS (because it's an I/O to user 145 * memory, and B_RAW, because B_RAW is to be 146 * "Set by physio for raw transfers.", in addition 147 * to the "busy" and read/write flag.) 148 */ 149 bp->b_flags = B_BUSY | B_PHYS | B_RAW | flags; 150 151 /* [set up the buffer for a maximum-sized transfer] */ 152 bp->b_blkno = btodb(uio->uio_offset); 153 bp->b_bcount = iovp->iov_len; 154 bp->b_data = iovp->iov_base; 155 156 /* 157 * [call minphys to bound the tranfer size] 158 * and remember the amount of data to transfer, 159 * for later comparison. 160 */ 161 (*minphys)(bp); 162 todo = bp->b_bcount; 163 #ifdef DIAGNOSTIC 164 if (todo < 0) 165 panic("todo < 0; minphys broken"); 166 if (todo > MAXPHYS) 167 panic("todo > MAXPHYS; minphys broken"); 168 #endif 169 170 /* 171 * [lock the part of the user address space involved 172 * in the transfer] 173 * Beware vmapbuf(); it clobbers b_data and 174 * saves it in b_saveaddr. However, vunmapbuf() 175 * restores it. 176 */ 177 PHOLD(p); 178 if (uvm_vslock(p, bp->b_data, todo, (flags & B_READ) ? 179 VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ) != 180 KERN_SUCCESS) { 181 bp->b_flags |= B_ERROR; 182 bp->b_error = EFAULT; 183 goto after_unlock; 184 } 185 vmapbuf(bp, todo); 186 187 /* [call strategy to start the transfer] */ 188 (*strategy)(bp); 189 190 /* 191 * Note that the raise/wait/lower/get error 192 * steps below would be done by biowait(), but 193 * we want to unlock the address space before 194 * we lower the priority. 195 * 196 * [raise the priority level to splbio] 197 */ 198 s = splbio(); 199 200 /* [wait for the transfer to complete] */ 201 while ((bp->b_flags & B_DONE) == 0) 202 tsleep((caddr_t) bp, PRIBIO + 1, "physio", 0); 203 204 /* Mark it busy again, so nobody else will use it. */ 205 bp->b_flags |= B_BUSY; 206 207 /* [lower the priority level] */ 208 splx(s); 209 210 /* 211 * [unlock the part of the address space previously 212 * locked] 213 */ 214 vunmapbuf(bp, todo); 215 uvm_vsunlock(p, bp->b_data, todo); 216 after_unlock: 217 PRELE(p); 218 219 /* remember error value (save a splbio/splx pair) */ 220 if (bp->b_flags & B_ERROR) 221 error = (bp->b_error ? bp->b_error : EIO); 222 223 /* 224 * [deduct the transfer size from the total number 225 * of data to transfer] 226 */ 227 done = bp->b_bcount - bp->b_resid; 228 #ifdef DIAGNOSTIC 229 if (done < 0) 230 panic("done < 0; strategy broken"); 231 if (done > todo) 232 panic("done > todo; strategy broken"); 233 #endif 234 iovp->iov_len -= done; 235 iovp->iov_base = (caddr_t)iovp->iov_base + done; 236 uio->uio_offset += done; 237 uio->uio_resid -= done; 238 239 /* 240 * Now, check for an error. 241 * Also, handle weird end-of-disk semantics. 242 */ 243 if (error || done < todo) 244 goto done; 245 } 246 } 247 248 done: 249 /* 250 * [clean up the state of the buffer] 251 * Remember if somebody wants it, so we can wake them up below. 252 * Also, if we had to steal it, give it back. 253 */ 254 s = splbio(); 255 bp->b_flags &= ~(B_BUSY | B_PHYS | B_RAW); 256 if (nobuf) 257 putphysbuf(bp); 258 else { 259 /* 260 * [if another process is waiting for the raw I/O buffer, 261 * wake up processes waiting to do physical I/O; 262 */ 263 if (bp->b_flags & B_WANTED) { 264 bp->b_flags &= ~B_WANTED; 265 wakeup(bp); 266 } 267 } 268 splx(s); 269 270 return (error); 271 } 272 273 /* 274 * Get a swap buffer structure, for use in physical I/O. 275 * Mostly taken from /sys/vm/swap_pager.c, except that it no longer 276 * records buffer list-empty conditions, and sleeps at PRIBIO + 1, 277 * rather than PSWP + 1 (and on a different wchan). 278 */ 279 struct buf * 280 getphysbuf() 281 { 282 struct buf *bp; 283 284 bp = malloc(sizeof(*bp), M_TEMP, M_WAITOK); 285 bzero(bp, sizeof(*bp)); 286 287 /* XXXCDC: are the following two lines necessary? */ 288 bp->b_rcred = bp->b_wcred = NOCRED; 289 bp->b_vnbufs.le_next = NOLIST; 290 291 return (bp); 292 } 293 294 /* 295 * Get rid of a swap buffer structure which has been used in physical I/O. 296 * Mostly taken from /sys/vm/swap_pager.c, except that it now uses 297 * wakeup() rather than the VM-internal thread_wakeup(), and that the caller 298 * must mask disk interrupts, rather than putphysbuf() itself. 299 */ 300 void 301 putphysbuf(bp) 302 struct buf *bp; 303 { 304 /* XXXCDC: is this necesary? */ 305 if (bp->b_vp) 306 brelvp(bp); 307 308 if (bp->b_flags & B_WANTED) 309 panic("putphysbuf: private buf B_WANTED"); 310 free(bp, M_TEMP); 311 } 312 313 /* 314 * Leffler, et al., says on p. 231: 315 * "The minphys() routine is called by physio() to adjust the 316 * size of each I/O transfer before the latter is passed to 317 * the strategy routine..." 318 * 319 * so, just adjust the buffer's count accounting to MAXPHYS here, 320 * and return the new count; 321 */ 322 void 323 minphys(bp) 324 struct buf *bp; 325 { 326 327 if (bp->b_bcount > MAXPHYS) 328 bp->b_bcount = MAXPHYS; 329 } 330