1 /* $OpenBSD: kern_physio.c,v 1.39 2011/07/18 02:49:20 matthew 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 57 /* 58 * Do "physical I/O" on behalf of a user. "Physical I/O" is I/O directly 59 * from the raw device to user buffers, and bypasses the buffer cache. 60 * 61 * Comments in brackets are from Leffler, et al.'s pseudo-code implementation. 62 */ 63 int 64 physio(void (*strategy)(struct buf *), dev_t dev, int flags, 65 void (*minphys)(struct buf *), struct uio *uio) 66 { 67 struct iovec *iovp; 68 struct proc *p = curproc; 69 int error, done, i, s, todo; 70 struct buf *bp; 71 72 if ((uio->uio_offset % DEV_BSIZE) != 0) 73 return (EINVAL); 74 75 error = 0; 76 flags &= B_READ | B_WRITE; 77 78 /* Create a buffer. */ 79 s = splbio(); 80 bp = pool_get(&bufpool, PR_WAITOK | PR_ZERO); 81 82 /* [set up the fixed part of the buffer for a transfer] */ 83 bp->b_vnbufs.le_next = NOLIST; 84 bp->b_dev = dev; 85 bp->b_error = 0; 86 bp->b_proc = p; 87 bp->b_flags = B_BUSY; 88 LIST_INIT(&bp->b_dep); 89 splx(s); 90 91 /* 92 * [while there are data to transfer and no I/O error] 93 * Note that I/O errors are handled with a 'goto' at the bottom 94 * of the 'while' loop. 95 */ 96 for (i = 0; i < uio->uio_iovcnt; i++) { 97 iovp = &uio->uio_iov[i]; 98 while (iovp->iov_len > 0) { 99 void *map = NULL; 100 101 /* 102 * [mark the buffer busy for physical I/O] 103 * (i.e. set B_PHYS (because it's an I/O to user 104 * memory), and B_RAW, because B_RAW is to be 105 * "Set by physio for raw transfers.", in addition 106 * to the "busy" and read/write flag.) 107 */ 108 CLR(bp->b_flags, B_DONE | B_ERROR); 109 bp->b_flags |= (B_BUSY | B_PHYS | B_RAW | flags); 110 111 /* [set up the buffer for a maximum-sized transfer] */ 112 bp->b_blkno = btodb(uio->uio_offset); 113 114 /* 115 * Because iov_len is unsigned but b_bcount is signed, 116 * an overflow is possible. Therefore bound to MAXPHYS 117 * before calling minphys. 118 */ 119 if (iovp->iov_len > MAXPHYS) 120 bp->b_bcount = MAXPHYS; 121 else 122 bp->b_bcount = iovp->iov_len; 123 124 /* 125 * [call minphys to bound the transfer size] 126 * and remember the amount of data to transfer, 127 * for later comparison. 128 */ 129 (*minphys)(bp); 130 todo = bp->b_bcount; 131 #ifdef DIAGNOSTIC 132 if (todo < 0) 133 panic("todo < 0; minphys broken"); 134 if (todo > MAXPHYS) 135 panic("todo > MAXPHYS; minphys broken"); 136 #endif 137 138 /* 139 * [lock the part of the user address space involved 140 * in the transfer] 141 * Beware vmapbuf(); it clobbers b_data and 142 * saves it in b_saveaddr. However, vunmapbuf() 143 * restores it. 144 */ 145 error = uvm_vslock_device(p, iovp->iov_base, todo, 146 (flags & B_READ) ? 147 VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ, &map); 148 if (error) 149 goto done; 150 if (map) { 151 bp->b_data = map; 152 } else { 153 bp->b_data = iovp->iov_base; 154 vmapbuf(bp, todo); 155 } 156 157 /* [call strategy to start the transfer] */ 158 (*strategy)(bp); 159 160 /* 161 * Note that the raise/wait/lower/get error 162 * steps below would be done by biowait(), but 163 * we want to unlock the address space before 164 * we lower the priority. 165 * 166 * [raise the priority level to splbio] 167 */ 168 s = splbio(); 169 170 /* [wait for the transfer to complete] */ 171 while ((bp->b_flags & B_DONE) == 0) 172 tsleep(bp, PRIBIO + 1, "physio", 0); 173 174 /* Mark it busy again, so nobody else will use it. */ 175 bp->b_flags |= B_BUSY; 176 177 /* [lower the priority level] */ 178 splx(s); 179 180 /* 181 * [unlock the part of the address space previously 182 * locked] 183 */ 184 if (!map) 185 vunmapbuf(bp, todo); 186 uvm_vsunlock_device(p, iovp->iov_base, todo, map); 187 188 /* remember error value (save a splbio/splx pair) */ 189 if (bp->b_flags & B_ERROR) 190 error = (bp->b_error ? bp->b_error : EIO); 191 192 /* 193 * [deduct the transfer size from the total number 194 * of data to transfer] 195 */ 196 done = bp->b_bcount - bp->b_resid; 197 #ifdef DIAGNOSTIC 198 if (done < 0) 199 panic("done < 0; strategy broken"); 200 if (done > todo) 201 panic("done > todo; strategy broken"); 202 #endif 203 iovp->iov_len -= done; 204 iovp->iov_base = (caddr_t)iovp->iov_base + done; 205 uio->uio_offset += done; 206 uio->uio_resid -= done; 207 208 /* 209 * Now, check for an error. 210 * Also, handle weird end-of-disk semantics. 211 */ 212 if (error || done < todo) 213 goto done; 214 } 215 } 216 217 done: 218 /* 219 * [clean up the state of the buffer] 220 */ 221 s = splbio(); 222 /* XXXCDC: is this necessary? */ 223 if (bp->b_vp) 224 brelvp(bp); 225 splx(s); 226 pool_put(&bufpool, bp); 227 228 return (error); 229 } 230 231 /* 232 * Leffler, et al., says on p. 231: 233 * "The minphys() routine is called by physio() to adjust the 234 * size of each I/O transfer before the latter is passed to 235 * the strategy routine..." 236 * 237 * so, just adjust the buffer's count accounting to MAXPHYS here, 238 * and return the new count; 239 */ 240 void 241 minphys(struct buf *bp) 242 { 243 244 if (bp->b_bcount > MAXPHYS) 245 bp->b_bcount = MAXPHYS; 246 } 247