diff options
-rw-r--r-- | sys/geom/geom.h | 1 | ||||
-rw-r--r-- | sys/geom/geom_disk.c | 2 | ||||
-rw-r--r-- | sys/geom/geom_io.c | 111 | ||||
-rw-r--r-- | sys/geom/geom_slice.c | 1 |
4 files changed, 71 insertions, 44 deletions
diff --git a/sys/geom/geom.h b/sys/geom/geom.h index d46269a..7c85d2b 100644 --- a/sys/geom/geom.h +++ b/sys/geom/geom.h @@ -160,6 +160,7 @@ struct g_provider { struct g_event *event; TAILQ_ENTRY(g_provider) orphan; int index; + off_t mediasize; }; /* geom_dump.c */ diff --git a/sys/geom/geom_disk.c b/sys/geom/geom_disk.c index 3cae0a3..89ce70c 100644 --- a/sys/geom/geom_disk.c +++ b/sys/geom/geom_disk.c @@ -93,6 +93,8 @@ g_disk_access(struct g_provider *pp, int r, int w, int e) } else { error = 0; } + pp->mediasize = + dp->d_label.d_secsize * (off_t)dp->d_label.d_secperunit; return (error); } diff --git a/sys/geom/geom_io.c b/sys/geom/geom_io.c index 5d1fdc1..b070448 100644 --- a/sys/geom/geom_io.c +++ b/sys/geom/geom_io.c @@ -223,65 +223,88 @@ g_io_getattr(char *attr, struct g_consumer *cp, int *len, void *ptr) } void +g_io_fail(struct bio *bp, int error) +{ + + bp->bio_error = error; + + g_trace(G_T_BIO, + "bio_fail(%p) from %p(%s) to %p(%s) cmd %d error %d\n", + bp, bp->bio_from, bp->bio_from->geom->name, + bp->bio_to, bp->bio_to->name, bp->bio_cmd, bp->bio_error); + g_io_deliver(bp); + return; +} + +void g_io_request(struct bio *bp, struct g_consumer *cp) { int error; + off_t excess; KASSERT(cp != NULL, ("bio_request on thin air")); error = 0; bp->bio_from = cp; bp->bio_to = cp->provider; + bp->bio_error = 0; + bp->bio_completed = 0; /* begin_stats(&bp->stats); */ atomic_add_int(&cp->biocount, 1); + /* Fail on unattached consumers */ if (bp->bio_to == NULL) - error = ENXIO; - if (!error) { - switch(bp->bio_cmd) { - case BIO_READ: - case BIO_GETATTR: - if (cp->acr == 0) - error = EPERM; - break; - case BIO_WRITE: - if (cp->acw == 0) - error = EPERM; - break; - case BIO_SETATTR: - case BIO_DELETE: - if ((cp->acw == 0) || (cp->ace == 0)) - error = EPERM; - break; - default: - error = EPERM; - break; - } + return (g_io_fail(bp, ENXIO)); + /* Fail if access doesn't allow operation */ + switch(bp->bio_cmd) { + case BIO_READ: + case BIO_GETATTR: + if (cp->acr == 0) + return (g_io_fail(bp, EPERM)); + break; + case BIO_WRITE: + case BIO_DELETE: + if (cp->acw == 0) + return (g_io_fail(bp, EPERM)); + break; + case BIO_SETATTR: + if ((cp->acw == 0) || (cp->ace == 0)) + return (g_io_fail(bp, EPERM)); + break; + default: + return (g_io_fail(bp, EPERM)); } - /* if provider is marked for error, don't disturb */ - if (!error) - error = bp->bio_to->error; - if (error) { - bp->bio_error = error; - /* finish_stats(&bp->stats); */ - - g_trace(G_T_BIO, - "bio_request(%p) from %p(%s) to %p(%s) cmd %d error %d\n", - bp, bp->bio_from, bp->bio_from->geom->name, - bp->bio_to, bp->bio_to->name, bp->bio_cmd, bp->bio_error); - g_bioq_enqueue_tail(bp, &g_bio_run_up); - mtx_lock(&Giant); - wakeup(&g_wait_up); - mtx_unlock(&Giant); - } else { - g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d", - bp, bp->bio_from, bp->bio_from->geom->name, - bp->bio_to, bp->bio_to->name, bp->bio_cmd); - g_bioq_enqueue_tail(bp, &g_bio_run_down); - mtx_lock(&Giant); - wakeup(&g_wait_down); - mtx_unlock(&Giant); + /* if provider is marked for error, don't disturb. */ + if (bp->bio_to->error) + return (g_io_fail(bp, bp->bio_to->error)); + switch(bp->bio_cmd) { + case BIO_READ: + case BIO_WRITE: + case BIO_DELETE: + /* Reject requests past the end of media. */ + if (bp->bio_offset > bp->bio_to->mediasize) + return (g_io_fail(bp, EIO)); + /* 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) + return (g_io_deliver(bp)); + break; + default: + break; } + /* Pass it on down. */ + g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d", + bp, bp->bio_from, bp->bio_from->geom->name, + bp->bio_to, bp->bio_to->name, bp->bio_cmd); + g_bioq_enqueue_tail(bp, &g_bio_run_down); + mtx_lock(&Giant); + wakeup(&g_wait_down); + mtx_unlock(&Giant); } void diff --git a/sys/geom/geom_slice.c b/sys/geom/geom_slice.c index ad91ade..1ac1d0a 100644 --- a/sys/geom/geom_slice.c +++ b/sys/geom/geom_slice.c @@ -115,6 +115,7 @@ g_slice_access(struct g_provider *pp, int dr, int dw, int de) if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) de--; error = g_access_rel(cp, dr, dw, de); + pp->mediasize = gsp->slices[pp->index].length; return (error); } |