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