Home | History | Annotate | Line # | Download | only in kern
      1  1.102  riastrad /*	$NetBSD: kern_physio.c,v 1.102 2022/07/10 23:11:55 riastradh 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.102  riastrad __KERNEL_RCSID(0, "$NetBSD: kern_physio.c,v 1.102 2022/07/10 23:11:55 riastradh Exp $");
     75   1.30       mrg 
     76   1.20       cgd #include <sys/param.h>
     77   1.20       cgd #include <sys/systm.h>
     78   1.94   mlelstv #include <sys/conf.h>
     79   1.20       cgd #include <sys/buf.h>
     80   1.20       cgd #include <sys/proc.h>
     81   1.62      yamt #include <sys/once.h>
     82   1.62      yamt #include <sys/workqueue.h>
     83   1.84   hannken #include <sys/kmem.h>
     84   1.26  christos 
     85   1.29       mrg #include <uvm/uvm_extern.h>
     86   1.29       mrg 
     87   1.62      yamt ONCE_DECL(physio_initialized);
     88   1.62      yamt struct workqueue *physio_workqueue;
     89   1.62      yamt 
     90   1.90        ad int physio_concurrency = 16;
     91   1.20       cgd 
     92   1.70      yamt /* #define	PHYSIO_DEBUG */
     93   1.70      yamt #if defined(PHYSIO_DEBUG)
     94   1.70      yamt #define	DPRINTF(a)	printf a
     95   1.70      yamt #else /* defined(PHYSIO_DEBUG) */
     96   1.70      yamt #define	DPRINTF(a)	/* nothing */
     97   1.70      yamt #endif /* defined(PHYSIO_DEBUG) */
     98   1.70      yamt 
     99   1.84   hannken struct physio_stat {
    100   1.84   hannken 	int ps_running;
    101   1.84   hannken 	int ps_error;
    102   1.84   hannken 	int ps_failed;
    103   1.84   hannken 	off_t ps_endoffset;
    104   1.94   mlelstv 	size_t ps_resid;
    105   1.88   hannken 	buf_t *ps_orig_bp;
    106   1.84   hannken 	kmutex_t ps_lock;
    107   1.84   hannken 	kcondvar_t ps_cv;
    108   1.84   hannken };
    109   1.84   hannken 
    110   1.62      yamt static void
    111   1.76      yamt physio_done(struct work *wk, void *dummy)
    112   1.62      yamt {
    113   1.62      yamt 	struct buf *bp = (void *)wk;
    114   1.62      yamt 	size_t todo = bp->b_bufsize;
    115   1.66      yamt 	size_t done = bp->b_bcount - bp->b_resid;
    116   1.84   hannken 	struct physio_stat *ps = bp->b_private;
    117   1.91   hannken 	bool is_iobuf;
    118   1.62      yamt 
    119   1.62      yamt 	KASSERT(&bp->b_work == wk);
    120   1.62      yamt 	KASSERT(bp->b_bcount <= todo);
    121   1.62      yamt 	KASSERT(bp->b_resid <= bp->b_bcount);
    122   1.62      yamt 	KASSERT((bp->b_flags & B_PHYS) != 0);
    123   1.62      yamt 	KASSERT(dummy == NULL);
    124   1.62      yamt 
    125   1.62      yamt 	vunmapbuf(bp, todo);
    126   1.74       chs 	uvm_vsunlock(bp->b_proc->p_vmspace, bp->b_data, todo);
    127   1.62      yamt 
    128   1.84   hannken 	mutex_enter(&ps->ps_lock);
    129   1.91   hannken 	is_iobuf = (bp != ps->ps_orig_bp);
    130   1.66      yamt 	if (__predict_false(done != todo)) {
    131   1.66      yamt 		off_t endoffset = dbtob(bp->b_blkno) + done;
    132   1.66      yamt 
    133   1.70      yamt 		/*
    134   1.70      yamt 		 * we got an error or hit EOM.
    135   1.70      yamt 		 *
    136   1.70      yamt 		 * we only care about the first one.
    137   1.70      yamt 		 * ie. the one at the lowest offset.
    138   1.70      yamt 		 */
    139   1.70      yamt 
    140   1.84   hannken 		KASSERT(ps->ps_endoffset != endoffset);
    141   1.70      yamt 		DPRINTF(("%s: error=%d at %" PRIu64 " - %" PRIu64
    142   1.70      yamt 		    ", blkno=%" PRIu64 ", bcount=%d, flags=0x%x\n",
    143   1.70      yamt 		    __func__, bp->b_error, dbtob(bp->b_blkno), endoffset,
    144   1.70      yamt 		    bp->b_blkno, bp->b_bcount, bp->b_flags));
    145   1.70      yamt 
    146   1.84   hannken 		if (ps->ps_endoffset == -1 || endoffset < ps->ps_endoffset) {
    147   1.84   hannken 			DPRINTF(("%s: ps=%p, error %d -> %d, endoff %" PRIu64
    148   1.70      yamt 			    " -> %" PRIu64 "\n",
    149   1.84   hannken 			    __func__, ps,
    150   1.84   hannken 			    ps->ps_error, bp->b_error,
    151   1.84   hannken 			    ps->ps_endoffset, endoffset));
    152   1.66      yamt 
    153   1.84   hannken 			ps->ps_endoffset = endoffset;
    154   1.84   hannken 			ps->ps_error = bp->b_error;
    155   1.62      yamt 		}
    156   1.84   hannken 		ps->ps_failed++;
    157   1.94   mlelstv 
    158   1.94   mlelstv 		ps->ps_resid += todo - done;
    159   1.70      yamt 	} else {
    160   1.83        ad 		KASSERT(bp->b_error == 0);
    161   1.62      yamt 	}
    162   1.67      yamt 
    163   1.84   hannken 	ps->ps_running--;
    164   1.84   hannken 	cv_signal(&ps->ps_cv);
    165   1.84   hannken 	mutex_exit(&ps->ps_lock);
    166   1.62      yamt 
    167   1.91   hannken 	if (is_iobuf)
    168   1.88   hannken 		putiobuf(bp);
    169   1.62      yamt }
    170   1.62      yamt 
    171   1.62      yamt static void
    172   1.62      yamt physio_biodone(struct buf *bp)
    173   1.62      yamt {
    174   1.63      yamt #if defined(DIAGNOSTIC)
    175   1.84   hannken 	struct physio_stat *ps = bp->b_private;
    176   1.62      yamt 	size_t todo = bp->b_bufsize;
    177   1.89    bouyer 	size_t done = bp->b_bcount - bp->b_resid;
    178   1.62      yamt 
    179   1.84   hannken 	KASSERT(ps->ps_running > 0);
    180   1.62      yamt 	KASSERT(bp->b_bcount <= todo);
    181   1.62      yamt 	KASSERT(bp->b_resid <= bp->b_bcount);
    182   1.89    bouyer 	if (done == todo)
    183  1.101  riastrad 		KASSERTMSG(bp->b_error == 0, "error=%d", bp->b_error);
    184   1.63      yamt #endif /* defined(DIAGNOSTIC) */
    185   1.62      yamt 
    186   1.82     rmind 	workqueue_enqueue(physio_workqueue, &bp->b_work, NULL);
    187   1.62      yamt }
    188   1.62      yamt 
    189   1.84   hannken static void
    190   1.84   hannken physio_wait(struct physio_stat *ps, int n)
    191   1.62      yamt {
    192   1.62      yamt 
    193   1.84   hannken 	KASSERT(mutex_owned(&ps->ps_lock));
    194   1.62      yamt 
    195   1.84   hannken 	while (ps->ps_running > n)
    196   1.84   hannken 		cv_wait(&ps->ps_cv, &ps->ps_lock);
    197   1.62      yamt }
    198   1.62      yamt 
    199   1.72      yamt static int
    200   1.62      yamt physio_init(void)
    201   1.62      yamt {
    202   1.72      yamt 	int error;
    203   1.62      yamt 
    204   1.62      yamt 	KASSERT(physio_workqueue == NULL);
    205   1.62      yamt 
    206   1.72      yamt 	error = workqueue_create(&physio_workqueue, "physiod",
    207   1.86        ad 	    physio_done, NULL, PRI_BIO, IPL_BIO, WQ_MPSAFE);
    208   1.72      yamt 
    209   1.72      yamt 	return error;
    210   1.62      yamt }
    211   1.62      yamt 
    212   1.20       cgd /*
    213   1.20       cgd  * Do "physical I/O" on behalf of a user.  "Physical I/O" is I/O directly
    214   1.20       cgd  * from the raw device to user buffers, and bypasses the buffer cache.
    215   1.20       cgd  */
    216   1.20       cgd int
    217   1.62      yamt physio(void (*strategy)(struct buf *), struct buf *obp, dev_t dev, int flags,
    218   1.61   thorpej     void (*min_phys)(struct buf *), struct uio *uio)
    219   1.20       cgd {
    220   1.20       cgd 	struct iovec *iovp;
    221   1.54   thorpej 	struct lwp *l = curlwp;
    222   1.54   thorpej 	struct proc *p = l->l_proc;
    223   1.86        ad 	int i, error;
    224   1.62      yamt 	struct buf *bp = NULL;
    225   1.84   hannken 	struct physio_stat *ps;
    226   1.90        ad 	int concurrency = physio_concurrency - 1;
    227   1.94   mlelstv 	int isdisk;
    228   1.62      yamt 
    229   1.72      yamt 	error = RUN_ONCE(&physio_initialized, physio_init);
    230   1.72      yamt 	if (__predict_false(error != 0)) {
    231   1.72      yamt 		return error;
    232   1.72      yamt 	}
    233   1.20       cgd 
    234   1.70      yamt 	DPRINTF(("%s: called: off=%" PRIu64 ", resid=%zu\n",
    235   1.70      yamt 	    __func__, uio->uio_offset, uio->uio_resid));
    236   1.70      yamt 
    237   1.53   hannken 	flags &= B_READ | B_WRITE;
    238   1.20       cgd 
    239   1.93     pooka 	ps = kmem_zalloc(sizeof(*ps), KM_SLEEP);
    240   1.84   hannken 	/* ps->ps_running = 0; */
    241   1.84   hannken 	/* ps->ps_error = 0; */
    242   1.84   hannken 	/* ps->ps_failed = 0; */
    243   1.88   hannken 	ps->ps_orig_bp = obp;
    244   1.84   hannken 	ps->ps_endoffset = -1;
    245   1.94   mlelstv 	ps->ps_resid = 0;
    246   1.84   hannken 	mutex_init(&ps->ps_lock, MUTEX_DEFAULT, IPL_NONE);
    247   1.84   hannken 	cv_init(&ps->ps_cv, "physio");
    248   1.84   hannken 
    249   1.94   mlelstv 	/* Allow concurrent I/O only for disks */
    250   1.94   mlelstv 	isdisk = cdev_type(dev) == D_DISK;
    251   1.94   mlelstv 	if (!isdisk)
    252   1.94   mlelstv 		concurrency = 0;
    253   1.94   mlelstv 
    254   1.20       cgd 	/* Make sure we have a buffer, creating one if necessary. */
    255   1.62      yamt 	if (obp != NULL) {
    256   1.86        ad 		mutex_enter(&bufcache_lock);
    257   1.88   hannken 		/* Mark it busy, so nobody else will use it. */
    258   1.87        ad 		while (bbusy(obp, false, 0, NULL) == EPASSTHROUGH)
    259   1.86        ad 			;
    260   1.86        ad 		mutex_exit(&bufcache_lock);
    261   1.65      yamt 		concurrency = 0; /* see "XXXkludge" comment below */
    262   1.29       mrg 	}
    263   1.20       cgd 
    264   1.20       cgd 	for (i = 0; i < uio->uio_iovcnt; i++) {
    265   1.79   thorpej 		bool sync = true;
    266   1.68      yamt 
    267   1.20       cgd 		iovp = &uio->uio_iov[i];
    268   1.20       cgd 		while (iovp->iov_len > 0) {
    269   1.68      yamt 			size_t todo;
    270   1.69      yamt 			vaddr_t endp;
    271   1.68      yamt 
    272   1.84   hannken 			mutex_enter(&ps->ps_lock);
    273   1.84   hannken 			if (ps->ps_failed != 0) {
    274   1.62      yamt 				goto done_locked;
    275   1.62      yamt 			}
    276   1.84   hannken 			physio_wait(ps, sync ? 0 : concurrency);
    277   1.84   hannken 			mutex_exit(&ps->ps_lock);
    278   1.65      yamt 			if (obp != NULL) {
    279   1.65      yamt 				/*
    280   1.65      yamt 				 * XXXkludge
    281   1.65      yamt 				 * some drivers use "obp" as an identifier.
    282   1.65      yamt 				 */
    283   1.65      yamt 				bp = obp;
    284   1.65      yamt 			} else {
    285   1.88   hannken 				bp = getiobuf(NULL, true);
    286   1.98        ad 				bp->b_cflags |= BC_BUSY;
    287   1.65      yamt 			}
    288   1.62      yamt 			bp->b_dev = dev;
    289   1.62      yamt 			bp->b_proc = p;
    290   1.84   hannken 			bp->b_private = ps;
    291   1.47       chs 
    292   1.20       cgd 			/*
    293   1.90        ad 			 * Mrk the buffer busy for physical I/O.  Also set
    294   1.90        ad 			 * B_PHYS because it's an I/O to user memory, and
    295   1.90        ad 			 * B_RAW because B_RAW is to be "set by physio for
    296   1.90        ad 			 * raw transfers".
    297   1.20       cgd 			 */
    298   1.86        ad 			bp->b_oflags = 0;
    299   1.98        ad 			bp->b_cflags |= BC_BUSY;
    300   1.86        ad 			bp->b_flags = flags | B_PHYS | B_RAW;
    301   1.62      yamt 			bp->b_iodone = physio_biodone;
    302   1.20       cgd 
    303   1.90        ad 			/* Set up the buffer for a maximum-sized transfer. */
    304   1.20       cgd 			bp->b_blkno = btodb(uio->uio_offset);
    305   1.94   mlelstv 			if (isdisk) {
    306   1.94   mlelstv 				/*
    307   1.94   mlelstv 				 * For disks, check that offsets are at least block
    308   1.94   mlelstv 				 * aligned, the block addresses are used to track
    309   1.94   mlelstv 				 * errors of finished requests.
    310   1.94   mlelstv 				 */
    311  1.102  riastrad 				if (uio->uio_offset & (DEV_BSIZE - 1)) {
    312   1.94   mlelstv 					error = EINVAL;
    313   1.94   mlelstv 					goto done;
    314   1.94   mlelstv 				}
    315   1.94   mlelstv 				/*
    316   1.94   mlelstv 				 * Split request into MAXPHYS chunks
    317   1.94   mlelstv 				 */
    318   1.94   mlelstv 				bp->b_bcount = MIN(MAXPHYS, iovp->iov_len);
    319   1.94   mlelstv 			} else {
    320  1.100  riastrad 				bp->b_bcount = MIN(INT_MAX, iovp->iov_len);
    321   1.62      yamt 			}
    322   1.20       cgd 			bp->b_data = iovp->iov_base;
    323   1.24   mycroft 
    324   1.20       cgd 			/*
    325   1.90        ad 			 * Call minphys to bound the transfer size,
    326   1.20       cgd 			 * and remember the amount of data to transfer,
    327   1.20       cgd 			 * for later comparison.
    328   1.20       cgd 			 */
    329   1.59    simonb 			(*min_phys)(bp);
    330   1.69      yamt 			todo = bp->b_bufsize = bp->b_bcount;
    331   1.62      yamt #if defined(DIAGNOSTIC)
    332   1.69      yamt 			if (todo > MAXPHYS)
    333   1.62      yamt 				panic("todo(%zu) > MAXPHYS; minphys broken",
    334   1.69      yamt 				    todo);
    335   1.62      yamt #endif /* defined(DIAGNOSTIC) */
    336   1.20       cgd 
    337   1.79   thorpej 			sync = false;
    338   1.69      yamt 			endp = (vaddr_t)bp->b_data + todo;
    339   1.69      yamt 			if (trunc_page(endp) != endp) {
    340   1.69      yamt 				/*
    341   1.90        ad 				 * Following requests can overlap.
    342   1.69      yamt 				 * note that uvm_vslock does round_page.
    343   1.69      yamt 				 */
    344   1.79   thorpej 				sync = true;
    345   1.68      yamt 			}
    346   1.68      yamt 
    347   1.20       cgd 			/*
    348   1.90        ad 			 * Lock the part of the user address space involved
    349   1.92     pooka 			 * in the transfer.
    350   1.20       cgd 			 */
    351   1.74       chs 			error = uvm_vslock(p->p_vmspace, bp->b_data, todo,
    352   1.62      yamt 			    (flags & B_READ) ?  VM_PROT_WRITE : VM_PROT_READ);
    353   1.47       chs 			if (error) {
    354   1.62      yamt 				goto done;
    355   1.37   thorpej 			}
    356   1.92     pooka 
    357   1.92     pooka 			/*
    358   1.99    andvar 			 * Beware vmapbuf(); if successful it clobbers
    359   1.92     pooka 			 * b_data and saves it in b_saveaddr.
    360   1.92     pooka 			 * However, vunmapbuf() restores b_data.
    361   1.92     pooka 			 */
    362   1.92     pooka 			if ((error = vmapbuf(bp, todo)) != 0) {
    363   1.92     pooka 				uvm_vsunlock(p->p_vmspace, bp->b_data, todo);
    364   1.92     pooka 				goto done;
    365   1.92     pooka 			}
    366   1.20       cgd 
    367   1.58      yamt 			BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
    368   1.58      yamt 
    369   1.84   hannken 			mutex_enter(&ps->ps_lock);
    370   1.84   hannken 			ps->ps_running++;
    371   1.84   hannken 			mutex_exit(&ps->ps_lock);
    372   1.62      yamt 
    373   1.90        ad 			/* Call strategy to start the transfer. */
    374   1.20       cgd 			(*strategy)(bp);
    375   1.62      yamt 			bp = NULL;
    376   1.20       cgd 
    377   1.62      yamt 			iovp->iov_len -= todo;
    378   1.80  christos 			iovp->iov_base = (char *)iovp->iov_base + todo;
    379   1.62      yamt 			uio->uio_offset += todo;
    380   1.62      yamt 			uio->uio_resid -= todo;
    381   1.20       cgd 		}
    382   1.20       cgd 	}
    383   1.20       cgd 
    384   1.20       cgd done:
    385   1.84   hannken 	mutex_enter(&ps->ps_lock);
    386   1.62      yamt done_locked:
    387   1.84   hannken 	physio_wait(ps, 0);
    388   1.84   hannken 	mutex_exit(&ps->ps_lock);
    389   1.66      yamt 
    390   1.94   mlelstv 	KASSERT(ps->ps_failed || ps->ps_endoffset == -1);
    391   1.70      yamt 
    392   1.94   mlelstv 	/*
    393   1.94   mlelstv 	 * Compute residual, for disks adjust for the
    394   1.94   mlelstv 	 * lowest numbered block that returned an error.
    395   1.94   mlelstv 	 */
    396   1.94   mlelstv 	if (isdisk) {
    397   1.94   mlelstv 		if (ps->ps_failed != 0) {
    398   1.94   mlelstv 			off_t delta;
    399   1.94   mlelstv 
    400   1.94   mlelstv 			delta = uio->uio_offset - ps->ps_endoffset;
    401   1.94   mlelstv 			KASSERT(delta > 0);
    402   1.94   mlelstv 			uio->uio_resid += delta;
    403   1.94   mlelstv 			/* uio->uio_offset = ps->ps_endoffset; */
    404   1.94   mlelstv 		}
    405   1.66      yamt 	} else {
    406   1.94   mlelstv 		uio->uio_resid += ps->ps_resid;
    407   1.62      yamt 	}
    408   1.94   mlelstv 
    409   1.88   hannken 	if (bp != NULL && bp != obp) {
    410   1.88   hannken 		putiobuf(bp);
    411   1.62      yamt 	}
    412   1.62      yamt 	if (error == 0) {
    413   1.84   hannken 		error = ps->ps_error;
    414   1.62      yamt 	}
    415   1.84   hannken 	mutex_destroy(&ps->ps_lock);
    416   1.84   hannken 	cv_destroy(&ps->ps_cv);
    417   1.84   hannken 	kmem_free(ps, sizeof(*ps));
    418   1.62      yamt 
    419   1.20       cgd 	/*
    420   1.90        ad 	 * Clean up the state of the buffer.  Remember if somebody wants
    421   1.90        ad 	 * it, so we can wake them up below.  Also, if we had to steal it,
    422   1.90        ad 	 * give it back.
    423   1.20       cgd 	 */
    424   1.62      yamt 	if (obp != NULL) {
    425   1.86        ad 		KASSERT((obp->b_cflags & BC_BUSY) != 0);
    426   1.65      yamt 
    427   1.20       cgd 		/*
    428   1.90        ad 		 * If another process is waiting for the raw I/O buffer,
    429   1.90        ad 		 * wake up processes waiting to do physical I/O;
    430   1.20       cgd 		 */
    431   1.86        ad 		mutex_enter(&bufcache_lock);
    432   1.88   hannken 		obp->b_cflags &= ~(BC_BUSY | BC_WANTED);
    433   1.86        ad 		obp->b_flags &= ~(B_PHYS | B_RAW);
    434   1.86        ad 		obp->b_iodone = NULL;
    435   1.97        ad 		cv_broadcast(&obp->b_busy);
    436   1.86        ad 		mutex_exit(&bufcache_lock);
    437   1.20       cgd 	}
    438   1.20       cgd 
    439   1.70      yamt 	DPRINTF(("%s: done: off=%" PRIu64 ", resid=%zu\n",
    440   1.70      yamt 	    __func__, uio->uio_offset, uio->uio_resid));
    441   1.70      yamt 
    442   1.62      yamt 	return error;
    443   1.20       cgd }
    444   1.20       cgd 
    445   1.20       cgd /*
    446   1.90        ad  * A minphys() routine is called by physio() to adjust the size of each
    447   1.90        ad  * I/O transfer before the latter is passed to the strategy routine.
    448   1.20       cgd  *
    449   1.90        ad  * This minphys() is a default that must be called to enforce limits
    450   1.90        ad  * that are applicable to all devices, because of limitations in the
    451   1.90        ad  * kernel or the hardware platform.
    452   1.20       cgd  */
    453   1.24   mycroft void
    454   1.61   thorpej minphys(struct buf *bp)
    455   1.20       cgd {
    456   1.20       cgd 
    457   1.24   mycroft 	if (bp->b_bcount > MAXPHYS)
    458   1.24   mycroft 		bp->b_bcount = MAXPHYS;
    459   1.20       cgd }
    460