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