Home | History | Annotate | Line # | Download | only in kern
kern_physio.c revision 1.46.2.1
      1 /*	$NetBSD: kern_physio.c,v 1.46.2.1 2001/03/05 22:49:41 nathanw 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/malloc.h>
     48 #include <sys/lwp.h>
     49 #include <sys/proc.h>
     50 
     51 #include <uvm/uvm_extern.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 
     64 struct buf *getphysbuf __P((void));
     65 void 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  */
     73 int
     74 physio(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 lwp *l = curproc;
     84 	struct proc *p = l->l_proc;
     85 	int error, done, i, nobuf, s;
     86 	long todo;
     87 
     88 	error = 0;
     89 	flags &= B_READ | B_WRITE | B_ORDERED;
     90 
     91 	/*
     92 	 * [check user read/write access to the data buffer]
     93 	 *
     94 	 * Check each iov one by one.  Note that we know if we're reading or
     95 	 * writing, so we ignore the uio's rw parameter.  Also note that if
     96 	 * we're doing a read, that's a *write* to user-space.
     97 	 */
     98 	if (uio->uio_segflg == UIO_USERSPACE) {
     99 		for (i = 0; i < uio->uio_iovcnt; i++) {
    100 			/* XXXCDC: map not locked, rethink */
    101 			if (__predict_false(!uvm_useracc(uio->uio_iov[i].iov_base,
    102 				     uio->uio_iov[i].iov_len,
    103 				     (flags == B_READ) ? B_WRITE : B_READ)))
    104 				return (EFAULT);
    105 		}
    106 	}
    107 
    108 	/* Make sure we have a buffer, creating one if necessary. */
    109 	if ((nobuf = (bp == NULL)) != 0) {
    110 
    111 		bp = getphysbuf();
    112 		/* bp was just malloc'd so can't already be busy */
    113 		bp->b_flags |= B_BUSY;
    114 
    115 	} else {
    116 
    117 		/* [raise the processor priority level to splbio;] */
    118 		s = splbio();
    119 
    120 		/* [while the buffer is marked busy] */
    121 		while (bp->b_flags & B_BUSY) {
    122 			/* [mark the buffer wanted] */
    123 			bp->b_flags |= B_WANTED;
    124 			/* [wait until the buffer is available] */
    125 			tsleep((caddr_t)bp, PRIBIO+1, "physbuf", 0);
    126 		}
    127 
    128 		/* Mark it busy, so nobody else will use it. */
    129 		bp->b_flags |= B_BUSY;
    130 
    131 		/* [lower the priority level] */
    132 		splx(s);
    133 
    134 	}
    135 
    136 	/* [set up the fixed part of the buffer for a transfer] */
    137 	bp->b_dev = dev;
    138 	bp->b_error = 0;
    139 	bp->b_proc = p;
    140 	LIST_INIT(&bp->b_dep);
    141 
    142 	/*
    143 	 * [while there are data to transfer and no I/O error]
    144 	 * Note that I/O errors are handled with a 'goto' at the bottom
    145 	 * of the 'while' loop.
    146 	 */
    147 	for (i = 0; i < uio->uio_iovcnt; i++) {
    148 		iovp = &uio->uio_iov[i];
    149 		while (iovp->iov_len > 0) {
    150 			/*
    151 			 * [mark the buffer busy for physical I/O]
    152 			 * (i.e. set B_PHYS (because it's an I/O to user
    153 			 * memory, and B_RAW, because B_RAW is to be
    154 			 * "Set by physio for raw transfers.", in addition
    155 			 * to the "busy" and read/write flag.)
    156 			 */
    157 			bp->b_flags = B_BUSY | B_PHYS | B_RAW | flags;
    158 
    159 			/* [set up the buffer for a maximum-sized transfer] */
    160 			bp->b_blkno = btodb(uio->uio_offset);
    161 			bp->b_bcount = iovp->iov_len;
    162 			bp->b_data = iovp->iov_base;
    163 
    164 			/*
    165 			 * [call minphys to bound the tranfer size]
    166 			 * and remember the amount of data to transfer,
    167 			 * for later comparison.
    168 			 */
    169 			(*minphys)(bp);
    170 			todo = bp->b_bcount;
    171 #ifdef DIAGNOSTIC
    172 			if (todo <= 0)
    173 				panic("todo(%ld) <= 0; minphys broken", todo);
    174 			if (todo > MAXPHYS)
    175 				panic("todo(%ld) > MAXPHYS; minphys broken",
    176 				      todo);
    177 #endif
    178 
    179 			/*
    180 			 * [lock the part of the user address space involved
    181 			 *    in the transfer]
    182 			 * Beware vmapbuf(); it clobbers b_data and
    183 			 * saves it in b_saveaddr.  However, vunmapbuf()
    184 			 * restores it.
    185 			 */
    186 			PHOLD(l);
    187 			if (__predict_false(uvm_vslock(p, bp->b_data, todo,
    188 			    (flags & B_READ) ?
    189 			    VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ)
    190 			    != KERN_SUCCESS)) {
    191 				bp->b_flags |= B_ERROR;
    192 				bp->b_error = EFAULT;
    193 				goto after_vsunlock;
    194 			}
    195 			vmapbuf(bp, todo);
    196 
    197 			/* [call strategy to start the transfer] */
    198 			(*strategy)(bp);
    199 
    200 			/*
    201 			 * Note that the raise/wait/lower/get error
    202 			 * steps below would be done by biowait(), but
    203 			 * we want to unlock the address space before
    204 			 * we lower the priority.
    205 			 *
    206 			 * [raise the priority level to splbio]
    207 			 */
    208 			s = splbio();
    209 
    210 			/* [wait for the transfer to complete] */
    211 			while ((bp->b_flags & B_DONE) == 0)
    212 				tsleep((caddr_t) bp, PRIBIO + 1, "physio", 0);
    213 
    214 			/* Mark it busy again, so nobody else will use it. */
    215 			bp->b_flags |= B_BUSY;
    216 
    217 			/* [lower the priority level] */
    218 			splx(s);
    219 
    220 			/*
    221 			 * [unlock the part of the address space previously
    222 			 *    locked]
    223 			 */
    224 			vunmapbuf(bp, todo);
    225 			uvm_vsunlock(p, bp->b_data, todo);
    226  after_vsunlock:
    227 			PRELE(l);
    228 
    229 			/* remember error value (save a splbio/splx pair) */
    230 			if (bp->b_flags & B_ERROR)
    231 				error = (bp->b_error ? bp->b_error : EIO);
    232 
    233 			/*
    234 			 * [deduct the transfer size from the total number
    235 			 *    of data to transfer]
    236 			 */
    237 			done = bp->b_bcount - bp->b_resid;
    238 #ifdef DIAGNOSTIC
    239 			if (__predict_false(done < 0))
    240 				panic("done < 0; strategy broken");
    241 			if (__predict_false(done > todo))
    242 				panic("done > todo; strategy broken");
    243 #endif
    244 			iovp->iov_len -= done;
    245 			iovp->iov_base = (caddr_t)iovp->iov_base + done;
    246 			uio->uio_offset += done;
    247 			uio->uio_resid -= done;
    248 
    249 			/*
    250 			 * Now, check for an error.
    251 			 * Also, handle weird end-of-disk semantics.
    252 			 */
    253 			if (error || done < todo)
    254 				goto done;
    255 		}
    256 	}
    257 
    258 done:
    259 	/*
    260 	 * [clean up the state of the buffer]
    261 	 * Remember if somebody wants it, so we can wake them up below.
    262 	 * Also, if we had to steal it, give it back.
    263 	 */
    264 	s = splbio();
    265 	bp->b_flags &= ~(B_BUSY | B_PHYS | B_RAW);
    266 	if (nobuf)
    267 		putphysbuf(bp);
    268 	else {
    269 		/*
    270 		 * [if another process is waiting for the raw I/O buffer,
    271 		 *    wake up processes waiting to do physical I/O;
    272 		 */
    273 		if (bp->b_flags & B_WANTED) {
    274 			bp->b_flags &= ~B_WANTED;
    275 			wakeup(bp);
    276 		}
    277 	}
    278 	splx(s);
    279 
    280 	return (error);
    281 }
    282 
    283 /*
    284  * allocate a buffer structure for use in physical I/O.
    285  */
    286 struct buf *
    287 getphysbuf()
    288 {
    289 	struct buf *bp;
    290 	int s;
    291 
    292 	s = splbio();
    293 	bp = pool_get(&bufpool, PR_WAITOK);
    294 	splx(s);
    295 	memset(bp, 0, sizeof(*bp));
    296 
    297 	/* XXXCDC: is the following line necessary? */
    298 	bp->b_vnbufs.le_next = NOLIST;
    299 
    300 	return(bp);
    301 }
    302 
    303 /*
    304  * get rid of a swap buffer structure which has been used in physical I/O.
    305  */
    306 void
    307 putphysbuf(bp)
    308         struct buf *bp;
    309 {
    310 	int s;
    311 
    312 	/* XXXCDC: is this necesary? */
    313 	if (bp->b_vp)
    314 		brelvp(bp);
    315 
    316 	if (__predict_false(bp->b_flags & B_WANTED))
    317 		panic("putphysbuf: private buf B_WANTED");
    318 	s = splbio();
    319 	pool_put(&bufpool, bp);
    320 	splx(s);
    321 }
    322 
    323 /*
    324  * Leffler, et al., says on p. 231:
    325  * "The minphys() routine is called by physio() to adjust the
    326  * size of each I/O transfer before the latter is passed to
    327  * the strategy routine..."
    328  *
    329  * so, just adjust the buffer's count accounting to MAXPHYS here,
    330  * and return the new count;
    331  */
    332 void
    333 minphys(bp)
    334 	struct buf *bp;
    335 {
    336 
    337 	if (bp->b_bcount > MAXPHYS)
    338 		bp->b_bcount = MAXPHYS;
    339 }
    340