2 * Block protocol for I/O error injection
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu-common.h"
26 #include "qemu/config-file.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29 #include "qapi/qmp/qbool.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qapi/qmp/qint.h"
32 #include "qapi/qmp/qstring.h"
33 #include "sysemu/qtest.h"
35 typedef struct BDRVBlkdebugState {
39 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
40 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
41 QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
44 typedef struct BlkdebugAIOCB {
50 typedef struct BlkdebugSuspendedReq {
53 QLIST_ENTRY(BlkdebugSuspendedReq) next;
54 } BlkdebugSuspendedReq;
56 static const AIOCBInfo blkdebug_aiocb_info = {
57 .aiocb_size = sizeof(BlkdebugAIOCB),
66 typedef struct BlkdebugRule {
84 QLIST_ENTRY(BlkdebugRule) next;
85 QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
88 static QemuOptsList inject_error_opts = {
89 .name = "inject-error",
90 .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
94 .type = QEMU_OPT_STRING,
98 .type = QEMU_OPT_NUMBER,
102 .type = QEMU_OPT_NUMBER,
106 .type = QEMU_OPT_NUMBER,
110 .type = QEMU_OPT_BOOL,
113 .name = "immediately",
114 .type = QEMU_OPT_BOOL,
116 { /* end of list */ }
120 static QemuOptsList set_state_opts = {
122 .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
126 .type = QEMU_OPT_STRING,
130 .type = QEMU_OPT_NUMBER,
134 .type = QEMU_OPT_NUMBER,
136 { /* end of list */ }
140 static QemuOptsList *config_groups[] = {
146 static int get_event_by_name(const char *name, BlkdebugEvent *event)
150 for (i = 0; i < BLKDBG__MAX; i++) {
151 if (!strcmp(BlkdebugEvent_lookup[i], name)) {
160 struct add_rule_data {
161 BDRVBlkdebugState *s;
165 static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
167 struct add_rule_data *d = opaque;
168 BDRVBlkdebugState *s = d->s;
169 const char* event_name;
171 struct BlkdebugRule *rule;
173 /* Find the right event for the rule */
174 event_name = qemu_opt_get(opts, "event");
176 error_setg(errp, "Missing event name for rule");
178 } else if (get_event_by_name(event_name, &event) < 0) {
179 error_setg(errp, "Invalid event name \"%s\"", event_name);
183 /* Set attributes common for all actions */
184 rule = g_malloc0(sizeof(*rule));
185 *rule = (struct BlkdebugRule) {
188 .state = qemu_opt_get_number(opts, "state", 0),
191 /* Parse action-specific options */
193 case ACTION_INJECT_ERROR:
194 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
195 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
196 rule->options.inject.immediately =
197 qemu_opt_get_bool(opts, "immediately", 0);
198 rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1);
201 case ACTION_SET_STATE:
202 rule->options.set_state.new_state =
203 qemu_opt_get_number(opts, "new_state", 0);
207 rule->options.suspend.tag =
208 g_strdup(qemu_opt_get(opts, "tag"));
213 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
218 static void remove_rule(BlkdebugRule *rule)
220 switch (rule->action) {
221 case ACTION_INJECT_ERROR:
222 case ACTION_SET_STATE:
225 g_free(rule->options.suspend.tag);
229 QLIST_REMOVE(rule, next);
233 static int read_config(BDRVBlkdebugState *s, const char *filename,
234 QDict *options, Error **errp)
238 struct add_rule_data d;
239 Error *local_err = NULL;
242 f = fopen(filename, "r");
244 error_setg_errno(errp, errno, "Could not read blkdebug config file");
248 ret = qemu_config_parse(f, config_groups, filename);
250 error_setg(errp, "Could not parse blkdebug config file");
256 qemu_config_parse_qdict(options, config_groups, &local_err);
258 error_propagate(errp, local_err);
264 d.action = ACTION_INJECT_ERROR;
265 qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
267 error_propagate(errp, local_err);
272 d.action = ACTION_SET_STATE;
273 qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
275 error_propagate(errp, local_err);
282 qemu_opts_reset(&inject_error_opts);
283 qemu_opts_reset(&set_state_opts);
290 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
291 static void blkdebug_parse_filename(const char *filename, QDict *options,
296 /* Parse the blkdebug: prefix */
297 if (!strstart(filename, "blkdebug:", &filename)) {
298 /* There was no prefix; therefore, all options have to be already
299 present in the QDict (except for the filename) */
300 qdict_put(options, "x-image", qstring_from_str(filename));
304 /* Parse config file path */
305 c = strchr(filename, ':');
307 error_setg(errp, "blkdebug requires both config file and image path");
312 QString *config_path;
313 config_path = qstring_from_substr(filename, 0, c - filename - 1);
314 qdict_put(options, "config", config_path);
317 /* TODO Allow multi-level nesting and set file.filename here */
319 qdict_put(options, "x-image", qstring_from_str(filename));
322 static QemuOptsList runtime_opts = {
324 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
328 .type = QEMU_OPT_STRING,
329 .help = "Path to the configuration file",
333 .type = QEMU_OPT_STRING,
334 .help = "[internal use only, will be removed]",
338 .type = QEMU_OPT_SIZE,
339 .help = "Required alignment in bytes",
341 { /* end of list */ }
345 static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
348 BDRVBlkdebugState *s = bs->opaque;
350 Error *local_err = NULL;
355 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
356 qemu_opts_absorb_qdict(opts, options, &local_err);
358 error_propagate(errp, local_err);
363 /* Read rules from config file or command line options */
364 config = qemu_opt_get(opts, "config");
365 ret = read_config(s, config, options, errp);
370 /* Set initial state */
373 /* Open the image file */
374 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
375 bs, &child_file, false, &local_err);
378 error_propagate(errp, local_err);
382 /* Set request alignment */
383 align = qemu_opt_get_size(opts, "align", bs->request_alignment);
384 if (align > 0 && align < INT_MAX && !(align & (align - 1))) {
385 bs->request_alignment = align;
387 error_setg(errp, "Invalid alignment");
396 bdrv_unref_child(bs, bs->file);
402 static void error_callback_bh(void *opaque)
404 struct BlkdebugAIOCB *acb = opaque;
405 qemu_bh_delete(acb->bh);
406 acb->common.cb(acb->common.opaque, acb->ret);
410 static BlockAIOCB *inject_error(BlockDriverState *bs,
411 BlockCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
413 BDRVBlkdebugState *s = bs->opaque;
414 int error = rule->options.inject.error;
415 struct BlkdebugAIOCB *acb;
417 bool immediately = rule->options.inject.immediately;
419 if (rule->options.inject.once) {
420 QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
428 acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
431 bh = aio_bh_new(bdrv_get_aio_context(bs), error_callback_bh, acb);
433 qemu_bh_schedule(bh);
438 static BlockAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
439 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
440 BlockCompletionFunc *cb, void *opaque)
442 BDRVBlkdebugState *s = bs->opaque;
443 BlkdebugRule *rule = NULL;
445 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
446 if (rule->options.inject.sector == -1 ||
447 (rule->options.inject.sector >= sector_num &&
448 rule->options.inject.sector < sector_num + nb_sectors)) {
453 if (rule && rule->options.inject.error) {
454 return inject_error(bs, cb, opaque, rule);
457 return bdrv_aio_readv(bs->file->bs, sector_num, qiov, nb_sectors,
461 static BlockAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
462 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
463 BlockCompletionFunc *cb, void *opaque)
465 BDRVBlkdebugState *s = bs->opaque;
466 BlkdebugRule *rule = NULL;
468 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
469 if (rule->options.inject.sector == -1 ||
470 (rule->options.inject.sector >= sector_num &&
471 rule->options.inject.sector < sector_num + nb_sectors)) {
476 if (rule && rule->options.inject.error) {
477 return inject_error(bs, cb, opaque, rule);
480 return bdrv_aio_writev(bs->file->bs, sector_num, qiov, nb_sectors,
484 static BlockAIOCB *blkdebug_aio_flush(BlockDriverState *bs,
485 BlockCompletionFunc *cb, void *opaque)
487 BDRVBlkdebugState *s = bs->opaque;
488 BlkdebugRule *rule = NULL;
490 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
491 if (rule->options.inject.sector == -1) {
496 if (rule && rule->options.inject.error) {
497 return inject_error(bs, cb, opaque, rule);
500 return bdrv_aio_flush(bs->file->bs, cb, opaque);
504 static void blkdebug_close(BlockDriverState *bs)
506 BDRVBlkdebugState *s = bs->opaque;
507 BlkdebugRule *rule, *next;
510 for (i = 0; i < BLKDBG__MAX; i++) {
511 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
517 static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
519 BDRVBlkdebugState *s = bs->opaque;
520 BlkdebugSuspendedReq r;
522 r = (BlkdebugSuspendedReq) {
523 .co = qemu_coroutine_self(),
524 .tag = g_strdup(rule->options.suspend.tag),
528 QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
530 if (!qtest_enabled()) {
531 printf("blkdebug: Suspended request '%s'\n", r.tag);
533 qemu_coroutine_yield();
534 if (!qtest_enabled()) {
535 printf("blkdebug: Resuming request '%s'\n", r.tag);
538 QLIST_REMOVE(&r, next);
542 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
545 BDRVBlkdebugState *s = bs->opaque;
547 /* Only process rules for the current state */
548 if (rule->state && rule->state != s->state) {
552 /* Take the action */
553 switch (rule->action) {
554 case ACTION_INJECT_ERROR:
556 QSIMPLEQ_INIT(&s->active_rules);
559 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
562 case ACTION_SET_STATE:
563 s->new_state = rule->options.set_state.new_state;
567 suspend_request(bs, rule);
573 static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
575 BDRVBlkdebugState *s = bs->opaque;
576 struct BlkdebugRule *rule, *next;
579 assert((int)event >= 0 && event < BLKDBG__MAX);
582 s->new_state = s->state;
583 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
584 injected = process_rule(bs, rule, injected);
586 s->state = s->new_state;
589 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
592 BDRVBlkdebugState *s = bs->opaque;
593 struct BlkdebugRule *rule;
594 BlkdebugEvent blkdebug_event;
596 if (get_event_by_name(event, &blkdebug_event) < 0) {
601 rule = g_malloc(sizeof(*rule));
602 *rule = (struct BlkdebugRule) {
603 .event = blkdebug_event,
604 .action = ACTION_SUSPEND,
606 .options.suspend.tag = g_strdup(tag),
609 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
614 static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
616 BDRVBlkdebugState *s = bs->opaque;
617 BlkdebugSuspendedReq *r, *next;
619 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
620 if (!strcmp(r->tag, tag)) {
621 qemu_coroutine_enter(r->co, NULL);
628 static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
631 BDRVBlkdebugState *s = bs->opaque;
632 BlkdebugSuspendedReq *r, *r_next;
633 BlkdebugRule *rule, *next;
634 int i, ret = -ENOENT;
636 for (i = 0; i < BLKDBG__MAX; i++) {
637 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
638 if (rule->action == ACTION_SUSPEND &&
639 !strcmp(rule->options.suspend.tag, tag)) {
645 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
646 if (!strcmp(r->tag, tag)) {
647 qemu_coroutine_enter(r->co, NULL);
654 static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
656 BDRVBlkdebugState *s = bs->opaque;
657 BlkdebugSuspendedReq *r;
659 QLIST_FOREACH(r, &s->suspended_reqs, next) {
660 if (!strcmp(r->tag, tag)) {
667 static int64_t blkdebug_getlength(BlockDriverState *bs)
669 return bdrv_getlength(bs->file->bs);
672 static int blkdebug_truncate(BlockDriverState *bs, int64_t offset)
674 return bdrv_truncate(bs->file->bs, offset);
677 static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
681 bool force_json = false;
683 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
684 if (strcmp(qdict_entry_key(e), "config") &&
685 strcmp(qdict_entry_key(e), "x-image"))
692 if (force_json && !bs->file->bs->full_open_options) {
693 /* The config file cannot be recreated, so creating a plain filename
698 if (!force_json && bs->file->bs->exact_filename[0]) {
699 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
701 qdict_get_try_str(options, "config") ?: "",
702 bs->file->bs->exact_filename);
706 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkdebug")));
708 QINCREF(bs->file->bs->full_open_options);
709 qdict_put_obj(opts, "image", QOBJECT(bs->file->bs->full_open_options));
711 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
712 if (strcmp(qdict_entry_key(e), "x-image")) {
713 qobject_incref(qdict_entry_value(e));
714 qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e));
718 bs->full_open_options = opts;
721 static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
722 BlockReopenQueue *queue, Error **errp)
727 static BlockDriver bdrv_blkdebug = {
728 .format_name = "blkdebug",
729 .protocol_name = "blkdebug",
730 .instance_size = sizeof(BDRVBlkdebugState),
732 .bdrv_parse_filename = blkdebug_parse_filename,
733 .bdrv_file_open = blkdebug_open,
734 .bdrv_close = blkdebug_close,
735 .bdrv_reopen_prepare = blkdebug_reopen_prepare,
736 .bdrv_getlength = blkdebug_getlength,
737 .bdrv_truncate = blkdebug_truncate,
738 .bdrv_refresh_filename = blkdebug_refresh_filename,
740 .bdrv_aio_readv = blkdebug_aio_readv,
741 .bdrv_aio_writev = blkdebug_aio_writev,
742 .bdrv_aio_flush = blkdebug_aio_flush,
744 .bdrv_debug_event = blkdebug_debug_event,
745 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
746 .bdrv_debug_remove_breakpoint
747 = blkdebug_debug_remove_breakpoint,
748 .bdrv_debug_resume = blkdebug_debug_resume,
749 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
752 static void bdrv_blkdebug_init(void)
754 bdrv_register(&bdrv_blkdebug);
757 block_init(bdrv_blkdebug_init);