2 * Block protocol for I/O error injection
4 * Copyright (C) 2016-2017 Red Hat, Inc.
5 * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "qemu/cutils.h"
29 #include "qemu/config-file.h"
30 #include "block/block_int.h"
31 #include "qemu/module.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qint.h"
35 #include "qapi/qmp/qstring.h"
36 #include "sysemu/qtest.h"
38 typedef struct BDRVBlkdebugState
{
43 /* For blkdebug_refresh_filename() */
46 QLIST_HEAD(, BlkdebugRule
) rules
[BLKDBG__MAX
];
47 QSIMPLEQ_HEAD(, BlkdebugRule
) active_rules
;
48 QLIST_HEAD(, BlkdebugSuspendedReq
) suspended_reqs
;
51 typedef struct BlkdebugAIOCB
{
56 typedef struct BlkdebugSuspendedReq
{
59 QLIST_ENTRY(BlkdebugSuspendedReq
) next
;
60 } BlkdebugSuspendedReq
;
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
;
176 /* Find the right event for the rule */
177 event_name
= qemu_opt_get(opts
, "event");
179 error_setg(errp
, "Missing event name for rule");
181 } else if (get_event_by_name(event_name
, &event
) < 0) {
182 error_setg(errp
, "Invalid event name \"%s\"", event_name
);
186 /* Set attributes common for all actions */
187 rule
= g_malloc0(sizeof(*rule
));
188 *rule
= (struct BlkdebugRule
) {
191 .state
= qemu_opt_get_number(opts
, "state", 0),
194 /* Parse action-specific options */
196 case ACTION_INJECT_ERROR
:
197 rule
->options
.inject
.error
= qemu_opt_get_number(opts
, "errno", EIO
);
198 rule
->options
.inject
.once
= qemu_opt_get_bool(opts
, "once", 0);
199 rule
->options
.inject
.immediately
=
200 qemu_opt_get_bool(opts
, "immediately", 0);
201 sector
= qemu_opt_get_number(opts
, "sector", -1);
202 rule
->options
.inject
.offset
=
203 sector
== -1 ? -1 : sector
* BDRV_SECTOR_SIZE
;
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_str(options
, "x-image", 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_str(options
, "x-image", 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
;
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 bs
->supported_write_flags
= BDRV_REQ_FUA
&
386 bs
->file
->bs
->supported_write_flags
;
387 bs
->supported_zero_flags
= (BDRV_REQ_FUA
| BDRV_REQ_MAY_UNMAP
) &
388 bs
->file
->bs
->supported_zero_flags
;
391 /* Set request alignment */
392 s
->align
= qemu_opt_get_size(opts
, "align", 0);
393 if (s
->align
&& (s
->align
>= INT_MAX
|| !is_power_of_2(s
->align
))) {
394 error_setg(errp
, "Cannot meet constraints with align %" PRIu64
,
402 g_free(s
->config_file
);
408 static int rule_check(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
)
410 BDRVBlkdebugState
*s
= bs
->opaque
;
411 BlkdebugRule
*rule
= NULL
;
415 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
416 uint64_t inject_offset
= rule
->options
.inject
.offset
;
418 if (inject_offset
== -1 ||
419 (bytes
&& inject_offset
>= offset
&&
420 inject_offset
< offset
+ bytes
))
426 if (!rule
|| !rule
->options
.inject
.error
) {
430 immediately
= rule
->options
.inject
.immediately
;
431 error
= rule
->options
.inject
.error
;
433 if (rule
->options
.inject
.once
) {
434 QSIMPLEQ_REMOVE(&s
->active_rules
, rule
, BlkdebugRule
, active_next
);
439 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
440 qemu_coroutine_yield();
446 static int coroutine_fn
447 blkdebug_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
448 QEMUIOVector
*qiov
, int flags
)
452 /* Sanity check block layer guarantees */
453 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
454 assert(QEMU_IS_ALIGNED(bytes
, bs
->bl
.request_alignment
));
455 if (bs
->bl
.max_transfer
) {
456 assert(bytes
<= bs
->bl
.max_transfer
);
459 err
= rule_check(bs
, offset
, bytes
);
464 return bdrv_co_preadv(bs
->file
, offset
, bytes
, qiov
, flags
);
467 static int coroutine_fn
468 blkdebug_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
469 QEMUIOVector
*qiov
, int flags
)
473 /* Sanity check block layer guarantees */
474 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
475 assert(QEMU_IS_ALIGNED(bytes
, bs
->bl
.request_alignment
));
476 if (bs
->bl
.max_transfer
) {
477 assert(bytes
<= bs
->bl
.max_transfer
);
480 err
= rule_check(bs
, offset
, bytes
);
485 return bdrv_co_pwritev(bs
->file
, offset
, bytes
, qiov
, flags
);
488 static int blkdebug_co_flush(BlockDriverState
*bs
)
490 int err
= rule_check(bs
, 0, 0);
496 return bdrv_co_flush(bs
->file
->bs
);
499 static int coroutine_fn
blkdebug_co_pwrite_zeroes(BlockDriverState
*bs
,
500 int64_t offset
, int count
,
501 BdrvRequestFlags flags
)
503 uint32_t align
= MAX(bs
->bl
.request_alignment
,
504 bs
->bl
.pwrite_zeroes_alignment
);
507 /* Only pass through requests that are larger than requested
508 * preferred alignment (so that we test the fallback to writes on
509 * unaligned portions), and check that the block layer never hands
510 * us anything unaligned that crosses an alignment boundary. */
512 assert(QEMU_IS_ALIGNED(offset
, align
) ||
513 QEMU_IS_ALIGNED(offset
+ count
, align
) ||
514 DIV_ROUND_UP(offset
, align
) ==
515 DIV_ROUND_UP(offset
+ count
, align
));
518 assert(QEMU_IS_ALIGNED(offset
, align
));
519 assert(QEMU_IS_ALIGNED(count
, align
));
520 if (bs
->bl
.max_pwrite_zeroes
) {
521 assert(count
<= bs
->bl
.max_pwrite_zeroes
);
524 err
= rule_check(bs
, offset
, count
);
529 return bdrv_co_pwrite_zeroes(bs
->file
, offset
, count
, flags
);
532 static int coroutine_fn
blkdebug_co_pdiscard(BlockDriverState
*bs
,
533 int64_t offset
, int count
)
535 uint32_t align
= bs
->bl
.pdiscard_alignment
;
538 /* Only pass through requests that are larger than requested
539 * minimum alignment, and ensure that unaligned requests do not
540 * cross optimum discard boundaries. */
541 if (count
< bs
->bl
.request_alignment
) {
542 assert(QEMU_IS_ALIGNED(offset
, align
) ||
543 QEMU_IS_ALIGNED(offset
+ count
, align
) ||
544 DIV_ROUND_UP(offset
, align
) ==
545 DIV_ROUND_UP(offset
+ count
, align
));
548 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
549 assert(QEMU_IS_ALIGNED(count
, bs
->bl
.request_alignment
));
550 if (align
&& count
>= align
) {
551 assert(QEMU_IS_ALIGNED(offset
, align
));
552 assert(QEMU_IS_ALIGNED(count
, align
));
554 if (bs
->bl
.max_pdiscard
) {
555 assert(count
<= bs
->bl
.max_pdiscard
);
558 err
= rule_check(bs
, offset
, count
);
563 return bdrv_co_pdiscard(bs
->file
->bs
, offset
, count
);
566 static void blkdebug_close(BlockDriverState
*bs
)
568 BDRVBlkdebugState
*s
= bs
->opaque
;
569 BlkdebugRule
*rule
, *next
;
572 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
573 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
578 g_free(s
->config_file
);
581 static void suspend_request(BlockDriverState
*bs
, BlkdebugRule
*rule
)
583 BDRVBlkdebugState
*s
= bs
->opaque
;
584 BlkdebugSuspendedReq r
;
586 r
= (BlkdebugSuspendedReq
) {
587 .co
= qemu_coroutine_self(),
588 .tag
= g_strdup(rule
->options
.suspend
.tag
),
592 QLIST_INSERT_HEAD(&s
->suspended_reqs
, &r
, next
);
594 if (!qtest_enabled()) {
595 printf("blkdebug: Suspended request '%s'\n", r
.tag
);
597 qemu_coroutine_yield();
598 if (!qtest_enabled()) {
599 printf("blkdebug: Resuming request '%s'\n", r
.tag
);
602 QLIST_REMOVE(&r
, next
);
606 static bool process_rule(BlockDriverState
*bs
, struct BlkdebugRule
*rule
,
609 BDRVBlkdebugState
*s
= bs
->opaque
;
611 /* Only process rules for the current state */
612 if (rule
->state
&& rule
->state
!= s
->state
) {
616 /* Take the action */
617 switch (rule
->action
) {
618 case ACTION_INJECT_ERROR
:
620 QSIMPLEQ_INIT(&s
->active_rules
);
623 QSIMPLEQ_INSERT_HEAD(&s
->active_rules
, rule
, active_next
);
626 case ACTION_SET_STATE
:
627 s
->new_state
= rule
->options
.set_state
.new_state
;
631 suspend_request(bs
, rule
);
637 static void blkdebug_debug_event(BlockDriverState
*bs
, BlkdebugEvent event
)
639 BDRVBlkdebugState
*s
= bs
->opaque
;
640 struct BlkdebugRule
*rule
, *next
;
643 assert((int)event
>= 0 && event
< BLKDBG__MAX
);
646 s
->new_state
= s
->state
;
647 QLIST_FOREACH_SAFE(rule
, &s
->rules
[event
], next
, next
) {
648 injected
= process_rule(bs
, rule
, injected
);
650 s
->state
= s
->new_state
;
653 static int blkdebug_debug_breakpoint(BlockDriverState
*bs
, const char *event
,
656 BDRVBlkdebugState
*s
= bs
->opaque
;
657 struct BlkdebugRule
*rule
;
658 BlkdebugEvent blkdebug_event
;
660 if (get_event_by_name(event
, &blkdebug_event
) < 0) {
665 rule
= g_malloc(sizeof(*rule
));
666 *rule
= (struct BlkdebugRule
) {
667 .event
= blkdebug_event
,
668 .action
= ACTION_SUSPEND
,
670 .options
.suspend
.tag
= g_strdup(tag
),
673 QLIST_INSERT_HEAD(&s
->rules
[blkdebug_event
], rule
, next
);
678 static int blkdebug_debug_resume(BlockDriverState
*bs
, const char *tag
)
680 BDRVBlkdebugState
*s
= bs
->opaque
;
681 BlkdebugSuspendedReq
*r
, *next
;
683 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, next
) {
684 if (!strcmp(r
->tag
, tag
)) {
685 qemu_coroutine_enter(r
->co
);
692 static int blkdebug_debug_remove_breakpoint(BlockDriverState
*bs
,
695 BDRVBlkdebugState
*s
= bs
->opaque
;
696 BlkdebugSuspendedReq
*r
, *r_next
;
697 BlkdebugRule
*rule
, *next
;
698 int i
, ret
= -ENOENT
;
700 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
701 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
702 if (rule
->action
== ACTION_SUSPEND
&&
703 !strcmp(rule
->options
.suspend
.tag
, tag
)) {
709 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, r_next
) {
710 if (!strcmp(r
->tag
, tag
)) {
711 qemu_coroutine_enter(r
->co
);
718 static bool blkdebug_debug_is_suspended(BlockDriverState
*bs
, const char *tag
)
720 BDRVBlkdebugState
*s
= bs
->opaque
;
721 BlkdebugSuspendedReq
*r
;
723 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
724 if (!strcmp(r
->tag
, tag
)) {
731 static int64_t blkdebug_getlength(BlockDriverState
*bs
)
733 return bdrv_getlength(bs
->file
->bs
);
736 static int blkdebug_truncate(BlockDriverState
*bs
, int64_t offset
, Error
**errp
)
738 return bdrv_truncate(bs
->file
, offset
, errp
);
741 static void blkdebug_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
743 BDRVBlkdebugState
*s
= bs
->opaque
;
746 bool force_json
= false;
748 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
749 if (strcmp(qdict_entry_key(e
), "config") &&
750 strcmp(qdict_entry_key(e
), "x-image"))
757 if (force_json
&& !bs
->file
->bs
->full_open_options
) {
758 /* The config file cannot be recreated, so creating a plain filename
763 if (!force_json
&& bs
->file
->bs
->exact_filename
[0]) {
764 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
765 "blkdebug:%s:%s", s
->config_file
?: "",
766 bs
->file
->bs
->exact_filename
);
770 qdict_put_str(opts
, "driver", "blkdebug");
772 QINCREF(bs
->file
->bs
->full_open_options
);
773 qdict_put(opts
, "image", bs
->file
->bs
->full_open_options
);
775 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
776 if (strcmp(qdict_entry_key(e
), "x-image")) {
777 qobject_incref(qdict_entry_value(e
));
778 qdict_put_obj(opts
, qdict_entry_key(e
), qdict_entry_value(e
));
782 bs
->full_open_options
= opts
;
785 static void blkdebug_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
787 BDRVBlkdebugState
*s
= bs
->opaque
;
790 bs
->bl
.request_alignment
= s
->align
;
794 static int blkdebug_reopen_prepare(BDRVReopenState
*reopen_state
,
795 BlockReopenQueue
*queue
, Error
**errp
)
800 static BlockDriver bdrv_blkdebug
= {
801 .format_name
= "blkdebug",
802 .protocol_name
= "blkdebug",
803 .instance_size
= sizeof(BDRVBlkdebugState
),
805 .bdrv_parse_filename
= blkdebug_parse_filename
,
806 .bdrv_file_open
= blkdebug_open
,
807 .bdrv_close
= blkdebug_close
,
808 .bdrv_reopen_prepare
= blkdebug_reopen_prepare
,
809 .bdrv_child_perm
= bdrv_filter_default_perms
,
811 .bdrv_getlength
= blkdebug_getlength
,
812 .bdrv_truncate
= blkdebug_truncate
,
813 .bdrv_refresh_filename
= blkdebug_refresh_filename
,
814 .bdrv_refresh_limits
= blkdebug_refresh_limits
,
816 .bdrv_co_preadv
= blkdebug_co_preadv
,
817 .bdrv_co_pwritev
= blkdebug_co_pwritev
,
818 .bdrv_co_flush_to_disk
= blkdebug_co_flush
,
819 .bdrv_co_pwrite_zeroes
= blkdebug_co_pwrite_zeroes
,
820 .bdrv_co_pdiscard
= blkdebug_co_pdiscard
,
822 .bdrv_debug_event
= blkdebug_debug_event
,
823 .bdrv_debug_breakpoint
= blkdebug_debug_breakpoint
,
824 .bdrv_debug_remove_breakpoint
825 = blkdebug_debug_remove_breakpoint
,
826 .bdrv_debug_resume
= blkdebug_debug_resume
,
827 .bdrv_debug_is_suspended
= blkdebug_debug_is_suspended
,
830 static void bdrv_blkdebug_init(void)
832 bdrv_register(&bdrv_blkdebug
);
835 block_init(bdrv_blkdebug_init
);