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