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
{
55 typedef struct BlkdebugSuspendedReq
{
58 QLIST_ENTRY(BlkdebugSuspendedReq
) next
;
59 } BlkdebugSuspendedReq
;
61 static const AIOCBInfo blkdebug_aiocb_info
= {
62 .aiocb_size
= sizeof(BlkdebugAIOCB
),
71 typedef struct BlkdebugRule
{
89 QLIST_ENTRY(BlkdebugRule
) next
;
90 QSIMPLEQ_ENTRY(BlkdebugRule
) active_next
;
93 static QemuOptsList inject_error_opts
= {
94 .name
= "inject-error",
95 .head
= QTAILQ_HEAD_INITIALIZER(inject_error_opts
.head
),
99 .type
= QEMU_OPT_STRING
,
103 .type
= QEMU_OPT_NUMBER
,
107 .type
= QEMU_OPT_NUMBER
,
111 .type
= QEMU_OPT_NUMBER
,
115 .type
= QEMU_OPT_BOOL
,
118 .name
= "immediately",
119 .type
= QEMU_OPT_BOOL
,
121 { /* end of list */ }
125 static QemuOptsList set_state_opts
= {
127 .head
= QTAILQ_HEAD_INITIALIZER(set_state_opts
.head
),
131 .type
= QEMU_OPT_STRING
,
135 .type
= QEMU_OPT_NUMBER
,
139 .type
= QEMU_OPT_NUMBER
,
141 { /* end of list */ }
145 static QemuOptsList
*config_groups
[] = {
151 static int get_event_by_name(const char *name
, BlkdebugEvent
*event
)
155 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
156 if (!strcmp(BlkdebugEvent_lookup
[i
], name
)) {
165 struct add_rule_data
{
166 BDRVBlkdebugState
*s
;
170 static int add_rule(void *opaque
, QemuOpts
*opts
, Error
**errp
)
172 struct add_rule_data
*d
= opaque
;
173 BDRVBlkdebugState
*s
= d
->s
;
174 const char* event_name
;
176 struct BlkdebugRule
*rule
;
178 /* Find the right event for the rule */
179 event_name
= qemu_opt_get(opts
, "event");
181 error_setg(errp
, "Missing event name for rule");
183 } else if (get_event_by_name(event_name
, &event
) < 0) {
184 error_setg(errp
, "Invalid event name \"%s\"", event_name
);
188 /* Set attributes common for all actions */
189 rule
= g_malloc0(sizeof(*rule
));
190 *rule
= (struct BlkdebugRule
) {
193 .state
= qemu_opt_get_number(opts
, "state", 0),
196 /* Parse action-specific options */
198 case ACTION_INJECT_ERROR
:
199 rule
->options
.inject
.error
= qemu_opt_get_number(opts
, "errno", EIO
);
200 rule
->options
.inject
.once
= qemu_opt_get_bool(opts
, "once", 0);
201 rule
->options
.inject
.immediately
=
202 qemu_opt_get_bool(opts
, "immediately", 0);
203 rule
->options
.inject
.sector
= qemu_opt_get_number(opts
, "sector", -1);
206 case ACTION_SET_STATE
:
207 rule
->options
.set_state
.new_state
=
208 qemu_opt_get_number(opts
, "new_state", 0);
212 rule
->options
.suspend
.tag
=
213 g_strdup(qemu_opt_get(opts
, "tag"));
218 QLIST_INSERT_HEAD(&s
->rules
[event
], rule
, next
);
223 static void remove_rule(BlkdebugRule
*rule
)
225 switch (rule
->action
) {
226 case ACTION_INJECT_ERROR
:
227 case ACTION_SET_STATE
:
230 g_free(rule
->options
.suspend
.tag
);
234 QLIST_REMOVE(rule
, next
);
238 static int read_config(BDRVBlkdebugState
*s
, const char *filename
,
239 QDict
*options
, Error
**errp
)
243 struct add_rule_data d
;
244 Error
*local_err
= NULL
;
247 f
= fopen(filename
, "r");
249 error_setg_errno(errp
, errno
, "Could not read blkdebug config file");
253 ret
= qemu_config_parse(f
, config_groups
, filename
);
255 error_setg(errp
, "Could not parse blkdebug config file");
261 qemu_config_parse_qdict(options
, config_groups
, &local_err
);
263 error_propagate(errp
, local_err
);
269 d
.action
= ACTION_INJECT_ERROR
;
270 qemu_opts_foreach(&inject_error_opts
, add_rule
, &d
, &local_err
);
272 error_propagate(errp
, local_err
);
277 d
.action
= ACTION_SET_STATE
;
278 qemu_opts_foreach(&set_state_opts
, add_rule
, &d
, &local_err
);
280 error_propagate(errp
, local_err
);
287 qemu_opts_reset(&inject_error_opts
);
288 qemu_opts_reset(&set_state_opts
);
295 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
296 static void blkdebug_parse_filename(const char *filename
, QDict
*options
,
301 /* Parse the blkdebug: prefix */
302 if (!strstart(filename
, "blkdebug:", &filename
)) {
303 /* There was no prefix; therefore, all options have to be already
304 present in the QDict (except for the filename) */
305 qdict_put(options
, "x-image", qstring_from_str(filename
));
309 /* Parse config file path */
310 c
= strchr(filename
, ':');
312 error_setg(errp
, "blkdebug requires both config file and image path");
317 QString
*config_path
;
318 config_path
= qstring_from_substr(filename
, 0, c
- filename
- 1);
319 qdict_put(options
, "config", config_path
);
322 /* TODO Allow multi-level nesting and set file.filename here */
324 qdict_put(options
, "x-image", qstring_from_str(filename
));
327 static QemuOptsList runtime_opts
= {
329 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
333 .type
= QEMU_OPT_STRING
,
334 .help
= "Path to the configuration file",
338 .type
= QEMU_OPT_STRING
,
339 .help
= "[internal use only, will be removed]",
343 .type
= QEMU_OPT_SIZE
,
344 .help
= "Required alignment in bytes",
346 { /* end of list */ }
350 static int blkdebug_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
353 BDRVBlkdebugState
*s
= bs
->opaque
;
355 Error
*local_err
= NULL
;
359 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
360 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
362 error_propagate(errp
, local_err
);
367 /* Read rules from config file or command line options */
368 s
->config_file
= g_strdup(qemu_opt_get(opts
, "config"));
369 ret
= read_config(s
, s
->config_file
, options
, errp
);
374 /* Set initial state */
377 /* Open the image file */
378 bs
->file
= bdrv_open_child(qemu_opt_get(opts
, "x-image"), options
, "image",
379 bs
, &child_file
, false, &local_err
);
382 error_propagate(errp
, local_err
);
386 /* Set request alignment */
387 align
= qemu_opt_get_size(opts
, "align", 0);
388 if (align
< INT_MAX
&& is_power_of_2(align
)) {
391 error_setg(errp
, "Invalid alignment");
400 bdrv_unref_child(bs
, bs
->file
);
403 g_free(s
->config_file
);
409 static void error_callback_bh(void *opaque
)
411 struct BlkdebugAIOCB
*acb
= opaque
;
412 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
416 static BlockAIOCB
*inject_error(BlockDriverState
*bs
,
417 BlockCompletionFunc
*cb
, void *opaque
, BlkdebugRule
*rule
)
419 BDRVBlkdebugState
*s
= bs
->opaque
;
420 int error
= rule
->options
.inject
.error
;
421 struct BlkdebugAIOCB
*acb
;
422 bool immediately
= rule
->options
.inject
.immediately
;
424 if (rule
->options
.inject
.once
) {
425 QSIMPLEQ_REMOVE(&s
->active_rules
, rule
, BlkdebugRule
, active_next
);
433 acb
= qemu_aio_get(&blkdebug_aiocb_info
, bs
, cb
, opaque
);
436 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs
), error_callback_bh
, acb
);
441 static BlockAIOCB
*blkdebug_aio_readv(BlockDriverState
*bs
,
442 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
443 BlockCompletionFunc
*cb
, void *opaque
)
445 BDRVBlkdebugState
*s
= bs
->opaque
;
446 BlkdebugRule
*rule
= NULL
;
448 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
449 if (rule
->options
.inject
.sector
== -1 ||
450 (rule
->options
.inject
.sector
>= sector_num
&&
451 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
456 if (rule
&& rule
->options
.inject
.error
) {
457 return inject_error(bs
, cb
, opaque
, rule
);
460 return bdrv_aio_readv(bs
->file
, sector_num
, qiov
, nb_sectors
,
464 static BlockAIOCB
*blkdebug_aio_writev(BlockDriverState
*bs
,
465 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
466 BlockCompletionFunc
*cb
, void *opaque
)
468 BDRVBlkdebugState
*s
= bs
->opaque
;
469 BlkdebugRule
*rule
= NULL
;
471 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
472 if (rule
->options
.inject
.sector
== -1 ||
473 (rule
->options
.inject
.sector
>= sector_num
&&
474 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
479 if (rule
&& rule
->options
.inject
.error
) {
480 return inject_error(bs
, cb
, opaque
, rule
);
483 return bdrv_aio_writev(bs
->file
, sector_num
, qiov
, nb_sectors
,
487 static BlockAIOCB
*blkdebug_aio_flush(BlockDriverState
*bs
,
488 BlockCompletionFunc
*cb
, void *opaque
)
490 BDRVBlkdebugState
*s
= bs
->opaque
;
491 BlkdebugRule
*rule
= NULL
;
493 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
494 if (rule
->options
.inject
.sector
== -1) {
499 if (rule
&& rule
->options
.inject
.error
) {
500 return inject_error(bs
, cb
, opaque
, rule
);
503 return bdrv_aio_flush(bs
->file
->bs
, cb
, opaque
);
507 static void blkdebug_close(BlockDriverState
*bs
)
509 BDRVBlkdebugState
*s
= bs
->opaque
;
510 BlkdebugRule
*rule
, *next
;
513 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
514 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
519 g_free(s
->config_file
);
522 static void suspend_request(BlockDriverState
*bs
, BlkdebugRule
*rule
)
524 BDRVBlkdebugState
*s
= bs
->opaque
;
525 BlkdebugSuspendedReq r
;
527 r
= (BlkdebugSuspendedReq
) {
528 .co
= qemu_coroutine_self(),
529 .tag
= g_strdup(rule
->options
.suspend
.tag
),
533 QLIST_INSERT_HEAD(&s
->suspended_reqs
, &r
, next
);
535 if (!qtest_enabled()) {
536 printf("blkdebug: Suspended request '%s'\n", r
.tag
);
538 qemu_coroutine_yield();
539 if (!qtest_enabled()) {
540 printf("blkdebug: Resuming request '%s'\n", r
.tag
);
543 QLIST_REMOVE(&r
, next
);
547 static bool process_rule(BlockDriverState
*bs
, struct BlkdebugRule
*rule
,
550 BDRVBlkdebugState
*s
= bs
->opaque
;
552 /* Only process rules for the current state */
553 if (rule
->state
&& rule
->state
!= s
->state
) {
557 /* Take the action */
558 switch (rule
->action
) {
559 case ACTION_INJECT_ERROR
:
561 QSIMPLEQ_INIT(&s
->active_rules
);
564 QSIMPLEQ_INSERT_HEAD(&s
->active_rules
, rule
, active_next
);
567 case ACTION_SET_STATE
:
568 s
->new_state
= rule
->options
.set_state
.new_state
;
572 suspend_request(bs
, rule
);
578 static void blkdebug_debug_event(BlockDriverState
*bs
, BlkdebugEvent event
)
580 BDRVBlkdebugState
*s
= bs
->opaque
;
581 struct BlkdebugRule
*rule
, *next
;
584 assert((int)event
>= 0 && event
< BLKDBG__MAX
);
587 s
->new_state
= s
->state
;
588 QLIST_FOREACH_SAFE(rule
, &s
->rules
[event
], next
, next
) {
589 injected
= process_rule(bs
, rule
, injected
);
591 s
->state
= s
->new_state
;
594 static int blkdebug_debug_breakpoint(BlockDriverState
*bs
, const char *event
,
597 BDRVBlkdebugState
*s
= bs
->opaque
;
598 struct BlkdebugRule
*rule
;
599 BlkdebugEvent blkdebug_event
;
601 if (get_event_by_name(event
, &blkdebug_event
) < 0) {
606 rule
= g_malloc(sizeof(*rule
));
607 *rule
= (struct BlkdebugRule
) {
608 .event
= blkdebug_event
,
609 .action
= ACTION_SUSPEND
,
611 .options
.suspend
.tag
= g_strdup(tag
),
614 QLIST_INSERT_HEAD(&s
->rules
[blkdebug_event
], rule
, next
);
619 static int blkdebug_debug_resume(BlockDriverState
*bs
, const char *tag
)
621 BDRVBlkdebugState
*s
= bs
->opaque
;
622 BlkdebugSuspendedReq
*r
, *next
;
624 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, next
) {
625 if (!strcmp(r
->tag
, tag
)) {
626 qemu_coroutine_enter(r
->co
);
633 static int blkdebug_debug_remove_breakpoint(BlockDriverState
*bs
,
636 BDRVBlkdebugState
*s
= bs
->opaque
;
637 BlkdebugSuspendedReq
*r
, *r_next
;
638 BlkdebugRule
*rule
, *next
;
639 int i
, ret
= -ENOENT
;
641 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
642 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
643 if (rule
->action
== ACTION_SUSPEND
&&
644 !strcmp(rule
->options
.suspend
.tag
, tag
)) {
650 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, r_next
) {
651 if (!strcmp(r
->tag
, tag
)) {
652 qemu_coroutine_enter(r
->co
);
659 static bool blkdebug_debug_is_suspended(BlockDriverState
*bs
, const char *tag
)
661 BDRVBlkdebugState
*s
= bs
->opaque
;
662 BlkdebugSuspendedReq
*r
;
664 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
665 if (!strcmp(r
->tag
, tag
)) {
672 static int64_t blkdebug_getlength(BlockDriverState
*bs
)
674 return bdrv_getlength(bs
->file
->bs
);
677 static int blkdebug_truncate(BlockDriverState
*bs
, int64_t offset
)
679 return bdrv_truncate(bs
->file
->bs
, offset
);
682 static void blkdebug_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
684 BDRVBlkdebugState
*s
= bs
->opaque
;
687 bool force_json
= false;
689 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
690 if (strcmp(qdict_entry_key(e
), "config") &&
691 strcmp(qdict_entry_key(e
), "x-image"))
698 if (force_json
&& !bs
->file
->bs
->full_open_options
) {
699 /* The config file cannot be recreated, so creating a plain filename
704 if (!force_json
&& bs
->file
->bs
->exact_filename
[0]) {
705 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
706 "blkdebug:%s:%s", s
->config_file
?: "",
707 bs
->file
->bs
->exact_filename
);
711 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("blkdebug")));
713 QINCREF(bs
->file
->bs
->full_open_options
);
714 qdict_put_obj(opts
, "image", QOBJECT(bs
->file
->bs
->full_open_options
));
716 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
717 if (strcmp(qdict_entry_key(e
), "x-image")) {
718 qobject_incref(qdict_entry_value(e
));
719 qdict_put_obj(opts
, qdict_entry_key(e
), qdict_entry_value(e
));
723 bs
->full_open_options
= opts
;
726 static void blkdebug_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
728 BDRVBlkdebugState
*s
= bs
->opaque
;
731 bs
->bl
.request_alignment
= s
->align
;
735 static int blkdebug_reopen_prepare(BDRVReopenState
*reopen_state
,
736 BlockReopenQueue
*queue
, Error
**errp
)
741 static BlockDriver bdrv_blkdebug
= {
742 .format_name
= "blkdebug",
743 .protocol_name
= "blkdebug",
744 .instance_size
= sizeof(BDRVBlkdebugState
),
746 .bdrv_parse_filename
= blkdebug_parse_filename
,
747 .bdrv_file_open
= blkdebug_open
,
748 .bdrv_close
= blkdebug_close
,
749 .bdrv_reopen_prepare
= blkdebug_reopen_prepare
,
750 .bdrv_getlength
= blkdebug_getlength
,
751 .bdrv_truncate
= blkdebug_truncate
,
752 .bdrv_refresh_filename
= blkdebug_refresh_filename
,
753 .bdrv_refresh_limits
= blkdebug_refresh_limits
,
755 .bdrv_aio_readv
= blkdebug_aio_readv
,
756 .bdrv_aio_writev
= blkdebug_aio_writev
,
757 .bdrv_aio_flush
= blkdebug_aio_flush
,
759 .bdrv_debug_event
= blkdebug_debug_event
,
760 .bdrv_debug_breakpoint
= blkdebug_debug_breakpoint
,
761 .bdrv_debug_remove_breakpoint
762 = blkdebug_debug_remove_breakpoint
,
763 .bdrv_debug_resume
= blkdebug_debug_resume
,
764 .bdrv_debug_is_suspended
= blkdebug_debug_is_suspended
,
767 static void bdrv_blkdebug_init(void)
769 bdrv_register(&bdrv_blkdebug
);
772 block_init(bdrv_blkdebug_init
);