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
;
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
;
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 sector
= qemu_opt_get_number(opts
, "sector", -1);
201 rule
->options
.inject
.offset
=
202 sector
== -1 ? -1 : sector
* BDRV_SECTOR_SIZE
;
205 case ACTION_SET_STATE
:
206 rule
->options
.set_state
.new_state
=
207 qemu_opt_get_number(opts
, "new_state", 0);
211 rule
->options
.suspend
.tag
=
212 g_strdup(qemu_opt_get(opts
, "tag"));
217 QLIST_INSERT_HEAD(&s
->rules
[event
], rule
, next
);
222 static void remove_rule(BlkdebugRule
*rule
)
224 switch (rule
->action
) {
225 case ACTION_INJECT_ERROR
:
226 case ACTION_SET_STATE
:
229 g_free(rule
->options
.suspend
.tag
);
233 QLIST_REMOVE(rule
, next
);
237 static int read_config(BDRVBlkdebugState
*s
, const char *filename
,
238 QDict
*options
, Error
**errp
)
242 struct add_rule_data d
;
243 Error
*local_err
= NULL
;
246 f
= fopen(filename
, "r");
248 error_setg_errno(errp
, errno
, "Could not read blkdebug config file");
252 ret
= qemu_config_parse(f
, config_groups
, filename
);
254 error_setg(errp
, "Could not parse blkdebug config file");
260 qemu_config_parse_qdict(options
, config_groups
, &local_err
);
262 error_propagate(errp
, local_err
);
268 d
.action
= ACTION_INJECT_ERROR
;
269 qemu_opts_foreach(&inject_error_opts
, add_rule
, &d
, &local_err
);
271 error_propagate(errp
, local_err
);
276 d
.action
= ACTION_SET_STATE
;
277 qemu_opts_foreach(&set_state_opts
, add_rule
, &d
, &local_err
);
279 error_propagate(errp
, local_err
);
286 qemu_opts_reset(&inject_error_opts
);
287 qemu_opts_reset(&set_state_opts
);
294 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
295 static void blkdebug_parse_filename(const char *filename
, QDict
*options
,
300 /* Parse the blkdebug: prefix */
301 if (!strstart(filename
, "blkdebug:", &filename
)) {
302 /* There was no prefix; therefore, all options have to be already
303 present in the QDict (except for the filename) */
304 qdict_put(options
, "x-image", qstring_from_str(filename
));
308 /* Parse config file path */
309 c
= strchr(filename
, ':');
311 error_setg(errp
, "blkdebug requires both config file and image path");
316 QString
*config_path
;
317 config_path
= qstring_from_substr(filename
, 0, c
- filename
- 1);
318 qdict_put(options
, "config", config_path
);
321 /* TODO Allow multi-level nesting and set file.filename here */
323 qdict_put(options
, "x-image", qstring_from_str(filename
));
326 static QemuOptsList runtime_opts
= {
328 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
332 .type
= QEMU_OPT_STRING
,
333 .help
= "Path to the configuration file",
337 .type
= QEMU_OPT_STRING
,
338 .help
= "[internal use only, will be removed]",
342 .type
= QEMU_OPT_SIZE
,
343 .help
= "Required alignment in bytes",
345 { /* end of list */ }
349 static int blkdebug_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
352 BDRVBlkdebugState
*s
= bs
->opaque
;
354 Error
*local_err
= NULL
;
358 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
359 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
361 error_propagate(errp
, local_err
);
366 /* Read rules from config file or command line options */
367 s
->config_file
= g_strdup(qemu_opt_get(opts
, "config"));
368 ret
= read_config(s
, s
->config_file
, options
, errp
);
373 /* Set initial state */
376 /* Open the image file */
377 bs
->file
= bdrv_open_child(qemu_opt_get(opts
, "x-image"), options
, "image",
378 bs
, &child_file
, false, &local_err
);
381 error_propagate(errp
, local_err
);
385 /* Set request alignment */
386 align
= qemu_opt_get_size(opts
, "align", 0);
387 if (align
< INT_MAX
&& is_power_of_2(align
)) {
390 error_setg(errp
, "Invalid alignment");
399 bdrv_unref_child(bs
, bs
->file
);
402 g_free(s
->config_file
);
408 static int inject_error(BlockDriverState
*bs
, BlkdebugRule
*rule
)
410 BDRVBlkdebugState
*s
= bs
->opaque
;
411 int error
= rule
->options
.inject
.error
;
412 bool immediately
= rule
->options
.inject
.immediately
;
414 if (rule
->options
.inject
.once
) {
415 QSIMPLEQ_REMOVE(&s
->active_rules
, rule
, BlkdebugRule
, active_next
);
420 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
421 qemu_coroutine_yield();
427 static int coroutine_fn
428 blkdebug_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
429 QEMUIOVector
*qiov
, int flags
)
431 BDRVBlkdebugState
*s
= bs
->opaque
;
432 BlkdebugRule
*rule
= NULL
;
434 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
435 uint64_t inject_offset
= rule
->options
.inject
.offset
;
437 if (inject_offset
== -1 ||
438 (inject_offset
>= offset
&& inject_offset
< offset
+ bytes
))
444 if (rule
&& rule
->options
.inject
.error
) {
445 return inject_error(bs
, rule
);
448 return bdrv_co_preadv(bs
->file
, offset
, bytes
, qiov
, flags
);
451 static int coroutine_fn
452 blkdebug_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
453 QEMUIOVector
*qiov
, int flags
)
455 BDRVBlkdebugState
*s
= bs
->opaque
;
456 BlkdebugRule
*rule
= NULL
;
458 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
459 uint64_t inject_offset
= rule
->options
.inject
.offset
;
461 if (inject_offset
== -1 ||
462 (inject_offset
>= offset
&& inject_offset
< offset
+ bytes
))
468 if (rule
&& rule
->options
.inject
.error
) {
469 return inject_error(bs
, rule
);
472 return bdrv_co_pwritev(bs
->file
, offset
, bytes
, qiov
, flags
);
475 static int blkdebug_co_flush(BlockDriverState
*bs
)
477 BDRVBlkdebugState
*s
= bs
->opaque
;
478 BlkdebugRule
*rule
= NULL
;
480 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
481 if (rule
->options
.inject
.offset
== -1) {
486 if (rule
&& rule
->options
.inject
.error
) {
487 return inject_error(bs
, rule
);
490 return bdrv_co_flush(bs
->file
->bs
);
494 static void blkdebug_close(BlockDriverState
*bs
)
496 BDRVBlkdebugState
*s
= bs
->opaque
;
497 BlkdebugRule
*rule
, *next
;
500 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
501 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
506 g_free(s
->config_file
);
509 static void suspend_request(BlockDriverState
*bs
, BlkdebugRule
*rule
)
511 BDRVBlkdebugState
*s
= bs
->opaque
;
512 BlkdebugSuspendedReq r
;
514 r
= (BlkdebugSuspendedReq
) {
515 .co
= qemu_coroutine_self(),
516 .tag
= g_strdup(rule
->options
.suspend
.tag
),
520 QLIST_INSERT_HEAD(&s
->suspended_reqs
, &r
, next
);
522 if (!qtest_enabled()) {
523 printf("blkdebug: Suspended request '%s'\n", r
.tag
);
525 qemu_coroutine_yield();
526 if (!qtest_enabled()) {
527 printf("blkdebug: Resuming request '%s'\n", r
.tag
);
530 QLIST_REMOVE(&r
, next
);
534 static bool process_rule(BlockDriverState
*bs
, struct BlkdebugRule
*rule
,
537 BDRVBlkdebugState
*s
= bs
->opaque
;
539 /* Only process rules for the current state */
540 if (rule
->state
&& rule
->state
!= s
->state
) {
544 /* Take the action */
545 switch (rule
->action
) {
546 case ACTION_INJECT_ERROR
:
548 QSIMPLEQ_INIT(&s
->active_rules
);
551 QSIMPLEQ_INSERT_HEAD(&s
->active_rules
, rule
, active_next
);
554 case ACTION_SET_STATE
:
555 s
->new_state
= rule
->options
.set_state
.new_state
;
559 suspend_request(bs
, rule
);
565 static void blkdebug_debug_event(BlockDriverState
*bs
, BlkdebugEvent event
)
567 BDRVBlkdebugState
*s
= bs
->opaque
;
568 struct BlkdebugRule
*rule
, *next
;
571 assert((int)event
>= 0 && event
< BLKDBG__MAX
);
574 s
->new_state
= s
->state
;
575 QLIST_FOREACH_SAFE(rule
, &s
->rules
[event
], next
, next
) {
576 injected
= process_rule(bs
, rule
, injected
);
578 s
->state
= s
->new_state
;
581 static int blkdebug_debug_breakpoint(BlockDriverState
*bs
, const char *event
,
584 BDRVBlkdebugState
*s
= bs
->opaque
;
585 struct BlkdebugRule
*rule
;
586 BlkdebugEvent blkdebug_event
;
588 if (get_event_by_name(event
, &blkdebug_event
) < 0) {
593 rule
= g_malloc(sizeof(*rule
));
594 *rule
= (struct BlkdebugRule
) {
595 .event
= blkdebug_event
,
596 .action
= ACTION_SUSPEND
,
598 .options
.suspend
.tag
= g_strdup(tag
),
601 QLIST_INSERT_HEAD(&s
->rules
[blkdebug_event
], rule
, next
);
606 static int blkdebug_debug_resume(BlockDriverState
*bs
, const char *tag
)
608 BDRVBlkdebugState
*s
= bs
->opaque
;
609 BlkdebugSuspendedReq
*r
, *next
;
611 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, next
) {
612 if (!strcmp(r
->tag
, tag
)) {
613 qemu_coroutine_enter(r
->co
);
620 static int blkdebug_debug_remove_breakpoint(BlockDriverState
*bs
,
623 BDRVBlkdebugState
*s
= bs
->opaque
;
624 BlkdebugSuspendedReq
*r
, *r_next
;
625 BlkdebugRule
*rule
, *next
;
626 int i
, ret
= -ENOENT
;
628 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
629 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
630 if (rule
->action
== ACTION_SUSPEND
&&
631 !strcmp(rule
->options
.suspend
.tag
, tag
)) {
637 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, r_next
) {
638 if (!strcmp(r
->tag
, tag
)) {
639 qemu_coroutine_enter(r
->co
);
646 static bool blkdebug_debug_is_suspended(BlockDriverState
*bs
, const char *tag
)
648 BDRVBlkdebugState
*s
= bs
->opaque
;
649 BlkdebugSuspendedReq
*r
;
651 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
652 if (!strcmp(r
->tag
, tag
)) {
659 static int64_t blkdebug_getlength(BlockDriverState
*bs
)
661 return bdrv_getlength(bs
->file
->bs
);
664 static int blkdebug_truncate(BlockDriverState
*bs
, int64_t offset
)
666 return bdrv_truncate(bs
->file
, offset
);
669 static void blkdebug_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
671 BDRVBlkdebugState
*s
= bs
->opaque
;
674 bool force_json
= false;
676 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
677 if (strcmp(qdict_entry_key(e
), "config") &&
678 strcmp(qdict_entry_key(e
), "x-image"))
685 if (force_json
&& !bs
->file
->bs
->full_open_options
) {
686 /* The config file cannot be recreated, so creating a plain filename
691 if (!force_json
&& bs
->file
->bs
->exact_filename
[0]) {
692 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
693 "blkdebug:%s:%s", s
->config_file
?: "",
694 bs
->file
->bs
->exact_filename
);
698 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("blkdebug")));
700 QINCREF(bs
->file
->bs
->full_open_options
);
701 qdict_put_obj(opts
, "image", QOBJECT(bs
->file
->bs
->full_open_options
));
703 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
704 if (strcmp(qdict_entry_key(e
), "x-image")) {
705 qobject_incref(qdict_entry_value(e
));
706 qdict_put_obj(opts
, qdict_entry_key(e
), qdict_entry_value(e
));
710 bs
->full_open_options
= opts
;
713 static void blkdebug_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
715 BDRVBlkdebugState
*s
= bs
->opaque
;
718 bs
->bl
.request_alignment
= s
->align
;
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_child_perm
= bdrv_filter_default_perms
,
739 .bdrv_getlength
= blkdebug_getlength
,
740 .bdrv_truncate
= blkdebug_truncate
,
741 .bdrv_refresh_filename
= blkdebug_refresh_filename
,
742 .bdrv_refresh_limits
= blkdebug_refresh_limits
,
744 .bdrv_co_preadv
= blkdebug_co_preadv
,
745 .bdrv_co_pwritev
= blkdebug_co_pwritev
,
746 .bdrv_co_flush_to_disk
= blkdebug_co_flush
,
748 .bdrv_debug_event
= blkdebug_debug_event
,
749 .bdrv_debug_breakpoint
= blkdebug_debug_breakpoint
,
750 .bdrv_debug_remove_breakpoint
751 = blkdebug_debug_remove_breakpoint
,
752 .bdrv_debug_resume
= blkdebug_debug_resume
,
753 .bdrv_debug_is_suspended
= blkdebug_debug_is_suspended
,
756 static void bdrv_blkdebug_init(void)
758 bdrv_register(&bdrv_blkdebug
);
761 block_init(bdrv_blkdebug_init
);