1 /* $OpenBSD: kern_physio.c,v 1.47 2020/02/20 16:26:01 krw 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/pool.h> 45 46 #include <uvm/uvm_extern.h> 47 48 /* 49 * The routines implemented in this file are described in: 50 * Leffler, et al.: The Design and Implementation of the 4.3BSD 51 * UNIX Operating System (Addison Welley, 1989) 52 * on pages 231-233. 53 */ 54 55 /* 56 * Do "physical I/O" on behalf of a user. "Physical I/O" is I/O directly 57 * from the raw device to user buffers, and bypasses the buffer cache. 58 * 59 * Comments in brackets are from Leffler, et al.'s pseudo-code implementation. 60 */ 61 int 62 physio(void (*strategy)(struct buf *), dev_t dev, int flags, 63 void (*minphys)(struct buf *), struct uio *uio) 64 { 65 struct iovec *iovp; 66 struct proc *p = curproc; 67 long done, todo; 68 int error, i, s; 69 struct buf *bp; 70 71 if ((uio->uio_offset % DEV_BSIZE) != 0) 72 return (EINVAL); 73 74 error = 0; 75 flags &= B_READ | B_WRITE; 76 77 /* Create a buffer. */ 78 s = splbio(); 79 bp = pool_get(&bufpool, PR_WAITOK | PR_ZERO); 80 81 /* [set up the fixed part of the buffer for a transfer] */ 82 bp->b_vnbufs.le_next = NOLIST; 83 bp->b_dev = dev; 84 bp->b_error = 0; 85 bp->b_proc = p; 86 bp->b_flags = B_BUSY; 87 LIST_INIT(&bp->b_dep); 88 splx(s); 89 90 /* 91 * [while there are data to transfer and no I/O error] 92 * Note that I/O errors are handled with a 'goto' at the bottom 93 * of the 'while' loop. 94 */ 95 for (i = 0; i < uio->uio_iovcnt; i++) { 96 iovp = &uio->uio_iov[i]; 97 while (iovp->iov_len > 0) { 98 void *map = NULL; 99 100 /* 101 * [mark the buffer busy for physical I/O] 102 * (i.e. set B_PHYS (because it's an I/O to user 103 * memory), and B_RAW, because B_RAW is to be 104 * "Set by physio for raw transfers.", in addition 105 * to the "busy" and read/write flag.) 106 */ 107 CLR(bp->b_flags, B_DONE | B_ERROR); 108 bp->b_flags |= (B_BUSY | B_PHYS | B_RAW | flags); 109 110 /* [set up the buffer for a maximum-sized transfer] */ 111 bp->b_blkno = btodb(uio->uio_offset); 112 113 /* 114 * Because iov_len is size_t (unsigned) but b_bcount is 115 * long (signed), an overflow is possible. Therefore 116 * limit b_bcount to LONG_MAX before calling the provided 117 * minphys. 118 */ 119 if (iovp->iov_len > LONG_MAX) 120 bp->b_bcount = LONG_MAX; 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 133 /* 134 * [lock the part of the user address space involved 135 * in the transfer] 136 * Beware vmapbuf(); it clobbers b_data and 137 * saves it in b_saveaddr. However, vunmapbuf() 138 * restores it. 139 */ 140 error = uvm_vslock_device(p, iovp->iov_base, todo, 141 (flags & B_READ) ? 142 PROT_READ | PROT_WRITE : PROT_READ, &map); 143 if (error) 144 goto done; 145 if (map) { 146 bp->b_data = map; 147 } else { 148 bp->b_data = iovp->iov_base; 149 vmapbuf(bp, todo); 150 } 151 152 /* [call strategy to start the transfer] */ 153 (*strategy)(bp); 154 155 /* 156 * Note that the raise/wait/lower/get error 157 * steps below would be done by biowait(), but 158 * we want to unlock the address space before 159 * we lower the priority. 160 * 161 * [raise the priority level to splbio] 162 */ 163 s = splbio(); 164 165 /* [wait for the transfer to complete] */ 166 while ((bp->b_flags & B_DONE) == 0) 167 tsleep_nsec(bp, PRIBIO + 1, "physio", INFSLP); 168 169 /* Mark it busy again, so nobody else will use it. */ 170 bp->b_flags |= B_BUSY; 171 172 /* [lower the priority level] */ 173 splx(s); 174 175 /* 176 * [unlock the part of the address space previously 177 * locked] 178 */ 179 if (!map) 180 vunmapbuf(bp, todo); 181 uvm_vsunlock_device(p, iovp->iov_base, todo, map); 182 183 /* remember error value (save a splbio/splx pair) */ 184 if (bp->b_flags & B_ERROR) 185 error = (bp->b_error ? bp->b_error : EIO); 186 187 /* 188 * [deduct the transfer size from the total number 189 * of data to transfer] 190 */ 191 KASSERTMSG(bp->b_resid <= LONG_MAX, "strategy broken"); 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