Home | History | Annotate | Line # | Download | only in kern
kern_physio.c revision 1.66
      1 /*	$NetBSD: kern_physio.c,v 1.66 2005/11/04 08:39:33 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.66 2005/11/04 08:39:33 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 /* abuse these members/flags of struct buf */
    102 #define	b_running	b_freelistindex
    103 #define	b_endoffset	b_lblkno
    104 #define	B_DONTFREE	B_AGE
    105 
    106 /*
    107  * allocate a buffer structure for use in physical I/O.
    108  */
    109 static struct buf *
    110 getphysbuf(void)
    111 {
    112 	struct buf *bp;
    113 	int s;
    114 
    115 	s = splbio();
    116 	bp = pool_get(&bufpool, PR_WAITOK);
    117 	splx(s);
    118 	BUF_INIT(bp);
    119 	bp->b_error = 0;
    120 	bp->b_flags = B_BUSY;
    121 	return(bp);
    122 }
    123 
    124 /*
    125  * get rid of a swap buffer structure which has been used in physical I/O.
    126  */
    127 static void
    128 putphysbuf(struct buf *bp)
    129 {
    130 	int s;
    131 
    132 	if ((bp->b_flags & B_DONTFREE) != 0) {
    133 		return;
    134 	}
    135 
    136 	if (__predict_false(bp->b_flags & B_WANTED))
    137 		panic("putphysbuf: private buf B_WANTED");
    138 	s = splbio();
    139 	pool_put(&bufpool, bp);
    140 	splx(s);
    141 }
    142 
    143 static void
    144 physio_done(struct work *wk, void *dummy)
    145 {
    146 	struct buf *bp = (void *)wk;
    147 	size_t todo = bp->b_bufsize;
    148 	size_t done = bp->b_bcount - bp->b_resid;
    149 	struct buf *mbp = bp->b_private;
    150 
    151 	KASSERT(&bp->b_work == wk);
    152 	KASSERT(bp->b_bcount <= todo);
    153 	KASSERT(bp->b_resid <= bp->b_bcount);
    154 	KASSERT((bp->b_flags & B_PHYS) != 0);
    155 	KASSERT(dummy == NULL);
    156 
    157 	vunmapbuf(bp, todo);
    158 	uvm_vsunlock(bp->b_proc, bp->b_data, todo);
    159 
    160 	simple_lock(&mbp->b_interlock);
    161 	if (__predict_false(done != todo)) {
    162 		off_t endoffset = dbtob(bp->b_blkno) + done;
    163 
    164 		if (mbp->b_endoffset == -1 || endoffset < mbp->b_endoffset) {
    165 			mbp->b_endoffset = endoffset;
    166 		}
    167 		mbp->b_flags |= B_ERROR;
    168 	}
    169 
    170 	/*
    171 	 * EINVAL is not very important as it happens for i/o past the end
    172 	 * of the partition.
    173 	 */
    174 
    175 	if (__predict_false((bp->b_flags & B_ERROR) != 0 &&
    176 	    ((mbp->b_flags & B_ERROR) == 0 || mbp->b_error == EINVAL))) {
    177 		if (bp->b_error == 0) {
    178 			mbp->b_error = EIO; /* XXX */
    179 		} else {
    180 			mbp->b_error = bp->b_error;
    181 		}
    182 		mbp->b_flags |= B_ERROR;
    183 		goto done;
    184 	}
    185 done:
    186 	mbp->b_running--;
    187 	if ((mbp->b_flags & B_WANTED) != 0) {
    188 		mbp->b_flags &= ~B_WANTED;
    189 		wakeup(mbp);
    190 	}
    191 	simple_unlock(&mbp->b_interlock);
    192 
    193 	putphysbuf(bp);
    194 }
    195 
    196 static void
    197 physio_biodone(struct buf *bp)
    198 {
    199 #if defined(DIAGNOSTIC)
    200 	struct buf *mbp = bp->b_private;
    201 	size_t todo = bp->b_bufsize;
    202 
    203 	KASSERT(mbp->b_running > 0);
    204 	KASSERT(bp->b_bcount <= todo);
    205 	KASSERT(bp->b_resid <= bp->b_bcount);
    206 #endif /* defined(DIAGNOSTIC) */
    207 
    208 	workqueue_enqueue(physio_workqueue, &bp->b_work);
    209 }
    210 
    211 static int
    212 physio_wait(struct buf *bp, int n, const char *wchan)
    213 {
    214 	int error = 0;
    215 
    216 	LOCK_ASSERT(simple_lock_held(&bp->b_interlock));
    217 
    218 	while (bp->b_running > n) {
    219 		bp->b_flags |= B_WANTED;
    220 		error = ltsleep(bp, PRIBIO + 1, wchan, 0, &bp->b_interlock);
    221 		if (error) {
    222 			break;
    223 		}
    224 	}
    225 
    226 	return error;
    227 }
    228 
    229 static void
    230 physio_init(void)
    231 {
    232 
    233 	KASSERT(physio_workqueue == NULL);
    234 
    235 	if (workqueue_create(&physio_workqueue, "physiod",
    236 	    physio_done, NULL, PRIBIO, 0/* IPL_BIO notyet */, 0)) {
    237 		panic("physiod create");
    238 	}
    239 }
    240 
    241 #define	PHYSIO_CONCURRENCY	16	/* XXX tune */
    242 
    243 /*
    244  * Do "physical I/O" on behalf of a user.  "Physical I/O" is I/O directly
    245  * from the raw device to user buffers, and bypasses the buffer cache.
    246  *
    247  * Comments in brackets are from Leffler, et al.'s pseudo-code implementation.
    248  */
    249 int
    250 physio(void (*strategy)(struct buf *), struct buf *obp, dev_t dev, int flags,
    251     void (*min_phys)(struct buf *), struct uio *uio)
    252 {
    253 	struct iovec *iovp;
    254 	struct lwp *l = curlwp;
    255 	struct proc *p = l->l_proc;
    256 	int i, s;
    257 	int error = 0;
    258 	int error2;
    259 	size_t todo;
    260 	struct buf *bp = NULL;
    261 	struct buf *mbp;
    262 	int concurrency = PHYSIO_CONCURRENCY - 1;
    263 
    264 	RUN_ONCE(&physio_initialized, physio_init);
    265 
    266 	flags &= B_READ | B_WRITE;
    267 
    268 	/* Make sure we have a buffer, creating one if necessary. */
    269 	if (obp != NULL) {
    270 		/* [raise the processor priority level to splbio;] */
    271 		s = splbio();
    272 		simple_lock(&obp->b_interlock);
    273 
    274 		/* [while the buffer is marked busy] */
    275 		while (obp->b_flags & B_BUSY) {
    276 			/* [mark the buffer wanted] */
    277 			obp->b_flags |= B_WANTED;
    278 			/* [wait until the buffer is available] */
    279 			ltsleep(obp, PRIBIO+1, "physbuf", 0, &bp->b_interlock);
    280 		}
    281 
    282 		/* Mark it busy, so nobody else will use it. */
    283 		obp->b_flags = B_BUSY | B_DONTFREE;
    284 
    285 		/* [lower the priority level] */
    286 		simple_unlock(&obp->b_interlock);
    287 		splx(s);
    288 
    289 		concurrency = 0; /* see "XXXkludge" comment below */
    290 	}
    291 
    292 	mbp = getphysbuf();
    293 	mbp->b_running = 0;
    294 	mbp->b_endoffset = -1;
    295 
    296 	PHOLD(l);
    297 
    298 	for (i = 0; i < uio->uio_iovcnt; i++) {
    299 		iovp = &uio->uio_iov[i];
    300 		while (iovp->iov_len > 0) {
    301 			simple_lock(&mbp->b_interlock);
    302 			if ((mbp->b_flags & B_ERROR) != 0) {
    303 				error = mbp->b_error;
    304 				goto done_locked;
    305 			}
    306 			error = physio_wait(mbp, concurrency, "physio1");
    307 			if (error) {
    308 				goto done_locked;
    309 			}
    310 			simple_unlock(&mbp->b_interlock);
    311 			if (obp != NULL) {
    312 				/*
    313 				 * XXXkludge
    314 				 * some drivers use "obp" as an identifier.
    315 				 */
    316 				bp = obp;
    317 			} else {
    318 				bp = getphysbuf();
    319 			}
    320 			bp->b_dev = dev;
    321 			bp->b_proc = p;
    322 			bp->b_private = mbp;
    323 			bp->b_vp = NULL;
    324 
    325 			/*
    326 			 * [mark the buffer busy for physical I/O]
    327 			 * (i.e. set B_PHYS (because it's an I/O to user
    328 			 * memory, and B_RAW, because B_RAW is to be
    329 			 * "Set by physio for raw transfers.", in addition
    330 			 * to the "busy" and read/write flag.)
    331 			 */
    332 			bp->b_flags = (bp->b_flags & B_DONTFREE) |
    333 			    B_BUSY | B_PHYS | B_RAW | B_CALL | flags;
    334 			bp->b_iodone = physio_biodone;
    335 
    336 			/* [set up the buffer for a maximum-sized transfer] */
    337 			bp->b_blkno = btodb(uio->uio_offset);
    338 			if (dbtob(bp->b_blkno) != uio->uio_offset) {
    339 				error = EINVAL;
    340 				goto done;
    341 			}
    342 			bp->b_bcount = MIN(MAXPHYS, iovp->iov_len);
    343 			bp->b_data = iovp->iov_base;
    344 
    345 			/*
    346 			 * [call minphys to bound the transfer size]
    347 			 * and remember the amount of data to transfer,
    348 			 * for later comparison.
    349 			 */
    350 			(*min_phys)(bp);
    351 			todo = bp->b_bufsize = bp->b_bcount;
    352 #if defined(DIAGNOSTIC)
    353 			if (todo > MAXPHYS)
    354 				panic("todo(%zu) > MAXPHYS; minphys broken",
    355 				    todo);
    356 #endif /* defined(DIAGNOSTIC) */
    357 
    358 			/*
    359 			 * [lock the part of the user address space involved
    360 			 *    in the transfer]
    361 			 * Beware vmapbuf(); it clobbers b_data and
    362 			 * saves it in b_saveaddr.  However, vunmapbuf()
    363 			 * restores it.
    364 			 */
    365 			error = uvm_vslock(p, bp->b_data, todo,
    366 			    (flags & B_READ) ?  VM_PROT_WRITE : VM_PROT_READ);
    367 			if (error) {
    368 				goto done;
    369 			}
    370 			vmapbuf(bp, todo);
    371 
    372 			BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
    373 
    374 			simple_lock(&mbp->b_interlock);
    375 			mbp->b_running++;
    376 			simple_unlock(&mbp->b_interlock);
    377 
    378 			/* [call strategy to start the transfer] */
    379 			(*strategy)(bp);
    380 			bp = NULL;
    381 
    382 			iovp->iov_len -= todo;
    383 			iovp->iov_base = (caddr_t)iovp->iov_base + todo;
    384 			uio->uio_offset += todo;
    385 			uio->uio_resid -= todo;
    386 		}
    387 	}
    388 
    389 done:
    390 	simple_lock(&mbp->b_interlock);
    391 done_locked:
    392 	error2 = physio_wait(mbp, 0, "physio2");
    393 	if (error == 0) {
    394 		error = error2;
    395 	}
    396 	simple_unlock(&mbp->b_interlock);
    397 
    398 	if ((mbp->b_flags & B_ERROR) != 0) {
    399 		uio->uio_resid = uio->uio_offset - mbp->b_endoffset;
    400 	} else {
    401 		KASSERT(mbp->b_endoffset == -1);
    402 	}
    403 	if (bp != NULL) {
    404 		putphysbuf(bp);
    405 	}
    406 	if (error == 0) {
    407 		error = mbp->b_error;
    408 	}
    409 	putphysbuf(mbp);
    410 
    411 	/*
    412 	 * [clean up the state of the buffer]
    413 	 * Remember if somebody wants it, so we can wake them up below.
    414 	 * Also, if we had to steal it, give it back.
    415 	 */
    416 	if (obp != NULL) {
    417 		KASSERT((obp->b_flags & B_BUSY) != 0);
    418 		KASSERT((obp->b_flags & B_DONTFREE) != 0);
    419 
    420 		/*
    421 		 * [if another process is waiting for the raw I/O buffer,
    422 		 *    wake up processes waiting to do physical I/O;
    423 		 */
    424 		s = splbio();
    425 		simple_lock(&obp->b_interlock);
    426 		obp->b_flags &=
    427 		    ~(B_BUSY | B_PHYS | B_RAW | B_CALL | B_DONTFREE);
    428 		if ((obp->b_flags & B_WANTED) != 0) {
    429 			obp->b_flags &= ~B_WANTED;
    430 			wakeup(obp);
    431 		}
    432 		simple_unlock(&obp->b_interlock);
    433 		splx(s);
    434 	}
    435 	PRELE(l);
    436 
    437 	return error;
    438 }
    439 
    440 /*
    441  * Leffler, et al., says on p. 231:
    442  * "The minphys() routine is called by physio() to adjust the
    443  * size of each I/O transfer before the latter is passed to
    444  * the strategy routine..."
    445  *
    446  * so, just adjust the buffer's count accounting to MAXPHYS here,
    447  * and return the new count;
    448  */
    449 void
    450 minphys(struct buf *bp)
    451 {
    452 
    453 	if (bp->b_bcount > MAXPHYS)
    454 		bp->b_bcount = MAXPHYS;
    455 }
    456