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