blkdebug: Add "none" event
[qemu/ar7.git] / block / blkdebug.c
blob1663ed25af16d54950cc0050455acdcd013f6ff6
1 /*
2 * Block protocol for I/O error injection
4 * Copyright (C) 2016-2017 Red Hat, Inc.
5 * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
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 "qemu/module.h"
32 #include "qemu/option.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qstring.h"
35 #include "sysemu/qtest.h"
37 typedef struct BDRVBlkdebugState {
38 int state;
39 int new_state;
40 uint64_t align;
41 uint64_t max_transfer;
42 uint64_t opt_write_zero;
43 uint64_t max_write_zero;
44 uint64_t opt_discard;
45 uint64_t max_discard;
47 /* For blkdebug_refresh_filename() */
48 char *config_file;
50 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
51 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
52 QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
53 } BDRVBlkdebugState;
55 typedef struct BlkdebugAIOCB {
56 BlockAIOCB common;
57 int ret;
58 } BlkdebugAIOCB;
60 typedef struct BlkdebugSuspendedReq {
61 Coroutine *co;
62 char *tag;
63 QLIST_ENTRY(BlkdebugSuspendedReq) next;
64 } BlkdebugSuspendedReq;
66 enum {
67 ACTION_INJECT_ERROR,
68 ACTION_SET_STATE,
69 ACTION_SUSPEND,
72 typedef struct BlkdebugRule {
73 BlkdebugEvent event;
74 int action;
75 int state;
76 union {
77 struct {
78 uint64_t iotype_mask;
79 int error;
80 int immediately;
81 int once;
82 int64_t offset;
83 } inject;
84 struct {
85 int new_state;
86 } set_state;
87 struct {
88 char *tag;
89 } suspend;
90 } options;
91 QLIST_ENTRY(BlkdebugRule) next;
92 QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
93 } BlkdebugRule;
95 QEMU_BUILD_BUG_MSG(BLKDEBUG_IO_TYPE__MAX > 64,
96 "BlkdebugIOType mask does not fit into an uint64_t");
98 static QemuOptsList inject_error_opts = {
99 .name = "inject-error",
100 .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
101 .desc = {
103 .name = "event",
104 .type = QEMU_OPT_STRING,
107 .name = "state",
108 .type = QEMU_OPT_NUMBER,
111 .name = "iotype",
112 .type = QEMU_OPT_STRING,
115 .name = "errno",
116 .type = QEMU_OPT_NUMBER,
119 .name = "sector",
120 .type = QEMU_OPT_NUMBER,
123 .name = "once",
124 .type = QEMU_OPT_BOOL,
127 .name = "immediately",
128 .type = QEMU_OPT_BOOL,
130 { /* end of list */ }
134 static QemuOptsList set_state_opts = {
135 .name = "set-state",
136 .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
137 .desc = {
139 .name = "event",
140 .type = QEMU_OPT_STRING,
143 .name = "state",
144 .type = QEMU_OPT_NUMBER,
147 .name = "new_state",
148 .type = QEMU_OPT_NUMBER,
150 { /* end of list */ }
154 static QemuOptsList *config_groups[] = {
155 &inject_error_opts,
156 &set_state_opts,
157 NULL
160 struct add_rule_data {
161 BDRVBlkdebugState *s;
162 int action;
165 static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
167 struct add_rule_data *d = opaque;
168 BDRVBlkdebugState *s = d->s;
169 const char* event_name;
170 int event;
171 struct BlkdebugRule *rule;
172 int64_t sector;
173 BlkdebugIOType iotype;
174 Error *local_error = NULL;
176 /* Find the right event for the rule */
177 event_name = qemu_opt_get(opts, "event");
178 if (!event_name) {
179 error_setg(errp, "Missing event name for rule");
180 return -1;
182 event = qapi_enum_parse(&BlkdebugEvent_lookup, event_name, -1, errp);
183 if (event < 0) {
184 return -1;
187 /* Set attributes common for all actions */
188 rule = g_malloc0(sizeof(*rule));
189 *rule = (struct BlkdebugRule) {
190 .event = event,
191 .action = d->action,
192 .state = qemu_opt_get_number(opts, "state", 0),
195 /* Parse action-specific options */
196 switch (d->action) {
197 case ACTION_INJECT_ERROR:
198 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
199 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
200 rule->options.inject.immediately =
201 qemu_opt_get_bool(opts, "immediately", 0);
202 sector = qemu_opt_get_number(opts, "sector", -1);
203 rule->options.inject.offset =
204 sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE;
206 iotype = qapi_enum_parse(&BlkdebugIOType_lookup,
207 qemu_opt_get(opts, "iotype"),
208 BLKDEBUG_IO_TYPE__MAX, &local_error);
209 if (local_error) {
210 error_propagate(errp, local_error);
211 return -1;
213 if (iotype != BLKDEBUG_IO_TYPE__MAX) {
214 rule->options.inject.iotype_mask = (1ull << iotype);
215 } else {
216 /* Apply the default */
217 rule->options.inject.iotype_mask =
218 (1ull << BLKDEBUG_IO_TYPE_READ)
219 | (1ull << BLKDEBUG_IO_TYPE_WRITE)
220 | (1ull << BLKDEBUG_IO_TYPE_WRITE_ZEROES)
221 | (1ull << BLKDEBUG_IO_TYPE_DISCARD)
222 | (1ull << BLKDEBUG_IO_TYPE_FLUSH);
225 break;
227 case ACTION_SET_STATE:
228 rule->options.set_state.new_state =
229 qemu_opt_get_number(opts, "new_state", 0);
230 break;
232 case ACTION_SUSPEND:
233 rule->options.suspend.tag =
234 g_strdup(qemu_opt_get(opts, "tag"));
235 break;
238 /* Add the rule */
239 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
241 return 0;
244 static void remove_rule(BlkdebugRule *rule)
246 switch (rule->action) {
247 case ACTION_INJECT_ERROR:
248 case ACTION_SET_STATE:
249 break;
250 case ACTION_SUSPEND:
251 g_free(rule->options.suspend.tag);
252 break;
255 QLIST_REMOVE(rule, next);
256 g_free(rule);
259 static int read_config(BDRVBlkdebugState *s, const char *filename,
260 QDict *options, Error **errp)
262 FILE *f = NULL;
263 int ret;
264 struct add_rule_data d;
265 Error *local_err = NULL;
267 if (filename) {
268 f = fopen(filename, "r");
269 if (f == NULL) {
270 error_setg_errno(errp, errno, "Could not read blkdebug config file");
271 return -errno;
274 ret = qemu_config_parse(f, config_groups, filename);
275 if (ret < 0) {
276 error_setg(errp, "Could not parse blkdebug config file");
277 goto fail;
281 qemu_config_parse_qdict(options, config_groups, &local_err);
282 if (local_err) {
283 error_propagate(errp, local_err);
284 ret = -EINVAL;
285 goto fail;
288 d.s = s;
289 d.action = ACTION_INJECT_ERROR;
290 qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
291 if (local_err) {
292 error_propagate(errp, local_err);
293 ret = -EINVAL;
294 goto fail;
297 d.action = ACTION_SET_STATE;
298 qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
299 if (local_err) {
300 error_propagate(errp, local_err);
301 ret = -EINVAL;
302 goto fail;
305 ret = 0;
306 fail:
307 qemu_opts_reset(&inject_error_opts);
308 qemu_opts_reset(&set_state_opts);
309 if (f) {
310 fclose(f);
312 return ret;
315 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
316 static void blkdebug_parse_filename(const char *filename, QDict *options,
317 Error **errp)
319 const char *c;
321 /* Parse the blkdebug: prefix */
322 if (!strstart(filename, "blkdebug:", &filename)) {
323 /* There was no prefix; therefore, all options have to be already
324 present in the QDict (except for the filename) */
325 qdict_put_str(options, "x-image", filename);
326 return;
329 /* Parse config file path */
330 c = strchr(filename, ':');
331 if (c == NULL) {
332 error_setg(errp, "blkdebug requires both config file and image path");
333 return;
336 if (c != filename) {
337 QString *config_path;
338 config_path = qstring_from_substr(filename, 0, c - filename);
339 qdict_put(options, "config", config_path);
342 /* TODO Allow multi-level nesting and set file.filename here */
343 filename = c + 1;
344 qdict_put_str(options, "x-image", filename);
347 static QemuOptsList runtime_opts = {
348 .name = "blkdebug",
349 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
350 .desc = {
352 .name = "config",
353 .type = QEMU_OPT_STRING,
354 .help = "Path to the configuration file",
357 .name = "x-image",
358 .type = QEMU_OPT_STRING,
359 .help = "[internal use only, will be removed]",
362 .name = "align",
363 .type = QEMU_OPT_SIZE,
364 .help = "Required alignment in bytes",
367 .name = "max-transfer",
368 .type = QEMU_OPT_SIZE,
369 .help = "Maximum transfer size in bytes",
372 .name = "opt-write-zero",
373 .type = QEMU_OPT_SIZE,
374 .help = "Optimum write zero alignment in bytes",
377 .name = "max-write-zero",
378 .type = QEMU_OPT_SIZE,
379 .help = "Maximum write zero size in bytes",
382 .name = "opt-discard",
383 .type = QEMU_OPT_SIZE,
384 .help = "Optimum discard alignment in bytes",
387 .name = "max-discard",
388 .type = QEMU_OPT_SIZE,
389 .help = "Maximum discard size in bytes",
391 { /* end of list */ }
395 static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
396 Error **errp)
398 BDRVBlkdebugState *s = bs->opaque;
399 QemuOpts *opts;
400 Error *local_err = NULL;
401 int ret;
402 uint64_t align;
404 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
405 qemu_opts_absorb_qdict(opts, options, &local_err);
406 if (local_err) {
407 error_propagate(errp, local_err);
408 ret = -EINVAL;
409 goto out;
412 /* Read rules from config file or command line options */
413 s->config_file = g_strdup(qemu_opt_get(opts, "config"));
414 ret = read_config(s, s->config_file, options, errp);
415 if (ret) {
416 goto out;
419 /* Set initial state */
420 s->state = 1;
422 /* Open the image file */
423 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
424 bs, &child_file, false, &local_err);
425 if (local_err) {
426 ret = -EINVAL;
427 error_propagate(errp, local_err);
428 goto out;
431 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
432 (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
433 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
434 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
435 bs->file->bs->supported_zero_flags);
436 ret = -EINVAL;
438 /* Set alignment overrides */
439 s->align = qemu_opt_get_size(opts, "align", 0);
440 if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) {
441 error_setg(errp, "Cannot meet constraints with align %" PRIu64,
442 s->align);
443 goto out;
445 align = MAX(s->align, bs->file->bs->bl.request_alignment);
447 s->max_transfer = qemu_opt_get_size(opts, "max-transfer", 0);
448 if (s->max_transfer &&
449 (s->max_transfer >= INT_MAX ||
450 !QEMU_IS_ALIGNED(s->max_transfer, align))) {
451 error_setg(errp, "Cannot meet constraints with max-transfer %" PRIu64,
452 s->max_transfer);
453 goto out;
456 s->opt_write_zero = qemu_opt_get_size(opts, "opt-write-zero", 0);
457 if (s->opt_write_zero &&
458 (s->opt_write_zero >= INT_MAX ||
459 !QEMU_IS_ALIGNED(s->opt_write_zero, align))) {
460 error_setg(errp, "Cannot meet constraints with opt-write-zero %" PRIu64,
461 s->opt_write_zero);
462 goto out;
465 s->max_write_zero = qemu_opt_get_size(opts, "max-write-zero", 0);
466 if (s->max_write_zero &&
467 (s->max_write_zero >= INT_MAX ||
468 !QEMU_IS_ALIGNED(s->max_write_zero,
469 MAX(s->opt_write_zero, align)))) {
470 error_setg(errp, "Cannot meet constraints with max-write-zero %" PRIu64,
471 s->max_write_zero);
472 goto out;
475 s->opt_discard = qemu_opt_get_size(opts, "opt-discard", 0);
476 if (s->opt_discard &&
477 (s->opt_discard >= INT_MAX ||
478 !QEMU_IS_ALIGNED(s->opt_discard, align))) {
479 error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu64,
480 s->opt_discard);
481 goto out;
484 s->max_discard = qemu_opt_get_size(opts, "max-discard", 0);
485 if (s->max_discard &&
486 (s->max_discard >= INT_MAX ||
487 !QEMU_IS_ALIGNED(s->max_discard,
488 MAX(s->opt_discard, align)))) {
489 error_setg(errp, "Cannot meet constraints with max-discard %" PRIu64,
490 s->max_discard);
491 goto out;
494 bdrv_debug_event(bs, BLKDBG_NONE);
496 ret = 0;
497 out:
498 if (ret < 0) {
499 g_free(s->config_file);
501 qemu_opts_del(opts);
502 return ret;
505 static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
506 BlkdebugIOType iotype)
508 BDRVBlkdebugState *s = bs->opaque;
509 BlkdebugRule *rule = NULL;
510 int error;
511 bool immediately;
513 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
514 uint64_t inject_offset = rule->options.inject.offset;
516 if ((inject_offset == -1 ||
517 (bytes && inject_offset >= offset &&
518 inject_offset < offset + bytes)) &&
519 (rule->options.inject.iotype_mask & (1ull << iotype)))
521 break;
525 if (!rule || !rule->options.inject.error) {
526 return 0;
529 immediately = rule->options.inject.immediately;
530 error = rule->options.inject.error;
532 if (rule->options.inject.once) {
533 QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
534 remove_rule(rule);
537 if (!immediately) {
538 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
539 qemu_coroutine_yield();
542 return -error;
545 static int coroutine_fn
546 blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
547 QEMUIOVector *qiov, int flags)
549 int err;
551 /* Sanity check block layer guarantees */
552 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
553 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
554 if (bs->bl.max_transfer) {
555 assert(bytes <= bs->bl.max_transfer);
558 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_READ);
559 if (err) {
560 return err;
563 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
566 static int coroutine_fn
567 blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
568 QEMUIOVector *qiov, int flags)
570 int err;
572 /* Sanity check block layer guarantees */
573 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
574 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
575 if (bs->bl.max_transfer) {
576 assert(bytes <= bs->bl.max_transfer);
579 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE);
580 if (err) {
581 return err;
584 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
587 static int blkdebug_co_flush(BlockDriverState *bs)
589 int err = rule_check(bs, 0, 0, BLKDEBUG_IO_TYPE_FLUSH);
591 if (err) {
592 return err;
595 return bdrv_co_flush(bs->file->bs);
598 static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs,
599 int64_t offset, int bytes,
600 BdrvRequestFlags flags)
602 uint32_t align = MAX(bs->bl.request_alignment,
603 bs->bl.pwrite_zeroes_alignment);
604 int err;
606 /* Only pass through requests that are larger than requested
607 * preferred alignment (so that we test the fallback to writes on
608 * unaligned portions), and check that the block layer never hands
609 * us anything unaligned that crosses an alignment boundary. */
610 if (bytes < align) {
611 assert(QEMU_IS_ALIGNED(offset, align) ||
612 QEMU_IS_ALIGNED(offset + bytes, align) ||
613 DIV_ROUND_UP(offset, align) ==
614 DIV_ROUND_UP(offset + bytes, align));
615 return -ENOTSUP;
617 assert(QEMU_IS_ALIGNED(offset, align));
618 assert(QEMU_IS_ALIGNED(bytes, align));
619 if (bs->bl.max_pwrite_zeroes) {
620 assert(bytes <= bs->bl.max_pwrite_zeroes);
623 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE_ZEROES);
624 if (err) {
625 return err;
628 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
631 static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,
632 int64_t offset, int bytes)
634 uint32_t align = bs->bl.pdiscard_alignment;
635 int err;
637 /* Only pass through requests that are larger than requested
638 * minimum alignment, and ensure that unaligned requests do not
639 * cross optimum discard boundaries. */
640 if (bytes < bs->bl.request_alignment) {
641 assert(QEMU_IS_ALIGNED(offset, align) ||
642 QEMU_IS_ALIGNED(offset + bytes, align) ||
643 DIV_ROUND_UP(offset, align) ==
644 DIV_ROUND_UP(offset + bytes, align));
645 return -ENOTSUP;
647 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
648 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
649 if (align && bytes >= align) {
650 assert(QEMU_IS_ALIGNED(offset, align));
651 assert(QEMU_IS_ALIGNED(bytes, align));
653 if (bs->bl.max_pdiscard) {
654 assert(bytes <= bs->bl.max_pdiscard);
657 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_DISCARD);
658 if (err) {
659 return err;
662 return bdrv_co_pdiscard(bs->file, offset, bytes);
665 static int coroutine_fn blkdebug_co_block_status(BlockDriverState *bs,
666 bool want_zero,
667 int64_t offset,
668 int64_t bytes,
669 int64_t *pnum,
670 int64_t *map,
671 BlockDriverState **file)
673 assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment));
674 return bdrv_co_block_status_from_file(bs, want_zero, offset, bytes,
675 pnum, map, file);
678 static void blkdebug_close(BlockDriverState *bs)
680 BDRVBlkdebugState *s = bs->opaque;
681 BlkdebugRule *rule, *next;
682 int i;
684 for (i = 0; i < BLKDBG__MAX; i++) {
685 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
686 remove_rule(rule);
690 g_free(s->config_file);
693 static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
695 BDRVBlkdebugState *s = bs->opaque;
696 BlkdebugSuspendedReq r;
698 r = (BlkdebugSuspendedReq) {
699 .co = qemu_coroutine_self(),
700 .tag = g_strdup(rule->options.suspend.tag),
703 remove_rule(rule);
704 QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
706 if (!qtest_enabled()) {
707 printf("blkdebug: Suspended request '%s'\n", r.tag);
709 qemu_coroutine_yield();
710 if (!qtest_enabled()) {
711 printf("blkdebug: Resuming request '%s'\n", r.tag);
714 QLIST_REMOVE(&r, next);
715 g_free(r.tag);
718 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
719 bool injected)
721 BDRVBlkdebugState *s = bs->opaque;
723 /* Only process rules for the current state */
724 if (rule->state && rule->state != s->state) {
725 return injected;
728 /* Take the action */
729 switch (rule->action) {
730 case ACTION_INJECT_ERROR:
731 if (!injected) {
732 QSIMPLEQ_INIT(&s->active_rules);
733 injected = true;
735 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
736 break;
738 case ACTION_SET_STATE:
739 s->new_state = rule->options.set_state.new_state;
740 break;
742 case ACTION_SUSPEND:
743 suspend_request(bs, rule);
744 break;
746 return injected;
749 static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
751 BDRVBlkdebugState *s = bs->opaque;
752 struct BlkdebugRule *rule, *next;
753 bool injected;
755 assert((int)event >= 0 && event < BLKDBG__MAX);
757 injected = false;
758 s->new_state = s->state;
759 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
760 injected = process_rule(bs, rule, injected);
762 s->state = s->new_state;
765 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
766 const char *tag)
768 BDRVBlkdebugState *s = bs->opaque;
769 struct BlkdebugRule *rule;
770 int blkdebug_event;
772 blkdebug_event = qapi_enum_parse(&BlkdebugEvent_lookup, event, -1, NULL);
773 if (blkdebug_event < 0) {
774 return -ENOENT;
777 rule = g_malloc(sizeof(*rule));
778 *rule = (struct BlkdebugRule) {
779 .event = blkdebug_event,
780 .action = ACTION_SUSPEND,
781 .state = 0,
782 .options.suspend.tag = g_strdup(tag),
785 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
787 return 0;
790 static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
792 BDRVBlkdebugState *s = bs->opaque;
793 BlkdebugSuspendedReq *r, *next;
795 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
796 if (!strcmp(r->tag, tag)) {
797 qemu_coroutine_enter(r->co);
798 return 0;
801 return -ENOENT;
804 static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
805 const char *tag)
807 BDRVBlkdebugState *s = bs->opaque;
808 BlkdebugSuspendedReq *r, *r_next;
809 BlkdebugRule *rule, *next;
810 int i, ret = -ENOENT;
812 for (i = 0; i < BLKDBG__MAX; i++) {
813 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
814 if (rule->action == ACTION_SUSPEND &&
815 !strcmp(rule->options.suspend.tag, tag)) {
816 remove_rule(rule);
817 ret = 0;
821 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
822 if (!strcmp(r->tag, tag)) {
823 qemu_coroutine_enter(r->co);
824 ret = 0;
827 return ret;
830 static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
832 BDRVBlkdebugState *s = bs->opaque;
833 BlkdebugSuspendedReq *r;
835 QLIST_FOREACH(r, &s->suspended_reqs, next) {
836 if (!strcmp(r->tag, tag)) {
837 return true;
840 return false;
843 static int64_t blkdebug_getlength(BlockDriverState *bs)
845 return bdrv_getlength(bs->file->bs);
848 static void blkdebug_refresh_filename(BlockDriverState *bs)
850 BDRVBlkdebugState *s = bs->opaque;
851 const QDictEntry *e;
852 int ret;
854 if (!bs->file->bs->exact_filename[0]) {
855 return;
858 for (e = qdict_first(bs->full_open_options); e;
859 e = qdict_next(bs->full_open_options, e))
861 /* Real child options are under "image", but "x-image" may
862 * contain a filename */
863 if (strcmp(qdict_entry_key(e), "config") &&
864 strcmp(qdict_entry_key(e), "image") &&
865 strcmp(qdict_entry_key(e), "x-image") &&
866 strcmp(qdict_entry_key(e), "driver"))
868 return;
872 ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
873 "blkdebug:%s:%s",
874 s->config_file ?: "", bs->file->bs->exact_filename);
875 if (ret >= sizeof(bs->exact_filename)) {
876 /* An overflow makes the filename unusable, so do not report any */
877 bs->exact_filename[0] = 0;
881 static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp)
883 BDRVBlkdebugState *s = bs->opaque;
885 if (s->align) {
886 bs->bl.request_alignment = s->align;
888 if (s->max_transfer) {
889 bs->bl.max_transfer = s->max_transfer;
891 if (s->opt_write_zero) {
892 bs->bl.pwrite_zeroes_alignment = s->opt_write_zero;
894 if (s->max_write_zero) {
895 bs->bl.max_pwrite_zeroes = s->max_write_zero;
897 if (s->opt_discard) {
898 bs->bl.pdiscard_alignment = s->opt_discard;
900 if (s->max_discard) {
901 bs->bl.max_pdiscard = s->max_discard;
905 static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
906 BlockReopenQueue *queue, Error **errp)
908 return 0;
911 static const char *const blkdebug_strong_runtime_opts[] = {
912 "config",
913 "inject-error.",
914 "set-state.",
915 "align",
916 "max-transfer",
917 "opt-write-zero",
918 "max-write-zero",
919 "opt-discard",
920 "max-discard",
922 NULL
925 static BlockDriver bdrv_blkdebug = {
926 .format_name = "blkdebug",
927 .protocol_name = "blkdebug",
928 .instance_size = sizeof(BDRVBlkdebugState),
929 .is_filter = true,
931 .bdrv_parse_filename = blkdebug_parse_filename,
932 .bdrv_file_open = blkdebug_open,
933 .bdrv_close = blkdebug_close,
934 .bdrv_reopen_prepare = blkdebug_reopen_prepare,
935 .bdrv_child_perm = bdrv_filter_default_perms,
937 .bdrv_getlength = blkdebug_getlength,
938 .bdrv_refresh_filename = blkdebug_refresh_filename,
939 .bdrv_refresh_limits = blkdebug_refresh_limits,
941 .bdrv_co_preadv = blkdebug_co_preadv,
942 .bdrv_co_pwritev = blkdebug_co_pwritev,
943 .bdrv_co_flush_to_disk = blkdebug_co_flush,
944 .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes,
945 .bdrv_co_pdiscard = blkdebug_co_pdiscard,
946 .bdrv_co_block_status = blkdebug_co_block_status,
948 .bdrv_debug_event = blkdebug_debug_event,
949 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
950 .bdrv_debug_remove_breakpoint
951 = blkdebug_debug_remove_breakpoint,
952 .bdrv_debug_resume = blkdebug_debug_resume,
953 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
955 .strong_runtime_opts = blkdebug_strong_runtime_opts,
958 static void bdrv_blkdebug_init(void)
960 bdrv_register(&bdrv_blkdebug);
963 block_init(bdrv_blkdebug_init);