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-common.h"
26 #include "qemu/config-file.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
30 typedef struct BDRVBlkdebugState
{
34 QLIST_HEAD(, BlkdebugRule
) rules
[BLKDBG_EVENT_MAX
];
35 QSIMPLEQ_HEAD(, BlkdebugRule
) active_rules
;
36 QLIST_HEAD(, BlkdebugSuspendedReq
) suspended_reqs
;
39 typedef struct BlkdebugAIOCB
{
40 BlockDriverAIOCB common
;
45 typedef struct BlkdebugSuspendedReq
{
48 QLIST_ENTRY(BlkdebugSuspendedReq
) next
;
49 } BlkdebugSuspendedReq
;
51 static void blkdebug_aio_cancel(BlockDriverAIOCB
*blockacb
);
53 static const AIOCBInfo blkdebug_aiocb_info
= {
54 .aiocb_size
= sizeof(BlkdebugAIOCB
),
55 .cancel
= blkdebug_aio_cancel
,
64 typedef struct BlkdebugRule
{
82 QLIST_ENTRY(BlkdebugRule
) next
;
83 QSIMPLEQ_ENTRY(BlkdebugRule
) active_next
;
86 static QemuOptsList inject_error_opts
= {
87 .name
= "inject-error",
88 .head
= QTAILQ_HEAD_INITIALIZER(inject_error_opts
.head
),
92 .type
= QEMU_OPT_STRING
,
96 .type
= QEMU_OPT_NUMBER
,
100 .type
= QEMU_OPT_NUMBER
,
104 .type
= QEMU_OPT_NUMBER
,
108 .type
= QEMU_OPT_BOOL
,
111 .name
= "immediately",
112 .type
= QEMU_OPT_BOOL
,
114 { /* end of list */ }
118 static QemuOptsList set_state_opts
= {
120 .head
= QTAILQ_HEAD_INITIALIZER(set_state_opts
.head
),
124 .type
= QEMU_OPT_STRING
,
128 .type
= QEMU_OPT_NUMBER
,
132 .type
= QEMU_OPT_NUMBER
,
134 { /* end of list */ }
138 static QemuOptsList
*config_groups
[] = {
144 static const char *event_names
[BLKDBG_EVENT_MAX
] = {
145 [BLKDBG_L1_UPDATE
] = "l1_update",
146 [BLKDBG_L1_GROW_ALLOC_TABLE
] = "l1_grow.alloc_table",
147 [BLKDBG_L1_GROW_WRITE_TABLE
] = "l1_grow.write_table",
148 [BLKDBG_L1_GROW_ACTIVATE_TABLE
] = "l1_grow.activate_table",
150 [BLKDBG_L2_LOAD
] = "l2_load",
151 [BLKDBG_L2_UPDATE
] = "l2_update",
152 [BLKDBG_L2_UPDATE_COMPRESSED
] = "l2_update_compressed",
153 [BLKDBG_L2_ALLOC_COW_READ
] = "l2_alloc.cow_read",
154 [BLKDBG_L2_ALLOC_WRITE
] = "l2_alloc.write",
156 [BLKDBG_READ_AIO
] = "read_aio",
157 [BLKDBG_READ_BACKING_AIO
] = "read_backing_aio",
158 [BLKDBG_READ_COMPRESSED
] = "read_compressed",
160 [BLKDBG_WRITE_AIO
] = "write_aio",
161 [BLKDBG_WRITE_COMPRESSED
] = "write_compressed",
163 [BLKDBG_VMSTATE_LOAD
] = "vmstate_load",
164 [BLKDBG_VMSTATE_SAVE
] = "vmstate_save",
166 [BLKDBG_COW_READ
] = "cow_read",
167 [BLKDBG_COW_WRITE
] = "cow_write",
169 [BLKDBG_REFTABLE_LOAD
] = "reftable_load",
170 [BLKDBG_REFTABLE_GROW
] = "reftable_grow",
171 [BLKDBG_REFTABLE_UPDATE
] = "reftable_update",
173 [BLKDBG_REFBLOCK_LOAD
] = "refblock_load",
174 [BLKDBG_REFBLOCK_UPDATE
] = "refblock_update",
175 [BLKDBG_REFBLOCK_UPDATE_PART
] = "refblock_update_part",
176 [BLKDBG_REFBLOCK_ALLOC
] = "refblock_alloc",
177 [BLKDBG_REFBLOCK_ALLOC_HOOKUP
] = "refblock_alloc.hookup",
178 [BLKDBG_REFBLOCK_ALLOC_WRITE
] = "refblock_alloc.write",
179 [BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS
] = "refblock_alloc.write_blocks",
180 [BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE
] = "refblock_alloc.write_table",
181 [BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE
] = "refblock_alloc.switch_table",
183 [BLKDBG_CLUSTER_ALLOC
] = "cluster_alloc",
184 [BLKDBG_CLUSTER_ALLOC_BYTES
] = "cluster_alloc_bytes",
185 [BLKDBG_CLUSTER_FREE
] = "cluster_free",
187 [BLKDBG_FLUSH_TO_OS
] = "flush_to_os",
188 [BLKDBG_FLUSH_TO_DISK
] = "flush_to_disk",
191 static int get_event_by_name(const char *name
, BlkDebugEvent
*event
)
195 for (i
= 0; i
< BLKDBG_EVENT_MAX
; i
++) {
196 if (!strcmp(event_names
[i
], name
)) {
205 struct add_rule_data
{
206 BDRVBlkdebugState
*s
;
210 static int add_rule(QemuOpts
*opts
, void *opaque
)
212 struct add_rule_data
*d
= opaque
;
213 BDRVBlkdebugState
*s
= d
->s
;
214 const char* event_name
;
216 struct BlkdebugRule
*rule
;
218 /* Find the right event for the rule */
219 event_name
= qemu_opt_get(opts
, "event");
220 if (!event_name
|| get_event_by_name(event_name
, &event
) < 0) {
224 /* Set attributes common for all actions */
225 rule
= g_malloc0(sizeof(*rule
));
226 *rule
= (struct BlkdebugRule
) {
229 .state
= qemu_opt_get_number(opts
, "state", 0),
232 /* Parse action-specific options */
234 case ACTION_INJECT_ERROR
:
235 rule
->options
.inject
.error
= qemu_opt_get_number(opts
, "errno", EIO
);
236 rule
->options
.inject
.once
= qemu_opt_get_bool(opts
, "once", 0);
237 rule
->options
.inject
.immediately
=
238 qemu_opt_get_bool(opts
, "immediately", 0);
239 rule
->options
.inject
.sector
= qemu_opt_get_number(opts
, "sector", -1);
242 case ACTION_SET_STATE
:
243 rule
->options
.set_state
.new_state
=
244 qemu_opt_get_number(opts
, "new_state", 0);
248 rule
->options
.suspend
.tag
=
249 g_strdup(qemu_opt_get(opts
, "tag"));
254 QLIST_INSERT_HEAD(&s
->rules
[event
], rule
, next
);
259 static void remove_rule(BlkdebugRule
*rule
)
261 switch (rule
->action
) {
262 case ACTION_INJECT_ERROR
:
263 case ACTION_SET_STATE
:
266 g_free(rule
->options
.suspend
.tag
);
270 QLIST_REMOVE(rule
, next
);
274 static int read_config(BDRVBlkdebugState
*s
, const char *filename
,
275 QDict
*options
, Error
**errp
)
279 struct add_rule_data d
;
280 Error
*local_err
= NULL
;
283 f
= fopen(filename
, "r");
285 error_setg_errno(errp
, errno
, "Could not read blkdebug config file");
289 ret
= qemu_config_parse(f
, config_groups
, filename
);
291 error_setg(errp
, "Could not parse blkdebug config file");
297 qemu_config_parse_qdict(options
, config_groups
, &local_err
);
298 if (error_is_set(&local_err
)) {
299 error_propagate(errp
, local_err
);
305 d
.action
= ACTION_INJECT_ERROR
;
306 qemu_opts_foreach(&inject_error_opts
, add_rule
, &d
, 0);
308 d
.action
= ACTION_SET_STATE
;
309 qemu_opts_foreach(&set_state_opts
, add_rule
, &d
, 0);
313 qemu_opts_reset(&inject_error_opts
);
314 qemu_opts_reset(&set_state_opts
);
321 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
322 static void blkdebug_parse_filename(const char *filename
, QDict
*options
,
327 /* Parse the blkdebug: prefix */
328 if (!strstart(filename
, "blkdebug:", &filename
)) {
329 /* There was no prefix; therefore, all options have to be already
330 present in the QDict (except for the filename) */
331 qdict_put(options
, "x-image", qstring_from_str(filename
));
335 /* Parse config file path */
336 c
= strchr(filename
, ':');
338 error_setg(errp
, "blkdebug requires both config file and image path");
343 QString
*config_path
;
344 config_path
= qstring_from_substr(filename
, 0, c
- filename
- 1);
345 qdict_put(options
, "config", config_path
);
348 /* TODO Allow multi-level nesting and set file.filename here */
350 qdict_put(options
, "x-image", qstring_from_str(filename
));
353 static QemuOptsList runtime_opts
= {
355 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
359 .type
= QEMU_OPT_STRING
,
360 .help
= "Path to the configuration file",
364 .type
= QEMU_OPT_STRING
,
365 .help
= "[internal use only, will be removed]",
369 .type
= QEMU_OPT_SIZE
,
370 .help
= "Required alignment in bytes",
372 { /* end of list */ }
376 static int blkdebug_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
379 BDRVBlkdebugState
*s
= bs
->opaque
;
381 Error
*local_err
= NULL
;
386 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
387 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
388 if (error_is_set(&local_err
)) {
389 error_propagate(errp
, local_err
);
394 /* Read rules from config file or command line options */
395 config
= qemu_opt_get(opts
, "config");
396 ret
= read_config(s
, config
, options
, errp
);
401 /* Set initial state */
404 /* Open the backing file */
405 ret
= bdrv_open_image(&bs
->file
, qemu_opt_get(opts
, "x-image"), options
, "image",
406 flags
, true, false, &local_err
);
408 error_propagate(errp
, local_err
);
412 /* Set request alignment */
413 align
= qemu_opt_get_size(opts
, "align", bs
->request_alignment
);
414 if (align
> 0 && align
< INT_MAX
&& !(align
& (align
- 1))) {
415 bs
->request_alignment
= align
;
417 error_setg(errp
, "Invalid alignment");
428 static void error_callback_bh(void *opaque
)
430 struct BlkdebugAIOCB
*acb
= opaque
;
431 qemu_bh_delete(acb
->bh
);
432 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
433 qemu_aio_release(acb
);
436 static void blkdebug_aio_cancel(BlockDriverAIOCB
*blockacb
)
438 BlkdebugAIOCB
*acb
= container_of(blockacb
, BlkdebugAIOCB
, common
);
439 qemu_aio_release(acb
);
442 static BlockDriverAIOCB
*inject_error(BlockDriverState
*bs
,
443 BlockDriverCompletionFunc
*cb
, void *opaque
, BlkdebugRule
*rule
)
445 BDRVBlkdebugState
*s
= bs
->opaque
;
446 int error
= rule
->options
.inject
.error
;
447 struct BlkdebugAIOCB
*acb
;
450 if (rule
->options
.inject
.once
) {
451 QSIMPLEQ_INIT(&s
->active_rules
);
454 if (rule
->options
.inject
.immediately
) {
458 acb
= qemu_aio_get(&blkdebug_aiocb_info
, bs
, cb
, opaque
);
461 bh
= qemu_bh_new(error_callback_bh
, acb
);
463 qemu_bh_schedule(bh
);
468 static BlockDriverAIOCB
*blkdebug_aio_readv(BlockDriverState
*bs
,
469 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
470 BlockDriverCompletionFunc
*cb
, void *opaque
)
472 BDRVBlkdebugState
*s
= bs
->opaque
;
473 BlkdebugRule
*rule
= NULL
;
475 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
476 if (rule
->options
.inject
.sector
== -1 ||
477 (rule
->options
.inject
.sector
>= sector_num
&&
478 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
483 if (rule
&& rule
->options
.inject
.error
) {
484 return inject_error(bs
, cb
, opaque
, rule
);
487 return bdrv_aio_readv(bs
->file
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
490 static BlockDriverAIOCB
*blkdebug_aio_writev(BlockDriverState
*bs
,
491 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
492 BlockDriverCompletionFunc
*cb
, void *opaque
)
494 BDRVBlkdebugState
*s
= bs
->opaque
;
495 BlkdebugRule
*rule
= NULL
;
497 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
498 if (rule
->options
.inject
.sector
== -1 ||
499 (rule
->options
.inject
.sector
>= sector_num
&&
500 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
505 if (rule
&& rule
->options
.inject
.error
) {
506 return inject_error(bs
, cb
, opaque
, rule
);
509 return bdrv_aio_writev(bs
->file
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
513 static void blkdebug_close(BlockDriverState
*bs
)
515 BDRVBlkdebugState
*s
= bs
->opaque
;
516 BlkdebugRule
*rule
, *next
;
519 for (i
= 0; i
< BLKDBG_EVENT_MAX
; i
++) {
520 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
526 static void suspend_request(BlockDriverState
*bs
, BlkdebugRule
*rule
)
528 BDRVBlkdebugState
*s
= bs
->opaque
;
529 BlkdebugSuspendedReq r
;
531 r
= (BlkdebugSuspendedReq
) {
532 .co
= qemu_coroutine_self(),
533 .tag
= g_strdup(rule
->options
.suspend
.tag
),
537 QLIST_INSERT_HEAD(&s
->suspended_reqs
, &r
, next
);
539 printf("blkdebug: Suspended request '%s'\n", r
.tag
);
540 qemu_coroutine_yield();
541 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_EVENT_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
, NULL
);
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_EVENT_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
, NULL
);
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
);
677 static BlockDriver bdrv_blkdebug
= {
678 .format_name
= "blkdebug",
679 .protocol_name
= "blkdebug",
680 .instance_size
= sizeof(BDRVBlkdebugState
),
682 .bdrv_parse_filename
= blkdebug_parse_filename
,
683 .bdrv_file_open
= blkdebug_open
,
684 .bdrv_close
= blkdebug_close
,
685 .bdrv_getlength
= blkdebug_getlength
,
687 .bdrv_aio_readv
= blkdebug_aio_readv
,
688 .bdrv_aio_writev
= blkdebug_aio_writev
,
690 .bdrv_debug_event
= blkdebug_debug_event
,
691 .bdrv_debug_breakpoint
= blkdebug_debug_breakpoint
,
692 .bdrv_debug_remove_breakpoint
693 = blkdebug_debug_remove_breakpoint
,
694 .bdrv_debug_resume
= blkdebug_debug_resume
,
695 .bdrv_debug_is_suspended
= blkdebug_debug_is_suspended
,
698 static void bdrv_blkdebug_init(void)
700 bdrv_register(&bdrv_blkdebug
);
703 block_init(bdrv_blkdebug_init
);