2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
9 * This file incorporates work covered by the following copyright and
12 * Copyright (c) 2003-2008 Fabrice Bellard
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "qemu/osdep.h"
34 #include "sysemu/block-backend.h"
35 #include "sysemu/blockdev.h"
36 #include "hw/block/block.h"
37 #include "block/blockjob.h"
38 #include "block/dirty-bitmap.h"
39 #include "block/qdict.h"
40 #include "block/throttle-groups.h"
41 #include "monitor/monitor.h"
42 #include "qemu/error-report.h"
43 #include "qemu/option.h"
44 #include "qemu/qemu-print.h"
45 #include "qemu/config-file.h"
46 #include "qapi/qapi-commands-block.h"
47 #include "qapi/qapi-commands-transaction.h"
48 #include "qapi/qapi-visit-block-core.h"
49 #include "qapi/qmp/qdict.h"
50 #include "qapi/qmp/qnum.h"
51 #include "qapi/qmp/qstring.h"
52 #include "qapi/error.h"
53 #include "qapi/qmp/qerror.h"
54 #include "qapi/qmp/qlist.h"
55 #include "qapi/qobject-output-visitor.h"
56 #include "sysemu/sysemu.h"
57 #include "sysemu/iothread.h"
58 #include "block/block_int.h"
59 #include "block/trace.h"
60 #include "sysemu/runstate.h"
61 #include "sysemu/replay.h"
62 #include "qemu/cutils.h"
63 #include "qemu/help_option.h"
64 #include "qemu/main-loop.h"
65 #include "qemu/throttle-options.h"
67 /* Protected by BQL */
68 QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
69 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
71 void bdrv_set_monitor_owned(BlockDriverState
*bs
)
74 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
77 static const char *const if_name
[IF_COUNT
] = {
81 [IF_FLOPPY
] = "floppy",
82 [IF_PFLASH
] = "pflash",
85 [IF_VIRTIO
] = "virtio",
89 static int if_max_devs
[IF_COUNT
] = {
91 * Do not change these numbers! They govern how drive option
92 * index maps to unit and bus. That mapping is ABI.
94 * All controllers used to implement if=T drives need to support
95 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
96 * Otherwise, some index values map to "impossible" bus, unit
99 * For instance, if you change [IF_SCSI] to 255, -drive
100 * if=scsi,index=12 no longer means bus=1,unit=5, but
101 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
102 * the drive can't be set up. Regression.
109 * Boards may call this to offer board-by-board overrides
110 * of the default, global values.
112 void override_max_devs(BlockInterfaceType type
, int max_devs
)
123 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
124 dinfo
= blk_legacy_dinfo(blk
);
125 if (dinfo
->type
== type
) {
126 fprintf(stderr
, "Cannot override units-per-bus property of"
127 " the %s interface, because a drive of that type has"
128 " already been added.\n", if_name
[type
]);
129 g_assert_not_reached();
133 if_max_devs
[type
] = max_devs
;
137 * We automatically delete the drive when a device using it gets
138 * unplugged. Questionable feature, but we can't just drop it.
139 * Device models call blockdev_mark_auto_del() to schedule the
140 * automatic deletion, and generic qdev code calls blockdev_auto_del()
141 * when deletion is actually safe.
143 void blockdev_mark_auto_del(BlockBackend
*blk
)
145 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
156 for (job
= block_job_next_locked(NULL
); job
;
157 job
= block_job_next_locked(job
)) {
158 if (block_job_has_bdrv(job
, blk_bs(blk
))) {
159 job_cancel_locked(&job
->job
, false);
166 void blockdev_auto_del(BlockBackend
*blk
)
168 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
171 if (dinfo
&& dinfo
->auto_del
) {
172 monitor_remove_blk(blk
);
177 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
179 int max_devs
= if_max_devs
[type
];
180 return max_devs
? index
/ max_devs
: 0;
183 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
185 int max_devs
= if_max_devs
[type
];
186 return max_devs
? index
% max_devs
: index
;
189 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
196 opts
= qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
200 if (type
!= IF_DEFAULT
) {
201 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
204 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
207 qemu_opt_set(opts
, "file", file
, &error_abort
);
211 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
218 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
219 dinfo
= blk_legacy_dinfo(blk
);
220 if (dinfo
&& dinfo
->type
== type
221 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
230 * Check board claimed all -drive that are meant to be claimed.
231 * Fatal error if any remain unclaimed.
233 void drive_check_orphaned(void)
238 bool orphans
= false;
242 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
243 dinfo
= blk_legacy_dinfo(blk
);
245 * Ignore default drives, because we create certain default
246 * drives unconditionally, then leave them unclaimed. Not the
248 * Ignore IF_VIRTIO, because it gets desugared into -device,
249 * so we can leave failing to -device.
250 * Ignore IF_NONE, because leaving unclaimed IF_NONE remains
251 * available for device_add is a feature.
253 if (dinfo
->is_default
|| dinfo
->type
== IF_VIRTIO
254 || dinfo
->type
== IF_NONE
) {
257 if (!blk_get_attached_dev(blk
)) {
259 qemu_opts_loc_restore(dinfo
->opts
);
260 error_report("machine type does not support"
261 " if=%s,bus=%d,unit=%d",
262 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
273 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
276 return drive_get(type
,
277 drive_index_to_bus_id(type
, index
),
278 drive_index_to_unit_id(type
, index
));
281 int drive_get_max_bus(BlockInterfaceType type
)
290 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
291 dinfo
= blk_legacy_dinfo(blk
);
292 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
293 max_bus
= dinfo
->bus
;
299 static void bdrv_format_print(void *opaque
, const char *name
)
301 qemu_printf(" %s", name
);
306 BlockDriverState
*bs
;
309 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
311 if (!strcmp(buf
, "ignore")) {
312 return BLOCKDEV_ON_ERROR_IGNORE
;
313 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
314 return BLOCKDEV_ON_ERROR_ENOSPC
;
315 } else if (!strcmp(buf
, "stop")) {
316 return BLOCKDEV_ON_ERROR_STOP
;
317 } else if (!strcmp(buf
, "report")) {
318 return BLOCKDEV_ON_ERROR_REPORT
;
320 error_setg(errp
, "'%s' invalid %s error action",
321 buf
, is_read
? "read" : "write");
326 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
329 const QListEntry
*entry
;
330 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
331 switch (qobject_type(entry
->value
)) {
333 case QTYPE_QSTRING
: {
334 unsigned long long length
;
335 const char *str
= qstring_get_str(qobject_to(QString
,
337 if (parse_uint_full(str
, &length
, 10) == 0 &&
338 length
> 0 && length
<= UINT_MAX
) {
339 block_acct_add_interval(stats
, (unsigned) length
);
341 error_setg(errp
, "Invalid interval length: %s", str
);
348 int64_t length
= qnum_get_int(qobject_to(QNum
, entry
->value
));
350 if (length
> 0 && length
<= UINT_MAX
) {
351 block_acct_add_interval(stats
, (unsigned) length
);
353 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
360 error_setg(errp
, "The specification of stats-intervals is invalid");
367 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
369 /* All parameters but @opts are optional and may be set to NULL. */
370 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
371 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
372 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
374 Error
*local_error
= NULL
;
378 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
379 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
382 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
383 if (bdrv_parse_aio(aio
, bdrv_flags
) < 0) {
384 error_setg(errp
, "invalid aio option");
390 /* disk I/O throttling */
391 if (throttling_group
) {
392 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
396 throttle_config_init(throttle_cfg
);
397 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
398 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
399 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
400 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
401 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
402 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
403 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
404 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
405 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
406 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
407 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
408 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
410 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
411 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
412 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
413 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
414 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
415 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
416 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
417 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
418 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
419 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
420 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
421 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
423 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
424 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
425 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
426 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
427 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
428 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
429 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
430 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
431 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
432 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
433 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
434 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
436 throttle_cfg
->op_size
=
437 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
439 if (!throttle_is_valid(throttle_cfg
, errp
)) {
446 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup
,
447 qemu_opt_get(opts
, "detect-zeroes"),
448 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
451 error_propagate(errp
, local_error
);
457 static OnOffAuto
account_get_opt(QemuOpts
*opts
, const char *name
)
459 if (!qemu_opt_find(opts
, name
)) {
460 return ON_OFF_AUTO_AUTO
;
462 if (qemu_opt_get_bool(opts
, name
, true)) {
463 return ON_OFF_AUTO_ON
;
465 return ON_OFF_AUTO_OFF
;
468 /* Takes the ownership of bs_opts */
469 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
474 int on_read_error
, on_write_error
;
475 OnOffAuto account_invalid
, account_failed
;
476 bool writethrough
, read_only
;
478 BlockDriverState
*bs
;
483 QDict
*interval_dict
= NULL
;
484 QList
*interval_list
= NULL
;
486 BlockdevDetectZeroesOptions detect_zeroes
=
487 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
488 const char *throttling_group
= NULL
;
490 /* Check common options by copying from bs_opts to opts, all other options
491 * stay in bs_opts for processing by bdrv_open(). */
492 id
= qdict_get_try_str(bs_opts
, "id");
493 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, errp
);
498 if (!qemu_opts_absorb_qdict(opts
, bs_opts
, errp
)) {
503 qdict_del(bs_opts
, "id");
506 /* extract parameters */
507 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
509 account_invalid
= account_get_opt(opts
, "stats-account-invalid");
510 account_failed
= account_get_opt(opts
, "stats-account-failed");
512 writethrough
= !qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true);
514 id
= qemu_opts_id(opts
);
516 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
517 qdict_array_split(interval_dict
, &interval_list
);
519 if (qdict_size(interval_dict
) != 0) {
520 error_setg(errp
, "Invalid option stats-intervals.%s",
521 qdict_first(interval_dict
)->key
);
525 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
526 &detect_zeroes
, &error
);
528 error_propagate(errp
, error
);
532 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
533 if (is_help_option(buf
)) {
534 qemu_printf("Supported formats:");
535 bdrv_iterate_format(bdrv_format_print
, NULL
, false);
536 qemu_printf("\nSupported formats (read-only):");
537 bdrv_iterate_format(bdrv_format_print
, NULL
, true);
542 if (qdict_haskey(bs_opts
, "driver")) {
543 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
546 qdict_put_str(bs_opts
, "driver", buf
);
549 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
550 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
551 on_write_error
= parse_block_error_action(buf
, 0, &error
);
553 error_propagate(errp
, error
);
558 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
559 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
560 on_read_error
= parse_block_error_action(buf
, 1, &error
);
562 error_propagate(errp
, error
);
568 bdrv_flags
|= BDRV_O_SNAPSHOT
;
571 read_only
= qemu_opt_get_bool(opts
, BDRV_OPT_READ_ONLY
, false);
574 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
575 BlockBackendRootState
*blk_rs
;
577 blk
= blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL
);
578 blk_rs
= blk_get_root_state(blk
);
579 blk_rs
->open_flags
= bdrv_flags
| (read_only
? 0 : BDRV_O_RDWR
);
580 blk_rs
->detect_zeroes
= detect_zeroes
;
582 qobject_unref(bs_opts
);
584 if (file
&& !*file
) {
588 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
589 * with other callers) rather than what we want as the real defaults.
590 * Apply the defaults here instead. */
591 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
592 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
593 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
,
594 read_only
? "on" : "off");
595 qdict_set_default_str(bs_opts
, BDRV_OPT_AUTO_READ_ONLY
, "on");
596 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
598 if (runstate_check(RUN_STATE_INMIGRATE
)) {
599 bdrv_flags
|= BDRV_O_INACTIVE
;
602 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
608 bs
->detect_zeroes
= detect_zeroes
;
610 block_acct_setup(blk_get_stats(blk
), account_invalid
, account_failed
);
612 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
619 /* disk I/O throttling */
620 if (throttle_enabled(&cfg
)) {
621 if (!throttling_group
) {
622 throttling_group
= id
;
624 blk_io_limits_enable(blk
, throttling_group
);
625 blk_set_io_limits(blk
, &cfg
);
628 blk_set_enable_write_cache(blk
, !writethrough
);
629 blk_set_on_error(blk
, on_read_error
, on_write_error
);
631 if (!monitor_add_blk(blk
, id
, errp
)) {
639 qobject_unref(interval_dict
);
640 qobject_unref(interval_list
);
645 qobject_unref(interval_dict
);
646 qobject_unref(interval_list
);
648 qobject_unref(bs_opts
);
652 /* Takes the ownership of bs_opts */
653 BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
658 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
659 * with other callers) rather than what we want as the real defaults.
660 * Apply the defaults here instead. */
661 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
662 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
663 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
, "off");
665 if (runstate_check(RUN_STATE_INMIGRATE
)) {
666 bdrv_flags
|= BDRV_O_INACTIVE
;
669 return bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
672 void blockdev_close_all_bdrv_states(void)
674 BlockDriverState
*bs
, *next_bs
;
677 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
678 AioContext
*ctx
= bdrv_get_aio_context(bs
);
680 aio_context_acquire(ctx
);
682 aio_context_release(ctx
);
686 /* Iterates over the list of monitor-owned BlockDriverStates */
687 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
690 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
691 : QTAILQ_FIRST(&monitor_bdrv_states
);
694 static bool qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
699 value
= qemu_opt_get(opts
, from
);
701 if (qemu_opt_find(opts
, to
)) {
702 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
703 "same time", to
, from
);
708 /* rename all items in opts */
709 while ((value
= qemu_opt_get(opts
, from
))) {
710 qemu_opt_set(opts
, to
, value
, &error_abort
);
711 qemu_opt_unset(opts
, from
);
716 QemuOptsList qemu_legacy_drive_opts
= {
718 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
722 .type
= QEMU_OPT_NUMBER
,
723 .help
= "bus number",
726 .type
= QEMU_OPT_NUMBER
,
727 .help
= "unit number (i.e. lun for scsi)",
730 .type
= QEMU_OPT_NUMBER
,
731 .help
= "index number",
734 .type
= QEMU_OPT_STRING
,
735 .help
= "media type (disk, cdrom)",
738 .type
= QEMU_OPT_STRING
,
739 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
742 .type
= QEMU_OPT_STRING
,
746 /* Options that are passed on, but have special semantics with -drive */
748 .name
= BDRV_OPT_READ_ONLY
,
749 .type
= QEMU_OPT_BOOL
,
750 .help
= "open drive file as read-only",
753 .type
= QEMU_OPT_STRING
,
754 .help
= "read error action",
757 .type
= QEMU_OPT_STRING
,
758 .help
= "write error action",
760 .name
= "copy-on-read",
761 .type
= QEMU_OPT_BOOL
,
762 .help
= "copy read data from backing file into image file",
765 { /* end of list */ }
769 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
,
774 DriveInfo
*dinfo
= NULL
;
776 QemuOpts
*legacy_opts
;
777 DriveMediaType media
= MEDIA_DISK
;
778 BlockInterfaceType type
;
779 int max_devs
, bus_id
, unit_id
, index
;
780 const char *werror
, *rerror
;
781 bool read_only
= false;
783 const char *filename
;
788 /* Change legacy command line options into QMP ones */
789 static const struct {
793 { "iops", "throttling.iops-total" },
794 { "iops_rd", "throttling.iops-read" },
795 { "iops_wr", "throttling.iops-write" },
797 { "bps", "throttling.bps-total" },
798 { "bps_rd", "throttling.bps-read" },
799 { "bps_wr", "throttling.bps-write" },
801 { "iops_max", "throttling.iops-total-max" },
802 { "iops_rd_max", "throttling.iops-read-max" },
803 { "iops_wr_max", "throttling.iops-write-max" },
805 { "bps_max", "throttling.bps-total-max" },
806 { "bps_rd_max", "throttling.bps-read-max" },
807 { "bps_wr_max", "throttling.bps-write-max" },
809 { "iops_size", "throttling.iops-size" },
811 { "group", "throttling.group" },
813 { "readonly", BDRV_OPT_READ_ONLY
},
816 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
817 if (!qemu_opt_rename(all_opts
, opt_renames
[i
].from
,
818 opt_renames
[i
].to
, errp
)) {
823 value
= qemu_opt_get(all_opts
, "cache");
828 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
829 error_setg(errp
, "invalid cache option");
833 /* Specific options take precedence */
834 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
835 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
836 !writethrough
, &error_abort
);
838 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
839 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
840 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
842 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
843 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
844 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
846 qemu_opt_unset(all_opts
, "cache");
849 /* Get a QDict for processing the options */
850 bs_opts
= qdict_new();
851 qemu_opts_to_qdict(all_opts
, bs_opts
);
853 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
855 if (!qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, errp
)) {
860 value
= qemu_opt_get(legacy_opts
, "media");
862 if (!strcmp(value
, "disk")) {
864 } else if (!strcmp(value
, "cdrom")) {
868 error_setg(errp
, "'%s' invalid media", value
);
873 /* copy-on-read is disabled with a warning for read-only devices */
874 read_only
|= qemu_opt_get_bool(legacy_opts
, BDRV_OPT_READ_ONLY
, false);
875 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
877 if (read_only
&& copy_on_read
) {
878 warn_report("disabling copy-on-read on read-only drive");
879 copy_on_read
= false;
882 qdict_put_str(bs_opts
, BDRV_OPT_READ_ONLY
, read_only
? "on" : "off");
883 qdict_put_str(bs_opts
, "copy-on-read", copy_on_read
? "on" : "off");
885 /* Controller type */
886 value
= qemu_opt_get(legacy_opts
, "if");
889 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
892 if (type
== IF_COUNT
) {
893 error_setg(errp
, "unsupported bus type '%s'", value
);
897 type
= block_default_type
;
900 /* Device address specified by bus/unit or index.
901 * If none was specified, try to find the first free one. */
902 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
903 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
904 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
906 max_devs
= if_max_devs
[type
];
909 if (bus_id
!= 0 || unit_id
!= -1) {
910 error_setg(errp
, "index cannot be used with bus and unit");
913 bus_id
= drive_index_to_bus_id(type
, index
);
914 unit_id
= drive_index_to_unit_id(type
, index
);
919 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
921 if (max_devs
&& unit_id
>= max_devs
) {
928 if (max_devs
&& unit_id
>= max_devs
) {
929 error_setg(errp
, "unit %d too big (max is %d)", unit_id
, max_devs
- 1);
933 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
934 error_setg(errp
, "drive with bus=%d, unit=%d (index=%d) exists",
935 bus_id
, unit_id
, index
);
939 /* no id supplied -> create one */
940 if (qemu_opts_id(all_opts
) == NULL
) {
942 const char *mediastr
= "";
943 if (type
== IF_IDE
|| type
== IF_SCSI
) {
944 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
947 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
950 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
953 qdict_put_str(bs_opts
, "id", new_id
);
957 /* Add virtio block device */
958 if (type
== IF_VIRTIO
) {
960 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
962 qemu_opt_set(devopts
, "driver", "virtio-blk", &error_abort
);
963 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
967 filename
= qemu_opt_get(legacy_opts
, "file");
969 /* Check werror/rerror compatibility with if=... */
970 werror
= qemu_opt_get(legacy_opts
, "werror");
971 if (werror
!= NULL
) {
972 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
974 error_setg(errp
, "werror is not supported by this bus type");
977 qdict_put_str(bs_opts
, "werror", werror
);
980 rerror
= qemu_opt_get(legacy_opts
, "rerror");
981 if (rerror
!= NULL
) {
982 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
984 error_setg(errp
, "rerror is not supported by this bus type");
987 qdict_put_str(bs_opts
, "rerror", rerror
);
990 /* Actual block device init: Functionality shared with blockdev-add */
991 blk
= blockdev_init(filename
, bs_opts
, errp
);
997 /* Create legacy DriveInfo */
998 dinfo
= g_malloc0(sizeof(*dinfo
));
999 dinfo
->opts
= all_opts
;
1002 dinfo
->bus
= bus_id
;
1003 dinfo
->unit
= unit_id
;
1005 blk_set_legacy_dinfo(blk
, dinfo
);
1012 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1019 qemu_opts_del(legacy_opts
);
1020 qobject_unref(bs_opts
);
1024 static BlockDriverState
*qmp_get_root_bs(const char *name
, Error
**errp
)
1026 BlockDriverState
*bs
;
1028 bs
= bdrv_lookup_bs(name
, name
, errp
);
1033 if (!bdrv_is_root_node(bs
)) {
1034 error_setg(errp
, "Need a root block node");
1038 if (!bdrv_is_inserted(bs
)) {
1039 error_setg(errp
, "Device has no medium");
1046 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1048 TransactionActionList list
;
1050 list
.value
= action
;
1052 qmp_transaction(&list
, NULL
, errp
);
1055 void qmp_blockdev_snapshot_sync(const char *device
, const char *node_name
,
1056 const char *snapshot_file
,
1057 const char *snapshot_node_name
,
1059 bool has_mode
, NewImageMode mode
, Error
**errp
)
1061 BlockdevSnapshotSync snapshot
= {
1062 .device
= (char *) device
,
1063 .node_name
= (char *) node_name
,
1064 .snapshot_file
= (char *) snapshot_file
,
1065 .snapshot_node_name
= (char *) snapshot_node_name
,
1066 .format
= (char *) format
,
1067 .has_mode
= has_mode
,
1070 TransactionAction action
= {
1071 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1072 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1074 blockdev_do_action(&action
, errp
);
1077 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1080 BlockdevSnapshot snapshot_data
= {
1081 .node
= (char *) node
,
1082 .overlay
= (char *) overlay
1084 TransactionAction action
= {
1085 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1086 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1088 blockdev_do_action(&action
, errp
);
1091 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1095 BlockdevSnapshotInternal snapshot
= {
1096 .device
= (char *) device
,
1097 .name
= (char *) name
1099 TransactionAction action
= {
1100 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1101 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1103 blockdev_do_action(&action
, errp
);
1106 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1111 BlockDriverState
*bs
;
1112 AioContext
*aio_context
;
1113 QEMUSnapshotInfo sn
;
1114 Error
*local_err
= NULL
;
1115 SnapshotInfo
*info
= NULL
;
1118 bs
= qmp_get_root_bs(device
, errp
);
1122 aio_context
= bdrv_get_aio_context(bs
);
1123 aio_context_acquire(aio_context
);
1126 error_setg(errp
, "Name or id must be provided");
1127 goto out_aio_context
;
1130 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1131 goto out_aio_context
;
1134 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1136 error_propagate(errp
, local_err
);
1137 goto out_aio_context
;
1141 "Snapshot with id '%s' and name '%s' does not exist on "
1143 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1144 goto out_aio_context
;
1147 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1149 error_propagate(errp
, local_err
);
1150 goto out_aio_context
;
1153 aio_context_release(aio_context
);
1155 info
= g_new0(SnapshotInfo
, 1);
1156 info
->id
= g_strdup(sn
.id_str
);
1157 info
->name
= g_strdup(sn
.name
);
1158 info
->date_nsec
= sn
.date_nsec
;
1159 info
->date_sec
= sn
.date_sec
;
1160 info
->vm_state_size
= sn
.vm_state_size
;
1161 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1162 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1163 if (sn
.icount
!= -1ULL) {
1164 info
->icount
= sn
.icount
;
1165 info
->has_icount
= true;
1171 aio_context_release(aio_context
);
1175 /* New and old BlockDriverState structs for atomic group operations */
1177 typedef struct BlkActionState BlkActionState
;
1181 * Table of operations that define an Action.
1183 * @instance_size: Size of state struct, in bytes.
1184 * @prepare: Prepare the work, must NOT be NULL.
1185 * @commit: Commit the changes, can be NULL.
1186 * @abort: Abort the changes on fail, can be NULL.
1187 * @clean: Clean up resources after all transaction actions have called
1188 * commit() or abort(). Can be NULL.
1190 * Only prepare() may fail. In a single transaction, only one of commit() or
1191 * abort() will be called. clean() will always be called if it is present.
1193 * Always run under BQL.
1195 typedef struct BlkActionOps
{
1196 size_t instance_size
;
1197 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1198 void (*commit
)(BlkActionState
*common
);
1199 void (*abort
)(BlkActionState
*common
);
1200 void (*clean
)(BlkActionState
*common
);
1205 * Describes one Action's state within a Transaction.
1207 * @action: QAPI-defined enum identifying which Action to perform.
1208 * @ops: Table of ActionOps this Action can perform.
1209 * @block_job_txn: Transaction which this action belongs to.
1210 * @entry: List membership for all Actions in this Transaction.
1212 * This structure must be arranged as first member in a subclassed type,
1213 * assuming that the compiler will also arrange it to the same offsets as the
1216 struct BlkActionState
{
1217 TransactionAction
*action
;
1218 const BlkActionOps
*ops
;
1219 JobTxn
*block_job_txn
;
1220 TransactionProperties
*txn_props
;
1221 QTAILQ_ENTRY(BlkActionState
) entry
;
1224 /* internal snapshot private data */
1225 typedef struct InternalSnapshotState
{
1226 BlkActionState common
;
1227 BlockDriverState
*bs
;
1228 QEMUSnapshotInfo sn
;
1230 } InternalSnapshotState
;
1233 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1235 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1237 "Action '%s' does not support Transaction property "
1238 "completion-mode = %s",
1239 TransactionActionKind_str(s
->action
->type
),
1240 ActionCompletionMode_str(s
->txn_props
->completion_mode
));
1246 static void internal_snapshot_prepare(BlkActionState
*common
,
1249 Error
*local_err
= NULL
;
1252 BlockDriverState
*bs
;
1253 QEMUSnapshotInfo old_sn
, *sn
;
1256 BlockdevSnapshotInternal
*internal
;
1257 InternalSnapshotState
*state
;
1258 AioContext
*aio_context
;
1261 g_assert(common
->action
->type
==
1262 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1263 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1264 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1266 /* 1. parse input */
1267 device
= internal
->device
;
1268 name
= internal
->name
;
1270 /* 2. check for validation */
1271 if (action_check_completion_mode(common
, errp
) < 0) {
1275 bs
= qmp_get_root_bs(device
, errp
);
1280 aio_context
= bdrv_get_aio_context(bs
);
1281 aio_context_acquire(aio_context
);
1285 /* Paired with .clean() */
1286 bdrv_drained_begin(bs
);
1288 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1292 if (bdrv_is_read_only(bs
)) {
1293 error_setg(errp
, "Device '%s' is read only", device
);
1297 if (!bdrv_can_snapshot(bs
)) {
1298 error_setg(errp
, "Block format '%s' used by device '%s' "
1299 "does not support internal snapshots",
1300 bs
->drv
->format_name
, device
);
1304 if (!strlen(name
)) {
1305 error_setg(errp
, "Name is empty");
1309 /* check whether a snapshot with name exist */
1310 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1313 error_propagate(errp
, local_err
);
1317 "Snapshot with name '%s' already exists on device '%s'",
1322 /* 3. take the snapshot */
1324 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1325 rt
= g_get_real_time();
1326 sn
->date_sec
= rt
/ G_USEC_PER_SEC
;
1327 sn
->date_nsec
= (rt
% G_USEC_PER_SEC
) * 1000;
1328 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1329 if (replay_mode
!= REPLAY_MODE_NONE
) {
1330 sn
->icount
= replay_get_current_icount();
1335 ret1
= bdrv_snapshot_create(bs
, sn
);
1337 error_setg_errno(errp
, -ret1
,
1338 "Failed to create snapshot '%s' on device '%s'",
1343 /* 4. succeed, mark a snapshot is created */
1344 state
->created
= true;
1347 aio_context_release(aio_context
);
1350 static void internal_snapshot_abort(BlkActionState
*common
)
1352 InternalSnapshotState
*state
=
1353 DO_UPCAST(InternalSnapshotState
, common
, common
);
1354 BlockDriverState
*bs
= state
->bs
;
1355 QEMUSnapshotInfo
*sn
= &state
->sn
;
1356 AioContext
*aio_context
;
1357 Error
*local_error
= NULL
;
1359 if (!state
->created
) {
1363 aio_context
= bdrv_get_aio_context(state
->bs
);
1364 aio_context_acquire(aio_context
);
1366 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1367 error_reportf_err(local_error
,
1368 "Failed to delete snapshot with id '%s' and "
1369 "name '%s' on device '%s' in abort: ",
1370 sn
->id_str
, sn
->name
,
1371 bdrv_get_device_name(bs
));
1374 aio_context_release(aio_context
);
1377 static void internal_snapshot_clean(BlkActionState
*common
)
1379 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1381 AioContext
*aio_context
;
1387 aio_context
= bdrv_get_aio_context(state
->bs
);
1388 aio_context_acquire(aio_context
);
1390 bdrv_drained_end(state
->bs
);
1392 aio_context_release(aio_context
);
1395 /* external snapshot private data */
1396 typedef struct ExternalSnapshotState
{
1397 BlkActionState common
;
1398 BlockDriverState
*old_bs
;
1399 BlockDriverState
*new_bs
;
1400 bool overlay_appended
;
1401 } ExternalSnapshotState
;
1403 static void external_snapshot_prepare(BlkActionState
*common
,
1408 QDict
*options
= NULL
;
1409 Error
*local_err
= NULL
;
1410 /* Device and node name of the image to generate the snapshot from */
1412 const char *node_name
;
1413 /* Reference to the new image (for 'blockdev-snapshot') */
1414 const char *snapshot_ref
;
1415 /* File name of the new image (for 'blockdev-snapshot-sync') */
1416 const char *new_image_file
;
1417 ExternalSnapshotState
*state
=
1418 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1419 TransactionAction
*action
= common
->action
;
1420 AioContext
*aio_context
;
1421 uint64_t perm
, shared
;
1423 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1424 * purpose but a different set of parameters */
1425 switch (action
->type
) {
1426 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1428 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1430 node_name
= s
->node
;
1431 new_image_file
= NULL
;
1432 snapshot_ref
= s
->overlay
;
1435 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1437 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1439 node_name
= s
->node_name
;
1440 new_image_file
= s
->snapshot_file
;
1441 snapshot_ref
= NULL
;
1445 g_assert_not_reached();
1448 /* start processing */
1449 if (action_check_completion_mode(common
, errp
) < 0) {
1453 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1454 if (!state
->old_bs
) {
1458 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1459 aio_context_acquire(aio_context
);
1461 /* Paired with .clean() */
1462 bdrv_drained_begin(state
->old_bs
);
1464 if (!bdrv_is_inserted(state
->old_bs
)) {
1465 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1469 if (bdrv_op_is_blocked(state
->old_bs
,
1470 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1474 if (!bdrv_is_read_only(state
->old_bs
)) {
1475 if (bdrv_flush(state
->old_bs
)) {
1476 error_setg(errp
, QERR_IO_ERROR
);
1481 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1482 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1483 const char *format
= s
->format
?: "qcow2";
1484 enum NewImageMode mode
;
1485 const char *snapshot_node_name
= s
->snapshot_node_name
;
1487 if (node_name
&& !snapshot_node_name
) {
1488 error_setg(errp
, "New overlay node-name missing");
1492 if (snapshot_node_name
&&
1493 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1494 error_setg(errp
, "New overlay node-name already in use");
1498 flags
= state
->old_bs
->open_flags
;
1499 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_COPY_ON_READ
);
1500 flags
|= BDRV_O_NO_BACKING
;
1502 /* create new image w/backing file */
1503 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1504 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1505 int64_t size
= bdrv_getlength(state
->old_bs
);
1507 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1510 bdrv_refresh_filename(state
->old_bs
);
1512 aio_context_release(aio_context
);
1513 bdrv_img_create(new_image_file
, format
,
1514 state
->old_bs
->filename
,
1515 state
->old_bs
->drv
->format_name
,
1516 NULL
, size
, flags
, false, &local_err
);
1517 aio_context_acquire(aio_context
);
1520 error_propagate(errp
, local_err
);
1525 options
= qdict_new();
1526 if (snapshot_node_name
) {
1527 qdict_put_str(options
, "node-name", snapshot_node_name
);
1529 qdict_put_str(options
, "driver", format
);
1532 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1534 /* We will manually add the backing_hd field to the bs later */
1535 if (!state
->new_bs
) {
1540 * Allow attaching a backing file to an overlay that's already in use only
1541 * if the parents don't assume that they are already seeing a valid image.
1542 * (Specifically, allow it as a mirror target, which is write-only access.)
1544 bdrv_get_cumulative_perm(state
->new_bs
, &perm
, &shared
);
1545 if (perm
& BLK_PERM_CONSISTENT_READ
) {
1546 error_setg(errp
, "The overlay is already in use");
1550 if (state
->new_bs
->drv
->is_filter
) {
1551 error_setg(errp
, "Filters cannot be used as overlays");
1555 if (bdrv_cow_child(state
->new_bs
)) {
1556 error_setg(errp
, "The overlay already has a backing image");
1560 if (!state
->new_bs
->drv
->supports_backing
) {
1561 error_setg(errp
, "The overlay does not support backing images");
1565 ret
= bdrv_append(state
->new_bs
, state
->old_bs
, errp
);
1569 state
->overlay_appended
= true;
1572 aio_context_release(aio_context
);
1575 static void external_snapshot_commit(BlkActionState
*common
)
1577 ExternalSnapshotState
*state
=
1578 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1579 AioContext
*aio_context
;
1581 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1582 aio_context_acquire(aio_context
);
1584 /* We don't need (or want) to use the transactional
1585 * bdrv_reopen_multiple() across all the entries at once, because we
1586 * don't want to abort all of them if one of them fails the reopen */
1587 if (!qatomic_read(&state
->old_bs
->copy_on_read
)) {
1588 bdrv_reopen_set_read_only(state
->old_bs
, true, NULL
);
1591 aio_context_release(aio_context
);
1594 static void external_snapshot_abort(BlkActionState
*common
)
1596 ExternalSnapshotState
*state
=
1597 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1598 if (state
->new_bs
) {
1599 if (state
->overlay_appended
) {
1600 AioContext
*aio_context
;
1601 AioContext
*tmp_context
;
1604 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1605 aio_context_acquire(aio_context
);
1607 bdrv_ref(state
->old_bs
); /* we can't let bdrv_set_backind_hd()
1608 close state->old_bs; we need it */
1609 bdrv_set_backing_hd(state
->new_bs
, NULL
, &error_abort
);
1612 * The call to bdrv_set_backing_hd() above returns state->old_bs to
1613 * the main AioContext. As we're still going to be using it, return
1614 * it to the AioContext it was before.
1616 tmp_context
= bdrv_get_aio_context(state
->old_bs
);
1617 if (aio_context
!= tmp_context
) {
1618 aio_context_release(aio_context
);
1619 aio_context_acquire(tmp_context
);
1621 ret
= bdrv_try_change_aio_context(state
->old_bs
,
1622 aio_context
, NULL
, NULL
);
1625 aio_context_release(tmp_context
);
1626 aio_context_acquire(aio_context
);
1629 bdrv_replace_node(state
->new_bs
, state
->old_bs
, &error_abort
);
1630 bdrv_unref(state
->old_bs
); /* bdrv_replace_node() ref'ed old_bs */
1632 aio_context_release(aio_context
);
1637 static void external_snapshot_clean(BlkActionState
*common
)
1639 ExternalSnapshotState
*state
=
1640 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1641 AioContext
*aio_context
;
1643 if (!state
->old_bs
) {
1647 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1648 aio_context_acquire(aio_context
);
1650 bdrv_drained_end(state
->old_bs
);
1651 bdrv_unref(state
->new_bs
);
1653 aio_context_release(aio_context
);
1656 typedef struct DriveBackupState
{
1657 BlkActionState common
;
1658 BlockDriverState
*bs
;
1662 static BlockJob
*do_backup_common(BackupCommon
*backup
,
1663 BlockDriverState
*bs
,
1664 BlockDriverState
*target_bs
,
1665 AioContext
*aio_context
,
1666 JobTxn
*txn
, Error
**errp
);
1668 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1670 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1671 DriveBackup
*backup
;
1672 BlockDriverState
*bs
;
1673 BlockDriverState
*target_bs
;
1674 BlockDriverState
*source
= NULL
;
1675 AioContext
*aio_context
;
1676 AioContext
*old_context
;
1679 Error
*local_err
= NULL
;
1682 bool set_backing_hd
= false;
1685 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1686 backup
= common
->action
->u
.drive_backup
.data
;
1688 if (!backup
->has_mode
) {
1689 backup
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1692 bs
= bdrv_lookup_bs(backup
->device
, backup
->device
, errp
);
1698 error_setg(errp
, "Device has no medium");
1702 aio_context
= bdrv_get_aio_context(bs
);
1703 aio_context_acquire(aio_context
);
1706 /* Paired with .clean() */
1707 bdrv_drained_begin(bs
);
1709 format
= backup
->format
;
1710 if (!format
&& backup
->mode
!= NEW_IMAGE_MODE_EXISTING
) {
1711 format
= bs
->drv
->format_name
;
1714 /* Early check to avoid creating target */
1715 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
1719 flags
= bs
->open_flags
| BDRV_O_RDWR
;
1722 * See if we have a backing HD we can use to create our new image
1725 if (backup
->sync
== MIRROR_SYNC_MODE_TOP
) {
1727 * Backup will not replace the source by the target, so none
1728 * of the filters skipped here will be removed (in contrast to
1729 * mirror). Therefore, we can skip all of them when looking
1730 * for the first COW relationship.
1732 source
= bdrv_cow_bs(bdrv_skip_filters(bs
));
1734 backup
->sync
= MIRROR_SYNC_MODE_FULL
;
1737 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
1739 flags
|= BDRV_O_NO_BACKING
;
1740 set_backing_hd
= true;
1743 size
= bdrv_getlength(bs
);
1745 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1749 if (backup
->mode
!= NEW_IMAGE_MODE_EXISTING
) {
1752 /* Implicit filters should not appear in the filename */
1753 BlockDriverState
*explicit_backing
=
1754 bdrv_skip_implicit_filters(source
);
1756 bdrv_refresh_filename(explicit_backing
);
1757 bdrv_img_create(backup
->target
, format
,
1758 explicit_backing
->filename
,
1759 explicit_backing
->drv
->format_name
, NULL
,
1760 size
, flags
, false, &local_err
);
1762 bdrv_img_create(backup
->target
, format
, NULL
, NULL
, NULL
,
1763 size
, flags
, false, &local_err
);
1768 error_propagate(errp
, local_err
);
1772 options
= qdict_new();
1773 qdict_put_str(options
, "discard", "unmap");
1774 qdict_put_str(options
, "detect-zeroes", "unmap");
1776 qdict_put_str(options
, "driver", format
);
1779 target_bs
= bdrv_open(backup
->target
, NULL
, options
, flags
, errp
);
1784 /* Honor bdrv_try_change_aio_context() context acquisition requirements. */
1785 old_context
= bdrv_get_aio_context(target_bs
);
1786 aio_context_release(aio_context
);
1787 aio_context_acquire(old_context
);
1789 ret
= bdrv_try_change_aio_context(target_bs
, aio_context
, NULL
, errp
);
1791 bdrv_unref(target_bs
);
1792 aio_context_release(old_context
);
1796 aio_context_release(old_context
);
1797 aio_context_acquire(aio_context
);
1799 if (set_backing_hd
) {
1800 if (bdrv_set_backing_hd(target_bs
, source
, errp
) < 0) {
1805 state
->job
= do_backup_common(qapi_DriveBackup_base(backup
),
1806 bs
, target_bs
, aio_context
,
1807 common
->block_job_txn
, errp
);
1810 bdrv_unref(target_bs
);
1812 aio_context_release(aio_context
);
1815 static void drive_backup_commit(BlkActionState
*common
)
1817 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1818 AioContext
*aio_context
;
1820 aio_context
= bdrv_get_aio_context(state
->bs
);
1821 aio_context_acquire(aio_context
);
1824 job_start(&state
->job
->job
);
1826 aio_context_release(aio_context
);
1829 static void drive_backup_abort(BlkActionState
*common
)
1831 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1834 job_cancel_sync(&state
->job
->job
, true);
1838 static void drive_backup_clean(BlkActionState
*common
)
1840 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1841 AioContext
*aio_context
;
1847 aio_context
= bdrv_get_aio_context(state
->bs
);
1848 aio_context_acquire(aio_context
);
1850 bdrv_drained_end(state
->bs
);
1852 aio_context_release(aio_context
);
1855 typedef struct BlockdevBackupState
{
1856 BlkActionState common
;
1857 BlockDriverState
*bs
;
1859 } BlockdevBackupState
;
1861 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1863 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1864 BlockdevBackup
*backup
;
1865 BlockDriverState
*bs
;
1866 BlockDriverState
*target_bs
;
1867 AioContext
*aio_context
;
1868 AioContext
*old_context
;
1871 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1872 backup
= common
->action
->u
.blockdev_backup
.data
;
1874 bs
= bdrv_lookup_bs(backup
->device
, backup
->device
, errp
);
1879 target_bs
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
1884 /* Honor bdrv_try_change_aio_context() context acquisition requirements. */
1885 aio_context
= bdrv_get_aio_context(bs
);
1886 old_context
= bdrv_get_aio_context(target_bs
);
1887 aio_context_acquire(old_context
);
1889 ret
= bdrv_try_change_aio_context(target_bs
, aio_context
, NULL
, errp
);
1891 aio_context_release(old_context
);
1895 aio_context_release(old_context
);
1896 aio_context_acquire(aio_context
);
1899 /* Paired with .clean() */
1900 bdrv_drained_begin(state
->bs
);
1902 state
->job
= do_backup_common(qapi_BlockdevBackup_base(backup
),
1903 bs
, target_bs
, aio_context
,
1904 common
->block_job_txn
, errp
);
1906 aio_context_release(aio_context
);
1909 static void blockdev_backup_commit(BlkActionState
*common
)
1911 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1912 AioContext
*aio_context
;
1914 aio_context
= bdrv_get_aio_context(state
->bs
);
1915 aio_context_acquire(aio_context
);
1918 job_start(&state
->job
->job
);
1920 aio_context_release(aio_context
);
1923 static void blockdev_backup_abort(BlkActionState
*common
)
1925 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1928 job_cancel_sync(&state
->job
->job
, true);
1932 static void blockdev_backup_clean(BlkActionState
*common
)
1934 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1935 AioContext
*aio_context
;
1941 aio_context
= bdrv_get_aio_context(state
->bs
);
1942 aio_context_acquire(aio_context
);
1944 bdrv_drained_end(state
->bs
);
1946 aio_context_release(aio_context
);
1949 typedef struct BlockDirtyBitmapState
{
1950 BlkActionState common
;
1951 BdrvDirtyBitmap
*bitmap
;
1952 BlockDriverState
*bs
;
1956 } BlockDirtyBitmapState
;
1958 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
1961 Error
*local_err
= NULL
;
1962 BlockDirtyBitmapAdd
*action
;
1963 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1966 if (action_check_completion_mode(common
, errp
) < 0) {
1970 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
1971 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1972 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
1973 action
->has_granularity
, action
->granularity
,
1974 action
->has_persistent
, action
->persistent
,
1975 action
->has_disabled
, action
->disabled
,
1979 state
->prepared
= true;
1981 error_propagate(errp
, local_err
);
1985 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
1987 BlockDirtyBitmapAdd
*action
;
1988 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1991 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
1992 /* Should not be able to fail: IF the bitmap was added via .prepare(),
1993 * then the node reference and bitmap name must have been valid.
1995 if (state
->prepared
) {
1996 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2000 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2003 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2005 BlockDirtyBitmap
*action
;
2007 if (action_check_completion_mode(common
, errp
) < 0) {
2011 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2012 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2016 if (!state
->bitmap
) {
2020 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_DEFAULT
, errp
)) {
2024 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2027 static void block_dirty_bitmap_restore(BlkActionState
*common
)
2029 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2032 if (state
->backup
) {
2033 bdrv_restore_dirty_bitmap(state
->bitmap
, state
->backup
);
2037 static void block_dirty_bitmap_free_backup(BlkActionState
*common
)
2039 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2042 hbitmap_free(state
->backup
);
2045 static void block_dirty_bitmap_enable_prepare(BlkActionState
*common
,
2048 BlockDirtyBitmap
*action
;
2049 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2052 if (action_check_completion_mode(common
, errp
) < 0) {
2056 action
= common
->action
->u
.block_dirty_bitmap_enable
.data
;
2057 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2061 if (!state
->bitmap
) {
2065 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2069 state
->was_enabled
= bdrv_dirty_bitmap_enabled(state
->bitmap
);
2070 bdrv_enable_dirty_bitmap(state
->bitmap
);
2073 static void block_dirty_bitmap_enable_abort(BlkActionState
*common
)
2075 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2078 if (!state
->was_enabled
) {
2079 bdrv_disable_dirty_bitmap(state
->bitmap
);
2083 static void block_dirty_bitmap_disable_prepare(BlkActionState
*common
,
2086 BlockDirtyBitmap
*action
;
2087 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2090 if (action_check_completion_mode(common
, errp
) < 0) {
2094 action
= common
->action
->u
.block_dirty_bitmap_disable
.data
;
2095 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2099 if (!state
->bitmap
) {
2103 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2107 state
->was_enabled
= bdrv_dirty_bitmap_enabled(state
->bitmap
);
2108 bdrv_disable_dirty_bitmap(state
->bitmap
);
2111 static void block_dirty_bitmap_disable_abort(BlkActionState
*common
)
2113 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2116 if (state
->was_enabled
) {
2117 bdrv_enable_dirty_bitmap(state
->bitmap
);
2121 static void block_dirty_bitmap_merge_prepare(BlkActionState
*common
,
2124 BlockDirtyBitmapMerge
*action
;
2125 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2128 if (action_check_completion_mode(common
, errp
) < 0) {
2132 action
= common
->action
->u
.block_dirty_bitmap_merge
.data
;
2134 state
->bitmap
= block_dirty_bitmap_merge(action
->node
, action
->target
,
2135 action
->bitmaps
, &state
->backup
,
2139 static void block_dirty_bitmap_remove_prepare(BlkActionState
*common
,
2142 BlockDirtyBitmap
*action
;
2143 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2146 if (action_check_completion_mode(common
, errp
) < 0) {
2150 action
= common
->action
->u
.block_dirty_bitmap_remove
.data
;
2152 state
->bitmap
= block_dirty_bitmap_remove(action
->node
, action
->name
,
2153 false, &state
->bs
, errp
);
2154 if (state
->bitmap
) {
2155 bdrv_dirty_bitmap_skip_store(state
->bitmap
, true);
2156 bdrv_dirty_bitmap_set_busy(state
->bitmap
, true);
2160 static void block_dirty_bitmap_remove_abort(BlkActionState
*common
)
2162 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2165 if (state
->bitmap
) {
2166 bdrv_dirty_bitmap_skip_store(state
->bitmap
, false);
2167 bdrv_dirty_bitmap_set_busy(state
->bitmap
, false);
2171 static void block_dirty_bitmap_remove_commit(BlkActionState
*common
)
2173 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2176 bdrv_dirty_bitmap_set_busy(state
->bitmap
, false);
2177 bdrv_release_dirty_bitmap(state
->bitmap
);
2180 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2182 error_setg(errp
, "Transaction aborted using Abort action");
2185 static void abort_commit(BlkActionState
*common
)
2187 g_assert_not_reached(); /* this action never succeeds */
2190 static const BlkActionOps actions
[] = {
2191 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2192 .instance_size
= sizeof(ExternalSnapshotState
),
2193 .prepare
= external_snapshot_prepare
,
2194 .commit
= external_snapshot_commit
,
2195 .abort
= external_snapshot_abort
,
2196 .clean
= external_snapshot_clean
,
2198 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2199 .instance_size
= sizeof(ExternalSnapshotState
),
2200 .prepare
= external_snapshot_prepare
,
2201 .commit
= external_snapshot_commit
,
2202 .abort
= external_snapshot_abort
,
2203 .clean
= external_snapshot_clean
,
2205 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2206 .instance_size
= sizeof(DriveBackupState
),
2207 .prepare
= drive_backup_prepare
,
2208 .commit
= drive_backup_commit
,
2209 .abort
= drive_backup_abort
,
2210 .clean
= drive_backup_clean
,
2212 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2213 .instance_size
= sizeof(BlockdevBackupState
),
2214 .prepare
= blockdev_backup_prepare
,
2215 .commit
= blockdev_backup_commit
,
2216 .abort
= blockdev_backup_abort
,
2217 .clean
= blockdev_backup_clean
,
2219 [TRANSACTION_ACTION_KIND_ABORT
] = {
2220 .instance_size
= sizeof(BlkActionState
),
2221 .prepare
= abort_prepare
,
2222 .commit
= abort_commit
,
2224 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2225 .instance_size
= sizeof(InternalSnapshotState
),
2226 .prepare
= internal_snapshot_prepare
,
2227 .abort
= internal_snapshot_abort
,
2228 .clean
= internal_snapshot_clean
,
2230 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2231 .instance_size
= sizeof(BlockDirtyBitmapState
),
2232 .prepare
= block_dirty_bitmap_add_prepare
,
2233 .abort
= block_dirty_bitmap_add_abort
,
2235 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2236 .instance_size
= sizeof(BlockDirtyBitmapState
),
2237 .prepare
= block_dirty_bitmap_clear_prepare
,
2238 .commit
= block_dirty_bitmap_free_backup
,
2239 .abort
= block_dirty_bitmap_restore
,
2241 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE
] = {
2242 .instance_size
= sizeof(BlockDirtyBitmapState
),
2243 .prepare
= block_dirty_bitmap_enable_prepare
,
2244 .abort
= block_dirty_bitmap_enable_abort
,
2246 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE
] = {
2247 .instance_size
= sizeof(BlockDirtyBitmapState
),
2248 .prepare
= block_dirty_bitmap_disable_prepare
,
2249 .abort
= block_dirty_bitmap_disable_abort
,
2251 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE
] = {
2252 .instance_size
= sizeof(BlockDirtyBitmapState
),
2253 .prepare
= block_dirty_bitmap_merge_prepare
,
2254 .commit
= block_dirty_bitmap_free_backup
,
2255 .abort
= block_dirty_bitmap_restore
,
2257 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE
] = {
2258 .instance_size
= sizeof(BlockDirtyBitmapState
),
2259 .prepare
= block_dirty_bitmap_remove_prepare
,
2260 .commit
= block_dirty_bitmap_remove_commit
,
2261 .abort
= block_dirty_bitmap_remove_abort
,
2263 /* Where are transactions for MIRROR, COMMIT and STREAM?
2264 * Although these blockjobs use transaction callbacks like the backup job,
2265 * these jobs do not necessarily adhere to transaction semantics.
2266 * These jobs may not fully undo all of their actions on abort, nor do they
2267 * necessarily work in transactions with more than one job in them.
2272 * Allocate a TransactionProperties structure if necessary, and fill
2273 * that structure with desired defaults if they are unset.
2275 static TransactionProperties
*get_transaction_properties(
2276 TransactionProperties
*props
)
2279 props
= g_new0(TransactionProperties
, 1);
2282 if (!props
->has_completion_mode
) {
2283 props
->has_completion_mode
= true;
2284 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2291 * 'Atomic' group operations. The operations are performed as a set, and if
2292 * any fail then we roll back all operations in the group.
2294 * Always run under BQL.
2296 void qmp_transaction(TransactionActionList
*dev_list
,
2297 struct TransactionProperties
*props
,
2300 TransactionActionList
*dev_entry
= dev_list
;
2301 bool has_props
= !!props
;
2302 JobTxn
*block_job_txn
= NULL
;
2303 BlkActionState
*state
, *next
;
2304 Error
*local_err
= NULL
;
2306 GLOBAL_STATE_CODE();
2308 QTAILQ_HEAD(, BlkActionState
) snap_bdrv_states
;
2309 QTAILQ_INIT(&snap_bdrv_states
);
2311 /* Does this transaction get canceled as a group on failure?
2312 * If not, we don't really need to make a JobTxn.
2314 props
= get_transaction_properties(props
);
2315 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2316 block_job_txn
= job_txn_new();
2319 /* drain all i/o before any operations */
2322 /* We don't do anything in this loop that commits us to the operations */
2323 while (NULL
!= dev_entry
) {
2324 TransactionAction
*dev_info
= NULL
;
2325 const BlkActionOps
*ops
;
2327 dev_info
= dev_entry
->value
;
2328 dev_entry
= dev_entry
->next
;
2330 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2332 ops
= &actions
[dev_info
->type
];
2333 assert(ops
->instance_size
> 0);
2335 state
= g_malloc0(ops
->instance_size
);
2337 state
->action
= dev_info
;
2338 state
->block_job_txn
= block_job_txn
;
2339 state
->txn_props
= props
;
2340 QTAILQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2342 state
->ops
->prepare(state
, &local_err
);
2344 error_propagate(errp
, local_err
);
2345 goto delete_and_fail
;
2349 QTAILQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2350 if (state
->ops
->commit
) {
2351 state
->ops
->commit(state
);
2359 /* failure, and it is all-or-none; roll back all operations */
2360 QTAILQ_FOREACH_REVERSE(state
, &snap_bdrv_states
, entry
) {
2361 if (state
->ops
->abort
) {
2362 state
->ops
->abort(state
);
2366 QTAILQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2367 if (state
->ops
->clean
) {
2368 state
->ops
->clean(state
);
2373 qapi_free_TransactionProperties(props
);
2375 job_txn_unref(block_job_txn
);
2378 BlockDirtyBitmapSha256
*qmp_x_debug_block_dirty_bitmap_sha256(const char *node
,
2382 BdrvDirtyBitmap
*bitmap
;
2383 BlockDriverState
*bs
;
2384 BlockDirtyBitmapSha256
*ret
= NULL
;
2387 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2388 if (!bitmap
|| !bs
) {
2392 sha256
= bdrv_dirty_bitmap_sha256(bitmap
, errp
);
2393 if (sha256
== NULL
) {
2397 ret
= g_new(BlockDirtyBitmapSha256
, 1);
2398 ret
->sha256
= sha256
;
2403 void coroutine_fn
qmp_block_resize(const char *device
, const char *node_name
,
2404 int64_t size
, Error
**errp
)
2406 Error
*local_err
= NULL
;
2408 BlockDriverState
*bs
;
2409 AioContext
*old_ctx
;
2411 bs
= bdrv_lookup_bs(device
, node_name
, &local_err
);
2413 error_propagate(errp
, local_err
);
2418 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2422 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2423 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2427 blk
= blk_new_with_bs(bs
, BLK_PERM_RESIZE
, BLK_PERM_ALL
, errp
);
2433 bdrv_drained_begin(bs
);
2436 old_ctx
= bdrv_co_enter(bs
);
2437 blk_co_truncate(blk
, size
, false, PREALLOC_MODE_OFF
, 0, errp
);
2438 bdrv_co_leave(bs
, old_ctx
);
2441 bdrv_drained_end(bs
);
2446 void qmp_block_stream(const char *job_id
, const char *device
,
2448 const char *base_node
,
2449 const char *backing_file
,
2451 bool has_speed
, int64_t speed
,
2452 bool has_on_error
, BlockdevOnError on_error
,
2453 const char *filter_node_name
,
2454 bool has_auto_finalize
, bool auto_finalize
,
2455 bool has_auto_dismiss
, bool auto_dismiss
,
2458 BlockDriverState
*bs
, *iter
, *iter_end
;
2459 BlockDriverState
*base_bs
= NULL
;
2460 BlockDriverState
*bottom_bs
= NULL
;
2461 AioContext
*aio_context
;
2462 Error
*local_err
= NULL
;
2463 int job_flags
= JOB_DEFAULT
;
2465 if (base
&& base_node
) {
2466 error_setg(errp
, "'base' and 'base-node' cannot be specified "
2467 "at the same time");
2471 if (base
&& bottom
) {
2472 error_setg(errp
, "'base' and 'bottom' cannot be specified "
2473 "at the same time");
2477 if (bottom
&& base_node
) {
2478 error_setg(errp
, "'bottom' and 'base-node' cannot be specified "
2479 "at the same time");
2483 if (!has_on_error
) {
2484 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2487 bs
= bdrv_lookup_bs(device
, device
, errp
);
2492 aio_context
= bdrv_get_aio_context(bs
);
2493 aio_context_acquire(aio_context
);
2496 base_bs
= bdrv_find_backing_image(bs
, base
);
2497 if (base_bs
== NULL
) {
2498 error_setg(errp
, "Can't find '%s' in the backing chain", base
);
2501 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2505 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
2509 if (bs
== base_bs
|| !bdrv_chain_contains(bs
, base_bs
)) {
2510 error_setg(errp
, "Node '%s' is not a backing image of '%s'",
2514 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2515 bdrv_refresh_filename(base_bs
);
2519 bottom_bs
= bdrv_lookup_bs(NULL
, bottom
, errp
);
2523 if (!bottom_bs
->drv
) {
2524 error_setg(errp
, "Node '%s' is not open", bottom
);
2527 if (bottom_bs
->drv
->is_filter
) {
2528 error_setg(errp
, "Node '%s' is a filter, use a non-filter node "
2529 "as 'bottom'", bottom
);
2532 if (!bdrv_chain_contains(bs
, bottom_bs
)) {
2533 error_setg(errp
, "Node '%s' is not in a chain starting from '%s'",
2537 assert(bdrv_get_aio_context(bottom_bs
) == aio_context
);
2541 * Check for op blockers in the whole chain between bs and base (or bottom)
2543 iter_end
= bottom
? bdrv_filter_or_cow_bs(bottom_bs
) : base_bs
;
2544 for (iter
= bs
; iter
&& iter
!= iter_end
;
2545 iter
= bdrv_filter_or_cow_bs(iter
))
2547 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2552 /* if we are streaming the entire chain, the result will have no backing
2553 * file, and specifying one is therefore an error */
2554 if (!base_bs
&& backing_file
) {
2555 error_setg(errp
, "backing file specified, but streaming the "
2560 if (has_auto_finalize
&& !auto_finalize
) {
2561 job_flags
|= JOB_MANUAL_FINALIZE
;
2563 if (has_auto_dismiss
&& !auto_dismiss
) {
2564 job_flags
|= JOB_MANUAL_DISMISS
;
2567 stream_start(job_id
, bs
, base_bs
, backing_file
,
2568 bottom_bs
, job_flags
, has_speed
? speed
: 0, on_error
,
2569 filter_node_name
, &local_err
);
2571 error_propagate(errp
, local_err
);
2575 trace_qmp_block_stream(bs
);
2578 aio_context_release(aio_context
);
2581 void qmp_block_commit(const char *job_id
, const char *device
,
2582 const char *base_node
,
2584 const char *top_node
,
2586 const char *backing_file
,
2587 bool has_speed
, int64_t speed
,
2588 bool has_on_error
, BlockdevOnError on_error
,
2589 const char *filter_node_name
,
2590 bool has_auto_finalize
, bool auto_finalize
,
2591 bool has_auto_dismiss
, bool auto_dismiss
,
2594 BlockDriverState
*bs
;
2595 BlockDriverState
*iter
;
2596 BlockDriverState
*base_bs
, *top_bs
;
2597 AioContext
*aio_context
;
2598 Error
*local_err
= NULL
;
2599 int job_flags
= JOB_DEFAULT
;
2600 uint64_t top_perm
, top_shared
;
2605 if (!has_on_error
) {
2606 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2608 if (has_auto_finalize
&& !auto_finalize
) {
2609 job_flags
|= JOB_MANUAL_FINALIZE
;
2611 if (has_auto_dismiss
&& !auto_dismiss
) {
2612 job_flags
|= JOB_MANUAL_DISMISS
;
2616 * libvirt relies on the DeviceNotFound error class in order to probe for
2617 * live commit feature versions; for this to work, we must make sure to
2618 * perform the device lookup before any generic errors that may occur in a
2619 * scenario in which all optional arguments are omitted. */
2620 bs
= qmp_get_root_bs(device
, &local_err
);
2622 bs
= bdrv_lookup_bs(device
, device
, NULL
);
2624 error_free(local_err
);
2625 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2626 "Device '%s' not found", device
);
2628 error_propagate(errp
, local_err
);
2633 aio_context
= bdrv_get_aio_context(bs
);
2634 aio_context_acquire(aio_context
);
2636 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2640 /* default top_bs is the active layer */
2643 if (top_node
&& top
) {
2644 error_setg(errp
, "'top-node' and 'top' are mutually exclusive");
2646 } else if (top_node
) {
2647 top_bs
= bdrv_lookup_bs(NULL
, top_node
, errp
);
2648 if (top_bs
== NULL
) {
2651 if (!bdrv_chain_contains(bs
, top_bs
)) {
2652 error_setg(errp
, "'%s' is not in this backing file chain",
2657 /* This strcmp() is just a shortcut, there is no need to
2658 * refresh @bs's filename. If it mismatches,
2659 * bdrv_find_backing_image() will do the refresh and may still
2661 if (strcmp(bs
->filename
, top
) != 0) {
2662 top_bs
= bdrv_find_backing_image(bs
, top
);
2666 if (top_bs
== NULL
) {
2667 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2671 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
2673 if (base_node
&& base
) {
2674 error_setg(errp
, "'base-node' and 'base' are mutually exclusive");
2676 } else if (base_node
) {
2677 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
2678 if (base_bs
== NULL
) {
2681 if (!bdrv_chain_contains(top_bs
, base_bs
)) {
2682 error_setg(errp
, "'%s' is not in this backing file chain",
2687 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2688 if (base_bs
== NULL
) {
2689 error_setg(errp
, "Can't find '%s' in the backing chain", base
);
2693 base_bs
= bdrv_find_base(top_bs
);
2694 if (base_bs
== NULL
) {
2695 error_setg(errp
, "There is no backimg image");
2700 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2702 for (iter
= top_bs
; iter
!= bdrv_filter_or_cow_bs(base_bs
);
2703 iter
= bdrv_filter_or_cow_bs(iter
))
2705 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2710 /* Do not allow attempts to commit an image into itself */
2711 if (top_bs
== base_bs
) {
2712 error_setg(errp
, "cannot commit an image into itself");
2717 * Active commit is required if and only if someone has taken a
2718 * WRITE permission on the top node. Historically, we have always
2719 * used active commit for top nodes, so continue that practice
2720 * lest we possibly break clients that rely on this behavior, e.g.
2721 * to later attach this node to a writing parent.
2722 * (Active commit is never really wrong.)
2724 bdrv_get_cumulative_perm(top_bs
, &top_perm
, &top_shared
);
2725 if (top_perm
& BLK_PERM_WRITE
||
2726 bdrv_skip_filters(top_bs
) == bdrv_skip_filters(bs
))
2729 if (bdrv_skip_filters(top_bs
) == bdrv_skip_filters(bs
)) {
2730 error_setg(errp
, "'backing-file' specified,"
2731 " but 'top' is the active layer");
2733 error_setg(errp
, "'backing-file' specified, but 'top' has a "
2740 * Emulate here what block_job_create() does, because it
2741 * is possible that @bs != @top_bs (the block job should
2742 * be named after @bs, even if @top_bs is the actual
2745 job_id
= bdrv_get_device_name(bs
);
2747 commit_active_start(job_id
, top_bs
, base_bs
, job_flags
, speed
, on_error
,
2748 filter_node_name
, NULL
, NULL
, false, &local_err
);
2750 BlockDriverState
*overlay_bs
= bdrv_find_overlay(bs
, top_bs
);
2751 if (bdrv_op_is_blocked(overlay_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2754 commit_start(job_id
, bs
, base_bs
, top_bs
, job_flags
,
2755 speed
, on_error
, backing_file
,
2756 filter_node_name
, &local_err
);
2758 if (local_err
!= NULL
) {
2759 error_propagate(errp
, local_err
);
2764 aio_context_release(aio_context
);
2767 /* Common QMP interface for drive-backup and blockdev-backup */
2768 static BlockJob
*do_backup_common(BackupCommon
*backup
,
2769 BlockDriverState
*bs
,
2770 BlockDriverState
*target_bs
,
2771 AioContext
*aio_context
,
2772 JobTxn
*txn
, Error
**errp
)
2774 BlockJob
*job
= NULL
;
2775 BdrvDirtyBitmap
*bmap
= NULL
;
2776 BackupPerf perf
= { .max_workers
= 64 };
2777 int job_flags
= JOB_DEFAULT
;
2779 if (!backup
->has_speed
) {
2782 if (!backup
->has_on_source_error
) {
2783 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2785 if (!backup
->has_on_target_error
) {
2786 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2788 if (!backup
->has_auto_finalize
) {
2789 backup
->auto_finalize
= true;
2791 if (!backup
->has_auto_dismiss
) {
2792 backup
->auto_dismiss
= true;
2794 if (!backup
->has_compress
) {
2795 backup
->compress
= false;
2798 if (backup
->x_perf
) {
2799 if (backup
->x_perf
->has_use_copy_range
) {
2800 perf
.use_copy_range
= backup
->x_perf
->use_copy_range
;
2802 if (backup
->x_perf
->has_max_workers
) {
2803 perf
.max_workers
= backup
->x_perf
->max_workers
;
2805 if (backup
->x_perf
->has_max_chunk
) {
2806 perf
.max_chunk
= backup
->x_perf
->max_chunk
;
2810 if ((backup
->sync
== MIRROR_SYNC_MODE_BITMAP
) ||
2811 (backup
->sync
== MIRROR_SYNC_MODE_INCREMENTAL
)) {
2812 /* done before desugaring 'incremental' to print the right message */
2813 if (!backup
->bitmap
) {
2814 error_setg(errp
, "must provide a valid bitmap name for "
2815 "'%s' sync mode", MirrorSyncMode_str(backup
->sync
));
2820 if (backup
->sync
== MIRROR_SYNC_MODE_INCREMENTAL
) {
2821 if (backup
->has_bitmap_mode
&&
2822 backup
->bitmap_mode
!= BITMAP_SYNC_MODE_ON_SUCCESS
) {
2823 error_setg(errp
, "Bitmap sync mode must be '%s' "
2824 "when using sync mode '%s'",
2825 BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS
),
2826 MirrorSyncMode_str(backup
->sync
));
2829 backup
->has_bitmap_mode
= true;
2830 backup
->sync
= MIRROR_SYNC_MODE_BITMAP
;
2831 backup
->bitmap_mode
= BITMAP_SYNC_MODE_ON_SUCCESS
;
2834 if (backup
->bitmap
) {
2835 bmap
= bdrv_find_dirty_bitmap(bs
, backup
->bitmap
);
2837 error_setg(errp
, "Bitmap '%s' could not be found", backup
->bitmap
);
2840 if (!backup
->has_bitmap_mode
) {
2841 error_setg(errp
, "Bitmap sync mode must be given "
2842 "when providing a bitmap");
2845 if (bdrv_dirty_bitmap_check(bmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2849 /* This does not produce a useful bitmap artifact: */
2850 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
2851 error_setg(errp
, "sync mode '%s' does not produce meaningful bitmap"
2852 " outputs", MirrorSyncMode_str(backup
->sync
));
2856 /* If the bitmap isn't used for input or output, this is useless: */
2857 if (backup
->bitmap_mode
== BITMAP_SYNC_MODE_NEVER
&&
2858 backup
->sync
!= MIRROR_SYNC_MODE_BITMAP
) {
2859 error_setg(errp
, "Bitmap sync mode '%s' has no meaningful effect"
2860 " when combined with sync mode '%s'",
2861 BitmapSyncMode_str(backup
->bitmap_mode
),
2862 MirrorSyncMode_str(backup
->sync
));
2867 if (!backup
->bitmap
&& backup
->has_bitmap_mode
) {
2868 error_setg(errp
, "Cannot specify bitmap sync mode without a bitmap");
2872 if (!backup
->auto_finalize
) {
2873 job_flags
|= JOB_MANUAL_FINALIZE
;
2875 if (!backup
->auto_dismiss
) {
2876 job_flags
|= JOB_MANUAL_DISMISS
;
2879 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
2880 backup
->sync
, bmap
, backup
->bitmap_mode
,
2882 backup
->filter_node_name
,
2884 backup
->on_source_error
,
2885 backup
->on_target_error
,
2886 job_flags
, NULL
, NULL
, txn
, errp
);
2890 void qmp_drive_backup(DriveBackup
*backup
, Error
**errp
)
2892 TransactionAction action
= {
2893 .type
= TRANSACTION_ACTION_KIND_DRIVE_BACKUP
,
2894 .u
.drive_backup
.data
= backup
,
2896 blockdev_do_action(&action
, errp
);
2899 BlockDeviceInfoList
*qmp_query_named_block_nodes(bool has_flat
,
2903 bool return_flat
= has_flat
&& flat
;
2905 return bdrv_named_nodes_list(return_flat
, errp
);
2908 XDbgBlockGraph
*qmp_x_debug_query_block_graph(Error
**errp
)
2910 return bdrv_get_xdbg_block_graph(errp
);
2913 void qmp_blockdev_backup(BlockdevBackup
*backup
, Error
**errp
)
2915 TransactionAction action
= {
2916 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
,
2917 .u
.blockdev_backup
.data
= backup
,
2919 blockdev_do_action(&action
, errp
);
2922 /* Parameter check and block job starting for drive mirroring.
2923 * Caller should hold @device and @target's aio context (must be the same).
2925 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
2926 BlockDriverState
*target
,
2927 const char *replaces
,
2928 enum MirrorSyncMode sync
,
2929 BlockMirrorBackingMode backing_mode
,
2931 bool has_speed
, int64_t speed
,
2932 bool has_granularity
, uint32_t granularity
,
2933 bool has_buf_size
, int64_t buf_size
,
2934 bool has_on_source_error
,
2935 BlockdevOnError on_source_error
,
2936 bool has_on_target_error
,
2937 BlockdevOnError on_target_error
,
2938 bool has_unmap
, bool unmap
,
2939 const char *filter_node_name
,
2940 bool has_copy_mode
, MirrorCopyMode copy_mode
,
2941 bool has_auto_finalize
, bool auto_finalize
,
2942 bool has_auto_dismiss
, bool auto_dismiss
,
2945 BlockDriverState
*unfiltered_bs
;
2946 int job_flags
= JOB_DEFAULT
;
2951 if (!has_on_source_error
) {
2952 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2954 if (!has_on_target_error
) {
2955 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2957 if (!has_granularity
) {
2960 if (!has_buf_size
) {
2966 if (!has_copy_mode
) {
2967 copy_mode
= MIRROR_COPY_MODE_BACKGROUND
;
2969 if (has_auto_finalize
&& !auto_finalize
) {
2970 job_flags
|= JOB_MANUAL_FINALIZE
;
2972 if (has_auto_dismiss
&& !auto_dismiss
) {
2973 job_flags
|= JOB_MANUAL_DISMISS
;
2976 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
2977 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2978 "a value in range [512B, 64MB]");
2981 if (granularity
& (granularity
- 1)) {
2982 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2987 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
2990 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
2994 if (!bdrv_backing_chain_next(bs
) && sync
== MIRROR_SYNC_MODE_TOP
) {
2995 sync
= MIRROR_SYNC_MODE_FULL
;
2999 /* We want to mirror from @bs, but keep implicit filters on top */
3000 unfiltered_bs
= bdrv_skip_implicit_filters(bs
);
3001 if (unfiltered_bs
!= bs
) {
3002 replaces
= unfiltered_bs
->node_name
;
3007 BlockDriverState
*to_replace_bs
;
3008 AioContext
*replace_aio_context
;
3009 int64_t bs_size
, replace_size
;
3011 bs_size
= bdrv_getlength(bs
);
3013 error_setg_errno(errp
, -bs_size
, "Failed to query device's size");
3017 to_replace_bs
= check_to_replace_node(bs
, replaces
, errp
);
3018 if (!to_replace_bs
) {
3022 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3023 aio_context_acquire(replace_aio_context
);
3024 replace_size
= bdrv_getlength(to_replace_bs
);
3025 aio_context_release(replace_aio_context
);
3027 if (replace_size
< 0) {
3028 error_setg_errno(errp
, -replace_size
,
3029 "Failed to query the replacement node's size");
3032 if (bs_size
!= replace_size
) {
3033 error_setg(errp
, "cannot replace image with a mirror image of "
3039 /* pass the node name to replace to mirror start since it's loose coupling
3040 * and will allow to check whether the node still exist at mirror completion
3042 mirror_start(job_id
, bs
, target
,
3043 replaces
, job_flags
,
3044 speed
, granularity
, buf_size
, sync
, backing_mode
, zero_target
,
3045 on_source_error
, on_target_error
, unmap
, filter_node_name
,
3049 void qmp_drive_mirror(DriveMirror
*arg
, Error
**errp
)
3051 BlockDriverState
*bs
;
3052 BlockDriverState
*target_backing_bs
, *target_bs
;
3053 AioContext
*aio_context
;
3054 AioContext
*old_context
;
3055 BlockMirrorBackingMode backing_mode
;
3056 Error
*local_err
= NULL
;
3057 QDict
*options
= NULL
;
3060 const char *format
= arg
->format
;
3064 bs
= qmp_get_root_bs(arg
->device
, errp
);
3069 /* Early check to avoid creating target */
3070 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3074 aio_context
= bdrv_get_aio_context(bs
);
3075 aio_context_acquire(aio_context
);
3077 if (!arg
->has_mode
) {
3078 arg
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3082 format
= (arg
->mode
== NEW_IMAGE_MODE_EXISTING
3083 ? NULL
: bs
->drv
->format_name
);
3086 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3087 target_backing_bs
= bdrv_cow_bs(bdrv_skip_filters(bs
));
3088 if (!target_backing_bs
&& arg
->sync
== MIRROR_SYNC_MODE_TOP
) {
3089 arg
->sync
= MIRROR_SYNC_MODE_FULL
;
3091 if (arg
->sync
== MIRROR_SYNC_MODE_NONE
) {
3092 target_backing_bs
= bs
;
3095 size
= bdrv_getlength(bs
);
3097 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3101 if (arg
->replaces
) {
3102 if (!arg
->node_name
) {
3103 error_setg(errp
, "a node-name must be provided when replacing a"
3104 " named node of the graph");
3109 if (arg
->mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3110 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3112 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3115 /* Don't open backing image in create() */
3116 flags
|= BDRV_O_NO_BACKING
;
3118 if ((arg
->sync
== MIRROR_SYNC_MODE_FULL
|| !target_backing_bs
)
3119 && arg
->mode
!= NEW_IMAGE_MODE_EXISTING
)
3121 /* create new image w/o backing file */
3123 bdrv_img_create(arg
->target
, format
,
3124 NULL
, NULL
, NULL
, size
, flags
, false, &local_err
);
3126 /* Implicit filters should not appear in the filename */
3127 BlockDriverState
*explicit_backing
=
3128 bdrv_skip_implicit_filters(target_backing_bs
);
3130 switch (arg
->mode
) {
3131 case NEW_IMAGE_MODE_EXISTING
:
3133 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3134 /* create new image with backing file */
3135 bdrv_refresh_filename(explicit_backing
);
3136 bdrv_img_create(arg
->target
, format
,
3137 explicit_backing
->filename
,
3138 explicit_backing
->drv
->format_name
,
3139 NULL
, size
, flags
, false, &local_err
);
3147 error_propagate(errp
, local_err
);
3151 options
= qdict_new();
3152 if (arg
->node_name
) {
3153 qdict_put_str(options
, "node-name", arg
->node_name
);
3156 qdict_put_str(options
, "driver", format
);
3159 /* Mirroring takes care of copy-on-write using the source's backing
3162 target_bs
= bdrv_open(arg
->target
, NULL
, options
, flags
, errp
);
3167 zero_target
= (arg
->sync
== MIRROR_SYNC_MODE_FULL
&&
3168 (arg
->mode
== NEW_IMAGE_MODE_EXISTING
||
3169 !bdrv_has_zero_init(target_bs
)));
3172 /* Honor bdrv_try_change_aio_context() context acquisition requirements. */
3173 old_context
= bdrv_get_aio_context(target_bs
);
3174 aio_context_release(aio_context
);
3175 aio_context_acquire(old_context
);
3177 ret
= bdrv_try_change_aio_context(target_bs
, aio_context
, NULL
, errp
);
3179 bdrv_unref(target_bs
);
3180 aio_context_release(old_context
);
3184 aio_context_release(old_context
);
3185 aio_context_acquire(aio_context
);
3187 blockdev_mirror_common(arg
->job_id
, bs
, target_bs
,
3188 arg
->replaces
, arg
->sync
,
3189 backing_mode
, zero_target
,
3190 arg
->has_speed
, arg
->speed
,
3191 arg
->has_granularity
, arg
->granularity
,
3192 arg
->has_buf_size
, arg
->buf_size
,
3193 arg
->has_on_source_error
, arg
->on_source_error
,
3194 arg
->has_on_target_error
, arg
->on_target_error
,
3195 arg
->has_unmap
, arg
->unmap
,
3197 arg
->has_copy_mode
, arg
->copy_mode
,
3198 arg
->has_auto_finalize
, arg
->auto_finalize
,
3199 arg
->has_auto_dismiss
, arg
->auto_dismiss
,
3201 bdrv_unref(target_bs
);
3203 aio_context_release(aio_context
);
3206 void qmp_blockdev_mirror(const char *job_id
,
3207 const char *device
, const char *target
,
3208 const char *replaces
,
3209 MirrorSyncMode sync
,
3210 bool has_speed
, int64_t speed
,
3211 bool has_granularity
, uint32_t granularity
,
3212 bool has_buf_size
, int64_t buf_size
,
3213 bool has_on_source_error
,
3214 BlockdevOnError on_source_error
,
3215 bool has_on_target_error
,
3216 BlockdevOnError on_target_error
,
3217 const char *filter_node_name
,
3218 bool has_copy_mode
, MirrorCopyMode copy_mode
,
3219 bool has_auto_finalize
, bool auto_finalize
,
3220 bool has_auto_dismiss
, bool auto_dismiss
,
3223 BlockDriverState
*bs
;
3224 BlockDriverState
*target_bs
;
3225 AioContext
*aio_context
;
3226 AioContext
*old_context
;
3227 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3231 bs
= qmp_get_root_bs(device
, errp
);
3236 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3241 zero_target
= (sync
== MIRROR_SYNC_MODE_FULL
);
3243 /* Honor bdrv_try_change_aio_context() context acquisition requirements. */
3244 old_context
= bdrv_get_aio_context(target_bs
);
3245 aio_context
= bdrv_get_aio_context(bs
);
3246 aio_context_acquire(old_context
);
3248 ret
= bdrv_try_change_aio_context(target_bs
, aio_context
, NULL
, errp
);
3250 aio_context_release(old_context
);
3251 aio_context_acquire(aio_context
);
3257 blockdev_mirror_common(job_id
, bs
, target_bs
,
3258 replaces
, sync
, backing_mode
,
3259 zero_target
, has_speed
, speed
,
3260 has_granularity
, granularity
,
3261 has_buf_size
, buf_size
,
3262 has_on_source_error
, on_source_error
,
3263 has_on_target_error
, on_target_error
,
3264 true, true, filter_node_name
,
3265 has_copy_mode
, copy_mode
,
3266 has_auto_finalize
, auto_finalize
,
3267 has_auto_dismiss
, auto_dismiss
,
3270 aio_context_release(aio_context
);
3274 * Get a block job using its ID. Called with job_mutex held.
3276 static BlockJob
*find_block_job_locked(const char *id
, Error
**errp
)
3282 job
= block_job_get_locked(id
);
3285 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3286 "Block job '%s' not found", id
);
3293 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3298 job
= find_block_job_locked(device
, errp
);
3304 block_job_set_speed_locked(job
, speed
, errp
);
3307 void qmp_block_job_cancel(const char *device
,
3308 bool has_force
, bool force
, Error
**errp
)
3313 job
= find_block_job_locked(device
, errp
);
3323 if (job_user_paused_locked(&job
->job
) && !force
) {
3324 error_setg(errp
, "The block job for device '%s' is currently paused",
3329 trace_qmp_block_job_cancel(job
);
3330 job_user_cancel_locked(&job
->job
, force
, errp
);
3333 void qmp_block_job_pause(const char *device
, Error
**errp
)
3338 job
= find_block_job_locked(device
, errp
);
3344 trace_qmp_block_job_pause(job
);
3345 job_user_pause_locked(&job
->job
, errp
);
3348 void qmp_block_job_resume(const char *device
, Error
**errp
)
3353 job
= find_block_job_locked(device
, errp
);
3359 trace_qmp_block_job_resume(job
);
3360 job_user_resume_locked(&job
->job
, errp
);
3363 void qmp_block_job_complete(const char *device
, Error
**errp
)
3368 job
= find_block_job_locked(device
, errp
);
3374 trace_qmp_block_job_complete(job
);
3375 job_complete_locked(&job
->job
, errp
);
3378 void qmp_block_job_finalize(const char *id
, Error
**errp
)
3383 job
= find_block_job_locked(id
, errp
);
3389 trace_qmp_block_job_finalize(job
);
3390 job_ref_locked(&job
->job
);
3391 job_finalize_locked(&job
->job
, errp
);
3393 job_unref_locked(&job
->job
);
3396 void qmp_block_job_dismiss(const char *id
, Error
**errp
)
3402 bjob
= find_block_job_locked(id
, errp
);
3408 trace_qmp_block_job_dismiss(bjob
);
3410 job_dismiss_locked(&job
, errp
);
3413 void qmp_change_backing_file(const char *device
,
3414 const char *image_node_name
,
3415 const char *backing_file
,
3418 BlockDriverState
*bs
= NULL
;
3419 AioContext
*aio_context
;
3420 BlockDriverState
*image_bs
= NULL
;
3421 Error
*local_err
= NULL
;
3425 bs
= qmp_get_root_bs(device
, errp
);
3430 aio_context
= bdrv_get_aio_context(bs
);
3431 aio_context_acquire(aio_context
);
3433 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3435 error_propagate(errp
, local_err
);
3440 error_setg(errp
, "image file not found");
3444 if (bdrv_find_base(image_bs
) == image_bs
) {
3445 error_setg(errp
, "not allowing backing file change on an image "
3446 "without a backing file");
3450 /* even though we are not necessarily operating on bs, we need it to
3451 * determine if block ops are currently prohibited on the chain */
3452 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3456 /* final sanity check */
3457 if (!bdrv_chain_contains(bs
, image_bs
)) {
3458 error_setg(errp
, "'%s' and image file are not in the same chain",
3463 /* if not r/w, reopen to make r/w */
3464 ro
= bdrv_is_read_only(image_bs
);
3467 if (bdrv_reopen_set_read_only(image_bs
, false, errp
) != 0) {
3472 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3473 image_bs
->drv
? image_bs
->drv
->format_name
: "",
3477 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3479 /* don't exit here, so we can try to restore open flags if
3484 bdrv_reopen_set_read_only(image_bs
, true, errp
);
3488 aio_context_release(aio_context
);
3491 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3493 BlockDriverState
*bs
;
3495 Visitor
*v
= qobject_output_visitor_new(&obj
);
3498 visit_type_BlockdevOptions(v
, NULL
, &options
, &error_abort
);
3499 visit_complete(v
, &obj
);
3500 qdict
= qobject_to(QDict
, obj
);
3502 qdict_flatten(qdict
);
3504 if (!qdict_get_try_str(qdict
, "node-name")) {
3505 error_setg(errp
, "'node-name' must be specified for the root node");
3509 bs
= bds_tree_init(qdict
, errp
);
3514 bdrv_set_monitor_owned(bs
);
3520 void qmp_blockdev_reopen(BlockdevOptionsList
*reopen_list
, Error
**errp
)
3522 BlockReopenQueue
*queue
= NULL
;
3524 /* Add each one of the BDS that we want to reopen to the queue */
3525 for (; reopen_list
!= NULL
; reopen_list
= reopen_list
->next
) {
3526 BlockdevOptions
*options
= reopen_list
->value
;
3527 BlockDriverState
*bs
;
3533 /* Check for the selected node name */
3534 if (!options
->node_name
) {
3535 error_setg(errp
, "node-name not specified");
3539 bs
= bdrv_find_node(options
->node_name
);
3541 error_setg(errp
, "Failed to find node with node-name='%s'",
3542 options
->node_name
);
3546 /* Put all options in a QDict and flatten it */
3547 v
= qobject_output_visitor_new(&obj
);
3548 visit_type_BlockdevOptions(v
, NULL
, &options
, &error_abort
);
3549 visit_complete(v
, &obj
);
3552 qdict
= qobject_to(QDict
, obj
);
3554 qdict_flatten(qdict
);
3556 ctx
= bdrv_get_aio_context(bs
);
3557 aio_context_acquire(ctx
);
3559 queue
= bdrv_reopen_queue(queue
, bs
, qdict
, false);
3561 aio_context_release(ctx
);
3564 /* Perform the reopen operation */
3565 bdrv_reopen_multiple(queue
, errp
);
3569 bdrv_reopen_queue_free(queue
);
3572 void qmp_blockdev_del(const char *node_name
, Error
**errp
)
3574 AioContext
*aio_context
;
3575 BlockDriverState
*bs
;
3577 GLOBAL_STATE_CODE();
3579 bs
= bdrv_find_node(node_name
);
3581 error_setg(errp
, "Failed to find node with node-name='%s'", node_name
);
3584 if (bdrv_has_blk(bs
)) {
3585 error_setg(errp
, "Node %s is in use", node_name
);
3588 aio_context
= bdrv_get_aio_context(bs
);
3589 aio_context_acquire(aio_context
);
3591 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3595 if (!QTAILQ_IN_USE(bs
, monitor_list
)) {
3596 error_setg(errp
, "Node %s is not owned by the monitor",
3601 if (bs
->refcnt
> 1) {
3602 error_setg(errp
, "Block device %s is in use",
3603 bdrv_get_device_or_node_name(bs
));
3607 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3611 aio_context_release(aio_context
);
3614 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
3615 const char *child_name
)
3619 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
3620 if (strcmp(child
->name
, child_name
) == 0) {
3628 void qmp_x_blockdev_change(const char *parent
, const char *child
,
3629 const char *node
, Error
**errp
)
3631 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
3634 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
3639 if (!child
== !node
) {
3641 error_setg(errp
, "The parameters child and node are in conflict");
3643 error_setg(errp
, "Either child or node must be specified");
3649 p_child
= bdrv_find_child(parent_bs
, child
);
3651 error_setg(errp
, "Node '%s' does not have child '%s'",
3655 bdrv_del_child(parent_bs
, p_child
, errp
);
3659 new_bs
= bdrv_find_node(node
);
3661 error_setg(errp
, "Node '%s' not found", node
);
3664 bdrv_add_child(parent_bs
, new_bs
, errp
);
3668 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3670 BlockJobInfoList
*head
= NULL
, **tail
= &head
;
3675 for (job
= block_job_next_locked(NULL
); job
;
3676 job
= block_job_next_locked(job
)) {
3677 BlockJobInfo
*value
;
3679 if (block_job_is_internal(job
)) {
3682 value
= block_job_query_locked(job
, errp
);
3684 qapi_free_BlockJobInfoList(head
);
3687 QAPI_LIST_APPEND(tail
, value
);
3693 void qmp_x_blockdev_set_iothread(const char *node_name
, StrOrNull
*iothread
,
3694 bool has_force
, bool force
, Error
**errp
)
3696 AioContext
*old_context
;
3697 AioContext
*new_context
;
3698 BlockDriverState
*bs
;
3700 bs
= bdrv_find_node(node_name
);
3702 error_setg(errp
, "Failed to find node with node-name='%s'", node_name
);
3706 /* Protects against accidents. */
3707 if (!(has_force
&& force
) && bdrv_has_blk(bs
)) {
3708 error_setg(errp
, "Node %s is associated with a BlockBackend and could "
3709 "be in use (use force=true to override this check)",
3714 if (iothread
->type
== QTYPE_QSTRING
) {
3715 IOThread
*obj
= iothread_by_id(iothread
->u
.s
);
3717 error_setg(errp
, "Cannot find iothread %s", iothread
->u
.s
);
3721 new_context
= iothread_get_aio_context(obj
);
3723 new_context
= qemu_get_aio_context();
3726 old_context
= bdrv_get_aio_context(bs
);
3727 aio_context_acquire(old_context
);
3729 bdrv_try_change_aio_context(bs
, new_context
, NULL
, errp
);
3731 aio_context_release(old_context
);
3734 QemuOptsList qemu_common_drive_opts
= {
3736 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3740 .type
= QEMU_OPT_BOOL
,
3741 .help
= "enable/disable snapshot mode",
3744 .type
= QEMU_OPT_STRING
,
3745 .help
= "host AIO implementation (threads, native, io_uring)",
3747 .name
= BDRV_OPT_CACHE_WB
,
3748 .type
= QEMU_OPT_BOOL
,
3749 .help
= "Enable writeback mode",
3752 .type
= QEMU_OPT_STRING
,
3753 .help
= "disk format (raw, qcow2, ...)",
3756 .type
= QEMU_OPT_STRING
,
3757 .help
= "read error action",
3760 .type
= QEMU_OPT_STRING
,
3761 .help
= "write error action",
3763 .name
= BDRV_OPT_READ_ONLY
,
3764 .type
= QEMU_OPT_BOOL
,
3765 .help
= "open drive file as read-only",
3771 .name
= "throttling.group",
3772 .type
= QEMU_OPT_STRING
,
3773 .help
= "name of the block throttling group",
3775 .name
= "copy-on-read",
3776 .type
= QEMU_OPT_BOOL
,
3777 .help
= "copy read data from backing file into image file",
3779 .name
= "detect-zeroes",
3780 .type
= QEMU_OPT_STRING
,
3781 .help
= "try to optimize zero writes (off, on, unmap)",
3783 .name
= "stats-account-invalid",
3784 .type
= QEMU_OPT_BOOL
,
3785 .help
= "whether to account for invalid I/O operations "
3786 "in the statistics",
3788 .name
= "stats-account-failed",
3789 .type
= QEMU_OPT_BOOL
,
3790 .help
= "whether to account for failed I/O operations "
3791 "in the statistics",
3793 { /* end of list */ }
3797 QemuOptsList qemu_drive_opts
= {
3799 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
3802 * no elements => accept any params
3803 * validation will happen later
3805 { /* end of list */ }