Home | History | Annotate | Line # | Download | only in kern
kern_physio.c revision 1.37.8.2
      1 /*	$NetBSD: kern_physio.c,v 1.37.8.2 1999/12/27 18:35:51 wrstuden Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1994 Christopher G. Demetriou
      5  * Copyright (c) 1982, 1986, 1990, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  * (c) UNIX System Laboratories, Inc.
      8  * All or some portions of this file are derived from material licensed
      9  * to the University of California by American Telephone and Telegraph
     10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     11  * the permission of UNIX System Laboratories, Inc.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by the University of
     24  *	California, Berkeley and its contributors.
     25  * 4. Neither the name of the University nor the names of its contributors
     26  *    may be used to endorse or promote products derived from this software
     27  *    without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  *	@(#)kern_physio.c	8.1 (Berkeley) 6/10/93
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/buf.h>
     47 #include <sys/conf.h>
     48 #include <sys/malloc.h>
     49 #include <sys/proc.h>
     50 
     51 #include <vm/vm.h>
     52 
     53 #include <uvm/uvm_extern.h>
     54 
     55 /*
     56  * The routines implemented in this file are described in:
     57  *	Leffler, et al.: The Design and Implementation of the 4.3BSD
     58  *	    UNIX Operating System (Addison Welley, 1989)
     59  * on pages 231-233.
     60  *
     61  * The routines "getphysbuf" and "putphysbuf" steal and return a swap
     62  * buffer.  Leffler, et al., says that swap buffers are used to do the
     63  * I/O, so raw I/O requests don't have to be single-threaded.
     64  */
     65 
     66 struct buf *getphysbuf __P((void));
     67 void putphysbuf __P((struct buf *bp));
     68 
     69 /*
     70  * Do "physical I/O" on behalf of a user.  "Physical I/O" is I/O directly
     71  * from the raw device to user buffers, and bypasses the buffer cache.
     72  *
     73  * Comments in brackets are from Leffler, et al.'s pseudo-code implementation.
     74  */
     75 int
     76 physio(strategy, bp, dev, flags, minphys, uio, bshift)
     77 	void (*strategy) __P((struct buf *));
     78 	struct buf *bp;
     79 	dev_t dev;
     80 	int flags;
     81 	void (*minphys) __P((struct buf *));
     82 	struct uio *uio;
     83 	int bshift;
     84 {
     85 	struct iovec *iovp;
     86 	struct proc *p = curproc;
     87 	int error, done, i, nobuf, s, todo;
     88 
     89 	error = 0;
     90 	flags &= B_READ | B_WRITE;
     91 
     92 	/*
     93 	 * [check user read/write access to the data buffer]
     94 	 *
     95 	 * Check each iov one by one.  Note that we know if we're reading or
     96 	 * writing, so we ignore the uio's rw parameter.  Also note that if
     97 	 * we're doing a read, that's a *write* to user-space.
     98 	 */
     99 	if (uio->uio_segflg == UIO_USERSPACE)
    100 		for (i = 0; i < uio->uio_iovcnt; i++)
    101 			/* XXXCDC: map not locked, rethink */
    102 			if (!uvm_useracc(uio->uio_iov[i].iov_base,
    103 				     uio->uio_iov[i].iov_len,
    104 				     (flags == B_READ) ? B_WRITE : B_READ))
    105 				return (EFAULT);
    106 
    107 	/* Make sure we have a buffer, creating one if necessary. */
    108 	if ((nobuf = (bp == NULL)) != 0) {
    109 
    110 		bp = getphysbuf();
    111 		/* bp was just malloc'd so can't already be busy */
    112 		bp->b_flags |= B_BUSY;
    113 
    114 	} else {
    115 
    116 		/* [raise the processor priority level to splbio;] */
    117 		s = splbio();
    118 
    119 		/* [while the buffer is marked busy] */
    120 		while (bp->b_flags & B_BUSY) {
    121 			/* [mark the buffer wanted] */
    122 			bp->b_flags |= B_WANTED;
    123 			/* [wait until the buffer is available] */
    124 			tsleep((caddr_t)bp, PRIBIO+1, "physbuf", 0);
    125 		}
    126 
    127 		/* Mark it busy, so nobody else will use it. */
    128 		bp->b_flags |= B_BUSY;
    129 
    130 		/* [lower the priority level] */
    131 		splx(s);
    132 
    133 	}
    134 
    135 	/* [set up the fixed part of the buffer for a transfer] */
    136 	bp->b_dev = dev;
    137 	bp->b_error = 0;
    138 	bp->b_proc = p;
    139 	LIST_INIT(&bp->b_dep);
    140 
    141 	/*
    142 	 * [while there are data to transfer and no I/O error]
    143 	 * Note that I/O errors are handled with a 'goto' at the bottom
    144 	 * of the 'while' loop.
    145 	 */
    146 	for (i = 0; i < uio->uio_iovcnt; i++) {
    147 		iovp = &uio->uio_iov[i];
    148 		while (iovp->iov_len > 0) {
    149 			/*
    150 			 * [mark the buffer busy for physical I/O]
    151 			 * (i.e. set B_PHYS (because it's an I/O to user
    152 			 * memory, and B_RAW, because B_RAW is to be
    153 			 * "Set by physio for raw transfers.", in addition
    154 			 * to the "busy" and read/write flag.)
    155 			 */
    156 			bp->b_flags = B_BUSY | B_PHYS | B_RAW | flags;
    157 
    158 			/* [set up the buffer for a maximum-sized transfer] */
    159 			bp->b_blkno = btodb(uio->uio_offset, bshift);
    160 			bp->b_bcount = iovp->iov_len;
    161 			bp->b_data = iovp->iov_base;
    162 
    163 			/*
    164 			 * [call minphys to bound the tranfer size]
    165 			 * and remember the amount of data to transfer,
    166 			 * for later comparison.
    167 			 */
    168 			(*minphys)(bp);
    169 			todo = bp->b_bcount;
    170 #ifdef DIAGNOSTIC
    171 			if (todo < 0)
    172 				panic("todo < 0; minphys broken");
    173 			if (todo > MAXPHYS)
    174 				panic("todo > MAXPHYS; minphys broken");
    175 #endif
    176 
    177 			/*
    178 			 * [lock the part of the user address space involved
    179 			 *    in the transfer]
    180 			 * Beware vmapbuf(); it clobbers b_data and
    181 			 * saves it in b_saveaddr.  However, vunmapbuf()
    182 			 * restores it.
    183 			 */
    184 			PHOLD(p);
    185 			if (uvm_vslock(p, bp->b_data, todo, (flags & B_READ) ?
    186 			    VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ)
    187 			    != KERN_SUCCESS) {
    188 				bp->b_flags |= B_ERROR;
    189 				bp->b_error = EFAULT;
    190 				goto after_vsunlock;
    191 			}
    192 			vmapbuf(bp, todo);
    193 
    194 			/* [call strategy to start the transfer] */
    195 			(*strategy)(bp);
    196 
    197 			/*
    198 			 * Note that the raise/wait/lower/get error
    199 			 * steps below would be done by biowait(), but
    200 			 * we want to unlock the address space before
    201 			 * we lower the priority.
    202 			 *
    203 			 * [raise the priority level to splbio]
    204 			 */
    205 			s = splbio();
    206 
    207 			/* [wait for the transfer to complete] */
    208 			while ((bp->b_flags & B_DONE) == 0)
    209 				tsleep((caddr_t) bp, PRIBIO + 1, "physio", 0);
    210 
    211 			/* Mark it busy again, so nobody else will use it. */
    212 			bp->b_flags |= B_BUSY;
    213 
    214 			/* [lower the priority level] */
    215 			splx(s);
    216 
    217 			/*
    218 			 * [unlock the part of the address space previously
    219 			 *    locked]
    220 			 */
    221 			vunmapbuf(bp, todo);
    222 			uvm_vsunlock(p, bp->b_data, todo);
    223  after_vsunlock:
    224 			PRELE(p);
    225 
    226 			/* remember error value (save a splbio/splx pair) */
    227 			if (bp->b_flags & B_ERROR)
    228 				error = (bp->b_error ? bp->b_error : EIO);
    229 
    230 			/*
    231 			 * [deduct the transfer size from the total number
    232 			 *    of data to transfer]
    233 			 */
    234 			done = bp->b_bcount - bp->b_resid;
    235 #ifdef DIAGNOSTIC
    236 			if (done < 0)
    237 				panic("done < 0; strategy broken");
    238 			if (done > todo)
    239 				panic("done > todo; strategy broken");
    240 #endif
    241 			iovp->iov_len -= done;
    242 			iovp->iov_base = (caddr_t)iovp->iov_base + done;
    243 			uio->uio_offset += done;
    244 			uio->uio_resid -= done;
    245 
    246 			/*
    247 			 * Now, check for an error.
    248 			 * Also, handle weird end-of-disk semantics.
    249 			 */
    250 			if (error || done < todo)
    251 				goto done;
    252 		}
    253 	}
    254 
    255 done:
    256 	/*
    257 	 * [clean up the state of the buffer]
    258 	 * Remember if somebody wants it, so we can wake them up below.
    259 	 * Also, if we had to steal it, give it back.
    260 	 */
    261 	s = splbio();
    262 	bp->b_flags &= ~(B_BUSY | B_PHYS | B_RAW);
    263 	if (nobuf)
    264 		putphysbuf(bp);
    265 	else {
    266 		/*
    267 		 * [if another process is waiting for the raw I/O buffer,
    268 		 *    wake up processes waiting to do physical I/O;
    269 		 */
    270 		if (bp->b_flags & B_WANTED) {
    271 			bp->b_flags &= ~B_WANTED;
    272 			wakeup(bp);
    273 		}
    274 	}
    275 	splx(s);
    276 
    277 	return (error);
    278 }
    279 
    280 /*
    281  * allocate a buffer structure for use in physical I/O.
    282  */
    283 struct buf *
    284 getphysbuf()
    285 {
    286 	struct buf *bp;
    287 
    288 	bp = malloc(sizeof(*bp), M_TEMP, M_WAITOK);
    289 	memset(bp, 0, sizeof(*bp));
    290 
    291 	/* XXXCDC: are the following two lines necessary? */
    292 	bp->b_rcred = bp->b_wcred = NOCRED;
    293 	bp->b_vnbufs.le_next = NOLIST;
    294 
    295 	return(bp);
    296 }
    297 
    298 /*
    299  * get rid of a swap buffer structure which has been used in physical I/O.
    300  */
    301 void
    302 putphysbuf(bp)
    303         struct buf *bp;
    304 {
    305 
    306 	/* XXXCDC: is this necesary? */
    307 	if (bp->b_vp)
    308 		brelvp(bp);
    309 
    310 	if (bp->b_flags & B_WANTED)
    311 		panic("putphysbuf: private buf B_WANTED");
    312 	free(bp, M_TEMP);
    313 
    314 }
    315 
    316 /*
    317  * Leffler, et al., says on p. 231:
    318  * "The minphys() routine is called by physio() to adjust the
    319  * size of each I/O transfer before the latter is passed to
    320  * the strategy routine..."
    321  *
    322  * so, just adjust the buffer's count accounting to MAXPHYS here,
    323  * and return the new count;
    324  */
    325 void
    326 minphys(bp)
    327 	struct buf *bp;
    328 {
    329 
    330 	if (bp->b_bcount > MAXPHYS)
    331 		bp->b_bcount = MAXPHYS;
    332 }
    333