2003-02-25 23:35:31 +03:00
|
|
|
/* $NetBSD: kern_physio.c,v 1.56 2003/02/25 20:35:38 thorpej Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1994 Christopher G. Demetriou
|
|
|
|
* 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.
|
|
|
|
* 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>
|
2003-02-25 23:35:31 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_physio.c,v 1.56 2003/02/25 20:35:38 thorpej 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>
|
1998-02-05 10:59:28 +03:00
|
|
|
#include <sys/malloc.h>
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <sys/proc.h>
|
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
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
|
|
|
|
* I/O, so raw I/O requests don't have to be single-threaded.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct buf *getphysbuf __P((void));
|
|
|
|
void putphysbuf __P((struct buf *bp));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
physio(strategy, bp, dev, flags, minphys, uio)
|
1995-08-13 00:30:45 +04:00
|
|
|
void (*strategy) __P((struct buf *));
|
1994-06-29 10:29:24 +04:00
|
|
|
struct buf *bp;
|
|
|
|
dev_t dev;
|
|
|
|
int flags;
|
1995-08-13 00:30:45 +04:00
|
|
|
void (*minphys) __P((struct buf *));
|
1994-06-29 10:29:24 +04:00
|
|
|
struct uio *uio;
|
|
|
|
{
|
|
|
|
struct iovec *iovp;
|
2003-01-18 13:06:22 +03:00
|
|
|
struct lwp *l = curlwp;
|
|
|
|
struct proc *p = l->l_proc;
|
2000-12-08 05:25:50 +03:00
|
|
|
int error, done, i, nobuf, s;
|
|
|
|
long todo;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
error = 0;
|
2002-08-30 19:43:36 +04:00
|
|
|
flags &= B_READ | B_WRITE;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/* Make sure we have a buffer, creating one if necessary. */
|
1998-02-05 10:59:28 +03:00
|
|
|
if ((nobuf = (bp == NULL)) != 0) {
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
bp = getphysbuf();
|
1998-02-05 10:59:28 +03:00
|
|
|
/* bp was just malloc'd so can't already be busy */
|
|
|
|
bp->b_flags |= B_BUSY;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
} else {
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
/* [raise the processor priority level to splbio;] */
|
|
|
|
s = splbio();
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
/* [while the buffer is marked busy] */
|
|
|
|
while (bp->b_flags & B_BUSY) {
|
|
|
|
/* [mark the buffer wanted] */
|
|
|
|
bp->b_flags |= B_WANTED;
|
|
|
|
/* [wait until the buffer is available] */
|
|
|
|
tsleep((caddr_t)bp, PRIBIO+1, "physbuf", 0);
|
|
|
|
}
|
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. */
|
|
|
|
bp->b_flags |= B_BUSY;
|
|
|
|
|
|
|
|
/* [lower the priority level] */
|
|
|
|
splx(s);
|
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/* [set up the fixed part of the buffer for a transfer] */
|
|
|
|
bp->b_dev = dev;
|
|
|
|
bp->b_error = 0;
|
|
|
|
bp->b_proc = p;
|
1999-11-15 21:49:07 +03:00
|
|
|
LIST_INIT(&bp->b_dep);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* [while there are data to transfer and no I/O error]
|
|
|
|
* Note that I/O errors are handled with a 'goto' at the bottom
|
|
|
|
* of the 'while' loop.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < uio->uio_iovcnt; i++) {
|
|
|
|
iovp = &uio->uio_iov[i];
|
|
|
|
while (iovp->iov_len > 0) {
|
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.)
|
|
|
|
*/
|
|
|
|
bp->b_flags = B_BUSY | B_PHYS | B_RAW | flags;
|
|
|
|
|
|
|
|
/* [set up the buffer for a maximum-sized transfer] */
|
|
|
|
bp->b_blkno = btodb(uio->uio_offset);
|
|
|
|
bp->b_bcount = iovp->iov_len;
|
|
|
|
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.
|
|
|
|
*/
|
1995-08-13 00:30:45 +04:00
|
|
|
(*minphys)(bp);
|
|
|
|
todo = bp->b_bcount;
|
1995-07-24 11:45:24 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
2000-09-29 17:27:12 +04:00
|
|
|
if (todo <= 0)
|
2000-12-08 05:25:50 +03:00
|
|
|
panic("todo(%ld) <= 0; minphys broken", todo);
|
1995-07-24 11:45:24 +04:00
|
|
|
if (todo > MAXPHYS)
|
2000-12-08 05:25:50 +03:00
|
|
|
panic("todo(%ld) > MAXPHYS; minphys broken",
|
|
|
|
todo);
|
1995-07-24 11:45:24 +04:00
|
|
|
#endif
|
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.
|
|
|
|
*/
|
2003-01-18 13:06:22 +03:00
|
|
|
PHOLD(l);
|
2001-03-15 09:10:32 +03:00
|
|
|
error = uvm_vslock(p, bp->b_data, todo,
|
|
|
|
(flags & B_READ) ?
|
2002-02-14 10:08:02 +03:00
|
|
|
VM_PROT_WRITE : VM_PROT_READ);
|
2001-03-15 09:10:32 +03:00
|
|
|
if (error) {
|
1999-06-17 19:47:22 +04:00
|
|
|
bp->b_flags |= B_ERROR;
|
2001-03-15 09:10:32 +03:00
|
|
|
bp->b_error = error;
|
1999-06-17 19:47:22 +04:00
|
|
|
goto after_vsunlock;
|
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
vmapbuf(bp, todo);
|
|
|
|
|
|
|
|
/* [call strategy to start the transfer] */
|
|
|
|
(*strategy)(bp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that the raise/wait/lower/get error
|
|
|
|
* steps below would be done by biowait(), but
|
|
|
|
* we want to unlock the address space before
|
|
|
|
* we lower the priority.
|
|
|
|
*
|
|
|
|
* [raise the priority level to splbio]
|
|
|
|
*/
|
|
|
|
s = splbio();
|
|
|
|
|
|
|
|
/* [wait for the transfer to complete] */
|
|
|
|
while ((bp->b_flags & B_DONE) == 0)
|
|
|
|
tsleep((caddr_t) bp, PRIBIO + 1, "physio", 0);
|
|
|
|
|
1995-07-27 06:37:12 +04:00
|
|
|
/* Mark it busy again, so nobody else will use it. */
|
|
|
|
bp->b_flags |= B_BUSY;
|
|
|
|
|
|
|
|
/* [lower the priority level] */
|
|
|
|
splx(s);
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/*
|
|
|
|
* [unlock the part of the address space previously
|
|
|
|
* locked]
|
|
|
|
*/
|
|
|
|
vunmapbuf(bp, todo);
|
1998-04-30 10:28:57 +04:00
|
|
|
uvm_vsunlock(p, bp->b_data, todo);
|
1999-06-17 19:47:22 +04:00
|
|
|
after_vsunlock:
|
2003-01-18 13:06:22 +03:00
|
|
|
PRELE(l);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/* remember error value (save a splbio/splx pair) */
|
|
|
|
if (bp->b_flags & B_ERROR)
|
|
|
|
error = (bp->b_error ? bp->b_error : EIO);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* [deduct the transfer size from the total number
|
|
|
|
* of data to transfer]
|
|
|
|
*/
|
|
|
|
done = bp->b_bcount - bp->b_resid;
|
2001-03-15 09:10:32 +03:00
|
|
|
KASSERT(done >= 0);
|
|
|
|
KASSERT(done <= todo);
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
iovp->iov_len -= done;
|
1999-02-10 20:03:26 +03:00
|
|
|
iovp->iov_base = (caddr_t)iovp->iov_base + done;
|
1998-05-08 22:18:55 +04:00
|
|
|
uio->uio_offset += done;
|
|
|
|
uio->uio_resid -= done;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now, check for an error.
|
|
|
|
* Also, handle weird end-of-disk semantics.
|
|
|
|
*/
|
|
|
|
if (error || done < todo)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
/*
|
|
|
|
* [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.
|
|
|
|
*/
|
|
|
|
s = splbio();
|
|
|
|
bp->b_flags &= ~(B_BUSY | B_PHYS | B_RAW);
|
|
|
|
if (nobuf)
|
|
|
|
putphysbuf(bp);
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* [if another process is waiting for the raw I/O buffer,
|
|
|
|
* wake up processes waiting to do physical I/O;
|
|
|
|
*/
|
|
|
|
if (bp->b_flags & B_WANTED) {
|
|
|
|
bp->b_flags &= ~B_WANTED;
|
|
|
|
wakeup(bp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-02-05 10:59:28 +03:00
|
|
|
* allocate a buffer structure for use in physical I/O.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
struct buf *
|
|
|
|
getphysbuf()
|
|
|
|
{
|
|
|
|
struct buf *bp;
|
2000-02-14 23:12:02 +03:00
|
|
|
int s;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2000-02-14 23:12:02 +03:00
|
|
|
s = splbio();
|
|
|
|
bp = pool_get(&bufpool, PR_WAITOK);
|
|
|
|
splx(s);
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memset(bp, 0, sizeof(*bp));
|
2003-02-25 23:35:31 +03:00
|
|
|
BUF_INIT(bp);
|
1998-02-05 10:59:28 +03:00
|
|
|
return(bp);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-02-05 10:59:28 +03:00
|
|
|
* get rid of a swap buffer structure which has been used in physical I/O.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
putphysbuf(bp)
|
1998-02-05 10:59:28 +03:00
|
|
|
struct buf *bp;
|
1994-06-29 10:29:24 +04:00
|
|
|
{
|
2000-02-14 23:12:02 +03:00
|
|
|
int s;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2000-05-09 00:03:20 +04:00
|
|
|
if (__predict_false(bp->b_flags & B_WANTED))
|
1998-02-05 10:59:28 +03:00
|
|
|
panic("putphysbuf: private buf B_WANTED");
|
2000-02-14 23:12:02 +03:00
|
|
|
s = splbio();
|
|
|
|
pool_put(&bufpool, bp);
|
|
|
|
splx(s);
|
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
|
1994-06-29 10:29:24 +04:00
|
|
|
minphys(bp)
|
|
|
|
struct buf *bp;
|
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|