xref: /openbmc/qemu/block/blkdebug.c (revision 2c9b15ca)
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 
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 
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",
181 
182     [BLKDBG_CLUSTER_ALLOC]                  = "cluster_alloc",
183     [BLKDBG_CLUSTER_ALLOC_BYTES]            = "cluster_alloc_bytes",
184     [BLKDBG_CLUSTER_FREE]                   = "cluster_free",
185 
186     [BLKDBG_FLUSH_TO_OS]                    = "flush_to_os",
187     [BLKDBG_FLUSH_TO_DISK]                  = "flush_to_disk",
188 };
189 
190 static int get_event_by_name(const char *name, BlkDebugEvent *event)
191 {
192     int i;
193 
194     for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
195         if (!strcmp(event_names[i], name)) {
196             *event = i;
197             return 0;
198         }
199     }
200 
201     return -1;
202 }
203 
204 struct add_rule_data {
205     BDRVBlkdebugState *s;
206     int action;
207 };
208 
209 static int add_rule(QemuOpts *opts, void *opaque)
210 {
211     struct add_rule_data *d = opaque;
212     BDRVBlkdebugState *s = d->s;
213     const char* event_name;
214     BlkDebugEvent event;
215     struct BlkdebugRule *rule;
216 
217     /* Find the right event for the rule */
218     event_name = qemu_opt_get(opts, "event");
219     if (!event_name || get_event_by_name(event_name, &event) < 0) {
220         return -1;
221     }
222 
223     /* Set attributes common for all actions */
224     rule = g_malloc0(sizeof(*rule));
225     *rule = (struct BlkdebugRule) {
226         .event  = event,
227         .action = d->action,
228         .state  = qemu_opt_get_number(opts, "state", 0),
229     };
230 
231     /* Parse action-specific options */
232     switch (d->action) {
233     case ACTION_INJECT_ERROR:
234         rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
235         rule->options.inject.once  = qemu_opt_get_bool(opts, "once", 0);
236         rule->options.inject.immediately =
237             qemu_opt_get_bool(opts, "immediately", 0);
238         rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1);
239         break;
240 
241     case ACTION_SET_STATE:
242         rule->options.set_state.new_state =
243             qemu_opt_get_number(opts, "new_state", 0);
244         break;
245 
246     case ACTION_SUSPEND:
247         rule->options.suspend.tag =
248             g_strdup(qemu_opt_get(opts, "tag"));
249         break;
250     };
251 
252     /* Add the rule */
253     QLIST_INSERT_HEAD(&s->rules[event], rule, next);
254 
255     return 0;
256 }
257 
258 static void remove_rule(BlkdebugRule *rule)
259 {
260     switch (rule->action) {
261     case ACTION_INJECT_ERROR:
262     case ACTION_SET_STATE:
263         break;
264     case ACTION_SUSPEND:
265         g_free(rule->options.suspend.tag);
266         break;
267     }
268 
269     QLIST_REMOVE(rule, next);
270     g_free(rule);
271 }
272 
273 static int read_config(BDRVBlkdebugState *s, const char *filename)
274 {
275     FILE *f;
276     int ret;
277     struct add_rule_data d;
278 
279     f = fopen(filename, "r");
280     if (f == NULL) {
281         return -errno;
282     }
283 
284     ret = qemu_config_parse(f, config_groups, filename);
285     if (ret < 0) {
286         goto fail;
287     }
288 
289     d.s = s;
290     d.action = ACTION_INJECT_ERROR;
291     qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0);
292 
293     d.action = ACTION_SET_STATE;
294     qemu_opts_foreach(&set_state_opts, add_rule, &d, 0);
295 
296     ret = 0;
297 fail:
298     qemu_opts_reset(&inject_error_opts);
299     qemu_opts_reset(&set_state_opts);
300     fclose(f);
301     return ret;
302 }
303 
304 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
305 static void blkdebug_parse_filename(const char *filename, QDict *options,
306                                     Error **errp)
307 {
308     const char *c;
309 
310     /* Parse the blkdebug: prefix */
311     if (!strstart(filename, "blkdebug:", &filename)) {
312         error_setg(errp, "File name string must start with 'blkdebug:'");
313         return;
314     }
315 
316     /* Parse config file path */
317     c = strchr(filename, ':');
318     if (c == NULL) {
319         error_setg(errp, "blkdebug requires both config file and image path");
320         return;
321     }
322 
323     if (c != filename) {
324         QString *config_path;
325         config_path = qstring_from_substr(filename, 0, c - filename - 1);
326         qdict_put(options, "config", config_path);
327     }
328 
329     /* TODO Allow multi-level nesting and set file.filename here */
330     filename = c + 1;
331     qdict_put(options, "x-image", qstring_from_str(filename));
332 }
333 
334 static QemuOptsList runtime_opts = {
335     .name = "blkdebug",
336     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
337     .desc = {
338         {
339             .name = "config",
340             .type = QEMU_OPT_STRING,
341             .help = "Path to the configuration file",
342         },
343         {
344             .name = "x-image",
345             .type = QEMU_OPT_STRING,
346             .help = "[internal use only, will be removed]",
347         },
348         { /* end of list */ }
349     },
350 };
351 
352 static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags)
353 {
354     BDRVBlkdebugState *s = bs->opaque;
355     QemuOpts *opts;
356     Error *local_err = NULL;
357     const char *filename, *config;
358     int ret;
359 
360     opts = qemu_opts_create_nofail(&runtime_opts);
361     qemu_opts_absorb_qdict(opts, options, &local_err);
362     if (error_is_set(&local_err)) {
363         qerror_report_err(local_err);
364         error_free(local_err);
365         ret = -EINVAL;
366         goto fail;
367     }
368 
369     /* Read rules from config file */
370     config = qemu_opt_get(opts, "config");
371     if (config) {
372         ret = read_config(s, config);
373         if (ret < 0) {
374             goto fail;
375         }
376     }
377 
378     /* Set initial state */
379     s->state = 1;
380 
381     /* Open the backing file */
382     filename = qemu_opt_get(opts, "x-image");
383     if (filename == NULL) {
384         ret = -EINVAL;
385         goto fail;
386     }
387 
388     ret = bdrv_file_open(&bs->file, filename, NULL, flags);
389     if (ret < 0) {
390         goto fail;
391     }
392 
393     ret = 0;
394 fail:
395     qemu_opts_del(opts);
396     return ret;
397 }
398 
399 static void error_callback_bh(void *opaque)
400 {
401     struct BlkdebugAIOCB *acb = opaque;
402     qemu_bh_delete(acb->bh);
403     acb->common.cb(acb->common.opaque, acb->ret);
404     qemu_aio_release(acb);
405 }
406 
407 static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb)
408 {
409     BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common);
410     qemu_aio_release(acb);
411 }
412 
413 static BlockDriverAIOCB *inject_error(BlockDriverState *bs,
414     BlockDriverCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
415 {
416     BDRVBlkdebugState *s = bs->opaque;
417     int error = rule->options.inject.error;
418     struct BlkdebugAIOCB *acb;
419     QEMUBH *bh;
420 
421     if (rule->options.inject.once) {
422         QSIMPLEQ_INIT(&s->active_rules);
423     }
424 
425     if (rule->options.inject.immediately) {
426         return NULL;
427     }
428 
429     acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
430     acb->ret = -error;
431 
432     bh = qemu_bh_new(error_callback_bh, acb);
433     acb->bh = bh;
434     qemu_bh_schedule(bh);
435 
436     return &acb->common;
437 }
438 
439 static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
440     int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
441     BlockDriverCompletionFunc *cb, void *opaque)
442 {
443     BDRVBlkdebugState *s = bs->opaque;
444     BlkdebugRule *rule = NULL;
445 
446     QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
447         if (rule->options.inject.sector == -1 ||
448             (rule->options.inject.sector >= sector_num &&
449              rule->options.inject.sector < sector_num + nb_sectors)) {
450             break;
451         }
452     }
453 
454     if (rule && rule->options.inject.error) {
455         return inject_error(bs, cb, opaque, rule);
456     }
457 
458     return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
459 }
460 
461 static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
462     int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
463     BlockDriverCompletionFunc *cb, void *opaque)
464 {
465     BDRVBlkdebugState *s = bs->opaque;
466     BlkdebugRule *rule = NULL;
467 
468     QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
469         if (rule->options.inject.sector == -1 ||
470             (rule->options.inject.sector >= sector_num &&
471              rule->options.inject.sector < sector_num + nb_sectors)) {
472             break;
473         }
474     }
475 
476     if (rule && rule->options.inject.error) {
477         return inject_error(bs, cb, opaque, rule);
478     }
479 
480     return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
481 }
482 
483 
484 static void blkdebug_close(BlockDriverState *bs)
485 {
486     BDRVBlkdebugState *s = bs->opaque;
487     BlkdebugRule *rule, *next;
488     int i;
489 
490     for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
491         QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
492             remove_rule(rule);
493         }
494     }
495 }
496 
497 static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
498 {
499     BDRVBlkdebugState *s = bs->opaque;
500     BlkdebugSuspendedReq r;
501 
502     r = (BlkdebugSuspendedReq) {
503         .co         = qemu_coroutine_self(),
504         .tag        = g_strdup(rule->options.suspend.tag),
505     };
506 
507     remove_rule(rule);
508     QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
509 
510     printf("blkdebug: Suspended request '%s'\n", r.tag);
511     qemu_coroutine_yield();
512     printf("blkdebug: Resuming request '%s'\n", r.tag);
513 
514     QLIST_REMOVE(&r, next);
515     g_free(r.tag);
516 }
517 
518 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
519     bool injected)
520 {
521     BDRVBlkdebugState *s = bs->opaque;
522 
523     /* Only process rules for the current state */
524     if (rule->state && rule->state != s->state) {
525         return injected;
526     }
527 
528     /* Take the action */
529     switch (rule->action) {
530     case ACTION_INJECT_ERROR:
531         if (!injected) {
532             QSIMPLEQ_INIT(&s->active_rules);
533             injected = true;
534         }
535         QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
536         break;
537 
538     case ACTION_SET_STATE:
539         s->new_state = rule->options.set_state.new_state;
540         break;
541 
542     case ACTION_SUSPEND:
543         suspend_request(bs, rule);
544         break;
545     }
546     return injected;
547 }
548 
549 static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event)
550 {
551     BDRVBlkdebugState *s = bs->opaque;
552     struct BlkdebugRule *rule, *next;
553     bool injected;
554 
555     assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
556 
557     injected = false;
558     s->new_state = s->state;
559     QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
560         injected = process_rule(bs, rule, injected);
561     }
562     s->state = s->new_state;
563 }
564 
565 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
566                                      const char *tag)
567 {
568     BDRVBlkdebugState *s = bs->opaque;
569     struct BlkdebugRule *rule;
570     BlkDebugEvent blkdebug_event;
571 
572     if (get_event_by_name(event, &blkdebug_event) < 0) {
573         return -ENOENT;
574     }
575 
576 
577     rule = g_malloc(sizeof(*rule));
578     *rule = (struct BlkdebugRule) {
579         .event  = blkdebug_event,
580         .action = ACTION_SUSPEND,
581         .state  = 0,
582         .options.suspend.tag = g_strdup(tag),
583     };
584 
585     QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
586 
587     return 0;
588 }
589 
590 static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
591 {
592     BDRVBlkdebugState *s = bs->opaque;
593     BlkdebugSuspendedReq *r;
594 
595     QLIST_FOREACH(r, &s->suspended_reqs, next) {
596         if (!strcmp(r->tag, tag)) {
597             qemu_coroutine_enter(r->co, NULL);
598             return 0;
599         }
600     }
601     return -ENOENT;
602 }
603 
604 
605 static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
606 {
607     BDRVBlkdebugState *s = bs->opaque;
608     BlkdebugSuspendedReq *r;
609 
610     QLIST_FOREACH(r, &s->suspended_reqs, next) {
611         if (!strcmp(r->tag, tag)) {
612             return true;
613         }
614     }
615     return false;
616 }
617 
618 static int64_t blkdebug_getlength(BlockDriverState *bs)
619 {
620     return bdrv_getlength(bs->file);
621 }
622 
623 static BlockDriver bdrv_blkdebug = {
624     .format_name            = "blkdebug",
625     .protocol_name          = "blkdebug",
626     .instance_size          = sizeof(BDRVBlkdebugState),
627 
628     .bdrv_parse_filename    = blkdebug_parse_filename,
629     .bdrv_file_open         = blkdebug_open,
630     .bdrv_close             = blkdebug_close,
631     .bdrv_getlength         = blkdebug_getlength,
632 
633     .bdrv_aio_readv         = blkdebug_aio_readv,
634     .bdrv_aio_writev        = blkdebug_aio_writev,
635 
636     .bdrv_debug_event           = blkdebug_debug_event,
637     .bdrv_debug_breakpoint      = blkdebug_debug_breakpoint,
638     .bdrv_debug_resume          = blkdebug_debug_resume,
639     .bdrv_debug_is_suspended    = blkdebug_debug_is_suspended,
640 };
641 
642 static void bdrv_blkdebug_init(void)
643 {
644     bdrv_register(&bdrv_blkdebug);
645 }
646 
647 block_init(bdrv_blkdebug_init);
648