Home | History | Annotate | Line # | Download | only in kern
kern_physio.c revision 1.62
      1 /*	$NetBSD: kern_physio.c,v 1.62 2005/10/29 11:23:19 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1982, 1986, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)kern_physio.c	8.1 (Berkeley) 6/10/93
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1994 Christopher G. Demetriou
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)kern_physio.c	8.1 (Berkeley) 6/10/93
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: kern_physio.c,v 1.62 2005/10/29 11:23:19 yamt Exp $");
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/buf.h>
     79 #include <sys/proc.h>
     80 #include <sys/once.h>
     81 #include <sys/workqueue.h>
     82 
     83 #include <uvm/uvm_extern.h>
     84 
     85 ONCE_DECL(physio_initialized);
     86 struct workqueue *physio_workqueue;
     87 
     88 /*
     89  * The routines implemented in this file are described in:
     90  *	Leffler, et al.: The Design and Implementation of the 4.3BSD
     91  *	    UNIX Operating System (Addison Welley, 1989)
     92  * on pages 231-233.
     93  *
     94  * The routines "getphysbuf" and "putphysbuf" steal and return a swap
     95  * buffer.  Leffler, et al., says that swap buffers are used to do the
     96  * I/O, so raw I/O requests don't have to be single-threaded.  Of course,
     97  * NetBSD doesn't use "swap buffers" -- we have our own memory pool for
     98  * buffer descriptors.
     99  */
    100 
    101 /*
    102  * allocate a buffer structure for use in physical I/O.
    103  */
    104 static struct buf *
    105 getphysbuf(void)
    106 {
    107 	struct buf *bp;
    108 	int s;
    109 
    110 	s = splbio();
    111 	bp = pool_get(&bufpool, PR_WAITOK);
    112 	splx(s);
    113 	BUF_INIT(bp);
    114 	bp->b_error = 0;
    115 	return(bp);
    116 }
    117 
    118 /*
    119  * get rid of a swap buffer structure which has been used in physical I/O.
    120  */
    121 static void
    122 putphysbuf(struct buf *bp)
    123 {
    124 	int s;
    125 
    126 	if (__predict_false(bp->b_flags & B_WANTED))
    127 		panic("putphysbuf: private buf B_WANTED");
    128 	s = splbio();
    129 	pool_put(&bufpool, bp);
    130 	splx(s);
    131 }
    132 
    133 /* abuse these members of struct buf */
    134 #define	b_running	b_freelistindex
    135 #define	b_eomoffset	b_lblkno
    136 
    137 static void
    138 physio_done(struct work *wk, void *dummy)
    139 {
    140 	struct buf *bp = (void *)wk;
    141 	size_t todo = bp->b_bufsize;
    142 	struct buf *mbp = bp->b_private;
    143 
    144 	KASSERT(&bp->b_work == wk);
    145 	KASSERT(bp->b_bcount <= todo);
    146 	KASSERT(bp->b_resid <= bp->b_bcount);
    147 	KASSERT((bp->b_flags & B_PHYS) != 0);
    148 	KASSERT(dummy == NULL);
    149 
    150 	vunmapbuf(bp, todo);
    151 	uvm_vsunlock(bp->b_proc, bp->b_data, todo);
    152 
    153 	simple_lock(&mbp->b_interlock);
    154 	if ((mbp->b_flags & B_ERROR) != 0) {
    155 		goto done;
    156 	}
    157 	if ((bp->b_flags & B_ERROR) != 0) {
    158 		if (bp->b_error == 0) {
    159 			mbp->b_error = EIO; /* XXX */
    160 		} else {
    161 			mbp->b_error = bp->b_error;
    162 		}
    163 		mbp->b_flags |= B_ERROR;
    164 		goto done;
    165 	}
    166 	KASSERT(bp->b_resid == 0); /* XXX */
    167 	if (bp->b_bcount != todo) {
    168 #if defined(DIAGNOSTIC)
    169 		off_t eomoffset = dbtob(bp->b_blkno);
    170 
    171 		if ((mbp->b_flags & B_ERROR) != 0 &&
    172 		    mbp->b_eomoffset != eomoffset) {
    173 			panic("%s: eom mismatch", __func__);
    174 		}
    175 		mbp->b_eomoffset = eomoffset;
    176 #endif /* defined(DIAGNOSTIC) */
    177 		mbp->b_flags |= B_ERROR;
    178 		mbp->b_error = 0;
    179 		goto done;
    180 	}
    181 done:
    182 	mbp->b_resid -= bp->b_bcount - bp->b_resid;
    183 	mbp->b_running--;
    184 	if ((mbp->b_flags & B_WANTED) != 0) {
    185 		mbp->b_flags &= ~B_WANTED;
    186 		wakeup(mbp);
    187 	}
    188 	simple_unlock(&mbp->b_interlock);
    189 
    190 	putphysbuf(bp);
    191 }
    192 
    193 static void
    194 physio_biodone(struct buf *bp)
    195 {
    196 	struct buf *mbp = bp->b_private;
    197 	size_t todo = bp->b_bufsize;
    198 
    199 	KASSERT(mbp->b_running > 0);
    200 	KASSERT(todo <= mbp->b_resid);
    201 	KASSERT(bp->b_bcount <= todo);
    202 	KASSERT(bp->b_resid <= bp->b_bcount);
    203 
    204 	workqueue_enqueue(physio_workqueue, &bp->b_work);
    205 }
    206 
    207 static int
    208 physio_wait(struct buf *bp, int n, const char *wchan)
    209 {
    210 	int error = 0;
    211 
    212 	LOCK_ASSERT(simple_lock_held(&bp->b_interlock));
    213 
    214 	while (bp->b_running > n) {
    215 		bp->b_flags |= B_WANTED;
    216 		error = ltsleep(bp, PRIBIO + 1, wchan, 0, &bp->b_interlock);
    217 		if (error) {
    218 			break;
    219 		}
    220 	}
    221 
    222 	return error;
    223 }
    224 
    225 static void
    226 physio_init(void)
    227 {
    228 
    229 	KASSERT(physio_workqueue == NULL);
    230 
    231 	if (workqueue_create(&physio_workqueue, "physiod",
    232 	    physio_done, NULL, PRIBIO, IPL_BIO, 0)) {
    233 		panic("physiod create");
    234 	}
    235 }
    236 
    237 #define	PHYSIO_CONCURRENCY	16	/* XXX tune */
    238 
    239 /*
    240  * Do "physical I/O" on behalf of a user.  "Physical I/O" is I/O directly
    241  * from the raw device to user buffers, and bypasses the buffer cache.
    242  *
    243  * Comments in brackets are from Leffler, et al.'s pseudo-code implementation.
    244  */
    245 int
    246 physio(void (*strategy)(struct buf *), struct buf *obp, dev_t dev, int flags,
    247     void (*min_phys)(struct buf *), struct uio *uio)
    248 {
    249 	struct iovec *iovp;
    250 	struct lwp *l = curlwp;
    251 	struct proc *p = l->l_proc;
    252 	int i, s;
    253 	int error = 0;
    254 	int error2;
    255 	size_t todo;
    256 	struct buf *bp = NULL;
    257 	struct buf *mbp;
    258 
    259 	RUN_ONCE(&physio_initialized, physio_init);
    260 
    261 	flags &= B_READ | B_WRITE;
    262 
    263 	/* Make sure we have a buffer, creating one if necessary. */
    264 	if (obp != NULL) {
    265 		/* [raise the processor priority level to splbio;] */
    266 		s = splbio();
    267 
    268 		/* [while the buffer is marked busy] */
    269 		while (obp->b_flags & B_BUSY) {
    270 			/* [mark the buffer wanted] */
    271 			obp->b_flags |= B_WANTED;
    272 			/* [wait until the buffer is available] */
    273 			tsleep(obp, PRIBIO+1, "physbuf", 0);
    274 		}
    275 
    276 		/* Mark it busy, so nobody else will use it. */
    277 		obp->b_flags |= B_BUSY;
    278 
    279 		/* [lower the priority level] */
    280 		splx(s);
    281 	}
    282 
    283 	mbp = getphysbuf();
    284 	mbp->b_resid = uio->uio_resid;
    285 	mbp->b_running = 0;
    286 	mbp->b_flags = 0;
    287 
    288 	PHOLD(l);
    289 
    290 	for (i = 0; i < uio->uio_iovcnt; i++) {
    291 		iovp = &uio->uio_iov[i];
    292 		while (iovp->iov_len > 0) {
    293 			simple_lock(&mbp->b_interlock);
    294 			if ((mbp->b_flags & B_ERROR) != 0) {
    295 				error = mbp->b_error;
    296 				goto done_locked;
    297 			}
    298 			error = physio_wait(mbp, PHYSIO_CONCURRENCY - 1,
    299 			    "physio1");
    300 			if (error) {
    301 				goto done_locked;
    302 			}
    303 			simple_unlock(&mbp->b_interlock);
    304 			bp = getphysbuf();
    305 			bp->b_dev = dev;
    306 			bp->b_proc = p;
    307 			bp->b_private = mbp;
    308 			bp->b_vp = NULL;
    309 
    310 			/*
    311 			 * [mark the buffer busy for physical I/O]
    312 			 * (i.e. set B_PHYS (because it's an I/O to user
    313 			 * memory, and B_RAW, because B_RAW is to be
    314 			 * "Set by physio for raw transfers.", in addition
    315 			 * to the "busy" and read/write flag.)
    316 			 */
    317 			bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_CALL | flags;
    318 			bp->b_iodone = physio_biodone;
    319 
    320 			/* [set up the buffer for a maximum-sized transfer] */
    321 			bp->b_blkno = btodb(uio->uio_offset);
    322 			if (dbtob(bp->b_blkno) != uio->uio_offset) {
    323 				error = EINVAL;
    324 				goto done;
    325 			}
    326 			bp->b_bcount = MIN(MAXPHYS, iovp->iov_len);
    327 			bp->b_data = iovp->iov_base;
    328 
    329 			/*
    330 			 * [call minphys to bound the transfer size]
    331 			 * and remember the amount of data to transfer,
    332 			 * for later comparison.
    333 			 */
    334 			(*min_phys)(bp);
    335 			todo = bp->b_bufsize = bp->b_bcount;
    336 #if defined(DIAGNOSTIC)
    337 			if (todo > MAXPHYS)
    338 				panic("todo(%zu) > MAXPHYS; minphys broken",
    339 				    todo);
    340 #endif /* defined(DIAGNOSTIC) */
    341 
    342 			/*
    343 			 * [lock the part of the user address space involved
    344 			 *    in the transfer]
    345 			 * Beware vmapbuf(); it clobbers b_data and
    346 			 * saves it in b_saveaddr.  However, vunmapbuf()
    347 			 * restores it.
    348 			 */
    349 			error = uvm_vslock(p, bp->b_data, todo,
    350 			    (flags & B_READ) ?  VM_PROT_WRITE : VM_PROT_READ);
    351 			if (error) {
    352 				goto done;
    353 			}
    354 			vmapbuf(bp, todo);
    355 
    356 			BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
    357 
    358 			simple_lock(&mbp->b_interlock);
    359 			mbp->b_running++;
    360 			simple_unlock(&mbp->b_interlock);
    361 
    362 			/* [call strategy to start the transfer] */
    363 			(*strategy)(bp);
    364 			bp = NULL;
    365 
    366 			iovp->iov_len -= todo;
    367 			iovp->iov_base = (caddr_t)iovp->iov_base + todo;
    368 			uio->uio_offset += todo;
    369 			uio->uio_resid -= todo;
    370 		}
    371 	}
    372 
    373 done:
    374 	simple_lock(&mbp->b_interlock);
    375 done_locked:
    376 	error2 = physio_wait(mbp, 0, "physio2");
    377 	if (error == 0) {
    378 		error = error2;
    379 	}
    380 	simple_unlock(&mbp->b_interlock);
    381 	KASSERT((mbp->b_flags & B_ERROR) != 0 ||
    382 	    mbp->b_resid == uio->uio_resid);
    383 #if defined(DIAGNOSTIC)
    384 	if ((mbp->b_flags & B_ERROR) != 0 && mbp->b_error == 0 &&
    385 	    uio->uio_offset - mbp->b_resid != mbp->b_eomoffset) {
    386 		panic("%s: eom", __func__);
    387 	}
    388 #endif /* defined(DIAGNOSTIC) */
    389 	uio->uio_resid = mbp->b_resid;
    390 	if (bp != NULL) {
    391 		putphysbuf(bp);
    392 	}
    393 	if (error == 0) {
    394 		error = mbp->b_error;
    395 	}
    396 	putphysbuf(mbp);
    397 
    398 	/*
    399 	 * [clean up the state of the buffer]
    400 	 * Remember if somebody wants it, so we can wake them up below.
    401 	 * Also, if we had to steal it, give it back.
    402 	 */
    403 	if (obp != NULL) {
    404 		s = splbio();
    405 		/*
    406 		 * [if another process is waiting for the raw I/O buffer,
    407 		 *    wake up processes waiting to do physical I/O;
    408 		 */
    409 		if (obp->b_flags & B_WANTED) {
    410 			obp->b_flags &= ~B_WANTED;
    411 			wakeup(obp);
    412 		}
    413 		splx(s);
    414 	}
    415 	PRELE(l);
    416 
    417 	return error;
    418 }
    419 
    420 /*
    421  * Leffler, et al., says on p. 231:
    422  * "The minphys() routine is called by physio() to adjust the
    423  * size of each I/O transfer before the latter is passed to
    424  * the strategy routine..."
    425  *
    426  * so, just adjust the buffer's count accounting to MAXPHYS here,
    427  * and return the new count;
    428  */
    429 void
    430 minphys(struct buf *bp)
    431 {
    432 
    433 	if (bp->b_bcount > MAXPHYS)
    434 		bp->b_bcount = MAXPHYS;
    435 }
    436