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