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
{
42 /* For blkdebug_refresh_filename() */
45 QLIST_HEAD(, BlkdebugRule
) rules
[BLKDBG__MAX
];
46 QSIMPLEQ_HEAD(, BlkdebugRule
) active_rules
;
47 QLIST_HEAD(, BlkdebugSuspendedReq
) suspended_reqs
;
50 typedef struct BlkdebugAIOCB
{
56 typedef struct BlkdebugSuspendedReq
{
59 QLIST_ENTRY(BlkdebugSuspendedReq
) next
;
60 } BlkdebugSuspendedReq
;
62 static const AIOCBInfo blkdebug_aiocb_info
= {
63 .aiocb_size
= sizeof(BlkdebugAIOCB
),
72 typedef struct BlkdebugRule
{
90 QLIST_ENTRY(BlkdebugRule
) next
;
91 QSIMPLEQ_ENTRY(BlkdebugRule
) active_next
;
94 static QemuOptsList inject_error_opts
= {
95 .name
= "inject-error",
96 .head
= QTAILQ_HEAD_INITIALIZER(inject_error_opts
.head
),
100 .type
= QEMU_OPT_STRING
,
104 .type
= QEMU_OPT_NUMBER
,
108 .type
= QEMU_OPT_NUMBER
,
112 .type
= QEMU_OPT_NUMBER
,
116 .type
= QEMU_OPT_BOOL
,
119 .name
= "immediately",
120 .type
= QEMU_OPT_BOOL
,
122 { /* end of list */ }
126 static QemuOptsList set_state_opts
= {
128 .head
= QTAILQ_HEAD_INITIALIZER(set_state_opts
.head
),
132 .type
= QEMU_OPT_STRING
,
136 .type
= QEMU_OPT_NUMBER
,
140 .type
= QEMU_OPT_NUMBER
,
142 { /* end of list */ }
146 static QemuOptsList
*config_groups
[] = {
152 static int get_event_by_name(const char *name
, BlkdebugEvent
*event
)
156 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
157 if (!strcmp(BlkdebugEvent_lookup
[i
], name
)) {
166 struct add_rule_data
{
167 BDRVBlkdebugState
*s
;
171 static int add_rule(void *opaque
, QemuOpts
*opts
, Error
**errp
)
173 struct add_rule_data
*d
= opaque
;
174 BDRVBlkdebugState
*s
= d
->s
;
175 const char* event_name
;
177 struct BlkdebugRule
*rule
;
179 /* Find the right event for the rule */
180 event_name
= qemu_opt_get(opts
, "event");
182 error_setg(errp
, "Missing event name for rule");
184 } else if (get_event_by_name(event_name
, &event
) < 0) {
185 error_setg(errp
, "Invalid event name \"%s\"", event_name
);
189 /* Set attributes common for all actions */
190 rule
= g_malloc0(sizeof(*rule
));
191 *rule
= (struct BlkdebugRule
) {
194 .state
= qemu_opt_get_number(opts
, "state", 0),
197 /* Parse action-specific options */
199 case ACTION_INJECT_ERROR
:
200 rule
->options
.inject
.error
= qemu_opt_get_number(opts
, "errno", EIO
);
201 rule
->options
.inject
.once
= qemu_opt_get_bool(opts
, "once", 0);
202 rule
->options
.inject
.immediately
=
203 qemu_opt_get_bool(opts
, "immediately", 0);
204 rule
->options
.inject
.sector
= qemu_opt_get_number(opts
, "sector", -1);
207 case ACTION_SET_STATE
:
208 rule
->options
.set_state
.new_state
=
209 qemu_opt_get_number(opts
, "new_state", 0);
213 rule
->options
.suspend
.tag
=
214 g_strdup(qemu_opt_get(opts
, "tag"));
219 QLIST_INSERT_HEAD(&s
->rules
[event
], rule
, next
);
224 static void remove_rule(BlkdebugRule
*rule
)
226 switch (rule
->action
) {
227 case ACTION_INJECT_ERROR
:
228 case ACTION_SET_STATE
:
231 g_free(rule
->options
.suspend
.tag
);
235 QLIST_REMOVE(rule
, next
);
239 static int read_config(BDRVBlkdebugState
*s
, const char *filename
,
240 QDict
*options
, Error
**errp
)
244 struct add_rule_data d
;
245 Error
*local_err
= NULL
;
248 f
= fopen(filename
, "r");
250 error_setg_errno(errp
, errno
, "Could not read blkdebug config file");
254 ret
= qemu_config_parse(f
, config_groups
, filename
);
256 error_setg(errp
, "Could not parse blkdebug config file");
262 qemu_config_parse_qdict(options
, config_groups
, &local_err
);
264 error_propagate(errp
, local_err
);
270 d
.action
= ACTION_INJECT_ERROR
;
271 qemu_opts_foreach(&inject_error_opts
, add_rule
, &d
, &local_err
);
273 error_propagate(errp
, local_err
);
278 d
.action
= ACTION_SET_STATE
;
279 qemu_opts_foreach(&set_state_opts
, add_rule
, &d
, &local_err
);
281 error_propagate(errp
, local_err
);
288 qemu_opts_reset(&inject_error_opts
);
289 qemu_opts_reset(&set_state_opts
);
296 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
297 static void blkdebug_parse_filename(const char *filename
, QDict
*options
,
302 /* Parse the blkdebug: prefix */
303 if (!strstart(filename
, "blkdebug:", &filename
)) {
304 /* There was no prefix; therefore, all options have to be already
305 present in the QDict (except for the filename) */
306 qdict_put(options
, "x-image", qstring_from_str(filename
));
310 /* Parse config file path */
311 c
= strchr(filename
, ':');
313 error_setg(errp
, "blkdebug requires both config file and image path");
318 QString
*config_path
;
319 config_path
= qstring_from_substr(filename
, 0, c
- filename
- 1);
320 qdict_put(options
, "config", config_path
);
323 /* TODO Allow multi-level nesting and set file.filename here */
325 qdict_put(options
, "x-image", qstring_from_str(filename
));
328 static QemuOptsList runtime_opts
= {
330 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
334 .type
= QEMU_OPT_STRING
,
335 .help
= "Path to the configuration file",
339 .type
= QEMU_OPT_STRING
,
340 .help
= "[internal use only, will be removed]",
344 .type
= QEMU_OPT_SIZE
,
345 .help
= "Required alignment in bytes",
347 { /* end of list */ }
351 static int blkdebug_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
354 BDRVBlkdebugState
*s
= bs
->opaque
;
356 Error
*local_err
= NULL
;
360 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
361 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
363 error_propagate(errp
, local_err
);
368 /* Read rules from config file or command line options */
369 s
->config_file
= g_strdup(qemu_opt_get(opts
, "config"));
370 ret
= read_config(s
, s
->config_file
, options
, errp
);
375 /* Set initial state */
378 /* Open the image file */
379 bs
->file
= bdrv_open_child(qemu_opt_get(opts
, "x-image"), options
, "image",
380 bs
, &child_file
, false, &local_err
);
383 error_propagate(errp
, local_err
);
387 /* Set request alignment */
388 align
= qemu_opt_get_size(opts
, "align", 0);
389 if (align
< INT_MAX
&& is_power_of_2(align
)) {
392 error_setg(errp
, "Invalid alignment");
401 bdrv_unref_child(bs
, bs
->file
);
404 g_free(s
->config_file
);
410 static void error_callback_bh(void *opaque
)
412 struct BlkdebugAIOCB
*acb
= opaque
;
413 qemu_bh_delete(acb
->bh
);
414 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
418 static BlockAIOCB
*inject_error(BlockDriverState
*bs
,
419 BlockCompletionFunc
*cb
, void *opaque
, BlkdebugRule
*rule
)
421 BDRVBlkdebugState
*s
= bs
->opaque
;
422 int error
= rule
->options
.inject
.error
;
423 struct BlkdebugAIOCB
*acb
;
425 bool immediately
= rule
->options
.inject
.immediately
;
427 if (rule
->options
.inject
.once
) {
428 QSIMPLEQ_REMOVE(&s
->active_rules
, rule
, BlkdebugRule
, active_next
);
436 acb
= qemu_aio_get(&blkdebug_aiocb_info
, bs
, cb
, opaque
);
439 bh
= aio_bh_new(bdrv_get_aio_context(bs
), error_callback_bh
, acb
);
441 qemu_bh_schedule(bh
);
446 static BlockAIOCB
*blkdebug_aio_readv(BlockDriverState
*bs
,
447 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
448 BlockCompletionFunc
*cb
, void *opaque
)
450 BDRVBlkdebugState
*s
= bs
->opaque
;
451 BlkdebugRule
*rule
= NULL
;
453 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
454 if (rule
->options
.inject
.sector
== -1 ||
455 (rule
->options
.inject
.sector
>= sector_num
&&
456 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
461 if (rule
&& rule
->options
.inject
.error
) {
462 return inject_error(bs
, cb
, opaque
, rule
);
465 return bdrv_aio_readv(bs
->file
, sector_num
, qiov
, nb_sectors
,
469 static BlockAIOCB
*blkdebug_aio_writev(BlockDriverState
*bs
,
470 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
471 BlockCompletionFunc
*cb
, void *opaque
)
473 BDRVBlkdebugState
*s
= bs
->opaque
;
474 BlkdebugRule
*rule
= NULL
;
476 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
477 if (rule
->options
.inject
.sector
== -1 ||
478 (rule
->options
.inject
.sector
>= sector_num
&&
479 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
484 if (rule
&& rule
->options
.inject
.error
) {
485 return inject_error(bs
, cb
, opaque
, rule
);
488 return bdrv_aio_writev(bs
->file
, sector_num
, qiov
, nb_sectors
,
492 static BlockAIOCB
*blkdebug_aio_flush(BlockDriverState
*bs
,
493 BlockCompletionFunc
*cb
, void *opaque
)
495 BDRVBlkdebugState
*s
= bs
->opaque
;
496 BlkdebugRule
*rule
= NULL
;
498 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
499 if (rule
->options
.inject
.sector
== -1) {
504 if (rule
&& rule
->options
.inject
.error
) {
505 return inject_error(bs
, cb
, opaque
, rule
);
508 return bdrv_aio_flush(bs
->file
->bs
, cb
, opaque
);
512 static void blkdebug_close(BlockDriverState
*bs
)
514 BDRVBlkdebugState
*s
= bs
->opaque
;
515 BlkdebugRule
*rule
, *next
;
518 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
519 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
524 g_free(s
->config_file
);
527 static void suspend_request(BlockDriverState
*bs
, BlkdebugRule
*rule
)
529 BDRVBlkdebugState
*s
= bs
->opaque
;
530 BlkdebugSuspendedReq r
;
532 r
= (BlkdebugSuspendedReq
) {
533 .co
= qemu_coroutine_self(),
534 .tag
= g_strdup(rule
->options
.suspend
.tag
),
538 QLIST_INSERT_HEAD(&s
->suspended_reqs
, &r
, next
);
540 if (!qtest_enabled()) {
541 printf("blkdebug: Suspended request '%s'\n", r
.tag
);
543 qemu_coroutine_yield();
544 if (!qtest_enabled()) {
545 printf("blkdebug: Resuming request '%s'\n", r
.tag
);
548 QLIST_REMOVE(&r
, next
);
552 static bool process_rule(BlockDriverState
*bs
, struct BlkdebugRule
*rule
,
555 BDRVBlkdebugState
*s
= bs
->opaque
;
557 /* Only process rules for the current state */
558 if (rule
->state
&& rule
->state
!= s
->state
) {
562 /* Take the action */
563 switch (rule
->action
) {
564 case ACTION_INJECT_ERROR
:
566 QSIMPLEQ_INIT(&s
->active_rules
);
569 QSIMPLEQ_INSERT_HEAD(&s
->active_rules
, rule
, active_next
);
572 case ACTION_SET_STATE
:
573 s
->new_state
= rule
->options
.set_state
.new_state
;
577 suspend_request(bs
, rule
);
583 static void blkdebug_debug_event(BlockDriverState
*bs
, BlkdebugEvent event
)
585 BDRVBlkdebugState
*s
= bs
->opaque
;
586 struct BlkdebugRule
*rule
, *next
;
589 assert((int)event
>= 0 && event
< BLKDBG__MAX
);
592 s
->new_state
= s
->state
;
593 QLIST_FOREACH_SAFE(rule
, &s
->rules
[event
], next
, next
) {
594 injected
= process_rule(bs
, rule
, injected
);
596 s
->state
= s
->new_state
;
599 static int blkdebug_debug_breakpoint(BlockDriverState
*bs
, const char *event
,
602 BDRVBlkdebugState
*s
= bs
->opaque
;
603 struct BlkdebugRule
*rule
;
604 BlkdebugEvent blkdebug_event
;
606 if (get_event_by_name(event
, &blkdebug_event
) < 0) {
611 rule
= g_malloc(sizeof(*rule
));
612 *rule
= (struct BlkdebugRule
) {
613 .event
= blkdebug_event
,
614 .action
= ACTION_SUSPEND
,
616 .options
.suspend
.tag
= g_strdup(tag
),
619 QLIST_INSERT_HEAD(&s
->rules
[blkdebug_event
], rule
, next
);
624 static int blkdebug_debug_resume(BlockDriverState
*bs
, const char *tag
)
626 BDRVBlkdebugState
*s
= bs
->opaque
;
627 BlkdebugSuspendedReq
*r
, *next
;
629 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, next
) {
630 if (!strcmp(r
->tag
, tag
)) {
631 qemu_coroutine_enter(r
->co
);
638 static int blkdebug_debug_remove_breakpoint(BlockDriverState
*bs
,
641 BDRVBlkdebugState
*s
= bs
->opaque
;
642 BlkdebugSuspendedReq
*r
, *r_next
;
643 BlkdebugRule
*rule
, *next
;
644 int i
, ret
= -ENOENT
;
646 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
647 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
648 if (rule
->action
== ACTION_SUSPEND
&&
649 !strcmp(rule
->options
.suspend
.tag
, tag
)) {
655 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, r_next
) {
656 if (!strcmp(r
->tag
, tag
)) {
657 qemu_coroutine_enter(r
->co
);
664 static bool blkdebug_debug_is_suspended(BlockDriverState
*bs
, const char *tag
)
666 BDRVBlkdebugState
*s
= bs
->opaque
;
667 BlkdebugSuspendedReq
*r
;
669 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
670 if (!strcmp(r
->tag
, tag
)) {
677 static int64_t blkdebug_getlength(BlockDriverState
*bs
)
679 return bdrv_getlength(bs
->file
->bs
);
682 static int blkdebug_truncate(BlockDriverState
*bs
, int64_t offset
)
684 return bdrv_truncate(bs
->file
->bs
, offset
);
687 static void blkdebug_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
689 BDRVBlkdebugState
*s
= bs
->opaque
;
692 bool force_json
= false;
694 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
695 if (strcmp(qdict_entry_key(e
), "config") &&
696 strcmp(qdict_entry_key(e
), "x-image"))
703 if (force_json
&& !bs
->file
->bs
->full_open_options
) {
704 /* The config file cannot be recreated, so creating a plain filename
709 if (!force_json
&& bs
->file
->bs
->exact_filename
[0]) {
710 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
711 "blkdebug:%s:%s", s
->config_file
?: "",
712 bs
->file
->bs
->exact_filename
);
716 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("blkdebug")));
718 QINCREF(bs
->file
->bs
->full_open_options
);
719 qdict_put_obj(opts
, "image", QOBJECT(bs
->file
->bs
->full_open_options
));
721 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
722 if (strcmp(qdict_entry_key(e
), "x-image")) {
723 qobject_incref(qdict_entry_value(e
));
724 qdict_put_obj(opts
, qdict_entry_key(e
), qdict_entry_value(e
));
728 bs
->full_open_options
= opts
;
731 static void blkdebug_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
733 BDRVBlkdebugState
*s
= bs
->opaque
;
736 bs
->bl
.request_alignment
= s
->align
;
740 static int blkdebug_reopen_prepare(BDRVReopenState
*reopen_state
,
741 BlockReopenQueue
*queue
, Error
**errp
)
746 static BlockDriver bdrv_blkdebug
= {
747 .format_name
= "blkdebug",
748 .protocol_name
= "blkdebug",
749 .instance_size
= sizeof(BDRVBlkdebugState
),
751 .bdrv_parse_filename
= blkdebug_parse_filename
,
752 .bdrv_file_open
= blkdebug_open
,
753 .bdrv_close
= blkdebug_close
,
754 .bdrv_reopen_prepare
= blkdebug_reopen_prepare
,
755 .bdrv_getlength
= blkdebug_getlength
,
756 .bdrv_truncate
= blkdebug_truncate
,
757 .bdrv_refresh_filename
= blkdebug_refresh_filename
,
758 .bdrv_refresh_limits
= blkdebug_refresh_limits
,
760 .bdrv_aio_readv
= blkdebug_aio_readv
,
761 .bdrv_aio_writev
= blkdebug_aio_writev
,
762 .bdrv_aio_flush
= blkdebug_aio_flush
,
764 .bdrv_debug_event
= blkdebug_debug_event
,
765 .bdrv_debug_breakpoint
= blkdebug_debug_breakpoint
,
766 .bdrv_debug_remove_breakpoint
767 = blkdebug_debug_remove_breakpoint
,
768 .bdrv_debug_resume
= blkdebug_debug_resume
,
769 .bdrv_debug_is_suspended
= blkdebug_debug_is_suspended
,
772 static void bdrv_blkdebug_init(void)
774 bdrv_register(&bdrv_blkdebug
);
777 block_init(bdrv_blkdebug_init
);