[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v10 13/17] blkdebug: Refactor error injection
From: |
Eric Blake |
Subject: |
[Qemu-devel] [PATCH v10 13/17] blkdebug: Refactor error injection |
Date: |
Wed, 26 Apr 2017 20:46:22 -0500 |
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 <address@hidden>
Reviewed-by: Max Reitz <address@hidden>
---
v10: no change
v9: no change
v7-v8: not submitted (earlier half of series sent for 2.9)
v6: new patch
---
block/blkdebug.c | 74 +++++++++++++++++++++++++-------------------------------
1 file changed, 33 insertions(+), 41 deletions(-)
diff --git a/block/blkdebug.c b/block/blkdebug.c
index 14d3fc5..db58db0 100644
--- a/block/blkdebug.c
+++ b/block/blkdebug.c
@@ -405,11 +405,30 @@ out:
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;
- int error = rule->options.inject.error;
- bool immediately = rule->options.inject.immediately;
+ BlkdebugRule *rule = NULL;
+ 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) {
QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
@@ -428,8 +447,7 @@ static int coroutine_fn
blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
QEMUIOVector *qiov, int flags)
{
- BDRVBlkdebugState *s = bs->opaque;
- BlkdebugRule *rule = NULL;
+ int err;
/* Sanity check block layer guarantees */
assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
@@ -438,18 +456,9 @@ blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset,
uint64_t bytes,
assert(bytes <= bs->bl.max_transfer);
}
- QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
- uint64_t inject_offset = rule->options.inject.offset;
-
- if (inject_offset == -1 ||
- (inject_offset >= offset && inject_offset < offset + bytes))
- {
- break;
- }
- }
-
- if (rule && rule->options.inject.error) {
- return inject_error(bs, rule);
+ err = rule_check(bs, offset, bytes);
+ if (err) {
+ return err;
}
return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
@@ -459,8 +468,7 @@ static int coroutine_fn
blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
QEMUIOVector *qiov, int flags)
{
- BDRVBlkdebugState *s = bs->opaque;
- BlkdebugRule *rule = NULL;
+ int err;
/* Sanity check block layer guarantees */
assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
@@ -469,18 +477,9 @@ blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset,
uint64_t bytes,
assert(bytes <= bs->bl.max_transfer);
}
- QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
- uint64_t inject_offset = rule->options.inject.offset;
-
- if (inject_offset == -1 ||
- (inject_offset >= offset && inject_offset < offset + bytes))
- {
- break;
- }
- }
-
- if (rule && rule->options.inject.error) {
- return inject_error(bs, rule);
+ err = rule_check(bs, offset, bytes);
+ if (err) {
+ return err;
}
return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
@@ -488,17 +487,10 @@ blkdebug_co_pwritev(BlockDriverState *bs, uint64_t
offset, uint64_t bytes,
static int blkdebug_co_flush(BlockDriverState *bs)
{
- BDRVBlkdebugState *s = bs->opaque;
- BlkdebugRule *rule = NULL;
+ int err = rule_check(bs, 0, 0);
- QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
- if (rule->options.inject.offset == -1) {
- break;
- }
- }
-
- if (rule && rule->options.inject.error) {
- return inject_error(bs, rule);
+ if (err) {
+ return err;
}
return bdrv_co_flush(bs->file->bs);
--
2.9.3
- Re: [Qemu-devel] [PATCH v10 07/17] qemu-io: Switch 'alloc' command to byte-based length, (continued)
- [Qemu-devel] [PATCH v10 05/17] iotests: Add test 179 to cover write zeroes with unmap, Eric Blake, 2017/04/26
- [Qemu-devel] [PATCH v10 10/17] qcow2: Assert that cluster operations are aligned, Eric Blake, 2017/04/26
- [Qemu-devel] [PATCH v10 12/17] blkdebug: Sanity check block layer guarantees, Eric Blake, 2017/04/26
- [Qemu-devel] [PATCH v10 11/17] qcow2: Discard/zero clusters by byte count, Eric Blake, 2017/04/26
- [Qemu-devel] [PATCH v10 08/17] qemu-io: Switch 'map' output to byte-based reporting, Eric Blake, 2017/04/26
- [Qemu-devel] [PATCH v10 13/17] blkdebug: Refactor error injection,
Eric Blake <=
- [Qemu-devel] [PATCH v10 09/17] qcow2: Optimize write zero of unaligned tail cluster, Eric Blake, 2017/04/26
- [Qemu-devel] [PATCH v10 14/17] blkdebug: Add pass-through write_zero and discard support, Eric Blake, 2017/04/26
- [Qemu-devel] [PATCH v10 16/17] blkdebug: Add ability to override unmap geometries, Eric Blake, 2017/04/26
- [Qemu-devel] [PATCH v10 15/17] blkdebug: Simplify override logic, Eric Blake, 2017/04/26
- [Qemu-devel] [PATCH v10 17/17] tests: Add coverage for recent block geometry fixes, Eric Blake, 2017/04/26