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 "block_int.h"
29 typedef struct BDRVBlkdebugState
{
32 QLIST_HEAD(, BlkdebugRule
) rules
[BLKDBG_EVENT_MAX
];
33 QSIMPLEQ_HEAD(, BlkdebugRule
) active_rules
;
36 typedef struct BlkdebugAIOCB
{
37 BlockDriverAIOCB common
;
42 static void blkdebug_aio_cancel(BlockDriverAIOCB
*blockacb
);
44 static AIOPool blkdebug_aio_pool
= {
45 .aiocb_size
= sizeof(BlkdebugAIOCB
),
46 .cancel
= blkdebug_aio_cancel
,
54 typedef struct BlkdebugRule
{
69 QLIST_ENTRY(BlkdebugRule
) next
;
70 QSIMPLEQ_ENTRY(BlkdebugRule
) active_next
;
73 static QemuOptsList inject_error_opts
= {
74 .name
= "inject-error",
75 .head
= QTAILQ_HEAD_INITIALIZER(inject_error_opts
.head
),
79 .type
= QEMU_OPT_STRING
,
83 .type
= QEMU_OPT_NUMBER
,
87 .type
= QEMU_OPT_NUMBER
,
91 .type
= QEMU_OPT_NUMBER
,
95 .type
= QEMU_OPT_BOOL
,
98 .name
= "immediately",
99 .type
= QEMU_OPT_BOOL
,
101 { /* end of list */ }
105 static QemuOptsList set_state_opts
= {
107 .head
= QTAILQ_HEAD_INITIALIZER(set_state_opts
.head
),
111 .type
= QEMU_OPT_STRING
,
115 .type
= QEMU_OPT_NUMBER
,
119 .type
= QEMU_OPT_NUMBER
,
121 { /* end of list */ }
125 static QemuOptsList
*config_groups
[] = {
131 static const char *event_names
[BLKDBG_EVENT_MAX
] = {
132 [BLKDBG_L1_UPDATE
] = "l1_update",
133 [BLKDBG_L1_GROW_ALLOC_TABLE
] = "l1_grow.alloc_table",
134 [BLKDBG_L1_GROW_WRITE_TABLE
] = "l1_grow.write_table",
135 [BLKDBG_L1_GROW_ACTIVATE_TABLE
] = "l1_grow.activate_table",
137 [BLKDBG_L2_LOAD
] = "l2_load",
138 [BLKDBG_L2_UPDATE
] = "l2_update",
139 [BLKDBG_L2_UPDATE_COMPRESSED
] = "l2_update_compressed",
140 [BLKDBG_L2_ALLOC_COW_READ
] = "l2_alloc.cow_read",
141 [BLKDBG_L2_ALLOC_WRITE
] = "l2_alloc.write",
143 [BLKDBG_READ_AIO
] = "read_aio",
144 [BLKDBG_READ_BACKING_AIO
] = "read_backing_aio",
145 [BLKDBG_READ_COMPRESSED
] = "read_compressed",
147 [BLKDBG_WRITE_AIO
] = "write_aio",
148 [BLKDBG_WRITE_COMPRESSED
] = "write_compressed",
150 [BLKDBG_VMSTATE_LOAD
] = "vmstate_load",
151 [BLKDBG_VMSTATE_SAVE
] = "vmstate_save",
153 [BLKDBG_COW_READ
] = "cow_read",
154 [BLKDBG_COW_WRITE
] = "cow_write",
156 [BLKDBG_REFTABLE_LOAD
] = "reftable_load",
157 [BLKDBG_REFTABLE_GROW
] = "reftable_grow",
159 [BLKDBG_REFBLOCK_LOAD
] = "refblock_load",
160 [BLKDBG_REFBLOCK_UPDATE
] = "refblock_update",
161 [BLKDBG_REFBLOCK_UPDATE_PART
] = "refblock_update_part",
162 [BLKDBG_REFBLOCK_ALLOC
] = "refblock_alloc",
163 [BLKDBG_REFBLOCK_ALLOC_HOOKUP
] = "refblock_alloc.hookup",
164 [BLKDBG_REFBLOCK_ALLOC_WRITE
] = "refblock_alloc.write",
165 [BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS
] = "refblock_alloc.write_blocks",
166 [BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE
] = "refblock_alloc.write_table",
167 [BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE
] = "refblock_alloc.switch_table",
169 [BLKDBG_CLUSTER_ALLOC
] = "cluster_alloc",
170 [BLKDBG_CLUSTER_ALLOC_BYTES
] = "cluster_alloc_bytes",
171 [BLKDBG_CLUSTER_FREE
] = "cluster_free",
174 static int get_event_by_name(const char *name
, BlkDebugEvent
*event
)
178 for (i
= 0; i
< BLKDBG_EVENT_MAX
; i
++) {
179 if (!strcmp(event_names
[i
], name
)) {
188 struct add_rule_data
{
189 BDRVBlkdebugState
*s
;
193 static int add_rule(QemuOpts
*opts
, void *opaque
)
195 struct add_rule_data
*d
= opaque
;
196 BDRVBlkdebugState
*s
= d
->s
;
197 const char* event_name
;
199 struct BlkdebugRule
*rule
;
201 /* Find the right event for the rule */
202 event_name
= qemu_opt_get(opts
, "event");
203 if (!event_name
|| get_event_by_name(event_name
, &event
) < 0) {
207 /* Set attributes common for all actions */
208 rule
= g_malloc0(sizeof(*rule
));
209 *rule
= (struct BlkdebugRule
) {
212 .state
= qemu_opt_get_number(opts
, "state", 0),
215 /* Parse action-specific options */
217 case ACTION_INJECT_ERROR
:
218 rule
->options
.inject
.error
= qemu_opt_get_number(opts
, "errno", EIO
);
219 rule
->options
.inject
.once
= qemu_opt_get_bool(opts
, "once", 0);
220 rule
->options
.inject
.immediately
=
221 qemu_opt_get_bool(opts
, "immediately", 0);
222 rule
->options
.inject
.sector
= qemu_opt_get_number(opts
, "sector", -1);
225 case ACTION_SET_STATE
:
226 rule
->options
.set_state
.new_state
=
227 qemu_opt_get_number(opts
, "new_state", 0);
232 QLIST_INSERT_HEAD(&s
->rules
[event
], rule
, next
);
237 static int read_config(BDRVBlkdebugState
*s
, const char *filename
)
241 struct add_rule_data d
;
243 f
= fopen(filename
, "r");
248 ret
= qemu_config_parse(f
, config_groups
, filename
);
254 d
.action
= ACTION_INJECT_ERROR
;
255 qemu_opts_foreach(&inject_error_opts
, add_rule
, &d
, 0);
257 d
.action
= ACTION_SET_STATE
;
258 qemu_opts_foreach(&set_state_opts
, add_rule
, &d
, 0);
262 qemu_opts_reset(&inject_error_opts
);
263 qemu_opts_reset(&set_state_opts
);
268 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
269 static int blkdebug_open(BlockDriverState
*bs
, const char *filename
, int flags
)
271 BDRVBlkdebugState
*s
= bs
->opaque
;
275 /* Parse the blkdebug: prefix */
276 if (strncmp(filename
, "blkdebug:", strlen("blkdebug:"))) {
279 filename
+= strlen("blkdebug:");
281 /* Read rules from config file */
282 c
= strchr(filename
, ':');
287 config
= g_strdup(filename
);
288 config
[c
- filename
] = '\0';
289 ret
= read_config(s
, config
);
296 /* Set initial state */
299 /* Open the backing file */
300 ret
= bdrv_file_open(&bs
->file
, filename
, flags
);
308 static void error_callback_bh(void *opaque
)
310 struct BlkdebugAIOCB
*acb
= opaque
;
311 qemu_bh_delete(acb
->bh
);
312 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
313 qemu_aio_release(acb
);
316 static void blkdebug_aio_cancel(BlockDriverAIOCB
*blockacb
)
318 BlkdebugAIOCB
*acb
= container_of(blockacb
, BlkdebugAIOCB
, common
);
319 qemu_aio_release(acb
);
322 static BlockDriverAIOCB
*inject_error(BlockDriverState
*bs
,
323 BlockDriverCompletionFunc
*cb
, void *opaque
, BlkdebugRule
*rule
)
325 BDRVBlkdebugState
*s
= bs
->opaque
;
326 int error
= rule
->options
.inject
.error
;
327 struct BlkdebugAIOCB
*acb
;
330 if (rule
->options
.inject
.once
) {
331 QSIMPLEQ_INIT(&s
->active_rules
);
334 if (rule
->options
.inject
.immediately
) {
338 acb
= qemu_aio_get(&blkdebug_aio_pool
, bs
, cb
, opaque
);
341 bh
= qemu_bh_new(error_callback_bh
, acb
);
343 qemu_bh_schedule(bh
);
348 static BlockDriverAIOCB
*blkdebug_aio_readv(BlockDriverState
*bs
,
349 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
350 BlockDriverCompletionFunc
*cb
, void *opaque
)
352 BDRVBlkdebugState
*s
= bs
->opaque
;
353 BlkdebugRule
*rule
= NULL
;
355 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
356 if (rule
->options
.inject
.sector
== -1 ||
357 (rule
->options
.inject
.sector
>= sector_num
&&
358 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
363 if (rule
&& rule
->options
.inject
.error
) {
364 return inject_error(bs
, cb
, opaque
, rule
);
367 return bdrv_aio_readv(bs
->file
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
370 static BlockDriverAIOCB
*blkdebug_aio_writev(BlockDriverState
*bs
,
371 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
372 BlockDriverCompletionFunc
*cb
, void *opaque
)
374 BDRVBlkdebugState
*s
= bs
->opaque
;
375 BlkdebugRule
*rule
= NULL
;
377 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
378 if (rule
->options
.inject
.sector
== -1 ||
379 (rule
->options
.inject
.sector
>= sector_num
&&
380 rule
->options
.inject
.sector
< sector_num
+ nb_sectors
)) {
385 if (rule
&& rule
->options
.inject
.error
) {
386 return inject_error(bs
, cb
, opaque
, rule
);
389 return bdrv_aio_writev(bs
->file
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
392 static void blkdebug_close(BlockDriverState
*bs
)
394 BDRVBlkdebugState
*s
= bs
->opaque
;
395 BlkdebugRule
*rule
, *next
;
398 for (i
= 0; i
< BLKDBG_EVENT_MAX
; i
++) {
399 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
400 QLIST_REMOVE(rule
, next
);
406 static bool process_rule(BlockDriverState
*bs
, struct BlkdebugRule
*rule
,
409 BDRVBlkdebugState
*s
= bs
->opaque
;
411 /* Only process rules for the current state */
412 if (rule
->state
&& rule
->state
!= s
->state
) {
416 /* Take the action */
417 switch (rule
->action
) {
418 case ACTION_INJECT_ERROR
:
420 QSIMPLEQ_INIT(&s
->active_rules
);
423 QSIMPLEQ_INSERT_HEAD(&s
->active_rules
, rule
, active_next
);
426 case ACTION_SET_STATE
:
427 s
->new_state
= rule
->options
.set_state
.new_state
;
433 static void blkdebug_debug_event(BlockDriverState
*bs
, BlkDebugEvent event
)
435 BDRVBlkdebugState
*s
= bs
->opaque
;
436 struct BlkdebugRule
*rule
;
439 assert((int)event
>= 0 && event
< BLKDBG_EVENT_MAX
);
442 s
->new_state
= s
->state
;
443 QLIST_FOREACH(rule
, &s
->rules
[event
], next
) {
444 injected
= process_rule(bs
, rule
, injected
);
446 s
->state
= s
->new_state
;
449 static int64_t blkdebug_getlength(BlockDriverState
*bs
)
451 return bdrv_getlength(bs
->file
);
454 static BlockDriver bdrv_blkdebug
= {
455 .format_name
= "blkdebug",
456 .protocol_name
= "blkdebug",
458 .instance_size
= sizeof(BDRVBlkdebugState
),
460 .bdrv_file_open
= blkdebug_open
,
461 .bdrv_close
= blkdebug_close
,
462 .bdrv_getlength
= blkdebug_getlength
,
464 .bdrv_aio_readv
= blkdebug_aio_readv
,
465 .bdrv_aio_writev
= blkdebug_aio_writev
,
467 .bdrv_debug_event
= blkdebug_debug_event
,
470 static void bdrv_blkdebug_init(void)
472 bdrv_register(&bdrv_blkdebug
);
475 block_init(bdrv_blkdebug_init
);