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