mirror of
https://github.com/qemu/qemu.git
synced 2025-08-08 08:05:17 +00:00
blkdebug: Refactor error injection
Rather than repeat the logic at each caller of checking if a Rule exists that warrants an error injection, fold that logic into inject_error(); and rename it to rule_check() for legibility. This will help the next patch, which adds two more callers that need to check rules for the potential of injecting errors. Signed-off-by: Eric Blake <eblake@redhat.com> Reviewed-by: Max Reitz <mreitz@redhat.com> Message-id: 20170429191419.30051-6-eblake@redhat.com Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
parent
e0ef439588
commit
d157ed5f72
@ -403,11 +403,30 @@ out:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int inject_error(BlockDriverState *bs, BlkdebugRule *rule)
|
static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
int error = rule->options.inject.error;
|
BlkdebugRule *rule = NULL;
|
||||||
bool immediately = rule->options.inject.immediately;
|
int error;
|
||||||
|
bool immediately;
|
||||||
|
|
||||||
|
QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
|
||||||
|
uint64_t inject_offset = rule->options.inject.offset;
|
||||||
|
|
||||||
|
if (inject_offset == -1 ||
|
||||||
|
(bytes && inject_offset >= offset &&
|
||||||
|
inject_offset < offset + bytes))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!rule || !rule->options.inject.error) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
immediately = rule->options.inject.immediately;
|
||||||
|
error = rule->options.inject.error;
|
||||||
|
|
||||||
if (rule->options.inject.once) {
|
if (rule->options.inject.once) {
|
||||||
QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
|
QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
|
||||||
@ -426,8 +445,7 @@ static int coroutine_fn
|
|||||||
blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
||||||
QEMUIOVector *qiov, int flags)
|
QEMUIOVector *qiov, int flags)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
int err;
|
||||||
BlkdebugRule *rule = NULL;
|
|
||||||
|
|
||||||
/* Sanity check block layer guarantees */
|
/* Sanity check block layer guarantees */
|
||||||
assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
|
assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
|
||||||
@ -436,18 +454,9 @@ blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
|||||||
assert(bytes <= bs->bl.max_transfer);
|
assert(bytes <= bs->bl.max_transfer);
|
||||||
}
|
}
|
||||||
|
|
||||||
QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
|
err = rule_check(bs, offset, bytes);
|
||||||
uint64_t inject_offset = rule->options.inject.offset;
|
if (err) {
|
||||||
|
return err;
|
||||||
if (inject_offset == -1 ||
|
|
||||||
(inject_offset >= offset && inject_offset < offset + bytes))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rule && rule->options.inject.error) {
|
|
||||||
return inject_error(bs, rule);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
|
return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
|
||||||
@ -457,8 +466,7 @@ static int coroutine_fn
|
|||||||
blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
||||||
QEMUIOVector *qiov, int flags)
|
QEMUIOVector *qiov, int flags)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
int err;
|
||||||
BlkdebugRule *rule = NULL;
|
|
||||||
|
|
||||||
/* Sanity check block layer guarantees */
|
/* Sanity check block layer guarantees */
|
||||||
assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
|
assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
|
||||||
@ -467,18 +475,9 @@ blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
|||||||
assert(bytes <= bs->bl.max_transfer);
|
assert(bytes <= bs->bl.max_transfer);
|
||||||
}
|
}
|
||||||
|
|
||||||
QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
|
err = rule_check(bs, offset, bytes);
|
||||||
uint64_t inject_offset = rule->options.inject.offset;
|
if (err) {
|
||||||
|
return err;
|
||||||
if (inject_offset == -1 ||
|
|
||||||
(inject_offset >= offset && inject_offset < offset + bytes))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rule && rule->options.inject.error) {
|
|
||||||
return inject_error(bs, rule);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
|
return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
|
||||||
@ -486,17 +485,10 @@ blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
|||||||
|
|
||||||
static int blkdebug_co_flush(BlockDriverState *bs)
|
static int blkdebug_co_flush(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
int err = rule_check(bs, 0, 0);
|
||||||
BlkdebugRule *rule = NULL;
|
|
||||||
|
|
||||||
QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
|
if (err) {
|
||||||
if (rule->options.inject.offset == -1) {
|
return err;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rule && rule->options.inject.error) {
|
|
||||||
return inject_error(bs, rule);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bdrv_co_flush(bs->file->bs);
|
return bdrv_co_flush(bs->file->bs);
|
||||||
|
Loading…
Reference in New Issue
Block a user