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",
172 [BLKDBG_REFBLOCK_LOAD
] = "refblock_load",
173 [BLKDBG_REFBLOCK_UPDATE
] = "refblock_update",
174 [BLKDBG_REFBLOCK_UPDATE_PART
] = "refblock_update_part",
175 [BLKDBG_REFBLOCK_ALLOC
] = "refblock_alloc",
176 [BLKDBG_REFBLOCK_ALLOC_HOOKUP
] = "refblock_alloc.hookup",
177 [BLKDBG_REFBLOCK_ALLOC_WRITE
] = "refblock_alloc.write",
178 [BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS
] = "refblock_alloc.write_blocks",
179 [BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE
] = "refblock_alloc.write_table",
180 [BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE
] = "refblock_alloc.switch_table",
182 [BLKDBG_CLUSTER_ALLOC
] = "cluster_alloc",
183 [BLKDBG_CLUSTER_ALLOC_BYTES
] = "cluster_alloc_bytes",
184 [BLKDBG_CLUSTER_FREE
] = "cluster_free",
187 static int get_event_by_name(const char *name
, BlkDebugEvent
*event
)
191 for (i
= 0; i
< BLKDBG_EVENT_MAX
; i
++) {
192 if (!strcmp(event_names
[i
], name
)) {
201 struct add_rule_data
{
202 BDRVBlkdebugState
*s
;
206 static int add_rule(QemuOpts
*opts
, void *opaque
)
208 struct add_rule_data
*d
= opaque
;
209 BDRVBlkdebugState
*s
= d
->s
;
210 const char* event_name
;
212 struct BlkdebugRule
*rule
;
214 /* Find the right event for the rule */
215 event_name
= qemu_opt_get(opts
, "event");
216 if (!event_name
|| get_event_by_name(event_name
, &event
) < 0) {
220 /* Set attributes common for all actions */
221 rule
= g_malloc0(sizeof(*rule
));
222 *rule
= (struct BlkdebugRule
) {
225 .state
= qemu_opt_get_number(opts
, "state", 0),
228 /* Parse action-specific options */
230 case ACTION_INJECT_ERROR
:
231 rule
->options
.inject
.error
= qemu_opt_get_number(opts
, "errno", EIO
);
232 rule
->options
.inject
.once
= qemu_opt_get_bool(opts
, "once", 0);
233 rule
->options
.inject
.immediately
=
234 qemu_opt_get_bool(opts
, "immediately", 0);
235 rule
->options
.inject
.sector
= qemu_opt_get_number(opts
, "sector", -1);
238 case ACTION_SET_STATE
:
239 rule
->options
.set_state
.new_state
=
240 qemu_opt_get_number(opts
, "new_state", 0);
244 rule
->options
.suspend
.tag
=
245 g_strdup(qemu_opt_get(opts
, "tag"));
250 QLIST_INSERT_HEAD(&s
->rules
[event
], rule
, next
);
255 static void remove_rule(BlkdebugRule
*rule
)
257 switch (rule
->action
) {
258 case ACTION_INJECT_ERROR
:
259 case ACTION_SET_STATE
:
262 g_free(rule
->options
.suspend
.tag
);
266 QLIST_REMOVE(rule
, next
);
270 static int read_config(BDRVBlkdebugState
*s
, const char *filename
)
274 struct add_rule_data d
;
276 /* Allow usage without config file */
281 f
= fopen(filename
, "r");
286 ret
= qemu_config_parse(f
, config_groups
, filename
);
292 d
.action
= ACTION_INJECT_ERROR
;
293 qemu_opts_foreach(&inject_error_opts
, add_rule
, &d
, 0);
295 d
.action
= ACTION_SET_STATE
;
296 qemu_opts_foreach(&set_state_opts
, add_rule
, &d
, 0);
300 qemu_opts_reset(&inject_error_opts
);
301 qemu_opts_reset(&set_state_opts
);
306 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
307 static int blkdebug_open(BlockDriverState
*bs
, const char *filename
,
308 QDict
*options
, int flags
)
310 BDRVBlkdebugState
*s
= bs
->opaque
;
314 /* Parse the blkdebug: prefix */
315 if (strncmp(filename
, "blkdebug:", strlen("blkdebug:"))) {
318 filename
+= strlen("blkdebug:");
320 /* Read rules from config file */
321 c
= strchr(filename
, ':');
326 config
= g_strdup(filename
);
327 config
[c
- filename
] = '\0';
328 ret
= read_config(s
, config
);
335 /* Set initial state */
338 /* Open the backing file */
339 ret
= bdrv_file_open(&bs
->file
, filename
, NULL
, flags
);
347 static void error_callback_bh(void *opaque
)
349 struct BlkdebugAIOCB
*acb
= opaque
;
350 qemu_bh_delete(acb
->bh
);
351 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
352 qemu_aio_release(acb
);
355 static void blkdebug_aio_cancel(BlockDriverAIOCB
*blockacb
)
357 BlkdebugAIOCB
*acb
= container_of(blockacb
, BlkdebugAIOCB
, common
);
358 qemu_aio_release(acb
);
361 static BlockDriverAIOCB
*inject_error(BlockDriverState
*bs
,
362 BlockDriverCompletionFunc
*cb
, void *opaque
, BlkdebugRule
*rule
)
364 BDRVBlkdebugState
*s
= bs
->opaque
;
365 int error
= rule
->options
.inject
.error
;
366 struct BlkdebugAIOCB
*acb
;
369 if (rule
->options
.inject
.once
) {
370 QSIMPLEQ_INIT(&s
->active_rules
);
373 if (rule
->options
.inject
.immediately
) {
377 acb
= qemu_aio_get(&blkdebug_aiocb_info
, bs
, cb
, opaque
);
380 bh
= qemu_bh_new(error_callback_bh
, acb
);
382 qemu_bh_schedule(bh
);
387 static BlockDriverAIOCB
*blkdebug_aio_readv(BlockDriverState
*bs
,
388 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
389 BlockDriverCompletionFunc
*cb
, void *opaque
)
391 BDRVBlkdebugState
*s
= bs
->opaque
;
392 BlkdebugRule
*rule
= NULL
;
394 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
395 if (rule
->options
.inject
.sector
== -1 ||
396 (rule
->options
.inject
.sector
>= sector_num
&&
397 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
402 if (rule
&& rule
->options
.inject
.error
) {
403 return inject_error(bs
, cb
, opaque
, rule
);
406 return bdrv_aio_readv(bs
->file
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
409 static BlockDriverAIOCB
*blkdebug_aio_writev(BlockDriverState
*bs
,
410 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
411 BlockDriverCompletionFunc
*cb
, void *opaque
)
413 BDRVBlkdebugState
*s
= bs
->opaque
;
414 BlkdebugRule
*rule
= NULL
;
416 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
417 if (rule
->options
.inject
.sector
== -1 ||
418 (rule
->options
.inject
.sector
>= sector_num
&&
419 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
424 if (rule
&& rule
->options
.inject
.error
) {
425 return inject_error(bs
, cb
, opaque
, rule
);
428 return bdrv_aio_writev(bs
->file
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
432 static void blkdebug_close(BlockDriverState
*bs
)
434 BDRVBlkdebugState
*s
= bs
->opaque
;
435 BlkdebugRule
*rule
, *next
;
438 for (i
= 0; i
< BLKDBG_EVENT_MAX
; i
++) {
439 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
445 static void suspend_request(BlockDriverState
*bs
, BlkdebugRule
*rule
)
447 BDRVBlkdebugState
*s
= bs
->opaque
;
448 BlkdebugSuspendedReq r
;
450 r
= (BlkdebugSuspendedReq
) {
451 .co
= qemu_coroutine_self(),
452 .tag
= g_strdup(rule
->options
.suspend
.tag
),
456 QLIST_INSERT_HEAD(&s
->suspended_reqs
, &r
, next
);
458 printf("blkdebug: Suspended request '%s'\n", r
.tag
);
459 qemu_coroutine_yield();
460 printf("blkdebug: Resuming request '%s'\n", r
.tag
);
462 QLIST_REMOVE(&r
, next
);
466 static bool process_rule(BlockDriverState
*bs
, struct BlkdebugRule
*rule
,
469 BDRVBlkdebugState
*s
= bs
->opaque
;
471 /* Only process rules for the current state */
472 if (rule
->state
&& rule
->state
!= s
->state
) {
476 /* Take the action */
477 switch (rule
->action
) {
478 case ACTION_INJECT_ERROR
:
480 QSIMPLEQ_INIT(&s
->active_rules
);
483 QSIMPLEQ_INSERT_HEAD(&s
->active_rules
, rule
, active_next
);
486 case ACTION_SET_STATE
:
487 s
->new_state
= rule
->options
.set_state
.new_state
;
491 suspend_request(bs
, rule
);
497 static void blkdebug_debug_event(BlockDriverState
*bs
, BlkDebugEvent event
)
499 BDRVBlkdebugState
*s
= bs
->opaque
;
500 struct BlkdebugRule
*rule
, *next
;
503 assert((int)event
>= 0 && event
< BLKDBG_EVENT_MAX
);
506 s
->new_state
= s
->state
;
507 QLIST_FOREACH_SAFE(rule
, &s
->rules
[event
], next
, next
) {
508 injected
= process_rule(bs
, rule
, injected
);
510 s
->state
= s
->new_state
;
513 static int blkdebug_debug_breakpoint(BlockDriverState
*bs
, const char *event
,
516 BDRVBlkdebugState
*s
= bs
->opaque
;
517 struct BlkdebugRule
*rule
;
518 BlkDebugEvent blkdebug_event
;
520 if (get_event_by_name(event
, &blkdebug_event
) < 0) {
525 rule
= g_malloc(sizeof(*rule
));
526 *rule
= (struct BlkdebugRule
) {
527 .event
= blkdebug_event
,
528 .action
= ACTION_SUSPEND
,
530 .options
.suspend
.tag
= g_strdup(tag
),
533 QLIST_INSERT_HEAD(&s
->rules
[blkdebug_event
], rule
, next
);
538 static int blkdebug_debug_resume(BlockDriverState
*bs
, const char *tag
)
540 BDRVBlkdebugState
*s
= bs
->opaque
;
541 BlkdebugSuspendedReq
*r
;
543 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
544 if (!strcmp(r
->tag
, tag
)) {
545 qemu_coroutine_enter(r
->co
, NULL
);
553 static bool blkdebug_debug_is_suspended(BlockDriverState
*bs
, const char *tag
)
555 BDRVBlkdebugState
*s
= bs
->opaque
;
556 BlkdebugSuspendedReq
*r
;
558 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
559 if (!strcmp(r
->tag
, tag
)) {
566 static int64_t blkdebug_getlength(BlockDriverState
*bs
)
568 return bdrv_getlength(bs
->file
);
571 static BlockDriver bdrv_blkdebug
= {
572 .format_name
= "blkdebug",
573 .protocol_name
= "blkdebug",
575 .instance_size
= sizeof(BDRVBlkdebugState
),
577 .bdrv_file_open
= blkdebug_open
,
578 .bdrv_close
= blkdebug_close
,
579 .bdrv_getlength
= blkdebug_getlength
,
581 .bdrv_aio_readv
= blkdebug_aio_readv
,
582 .bdrv_aio_writev
= blkdebug_aio_writev
,
584 .bdrv_debug_event
= blkdebug_debug_event
,
585 .bdrv_debug_breakpoint
= blkdebug_debug_breakpoint
,
586 .bdrv_debug_resume
= blkdebug_debug_resume
,
587 .bdrv_debug_is_suspended
= blkdebug_debug_is_suspended
,
590 static void bdrv_blkdebug_init(void)
592 bdrv_register(&bdrv_blkdebug
);
595 block_init(bdrv_blkdebug_init
);