xref: /openbmc/qemu/block/blkdebug.c (revision 1dde0f48d53ad39401ec5064a61162d6784aad44)
1 /*
2  * Block protocol for I/O error injection
3  *
4  * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu-common.h"
26 #include "qemu/config-file.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29 #include "qapi/qmp/qbool.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qapi/qmp/qint.h"
32 #include "qapi/qmp/qstring.h"
33 
34 typedef struct BDRVBlkdebugState {
35     int state;
36     int new_state;
37 
38     QLIST_HEAD(, BlkdebugRule) rules[BLKDBG_EVENT_MAX];
39     QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
40     QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
41 } BDRVBlkdebugState;
42 
43 typedef struct BlkdebugAIOCB {
44     BlockDriverAIOCB common;
45     QEMUBH *bh;
46     int ret;
47 } BlkdebugAIOCB;
48 
49 typedef struct BlkdebugSuspendedReq {
50     Coroutine *co;
51     char *tag;
52     QLIST_ENTRY(BlkdebugSuspendedReq) next;
53 } BlkdebugSuspendedReq;
54 
55 static const AIOCBInfo blkdebug_aiocb_info = {
56     .aiocb_size    = sizeof(BlkdebugAIOCB),
57 };
58 
59 enum {
60     ACTION_INJECT_ERROR,
61     ACTION_SET_STATE,
62     ACTION_SUSPEND,
63 };
64 
65 typedef struct BlkdebugRule {
66     BlkDebugEvent event;
67     int action;
68     int state;
69     union {
70         struct {
71             int error;
72             int immediately;
73             int once;
74             int64_t sector;
75         } inject;
76         struct {
77             int new_state;
78         } set_state;
79         struct {
80             char *tag;
81         } suspend;
82     } options;
83     QLIST_ENTRY(BlkdebugRule) next;
84     QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
85 } BlkdebugRule;
86 
87 static QemuOptsList inject_error_opts = {
88     .name = "inject-error",
89     .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
90     .desc = {
91         {
92             .name = "event",
93             .type = QEMU_OPT_STRING,
94         },
95         {
96             .name = "state",
97             .type = QEMU_OPT_NUMBER,
98         },
99         {
100             .name = "errno",
101             .type = QEMU_OPT_NUMBER,
102         },
103         {
104             .name = "sector",
105             .type = QEMU_OPT_NUMBER,
106         },
107         {
108             .name = "once",
109             .type = QEMU_OPT_BOOL,
110         },
111         {
112             .name = "immediately",
113             .type = QEMU_OPT_BOOL,
114         },
115         { /* end of list */ }
116     },
117 };
118 
119 static QemuOptsList set_state_opts = {
120     .name = "set-state",
121     .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
122     .desc = {
123         {
124             .name = "event",
125             .type = QEMU_OPT_STRING,
126         },
127         {
128             .name = "state",
129             .type = QEMU_OPT_NUMBER,
130         },
131         {
132             .name = "new_state",
133             .type = QEMU_OPT_NUMBER,
134         },
135         { /* end of list */ }
136     },
137 };
138 
139 static QemuOptsList *config_groups[] = {
140     &inject_error_opts,
141     &set_state_opts,
142     NULL
143 };
144 
145 static const char *event_names[BLKDBG_EVENT_MAX] = {
146     [BLKDBG_L1_UPDATE]                      = "l1_update",
147     [BLKDBG_L1_GROW_ALLOC_TABLE]            = "l1_grow.alloc_table",
148     [BLKDBG_L1_GROW_WRITE_TABLE]            = "l1_grow.write_table",
149     [BLKDBG_L1_GROW_ACTIVATE_TABLE]         = "l1_grow.activate_table",
150 
151     [BLKDBG_L2_LOAD]                        = "l2_load",
152     [BLKDBG_L2_UPDATE]                      = "l2_update",
153     [BLKDBG_L2_UPDATE_COMPRESSED]           = "l2_update_compressed",
154     [BLKDBG_L2_ALLOC_COW_READ]              = "l2_alloc.cow_read",
155     [BLKDBG_L2_ALLOC_WRITE]                 = "l2_alloc.write",
156 
157     [BLKDBG_READ_AIO]                       = "read_aio",
158     [BLKDBG_READ_BACKING_AIO]               = "read_backing_aio",
159     [BLKDBG_READ_COMPRESSED]                = "read_compressed",
160 
161     [BLKDBG_WRITE_AIO]                      = "write_aio",
162     [BLKDBG_WRITE_COMPRESSED]               = "write_compressed",
163 
164     [BLKDBG_VMSTATE_LOAD]                   = "vmstate_load",
165     [BLKDBG_VMSTATE_SAVE]                   = "vmstate_save",
166 
167     [BLKDBG_COW_READ]                       = "cow_read",
168     [BLKDBG_COW_WRITE]                      = "cow_write",
169 
170     [BLKDBG_REFTABLE_LOAD]                  = "reftable_load",
171     [BLKDBG_REFTABLE_GROW]                  = "reftable_grow",
172     [BLKDBG_REFTABLE_UPDATE]                = "reftable_update",
173 
174     [BLKDBG_REFBLOCK_LOAD]                  = "refblock_load",
175     [BLKDBG_REFBLOCK_UPDATE]                = "refblock_update",
176     [BLKDBG_REFBLOCK_UPDATE_PART]           = "refblock_update_part",
177     [BLKDBG_REFBLOCK_ALLOC]                 = "refblock_alloc",
178     [BLKDBG_REFBLOCK_ALLOC_HOOKUP]          = "refblock_alloc.hookup",
179     [BLKDBG_REFBLOCK_ALLOC_WRITE]           = "refblock_alloc.write",
180     [BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS]    = "refblock_alloc.write_blocks",
181     [BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE]     = "refblock_alloc.write_table",
182     [BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE]    = "refblock_alloc.switch_table",
183 
184     [BLKDBG_CLUSTER_ALLOC]                  = "cluster_alloc",
185     [BLKDBG_CLUSTER_ALLOC_BYTES]            = "cluster_alloc_bytes",
186     [BLKDBG_CLUSTER_FREE]                   = "cluster_free",
187 
188     [BLKDBG_FLUSH_TO_OS]                    = "flush_to_os",
189     [BLKDBG_FLUSH_TO_DISK]                  = "flush_to_disk",
190 
191     [BLKDBG_PWRITEV_RMW_HEAD]               = "pwritev_rmw.head",
192     [BLKDBG_PWRITEV_RMW_AFTER_HEAD]         = "pwritev_rmw.after_head",
193     [BLKDBG_PWRITEV_RMW_TAIL]               = "pwritev_rmw.tail",
194     [BLKDBG_PWRITEV_RMW_AFTER_TAIL]         = "pwritev_rmw.after_tail",
195     [BLKDBG_PWRITEV]                        = "pwritev",
196     [BLKDBG_PWRITEV_ZERO]                   = "pwritev_zero",
197     [BLKDBG_PWRITEV_DONE]                   = "pwritev_done",
198 };
199 
200 static int get_event_by_name(const char *name, BlkDebugEvent *event)
201 {
202     int i;
203 
204     for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
205         if (!strcmp(event_names[i], name)) {
206             *event = i;
207             return 0;
208         }
209     }
210 
211     return -1;
212 }
213 
214 struct add_rule_data {
215     BDRVBlkdebugState *s;
216     int action;
217 };
218 
219 static int add_rule(QemuOpts *opts, void *opaque)
220 {
221     struct add_rule_data *d = opaque;
222     BDRVBlkdebugState *s = d->s;
223     const char* event_name;
224     BlkDebugEvent event;
225     struct BlkdebugRule *rule;
226 
227     /* Find the right event for the rule */
228     event_name = qemu_opt_get(opts, "event");
229     if (!event_name || get_event_by_name(event_name, &event) < 0) {
230         return -1;
231     }
232 
233     /* Set attributes common for all actions */
234     rule = g_malloc0(sizeof(*rule));
235     *rule = (struct BlkdebugRule) {
236         .event  = event,
237         .action = d->action,
238         .state  = qemu_opt_get_number(opts, "state", 0),
239     };
240 
241     /* Parse action-specific options */
242     switch (d->action) {
243     case ACTION_INJECT_ERROR:
244         rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
245         rule->options.inject.once  = qemu_opt_get_bool(opts, "once", 0);
246         rule->options.inject.immediately =
247             qemu_opt_get_bool(opts, "immediately", 0);
248         rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1);
249         break;
250 
251     case ACTION_SET_STATE:
252         rule->options.set_state.new_state =
253             qemu_opt_get_number(opts, "new_state", 0);
254         break;
255 
256     case ACTION_SUSPEND:
257         rule->options.suspend.tag =
258             g_strdup(qemu_opt_get(opts, "tag"));
259         break;
260     };
261 
262     /* Add the rule */
263     QLIST_INSERT_HEAD(&s->rules[event], rule, next);
264 
265     return 0;
266 }
267 
268 static void remove_rule(BlkdebugRule *rule)
269 {
270     switch (rule->action) {
271     case ACTION_INJECT_ERROR:
272     case ACTION_SET_STATE:
273         break;
274     case ACTION_SUSPEND:
275         g_free(rule->options.suspend.tag);
276         break;
277     }
278 
279     QLIST_REMOVE(rule, next);
280     g_free(rule);
281 }
282 
283 static int read_config(BDRVBlkdebugState *s, const char *filename,
284                        QDict *options, Error **errp)
285 {
286     FILE *f = NULL;
287     int ret;
288     struct add_rule_data d;
289     Error *local_err = NULL;
290 
291     if (filename) {
292         f = fopen(filename, "r");
293         if (f == NULL) {
294             error_setg_errno(errp, errno, "Could not read blkdebug config file");
295             return -errno;
296         }
297 
298         ret = qemu_config_parse(f, config_groups, filename);
299         if (ret < 0) {
300             error_setg(errp, "Could not parse blkdebug config file");
301             ret = -EINVAL;
302             goto fail;
303         }
304     }
305 
306     qemu_config_parse_qdict(options, config_groups, &local_err);
307     if (local_err) {
308         error_propagate(errp, local_err);
309         ret = -EINVAL;
310         goto fail;
311     }
312 
313     d.s = s;
314     d.action = ACTION_INJECT_ERROR;
315     qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0);
316 
317     d.action = ACTION_SET_STATE;
318     qemu_opts_foreach(&set_state_opts, add_rule, &d, 0);
319 
320     ret = 0;
321 fail:
322     qemu_opts_reset(&inject_error_opts);
323     qemu_opts_reset(&set_state_opts);
324     if (f) {
325         fclose(f);
326     }
327     return ret;
328 }
329 
330 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
331 static void blkdebug_parse_filename(const char *filename, QDict *options,
332                                     Error **errp)
333 {
334     const char *c;
335 
336     /* Parse the blkdebug: prefix */
337     if (!strstart(filename, "blkdebug:", &filename)) {
338         /* There was no prefix; therefore, all options have to be already
339            present in the QDict (except for the filename) */
340         qdict_put(options, "x-image", qstring_from_str(filename));
341         return;
342     }
343 
344     /* Parse config file path */
345     c = strchr(filename, ':');
346     if (c == NULL) {
347         error_setg(errp, "blkdebug requires both config file and image path");
348         return;
349     }
350 
351     if (c != filename) {
352         QString *config_path;
353         config_path = qstring_from_substr(filename, 0, c - filename - 1);
354         qdict_put(options, "config", config_path);
355     }
356 
357     /* TODO Allow multi-level nesting and set file.filename here */
358     filename = c + 1;
359     qdict_put(options, "x-image", qstring_from_str(filename));
360 }
361 
362 static QemuOptsList runtime_opts = {
363     .name = "blkdebug",
364     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
365     .desc = {
366         {
367             .name = "config",
368             .type = QEMU_OPT_STRING,
369             .help = "Path to the configuration file",
370         },
371         {
372             .name = "x-image",
373             .type = QEMU_OPT_STRING,
374             .help = "[internal use only, will be removed]",
375         },
376         {
377             .name = "align",
378             .type = QEMU_OPT_SIZE,
379             .help = "Required alignment in bytes",
380         },
381         { /* end of list */ }
382     },
383 };
384 
385 static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
386                          Error **errp)
387 {
388     BDRVBlkdebugState *s = bs->opaque;
389     QemuOpts *opts;
390     Error *local_err = NULL;
391     const char *config;
392     uint64_t align;
393     int ret;
394 
395     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
396     qemu_opts_absorb_qdict(opts, options, &local_err);
397     if (local_err) {
398         error_propagate(errp, local_err);
399         ret = -EINVAL;
400         goto out;
401     }
402 
403     /* Read rules from config file or command line options */
404     config = qemu_opt_get(opts, "config");
405     ret = read_config(s, config, options, errp);
406     if (ret) {
407         goto out;
408     }
409 
410     /* Set initial state */
411     s->state = 1;
412 
413     /* Open the backing file */
414     assert(bs->file == NULL);
415     ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-image"), options, "image",
416                           flags | BDRV_O_PROTOCOL, false, &local_err);
417     if (ret < 0) {
418         error_propagate(errp, local_err);
419         goto out;
420     }
421 
422     /* Set request alignment */
423     align = qemu_opt_get_size(opts, "align", bs->request_alignment);
424     if (align > 0 && align < INT_MAX && !(align & (align - 1))) {
425         bs->request_alignment = align;
426     } else {
427         error_setg(errp, "Invalid alignment");
428         ret = -EINVAL;
429         goto fail_unref;
430     }
431 
432     ret = 0;
433     goto out;
434 
435 fail_unref:
436     bdrv_unref(bs->file);
437 out:
438     qemu_opts_del(opts);
439     return ret;
440 }
441 
442 static void error_callback_bh(void *opaque)
443 {
444     struct BlkdebugAIOCB *acb = opaque;
445     qemu_bh_delete(acb->bh);
446     acb->common.cb(acb->common.opaque, acb->ret);
447     qemu_aio_unref(acb);
448 }
449 
450 static BlockDriverAIOCB *inject_error(BlockDriverState *bs,
451     BlockDriverCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
452 {
453     BDRVBlkdebugState *s = bs->opaque;
454     int error = rule->options.inject.error;
455     struct BlkdebugAIOCB *acb;
456     QEMUBH *bh;
457 
458     if (rule->options.inject.once) {
459         QSIMPLEQ_INIT(&s->active_rules);
460     }
461 
462     if (rule->options.inject.immediately) {
463         return NULL;
464     }
465 
466     acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
467     acb->ret = -error;
468 
469     bh = aio_bh_new(bdrv_get_aio_context(bs), error_callback_bh, acb);
470     acb->bh = bh;
471     qemu_bh_schedule(bh);
472 
473     return &acb->common;
474 }
475 
476 static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
477     int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
478     BlockDriverCompletionFunc *cb, void *opaque)
479 {
480     BDRVBlkdebugState *s = bs->opaque;
481     BlkdebugRule *rule = NULL;
482 
483     QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
484         if (rule->options.inject.sector == -1 ||
485             (rule->options.inject.sector >= sector_num &&
486              rule->options.inject.sector < sector_num + nb_sectors)) {
487             break;
488         }
489     }
490 
491     if (rule && rule->options.inject.error) {
492         return inject_error(bs, cb, opaque, rule);
493     }
494 
495     return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
496 }
497 
498 static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
499     int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
500     BlockDriverCompletionFunc *cb, void *opaque)
501 {
502     BDRVBlkdebugState *s = bs->opaque;
503     BlkdebugRule *rule = NULL;
504 
505     QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
506         if (rule->options.inject.sector == -1 ||
507             (rule->options.inject.sector >= sector_num &&
508              rule->options.inject.sector < sector_num + nb_sectors)) {
509             break;
510         }
511     }
512 
513     if (rule && rule->options.inject.error) {
514         return inject_error(bs, cb, opaque, rule);
515     }
516 
517     return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
518 }
519 
520 static BlockDriverAIOCB *blkdebug_aio_flush(BlockDriverState *bs,
521     BlockDriverCompletionFunc *cb, void *opaque)
522 {
523     BDRVBlkdebugState *s = bs->opaque;
524     BlkdebugRule *rule = NULL;
525 
526     QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
527         if (rule->options.inject.sector == -1) {
528             break;
529         }
530     }
531 
532     if (rule && rule->options.inject.error) {
533         return inject_error(bs, cb, opaque, rule);
534     }
535 
536     return bdrv_aio_flush(bs->file, cb, opaque);
537 }
538 
539 
540 static void blkdebug_close(BlockDriverState *bs)
541 {
542     BDRVBlkdebugState *s = bs->opaque;
543     BlkdebugRule *rule, *next;
544     int i;
545 
546     for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
547         QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
548             remove_rule(rule);
549         }
550     }
551 }
552 
553 static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
554 {
555     BDRVBlkdebugState *s = bs->opaque;
556     BlkdebugSuspendedReq r;
557 
558     r = (BlkdebugSuspendedReq) {
559         .co         = qemu_coroutine_self(),
560         .tag        = g_strdup(rule->options.suspend.tag),
561     };
562 
563     remove_rule(rule);
564     QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
565 
566     printf("blkdebug: Suspended request '%s'\n", r.tag);
567     qemu_coroutine_yield();
568     printf("blkdebug: Resuming request '%s'\n", r.tag);
569 
570     QLIST_REMOVE(&r, next);
571     g_free(r.tag);
572 }
573 
574 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
575     bool injected)
576 {
577     BDRVBlkdebugState *s = bs->opaque;
578 
579     /* Only process rules for the current state */
580     if (rule->state && rule->state != s->state) {
581         return injected;
582     }
583 
584     /* Take the action */
585     switch (rule->action) {
586     case ACTION_INJECT_ERROR:
587         if (!injected) {
588             QSIMPLEQ_INIT(&s->active_rules);
589             injected = true;
590         }
591         QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
592         break;
593 
594     case ACTION_SET_STATE:
595         s->new_state = rule->options.set_state.new_state;
596         break;
597 
598     case ACTION_SUSPEND:
599         suspend_request(bs, rule);
600         break;
601     }
602     return injected;
603 }
604 
605 static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event)
606 {
607     BDRVBlkdebugState *s = bs->opaque;
608     struct BlkdebugRule *rule, *next;
609     bool injected;
610 
611     assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
612 
613     injected = false;
614     s->new_state = s->state;
615     QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
616         injected = process_rule(bs, rule, injected);
617     }
618     s->state = s->new_state;
619 }
620 
621 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
622                                      const char *tag)
623 {
624     BDRVBlkdebugState *s = bs->opaque;
625     struct BlkdebugRule *rule;
626     BlkDebugEvent blkdebug_event;
627 
628     if (get_event_by_name(event, &blkdebug_event) < 0) {
629         return -ENOENT;
630     }
631 
632 
633     rule = g_malloc(sizeof(*rule));
634     *rule = (struct BlkdebugRule) {
635         .event  = blkdebug_event,
636         .action = ACTION_SUSPEND,
637         .state  = 0,
638         .options.suspend.tag = g_strdup(tag),
639     };
640 
641     QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
642 
643     return 0;
644 }
645 
646 static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
647 {
648     BDRVBlkdebugState *s = bs->opaque;
649     BlkdebugSuspendedReq *r, *next;
650 
651     QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
652         if (!strcmp(r->tag, tag)) {
653             qemu_coroutine_enter(r->co, NULL);
654             return 0;
655         }
656     }
657     return -ENOENT;
658 }
659 
660 static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
661                                             const char *tag)
662 {
663     BDRVBlkdebugState *s = bs->opaque;
664     BlkdebugSuspendedReq *r, *r_next;
665     BlkdebugRule *rule, *next;
666     int i, ret = -ENOENT;
667 
668     for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
669         QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
670             if (rule->action == ACTION_SUSPEND &&
671                 !strcmp(rule->options.suspend.tag, tag)) {
672                 remove_rule(rule);
673                 ret = 0;
674             }
675         }
676     }
677     QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
678         if (!strcmp(r->tag, tag)) {
679             qemu_coroutine_enter(r->co, NULL);
680             ret = 0;
681         }
682     }
683     return ret;
684 }
685 
686 static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
687 {
688     BDRVBlkdebugState *s = bs->opaque;
689     BlkdebugSuspendedReq *r;
690 
691     QLIST_FOREACH(r, &s->suspended_reqs, next) {
692         if (!strcmp(r->tag, tag)) {
693             return true;
694         }
695     }
696     return false;
697 }
698 
699 static int64_t blkdebug_getlength(BlockDriverState *bs)
700 {
701     return bdrv_getlength(bs->file);
702 }
703 
704 static void blkdebug_refresh_filename(BlockDriverState *bs)
705 {
706     BDRVBlkdebugState *s = bs->opaque;
707     struct BlkdebugRule *rule;
708     QDict *opts;
709     QList *inject_error_list = NULL, *set_state_list = NULL;
710     QList *suspend_list = NULL;
711     int event;
712 
713     if (!bs->file->full_open_options) {
714         /* The config file cannot be recreated, so creating a plain filename
715          * is impossible */
716         return;
717     }
718 
719     opts = qdict_new();
720     qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkdebug")));
721 
722     QINCREF(bs->file->full_open_options);
723     qdict_put_obj(opts, "image", QOBJECT(bs->file->full_open_options));
724 
725     for (event = 0; event < BLKDBG_EVENT_MAX; event++) {
726         QLIST_FOREACH(rule, &s->rules[event], next) {
727             if (rule->action == ACTION_INJECT_ERROR) {
728                 QDict *inject_error = qdict_new();
729 
730                 qdict_put_obj(inject_error, "event", QOBJECT(qstring_from_str(
731                               BlkdebugEvent_lookup[rule->event])));
732                 qdict_put_obj(inject_error, "state",
733                               QOBJECT(qint_from_int(rule->state)));
734                 qdict_put_obj(inject_error, "errno", QOBJECT(qint_from_int(
735                               rule->options.inject.error)));
736                 qdict_put_obj(inject_error, "sector", QOBJECT(qint_from_int(
737                               rule->options.inject.sector)));
738                 qdict_put_obj(inject_error, "once", QOBJECT(qbool_from_int(
739                               rule->options.inject.once)));
740                 qdict_put_obj(inject_error, "immediately",
741                               QOBJECT(qbool_from_int(
742                               rule->options.inject.immediately)));
743 
744                 if (!inject_error_list) {
745                     inject_error_list = qlist_new();
746                 }
747 
748                 qlist_append_obj(inject_error_list, QOBJECT(inject_error));
749             } else if (rule->action == ACTION_SET_STATE) {
750                 QDict *set_state = qdict_new();
751 
752                 qdict_put_obj(set_state, "event", QOBJECT(qstring_from_str(
753                               BlkdebugEvent_lookup[rule->event])));
754                 qdict_put_obj(set_state, "state",
755                               QOBJECT(qint_from_int(rule->state)));
756                 qdict_put_obj(set_state, "new_state", QOBJECT(qint_from_int(
757                               rule->options.set_state.new_state)));
758 
759                 if (!set_state_list) {
760                     set_state_list = qlist_new();
761                 }
762 
763                 qlist_append_obj(set_state_list, QOBJECT(set_state));
764             } else if (rule->action == ACTION_SUSPEND) {
765                 QDict *suspend = qdict_new();
766 
767                 qdict_put_obj(suspend, "event", QOBJECT(qstring_from_str(
768                               BlkdebugEvent_lookup[rule->event])));
769                 qdict_put_obj(suspend, "state",
770                               QOBJECT(qint_from_int(rule->state)));
771                 qdict_put_obj(suspend, "tag", QOBJECT(qstring_from_str(
772                               rule->options.suspend.tag)));
773 
774                 if (!suspend_list) {
775                     suspend_list = qlist_new();
776                 }
777 
778                 qlist_append_obj(suspend_list, QOBJECT(suspend));
779             }
780         }
781     }
782 
783     if (inject_error_list) {
784         qdict_put_obj(opts, "inject-error", QOBJECT(inject_error_list));
785     }
786     if (set_state_list) {
787         qdict_put_obj(opts, "set-state", QOBJECT(set_state_list));
788     }
789     if (suspend_list) {
790         qdict_put_obj(opts, "suspend", QOBJECT(suspend_list));
791     }
792 
793     bs->full_open_options = opts;
794 }
795 
796 static BlockDriver bdrv_blkdebug = {
797     .format_name            = "blkdebug",
798     .protocol_name          = "blkdebug",
799     .instance_size          = sizeof(BDRVBlkdebugState),
800 
801     .bdrv_parse_filename    = blkdebug_parse_filename,
802     .bdrv_file_open         = blkdebug_open,
803     .bdrv_close             = blkdebug_close,
804     .bdrv_getlength         = blkdebug_getlength,
805     .bdrv_refresh_filename  = blkdebug_refresh_filename,
806 
807     .bdrv_aio_readv         = blkdebug_aio_readv,
808     .bdrv_aio_writev        = blkdebug_aio_writev,
809     .bdrv_aio_flush         = blkdebug_aio_flush,
810 
811     .bdrv_debug_event           = blkdebug_debug_event,
812     .bdrv_debug_breakpoint      = blkdebug_debug_breakpoint,
813     .bdrv_debug_remove_breakpoint
814                                 = blkdebug_debug_remove_breakpoint,
815     .bdrv_debug_resume          = blkdebug_debug_resume,
816     .bdrv_debug_is_suspended    = blkdebug_debug_is_suspended,
817 };
818 
819 static void bdrv_blkdebug_init(void)
820 {
821     bdrv_register(&bdrv_blkdebug);
822 }
823 
824 block_init(bdrv_blkdebug_init);
825