修訂 | 16789db3de64147c7271d30a9d379b2a8aa2b81e (tree) |
---|---|
時間 | 2019-06-14 21:16:57 |
作者 | Max Reitz <mreitz@redh...> |
Commiter | Max Reitz |
blkdebug: Add @iotype error option
This new error option allows users of blkdebug to inject errors only on
certain kinds of I/O operations. Users usually want to make a very
specific operation fail, not just any; but right now they simply hope
that the event that triggers the error injection is followed up with
that very operation. That may not be true, however, because the block
layer is changing (including blkdebug, which may increase the number of
types of I/O operations on which to inject errors).
The new option's default has been chosen to keep backwards
compatibility.
Note that similar to the internal representation, we could choose to
expose this option as a list of I/O types. But there is no practical
use for this, because as described above, users usually know exactly
which kind of operation they want to make fail, so there is no need to
specify multiple I/O types at once. In addition, exposing this option
as a list would require non-trivial changes to qemu_opts_absorb_qdict().
Signed-off-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-id: 20190507203508.18026-4-mreitz@redhat.com
Signed-off-by: Max Reitz <mreitz@redhat.com>
@@ -75,6 +75,7 @@ typedef struct BlkdebugRule { | ||
75 | 75 | int state; |
76 | 76 | union { |
77 | 77 | struct { |
78 | + uint64_t iotype_mask; | |
78 | 79 | int error; |
79 | 80 | int immediately; |
80 | 81 | int once; |
@@ -91,6 +92,9 @@ typedef struct BlkdebugRule { | ||
91 | 92 | QSIMPLEQ_ENTRY(BlkdebugRule) active_next; |
92 | 93 | } BlkdebugRule; |
93 | 94 | |
95 | +QEMU_BUILD_BUG_MSG(BLKDEBUG_IO_TYPE__MAX > 64, | |
96 | + "BlkdebugIOType mask does not fit into an uint64_t"); | |
97 | + | |
94 | 98 | static QemuOptsList inject_error_opts = { |
95 | 99 | .name = "inject-error", |
96 | 100 | .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head), |
@@ -104,6 +108,10 @@ static QemuOptsList inject_error_opts = { | ||
104 | 108 | .type = QEMU_OPT_NUMBER, |
105 | 109 | }, |
106 | 110 | { |
111 | + .name = "iotype", | |
112 | + .type = QEMU_OPT_STRING, | |
113 | + }, | |
114 | + { | |
107 | 115 | .name = "errno", |
108 | 116 | .type = QEMU_OPT_NUMBER, |
109 | 117 | }, |
@@ -162,6 +170,8 @@ static int add_rule(void *opaque, QemuOpts *opts, Error **errp) | ||
162 | 170 | int event; |
163 | 171 | struct BlkdebugRule *rule; |
164 | 172 | int64_t sector; |
173 | + BlkdebugIOType iotype; | |
174 | + Error *local_error = NULL; | |
165 | 175 | |
166 | 176 | /* Find the right event for the rule */ |
167 | 177 | event_name = qemu_opt_get(opts, "event"); |
@@ -192,6 +202,26 @@ static int add_rule(void *opaque, QemuOpts *opts, Error **errp) | ||
192 | 202 | sector = qemu_opt_get_number(opts, "sector", -1); |
193 | 203 | rule->options.inject.offset = |
194 | 204 | sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE; |
205 | + | |
206 | + iotype = qapi_enum_parse(&BlkdebugIOType_lookup, | |
207 | + qemu_opt_get(opts, "iotype"), | |
208 | + BLKDEBUG_IO_TYPE__MAX, &local_error); | |
209 | + if (local_error) { | |
210 | + error_propagate(errp, local_error); | |
211 | + return -1; | |
212 | + } | |
213 | + if (iotype != BLKDEBUG_IO_TYPE__MAX) { | |
214 | + rule->options.inject.iotype_mask = (1ull << iotype); | |
215 | + } else { | |
216 | + /* Apply the default */ | |
217 | + rule->options.inject.iotype_mask = | |
218 | + (1ull << BLKDEBUG_IO_TYPE_READ) | |
219 | + | (1ull << BLKDEBUG_IO_TYPE_WRITE) | |
220 | + | (1ull << BLKDEBUG_IO_TYPE_WRITE_ZEROES) | |
221 | + | (1ull << BLKDEBUG_IO_TYPE_DISCARD) | |
222 | + | (1ull << BLKDEBUG_IO_TYPE_FLUSH); | |
223 | + } | |
224 | + | |
195 | 225 | break; |
196 | 226 | |
197 | 227 | case ACTION_SET_STATE: |
@@ -470,7 +500,8 @@ out: | ||
470 | 500 | return ret; |
471 | 501 | } |
472 | 502 | |
473 | -static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes) | |
503 | +static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | |
504 | + BlkdebugIOType iotype) | |
474 | 505 | { |
475 | 506 | BDRVBlkdebugState *s = bs->opaque; |
476 | 507 | BlkdebugRule *rule = NULL; |
@@ -480,9 +511,10 @@ static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes) | ||
480 | 511 | QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { |
481 | 512 | uint64_t inject_offset = rule->options.inject.offset; |
482 | 513 | |
483 | - if (inject_offset == -1 || | |
484 | - (bytes && inject_offset >= offset && | |
485 | - inject_offset < offset + bytes)) | |
514 | + if ((inject_offset == -1 || | |
515 | + (bytes && inject_offset >= offset && | |
516 | + inject_offset < offset + bytes)) && | |
517 | + (rule->options.inject.iotype_mask & (1ull << iotype))) | |
486 | 518 | { |
487 | 519 | break; |
488 | 520 | } |
@@ -521,7 +553,7 @@ blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | ||
521 | 553 | assert(bytes <= bs->bl.max_transfer); |
522 | 554 | } |
523 | 555 | |
524 | - err = rule_check(bs, offset, bytes); | |
556 | + err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_READ); | |
525 | 557 | if (err) { |
526 | 558 | return err; |
527 | 559 | } |
@@ -542,7 +574,7 @@ blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | ||
542 | 574 | assert(bytes <= bs->bl.max_transfer); |
543 | 575 | } |
544 | 576 | |
545 | - err = rule_check(bs, offset, bytes); | |
577 | + err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE); | |
546 | 578 | if (err) { |
547 | 579 | return err; |
548 | 580 | } |
@@ -552,7 +584,7 @@ blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | ||
552 | 584 | |
553 | 585 | static int blkdebug_co_flush(BlockDriverState *bs) |
554 | 586 | { |
555 | - int err = rule_check(bs, 0, 0); | |
587 | + int err = rule_check(bs, 0, 0, BLKDEBUG_IO_TYPE_FLUSH); | |
556 | 588 | |
557 | 589 | if (err) { |
558 | 590 | return err; |
@@ -586,7 +618,7 @@ static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs, | ||
586 | 618 | assert(bytes <= bs->bl.max_pwrite_zeroes); |
587 | 619 | } |
588 | 620 | |
589 | - err = rule_check(bs, offset, bytes); | |
621 | + err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE_ZEROES); | |
590 | 622 | if (err) { |
591 | 623 | return err; |
592 | 624 | } |
@@ -620,7 +652,7 @@ static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs, | ||
620 | 652 | assert(bytes <= bs->bl.max_pdiscard); |
621 | 653 | } |
622 | 654 | |
623 | - err = rule_check(bs, offset, bytes); | |
655 | + err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_DISCARD); | |
624 | 656 | if (err) { |
625 | 657 | return err; |
626 | 658 | } |
@@ -3265,6 +3265,26 @@ | ||
3265 | 3265 | 'cor_write', 'cluster_alloc_space'] } |
3266 | 3266 | |
3267 | 3267 | ## |
3268 | +# @BlkdebugIOType: | |
3269 | +# | |
3270 | +# Kinds of I/O that blkdebug can inject errors in. | |
3271 | +# | |
3272 | +# @read: .bdrv_co_preadv() | |
3273 | +# | |
3274 | +# @write: .bdrv_co_pwritev() | |
3275 | +# | |
3276 | +# @write-zeroes: .bdrv_co_pwrite_zeroes() | |
3277 | +# | |
3278 | +# @discard: .bdrv_co_pdiscard() | |
3279 | +# | |
3280 | +# @flush: .bdrv_co_flush_to_disk() | |
3281 | +# | |
3282 | +# Since: 4.1 | |
3283 | +## | |
3284 | +{ 'enum': 'BlkdebugIOType', 'prefix': 'BLKDEBUG_IO_TYPE', | |
3285 | + 'data': [ 'read', 'write', 'write-zeroes', 'discard', 'flush' ] } | |
3286 | + | |
3287 | +## | |
3268 | 3288 | # @BlkdebugInjectErrorOptions: |
3269 | 3289 | # |
3270 | 3290 | # Describes a single error injection for blkdebug. |
@@ -3274,6 +3294,11 @@ | ||
3274 | 3294 | # @state: the state identifier blkdebug needs to be in to |
3275 | 3295 | # actually trigger the event; defaults to "any" |
3276 | 3296 | # |
3297 | +# @iotype: the type of I/O operations on which this error should | |
3298 | +# be injected; defaults to "all read, write, | |
3299 | +# write-zeroes, discard, and flush operations" | |
3300 | +# (since: 4.1) | |
3301 | +# | |
3277 | 3302 | # @errno: error identifier (errno) to be returned; defaults to |
3278 | 3303 | # EIO |
3279 | 3304 | # |
@@ -3291,6 +3316,7 @@ | ||
3291 | 3316 | { 'struct': 'BlkdebugInjectErrorOptions', |
3292 | 3317 | 'data': { 'event': 'BlkdebugEvent', |
3293 | 3318 | '*state': 'int', |
3319 | + '*iotype': 'BlkdebugIOType', | |
3294 | 3320 | '*errno': 'int', |
3295 | 3321 | '*sector': 'int', |
3296 | 3322 | '*once': 'bool', |