2 * Block protocol for I/O error injection
4 * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
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/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/cutils.h"
28 #include "qemu/config-file.h"
29 #include "block/block_int.h"
30 #include "qemu/module.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qapi/qmp/qdict.h"
33 #include "qapi/qmp/qint.h"
34 #include "qapi/qmp/qstring.h"
35 #include "sysemu/qtest.h"
37 typedef struct BDRVBlkdebugState
{
41 QLIST_HEAD(, BlkdebugRule
) rules
[BLKDBG__MAX
];
42 QSIMPLEQ_HEAD(, BlkdebugRule
) active_rules
;
43 QLIST_HEAD(, BlkdebugSuspendedReq
) suspended_reqs
;
46 typedef struct BlkdebugAIOCB
{
52 typedef struct BlkdebugSuspendedReq
{
55 QLIST_ENTRY(BlkdebugSuspendedReq
) next
;
56 } BlkdebugSuspendedReq
;
58 static const AIOCBInfo blkdebug_aiocb_info
= {
59 .aiocb_size
= sizeof(BlkdebugAIOCB
),
68 typedef struct BlkdebugRule
{
86 QLIST_ENTRY(BlkdebugRule
) next
;
87 QSIMPLEQ_ENTRY(BlkdebugRule
) active_next
;
90 static QemuOptsList inject_error_opts
= {
91 .name
= "inject-error",
92 .head
= QTAILQ_HEAD_INITIALIZER(inject_error_opts
.head
),
96 .type
= QEMU_OPT_STRING
,
100 .type
= QEMU_OPT_NUMBER
,
104 .type
= QEMU_OPT_NUMBER
,
108 .type
= QEMU_OPT_NUMBER
,
112 .type
= QEMU_OPT_BOOL
,
115 .name
= "immediately",
116 .type
= QEMU_OPT_BOOL
,
118 { /* end of list */ }
122 static QemuOptsList set_state_opts
= {
124 .head
= QTAILQ_HEAD_INITIALIZER(set_state_opts
.head
),
128 .type
= QEMU_OPT_STRING
,
132 .type
= QEMU_OPT_NUMBER
,
136 .type
= QEMU_OPT_NUMBER
,
138 { /* end of list */ }
142 static QemuOptsList
*config_groups
[] = {
148 static int get_event_by_name(const char *name
, BlkdebugEvent
*event
)
152 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
153 if (!strcmp(BlkdebugEvent_lookup
[i
], name
)) {
162 struct add_rule_data
{
163 BDRVBlkdebugState
*s
;
167 static int add_rule(void *opaque
, QemuOpts
*opts
, Error
**errp
)
169 struct add_rule_data
*d
= opaque
;
170 BDRVBlkdebugState
*s
= d
->s
;
171 const char* event_name
;
173 struct BlkdebugRule
*rule
;
175 /* Find the right event for the rule */
176 event_name
= qemu_opt_get(opts
, "event");
178 error_setg(errp
, "Missing event name for rule");
180 } else if (get_event_by_name(event_name
, &event
) < 0) {
181 error_setg(errp
, "Invalid event name \"%s\"", event_name
);
185 /* Set attributes common for all actions */
186 rule
= g_malloc0(sizeof(*rule
));
187 *rule
= (struct BlkdebugRule
) {
190 .state
= qemu_opt_get_number(opts
, "state", 0),
193 /* Parse action-specific options */
195 case ACTION_INJECT_ERROR
:
196 rule
->options
.inject
.error
= qemu_opt_get_number(opts
, "errno", EIO
);
197 rule
->options
.inject
.once
= qemu_opt_get_bool(opts
, "once", 0);
198 rule
->options
.inject
.immediately
=
199 qemu_opt_get_bool(opts
, "immediately", 0);
200 rule
->options
.inject
.sector
= qemu_opt_get_number(opts
, "sector", -1);
203 case ACTION_SET_STATE
:
204 rule
->options
.set_state
.new_state
=
205 qemu_opt_get_number(opts
, "new_state", 0);
209 rule
->options
.suspend
.tag
=
210 g_strdup(qemu_opt_get(opts
, "tag"));
215 QLIST_INSERT_HEAD(&s
->rules
[event
], rule
, next
);
220 static void remove_rule(BlkdebugRule
*rule
)
222 switch (rule
->action
) {
223 case ACTION_INJECT_ERROR
:
224 case ACTION_SET_STATE
:
227 g_free(rule
->options
.suspend
.tag
);
231 QLIST_REMOVE(rule
, next
);
235 static int read_config(BDRVBlkdebugState
*s
, const char *filename
,
236 QDict
*options
, Error
**errp
)
240 struct add_rule_data d
;
241 Error
*local_err
= NULL
;
244 f
= fopen(filename
, "r");
246 error_setg_errno(errp
, errno
, "Could not read blkdebug config file");
250 ret
= qemu_config_parse(f
, config_groups
, filename
);
252 error_setg(errp
, "Could not parse blkdebug config file");
258 qemu_config_parse_qdict(options
, config_groups
, &local_err
);
260 error_propagate(errp
, local_err
);
266 d
.action
= ACTION_INJECT_ERROR
;
267 qemu_opts_foreach(&inject_error_opts
, add_rule
, &d
, &local_err
);
269 error_propagate(errp
, local_err
);
274 d
.action
= ACTION_SET_STATE
;
275 qemu_opts_foreach(&set_state_opts
, add_rule
, &d
, &local_err
);
277 error_propagate(errp
, local_err
);
284 qemu_opts_reset(&inject_error_opts
);
285 qemu_opts_reset(&set_state_opts
);
292 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
293 static void blkdebug_parse_filename(const char *filename
, QDict
*options
,
298 /* Parse the blkdebug: prefix */
299 if (!strstart(filename
, "blkdebug:", &filename
)) {
300 /* There was no prefix; therefore, all options have to be already
301 present in the QDict (except for the filename) */
302 qdict_put(options
, "x-image", qstring_from_str(filename
));
306 /* Parse config file path */
307 c
= strchr(filename
, ':');
309 error_setg(errp
, "blkdebug requires both config file and image path");
314 QString
*config_path
;
315 config_path
= qstring_from_substr(filename
, 0, c
- filename
- 1);
316 qdict_put(options
, "config", config_path
);
319 /* TODO Allow multi-level nesting and set file.filename here */
321 qdict_put(options
, "x-image", qstring_from_str(filename
));
324 static QemuOptsList runtime_opts
= {
326 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
330 .type
= QEMU_OPT_STRING
,
331 .help
= "Path to the configuration file",
335 .type
= QEMU_OPT_STRING
,
336 .help
= "[internal use only, will be removed]",
340 .type
= QEMU_OPT_SIZE
,
341 .help
= "Required alignment in bytes",
343 { /* end of list */ }
347 static int blkdebug_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
350 BDRVBlkdebugState
*s
= bs
->opaque
;
352 Error
*local_err
= NULL
;
357 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
358 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
360 error_propagate(errp
, local_err
);
365 /* Read rules from config file or command line options */
366 config
= qemu_opt_get(opts
, "config");
367 ret
= read_config(s
, config
, options
, errp
);
372 /* Set initial state */
375 /* Open the image file */
376 bs
->file
= bdrv_open_child(qemu_opt_get(opts
, "x-image"), options
, "image",
377 bs
, &child_file
, false, &local_err
);
380 error_propagate(errp
, local_err
);
384 /* Set request alignment */
385 align
= qemu_opt_get_size(opts
, "align", bs
->request_alignment
);
386 if (align
> 0 && align
< INT_MAX
&& !(align
& (align
- 1))) {
387 bs
->request_alignment
= align
;
389 error_setg(errp
, "Invalid alignment");
398 bdrv_unref_child(bs
, bs
->file
);
404 static void error_callback_bh(void *opaque
)
406 struct BlkdebugAIOCB
*acb
= opaque
;
407 qemu_bh_delete(acb
->bh
);
408 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
412 static BlockAIOCB
*inject_error(BlockDriverState
*bs
,
413 BlockCompletionFunc
*cb
, void *opaque
, BlkdebugRule
*rule
)
415 BDRVBlkdebugState
*s
= bs
->opaque
;
416 int error
= rule
->options
.inject
.error
;
417 struct BlkdebugAIOCB
*acb
;
419 bool immediately
= rule
->options
.inject
.immediately
;
421 if (rule
->options
.inject
.once
) {
422 QSIMPLEQ_REMOVE(&s
->active_rules
, rule
, BlkdebugRule
, active_next
);
430 acb
= qemu_aio_get(&blkdebug_aiocb_info
, bs
, cb
, opaque
);
433 bh
= aio_bh_new(bdrv_get_aio_context(bs
), error_callback_bh
, acb
);
435 qemu_bh_schedule(bh
);
440 static BlockAIOCB
*blkdebug_aio_readv(BlockDriverState
*bs
,
441 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
442 BlockCompletionFunc
*cb
, void *opaque
)
444 BDRVBlkdebugState
*s
= bs
->opaque
;
445 BlkdebugRule
*rule
= NULL
;
447 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
448 if (rule
->options
.inject
.sector
== -1 ||
449 (rule
->options
.inject
.sector
>= sector_num
&&
450 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
455 if (rule
&& rule
->options
.inject
.error
) {
456 return inject_error(bs
, cb
, opaque
, rule
);
459 return bdrv_aio_readv(bs
->file
->bs
, sector_num
, qiov
, nb_sectors
,
463 static BlockAIOCB
*blkdebug_aio_writev(BlockDriverState
*bs
,
464 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
465 BlockCompletionFunc
*cb
, void *opaque
)
467 BDRVBlkdebugState
*s
= bs
->opaque
;
468 BlkdebugRule
*rule
= NULL
;
470 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
471 if (rule
->options
.inject
.sector
== -1 ||
472 (rule
->options
.inject
.sector
>= sector_num
&&
473 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
478 if (rule
&& rule
->options
.inject
.error
) {
479 return inject_error(bs
, cb
, opaque
, rule
);
482 return bdrv_aio_writev(bs
->file
->bs
, sector_num
, qiov
, nb_sectors
,
486 static BlockAIOCB
*blkdebug_aio_flush(BlockDriverState
*bs
,
487 BlockCompletionFunc
*cb
, void *opaque
)
489 BDRVBlkdebugState
*s
= bs
->opaque
;
490 BlkdebugRule
*rule
= NULL
;
492 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
493 if (rule
->options
.inject
.sector
== -1) {
498 if (rule
&& rule
->options
.inject
.error
) {
499 return inject_error(bs
, cb
, opaque
, rule
);
502 return bdrv_aio_flush(bs
->file
->bs
, cb
, opaque
);
506 static void blkdebug_close(BlockDriverState
*bs
)
508 BDRVBlkdebugState
*s
= bs
->opaque
;
509 BlkdebugRule
*rule
, *next
;
512 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
513 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
519 static void suspend_request(BlockDriverState
*bs
, BlkdebugRule
*rule
)
521 BDRVBlkdebugState
*s
= bs
->opaque
;
522 BlkdebugSuspendedReq r
;
524 r
= (BlkdebugSuspendedReq
) {
525 .co
= qemu_coroutine_self(),
526 .tag
= g_strdup(rule
->options
.suspend
.tag
),
530 QLIST_INSERT_HEAD(&s
->suspended_reqs
, &r
, next
);
532 if (!qtest_enabled()) {
533 printf("blkdebug: Suspended request '%s'\n", r
.tag
);
535 qemu_coroutine_yield();
536 if (!qtest_enabled()) {
537 printf("blkdebug: Resuming request '%s'\n", r
.tag
);
540 QLIST_REMOVE(&r
, next
);
544 static bool process_rule(BlockDriverState
*bs
, struct BlkdebugRule
*rule
,
547 BDRVBlkdebugState
*s
= bs
->opaque
;
549 /* Only process rules for the current state */
550 if (rule
->state
&& rule
->state
!= s
->state
) {
554 /* Take the action */
555 switch (rule
->action
) {
556 case ACTION_INJECT_ERROR
:
558 QSIMPLEQ_INIT(&s
->active_rules
);
561 QSIMPLEQ_INSERT_HEAD(&s
->active_rules
, rule
, active_next
);
564 case ACTION_SET_STATE
:
565 s
->new_state
= rule
->options
.set_state
.new_state
;
569 suspend_request(bs
, rule
);
575 static void blkdebug_debug_event(BlockDriverState
*bs
, BlkdebugEvent event
)
577 BDRVBlkdebugState
*s
= bs
->opaque
;
578 struct BlkdebugRule
*rule
, *next
;
581 assert((int)event
>= 0 && event
< BLKDBG__MAX
);
584 s
->new_state
= s
->state
;
585 QLIST_FOREACH_SAFE(rule
, &s
->rules
[event
], next
, next
) {
586 injected
= process_rule(bs
, rule
, injected
);
588 s
->state
= s
->new_state
;
591 static int blkdebug_debug_breakpoint(BlockDriverState
*bs
, const char *event
,
594 BDRVBlkdebugState
*s
= bs
->opaque
;
595 struct BlkdebugRule
*rule
;
596 BlkdebugEvent blkdebug_event
;
598 if (get_event_by_name(event
, &blkdebug_event
) < 0) {
603 rule
= g_malloc(sizeof(*rule
));
604 *rule
= (struct BlkdebugRule
) {
605 .event
= blkdebug_event
,
606 .action
= ACTION_SUSPEND
,
608 .options
.suspend
.tag
= g_strdup(tag
),
611 QLIST_INSERT_HEAD(&s
->rules
[blkdebug_event
], rule
, next
);
616 static int blkdebug_debug_resume(BlockDriverState
*bs
, const char *tag
)
618 BDRVBlkdebugState
*s
= bs
->opaque
;
619 BlkdebugSuspendedReq
*r
, *next
;
621 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, next
) {
622 if (!strcmp(r
->tag
, tag
)) {
623 qemu_coroutine_enter(r
->co
, NULL
);
630 static int blkdebug_debug_remove_breakpoint(BlockDriverState
*bs
,
633 BDRVBlkdebugState
*s
= bs
->opaque
;
634 BlkdebugSuspendedReq
*r
, *r_next
;
635 BlkdebugRule
*rule
, *next
;
636 int i
, ret
= -ENOENT
;
638 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
639 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
640 if (rule
->action
== ACTION_SUSPEND
&&
641 !strcmp(rule
->options
.suspend
.tag
, tag
)) {
647 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, r_next
) {
648 if (!strcmp(r
->tag
, tag
)) {
649 qemu_coroutine_enter(r
->co
, NULL
);
656 static bool blkdebug_debug_is_suspended(BlockDriverState
*bs
, const char *tag
)
658 BDRVBlkdebugState
*s
= bs
->opaque
;
659 BlkdebugSuspendedReq
*r
;
661 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
662 if (!strcmp(r
->tag
, tag
)) {
669 static int64_t blkdebug_getlength(BlockDriverState
*bs
)
671 return bdrv_getlength(bs
->file
->bs
);
674 static int blkdebug_truncate(BlockDriverState
*bs
, int64_t offset
)
676 return bdrv_truncate(bs
->file
->bs
, offset
);
679 static void blkdebug_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
683 bool force_json
= false;
685 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
686 if (strcmp(qdict_entry_key(e
), "config") &&
687 strcmp(qdict_entry_key(e
), "x-image"))
694 if (force_json
&& !bs
->file
->bs
->full_open_options
) {
695 /* The config file cannot be recreated, so creating a plain filename
700 if (!force_json
&& bs
->file
->bs
->exact_filename
[0]) {
701 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
703 qdict_get_try_str(options
, "config") ?: "",
704 bs
->file
->bs
->exact_filename
);
708 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("blkdebug")));
710 QINCREF(bs
->file
->bs
->full_open_options
);
711 qdict_put_obj(opts
, "image", QOBJECT(bs
->file
->bs
->full_open_options
));
713 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
714 if (strcmp(qdict_entry_key(e
), "x-image")) {
715 qobject_incref(qdict_entry_value(e
));
716 qdict_put_obj(opts
, qdict_entry_key(e
), qdict_entry_value(e
));
720 bs
->full_open_options
= opts
;
723 static int blkdebug_reopen_prepare(BDRVReopenState
*reopen_state
,
724 BlockReopenQueue
*queue
, Error
**errp
)
729 static BlockDriver bdrv_blkdebug
= {
730 .format_name
= "blkdebug",
731 .protocol_name
= "blkdebug",
732 .instance_size
= sizeof(BDRVBlkdebugState
),
734 .bdrv_parse_filename
= blkdebug_parse_filename
,
735 .bdrv_file_open
= blkdebug_open
,
736 .bdrv_close
= blkdebug_close
,
737 .bdrv_reopen_prepare
= blkdebug_reopen_prepare
,
738 .bdrv_getlength
= blkdebug_getlength
,
739 .bdrv_truncate
= blkdebug_truncate
,
740 .bdrv_refresh_filename
= blkdebug_refresh_filename
,
742 .bdrv_aio_readv
= blkdebug_aio_readv
,
743 .bdrv_aio_writev
= blkdebug_aio_writev
,
744 .bdrv_aio_flush
= blkdebug_aio_flush
,
746 .bdrv_debug_event
= blkdebug_debug_event
,
747 .bdrv_debug_breakpoint
= blkdebug_debug_breakpoint
,
748 .bdrv_debug_remove_breakpoint
749 = blkdebug_debug_remove_breakpoint
,
750 .bdrv_debug_resume
= blkdebug_debug_resume
,
751 .bdrv_debug_is_suspended
= blkdebug_debug_is_suspended
,
754 static void bdrv_blkdebug_init(void)
756 bdrv_register(&bdrv_blkdebug
);
759 block_init(bdrv_blkdebug_init
);