1 /* $OpenBSD: kern_physio.c,v 1.40 2014/07/13 23:49:40 uebayasi 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 KASSERTMSG(todo >= 0, "minphys broken"); 132 KASSERTMSG(todo <= MAXPHYS, "minphys broken"); 133 134 /* 135 * [lock the part of the user address space involved 136 * in the transfer] 137 * Beware vmapbuf(); it clobbers b_data and 138 * saves it in b_saveaddr. However, vunmapbuf() 139 * restores it. 140 */ 141 error = uvm_vslock_device(p, iovp->iov_base, todo, 142 (flags & B_READ) ? 143 VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ, &map); 144 if (error) 145 goto done; 146 if (map) { 147 bp->b_data = map; 148 } else { 149 bp->b_data = iovp->iov_base; 150 vmapbuf(bp, todo); 151 } 152 153 /* [call strategy to start the transfer] */ 154 (*strategy)(bp); 155 156 /* 157 * Note that the raise/wait/lower/get error 158 * steps below would be done by biowait(), but 159 * we want to unlock the address space before 160 * we lower the priority. 161 * 162 * [raise the priority level to splbio] 163 */ 164 s = splbio(); 165 166 /* [wait for the transfer to complete] */ 167 while ((bp->b_flags & B_DONE) == 0) 168 tsleep(bp, PRIBIO + 1, "physio", 0); 169 170 /* Mark it busy again, so nobody else will use it. */ 171 bp->b_flags |= B_BUSY; 172 173 /* [lower the priority level] */ 174 splx(s); 175 176 /* 177 * [unlock the part of the address space previously 178 * locked] 179 */ 180 if (!map) 181 vunmapbuf(bp, todo); 182 uvm_vsunlock_device(p, iovp->iov_base, todo, map); 183 184 /* remember error value (save a splbio/splx pair) */ 185 if (bp->b_flags & B_ERROR) 186 error = (bp->b_error ? bp->b_error : EIO); 187 188 /* 189 * [deduct the transfer size from the total number 190 * of data to transfer] 191 */ 192 done = bp->b_bcount - bp->b_resid; 193 KASSERTMSG(done >= 0, "strategy broken"); 194 KASSERTMSG(done <= todo, "strategy broken"); 195 iovp->iov_len -= done; 196 iovp->iov_base = (caddr_t)iovp->iov_base + done; 197 uio->uio_offset += done; 198 uio->uio_resid -= done; 199 200 /* 201 * Now, check for an error. 202 * Also, handle weird end-of-disk semantics. 203 */ 204 if (error || done < todo) 205 goto done; 206 } 207 } 208 209 done: 210 /* 211 * [clean up the state of the buffer] 212 */ 213 s = splbio(); 214 /* XXXCDC: is this necessary? */ 215 if (bp->b_vp) 216 brelvp(bp); 217 splx(s); 218 pool_put(&bufpool, bp); 219 220 return (error); 221 } 222 223 /* 224 * Leffler, et al., says on p. 231: 225 * "The minphys() routine is called by physio() to adjust the 226 * size of each I/O transfer before the latter is passed to 227 * the strategy routine..." 228 * 229 * so, just adjust the buffer's count accounting to MAXPHYS here, 230 * and return the new count; 231 */ 232 void 233 minphys(struct buf *bp) 234 { 235 236 if (bp->b_bcount > MAXPHYS) 237 bp->b_bcount = MAXPHYS; 238 } 239