]> Git Repo - qemu.git/blob - block/blkdebug.c
blkdebug: Always call read_config()
[qemu.git] / block / blkdebug.c
1 /*
2  * Block protocol for I/O error injection
3  *
4  * Copyright (c) 2010 Kevin Wolf <[email protected]>
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
30 typedef struct BDRVBlkdebugState {
31     int state;
32     int new_state;
33
34     QLIST_HEAD(, BlkdebugRule) rules[BLKDBG_EVENT_MAX];
35     QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
36     QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
37 } BDRVBlkdebugState;
38
39 typedef struct BlkdebugAIOCB {
40     BlockDriverAIOCB common;
41     QEMUBH *bh;
42     int ret;
43 } BlkdebugAIOCB;
44
45 typedef struct BlkdebugSuspendedReq {
46     Coroutine *co;
47     char *tag;
48     QLIST_ENTRY(BlkdebugSuspendedReq) next;
49 } BlkdebugSuspendedReq;
50
51 static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb);
52
53 static const AIOCBInfo blkdebug_aiocb_info = {
54     .aiocb_size = sizeof(BlkdebugAIOCB),
55     .cancel     = blkdebug_aio_cancel,
56 };
57
58 enum {
59     ACTION_INJECT_ERROR,
60     ACTION_SET_STATE,
61     ACTION_SUSPEND,
62 };
63
64 typedef struct BlkdebugRule {
65     BlkDebugEvent event;
66     int action;
67     int state;
68     union {
69         struct {
70             int error;
71             int immediately;
72             int once;
73             int64_t sector;
74         } inject;
75         struct {
76             int new_state;
77         } set_state;
78         struct {
79             char *tag;
80         } suspend;
81     } options;
82     QLIST_ENTRY(BlkdebugRule) next;
83     QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
84 } BlkdebugRule;
85
86 static QemuOptsList inject_error_opts = {
87     .name = "inject-error",
88     .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
89     .desc = {
90         {
91             .name = "event",
92             .type = QEMU_OPT_STRING,
93         },
94         {
95             .name = "state",
96             .type = QEMU_OPT_NUMBER,
97         },
98         {
99             .name = "errno",
100             .type = QEMU_OPT_NUMBER,
101         },
102         {
103             .name = "sector",
104             .type = QEMU_OPT_NUMBER,
105         },
106         {
107             .name = "once",
108             .type = QEMU_OPT_BOOL,
109         },
110         {
111             .name = "immediately",
112             .type = QEMU_OPT_BOOL,
113         },
114         { /* end of list */ }
115     },
116 };
117
118 static QemuOptsList set_state_opts = {
119     .name = "set-state",
120     .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
121     .desc = {
122         {
123             .name = "event",
124             .type = QEMU_OPT_STRING,
125         },
126         {
127             .name = "state",
128             .type = QEMU_OPT_NUMBER,
129         },
130         {
131             .name = "new_state",
132             .type = QEMU_OPT_NUMBER,
133         },
134         { /* end of list */ }
135     },
136 };
137
138 static QemuOptsList *config_groups[] = {
139     &inject_error_opts,
140     &set_state_opts,
141     NULL
142 };
143
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",
149
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",
155
156     [BLKDBG_READ_AIO]                       = "read_aio",
157     [BLKDBG_READ_BACKING_AIO]               = "read_backing_aio",
158     [BLKDBG_READ_COMPRESSED]                = "read_compressed",
159
160     [BLKDBG_WRITE_AIO]                      = "write_aio",
161     [BLKDBG_WRITE_COMPRESSED]               = "write_compressed",
162
163     [BLKDBG_VMSTATE_LOAD]                   = "vmstate_load",
164     [BLKDBG_VMSTATE_SAVE]                   = "vmstate_save",
165
166     [BLKDBG_COW_READ]                       = "cow_read",
167     [BLKDBG_COW_WRITE]                      = "cow_write",
168
169     [BLKDBG_REFTABLE_LOAD]                  = "reftable_load",
170     [BLKDBG_REFTABLE_GROW]                  = "reftable_grow",
171     [BLKDBG_REFTABLE_UPDATE]                = "reftable_update",
172
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",
182
183     [BLKDBG_CLUSTER_ALLOC]                  = "cluster_alloc",
184     [BLKDBG_CLUSTER_ALLOC_BYTES]            = "cluster_alloc_bytes",
185     [BLKDBG_CLUSTER_FREE]                   = "cluster_free",
186
187     [BLKDBG_FLUSH_TO_OS]                    = "flush_to_os",
188     [BLKDBG_FLUSH_TO_DISK]                  = "flush_to_disk",
189 };
190
191 static int get_event_by_name(const char *name, BlkDebugEvent *event)
192 {
193     int i;
194
195     for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
196         if (!strcmp(event_names[i], name)) {
197             *event = i;
198             return 0;
199         }
200     }
201
202     return -1;
203 }
204
205 struct add_rule_data {
206     BDRVBlkdebugState *s;
207     int action;
208 };
209
210 static int add_rule(QemuOpts *opts, void *opaque)
211 {
212     struct add_rule_data *d = opaque;
213     BDRVBlkdebugState *s = d->s;
214     const char* event_name;
215     BlkDebugEvent event;
216     struct BlkdebugRule *rule;
217
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) {
221         return -1;
222     }
223
224     /* Set attributes common for all actions */
225     rule = g_malloc0(sizeof(*rule));
226     *rule = (struct BlkdebugRule) {
227         .event  = event,
228         .action = d->action,
229         .state  = qemu_opt_get_number(opts, "state", 0),
230     };
231
232     /* Parse action-specific options */
233     switch (d->action) {
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);
240         break;
241
242     case ACTION_SET_STATE:
243         rule->options.set_state.new_state =
244             qemu_opt_get_number(opts, "new_state", 0);
245         break;
246
247     case ACTION_SUSPEND:
248         rule->options.suspend.tag =
249             g_strdup(qemu_opt_get(opts, "tag"));
250         break;
251     };
252
253     /* Add the rule */
254     QLIST_INSERT_HEAD(&s->rules[event], rule, next);
255
256     return 0;
257 }
258
259 static void remove_rule(BlkdebugRule *rule)
260 {
261     switch (rule->action) {
262     case ACTION_INJECT_ERROR:
263     case ACTION_SET_STATE:
264         break;
265     case ACTION_SUSPEND:
266         g_free(rule->options.suspend.tag);
267         break;
268     }
269
270     QLIST_REMOVE(rule, next);
271     g_free(rule);
272 }
273
274 static int read_config(BDRVBlkdebugState *s, const char *filename, Error **errp)
275 {
276     FILE *f = NULL;
277     int ret;
278     struct add_rule_data d;
279
280     if (filename) {
281         f = fopen(filename, "r");
282         if (f == NULL) {
283             error_setg_errno(errp, errno, "Could not read blkdebug config file");
284             return -errno;
285         }
286
287         ret = qemu_config_parse(f, config_groups, filename);
288         if (ret < 0) {
289             error_setg(errp, "Could not parse blkdebug config file");
290             ret = -EINVAL;
291             goto fail;
292         }
293     }
294
295     d.s = s;
296     d.action = ACTION_INJECT_ERROR;
297     qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0);
298
299     d.action = ACTION_SET_STATE;
300     qemu_opts_foreach(&set_state_opts, add_rule, &d, 0);
301
302     ret = 0;
303 fail:
304     qemu_opts_reset(&inject_error_opts);
305     qemu_opts_reset(&set_state_opts);
306     if (f) {
307         fclose(f);
308     }
309     return ret;
310 }
311
312 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
313 static void blkdebug_parse_filename(const char *filename, QDict *options,
314                                     Error **errp)
315 {
316     const char *c;
317
318     /* Parse the blkdebug: prefix */
319     if (!strstart(filename, "blkdebug:", &filename)) {
320         /* There was no prefix; therefore, all options have to be already
321            present in the QDict (except for the filename) */
322         qdict_put(options, "x-image", qstring_from_str(filename));
323         return;
324     }
325
326     /* Parse config file path */
327     c = strchr(filename, ':');
328     if (c == NULL) {
329         error_setg(errp, "blkdebug requires both config file and image path");
330         return;
331     }
332
333     if (c != filename) {
334         QString *config_path;
335         config_path = qstring_from_substr(filename, 0, c - filename - 1);
336         qdict_put(options, "config", config_path);
337     }
338
339     /* TODO Allow multi-level nesting and set file.filename here */
340     filename = c + 1;
341     qdict_put(options, "x-image", qstring_from_str(filename));
342 }
343
344 static QemuOptsList runtime_opts = {
345     .name = "blkdebug",
346     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
347     .desc = {
348         {
349             .name = "config",
350             .type = QEMU_OPT_STRING,
351             .help = "Path to the configuration file",
352         },
353         {
354             .name = "x-image",
355             .type = QEMU_OPT_STRING,
356             .help = "[internal use only, will be removed]",
357         },
358         { /* end of list */ }
359     },
360 };
361
362 static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
363                          Error **errp)
364 {
365     BDRVBlkdebugState *s = bs->opaque;
366     QemuOpts *opts;
367     Error *local_err = NULL;
368     const char *filename, *config;
369     int ret;
370
371     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
372     qemu_opts_absorb_qdict(opts, options, &local_err);
373     if (error_is_set(&local_err)) {
374         error_propagate(errp, local_err);
375         ret = -EINVAL;
376         goto fail;
377     }
378
379     /* Read rules from config file */
380     config = qemu_opt_get(opts, "config");
381     ret = read_config(s, config, errp);
382     if (ret) {
383         goto fail;
384     }
385
386     /* Set initial state */
387     s->state = 1;
388
389     /* Open the backing file */
390     filename = qemu_opt_get(opts, "x-image");
391     if (filename == NULL) {
392         error_setg(errp, "Could not retrieve image file name");
393         ret = -EINVAL;
394         goto fail;
395     }
396
397     ret = bdrv_file_open(&bs->file, filename, NULL, flags, &local_err);
398     if (ret < 0) {
399         error_propagate(errp, local_err);
400         goto fail;
401     }
402
403     ret = 0;
404 fail:
405     qemu_opts_del(opts);
406     return ret;
407 }
408
409 static void error_callback_bh(void *opaque)
410 {
411     struct BlkdebugAIOCB *acb = opaque;
412     qemu_bh_delete(acb->bh);
413     acb->common.cb(acb->common.opaque, acb->ret);
414     qemu_aio_release(acb);
415 }
416
417 static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb)
418 {
419     BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common);
420     qemu_aio_release(acb);
421 }
422
423 static BlockDriverAIOCB *inject_error(BlockDriverState *bs,
424     BlockDriverCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
425 {
426     BDRVBlkdebugState *s = bs->opaque;
427     int error = rule->options.inject.error;
428     struct BlkdebugAIOCB *acb;
429     QEMUBH *bh;
430
431     if (rule->options.inject.once) {
432         QSIMPLEQ_INIT(&s->active_rules);
433     }
434
435     if (rule->options.inject.immediately) {
436         return NULL;
437     }
438
439     acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
440     acb->ret = -error;
441
442     bh = qemu_bh_new(error_callback_bh, acb);
443     acb->bh = bh;
444     qemu_bh_schedule(bh);
445
446     return &acb->common;
447 }
448
449 static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
450     int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
451     BlockDriverCompletionFunc *cb, void *opaque)
452 {
453     BDRVBlkdebugState *s = bs->opaque;
454     BlkdebugRule *rule = NULL;
455
456     QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
457         if (rule->options.inject.sector == -1 ||
458             (rule->options.inject.sector >= sector_num &&
459              rule->options.inject.sector < sector_num + nb_sectors)) {
460             break;
461         }
462     }
463
464     if (rule && rule->options.inject.error) {
465         return inject_error(bs, cb, opaque, rule);
466     }
467
468     return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
469 }
470
471 static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
472     int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
473     BlockDriverCompletionFunc *cb, void *opaque)
474 {
475     BDRVBlkdebugState *s = bs->opaque;
476     BlkdebugRule *rule = NULL;
477
478     QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
479         if (rule->options.inject.sector == -1 ||
480             (rule->options.inject.sector >= sector_num &&
481              rule->options.inject.sector < sector_num + nb_sectors)) {
482             break;
483         }
484     }
485
486     if (rule && rule->options.inject.error) {
487         return inject_error(bs, cb, opaque, rule);
488     }
489
490     return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
491 }
492
493
494 static void blkdebug_close(BlockDriverState *bs)
495 {
496     BDRVBlkdebugState *s = bs->opaque;
497     BlkdebugRule *rule, *next;
498     int i;
499
500     for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
501         QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
502             remove_rule(rule);
503         }
504     }
505 }
506
507 static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
508 {
509     BDRVBlkdebugState *s = bs->opaque;
510     BlkdebugSuspendedReq r;
511
512     r = (BlkdebugSuspendedReq) {
513         .co         = qemu_coroutine_self(),
514         .tag        = g_strdup(rule->options.suspend.tag),
515     };
516
517     remove_rule(rule);
518     QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
519
520     printf("blkdebug: Suspended request '%s'\n", r.tag);
521     qemu_coroutine_yield();
522     printf("blkdebug: Resuming request '%s'\n", r.tag);
523
524     QLIST_REMOVE(&r, next);
525     g_free(r.tag);
526 }
527
528 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
529     bool injected)
530 {
531     BDRVBlkdebugState *s = bs->opaque;
532
533     /* Only process rules for the current state */
534     if (rule->state && rule->state != s->state) {
535         return injected;
536     }
537
538     /* Take the action */
539     switch (rule->action) {
540     case ACTION_INJECT_ERROR:
541         if (!injected) {
542             QSIMPLEQ_INIT(&s->active_rules);
543             injected = true;
544         }
545         QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
546         break;
547
548     case ACTION_SET_STATE:
549         s->new_state = rule->options.set_state.new_state;
550         break;
551
552     case ACTION_SUSPEND:
553         suspend_request(bs, rule);
554         break;
555     }
556     return injected;
557 }
558
559 static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event)
560 {
561     BDRVBlkdebugState *s = bs->opaque;
562     struct BlkdebugRule *rule, *next;
563     bool injected;
564
565     assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
566
567     injected = false;
568     s->new_state = s->state;
569     QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
570         injected = process_rule(bs, rule, injected);
571     }
572     s->state = s->new_state;
573 }
574
575 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
576                                      const char *tag)
577 {
578     BDRVBlkdebugState *s = bs->opaque;
579     struct BlkdebugRule *rule;
580     BlkDebugEvent blkdebug_event;
581
582     if (get_event_by_name(event, &blkdebug_event) < 0) {
583         return -ENOENT;
584     }
585
586
587     rule = g_malloc(sizeof(*rule));
588     *rule = (struct BlkdebugRule) {
589         .event  = blkdebug_event,
590         .action = ACTION_SUSPEND,
591         .state  = 0,
592         .options.suspend.tag = g_strdup(tag),
593     };
594
595     QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
596
597     return 0;
598 }
599
600 static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
601 {
602     BDRVBlkdebugState *s = bs->opaque;
603     BlkdebugSuspendedReq *r, *next;
604
605     QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
606         if (!strcmp(r->tag, tag)) {
607             qemu_coroutine_enter(r->co, NULL);
608             return 0;
609         }
610     }
611     return -ENOENT;
612 }
613
614 static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
615                                             const char *tag)
616 {
617     BDRVBlkdebugState *s = bs->opaque;
618     BlkdebugSuspendedReq *r, *r_next;
619     BlkdebugRule *rule, *next;
620     int i, ret = -ENOENT;
621
622     for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
623         QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
624             if (rule->action == ACTION_SUSPEND &&
625                 !strcmp(rule->options.suspend.tag, tag)) {
626                 remove_rule(rule);
627                 ret = 0;
628             }
629         }
630     }
631     QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
632         if (!strcmp(r->tag, tag)) {
633             qemu_coroutine_enter(r->co, NULL);
634             ret = 0;
635         }
636     }
637     return ret;
638 }
639
640 static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
641 {
642     BDRVBlkdebugState *s = bs->opaque;
643     BlkdebugSuspendedReq *r;
644
645     QLIST_FOREACH(r, &s->suspended_reqs, next) {
646         if (!strcmp(r->tag, tag)) {
647             return true;
648         }
649     }
650     return false;
651 }
652
653 static int64_t blkdebug_getlength(BlockDriverState *bs)
654 {
655     return bdrv_getlength(bs->file);
656 }
657
658 static BlockDriver bdrv_blkdebug = {
659     .format_name            = "blkdebug",
660     .protocol_name          = "blkdebug",
661     .instance_size          = sizeof(BDRVBlkdebugState),
662
663     .bdrv_parse_filename    = blkdebug_parse_filename,
664     .bdrv_file_open         = blkdebug_open,
665     .bdrv_close             = blkdebug_close,
666     .bdrv_getlength         = blkdebug_getlength,
667
668     .bdrv_aio_readv         = blkdebug_aio_readv,
669     .bdrv_aio_writev        = blkdebug_aio_writev,
670
671     .bdrv_debug_event           = blkdebug_debug_event,
672     .bdrv_debug_breakpoint      = blkdebug_debug_breakpoint,
673     .bdrv_debug_remove_breakpoint
674                                 = blkdebug_debug_remove_breakpoint,
675     .bdrv_debug_resume          = blkdebug_debug_resume,
676     .bdrv_debug_is_suspended    = blkdebug_debug_is_suspended,
677 };
678
679 static void bdrv_blkdebug_init(void)
680 {
681     bdrv_register(&bdrv_blkdebug);
682 }
683
684 block_init(bdrv_blkdebug_init);
This page took 0.062323 seconds and 4 git commands to generate.