2007-11-06 03:42:39 +03:00
|
|
|
/* $NetBSD: kern_physio.c,v 1.85 2007/11/06 00:42:42 ad Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1982, 1986, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1994 Christopher G. Demetriou
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1994-06-29 10:29:24 +04:00
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
|
|
|
|
*/
|
|
|
|
|
2001-11-12 18:25:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2007-11-06 03:42:39 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_physio.c,v 1.85 2007/11/06 00:42:42 ad Exp $");
|
2001-11-12 18:25:01 +03:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/proc.h>
|
2005-10-29 15:23:19 +04:00
|
|
|
#include <sys/once.h>
|
|
|
|
#include <sys/workqueue.h>
|
2007-09-18 12:34:34 +04:00
|
|
|
#include <sys/kmem.h>
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
2005-10-29 15:23:19 +04:00
|
|
|
ONCE_DECL(physio_initialized);
|
|
|
|
struct workqueue *physio_workqueue;
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* The routines implemented in this file are described in:
|
|
|
|
* Leffler, et al.: The Design and Implementation of the 4.3BSD
|
|
|
|
* UNIX Operating System (Addison Welley, 1989)
|
|
|
|
* on pages 231-233.
|
|
|
|
*
|
|
|
|
* The routines "getphysbuf" and "putphysbuf" steal and return a swap
|
|
|
|
* buffer. Leffler, et al., says that swap buffers are used to do the
|
2005-06-24 03:15:12 +04:00
|
|
|
* I/O, so raw I/O requests don't have to be single-threaded. Of course,
|
|
|
|
* NetBSD doesn't use "swap buffers" -- we have our own memory pool for
|
|
|
|
* buffer descriptors.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
|
2005-12-17 08:26:41 +03:00
|
|
|
/* #define PHYSIO_DEBUG */
|
|
|
|
#if defined(PHYSIO_DEBUG)
|
|
|
|
#define DPRINTF(a) printf a
|
|
|
|
#else /* defined(PHYSIO_DEBUG) */
|
|
|
|
#define DPRINTF(a) /* nothing */
|
|
|
|
#endif /* defined(PHYSIO_DEBUG) */
|
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
struct physio_stat {
|
|
|
|
int ps_running;
|
|
|
|
int ps_error;
|
|
|
|
int ps_failed;
|
|
|
|
off_t ps_endoffset;
|
|
|
|
kmutex_t ps_lock;
|
|
|
|
kcondvar_t ps_cv;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* abuse these flags of struct buf */
|
2005-10-31 17:36:41 +03:00
|
|
|
#define B_DONTFREE B_AGE
|
|
|
|
|
2005-06-24 03:15:12 +04:00
|
|
|
/*
|
|
|
|
* allocate a buffer structure for use in physical I/O.
|
|
|
|
*/
|
|
|
|
static struct buf *
|
|
|
|
getphysbuf(void)
|
|
|
|
{
|
|
|
|
struct buf *bp;
|
|
|
|
|
2006-01-04 13:13:05 +03:00
|
|
|
bp = getiobuf();
|
2005-10-29 15:23:19 +04:00
|
|
|
bp->b_error = 0;
|
2005-10-31 17:36:41 +03:00
|
|
|
bp->b_flags = B_BUSY;
|
2005-06-24 03:15:12 +04:00
|
|
|
return(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get rid of a swap buffer structure which has been used in physical I/O.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
putphysbuf(struct buf *bp)
|
|
|
|
{
|
|
|
|
|
2005-10-31 17:36:41 +03:00
|
|
|
if ((bp->b_flags & B_DONTFREE) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-06-24 03:15:12 +04:00
|
|
|
if (__predict_false(bp->b_flags & B_WANTED))
|
|
|
|
panic("putphysbuf: private buf B_WANTED");
|
2006-01-04 13:13:05 +03:00
|
|
|
putiobuf(bp);
|
2005-06-24 03:15:12 +04:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2005-10-29 15:23:19 +04:00
|
|
|
static void
|
2006-11-01 13:17:58 +03:00
|
|
|
physio_done(struct work *wk, void *dummy)
|
2005-10-29 15:23:19 +04:00
|
|
|
{
|
|
|
|
struct buf *bp = (void *)wk;
|
|
|
|
size_t todo = bp->b_bufsize;
|
2005-11-04 11:39:33 +03:00
|
|
|
size_t done = bp->b_bcount - bp->b_resid;
|
2007-09-18 12:34:34 +04:00
|
|
|
struct physio_stat *ps = bp->b_private;
|
2005-10-29 15:23:19 +04:00
|
|
|
|
|
|
|
KASSERT(&bp->b_work == wk);
|
|
|
|
KASSERT(bp->b_bcount <= todo);
|
|
|
|
KASSERT(bp->b_resid <= bp->b_bcount);
|
|
|
|
KASSERT((bp->b_flags & B_PHYS) != 0);
|
|
|
|
KASSERT(dummy == NULL);
|
|
|
|
|
|
|
|
vunmapbuf(bp, todo);
|
2006-10-05 18:48:32 +04:00
|
|
|
uvm_vsunlock(bp->b_proc->p_vmspace, bp->b_data, todo);
|
2005-10-29 15:23:19 +04:00
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
mutex_enter(&ps->ps_lock);
|
2005-11-04 11:39:33 +03:00
|
|
|
if (__predict_false(done != todo)) {
|
|
|
|
off_t endoffset = dbtob(bp->b_blkno) + done;
|
|
|
|
|
2005-12-17 08:26:41 +03:00
|
|
|
/*
|
|
|
|
* we got an error or hit EOM.
|
|
|
|
*
|
|
|
|
* we only care about the first one.
|
|
|
|
* ie. the one at the lowest offset.
|
|
|
|
*/
|
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
KASSERT(ps->ps_endoffset != endoffset);
|
2005-12-17 08:26:41 +03:00
|
|
|
DPRINTF(("%s: error=%d at %" PRIu64 " - %" PRIu64
|
|
|
|
", blkno=%" PRIu64 ", bcount=%d, flags=0x%x\n",
|
|
|
|
__func__, bp->b_error, dbtob(bp->b_blkno), endoffset,
|
|
|
|
bp->b_blkno, bp->b_bcount, bp->b_flags));
|
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
if (ps->ps_endoffset == -1 || endoffset < ps->ps_endoffset) {
|
|
|
|
DPRINTF(("%s: ps=%p, error %d -> %d, endoff %" PRIu64
|
2005-12-17 08:26:41 +03:00
|
|
|
" -> %" PRIu64 "\n",
|
2007-09-18 12:34:34 +04:00
|
|
|
__func__, ps,
|
|
|
|
ps->ps_error, bp->b_error,
|
|
|
|
ps->ps_endoffset, endoffset));
|
2005-11-04 11:39:33 +03:00
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
ps->ps_endoffset = endoffset;
|
|
|
|
ps->ps_error = bp->b_error;
|
2005-10-29 15:23:19 +04:00
|
|
|
}
|
2007-09-18 12:34:34 +04:00
|
|
|
ps->ps_failed++;
|
2005-12-17 08:26:41 +03:00
|
|
|
} else {
|
2007-07-29 16:15:35 +04:00
|
|
|
KASSERT(bp->b_error == 0);
|
2005-10-29 15:23:19 +04:00
|
|
|
}
|
2005-12-05 02:34:00 +03:00
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
ps->ps_running--;
|
|
|
|
cv_signal(&ps->ps_cv);
|
|
|
|
mutex_exit(&ps->ps_lock);
|
2005-10-29 15:23:19 +04:00
|
|
|
|
|
|
|
putphysbuf(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
physio_biodone(struct buf *bp)
|
|
|
|
{
|
2005-10-29 15:49:01 +04:00
|
|
|
#if defined(DIAGNOSTIC)
|
2007-09-18 12:34:34 +04:00
|
|
|
struct physio_stat *ps = bp->b_private;
|
2005-10-29 15:23:19 +04:00
|
|
|
size_t todo = bp->b_bufsize;
|
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
KASSERT(ps->ps_running > 0);
|
2005-10-29 15:23:19 +04:00
|
|
|
KASSERT(bp->b_bcount <= todo);
|
|
|
|
KASSERT(bp->b_resid <= bp->b_bcount);
|
2005-10-29 15:49:01 +04:00
|
|
|
#endif /* defined(DIAGNOSTIC) */
|
2005-10-29 15:23:19 +04:00
|
|
|
|
2007-07-13 00:39:56 +04:00
|
|
|
workqueue_enqueue(physio_workqueue, &bp->b_work, NULL);
|
2005-10-29 15:23:19 +04:00
|
|
|
}
|
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
static void
|
|
|
|
physio_wait(struct physio_stat *ps, int n)
|
2005-10-29 15:23:19 +04:00
|
|
|
{
|
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
KASSERT(mutex_owned(&ps->ps_lock));
|
2005-10-29 15:23:19 +04:00
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
while (ps->ps_running > n)
|
|
|
|
cv_wait(&ps->ps_cv, &ps->ps_lock);
|
2005-10-29 15:23:19 +04:00
|
|
|
}
|
|
|
|
|
2006-01-17 00:45:38 +03:00
|
|
|
static int
|
2005-10-29 15:23:19 +04:00
|
|
|
physio_init(void)
|
|
|
|
{
|
2006-01-17 00:45:38 +03:00
|
|
|
int error;
|
2005-10-29 15:23:19 +04:00
|
|
|
|
|
|
|
KASSERT(physio_workqueue == NULL);
|
|
|
|
|
2006-01-17 00:45:38 +03:00
|
|
|
error = workqueue_create(&physio_workqueue, "physiod",
|
2007-11-06 03:42:39 +03:00
|
|
|
physio_done, NULL, PRI_BIO, IPL_BIO, 0);
|
2006-01-17 00:45:38 +03:00
|
|
|
|
|
|
|
return error;
|
2005-10-29 15:23:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PHYSIO_CONCURRENCY 16 /* XXX tune */
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* Do "physical I/O" on behalf of a user. "Physical I/O" is I/O directly
|
|
|
|
* from the raw device to user buffers, and bypasses the buffer cache.
|
|
|
|
*
|
|
|
|
* Comments in brackets are from Leffler, et al.'s pseudo-code implementation.
|
|
|
|
*/
|
|
|
|
int
|
2005-10-29 15:23:19 +04:00
|
|
|
physio(void (*strategy)(struct buf *), struct buf *obp, dev_t dev, int flags,
|
2005-06-24 03:15:12 +04:00
|
|
|
void (*min_phys)(struct buf *), struct uio *uio)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
|
|
|
struct iovec *iovp;
|
2003-01-18 13:06:22 +03:00
|
|
|
struct lwp *l = curlwp;
|
|
|
|
struct proc *p = l->l_proc;
|
2005-10-29 15:23:19 +04:00
|
|
|
int i, s;
|
2006-01-17 00:45:38 +03:00
|
|
|
int error;
|
2005-10-29 15:23:19 +04:00
|
|
|
struct buf *bp = NULL;
|
2007-09-18 12:34:34 +04:00
|
|
|
struct physio_stat *ps;
|
2005-10-31 17:36:41 +03:00
|
|
|
int concurrency = PHYSIO_CONCURRENCY - 1;
|
2005-10-29 15:23:19 +04:00
|
|
|
|
2006-01-17 00:45:38 +03:00
|
|
|
error = RUN_ONCE(&physio_initialized, physio_init);
|
|
|
|
if (__predict_false(error != 0)) {
|
|
|
|
return error;
|
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2005-12-17 08:26:41 +03:00
|
|
|
DPRINTF(("%s: called: off=%" PRIu64 ", resid=%zu\n",
|
|
|
|
__func__, uio->uio_offset, uio->uio_resid));
|
|
|
|
|
2002-08-30 19:43:36 +04:00
|
|
|
flags &= B_READ | B_WRITE;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
if ((ps = kmem_zalloc(sizeof(*ps), KM_SLEEP)) == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
/* ps->ps_running = 0; */
|
|
|
|
/* ps->ps_error = 0; */
|
|
|
|
/* ps->ps_failed = 0; */
|
|
|
|
ps->ps_endoffset = -1;
|
|
|
|
mutex_init(&ps->ps_lock, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
cv_init(&ps->ps_cv, "physio");
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/* Make sure we have a buffer, creating one if necessary. */
|
2005-10-29 15:23:19 +04:00
|
|
|
if (obp != NULL) {
|
1998-02-05 10:59:28 +03:00
|
|
|
/* [raise the processor priority level to splbio;] */
|
|
|
|
s = splbio();
|
2005-10-31 17:36:41 +03:00
|
|
|
simple_lock(&obp->b_interlock);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
/* [while the buffer is marked busy] */
|
2005-10-29 15:23:19 +04:00
|
|
|
while (obp->b_flags & B_BUSY) {
|
1998-02-05 10:59:28 +03:00
|
|
|
/* [mark the buffer wanted] */
|
2005-10-29 15:23:19 +04:00
|
|
|
obp->b_flags |= B_WANTED;
|
1998-02-05 10:59:28 +03:00
|
|
|
/* [wait until the buffer is available] */
|
2006-04-18 13:54:32 +04:00
|
|
|
ltsleep(obp, PRIBIO+1, "physbuf", 0, &obp->b_interlock);
|
1998-02-05 10:59:28 +03:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
/* Mark it busy, so nobody else will use it. */
|
2005-10-31 17:36:41 +03:00
|
|
|
obp->b_flags = B_BUSY | B_DONTFREE;
|
1998-02-05 10:59:28 +03:00
|
|
|
|
|
|
|
/* [lower the priority level] */
|
2005-10-31 17:36:41 +03:00
|
|
|
simple_unlock(&obp->b_interlock);
|
1998-02-05 10:59:28 +03:00
|
|
|
splx(s);
|
2005-10-31 17:36:41 +03:00
|
|
|
|
|
|
|
concurrency = 0; /* see "XXXkludge" comment below */
|
1998-02-05 10:59:28 +03:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2007-07-10 00:51:58 +04:00
|
|
|
uvm_lwp_hold(l);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
for (i = 0; i < uio->uio_iovcnt; i++) {
|
2007-02-22 09:34:42 +03:00
|
|
|
bool sync = true;
|
2005-12-13 15:29:32 +03:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
iovp = &uio->uio_iov[i];
|
|
|
|
while (iovp->iov_len > 0) {
|
2005-12-13 15:29:32 +03:00
|
|
|
size_t todo;
|
2005-12-14 04:58:01 +03:00
|
|
|
vaddr_t endp;
|
2005-12-13 15:29:32 +03:00
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
mutex_enter(&ps->ps_lock);
|
|
|
|
if (ps->ps_failed != 0) {
|
2005-10-29 15:23:19 +04:00
|
|
|
goto done_locked;
|
|
|
|
}
|
2007-09-18 12:34:34 +04:00
|
|
|
physio_wait(ps, sync ? 0 : concurrency);
|
|
|
|
mutex_exit(&ps->ps_lock);
|
2005-10-31 17:36:41 +03:00
|
|
|
if (obp != NULL) {
|
|
|
|
/*
|
|
|
|
* XXXkludge
|
|
|
|
* some drivers use "obp" as an identifier.
|
|
|
|
*/
|
|
|
|
bp = obp;
|
|
|
|
} else {
|
|
|
|
bp = getphysbuf();
|
|
|
|
}
|
2005-10-29 15:23:19 +04:00
|
|
|
bp->b_dev = dev;
|
|
|
|
bp->b_proc = p;
|
2007-09-18 12:34:34 +04:00
|
|
|
bp->b_private = ps;
|
2005-10-29 15:23:19 +04:00
|
|
|
bp->b_vp = NULL;
|
2001-03-15 09:10:32 +03:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* [mark the buffer busy for physical I/O]
|
|
|
|
* (i.e. set B_PHYS (because it's an I/O to user
|
|
|
|
* memory, and B_RAW, because B_RAW is to be
|
|
|
|
* "Set by physio for raw transfers.", in addition
|
|
|
|
* to the "busy" and read/write flag.)
|
|
|
|
*/
|
2005-10-31 17:36:41 +03:00
|
|
|
bp->b_flags = (bp->b_flags & B_DONTFREE) |
|
|
|
|
B_BUSY | B_PHYS | B_RAW | B_CALL | flags;
|
2005-10-29 15:23:19 +04:00
|
|
|
bp->b_iodone = physio_biodone;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/* [set up the buffer for a maximum-sized transfer] */
|
|
|
|
bp->b_blkno = btodb(uio->uio_offset);
|
2005-10-29 15:23:19 +04:00
|
|
|
if (dbtob(bp->b_blkno) != uio->uio_offset) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
bp->b_bcount = MIN(MAXPHYS, iovp->iov_len);
|
1994-06-29 10:29:24 +04:00
|
|
|
bp->b_data = iovp->iov_base;
|
1995-08-13 00:30:45 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
2001-07-08 22:06:42 +04:00
|
|
|
* [call minphys to bound the transfer size]
|
1994-06-29 10:29:24 +04:00
|
|
|
* and remember the amount of data to transfer,
|
|
|
|
* for later comparison.
|
|
|
|
*/
|
2004-02-17 14:36:01 +03:00
|
|
|
(*min_phys)(bp);
|
2005-12-14 04:58:01 +03:00
|
|
|
todo = bp->b_bufsize = bp->b_bcount;
|
2005-10-29 15:23:19 +04:00
|
|
|
#if defined(DIAGNOSTIC)
|
2005-12-14 04:58:01 +03:00
|
|
|
if (todo > MAXPHYS)
|
2005-10-29 15:23:19 +04:00
|
|
|
panic("todo(%zu) > MAXPHYS; minphys broken",
|
2005-12-14 04:58:01 +03:00
|
|
|
todo);
|
2005-10-29 15:23:19 +04:00
|
|
|
#endif /* defined(DIAGNOSTIC) */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2007-02-22 09:34:42 +03:00
|
|
|
sync = false;
|
2005-12-14 04:58:01 +03:00
|
|
|
endp = (vaddr_t)bp->b_data + todo;
|
|
|
|
if (trunc_page(endp) != endp) {
|
|
|
|
/*
|
|
|
|
* following requests can overlap.
|
|
|
|
* note that uvm_vslock does round_page.
|
|
|
|
*/
|
2007-02-22 09:34:42 +03:00
|
|
|
sync = true;
|
2005-12-13 15:29:32 +03:00
|
|
|
}
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* [lock the part of the user address space involved
|
|
|
|
* in the transfer]
|
|
|
|
* Beware vmapbuf(); it clobbers b_data and
|
|
|
|
* saves it in b_saveaddr. However, vunmapbuf()
|
|
|
|
* restores it.
|
|
|
|
*/
|
2006-10-05 18:48:32 +04:00
|
|
|
error = uvm_vslock(p->p_vmspace, bp->b_data, todo,
|
2005-10-29 15:23:19 +04:00
|
|
|
(flags & B_READ) ? VM_PROT_WRITE : VM_PROT_READ);
|
2001-03-15 09:10:32 +03:00
|
|
|
if (error) {
|
2005-10-29 15:23:19 +04:00
|
|
|
goto done;
|
1999-06-17 19:47:22 +04:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
vmapbuf(bp, todo);
|
|
|
|
|
2004-01-10 17:39:50 +03:00
|
|
|
BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
|
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
mutex_enter(&ps->ps_lock);
|
|
|
|
ps->ps_running++;
|
|
|
|
mutex_exit(&ps->ps_lock);
|
2005-10-29 15:23:19 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/* [call strategy to start the transfer] */
|
|
|
|
(*strategy)(bp);
|
2005-10-29 15:23:19 +04:00
|
|
|
bp = NULL;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2005-10-29 15:23:19 +04:00
|
|
|
iovp->iov_len -= todo;
|
2007-03-04 08:59:00 +03:00
|
|
|
iovp->iov_base = (char *)iovp->iov_base + todo;
|
2005-10-29 15:23:19 +04:00
|
|
|
uio->uio_offset += todo;
|
|
|
|
uio->uio_resid -= todo;
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2007-09-18 12:34:34 +04:00
|
|
|
mutex_enter(&ps->ps_lock);
|
2005-10-29 15:23:19 +04:00
|
|
|
done_locked:
|
2007-09-18 12:34:34 +04:00
|
|
|
physio_wait(ps, 0);
|
|
|
|
mutex_exit(&ps->ps_lock);
|
2005-11-04 11:39:33 +03:00
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
if (ps->ps_failed != 0) {
|
2005-12-17 08:26:41 +03:00
|
|
|
off_t delta;
|
|
|
|
|
2007-09-18 12:34:34 +04:00
|
|
|
delta = uio->uio_offset - ps->ps_endoffset;
|
2005-12-17 08:26:41 +03:00
|
|
|
KASSERT(delta > 0);
|
|
|
|
uio->uio_resid += delta;
|
2007-09-18 12:34:34 +04:00
|
|
|
/* uio->uio_offset = ps->ps_endoffset; */
|
2005-11-04 11:39:33 +03:00
|
|
|
} else {
|
2007-09-18 12:34:34 +04:00
|
|
|
KASSERT(ps->ps_endoffset == -1);
|
2005-10-29 15:23:19 +04:00
|
|
|
}
|
|
|
|
if (bp != NULL) {
|
|
|
|
putphysbuf(bp);
|
|
|
|
}
|
|
|
|
if (error == 0) {
|
2007-09-18 12:34:34 +04:00
|
|
|
error = ps->ps_error;
|
2005-10-29 15:23:19 +04:00
|
|
|
}
|
2007-09-18 12:34:34 +04:00
|
|
|
mutex_destroy(&ps->ps_lock);
|
|
|
|
cv_destroy(&ps->ps_cv);
|
|
|
|
kmem_free(ps, sizeof(*ps));
|
2005-10-29 15:23:19 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* [clean up the state of the buffer]
|
|
|
|
* Remember if somebody wants it, so we can wake them up below.
|
|
|
|
* Also, if we had to steal it, give it back.
|
|
|
|
*/
|
2005-10-29 15:23:19 +04:00
|
|
|
if (obp != NULL) {
|
2005-10-31 17:36:41 +03:00
|
|
|
KASSERT((obp->b_flags & B_BUSY) != 0);
|
|
|
|
KASSERT((obp->b_flags & B_DONTFREE) != 0);
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* [if another process is waiting for the raw I/O buffer,
|
|
|
|
* wake up processes waiting to do physical I/O;
|
|
|
|
*/
|
2005-10-31 17:36:41 +03:00
|
|
|
s = splbio();
|
|
|
|
simple_lock(&obp->b_interlock);
|
|
|
|
obp->b_flags &=
|
|
|
|
~(B_BUSY | B_PHYS | B_RAW | B_CALL | B_DONTFREE);
|
|
|
|
if ((obp->b_flags & B_WANTED) != 0) {
|
2005-10-29 15:23:19 +04:00
|
|
|
obp->b_flags &= ~B_WANTED;
|
|
|
|
wakeup(obp);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
2005-10-31 17:36:41 +03:00
|
|
|
simple_unlock(&obp->b_interlock);
|
2005-10-29 15:23:19 +04:00
|
|
|
splx(s);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
2007-07-10 00:51:58 +04:00
|
|
|
uvm_lwp_rele(l);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2005-12-17 08:26:41 +03:00
|
|
|
DPRINTF(("%s: done: off=%" PRIu64 ", resid=%zu\n",
|
|
|
|
__func__, uio->uio_offset, uio->uio_resid));
|
|
|
|
|
2005-10-29 15:23:19 +04:00
|
|
|
return error;
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Leffler, et al., says on p. 231:
|
|
|
|
* "The minphys() routine is called by physio() to adjust the
|
|
|
|
* size of each I/O transfer before the latter is passed to
|
1995-08-13 00:30:45 +04:00
|
|
|
* the strategy routine..."
|
1994-06-29 10:29:24 +04:00
|
|
|
*
|
|
|
|
* so, just adjust the buffer's count accounting to MAXPHYS here,
|
|
|
|
* and return the new count;
|
|
|
|
*/
|
1995-08-13 00:30:45 +04:00
|
|
|
void
|
2005-06-24 03:15:12 +04:00
|
|
|
minphys(struct buf *bp)
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
|
|
|
|
1995-08-13 00:30:45 +04:00
|
|
|
if (bp->b_bcount > MAXPHYS)
|
|
|
|
bp->b_bcount = MAXPHYS;
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|