xref: /openbmc/qemu/block/blkdebug.c (revision 7acafcfa)
1 /*
2  * Block protocol for I/O error injection
3  *
4  * Copyright (C) 2016-2017 Red Hat, Inc.
5  * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "qemu/cutils.h"
29 #include "qemu/config-file.h"
30 #include "block/block_int.h"
31 #include "block/qdict.h"
32 #include "qemu/module.h"
33 #include "qemu/option.h"
34 #include "qapi/qapi-visit-block-core.h"
35 #include "qapi/qmp/qdict.h"
36 #include "qapi/qmp/qlist.h"
37 #include "qapi/qmp/qstring.h"
38 #include "qapi/qobject-input-visitor.h"
39 #include "sysemu/qtest.h"
40 
41 typedef struct BDRVBlkdebugState {
42     int state;
43     int new_state;
44     uint64_t align;
45     uint64_t max_transfer;
46     uint64_t opt_write_zero;
47     uint64_t max_write_zero;
48     uint64_t opt_discard;
49     uint64_t max_discard;
50 
51     uint64_t take_child_perms;
52     uint64_t unshare_child_perms;
53 
54     /* For blkdebug_refresh_filename() */
55     char *config_file;
56 
57     QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
58     QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
59     QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
60 } BDRVBlkdebugState;
61 
62 typedef struct BlkdebugAIOCB {
63     BlockAIOCB common;
64     int ret;
65 } BlkdebugAIOCB;
66 
67 typedef struct BlkdebugSuspendedReq {
68     Coroutine *co;
69     char *tag;
70     QLIST_ENTRY(BlkdebugSuspendedReq) next;
71 } BlkdebugSuspendedReq;
72 
73 enum {
74     ACTION_INJECT_ERROR,
75     ACTION_SET_STATE,
76     ACTION_SUSPEND,
77 };
78 
79 typedef struct BlkdebugRule {
80     BlkdebugEvent event;
81     int action;
82     int state;
83     union {
84         struct {
85             uint64_t iotype_mask;
86             int error;
87             int immediately;
88             int once;
89             int64_t offset;
90         } inject;
91         struct {
92             int new_state;
93         } set_state;
94         struct {
95             char *tag;
96         } suspend;
97     } options;
98     QLIST_ENTRY(BlkdebugRule) next;
99     QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
100 } BlkdebugRule;
101 
102 QEMU_BUILD_BUG_MSG(BLKDEBUG_IO_TYPE__MAX > 64,
103                    "BlkdebugIOType mask does not fit into an uint64_t");
104 
105 static QemuOptsList inject_error_opts = {
106     .name = "inject-error",
107     .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
108     .desc = {
109         {
110             .name = "event",
111             .type = QEMU_OPT_STRING,
112         },
113         {
114             .name = "state",
115             .type = QEMU_OPT_NUMBER,
116         },
117         {
118             .name = "iotype",
119             .type = QEMU_OPT_STRING,
120         },
121         {
122             .name = "errno",
123             .type = QEMU_OPT_NUMBER,
124         },
125         {
126             .name = "sector",
127             .type = QEMU_OPT_NUMBER,
128         },
129         {
130             .name = "once",
131             .type = QEMU_OPT_BOOL,
132         },
133         {
134             .name = "immediately",
135             .type = QEMU_OPT_BOOL,
136         },
137         { /* end of list */ }
138     },
139 };
140 
141 static QemuOptsList set_state_opts = {
142     .name = "set-state",
143     .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
144     .desc = {
145         {
146             .name = "event",
147             .type = QEMU_OPT_STRING,
148         },
149         {
150             .name = "state",
151             .type = QEMU_OPT_NUMBER,
152         },
153         {
154             .name = "new_state",
155             .type = QEMU_OPT_NUMBER,
156         },
157         { /* end of list */ }
158     },
159 };
160 
161 static QemuOptsList *config_groups[] = {
162     &inject_error_opts,
163     &set_state_opts,
164     NULL
165 };
166 
167 struct add_rule_data {
168     BDRVBlkdebugState *s;
169     int action;
170 };
171 
172 static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
173 {
174     struct add_rule_data *d = opaque;
175     BDRVBlkdebugState *s = d->s;
176     const char* event_name;
177     int event;
178     struct BlkdebugRule *rule;
179     int64_t sector;
180     BlkdebugIOType iotype;
181     Error *local_error = NULL;
182 
183     /* Find the right event for the rule */
184     event_name = qemu_opt_get(opts, "event");
185     if (!event_name) {
186         error_setg(errp, "Missing event name for rule");
187         return -1;
188     }
189     event = qapi_enum_parse(&BlkdebugEvent_lookup, event_name, -1, errp);
190     if (event < 0) {
191         return -1;
192     }
193 
194     /* Set attributes common for all actions */
195     rule = g_malloc0(sizeof(*rule));
196     *rule = (struct BlkdebugRule) {
197         .event  = event,
198         .action = d->action,
199         .state  = qemu_opt_get_number(opts, "state", 0),
200     };
201 
202     /* Parse action-specific options */
203     switch (d->action) {
204     case ACTION_INJECT_ERROR:
205         rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
206         rule->options.inject.once  = qemu_opt_get_bool(opts, "once", 0);
207         rule->options.inject.immediately =
208             qemu_opt_get_bool(opts, "immediately", 0);
209         sector = qemu_opt_get_number(opts, "sector", -1);
210         rule->options.inject.offset =
211             sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE;
212 
213         iotype = qapi_enum_parse(&BlkdebugIOType_lookup,
214                                  qemu_opt_get(opts, "iotype"),
215                                  BLKDEBUG_IO_TYPE__MAX, &local_error);
216         if (local_error) {
217             error_propagate(errp, local_error);
218             return -1;
219         }
220         if (iotype != BLKDEBUG_IO_TYPE__MAX) {
221             rule->options.inject.iotype_mask = (1ull << iotype);
222         } else {
223             /* Apply the default */
224             rule->options.inject.iotype_mask =
225                 (1ull << BLKDEBUG_IO_TYPE_READ)
226                 | (1ull << BLKDEBUG_IO_TYPE_WRITE)
227                 | (1ull << BLKDEBUG_IO_TYPE_WRITE_ZEROES)
228                 | (1ull << BLKDEBUG_IO_TYPE_DISCARD)
229                 | (1ull << BLKDEBUG_IO_TYPE_FLUSH);
230         }
231 
232         break;
233 
234     case ACTION_SET_STATE:
235         rule->options.set_state.new_state =
236             qemu_opt_get_number(opts, "new_state", 0);
237         break;
238 
239     case ACTION_SUSPEND:
240         rule->options.suspend.tag =
241             g_strdup(qemu_opt_get(opts, "tag"));
242         break;
243     };
244 
245     /* Add the rule */
246     QLIST_INSERT_HEAD(&s->rules[event], rule, next);
247 
248     return 0;
249 }
250 
251 static void remove_rule(BlkdebugRule *rule)
252 {
253     switch (rule->action) {
254     case ACTION_INJECT_ERROR:
255     case ACTION_SET_STATE:
256         break;
257     case ACTION_SUSPEND:
258         g_free(rule->options.suspend.tag);
259         break;
260     }
261 
262     QLIST_REMOVE(rule, next);
263     g_free(rule);
264 }
265 
266 static int read_config(BDRVBlkdebugState *s, const char *filename,
267                        QDict *options, Error **errp)
268 {
269     FILE *f = NULL;
270     int ret;
271     struct add_rule_data d;
272     Error *local_err = NULL;
273 
274     if (filename) {
275         f = fopen(filename, "r");
276         if (f == NULL) {
277             error_setg_errno(errp, errno, "Could not read blkdebug config file");
278             return -errno;
279         }
280 
281         ret = qemu_config_parse(f, config_groups, filename);
282         if (ret < 0) {
283             error_setg(errp, "Could not parse blkdebug config file");
284             goto fail;
285         }
286     }
287 
288     qemu_config_parse_qdict(options, config_groups, &local_err);
289     if (local_err) {
290         error_propagate(errp, local_err);
291         ret = -EINVAL;
292         goto fail;
293     }
294 
295     d.s = s;
296     d.action = ACTION_INJECT_ERROR;
297     qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
298     if (local_err) {
299         error_propagate(errp, local_err);
300         ret = -EINVAL;
301         goto fail;
302     }
303 
304     d.action = ACTION_SET_STATE;
305     qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
306     if (local_err) {
307         error_propagate(errp, local_err);
308         ret = -EINVAL;
309         goto fail;
310     }
311 
312     ret = 0;
313 fail:
314     qemu_opts_reset(&inject_error_opts);
315     qemu_opts_reset(&set_state_opts);
316     if (f) {
317         fclose(f);
318     }
319     return ret;
320 }
321 
322 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
323 static void blkdebug_parse_filename(const char *filename, QDict *options,
324                                     Error **errp)
325 {
326     const char *c;
327 
328     /* Parse the blkdebug: prefix */
329     if (!strstart(filename, "blkdebug:", &filename)) {
330         /* There was no prefix; therefore, all options have to be already
331            present in the QDict (except for the filename) */
332         qdict_put_str(options, "x-image", filename);
333         return;
334     }
335 
336     /* Parse config file path */
337     c = strchr(filename, ':');
338     if (c == NULL) {
339         error_setg(errp, "blkdebug requires both config file and image path");
340         return;
341     }
342 
343     if (c != filename) {
344         QString *config_path;
345         config_path = qstring_from_substr(filename, 0, c - filename);
346         qdict_put(options, "config", config_path);
347     }
348 
349     /* TODO Allow multi-level nesting and set file.filename here */
350     filename = c + 1;
351     qdict_put_str(options, "x-image", filename);
352 }
353 
354 static int blkdebug_parse_perm_list(uint64_t *dest, QDict *options,
355                                     const char *prefix, Error **errp)
356 {
357     int ret = 0;
358     QDict *subqdict = NULL;
359     QObject *crumpled_subqdict = NULL;
360     Visitor *v = NULL;
361     BlockPermissionList *perm_list = NULL, *element;
362 
363     *dest = 0;
364 
365     qdict_extract_subqdict(options, &subqdict, prefix);
366     if (!qdict_size(subqdict)) {
367         goto out;
368     }
369 
370     crumpled_subqdict = qdict_crumple(subqdict, errp);
371     if (!crumpled_subqdict) {
372         ret = -EINVAL;
373         goto out;
374     }
375 
376     v = qobject_input_visitor_new(crumpled_subqdict);
377     if (!visit_type_BlockPermissionList(v, NULL, &perm_list, errp)) {
378         ret = -EINVAL;
379         goto out;
380     }
381 
382     for (element = perm_list; element; element = element->next) {
383         *dest |= bdrv_qapi_perm_to_blk_perm(element->value);
384     }
385 
386 out:
387     qapi_free_BlockPermissionList(perm_list);
388     visit_free(v);
389     qobject_unref(subqdict);
390     qobject_unref(crumpled_subqdict);
391     return ret;
392 }
393 
394 static int blkdebug_parse_perms(BDRVBlkdebugState *s, QDict *options,
395                                 Error **errp)
396 {
397     int ret;
398 
399     ret = blkdebug_parse_perm_list(&s->take_child_perms, options,
400                                    "take-child-perms.", errp);
401     if (ret < 0) {
402         return ret;
403     }
404 
405     ret = blkdebug_parse_perm_list(&s->unshare_child_perms, options,
406                                    "unshare-child-perms.", errp);
407     if (ret < 0) {
408         return ret;
409     }
410 
411     return 0;
412 }
413 
414 static QemuOptsList runtime_opts = {
415     .name = "blkdebug",
416     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
417     .desc = {
418         {
419             .name = "config",
420             .type = QEMU_OPT_STRING,
421             .help = "Path to the configuration file",
422         },
423         {
424             .name = "x-image",
425             .type = QEMU_OPT_STRING,
426             .help = "[internal use only, will be removed]",
427         },
428         {
429             .name = "align",
430             .type = QEMU_OPT_SIZE,
431             .help = "Required alignment in bytes",
432         },
433         {
434             .name = "max-transfer",
435             .type = QEMU_OPT_SIZE,
436             .help = "Maximum transfer size in bytes",
437         },
438         {
439             .name = "opt-write-zero",
440             .type = QEMU_OPT_SIZE,
441             .help = "Optimum write zero alignment in bytes",
442         },
443         {
444             .name = "max-write-zero",
445             .type = QEMU_OPT_SIZE,
446             .help = "Maximum write zero size in bytes",
447         },
448         {
449             .name = "opt-discard",
450             .type = QEMU_OPT_SIZE,
451             .help = "Optimum discard alignment in bytes",
452         },
453         {
454             .name = "max-discard",
455             .type = QEMU_OPT_SIZE,
456             .help = "Maximum discard size in bytes",
457         },
458         { /* end of list */ }
459     },
460 };
461 
462 static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
463                          Error **errp)
464 {
465     BDRVBlkdebugState *s = bs->opaque;
466     QemuOpts *opts;
467     Error *local_err = NULL;
468     int ret;
469     uint64_t align;
470 
471     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
472     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
473         ret = -EINVAL;
474         goto out;
475     }
476 
477     /* Read rules from config file or command line options */
478     s->config_file = g_strdup(qemu_opt_get(opts, "config"));
479     ret = read_config(s, s->config_file, options, errp);
480     if (ret) {
481         goto out;
482     }
483 
484     /* Set initial state */
485     s->state = 1;
486 
487     /* Parse permissions modifiers before opening the image file */
488     ret = blkdebug_parse_perms(s, options, errp);
489     if (ret < 0) {
490         goto out;
491     }
492 
493     /* Open the image file */
494     bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
495                                bs, &child_of_bds,
496                                BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
497                                false, &local_err);
498     if (local_err) {
499         ret = -EINVAL;
500         error_propagate(errp, local_err);
501         goto out;
502     }
503 
504     bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
505         (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
506     bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
507         ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
508             bs->file->bs->supported_zero_flags);
509     ret = -EINVAL;
510 
511     /* Set alignment overrides */
512     s->align = qemu_opt_get_size(opts, "align", 0);
513     if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) {
514         error_setg(errp, "Cannot meet constraints with align %" PRIu64,
515                    s->align);
516         goto out;
517     }
518     align = MAX(s->align, bs->file->bs->bl.request_alignment);
519 
520     s->max_transfer = qemu_opt_get_size(opts, "max-transfer", 0);
521     if (s->max_transfer &&
522         (s->max_transfer >= INT_MAX ||
523          !QEMU_IS_ALIGNED(s->max_transfer, align))) {
524         error_setg(errp, "Cannot meet constraints with max-transfer %" PRIu64,
525                    s->max_transfer);
526         goto out;
527     }
528 
529     s->opt_write_zero = qemu_opt_get_size(opts, "opt-write-zero", 0);
530     if (s->opt_write_zero &&
531         (s->opt_write_zero >= INT_MAX ||
532          !QEMU_IS_ALIGNED(s->opt_write_zero, align))) {
533         error_setg(errp, "Cannot meet constraints with opt-write-zero %" PRIu64,
534                    s->opt_write_zero);
535         goto out;
536     }
537 
538     s->max_write_zero = qemu_opt_get_size(opts, "max-write-zero", 0);
539     if (s->max_write_zero &&
540         (s->max_write_zero >= INT_MAX ||
541          !QEMU_IS_ALIGNED(s->max_write_zero,
542                           MAX(s->opt_write_zero, align)))) {
543         error_setg(errp, "Cannot meet constraints with max-write-zero %" PRIu64,
544                    s->max_write_zero);
545         goto out;
546     }
547 
548     s->opt_discard = qemu_opt_get_size(opts, "opt-discard", 0);
549     if (s->opt_discard &&
550         (s->opt_discard >= INT_MAX ||
551          !QEMU_IS_ALIGNED(s->opt_discard, align))) {
552         error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu64,
553                    s->opt_discard);
554         goto out;
555     }
556 
557     s->max_discard = qemu_opt_get_size(opts, "max-discard", 0);
558     if (s->max_discard &&
559         (s->max_discard >= INT_MAX ||
560          !QEMU_IS_ALIGNED(s->max_discard,
561                           MAX(s->opt_discard, align)))) {
562         error_setg(errp, "Cannot meet constraints with max-discard %" PRIu64,
563                    s->max_discard);
564         goto out;
565     }
566 
567     bdrv_debug_event(bs, BLKDBG_NONE);
568 
569     ret = 0;
570 out:
571     if (ret < 0) {
572         g_free(s->config_file);
573     }
574     qemu_opts_del(opts);
575     return ret;
576 }
577 
578 static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
579                       BlkdebugIOType iotype)
580 {
581     BDRVBlkdebugState *s = bs->opaque;
582     BlkdebugRule *rule = NULL;
583     int error;
584     bool immediately;
585 
586     QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
587         uint64_t inject_offset = rule->options.inject.offset;
588 
589         if ((inject_offset == -1 ||
590              (bytes && inject_offset >= offset &&
591               inject_offset < offset + bytes)) &&
592             (rule->options.inject.iotype_mask & (1ull << iotype)))
593         {
594             break;
595         }
596     }
597 
598     if (!rule || !rule->options.inject.error) {
599         return 0;
600     }
601 
602     immediately = rule->options.inject.immediately;
603     error = rule->options.inject.error;
604 
605     if (rule->options.inject.once) {
606         QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
607         remove_rule(rule);
608     }
609 
610     if (!immediately) {
611         aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
612         qemu_coroutine_yield();
613     }
614 
615     return -error;
616 }
617 
618 static int coroutine_fn
619 blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
620                    QEMUIOVector *qiov, int flags)
621 {
622     int err;
623 
624     /* Sanity check block layer guarantees */
625     assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
626     assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
627     if (bs->bl.max_transfer) {
628         assert(bytes <= bs->bl.max_transfer);
629     }
630 
631     err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_READ);
632     if (err) {
633         return err;
634     }
635 
636     return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
637 }
638 
639 static int coroutine_fn
640 blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
641                     QEMUIOVector *qiov, int flags)
642 {
643     int err;
644 
645     /* Sanity check block layer guarantees */
646     assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
647     assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
648     if (bs->bl.max_transfer) {
649         assert(bytes <= bs->bl.max_transfer);
650     }
651 
652     err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE);
653     if (err) {
654         return err;
655     }
656 
657     return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
658 }
659 
660 static int blkdebug_co_flush(BlockDriverState *bs)
661 {
662     int err = rule_check(bs, 0, 0, BLKDEBUG_IO_TYPE_FLUSH);
663 
664     if (err) {
665         return err;
666     }
667 
668     return bdrv_co_flush(bs->file->bs);
669 }
670 
671 static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs,
672                                                   int64_t offset, int bytes,
673                                                   BdrvRequestFlags flags)
674 {
675     uint32_t align = MAX(bs->bl.request_alignment,
676                          bs->bl.pwrite_zeroes_alignment);
677     int err;
678 
679     /* Only pass through requests that are larger than requested
680      * preferred alignment (so that we test the fallback to writes on
681      * unaligned portions), and check that the block layer never hands
682      * us anything unaligned that crosses an alignment boundary.  */
683     if (bytes < align) {
684         assert(QEMU_IS_ALIGNED(offset, align) ||
685                QEMU_IS_ALIGNED(offset + bytes, align) ||
686                DIV_ROUND_UP(offset, align) ==
687                DIV_ROUND_UP(offset + bytes, align));
688         return -ENOTSUP;
689     }
690     assert(QEMU_IS_ALIGNED(offset, align));
691     assert(QEMU_IS_ALIGNED(bytes, align));
692     if (bs->bl.max_pwrite_zeroes) {
693         assert(bytes <= bs->bl.max_pwrite_zeroes);
694     }
695 
696     err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE_ZEROES);
697     if (err) {
698         return err;
699     }
700 
701     return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
702 }
703 
704 static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,
705                                              int64_t offset, int bytes)
706 {
707     uint32_t align = bs->bl.pdiscard_alignment;
708     int err;
709 
710     /* Only pass through requests that are larger than requested
711      * minimum alignment, and ensure that unaligned requests do not
712      * cross optimum discard boundaries. */
713     if (bytes < bs->bl.request_alignment) {
714         assert(QEMU_IS_ALIGNED(offset, align) ||
715                QEMU_IS_ALIGNED(offset + bytes, align) ||
716                DIV_ROUND_UP(offset, align) ==
717                DIV_ROUND_UP(offset + bytes, align));
718         return -ENOTSUP;
719     }
720     assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
721     assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
722     if (align && bytes >= align) {
723         assert(QEMU_IS_ALIGNED(offset, align));
724         assert(QEMU_IS_ALIGNED(bytes, align));
725     }
726     if (bs->bl.max_pdiscard) {
727         assert(bytes <= bs->bl.max_pdiscard);
728     }
729 
730     err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_DISCARD);
731     if (err) {
732         return err;
733     }
734 
735     return bdrv_co_pdiscard(bs->file, offset, bytes);
736 }
737 
738 static int coroutine_fn blkdebug_co_block_status(BlockDriverState *bs,
739                                                  bool want_zero,
740                                                  int64_t offset,
741                                                  int64_t bytes,
742                                                  int64_t *pnum,
743                                                  int64_t *map,
744                                                  BlockDriverState **file)
745 {
746     int err;
747 
748     assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment));
749 
750     err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_BLOCK_STATUS);
751     if (err) {
752         return err;
753     }
754 
755     return bdrv_co_block_status_from_file(bs, want_zero, offset, bytes,
756                                           pnum, map, file);
757 }
758 
759 static void blkdebug_close(BlockDriverState *bs)
760 {
761     BDRVBlkdebugState *s = bs->opaque;
762     BlkdebugRule *rule, *next;
763     int i;
764 
765     for (i = 0; i < BLKDBG__MAX; i++) {
766         QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
767             remove_rule(rule);
768         }
769     }
770 
771     g_free(s->config_file);
772 }
773 
774 static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
775 {
776     BDRVBlkdebugState *s = bs->opaque;
777     BlkdebugSuspendedReq r;
778 
779     r = (BlkdebugSuspendedReq) {
780         .co         = qemu_coroutine_self(),
781         .tag        = g_strdup(rule->options.suspend.tag),
782     };
783 
784     remove_rule(rule);
785     QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
786 
787     if (!qtest_enabled()) {
788         printf("blkdebug: Suspended request '%s'\n", r.tag);
789     }
790     qemu_coroutine_yield();
791     if (!qtest_enabled()) {
792         printf("blkdebug: Resuming request '%s'\n", r.tag);
793     }
794 
795     QLIST_REMOVE(&r, next);
796     g_free(r.tag);
797 }
798 
799 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
800     bool injected)
801 {
802     BDRVBlkdebugState *s = bs->opaque;
803 
804     /* Only process rules for the current state */
805     if (rule->state && rule->state != s->state) {
806         return injected;
807     }
808 
809     /* Take the action */
810     switch (rule->action) {
811     case ACTION_INJECT_ERROR:
812         if (!injected) {
813             QSIMPLEQ_INIT(&s->active_rules);
814             injected = true;
815         }
816         QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
817         break;
818 
819     case ACTION_SET_STATE:
820         s->new_state = rule->options.set_state.new_state;
821         break;
822 
823     case ACTION_SUSPEND:
824         suspend_request(bs, rule);
825         break;
826     }
827     return injected;
828 }
829 
830 static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
831 {
832     BDRVBlkdebugState *s = bs->opaque;
833     struct BlkdebugRule *rule, *next;
834     bool injected;
835 
836     assert((int)event >= 0 && event < BLKDBG__MAX);
837 
838     injected = false;
839     s->new_state = s->state;
840     QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
841         injected = process_rule(bs, rule, injected);
842     }
843     s->state = s->new_state;
844 }
845 
846 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
847                                      const char *tag)
848 {
849     BDRVBlkdebugState *s = bs->opaque;
850     struct BlkdebugRule *rule;
851     int blkdebug_event;
852 
853     blkdebug_event = qapi_enum_parse(&BlkdebugEvent_lookup, event, -1, NULL);
854     if (blkdebug_event < 0) {
855         return -ENOENT;
856     }
857 
858     rule = g_malloc(sizeof(*rule));
859     *rule = (struct BlkdebugRule) {
860         .event  = blkdebug_event,
861         .action = ACTION_SUSPEND,
862         .state  = 0,
863         .options.suspend.tag = g_strdup(tag),
864     };
865 
866     QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
867 
868     return 0;
869 }
870 
871 static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
872 {
873     BDRVBlkdebugState *s = bs->opaque;
874     BlkdebugSuspendedReq *r, *next;
875 
876     QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
877         if (!strcmp(r->tag, tag)) {
878             qemu_coroutine_enter(r->co);
879             return 0;
880         }
881     }
882     return -ENOENT;
883 }
884 
885 static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
886                                             const char *tag)
887 {
888     BDRVBlkdebugState *s = bs->opaque;
889     BlkdebugSuspendedReq *r, *r_next;
890     BlkdebugRule *rule, *next;
891     int i, ret = -ENOENT;
892 
893     for (i = 0; i < BLKDBG__MAX; i++) {
894         QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
895             if (rule->action == ACTION_SUSPEND &&
896                 !strcmp(rule->options.suspend.tag, tag)) {
897                 remove_rule(rule);
898                 ret = 0;
899             }
900         }
901     }
902     QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
903         if (!strcmp(r->tag, tag)) {
904             qemu_coroutine_enter(r->co);
905             ret = 0;
906         }
907     }
908     return ret;
909 }
910 
911 static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
912 {
913     BDRVBlkdebugState *s = bs->opaque;
914     BlkdebugSuspendedReq *r;
915 
916     QLIST_FOREACH(r, &s->suspended_reqs, next) {
917         if (!strcmp(r->tag, tag)) {
918             return true;
919         }
920     }
921     return false;
922 }
923 
924 static int64_t blkdebug_getlength(BlockDriverState *bs)
925 {
926     return bdrv_getlength(bs->file->bs);
927 }
928 
929 static void blkdebug_refresh_filename(BlockDriverState *bs)
930 {
931     BDRVBlkdebugState *s = bs->opaque;
932     const QDictEntry *e;
933     int ret;
934 
935     if (!bs->file->bs->exact_filename[0]) {
936         return;
937     }
938 
939     for (e = qdict_first(bs->full_open_options); e;
940          e = qdict_next(bs->full_open_options, e))
941     {
942         /* Real child options are under "image", but "x-image" may
943          * contain a filename */
944         if (strcmp(qdict_entry_key(e), "config") &&
945             strcmp(qdict_entry_key(e), "image") &&
946             strcmp(qdict_entry_key(e), "x-image") &&
947             strcmp(qdict_entry_key(e), "driver"))
948         {
949             return;
950         }
951     }
952 
953     ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
954                    "blkdebug:%s:%s",
955                    s->config_file ?: "", bs->file->bs->exact_filename);
956     if (ret >= sizeof(bs->exact_filename)) {
957         /* An overflow makes the filename unusable, so do not report any */
958         bs->exact_filename[0] = 0;
959     }
960 }
961 
962 static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp)
963 {
964     BDRVBlkdebugState *s = bs->opaque;
965 
966     if (s->align) {
967         bs->bl.request_alignment = s->align;
968     }
969     if (s->max_transfer) {
970         bs->bl.max_transfer = s->max_transfer;
971     }
972     if (s->opt_write_zero) {
973         bs->bl.pwrite_zeroes_alignment = s->opt_write_zero;
974     }
975     if (s->max_write_zero) {
976         bs->bl.max_pwrite_zeroes = s->max_write_zero;
977     }
978     if (s->opt_discard) {
979         bs->bl.pdiscard_alignment = s->opt_discard;
980     }
981     if (s->max_discard) {
982         bs->bl.max_pdiscard = s->max_discard;
983     }
984 }
985 
986 static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
987                                    BlockReopenQueue *queue, Error **errp)
988 {
989     return 0;
990 }
991 
992 static void blkdebug_child_perm(BlockDriverState *bs, BdrvChild *c,
993                                 BdrvChildRole role,
994                                 BlockReopenQueue *reopen_queue,
995                                 uint64_t perm, uint64_t shared,
996                                 uint64_t *nperm, uint64_t *nshared)
997 {
998     BDRVBlkdebugState *s = bs->opaque;
999 
1000     bdrv_default_perms(bs, c, role, reopen_queue,
1001                        perm, shared, nperm, nshared);
1002 
1003     *nperm |= s->take_child_perms;
1004     *nshared &= ~s->unshare_child_perms;
1005 }
1006 
1007 static const char *const blkdebug_strong_runtime_opts[] = {
1008     "config",
1009     "inject-error.",
1010     "set-state.",
1011     "align",
1012     "max-transfer",
1013     "opt-write-zero",
1014     "max-write-zero",
1015     "opt-discard",
1016     "max-discard",
1017 
1018     NULL
1019 };
1020 
1021 static BlockDriver bdrv_blkdebug = {
1022     .format_name            = "blkdebug",
1023     .protocol_name          = "blkdebug",
1024     .instance_size          = sizeof(BDRVBlkdebugState),
1025     .is_filter              = true,
1026 
1027     .bdrv_parse_filename    = blkdebug_parse_filename,
1028     .bdrv_file_open         = blkdebug_open,
1029     .bdrv_close             = blkdebug_close,
1030     .bdrv_reopen_prepare    = blkdebug_reopen_prepare,
1031     .bdrv_child_perm        = blkdebug_child_perm,
1032 
1033     .bdrv_getlength         = blkdebug_getlength,
1034     .bdrv_refresh_filename  = blkdebug_refresh_filename,
1035     .bdrv_refresh_limits    = blkdebug_refresh_limits,
1036 
1037     .bdrv_co_preadv         = blkdebug_co_preadv,
1038     .bdrv_co_pwritev        = blkdebug_co_pwritev,
1039     .bdrv_co_flush_to_disk  = blkdebug_co_flush,
1040     .bdrv_co_pwrite_zeroes  = blkdebug_co_pwrite_zeroes,
1041     .bdrv_co_pdiscard       = blkdebug_co_pdiscard,
1042     .bdrv_co_block_status   = blkdebug_co_block_status,
1043 
1044     .bdrv_debug_event           = blkdebug_debug_event,
1045     .bdrv_debug_breakpoint      = blkdebug_debug_breakpoint,
1046     .bdrv_debug_remove_breakpoint
1047                                 = blkdebug_debug_remove_breakpoint,
1048     .bdrv_debug_resume          = blkdebug_debug_resume,
1049     .bdrv_debug_is_suspended    = blkdebug_debug_is_suspended,
1050 
1051     .strong_runtime_opts        = blkdebug_strong_runtime_opts,
1052 };
1053 
1054 static void bdrv_blkdebug_init(void)
1055 {
1056     bdrv_register(&bdrv_blkdebug);
1057 }
1058 
1059 block_init(bdrv_blkdebug_init);
1060