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 "qemu-common.h"
27 #include "qemu/config-file.h"
28 #include "block/block_int.h"
29 #include "qemu/module.h"
30 #include "qapi/qmp/qbool.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qapi/qmp/qint.h"
33 #include "qapi/qmp/qstring.h"
34 #include "sysemu/qtest.h"
36 typedef struct BDRVBlkdebugState
{
40 QLIST_HEAD(, BlkdebugRule
) rules
[BLKDBG__MAX
];
41 QSIMPLEQ_HEAD(, BlkdebugRule
) active_rules
;
42 QLIST_HEAD(, BlkdebugSuspendedReq
) suspended_reqs
;
45 typedef struct BlkdebugAIOCB
{
51 typedef struct BlkdebugSuspendedReq
{
54 QLIST_ENTRY(BlkdebugSuspendedReq
) next
;
55 } BlkdebugSuspendedReq
;
57 static const AIOCBInfo blkdebug_aiocb_info
= {
58 .aiocb_size
= sizeof(BlkdebugAIOCB
),
67 typedef struct BlkdebugRule
{
85 QLIST_ENTRY(BlkdebugRule
) next
;
86 QSIMPLEQ_ENTRY(BlkdebugRule
) active_next
;
89 static QemuOptsList inject_error_opts
= {
90 .name
= "inject-error",
91 .head
= QTAILQ_HEAD_INITIALIZER(inject_error_opts
.head
),
95 .type
= QEMU_OPT_STRING
,
99 .type
= QEMU_OPT_NUMBER
,
103 .type
= QEMU_OPT_NUMBER
,
107 .type
= QEMU_OPT_NUMBER
,
111 .type
= QEMU_OPT_BOOL
,
114 .name
= "immediately",
115 .type
= QEMU_OPT_BOOL
,
117 { /* end of list */ }
121 static QemuOptsList set_state_opts
= {
123 .head
= QTAILQ_HEAD_INITIALIZER(set_state_opts
.head
),
127 .type
= QEMU_OPT_STRING
,
131 .type
= QEMU_OPT_NUMBER
,
135 .type
= QEMU_OPT_NUMBER
,
137 { /* end of list */ }
141 static QemuOptsList
*config_groups
[] = {
147 static int get_event_by_name(const char *name
, BlkdebugEvent
*event
)
151 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
152 if (!strcmp(BlkdebugEvent_lookup
[i
], name
)) {
161 struct add_rule_data
{
162 BDRVBlkdebugState
*s
;
166 static int add_rule(void *opaque
, QemuOpts
*opts
, Error
**errp
)
168 struct add_rule_data
*d
= opaque
;
169 BDRVBlkdebugState
*s
= d
->s
;
170 const char* event_name
;
172 struct BlkdebugRule
*rule
;
174 /* Find the right event for the rule */
175 event_name
= qemu_opt_get(opts
, "event");
177 error_setg(errp
, "Missing event name for rule");
179 } else if (get_event_by_name(event_name
, &event
) < 0) {
180 error_setg(errp
, "Invalid event name \"%s\"", event_name
);
184 /* Set attributes common for all actions */
185 rule
= g_malloc0(sizeof(*rule
));
186 *rule
= (struct BlkdebugRule
) {
189 .state
= qemu_opt_get_number(opts
, "state", 0),
192 /* Parse action-specific options */
194 case ACTION_INJECT_ERROR
:
195 rule
->options
.inject
.error
= qemu_opt_get_number(opts
, "errno", EIO
);
196 rule
->options
.inject
.once
= qemu_opt_get_bool(opts
, "once", 0);
197 rule
->options
.inject
.immediately
=
198 qemu_opt_get_bool(opts
, "immediately", 0);
199 rule
->options
.inject
.sector
= qemu_opt_get_number(opts
, "sector", -1);
202 case ACTION_SET_STATE
:
203 rule
->options
.set_state
.new_state
=
204 qemu_opt_get_number(opts
, "new_state", 0);
208 rule
->options
.suspend
.tag
=
209 g_strdup(qemu_opt_get(opts
, "tag"));
214 QLIST_INSERT_HEAD(&s
->rules
[event
], rule
, next
);
219 static void remove_rule(BlkdebugRule
*rule
)
221 switch (rule
->action
) {
222 case ACTION_INJECT_ERROR
:
223 case ACTION_SET_STATE
:
226 g_free(rule
->options
.suspend
.tag
);
230 QLIST_REMOVE(rule
, next
);
234 static int read_config(BDRVBlkdebugState
*s
, const char *filename
,
235 QDict
*options
, Error
**errp
)
239 struct add_rule_data d
;
240 Error
*local_err
= NULL
;
243 f
= fopen(filename
, "r");
245 error_setg_errno(errp
, errno
, "Could not read blkdebug config file");
249 ret
= qemu_config_parse(f
, config_groups
, filename
);
251 error_setg(errp
, "Could not parse blkdebug config file");
257 qemu_config_parse_qdict(options
, config_groups
, &local_err
);
259 error_propagate(errp
, local_err
);
265 d
.action
= ACTION_INJECT_ERROR
;
266 qemu_opts_foreach(&inject_error_opts
, add_rule
, &d
, &local_err
);
268 error_propagate(errp
, local_err
);
273 d
.action
= ACTION_SET_STATE
;
274 qemu_opts_foreach(&set_state_opts
, add_rule
, &d
, &local_err
);
276 error_propagate(errp
, local_err
);
283 qemu_opts_reset(&inject_error_opts
);
284 qemu_opts_reset(&set_state_opts
);
291 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
292 static void blkdebug_parse_filename(const char *filename
, QDict
*options
,
297 /* Parse the blkdebug: prefix */
298 if (!strstart(filename
, "blkdebug:", &filename
)) {
299 /* There was no prefix; therefore, all options have to be already
300 present in the QDict (except for the filename) */
301 qdict_put(options
, "x-image", qstring_from_str(filename
));
305 /* Parse config file path */
306 c
= strchr(filename
, ':');
308 error_setg(errp
, "blkdebug requires both config file and image path");
313 QString
*config_path
;
314 config_path
= qstring_from_substr(filename
, 0, c
- filename
- 1);
315 qdict_put(options
, "config", config_path
);
318 /* TODO Allow multi-level nesting and set file.filename here */
320 qdict_put(options
, "x-image", qstring_from_str(filename
));
323 static QemuOptsList runtime_opts
= {
325 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
329 .type
= QEMU_OPT_STRING
,
330 .help
= "Path to the configuration file",
334 .type
= QEMU_OPT_STRING
,
335 .help
= "[internal use only, will be removed]",
339 .type
= QEMU_OPT_SIZE
,
340 .help
= "Required alignment in bytes",
342 { /* end of list */ }
346 static int blkdebug_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
349 BDRVBlkdebugState
*s
= bs
->opaque
;
351 Error
*local_err
= NULL
;
356 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
357 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
359 error_propagate(errp
, local_err
);
364 /* Read rules from config file or command line options */
365 config
= qemu_opt_get(opts
, "config");
366 ret
= read_config(s
, config
, options
, errp
);
371 /* Set initial state */
374 /* Open the image file */
375 bs
->file
= bdrv_open_child(qemu_opt_get(opts
, "x-image"), options
, "image",
376 bs
, &child_file
, false, &local_err
);
379 error_propagate(errp
, local_err
);
383 /* Set request alignment */
384 align
= qemu_opt_get_size(opts
, "align", bs
->request_alignment
);
385 if (align
> 0 && align
< INT_MAX
&& !(align
& (align
- 1))) {
386 bs
->request_alignment
= align
;
388 error_setg(errp
, "Invalid alignment");
397 bdrv_unref_child(bs
, bs
->file
);
403 static void error_callback_bh(void *opaque
)
405 struct BlkdebugAIOCB
*acb
= opaque
;
406 qemu_bh_delete(acb
->bh
);
407 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
411 static BlockAIOCB
*inject_error(BlockDriverState
*bs
,
412 BlockCompletionFunc
*cb
, void *opaque
, BlkdebugRule
*rule
)
414 BDRVBlkdebugState
*s
= bs
->opaque
;
415 int error
= rule
->options
.inject
.error
;
416 struct BlkdebugAIOCB
*acb
;
418 bool immediately
= rule
->options
.inject
.immediately
;
420 if (rule
->options
.inject
.once
) {
421 QSIMPLEQ_REMOVE(&s
->active_rules
, rule
, BlkdebugRule
, active_next
);
429 acb
= qemu_aio_get(&blkdebug_aiocb_info
, bs
, cb
, opaque
);
432 bh
= aio_bh_new(bdrv_get_aio_context(bs
), error_callback_bh
, acb
);
434 qemu_bh_schedule(bh
);
439 static BlockAIOCB
*blkdebug_aio_readv(BlockDriverState
*bs
,
440 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
441 BlockCompletionFunc
*cb
, void *opaque
)
443 BDRVBlkdebugState
*s
= bs
->opaque
;
444 BlkdebugRule
*rule
= NULL
;
446 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
447 if (rule
->options
.inject
.sector
== -1 ||
448 (rule
->options
.inject
.sector
>= sector_num
&&
449 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
454 if (rule
&& rule
->options
.inject
.error
) {
455 return inject_error(bs
, cb
, opaque
, rule
);
458 return bdrv_aio_readv(bs
->file
->bs
, sector_num
, qiov
, nb_sectors
,
462 static BlockAIOCB
*blkdebug_aio_writev(BlockDriverState
*bs
,
463 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
464 BlockCompletionFunc
*cb
, void *opaque
)
466 BDRVBlkdebugState
*s
= bs
->opaque
;
467 BlkdebugRule
*rule
= NULL
;
469 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
470 if (rule
->options
.inject
.sector
== -1 ||
471 (rule
->options
.inject
.sector
>= sector_num
&&
472 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
477 if (rule
&& rule
->options
.inject
.error
) {
478 return inject_error(bs
, cb
, opaque
, rule
);
481 return bdrv_aio_writev(bs
->file
->bs
, sector_num
, qiov
, nb_sectors
,
485 static BlockAIOCB
*blkdebug_aio_flush(BlockDriverState
*bs
,
486 BlockCompletionFunc
*cb
, void *opaque
)
488 BDRVBlkdebugState
*s
= bs
->opaque
;
489 BlkdebugRule
*rule
= NULL
;
491 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
492 if (rule
->options
.inject
.sector
== -1) {
497 if (rule
&& rule
->options
.inject
.error
) {
498 return inject_error(bs
, cb
, opaque
, rule
);
501 return bdrv_aio_flush(bs
->file
->bs
, cb
, opaque
);
505 static void blkdebug_close(BlockDriverState
*bs
)
507 BDRVBlkdebugState
*s
= bs
->opaque
;
508 BlkdebugRule
*rule
, *next
;
511 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
512 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
518 static void suspend_request(BlockDriverState
*bs
, BlkdebugRule
*rule
)
520 BDRVBlkdebugState
*s
= bs
->opaque
;
521 BlkdebugSuspendedReq r
;
523 r
= (BlkdebugSuspendedReq
) {
524 .co
= qemu_coroutine_self(),
525 .tag
= g_strdup(rule
->options
.suspend
.tag
),
529 QLIST_INSERT_HEAD(&s
->suspended_reqs
, &r
, next
);
531 if (!qtest_enabled()) {
532 printf("blkdebug: Suspended request '%s'\n", r
.tag
);
534 qemu_coroutine_yield();
535 if (!qtest_enabled()) {
536 printf("blkdebug: Resuming request '%s'\n", r
.tag
);
539 QLIST_REMOVE(&r
, next
);
543 static bool process_rule(BlockDriverState
*bs
, struct BlkdebugRule
*rule
,
546 BDRVBlkdebugState
*s
= bs
->opaque
;
548 /* Only process rules for the current state */
549 if (rule
->state
&& rule
->state
!= s
->state
) {
553 /* Take the action */
554 switch (rule
->action
) {
555 case ACTION_INJECT_ERROR
:
557 QSIMPLEQ_INIT(&s
->active_rules
);
560 QSIMPLEQ_INSERT_HEAD(&s
->active_rules
, rule
, active_next
);
563 case ACTION_SET_STATE
:
564 s
->new_state
= rule
->options
.set_state
.new_state
;
568 suspend_request(bs
, rule
);
574 static void blkdebug_debug_event(BlockDriverState
*bs
, BlkdebugEvent event
)
576 BDRVBlkdebugState
*s
= bs
->opaque
;
577 struct BlkdebugRule
*rule
, *next
;
580 assert((int)event
>= 0 && event
< BLKDBG__MAX
);
583 s
->new_state
= s
->state
;
584 QLIST_FOREACH_SAFE(rule
, &s
->rules
[event
], next
, next
) {
585 injected
= process_rule(bs
, rule
, injected
);
587 s
->state
= s
->new_state
;
590 static int blkdebug_debug_breakpoint(BlockDriverState
*bs
, const char *event
,
593 BDRVBlkdebugState
*s
= bs
->opaque
;
594 struct BlkdebugRule
*rule
;
595 BlkdebugEvent blkdebug_event
;
597 if (get_event_by_name(event
, &blkdebug_event
) < 0) {
602 rule
= g_malloc(sizeof(*rule
));
603 *rule
= (struct BlkdebugRule
) {
604 .event
= blkdebug_event
,
605 .action
= ACTION_SUSPEND
,
607 .options
.suspend
.tag
= g_strdup(tag
),
610 QLIST_INSERT_HEAD(&s
->rules
[blkdebug_event
], rule
, next
);
615 static int blkdebug_debug_resume(BlockDriverState
*bs
, const char *tag
)
617 BDRVBlkdebugState
*s
= bs
->opaque
;
618 BlkdebugSuspendedReq
*r
, *next
;
620 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, next
) {
621 if (!strcmp(r
->tag
, tag
)) {
622 qemu_coroutine_enter(r
->co
, NULL
);
629 static int blkdebug_debug_remove_breakpoint(BlockDriverState
*bs
,
632 BDRVBlkdebugState
*s
= bs
->opaque
;
633 BlkdebugSuspendedReq
*r
, *r_next
;
634 BlkdebugRule
*rule
, *next
;
635 int i
, ret
= -ENOENT
;
637 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
638 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
639 if (rule
->action
== ACTION_SUSPEND
&&
640 !strcmp(rule
->options
.suspend
.tag
, tag
)) {
646 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, r_next
) {
647 if (!strcmp(r
->tag
, tag
)) {
648 qemu_coroutine_enter(r
->co
, NULL
);
655 static bool blkdebug_debug_is_suspended(BlockDriverState
*bs
, const char *tag
)
657 BDRVBlkdebugState
*s
= bs
->opaque
;
658 BlkdebugSuspendedReq
*r
;
660 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
661 if (!strcmp(r
->tag
, tag
)) {
668 static int64_t blkdebug_getlength(BlockDriverState
*bs
)
670 return bdrv_getlength(bs
->file
->bs
);
673 static int blkdebug_truncate(BlockDriverState
*bs
, int64_t offset
)
675 return bdrv_truncate(bs
->file
->bs
, offset
);
678 static void blkdebug_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
682 bool force_json
= false;
684 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
685 if (strcmp(qdict_entry_key(e
), "config") &&
686 strcmp(qdict_entry_key(e
), "x-image"))
693 if (force_json
&& !bs
->file
->bs
->full_open_options
) {
694 /* The config file cannot be recreated, so creating a plain filename
699 if (!force_json
&& bs
->file
->bs
->exact_filename
[0]) {
700 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
702 qdict_get_try_str(options
, "config") ?: "",
703 bs
->file
->bs
->exact_filename
);
707 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("blkdebug")));
709 QINCREF(bs
->file
->bs
->full_open_options
);
710 qdict_put_obj(opts
, "image", QOBJECT(bs
->file
->bs
->full_open_options
));
712 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
713 if (strcmp(qdict_entry_key(e
), "x-image")) {
714 qobject_incref(qdict_entry_value(e
));
715 qdict_put_obj(opts
, qdict_entry_key(e
), qdict_entry_value(e
));
719 bs
->full_open_options
= opts
;
722 static int blkdebug_reopen_prepare(BDRVReopenState
*reopen_state
,
723 BlockReopenQueue
*queue
, Error
**errp
)
728 static BlockDriver bdrv_blkdebug
= {
729 .format_name
= "blkdebug",
730 .protocol_name
= "blkdebug",
731 .instance_size
= sizeof(BDRVBlkdebugState
),
733 .bdrv_parse_filename
= blkdebug_parse_filename
,
734 .bdrv_file_open
= blkdebug_open
,
735 .bdrv_close
= blkdebug_close
,
736 .bdrv_reopen_prepare
= blkdebug_reopen_prepare
,
737 .bdrv_getlength
= blkdebug_getlength
,
738 .bdrv_truncate
= blkdebug_truncate
,
739 .bdrv_refresh_filename
= blkdebug_refresh_filename
,
741 .bdrv_aio_readv
= blkdebug_aio_readv
,
742 .bdrv_aio_writev
= blkdebug_aio_writev
,
743 .bdrv_aio_flush
= blkdebug_aio_flush
,
745 .bdrv_debug_event
= blkdebug_debug_event
,
746 .bdrv_debug_breakpoint
= blkdebug_debug_breakpoint
,
747 .bdrv_debug_remove_breakpoint
748 = blkdebug_debug_remove_breakpoint
,
749 .bdrv_debug_resume
= blkdebug_debug_resume
,
750 .bdrv_debug_is_suspended
= blkdebug_debug_is_suspended
,
753 static void bdrv_blkdebug_init(void)
755 bdrv_register(&bdrv_blkdebug
);
758 block_init(bdrv_blkdebug_init
);