blkdebug: Implement bdrv_co_preadv/pwritev/flush

This enables byte granularity requests for blkdebug, and at the same
time gets us rid of another user of the BDS-level AIO emulation.

Note that unless align=512 is specified, this can behave subtly
different from the old behaviour because bdrv_co_preadv/pwritev don't
have to perform alignment adjustments any more.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Kevin Wolf 2016-11-04 21:13:45 +01:00
parent 7c37f941d0
commit 7c3a998531
1 changed files with 40 additions and 46 deletions

View File

@ -58,10 +58,6 @@ typedef struct BlkdebugSuspendedReq {
QLIST_ENTRY(BlkdebugSuspendedReq) next; QLIST_ENTRY(BlkdebugSuspendedReq) next;
} BlkdebugSuspendedReq; } BlkdebugSuspendedReq;
static const AIOCBInfo blkdebug_aiocb_info = {
.aiocb_size = sizeof(BlkdebugAIOCB),
};
enum { enum {
ACTION_INJECT_ERROR, ACTION_INJECT_ERROR,
ACTION_SET_STATE, ACTION_SET_STATE,
@ -77,7 +73,7 @@ typedef struct BlkdebugRule {
int error; int error;
int immediately; int immediately;
int once; int once;
int64_t sector; int64_t offset;
} inject; } inject;
struct { struct {
int new_state; int new_state;
@ -174,6 +170,7 @@ static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
const char* event_name; const char* event_name;
BlkdebugEvent event; BlkdebugEvent event;
struct BlkdebugRule *rule; struct BlkdebugRule *rule;
int64_t sector;
/* Find the right event for the rule */ /* Find the right event for the rule */
event_name = qemu_opt_get(opts, "event"); event_name = qemu_opt_get(opts, "event");
@ -200,7 +197,9 @@ static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0); rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
rule->options.inject.immediately = rule->options.inject.immediately =
qemu_opt_get_bool(opts, "immediately", 0); qemu_opt_get_bool(opts, "immediately", 0);
rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1); sector = qemu_opt_get_number(opts, "sector", -1);
rule->options.inject.offset =
sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE;
break; break;
case ACTION_SET_STATE: case ACTION_SET_STATE:
@ -408,17 +407,14 @@ out:
static void error_callback_bh(void *opaque) static void error_callback_bh(void *opaque)
{ {
struct BlkdebugAIOCB *acb = opaque; Coroutine *co = opaque;
acb->common.cb(acb->common.opaque, acb->ret); qemu_coroutine_enter(co);
qemu_aio_unref(acb);
} }
static BlockAIOCB *inject_error(BlockDriverState *bs, static int inject_error(BlockDriverState *bs, BlkdebugRule *rule)
BlockCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
{ {
BDRVBlkdebugState *s = bs->opaque; BDRVBlkdebugState *s = bs->opaque;
int error = rule->options.inject.error; int error = rule->options.inject.error;
struct BlkdebugAIOCB *acb;
bool immediately = rule->options.inject.immediately; bool immediately = rule->options.inject.immediately;
if (rule->options.inject.once) { if (rule->options.inject.once) {
@ -426,81 +422,79 @@ static BlockAIOCB *inject_error(BlockDriverState *bs,
remove_rule(rule); remove_rule(rule);
} }
if (immediately) { if (!immediately) {
return NULL; aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), error_callback_bh,
qemu_coroutine_self());
qemu_coroutine_yield();
} }
acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque); return -error;
acb->ret = -error;
aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), error_callback_bh, acb);
return &acb->common;
} }
static BlockAIOCB *blkdebug_aio_readv(BlockDriverState *bs, static int coroutine_fn
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
BlockCompletionFunc *cb, void *opaque) QEMUIOVector *qiov, int flags)
{ {
BDRVBlkdebugState *s = bs->opaque; BDRVBlkdebugState *s = bs->opaque;
BlkdebugRule *rule = NULL; BlkdebugRule *rule = NULL;
QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
if (rule->options.inject.sector == -1 || uint64_t inject_offset = rule->options.inject.offset;
(rule->options.inject.sector >= sector_num &&
rule->options.inject.sector < sector_num + nb_sectors)) { if (inject_offset == -1 ||
(inject_offset >= offset && inject_offset < offset + bytes))
{
break; break;
} }
} }
if (rule && rule->options.inject.error) { if (rule && rule->options.inject.error) {
return inject_error(bs, cb, opaque, rule); return inject_error(bs, rule);
} }
return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
cb, opaque);
} }
static BlockAIOCB *blkdebug_aio_writev(BlockDriverState *bs, static int coroutine_fn
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
BlockCompletionFunc *cb, void *opaque) QEMUIOVector *qiov, int flags)
{ {
BDRVBlkdebugState *s = bs->opaque; BDRVBlkdebugState *s = bs->opaque;
BlkdebugRule *rule = NULL; BlkdebugRule *rule = NULL;
QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
if (rule->options.inject.sector == -1 || uint64_t inject_offset = rule->options.inject.offset;
(rule->options.inject.sector >= sector_num &&
rule->options.inject.sector < sector_num + nb_sectors)) { if (inject_offset == -1 ||
(inject_offset >= offset && inject_offset < offset + bytes))
{
break; break;
} }
} }
if (rule && rule->options.inject.error) { if (rule && rule->options.inject.error) {
return inject_error(bs, cb, opaque, rule); return inject_error(bs, rule);
} }
return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
cb, opaque);
} }
static BlockAIOCB *blkdebug_aio_flush(BlockDriverState *bs, static int blkdebug_co_flush(BlockDriverState *bs)
BlockCompletionFunc *cb, void *opaque)
{ {
BDRVBlkdebugState *s = bs->opaque; BDRVBlkdebugState *s = bs->opaque;
BlkdebugRule *rule = NULL; BlkdebugRule *rule = NULL;
QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
if (rule->options.inject.sector == -1) { if (rule->options.inject.offset == -1) {
break; break;
} }
} }
if (rule && rule->options.inject.error) { if (rule && rule->options.inject.error) {
return inject_error(bs, cb, opaque, rule); return inject_error(bs, rule);
} }
return bdrv_aio_flush(bs->file->bs, cb, opaque); return bdrv_co_flush(bs->file->bs);
} }
@ -752,9 +746,9 @@ static BlockDriver bdrv_blkdebug = {
.bdrv_refresh_filename = blkdebug_refresh_filename, .bdrv_refresh_filename = blkdebug_refresh_filename,
.bdrv_refresh_limits = blkdebug_refresh_limits, .bdrv_refresh_limits = blkdebug_refresh_limits,
.bdrv_aio_readv = blkdebug_aio_readv, .bdrv_co_preadv = blkdebug_co_preadv,
.bdrv_aio_writev = blkdebug_aio_writev, .bdrv_co_pwritev = blkdebug_co_pwritev,
.bdrv_aio_flush = blkdebug_aio_flush, .bdrv_co_flush_to_disk = blkdebug_co_flush,
.bdrv_debug_event = blkdebug_debug_event, .bdrv_debug_event = blkdebug_debug_event,
.bdrv_debug_breakpoint = blkdebug_debug_breakpoint, .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,