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 "sysemu/block-backend.h"
34 #include "sysemu/blockdev.h"
35 #include "hw/block/block.h"
36 #include "block/blockjob.h"
37 #include "block/throttle-groups.h"
38 #include "monitor/monitor.h"
39 #include "qemu/error-report.h"
40 #include "qemu/option.h"
41 #include "qemu/config-file.h"
42 #include "qapi/qmp/types.h"
43 #include "qapi-visit.h"
44 #include "qapi/qmp/qerror.h"
45 #include "qapi/qmp-output-visitor.h"
46 #include "qapi/util.h"
47 #include "sysemu/sysemu.h"
48 #include "block/block_int.h"
49 #include "qmp-commands.h"
51 #include "sysemu/arch_init.h"
53 static const char *const if_name
[IF_COUNT
] = {
57 [IF_FLOPPY
] = "floppy",
58 [IF_PFLASH
] = "pflash",
61 [IF_VIRTIO
] = "virtio",
65 static int if_max_devs
[IF_COUNT
] = {
67 * Do not change these numbers! They govern how drive option
68 * index maps to unit and bus. That mapping is ABI.
70 * All controllers used to imlement if=T drives need to support
71 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
72 * Otherwise, some index values map to "impossible" bus, unit
75 * For instance, if you change [IF_SCSI] to 255, -drive
76 * if=scsi,index=12 no longer means bus=1,unit=5, but
77 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
78 * the drive can't be set up. Regression.
85 * Boards may call this to offer board-by-board overrides
86 * of the default, global values.
88 void override_max_devs(BlockInterfaceType type
, int max_devs
)
97 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
98 dinfo
= blk_legacy_dinfo(blk
);
99 if (dinfo
->type
== type
) {
100 fprintf(stderr
, "Cannot override units-per-bus property of"
101 " the %s interface, because a drive of that type has"
102 " already been added.\n", if_name
[type
]);
103 g_assert_not_reached();
107 if_max_devs
[type
] = max_devs
;
111 * We automatically delete the drive when a device using it gets
112 * unplugged. Questionable feature, but we can't just drop it.
113 * Device models call blockdev_mark_auto_del() to schedule the
114 * automatic deletion, and generic qdev code calls blockdev_auto_del()
115 * when deletion is actually safe.
117 void blockdev_mark_auto_del(BlockBackend
*blk
)
119 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
120 BlockDriverState
*bs
= blk_bs(blk
);
121 AioContext
*aio_context
;
128 aio_context
= bdrv_get_aio_context(bs
);
129 aio_context_acquire(aio_context
);
132 block_job_cancel(bs
->job
);
135 aio_context_release(aio_context
);
141 void blockdev_auto_del(BlockBackend
*blk
)
143 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
145 if (dinfo
&& dinfo
->auto_del
) {
151 * Returns the current mapping of how many units per bus
152 * a particular interface can support.
154 * A positive integer indicates n units per bus.
155 * 0 implies the mapping has not been established.
156 * -1 indicates an invalid BlockInterfaceType was given.
158 int drive_get_max_devs(BlockInterfaceType type
)
160 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
161 return if_max_devs
[type
];
167 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
169 int max_devs
= if_max_devs
[type
];
170 return max_devs
? index
/ max_devs
: 0;
173 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
175 int max_devs
= if_max_devs
[type
];
176 return max_devs
? index
% max_devs
: index
;
179 QemuOpts
*drive_def(const char *optstr
)
181 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
184 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
189 opts
= drive_def(optstr
);
193 if (type
!= IF_DEFAULT
) {
194 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
197 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
200 qemu_opt_set(opts
, "file", file
, &error_abort
);
204 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
209 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
210 dinfo
= blk_legacy_dinfo(blk
);
211 if (dinfo
&& dinfo
->type
== type
212 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
220 bool drive_check_orphaned(void)
226 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
227 dinfo
= blk_legacy_dinfo(blk
);
228 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
229 /* Unless this is a default drive, this may be an oversight. */
230 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
231 dinfo
->type
!= IF_NONE
) {
232 fprintf(stderr
, "Warning: Orphaned drive without device: "
233 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
234 blk_name(blk
), blk_bs(blk
) ? blk_bs(blk
)->filename
: "",
235 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
243 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
245 return drive_get(type
,
246 drive_index_to_bus_id(type
, index
),
247 drive_index_to_unit_id(type
, index
));
250 int drive_get_max_bus(BlockInterfaceType type
)
257 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
258 dinfo
= blk_legacy_dinfo(blk
);
259 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
260 max_bus
= dinfo
->bus
;
266 /* Get a block device. This should only be used for single-drive devices
267 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
269 DriveInfo
*drive_get_next(BlockInterfaceType type
)
271 static int next_block_unit
[IF_COUNT
];
273 return drive_get(type
, 0, next_block_unit
[type
]++);
276 static void bdrv_format_print(void *opaque
, const char *name
)
278 error_printf(" %s", name
);
283 BlockDriverState
*bs
;
286 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
288 if (!strcmp(buf
, "ignore")) {
289 return BLOCKDEV_ON_ERROR_IGNORE
;
290 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
291 return BLOCKDEV_ON_ERROR_ENOSPC
;
292 } else if (!strcmp(buf
, "stop")) {
293 return BLOCKDEV_ON_ERROR_STOP
;
294 } else if (!strcmp(buf
, "report")) {
295 return BLOCKDEV_ON_ERROR_REPORT
;
297 error_setg(errp
, "'%s' invalid %s error action",
298 buf
, is_read
? "read" : "write");
303 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
305 if (throttle_conflicting(cfg
)) {
306 error_setg(errp
, "bps/iops/max total values and read/write values"
307 " cannot be used at the same time");
311 if (!throttle_is_valid(cfg
)) {
312 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
316 if (throttle_max_is_missing_limit(cfg
)) {
317 error_setg(errp
, "bps_max/iops_max require corresponding"
325 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
327 /* All parameters but @opts are optional and may be set to NULL. */
328 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
329 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
330 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
333 Error
*local_error
= NULL
;
337 if (!qemu_opt_get_bool(opts
, "read-only", false)) {
338 *bdrv_flags
|= BDRV_O_RDWR
;
340 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
341 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
344 if ((discard
= qemu_opt_get(opts
, "discard")) != NULL
) {
345 if (bdrv_parse_discard_flags(discard
, bdrv_flags
) != 0) {
346 error_setg(errp
, "Invalid discard option");
351 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true)) {
352 *bdrv_flags
|= BDRV_O_CACHE_WB
;
354 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_DIRECT
, false)) {
355 *bdrv_flags
|= BDRV_O_NOCACHE
;
357 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_NO_FLUSH
, false)) {
358 *bdrv_flags
|= BDRV_O_NO_FLUSH
;
361 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
362 if (!strcmp(aio
, "native")) {
363 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
364 } else if (!strcmp(aio
, "threads")) {
365 /* this is the default */
367 error_setg(errp
, "invalid aio option");
373 /* disk I/O throttling */
374 if (throttling_group
) {
375 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
379 memset(throttle_cfg
, 0, sizeof(*throttle_cfg
));
380 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
381 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
382 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
383 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
384 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
385 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
386 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
387 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
388 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
389 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
390 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
391 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
393 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
394 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
395 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
396 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
397 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
398 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
399 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
400 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
401 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
402 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
403 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
404 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
406 throttle_cfg
->op_size
=
407 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
409 if (!check_throttle_config(throttle_cfg
, errp
)) {
416 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
417 qemu_opt_get(opts
, "detect-zeroes"),
418 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX
,
419 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
422 error_propagate(errp
, local_error
);
427 *detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
428 !(*bdrv_flags
& BDRV_O_UNMAP
))
430 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
431 "without setting discard operation to unmap");
437 /* Takes the ownership of bs_opts */
438 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
443 int on_read_error
, on_write_error
;
444 bool account_invalid
, account_failed
;
446 BlockDriverState
*bs
;
452 bool has_driver_specific_opts
;
453 BlockdevDetectZeroesOptions detect_zeroes
=
454 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
455 const char *throttling_group
= NULL
;
457 /* Check common options by copying from bs_opts to opts, all other options
458 * stay in bs_opts for processing by bdrv_open(). */
459 id
= qdict_get_try_str(bs_opts
, "id");
460 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
462 error_propagate(errp
, error
);
466 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
468 error_propagate(errp
, error
);
473 qdict_del(bs_opts
, "id");
476 has_driver_specific_opts
= !!qdict_size(bs_opts
);
478 /* extract parameters */
479 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
481 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
482 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
484 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
485 &detect_zeroes
, &error
);
487 error_propagate(errp
, error
);
491 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
492 if (is_help_option(buf
)) {
493 error_printf("Supported formats:");
494 bdrv_iterate_format(bdrv_format_print
, NULL
);
499 if (qdict_haskey(bs_opts
, "driver")) {
500 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
503 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
506 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
507 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
508 on_write_error
= parse_block_error_action(buf
, 0, &error
);
510 error_propagate(errp
, error
);
515 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
516 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
517 on_read_error
= parse_block_error_action(buf
, 1, &error
);
519 error_propagate(errp
, error
);
525 /* always use cache=unsafe with snapshot */
526 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
527 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
531 if ((!file
|| !*file
) && !has_driver_specific_opts
) {
532 BlockBackendRootState
*blk_rs
;
534 blk
= blk_new(qemu_opts_id(opts
), errp
);
539 blk_rs
= blk_get_root_state(blk
);
540 blk_rs
->open_flags
= bdrv_flags
;
541 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
542 blk_rs
->detect_zeroes
= detect_zeroes
;
544 if (throttle_enabled(&cfg
)) {
545 if (!throttling_group
) {
546 throttling_group
= blk_name(blk
);
548 blk_rs
->throttle_group
= g_strdup(throttling_group
);
549 blk_rs
->throttle_state
= throttle_group_incref(throttling_group
);
550 blk_rs
->throttle_state
->cfg
= cfg
;
555 if (file
&& !*file
) {
559 blk
= blk_new_open(qemu_opts_id(opts
), file
, NULL
, bs_opts
, bdrv_flags
,
566 bs
->detect_zeroes
= detect_zeroes
;
568 /* disk I/O throttling */
569 if (throttle_enabled(&cfg
)) {
570 if (!throttling_group
) {
571 throttling_group
= blk_name(blk
);
573 bdrv_io_limits_enable(bs
, throttling_group
);
574 bdrv_set_io_limits(bs
, &cfg
);
577 if (bdrv_key_required(bs
)) {
581 block_acct_init(blk_get_stats(blk
), account_invalid
, account_failed
);
584 blk_set_on_error(blk
, on_read_error
, on_write_error
);
597 static QemuOptsList qemu_root_bds_opts
;
599 /* Takes the ownership of bs_opts */
600 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
602 BlockDriverState
*bs
;
604 Error
*local_error
= NULL
;
605 BlockdevDetectZeroesOptions detect_zeroes
;
609 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
614 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
616 error_propagate(errp
, local_error
);
620 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
621 &detect_zeroes
, &local_error
);
623 error_propagate(errp
, local_error
);
628 ret
= bdrv_open(&bs
, NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
630 goto fail_no_bs_opts
;
633 bs
->detect_zeroes
= detect_zeroes
;
645 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
650 value
= qemu_opt_get(opts
, from
);
652 if (qemu_opt_find(opts
, to
)) {
653 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
654 "same time", to
, from
);
659 /* rename all items in opts */
660 while ((value
= qemu_opt_get(opts
, from
))) {
661 qemu_opt_set(opts
, to
, value
, &error_abort
);
662 qemu_opt_unset(opts
, from
);
666 QemuOptsList qemu_legacy_drive_opts
= {
668 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
672 .type
= QEMU_OPT_NUMBER
,
673 .help
= "bus number",
676 .type
= QEMU_OPT_NUMBER
,
677 .help
= "unit number (i.e. lun for scsi)",
680 .type
= QEMU_OPT_NUMBER
,
681 .help
= "index number",
684 .type
= QEMU_OPT_STRING
,
685 .help
= "media type (disk, cdrom)",
688 .type
= QEMU_OPT_STRING
,
689 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
692 .type
= QEMU_OPT_NUMBER
,
693 .help
= "number of cylinders (ide disk geometry)",
696 .type
= QEMU_OPT_NUMBER
,
697 .help
= "number of heads (ide disk geometry)",
700 .type
= QEMU_OPT_NUMBER
,
701 .help
= "number of sectors (ide disk geometry)",
704 .type
= QEMU_OPT_STRING
,
705 .help
= "chs translation (auto, lba, none)",
708 .type
= QEMU_OPT_BOOL
,
709 .help
= "(deprecated, ignored)",
712 .type
= QEMU_OPT_STRING
,
713 .help
= "pci address (virtio only)",
716 .type
= QEMU_OPT_STRING
,
717 .help
= "disk serial number",
720 .type
= QEMU_OPT_STRING
,
724 /* Options that are passed on, but have special semantics with -drive */
727 .type
= QEMU_OPT_BOOL
,
728 .help
= "open drive file as read-only",
731 .type
= QEMU_OPT_STRING
,
732 .help
= "read error action",
735 .type
= QEMU_OPT_STRING
,
736 .help
= "write error action",
738 .name
= "copy-on-read",
739 .type
= QEMU_OPT_BOOL
,
740 .help
= "copy read data from backing file into image file",
743 { /* end of list */ }
747 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
751 DriveInfo
*dinfo
= NULL
;
753 QemuOpts
*legacy_opts
;
754 DriveMediaType media
= MEDIA_DISK
;
755 BlockInterfaceType type
;
756 int cyls
, heads
, secs
, translation
;
757 int max_devs
, bus_id
, unit_id
, index
;
759 const char *werror
, *rerror
;
760 bool read_only
= false;
763 const char *filename
;
764 Error
*local_err
= NULL
;
767 /* Change legacy command line options into QMP ones */
768 static const struct {
772 { "iops", "throttling.iops-total" },
773 { "iops_rd", "throttling.iops-read" },
774 { "iops_wr", "throttling.iops-write" },
776 { "bps", "throttling.bps-total" },
777 { "bps_rd", "throttling.bps-read" },
778 { "bps_wr", "throttling.bps-write" },
780 { "iops_max", "throttling.iops-total-max" },
781 { "iops_rd_max", "throttling.iops-read-max" },
782 { "iops_wr_max", "throttling.iops-write-max" },
784 { "bps_max", "throttling.bps-total-max" },
785 { "bps_rd_max", "throttling.bps-read-max" },
786 { "bps_wr_max", "throttling.bps-write-max" },
788 { "iops_size", "throttling.iops-size" },
790 { "group", "throttling.group" },
792 { "readonly", "read-only" },
795 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
796 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
799 error_report_err(local_err
);
804 value
= qemu_opt_get(all_opts
, "cache");
808 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
809 error_report("invalid cache option");
813 /* Specific options take precedence */
814 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
815 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
816 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
818 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
819 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
820 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
822 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
823 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
824 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
826 qemu_opt_unset(all_opts
, "cache");
829 /* Get a QDict for processing the options */
830 bs_opts
= qdict_new();
831 qemu_opts_to_qdict(all_opts
, bs_opts
);
833 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
835 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
837 error_report_err(local_err
);
841 /* Deprecated option boot=[on|off] */
842 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
843 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
844 "ignored. Future versions will reject this parameter. Please "
845 "update your scripts.\n");
849 value
= qemu_opt_get(legacy_opts
, "media");
851 if (!strcmp(value
, "disk")) {
853 } else if (!strcmp(value
, "cdrom")) {
857 error_report("'%s' invalid media", value
);
862 /* copy-on-read is disabled with a warning for read-only devices */
863 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
864 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
866 if (read_only
&& copy_on_read
) {
867 error_report("warning: disabling copy-on-read on read-only drive");
868 copy_on_read
= false;
871 qdict_put(bs_opts
, "read-only",
872 qstring_from_str(read_only
? "on" : "off"));
873 qdict_put(bs_opts
, "copy-on-read",
874 qstring_from_str(copy_on_read
? "on" :"off"));
876 /* Controller type */
877 value
= qemu_opt_get(legacy_opts
, "if");
880 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
883 if (type
== IF_COUNT
) {
884 error_report("unsupported bus type '%s'", value
);
888 type
= block_default_type
;
892 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
893 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
894 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
896 if (cyls
|| heads
|| secs
) {
898 error_report("invalid physical cyls number");
902 error_report("invalid physical heads number");
906 error_report("invalid physical secs number");
911 translation
= BIOS_ATA_TRANSLATION_AUTO
;
912 value
= qemu_opt_get(legacy_opts
, "trans");
915 error_report("'%s' trans must be used with cyls, heads and secs",
919 if (!strcmp(value
, "none")) {
920 translation
= BIOS_ATA_TRANSLATION_NONE
;
921 } else if (!strcmp(value
, "lba")) {
922 translation
= BIOS_ATA_TRANSLATION_LBA
;
923 } else if (!strcmp(value
, "large")) {
924 translation
= BIOS_ATA_TRANSLATION_LARGE
;
925 } else if (!strcmp(value
, "rechs")) {
926 translation
= BIOS_ATA_TRANSLATION_RECHS
;
927 } else if (!strcmp(value
, "auto")) {
928 translation
= BIOS_ATA_TRANSLATION_AUTO
;
930 error_report("'%s' invalid translation type", value
);
935 if (media
== MEDIA_CDROM
) {
936 if (cyls
|| secs
|| heads
) {
937 error_report("CHS can't be set with media=cdrom");
942 /* Device address specified by bus/unit or index.
943 * If none was specified, try to find the first free one. */
944 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
945 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
946 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
948 max_devs
= if_max_devs
[type
];
951 if (bus_id
!= 0 || unit_id
!= -1) {
952 error_report("index cannot be used with bus and unit");
955 bus_id
= drive_index_to_bus_id(type
, index
);
956 unit_id
= drive_index_to_unit_id(type
, index
);
961 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
963 if (max_devs
&& unit_id
>= max_devs
) {
970 if (max_devs
&& unit_id
>= max_devs
) {
971 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
975 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
976 error_report("drive with bus=%d, unit=%d (index=%d) exists",
977 bus_id
, unit_id
, index
);
982 serial
= qemu_opt_get(legacy_opts
, "serial");
984 /* no id supplied -> create one */
985 if (qemu_opts_id(all_opts
) == NULL
) {
987 const char *mediastr
= "";
988 if (type
== IF_IDE
|| type
== IF_SCSI
) {
989 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
992 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
995 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
998 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1002 /* Add virtio block device */
1003 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1004 if (devaddr
&& type
!= IF_VIRTIO
) {
1005 error_report("addr is not supported by this bus type");
1009 if (type
== IF_VIRTIO
) {
1011 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1013 if (arch_type
== QEMU_ARCH_S390X
) {
1014 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1016 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1018 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1021 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1025 filename
= qemu_opt_get(legacy_opts
, "file");
1027 /* Check werror/rerror compatibility with if=... */
1028 werror
= qemu_opt_get(legacy_opts
, "werror");
1029 if (werror
!= NULL
) {
1030 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1032 error_report("werror is not supported by this bus type");
1035 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1038 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1039 if (rerror
!= NULL
) {
1040 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1042 error_report("rerror is not supported by this bus type");
1045 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1048 /* Actual block device init: Functionality shared with blockdev-add */
1049 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1053 error_report_err(local_err
);
1060 /* Create legacy DriveInfo */
1061 dinfo
= g_malloc0(sizeof(*dinfo
));
1062 dinfo
->opts
= all_opts
;
1065 dinfo
->heads
= heads
;
1067 dinfo
->trans
= translation
;
1070 dinfo
->bus
= bus_id
;
1071 dinfo
->unit
= unit_id
;
1072 dinfo
->devaddr
= devaddr
;
1073 dinfo
->serial
= g_strdup(serial
);
1075 blk_set_legacy_dinfo(blk
, dinfo
);
1082 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1089 qemu_opts_del(legacy_opts
);
1094 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1096 const char *device
= qdict_get_str(qdict
, "device");
1100 if (!strcmp(device
, "all")) {
1101 ret
= bdrv_commit_all();
1103 BlockDriverState
*bs
;
1104 AioContext
*aio_context
;
1106 blk
= blk_by_name(device
);
1108 monitor_printf(mon
, "Device '%s' not found\n", device
);
1111 if (!blk_is_available(blk
)) {
1112 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1117 aio_context
= bdrv_get_aio_context(bs
);
1118 aio_context_acquire(aio_context
);
1120 ret
= bdrv_commit(bs
);
1122 aio_context_release(aio_context
);
1125 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1130 static void blockdev_do_action(TransactionActionKind type
, void *data
,
1133 TransactionAction action
;
1134 TransactionActionList list
;
1137 action
.u
.data
= data
;
1138 list
.value
= &action
;
1140 qmp_transaction(&list
, false, NULL
, errp
);
1143 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1144 bool has_node_name
, const char *node_name
,
1145 const char *snapshot_file
,
1146 bool has_snapshot_node_name
,
1147 const char *snapshot_node_name
,
1148 bool has_format
, const char *format
,
1149 bool has_mode
, NewImageMode mode
, Error
**errp
)
1151 BlockdevSnapshotSync snapshot
= {
1152 .has_device
= has_device
,
1153 .device
= (char *) device
,
1154 .has_node_name
= has_node_name
,
1155 .node_name
= (char *) node_name
,
1156 .snapshot_file
= (char *) snapshot_file
,
1157 .has_snapshot_node_name
= has_snapshot_node_name
,
1158 .snapshot_node_name
= (char *) snapshot_node_name
,
1159 .has_format
= has_format
,
1160 .format
= (char *) format
,
1161 .has_mode
= has_mode
,
1164 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1168 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1171 BlockdevSnapshot snapshot_data
= {
1172 .node
= (char *) node
,
1173 .overlay
= (char *) overlay
1176 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1177 &snapshot_data
, errp
);
1180 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1184 BlockdevSnapshotInternal snapshot
= {
1185 .device
= (char *) device
,
1186 .name
= (char *) name
1189 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1193 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1200 BlockDriverState
*bs
;
1202 AioContext
*aio_context
;
1203 QEMUSnapshotInfo sn
;
1204 Error
*local_err
= NULL
;
1205 SnapshotInfo
*info
= NULL
;
1208 blk
= blk_by_name(device
);
1210 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1211 "Device '%s' not found", device
);
1215 aio_context
= blk_get_aio_context(blk
);
1216 aio_context_acquire(aio_context
);
1227 error_setg(errp
, "Name or id must be provided");
1228 goto out_aio_context
;
1231 if (!blk_is_available(blk
)) {
1232 error_setg(errp
, "Device '%s' has no medium", device
);
1233 goto out_aio_context
;
1237 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1238 goto out_aio_context
;
1241 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1243 error_propagate(errp
, local_err
);
1244 goto out_aio_context
;
1248 "Snapshot with id '%s' and name '%s' does not exist on "
1250 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1251 goto out_aio_context
;
1254 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1256 error_propagate(errp
, local_err
);
1257 goto out_aio_context
;
1260 aio_context_release(aio_context
);
1262 info
= g_new0(SnapshotInfo
, 1);
1263 info
->id
= g_strdup(sn
.id_str
);
1264 info
->name
= g_strdup(sn
.name
);
1265 info
->date_nsec
= sn
.date_nsec
;
1266 info
->date_sec
= sn
.date_sec
;
1267 info
->vm_state_size
= sn
.vm_state_size
;
1268 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1269 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1274 aio_context_release(aio_context
);
1279 * block_dirty_bitmap_lookup:
1280 * Return a dirty bitmap (if present), after validating
1281 * the node reference and bitmap names.
1283 * @node: The name of the BDS node to search for bitmaps
1284 * @name: The name of the bitmap to search for
1285 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1286 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1287 * @errp: Output pointer for error information. Can be NULL.
1289 * @return: A bitmap object on success, or NULL on failure.
1291 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1293 BlockDriverState
**pbs
,
1297 BlockDriverState
*bs
;
1298 BdrvDirtyBitmap
*bitmap
;
1299 AioContext
*aio_context
;
1302 error_setg(errp
, "Node cannot be NULL");
1306 error_setg(errp
, "Bitmap name cannot be NULL");
1309 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1311 error_setg(errp
, "Node '%s' not found", node
);
1315 aio_context
= bdrv_get_aio_context(bs
);
1316 aio_context_acquire(aio_context
);
1318 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1320 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1328 *paio
= aio_context
;
1330 aio_context_release(aio_context
);
1336 aio_context_release(aio_context
);
1340 /* New and old BlockDriverState structs for atomic group operations */
1342 typedef struct BlkActionState BlkActionState
;
1346 * Table of operations that define an Action.
1348 * @instance_size: Size of state struct, in bytes.
1349 * @prepare: Prepare the work, must NOT be NULL.
1350 * @commit: Commit the changes, can be NULL.
1351 * @abort: Abort the changes on fail, can be NULL.
1352 * @clean: Clean up resources after all transaction actions have called
1353 * commit() or abort(). Can be NULL.
1355 * Only prepare() may fail. In a single transaction, only one of commit() or
1356 * abort() will be called. clean() will always be called if it is present.
1358 typedef struct BlkActionOps
{
1359 size_t instance_size
;
1360 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1361 void (*commit
)(BlkActionState
*common
);
1362 void (*abort
)(BlkActionState
*common
);
1363 void (*clean
)(BlkActionState
*common
);
1368 * Describes one Action's state within a Transaction.
1370 * @action: QAPI-defined enum identifying which Action to perform.
1371 * @ops: Table of ActionOps this Action can perform.
1372 * @block_job_txn: Transaction which this action belongs to.
1373 * @entry: List membership for all Actions in this Transaction.
1375 * This structure must be arranged as first member in a subclassed type,
1376 * assuming that the compiler will also arrange it to the same offsets as the
1379 struct BlkActionState
{
1380 TransactionAction
*action
;
1381 const BlkActionOps
*ops
;
1382 BlockJobTxn
*block_job_txn
;
1383 TransactionProperties
*txn_props
;
1384 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1387 /* internal snapshot private data */
1388 typedef struct InternalSnapshotState
{
1389 BlkActionState common
;
1390 BlockDriverState
*bs
;
1391 AioContext
*aio_context
;
1392 QEMUSnapshotInfo sn
;
1394 } InternalSnapshotState
;
1397 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1399 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1401 "Action '%s' does not support Transaction property "
1402 "completion-mode = %s",
1403 TransactionActionKind_lookup
[s
->action
->type
],
1404 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1410 static void internal_snapshot_prepare(BlkActionState
*common
,
1413 Error
*local_err
= NULL
;
1417 BlockDriverState
*bs
;
1418 QEMUSnapshotInfo old_sn
, *sn
;
1421 BlockdevSnapshotInternal
*internal
;
1422 InternalSnapshotState
*state
;
1425 g_assert(common
->action
->type
==
1426 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1427 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
;
1428 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1430 /* 1. parse input */
1431 device
= internal
->device
;
1432 name
= internal
->name
;
1434 /* 2. check for validation */
1435 if (action_check_completion_mode(common
, errp
) < 0) {
1439 blk
= blk_by_name(device
);
1441 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1442 "Device '%s' not found", device
);
1446 /* AioContext is released in .clean() */
1447 state
->aio_context
= blk_get_aio_context(blk
);
1448 aio_context_acquire(state
->aio_context
);
1450 if (!blk_is_available(blk
)) {
1451 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1457 bdrv_drained_begin(bs
);
1459 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1463 if (bdrv_is_read_only(bs
)) {
1464 error_setg(errp
, "Device '%s' is read only", device
);
1468 if (!bdrv_can_snapshot(bs
)) {
1469 error_setg(errp
, "Block format '%s' used by device '%s' "
1470 "does not support internal snapshots",
1471 bs
->drv
->format_name
, device
);
1475 if (!strlen(name
)) {
1476 error_setg(errp
, "Name is empty");
1480 /* check whether a snapshot with name exist */
1481 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1484 error_propagate(errp
, local_err
);
1488 "Snapshot with name '%s' already exists on device '%s'",
1493 /* 3. take the snapshot */
1495 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1496 qemu_gettimeofday(&tv
);
1497 sn
->date_sec
= tv
.tv_sec
;
1498 sn
->date_nsec
= tv
.tv_usec
* 1000;
1499 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1501 ret1
= bdrv_snapshot_create(bs
, sn
);
1503 error_setg_errno(errp
, -ret1
,
1504 "Failed to create snapshot '%s' on device '%s'",
1509 /* 4. succeed, mark a snapshot is created */
1510 state
->created
= true;
1513 static void internal_snapshot_abort(BlkActionState
*common
)
1515 InternalSnapshotState
*state
=
1516 DO_UPCAST(InternalSnapshotState
, common
, common
);
1517 BlockDriverState
*bs
= state
->bs
;
1518 QEMUSnapshotInfo
*sn
= &state
->sn
;
1519 Error
*local_error
= NULL
;
1521 if (!state
->created
) {
1525 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1526 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1527 "device '%s' in abort: %s",
1530 bdrv_get_device_name(bs
),
1531 error_get_pretty(local_error
));
1532 error_free(local_error
);
1536 static void internal_snapshot_clean(BlkActionState
*common
)
1538 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1541 if (state
->aio_context
) {
1543 bdrv_drained_end(state
->bs
);
1545 aio_context_release(state
->aio_context
);
1549 /* external snapshot private data */
1550 typedef struct ExternalSnapshotState
{
1551 BlkActionState common
;
1552 BlockDriverState
*old_bs
;
1553 BlockDriverState
*new_bs
;
1554 AioContext
*aio_context
;
1555 } ExternalSnapshotState
;
1557 static void external_snapshot_prepare(BlkActionState
*common
,
1561 QDict
*options
= NULL
;
1562 Error
*local_err
= NULL
;
1563 /* Device and node name of the image to generate the snapshot from */
1565 const char *node_name
;
1566 /* Reference to the new image (for 'blockdev-snapshot') */
1567 const char *snapshot_ref
;
1568 /* File name of the new image (for 'blockdev-snapshot-sync') */
1569 const char *new_image_file
;
1570 ExternalSnapshotState
*state
=
1571 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1572 TransactionAction
*action
= common
->action
;
1574 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1575 * purpose but a different set of parameters */
1576 switch (action
->type
) {
1577 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1579 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
;
1581 node_name
= s
->node
;
1582 new_image_file
= NULL
;
1583 snapshot_ref
= s
->overlay
;
1586 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1588 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1589 device
= s
->has_device
? s
->device
: NULL
;
1590 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1591 new_image_file
= s
->snapshot_file
;
1592 snapshot_ref
= NULL
;
1596 g_assert_not_reached();
1599 /* start processing */
1600 if (action_check_completion_mode(common
, errp
) < 0) {
1604 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1605 if (!state
->old_bs
) {
1609 /* Acquire AioContext now so any threads operating on old_bs stop */
1610 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1611 aio_context_acquire(state
->aio_context
);
1612 bdrv_drained_begin(state
->old_bs
);
1614 if (!bdrv_is_inserted(state
->old_bs
)) {
1615 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1619 if (bdrv_op_is_blocked(state
->old_bs
,
1620 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1624 if (!bdrv_is_read_only(state
->old_bs
)) {
1625 if (bdrv_flush(state
->old_bs
)) {
1626 error_setg(errp
, QERR_IO_ERROR
);
1631 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1632 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1636 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1637 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1638 const char *format
= s
->has_format
? s
->format
: "qcow2";
1639 enum NewImageMode mode
;
1640 const char *snapshot_node_name
=
1641 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1643 if (node_name
&& !snapshot_node_name
) {
1644 error_setg(errp
, "New snapshot node name missing");
1648 if (snapshot_node_name
&&
1649 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1650 error_setg(errp
, "New snapshot node name already in use");
1654 flags
= state
->old_bs
->open_flags
;
1656 /* create new image w/backing file */
1657 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1658 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1659 bdrv_img_create(new_image_file
, format
,
1660 state
->old_bs
->filename
,
1661 state
->old_bs
->drv
->format_name
,
1662 NULL
, -1, flags
, &local_err
, false);
1664 error_propagate(errp
, local_err
);
1669 options
= qdict_new();
1670 if (s
->has_snapshot_node_name
) {
1671 qdict_put(options
, "node-name",
1672 qstring_from_str(snapshot_node_name
));
1674 qdict_put(options
, "driver", qstring_from_str(format
));
1676 flags
|= BDRV_O_NO_BACKING
;
1679 assert(state
->new_bs
== NULL
);
1680 ret
= bdrv_open(&state
->new_bs
, new_image_file
, snapshot_ref
, options
,
1682 /* We will manually add the backing_hd field to the bs later */
1687 if (state
->new_bs
->blk
!= NULL
) {
1688 error_setg(errp
, "The snapshot is already in use by %s",
1689 blk_name(state
->new_bs
->blk
));
1693 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1698 if (state
->new_bs
->backing
!= NULL
) {
1699 error_setg(errp
, "The snapshot already has a backing image");
1703 if (!state
->new_bs
->drv
->supports_backing
) {
1704 error_setg(errp
, "The snapshot does not support backing images");
1708 static void external_snapshot_commit(BlkActionState
*common
)
1710 ExternalSnapshotState
*state
=
1711 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1713 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1715 /* This removes our old bs and adds the new bs */
1716 bdrv_append(state
->new_bs
, state
->old_bs
);
1717 /* We don't need (or want) to use the transactional
1718 * bdrv_reopen_multiple() across all the entries at once, because we
1719 * don't want to abort all of them if one of them fails the reopen */
1720 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1724 static void external_snapshot_abort(BlkActionState
*common
)
1726 ExternalSnapshotState
*state
=
1727 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1728 if (state
->new_bs
) {
1729 bdrv_unref(state
->new_bs
);
1733 static void external_snapshot_clean(BlkActionState
*common
)
1735 ExternalSnapshotState
*state
=
1736 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1737 if (state
->aio_context
) {
1738 bdrv_drained_end(state
->old_bs
);
1739 aio_context_release(state
->aio_context
);
1743 typedef struct DriveBackupState
{
1744 BlkActionState common
;
1745 BlockDriverState
*bs
;
1746 AioContext
*aio_context
;
1750 static void do_drive_backup(const char *device
, const char *target
,
1751 bool has_format
, const char *format
,
1752 enum MirrorSyncMode sync
,
1753 bool has_mode
, enum NewImageMode mode
,
1754 bool has_speed
, int64_t speed
,
1755 bool has_bitmap
, const char *bitmap
,
1756 bool has_on_source_error
,
1757 BlockdevOnError on_source_error
,
1758 bool has_on_target_error
,
1759 BlockdevOnError on_target_error
,
1760 BlockJobTxn
*txn
, Error
**errp
);
1762 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1764 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1766 DriveBackup
*backup
;
1767 Error
*local_err
= NULL
;
1769 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1770 backup
= common
->action
->u
.drive_backup
;
1772 blk
= blk_by_name(backup
->device
);
1774 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1775 "Device '%s' not found", backup
->device
);
1779 if (!blk_is_available(blk
)) {
1780 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1784 /* AioContext is released in .clean() */
1785 state
->aio_context
= blk_get_aio_context(blk
);
1786 aio_context_acquire(state
->aio_context
);
1787 bdrv_drained_begin(blk_bs(blk
));
1788 state
->bs
= blk_bs(blk
);
1790 do_drive_backup(backup
->device
, backup
->target
,
1791 backup
->has_format
, backup
->format
,
1793 backup
->has_mode
, backup
->mode
,
1794 backup
->has_speed
, backup
->speed
,
1795 backup
->has_bitmap
, backup
->bitmap
,
1796 backup
->has_on_source_error
, backup
->on_source_error
,
1797 backup
->has_on_target_error
, backup
->on_target_error
,
1798 common
->block_job_txn
, &local_err
);
1800 error_propagate(errp
, local_err
);
1804 state
->job
= state
->bs
->job
;
1807 static void drive_backup_abort(BlkActionState
*common
)
1809 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1810 BlockDriverState
*bs
= state
->bs
;
1812 /* Only cancel if it's the job we started */
1813 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1814 block_job_cancel_sync(bs
->job
);
1818 static void drive_backup_clean(BlkActionState
*common
)
1820 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1822 if (state
->aio_context
) {
1823 bdrv_drained_end(state
->bs
);
1824 aio_context_release(state
->aio_context
);
1828 typedef struct BlockdevBackupState
{
1829 BlkActionState common
;
1830 BlockDriverState
*bs
;
1832 AioContext
*aio_context
;
1833 } BlockdevBackupState
;
1835 static void do_blockdev_backup(const char *device
, const char *target
,
1836 enum MirrorSyncMode sync
,
1837 bool has_speed
, int64_t speed
,
1838 bool has_on_source_error
,
1839 BlockdevOnError on_source_error
,
1840 bool has_on_target_error
,
1841 BlockdevOnError on_target_error
,
1842 BlockJobTxn
*txn
, Error
**errp
);
1844 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1846 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1847 BlockdevBackup
*backup
;
1848 BlockBackend
*blk
, *target
;
1849 Error
*local_err
= NULL
;
1851 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1852 backup
= common
->action
->u
.blockdev_backup
;
1854 blk
= blk_by_name(backup
->device
);
1856 error_setg(errp
, "Device '%s' not found", backup
->device
);
1860 if (!blk_is_available(blk
)) {
1861 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1865 target
= blk_by_name(backup
->target
);
1867 error_setg(errp
, "Device '%s' not found", backup
->target
);
1871 /* AioContext is released in .clean() */
1872 state
->aio_context
= blk_get_aio_context(blk
);
1873 if (state
->aio_context
!= blk_get_aio_context(target
)) {
1874 state
->aio_context
= NULL
;
1875 error_setg(errp
, "Backup between two IO threads is not implemented");
1878 aio_context_acquire(state
->aio_context
);
1879 state
->bs
= blk_bs(blk
);
1880 bdrv_drained_begin(state
->bs
);
1882 do_blockdev_backup(backup
->device
, backup
->target
,
1884 backup
->has_speed
, backup
->speed
,
1885 backup
->has_on_source_error
, backup
->on_source_error
,
1886 backup
->has_on_target_error
, backup
->on_target_error
,
1887 common
->block_job_txn
, &local_err
);
1889 error_propagate(errp
, local_err
);
1893 state
->job
= state
->bs
->job
;
1896 static void blockdev_backup_abort(BlkActionState
*common
)
1898 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1899 BlockDriverState
*bs
= state
->bs
;
1901 /* Only cancel if it's the job we started */
1902 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1903 block_job_cancel_sync(bs
->job
);
1907 static void blockdev_backup_clean(BlkActionState
*common
)
1909 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1911 if (state
->aio_context
) {
1912 bdrv_drained_end(state
->bs
);
1913 aio_context_release(state
->aio_context
);
1917 typedef struct BlockDirtyBitmapState
{
1918 BlkActionState common
;
1919 BdrvDirtyBitmap
*bitmap
;
1920 BlockDriverState
*bs
;
1921 AioContext
*aio_context
;
1924 } BlockDirtyBitmapState
;
1926 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
1929 Error
*local_err
= NULL
;
1930 BlockDirtyBitmapAdd
*action
;
1931 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1934 if (action_check_completion_mode(common
, errp
) < 0) {
1938 action
= common
->action
->u
.block_dirty_bitmap_add
;
1939 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1940 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
1941 action
->has_granularity
, action
->granularity
,
1945 state
->prepared
= true;
1947 error_propagate(errp
, local_err
);
1951 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
1953 BlockDirtyBitmapAdd
*action
;
1954 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1957 action
= common
->action
->u
.block_dirty_bitmap_add
;
1958 /* Should not be able to fail: IF the bitmap was added via .prepare(),
1959 * then the node reference and bitmap name must have been valid.
1961 if (state
->prepared
) {
1962 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
1966 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
1969 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1971 BlockDirtyBitmap
*action
;
1973 if (action_check_completion_mode(common
, errp
) < 0) {
1977 action
= common
->action
->u
.block_dirty_bitmap_clear
;
1978 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
1981 &state
->aio_context
,
1983 if (!state
->bitmap
) {
1987 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
1988 error_setg(errp
, "Cannot modify a frozen bitmap");
1990 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
1991 error_setg(errp
, "Cannot clear a disabled bitmap");
1995 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
1996 /* AioContext is released in .clean() */
1999 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2001 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2004 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2007 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2009 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2012 hbitmap_free(state
->backup
);
2015 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2017 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2020 if (state
->aio_context
) {
2021 aio_context_release(state
->aio_context
);
2025 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2027 error_setg(errp
, "Transaction aborted using Abort action");
2030 static void abort_commit(BlkActionState
*common
)
2032 g_assert_not_reached(); /* this action never succeeds */
2035 static const BlkActionOps actions
[] = {
2036 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2037 .instance_size
= sizeof(ExternalSnapshotState
),
2038 .prepare
= external_snapshot_prepare
,
2039 .commit
= external_snapshot_commit
,
2040 .abort
= external_snapshot_abort
,
2042 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2043 .instance_size
= sizeof(ExternalSnapshotState
),
2044 .prepare
= external_snapshot_prepare
,
2045 .commit
= external_snapshot_commit
,
2046 .abort
= external_snapshot_abort
,
2047 .clean
= external_snapshot_clean
,
2049 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2050 .instance_size
= sizeof(DriveBackupState
),
2051 .prepare
= drive_backup_prepare
,
2052 .abort
= drive_backup_abort
,
2053 .clean
= drive_backup_clean
,
2055 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2056 .instance_size
= sizeof(BlockdevBackupState
),
2057 .prepare
= blockdev_backup_prepare
,
2058 .abort
= blockdev_backup_abort
,
2059 .clean
= blockdev_backup_clean
,
2061 [TRANSACTION_ACTION_KIND_ABORT
] = {
2062 .instance_size
= sizeof(BlkActionState
),
2063 .prepare
= abort_prepare
,
2064 .commit
= abort_commit
,
2066 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2067 .instance_size
= sizeof(InternalSnapshotState
),
2068 .prepare
= internal_snapshot_prepare
,
2069 .abort
= internal_snapshot_abort
,
2070 .clean
= internal_snapshot_clean
,
2072 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2073 .instance_size
= sizeof(BlockDirtyBitmapState
),
2074 .prepare
= block_dirty_bitmap_add_prepare
,
2075 .abort
= block_dirty_bitmap_add_abort
,
2077 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2078 .instance_size
= sizeof(BlockDirtyBitmapState
),
2079 .prepare
= block_dirty_bitmap_clear_prepare
,
2080 .commit
= block_dirty_bitmap_clear_commit
,
2081 .abort
= block_dirty_bitmap_clear_abort
,
2082 .clean
= block_dirty_bitmap_clear_clean
,
2087 * Allocate a TransactionProperties structure if necessary, and fill
2088 * that structure with desired defaults if they are unset.
2090 static TransactionProperties
*get_transaction_properties(
2091 TransactionProperties
*props
)
2094 props
= g_new0(TransactionProperties
, 1);
2097 if (!props
->has_completion_mode
) {
2098 props
->has_completion_mode
= true;
2099 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2106 * 'Atomic' group operations. The operations are performed as a set, and if
2107 * any fail then we roll back all operations in the group.
2109 void qmp_transaction(TransactionActionList
*dev_list
,
2111 struct TransactionProperties
*props
,
2114 TransactionActionList
*dev_entry
= dev_list
;
2115 BlockJobTxn
*block_job_txn
= NULL
;
2116 BlkActionState
*state
, *next
;
2117 Error
*local_err
= NULL
;
2119 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2120 QSIMPLEQ_INIT(&snap_bdrv_states
);
2122 /* Does this transaction get canceled as a group on failure?
2123 * If not, we don't really need to make a BlockJobTxn.
2125 props
= get_transaction_properties(props
);
2126 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2127 block_job_txn
= block_job_txn_new();
2130 /* drain all i/o before any operations */
2133 /* We don't do anything in this loop that commits us to the operations */
2134 while (NULL
!= dev_entry
) {
2135 TransactionAction
*dev_info
= NULL
;
2136 const BlkActionOps
*ops
;
2138 dev_info
= dev_entry
->value
;
2139 dev_entry
= dev_entry
->next
;
2141 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2143 ops
= &actions
[dev_info
->type
];
2144 assert(ops
->instance_size
> 0);
2146 state
= g_malloc0(ops
->instance_size
);
2148 state
->action
= dev_info
;
2149 state
->block_job_txn
= block_job_txn
;
2150 state
->txn_props
= props
;
2151 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2153 state
->ops
->prepare(state
, &local_err
);
2155 error_propagate(errp
, local_err
);
2156 goto delete_and_fail
;
2160 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2161 if (state
->ops
->commit
) {
2162 state
->ops
->commit(state
);
2170 /* failure, and it is all-or-none; roll back all operations */
2171 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2172 if (state
->ops
->abort
) {
2173 state
->ops
->abort(state
);
2177 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2178 if (state
->ops
->clean
) {
2179 state
->ops
->clean(state
);
2184 qapi_free_TransactionProperties(props
);
2186 block_job_txn_unref(block_job_txn
);
2189 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
2191 Error
*local_err
= NULL
;
2193 qmp_blockdev_open_tray(device
, has_force
, force
, &local_err
);
2195 error_propagate(errp
, local_err
);
2199 qmp_blockdev_remove_medium(device
, errp
);
2202 void qmp_block_passwd(bool has_device
, const char *device
,
2203 bool has_node_name
, const char *node_name
,
2204 const char *password
, Error
**errp
)
2206 Error
*local_err
= NULL
;
2207 BlockDriverState
*bs
;
2208 AioContext
*aio_context
;
2210 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2211 has_node_name
? node_name
: NULL
,
2214 error_propagate(errp
, local_err
);
2218 aio_context
= bdrv_get_aio_context(bs
);
2219 aio_context_acquire(aio_context
);
2221 bdrv_add_key(bs
, password
, errp
);
2223 aio_context_release(aio_context
);
2226 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2236 blk
= blk_by_name(device
);
2238 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2239 "Device '%s' not found", device
);
2243 if (!blk_dev_has_removable_media(blk
)) {
2244 error_setg(errp
, "Device '%s' is not removable", device
);
2248 if (blk_dev_is_tray_open(blk
)) {
2252 locked
= blk_dev_is_medium_locked(blk
);
2254 blk_dev_eject_request(blk
, force
);
2257 if (!locked
|| force
) {
2258 blk_dev_change_media_cb(blk
, false);
2262 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2266 blk
= blk_by_name(device
);
2268 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2269 "Device '%s' not found", device
);
2273 if (!blk_dev_has_removable_media(blk
)) {
2274 error_setg(errp
, "Device '%s' is not removable", device
);
2278 if (!blk_dev_is_tray_open(blk
)) {
2282 blk_dev_change_media_cb(blk
, true);
2285 void qmp_blockdev_remove_medium(const char *device
, Error
**errp
)
2288 BlockDriverState
*bs
;
2289 AioContext
*aio_context
;
2292 blk
= blk_by_name(device
);
2294 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2295 "Device '%s' not found", device
);
2299 /* For BBs without a device, we can exchange the BDS tree at will */
2300 has_device
= blk_get_attached_dev(blk
);
2302 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2303 error_setg(errp
, "Device '%s' is not removable", device
);
2307 if (has_device
&& !blk_dev_is_tray_open(blk
)) {
2308 error_setg(errp
, "Tray of device '%s' is not open", device
);
2317 aio_context
= bdrv_get_aio_context(bs
);
2318 aio_context_acquire(aio_context
);
2320 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2324 /* This follows the convention established by bdrv_make_anon() */
2325 if (bs
->device_list
.tqe_prev
) {
2326 QTAILQ_REMOVE(&bdrv_states
, bs
, device_list
);
2327 bs
->device_list
.tqe_prev
= NULL
;
2333 aio_context_release(aio_context
);
2336 static void qmp_blockdev_insert_anon_medium(const char *device
,
2337 BlockDriverState
*bs
, Error
**errp
)
2342 blk
= blk_by_name(device
);
2344 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2345 "Device '%s' not found", device
);
2349 /* For BBs without a device, we can exchange the BDS tree at will */
2350 has_device
= blk_get_attached_dev(blk
);
2352 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2353 error_setg(errp
, "Device '%s' is not removable", device
);
2357 if (has_device
&& !blk_dev_is_tray_open(blk
)) {
2358 error_setg(errp
, "Tray of device '%s' is not open", device
);
2363 error_setg(errp
, "There already is a medium in device '%s'", device
);
2367 blk_insert_bs(blk
, bs
);
2369 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, device_list
);
2372 void qmp_blockdev_insert_medium(const char *device
, const char *node_name
,
2375 BlockDriverState
*bs
;
2377 bs
= bdrv_find_node(node_name
);
2379 error_setg(errp
, "Node '%s' not found", node_name
);
2384 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2389 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2392 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2393 bool has_format
, const char *format
,
2395 BlockdevChangeReadOnlyMode read_only
,
2399 BlockDriverState
*medium_bs
= NULL
;
2400 int bdrv_flags
, ret
;
2401 QDict
*options
= NULL
;
2404 blk
= blk_by_name(device
);
2406 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2407 "Device '%s' not found", device
);
2412 blk_update_root_state(blk
);
2415 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2417 if (!has_read_only
) {
2418 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2421 switch (read_only
) {
2422 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2425 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2426 bdrv_flags
&= ~BDRV_O_RDWR
;
2429 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2430 bdrv_flags
|= BDRV_O_RDWR
;
2438 options
= qdict_new();
2439 qdict_put(options
, "driver", qstring_from_str(format
));
2443 ret
= bdrv_open(&medium_bs
, filename
, NULL
, options
, bdrv_flags
, errp
);
2448 blk_apply_root_state(blk
, medium_bs
);
2450 bdrv_add_key(medium_bs
, NULL
, &err
);
2452 error_propagate(errp
, err
);
2456 qmp_blockdev_open_tray(device
, false, false, &err
);
2458 error_propagate(errp
, err
);
2462 qmp_blockdev_remove_medium(device
, &err
);
2464 error_propagate(errp
, err
);
2468 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2470 error_propagate(errp
, err
);
2474 qmp_blockdev_close_tray(device
, errp
);
2477 /* If the medium has been inserted, the device has its own reference, so
2478 * ours must be relinquished; and if it has not been inserted successfully,
2479 * the reference must be relinquished anyway */
2480 bdrv_unref(medium_bs
);
2483 /* throttling disk I/O limits */
2484 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
2491 bool has_bps_rd_max
,
2493 bool has_bps_wr_max
,
2497 bool has_iops_rd_max
,
2498 int64_t iops_rd_max
,
2499 bool has_iops_wr_max
,
2500 int64_t iops_wr_max
,
2504 const char *group
, Error
**errp
)
2507 BlockDriverState
*bs
;
2509 AioContext
*aio_context
;
2511 blk
= blk_by_name(device
);
2513 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2514 "Device '%s' not found", device
);
2518 aio_context
= blk_get_aio_context(blk
);
2519 aio_context_acquire(aio_context
);
2523 error_setg(errp
, "Device '%s' has no medium", device
);
2527 memset(&cfg
, 0, sizeof(cfg
));
2528 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
2529 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
2530 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
2532 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
2533 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
2534 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
2537 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
2539 if (has_bps_rd_max
) {
2540 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2542 if (has_bps_wr_max
) {
2543 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2546 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2548 if (has_iops_rd_max
) {
2549 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2551 if (has_iops_wr_max
) {
2552 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2555 if (has_iops_size
) {
2556 cfg
.op_size
= iops_size
;
2559 if (!check_throttle_config(&cfg
, errp
)) {
2563 if (throttle_enabled(&cfg
)) {
2564 /* Enable I/O limits if they're not enabled yet, otherwise
2565 * just update the throttling group. */
2566 if (!bs
->throttle_state
) {
2567 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2568 } else if (has_group
) {
2569 bdrv_io_limits_update_group(bs
, group
);
2571 /* Set the new throttling configuration */
2572 bdrv_set_io_limits(bs
, &cfg
);
2573 } else if (bs
->throttle_state
) {
2574 /* If all throttling settings are set to 0, disable I/O limits */
2575 bdrv_io_limits_disable(bs
);
2579 aio_context_release(aio_context
);
2582 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2583 bool has_granularity
, uint32_t granularity
,
2586 AioContext
*aio_context
;
2587 BlockDriverState
*bs
;
2589 if (!name
|| name
[0] == '\0') {
2590 error_setg(errp
, "Bitmap name cannot be empty");
2594 bs
= bdrv_lookup_bs(node
, node
, errp
);
2599 aio_context
= bdrv_get_aio_context(bs
);
2600 aio_context_acquire(aio_context
);
2602 if (has_granularity
) {
2603 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2604 error_setg(errp
, "Granularity must be power of 2 "
2605 "and at least 512");
2609 /* Default to cluster size, if available: */
2610 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2613 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2616 aio_context_release(aio_context
);
2619 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2622 AioContext
*aio_context
;
2623 BlockDriverState
*bs
;
2624 BdrvDirtyBitmap
*bitmap
;
2626 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2627 if (!bitmap
|| !bs
) {
2631 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2633 "Bitmap '%s' is currently frozen and cannot be removed",
2637 bdrv_dirty_bitmap_make_anon(bitmap
);
2638 bdrv_release_dirty_bitmap(bs
, bitmap
);
2641 aio_context_release(aio_context
);
2645 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2646 * immediately after a full backup operation.
2648 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2651 AioContext
*aio_context
;
2652 BdrvDirtyBitmap
*bitmap
;
2653 BlockDriverState
*bs
;
2655 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2656 if (!bitmap
|| !bs
) {
2660 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2662 "Bitmap '%s' is currently frozen and cannot be modified",
2665 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2667 "Bitmap '%s' is currently disabled and cannot be cleared",
2672 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2675 aio_context_release(aio_context
);
2678 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2680 const char *id
= qdict_get_str(qdict
, "id");
2682 BlockDriverState
*bs
;
2683 AioContext
*aio_context
;
2684 Error
*local_err
= NULL
;
2686 blk
= blk_by_name(id
);
2688 error_report("Device '%s' not found", id
);
2692 if (!blk_legacy_dinfo(blk
)) {
2693 error_report("Deleting device added with blockdev-add"
2694 " is not supported");
2698 aio_context
= blk_get_aio_context(blk
);
2699 aio_context_acquire(aio_context
);
2703 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2704 error_report_err(local_err
);
2705 aio_context_release(aio_context
);
2712 /* if we have a device attached to this BlockDriverState
2713 * then we need to make the drive anonymous until the device
2714 * can be removed. If this is a drive with no device backing
2715 * then we can just get rid of the block driver state right here.
2717 if (blk_get_attached_dev(blk
)) {
2718 blk_hide_on_behalf_of_hmp_drive_del(blk
);
2719 /* Further I/O must not pause the guest */
2720 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2721 BLOCKDEV_ON_ERROR_REPORT
);
2726 aio_context_release(aio_context
);
2729 void qmp_block_resize(bool has_device
, const char *device
,
2730 bool has_node_name
, const char *node_name
,
2731 int64_t size
, Error
**errp
)
2733 Error
*local_err
= NULL
;
2734 BlockDriverState
*bs
;
2735 AioContext
*aio_context
;
2738 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2739 has_node_name
? node_name
: NULL
,
2742 error_propagate(errp
, local_err
);
2746 aio_context
= bdrv_get_aio_context(bs
);
2747 aio_context_acquire(aio_context
);
2749 if (!bdrv_is_first_non_filter(bs
)) {
2750 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2755 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2759 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2760 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2764 /* complete all in-flight operations before resizing the device */
2767 ret
= bdrv_truncate(bs
, size
);
2772 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2775 error_setg(errp
, QERR_UNSUPPORTED
);
2778 error_setg(errp
, "Device '%s' is read only", device
);
2781 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2784 error_setg_errno(errp
, -ret
, "Could not resize");
2789 aio_context_release(aio_context
);
2792 static void block_job_cb(void *opaque
, int ret
)
2794 /* Note that this function may be executed from another AioContext besides
2795 * the QEMU main loop. If you need to access anything that assumes the
2796 * QEMU global mutex, use a BH or introduce a mutex.
2799 BlockDriverState
*bs
= opaque
;
2800 const char *msg
= NULL
;
2802 trace_block_job_cb(bs
, bs
->job
, ret
);
2807 msg
= strerror(-ret
);
2810 if (block_job_is_cancelled(bs
->job
)) {
2811 block_job_event_cancelled(bs
->job
);
2813 block_job_event_completed(bs
->job
, msg
);
2817 void qmp_block_stream(const char *device
,
2818 bool has_base
, const char *base
,
2819 bool has_backing_file
, const char *backing_file
,
2820 bool has_speed
, int64_t speed
,
2821 bool has_on_error
, BlockdevOnError on_error
,
2825 BlockDriverState
*bs
;
2826 BlockDriverState
*base_bs
= NULL
;
2827 AioContext
*aio_context
;
2828 Error
*local_err
= NULL
;
2829 const char *base_name
= NULL
;
2831 if (!has_on_error
) {
2832 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2835 blk
= blk_by_name(device
);
2837 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2838 "Device '%s' not found", device
);
2842 aio_context
= blk_get_aio_context(blk
);
2843 aio_context_acquire(aio_context
);
2845 if (!blk_is_available(blk
)) {
2846 error_setg(errp
, "Device '%s' has no medium", device
);
2851 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2856 base_bs
= bdrv_find_backing_image(bs
, base
);
2857 if (base_bs
== NULL
) {
2858 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2861 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2865 /* if we are streaming the entire chain, the result will have no backing
2866 * file, and specifying one is therefore an error */
2867 if (base_bs
== NULL
&& has_backing_file
) {
2868 error_setg(errp
, "backing file specified, but streaming the "
2873 /* backing_file string overrides base bs filename */
2874 base_name
= has_backing_file
? backing_file
: base_name
;
2876 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
2877 on_error
, block_job_cb
, bs
, &local_err
);
2879 error_propagate(errp
, local_err
);
2883 trace_qmp_block_stream(bs
, bs
->job
);
2886 aio_context_release(aio_context
);
2889 void qmp_block_commit(const char *device
,
2890 bool has_base
, const char *base
,
2891 bool has_top
, const char *top
,
2892 bool has_backing_file
, const char *backing_file
,
2893 bool has_speed
, int64_t speed
,
2897 BlockDriverState
*bs
;
2898 BlockDriverState
*base_bs
, *top_bs
;
2899 AioContext
*aio_context
;
2900 Error
*local_err
= NULL
;
2901 /* This will be part of the QMP command, if/when the
2902 * BlockdevOnError change for blkmirror makes it in
2904 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2911 * libvirt relies on the DeviceNotFound error class in order to probe for
2912 * live commit feature versions; for this to work, we must make sure to
2913 * perform the device lookup before any generic errors that may occur in a
2914 * scenario in which all optional arguments are omitted. */
2915 blk
= blk_by_name(device
);
2917 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2918 "Device '%s' not found", device
);
2922 aio_context
= blk_get_aio_context(blk
);
2923 aio_context_acquire(aio_context
);
2925 if (!blk_is_available(blk
)) {
2926 error_setg(errp
, "Device '%s' has no medium", device
);
2931 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2935 /* default top_bs is the active layer */
2938 if (has_top
&& top
) {
2939 if (strcmp(bs
->filename
, top
) != 0) {
2940 top_bs
= bdrv_find_backing_image(bs
, top
);
2944 if (top_bs
== NULL
) {
2945 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2949 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
2951 if (has_base
&& base
) {
2952 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2954 base_bs
= bdrv_find_base(top_bs
);
2957 if (base_bs
== NULL
) {
2958 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2962 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2964 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2968 /* Do not allow attempts to commit an image into itself */
2969 if (top_bs
== base_bs
) {
2970 error_setg(errp
, "cannot commit an image into itself");
2975 if (has_backing_file
) {
2976 error_setg(errp
, "'backing-file' specified,"
2977 " but 'top' is the active layer");
2980 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
2983 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
2984 has_backing_file
? backing_file
: NULL
, &local_err
);
2986 if (local_err
!= NULL
) {
2987 error_propagate(errp
, local_err
);
2992 aio_context_release(aio_context
);
2995 static void do_drive_backup(const char *device
, const char *target
,
2996 bool has_format
, const char *format
,
2997 enum MirrorSyncMode sync
,
2998 bool has_mode
, enum NewImageMode mode
,
2999 bool has_speed
, int64_t speed
,
3000 bool has_bitmap
, const char *bitmap
,
3001 bool has_on_source_error
,
3002 BlockdevOnError on_source_error
,
3003 bool has_on_target_error
,
3004 BlockdevOnError on_target_error
,
3005 BlockJobTxn
*txn
, Error
**errp
)
3008 BlockDriverState
*bs
;
3009 BlockDriverState
*target_bs
;
3010 BlockDriverState
*source
= NULL
;
3011 BdrvDirtyBitmap
*bmap
= NULL
;
3012 AioContext
*aio_context
;
3013 QDict
*options
= NULL
;
3014 Error
*local_err
= NULL
;
3022 if (!has_on_source_error
) {
3023 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3025 if (!has_on_target_error
) {
3026 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3029 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3032 blk
= blk_by_name(device
);
3034 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3035 "Device '%s' not found", device
);
3039 aio_context
= blk_get_aio_context(blk
);
3040 aio_context_acquire(aio_context
);
3042 /* Although backup_run has this check too, we need to use bs->drv below, so
3043 * do an early check redundantly. */
3044 if (!blk_is_available(blk
)) {
3045 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3051 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3054 /* Early check to avoid creating target */
3055 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3059 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3061 /* See if we have a backing HD we can use to create our new image
3063 if (sync
== MIRROR_SYNC_MODE_TOP
) {
3064 source
= backing_bs(bs
);
3066 sync
= MIRROR_SYNC_MODE_FULL
;
3069 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3073 size
= bdrv_getlength(bs
);
3075 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3079 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
3082 bdrv_img_create(target
, format
, source
->filename
,
3083 source
->drv
->format_name
, NULL
,
3084 size
, flags
, &local_err
, false);
3086 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
3087 size
, flags
, &local_err
, false);
3092 error_propagate(errp
, local_err
);
3097 options
= qdict_new();
3098 qdict_put(options
, "driver", qstring_from_str(format
));
3102 ret
= bdrv_open(&target_bs
, target
, NULL
, options
, flags
, &local_err
);
3104 error_propagate(errp
, local_err
);
3108 bdrv_set_aio_context(target_bs
, aio_context
);
3111 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
3113 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
3118 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
3119 on_source_error
, on_target_error
,
3120 block_job_cb
, bs
, txn
, &local_err
);
3121 if (local_err
!= NULL
) {
3122 bdrv_unref(target_bs
);
3123 error_propagate(errp
, local_err
);
3128 aio_context_release(aio_context
);
3131 void qmp_drive_backup(const char *device
, const char *target
,
3132 bool has_format
, const char *format
,
3133 enum MirrorSyncMode sync
,
3134 bool has_mode
, enum NewImageMode mode
,
3135 bool has_speed
, int64_t speed
,
3136 bool has_bitmap
, const char *bitmap
,
3137 bool has_on_source_error
, BlockdevOnError on_source_error
,
3138 bool has_on_target_error
, BlockdevOnError on_target_error
,
3141 return do_drive_backup(device
, target
, has_format
, format
, sync
,
3142 has_mode
, mode
, has_speed
, speed
,
3144 has_on_source_error
, on_source_error
,
3145 has_on_target_error
, on_target_error
,
3149 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3151 return bdrv_named_nodes_list(errp
);
3154 void do_blockdev_backup(const char *device
, const char *target
,
3155 enum MirrorSyncMode sync
,
3156 bool has_speed
, int64_t speed
,
3157 bool has_on_source_error
,
3158 BlockdevOnError on_source_error
,
3159 bool has_on_target_error
,
3160 BlockdevOnError on_target_error
,
3161 BlockJobTxn
*txn
, Error
**errp
)
3163 BlockBackend
*blk
, *target_blk
;
3164 BlockDriverState
*bs
;
3165 BlockDriverState
*target_bs
;
3166 Error
*local_err
= NULL
;
3167 AioContext
*aio_context
;
3172 if (!has_on_source_error
) {
3173 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3175 if (!has_on_target_error
) {
3176 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3179 blk
= blk_by_name(device
);
3181 error_setg(errp
, "Device '%s' not found", device
);
3185 aio_context
= blk_get_aio_context(blk
);
3186 aio_context_acquire(aio_context
);
3188 if (!blk_is_available(blk
)) {
3189 error_setg(errp
, "Device '%s' has no medium", device
);
3194 target_blk
= blk_by_name(target
);
3196 error_setg(errp
, "Device '%s' not found", target
);
3200 if (!blk_is_available(target_blk
)) {
3201 error_setg(errp
, "Device '%s' has no medium", target
);
3204 target_bs
= blk_bs(target_blk
);
3206 bdrv_ref(target_bs
);
3207 bdrv_set_aio_context(target_bs
, aio_context
);
3208 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
3209 on_target_error
, block_job_cb
, bs
, txn
, &local_err
);
3210 if (local_err
!= NULL
) {
3211 bdrv_unref(target_bs
);
3212 error_propagate(errp
, local_err
);
3215 aio_context_release(aio_context
);
3218 void qmp_blockdev_backup(const char *device
, const char *target
,
3219 enum MirrorSyncMode sync
,
3220 bool has_speed
, int64_t speed
,
3221 bool has_on_source_error
,
3222 BlockdevOnError on_source_error
,
3223 bool has_on_target_error
,
3224 BlockdevOnError on_target_error
,
3227 do_blockdev_backup(device
, target
, sync
, has_speed
, speed
,
3228 has_on_source_error
, on_source_error
,
3229 has_on_target_error
, on_target_error
,
3233 void qmp_drive_mirror(const char *device
, const char *target
,
3234 bool has_format
, const char *format
,
3235 bool has_node_name
, const char *node_name
,
3236 bool has_replaces
, const char *replaces
,
3237 enum MirrorSyncMode sync
,
3238 bool has_mode
, enum NewImageMode mode
,
3239 bool has_speed
, int64_t speed
,
3240 bool has_granularity
, uint32_t granularity
,
3241 bool has_buf_size
, int64_t buf_size
,
3242 bool has_on_source_error
, BlockdevOnError on_source_error
,
3243 bool has_on_target_error
, BlockdevOnError on_target_error
,
3244 bool has_unmap
, bool unmap
,
3248 BlockDriverState
*bs
;
3249 BlockDriverState
*source
, *target_bs
;
3250 AioContext
*aio_context
;
3251 Error
*local_err
= NULL
;
3260 if (!has_on_source_error
) {
3261 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3263 if (!has_on_target_error
) {
3264 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3267 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3269 if (!has_granularity
) {
3272 if (!has_buf_size
) {
3279 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3280 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3281 "a value in range [512B, 64MB]");
3284 if (granularity
& (granularity
- 1)) {
3285 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3290 blk
= blk_by_name(device
);
3292 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3293 "Device '%s' not found", device
);
3297 aio_context
= blk_get_aio_context(blk
);
3298 aio_context_acquire(aio_context
);
3300 if (!blk_is_available(blk
)) {
3301 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3307 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3310 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR
, errp
)) {
3314 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3315 source
= backing_bs(bs
);
3316 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3317 sync
= MIRROR_SYNC_MODE_FULL
;
3319 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3323 size
= bdrv_getlength(bs
);
3325 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3330 BlockDriverState
*to_replace_bs
;
3331 AioContext
*replace_aio_context
;
3332 int64_t replace_size
;
3334 if (!has_node_name
) {
3335 error_setg(errp
, "a node-name must be provided when replacing a"
3336 " named node of the graph");
3340 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
3342 if (!to_replace_bs
) {
3343 error_propagate(errp
, local_err
);
3347 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3348 aio_context_acquire(replace_aio_context
);
3349 replace_size
= bdrv_getlength(to_replace_bs
);
3350 aio_context_release(replace_aio_context
);
3352 if (size
!= replace_size
) {
3353 error_setg(errp
, "cannot replace image with a mirror image of "
3359 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3360 && mode
!= NEW_IMAGE_MODE_EXISTING
)
3362 /* create new image w/o backing file */
3364 bdrv_img_create(target
, format
,
3365 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3368 case NEW_IMAGE_MODE_EXISTING
:
3370 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3371 /* create new image with backing file */
3372 bdrv_img_create(target
, format
,
3374 source
->drv
->format_name
,
3375 NULL
, size
, flags
, &local_err
, false);
3383 error_propagate(errp
, local_err
);
3387 options
= qdict_new();
3388 if (has_node_name
) {
3389 qdict_put(options
, "node-name", qstring_from_str(node_name
));
3392 qdict_put(options
, "driver", qstring_from_str(format
));
3395 /* Mirroring takes care of copy-on-write using the source's backing
3399 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
3400 flags
| BDRV_O_NO_BACKING
, &local_err
);
3402 error_propagate(errp
, local_err
);
3406 bdrv_set_aio_context(target_bs
, aio_context
);
3408 /* pass the node name to replace to mirror start since it's loose coupling
3409 * and will allow to check whether the node still exist at mirror completion
3411 mirror_start(bs
, target_bs
,
3412 has_replaces
? replaces
: NULL
,
3413 speed
, granularity
, buf_size
, sync
,
3414 on_source_error
, on_target_error
,
3416 block_job_cb
, bs
, &local_err
);
3417 if (local_err
!= NULL
) {
3418 bdrv_unref(target_bs
);
3419 error_propagate(errp
, local_err
);
3424 aio_context_release(aio_context
);
3427 /* Get the block job for a given device name and acquire its AioContext */
3428 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
3432 BlockDriverState
*bs
;
3434 *aio_context
= NULL
;
3436 blk
= blk_by_name(device
);
3441 *aio_context
= blk_get_aio_context(blk
);
3442 aio_context_acquire(*aio_context
);
3444 if (!blk_is_available(blk
)) {
3456 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3457 "No active block job on device '%s'", device
);
3459 aio_context_release(*aio_context
);
3460 *aio_context
= NULL
;
3465 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3467 AioContext
*aio_context
;
3468 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3474 block_job_set_speed(job
, speed
, errp
);
3475 aio_context_release(aio_context
);
3478 void qmp_block_job_cancel(const char *device
,
3479 bool has_force
, bool force
, Error
**errp
)
3481 AioContext
*aio_context
;
3482 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3492 if (job
->user_paused
&& !force
) {
3493 error_setg(errp
, "The block job for device '%s' is currently paused",
3498 trace_qmp_block_job_cancel(job
);
3499 block_job_cancel(job
);
3501 aio_context_release(aio_context
);
3504 void qmp_block_job_pause(const char *device
, Error
**errp
)
3506 AioContext
*aio_context
;
3507 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3509 if (!job
|| job
->user_paused
) {
3513 job
->user_paused
= true;
3514 trace_qmp_block_job_pause(job
);
3515 block_job_pause(job
);
3516 aio_context_release(aio_context
);
3519 void qmp_block_job_resume(const char *device
, Error
**errp
)
3521 AioContext
*aio_context
;
3522 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3524 if (!job
|| !job
->user_paused
) {
3528 job
->user_paused
= false;
3529 trace_qmp_block_job_resume(job
);
3530 block_job_resume(job
);
3531 aio_context_release(aio_context
);
3534 void qmp_block_job_complete(const char *device
, Error
**errp
)
3536 AioContext
*aio_context
;
3537 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3543 trace_qmp_block_job_complete(job
);
3544 block_job_complete(job
, errp
);
3545 aio_context_release(aio_context
);
3548 void qmp_change_backing_file(const char *device
,
3549 const char *image_node_name
,
3550 const char *backing_file
,
3554 BlockDriverState
*bs
= NULL
;
3555 AioContext
*aio_context
;
3556 BlockDriverState
*image_bs
= NULL
;
3557 Error
*local_err
= NULL
;
3562 blk
= blk_by_name(device
);
3564 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3565 "Device '%s' not found", device
);
3569 aio_context
= blk_get_aio_context(blk
);
3570 aio_context_acquire(aio_context
);
3572 if (!blk_is_available(blk
)) {
3573 error_setg(errp
, "Device '%s' has no medium", device
);
3578 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3580 error_propagate(errp
, local_err
);
3585 error_setg(errp
, "image file not found");
3589 if (bdrv_find_base(image_bs
) == image_bs
) {
3590 error_setg(errp
, "not allowing backing file change on an image "
3591 "without a backing file");
3595 /* even though we are not necessarily operating on bs, we need it to
3596 * determine if block ops are currently prohibited on the chain */
3597 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3601 /* final sanity check */
3602 if (!bdrv_chain_contains(bs
, image_bs
)) {
3603 error_setg(errp
, "'%s' and image file are not in the same chain",
3608 /* if not r/w, reopen to make r/w */
3609 open_flags
= image_bs
->open_flags
;
3610 ro
= bdrv_is_read_only(image_bs
);
3613 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3615 error_propagate(errp
, local_err
);
3620 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3621 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3624 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3626 /* don't exit here, so we can try to restore open flags if
3631 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3633 error_propagate(errp
, local_err
); /* will preserve prior errp */
3638 aio_context_release(aio_context
);
3641 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3643 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3644 BlockDriverState
*bs
;
3645 BlockBackend
*blk
= NULL
;
3648 Error
*local_err
= NULL
;
3650 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3651 * cache.direct=false instead of silently switching to aio=threads, except
3652 * when called from drive_new().
3654 * For now, simply forbidding the combination for all drivers will do. */
3655 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3656 bool direct
= options
->has_cache
&&
3657 options
->cache
->has_direct
&&
3658 options
->cache
->direct
;
3660 error_setg(errp
, "aio=native requires cache.direct=true");
3665 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
3666 &options
, NULL
, &local_err
);
3668 error_propagate(errp
, local_err
);
3672 obj
= qmp_output_get_qobject(ov
);
3673 qdict
= qobject_to_qdict(obj
);
3675 qdict_flatten(qdict
);
3677 if (options
->has_id
) {
3678 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3680 error_propagate(errp
, local_err
);
3686 if (!qdict_get_try_str(qdict
, "node-name")) {
3687 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3692 bs
= bds_tree_init(qdict
, errp
);
3698 if (bs
&& bdrv_key_required(bs
)) {
3704 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3709 qmp_output_visitor_cleanup(ov
);
3712 void qmp_x_blockdev_del(bool has_id
, const char *id
,
3713 bool has_node_name
, const char *node_name
, Error
**errp
)
3715 AioContext
*aio_context
;
3717 BlockDriverState
*bs
;
3719 if (has_id
&& has_node_name
) {
3720 error_setg(errp
, "Only one of id and node-name must be specified");
3722 } else if (!has_id
&& !has_node_name
) {
3723 error_setg(errp
, "No block device specified");
3728 blk
= blk_by_name(id
);
3730 error_setg(errp
, "Cannot find block backend %s", id
);
3733 if (blk_get_refcnt(blk
) > 1) {
3734 error_setg(errp
, "Block backend %s is in use", id
);
3738 aio_context
= blk_get_aio_context(blk
);
3740 bs
= bdrv_find_node(node_name
);
3742 error_setg(errp
, "Cannot find node %s", node_name
);
3747 error_setg(errp
, "Node %s is in use by %s",
3748 node_name
, blk_name(blk
));
3751 aio_context
= bdrv_get_aio_context(bs
);
3754 aio_context_acquire(aio_context
);
3757 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3761 if (bs
->refcnt
> 1 || !QLIST_EMPTY(&bs
->parents
)) {
3762 error_setg(errp
, "Block device %s is in use",
3763 bdrv_get_device_or_node_name(bs
));
3775 aio_context_release(aio_context
);
3778 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3780 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3781 BlockDriverState
*bs
;
3783 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3784 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
3786 aio_context_acquire(aio_context
);
3789 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
3790 elem
->value
= block_job_query(bs
->job
);
3792 p_next
= &elem
->next
;
3795 aio_context_release(aio_context
);
3801 QemuOptsList qemu_common_drive_opts
= {
3803 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3807 .type
= QEMU_OPT_BOOL
,
3808 .help
= "enable/disable snapshot mode",
3811 .type
= QEMU_OPT_STRING
,
3812 .help
= "discard operation (ignore/off, unmap/on)",
3814 .name
= BDRV_OPT_CACHE_WB
,
3815 .type
= QEMU_OPT_BOOL
,
3816 .help
= "enables writeback mode for any caches",
3818 .name
= BDRV_OPT_CACHE_DIRECT
,
3819 .type
= QEMU_OPT_BOOL
,
3820 .help
= "enables use of O_DIRECT (bypass the host page cache)",
3822 .name
= BDRV_OPT_CACHE_NO_FLUSH
,
3823 .type
= QEMU_OPT_BOOL
,
3824 .help
= "ignore any flush requests for the device",
3827 .type
= QEMU_OPT_STRING
,
3828 .help
= "host AIO implementation (threads, native)",
3831 .type
= QEMU_OPT_STRING
,
3832 .help
= "disk format (raw, qcow2, ...)",
3835 .type
= QEMU_OPT_STRING
,
3836 .help
= "read error action",
3839 .type
= QEMU_OPT_STRING
,
3840 .help
= "write error action",
3842 .name
= "read-only",
3843 .type
= QEMU_OPT_BOOL
,
3844 .help
= "open drive file as read-only",
3846 .name
= "throttling.iops-total",
3847 .type
= QEMU_OPT_NUMBER
,
3848 .help
= "limit total I/O operations per second",
3850 .name
= "throttling.iops-read",
3851 .type
= QEMU_OPT_NUMBER
,
3852 .help
= "limit read operations per second",
3854 .name
= "throttling.iops-write",
3855 .type
= QEMU_OPT_NUMBER
,
3856 .help
= "limit write operations per second",
3858 .name
= "throttling.bps-total",
3859 .type
= QEMU_OPT_NUMBER
,
3860 .help
= "limit total bytes per second",
3862 .name
= "throttling.bps-read",
3863 .type
= QEMU_OPT_NUMBER
,
3864 .help
= "limit read bytes per second",
3866 .name
= "throttling.bps-write",
3867 .type
= QEMU_OPT_NUMBER
,
3868 .help
= "limit write bytes per second",
3870 .name
= "throttling.iops-total-max",
3871 .type
= QEMU_OPT_NUMBER
,
3872 .help
= "I/O operations burst",
3874 .name
= "throttling.iops-read-max",
3875 .type
= QEMU_OPT_NUMBER
,
3876 .help
= "I/O operations read burst",
3878 .name
= "throttling.iops-write-max",
3879 .type
= QEMU_OPT_NUMBER
,
3880 .help
= "I/O operations write burst",
3882 .name
= "throttling.bps-total-max",
3883 .type
= QEMU_OPT_NUMBER
,
3884 .help
= "total bytes burst",
3886 .name
= "throttling.bps-read-max",
3887 .type
= QEMU_OPT_NUMBER
,
3888 .help
= "total bytes read burst",
3890 .name
= "throttling.bps-write-max",
3891 .type
= QEMU_OPT_NUMBER
,
3892 .help
= "total bytes write burst",
3894 .name
= "throttling.iops-size",
3895 .type
= QEMU_OPT_NUMBER
,
3896 .help
= "when limiting by iops max size of an I/O in bytes",
3898 .name
= "throttling.group",
3899 .type
= QEMU_OPT_STRING
,
3900 .help
= "name of the block throttling group",
3902 .name
= "copy-on-read",
3903 .type
= QEMU_OPT_BOOL
,
3904 .help
= "copy read data from backing file into image file",
3906 .name
= "detect-zeroes",
3907 .type
= QEMU_OPT_STRING
,
3908 .help
= "try to optimize zero writes (off, on, unmap)",
3910 .name
= "stats-account-invalid",
3911 .type
= QEMU_OPT_BOOL
,
3912 .help
= "whether to account for invalid I/O operations "
3913 "in the statistics",
3915 .name
= "stats-account-failed",
3916 .type
= QEMU_OPT_BOOL
,
3917 .help
= "whether to account for failed I/O operations "
3918 "in the statistics",
3920 { /* end of list */ }
3924 static QemuOptsList qemu_root_bds_opts
= {
3926 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3930 .type
= QEMU_OPT_STRING
,
3931 .help
= "discard operation (ignore/off, unmap/on)",
3933 .name
= "cache.writeback",
3934 .type
= QEMU_OPT_BOOL
,
3935 .help
= "enables writeback mode for any caches",
3937 .name
= "cache.direct",
3938 .type
= QEMU_OPT_BOOL
,
3939 .help
= "enables use of O_DIRECT (bypass the host page cache)",
3941 .name
= "cache.no-flush",
3942 .type
= QEMU_OPT_BOOL
,
3943 .help
= "ignore any flush requests for the device",
3946 .type
= QEMU_OPT_STRING
,
3947 .help
= "host AIO implementation (threads, native)",
3949 .name
= "read-only",
3950 .type
= QEMU_OPT_BOOL
,
3951 .help
= "open drive file as read-only",
3953 .name
= "copy-on-read",
3954 .type
= QEMU_OPT_BOOL
,
3955 .help
= "copy read data from backing file into image file",
3957 .name
= "detect-zeroes",
3958 .type
= QEMU_OPT_STRING
,
3959 .help
= "try to optimize zero writes (off, on, unmap)",
3961 { /* end of list */ }
3965 QemuOptsList qemu_drive_opts
= {
3967 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
3970 * no elements => accept any params
3971 * validation will happen later
3973 { /* end of list */ }