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 parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
306 const QListEntry
*entry
;
307 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
308 switch (qobject_type(entry
->value
)) {
310 case QTYPE_QSTRING
: {
311 unsigned long long length
;
312 const char *str
= qstring_get_str(qobject_to_qstring(entry
->value
));
313 if (parse_uint_full(str
, &length
, 10) == 0 &&
314 length
> 0 && length
<= UINT_MAX
) {
315 block_acct_add_interval(stats
, (unsigned) length
);
317 error_setg(errp
, "Invalid interval length: %s", str
);
324 int64_t length
= qint_get_int(qobject_to_qint(entry
->value
));
325 if (length
> 0 && length
<= UINT_MAX
) {
326 block_acct_add_interval(stats
, (unsigned) length
);
328 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
335 error_setg(errp
, "The specification of stats-intervals is invalid");
342 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
344 if (throttle_conflicting(cfg
)) {
345 error_setg(errp
, "bps/iops/max total values and read/write values"
346 " cannot be used at the same time");
350 if (!throttle_is_valid(cfg
)) {
351 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
355 if (throttle_max_is_missing_limit(cfg
)) {
356 error_setg(errp
, "bps_max/iops_max require corresponding"
364 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
366 /* All parameters but @opts are optional and may be set to NULL. */
367 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
368 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
369 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
372 Error
*local_error
= NULL
;
376 if (!qemu_opt_get_bool(opts
, "read-only", false)) {
377 *bdrv_flags
|= BDRV_O_RDWR
;
379 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
380 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
383 if ((discard
= qemu_opt_get(opts
, "discard")) != NULL
) {
384 if (bdrv_parse_discard_flags(discard
, bdrv_flags
) != 0) {
385 error_setg(errp
, "Invalid discard option");
390 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true)) {
391 *bdrv_flags
|= BDRV_O_CACHE_WB
;
393 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_DIRECT
, false)) {
394 *bdrv_flags
|= BDRV_O_NOCACHE
;
396 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_NO_FLUSH
, false)) {
397 *bdrv_flags
|= BDRV_O_NO_FLUSH
;
400 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
401 if (!strcmp(aio
, "native")) {
402 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
403 } else if (!strcmp(aio
, "threads")) {
404 /* this is the default */
406 error_setg(errp
, "invalid aio option");
412 /* disk I/O throttling */
413 if (throttling_group
) {
414 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
418 memset(throttle_cfg
, 0, sizeof(*throttle_cfg
));
419 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
420 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
421 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
422 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
423 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
424 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
425 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
426 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
427 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
428 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
429 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
430 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
432 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
433 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
434 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
435 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
436 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
437 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
438 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
439 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
440 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
441 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
442 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
443 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
445 throttle_cfg
->op_size
=
446 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
448 if (!check_throttle_config(throttle_cfg
, errp
)) {
455 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
456 qemu_opt_get(opts
, "detect-zeroes"),
457 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX
,
458 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
461 error_propagate(errp
, local_error
);
466 *detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
467 !(*bdrv_flags
& BDRV_O_UNMAP
))
469 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
470 "without setting discard operation to unmap");
476 /* Takes the ownership of bs_opts */
477 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
482 int on_read_error
, on_write_error
;
483 bool account_invalid
, account_failed
;
485 BlockDriverState
*bs
;
490 QDict
*interval_dict
= NULL
;
491 QList
*interval_list
= NULL
;
493 bool has_driver_specific_opts
;
494 BlockdevDetectZeroesOptions detect_zeroes
=
495 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
496 const char *throttling_group
= NULL
;
498 /* Check common options by copying from bs_opts to opts, all other options
499 * stay in bs_opts for processing by bdrv_open(). */
500 id
= qdict_get_try_str(bs_opts
, "id");
501 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
503 error_propagate(errp
, error
);
507 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
509 error_propagate(errp
, error
);
514 qdict_del(bs_opts
, "id");
517 has_driver_specific_opts
= !!qdict_size(bs_opts
);
519 /* extract parameters */
520 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
522 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
523 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
525 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
526 qdict_array_split(interval_dict
, &interval_list
);
528 if (qdict_size(interval_dict
) != 0) {
529 error_setg(errp
, "Invalid option stats-intervals.%s",
530 qdict_first(interval_dict
)->key
);
534 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
535 &detect_zeroes
, &error
);
537 error_propagate(errp
, error
);
541 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
542 if (is_help_option(buf
)) {
543 error_printf("Supported formats:");
544 bdrv_iterate_format(bdrv_format_print
, NULL
);
549 if (qdict_haskey(bs_opts
, "driver")) {
550 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
553 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
556 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
557 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
558 on_write_error
= parse_block_error_action(buf
, 0, &error
);
560 error_propagate(errp
, error
);
565 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
566 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
567 on_read_error
= parse_block_error_action(buf
, 1, &error
);
569 error_propagate(errp
, error
);
575 /* always use cache=unsafe with snapshot */
576 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
577 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
581 if ((!file
|| !*file
) && !has_driver_specific_opts
) {
582 BlockBackendRootState
*blk_rs
;
584 blk
= blk_new(qemu_opts_id(opts
), errp
);
589 blk_rs
= blk_get_root_state(blk
);
590 blk_rs
->open_flags
= bdrv_flags
;
591 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
592 blk_rs
->detect_zeroes
= detect_zeroes
;
594 if (throttle_enabled(&cfg
)) {
595 if (!throttling_group
) {
596 throttling_group
= blk_name(blk
);
598 blk_rs
->throttle_group
= g_strdup(throttling_group
);
599 blk_rs
->throttle_state
= throttle_group_incref(throttling_group
);
600 blk_rs
->throttle_state
->cfg
= cfg
;
605 if (file
&& !*file
) {
609 blk
= blk_new_open(qemu_opts_id(opts
), file
, NULL
, bs_opts
, bdrv_flags
,
616 bs
->detect_zeroes
= detect_zeroes
;
618 /* disk I/O throttling */
619 if (throttle_enabled(&cfg
)) {
620 if (!throttling_group
) {
621 throttling_group
= blk_name(blk
);
623 bdrv_io_limits_enable(bs
, throttling_group
);
624 bdrv_set_io_limits(bs
, &cfg
);
627 if (bdrv_key_required(bs
)) {
631 block_acct_init(blk_get_stats(blk
), account_invalid
, account_failed
);
633 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
640 blk_set_on_error(blk
, on_read_error
, on_write_error
);
644 QDECREF(interval_dict
);
645 QDECREF(interval_list
);
650 QDECREF(interval_dict
);
651 QDECREF(interval_list
);
657 static QemuOptsList qemu_root_bds_opts
;
659 /* Takes the ownership of bs_opts */
660 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
662 BlockDriverState
*bs
;
664 Error
*local_error
= NULL
;
665 BlockdevDetectZeroesOptions detect_zeroes
;
669 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
674 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
676 error_propagate(errp
, local_error
);
680 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
681 &detect_zeroes
, &local_error
);
683 error_propagate(errp
, local_error
);
688 ret
= bdrv_open(&bs
, NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
690 goto fail_no_bs_opts
;
693 bs
->detect_zeroes
= detect_zeroes
;
705 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
710 value
= qemu_opt_get(opts
, from
);
712 if (qemu_opt_find(opts
, to
)) {
713 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
714 "same time", to
, from
);
719 /* rename all items in opts */
720 while ((value
= qemu_opt_get(opts
, from
))) {
721 qemu_opt_set(opts
, to
, value
, &error_abort
);
722 qemu_opt_unset(opts
, from
);
726 QemuOptsList qemu_legacy_drive_opts
= {
728 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
732 .type
= QEMU_OPT_NUMBER
,
733 .help
= "bus number",
736 .type
= QEMU_OPT_NUMBER
,
737 .help
= "unit number (i.e. lun for scsi)",
740 .type
= QEMU_OPT_NUMBER
,
741 .help
= "index number",
744 .type
= QEMU_OPT_STRING
,
745 .help
= "media type (disk, cdrom)",
748 .type
= QEMU_OPT_STRING
,
749 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
752 .type
= QEMU_OPT_NUMBER
,
753 .help
= "number of cylinders (ide disk geometry)",
756 .type
= QEMU_OPT_NUMBER
,
757 .help
= "number of heads (ide disk geometry)",
760 .type
= QEMU_OPT_NUMBER
,
761 .help
= "number of sectors (ide disk geometry)",
764 .type
= QEMU_OPT_STRING
,
765 .help
= "chs translation (auto, lba, none)",
768 .type
= QEMU_OPT_BOOL
,
769 .help
= "(deprecated, ignored)",
772 .type
= QEMU_OPT_STRING
,
773 .help
= "pci address (virtio only)",
776 .type
= QEMU_OPT_STRING
,
777 .help
= "disk serial number",
780 .type
= QEMU_OPT_STRING
,
784 /* Options that are passed on, but have special semantics with -drive */
787 .type
= QEMU_OPT_BOOL
,
788 .help
= "open drive file as read-only",
791 .type
= QEMU_OPT_STRING
,
792 .help
= "read error action",
795 .type
= QEMU_OPT_STRING
,
796 .help
= "write error action",
798 .name
= "copy-on-read",
799 .type
= QEMU_OPT_BOOL
,
800 .help
= "copy read data from backing file into image file",
803 { /* end of list */ }
807 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
811 DriveInfo
*dinfo
= NULL
;
813 QemuOpts
*legacy_opts
;
814 DriveMediaType media
= MEDIA_DISK
;
815 BlockInterfaceType type
;
816 int cyls
, heads
, secs
, translation
;
817 int max_devs
, bus_id
, unit_id
, index
;
819 const char *werror
, *rerror
;
820 bool read_only
= false;
823 const char *filename
;
824 Error
*local_err
= NULL
;
827 /* Change legacy command line options into QMP ones */
828 static const struct {
832 { "iops", "throttling.iops-total" },
833 { "iops_rd", "throttling.iops-read" },
834 { "iops_wr", "throttling.iops-write" },
836 { "bps", "throttling.bps-total" },
837 { "bps_rd", "throttling.bps-read" },
838 { "bps_wr", "throttling.bps-write" },
840 { "iops_max", "throttling.iops-total-max" },
841 { "iops_rd_max", "throttling.iops-read-max" },
842 { "iops_wr_max", "throttling.iops-write-max" },
844 { "bps_max", "throttling.bps-total-max" },
845 { "bps_rd_max", "throttling.bps-read-max" },
846 { "bps_wr_max", "throttling.bps-write-max" },
848 { "iops_size", "throttling.iops-size" },
850 { "group", "throttling.group" },
852 { "readonly", "read-only" },
855 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
856 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
859 error_report_err(local_err
);
864 value
= qemu_opt_get(all_opts
, "cache");
868 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
869 error_report("invalid cache option");
873 /* Specific options take precedence */
874 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
875 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
876 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
878 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
879 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
880 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
882 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
883 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
884 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
886 qemu_opt_unset(all_opts
, "cache");
889 /* Get a QDict for processing the options */
890 bs_opts
= qdict_new();
891 qemu_opts_to_qdict(all_opts
, bs_opts
);
893 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
895 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
897 error_report_err(local_err
);
901 /* Deprecated option boot=[on|off] */
902 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
903 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
904 "ignored. Future versions will reject this parameter. Please "
905 "update your scripts.\n");
909 value
= qemu_opt_get(legacy_opts
, "media");
911 if (!strcmp(value
, "disk")) {
913 } else if (!strcmp(value
, "cdrom")) {
917 error_report("'%s' invalid media", value
);
922 /* copy-on-read is disabled with a warning for read-only devices */
923 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
924 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
926 if (read_only
&& copy_on_read
) {
927 error_report("warning: disabling copy-on-read on read-only drive");
928 copy_on_read
= false;
931 qdict_put(bs_opts
, "read-only",
932 qstring_from_str(read_only
? "on" : "off"));
933 qdict_put(bs_opts
, "copy-on-read",
934 qstring_from_str(copy_on_read
? "on" :"off"));
936 /* Controller type */
937 value
= qemu_opt_get(legacy_opts
, "if");
940 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
943 if (type
== IF_COUNT
) {
944 error_report("unsupported bus type '%s'", value
);
948 type
= block_default_type
;
952 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
953 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
954 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
956 if (cyls
|| heads
|| secs
) {
958 error_report("invalid physical cyls number");
962 error_report("invalid physical heads number");
966 error_report("invalid physical secs number");
971 translation
= BIOS_ATA_TRANSLATION_AUTO
;
972 value
= qemu_opt_get(legacy_opts
, "trans");
975 error_report("'%s' trans must be used with cyls, heads and secs",
979 if (!strcmp(value
, "none")) {
980 translation
= BIOS_ATA_TRANSLATION_NONE
;
981 } else if (!strcmp(value
, "lba")) {
982 translation
= BIOS_ATA_TRANSLATION_LBA
;
983 } else if (!strcmp(value
, "large")) {
984 translation
= BIOS_ATA_TRANSLATION_LARGE
;
985 } else if (!strcmp(value
, "rechs")) {
986 translation
= BIOS_ATA_TRANSLATION_RECHS
;
987 } else if (!strcmp(value
, "auto")) {
988 translation
= BIOS_ATA_TRANSLATION_AUTO
;
990 error_report("'%s' invalid translation type", value
);
995 if (media
== MEDIA_CDROM
) {
996 if (cyls
|| secs
|| heads
) {
997 error_report("CHS can't be set with media=cdrom");
1002 /* Device address specified by bus/unit or index.
1003 * If none was specified, try to find the first free one. */
1004 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
1005 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
1006 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
1008 max_devs
= if_max_devs
[type
];
1011 if (bus_id
!= 0 || unit_id
!= -1) {
1012 error_report("index cannot be used with bus and unit");
1015 bus_id
= drive_index_to_bus_id(type
, index
);
1016 unit_id
= drive_index_to_unit_id(type
, index
);
1019 if (unit_id
== -1) {
1021 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1023 if (max_devs
&& unit_id
>= max_devs
) {
1024 unit_id
-= max_devs
;
1030 if (max_devs
&& unit_id
>= max_devs
) {
1031 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1035 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1036 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1037 bus_id
, unit_id
, index
);
1042 serial
= qemu_opt_get(legacy_opts
, "serial");
1044 /* no id supplied -> create one */
1045 if (qemu_opts_id(all_opts
) == NULL
) {
1047 const char *mediastr
= "";
1048 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1049 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1052 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1055 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1058 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1062 /* Add virtio block device */
1063 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1064 if (devaddr
&& type
!= IF_VIRTIO
) {
1065 error_report("addr is not supported by this bus type");
1069 if (type
== IF_VIRTIO
) {
1071 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1073 if (arch_type
== QEMU_ARCH_S390X
) {
1074 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1076 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1078 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1081 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1085 filename
= qemu_opt_get(legacy_opts
, "file");
1087 /* Check werror/rerror compatibility with if=... */
1088 werror
= qemu_opt_get(legacy_opts
, "werror");
1089 if (werror
!= NULL
) {
1090 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1092 error_report("werror is not supported by this bus type");
1095 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1098 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1099 if (rerror
!= NULL
) {
1100 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1102 error_report("rerror is not supported by this bus type");
1105 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1108 /* Actual block device init: Functionality shared with blockdev-add */
1109 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1113 error_report_err(local_err
);
1120 /* Create legacy DriveInfo */
1121 dinfo
= g_malloc0(sizeof(*dinfo
));
1122 dinfo
->opts
= all_opts
;
1125 dinfo
->heads
= heads
;
1127 dinfo
->trans
= translation
;
1130 dinfo
->bus
= bus_id
;
1131 dinfo
->unit
= unit_id
;
1132 dinfo
->devaddr
= devaddr
;
1133 dinfo
->serial
= g_strdup(serial
);
1135 blk_set_legacy_dinfo(blk
, dinfo
);
1142 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1149 qemu_opts_del(legacy_opts
);
1154 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1156 const char *device
= qdict_get_str(qdict
, "device");
1160 if (!strcmp(device
, "all")) {
1161 ret
= bdrv_commit_all();
1163 BlockDriverState
*bs
;
1164 AioContext
*aio_context
;
1166 blk
= blk_by_name(device
);
1168 monitor_printf(mon
, "Device '%s' not found\n", device
);
1171 if (!blk_is_available(blk
)) {
1172 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1177 aio_context
= bdrv_get_aio_context(bs
);
1178 aio_context_acquire(aio_context
);
1180 ret
= bdrv_commit(bs
);
1182 aio_context_release(aio_context
);
1185 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1190 static void blockdev_do_action(TransactionActionKind type
, void *data
,
1193 TransactionAction action
;
1194 TransactionActionList list
;
1197 action
.u
.data
= data
;
1198 list
.value
= &action
;
1200 qmp_transaction(&list
, false, NULL
, errp
);
1203 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1204 bool has_node_name
, const char *node_name
,
1205 const char *snapshot_file
,
1206 bool has_snapshot_node_name
,
1207 const char *snapshot_node_name
,
1208 bool has_format
, const char *format
,
1209 bool has_mode
, NewImageMode mode
, Error
**errp
)
1211 BlockdevSnapshotSync snapshot
= {
1212 .has_device
= has_device
,
1213 .device
= (char *) device
,
1214 .has_node_name
= has_node_name
,
1215 .node_name
= (char *) node_name
,
1216 .snapshot_file
= (char *) snapshot_file
,
1217 .has_snapshot_node_name
= has_snapshot_node_name
,
1218 .snapshot_node_name
= (char *) snapshot_node_name
,
1219 .has_format
= has_format
,
1220 .format
= (char *) format
,
1221 .has_mode
= has_mode
,
1224 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1228 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1231 BlockdevSnapshot snapshot_data
= {
1232 .node
= (char *) node
,
1233 .overlay
= (char *) overlay
1236 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1237 &snapshot_data
, errp
);
1240 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1244 BlockdevSnapshotInternal snapshot
= {
1245 .device
= (char *) device
,
1246 .name
= (char *) name
1249 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1253 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1260 BlockDriverState
*bs
;
1262 AioContext
*aio_context
;
1263 QEMUSnapshotInfo sn
;
1264 Error
*local_err
= NULL
;
1265 SnapshotInfo
*info
= NULL
;
1268 blk
= blk_by_name(device
);
1270 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1271 "Device '%s' not found", device
);
1275 aio_context
= blk_get_aio_context(blk
);
1276 aio_context_acquire(aio_context
);
1287 error_setg(errp
, "Name or id must be provided");
1288 goto out_aio_context
;
1291 if (!blk_is_available(blk
)) {
1292 error_setg(errp
, "Device '%s' has no medium", device
);
1293 goto out_aio_context
;
1297 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1298 goto out_aio_context
;
1301 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1303 error_propagate(errp
, local_err
);
1304 goto out_aio_context
;
1308 "Snapshot with id '%s' and name '%s' does not exist on "
1310 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1311 goto out_aio_context
;
1314 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1316 error_propagate(errp
, local_err
);
1317 goto out_aio_context
;
1320 aio_context_release(aio_context
);
1322 info
= g_new0(SnapshotInfo
, 1);
1323 info
->id
= g_strdup(sn
.id_str
);
1324 info
->name
= g_strdup(sn
.name
);
1325 info
->date_nsec
= sn
.date_nsec
;
1326 info
->date_sec
= sn
.date_sec
;
1327 info
->vm_state_size
= sn
.vm_state_size
;
1328 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1329 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1334 aio_context_release(aio_context
);
1339 * block_dirty_bitmap_lookup:
1340 * Return a dirty bitmap (if present), after validating
1341 * the node reference and bitmap names.
1343 * @node: The name of the BDS node to search for bitmaps
1344 * @name: The name of the bitmap to search for
1345 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1346 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1347 * @errp: Output pointer for error information. Can be NULL.
1349 * @return: A bitmap object on success, or NULL on failure.
1351 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1353 BlockDriverState
**pbs
,
1357 BlockDriverState
*bs
;
1358 BdrvDirtyBitmap
*bitmap
;
1359 AioContext
*aio_context
;
1362 error_setg(errp
, "Node cannot be NULL");
1366 error_setg(errp
, "Bitmap name cannot be NULL");
1369 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1371 error_setg(errp
, "Node '%s' not found", node
);
1375 aio_context
= bdrv_get_aio_context(bs
);
1376 aio_context_acquire(aio_context
);
1378 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1380 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1388 *paio
= aio_context
;
1390 aio_context_release(aio_context
);
1396 aio_context_release(aio_context
);
1400 /* New and old BlockDriverState structs for atomic group operations */
1402 typedef struct BlkActionState BlkActionState
;
1406 * Table of operations that define an Action.
1408 * @instance_size: Size of state struct, in bytes.
1409 * @prepare: Prepare the work, must NOT be NULL.
1410 * @commit: Commit the changes, can be NULL.
1411 * @abort: Abort the changes on fail, can be NULL.
1412 * @clean: Clean up resources after all transaction actions have called
1413 * commit() or abort(). Can be NULL.
1415 * Only prepare() may fail. In a single transaction, only one of commit() or
1416 * abort() will be called. clean() will always be called if it is present.
1418 typedef struct BlkActionOps
{
1419 size_t instance_size
;
1420 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1421 void (*commit
)(BlkActionState
*common
);
1422 void (*abort
)(BlkActionState
*common
);
1423 void (*clean
)(BlkActionState
*common
);
1428 * Describes one Action's state within a Transaction.
1430 * @action: QAPI-defined enum identifying which Action to perform.
1431 * @ops: Table of ActionOps this Action can perform.
1432 * @block_job_txn: Transaction which this action belongs to.
1433 * @entry: List membership for all Actions in this Transaction.
1435 * This structure must be arranged as first member in a subclassed type,
1436 * assuming that the compiler will also arrange it to the same offsets as the
1439 struct BlkActionState
{
1440 TransactionAction
*action
;
1441 const BlkActionOps
*ops
;
1442 BlockJobTxn
*block_job_txn
;
1443 TransactionProperties
*txn_props
;
1444 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1447 /* internal snapshot private data */
1448 typedef struct InternalSnapshotState
{
1449 BlkActionState common
;
1450 BlockDriverState
*bs
;
1451 AioContext
*aio_context
;
1452 QEMUSnapshotInfo sn
;
1454 } InternalSnapshotState
;
1457 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1459 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1461 "Action '%s' does not support Transaction property "
1462 "completion-mode = %s",
1463 TransactionActionKind_lookup
[s
->action
->type
],
1464 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1470 static void internal_snapshot_prepare(BlkActionState
*common
,
1473 Error
*local_err
= NULL
;
1477 BlockDriverState
*bs
;
1478 QEMUSnapshotInfo old_sn
, *sn
;
1481 BlockdevSnapshotInternal
*internal
;
1482 InternalSnapshotState
*state
;
1485 g_assert(common
->action
->type
==
1486 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1487 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
;
1488 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1490 /* 1. parse input */
1491 device
= internal
->device
;
1492 name
= internal
->name
;
1494 /* 2. check for validation */
1495 if (action_check_completion_mode(common
, errp
) < 0) {
1499 blk
= blk_by_name(device
);
1501 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1502 "Device '%s' not found", device
);
1506 /* AioContext is released in .clean() */
1507 state
->aio_context
= blk_get_aio_context(blk
);
1508 aio_context_acquire(state
->aio_context
);
1510 if (!blk_is_available(blk
)) {
1511 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1517 bdrv_drained_begin(bs
);
1519 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1523 if (bdrv_is_read_only(bs
)) {
1524 error_setg(errp
, "Device '%s' is read only", device
);
1528 if (!bdrv_can_snapshot(bs
)) {
1529 error_setg(errp
, "Block format '%s' used by device '%s' "
1530 "does not support internal snapshots",
1531 bs
->drv
->format_name
, device
);
1535 if (!strlen(name
)) {
1536 error_setg(errp
, "Name is empty");
1540 /* check whether a snapshot with name exist */
1541 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1544 error_propagate(errp
, local_err
);
1548 "Snapshot with name '%s' already exists on device '%s'",
1553 /* 3. take the snapshot */
1555 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1556 qemu_gettimeofday(&tv
);
1557 sn
->date_sec
= tv
.tv_sec
;
1558 sn
->date_nsec
= tv
.tv_usec
* 1000;
1559 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1561 ret1
= bdrv_snapshot_create(bs
, sn
);
1563 error_setg_errno(errp
, -ret1
,
1564 "Failed to create snapshot '%s' on device '%s'",
1569 /* 4. succeed, mark a snapshot is created */
1570 state
->created
= true;
1573 static void internal_snapshot_abort(BlkActionState
*common
)
1575 InternalSnapshotState
*state
=
1576 DO_UPCAST(InternalSnapshotState
, common
, common
);
1577 BlockDriverState
*bs
= state
->bs
;
1578 QEMUSnapshotInfo
*sn
= &state
->sn
;
1579 Error
*local_error
= NULL
;
1581 if (!state
->created
) {
1585 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1586 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1587 "device '%s' in abort: %s",
1590 bdrv_get_device_name(bs
),
1591 error_get_pretty(local_error
));
1592 error_free(local_error
);
1596 static void internal_snapshot_clean(BlkActionState
*common
)
1598 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1601 if (state
->aio_context
) {
1603 bdrv_drained_end(state
->bs
);
1605 aio_context_release(state
->aio_context
);
1609 /* external snapshot private data */
1610 typedef struct ExternalSnapshotState
{
1611 BlkActionState common
;
1612 BlockDriverState
*old_bs
;
1613 BlockDriverState
*new_bs
;
1614 AioContext
*aio_context
;
1615 } ExternalSnapshotState
;
1617 static void external_snapshot_prepare(BlkActionState
*common
,
1621 QDict
*options
= NULL
;
1622 Error
*local_err
= NULL
;
1623 /* Device and node name of the image to generate the snapshot from */
1625 const char *node_name
;
1626 /* Reference to the new image (for 'blockdev-snapshot') */
1627 const char *snapshot_ref
;
1628 /* File name of the new image (for 'blockdev-snapshot-sync') */
1629 const char *new_image_file
;
1630 ExternalSnapshotState
*state
=
1631 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1632 TransactionAction
*action
= common
->action
;
1634 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1635 * purpose but a different set of parameters */
1636 switch (action
->type
) {
1637 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1639 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
;
1641 node_name
= s
->node
;
1642 new_image_file
= NULL
;
1643 snapshot_ref
= s
->overlay
;
1646 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1648 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1649 device
= s
->has_device
? s
->device
: NULL
;
1650 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1651 new_image_file
= s
->snapshot_file
;
1652 snapshot_ref
= NULL
;
1656 g_assert_not_reached();
1659 /* start processing */
1660 if (action_check_completion_mode(common
, errp
) < 0) {
1664 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1665 if (!state
->old_bs
) {
1669 /* Acquire AioContext now so any threads operating on old_bs stop */
1670 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1671 aio_context_acquire(state
->aio_context
);
1672 bdrv_drained_begin(state
->old_bs
);
1674 if (!bdrv_is_inserted(state
->old_bs
)) {
1675 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1679 if (bdrv_op_is_blocked(state
->old_bs
,
1680 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1684 if (!bdrv_is_read_only(state
->old_bs
)) {
1685 if (bdrv_flush(state
->old_bs
)) {
1686 error_setg(errp
, QERR_IO_ERROR
);
1691 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1692 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1696 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1697 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1698 const char *format
= s
->has_format
? s
->format
: "qcow2";
1699 enum NewImageMode mode
;
1700 const char *snapshot_node_name
=
1701 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1703 if (node_name
&& !snapshot_node_name
) {
1704 error_setg(errp
, "New snapshot node name missing");
1708 if (snapshot_node_name
&&
1709 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1710 error_setg(errp
, "New snapshot node name already in use");
1714 flags
= state
->old_bs
->open_flags
;
1716 /* create new image w/backing file */
1717 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1718 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1719 bdrv_img_create(new_image_file
, format
,
1720 state
->old_bs
->filename
,
1721 state
->old_bs
->drv
->format_name
,
1722 NULL
, -1, flags
, &local_err
, false);
1724 error_propagate(errp
, local_err
);
1729 options
= qdict_new();
1730 if (s
->has_snapshot_node_name
) {
1731 qdict_put(options
, "node-name",
1732 qstring_from_str(snapshot_node_name
));
1734 qdict_put(options
, "driver", qstring_from_str(format
));
1736 flags
|= BDRV_O_NO_BACKING
;
1739 assert(state
->new_bs
== NULL
);
1740 ret
= bdrv_open(&state
->new_bs
, new_image_file
, snapshot_ref
, options
,
1742 /* We will manually add the backing_hd field to the bs later */
1747 if (state
->new_bs
->blk
!= NULL
) {
1748 error_setg(errp
, "The snapshot is already in use by %s",
1749 blk_name(state
->new_bs
->blk
));
1753 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1758 if (state
->new_bs
->backing
!= NULL
) {
1759 error_setg(errp
, "The snapshot already has a backing image");
1763 if (!state
->new_bs
->drv
->supports_backing
) {
1764 error_setg(errp
, "The snapshot does not support backing images");
1768 static void external_snapshot_commit(BlkActionState
*common
)
1770 ExternalSnapshotState
*state
=
1771 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1773 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1775 /* This removes our old bs and adds the new bs */
1776 bdrv_append(state
->new_bs
, state
->old_bs
);
1777 /* We don't need (or want) to use the transactional
1778 * bdrv_reopen_multiple() across all the entries at once, because we
1779 * don't want to abort all of them if one of them fails the reopen */
1780 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1784 static void external_snapshot_abort(BlkActionState
*common
)
1786 ExternalSnapshotState
*state
=
1787 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1788 if (state
->new_bs
) {
1789 bdrv_unref(state
->new_bs
);
1793 static void external_snapshot_clean(BlkActionState
*common
)
1795 ExternalSnapshotState
*state
=
1796 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1797 if (state
->aio_context
) {
1798 bdrv_drained_end(state
->old_bs
);
1799 aio_context_release(state
->aio_context
);
1803 typedef struct DriveBackupState
{
1804 BlkActionState common
;
1805 BlockDriverState
*bs
;
1806 AioContext
*aio_context
;
1810 static void do_drive_backup(const char *device
, const char *target
,
1811 bool has_format
, const char *format
,
1812 enum MirrorSyncMode sync
,
1813 bool has_mode
, enum NewImageMode mode
,
1814 bool has_speed
, int64_t speed
,
1815 bool has_bitmap
, const char *bitmap
,
1816 bool has_on_source_error
,
1817 BlockdevOnError on_source_error
,
1818 bool has_on_target_error
,
1819 BlockdevOnError on_target_error
,
1820 BlockJobTxn
*txn
, Error
**errp
);
1822 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1824 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1826 DriveBackup
*backup
;
1827 Error
*local_err
= NULL
;
1829 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1830 backup
= common
->action
->u
.drive_backup
;
1832 blk
= blk_by_name(backup
->device
);
1834 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1835 "Device '%s' not found", backup
->device
);
1839 if (!blk_is_available(blk
)) {
1840 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1844 /* AioContext is released in .clean() */
1845 state
->aio_context
= blk_get_aio_context(blk
);
1846 aio_context_acquire(state
->aio_context
);
1847 bdrv_drained_begin(blk_bs(blk
));
1848 state
->bs
= blk_bs(blk
);
1850 do_drive_backup(backup
->device
, backup
->target
,
1851 backup
->has_format
, backup
->format
,
1853 backup
->has_mode
, backup
->mode
,
1854 backup
->has_speed
, backup
->speed
,
1855 backup
->has_bitmap
, backup
->bitmap
,
1856 backup
->has_on_source_error
, backup
->on_source_error
,
1857 backup
->has_on_target_error
, backup
->on_target_error
,
1858 common
->block_job_txn
, &local_err
);
1860 error_propagate(errp
, local_err
);
1864 state
->job
= state
->bs
->job
;
1867 static void drive_backup_abort(BlkActionState
*common
)
1869 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1870 BlockDriverState
*bs
= state
->bs
;
1872 /* Only cancel if it's the job we started */
1873 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1874 block_job_cancel_sync(bs
->job
);
1878 static void drive_backup_clean(BlkActionState
*common
)
1880 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1882 if (state
->aio_context
) {
1883 bdrv_drained_end(state
->bs
);
1884 aio_context_release(state
->aio_context
);
1888 typedef struct BlockdevBackupState
{
1889 BlkActionState common
;
1890 BlockDriverState
*bs
;
1892 AioContext
*aio_context
;
1893 } BlockdevBackupState
;
1895 static void do_blockdev_backup(const char *device
, const char *target
,
1896 enum MirrorSyncMode sync
,
1897 bool has_speed
, int64_t speed
,
1898 bool has_on_source_error
,
1899 BlockdevOnError on_source_error
,
1900 bool has_on_target_error
,
1901 BlockdevOnError on_target_error
,
1902 BlockJobTxn
*txn
, Error
**errp
);
1904 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1906 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1907 BlockdevBackup
*backup
;
1908 BlockBackend
*blk
, *target
;
1909 Error
*local_err
= NULL
;
1911 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1912 backup
= common
->action
->u
.blockdev_backup
;
1914 blk
= blk_by_name(backup
->device
);
1916 error_setg(errp
, "Device '%s' not found", backup
->device
);
1920 if (!blk_is_available(blk
)) {
1921 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1925 target
= blk_by_name(backup
->target
);
1927 error_setg(errp
, "Device '%s' not found", backup
->target
);
1931 /* AioContext is released in .clean() */
1932 state
->aio_context
= blk_get_aio_context(blk
);
1933 if (state
->aio_context
!= blk_get_aio_context(target
)) {
1934 state
->aio_context
= NULL
;
1935 error_setg(errp
, "Backup between two IO threads is not implemented");
1938 aio_context_acquire(state
->aio_context
);
1939 state
->bs
= blk_bs(blk
);
1940 bdrv_drained_begin(state
->bs
);
1942 do_blockdev_backup(backup
->device
, backup
->target
,
1944 backup
->has_speed
, backup
->speed
,
1945 backup
->has_on_source_error
, backup
->on_source_error
,
1946 backup
->has_on_target_error
, backup
->on_target_error
,
1947 common
->block_job_txn
, &local_err
);
1949 error_propagate(errp
, local_err
);
1953 state
->job
= state
->bs
->job
;
1956 static void blockdev_backup_abort(BlkActionState
*common
)
1958 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1959 BlockDriverState
*bs
= state
->bs
;
1961 /* Only cancel if it's the job we started */
1962 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1963 block_job_cancel_sync(bs
->job
);
1967 static void blockdev_backup_clean(BlkActionState
*common
)
1969 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1971 if (state
->aio_context
) {
1972 bdrv_drained_end(state
->bs
);
1973 aio_context_release(state
->aio_context
);
1977 typedef struct BlockDirtyBitmapState
{
1978 BlkActionState common
;
1979 BdrvDirtyBitmap
*bitmap
;
1980 BlockDriverState
*bs
;
1981 AioContext
*aio_context
;
1984 } BlockDirtyBitmapState
;
1986 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
1989 Error
*local_err
= NULL
;
1990 BlockDirtyBitmapAdd
*action
;
1991 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1994 if (action_check_completion_mode(common
, errp
) < 0) {
1998 action
= common
->action
->u
.block_dirty_bitmap_add
;
1999 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2000 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2001 action
->has_granularity
, action
->granularity
,
2005 state
->prepared
= true;
2007 error_propagate(errp
, local_err
);
2011 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2013 BlockDirtyBitmapAdd
*action
;
2014 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2017 action
= common
->action
->u
.block_dirty_bitmap_add
;
2018 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2019 * then the node reference and bitmap name must have been valid.
2021 if (state
->prepared
) {
2022 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2026 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2029 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2031 BlockDirtyBitmap
*action
;
2033 if (action_check_completion_mode(common
, errp
) < 0) {
2037 action
= common
->action
->u
.block_dirty_bitmap_clear
;
2038 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2041 &state
->aio_context
,
2043 if (!state
->bitmap
) {
2047 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2048 error_setg(errp
, "Cannot modify a frozen bitmap");
2050 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2051 error_setg(errp
, "Cannot clear a disabled bitmap");
2055 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2056 /* AioContext is released in .clean() */
2059 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2061 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2064 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2067 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2069 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2072 hbitmap_free(state
->backup
);
2075 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2077 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2080 if (state
->aio_context
) {
2081 aio_context_release(state
->aio_context
);
2085 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2087 error_setg(errp
, "Transaction aborted using Abort action");
2090 static void abort_commit(BlkActionState
*common
)
2092 g_assert_not_reached(); /* this action never succeeds */
2095 static const BlkActionOps actions
[] = {
2096 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2097 .instance_size
= sizeof(ExternalSnapshotState
),
2098 .prepare
= external_snapshot_prepare
,
2099 .commit
= external_snapshot_commit
,
2100 .abort
= external_snapshot_abort
,
2102 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2103 .instance_size
= sizeof(ExternalSnapshotState
),
2104 .prepare
= external_snapshot_prepare
,
2105 .commit
= external_snapshot_commit
,
2106 .abort
= external_snapshot_abort
,
2107 .clean
= external_snapshot_clean
,
2109 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2110 .instance_size
= sizeof(DriveBackupState
),
2111 .prepare
= drive_backup_prepare
,
2112 .abort
= drive_backup_abort
,
2113 .clean
= drive_backup_clean
,
2115 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2116 .instance_size
= sizeof(BlockdevBackupState
),
2117 .prepare
= blockdev_backup_prepare
,
2118 .abort
= blockdev_backup_abort
,
2119 .clean
= blockdev_backup_clean
,
2121 [TRANSACTION_ACTION_KIND_ABORT
] = {
2122 .instance_size
= sizeof(BlkActionState
),
2123 .prepare
= abort_prepare
,
2124 .commit
= abort_commit
,
2126 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2127 .instance_size
= sizeof(InternalSnapshotState
),
2128 .prepare
= internal_snapshot_prepare
,
2129 .abort
= internal_snapshot_abort
,
2130 .clean
= internal_snapshot_clean
,
2132 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2133 .instance_size
= sizeof(BlockDirtyBitmapState
),
2134 .prepare
= block_dirty_bitmap_add_prepare
,
2135 .abort
= block_dirty_bitmap_add_abort
,
2137 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2138 .instance_size
= sizeof(BlockDirtyBitmapState
),
2139 .prepare
= block_dirty_bitmap_clear_prepare
,
2140 .commit
= block_dirty_bitmap_clear_commit
,
2141 .abort
= block_dirty_bitmap_clear_abort
,
2142 .clean
= block_dirty_bitmap_clear_clean
,
2147 * Allocate a TransactionProperties structure if necessary, and fill
2148 * that structure with desired defaults if they are unset.
2150 static TransactionProperties
*get_transaction_properties(
2151 TransactionProperties
*props
)
2154 props
= g_new0(TransactionProperties
, 1);
2157 if (!props
->has_completion_mode
) {
2158 props
->has_completion_mode
= true;
2159 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2166 * 'Atomic' group operations. The operations are performed as a set, and if
2167 * any fail then we roll back all operations in the group.
2169 void qmp_transaction(TransactionActionList
*dev_list
,
2171 struct TransactionProperties
*props
,
2174 TransactionActionList
*dev_entry
= dev_list
;
2175 BlockJobTxn
*block_job_txn
= NULL
;
2176 BlkActionState
*state
, *next
;
2177 Error
*local_err
= NULL
;
2179 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2180 QSIMPLEQ_INIT(&snap_bdrv_states
);
2182 /* Does this transaction get canceled as a group on failure?
2183 * If not, we don't really need to make a BlockJobTxn.
2185 props
= get_transaction_properties(props
);
2186 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2187 block_job_txn
= block_job_txn_new();
2190 /* drain all i/o before any operations */
2193 /* We don't do anything in this loop that commits us to the operations */
2194 while (NULL
!= dev_entry
) {
2195 TransactionAction
*dev_info
= NULL
;
2196 const BlkActionOps
*ops
;
2198 dev_info
= dev_entry
->value
;
2199 dev_entry
= dev_entry
->next
;
2201 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2203 ops
= &actions
[dev_info
->type
];
2204 assert(ops
->instance_size
> 0);
2206 state
= g_malloc0(ops
->instance_size
);
2208 state
->action
= dev_info
;
2209 state
->block_job_txn
= block_job_txn
;
2210 state
->txn_props
= props
;
2211 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2213 state
->ops
->prepare(state
, &local_err
);
2215 error_propagate(errp
, local_err
);
2216 goto delete_and_fail
;
2220 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2221 if (state
->ops
->commit
) {
2222 state
->ops
->commit(state
);
2230 /* failure, and it is all-or-none; roll back all operations */
2231 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2232 if (state
->ops
->abort
) {
2233 state
->ops
->abort(state
);
2237 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2238 if (state
->ops
->clean
) {
2239 state
->ops
->clean(state
);
2244 qapi_free_TransactionProperties(props
);
2246 block_job_txn_unref(block_job_txn
);
2249 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
2251 Error
*local_err
= NULL
;
2253 qmp_blockdev_open_tray(device
, has_force
, force
, &local_err
);
2255 error_propagate(errp
, local_err
);
2259 qmp_blockdev_remove_medium(device
, errp
);
2262 void qmp_block_passwd(bool has_device
, const char *device
,
2263 bool has_node_name
, const char *node_name
,
2264 const char *password
, Error
**errp
)
2266 Error
*local_err
= NULL
;
2267 BlockDriverState
*bs
;
2268 AioContext
*aio_context
;
2270 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2271 has_node_name
? node_name
: NULL
,
2274 error_propagate(errp
, local_err
);
2278 aio_context
= bdrv_get_aio_context(bs
);
2279 aio_context_acquire(aio_context
);
2281 bdrv_add_key(bs
, password
, errp
);
2283 aio_context_release(aio_context
);
2286 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2296 blk
= blk_by_name(device
);
2298 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2299 "Device '%s' not found", device
);
2303 if (!blk_dev_has_removable_media(blk
)) {
2304 error_setg(errp
, "Device '%s' is not removable", device
);
2308 if (blk_dev_is_tray_open(blk
)) {
2312 locked
= blk_dev_is_medium_locked(blk
);
2314 blk_dev_eject_request(blk
, force
);
2317 if (!locked
|| force
) {
2318 blk_dev_change_media_cb(blk
, false);
2322 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2326 blk
= blk_by_name(device
);
2328 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2329 "Device '%s' not found", device
);
2333 if (!blk_dev_has_removable_media(blk
)) {
2334 error_setg(errp
, "Device '%s' is not removable", device
);
2338 if (!blk_dev_is_tray_open(blk
)) {
2342 blk_dev_change_media_cb(blk
, true);
2345 void qmp_blockdev_remove_medium(const char *device
, Error
**errp
)
2348 BlockDriverState
*bs
;
2349 AioContext
*aio_context
;
2352 blk
= blk_by_name(device
);
2354 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2355 "Device '%s' not found", device
);
2359 /* For BBs without a device, we can exchange the BDS tree at will */
2360 has_device
= blk_get_attached_dev(blk
);
2362 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2363 error_setg(errp
, "Device '%s' is not removable", device
);
2367 if (has_device
&& !blk_dev_is_tray_open(blk
)) {
2368 error_setg(errp
, "Tray of device '%s' is not open", device
);
2377 aio_context
= bdrv_get_aio_context(bs
);
2378 aio_context_acquire(aio_context
);
2380 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2384 /* This follows the convention established by bdrv_make_anon() */
2385 if (bs
->device_list
.tqe_prev
) {
2386 QTAILQ_REMOVE(&bdrv_states
, bs
, device_list
);
2387 bs
->device_list
.tqe_prev
= NULL
;
2393 aio_context_release(aio_context
);
2396 static void qmp_blockdev_insert_anon_medium(const char *device
,
2397 BlockDriverState
*bs
, Error
**errp
)
2402 blk
= blk_by_name(device
);
2404 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2405 "Device '%s' not found", device
);
2409 /* For BBs without a device, we can exchange the BDS tree at will */
2410 has_device
= blk_get_attached_dev(blk
);
2412 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2413 error_setg(errp
, "Device '%s' is not removable", device
);
2417 if (has_device
&& !blk_dev_is_tray_open(blk
)) {
2418 error_setg(errp
, "Tray of device '%s' is not open", device
);
2423 error_setg(errp
, "There already is a medium in device '%s'", device
);
2427 blk_insert_bs(blk
, bs
);
2429 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, device_list
);
2432 void qmp_blockdev_insert_medium(const char *device
, const char *node_name
,
2435 BlockDriverState
*bs
;
2437 bs
= bdrv_find_node(node_name
);
2439 error_setg(errp
, "Node '%s' not found", node_name
);
2444 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2449 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2452 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2453 bool has_format
, const char *format
,
2455 BlockdevChangeReadOnlyMode read_only
,
2459 BlockDriverState
*medium_bs
= NULL
;
2460 int bdrv_flags
, ret
;
2461 QDict
*options
= NULL
;
2464 blk
= blk_by_name(device
);
2466 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2467 "Device '%s' not found", device
);
2472 blk_update_root_state(blk
);
2475 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2477 if (!has_read_only
) {
2478 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2481 switch (read_only
) {
2482 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2485 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2486 bdrv_flags
&= ~BDRV_O_RDWR
;
2489 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2490 bdrv_flags
|= BDRV_O_RDWR
;
2498 options
= qdict_new();
2499 qdict_put(options
, "driver", qstring_from_str(format
));
2503 ret
= bdrv_open(&medium_bs
, filename
, NULL
, options
, bdrv_flags
, errp
);
2508 blk_apply_root_state(blk
, medium_bs
);
2510 bdrv_add_key(medium_bs
, NULL
, &err
);
2512 error_propagate(errp
, err
);
2516 qmp_blockdev_open_tray(device
, false, false, &err
);
2518 error_propagate(errp
, err
);
2522 qmp_blockdev_remove_medium(device
, &err
);
2524 error_propagate(errp
, err
);
2528 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2530 error_propagate(errp
, err
);
2534 qmp_blockdev_close_tray(device
, errp
);
2537 /* If the medium has been inserted, the device has its own reference, so
2538 * ours must be relinquished; and if it has not been inserted successfully,
2539 * the reference must be relinquished anyway */
2540 bdrv_unref(medium_bs
);
2543 /* throttling disk I/O limits */
2544 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
2551 bool has_bps_rd_max
,
2553 bool has_bps_wr_max
,
2557 bool has_iops_rd_max
,
2558 int64_t iops_rd_max
,
2559 bool has_iops_wr_max
,
2560 int64_t iops_wr_max
,
2564 const char *group
, Error
**errp
)
2567 BlockDriverState
*bs
;
2569 AioContext
*aio_context
;
2571 blk
= blk_by_name(device
);
2573 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2574 "Device '%s' not found", device
);
2578 aio_context
= blk_get_aio_context(blk
);
2579 aio_context_acquire(aio_context
);
2583 error_setg(errp
, "Device '%s' has no medium", device
);
2587 memset(&cfg
, 0, sizeof(cfg
));
2588 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
2589 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
2590 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
2592 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
2593 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
2594 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
2597 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
2599 if (has_bps_rd_max
) {
2600 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2602 if (has_bps_wr_max
) {
2603 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2606 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2608 if (has_iops_rd_max
) {
2609 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2611 if (has_iops_wr_max
) {
2612 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2615 if (has_iops_size
) {
2616 cfg
.op_size
= iops_size
;
2619 if (!check_throttle_config(&cfg
, errp
)) {
2623 if (throttle_enabled(&cfg
)) {
2624 /* Enable I/O limits if they're not enabled yet, otherwise
2625 * just update the throttling group. */
2626 if (!bs
->throttle_state
) {
2627 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2628 } else if (has_group
) {
2629 bdrv_io_limits_update_group(bs
, group
);
2631 /* Set the new throttling configuration */
2632 bdrv_set_io_limits(bs
, &cfg
);
2633 } else if (bs
->throttle_state
) {
2634 /* If all throttling settings are set to 0, disable I/O limits */
2635 bdrv_io_limits_disable(bs
);
2639 aio_context_release(aio_context
);
2642 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2643 bool has_granularity
, uint32_t granularity
,
2646 AioContext
*aio_context
;
2647 BlockDriverState
*bs
;
2649 if (!name
|| name
[0] == '\0') {
2650 error_setg(errp
, "Bitmap name cannot be empty");
2654 bs
= bdrv_lookup_bs(node
, node
, errp
);
2659 aio_context
= bdrv_get_aio_context(bs
);
2660 aio_context_acquire(aio_context
);
2662 if (has_granularity
) {
2663 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2664 error_setg(errp
, "Granularity must be power of 2 "
2665 "and at least 512");
2669 /* Default to cluster size, if available: */
2670 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2673 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2676 aio_context_release(aio_context
);
2679 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2682 AioContext
*aio_context
;
2683 BlockDriverState
*bs
;
2684 BdrvDirtyBitmap
*bitmap
;
2686 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2687 if (!bitmap
|| !bs
) {
2691 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2693 "Bitmap '%s' is currently frozen and cannot be removed",
2697 bdrv_dirty_bitmap_make_anon(bitmap
);
2698 bdrv_release_dirty_bitmap(bs
, bitmap
);
2701 aio_context_release(aio_context
);
2705 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2706 * immediately after a full backup operation.
2708 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2711 AioContext
*aio_context
;
2712 BdrvDirtyBitmap
*bitmap
;
2713 BlockDriverState
*bs
;
2715 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2716 if (!bitmap
|| !bs
) {
2720 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2722 "Bitmap '%s' is currently frozen and cannot be modified",
2725 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2727 "Bitmap '%s' is currently disabled and cannot be cleared",
2732 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2735 aio_context_release(aio_context
);
2738 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2740 const char *id
= qdict_get_str(qdict
, "id");
2742 BlockDriverState
*bs
;
2743 AioContext
*aio_context
;
2744 Error
*local_err
= NULL
;
2746 blk
= blk_by_name(id
);
2748 error_report("Device '%s' not found", id
);
2752 if (!blk_legacy_dinfo(blk
)) {
2753 error_report("Deleting device added with blockdev-add"
2754 " is not supported");
2758 aio_context
= blk_get_aio_context(blk
);
2759 aio_context_acquire(aio_context
);
2763 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2764 error_report_err(local_err
);
2765 aio_context_release(aio_context
);
2772 /* if we have a device attached to this BlockDriverState
2773 * then we need to make the drive anonymous until the device
2774 * can be removed. If this is a drive with no device backing
2775 * then we can just get rid of the block driver state right here.
2777 if (blk_get_attached_dev(blk
)) {
2778 blk_hide_on_behalf_of_hmp_drive_del(blk
);
2779 /* Further I/O must not pause the guest */
2780 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2781 BLOCKDEV_ON_ERROR_REPORT
);
2786 aio_context_release(aio_context
);
2789 void qmp_block_resize(bool has_device
, const char *device
,
2790 bool has_node_name
, const char *node_name
,
2791 int64_t size
, Error
**errp
)
2793 Error
*local_err
= NULL
;
2794 BlockDriverState
*bs
;
2795 AioContext
*aio_context
;
2798 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2799 has_node_name
? node_name
: NULL
,
2802 error_propagate(errp
, local_err
);
2806 aio_context
= bdrv_get_aio_context(bs
);
2807 aio_context_acquire(aio_context
);
2809 if (!bdrv_is_first_non_filter(bs
)) {
2810 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2815 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2819 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2820 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2824 /* complete all in-flight operations before resizing the device */
2827 ret
= bdrv_truncate(bs
, size
);
2832 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2835 error_setg(errp
, QERR_UNSUPPORTED
);
2838 error_setg(errp
, "Device '%s' is read only", device
);
2841 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2844 error_setg_errno(errp
, -ret
, "Could not resize");
2849 aio_context_release(aio_context
);
2852 static void block_job_cb(void *opaque
, int ret
)
2854 /* Note that this function may be executed from another AioContext besides
2855 * the QEMU main loop. If you need to access anything that assumes the
2856 * QEMU global mutex, use a BH or introduce a mutex.
2859 BlockDriverState
*bs
= opaque
;
2860 const char *msg
= NULL
;
2862 trace_block_job_cb(bs
, bs
->job
, ret
);
2867 msg
= strerror(-ret
);
2870 if (block_job_is_cancelled(bs
->job
)) {
2871 block_job_event_cancelled(bs
->job
);
2873 block_job_event_completed(bs
->job
, msg
);
2877 void qmp_block_stream(const char *device
,
2878 bool has_base
, const char *base
,
2879 bool has_backing_file
, const char *backing_file
,
2880 bool has_speed
, int64_t speed
,
2881 bool has_on_error
, BlockdevOnError on_error
,
2885 BlockDriverState
*bs
;
2886 BlockDriverState
*base_bs
= NULL
;
2887 AioContext
*aio_context
;
2888 Error
*local_err
= NULL
;
2889 const char *base_name
= NULL
;
2891 if (!has_on_error
) {
2892 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2895 blk
= blk_by_name(device
);
2897 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2898 "Device '%s' not found", device
);
2902 aio_context
= blk_get_aio_context(blk
);
2903 aio_context_acquire(aio_context
);
2905 if (!blk_is_available(blk
)) {
2906 error_setg(errp
, "Device '%s' has no medium", device
);
2911 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2916 base_bs
= bdrv_find_backing_image(bs
, base
);
2917 if (base_bs
== NULL
) {
2918 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2921 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2925 /* if we are streaming the entire chain, the result will have no backing
2926 * file, and specifying one is therefore an error */
2927 if (base_bs
== NULL
&& has_backing_file
) {
2928 error_setg(errp
, "backing file specified, but streaming the "
2933 /* backing_file string overrides base bs filename */
2934 base_name
= has_backing_file
? backing_file
: base_name
;
2936 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
2937 on_error
, block_job_cb
, bs
, &local_err
);
2939 error_propagate(errp
, local_err
);
2943 trace_qmp_block_stream(bs
, bs
->job
);
2946 aio_context_release(aio_context
);
2949 void qmp_block_commit(const char *device
,
2950 bool has_base
, const char *base
,
2951 bool has_top
, const char *top
,
2952 bool has_backing_file
, const char *backing_file
,
2953 bool has_speed
, int64_t speed
,
2957 BlockDriverState
*bs
;
2958 BlockDriverState
*base_bs
, *top_bs
;
2959 AioContext
*aio_context
;
2960 Error
*local_err
= NULL
;
2961 /* This will be part of the QMP command, if/when the
2962 * BlockdevOnError change for blkmirror makes it in
2964 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2971 * libvirt relies on the DeviceNotFound error class in order to probe for
2972 * live commit feature versions; for this to work, we must make sure to
2973 * perform the device lookup before any generic errors that may occur in a
2974 * scenario in which all optional arguments are omitted. */
2975 blk
= blk_by_name(device
);
2977 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2978 "Device '%s' not found", device
);
2982 aio_context
= blk_get_aio_context(blk
);
2983 aio_context_acquire(aio_context
);
2985 if (!blk_is_available(blk
)) {
2986 error_setg(errp
, "Device '%s' has no medium", device
);
2991 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2995 /* default top_bs is the active layer */
2998 if (has_top
&& top
) {
2999 if (strcmp(bs
->filename
, top
) != 0) {
3000 top_bs
= bdrv_find_backing_image(bs
, top
);
3004 if (top_bs
== NULL
) {
3005 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3009 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3011 if (has_base
&& base
) {
3012 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3014 base_bs
= bdrv_find_base(top_bs
);
3017 if (base_bs
== NULL
) {
3018 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3022 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3024 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3028 /* Do not allow attempts to commit an image into itself */
3029 if (top_bs
== base_bs
) {
3030 error_setg(errp
, "cannot commit an image into itself");
3035 if (has_backing_file
) {
3036 error_setg(errp
, "'backing-file' specified,"
3037 " but 'top' is the active layer");
3040 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
3043 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
3044 has_backing_file
? backing_file
: NULL
, &local_err
);
3046 if (local_err
!= NULL
) {
3047 error_propagate(errp
, local_err
);
3052 aio_context_release(aio_context
);
3055 static void do_drive_backup(const char *device
, const char *target
,
3056 bool has_format
, const char *format
,
3057 enum MirrorSyncMode sync
,
3058 bool has_mode
, enum NewImageMode mode
,
3059 bool has_speed
, int64_t speed
,
3060 bool has_bitmap
, const char *bitmap
,
3061 bool has_on_source_error
,
3062 BlockdevOnError on_source_error
,
3063 bool has_on_target_error
,
3064 BlockdevOnError on_target_error
,
3065 BlockJobTxn
*txn
, Error
**errp
)
3068 BlockDriverState
*bs
;
3069 BlockDriverState
*target_bs
;
3070 BlockDriverState
*source
= NULL
;
3071 BdrvDirtyBitmap
*bmap
= NULL
;
3072 AioContext
*aio_context
;
3073 QDict
*options
= NULL
;
3074 Error
*local_err
= NULL
;
3082 if (!has_on_source_error
) {
3083 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3085 if (!has_on_target_error
) {
3086 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3089 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3092 blk
= blk_by_name(device
);
3094 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3095 "Device '%s' not found", device
);
3099 aio_context
= blk_get_aio_context(blk
);
3100 aio_context_acquire(aio_context
);
3102 /* Although backup_run has this check too, we need to use bs->drv below, so
3103 * do an early check redundantly. */
3104 if (!blk_is_available(blk
)) {
3105 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3111 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3114 /* Early check to avoid creating target */
3115 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3119 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3121 /* See if we have a backing HD we can use to create our new image
3123 if (sync
== MIRROR_SYNC_MODE_TOP
) {
3124 source
= backing_bs(bs
);
3126 sync
= MIRROR_SYNC_MODE_FULL
;
3129 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3133 size
= bdrv_getlength(bs
);
3135 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3139 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
3142 bdrv_img_create(target
, format
, source
->filename
,
3143 source
->drv
->format_name
, NULL
,
3144 size
, flags
, &local_err
, false);
3146 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
3147 size
, flags
, &local_err
, false);
3152 error_propagate(errp
, local_err
);
3157 options
= qdict_new();
3158 qdict_put(options
, "driver", qstring_from_str(format
));
3162 ret
= bdrv_open(&target_bs
, target
, NULL
, options
, flags
, &local_err
);
3164 error_propagate(errp
, local_err
);
3168 bdrv_set_aio_context(target_bs
, aio_context
);
3171 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
3173 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
3178 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
3179 on_source_error
, on_target_error
,
3180 block_job_cb
, bs
, txn
, &local_err
);
3181 if (local_err
!= NULL
) {
3182 bdrv_unref(target_bs
);
3183 error_propagate(errp
, local_err
);
3188 aio_context_release(aio_context
);
3191 void qmp_drive_backup(const char *device
, const char *target
,
3192 bool has_format
, const char *format
,
3193 enum MirrorSyncMode sync
,
3194 bool has_mode
, enum NewImageMode mode
,
3195 bool has_speed
, int64_t speed
,
3196 bool has_bitmap
, const char *bitmap
,
3197 bool has_on_source_error
, BlockdevOnError on_source_error
,
3198 bool has_on_target_error
, BlockdevOnError on_target_error
,
3201 return do_drive_backup(device
, target
, has_format
, format
, sync
,
3202 has_mode
, mode
, has_speed
, speed
,
3204 has_on_source_error
, on_source_error
,
3205 has_on_target_error
, on_target_error
,
3209 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3211 return bdrv_named_nodes_list(errp
);
3214 void do_blockdev_backup(const char *device
, const char *target
,
3215 enum MirrorSyncMode sync
,
3216 bool has_speed
, int64_t speed
,
3217 bool has_on_source_error
,
3218 BlockdevOnError on_source_error
,
3219 bool has_on_target_error
,
3220 BlockdevOnError on_target_error
,
3221 BlockJobTxn
*txn
, Error
**errp
)
3223 BlockBackend
*blk
, *target_blk
;
3224 BlockDriverState
*bs
;
3225 BlockDriverState
*target_bs
;
3226 Error
*local_err
= NULL
;
3227 AioContext
*aio_context
;
3232 if (!has_on_source_error
) {
3233 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3235 if (!has_on_target_error
) {
3236 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3239 blk
= blk_by_name(device
);
3241 error_setg(errp
, "Device '%s' not found", device
);
3245 aio_context
= blk_get_aio_context(blk
);
3246 aio_context_acquire(aio_context
);
3248 if (!blk_is_available(blk
)) {
3249 error_setg(errp
, "Device '%s' has no medium", device
);
3254 target_blk
= blk_by_name(target
);
3256 error_setg(errp
, "Device '%s' not found", target
);
3260 if (!blk_is_available(target_blk
)) {
3261 error_setg(errp
, "Device '%s' has no medium", target
);
3264 target_bs
= blk_bs(target_blk
);
3266 bdrv_ref(target_bs
);
3267 bdrv_set_aio_context(target_bs
, aio_context
);
3268 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
3269 on_target_error
, block_job_cb
, bs
, txn
, &local_err
);
3270 if (local_err
!= NULL
) {
3271 bdrv_unref(target_bs
);
3272 error_propagate(errp
, local_err
);
3275 aio_context_release(aio_context
);
3278 void qmp_blockdev_backup(const char *device
, const char *target
,
3279 enum MirrorSyncMode sync
,
3280 bool has_speed
, int64_t speed
,
3281 bool has_on_source_error
,
3282 BlockdevOnError on_source_error
,
3283 bool has_on_target_error
,
3284 BlockdevOnError on_target_error
,
3287 do_blockdev_backup(device
, target
, sync
, has_speed
, speed
,
3288 has_on_source_error
, on_source_error
,
3289 has_on_target_error
, on_target_error
,
3293 void qmp_drive_mirror(const char *device
, const char *target
,
3294 bool has_format
, const char *format
,
3295 bool has_node_name
, const char *node_name
,
3296 bool has_replaces
, const char *replaces
,
3297 enum MirrorSyncMode sync
,
3298 bool has_mode
, enum NewImageMode mode
,
3299 bool has_speed
, int64_t speed
,
3300 bool has_granularity
, uint32_t granularity
,
3301 bool has_buf_size
, int64_t buf_size
,
3302 bool has_on_source_error
, BlockdevOnError on_source_error
,
3303 bool has_on_target_error
, BlockdevOnError on_target_error
,
3304 bool has_unmap
, bool unmap
,
3308 BlockDriverState
*bs
;
3309 BlockDriverState
*source
, *target_bs
;
3310 AioContext
*aio_context
;
3311 Error
*local_err
= NULL
;
3320 if (!has_on_source_error
) {
3321 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3323 if (!has_on_target_error
) {
3324 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3327 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3329 if (!has_granularity
) {
3332 if (!has_buf_size
) {
3339 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3340 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3341 "a value in range [512B, 64MB]");
3344 if (granularity
& (granularity
- 1)) {
3345 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3350 blk
= blk_by_name(device
);
3352 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3353 "Device '%s' not found", device
);
3357 aio_context
= blk_get_aio_context(blk
);
3358 aio_context_acquire(aio_context
);
3360 if (!blk_is_available(blk
)) {
3361 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3367 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3370 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR
, errp
)) {
3374 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3375 source
= backing_bs(bs
);
3376 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3377 sync
= MIRROR_SYNC_MODE_FULL
;
3379 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3383 size
= bdrv_getlength(bs
);
3385 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3390 BlockDriverState
*to_replace_bs
;
3391 AioContext
*replace_aio_context
;
3392 int64_t replace_size
;
3394 if (!has_node_name
) {
3395 error_setg(errp
, "a node-name must be provided when replacing a"
3396 " named node of the graph");
3400 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
3402 if (!to_replace_bs
) {
3403 error_propagate(errp
, local_err
);
3407 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3408 aio_context_acquire(replace_aio_context
);
3409 replace_size
= bdrv_getlength(to_replace_bs
);
3410 aio_context_release(replace_aio_context
);
3412 if (size
!= replace_size
) {
3413 error_setg(errp
, "cannot replace image with a mirror image of "
3419 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3420 && mode
!= NEW_IMAGE_MODE_EXISTING
)
3422 /* create new image w/o backing file */
3424 bdrv_img_create(target
, format
,
3425 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3428 case NEW_IMAGE_MODE_EXISTING
:
3430 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3431 /* create new image with backing file */
3432 bdrv_img_create(target
, format
,
3434 source
->drv
->format_name
,
3435 NULL
, size
, flags
, &local_err
, false);
3443 error_propagate(errp
, local_err
);
3447 options
= qdict_new();
3448 if (has_node_name
) {
3449 qdict_put(options
, "node-name", qstring_from_str(node_name
));
3452 qdict_put(options
, "driver", qstring_from_str(format
));
3455 /* Mirroring takes care of copy-on-write using the source's backing
3459 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
3460 flags
| BDRV_O_NO_BACKING
, &local_err
);
3462 error_propagate(errp
, local_err
);
3466 bdrv_set_aio_context(target_bs
, aio_context
);
3468 /* pass the node name to replace to mirror start since it's loose coupling
3469 * and will allow to check whether the node still exist at mirror completion
3471 mirror_start(bs
, target_bs
,
3472 has_replaces
? replaces
: NULL
,
3473 speed
, granularity
, buf_size
, sync
,
3474 on_source_error
, on_target_error
,
3476 block_job_cb
, bs
, &local_err
);
3477 if (local_err
!= NULL
) {
3478 bdrv_unref(target_bs
);
3479 error_propagate(errp
, local_err
);
3484 aio_context_release(aio_context
);
3487 /* Get the block job for a given device name and acquire its AioContext */
3488 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
3492 BlockDriverState
*bs
;
3494 *aio_context
= NULL
;
3496 blk
= blk_by_name(device
);
3501 *aio_context
= blk_get_aio_context(blk
);
3502 aio_context_acquire(*aio_context
);
3504 if (!blk_is_available(blk
)) {
3516 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3517 "No active block job on device '%s'", device
);
3519 aio_context_release(*aio_context
);
3520 *aio_context
= NULL
;
3525 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3527 AioContext
*aio_context
;
3528 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3534 block_job_set_speed(job
, speed
, errp
);
3535 aio_context_release(aio_context
);
3538 void qmp_block_job_cancel(const char *device
,
3539 bool has_force
, bool force
, Error
**errp
)
3541 AioContext
*aio_context
;
3542 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3552 if (job
->user_paused
&& !force
) {
3553 error_setg(errp
, "The block job for device '%s' is currently paused",
3558 trace_qmp_block_job_cancel(job
);
3559 block_job_cancel(job
);
3561 aio_context_release(aio_context
);
3564 void qmp_block_job_pause(const char *device
, Error
**errp
)
3566 AioContext
*aio_context
;
3567 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3569 if (!job
|| job
->user_paused
) {
3573 job
->user_paused
= true;
3574 trace_qmp_block_job_pause(job
);
3575 block_job_pause(job
);
3576 aio_context_release(aio_context
);
3579 void qmp_block_job_resume(const char *device
, Error
**errp
)
3581 AioContext
*aio_context
;
3582 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3584 if (!job
|| !job
->user_paused
) {
3588 job
->user_paused
= false;
3589 trace_qmp_block_job_resume(job
);
3590 block_job_resume(job
);
3591 aio_context_release(aio_context
);
3594 void qmp_block_job_complete(const char *device
, Error
**errp
)
3596 AioContext
*aio_context
;
3597 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3603 trace_qmp_block_job_complete(job
);
3604 block_job_complete(job
, errp
);
3605 aio_context_release(aio_context
);
3608 void qmp_change_backing_file(const char *device
,
3609 const char *image_node_name
,
3610 const char *backing_file
,
3614 BlockDriverState
*bs
= NULL
;
3615 AioContext
*aio_context
;
3616 BlockDriverState
*image_bs
= NULL
;
3617 Error
*local_err
= NULL
;
3622 blk
= blk_by_name(device
);
3624 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3625 "Device '%s' not found", device
);
3629 aio_context
= blk_get_aio_context(blk
);
3630 aio_context_acquire(aio_context
);
3632 if (!blk_is_available(blk
)) {
3633 error_setg(errp
, "Device '%s' has no medium", device
);
3638 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3640 error_propagate(errp
, local_err
);
3645 error_setg(errp
, "image file not found");
3649 if (bdrv_find_base(image_bs
) == image_bs
) {
3650 error_setg(errp
, "not allowing backing file change on an image "
3651 "without a backing file");
3655 /* even though we are not necessarily operating on bs, we need it to
3656 * determine if block ops are currently prohibited on the chain */
3657 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3661 /* final sanity check */
3662 if (!bdrv_chain_contains(bs
, image_bs
)) {
3663 error_setg(errp
, "'%s' and image file are not in the same chain",
3668 /* if not r/w, reopen to make r/w */
3669 open_flags
= image_bs
->open_flags
;
3670 ro
= bdrv_is_read_only(image_bs
);
3673 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3675 error_propagate(errp
, local_err
);
3680 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3681 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3684 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3686 /* don't exit here, so we can try to restore open flags if
3691 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3693 error_propagate(errp
, local_err
); /* will preserve prior errp */
3698 aio_context_release(aio_context
);
3701 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3703 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3704 BlockDriverState
*bs
;
3705 BlockBackend
*blk
= NULL
;
3708 Error
*local_err
= NULL
;
3710 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3711 * cache.direct=false instead of silently switching to aio=threads, except
3712 * when called from drive_new().
3714 * For now, simply forbidding the combination for all drivers will do. */
3715 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3716 bool direct
= options
->has_cache
&&
3717 options
->cache
->has_direct
&&
3718 options
->cache
->direct
;
3720 error_setg(errp
, "aio=native requires cache.direct=true");
3725 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
3726 &options
, NULL
, &local_err
);
3728 error_propagate(errp
, local_err
);
3732 obj
= qmp_output_get_qobject(ov
);
3733 qdict
= qobject_to_qdict(obj
);
3735 qdict_flatten(qdict
);
3737 if (options
->has_id
) {
3738 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3740 error_propagate(errp
, local_err
);
3746 if (!qdict_get_try_str(qdict
, "node-name")) {
3747 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3752 bs
= bds_tree_init(qdict
, errp
);
3758 if (bs
&& bdrv_key_required(bs
)) {
3764 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3769 qmp_output_visitor_cleanup(ov
);
3772 void qmp_x_blockdev_del(bool has_id
, const char *id
,
3773 bool has_node_name
, const char *node_name
, Error
**errp
)
3775 AioContext
*aio_context
;
3777 BlockDriverState
*bs
;
3779 if (has_id
&& has_node_name
) {
3780 error_setg(errp
, "Only one of id and node-name must be specified");
3782 } else if (!has_id
&& !has_node_name
) {
3783 error_setg(errp
, "No block device specified");
3788 blk
= blk_by_name(id
);
3790 error_setg(errp
, "Cannot find block backend %s", id
);
3793 if (blk_get_refcnt(blk
) > 1) {
3794 error_setg(errp
, "Block backend %s is in use", id
);
3798 aio_context
= blk_get_aio_context(blk
);
3800 bs
= bdrv_find_node(node_name
);
3802 error_setg(errp
, "Cannot find node %s", node_name
);
3807 error_setg(errp
, "Node %s is in use by %s",
3808 node_name
, blk_name(blk
));
3811 aio_context
= bdrv_get_aio_context(bs
);
3814 aio_context_acquire(aio_context
);
3817 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3821 if (bs
->refcnt
> 1 || !QLIST_EMPTY(&bs
->parents
)) {
3822 error_setg(errp
, "Block device %s is in use",
3823 bdrv_get_device_or_node_name(bs
));
3835 aio_context_release(aio_context
);
3838 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3840 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3841 BlockDriverState
*bs
;
3843 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3844 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
3846 aio_context_acquire(aio_context
);
3849 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
3850 elem
->value
= block_job_query(bs
->job
);
3852 p_next
= &elem
->next
;
3855 aio_context_release(aio_context
);
3861 QemuOptsList qemu_common_drive_opts
= {
3863 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3867 .type
= QEMU_OPT_BOOL
,
3868 .help
= "enable/disable snapshot mode",
3871 .type
= QEMU_OPT_STRING
,
3872 .help
= "discard operation (ignore/off, unmap/on)",
3874 .name
= BDRV_OPT_CACHE_WB
,
3875 .type
= QEMU_OPT_BOOL
,
3876 .help
= "enables writeback mode for any caches",
3878 .name
= BDRV_OPT_CACHE_DIRECT
,
3879 .type
= QEMU_OPT_BOOL
,
3880 .help
= "enables use of O_DIRECT (bypass the host page cache)",
3882 .name
= BDRV_OPT_CACHE_NO_FLUSH
,
3883 .type
= QEMU_OPT_BOOL
,
3884 .help
= "ignore any flush requests for the device",
3887 .type
= QEMU_OPT_STRING
,
3888 .help
= "host AIO implementation (threads, native)",
3891 .type
= QEMU_OPT_STRING
,
3892 .help
= "disk format (raw, qcow2, ...)",
3895 .type
= QEMU_OPT_STRING
,
3896 .help
= "read error action",
3899 .type
= QEMU_OPT_STRING
,
3900 .help
= "write error action",
3902 .name
= "read-only",
3903 .type
= QEMU_OPT_BOOL
,
3904 .help
= "open drive file as read-only",
3906 .name
= "throttling.iops-total",
3907 .type
= QEMU_OPT_NUMBER
,
3908 .help
= "limit total I/O operations per second",
3910 .name
= "throttling.iops-read",
3911 .type
= QEMU_OPT_NUMBER
,
3912 .help
= "limit read operations per second",
3914 .name
= "throttling.iops-write",
3915 .type
= QEMU_OPT_NUMBER
,
3916 .help
= "limit write operations per second",
3918 .name
= "throttling.bps-total",
3919 .type
= QEMU_OPT_NUMBER
,
3920 .help
= "limit total bytes per second",
3922 .name
= "throttling.bps-read",
3923 .type
= QEMU_OPT_NUMBER
,
3924 .help
= "limit read bytes per second",
3926 .name
= "throttling.bps-write",
3927 .type
= QEMU_OPT_NUMBER
,
3928 .help
= "limit write bytes per second",
3930 .name
= "throttling.iops-total-max",
3931 .type
= QEMU_OPT_NUMBER
,
3932 .help
= "I/O operations burst",
3934 .name
= "throttling.iops-read-max",
3935 .type
= QEMU_OPT_NUMBER
,
3936 .help
= "I/O operations read burst",
3938 .name
= "throttling.iops-write-max",
3939 .type
= QEMU_OPT_NUMBER
,
3940 .help
= "I/O operations write burst",
3942 .name
= "throttling.bps-total-max",
3943 .type
= QEMU_OPT_NUMBER
,
3944 .help
= "total bytes burst",
3946 .name
= "throttling.bps-read-max",
3947 .type
= QEMU_OPT_NUMBER
,
3948 .help
= "total bytes read burst",
3950 .name
= "throttling.bps-write-max",
3951 .type
= QEMU_OPT_NUMBER
,
3952 .help
= "total bytes write burst",
3954 .name
= "throttling.iops-size",
3955 .type
= QEMU_OPT_NUMBER
,
3956 .help
= "when limiting by iops max size of an I/O in bytes",
3958 .name
= "throttling.group",
3959 .type
= QEMU_OPT_STRING
,
3960 .help
= "name of the block throttling group",
3962 .name
= "copy-on-read",
3963 .type
= QEMU_OPT_BOOL
,
3964 .help
= "copy read data from backing file into image file",
3966 .name
= "detect-zeroes",
3967 .type
= QEMU_OPT_STRING
,
3968 .help
= "try to optimize zero writes (off, on, unmap)",
3970 .name
= "stats-account-invalid",
3971 .type
= QEMU_OPT_BOOL
,
3972 .help
= "whether to account for invalid I/O operations "
3973 "in the statistics",
3975 .name
= "stats-account-failed",
3976 .type
= QEMU_OPT_BOOL
,
3977 .help
= "whether to account for failed I/O operations "
3978 "in the statistics",
3980 { /* end of list */ }
3984 static QemuOptsList qemu_root_bds_opts
= {
3986 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3990 .type
= QEMU_OPT_STRING
,
3991 .help
= "discard operation (ignore/off, unmap/on)",
3993 .name
= "cache.writeback",
3994 .type
= QEMU_OPT_BOOL
,
3995 .help
= "enables writeback mode for any caches",
3997 .name
= "cache.direct",
3998 .type
= QEMU_OPT_BOOL
,
3999 .help
= "enables use of O_DIRECT (bypass the host page cache)",
4001 .name
= "cache.no-flush",
4002 .type
= QEMU_OPT_BOOL
,
4003 .help
= "ignore any flush requests for the device",
4006 .type
= QEMU_OPT_STRING
,
4007 .help
= "host AIO implementation (threads, native)",
4009 .name
= "read-only",
4010 .type
= QEMU_OPT_BOOL
,
4011 .help
= "open drive file as read-only",
4013 .name
= "copy-on-read",
4014 .type
= QEMU_OPT_BOOL
,
4015 .help
= "copy read data from backing file into image file",
4017 .name
= "detect-zeroes",
4018 .type
= QEMU_OPT_STRING
,
4019 .help
= "try to optimize zero writes (off, on, unmap)",
4021 { /* end of list */ }
4025 QemuOptsList qemu_drive_opts
= {
4027 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4030 * no elements => accept any params
4031 * validation will happen later
4033 { /* end of list */ }