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 ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
391 if (!strcmp(aio
, "native")) {
392 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
393 } else if (!strcmp(aio
, "threads")) {
394 /* this is the default */
396 error_setg(errp
, "invalid aio option");
402 /* disk I/O throttling */
403 if (throttling_group
) {
404 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
408 memset(throttle_cfg
, 0, sizeof(*throttle_cfg
));
409 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
410 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
411 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
412 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
413 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
414 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
415 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
416 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
417 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
418 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
419 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
420 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
422 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
423 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
424 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
425 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
426 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
427 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
428 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
429 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
430 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
431 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
432 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
433 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
435 throttle_cfg
->op_size
=
436 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
438 if (!check_throttle_config(throttle_cfg
, errp
)) {
445 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
446 qemu_opt_get(opts
, "detect-zeroes"),
447 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX
,
448 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
451 error_propagate(errp
, local_error
);
456 *detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
457 !(*bdrv_flags
& BDRV_O_UNMAP
))
459 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
460 "without setting discard operation to unmap");
466 /* Takes the ownership of bs_opts */
467 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
472 int on_read_error
, on_write_error
;
473 bool account_invalid
, account_failed
;
475 BlockDriverState
*bs
;
480 QDict
*interval_dict
= NULL
;
481 QList
*interval_list
= NULL
;
483 BlockdevDetectZeroesOptions detect_zeroes
=
484 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
485 const char *throttling_group
= NULL
;
487 /* Check common options by copying from bs_opts to opts, all other options
488 * stay in bs_opts for processing by bdrv_open(). */
489 id
= qdict_get_try_str(bs_opts
, "id");
490 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
492 error_propagate(errp
, error
);
496 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
498 error_propagate(errp
, error
);
503 qdict_del(bs_opts
, "id");
506 /* extract parameters */
507 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
509 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
510 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
512 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
513 qdict_array_split(interval_dict
, &interval_list
);
515 if (qdict_size(interval_dict
) != 0) {
516 error_setg(errp
, "Invalid option stats-intervals.%s",
517 qdict_first(interval_dict
)->key
);
521 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
522 &detect_zeroes
, &error
);
524 error_propagate(errp
, error
);
528 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
529 if (is_help_option(buf
)) {
530 error_printf("Supported formats:");
531 bdrv_iterate_format(bdrv_format_print
, NULL
);
536 if (qdict_haskey(bs_opts
, "driver")) {
537 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
540 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
543 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
544 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
545 on_write_error
= parse_block_error_action(buf
, 0, &error
);
547 error_propagate(errp
, error
);
552 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
553 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
554 on_read_error
= parse_block_error_action(buf
, 1, &error
);
556 error_propagate(errp
, error
);
562 bdrv_flags
|= BDRV_O_SNAPSHOT
;
566 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
567 BlockBackendRootState
*blk_rs
;
569 blk
= blk_new(qemu_opts_id(opts
), errp
);
574 blk_rs
= blk_get_root_state(blk
);
575 blk_rs
->open_flags
= bdrv_flags
;
576 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
577 blk_rs
->detect_zeroes
= detect_zeroes
;
579 if (throttle_enabled(&cfg
)) {
580 if (!throttling_group
) {
581 throttling_group
= blk_name(blk
);
583 blk_rs
->throttle_group
= g_strdup(throttling_group
);
584 blk_rs
->throttle_state
= throttle_group_incref(throttling_group
);
585 blk_rs
->throttle_state
->cfg
= cfg
;
590 if (file
&& !*file
) {
594 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
595 * with other callers) rather than what we want as the real defaults.
596 * Apply the defaults here instead. */
597 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_WB
, "on");
598 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
599 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
602 /* always use cache=unsafe with snapshot */
603 qdict_put(bs_opts
, BDRV_OPT_CACHE_WB
, qstring_from_str("on"));
604 qdict_put(bs_opts
, BDRV_OPT_CACHE_DIRECT
, qstring_from_str("off"));
605 qdict_put(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, qstring_from_str("on"));
608 blk
= blk_new_open(qemu_opts_id(opts
), file
, NULL
, bs_opts
, bdrv_flags
,
615 bs
->detect_zeroes
= detect_zeroes
;
617 /* disk I/O throttling */
618 if (throttle_enabled(&cfg
)) {
619 if (!throttling_group
) {
620 throttling_group
= blk_name(blk
);
622 bdrv_io_limits_enable(bs
, throttling_group
);
623 bdrv_set_io_limits(bs
, &cfg
);
626 if (bdrv_key_required(bs
)) {
630 block_acct_init(blk_get_stats(blk
), account_invalid
, account_failed
);
632 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
639 blk_set_on_error(blk
, on_read_error
, on_write_error
);
643 QDECREF(interval_dict
);
644 QDECREF(interval_list
);
649 QDECREF(interval_dict
);
650 QDECREF(interval_list
);
656 static QemuOptsList qemu_root_bds_opts
;
658 /* Takes the ownership of bs_opts */
659 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
661 BlockDriverState
*bs
;
663 Error
*local_error
= NULL
;
664 BlockdevDetectZeroesOptions detect_zeroes
;
668 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
673 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
675 error_propagate(errp
, local_error
);
679 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
680 &detect_zeroes
, &local_error
);
682 error_propagate(errp
, local_error
);
687 ret
= bdrv_open(&bs
, NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
689 goto fail_no_bs_opts
;
692 bs
->detect_zeroes
= detect_zeroes
;
704 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
709 value
= qemu_opt_get(opts
, from
);
711 if (qemu_opt_find(opts
, to
)) {
712 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
713 "same time", to
, from
);
718 /* rename all items in opts */
719 while ((value
= qemu_opt_get(opts
, from
))) {
720 qemu_opt_set(opts
, to
, value
, &error_abort
);
721 qemu_opt_unset(opts
, from
);
725 QemuOptsList qemu_legacy_drive_opts
= {
727 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
731 .type
= QEMU_OPT_NUMBER
,
732 .help
= "bus number",
735 .type
= QEMU_OPT_NUMBER
,
736 .help
= "unit number (i.e. lun for scsi)",
739 .type
= QEMU_OPT_NUMBER
,
740 .help
= "index number",
743 .type
= QEMU_OPT_STRING
,
744 .help
= "media type (disk, cdrom)",
747 .type
= QEMU_OPT_STRING
,
748 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
751 .type
= QEMU_OPT_NUMBER
,
752 .help
= "number of cylinders (ide disk geometry)",
755 .type
= QEMU_OPT_NUMBER
,
756 .help
= "number of heads (ide disk geometry)",
759 .type
= QEMU_OPT_NUMBER
,
760 .help
= "number of sectors (ide disk geometry)",
763 .type
= QEMU_OPT_STRING
,
764 .help
= "chs translation (auto, lba, none)",
767 .type
= QEMU_OPT_BOOL
,
768 .help
= "(deprecated, ignored)",
771 .type
= QEMU_OPT_STRING
,
772 .help
= "pci address (virtio only)",
775 .type
= QEMU_OPT_STRING
,
776 .help
= "disk serial number",
779 .type
= QEMU_OPT_STRING
,
783 /* Options that are passed on, but have special semantics with -drive */
786 .type
= QEMU_OPT_BOOL
,
787 .help
= "open drive file as read-only",
790 .type
= QEMU_OPT_STRING
,
791 .help
= "read error action",
794 .type
= QEMU_OPT_STRING
,
795 .help
= "write error action",
797 .name
= "copy-on-read",
798 .type
= QEMU_OPT_BOOL
,
799 .help
= "copy read data from backing file into image file",
802 { /* end of list */ }
806 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
810 DriveInfo
*dinfo
= NULL
;
812 QemuOpts
*legacy_opts
;
813 DriveMediaType media
= MEDIA_DISK
;
814 BlockInterfaceType type
;
815 int cyls
, heads
, secs
, translation
;
816 int max_devs
, bus_id
, unit_id
, index
;
818 const char *werror
, *rerror
;
819 bool read_only
= false;
822 const char *filename
;
823 Error
*local_err
= NULL
;
826 /* Change legacy command line options into QMP ones */
827 static const struct {
831 { "iops", "throttling.iops-total" },
832 { "iops_rd", "throttling.iops-read" },
833 { "iops_wr", "throttling.iops-write" },
835 { "bps", "throttling.bps-total" },
836 { "bps_rd", "throttling.bps-read" },
837 { "bps_wr", "throttling.bps-write" },
839 { "iops_max", "throttling.iops-total-max" },
840 { "iops_rd_max", "throttling.iops-read-max" },
841 { "iops_wr_max", "throttling.iops-write-max" },
843 { "bps_max", "throttling.bps-total-max" },
844 { "bps_rd_max", "throttling.bps-read-max" },
845 { "bps_wr_max", "throttling.bps-write-max" },
847 { "iops_size", "throttling.iops-size" },
849 { "group", "throttling.group" },
851 { "readonly", "read-only" },
854 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
855 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
858 error_report_err(local_err
);
863 value
= qemu_opt_get(all_opts
, "cache");
867 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
868 error_report("invalid cache option");
872 /* Specific options take precedence */
873 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
874 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
875 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
877 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
878 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
879 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
881 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
882 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
883 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
885 qemu_opt_unset(all_opts
, "cache");
888 /* Get a QDict for processing the options */
889 bs_opts
= qdict_new();
890 qemu_opts_to_qdict(all_opts
, bs_opts
);
892 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
894 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
896 error_report_err(local_err
);
900 /* Deprecated option boot=[on|off] */
901 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
902 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
903 "ignored. Future versions will reject this parameter. Please "
904 "update your scripts.\n");
908 value
= qemu_opt_get(legacy_opts
, "media");
910 if (!strcmp(value
, "disk")) {
912 } else if (!strcmp(value
, "cdrom")) {
916 error_report("'%s' invalid media", value
);
921 /* copy-on-read is disabled with a warning for read-only devices */
922 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
923 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
925 if (read_only
&& copy_on_read
) {
926 error_report("warning: disabling copy-on-read on read-only drive");
927 copy_on_read
= false;
930 qdict_put(bs_opts
, "read-only",
931 qstring_from_str(read_only
? "on" : "off"));
932 qdict_put(bs_opts
, "copy-on-read",
933 qstring_from_str(copy_on_read
? "on" :"off"));
935 /* Controller type */
936 value
= qemu_opt_get(legacy_opts
, "if");
939 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
942 if (type
== IF_COUNT
) {
943 error_report("unsupported bus type '%s'", value
);
947 type
= block_default_type
;
951 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
952 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
953 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
955 if (cyls
|| heads
|| secs
) {
957 error_report("invalid physical cyls number");
961 error_report("invalid physical heads number");
965 error_report("invalid physical secs number");
970 translation
= BIOS_ATA_TRANSLATION_AUTO
;
971 value
= qemu_opt_get(legacy_opts
, "trans");
974 error_report("'%s' trans must be used with cyls, heads and secs",
978 if (!strcmp(value
, "none")) {
979 translation
= BIOS_ATA_TRANSLATION_NONE
;
980 } else if (!strcmp(value
, "lba")) {
981 translation
= BIOS_ATA_TRANSLATION_LBA
;
982 } else if (!strcmp(value
, "large")) {
983 translation
= BIOS_ATA_TRANSLATION_LARGE
;
984 } else if (!strcmp(value
, "rechs")) {
985 translation
= BIOS_ATA_TRANSLATION_RECHS
;
986 } else if (!strcmp(value
, "auto")) {
987 translation
= BIOS_ATA_TRANSLATION_AUTO
;
989 error_report("'%s' invalid translation type", value
);
994 if (media
== MEDIA_CDROM
) {
995 if (cyls
|| secs
|| heads
) {
996 error_report("CHS can't be set with media=cdrom");
1001 /* Device address specified by bus/unit or index.
1002 * If none was specified, try to find the first free one. */
1003 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
1004 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
1005 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
1007 max_devs
= if_max_devs
[type
];
1010 if (bus_id
!= 0 || unit_id
!= -1) {
1011 error_report("index cannot be used with bus and unit");
1014 bus_id
= drive_index_to_bus_id(type
, index
);
1015 unit_id
= drive_index_to_unit_id(type
, index
);
1018 if (unit_id
== -1) {
1020 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1022 if (max_devs
&& unit_id
>= max_devs
) {
1023 unit_id
-= max_devs
;
1029 if (max_devs
&& unit_id
>= max_devs
) {
1030 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1034 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1035 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1036 bus_id
, unit_id
, index
);
1041 serial
= qemu_opt_get(legacy_opts
, "serial");
1043 /* no id supplied -> create one */
1044 if (qemu_opts_id(all_opts
) == NULL
) {
1046 const char *mediastr
= "";
1047 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1048 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1051 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1054 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1057 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1061 /* Add virtio block device */
1062 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1063 if (devaddr
&& type
!= IF_VIRTIO
) {
1064 error_report("addr is not supported by this bus type");
1068 if (type
== IF_VIRTIO
) {
1070 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1072 if (arch_type
== QEMU_ARCH_S390X
) {
1073 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1075 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1077 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1080 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1084 filename
= qemu_opt_get(legacy_opts
, "file");
1086 /* Check werror/rerror compatibility with if=... */
1087 werror
= qemu_opt_get(legacy_opts
, "werror");
1088 if (werror
!= NULL
) {
1089 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1091 error_report("werror is not supported by this bus type");
1094 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1097 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1098 if (rerror
!= NULL
) {
1099 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1101 error_report("rerror is not supported by this bus type");
1104 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1107 /* Actual block device init: Functionality shared with blockdev-add */
1108 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1112 error_report_err(local_err
);
1119 /* Create legacy DriveInfo */
1120 dinfo
= g_malloc0(sizeof(*dinfo
));
1121 dinfo
->opts
= all_opts
;
1124 dinfo
->heads
= heads
;
1126 dinfo
->trans
= translation
;
1129 dinfo
->bus
= bus_id
;
1130 dinfo
->unit
= unit_id
;
1131 dinfo
->devaddr
= devaddr
;
1132 dinfo
->serial
= g_strdup(serial
);
1134 blk_set_legacy_dinfo(blk
, dinfo
);
1141 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1148 qemu_opts_del(legacy_opts
);
1153 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1155 const char *device
= qdict_get_str(qdict
, "device");
1159 if (!strcmp(device
, "all")) {
1160 ret
= bdrv_commit_all();
1162 BlockDriverState
*bs
;
1163 AioContext
*aio_context
;
1165 blk
= blk_by_name(device
);
1167 monitor_printf(mon
, "Device '%s' not found\n", device
);
1170 if (!blk_is_available(blk
)) {
1171 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1176 aio_context
= bdrv_get_aio_context(bs
);
1177 aio_context_acquire(aio_context
);
1179 ret
= bdrv_commit(bs
);
1181 aio_context_release(aio_context
);
1184 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1189 static void blockdev_do_action(TransactionActionKind type
, void *data
,
1192 TransactionAction action
;
1193 TransactionActionList list
;
1196 action
.u
.data
= data
;
1197 list
.value
= &action
;
1199 qmp_transaction(&list
, false, NULL
, errp
);
1202 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1203 bool has_node_name
, const char *node_name
,
1204 const char *snapshot_file
,
1205 bool has_snapshot_node_name
,
1206 const char *snapshot_node_name
,
1207 bool has_format
, const char *format
,
1208 bool has_mode
, NewImageMode mode
, Error
**errp
)
1210 BlockdevSnapshotSync snapshot
= {
1211 .has_device
= has_device
,
1212 .device
= (char *) device
,
1213 .has_node_name
= has_node_name
,
1214 .node_name
= (char *) node_name
,
1215 .snapshot_file
= (char *) snapshot_file
,
1216 .has_snapshot_node_name
= has_snapshot_node_name
,
1217 .snapshot_node_name
= (char *) snapshot_node_name
,
1218 .has_format
= has_format
,
1219 .format
= (char *) format
,
1220 .has_mode
= has_mode
,
1223 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1227 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1230 BlockdevSnapshot snapshot_data
= {
1231 .node
= (char *) node
,
1232 .overlay
= (char *) overlay
1235 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1236 &snapshot_data
, errp
);
1239 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1243 BlockdevSnapshotInternal snapshot
= {
1244 .device
= (char *) device
,
1245 .name
= (char *) name
1248 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1252 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1259 BlockDriverState
*bs
;
1261 AioContext
*aio_context
;
1262 QEMUSnapshotInfo sn
;
1263 Error
*local_err
= NULL
;
1264 SnapshotInfo
*info
= NULL
;
1267 blk
= blk_by_name(device
);
1269 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1270 "Device '%s' not found", device
);
1274 aio_context
= blk_get_aio_context(blk
);
1275 aio_context_acquire(aio_context
);
1286 error_setg(errp
, "Name or id must be provided");
1287 goto out_aio_context
;
1290 if (!blk_is_available(blk
)) {
1291 error_setg(errp
, "Device '%s' has no medium", device
);
1292 goto out_aio_context
;
1296 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1297 goto out_aio_context
;
1300 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1302 error_propagate(errp
, local_err
);
1303 goto out_aio_context
;
1307 "Snapshot with id '%s' and name '%s' does not exist on "
1309 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1310 goto out_aio_context
;
1313 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1315 error_propagate(errp
, local_err
);
1316 goto out_aio_context
;
1319 aio_context_release(aio_context
);
1321 info
= g_new0(SnapshotInfo
, 1);
1322 info
->id
= g_strdup(sn
.id_str
);
1323 info
->name
= g_strdup(sn
.name
);
1324 info
->date_nsec
= sn
.date_nsec
;
1325 info
->date_sec
= sn
.date_sec
;
1326 info
->vm_state_size
= sn
.vm_state_size
;
1327 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1328 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1333 aio_context_release(aio_context
);
1338 * block_dirty_bitmap_lookup:
1339 * Return a dirty bitmap (if present), after validating
1340 * the node reference and bitmap names.
1342 * @node: The name of the BDS node to search for bitmaps
1343 * @name: The name of the bitmap to search for
1344 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1345 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1346 * @errp: Output pointer for error information. Can be NULL.
1348 * @return: A bitmap object on success, or NULL on failure.
1350 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1352 BlockDriverState
**pbs
,
1356 BlockDriverState
*bs
;
1357 BdrvDirtyBitmap
*bitmap
;
1358 AioContext
*aio_context
;
1361 error_setg(errp
, "Node cannot be NULL");
1365 error_setg(errp
, "Bitmap name cannot be NULL");
1368 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1370 error_setg(errp
, "Node '%s' not found", node
);
1374 aio_context
= bdrv_get_aio_context(bs
);
1375 aio_context_acquire(aio_context
);
1377 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1379 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1387 *paio
= aio_context
;
1389 aio_context_release(aio_context
);
1395 aio_context_release(aio_context
);
1399 /* New and old BlockDriverState structs for atomic group operations */
1401 typedef struct BlkActionState BlkActionState
;
1405 * Table of operations that define an Action.
1407 * @instance_size: Size of state struct, in bytes.
1408 * @prepare: Prepare the work, must NOT be NULL.
1409 * @commit: Commit the changes, can be NULL.
1410 * @abort: Abort the changes on fail, can be NULL.
1411 * @clean: Clean up resources after all transaction actions have called
1412 * commit() or abort(). Can be NULL.
1414 * Only prepare() may fail. In a single transaction, only one of commit() or
1415 * abort() will be called. clean() will always be called if it is present.
1417 typedef struct BlkActionOps
{
1418 size_t instance_size
;
1419 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1420 void (*commit
)(BlkActionState
*common
);
1421 void (*abort
)(BlkActionState
*common
);
1422 void (*clean
)(BlkActionState
*common
);
1427 * Describes one Action's state within a Transaction.
1429 * @action: QAPI-defined enum identifying which Action to perform.
1430 * @ops: Table of ActionOps this Action can perform.
1431 * @block_job_txn: Transaction which this action belongs to.
1432 * @entry: List membership for all Actions in this Transaction.
1434 * This structure must be arranged as first member in a subclassed type,
1435 * assuming that the compiler will also arrange it to the same offsets as the
1438 struct BlkActionState
{
1439 TransactionAction
*action
;
1440 const BlkActionOps
*ops
;
1441 BlockJobTxn
*block_job_txn
;
1442 TransactionProperties
*txn_props
;
1443 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1446 /* internal snapshot private data */
1447 typedef struct InternalSnapshotState
{
1448 BlkActionState common
;
1449 BlockDriverState
*bs
;
1450 AioContext
*aio_context
;
1451 QEMUSnapshotInfo sn
;
1453 } InternalSnapshotState
;
1456 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1458 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1460 "Action '%s' does not support Transaction property "
1461 "completion-mode = %s",
1462 TransactionActionKind_lookup
[s
->action
->type
],
1463 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1469 static void internal_snapshot_prepare(BlkActionState
*common
,
1472 Error
*local_err
= NULL
;
1476 BlockDriverState
*bs
;
1477 QEMUSnapshotInfo old_sn
, *sn
;
1480 BlockdevSnapshotInternal
*internal
;
1481 InternalSnapshotState
*state
;
1484 g_assert(common
->action
->type
==
1485 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1486 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
;
1487 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1489 /* 1. parse input */
1490 device
= internal
->device
;
1491 name
= internal
->name
;
1493 /* 2. check for validation */
1494 if (action_check_completion_mode(common
, errp
) < 0) {
1498 blk
= blk_by_name(device
);
1500 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1501 "Device '%s' not found", device
);
1505 /* AioContext is released in .clean() */
1506 state
->aio_context
= blk_get_aio_context(blk
);
1507 aio_context_acquire(state
->aio_context
);
1509 if (!blk_is_available(blk
)) {
1510 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1516 bdrv_drained_begin(bs
);
1518 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1522 if (bdrv_is_read_only(bs
)) {
1523 error_setg(errp
, "Device '%s' is read only", device
);
1527 if (!bdrv_can_snapshot(bs
)) {
1528 error_setg(errp
, "Block format '%s' used by device '%s' "
1529 "does not support internal snapshots",
1530 bs
->drv
->format_name
, device
);
1534 if (!strlen(name
)) {
1535 error_setg(errp
, "Name is empty");
1539 /* check whether a snapshot with name exist */
1540 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1543 error_propagate(errp
, local_err
);
1547 "Snapshot with name '%s' already exists on device '%s'",
1552 /* 3. take the snapshot */
1554 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1555 qemu_gettimeofday(&tv
);
1556 sn
->date_sec
= tv
.tv_sec
;
1557 sn
->date_nsec
= tv
.tv_usec
* 1000;
1558 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1560 ret1
= bdrv_snapshot_create(bs
, sn
);
1562 error_setg_errno(errp
, -ret1
,
1563 "Failed to create snapshot '%s' on device '%s'",
1568 /* 4. succeed, mark a snapshot is created */
1569 state
->created
= true;
1572 static void internal_snapshot_abort(BlkActionState
*common
)
1574 InternalSnapshotState
*state
=
1575 DO_UPCAST(InternalSnapshotState
, common
, common
);
1576 BlockDriverState
*bs
= state
->bs
;
1577 QEMUSnapshotInfo
*sn
= &state
->sn
;
1578 Error
*local_error
= NULL
;
1580 if (!state
->created
) {
1584 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1585 error_reportf_err(local_error
,
1586 "Failed to delete snapshot with id '%s' and "
1587 "name '%s' on device '%s' in abort: ",
1588 sn
->id_str
, sn
->name
,
1589 bdrv_get_device_name(bs
));
1593 static void internal_snapshot_clean(BlkActionState
*common
)
1595 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1598 if (state
->aio_context
) {
1600 bdrv_drained_end(state
->bs
);
1602 aio_context_release(state
->aio_context
);
1606 /* external snapshot private data */
1607 typedef struct ExternalSnapshotState
{
1608 BlkActionState common
;
1609 BlockDriverState
*old_bs
;
1610 BlockDriverState
*new_bs
;
1611 AioContext
*aio_context
;
1612 } ExternalSnapshotState
;
1614 static void external_snapshot_prepare(BlkActionState
*common
,
1618 QDict
*options
= NULL
;
1619 Error
*local_err
= NULL
;
1620 /* Device and node name of the image to generate the snapshot from */
1622 const char *node_name
;
1623 /* Reference to the new image (for 'blockdev-snapshot') */
1624 const char *snapshot_ref
;
1625 /* File name of the new image (for 'blockdev-snapshot-sync') */
1626 const char *new_image_file
;
1627 ExternalSnapshotState
*state
=
1628 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1629 TransactionAction
*action
= common
->action
;
1631 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1632 * purpose but a different set of parameters */
1633 switch (action
->type
) {
1634 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1636 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
;
1638 node_name
= s
->node
;
1639 new_image_file
= NULL
;
1640 snapshot_ref
= s
->overlay
;
1643 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1645 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1646 device
= s
->has_device
? s
->device
: NULL
;
1647 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1648 new_image_file
= s
->snapshot_file
;
1649 snapshot_ref
= NULL
;
1653 g_assert_not_reached();
1656 /* start processing */
1657 if (action_check_completion_mode(common
, errp
) < 0) {
1661 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1662 if (!state
->old_bs
) {
1666 /* Acquire AioContext now so any threads operating on old_bs stop */
1667 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1668 aio_context_acquire(state
->aio_context
);
1669 bdrv_drained_begin(state
->old_bs
);
1671 if (!bdrv_is_inserted(state
->old_bs
)) {
1672 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1676 if (bdrv_op_is_blocked(state
->old_bs
,
1677 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1681 if (!bdrv_is_read_only(state
->old_bs
)) {
1682 if (bdrv_flush(state
->old_bs
)) {
1683 error_setg(errp
, QERR_IO_ERROR
);
1688 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1689 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1693 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1694 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1695 const char *format
= s
->has_format
? s
->format
: "qcow2";
1696 enum NewImageMode mode
;
1697 const char *snapshot_node_name
=
1698 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1700 if (node_name
&& !snapshot_node_name
) {
1701 error_setg(errp
, "New snapshot node name missing");
1705 if (snapshot_node_name
&&
1706 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1707 error_setg(errp
, "New snapshot node name already in use");
1711 flags
= state
->old_bs
->open_flags
;
1713 /* create new image w/backing file */
1714 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1715 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1716 bdrv_img_create(new_image_file
, format
,
1717 state
->old_bs
->filename
,
1718 state
->old_bs
->drv
->format_name
,
1719 NULL
, -1, flags
, &local_err
, false);
1721 error_propagate(errp
, local_err
);
1726 options
= qdict_new();
1727 if (s
->has_snapshot_node_name
) {
1728 qdict_put(options
, "node-name",
1729 qstring_from_str(snapshot_node_name
));
1731 qdict_put(options
, "driver", qstring_from_str(format
));
1733 flags
|= BDRV_O_NO_BACKING
;
1736 assert(state
->new_bs
== NULL
);
1737 ret
= bdrv_open(&state
->new_bs
, new_image_file
, snapshot_ref
, options
,
1739 /* We will manually add the backing_hd field to the bs later */
1744 if (state
->new_bs
->blk
!= NULL
) {
1745 error_setg(errp
, "The snapshot is already in use by %s",
1746 blk_name(state
->new_bs
->blk
));
1750 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1755 if (state
->new_bs
->backing
!= NULL
) {
1756 error_setg(errp
, "The snapshot already has a backing image");
1760 if (!state
->new_bs
->drv
->supports_backing
) {
1761 error_setg(errp
, "The snapshot does not support backing images");
1765 static void external_snapshot_commit(BlkActionState
*common
)
1767 ExternalSnapshotState
*state
=
1768 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1770 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1772 /* This removes our old bs and adds the new bs */
1773 bdrv_append(state
->new_bs
, state
->old_bs
);
1774 /* We don't need (or want) to use the transactional
1775 * bdrv_reopen_multiple() across all the entries at once, because we
1776 * don't want to abort all of them if one of them fails the reopen */
1777 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1781 static void external_snapshot_abort(BlkActionState
*common
)
1783 ExternalSnapshotState
*state
=
1784 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1785 if (state
->new_bs
) {
1786 bdrv_unref(state
->new_bs
);
1790 static void external_snapshot_clean(BlkActionState
*common
)
1792 ExternalSnapshotState
*state
=
1793 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1794 if (state
->aio_context
) {
1795 bdrv_drained_end(state
->old_bs
);
1796 aio_context_release(state
->aio_context
);
1800 typedef struct DriveBackupState
{
1801 BlkActionState common
;
1802 BlockDriverState
*bs
;
1803 AioContext
*aio_context
;
1807 static void do_drive_backup(const char *device
, const char *target
,
1808 bool has_format
, const char *format
,
1809 enum MirrorSyncMode sync
,
1810 bool has_mode
, enum NewImageMode mode
,
1811 bool has_speed
, int64_t speed
,
1812 bool has_bitmap
, const char *bitmap
,
1813 bool has_on_source_error
,
1814 BlockdevOnError on_source_error
,
1815 bool has_on_target_error
,
1816 BlockdevOnError on_target_error
,
1817 BlockJobTxn
*txn
, Error
**errp
);
1819 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1821 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1823 DriveBackup
*backup
;
1824 Error
*local_err
= NULL
;
1826 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1827 backup
= common
->action
->u
.drive_backup
;
1829 blk
= blk_by_name(backup
->device
);
1831 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1832 "Device '%s' not found", backup
->device
);
1836 if (!blk_is_available(blk
)) {
1837 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1841 /* AioContext is released in .clean() */
1842 state
->aio_context
= blk_get_aio_context(blk
);
1843 aio_context_acquire(state
->aio_context
);
1844 bdrv_drained_begin(blk_bs(blk
));
1845 state
->bs
= blk_bs(blk
);
1847 do_drive_backup(backup
->device
, backup
->target
,
1848 backup
->has_format
, backup
->format
,
1850 backup
->has_mode
, backup
->mode
,
1851 backup
->has_speed
, backup
->speed
,
1852 backup
->has_bitmap
, backup
->bitmap
,
1853 backup
->has_on_source_error
, backup
->on_source_error
,
1854 backup
->has_on_target_error
, backup
->on_target_error
,
1855 common
->block_job_txn
, &local_err
);
1857 error_propagate(errp
, local_err
);
1861 state
->job
= state
->bs
->job
;
1864 static void drive_backup_abort(BlkActionState
*common
)
1866 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1867 BlockDriverState
*bs
= state
->bs
;
1869 /* Only cancel if it's the job we started */
1870 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1871 block_job_cancel_sync(bs
->job
);
1875 static void drive_backup_clean(BlkActionState
*common
)
1877 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1879 if (state
->aio_context
) {
1880 bdrv_drained_end(state
->bs
);
1881 aio_context_release(state
->aio_context
);
1885 typedef struct BlockdevBackupState
{
1886 BlkActionState common
;
1887 BlockDriverState
*bs
;
1889 AioContext
*aio_context
;
1890 } BlockdevBackupState
;
1892 static void do_blockdev_backup(const char *device
, const char *target
,
1893 enum MirrorSyncMode sync
,
1894 bool has_speed
, int64_t speed
,
1895 bool has_on_source_error
,
1896 BlockdevOnError on_source_error
,
1897 bool has_on_target_error
,
1898 BlockdevOnError on_target_error
,
1899 BlockJobTxn
*txn
, Error
**errp
);
1901 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1903 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1904 BlockdevBackup
*backup
;
1905 BlockBackend
*blk
, *target
;
1906 Error
*local_err
= NULL
;
1908 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1909 backup
= common
->action
->u
.blockdev_backup
;
1911 blk
= blk_by_name(backup
->device
);
1913 error_setg(errp
, "Device '%s' not found", backup
->device
);
1917 if (!blk_is_available(blk
)) {
1918 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1922 target
= blk_by_name(backup
->target
);
1924 error_setg(errp
, "Device '%s' not found", backup
->target
);
1928 /* AioContext is released in .clean() */
1929 state
->aio_context
= blk_get_aio_context(blk
);
1930 if (state
->aio_context
!= blk_get_aio_context(target
)) {
1931 state
->aio_context
= NULL
;
1932 error_setg(errp
, "Backup between two IO threads is not implemented");
1935 aio_context_acquire(state
->aio_context
);
1936 state
->bs
= blk_bs(blk
);
1937 bdrv_drained_begin(state
->bs
);
1939 do_blockdev_backup(backup
->device
, backup
->target
,
1941 backup
->has_speed
, backup
->speed
,
1942 backup
->has_on_source_error
, backup
->on_source_error
,
1943 backup
->has_on_target_error
, backup
->on_target_error
,
1944 common
->block_job_txn
, &local_err
);
1946 error_propagate(errp
, local_err
);
1950 state
->job
= state
->bs
->job
;
1953 static void blockdev_backup_abort(BlkActionState
*common
)
1955 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1956 BlockDriverState
*bs
= state
->bs
;
1958 /* Only cancel if it's the job we started */
1959 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1960 block_job_cancel_sync(bs
->job
);
1964 static void blockdev_backup_clean(BlkActionState
*common
)
1966 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1968 if (state
->aio_context
) {
1969 bdrv_drained_end(state
->bs
);
1970 aio_context_release(state
->aio_context
);
1974 typedef struct BlockDirtyBitmapState
{
1975 BlkActionState common
;
1976 BdrvDirtyBitmap
*bitmap
;
1977 BlockDriverState
*bs
;
1978 AioContext
*aio_context
;
1981 } BlockDirtyBitmapState
;
1983 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
1986 Error
*local_err
= NULL
;
1987 BlockDirtyBitmapAdd
*action
;
1988 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1991 if (action_check_completion_mode(common
, errp
) < 0) {
1995 action
= common
->action
->u
.block_dirty_bitmap_add
;
1996 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1997 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
1998 action
->has_granularity
, action
->granularity
,
2002 state
->prepared
= true;
2004 error_propagate(errp
, local_err
);
2008 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2010 BlockDirtyBitmapAdd
*action
;
2011 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2014 action
= common
->action
->u
.block_dirty_bitmap_add
;
2015 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2016 * then the node reference and bitmap name must have been valid.
2018 if (state
->prepared
) {
2019 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2023 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2026 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2028 BlockDirtyBitmap
*action
;
2030 if (action_check_completion_mode(common
, errp
) < 0) {
2034 action
= common
->action
->u
.block_dirty_bitmap_clear
;
2035 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2038 &state
->aio_context
,
2040 if (!state
->bitmap
) {
2044 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2045 error_setg(errp
, "Cannot modify a frozen bitmap");
2047 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2048 error_setg(errp
, "Cannot clear a disabled bitmap");
2052 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2053 /* AioContext is released in .clean() */
2056 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2058 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2061 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2064 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2066 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2069 hbitmap_free(state
->backup
);
2072 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2074 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2077 if (state
->aio_context
) {
2078 aio_context_release(state
->aio_context
);
2082 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2084 error_setg(errp
, "Transaction aborted using Abort action");
2087 static void abort_commit(BlkActionState
*common
)
2089 g_assert_not_reached(); /* this action never succeeds */
2092 static const BlkActionOps actions
[] = {
2093 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2094 .instance_size
= sizeof(ExternalSnapshotState
),
2095 .prepare
= external_snapshot_prepare
,
2096 .commit
= external_snapshot_commit
,
2097 .abort
= external_snapshot_abort
,
2098 .clean
= external_snapshot_clean
,
2100 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2101 .instance_size
= sizeof(ExternalSnapshotState
),
2102 .prepare
= external_snapshot_prepare
,
2103 .commit
= external_snapshot_commit
,
2104 .abort
= external_snapshot_abort
,
2105 .clean
= external_snapshot_clean
,
2107 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2108 .instance_size
= sizeof(DriveBackupState
),
2109 .prepare
= drive_backup_prepare
,
2110 .abort
= drive_backup_abort
,
2111 .clean
= drive_backup_clean
,
2113 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2114 .instance_size
= sizeof(BlockdevBackupState
),
2115 .prepare
= blockdev_backup_prepare
,
2116 .abort
= blockdev_backup_abort
,
2117 .clean
= blockdev_backup_clean
,
2119 [TRANSACTION_ACTION_KIND_ABORT
] = {
2120 .instance_size
= sizeof(BlkActionState
),
2121 .prepare
= abort_prepare
,
2122 .commit
= abort_commit
,
2124 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2125 .instance_size
= sizeof(InternalSnapshotState
),
2126 .prepare
= internal_snapshot_prepare
,
2127 .abort
= internal_snapshot_abort
,
2128 .clean
= internal_snapshot_clean
,
2130 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2131 .instance_size
= sizeof(BlockDirtyBitmapState
),
2132 .prepare
= block_dirty_bitmap_add_prepare
,
2133 .abort
= block_dirty_bitmap_add_abort
,
2135 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2136 .instance_size
= sizeof(BlockDirtyBitmapState
),
2137 .prepare
= block_dirty_bitmap_clear_prepare
,
2138 .commit
= block_dirty_bitmap_clear_commit
,
2139 .abort
= block_dirty_bitmap_clear_abort
,
2140 .clean
= block_dirty_bitmap_clear_clean
,
2145 * Allocate a TransactionProperties structure if necessary, and fill
2146 * that structure with desired defaults if they are unset.
2148 static TransactionProperties
*get_transaction_properties(
2149 TransactionProperties
*props
)
2152 props
= g_new0(TransactionProperties
, 1);
2155 if (!props
->has_completion_mode
) {
2156 props
->has_completion_mode
= true;
2157 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2164 * 'Atomic' group operations. The operations are performed as a set, and if
2165 * any fail then we roll back all operations in the group.
2167 void qmp_transaction(TransactionActionList
*dev_list
,
2169 struct TransactionProperties
*props
,
2172 TransactionActionList
*dev_entry
= dev_list
;
2173 BlockJobTxn
*block_job_txn
= NULL
;
2174 BlkActionState
*state
, *next
;
2175 Error
*local_err
= NULL
;
2177 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2178 QSIMPLEQ_INIT(&snap_bdrv_states
);
2180 /* Does this transaction get canceled as a group on failure?
2181 * If not, we don't really need to make a BlockJobTxn.
2183 props
= get_transaction_properties(props
);
2184 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2185 block_job_txn
= block_job_txn_new();
2188 /* drain all i/o before any operations */
2191 /* We don't do anything in this loop that commits us to the operations */
2192 while (NULL
!= dev_entry
) {
2193 TransactionAction
*dev_info
= NULL
;
2194 const BlkActionOps
*ops
;
2196 dev_info
= dev_entry
->value
;
2197 dev_entry
= dev_entry
->next
;
2199 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2201 ops
= &actions
[dev_info
->type
];
2202 assert(ops
->instance_size
> 0);
2204 state
= g_malloc0(ops
->instance_size
);
2206 state
->action
= dev_info
;
2207 state
->block_job_txn
= block_job_txn
;
2208 state
->txn_props
= props
;
2209 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2211 state
->ops
->prepare(state
, &local_err
);
2213 error_propagate(errp
, local_err
);
2214 goto delete_and_fail
;
2218 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2219 if (state
->ops
->commit
) {
2220 state
->ops
->commit(state
);
2228 /* failure, and it is all-or-none; roll back all operations */
2229 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2230 if (state
->ops
->abort
) {
2231 state
->ops
->abort(state
);
2235 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2236 if (state
->ops
->clean
) {
2237 state
->ops
->clean(state
);
2242 qapi_free_TransactionProperties(props
);
2244 block_job_txn_unref(block_job_txn
);
2247 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
2249 Error
*local_err
= NULL
;
2251 qmp_blockdev_open_tray(device
, has_force
, force
, &local_err
);
2253 error_propagate(errp
, local_err
);
2257 qmp_x_blockdev_remove_medium(device
, errp
);
2260 void qmp_block_passwd(bool has_device
, const char *device
,
2261 bool has_node_name
, const char *node_name
,
2262 const char *password
, Error
**errp
)
2264 Error
*local_err
= NULL
;
2265 BlockDriverState
*bs
;
2266 AioContext
*aio_context
;
2268 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2269 has_node_name
? node_name
: NULL
,
2272 error_propagate(errp
, local_err
);
2276 aio_context
= bdrv_get_aio_context(bs
);
2277 aio_context_acquire(aio_context
);
2279 bdrv_add_key(bs
, password
, errp
);
2281 aio_context_release(aio_context
);
2284 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2294 blk
= blk_by_name(device
);
2296 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2297 "Device '%s' not found", device
);
2301 if (!blk_dev_has_removable_media(blk
)) {
2302 error_setg(errp
, "Device '%s' is not removable", device
);
2306 if (blk_dev_is_tray_open(blk
)) {
2310 locked
= blk_dev_is_medium_locked(blk
);
2312 blk_dev_eject_request(blk
, force
);
2315 if (!locked
|| force
) {
2316 blk_dev_change_media_cb(blk
, false);
2320 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2324 blk
= blk_by_name(device
);
2326 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2327 "Device '%s' not found", device
);
2331 if (!blk_dev_has_removable_media(blk
)) {
2332 error_setg(errp
, "Device '%s' is not removable", device
);
2336 if (!blk_dev_is_tray_open(blk
)) {
2340 blk_dev_change_media_cb(blk
, true);
2343 void qmp_x_blockdev_remove_medium(const char *device
, Error
**errp
)
2346 BlockDriverState
*bs
;
2347 AioContext
*aio_context
;
2350 blk
= blk_by_name(device
);
2352 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2353 "Device '%s' not found", device
);
2357 /* For BBs without a device, we can exchange the BDS tree at will */
2358 has_device
= blk_get_attached_dev(blk
);
2360 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2361 error_setg(errp
, "Device '%s' is not removable", device
);
2365 if (has_device
&& !blk_dev_is_tray_open(blk
)) {
2366 error_setg(errp
, "Tray of device '%s' is not open", device
);
2375 aio_context
= bdrv_get_aio_context(bs
);
2376 aio_context_acquire(aio_context
);
2378 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2382 /* This follows the convention established by bdrv_make_anon() */
2383 if (bs
->device_list
.tqe_prev
) {
2384 QTAILQ_REMOVE(&bdrv_states
, bs
, device_list
);
2385 bs
->device_list
.tqe_prev
= NULL
;
2391 aio_context_release(aio_context
);
2394 static void qmp_blockdev_insert_anon_medium(const char *device
,
2395 BlockDriverState
*bs
, Error
**errp
)
2400 blk
= blk_by_name(device
);
2402 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2403 "Device '%s' not found", device
);
2407 /* For BBs without a device, we can exchange the BDS tree at will */
2408 has_device
= blk_get_attached_dev(blk
);
2410 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2411 error_setg(errp
, "Device '%s' is not removable", device
);
2415 if (has_device
&& !blk_dev_is_tray_open(blk
)) {
2416 error_setg(errp
, "Tray of device '%s' is not open", device
);
2421 error_setg(errp
, "There already is a medium in device '%s'", device
);
2425 blk_insert_bs(blk
, bs
);
2427 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, device_list
);
2430 void qmp_x_blockdev_insert_medium(const char *device
, const char *node_name
,
2433 BlockDriverState
*bs
;
2435 bs
= bdrv_find_node(node_name
);
2437 error_setg(errp
, "Node '%s' not found", node_name
);
2442 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2447 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2450 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2451 bool has_format
, const char *format
,
2453 BlockdevChangeReadOnlyMode read_only
,
2457 BlockDriverState
*medium_bs
= NULL
;
2458 int bdrv_flags
, ret
;
2459 QDict
*options
= NULL
;
2462 blk
= blk_by_name(device
);
2464 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2465 "Device '%s' not found", device
);
2470 blk_update_root_state(blk
);
2473 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2475 if (!has_read_only
) {
2476 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2479 switch (read_only
) {
2480 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2483 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2484 bdrv_flags
&= ~BDRV_O_RDWR
;
2487 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2488 bdrv_flags
|= BDRV_O_RDWR
;
2496 options
= qdict_new();
2497 qdict_put(options
, "driver", qstring_from_str(format
));
2501 ret
= bdrv_open(&medium_bs
, filename
, NULL
, options
, bdrv_flags
, errp
);
2506 blk_apply_root_state(blk
, medium_bs
);
2508 bdrv_add_key(medium_bs
, NULL
, &err
);
2510 error_propagate(errp
, err
);
2514 qmp_blockdev_open_tray(device
, false, false, &err
);
2516 error_propagate(errp
, err
);
2520 qmp_x_blockdev_remove_medium(device
, &err
);
2522 error_propagate(errp
, err
);
2526 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2528 error_propagate(errp
, err
);
2532 qmp_blockdev_close_tray(device
, errp
);
2535 /* If the medium has been inserted, the device has its own reference, so
2536 * ours must be relinquished; and if it has not been inserted successfully,
2537 * the reference must be relinquished anyway */
2538 bdrv_unref(medium_bs
);
2541 /* throttling disk I/O limits */
2542 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
2549 bool has_bps_rd_max
,
2551 bool has_bps_wr_max
,
2555 bool has_iops_rd_max
,
2556 int64_t iops_rd_max
,
2557 bool has_iops_wr_max
,
2558 int64_t iops_wr_max
,
2562 const char *group
, Error
**errp
)
2565 BlockDriverState
*bs
;
2567 AioContext
*aio_context
;
2569 blk
= blk_by_name(device
);
2571 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2572 "Device '%s' not found", device
);
2576 aio_context
= blk_get_aio_context(blk
);
2577 aio_context_acquire(aio_context
);
2581 error_setg(errp
, "Device '%s' has no medium", device
);
2585 memset(&cfg
, 0, sizeof(cfg
));
2586 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
2587 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
2588 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
2590 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
2591 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
2592 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
2595 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
2597 if (has_bps_rd_max
) {
2598 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2600 if (has_bps_wr_max
) {
2601 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2604 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2606 if (has_iops_rd_max
) {
2607 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2609 if (has_iops_wr_max
) {
2610 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2613 if (has_iops_size
) {
2614 cfg
.op_size
= iops_size
;
2617 if (!check_throttle_config(&cfg
, errp
)) {
2621 if (throttle_enabled(&cfg
)) {
2622 /* Enable I/O limits if they're not enabled yet, otherwise
2623 * just update the throttling group. */
2624 if (!bs
->throttle_state
) {
2625 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2626 } else if (has_group
) {
2627 bdrv_io_limits_update_group(bs
, group
);
2629 /* Set the new throttling configuration */
2630 bdrv_set_io_limits(bs
, &cfg
);
2631 } else if (bs
->throttle_state
) {
2632 /* If all throttling settings are set to 0, disable I/O limits */
2633 bdrv_io_limits_disable(bs
);
2637 aio_context_release(aio_context
);
2640 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2641 bool has_granularity
, uint32_t granularity
,
2644 AioContext
*aio_context
;
2645 BlockDriverState
*bs
;
2647 if (!name
|| name
[0] == '\0') {
2648 error_setg(errp
, "Bitmap name cannot be empty");
2652 bs
= bdrv_lookup_bs(node
, node
, errp
);
2657 aio_context
= bdrv_get_aio_context(bs
);
2658 aio_context_acquire(aio_context
);
2660 if (has_granularity
) {
2661 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2662 error_setg(errp
, "Granularity must be power of 2 "
2663 "and at least 512");
2667 /* Default to cluster size, if available: */
2668 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2671 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2674 aio_context_release(aio_context
);
2677 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2680 AioContext
*aio_context
;
2681 BlockDriverState
*bs
;
2682 BdrvDirtyBitmap
*bitmap
;
2684 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2685 if (!bitmap
|| !bs
) {
2689 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2691 "Bitmap '%s' is currently frozen and cannot be removed",
2695 bdrv_dirty_bitmap_make_anon(bitmap
);
2696 bdrv_release_dirty_bitmap(bs
, bitmap
);
2699 aio_context_release(aio_context
);
2703 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2704 * immediately after a full backup operation.
2706 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2709 AioContext
*aio_context
;
2710 BdrvDirtyBitmap
*bitmap
;
2711 BlockDriverState
*bs
;
2713 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2714 if (!bitmap
|| !bs
) {
2718 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2720 "Bitmap '%s' is currently frozen and cannot be modified",
2723 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2725 "Bitmap '%s' is currently disabled and cannot be cleared",
2730 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2733 aio_context_release(aio_context
);
2736 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2738 const char *id
= qdict_get_str(qdict
, "id");
2740 BlockDriverState
*bs
;
2741 AioContext
*aio_context
;
2742 Error
*local_err
= NULL
;
2744 blk
= blk_by_name(id
);
2746 error_report("Device '%s' not found", id
);
2750 if (!blk_legacy_dinfo(blk
)) {
2751 error_report("Deleting device added with blockdev-add"
2752 " is not supported");
2756 aio_context
= blk_get_aio_context(blk
);
2757 aio_context_acquire(aio_context
);
2761 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2762 error_report_err(local_err
);
2763 aio_context_release(aio_context
);
2770 /* if we have a device attached to this BlockDriverState
2771 * then we need to make the drive anonymous until the device
2772 * can be removed. If this is a drive with no device backing
2773 * then we can just get rid of the block driver state right here.
2775 if (blk_get_attached_dev(blk
)) {
2776 blk_hide_on_behalf_of_hmp_drive_del(blk
);
2777 /* Further I/O must not pause the guest */
2778 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2779 BLOCKDEV_ON_ERROR_REPORT
);
2784 aio_context_release(aio_context
);
2787 void qmp_block_resize(bool has_device
, const char *device
,
2788 bool has_node_name
, const char *node_name
,
2789 int64_t size
, Error
**errp
)
2791 Error
*local_err
= NULL
;
2792 BlockDriverState
*bs
;
2793 AioContext
*aio_context
;
2796 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2797 has_node_name
? node_name
: NULL
,
2800 error_propagate(errp
, local_err
);
2804 aio_context
= bdrv_get_aio_context(bs
);
2805 aio_context_acquire(aio_context
);
2807 if (!bdrv_is_first_non_filter(bs
)) {
2808 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2813 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2817 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2818 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2822 /* complete all in-flight operations before resizing the device */
2825 ret
= bdrv_truncate(bs
, size
);
2830 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2833 error_setg(errp
, QERR_UNSUPPORTED
);
2836 error_setg(errp
, "Device '%s' is read only", device
);
2839 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2842 error_setg_errno(errp
, -ret
, "Could not resize");
2847 aio_context_release(aio_context
);
2850 static void block_job_cb(void *opaque
, int ret
)
2852 /* Note that this function may be executed from another AioContext besides
2853 * the QEMU main loop. If you need to access anything that assumes the
2854 * QEMU global mutex, use a BH or introduce a mutex.
2857 BlockDriverState
*bs
= opaque
;
2858 const char *msg
= NULL
;
2860 trace_block_job_cb(bs
, bs
->job
, ret
);
2865 msg
= strerror(-ret
);
2868 if (block_job_is_cancelled(bs
->job
)) {
2869 block_job_event_cancelled(bs
->job
);
2871 block_job_event_completed(bs
->job
, msg
);
2875 void qmp_block_stream(const char *device
,
2876 bool has_base
, const char *base
,
2877 bool has_backing_file
, const char *backing_file
,
2878 bool has_speed
, int64_t speed
,
2879 bool has_on_error
, BlockdevOnError on_error
,
2883 BlockDriverState
*bs
;
2884 BlockDriverState
*base_bs
= NULL
;
2885 AioContext
*aio_context
;
2886 Error
*local_err
= NULL
;
2887 const char *base_name
= NULL
;
2889 if (!has_on_error
) {
2890 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2893 blk
= blk_by_name(device
);
2895 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2896 "Device '%s' not found", device
);
2900 aio_context
= blk_get_aio_context(blk
);
2901 aio_context_acquire(aio_context
);
2903 if (!blk_is_available(blk
)) {
2904 error_setg(errp
, "Device '%s' has no medium", device
);
2909 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2914 base_bs
= bdrv_find_backing_image(bs
, base
);
2915 if (base_bs
== NULL
) {
2916 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2919 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2923 /* if we are streaming the entire chain, the result will have no backing
2924 * file, and specifying one is therefore an error */
2925 if (base_bs
== NULL
&& has_backing_file
) {
2926 error_setg(errp
, "backing file specified, but streaming the "
2931 /* backing_file string overrides base bs filename */
2932 base_name
= has_backing_file
? backing_file
: base_name
;
2934 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
2935 on_error
, block_job_cb
, bs
, &local_err
);
2937 error_propagate(errp
, local_err
);
2941 trace_qmp_block_stream(bs
, bs
->job
);
2944 aio_context_release(aio_context
);
2947 void qmp_block_commit(const char *device
,
2948 bool has_base
, const char *base
,
2949 bool has_top
, const char *top
,
2950 bool has_backing_file
, const char *backing_file
,
2951 bool has_speed
, int64_t speed
,
2955 BlockDriverState
*bs
;
2956 BlockDriverState
*base_bs
, *top_bs
;
2957 AioContext
*aio_context
;
2958 Error
*local_err
= NULL
;
2959 /* This will be part of the QMP command, if/when the
2960 * BlockdevOnError change for blkmirror makes it in
2962 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2969 * libvirt relies on the DeviceNotFound error class in order to probe for
2970 * live commit feature versions; for this to work, we must make sure to
2971 * perform the device lookup before any generic errors that may occur in a
2972 * scenario in which all optional arguments are omitted. */
2973 blk
= blk_by_name(device
);
2975 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2976 "Device '%s' not found", device
);
2980 aio_context
= blk_get_aio_context(blk
);
2981 aio_context_acquire(aio_context
);
2983 if (!blk_is_available(blk
)) {
2984 error_setg(errp
, "Device '%s' has no medium", device
);
2989 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2993 /* default top_bs is the active layer */
2996 if (has_top
&& top
) {
2997 if (strcmp(bs
->filename
, top
) != 0) {
2998 top_bs
= bdrv_find_backing_image(bs
, top
);
3002 if (top_bs
== NULL
) {
3003 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3007 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3009 if (has_base
&& base
) {
3010 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3012 base_bs
= bdrv_find_base(top_bs
);
3015 if (base_bs
== NULL
) {
3016 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3020 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3022 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3026 /* Do not allow attempts to commit an image into itself */
3027 if (top_bs
== base_bs
) {
3028 error_setg(errp
, "cannot commit an image into itself");
3033 if (has_backing_file
) {
3034 error_setg(errp
, "'backing-file' specified,"
3035 " but 'top' is the active layer");
3038 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
3041 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
3042 has_backing_file
? backing_file
: NULL
, &local_err
);
3044 if (local_err
!= NULL
) {
3045 error_propagate(errp
, local_err
);
3050 aio_context_release(aio_context
);
3053 static void do_drive_backup(const char *device
, const char *target
,
3054 bool has_format
, const char *format
,
3055 enum MirrorSyncMode sync
,
3056 bool has_mode
, enum NewImageMode mode
,
3057 bool has_speed
, int64_t speed
,
3058 bool has_bitmap
, const char *bitmap
,
3059 bool has_on_source_error
,
3060 BlockdevOnError on_source_error
,
3061 bool has_on_target_error
,
3062 BlockdevOnError on_target_error
,
3063 BlockJobTxn
*txn
, Error
**errp
)
3066 BlockDriverState
*bs
;
3067 BlockDriverState
*target_bs
;
3068 BlockDriverState
*source
= NULL
;
3069 BdrvDirtyBitmap
*bmap
= NULL
;
3070 AioContext
*aio_context
;
3071 QDict
*options
= NULL
;
3072 Error
*local_err
= NULL
;
3080 if (!has_on_source_error
) {
3081 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3083 if (!has_on_target_error
) {
3084 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3087 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3090 blk
= blk_by_name(device
);
3092 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3093 "Device '%s' not found", device
);
3097 aio_context
= blk_get_aio_context(blk
);
3098 aio_context_acquire(aio_context
);
3100 /* Although backup_run has this check too, we need to use bs->drv below, so
3101 * do an early check redundantly. */
3102 if (!blk_is_available(blk
)) {
3103 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3109 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3112 /* Early check to avoid creating target */
3113 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3117 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3119 /* See if we have a backing HD we can use to create our new image
3121 if (sync
== MIRROR_SYNC_MODE_TOP
) {
3122 source
= backing_bs(bs
);
3124 sync
= MIRROR_SYNC_MODE_FULL
;
3127 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3131 size
= bdrv_getlength(bs
);
3133 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3137 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
3140 bdrv_img_create(target
, format
, source
->filename
,
3141 source
->drv
->format_name
, NULL
,
3142 size
, flags
, &local_err
, false);
3144 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
3145 size
, flags
, &local_err
, false);
3150 error_propagate(errp
, local_err
);
3155 options
= qdict_new();
3156 qdict_put(options
, "driver", qstring_from_str(format
));
3160 ret
= bdrv_open(&target_bs
, target
, NULL
, options
, flags
, &local_err
);
3162 error_propagate(errp
, local_err
);
3166 bdrv_set_aio_context(target_bs
, aio_context
);
3169 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
3171 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
3172 bdrv_unref(target_bs
);
3177 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
3178 on_source_error
, on_target_error
,
3179 block_job_cb
, bs
, txn
, &local_err
);
3180 if (local_err
!= NULL
) {
3181 bdrv_unref(target_bs
);
3182 error_propagate(errp
, local_err
);
3187 aio_context_release(aio_context
);
3190 void qmp_drive_backup(const char *device
, const char *target
,
3191 bool has_format
, const char *format
,
3192 enum MirrorSyncMode sync
,
3193 bool has_mode
, enum NewImageMode mode
,
3194 bool has_speed
, int64_t speed
,
3195 bool has_bitmap
, const char *bitmap
,
3196 bool has_on_source_error
, BlockdevOnError on_source_error
,
3197 bool has_on_target_error
, BlockdevOnError on_target_error
,
3200 return do_drive_backup(device
, target
, has_format
, format
, sync
,
3201 has_mode
, mode
, has_speed
, speed
,
3203 has_on_source_error
, on_source_error
,
3204 has_on_target_error
, on_target_error
,
3208 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3210 return bdrv_named_nodes_list(errp
);
3213 void do_blockdev_backup(const char *device
, const char *target
,
3214 enum MirrorSyncMode sync
,
3215 bool has_speed
, int64_t speed
,
3216 bool has_on_source_error
,
3217 BlockdevOnError on_source_error
,
3218 bool has_on_target_error
,
3219 BlockdevOnError on_target_error
,
3220 BlockJobTxn
*txn
, Error
**errp
)
3222 BlockBackend
*blk
, *target_blk
;
3223 BlockDriverState
*bs
;
3224 BlockDriverState
*target_bs
;
3225 Error
*local_err
= NULL
;
3226 AioContext
*aio_context
;
3231 if (!has_on_source_error
) {
3232 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3234 if (!has_on_target_error
) {
3235 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3238 blk
= blk_by_name(device
);
3240 error_setg(errp
, "Device '%s' not found", device
);
3244 aio_context
= blk_get_aio_context(blk
);
3245 aio_context_acquire(aio_context
);
3247 if (!blk_is_available(blk
)) {
3248 error_setg(errp
, "Device '%s' has no medium", device
);
3253 target_blk
= blk_by_name(target
);
3255 error_setg(errp
, "Device '%s' not found", target
);
3259 if (!blk_is_available(target_blk
)) {
3260 error_setg(errp
, "Device '%s' has no medium", target
);
3263 target_bs
= blk_bs(target_blk
);
3265 bdrv_ref(target_bs
);
3266 bdrv_set_aio_context(target_bs
, aio_context
);
3267 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
3268 on_target_error
, block_job_cb
, bs
, txn
, &local_err
);
3269 if (local_err
!= NULL
) {
3270 bdrv_unref(target_bs
);
3271 error_propagate(errp
, local_err
);
3274 aio_context_release(aio_context
);
3277 void qmp_blockdev_backup(const char *device
, const char *target
,
3278 enum MirrorSyncMode sync
,
3279 bool has_speed
, int64_t speed
,
3280 bool has_on_source_error
,
3281 BlockdevOnError on_source_error
,
3282 bool has_on_target_error
,
3283 BlockdevOnError on_target_error
,
3286 do_blockdev_backup(device
, target
, sync
, has_speed
, speed
,
3287 has_on_source_error
, on_source_error
,
3288 has_on_target_error
, on_target_error
,
3292 /* Parameter check and block job starting for drive mirroring.
3293 * Caller should hold @device and @target's aio context (must be the same).
3295 static void blockdev_mirror_common(BlockDriverState
*bs
,
3296 BlockDriverState
*target
,
3297 bool has_replaces
, const char *replaces
,
3298 enum MirrorSyncMode sync
,
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
,
3303 BlockdevOnError on_source_error
,
3304 bool has_on_target_error
,
3305 BlockdevOnError on_target_error
,
3306 bool has_unmap
, bool unmap
,
3313 if (!has_on_source_error
) {
3314 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3316 if (!has_on_target_error
) {
3317 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3319 if (!has_granularity
) {
3322 if (!has_buf_size
) {
3329 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3330 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3331 "a value in range [512B, 64MB]");
3334 if (granularity
& (granularity
- 1)) {
3335 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3340 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3343 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3347 error_setg(errp
, "Cannot mirror to an attached block device");
3351 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3352 sync
= MIRROR_SYNC_MODE_FULL
;
3355 /* pass the node name to replace to mirror start since it's loose coupling
3356 * and will allow to check whether the node still exist at mirror completion
3358 mirror_start(bs
, target
,
3359 has_replaces
? replaces
: NULL
,
3360 speed
, granularity
, buf_size
, sync
,
3361 on_source_error
, on_target_error
, unmap
,
3362 block_job_cb
, bs
, errp
);
3365 void qmp_drive_mirror(const char *device
, const char *target
,
3366 bool has_format
, const char *format
,
3367 bool has_node_name
, const char *node_name
,
3368 bool has_replaces
, const char *replaces
,
3369 enum MirrorSyncMode sync
,
3370 bool has_mode
, enum NewImageMode mode
,
3371 bool has_speed
, int64_t speed
,
3372 bool has_granularity
, uint32_t granularity
,
3373 bool has_buf_size
, int64_t buf_size
,
3374 bool has_on_source_error
, BlockdevOnError on_source_error
,
3375 bool has_on_target_error
, BlockdevOnError on_target_error
,
3376 bool has_unmap
, bool unmap
,
3379 BlockDriverState
*bs
;
3381 BlockDriverState
*source
, *target_bs
;
3382 AioContext
*aio_context
;
3383 Error
*local_err
= NULL
;
3384 QDict
*options
= NULL
;
3389 blk
= blk_by_name(device
);
3391 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3392 "Device '%s' not found", device
);
3396 aio_context
= blk_get_aio_context(blk
);
3397 aio_context_acquire(aio_context
);
3399 if (!blk_is_available(blk
)) {
3400 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3405 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3409 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3412 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3413 source
= backing_bs(bs
);
3414 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3415 sync
= MIRROR_SYNC_MODE_FULL
;
3417 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3421 size
= bdrv_getlength(bs
);
3423 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3428 BlockDriverState
*to_replace_bs
;
3429 AioContext
*replace_aio_context
;
3430 int64_t replace_size
;
3432 if (!has_node_name
) {
3433 error_setg(errp
, "a node-name must be provided when replacing a"
3434 " named node of the graph");
3438 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
3440 if (!to_replace_bs
) {
3441 error_propagate(errp
, local_err
);
3445 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3446 aio_context_acquire(replace_aio_context
);
3447 replace_size
= bdrv_getlength(to_replace_bs
);
3448 aio_context_release(replace_aio_context
);
3450 if (size
!= replace_size
) {
3451 error_setg(errp
, "cannot replace image with a mirror image of "
3457 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3458 && mode
!= NEW_IMAGE_MODE_EXISTING
)
3460 /* create new image w/o backing file */
3462 bdrv_img_create(target
, format
,
3463 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3466 case NEW_IMAGE_MODE_EXISTING
:
3468 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3469 /* create new image with backing file */
3470 bdrv_img_create(target
, format
,
3472 source
->drv
->format_name
,
3473 NULL
, size
, flags
, &local_err
, false);
3481 error_propagate(errp
, local_err
);
3485 options
= qdict_new();
3486 if (has_node_name
) {
3487 qdict_put(options
, "node-name", qstring_from_str(node_name
));
3490 qdict_put(options
, "driver", qstring_from_str(format
));
3493 /* Mirroring takes care of copy-on-write using the source's backing
3497 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
3498 flags
| BDRV_O_NO_BACKING
, &local_err
);
3500 error_propagate(errp
, local_err
);
3504 bdrv_set_aio_context(target_bs
, aio_context
);
3506 blockdev_mirror_common(bs
, target_bs
,
3507 has_replaces
, replaces
, sync
,
3509 has_granularity
, granularity
,
3510 has_buf_size
, buf_size
,
3511 has_on_source_error
, on_source_error
,
3512 has_on_target_error
, on_target_error
,
3516 error_propagate(errp
, local_err
);
3517 bdrv_unref(target_bs
);
3520 aio_context_release(aio_context
);
3523 void qmp_blockdev_mirror(const char *device
, const char *target
,
3524 bool has_replaces
, const char *replaces
,
3525 MirrorSyncMode sync
,
3526 bool has_speed
, int64_t speed
,
3527 bool has_granularity
, uint32_t granularity
,
3528 bool has_buf_size
, int64_t buf_size
,
3529 bool has_on_source_error
,
3530 BlockdevOnError on_source_error
,
3531 bool has_on_target_error
,
3532 BlockdevOnError on_target_error
,
3535 BlockDriverState
*bs
;
3537 BlockDriverState
*target_bs
;
3538 AioContext
*aio_context
;
3539 Error
*local_err
= NULL
;
3541 blk
= blk_by_name(device
);
3543 error_setg(errp
, "Device '%s' not found", device
);
3549 error_setg(errp
, "Device '%s' has no media", device
);
3553 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3558 aio_context
= bdrv_get_aio_context(bs
);
3559 aio_context_acquire(aio_context
);
3561 bdrv_ref(target_bs
);
3562 bdrv_set_aio_context(target_bs
, aio_context
);
3564 blockdev_mirror_common(bs
, target_bs
,
3565 has_replaces
, replaces
, sync
,
3567 has_granularity
, granularity
,
3568 has_buf_size
, buf_size
,
3569 has_on_source_error
, on_source_error
,
3570 has_on_target_error
, on_target_error
,
3574 error_propagate(errp
, local_err
);
3575 bdrv_unref(target_bs
);
3578 aio_context_release(aio_context
);
3581 /* Get the block job for a given device name and acquire its AioContext */
3582 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
3586 BlockDriverState
*bs
;
3588 *aio_context
= NULL
;
3590 blk
= blk_by_name(device
);
3595 *aio_context
= blk_get_aio_context(blk
);
3596 aio_context_acquire(*aio_context
);
3598 if (!blk_is_available(blk
)) {
3610 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3611 "No active block job on device '%s'", device
);
3613 aio_context_release(*aio_context
);
3614 *aio_context
= NULL
;
3619 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3621 AioContext
*aio_context
;
3622 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3628 block_job_set_speed(job
, speed
, errp
);
3629 aio_context_release(aio_context
);
3632 void qmp_block_job_cancel(const char *device
,
3633 bool has_force
, bool force
, Error
**errp
)
3635 AioContext
*aio_context
;
3636 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3646 if (job
->user_paused
&& !force
) {
3647 error_setg(errp
, "The block job for device '%s' is currently paused",
3652 trace_qmp_block_job_cancel(job
);
3653 block_job_cancel(job
);
3655 aio_context_release(aio_context
);
3658 void qmp_block_job_pause(const char *device
, Error
**errp
)
3660 AioContext
*aio_context
;
3661 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3663 if (!job
|| job
->user_paused
) {
3667 job
->user_paused
= true;
3668 trace_qmp_block_job_pause(job
);
3669 block_job_pause(job
);
3670 aio_context_release(aio_context
);
3673 void qmp_block_job_resume(const char *device
, Error
**errp
)
3675 AioContext
*aio_context
;
3676 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3678 if (!job
|| !job
->user_paused
) {
3682 job
->user_paused
= false;
3683 trace_qmp_block_job_resume(job
);
3684 block_job_resume(job
);
3685 aio_context_release(aio_context
);
3688 void qmp_block_job_complete(const char *device
, Error
**errp
)
3690 AioContext
*aio_context
;
3691 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3697 trace_qmp_block_job_complete(job
);
3698 block_job_complete(job
, errp
);
3699 aio_context_release(aio_context
);
3702 void qmp_change_backing_file(const char *device
,
3703 const char *image_node_name
,
3704 const char *backing_file
,
3708 BlockDriverState
*bs
= NULL
;
3709 AioContext
*aio_context
;
3710 BlockDriverState
*image_bs
= NULL
;
3711 Error
*local_err
= NULL
;
3716 blk
= blk_by_name(device
);
3718 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3719 "Device '%s' not found", device
);
3723 aio_context
= blk_get_aio_context(blk
);
3724 aio_context_acquire(aio_context
);
3726 if (!blk_is_available(blk
)) {
3727 error_setg(errp
, "Device '%s' has no medium", device
);
3732 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3734 error_propagate(errp
, local_err
);
3739 error_setg(errp
, "image file not found");
3743 if (bdrv_find_base(image_bs
) == image_bs
) {
3744 error_setg(errp
, "not allowing backing file change on an image "
3745 "without a backing file");
3749 /* even though we are not necessarily operating on bs, we need it to
3750 * determine if block ops are currently prohibited on the chain */
3751 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3755 /* final sanity check */
3756 if (!bdrv_chain_contains(bs
, image_bs
)) {
3757 error_setg(errp
, "'%s' and image file are not in the same chain",
3762 /* if not r/w, reopen to make r/w */
3763 open_flags
= image_bs
->open_flags
;
3764 ro
= bdrv_is_read_only(image_bs
);
3767 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3769 error_propagate(errp
, local_err
);
3774 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3775 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3778 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3780 /* don't exit here, so we can try to restore open flags if
3785 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3787 error_propagate(errp
, local_err
); /* will preserve prior errp */
3792 aio_context_release(aio_context
);
3795 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3797 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3798 BlockDriverState
*bs
;
3799 BlockBackend
*blk
= NULL
;
3802 Error
*local_err
= NULL
;
3804 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3805 * cache.direct=false instead of silently switching to aio=threads, except
3806 * when called from drive_new().
3808 * For now, simply forbidding the combination for all drivers will do. */
3809 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3810 bool direct
= options
->has_cache
&&
3811 options
->cache
->has_direct
&&
3812 options
->cache
->direct
;
3814 error_setg(errp
, "aio=native requires cache.direct=true");
3819 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
3820 &options
, NULL
, &local_err
);
3822 error_propagate(errp
, local_err
);
3826 obj
= qmp_output_get_qobject(ov
);
3827 qdict
= qobject_to_qdict(obj
);
3829 qdict_flatten(qdict
);
3831 if (options
->has_id
) {
3832 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3834 error_propagate(errp
, local_err
);
3840 if (!qdict_get_try_str(qdict
, "node-name")) {
3841 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3846 bs
= bds_tree_init(qdict
, errp
);
3852 if (bs
&& bdrv_key_required(bs
)) {
3858 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3863 qmp_output_visitor_cleanup(ov
);
3866 void qmp_x_blockdev_del(bool has_id
, const char *id
,
3867 bool has_node_name
, const char *node_name
, Error
**errp
)
3869 AioContext
*aio_context
;
3871 BlockDriverState
*bs
;
3873 if (has_id
&& has_node_name
) {
3874 error_setg(errp
, "Only one of id and node-name must be specified");
3876 } else if (!has_id
&& !has_node_name
) {
3877 error_setg(errp
, "No block device specified");
3882 blk
= blk_by_name(id
);
3884 error_setg(errp
, "Cannot find block backend %s", id
);
3887 if (blk_get_refcnt(blk
) > 1) {
3888 error_setg(errp
, "Block backend %s is in use", id
);
3892 aio_context
= blk_get_aio_context(blk
);
3894 bs
= bdrv_find_node(node_name
);
3896 error_setg(errp
, "Cannot find node %s", node_name
);
3901 error_setg(errp
, "Node %s is in use by %s",
3902 node_name
, blk_name(blk
));
3905 aio_context
= bdrv_get_aio_context(bs
);
3908 aio_context_acquire(aio_context
);
3911 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3915 if (bs
->refcnt
> 1 || !QLIST_EMPTY(&bs
->parents
)) {
3916 error_setg(errp
, "Block device %s is in use",
3917 bdrv_get_device_or_node_name(bs
));
3929 aio_context_release(aio_context
);
3932 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3934 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3935 BlockDriverState
*bs
;
3937 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3938 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
3940 aio_context_acquire(aio_context
);
3943 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
3944 elem
->value
= block_job_query(bs
->job
);
3946 p_next
= &elem
->next
;
3949 aio_context_release(aio_context
);
3955 QemuOptsList qemu_common_drive_opts
= {
3957 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3961 .type
= QEMU_OPT_BOOL
,
3962 .help
= "enable/disable snapshot mode",
3965 .type
= QEMU_OPT_STRING
,
3966 .help
= "discard operation (ignore/off, unmap/on)",
3969 .type
= QEMU_OPT_STRING
,
3970 .help
= "host AIO implementation (threads, native)",
3973 .type
= QEMU_OPT_STRING
,
3974 .help
= "disk format (raw, qcow2, ...)",
3977 .type
= QEMU_OPT_STRING
,
3978 .help
= "read error action",
3981 .type
= QEMU_OPT_STRING
,
3982 .help
= "write error action",
3984 .name
= "read-only",
3985 .type
= QEMU_OPT_BOOL
,
3986 .help
= "open drive file as read-only",
3988 .name
= "throttling.iops-total",
3989 .type
= QEMU_OPT_NUMBER
,
3990 .help
= "limit total I/O operations per second",
3992 .name
= "throttling.iops-read",
3993 .type
= QEMU_OPT_NUMBER
,
3994 .help
= "limit read operations per second",
3996 .name
= "throttling.iops-write",
3997 .type
= QEMU_OPT_NUMBER
,
3998 .help
= "limit write operations per second",
4000 .name
= "throttling.bps-total",
4001 .type
= QEMU_OPT_NUMBER
,
4002 .help
= "limit total bytes per second",
4004 .name
= "throttling.bps-read",
4005 .type
= QEMU_OPT_NUMBER
,
4006 .help
= "limit read bytes per second",
4008 .name
= "throttling.bps-write",
4009 .type
= QEMU_OPT_NUMBER
,
4010 .help
= "limit write bytes per second",
4012 .name
= "throttling.iops-total-max",
4013 .type
= QEMU_OPT_NUMBER
,
4014 .help
= "I/O operations burst",
4016 .name
= "throttling.iops-read-max",
4017 .type
= QEMU_OPT_NUMBER
,
4018 .help
= "I/O operations read burst",
4020 .name
= "throttling.iops-write-max",
4021 .type
= QEMU_OPT_NUMBER
,
4022 .help
= "I/O operations write burst",
4024 .name
= "throttling.bps-total-max",
4025 .type
= QEMU_OPT_NUMBER
,
4026 .help
= "total bytes burst",
4028 .name
= "throttling.bps-read-max",
4029 .type
= QEMU_OPT_NUMBER
,
4030 .help
= "total bytes read burst",
4032 .name
= "throttling.bps-write-max",
4033 .type
= QEMU_OPT_NUMBER
,
4034 .help
= "total bytes write burst",
4036 .name
= "throttling.iops-size",
4037 .type
= QEMU_OPT_NUMBER
,
4038 .help
= "when limiting by iops max size of an I/O in bytes",
4040 .name
= "throttling.group",
4041 .type
= QEMU_OPT_STRING
,
4042 .help
= "name of the block throttling group",
4044 .name
= "copy-on-read",
4045 .type
= QEMU_OPT_BOOL
,
4046 .help
= "copy read data from backing file into image file",
4048 .name
= "detect-zeroes",
4049 .type
= QEMU_OPT_STRING
,
4050 .help
= "try to optimize zero writes (off, on, unmap)",
4052 .name
= "stats-account-invalid",
4053 .type
= QEMU_OPT_BOOL
,
4054 .help
= "whether to account for invalid I/O operations "
4055 "in the statistics",
4057 .name
= "stats-account-failed",
4058 .type
= QEMU_OPT_BOOL
,
4059 .help
= "whether to account for failed I/O operations "
4060 "in the statistics",
4062 { /* end of list */ }
4066 static QemuOptsList qemu_root_bds_opts
= {
4068 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4072 .type
= QEMU_OPT_STRING
,
4073 .help
= "discard operation (ignore/off, unmap/on)",
4076 .type
= QEMU_OPT_STRING
,
4077 .help
= "host AIO implementation (threads, native)",
4079 .name
= "read-only",
4080 .type
= QEMU_OPT_BOOL
,
4081 .help
= "open drive file as read-only",
4083 .name
= "copy-on-read",
4084 .type
= QEMU_OPT_BOOL
,
4085 .help
= "copy read data from backing file into image file",
4087 .name
= "detect-zeroes",
4088 .type
= QEMU_OPT_STRING
,
4089 .help
= "try to optimize zero writes (off, on, unmap)",
4091 { /* end of list */ }
4095 QemuOptsList qemu_drive_opts
= {
4097 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4100 * no elements => accept any params
4101 * validation will happen later
4103 { /* end of list */ }