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