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_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
{
41 uint64_t max_transfer
;
42 uint64_t opt_write_zero
;
43 uint64_t max_write_zero
;
47 /* For blkdebug_refresh_filename() */
50 QLIST_HEAD(, BlkdebugRule
) rules
[BLKDBG__MAX
];
51 QSIMPLEQ_HEAD(, BlkdebugRule
) active_rules
;
52 QLIST_HEAD(, BlkdebugSuspendedReq
) suspended_reqs
;
55 typedef struct BlkdebugAIOCB
{
60 typedef struct BlkdebugSuspendedReq
{
63 QLIST_ENTRY(BlkdebugSuspendedReq
) next
;
64 } BlkdebugSuspendedReq
;
72 typedef struct BlkdebugRule
{
91 QLIST_ENTRY(BlkdebugRule
) next
;
92 QSIMPLEQ_ENTRY(BlkdebugRule
) active_next
;
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
),
104 .type
= QEMU_OPT_STRING
,
108 .type
= QEMU_OPT_NUMBER
,
112 .type
= QEMU_OPT_STRING
,
116 .type
= QEMU_OPT_NUMBER
,
120 .type
= QEMU_OPT_NUMBER
,
124 .type
= QEMU_OPT_BOOL
,
127 .name
= "immediately",
128 .type
= QEMU_OPT_BOOL
,
130 { /* end of list */ }
134 static QemuOptsList set_state_opts
= {
136 .head
= QTAILQ_HEAD_INITIALIZER(set_state_opts
.head
),
140 .type
= QEMU_OPT_STRING
,
144 .type
= QEMU_OPT_NUMBER
,
148 .type
= QEMU_OPT_NUMBER
,
150 { /* end of list */ }
154 static QemuOptsList
*config_groups
[] = {
160 struct add_rule_data
{
161 BDRVBlkdebugState
*s
;
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
;
171 struct BlkdebugRule
*rule
;
173 BlkdebugIOType iotype
;
174 Error
*local_error
= NULL
;
176 /* Find the right event for the rule */
177 event_name
= qemu_opt_get(opts
, "event");
179 error_setg(errp
, "Missing event name for rule");
182 event
= qapi_enum_parse(&BlkdebugEvent_lookup
, event_name
, -1, errp
);
187 /* Set attributes common for all actions */
188 rule
= g_malloc0(sizeof(*rule
));
189 *rule
= (struct BlkdebugRule
) {
192 .state
= qemu_opt_get_number(opts
, "state", 0),
195 /* Parse action-specific options */
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
);
210 error_propagate(errp
, local_error
);
213 if (iotype
!= BLKDEBUG_IO_TYPE__MAX
) {
214 rule
->options
.inject
.iotype_mask
= (1ull << iotype
);
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
);
227 case ACTION_SET_STATE
:
228 rule
->options
.set_state
.new_state
=
229 qemu_opt_get_number(opts
, "new_state", 0);
233 rule
->options
.suspend
.tag
=
234 g_strdup(qemu_opt_get(opts
, "tag"));
239 QLIST_INSERT_HEAD(&s
->rules
[event
], rule
, next
);
244 static void remove_rule(BlkdebugRule
*rule
)
246 switch (rule
->action
) {
247 case ACTION_INJECT_ERROR
:
248 case ACTION_SET_STATE
:
251 g_free(rule
->options
.suspend
.tag
);
255 QLIST_REMOVE(rule
, next
);
259 static int read_config(BDRVBlkdebugState
*s
, const char *filename
,
260 QDict
*options
, Error
**errp
)
264 struct add_rule_data d
;
265 Error
*local_err
= NULL
;
268 f
= fopen(filename
, "r");
270 error_setg_errno(errp
, errno
, "Could not read blkdebug config file");
274 ret
= qemu_config_parse(f
, config_groups
, filename
);
276 error_setg(errp
, "Could not parse blkdebug config file");
281 qemu_config_parse_qdict(options
, config_groups
, &local_err
);
283 error_propagate(errp
, local_err
);
289 d
.action
= ACTION_INJECT_ERROR
;
290 qemu_opts_foreach(&inject_error_opts
, add_rule
, &d
, &local_err
);
292 error_propagate(errp
, local_err
);
297 d
.action
= ACTION_SET_STATE
;
298 qemu_opts_foreach(&set_state_opts
, add_rule
, &d
, &local_err
);
300 error_propagate(errp
, local_err
);
307 qemu_opts_reset(&inject_error_opts
);
308 qemu_opts_reset(&set_state_opts
);
315 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
316 static void blkdebug_parse_filename(const char *filename
, QDict
*options
,
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
);
329 /* Parse config file path */
330 c
= strchr(filename
, ':');
332 error_setg(errp
, "blkdebug requires both config file and image path");
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 */
344 qdict_put_str(options
, "x-image", filename
);
347 static QemuOptsList runtime_opts
= {
349 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
353 .type
= QEMU_OPT_STRING
,
354 .help
= "Path to the configuration file",
358 .type
= QEMU_OPT_STRING
,
359 .help
= "[internal use only, will be removed]",
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
,
398 BDRVBlkdebugState
*s
= bs
->opaque
;
400 Error
*local_err
= NULL
;
404 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
405 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
407 error_propagate(errp
, local_err
);
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
);
419 /* Set initial state */
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
);
427 error_propagate(errp
, local_err
);
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
);
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
,
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
,
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
,
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
,
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
,
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
,
494 bdrv_debug_event(bs
, BLKDBG_NONE
);
499 g_free(s
->config_file
);
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
;
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
)))
525 if (!rule
|| !rule
->options
.inject
.error
) {
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
);
538 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
539 qemu_coroutine_yield();
545 static int coroutine_fn
546 blkdebug_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
547 QEMUIOVector
*qiov
, int flags
)
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
);
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
)
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
);
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
);
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
);
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. */
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
));
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
);
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
;
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
));
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
);
662 return bdrv_co_pdiscard(bs
->file
, offset
, bytes
);
665 static int coroutine_fn
blkdebug_co_block_status(BlockDriverState
*bs
,
671 BlockDriverState
**file
)
675 assert(QEMU_IS_ALIGNED(offset
| bytes
, bs
->bl
.request_alignment
));
677 err
= rule_check(bs
, offset
, bytes
, BLKDEBUG_IO_TYPE_BLOCK_STATUS
);
682 return bdrv_co_block_status_from_file(bs
, want_zero
, offset
, bytes
,
686 static void blkdebug_close(BlockDriverState
*bs
)
688 BDRVBlkdebugState
*s
= bs
->opaque
;
689 BlkdebugRule
*rule
, *next
;
692 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
693 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
698 g_free(s
->config_file
);
701 static void suspend_request(BlockDriverState
*bs
, BlkdebugRule
*rule
)
703 BDRVBlkdebugState
*s
= bs
->opaque
;
704 BlkdebugSuspendedReq r
;
706 r
= (BlkdebugSuspendedReq
) {
707 .co
= qemu_coroutine_self(),
708 .tag
= g_strdup(rule
->options
.suspend
.tag
),
712 QLIST_INSERT_HEAD(&s
->suspended_reqs
, &r
, next
);
714 if (!qtest_enabled()) {
715 printf("blkdebug: Suspended request '%s'\n", r
.tag
);
717 qemu_coroutine_yield();
718 if (!qtest_enabled()) {
719 printf("blkdebug: Resuming request '%s'\n", r
.tag
);
722 QLIST_REMOVE(&r
, next
);
726 static bool process_rule(BlockDriverState
*bs
, struct BlkdebugRule
*rule
,
729 BDRVBlkdebugState
*s
= bs
->opaque
;
731 /* Only process rules for the current state */
732 if (rule
->state
&& rule
->state
!= s
->state
) {
736 /* Take the action */
737 switch (rule
->action
) {
738 case ACTION_INJECT_ERROR
:
740 QSIMPLEQ_INIT(&s
->active_rules
);
743 QSIMPLEQ_INSERT_HEAD(&s
->active_rules
, rule
, active_next
);
746 case ACTION_SET_STATE
:
747 s
->new_state
= rule
->options
.set_state
.new_state
;
751 suspend_request(bs
, rule
);
757 static void blkdebug_debug_event(BlockDriverState
*bs
, BlkdebugEvent event
)
759 BDRVBlkdebugState
*s
= bs
->opaque
;
760 struct BlkdebugRule
*rule
, *next
;
763 assert((int)event
>= 0 && event
< BLKDBG__MAX
);
766 s
->new_state
= s
->state
;
767 QLIST_FOREACH_SAFE(rule
, &s
->rules
[event
], next
, next
) {
768 injected
= process_rule(bs
, rule
, injected
);
770 s
->state
= s
->new_state
;
773 static int blkdebug_debug_breakpoint(BlockDriverState
*bs
, const char *event
,
776 BDRVBlkdebugState
*s
= bs
->opaque
;
777 struct BlkdebugRule
*rule
;
780 blkdebug_event
= qapi_enum_parse(&BlkdebugEvent_lookup
, event
, -1, NULL
);
781 if (blkdebug_event
< 0) {
785 rule
= g_malloc(sizeof(*rule
));
786 *rule
= (struct BlkdebugRule
) {
787 .event
= blkdebug_event
,
788 .action
= ACTION_SUSPEND
,
790 .options
.suspend
.tag
= g_strdup(tag
),
793 QLIST_INSERT_HEAD(&s
->rules
[blkdebug_event
], rule
, next
);
798 static int blkdebug_debug_resume(BlockDriverState
*bs
, const char *tag
)
800 BDRVBlkdebugState
*s
= bs
->opaque
;
801 BlkdebugSuspendedReq
*r
, *next
;
803 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, next
) {
804 if (!strcmp(r
->tag
, tag
)) {
805 qemu_coroutine_enter(r
->co
);
812 static int blkdebug_debug_remove_breakpoint(BlockDriverState
*bs
,
815 BDRVBlkdebugState
*s
= bs
->opaque
;
816 BlkdebugSuspendedReq
*r
, *r_next
;
817 BlkdebugRule
*rule
, *next
;
818 int i
, ret
= -ENOENT
;
820 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
821 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
822 if (rule
->action
== ACTION_SUSPEND
&&
823 !strcmp(rule
->options
.suspend
.tag
, tag
)) {
829 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, r_next
) {
830 if (!strcmp(r
->tag
, tag
)) {
831 qemu_coroutine_enter(r
->co
);
838 static bool blkdebug_debug_is_suspended(BlockDriverState
*bs
, const char *tag
)
840 BDRVBlkdebugState
*s
= bs
->opaque
;
841 BlkdebugSuspendedReq
*r
;
843 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
844 if (!strcmp(r
->tag
, tag
)) {
851 static int64_t blkdebug_getlength(BlockDriverState
*bs
)
853 return bdrv_getlength(bs
->file
->bs
);
856 static void blkdebug_refresh_filename(BlockDriverState
*bs
)
858 BDRVBlkdebugState
*s
= bs
->opaque
;
862 if (!bs
->file
->bs
->exact_filename
[0]) {
866 for (e
= qdict_first(bs
->full_open_options
); e
;
867 e
= qdict_next(bs
->full_open_options
, e
))
869 /* Real child options are under "image", but "x-image" may
870 * contain a filename */
871 if (strcmp(qdict_entry_key(e
), "config") &&
872 strcmp(qdict_entry_key(e
), "image") &&
873 strcmp(qdict_entry_key(e
), "x-image") &&
874 strcmp(qdict_entry_key(e
), "driver"))
880 ret
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
882 s
->config_file
?: "", bs
->file
->bs
->exact_filename
);
883 if (ret
>= sizeof(bs
->exact_filename
)) {
884 /* An overflow makes the filename unusable, so do not report any */
885 bs
->exact_filename
[0] = 0;
889 static void blkdebug_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
891 BDRVBlkdebugState
*s
= bs
->opaque
;
894 bs
->bl
.request_alignment
= s
->align
;
896 if (s
->max_transfer
) {
897 bs
->bl
.max_transfer
= s
->max_transfer
;
899 if (s
->opt_write_zero
) {
900 bs
->bl
.pwrite_zeroes_alignment
= s
->opt_write_zero
;
902 if (s
->max_write_zero
) {
903 bs
->bl
.max_pwrite_zeroes
= s
->max_write_zero
;
905 if (s
->opt_discard
) {
906 bs
->bl
.pdiscard_alignment
= s
->opt_discard
;
908 if (s
->max_discard
) {
909 bs
->bl
.max_pdiscard
= s
->max_discard
;
913 static int blkdebug_reopen_prepare(BDRVReopenState
*reopen_state
,
914 BlockReopenQueue
*queue
, Error
**errp
)
919 static const char *const blkdebug_strong_runtime_opts
[] = {
933 static BlockDriver bdrv_blkdebug
= {
934 .format_name
= "blkdebug",
935 .protocol_name
= "blkdebug",
936 .instance_size
= sizeof(BDRVBlkdebugState
),
939 .bdrv_parse_filename
= blkdebug_parse_filename
,
940 .bdrv_file_open
= blkdebug_open
,
941 .bdrv_close
= blkdebug_close
,
942 .bdrv_reopen_prepare
= blkdebug_reopen_prepare
,
943 .bdrv_child_perm
= bdrv_filter_default_perms
,
945 .bdrv_getlength
= blkdebug_getlength
,
946 .bdrv_refresh_filename
= blkdebug_refresh_filename
,
947 .bdrv_refresh_limits
= blkdebug_refresh_limits
,
949 .bdrv_co_preadv
= blkdebug_co_preadv
,
950 .bdrv_co_pwritev
= blkdebug_co_pwritev
,
951 .bdrv_co_flush_to_disk
= blkdebug_co_flush
,
952 .bdrv_co_pwrite_zeroes
= blkdebug_co_pwrite_zeroes
,
953 .bdrv_co_pdiscard
= blkdebug_co_pdiscard
,
954 .bdrv_co_block_status
= blkdebug_co_block_status
,
956 .bdrv_debug_event
= blkdebug_debug_event
,
957 .bdrv_debug_breakpoint
= blkdebug_debug_breakpoint
,
958 .bdrv_debug_remove_breakpoint
959 = blkdebug_debug_remove_breakpoint
,
960 .bdrv_debug_resume
= blkdebug_debug_resume
,
961 .bdrv_debug_is_suspended
= blkdebug_debug_is_suspended
,
963 .strong_runtime_opts
= blkdebug_strong_runtime_opts
,
966 static void bdrv_blkdebug_init(void)
968 bdrv_register(&bdrv_blkdebug
);
971 block_init(bdrv_blkdebug_init
);