2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
9 * This file incorporates work covered by the following copyright and
12 * Copyright (c) 2003-2008 Fabrice Bellard
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "qemu/osdep.h"
34 #include "sysemu/block-backend.h"
35 #include "sysemu/blockdev.h"
36 #include "hw/block/block.h"
37 #include "block/blockjob.h"
38 #include "block/throttle-groups.h"
39 #include "monitor/monitor.h"
40 #include "qemu/error-report.h"
41 #include "qemu/option.h"
42 #include "qemu/config-file.h"
43 #include "qapi/qmp/types.h"
44 #include "qapi-visit.h"
45 #include "qapi/qmp/qerror.h"
46 #include "qapi/qmp-output-visitor.h"
47 #include "qapi/util.h"
48 #include "sysemu/sysemu.h"
49 #include "block/block_int.h"
50 #include "qmp-commands.h"
52 #include "sysemu/arch_init.h"
54 static QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
55 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
57 static const char *const if_name
[IF_COUNT
] = {
61 [IF_FLOPPY
] = "floppy",
62 [IF_PFLASH
] = "pflash",
65 [IF_VIRTIO
] = "virtio",
69 static int if_max_devs
[IF_COUNT
] = {
71 * Do not change these numbers! They govern how drive option
72 * index maps to unit and bus. That mapping is ABI.
74 * All controllers used to imlement if=T drives need to support
75 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
76 * Otherwise, some index values map to "impossible" bus, unit
79 * For instance, if you change [IF_SCSI] to 255, -drive
80 * if=scsi,index=12 no longer means bus=1,unit=5, but
81 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
82 * the drive can't be set up. Regression.
89 * Boards may call this to offer board-by-board overrides
90 * of the default, global values.
92 void override_max_devs(BlockInterfaceType type
, int max_devs
)
101 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
102 dinfo
= blk_legacy_dinfo(blk
);
103 if (dinfo
->type
== type
) {
104 fprintf(stderr
, "Cannot override units-per-bus property of"
105 " the %s interface, because a drive of that type has"
106 " already been added.\n", if_name
[type
]);
107 g_assert_not_reached();
111 if_max_devs
[type
] = max_devs
;
115 * We automatically delete the drive when a device using it gets
116 * unplugged. Questionable feature, but we can't just drop it.
117 * Device models call blockdev_mark_auto_del() to schedule the
118 * automatic deletion, and generic qdev code calls blockdev_auto_del()
119 * when deletion is actually safe.
121 void blockdev_mark_auto_del(BlockBackend
*blk
)
123 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
124 BlockDriverState
*bs
= blk_bs(blk
);
125 AioContext
*aio_context
;
132 aio_context
= bdrv_get_aio_context(bs
);
133 aio_context_acquire(aio_context
);
136 block_job_cancel(bs
->job
);
139 aio_context_release(aio_context
);
145 void blockdev_auto_del(BlockBackend
*blk
)
147 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
149 if (dinfo
&& dinfo
->auto_del
) {
155 * Returns the current mapping of how many units per bus
156 * a particular interface can support.
158 * A positive integer indicates n units per bus.
159 * 0 implies the mapping has not been established.
160 * -1 indicates an invalid BlockInterfaceType was given.
162 int drive_get_max_devs(BlockInterfaceType type
)
164 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
165 return if_max_devs
[type
];
171 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
173 int max_devs
= if_max_devs
[type
];
174 return max_devs
? index
/ max_devs
: 0;
177 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
179 int max_devs
= if_max_devs
[type
];
180 return max_devs
? index
% max_devs
: index
;
183 QemuOpts
*drive_def(const char *optstr
)
185 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
188 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
193 opts
= drive_def(optstr
);
197 if (type
!= IF_DEFAULT
) {
198 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
201 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
204 qemu_opt_set(opts
, "file", file
, &error_abort
);
208 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
213 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
214 dinfo
= blk_legacy_dinfo(blk
);
215 if (dinfo
&& dinfo
->type
== type
216 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
224 bool drive_check_orphaned(void)
230 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
231 dinfo
= blk_legacy_dinfo(blk
);
232 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
233 /* Unless this is a default drive, this may be an oversight. */
234 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
235 dinfo
->type
!= IF_NONE
) {
236 fprintf(stderr
, "Warning: Orphaned drive without device: "
237 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
238 blk_name(blk
), blk_bs(blk
) ? blk_bs(blk
)->filename
: "",
239 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
247 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
249 return drive_get(type
,
250 drive_index_to_bus_id(type
, index
),
251 drive_index_to_unit_id(type
, index
));
254 int drive_get_max_bus(BlockInterfaceType type
)
261 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
262 dinfo
= blk_legacy_dinfo(blk
);
263 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
264 max_bus
= dinfo
->bus
;
270 /* Get a block device. This should only be used for single-drive devices
271 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
273 DriveInfo
*drive_get_next(BlockInterfaceType type
)
275 static int next_block_unit
[IF_COUNT
];
277 return drive_get(type
, 0, next_block_unit
[type
]++);
280 static void bdrv_format_print(void *opaque
, const char *name
)
282 error_printf(" %s", name
);
287 BlockDriverState
*bs
;
290 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
292 if (!strcmp(buf
, "ignore")) {
293 return BLOCKDEV_ON_ERROR_IGNORE
;
294 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
295 return BLOCKDEV_ON_ERROR_ENOSPC
;
296 } else if (!strcmp(buf
, "stop")) {
297 return BLOCKDEV_ON_ERROR_STOP
;
298 } else if (!strcmp(buf
, "report")) {
299 return BLOCKDEV_ON_ERROR_REPORT
;
301 error_setg(errp
, "'%s' invalid %s error action",
302 buf
, is_read
? "read" : "write");
307 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
310 const QListEntry
*entry
;
311 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
312 switch (qobject_type(entry
->value
)) {
314 case QTYPE_QSTRING
: {
315 unsigned long long length
;
316 const char *str
= qstring_get_str(qobject_to_qstring(entry
->value
));
317 if (parse_uint_full(str
, &length
, 10) == 0 &&
318 length
> 0 && length
<= UINT_MAX
) {
319 block_acct_add_interval(stats
, (unsigned) length
);
321 error_setg(errp
, "Invalid interval length: %s", str
);
328 int64_t length
= qint_get_int(qobject_to_qint(entry
->value
));
329 if (length
> 0 && length
<= UINT_MAX
) {
330 block_acct_add_interval(stats
, (unsigned) length
);
332 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
339 error_setg(errp
, "The specification of stats-intervals is invalid");
346 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
348 if (throttle_conflicting(cfg
, errp
)) {
352 if (!throttle_is_valid(cfg
)) {
353 error_setg(errp
, "bps/iops/max values must be within [0, %lld]",
358 if (throttle_max_is_missing_limit(cfg
)) {
359 error_setg(errp
, "bps_max/iops_max require corresponding"
367 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
369 /* All parameters but @opts are optional and may be set to NULL. */
370 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
371 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
372 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
375 Error
*local_error
= NULL
;
379 if (!qemu_opt_get_bool(opts
, "read-only", false)) {
380 *bdrv_flags
|= BDRV_O_RDWR
;
382 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
383 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
386 if ((discard
= qemu_opt_get(opts
, "discard")) != NULL
) {
387 if (bdrv_parse_discard_flags(discard
, bdrv_flags
) != 0) {
388 error_setg(errp
, "Invalid discard option");
393 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
394 if (!strcmp(aio
, "native")) {
395 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
396 } else if (!strcmp(aio
, "threads")) {
397 /* this is the default */
399 error_setg(errp
, "invalid aio option");
405 /* disk I/O throttling */
406 if (throttling_group
) {
407 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
411 memset(throttle_cfg
, 0, sizeof(*throttle_cfg
));
412 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
413 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
414 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
415 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
416 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
417 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
418 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
419 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
420 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
421 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
422 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
423 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
425 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
426 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
427 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
428 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
429 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
430 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
431 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
432 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
433 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
434 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
435 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
436 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
438 throttle_cfg
->op_size
=
439 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
441 if (!check_throttle_config(throttle_cfg
, errp
)) {
448 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
449 qemu_opt_get(opts
, "detect-zeroes"),
450 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX
,
451 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
454 error_propagate(errp
, local_error
);
459 *detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
460 !(*bdrv_flags
& BDRV_O_UNMAP
))
462 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
463 "without setting discard operation to unmap");
469 /* Takes the ownership of bs_opts */
470 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
475 int on_read_error
, on_write_error
;
476 bool account_invalid
, account_failed
;
478 BlockDriverState
*bs
;
483 QDict
*interval_dict
= NULL
;
484 QList
*interval_list
= NULL
;
486 BlockdevDetectZeroesOptions detect_zeroes
=
487 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
488 const char *throttling_group
= NULL
;
490 /* Check common options by copying from bs_opts to opts, all other options
491 * stay in bs_opts for processing by bdrv_open(). */
492 id
= qdict_get_try_str(bs_opts
, "id");
493 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
495 error_propagate(errp
, error
);
499 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
501 error_propagate(errp
, error
);
506 qdict_del(bs_opts
, "id");
509 /* extract parameters */
510 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
512 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
513 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
515 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
516 qdict_array_split(interval_dict
, &interval_list
);
518 if (qdict_size(interval_dict
) != 0) {
519 error_setg(errp
, "Invalid option stats-intervals.%s",
520 qdict_first(interval_dict
)->key
);
524 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
525 &detect_zeroes
, &error
);
527 error_propagate(errp
, error
);
531 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
532 if (is_help_option(buf
)) {
533 error_printf("Supported formats:");
534 bdrv_iterate_format(bdrv_format_print
, NULL
);
539 if (qdict_haskey(bs_opts
, "driver")) {
540 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
543 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
546 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
547 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
548 on_write_error
= parse_block_error_action(buf
, 0, &error
);
550 error_propagate(errp
, error
);
555 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
556 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
557 on_read_error
= parse_block_error_action(buf
, 1, &error
);
559 error_propagate(errp
, error
);
565 bdrv_flags
|= BDRV_O_SNAPSHOT
;
569 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
570 BlockBackendRootState
*blk_rs
;
572 blk
= blk_new(qemu_opts_id(opts
), errp
);
577 blk_rs
= blk_get_root_state(blk
);
578 blk_rs
->open_flags
= bdrv_flags
;
579 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
580 blk_rs
->detect_zeroes
= detect_zeroes
;
582 if (throttle_enabled(&cfg
)) {
583 if (!throttling_group
) {
584 throttling_group
= blk_name(blk
);
586 blk_rs
->throttle_group
= g_strdup(throttling_group
);
587 blk_rs
->throttle_state
= throttle_group_incref(throttling_group
);
588 blk_rs
->throttle_state
->cfg
= cfg
;
593 if (file
&& !*file
) {
597 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
598 * with other callers) rather than what we want as the real defaults.
599 * Apply the defaults here instead. */
600 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_WB
, "on");
601 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
602 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
605 /* always use cache=unsafe with snapshot */
606 qdict_put(bs_opts
, BDRV_OPT_CACHE_WB
, qstring_from_str("on"));
607 qdict_put(bs_opts
, BDRV_OPT_CACHE_DIRECT
, qstring_from_str("off"));
608 qdict_put(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, qstring_from_str("on"));
611 if (runstate_check(RUN_STATE_INMIGRATE
)) {
612 bdrv_flags
|= BDRV_O_INACTIVE
;
615 blk
= blk_new_open(qemu_opts_id(opts
), file
, NULL
, bs_opts
, bdrv_flags
,
622 bs
->detect_zeroes
= detect_zeroes
;
624 /* disk I/O throttling */
625 if (throttle_enabled(&cfg
)) {
626 if (!throttling_group
) {
627 throttling_group
= blk_name(blk
);
629 bdrv_io_limits_enable(bs
, throttling_group
);
630 bdrv_set_io_limits(bs
, &cfg
);
633 if (bdrv_key_required(bs
)) {
637 block_acct_init(blk_get_stats(blk
), account_invalid
, account_failed
);
639 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
646 blk_set_on_error(blk
, on_read_error
, on_write_error
);
650 QDECREF(interval_dict
);
651 QDECREF(interval_list
);
656 QDECREF(interval_dict
);
657 QDECREF(interval_list
);
663 static QemuOptsList qemu_root_bds_opts
;
665 /* Takes the ownership of bs_opts */
666 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
668 BlockDriverState
*bs
;
670 Error
*local_error
= NULL
;
671 BlockdevDetectZeroesOptions detect_zeroes
;
675 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
680 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
682 error_propagate(errp
, local_error
);
686 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
687 &detect_zeroes
, &local_error
);
689 error_propagate(errp
, local_error
);
693 if (runstate_check(RUN_STATE_INMIGRATE
)) {
694 bdrv_flags
|= BDRV_O_INACTIVE
;
698 ret
= bdrv_open(&bs
, NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
700 goto fail_no_bs_opts
;
703 bs
->detect_zeroes
= detect_zeroes
;
715 void blockdev_close_all_bdrv_states(void)
717 BlockDriverState
*bs
, *next_bs
;
719 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
720 AioContext
*ctx
= bdrv_get_aio_context(bs
);
722 aio_context_acquire(ctx
);
724 aio_context_release(ctx
);
728 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
733 value
= qemu_opt_get(opts
, from
);
735 if (qemu_opt_find(opts
, to
)) {
736 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
737 "same time", to
, from
);
742 /* rename all items in opts */
743 while ((value
= qemu_opt_get(opts
, from
))) {
744 qemu_opt_set(opts
, to
, value
, &error_abort
);
745 qemu_opt_unset(opts
, from
);
749 QemuOptsList qemu_legacy_drive_opts
= {
751 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
755 .type
= QEMU_OPT_NUMBER
,
756 .help
= "bus number",
759 .type
= QEMU_OPT_NUMBER
,
760 .help
= "unit number (i.e. lun for scsi)",
763 .type
= QEMU_OPT_NUMBER
,
764 .help
= "index number",
767 .type
= QEMU_OPT_STRING
,
768 .help
= "media type (disk, cdrom)",
771 .type
= QEMU_OPT_STRING
,
772 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
775 .type
= QEMU_OPT_NUMBER
,
776 .help
= "number of cylinders (ide disk geometry)",
779 .type
= QEMU_OPT_NUMBER
,
780 .help
= "number of heads (ide disk geometry)",
783 .type
= QEMU_OPT_NUMBER
,
784 .help
= "number of sectors (ide disk geometry)",
787 .type
= QEMU_OPT_STRING
,
788 .help
= "chs translation (auto, lba, none)",
791 .type
= QEMU_OPT_BOOL
,
792 .help
= "(deprecated, ignored)",
795 .type
= QEMU_OPT_STRING
,
796 .help
= "pci address (virtio only)",
799 .type
= QEMU_OPT_STRING
,
800 .help
= "disk serial number",
803 .type
= QEMU_OPT_STRING
,
807 /* Options that are passed on, but have special semantics with -drive */
810 .type
= QEMU_OPT_BOOL
,
811 .help
= "open drive file as read-only",
814 .type
= QEMU_OPT_STRING
,
815 .help
= "read error action",
818 .type
= QEMU_OPT_STRING
,
819 .help
= "write error action",
821 .name
= "copy-on-read",
822 .type
= QEMU_OPT_BOOL
,
823 .help
= "copy read data from backing file into image file",
826 { /* end of list */ }
830 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
834 DriveInfo
*dinfo
= NULL
;
836 QemuOpts
*legacy_opts
;
837 DriveMediaType media
= MEDIA_DISK
;
838 BlockInterfaceType type
;
839 int cyls
, heads
, secs
, translation
;
840 int max_devs
, bus_id
, unit_id
, index
;
842 const char *werror
, *rerror
;
843 bool read_only
= false;
846 const char *filename
;
847 Error
*local_err
= NULL
;
850 /* Change legacy command line options into QMP ones */
851 static const struct {
855 { "iops", "throttling.iops-total" },
856 { "iops_rd", "throttling.iops-read" },
857 { "iops_wr", "throttling.iops-write" },
859 { "bps", "throttling.bps-total" },
860 { "bps_rd", "throttling.bps-read" },
861 { "bps_wr", "throttling.bps-write" },
863 { "iops_max", "throttling.iops-total-max" },
864 { "iops_rd_max", "throttling.iops-read-max" },
865 { "iops_wr_max", "throttling.iops-write-max" },
867 { "bps_max", "throttling.bps-total-max" },
868 { "bps_rd_max", "throttling.bps-read-max" },
869 { "bps_wr_max", "throttling.bps-write-max" },
871 { "iops_size", "throttling.iops-size" },
873 { "group", "throttling.group" },
875 { "readonly", "read-only" },
878 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
879 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
882 error_report_err(local_err
);
887 value
= qemu_opt_get(all_opts
, "cache");
891 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
892 error_report("invalid cache option");
896 /* Specific options take precedence */
897 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
898 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
899 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
901 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
902 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
903 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
905 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
906 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
907 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
909 qemu_opt_unset(all_opts
, "cache");
912 /* Get a QDict for processing the options */
913 bs_opts
= qdict_new();
914 qemu_opts_to_qdict(all_opts
, bs_opts
);
916 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
918 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
920 error_report_err(local_err
);
924 /* Deprecated option boot=[on|off] */
925 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
926 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
927 "ignored. Future versions will reject this parameter. Please "
928 "update your scripts.\n");
932 value
= qemu_opt_get(legacy_opts
, "media");
934 if (!strcmp(value
, "disk")) {
936 } else if (!strcmp(value
, "cdrom")) {
940 error_report("'%s' invalid media", value
);
945 /* copy-on-read is disabled with a warning for read-only devices */
946 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
947 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
949 if (read_only
&& copy_on_read
) {
950 error_report("warning: disabling copy-on-read on read-only drive");
951 copy_on_read
= false;
954 qdict_put(bs_opts
, "read-only",
955 qstring_from_str(read_only
? "on" : "off"));
956 qdict_put(bs_opts
, "copy-on-read",
957 qstring_from_str(copy_on_read
? "on" :"off"));
959 /* Controller type */
960 value
= qemu_opt_get(legacy_opts
, "if");
963 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
966 if (type
== IF_COUNT
) {
967 error_report("unsupported bus type '%s'", value
);
971 type
= block_default_type
;
975 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
976 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
977 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
979 if (cyls
|| heads
|| secs
) {
981 error_report("invalid physical cyls number");
985 error_report("invalid physical heads number");
989 error_report("invalid physical secs number");
994 translation
= BIOS_ATA_TRANSLATION_AUTO
;
995 value
= qemu_opt_get(legacy_opts
, "trans");
998 error_report("'%s' trans must be used with cyls, heads and secs",
1002 if (!strcmp(value
, "none")) {
1003 translation
= BIOS_ATA_TRANSLATION_NONE
;
1004 } else if (!strcmp(value
, "lba")) {
1005 translation
= BIOS_ATA_TRANSLATION_LBA
;
1006 } else if (!strcmp(value
, "large")) {
1007 translation
= BIOS_ATA_TRANSLATION_LARGE
;
1008 } else if (!strcmp(value
, "rechs")) {
1009 translation
= BIOS_ATA_TRANSLATION_RECHS
;
1010 } else if (!strcmp(value
, "auto")) {
1011 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1013 error_report("'%s' invalid translation type", value
);
1018 if (media
== MEDIA_CDROM
) {
1019 if (cyls
|| secs
|| heads
) {
1020 error_report("CHS can't be set with media=cdrom");
1025 /* Device address specified by bus/unit or index.
1026 * If none was specified, try to find the first free one. */
1027 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
1028 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
1029 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
1031 max_devs
= if_max_devs
[type
];
1034 if (bus_id
!= 0 || unit_id
!= -1) {
1035 error_report("index cannot be used with bus and unit");
1038 bus_id
= drive_index_to_bus_id(type
, index
);
1039 unit_id
= drive_index_to_unit_id(type
, index
);
1042 if (unit_id
== -1) {
1044 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1046 if (max_devs
&& unit_id
>= max_devs
) {
1047 unit_id
-= max_devs
;
1053 if (max_devs
&& unit_id
>= max_devs
) {
1054 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1058 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1059 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1060 bus_id
, unit_id
, index
);
1065 serial
= qemu_opt_get(legacy_opts
, "serial");
1067 /* no id supplied -> create one */
1068 if (qemu_opts_id(all_opts
) == NULL
) {
1070 const char *mediastr
= "";
1071 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1072 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1075 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1078 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1081 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1085 /* Add virtio block device */
1086 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1087 if (devaddr
&& type
!= IF_VIRTIO
) {
1088 error_report("addr is not supported by this bus type");
1092 if (type
== IF_VIRTIO
) {
1094 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1096 if (arch_type
== QEMU_ARCH_S390X
) {
1097 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1099 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1101 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1104 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1108 filename
= qemu_opt_get(legacy_opts
, "file");
1110 /* Check werror/rerror compatibility with if=... */
1111 werror
= qemu_opt_get(legacy_opts
, "werror");
1112 if (werror
!= NULL
) {
1113 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1115 error_report("werror is not supported by this bus type");
1118 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1121 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1122 if (rerror
!= NULL
) {
1123 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1125 error_report("rerror is not supported by this bus type");
1128 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1131 /* Actual block device init: Functionality shared with blockdev-add */
1132 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1136 error_report_err(local_err
);
1143 /* Create legacy DriveInfo */
1144 dinfo
= g_malloc0(sizeof(*dinfo
));
1145 dinfo
->opts
= all_opts
;
1148 dinfo
->heads
= heads
;
1150 dinfo
->trans
= translation
;
1153 dinfo
->bus
= bus_id
;
1154 dinfo
->unit
= unit_id
;
1155 dinfo
->devaddr
= devaddr
;
1156 dinfo
->serial
= g_strdup(serial
);
1158 blk_set_legacy_dinfo(blk
, dinfo
);
1165 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1172 qemu_opts_del(legacy_opts
);
1177 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1179 const char *device
= qdict_get_str(qdict
, "device");
1183 if (!strcmp(device
, "all")) {
1184 ret
= bdrv_commit_all();
1186 BlockDriverState
*bs
;
1187 AioContext
*aio_context
;
1189 blk
= blk_by_name(device
);
1191 monitor_printf(mon
, "Device '%s' not found\n", device
);
1194 if (!blk_is_available(blk
)) {
1195 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1200 aio_context
= bdrv_get_aio_context(bs
);
1201 aio_context_acquire(aio_context
);
1203 ret
= bdrv_commit(bs
);
1205 aio_context_release(aio_context
);
1208 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1213 static void blockdev_do_action(TransactionActionKind type
, void *data
,
1216 TransactionAction action
;
1217 TransactionActionList list
;
1220 action
.u
.data
= data
;
1221 list
.value
= &action
;
1223 qmp_transaction(&list
, false, NULL
, errp
);
1226 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1227 bool has_node_name
, const char *node_name
,
1228 const char *snapshot_file
,
1229 bool has_snapshot_node_name
,
1230 const char *snapshot_node_name
,
1231 bool has_format
, const char *format
,
1232 bool has_mode
, NewImageMode mode
, Error
**errp
)
1234 BlockdevSnapshotSync snapshot
= {
1235 .has_device
= has_device
,
1236 .device
= (char *) device
,
1237 .has_node_name
= has_node_name
,
1238 .node_name
= (char *) node_name
,
1239 .snapshot_file
= (char *) snapshot_file
,
1240 .has_snapshot_node_name
= has_snapshot_node_name
,
1241 .snapshot_node_name
= (char *) snapshot_node_name
,
1242 .has_format
= has_format
,
1243 .format
= (char *) format
,
1244 .has_mode
= has_mode
,
1247 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1251 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1254 BlockdevSnapshot snapshot_data
= {
1255 .node
= (char *) node
,
1256 .overlay
= (char *) overlay
1259 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1260 &snapshot_data
, errp
);
1263 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1267 BlockdevSnapshotInternal snapshot
= {
1268 .device
= (char *) device
,
1269 .name
= (char *) name
1272 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1276 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1283 BlockDriverState
*bs
;
1285 AioContext
*aio_context
;
1286 QEMUSnapshotInfo sn
;
1287 Error
*local_err
= NULL
;
1288 SnapshotInfo
*info
= NULL
;
1291 blk
= blk_by_name(device
);
1293 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1294 "Device '%s' not found", device
);
1298 aio_context
= blk_get_aio_context(blk
);
1299 aio_context_acquire(aio_context
);
1310 error_setg(errp
, "Name or id must be provided");
1311 goto out_aio_context
;
1314 if (!blk_is_available(blk
)) {
1315 error_setg(errp
, "Device '%s' has no medium", device
);
1316 goto out_aio_context
;
1320 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1321 goto out_aio_context
;
1324 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1326 error_propagate(errp
, local_err
);
1327 goto out_aio_context
;
1331 "Snapshot with id '%s' and name '%s' does not exist on "
1333 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1334 goto out_aio_context
;
1337 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1339 error_propagate(errp
, local_err
);
1340 goto out_aio_context
;
1343 aio_context_release(aio_context
);
1345 info
= g_new0(SnapshotInfo
, 1);
1346 info
->id
= g_strdup(sn
.id_str
);
1347 info
->name
= g_strdup(sn
.name
);
1348 info
->date_nsec
= sn
.date_nsec
;
1349 info
->date_sec
= sn
.date_sec
;
1350 info
->vm_state_size
= sn
.vm_state_size
;
1351 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1352 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1357 aio_context_release(aio_context
);
1362 * block_dirty_bitmap_lookup:
1363 * Return a dirty bitmap (if present), after validating
1364 * the node reference and bitmap names.
1366 * @node: The name of the BDS node to search for bitmaps
1367 * @name: The name of the bitmap to search for
1368 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1369 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1370 * @errp: Output pointer for error information. Can be NULL.
1372 * @return: A bitmap object on success, or NULL on failure.
1374 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1376 BlockDriverState
**pbs
,
1380 BlockDriverState
*bs
;
1381 BdrvDirtyBitmap
*bitmap
;
1382 AioContext
*aio_context
;
1385 error_setg(errp
, "Node cannot be NULL");
1389 error_setg(errp
, "Bitmap name cannot be NULL");
1392 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1394 error_setg(errp
, "Node '%s' not found", node
);
1398 aio_context
= bdrv_get_aio_context(bs
);
1399 aio_context_acquire(aio_context
);
1401 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1403 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1411 *paio
= aio_context
;
1413 aio_context_release(aio_context
);
1419 aio_context_release(aio_context
);
1423 /* New and old BlockDriverState structs for atomic group operations */
1425 typedef struct BlkActionState BlkActionState
;
1429 * Table of operations that define an Action.
1431 * @instance_size: Size of state struct, in bytes.
1432 * @prepare: Prepare the work, must NOT be NULL.
1433 * @commit: Commit the changes, can be NULL.
1434 * @abort: Abort the changes on fail, can be NULL.
1435 * @clean: Clean up resources after all transaction actions have called
1436 * commit() or abort(). Can be NULL.
1438 * Only prepare() may fail. In a single transaction, only one of commit() or
1439 * abort() will be called. clean() will always be called if it is present.
1441 typedef struct BlkActionOps
{
1442 size_t instance_size
;
1443 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1444 void (*commit
)(BlkActionState
*common
);
1445 void (*abort
)(BlkActionState
*common
);
1446 void (*clean
)(BlkActionState
*common
);
1451 * Describes one Action's state within a Transaction.
1453 * @action: QAPI-defined enum identifying which Action to perform.
1454 * @ops: Table of ActionOps this Action can perform.
1455 * @block_job_txn: Transaction which this action belongs to.
1456 * @entry: List membership for all Actions in this Transaction.
1458 * This structure must be arranged as first member in a subclassed type,
1459 * assuming that the compiler will also arrange it to the same offsets as the
1462 struct BlkActionState
{
1463 TransactionAction
*action
;
1464 const BlkActionOps
*ops
;
1465 BlockJobTxn
*block_job_txn
;
1466 TransactionProperties
*txn_props
;
1467 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1470 /* internal snapshot private data */
1471 typedef struct InternalSnapshotState
{
1472 BlkActionState common
;
1473 BlockDriverState
*bs
;
1474 AioContext
*aio_context
;
1475 QEMUSnapshotInfo sn
;
1477 } InternalSnapshotState
;
1480 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1482 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1484 "Action '%s' does not support Transaction property "
1485 "completion-mode = %s",
1486 TransactionActionKind_lookup
[s
->action
->type
],
1487 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1493 static void internal_snapshot_prepare(BlkActionState
*common
,
1496 Error
*local_err
= NULL
;
1500 BlockDriverState
*bs
;
1501 QEMUSnapshotInfo old_sn
, *sn
;
1504 BlockdevSnapshotInternal
*internal
;
1505 InternalSnapshotState
*state
;
1508 g_assert(common
->action
->type
==
1509 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1510 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
;
1511 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1513 /* 1. parse input */
1514 device
= internal
->device
;
1515 name
= internal
->name
;
1517 /* 2. check for validation */
1518 if (action_check_completion_mode(common
, errp
) < 0) {
1522 blk
= blk_by_name(device
);
1524 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1525 "Device '%s' not found", device
);
1529 /* AioContext is released in .clean() */
1530 state
->aio_context
= blk_get_aio_context(blk
);
1531 aio_context_acquire(state
->aio_context
);
1533 if (!blk_is_available(blk
)) {
1534 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1540 bdrv_drained_begin(bs
);
1542 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1546 if (bdrv_is_read_only(bs
)) {
1547 error_setg(errp
, "Device '%s' is read only", device
);
1551 if (!bdrv_can_snapshot(bs
)) {
1552 error_setg(errp
, "Block format '%s' used by device '%s' "
1553 "does not support internal snapshots",
1554 bs
->drv
->format_name
, device
);
1558 if (!strlen(name
)) {
1559 error_setg(errp
, "Name is empty");
1563 /* check whether a snapshot with name exist */
1564 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1567 error_propagate(errp
, local_err
);
1571 "Snapshot with name '%s' already exists on device '%s'",
1576 /* 3. take the snapshot */
1578 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1579 qemu_gettimeofday(&tv
);
1580 sn
->date_sec
= tv
.tv_sec
;
1581 sn
->date_nsec
= tv
.tv_usec
* 1000;
1582 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1584 ret1
= bdrv_snapshot_create(bs
, sn
);
1586 error_setg_errno(errp
, -ret1
,
1587 "Failed to create snapshot '%s' on device '%s'",
1592 /* 4. succeed, mark a snapshot is created */
1593 state
->created
= true;
1596 static void internal_snapshot_abort(BlkActionState
*common
)
1598 InternalSnapshotState
*state
=
1599 DO_UPCAST(InternalSnapshotState
, common
, common
);
1600 BlockDriverState
*bs
= state
->bs
;
1601 QEMUSnapshotInfo
*sn
= &state
->sn
;
1602 Error
*local_error
= NULL
;
1604 if (!state
->created
) {
1608 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1609 error_reportf_err(local_error
,
1610 "Failed to delete snapshot with id '%s' and "
1611 "name '%s' on device '%s' in abort: ",
1612 sn
->id_str
, sn
->name
,
1613 bdrv_get_device_name(bs
));
1617 static void internal_snapshot_clean(BlkActionState
*common
)
1619 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1622 if (state
->aio_context
) {
1624 bdrv_drained_end(state
->bs
);
1626 aio_context_release(state
->aio_context
);
1630 /* external snapshot private data */
1631 typedef struct ExternalSnapshotState
{
1632 BlkActionState common
;
1633 BlockDriverState
*old_bs
;
1634 BlockDriverState
*new_bs
;
1635 AioContext
*aio_context
;
1636 } ExternalSnapshotState
;
1638 static void external_snapshot_prepare(BlkActionState
*common
,
1642 QDict
*options
= NULL
;
1643 Error
*local_err
= NULL
;
1644 /* Device and node name of the image to generate the snapshot from */
1646 const char *node_name
;
1647 /* Reference to the new image (for 'blockdev-snapshot') */
1648 const char *snapshot_ref
;
1649 /* File name of the new image (for 'blockdev-snapshot-sync') */
1650 const char *new_image_file
;
1651 ExternalSnapshotState
*state
=
1652 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1653 TransactionAction
*action
= common
->action
;
1655 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1656 * purpose but a different set of parameters */
1657 switch (action
->type
) {
1658 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1660 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
;
1662 node_name
= s
->node
;
1663 new_image_file
= NULL
;
1664 snapshot_ref
= s
->overlay
;
1667 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1669 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1670 device
= s
->has_device
? s
->device
: NULL
;
1671 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1672 new_image_file
= s
->snapshot_file
;
1673 snapshot_ref
= NULL
;
1677 g_assert_not_reached();
1680 /* start processing */
1681 if (action_check_completion_mode(common
, errp
) < 0) {
1685 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1686 if (!state
->old_bs
) {
1690 /* Acquire AioContext now so any threads operating on old_bs stop */
1691 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1692 aio_context_acquire(state
->aio_context
);
1693 bdrv_drained_begin(state
->old_bs
);
1695 if (!bdrv_is_inserted(state
->old_bs
)) {
1696 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1700 if (bdrv_op_is_blocked(state
->old_bs
,
1701 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1705 if (!bdrv_is_read_only(state
->old_bs
)) {
1706 if (bdrv_flush(state
->old_bs
)) {
1707 error_setg(errp
, QERR_IO_ERROR
);
1712 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1713 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1717 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1718 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1719 const char *format
= s
->has_format
? s
->format
: "qcow2";
1720 enum NewImageMode mode
;
1721 const char *snapshot_node_name
=
1722 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1724 if (node_name
&& !snapshot_node_name
) {
1725 error_setg(errp
, "New snapshot node name missing");
1729 if (snapshot_node_name
&&
1730 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1731 error_setg(errp
, "New snapshot node name already in use");
1735 flags
= state
->old_bs
->open_flags
;
1737 /* create new image w/backing file */
1738 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1739 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1740 bdrv_img_create(new_image_file
, format
,
1741 state
->old_bs
->filename
,
1742 state
->old_bs
->drv
->format_name
,
1743 NULL
, -1, flags
, &local_err
, false);
1745 error_propagate(errp
, local_err
);
1750 options
= qdict_new();
1751 if (s
->has_snapshot_node_name
) {
1752 qdict_put(options
, "node-name",
1753 qstring_from_str(snapshot_node_name
));
1755 qdict_put(options
, "driver", qstring_from_str(format
));
1757 flags
|= BDRV_O_NO_BACKING
;
1760 assert(state
->new_bs
== NULL
);
1761 ret
= bdrv_open(&state
->new_bs
, new_image_file
, snapshot_ref
, options
,
1763 /* We will manually add the backing_hd field to the bs later */
1768 if (state
->new_bs
->blk
!= NULL
) {
1769 error_setg(errp
, "The snapshot is already in use by %s",
1770 blk_name(state
->new_bs
->blk
));
1774 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1779 if (state
->new_bs
->backing
!= NULL
) {
1780 error_setg(errp
, "The snapshot already has a backing image");
1784 if (!state
->new_bs
->drv
->supports_backing
) {
1785 error_setg(errp
, "The snapshot does not support backing images");
1789 static void external_snapshot_commit(BlkActionState
*common
)
1791 ExternalSnapshotState
*state
=
1792 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1794 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1796 /* This removes our old bs and adds the new bs */
1797 bdrv_append(state
->new_bs
, state
->old_bs
);
1798 /* We don't need (or want) to use the transactional
1799 * bdrv_reopen_multiple() across all the entries at once, because we
1800 * don't want to abort all of them if one of them fails the reopen */
1801 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1805 static void external_snapshot_abort(BlkActionState
*common
)
1807 ExternalSnapshotState
*state
=
1808 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1809 if (state
->new_bs
) {
1810 bdrv_unref(state
->new_bs
);
1814 static void external_snapshot_clean(BlkActionState
*common
)
1816 ExternalSnapshotState
*state
=
1817 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1818 if (state
->aio_context
) {
1819 bdrv_drained_end(state
->old_bs
);
1820 aio_context_release(state
->aio_context
);
1824 typedef struct DriveBackupState
{
1825 BlkActionState common
;
1826 BlockDriverState
*bs
;
1827 AioContext
*aio_context
;
1831 static void do_drive_backup(const char *device
, const char *target
,
1832 bool has_format
, const char *format
,
1833 enum MirrorSyncMode sync
,
1834 bool has_mode
, enum NewImageMode mode
,
1835 bool has_speed
, int64_t speed
,
1836 bool has_bitmap
, const char *bitmap
,
1837 bool has_on_source_error
,
1838 BlockdevOnError on_source_error
,
1839 bool has_on_target_error
,
1840 BlockdevOnError on_target_error
,
1841 BlockJobTxn
*txn
, Error
**errp
);
1843 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1845 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1847 DriveBackup
*backup
;
1848 Error
*local_err
= NULL
;
1850 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1851 backup
= common
->action
->u
.drive_backup
;
1853 blk
= blk_by_name(backup
->device
);
1855 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1856 "Device '%s' not found", backup
->device
);
1860 if (!blk_is_available(blk
)) {
1861 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1865 /* AioContext is released in .clean() */
1866 state
->aio_context
= blk_get_aio_context(blk
);
1867 aio_context_acquire(state
->aio_context
);
1868 bdrv_drained_begin(blk_bs(blk
));
1869 state
->bs
= blk_bs(blk
);
1871 do_drive_backup(backup
->device
, backup
->target
,
1872 backup
->has_format
, backup
->format
,
1874 backup
->has_mode
, backup
->mode
,
1875 backup
->has_speed
, backup
->speed
,
1876 backup
->has_bitmap
, backup
->bitmap
,
1877 backup
->has_on_source_error
, backup
->on_source_error
,
1878 backup
->has_on_target_error
, backup
->on_target_error
,
1879 common
->block_job_txn
, &local_err
);
1881 error_propagate(errp
, local_err
);
1885 state
->job
= state
->bs
->job
;
1888 static void drive_backup_abort(BlkActionState
*common
)
1890 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1891 BlockDriverState
*bs
= state
->bs
;
1893 /* Only cancel if it's the job we started */
1894 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1895 block_job_cancel_sync(bs
->job
);
1899 static void drive_backup_clean(BlkActionState
*common
)
1901 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1903 if (state
->aio_context
) {
1904 bdrv_drained_end(state
->bs
);
1905 aio_context_release(state
->aio_context
);
1909 typedef struct BlockdevBackupState
{
1910 BlkActionState common
;
1911 BlockDriverState
*bs
;
1913 AioContext
*aio_context
;
1914 } BlockdevBackupState
;
1916 static void do_blockdev_backup(const char *device
, const char *target
,
1917 enum MirrorSyncMode sync
,
1918 bool has_speed
, int64_t speed
,
1919 bool has_on_source_error
,
1920 BlockdevOnError on_source_error
,
1921 bool has_on_target_error
,
1922 BlockdevOnError on_target_error
,
1923 BlockJobTxn
*txn
, Error
**errp
);
1925 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1927 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1928 BlockdevBackup
*backup
;
1929 BlockBackend
*blk
, *target
;
1930 Error
*local_err
= NULL
;
1932 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1933 backup
= common
->action
->u
.blockdev_backup
;
1935 blk
= blk_by_name(backup
->device
);
1937 error_setg(errp
, "Device '%s' not found", backup
->device
);
1941 if (!blk_is_available(blk
)) {
1942 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1946 target
= blk_by_name(backup
->target
);
1948 error_setg(errp
, "Device '%s' not found", backup
->target
);
1952 /* AioContext is released in .clean() */
1953 state
->aio_context
= blk_get_aio_context(blk
);
1954 if (state
->aio_context
!= blk_get_aio_context(target
)) {
1955 state
->aio_context
= NULL
;
1956 error_setg(errp
, "Backup between two IO threads is not implemented");
1959 aio_context_acquire(state
->aio_context
);
1960 state
->bs
= blk_bs(blk
);
1961 bdrv_drained_begin(state
->bs
);
1963 do_blockdev_backup(backup
->device
, backup
->target
,
1965 backup
->has_speed
, backup
->speed
,
1966 backup
->has_on_source_error
, backup
->on_source_error
,
1967 backup
->has_on_target_error
, backup
->on_target_error
,
1968 common
->block_job_txn
, &local_err
);
1970 error_propagate(errp
, local_err
);
1974 state
->job
= state
->bs
->job
;
1977 static void blockdev_backup_abort(BlkActionState
*common
)
1979 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1980 BlockDriverState
*bs
= state
->bs
;
1982 /* Only cancel if it's the job we started */
1983 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1984 block_job_cancel_sync(bs
->job
);
1988 static void blockdev_backup_clean(BlkActionState
*common
)
1990 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1992 if (state
->aio_context
) {
1993 bdrv_drained_end(state
->bs
);
1994 aio_context_release(state
->aio_context
);
1998 typedef struct BlockDirtyBitmapState
{
1999 BlkActionState common
;
2000 BdrvDirtyBitmap
*bitmap
;
2001 BlockDriverState
*bs
;
2002 AioContext
*aio_context
;
2005 } BlockDirtyBitmapState
;
2007 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
2010 Error
*local_err
= NULL
;
2011 BlockDirtyBitmapAdd
*action
;
2012 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2015 if (action_check_completion_mode(common
, errp
) < 0) {
2019 action
= common
->action
->u
.block_dirty_bitmap_add
;
2020 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2021 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2022 action
->has_granularity
, action
->granularity
,
2026 state
->prepared
= true;
2028 error_propagate(errp
, local_err
);
2032 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2034 BlockDirtyBitmapAdd
*action
;
2035 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2038 action
= common
->action
->u
.block_dirty_bitmap_add
;
2039 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2040 * then the node reference and bitmap name must have been valid.
2042 if (state
->prepared
) {
2043 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2047 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2050 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2052 BlockDirtyBitmap
*action
;
2054 if (action_check_completion_mode(common
, errp
) < 0) {
2058 action
= common
->action
->u
.block_dirty_bitmap_clear
;
2059 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2062 &state
->aio_context
,
2064 if (!state
->bitmap
) {
2068 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2069 error_setg(errp
, "Cannot modify a frozen bitmap");
2071 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2072 error_setg(errp
, "Cannot clear a disabled bitmap");
2076 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2077 /* AioContext is released in .clean() */
2080 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2082 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2085 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2088 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2090 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2093 hbitmap_free(state
->backup
);
2096 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2098 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2101 if (state
->aio_context
) {
2102 aio_context_release(state
->aio_context
);
2106 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2108 error_setg(errp
, "Transaction aborted using Abort action");
2111 static void abort_commit(BlkActionState
*common
)
2113 g_assert_not_reached(); /* this action never succeeds */
2116 static const BlkActionOps actions
[] = {
2117 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2118 .instance_size
= sizeof(ExternalSnapshotState
),
2119 .prepare
= external_snapshot_prepare
,
2120 .commit
= external_snapshot_commit
,
2121 .abort
= external_snapshot_abort
,
2122 .clean
= external_snapshot_clean
,
2124 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2125 .instance_size
= sizeof(ExternalSnapshotState
),
2126 .prepare
= external_snapshot_prepare
,
2127 .commit
= external_snapshot_commit
,
2128 .abort
= external_snapshot_abort
,
2129 .clean
= external_snapshot_clean
,
2131 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2132 .instance_size
= sizeof(DriveBackupState
),
2133 .prepare
= drive_backup_prepare
,
2134 .abort
= drive_backup_abort
,
2135 .clean
= drive_backup_clean
,
2137 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2138 .instance_size
= sizeof(BlockdevBackupState
),
2139 .prepare
= blockdev_backup_prepare
,
2140 .abort
= blockdev_backup_abort
,
2141 .clean
= blockdev_backup_clean
,
2143 [TRANSACTION_ACTION_KIND_ABORT
] = {
2144 .instance_size
= sizeof(BlkActionState
),
2145 .prepare
= abort_prepare
,
2146 .commit
= abort_commit
,
2148 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2149 .instance_size
= sizeof(InternalSnapshotState
),
2150 .prepare
= internal_snapshot_prepare
,
2151 .abort
= internal_snapshot_abort
,
2152 .clean
= internal_snapshot_clean
,
2154 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2155 .instance_size
= sizeof(BlockDirtyBitmapState
),
2156 .prepare
= block_dirty_bitmap_add_prepare
,
2157 .abort
= block_dirty_bitmap_add_abort
,
2159 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2160 .instance_size
= sizeof(BlockDirtyBitmapState
),
2161 .prepare
= block_dirty_bitmap_clear_prepare
,
2162 .commit
= block_dirty_bitmap_clear_commit
,
2163 .abort
= block_dirty_bitmap_clear_abort
,
2164 .clean
= block_dirty_bitmap_clear_clean
,
2169 * Allocate a TransactionProperties structure if necessary, and fill
2170 * that structure with desired defaults if they are unset.
2172 static TransactionProperties
*get_transaction_properties(
2173 TransactionProperties
*props
)
2176 props
= g_new0(TransactionProperties
, 1);
2179 if (!props
->has_completion_mode
) {
2180 props
->has_completion_mode
= true;
2181 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2188 * 'Atomic' group operations. The operations are performed as a set, and if
2189 * any fail then we roll back all operations in the group.
2191 void qmp_transaction(TransactionActionList
*dev_list
,
2193 struct TransactionProperties
*props
,
2196 TransactionActionList
*dev_entry
= dev_list
;
2197 BlockJobTxn
*block_job_txn
= NULL
;
2198 BlkActionState
*state
, *next
;
2199 Error
*local_err
= NULL
;
2201 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2202 QSIMPLEQ_INIT(&snap_bdrv_states
);
2204 /* Does this transaction get canceled as a group on failure?
2205 * If not, we don't really need to make a BlockJobTxn.
2207 props
= get_transaction_properties(props
);
2208 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2209 block_job_txn
= block_job_txn_new();
2212 /* drain all i/o before any operations */
2215 /* We don't do anything in this loop that commits us to the operations */
2216 while (NULL
!= dev_entry
) {
2217 TransactionAction
*dev_info
= NULL
;
2218 const BlkActionOps
*ops
;
2220 dev_info
= dev_entry
->value
;
2221 dev_entry
= dev_entry
->next
;
2223 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2225 ops
= &actions
[dev_info
->type
];
2226 assert(ops
->instance_size
> 0);
2228 state
= g_malloc0(ops
->instance_size
);
2230 state
->action
= dev_info
;
2231 state
->block_job_txn
= block_job_txn
;
2232 state
->txn_props
= props
;
2233 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2235 state
->ops
->prepare(state
, &local_err
);
2237 error_propagate(errp
, local_err
);
2238 goto delete_and_fail
;
2242 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2243 if (state
->ops
->commit
) {
2244 state
->ops
->commit(state
);
2252 /* failure, and it is all-or-none; roll back all operations */
2253 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2254 if (state
->ops
->abort
) {
2255 state
->ops
->abort(state
);
2259 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2260 if (state
->ops
->clean
) {
2261 state
->ops
->clean(state
);
2266 qapi_free_TransactionProperties(props
);
2268 block_job_txn_unref(block_job_txn
);
2271 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
2273 Error
*local_err
= NULL
;
2275 qmp_blockdev_open_tray(device
, has_force
, force
, &local_err
);
2277 error_propagate(errp
, local_err
);
2281 qmp_x_blockdev_remove_medium(device
, errp
);
2284 void qmp_block_passwd(bool has_device
, const char *device
,
2285 bool has_node_name
, const char *node_name
,
2286 const char *password
, Error
**errp
)
2288 Error
*local_err
= NULL
;
2289 BlockDriverState
*bs
;
2290 AioContext
*aio_context
;
2292 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2293 has_node_name
? node_name
: NULL
,
2296 error_propagate(errp
, local_err
);
2300 aio_context
= bdrv_get_aio_context(bs
);
2301 aio_context_acquire(aio_context
);
2303 bdrv_add_key(bs
, password
, errp
);
2305 aio_context_release(aio_context
);
2308 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2318 blk
= blk_by_name(device
);
2320 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2321 "Device '%s' not found", device
);
2325 if (!blk_dev_has_removable_media(blk
)) {
2326 error_setg(errp
, "Device '%s' is not removable", device
);
2330 if (!blk_dev_has_tray(blk
)) {
2331 /* Ignore this command on tray-less devices */
2335 if (blk_dev_is_tray_open(blk
)) {
2339 locked
= blk_dev_is_medium_locked(blk
);
2341 blk_dev_eject_request(blk
, force
);
2344 if (!locked
|| force
) {
2345 blk_dev_change_media_cb(blk
, false);
2349 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2353 blk
= blk_by_name(device
);
2355 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2356 "Device '%s' not found", device
);
2360 if (!blk_dev_has_removable_media(blk
)) {
2361 error_setg(errp
, "Device '%s' is not removable", device
);
2365 if (!blk_dev_has_tray(blk
)) {
2366 /* Ignore this command on tray-less devices */
2370 if (!blk_dev_is_tray_open(blk
)) {
2374 blk_dev_change_media_cb(blk
, true);
2377 void qmp_x_blockdev_remove_medium(const char *device
, Error
**errp
)
2380 BlockDriverState
*bs
;
2381 AioContext
*aio_context
;
2384 blk
= blk_by_name(device
);
2386 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2387 "Device '%s' not found", device
);
2391 /* For BBs without a device, we can exchange the BDS tree at will */
2392 has_device
= blk_get_attached_dev(blk
);
2394 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2395 error_setg(errp
, "Device '%s' is not removable", device
);
2399 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2400 error_setg(errp
, "Tray of device '%s' is not open", device
);
2409 aio_context
= bdrv_get_aio_context(bs
);
2410 aio_context_acquire(aio_context
);
2412 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2416 /* This follows the convention established by bdrv_make_anon() */
2417 if (bs
->device_list
.tqe_prev
) {
2418 bdrv_device_remove(bs
);
2423 if (!blk_dev_has_tray(blk
)) {
2424 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2425 * called at all); therefore, the medium needs to be ejected here.
2426 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2427 * value passed here (i.e. false). */
2428 blk_dev_change_media_cb(blk
, false);
2432 aio_context_release(aio_context
);
2435 static void qmp_blockdev_insert_anon_medium(const char *device
,
2436 BlockDriverState
*bs
, Error
**errp
)
2441 blk
= blk_by_name(device
);
2443 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2444 "Device '%s' not found", device
);
2448 /* For BBs without a device, we can exchange the BDS tree at will */
2449 has_device
= blk_get_attached_dev(blk
);
2451 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2452 error_setg(errp
, "Device '%s' is not removable", device
);
2456 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2457 error_setg(errp
, "Tray of device '%s' is not open", device
);
2462 error_setg(errp
, "There already is a medium in device '%s'", device
);
2466 blk_insert_bs(blk
, bs
);
2468 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, device_list
);
2470 if (!blk_dev_has_tray(blk
)) {
2471 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2472 * called at all); therefore, the medium needs to be pushed into the
2474 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2475 * value passed here (i.e. true). */
2476 blk_dev_change_media_cb(blk
, true);
2480 void qmp_x_blockdev_insert_medium(const char *device
, const char *node_name
,
2483 BlockDriverState
*bs
;
2485 bs
= bdrv_find_node(node_name
);
2487 error_setg(errp
, "Node '%s' not found", node_name
);
2492 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2497 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2500 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2501 bool has_format
, const char *format
,
2503 BlockdevChangeReadOnlyMode read_only
,
2507 BlockDriverState
*medium_bs
= NULL
;
2508 int bdrv_flags
, ret
;
2509 QDict
*options
= NULL
;
2512 blk
= blk_by_name(device
);
2514 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2515 "Device '%s' not found", device
);
2520 blk_update_root_state(blk
);
2523 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2525 if (!has_read_only
) {
2526 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2529 switch (read_only
) {
2530 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2533 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2534 bdrv_flags
&= ~BDRV_O_RDWR
;
2537 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2538 bdrv_flags
|= BDRV_O_RDWR
;
2546 options
= qdict_new();
2547 qdict_put(options
, "driver", qstring_from_str(format
));
2551 ret
= bdrv_open(&medium_bs
, filename
, NULL
, options
, bdrv_flags
, errp
);
2556 blk_apply_root_state(blk
, medium_bs
);
2558 bdrv_add_key(medium_bs
, NULL
, &err
);
2560 error_propagate(errp
, err
);
2564 qmp_blockdev_open_tray(device
, false, false, &err
);
2566 error_propagate(errp
, err
);
2570 qmp_x_blockdev_remove_medium(device
, &err
);
2572 error_propagate(errp
, err
);
2576 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2578 error_propagate(errp
, err
);
2582 qmp_blockdev_close_tray(device
, errp
);
2585 /* If the medium has been inserted, the device has its own reference, so
2586 * ours must be relinquished; and if it has not been inserted successfully,
2587 * the reference must be relinquished anyway */
2588 bdrv_unref(medium_bs
);
2591 /* throttling disk I/O limits */
2592 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
2599 bool has_bps_rd_max
,
2601 bool has_bps_wr_max
,
2605 bool has_iops_rd_max
,
2606 int64_t iops_rd_max
,
2607 bool has_iops_wr_max
,
2608 int64_t iops_wr_max
,
2612 const char *group
, Error
**errp
)
2615 BlockDriverState
*bs
;
2617 AioContext
*aio_context
;
2619 blk
= blk_by_name(device
);
2621 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2622 "Device '%s' not found", device
);
2626 aio_context
= blk_get_aio_context(blk
);
2627 aio_context_acquire(aio_context
);
2631 error_setg(errp
, "Device '%s' has no medium", device
);
2635 memset(&cfg
, 0, sizeof(cfg
));
2636 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
2637 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
2638 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
2640 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
2641 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
2642 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
2645 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
2647 if (has_bps_rd_max
) {
2648 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2650 if (has_bps_wr_max
) {
2651 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2654 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2656 if (has_iops_rd_max
) {
2657 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2659 if (has_iops_wr_max
) {
2660 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2663 if (has_iops_size
) {
2664 cfg
.op_size
= iops_size
;
2667 if (!check_throttle_config(&cfg
, errp
)) {
2671 if (throttle_enabled(&cfg
)) {
2672 /* Enable I/O limits if they're not enabled yet, otherwise
2673 * just update the throttling group. */
2674 if (!bs
->throttle_state
) {
2675 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2676 } else if (has_group
) {
2677 bdrv_io_limits_update_group(bs
, group
);
2679 /* Set the new throttling configuration */
2680 bdrv_set_io_limits(bs
, &cfg
);
2681 } else if (bs
->throttle_state
) {
2682 /* If all throttling settings are set to 0, disable I/O limits */
2683 bdrv_io_limits_disable(bs
);
2687 aio_context_release(aio_context
);
2690 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2691 bool has_granularity
, uint32_t granularity
,
2694 AioContext
*aio_context
;
2695 BlockDriverState
*bs
;
2697 if (!name
|| name
[0] == '\0') {
2698 error_setg(errp
, "Bitmap name cannot be empty");
2702 bs
= bdrv_lookup_bs(node
, node
, errp
);
2707 aio_context
= bdrv_get_aio_context(bs
);
2708 aio_context_acquire(aio_context
);
2710 if (has_granularity
) {
2711 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2712 error_setg(errp
, "Granularity must be power of 2 "
2713 "and at least 512");
2717 /* Default to cluster size, if available: */
2718 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2721 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2724 aio_context_release(aio_context
);
2727 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2730 AioContext
*aio_context
;
2731 BlockDriverState
*bs
;
2732 BdrvDirtyBitmap
*bitmap
;
2734 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2735 if (!bitmap
|| !bs
) {
2739 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2741 "Bitmap '%s' is currently frozen and cannot be removed",
2745 bdrv_dirty_bitmap_make_anon(bitmap
);
2746 bdrv_release_dirty_bitmap(bs
, bitmap
);
2749 aio_context_release(aio_context
);
2753 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2754 * immediately after a full backup operation.
2756 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2759 AioContext
*aio_context
;
2760 BdrvDirtyBitmap
*bitmap
;
2761 BlockDriverState
*bs
;
2763 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2764 if (!bitmap
|| !bs
) {
2768 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2770 "Bitmap '%s' is currently frozen and cannot be modified",
2773 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2775 "Bitmap '%s' is currently disabled and cannot be cleared",
2780 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2783 aio_context_release(aio_context
);
2786 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2788 const char *id
= qdict_get_str(qdict
, "id");
2790 BlockDriverState
*bs
;
2791 AioContext
*aio_context
;
2792 Error
*local_err
= NULL
;
2794 blk
= blk_by_name(id
);
2796 error_report("Device '%s' not found", id
);
2800 if (!blk_legacy_dinfo(blk
)) {
2801 error_report("Deleting device added with blockdev-add"
2802 " is not supported");
2806 aio_context
= blk_get_aio_context(blk
);
2807 aio_context_acquire(aio_context
);
2811 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2812 error_report_err(local_err
);
2813 aio_context_release(aio_context
);
2820 /* if we have a device attached to this BlockDriverState
2821 * then we need to make the drive anonymous until the device
2822 * can be removed. If this is a drive with no device backing
2823 * then we can just get rid of the block driver state right here.
2825 if (blk_get_attached_dev(blk
)) {
2826 blk_hide_on_behalf_of_hmp_drive_del(blk
);
2827 /* Further I/O must not pause the guest */
2828 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2829 BLOCKDEV_ON_ERROR_REPORT
);
2834 aio_context_release(aio_context
);
2837 void qmp_block_resize(bool has_device
, const char *device
,
2838 bool has_node_name
, const char *node_name
,
2839 int64_t size
, Error
**errp
)
2841 Error
*local_err
= NULL
;
2842 BlockDriverState
*bs
;
2843 AioContext
*aio_context
;
2846 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2847 has_node_name
? node_name
: NULL
,
2850 error_propagate(errp
, local_err
);
2854 aio_context
= bdrv_get_aio_context(bs
);
2855 aio_context_acquire(aio_context
);
2857 if (!bdrv_is_first_non_filter(bs
)) {
2858 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2863 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2867 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2868 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2872 /* complete all in-flight operations before resizing the device */
2875 ret
= bdrv_truncate(bs
, size
);
2880 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2883 error_setg(errp
, QERR_UNSUPPORTED
);
2886 error_setg(errp
, "Device '%s' is read only", device
);
2889 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2892 error_setg_errno(errp
, -ret
, "Could not resize");
2897 aio_context_release(aio_context
);
2900 static void block_job_cb(void *opaque
, int ret
)
2902 /* Note that this function may be executed from another AioContext besides
2903 * the QEMU main loop. If you need to access anything that assumes the
2904 * QEMU global mutex, use a BH or introduce a mutex.
2907 BlockDriverState
*bs
= opaque
;
2908 const char *msg
= NULL
;
2910 trace_block_job_cb(bs
, bs
->job
, ret
);
2915 msg
= strerror(-ret
);
2918 if (block_job_is_cancelled(bs
->job
)) {
2919 block_job_event_cancelled(bs
->job
);
2921 block_job_event_completed(bs
->job
, msg
);
2925 void qmp_block_stream(const char *device
,
2926 bool has_base
, const char *base
,
2927 bool has_backing_file
, const char *backing_file
,
2928 bool has_speed
, int64_t speed
,
2929 bool has_on_error
, BlockdevOnError on_error
,
2933 BlockDriverState
*bs
;
2934 BlockDriverState
*base_bs
= NULL
;
2935 AioContext
*aio_context
;
2936 Error
*local_err
= NULL
;
2937 const char *base_name
= NULL
;
2939 if (!has_on_error
) {
2940 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2943 blk
= blk_by_name(device
);
2945 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2946 "Device '%s' not found", device
);
2950 aio_context
= blk_get_aio_context(blk
);
2951 aio_context_acquire(aio_context
);
2953 if (!blk_is_available(blk
)) {
2954 error_setg(errp
, "Device '%s' has no medium", device
);
2959 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2964 base_bs
= bdrv_find_backing_image(bs
, base
);
2965 if (base_bs
== NULL
) {
2966 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2969 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2973 /* if we are streaming the entire chain, the result will have no backing
2974 * file, and specifying one is therefore an error */
2975 if (base_bs
== NULL
&& has_backing_file
) {
2976 error_setg(errp
, "backing file specified, but streaming the "
2981 /* backing_file string overrides base bs filename */
2982 base_name
= has_backing_file
? backing_file
: base_name
;
2984 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
2985 on_error
, block_job_cb
, bs
, &local_err
);
2987 error_propagate(errp
, local_err
);
2991 trace_qmp_block_stream(bs
, bs
->job
);
2994 aio_context_release(aio_context
);
2997 void qmp_block_commit(const char *device
,
2998 bool has_base
, const char *base
,
2999 bool has_top
, const char *top
,
3000 bool has_backing_file
, const char *backing_file
,
3001 bool has_speed
, int64_t speed
,
3005 BlockDriverState
*bs
;
3006 BlockDriverState
*base_bs
, *top_bs
;
3007 AioContext
*aio_context
;
3008 Error
*local_err
= NULL
;
3009 /* This will be part of the QMP command, if/when the
3010 * BlockdevOnError change for blkmirror makes it in
3012 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3019 * libvirt relies on the DeviceNotFound error class in order to probe for
3020 * live commit feature versions; for this to work, we must make sure to
3021 * perform the device lookup before any generic errors that may occur in a
3022 * scenario in which all optional arguments are omitted. */
3023 blk
= blk_by_name(device
);
3025 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3026 "Device '%s' not found", device
);
3030 aio_context
= blk_get_aio_context(blk
);
3031 aio_context_acquire(aio_context
);
3033 if (!blk_is_available(blk
)) {
3034 error_setg(errp
, "Device '%s' has no medium", device
);
3039 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3043 /* default top_bs is the active layer */
3046 if (has_top
&& top
) {
3047 if (strcmp(bs
->filename
, top
) != 0) {
3048 top_bs
= bdrv_find_backing_image(bs
, top
);
3052 if (top_bs
== NULL
) {
3053 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3057 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3059 if (has_base
&& base
) {
3060 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3062 base_bs
= bdrv_find_base(top_bs
);
3065 if (base_bs
== NULL
) {
3066 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3070 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3072 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3076 /* Do not allow attempts to commit an image into itself */
3077 if (top_bs
== base_bs
) {
3078 error_setg(errp
, "cannot commit an image into itself");
3083 if (has_backing_file
) {
3084 error_setg(errp
, "'backing-file' specified,"
3085 " but 'top' is the active layer");
3088 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
3091 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
3092 has_backing_file
? backing_file
: NULL
, &local_err
);
3094 if (local_err
!= NULL
) {
3095 error_propagate(errp
, local_err
);
3100 aio_context_release(aio_context
);
3103 static void do_drive_backup(const char *device
, const char *target
,
3104 bool has_format
, const char *format
,
3105 enum MirrorSyncMode sync
,
3106 bool has_mode
, enum NewImageMode mode
,
3107 bool has_speed
, int64_t speed
,
3108 bool has_bitmap
, const char *bitmap
,
3109 bool has_on_source_error
,
3110 BlockdevOnError on_source_error
,
3111 bool has_on_target_error
,
3112 BlockdevOnError on_target_error
,
3113 BlockJobTxn
*txn
, Error
**errp
)
3116 BlockDriverState
*bs
;
3117 BlockDriverState
*target_bs
;
3118 BlockDriverState
*source
= NULL
;
3119 BdrvDirtyBitmap
*bmap
= NULL
;
3120 AioContext
*aio_context
;
3121 QDict
*options
= NULL
;
3122 Error
*local_err
= NULL
;
3130 if (!has_on_source_error
) {
3131 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3133 if (!has_on_target_error
) {
3134 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3137 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3140 blk
= blk_by_name(device
);
3142 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3143 "Device '%s' not found", device
);
3147 aio_context
= blk_get_aio_context(blk
);
3148 aio_context_acquire(aio_context
);
3150 /* Although backup_run has this check too, we need to use bs->drv below, so
3151 * do an early check redundantly. */
3152 if (!blk_is_available(blk
)) {
3153 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3159 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3162 /* Early check to avoid creating target */
3163 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3167 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3169 /* See if we have a backing HD we can use to create our new image
3171 if (sync
== MIRROR_SYNC_MODE_TOP
) {
3172 source
= backing_bs(bs
);
3174 sync
= MIRROR_SYNC_MODE_FULL
;
3177 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3181 size
= bdrv_getlength(bs
);
3183 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3187 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
3190 bdrv_img_create(target
, format
, source
->filename
,
3191 source
->drv
->format_name
, NULL
,
3192 size
, flags
, &local_err
, false);
3194 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
3195 size
, flags
, &local_err
, false);
3200 error_propagate(errp
, local_err
);
3205 options
= qdict_new();
3206 qdict_put(options
, "driver", qstring_from_str(format
));
3210 ret
= bdrv_open(&target_bs
, target
, NULL
, options
, flags
, &local_err
);
3212 error_propagate(errp
, local_err
);
3216 bdrv_set_aio_context(target_bs
, aio_context
);
3219 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
3221 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
3222 bdrv_unref(target_bs
);
3227 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
3228 on_source_error
, on_target_error
,
3229 block_job_cb
, bs
, txn
, &local_err
);
3230 if (local_err
!= NULL
) {
3231 bdrv_unref(target_bs
);
3232 error_propagate(errp
, local_err
);
3237 aio_context_release(aio_context
);
3240 void qmp_drive_backup(const char *device
, const char *target
,
3241 bool has_format
, const char *format
,
3242 enum MirrorSyncMode sync
,
3243 bool has_mode
, enum NewImageMode mode
,
3244 bool has_speed
, int64_t speed
,
3245 bool has_bitmap
, const char *bitmap
,
3246 bool has_on_source_error
, BlockdevOnError on_source_error
,
3247 bool has_on_target_error
, BlockdevOnError on_target_error
,
3250 return do_drive_backup(device
, target
, has_format
, format
, sync
,
3251 has_mode
, mode
, has_speed
, speed
,
3253 has_on_source_error
, on_source_error
,
3254 has_on_target_error
, on_target_error
,
3258 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3260 return bdrv_named_nodes_list(errp
);
3263 void do_blockdev_backup(const char *device
, const char *target
,
3264 enum MirrorSyncMode sync
,
3265 bool has_speed
, int64_t speed
,
3266 bool has_on_source_error
,
3267 BlockdevOnError on_source_error
,
3268 bool has_on_target_error
,
3269 BlockdevOnError on_target_error
,
3270 BlockJobTxn
*txn
, Error
**errp
)
3272 BlockBackend
*blk
, *target_blk
;
3273 BlockDriverState
*bs
;
3274 BlockDriverState
*target_bs
;
3275 Error
*local_err
= NULL
;
3276 AioContext
*aio_context
;
3281 if (!has_on_source_error
) {
3282 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3284 if (!has_on_target_error
) {
3285 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3288 blk
= blk_by_name(device
);
3290 error_setg(errp
, "Device '%s' not found", device
);
3294 aio_context
= blk_get_aio_context(blk
);
3295 aio_context_acquire(aio_context
);
3297 if (!blk_is_available(blk
)) {
3298 error_setg(errp
, "Device '%s' has no medium", device
);
3303 target_blk
= blk_by_name(target
);
3305 error_setg(errp
, "Device '%s' not found", target
);
3309 if (!blk_is_available(target_blk
)) {
3310 error_setg(errp
, "Device '%s' has no medium", target
);
3313 target_bs
= blk_bs(target_blk
);
3315 bdrv_ref(target_bs
);
3316 bdrv_set_aio_context(target_bs
, aio_context
);
3317 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
3318 on_target_error
, block_job_cb
, bs
, txn
, &local_err
);
3319 if (local_err
!= NULL
) {
3320 bdrv_unref(target_bs
);
3321 error_propagate(errp
, local_err
);
3324 aio_context_release(aio_context
);
3327 void qmp_blockdev_backup(const char *device
, const char *target
,
3328 enum MirrorSyncMode sync
,
3329 bool has_speed
, int64_t speed
,
3330 bool has_on_source_error
,
3331 BlockdevOnError on_source_error
,
3332 bool has_on_target_error
,
3333 BlockdevOnError on_target_error
,
3336 do_blockdev_backup(device
, target
, sync
, has_speed
, speed
,
3337 has_on_source_error
, on_source_error
,
3338 has_on_target_error
, on_target_error
,
3342 /* Parameter check and block job starting for drive mirroring.
3343 * Caller should hold @device and @target's aio context (must be the same).
3345 static void blockdev_mirror_common(BlockDriverState
*bs
,
3346 BlockDriverState
*target
,
3347 bool has_replaces
, const char *replaces
,
3348 enum MirrorSyncMode sync
,
3349 bool has_speed
, int64_t speed
,
3350 bool has_granularity
, uint32_t granularity
,
3351 bool has_buf_size
, int64_t buf_size
,
3352 bool has_on_source_error
,
3353 BlockdevOnError on_source_error
,
3354 bool has_on_target_error
,
3355 BlockdevOnError on_target_error
,
3356 bool has_unmap
, bool unmap
,
3363 if (!has_on_source_error
) {
3364 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3366 if (!has_on_target_error
) {
3367 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3369 if (!has_granularity
) {
3372 if (!has_buf_size
) {
3379 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3380 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3381 "a value in range [512B, 64MB]");
3384 if (granularity
& (granularity
- 1)) {
3385 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3390 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3393 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3397 error_setg(errp
, "Cannot mirror to an attached block device");
3401 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3402 sync
= MIRROR_SYNC_MODE_FULL
;
3405 /* pass the node name to replace to mirror start since it's loose coupling
3406 * and will allow to check whether the node still exist at mirror completion
3408 mirror_start(bs
, target
,
3409 has_replaces
? replaces
: NULL
,
3410 speed
, granularity
, buf_size
, sync
,
3411 on_source_error
, on_target_error
, unmap
,
3412 block_job_cb
, bs
, errp
);
3415 void qmp_drive_mirror(const char *device
, const char *target
,
3416 bool has_format
, const char *format
,
3417 bool has_node_name
, const char *node_name
,
3418 bool has_replaces
, const char *replaces
,
3419 enum MirrorSyncMode sync
,
3420 bool has_mode
, enum NewImageMode mode
,
3421 bool has_speed
, int64_t speed
,
3422 bool has_granularity
, uint32_t granularity
,
3423 bool has_buf_size
, int64_t buf_size
,
3424 bool has_on_source_error
, BlockdevOnError on_source_error
,
3425 bool has_on_target_error
, BlockdevOnError on_target_error
,
3426 bool has_unmap
, bool unmap
,
3429 BlockDriverState
*bs
;
3431 BlockDriverState
*source
, *target_bs
;
3432 AioContext
*aio_context
;
3433 Error
*local_err
= NULL
;
3434 QDict
*options
= NULL
;
3439 blk
= blk_by_name(device
);
3441 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3442 "Device '%s' not found", device
);
3446 aio_context
= blk_get_aio_context(blk
);
3447 aio_context_acquire(aio_context
);
3449 if (!blk_is_available(blk
)) {
3450 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3455 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3459 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3462 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3463 source
= backing_bs(bs
);
3464 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3465 sync
= MIRROR_SYNC_MODE_FULL
;
3467 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3471 size
= bdrv_getlength(bs
);
3473 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3478 BlockDriverState
*to_replace_bs
;
3479 AioContext
*replace_aio_context
;
3480 int64_t replace_size
;
3482 if (!has_node_name
) {
3483 error_setg(errp
, "a node-name must be provided when replacing a"
3484 " named node of the graph");
3488 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
3490 if (!to_replace_bs
) {
3491 error_propagate(errp
, local_err
);
3495 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3496 aio_context_acquire(replace_aio_context
);
3497 replace_size
= bdrv_getlength(to_replace_bs
);
3498 aio_context_release(replace_aio_context
);
3500 if (size
!= replace_size
) {
3501 error_setg(errp
, "cannot replace image with a mirror image of "
3507 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3508 && mode
!= NEW_IMAGE_MODE_EXISTING
)
3510 /* create new image w/o backing file */
3512 bdrv_img_create(target
, format
,
3513 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3516 case NEW_IMAGE_MODE_EXISTING
:
3518 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3519 /* create new image with backing file */
3520 bdrv_img_create(target
, format
,
3522 source
->drv
->format_name
,
3523 NULL
, size
, flags
, &local_err
, false);
3531 error_propagate(errp
, local_err
);
3535 options
= qdict_new();
3536 if (has_node_name
) {
3537 qdict_put(options
, "node-name", qstring_from_str(node_name
));
3540 qdict_put(options
, "driver", qstring_from_str(format
));
3543 /* Mirroring takes care of copy-on-write using the source's backing
3547 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
3548 flags
| BDRV_O_NO_BACKING
, &local_err
);
3550 error_propagate(errp
, local_err
);
3554 bdrv_set_aio_context(target_bs
, aio_context
);
3556 blockdev_mirror_common(bs
, target_bs
,
3557 has_replaces
, replaces
, sync
,
3559 has_granularity
, granularity
,
3560 has_buf_size
, buf_size
,
3561 has_on_source_error
, on_source_error
,
3562 has_on_target_error
, on_target_error
,
3566 error_propagate(errp
, local_err
);
3567 bdrv_unref(target_bs
);
3570 aio_context_release(aio_context
);
3573 void qmp_blockdev_mirror(const char *device
, const char *target
,
3574 bool has_replaces
, const char *replaces
,
3575 MirrorSyncMode sync
,
3576 bool has_speed
, int64_t speed
,
3577 bool has_granularity
, uint32_t granularity
,
3578 bool has_buf_size
, int64_t buf_size
,
3579 bool has_on_source_error
,
3580 BlockdevOnError on_source_error
,
3581 bool has_on_target_error
,
3582 BlockdevOnError on_target_error
,
3585 BlockDriverState
*bs
;
3587 BlockDriverState
*target_bs
;
3588 AioContext
*aio_context
;
3589 Error
*local_err
= NULL
;
3591 blk
= blk_by_name(device
);
3593 error_setg(errp
, "Device '%s' not found", device
);
3599 error_setg(errp
, "Device '%s' has no media", device
);
3603 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3608 aio_context
= bdrv_get_aio_context(bs
);
3609 aio_context_acquire(aio_context
);
3611 bdrv_ref(target_bs
);
3612 bdrv_set_aio_context(target_bs
, aio_context
);
3614 blockdev_mirror_common(bs
, target_bs
,
3615 has_replaces
, replaces
, sync
,
3617 has_granularity
, granularity
,
3618 has_buf_size
, buf_size
,
3619 has_on_source_error
, on_source_error
,
3620 has_on_target_error
, on_target_error
,
3624 error_propagate(errp
, local_err
);
3625 bdrv_unref(target_bs
);
3628 aio_context_release(aio_context
);
3631 /* Get the block job for a given device name and acquire its AioContext */
3632 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
3636 BlockDriverState
*bs
;
3638 *aio_context
= NULL
;
3640 blk
= blk_by_name(device
);
3645 *aio_context
= blk_get_aio_context(blk
);
3646 aio_context_acquire(*aio_context
);
3648 if (!blk_is_available(blk
)) {
3660 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3661 "No active block job on device '%s'", device
);
3663 aio_context_release(*aio_context
);
3664 *aio_context
= NULL
;
3669 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3671 AioContext
*aio_context
;
3672 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3678 block_job_set_speed(job
, speed
, errp
);
3679 aio_context_release(aio_context
);
3682 void qmp_block_job_cancel(const char *device
,
3683 bool has_force
, bool force
, Error
**errp
)
3685 AioContext
*aio_context
;
3686 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3696 if (job
->user_paused
&& !force
) {
3697 error_setg(errp
, "The block job for device '%s' is currently paused",
3702 trace_qmp_block_job_cancel(job
);
3703 block_job_cancel(job
);
3705 aio_context_release(aio_context
);
3708 void qmp_block_job_pause(const char *device
, Error
**errp
)
3710 AioContext
*aio_context
;
3711 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3713 if (!job
|| job
->user_paused
) {
3717 job
->user_paused
= true;
3718 trace_qmp_block_job_pause(job
);
3719 block_job_pause(job
);
3720 aio_context_release(aio_context
);
3723 void qmp_block_job_resume(const char *device
, Error
**errp
)
3725 AioContext
*aio_context
;
3726 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3728 if (!job
|| !job
->user_paused
) {
3732 job
->user_paused
= false;
3733 trace_qmp_block_job_resume(job
);
3734 block_job_resume(job
);
3735 aio_context_release(aio_context
);
3738 void qmp_block_job_complete(const char *device
, Error
**errp
)
3740 AioContext
*aio_context
;
3741 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3747 trace_qmp_block_job_complete(job
);
3748 block_job_complete(job
, errp
);
3749 aio_context_release(aio_context
);
3752 void qmp_change_backing_file(const char *device
,
3753 const char *image_node_name
,
3754 const char *backing_file
,
3758 BlockDriverState
*bs
= NULL
;
3759 AioContext
*aio_context
;
3760 BlockDriverState
*image_bs
= NULL
;
3761 Error
*local_err
= NULL
;
3766 blk
= blk_by_name(device
);
3768 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3769 "Device '%s' not found", device
);
3773 aio_context
= blk_get_aio_context(blk
);
3774 aio_context_acquire(aio_context
);
3776 if (!blk_is_available(blk
)) {
3777 error_setg(errp
, "Device '%s' has no medium", device
);
3782 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3784 error_propagate(errp
, local_err
);
3789 error_setg(errp
, "image file not found");
3793 if (bdrv_find_base(image_bs
) == image_bs
) {
3794 error_setg(errp
, "not allowing backing file change on an image "
3795 "without a backing file");
3799 /* even though we are not necessarily operating on bs, we need it to
3800 * determine if block ops are currently prohibited on the chain */
3801 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3805 /* final sanity check */
3806 if (!bdrv_chain_contains(bs
, image_bs
)) {
3807 error_setg(errp
, "'%s' and image file are not in the same chain",
3812 /* if not r/w, reopen to make r/w */
3813 open_flags
= image_bs
->open_flags
;
3814 ro
= bdrv_is_read_only(image_bs
);
3817 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3819 error_propagate(errp
, local_err
);
3824 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3825 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3828 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3830 /* don't exit here, so we can try to restore open flags if
3835 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3837 error_propagate(errp
, local_err
); /* will preserve prior errp */
3842 aio_context_release(aio_context
);
3845 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3847 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3848 BlockDriverState
*bs
;
3849 BlockBackend
*blk
= NULL
;
3852 Error
*local_err
= NULL
;
3854 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3855 * cache.direct=false instead of silently switching to aio=threads, except
3856 * when called from drive_new().
3858 * For now, simply forbidding the combination for all drivers will do. */
3859 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3860 bool direct
= options
->has_cache
&&
3861 options
->cache
->has_direct
&&
3862 options
->cache
->direct
;
3864 error_setg(errp
, "aio=native requires cache.direct=true");
3869 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
), NULL
, &options
,
3872 error_propagate(errp
, local_err
);
3876 obj
= qmp_output_get_qobject(ov
);
3877 qdict
= qobject_to_qdict(obj
);
3879 qdict_flatten(qdict
);
3881 if (options
->has_id
) {
3882 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3884 error_propagate(errp
, local_err
);
3890 if (!qdict_get_try_str(qdict
, "node-name")) {
3891 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3896 bs
= bds_tree_init(qdict
, errp
);
3901 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3904 if (bs
&& bdrv_key_required(bs
)) {
3908 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3911 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3916 qmp_output_visitor_cleanup(ov
);
3919 void qmp_x_blockdev_del(bool has_id
, const char *id
,
3920 bool has_node_name
, const char *node_name
, Error
**errp
)
3922 AioContext
*aio_context
;
3924 BlockDriverState
*bs
;
3926 if (has_id
&& has_node_name
) {
3927 error_setg(errp
, "Only one of id and node-name must be specified");
3929 } else if (!has_id
&& !has_node_name
) {
3930 error_setg(errp
, "No block device specified");
3935 blk
= blk_by_name(id
);
3937 error_setg(errp
, "Cannot find block backend %s", id
);
3940 if (blk_get_refcnt(blk
) > 1) {
3941 error_setg(errp
, "Block backend %s is in use", id
);
3945 aio_context
= blk_get_aio_context(blk
);
3947 bs
= bdrv_find_node(node_name
);
3949 error_setg(errp
, "Cannot find node %s", node_name
);
3954 error_setg(errp
, "Node %s is in use by %s",
3955 node_name
, blk_name(blk
));
3958 aio_context
= bdrv_get_aio_context(bs
);
3961 aio_context_acquire(aio_context
);
3964 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3968 if (!blk
&& !bs
->monitor_list
.tqe_prev
) {
3969 error_setg(errp
, "Node %s is not owned by the monitor",
3974 if (bs
->refcnt
> 1) {
3975 error_setg(errp
, "Block device %s is in use",
3976 bdrv_get_device_or_node_name(bs
));
3984 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3989 aio_context_release(aio_context
);
3992 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3994 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3995 BlockDriverState
*bs
;
3997 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3998 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
4000 aio_context_acquire(aio_context
);
4003 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
4004 elem
->value
= block_job_query(bs
->job
);
4006 p_next
= &elem
->next
;
4009 aio_context_release(aio_context
);
4015 QemuOptsList qemu_common_drive_opts
= {
4017 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4021 .type
= QEMU_OPT_BOOL
,
4022 .help
= "enable/disable snapshot mode",
4025 .type
= QEMU_OPT_STRING
,
4026 .help
= "discard operation (ignore/off, unmap/on)",
4029 .type
= QEMU_OPT_STRING
,
4030 .help
= "host AIO implementation (threads, native)",
4033 .type
= QEMU_OPT_STRING
,
4034 .help
= "disk format (raw, qcow2, ...)",
4037 .type
= QEMU_OPT_STRING
,
4038 .help
= "read error action",
4041 .type
= QEMU_OPT_STRING
,
4042 .help
= "write error action",
4044 .name
= "read-only",
4045 .type
= QEMU_OPT_BOOL
,
4046 .help
= "open drive file as read-only",
4048 .name
= "throttling.iops-total",
4049 .type
= QEMU_OPT_NUMBER
,
4050 .help
= "limit total I/O operations per second",
4052 .name
= "throttling.iops-read",
4053 .type
= QEMU_OPT_NUMBER
,
4054 .help
= "limit read operations per second",
4056 .name
= "throttling.iops-write",
4057 .type
= QEMU_OPT_NUMBER
,
4058 .help
= "limit write operations per second",
4060 .name
= "throttling.bps-total",
4061 .type
= QEMU_OPT_NUMBER
,
4062 .help
= "limit total bytes per second",
4064 .name
= "throttling.bps-read",
4065 .type
= QEMU_OPT_NUMBER
,
4066 .help
= "limit read bytes per second",
4068 .name
= "throttling.bps-write",
4069 .type
= QEMU_OPT_NUMBER
,
4070 .help
= "limit write bytes per second",
4072 .name
= "throttling.iops-total-max",
4073 .type
= QEMU_OPT_NUMBER
,
4074 .help
= "I/O operations burst",
4076 .name
= "throttling.iops-read-max",
4077 .type
= QEMU_OPT_NUMBER
,
4078 .help
= "I/O operations read burst",
4080 .name
= "throttling.iops-write-max",
4081 .type
= QEMU_OPT_NUMBER
,
4082 .help
= "I/O operations write burst",
4084 .name
= "throttling.bps-total-max",
4085 .type
= QEMU_OPT_NUMBER
,
4086 .help
= "total bytes burst",
4088 .name
= "throttling.bps-read-max",
4089 .type
= QEMU_OPT_NUMBER
,
4090 .help
= "total bytes read burst",
4092 .name
= "throttling.bps-write-max",
4093 .type
= QEMU_OPT_NUMBER
,
4094 .help
= "total bytes write burst",
4096 .name
= "throttling.iops-size",
4097 .type
= QEMU_OPT_NUMBER
,
4098 .help
= "when limiting by iops max size of an I/O in bytes",
4100 .name
= "throttling.group",
4101 .type
= QEMU_OPT_STRING
,
4102 .help
= "name of the block throttling group",
4104 .name
= "copy-on-read",
4105 .type
= QEMU_OPT_BOOL
,
4106 .help
= "copy read data from backing file into image file",
4108 .name
= "detect-zeroes",
4109 .type
= QEMU_OPT_STRING
,
4110 .help
= "try to optimize zero writes (off, on, unmap)",
4112 .name
= "stats-account-invalid",
4113 .type
= QEMU_OPT_BOOL
,
4114 .help
= "whether to account for invalid I/O operations "
4115 "in the statistics",
4117 .name
= "stats-account-failed",
4118 .type
= QEMU_OPT_BOOL
,
4119 .help
= "whether to account for failed I/O operations "
4120 "in the statistics",
4122 { /* end of list */ }
4126 static QemuOptsList qemu_root_bds_opts
= {
4128 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4132 .type
= QEMU_OPT_STRING
,
4133 .help
= "discard operation (ignore/off, unmap/on)",
4136 .type
= QEMU_OPT_STRING
,
4137 .help
= "host AIO implementation (threads, native)",
4139 .name
= "read-only",
4140 .type
= QEMU_OPT_BOOL
,
4141 .help
= "open drive file as read-only",
4143 .name
= "copy-on-read",
4144 .type
= QEMU_OPT_BOOL
,
4145 .help
= "copy read data from backing file into image file",
4147 .name
= "detect-zeroes",
4148 .type
= QEMU_OPT_STRING
,
4149 .help
= "try to optimize zero writes (off, on, unmap)",
4151 { /* end of list */ }
4155 QemuOptsList qemu_drive_opts
= {
4157 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4160 * no elements => accept any params
4161 * validation will happen later
4163 { /* end of list */ }