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
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-io.h"
31 #include "block/block_int.h"
32 #include "block/qdict.h"
33 #include "qemu/module.h"
34 #include "qemu/option.h"
35 #include "qapi/qapi-visit-block-core.h"
36 #include "qapi/qmp/qdict.h"
37 #include "qapi/qmp/qlist.h"
38 #include "qapi/qmp/qstring.h"
39 #include "qapi/qobject-input-visitor.h"
40 #include "sysemu/qtest.h"
42 /* All APIs are thread-safe */
44 typedef struct BDRVBlkdebugState
{
45 /* IN: initialized in blkdebug_open() and never changed */
47 uint64_t max_transfer
;
48 uint64_t opt_write_zero
;
49 uint64_t max_write_zero
;
52 char *config_file
; /* For blkdebug_refresh_filename() */
53 /* initialized in blkdebug_parse_perms() */
54 uint64_t take_child_perms
;
55 uint64_t unshare_child_perms
;
57 /* State. Protected by lock */
59 QLIST_HEAD(, BlkdebugRule
) rules
[BLKDBG__MAX
];
60 QSIMPLEQ_HEAD(, BlkdebugRule
) active_rules
;
61 QLIST_HEAD(, BlkdebugSuspendedReq
) suspended_reqs
;
65 typedef struct BlkdebugAIOCB
{
70 typedef struct BlkdebugSuspendedReq
{
71 /* IN: initialized in suspend_request() */
75 /* List entry protected BDRVBlkdebugState's lock */
76 QLIST_ENTRY(BlkdebugSuspendedReq
) next
;
77 } BlkdebugSuspendedReq
;
86 typedef struct BlkdebugRule
{
87 /* IN: initialized in add_rule() or blkdebug_debug_breakpoint() */
107 /* List entries protected BDRVBlkdebugState's lock */
108 QLIST_ENTRY(BlkdebugRule
) next
;
109 QSIMPLEQ_ENTRY(BlkdebugRule
) active_next
;
112 QEMU_BUILD_BUG_MSG(BLKDEBUG_IO_TYPE__MAX
> 64,
113 "BlkdebugIOType mask does not fit into an uint64_t");
115 static QemuOptsList inject_error_opts
= {
116 .name
= "inject-error",
117 .head
= QTAILQ_HEAD_INITIALIZER(inject_error_opts
.head
),
121 .type
= QEMU_OPT_STRING
,
125 .type
= QEMU_OPT_NUMBER
,
129 .type
= QEMU_OPT_STRING
,
133 .type
= QEMU_OPT_NUMBER
,
137 .type
= QEMU_OPT_NUMBER
,
141 .type
= QEMU_OPT_BOOL
,
144 .name
= "immediately",
145 .type
= QEMU_OPT_BOOL
,
147 { /* end of list */ }
151 static QemuOptsList set_state_opts
= {
153 .head
= QTAILQ_HEAD_INITIALIZER(set_state_opts
.head
),
157 .type
= QEMU_OPT_STRING
,
161 .type
= QEMU_OPT_NUMBER
,
165 .type
= QEMU_OPT_NUMBER
,
167 { /* end of list */ }
171 static QemuOptsList
*config_groups
[] = {
177 struct add_rule_data
{
178 BDRVBlkdebugState
*s
;
182 static int add_rule(void *opaque
, QemuOpts
*opts
, Error
**errp
)
184 struct add_rule_data
*d
= opaque
;
185 BDRVBlkdebugState
*s
= d
->s
;
186 const char *event_name
;
188 struct BlkdebugRule
*rule
;
190 BlkdebugIOType iotype
;
191 Error
*local_error
= NULL
;
193 /* Find the right event for the rule */
194 event_name
= qemu_opt_get(opts
, "event");
196 error_setg(errp
, "Missing event name for rule");
199 event
= qapi_enum_parse(&BlkdebugEvent_lookup
, event_name
, -1, errp
);
204 /* Set attributes common for all actions */
205 rule
= g_malloc0(sizeof(*rule
));
206 *rule
= (struct BlkdebugRule
) {
209 .state
= qemu_opt_get_number(opts
, "state", 0),
212 /* Parse action-specific options */
214 case ACTION_INJECT_ERROR
:
215 rule
->options
.inject
.error
= qemu_opt_get_number(opts
, "errno", EIO
);
216 rule
->options
.inject
.once
= qemu_opt_get_bool(opts
, "once", 0);
217 rule
->options
.inject
.immediately
=
218 qemu_opt_get_bool(opts
, "immediately", 0);
219 sector
= qemu_opt_get_number(opts
, "sector", -1);
220 rule
->options
.inject
.offset
=
221 sector
== -1 ? -1 : sector
* BDRV_SECTOR_SIZE
;
223 iotype
= qapi_enum_parse(&BlkdebugIOType_lookup
,
224 qemu_opt_get(opts
, "iotype"),
225 BLKDEBUG_IO_TYPE__MAX
, &local_error
);
227 error_propagate(errp
, local_error
);
231 if (iotype
!= BLKDEBUG_IO_TYPE__MAX
) {
232 rule
->options
.inject
.iotype_mask
= (1ull << iotype
);
234 /* Apply the default */
235 rule
->options
.inject
.iotype_mask
=
236 (1ull << BLKDEBUG_IO_TYPE_READ
)
237 | (1ull << BLKDEBUG_IO_TYPE_WRITE
)
238 | (1ull << BLKDEBUG_IO_TYPE_WRITE_ZEROES
)
239 | (1ull << BLKDEBUG_IO_TYPE_DISCARD
)
240 | (1ull << BLKDEBUG_IO_TYPE_FLUSH
);
245 case ACTION_SET_STATE
:
246 rule
->options
.set_state
.new_state
=
247 qemu_opt_get_number(opts
, "new_state", 0);
251 rule
->options
.suspend
.tag
=
252 g_strdup(qemu_opt_get(opts
, "tag"));
257 qemu_mutex_lock(&s
->lock
);
258 QLIST_INSERT_HEAD(&s
->rules
[event
], rule
, next
);
259 qemu_mutex_unlock(&s
->lock
);
264 /* Called with lock held or from .bdrv_close */
265 static void remove_rule(BlkdebugRule
*rule
)
267 switch (rule
->action
) {
268 case ACTION_INJECT_ERROR
:
269 case ACTION_SET_STATE
:
272 g_free(rule
->options
.suspend
.tag
);
276 QLIST_REMOVE(rule
, next
);
280 static int read_config(BDRVBlkdebugState
*s
, const char *filename
,
281 QDict
*options
, Error
**errp
)
285 struct add_rule_data d
;
286 Error
*local_err
= NULL
;
289 f
= fopen(filename
, "r");
291 error_setg_errno(errp
, errno
, "Could not read blkdebug config file");
295 ret
= qemu_config_parse(f
, config_groups
, filename
, errp
);
301 if (!qemu_config_parse_qdict(options
, config_groups
, errp
)) {
307 d
.action
= ACTION_INJECT_ERROR
;
308 qemu_opts_foreach(&inject_error_opts
, add_rule
, &d
, &local_err
);
310 error_propagate(errp
, local_err
);
315 d
.action
= ACTION_SET_STATE
;
316 qemu_opts_foreach(&set_state_opts
, add_rule
, &d
, &local_err
);
318 error_propagate(errp
, local_err
);
325 qemu_opts_reset(&inject_error_opts
);
326 qemu_opts_reset(&set_state_opts
);
333 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
334 static void blkdebug_parse_filename(const char *filename
, QDict
*options
,
339 /* Parse the blkdebug: prefix */
340 if (!strstart(filename
, "blkdebug:", &filename
)) {
341 /* There was no prefix; therefore, all options have to be already
342 present in the QDict (except for the filename) */
343 qdict_put_str(options
, "x-image", filename
);
347 /* Parse config file path */
348 c
= strchr(filename
, ':');
350 error_setg(errp
, "blkdebug requires both config file and image path");
355 QString
*config_path
;
356 config_path
= qstring_from_substr(filename
, 0, c
- filename
);
357 qdict_put(options
, "config", config_path
);
360 /* TODO Allow multi-level nesting and set file.filename here */
362 qdict_put_str(options
, "x-image", filename
);
365 static int blkdebug_parse_perm_list(uint64_t *dest
, QDict
*options
,
366 const char *prefix
, Error
**errp
)
369 QDict
*subqdict
= NULL
;
370 QObject
*crumpled_subqdict
= NULL
;
372 BlockPermissionList
*perm_list
= NULL
, *element
;
376 qdict_extract_subqdict(options
, &subqdict
, prefix
);
377 if (!qdict_size(subqdict
)) {
381 crumpled_subqdict
= qdict_crumple(subqdict
, errp
);
382 if (!crumpled_subqdict
) {
387 v
= qobject_input_visitor_new(crumpled_subqdict
);
388 if (!visit_type_BlockPermissionList(v
, NULL
, &perm_list
, errp
)) {
393 for (element
= perm_list
; element
; element
= element
->next
) {
394 *dest
|= bdrv_qapi_perm_to_blk_perm(element
->value
);
398 qapi_free_BlockPermissionList(perm_list
);
400 qobject_unref(subqdict
);
401 qobject_unref(crumpled_subqdict
);
405 static int blkdebug_parse_perms(BDRVBlkdebugState
*s
, QDict
*options
,
410 ret
= blkdebug_parse_perm_list(&s
->take_child_perms
, options
,
411 "take-child-perms.", errp
);
416 ret
= blkdebug_parse_perm_list(&s
->unshare_child_perms
, options
,
417 "unshare-child-perms.", errp
);
425 static QemuOptsList runtime_opts
= {
427 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
431 .type
= QEMU_OPT_STRING
,
432 .help
= "Path to the configuration file",
436 .type
= QEMU_OPT_STRING
,
437 .help
= "[internal use only, will be removed]",
441 .type
= QEMU_OPT_SIZE
,
442 .help
= "Required alignment in bytes",
445 .name
= "max-transfer",
446 .type
= QEMU_OPT_SIZE
,
447 .help
= "Maximum transfer size in bytes",
450 .name
= "opt-write-zero",
451 .type
= QEMU_OPT_SIZE
,
452 .help
= "Optimum write zero alignment in bytes",
455 .name
= "max-write-zero",
456 .type
= QEMU_OPT_SIZE
,
457 .help
= "Maximum write zero size in bytes",
460 .name
= "opt-discard",
461 .type
= QEMU_OPT_SIZE
,
462 .help
= "Optimum discard alignment in bytes",
465 .name
= "max-discard",
466 .type
= QEMU_OPT_SIZE
,
467 .help
= "Maximum discard size in bytes",
469 { /* end of list */ }
473 static int blkdebug_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
476 BDRVBlkdebugState
*s
= bs
->opaque
;
481 qemu_mutex_init(&s
->lock
);
482 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
483 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
488 /* Read rules from config file or command line options */
489 s
->config_file
= g_strdup(qemu_opt_get(opts
, "config"));
490 ret
= read_config(s
, s
->config_file
, options
, errp
);
495 /* Set initial state */
498 /* Parse permissions modifiers before opening the image file */
499 ret
= blkdebug_parse_perms(s
, options
, errp
);
504 /* Open the image file */
505 ret
= bdrv_open_file_child(qemu_opt_get(opts
, "x-image"), options
, "image",
511 bdrv_graph_rdlock_main_loop();
513 bs
->supported_write_flags
= BDRV_REQ_WRITE_UNCHANGED
|
514 (BDRV_REQ_FUA
& bs
->file
->bs
->supported_write_flags
);
515 bs
->supported_zero_flags
= BDRV_REQ_WRITE_UNCHANGED
|
516 ((BDRV_REQ_FUA
| BDRV_REQ_MAY_UNMAP
| BDRV_REQ_NO_FALLBACK
) &
517 bs
->file
->bs
->supported_zero_flags
);
520 /* Set alignment overrides */
521 s
->align
= qemu_opt_get_size(opts
, "align", 0);
522 if (s
->align
&& (s
->align
>= INT_MAX
|| !is_power_of_2(s
->align
))) {
523 error_setg(errp
, "Cannot meet constraints with align %" PRIu64
,
527 align
= MAX(s
->align
, bs
->file
->bs
->bl
.request_alignment
);
529 s
->max_transfer
= qemu_opt_get_size(opts
, "max-transfer", 0);
530 if (s
->max_transfer
&&
531 (s
->max_transfer
>= INT_MAX
||
532 !QEMU_IS_ALIGNED(s
->max_transfer
, align
))) {
533 error_setg(errp
, "Cannot meet constraints with max-transfer %" PRIu64
,
538 s
->opt_write_zero
= qemu_opt_get_size(opts
, "opt-write-zero", 0);
539 if (s
->opt_write_zero
&&
540 (s
->opt_write_zero
>= INT_MAX
||
541 !QEMU_IS_ALIGNED(s
->opt_write_zero
, align
))) {
542 error_setg(errp
, "Cannot meet constraints with opt-write-zero %" PRIu64
,
547 s
->max_write_zero
= qemu_opt_get_size(opts
, "max-write-zero", 0);
548 if (s
->max_write_zero
&&
549 (s
->max_write_zero
>= INT_MAX
||
550 !QEMU_IS_ALIGNED(s
->max_write_zero
,
551 MAX(s
->opt_write_zero
, align
)))) {
552 error_setg(errp
, "Cannot meet constraints with max-write-zero %" PRIu64
,
557 s
->opt_discard
= qemu_opt_get_size(opts
, "opt-discard", 0);
558 if (s
->opt_discard
&&
559 (s
->opt_discard
>= INT_MAX
||
560 !QEMU_IS_ALIGNED(s
->opt_discard
, align
))) {
561 error_setg(errp
, "Cannot meet constraints with opt-discard %" PRIu64
,
566 s
->max_discard
= qemu_opt_get_size(opts
, "max-discard", 0);
567 if (s
->max_discard
&&
568 (s
->max_discard
>= INT_MAX
||
569 !QEMU_IS_ALIGNED(s
->max_discard
,
570 MAX(s
->opt_discard
, align
)))) {
571 error_setg(errp
, "Cannot meet constraints with max-discard %" PRIu64
,
576 bdrv_debug_event(bs
, BLKDBG_NONE
);
580 bdrv_graph_rdunlock_main_loop();
583 qemu_mutex_destroy(&s
->lock
);
584 g_free(s
->config_file
);
590 static int coroutine_fn
rule_check(BlockDriverState
*bs
, uint64_t offset
,
591 uint64_t bytes
, BlkdebugIOType iotype
)
593 BDRVBlkdebugState
*s
= bs
->opaque
;
594 BlkdebugRule
*rule
= NULL
;
598 qemu_mutex_lock(&s
->lock
);
599 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
600 uint64_t inject_offset
= rule
->options
.inject
.offset
;
602 if ((inject_offset
== -1 ||
603 (bytes
&& inject_offset
>= offset
&&
604 inject_offset
< offset
+ bytes
)) &&
605 (rule
->options
.inject
.iotype_mask
& (1ull << iotype
)))
611 if (!rule
|| !rule
->options
.inject
.error
) {
612 qemu_mutex_unlock(&s
->lock
);
616 immediately
= rule
->options
.inject
.immediately
;
617 error
= rule
->options
.inject
.error
;
619 if (rule
->options
.inject
.once
) {
620 QSIMPLEQ_REMOVE(&s
->active_rules
, rule
, BlkdebugRule
, active_next
);
624 qemu_mutex_unlock(&s
->lock
);
626 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
627 qemu_coroutine_yield();
633 static int coroutine_fn GRAPH_RDLOCK
634 blkdebug_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
635 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
639 /* Sanity check block layer guarantees */
640 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
641 assert(QEMU_IS_ALIGNED(bytes
, bs
->bl
.request_alignment
));
642 if (bs
->bl
.max_transfer
) {
643 assert(bytes
<= bs
->bl
.max_transfer
);
646 err
= rule_check(bs
, offset
, bytes
, BLKDEBUG_IO_TYPE_READ
);
651 return bdrv_co_preadv(bs
->file
, offset
, bytes
, qiov
, flags
);
654 static int coroutine_fn GRAPH_RDLOCK
655 blkdebug_co_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
656 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
660 /* Sanity check block layer guarantees */
661 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
662 assert(QEMU_IS_ALIGNED(bytes
, bs
->bl
.request_alignment
));
663 if (bs
->bl
.max_transfer
) {
664 assert(bytes
<= bs
->bl
.max_transfer
);
667 err
= rule_check(bs
, offset
, bytes
, BLKDEBUG_IO_TYPE_WRITE
);
672 return bdrv_co_pwritev(bs
->file
, offset
, bytes
, qiov
, flags
);
675 static int GRAPH_RDLOCK coroutine_fn
blkdebug_co_flush(BlockDriverState
*bs
)
677 int err
= rule_check(bs
, 0, 0, BLKDEBUG_IO_TYPE_FLUSH
);
683 return bdrv_co_flush(bs
->file
->bs
);
686 static int coroutine_fn GRAPH_RDLOCK
687 blkdebug_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
688 BdrvRequestFlags flags
)
690 uint32_t align
= MAX(bs
->bl
.request_alignment
,
691 bs
->bl
.pwrite_zeroes_alignment
);
694 /* Only pass through requests that are larger than requested
695 * preferred alignment (so that we test the fallback to writes on
696 * unaligned portions), and check that the block layer never hands
697 * us anything unaligned that crosses an alignment boundary. */
699 assert(QEMU_IS_ALIGNED(offset
, align
) ||
700 QEMU_IS_ALIGNED(offset
+ bytes
, align
) ||
701 DIV_ROUND_UP(offset
, align
) ==
702 DIV_ROUND_UP(offset
+ bytes
, align
));
705 assert(QEMU_IS_ALIGNED(offset
, align
));
706 assert(QEMU_IS_ALIGNED(bytes
, align
));
707 if (bs
->bl
.max_pwrite_zeroes
) {
708 assert(bytes
<= bs
->bl
.max_pwrite_zeroes
);
711 err
= rule_check(bs
, offset
, bytes
, BLKDEBUG_IO_TYPE_WRITE_ZEROES
);
716 return bdrv_co_pwrite_zeroes(bs
->file
, offset
, bytes
, flags
);
719 static int coroutine_fn GRAPH_RDLOCK
720 blkdebug_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
722 uint32_t align
= bs
->bl
.pdiscard_alignment
;
725 /* Only pass through requests that are larger than requested
726 * minimum alignment, and ensure that unaligned requests do not
727 * cross optimum discard boundaries. */
728 if (bytes
< bs
->bl
.request_alignment
) {
729 assert(QEMU_IS_ALIGNED(offset
, align
) ||
730 QEMU_IS_ALIGNED(offset
+ bytes
, align
) ||
731 DIV_ROUND_UP(offset
, align
) ==
732 DIV_ROUND_UP(offset
+ bytes
, align
));
735 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
736 assert(QEMU_IS_ALIGNED(bytes
, bs
->bl
.request_alignment
));
737 if (align
&& bytes
>= align
) {
738 assert(QEMU_IS_ALIGNED(offset
, align
));
739 assert(QEMU_IS_ALIGNED(bytes
, align
));
741 if (bs
->bl
.max_pdiscard
) {
742 assert(bytes
<= bs
->bl
.max_pdiscard
);
745 err
= rule_check(bs
, offset
, bytes
, BLKDEBUG_IO_TYPE_DISCARD
);
750 return bdrv_co_pdiscard(bs
->file
, offset
, bytes
);
753 static int coroutine_fn GRAPH_RDLOCK
754 blkdebug_co_block_status(BlockDriverState
*bs
, bool want_zero
, int64_t offset
,
755 int64_t bytes
, int64_t *pnum
, int64_t *map
,
756 BlockDriverState
**file
)
760 assert(QEMU_IS_ALIGNED(offset
| bytes
, bs
->bl
.request_alignment
));
762 err
= rule_check(bs
, offset
, bytes
, BLKDEBUG_IO_TYPE_BLOCK_STATUS
);
767 assert(bs
->file
&& bs
->file
->bs
);
770 *file
= bs
->file
->bs
;
771 return BDRV_BLOCK_RAW
| BDRV_BLOCK_OFFSET_VALID
;
774 static void blkdebug_close(BlockDriverState
*bs
)
776 BDRVBlkdebugState
*s
= bs
->opaque
;
777 BlkdebugRule
*rule
, *next
;
780 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
781 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
786 g_free(s
->config_file
);
787 qemu_mutex_destroy(&s
->lock
);
790 /* Called with lock held. */
791 static void suspend_request(BlockDriverState
*bs
, BlkdebugRule
*rule
)
793 BDRVBlkdebugState
*s
= bs
->opaque
;
794 BlkdebugSuspendedReq
*r
;
796 r
= g_new(BlkdebugSuspendedReq
, 1);
798 r
->co
= qemu_coroutine_self();
799 r
->tag
= g_strdup(rule
->options
.suspend
.tag
);
802 QLIST_INSERT_HEAD(&s
->suspended_reqs
, r
, next
);
804 if (!qtest_enabled()) {
805 printf("blkdebug: Suspended request '%s'\n", r
->tag
);
809 /* Called with lock held. */
810 static void process_rule(BlockDriverState
*bs
, struct BlkdebugRule
*rule
,
811 int *action_count
, int *new_state
)
813 BDRVBlkdebugState
*s
= bs
->opaque
;
815 /* Only process rules for the current state */
816 if (rule
->state
&& rule
->state
!= s
->state
) {
820 /* Take the action */
821 action_count
[rule
->action
]++;
822 switch (rule
->action
) {
823 case ACTION_INJECT_ERROR
:
824 if (action_count
[ACTION_INJECT_ERROR
] == 1) {
825 QSIMPLEQ_INIT(&s
->active_rules
);
827 QSIMPLEQ_INSERT_HEAD(&s
->active_rules
, rule
, active_next
);
830 case ACTION_SET_STATE
:
831 *new_state
= rule
->options
.set_state
.new_state
;
835 suspend_request(bs
, rule
);
840 static void coroutine_fn
841 blkdebug_co_debug_event(BlockDriverState
*bs
, BlkdebugEvent event
)
843 BDRVBlkdebugState
*s
= bs
->opaque
;
844 struct BlkdebugRule
*rule
, *next
;
846 int actions_count
[ACTION__MAX
] = { 0 };
848 assert((int)event
>= 0 && event
< BLKDBG__MAX
);
850 WITH_QEMU_LOCK_GUARD(&s
->lock
) {
851 new_state
= s
->state
;
852 QLIST_FOREACH_SAFE(rule
, &s
->rules
[event
], next
, next
) {
853 process_rule(bs
, rule
, actions_count
, &new_state
);
855 s
->state
= new_state
;
858 while (actions_count
[ACTION_SUSPEND
] > 0) {
859 qemu_coroutine_yield();
860 actions_count
[ACTION_SUSPEND
]--;
864 static int blkdebug_debug_breakpoint(BlockDriverState
*bs
, const char *event
,
867 BDRVBlkdebugState
*s
= bs
->opaque
;
868 struct BlkdebugRule
*rule
;
871 blkdebug_event
= qapi_enum_parse(&BlkdebugEvent_lookup
, event
, -1, NULL
);
872 if (blkdebug_event
< 0) {
876 rule
= g_malloc(sizeof(*rule
));
877 *rule
= (struct BlkdebugRule
) {
878 .event
= blkdebug_event
,
879 .action
= ACTION_SUSPEND
,
881 .options
.suspend
.tag
= g_strdup(tag
),
884 qemu_mutex_lock(&s
->lock
);
885 QLIST_INSERT_HEAD(&s
->rules
[blkdebug_event
], rule
, next
);
886 qemu_mutex_unlock(&s
->lock
);
891 /* Called with lock held. May temporarily release lock. */
892 static int resume_req_by_tag(BDRVBlkdebugState
*s
, const char *tag
, bool all
)
894 BlkdebugSuspendedReq
*r
;
898 * No need for _SAFE, since a different coroutine can remove another node
899 * (not the current one) in this list, and when the current one is removed
900 * the iteration starts back from beginning anyways.
902 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
903 if (!strcmp(r
->tag
, tag
)) {
904 Coroutine
*co
= r
->co
;
906 if (!qtest_enabled()) {
907 printf("blkdebug: Resuming request '%s'\n", r
->tag
);
910 QLIST_REMOVE(r
, next
);
914 qemu_mutex_unlock(&s
->lock
);
915 qemu_coroutine_enter(co
);
916 qemu_mutex_lock(&s
->lock
);
927 static int blkdebug_debug_resume(BlockDriverState
*bs
, const char *tag
)
929 BDRVBlkdebugState
*s
= bs
->opaque
;
930 QEMU_LOCK_GUARD(&s
->lock
);
931 return resume_req_by_tag(s
, tag
, false);
934 static int blkdebug_debug_remove_breakpoint(BlockDriverState
*bs
,
937 BDRVBlkdebugState
*s
= bs
->opaque
;
938 BlkdebugRule
*rule
, *next
;
939 int i
, ret
= -ENOENT
;
941 QEMU_LOCK_GUARD(&s
->lock
);
942 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
943 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
944 if (rule
->action
== ACTION_SUSPEND
&&
945 !strcmp(rule
->options
.suspend
.tag
, tag
)) {
951 if (resume_req_by_tag(s
, tag
, true) == 0) {
957 static bool blkdebug_debug_is_suspended(BlockDriverState
*bs
, const char *tag
)
959 BDRVBlkdebugState
*s
= bs
->opaque
;
960 BlkdebugSuspendedReq
*r
;
962 QEMU_LOCK_GUARD(&s
->lock
);
963 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
964 if (!strcmp(r
->tag
, tag
)) {
971 static int64_t coroutine_fn GRAPH_RDLOCK
972 blkdebug_co_getlength(BlockDriverState
*bs
)
974 return bdrv_co_getlength(bs
->file
->bs
);
977 static void GRAPH_RDLOCK
blkdebug_refresh_filename(BlockDriverState
*bs
)
979 BDRVBlkdebugState
*s
= bs
->opaque
;
983 if (!bs
->file
->bs
->exact_filename
[0]) {
987 for (e
= qdict_first(bs
->full_open_options
); e
;
988 e
= qdict_next(bs
->full_open_options
, e
))
990 /* Real child options are under "image", but "x-image" may
991 * contain a filename */
992 if (strcmp(qdict_entry_key(e
), "config") &&
993 strcmp(qdict_entry_key(e
), "image") &&
994 strcmp(qdict_entry_key(e
), "x-image") &&
995 strcmp(qdict_entry_key(e
), "driver"))
1001 ret
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
1003 s
->config_file
?: "", bs
->file
->bs
->exact_filename
);
1004 if (ret
>= sizeof(bs
->exact_filename
)) {
1005 /* An overflow makes the filename unusable, so do not report any */
1006 bs
->exact_filename
[0] = 0;
1010 static void blkdebug_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1012 BDRVBlkdebugState
*s
= bs
->opaque
;
1015 bs
->bl
.request_alignment
= s
->align
;
1017 if (s
->max_transfer
) {
1018 bs
->bl
.max_transfer
= s
->max_transfer
;
1020 if (s
->opt_write_zero
) {
1021 bs
->bl
.pwrite_zeroes_alignment
= s
->opt_write_zero
;
1023 if (s
->max_write_zero
) {
1024 bs
->bl
.max_pwrite_zeroes
= s
->max_write_zero
;
1026 if (s
->opt_discard
) {
1027 bs
->bl
.pdiscard_alignment
= s
->opt_discard
;
1029 if (s
->max_discard
) {
1030 bs
->bl
.max_pdiscard
= s
->max_discard
;
1034 static int blkdebug_reopen_prepare(BDRVReopenState
*reopen_state
,
1035 BlockReopenQueue
*queue
, Error
**errp
)
1040 static void blkdebug_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
1042 BlockReopenQueue
*reopen_queue
,
1043 uint64_t perm
, uint64_t shared
,
1044 uint64_t *nperm
, uint64_t *nshared
)
1046 BDRVBlkdebugState
*s
= bs
->opaque
;
1048 bdrv_default_perms(bs
, c
, role
, reopen_queue
,
1049 perm
, shared
, nperm
, nshared
);
1051 *nperm
|= s
->take_child_perms
;
1052 *nshared
&= ~s
->unshare_child_perms
;
1055 static const char *const blkdebug_strong_runtime_opts
[] = {
1069 static BlockDriver bdrv_blkdebug
= {
1070 .format_name
= "blkdebug",
1071 .protocol_name
= "blkdebug",
1072 .instance_size
= sizeof(BDRVBlkdebugState
),
1075 .bdrv_parse_filename
= blkdebug_parse_filename
,
1076 .bdrv_open
= blkdebug_open
,
1077 .bdrv_close
= blkdebug_close
,
1078 .bdrv_reopen_prepare
= blkdebug_reopen_prepare
,
1079 .bdrv_child_perm
= blkdebug_child_perm
,
1081 .bdrv_co_getlength
= blkdebug_co_getlength
,
1082 .bdrv_refresh_filename
= blkdebug_refresh_filename
,
1083 .bdrv_refresh_limits
= blkdebug_refresh_limits
,
1085 .bdrv_co_preadv
= blkdebug_co_preadv
,
1086 .bdrv_co_pwritev
= blkdebug_co_pwritev
,
1087 .bdrv_co_flush_to_disk
= blkdebug_co_flush
,
1088 .bdrv_co_pwrite_zeroes
= blkdebug_co_pwrite_zeroes
,
1089 .bdrv_co_pdiscard
= blkdebug_co_pdiscard
,
1090 .bdrv_co_block_status
= blkdebug_co_block_status
,
1092 .bdrv_co_debug_event
= blkdebug_co_debug_event
,
1093 .bdrv_debug_breakpoint
= blkdebug_debug_breakpoint
,
1094 .bdrv_debug_remove_breakpoint
1095 = blkdebug_debug_remove_breakpoint
,
1096 .bdrv_debug_resume
= blkdebug_debug_resume
,
1097 .bdrv_debug_is_suspended
= blkdebug_debug_is_suspended
,
1099 .strong_runtime_opts
= blkdebug_strong_runtime_opts
,
1102 static void bdrv_blkdebug_init(void)
1104 bdrv_register(&bdrv_blkdebug
);
1107 block_init(bdrv_blkdebug_init
);