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