2002-03-11 21:42:35 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2002 Poul-Henning Kamp
|
|
|
|
* Copyright (c) 2002 Networks Associates Technology, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed for the FreeBSD Project by Poul-Henning Kamp
|
|
|
|
* and NAI Labs, the Security Research Division of Network Associates, Inc.
|
|
|
|
* under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
|
|
|
|
* DARPA CHATS research program.
|
|
|
|
*
|
|
|
|
* 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. The names of the authors may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2002-10-20 08:45:17 +00:00
|
|
|
#include <sys/stdint.h>
|
2002-03-11 21:42:35 +00:00
|
|
|
#ifndef _KERNEL
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#else
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/bio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <geom/geom.h>
|
2002-03-26 22:07:38 +00:00
|
|
|
#include <geom/geom_int.h>
|
2003-02-08 13:03:57 +00:00
|
|
|
#include <geom/geom_stats.h>
|
2002-03-11 21:42:35 +00:00
|
|
|
|
|
|
|
static struct g_bioq g_bio_run_down;
|
|
|
|
static struct g_bioq g_bio_run_up;
|
2003-02-11 22:30:26 +00:00
|
|
|
static struct g_bioq g_bio_run_task;
|
2002-03-11 21:42:35 +00:00
|
|
|
static struct g_bioq g_bio_idle;
|
|
|
|
|
2002-11-02 11:08:07 +00:00
|
|
|
static u_int pace;
|
|
|
|
|
2002-03-11 21:42:35 +00:00
|
|
|
#include <machine/atomic.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_bioq_lock(struct g_bioq *bq)
|
|
|
|
{
|
|
|
|
|
|
|
|
mtx_lock(&bq->bio_queue_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_bioq_unlock(struct g_bioq *bq)
|
|
|
|
{
|
|
|
|
|
|
|
|
mtx_unlock(&bq->bio_queue_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void
|
|
|
|
g_bioq_destroy(struct g_bioq *bq)
|
|
|
|
{
|
|
|
|
|
|
|
|
mtx_destroy(&bq->bio_queue_lock);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_bioq_init(struct g_bioq *bq)
|
|
|
|
{
|
|
|
|
|
|
|
|
TAILQ_INIT(&bq->bio_queue);
|
2002-04-04 21:03:38 +00:00
|
|
|
mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct bio *
|
|
|
|
g_bioq_first(struct g_bioq *bq)
|
|
|
|
{
|
|
|
|
struct bio *bp;
|
|
|
|
|
|
|
|
bp = TAILQ_FIRST(&bq->bio_queue);
|
|
|
|
if (bp != NULL) {
|
|
|
|
TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
|
|
|
|
bq->bio_queue_length--;
|
|
|
|
}
|
|
|
|
return (bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_bioq_enqueue_tail(struct bio *bp, struct g_bioq *rq)
|
|
|
|
{
|
|
|
|
|
|
|
|
g_bioq_lock(rq);
|
|
|
|
TAILQ_INSERT_TAIL(&rq->bio_queue, bp, bio_queue);
|
|
|
|
rq->bio_queue_length++;
|
|
|
|
g_bioq_unlock(rq);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bio *
|
|
|
|
g_new_bio(void)
|
|
|
|
{
|
|
|
|
struct bio *bp;
|
|
|
|
|
2003-02-11 22:30:26 +00:00
|
|
|
g_bioq_lock(&g_bio_idle);
|
2002-03-11 21:42:35 +00:00
|
|
|
bp = g_bioq_first(&g_bio_idle);
|
2003-02-11 22:30:26 +00:00
|
|
|
g_bioq_unlock(&g_bio_idle);
|
2002-03-11 21:42:35 +00:00
|
|
|
if (bp == NULL)
|
2002-09-27 20:53:47 +00:00
|
|
|
bp = g_malloc(sizeof *bp, M_NOWAIT | M_ZERO);
|
2002-10-20 08:45:17 +00:00
|
|
|
/* g_trace(G_T_BIO, "g_new_bio() = %p", bp); */
|
2002-03-11 21:42:35 +00:00
|
|
|
return (bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_destroy_bio(struct bio *bp)
|
|
|
|
{
|
|
|
|
|
2002-10-20 08:45:17 +00:00
|
|
|
/* g_trace(G_T_BIO, "g_destroy_bio(%p)", bp); */
|
2002-03-11 21:42:35 +00:00
|
|
|
bzero(bp, sizeof *bp);
|
|
|
|
g_bioq_enqueue_tail(bp, &g_bio_idle);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bio *
|
|
|
|
g_clone_bio(struct bio *bp)
|
|
|
|
{
|
|
|
|
struct bio *bp2;
|
|
|
|
|
|
|
|
bp2 = g_new_bio();
|
2002-09-27 20:53:47 +00:00
|
|
|
if (bp2 != NULL) {
|
2003-02-07 21:09:51 +00:00
|
|
|
bp2->bio_parent = bp;
|
2002-09-27 20:53:47 +00:00
|
|
|
bp2->bio_cmd = bp->bio_cmd;
|
|
|
|
bp2->bio_length = bp->bio_length;
|
|
|
|
bp2->bio_offset = bp->bio_offset;
|
|
|
|
bp2->bio_data = bp->bio_data;
|
|
|
|
bp2->bio_attribute = bp->bio_attribute;
|
2003-02-07 23:08:24 +00:00
|
|
|
bp->bio_children++;
|
2002-09-27 20:53:47 +00:00
|
|
|
}
|
2002-10-20 08:45:17 +00:00
|
|
|
/* g_trace(G_T_BIO, "g_clone_bio(%p) = %p", bp, bp2); */
|
2002-03-11 21:42:35 +00:00
|
|
|
return(bp2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_io_init()
|
|
|
|
{
|
|
|
|
|
|
|
|
g_bioq_init(&g_bio_run_down);
|
|
|
|
g_bioq_init(&g_bio_run_up);
|
2003-02-11 22:30:26 +00:00
|
|
|
g_bioq_init(&g_bio_run_task);
|
2002-03-11 21:42:35 +00:00
|
|
|
g_bioq_init(&g_bio_idle);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-04-09 15:12:05 +00:00
|
|
|
g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
|
|
|
struct bio *bp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
g_trace(G_T_BIO, "bio_setattr(%s)", attr);
|
2002-09-27 21:12:47 +00:00
|
|
|
bp = g_new_bio();
|
|
|
|
bp->bio_cmd = BIO_SETATTR;
|
|
|
|
bp->bio_done = NULL;
|
|
|
|
bp->bio_attribute = attr;
|
|
|
|
bp->bio_length = len;
|
|
|
|
bp->bio_data = ptr;
|
|
|
|
g_io_request(bp, cp);
|
|
|
|
error = biowait(bp, "gsetattr");
|
|
|
|
g_destroy_bio(bp);
|
2002-03-11 21:42:35 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2002-04-09 15:12:05 +00:00
|
|
|
g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
|
|
|
struct bio *bp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
g_trace(G_T_BIO, "bio_getattr(%s)", attr);
|
2002-10-08 07:03:58 +00:00
|
|
|
bp = g_new_bio();
|
|
|
|
bp->bio_cmd = BIO_GETATTR;
|
|
|
|
bp->bio_done = NULL;
|
|
|
|
bp->bio_attribute = attr;
|
|
|
|
bp->bio_length = *len;
|
|
|
|
bp->bio_data = ptr;
|
|
|
|
g_io_request(bp, cp);
|
|
|
|
error = biowait(bp, "ggetattr");
|
|
|
|
*len = bp->bio_completed;
|
|
|
|
g_destroy_bio(bp);
|
2002-03-11 21:42:35 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2003-02-06 21:01:36 +00:00
|
|
|
static int
|
|
|
|
g_io_check(struct bio *bp)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
2003-02-06 21:01:36 +00:00
|
|
|
struct g_consumer *cp;
|
|
|
|
struct g_provider *pp;
|
2002-03-11 21:42:35 +00:00
|
|
|
|
2003-02-06 21:01:36 +00:00
|
|
|
cp = bp->bio_from;
|
|
|
|
pp = bp->bio_to;
|
2002-03-11 21:42:35 +00:00
|
|
|
|
2003-02-06 21:01:36 +00:00
|
|
|
/* Fail if access counters dont allow the operation */
|
2002-04-04 09:58:20 +00:00
|
|
|
switch(bp->bio_cmd) {
|
|
|
|
case BIO_READ:
|
|
|
|
case BIO_GETATTR:
|
2003-02-06 21:01:36 +00:00
|
|
|
if (cp->acr == 0)
|
|
|
|
return (EPERM);
|
2002-04-04 09:58:20 +00:00
|
|
|
break;
|
|
|
|
case BIO_WRITE:
|
|
|
|
case BIO_DELETE:
|
|
|
|
case BIO_SETATTR:
|
2003-02-06 21:01:36 +00:00
|
|
|
if (cp->acw == 0)
|
|
|
|
return (EPERM);
|
2002-04-04 09:58:20 +00:00
|
|
|
break;
|
|
|
|
default:
|
2003-02-06 21:01:36 +00:00
|
|
|
return (EPERM);
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
2002-04-04 09:58:20 +00:00
|
|
|
/* if provider is marked for error, don't disturb. */
|
2003-02-06 21:01:36 +00:00
|
|
|
if (pp->error)
|
|
|
|
return (pp->error);
|
|
|
|
|
2002-04-04 09:58:20 +00:00
|
|
|
switch(bp->bio_cmd) {
|
|
|
|
case BIO_READ:
|
|
|
|
case BIO_WRITE:
|
|
|
|
case BIO_DELETE:
|
2002-12-18 19:53:59 +00:00
|
|
|
/* Reject I/O not on sector boundary */
|
2003-02-06 21:01:36 +00:00
|
|
|
if (bp->bio_offset % pp->sectorsize)
|
|
|
|
return (EINVAL);
|
2002-12-18 19:53:59 +00:00
|
|
|
/* Reject I/O not integral sector long */
|
2003-02-06 21:01:36 +00:00
|
|
|
if (bp->bio_length % pp->sectorsize)
|
|
|
|
return (EINVAL);
|
2002-04-04 09:58:20 +00:00
|
|
|
/* Reject requests past the end of media. */
|
2003-02-06 21:01:36 +00:00
|
|
|
if (bp->bio_offset > pp->mediasize)
|
|
|
|
return (EIO);
|
2002-04-04 09:58:20 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
2003-02-06 21:01:36 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_io_request(struct bio *bp, struct g_consumer *cp)
|
|
|
|
{
|
2003-02-07 23:08:24 +00:00
|
|
|
struct g_provider *pp;
|
|
|
|
struct bintime bt;
|
2003-02-06 21:01:36 +00:00
|
|
|
|
2003-02-07 23:08:24 +00:00
|
|
|
pp = cp->provider;
|
2003-02-06 21:01:36 +00:00
|
|
|
KASSERT(cp != NULL, ("NULL cp in g_io_request"));
|
|
|
|
KASSERT(bp != NULL, ("NULL bp in g_io_request"));
|
|
|
|
KASSERT(bp->bio_data != NULL, ("NULL bp->data in g_io_request"));
|
2003-02-07 23:08:24 +00:00
|
|
|
KASSERT(pp != NULL, ("consumer not attached in g_io_request"));
|
|
|
|
|
2003-02-06 21:01:36 +00:00
|
|
|
bp->bio_from = cp;
|
2003-02-07 23:08:24 +00:00
|
|
|
bp->bio_to = pp;
|
2003-02-06 21:01:36 +00:00
|
|
|
bp->bio_error = 0;
|
|
|
|
bp->bio_completed = 0;
|
|
|
|
|
2003-02-07 23:08:24 +00:00
|
|
|
if (g_collectstats) {
|
2003-02-09 17:04:57 +00:00
|
|
|
binuptime(&bt);
|
|
|
|
bp->bio_t0 = bt;
|
|
|
|
if (cp->stat->nop == cp->stat->nend)
|
|
|
|
cp->stat->wentbusy = bt; /* Consumer is idle */
|
|
|
|
if (pp->stat->nop == pp->stat->nend)
|
|
|
|
pp->stat->wentbusy = bt; /* Provider is idle */
|
2003-02-07 23:08:24 +00:00
|
|
|
}
|
2003-02-08 13:03:57 +00:00
|
|
|
cp->stat->nop++;
|
|
|
|
pp->stat->nop++;
|
2003-02-06 21:01:36 +00:00
|
|
|
|
2002-04-04 09:58:20 +00:00
|
|
|
/* Pass it on down. */
|
|
|
|
g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
|
2003-02-07 23:08:24 +00:00
|
|
|
bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd);
|
2002-04-04 09:58:20 +00:00
|
|
|
g_bioq_enqueue_tail(bp, &g_bio_run_down);
|
|
|
|
wakeup(&g_wait_down);
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-09-30 08:54:46 +00:00
|
|
|
g_io_deliver(struct bio *bp, int error)
|
2002-03-11 21:42:35 +00:00
|
|
|
{
|
2003-02-07 23:08:24 +00:00
|
|
|
struct g_consumer *cp;
|
|
|
|
struct g_provider *pp;
|
2003-02-09 17:04:57 +00:00
|
|
|
struct bintime t1, dt;
|
2003-02-07 23:08:24 +00:00
|
|
|
int idx;
|
2002-03-11 21:42:35 +00:00
|
|
|
|
2003-02-07 23:08:24 +00:00
|
|
|
cp = bp->bio_from;
|
|
|
|
pp = bp->bio_to;
|
2002-10-14 10:02:24 +00:00
|
|
|
KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
|
2003-02-07 23:08:24 +00:00
|
|
|
KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
|
|
|
|
KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
|
|
|
|
KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
|
2002-12-26 21:02:50 +00:00
|
|
|
|
2002-03-11 21:42:35 +00:00
|
|
|
g_trace(G_T_BIO,
|
2002-12-26 21:02:50 +00:00
|
|
|
"g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
|
2003-02-07 23:08:24 +00:00
|
|
|
bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
|
2002-10-20 08:45:17 +00:00
|
|
|
(intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
|
2003-02-07 23:08:24 +00:00
|
|
|
|
|
|
|
if (g_collectstats) {
|
2003-02-09 17:04:57 +00:00
|
|
|
switch (bp->bio_cmd) {
|
|
|
|
case BIO_READ: idx = G_STAT_IDX_READ; break;
|
|
|
|
case BIO_WRITE: idx = G_STAT_IDX_WRITE; break;
|
|
|
|
case BIO_DELETE: idx = G_STAT_IDX_DELETE; break;
|
|
|
|
case BIO_GETATTR: idx = -1; break;
|
|
|
|
case BIO_SETATTR: idx = -1; break;
|
|
|
|
default:
|
|
|
|
panic("unknown bio_cmd in g_io_deliver");
|
|
|
|
break;
|
|
|
|
}
|
2003-02-07 23:08:24 +00:00
|
|
|
binuptime(&t1);
|
2003-02-09 17:04:57 +00:00
|
|
|
/* Raise the "inconsistent" flag for userland */
|
2003-02-11 11:01:26 +00:00
|
|
|
atomic_add_acq_int(&cp->stat->seq0, 1);
|
|
|
|
atomic_add_acq_int(&pp->stat->seq0, 1);
|
2003-02-07 23:08:24 +00:00
|
|
|
if (idx >= 0) {
|
2003-02-09 17:04:57 +00:00
|
|
|
/* Account the service time */
|
|
|
|
dt = t1;
|
|
|
|
bintime_sub(&dt, &bp->bio_t0);
|
|
|
|
bintime_add(&cp->stat->ops[idx].dt, &dt);
|
|
|
|
bintime_add(&pp->stat->ops[idx].dt, &dt);
|
|
|
|
/* ... and the metrics */
|
2003-02-08 13:03:57 +00:00
|
|
|
pp->stat->ops[idx].nbyte += bp->bio_completed;
|
|
|
|
cp->stat->ops[idx].nbyte += bp->bio_completed;
|
|
|
|
pp->stat->ops[idx].nop++;
|
|
|
|
cp->stat->ops[idx].nop++;
|
2003-02-09 17:04:57 +00:00
|
|
|
/* ... and any errors */
|
2003-02-07 23:08:24 +00:00
|
|
|
if (error == ENOMEM) {
|
2003-02-08 13:03:57 +00:00
|
|
|
cp->stat->ops[idx].nmem++;
|
|
|
|
pp->stat->ops[idx].nmem++;
|
2003-02-07 23:08:24 +00:00
|
|
|
} else if (error != 0) {
|
2003-02-08 13:03:57 +00:00
|
|
|
cp->stat->ops[idx].nerr++;
|
|
|
|
pp->stat->ops[idx].nerr++;
|
2003-02-07 23:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
2003-02-09 17:04:57 +00:00
|
|
|
/* Account for busy time on the consumer */
|
|
|
|
dt = t1;
|
|
|
|
bintime_sub(&dt, &cp->stat->wentbusy);
|
|
|
|
bintime_add(&cp->stat->bt, &dt);
|
|
|
|
cp->stat->wentbusy = t1;
|
|
|
|
/* Account for busy time on the provider */
|
|
|
|
dt = t1;
|
|
|
|
bintime_sub(&dt, &pp->stat->wentbusy);
|
|
|
|
bintime_add(&pp->stat->bt, &dt);
|
|
|
|
pp->stat->wentbusy = t1;
|
|
|
|
/* Mark the structures as consistent again */
|
2003-02-11 11:01:26 +00:00
|
|
|
atomic_add_acq_int(&cp->stat->seq1, 1);
|
|
|
|
atomic_add_acq_int(&pp->stat->seq1, 1);
|
2003-02-07 23:08:24 +00:00
|
|
|
}
|
2003-02-08 13:03:57 +00:00
|
|
|
cp->stat->nend++;
|
2003-02-09 17:04:57 +00:00
|
|
|
pp->stat->nend++;
|
2002-03-11 21:42:35 +00:00
|
|
|
|
2002-11-02 11:08:07 +00:00
|
|
|
if (error == ENOMEM) {
|
2003-02-07 23:08:24 +00:00
|
|
|
printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
|
|
|
|
g_io_request(bp, cp);
|
2002-11-02 11:08:07 +00:00
|
|
|
pace++;
|
|
|
|
return;
|
|
|
|
}
|
2002-09-30 08:54:46 +00:00
|
|
|
bp->bio_error = error;
|
2002-03-11 21:42:35 +00:00
|
|
|
g_bioq_enqueue_tail(bp, &g_bio_run_up);
|
|
|
|
wakeup(&g_wait_up);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_io_schedule_down(struct thread *tp __unused)
|
|
|
|
{
|
|
|
|
struct bio *bp;
|
2003-02-06 21:01:36 +00:00
|
|
|
off_t excess;
|
|
|
|
int error;
|
2003-02-11 22:30:26 +00:00
|
|
|
struct mtx mymutex;
|
|
|
|
|
|
|
|
bzero(&mymutex, sizeof mymutex);
|
|
|
|
mtx_init(&mymutex, "g_xdown", MTX_DEF, 0);
|
2002-03-11 21:42:35 +00:00
|
|
|
|
|
|
|
for(;;) {
|
2003-02-11 22:30:26 +00:00
|
|
|
g_bioq_lock(&g_bio_run_down);
|
2002-03-11 21:42:35 +00:00
|
|
|
bp = g_bioq_first(&g_bio_run_down);
|
2003-02-11 22:30:26 +00:00
|
|
|
if (bp == NULL) {
|
|
|
|
msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
|
|
|
|
PRIBIO | PDROP, "g_down", hz/10);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
g_bioq_unlock(&g_bio_run_down);
|
2003-02-06 21:01:36 +00:00
|
|
|
error = g_io_check(bp);
|
|
|
|
if (error) {
|
|
|
|
g_io_deliver(bp, error);
|
|
|
|
continue;
|
|
|
|
}
|
2003-02-11 13:13:10 +00:00
|
|
|
switch (bp->bio_cmd) {
|
|
|
|
case BIO_READ:
|
|
|
|
case BIO_WRITE:
|
|
|
|
case BIO_DELETE:
|
|
|
|
/* Truncate requests to the end of providers media. */
|
|
|
|
excess = bp->bio_offset + bp->bio_length;
|
|
|
|
if (excess > bp->bio_to->mediasize) {
|
|
|
|
excess -= bp->bio_to->mediasize;
|
|
|
|
bp->bio_length -= excess;
|
|
|
|
}
|
|
|
|
/* Deliver zero length transfers right here. */
|
|
|
|
if (bp->bio_length == 0) {
|
|
|
|
g_io_deliver(bp, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2003-02-06 21:01:36 +00:00
|
|
|
}
|
2003-02-11 22:30:26 +00:00
|
|
|
mtx_lock(&mymutex);
|
2002-03-11 21:42:35 +00:00
|
|
|
bp->bio_to->geom->start(bp);
|
2003-02-11 22:30:26 +00:00
|
|
|
mtx_unlock(&mymutex);
|
2002-11-02 11:08:07 +00:00
|
|
|
if (pace) {
|
|
|
|
pace--;
|
|
|
|
break;
|
|
|
|
}
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-11 22:30:26 +00:00
|
|
|
void
|
|
|
|
bio_taskqueue(struct bio *bp, bio_task_t *func, void *arg)
|
|
|
|
{
|
|
|
|
bp->bio_task = func;
|
|
|
|
bp->bio_task_arg = arg;
|
|
|
|
/*
|
|
|
|
* The taskqueue is actually just a second queue off the "up"
|
|
|
|
* queue, so we use the same lock.
|
|
|
|
*/
|
|
|
|
g_bioq_lock(&g_bio_run_up);
|
|
|
|
TAILQ_INSERT_TAIL(&g_bio_run_task.bio_queue, bp, bio_queue);
|
|
|
|
g_bio_run_task.bio_queue_length++;
|
|
|
|
wakeup(&g_wait_up);
|
|
|
|
g_bioq_unlock(&g_bio_run_up);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-11 21:42:35 +00:00
|
|
|
void
|
|
|
|
g_io_schedule_up(struct thread *tp __unused)
|
|
|
|
{
|
|
|
|
struct bio *bp;
|
2003-02-11 22:30:26 +00:00
|
|
|
struct mtx mymutex;
|
|
|
|
|
|
|
|
bzero(&mymutex, sizeof mymutex);
|
|
|
|
mtx_init(&mymutex, "g_xup", MTX_DEF, 0);
|
2002-03-11 21:42:35 +00:00
|
|
|
for(;;) {
|
2003-02-11 22:30:26 +00:00
|
|
|
g_bioq_lock(&g_bio_run_up);
|
|
|
|
bp = g_bioq_first(&g_bio_run_task);
|
|
|
|
if (bp != NULL) {
|
|
|
|
g_bioq_unlock(&g_bio_run_up);
|
|
|
|
mtx_lock(&mymutex);
|
|
|
|
bp->bio_task(bp, bp->bio_task_arg);
|
|
|
|
mtx_unlock(&mymutex);
|
|
|
|
continue;
|
|
|
|
}
|
2002-03-11 21:42:35 +00:00
|
|
|
bp = g_bioq_first(&g_bio_run_up);
|
2003-02-11 22:30:26 +00:00
|
|
|
if (bp != NULL) {
|
|
|
|
g_bioq_unlock(&g_bio_run_up);
|
|
|
|
mtx_lock(&mymutex);
|
|
|
|
biodone(bp);
|
|
|
|
mtx_unlock(&mymutex);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
|
|
|
|
PRIBIO | PDROP, "g_up", hz/10);
|
2002-03-11 21:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
|
|
|
|
{
|
|
|
|
struct bio *bp;
|
|
|
|
void *ptr;
|
|
|
|
int errorc;
|
|
|
|
|
2002-10-08 07:03:58 +00:00
|
|
|
bp = g_new_bio();
|
|
|
|
bp->bio_cmd = BIO_READ;
|
|
|
|
bp->bio_done = NULL;
|
|
|
|
bp->bio_offset = offset;
|
|
|
|
bp->bio_length = length;
|
2003-02-19 05:47:46 +00:00
|
|
|
ptr = g_malloc(length, M_WAITOK);
|
2002-10-08 07:03:58 +00:00
|
|
|
bp->bio_data = ptr;
|
|
|
|
g_io_request(bp, cp);
|
|
|
|
errorc = biowait(bp, "gread");
|
|
|
|
if (error != NULL)
|
|
|
|
*error = errorc;
|
|
|
|
g_destroy_bio(bp);
|
|
|
|
if (errorc) {
|
|
|
|
g_free(ptr);
|
|
|
|
ptr = NULL;
|
|
|
|
}
|
2002-03-11 21:42:35 +00:00
|
|
|
return (ptr);
|
|
|
|
}
|
2002-09-30 08:50:47 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
|
|
|
|
{
|
|
|
|
struct bio *bp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
bp = g_new_bio();
|
|
|
|
bp->bio_cmd = BIO_WRITE;
|
|
|
|
bp->bio_done = NULL;
|
|
|
|
bp->bio_offset = offset;
|
|
|
|
bp->bio_length = length;
|
|
|
|
bp->bio_data = ptr;
|
|
|
|
g_io_request(bp, cp);
|
|
|
|
error = biowait(bp, "gwrite");
|
|
|
|
g_destroy_bio(bp);
|
|
|
|
return (error);
|
|
|
|
}
|