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/qobject-output-visitor.h"
47 #include "qapi/util.h"
48 #include "sysemu/sysemu.h"
49 #include "block/block_int.h"
50 #include "qmp-commands.h"
51 #include "block/trace.h"
52 #include "sysemu/arch_init.h"
53 #include "qemu/cutils.h"
54 #include "qemu/help_option.h"
55 #include "qemu/throttle-options.h"
57 static QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
58 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
60 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
61 bool force
, Error
**errp
);
63 static const char *const if_name
[IF_COUNT
] = {
67 [IF_FLOPPY
] = "floppy",
68 [IF_PFLASH
] = "pflash",
71 [IF_VIRTIO
] = "virtio",
75 static int if_max_devs
[IF_COUNT
] = {
77 * Do not change these numbers! They govern how drive option
78 * index maps to unit and bus. That mapping is ABI.
80 * All controllers used to implement if=T drives need to support
81 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
82 * Otherwise, some index values map to "impossible" bus, unit
85 * For instance, if you change [IF_SCSI] to 255, -drive
86 * if=scsi,index=12 no longer means bus=1,unit=5, but
87 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
88 * the drive can't be set up. Regression.
95 * Boards may call this to offer board-by-board overrides
96 * of the default, global values.
98 void override_max_devs(BlockInterfaceType type
, int max_devs
)
107 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
108 dinfo
= blk_legacy_dinfo(blk
);
109 if (dinfo
->type
== type
) {
110 fprintf(stderr
, "Cannot override units-per-bus property of"
111 " the %s interface, because a drive of that type has"
112 " already been added.\n", if_name
[type
]);
113 g_assert_not_reached();
117 if_max_devs
[type
] = max_devs
;
121 * We automatically delete the drive when a device using it gets
122 * unplugged. Questionable feature, but we can't just drop it.
123 * Device models call blockdev_mark_auto_del() to schedule the
124 * automatic deletion, and generic qdev code calls blockdev_auto_del()
125 * when deletion is actually safe.
127 void blockdev_mark_auto_del(BlockBackend
*blk
)
129 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
130 BlockDriverState
*bs
= blk_bs(blk
);
131 AioContext
*aio_context
;
138 aio_context
= bdrv_get_aio_context(bs
);
139 aio_context_acquire(aio_context
);
142 block_job_cancel(bs
->job
);
145 aio_context_release(aio_context
);
151 void blockdev_auto_del(BlockBackend
*blk
)
153 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
155 if (dinfo
&& dinfo
->auto_del
) {
156 monitor_remove_blk(blk
);
162 * Returns the current mapping of how many units per bus
163 * a particular interface can support.
165 * A positive integer indicates n units per bus.
166 * 0 implies the mapping has not been established.
167 * -1 indicates an invalid BlockInterfaceType was given.
169 int drive_get_max_devs(BlockInterfaceType type
)
171 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
172 return if_max_devs
[type
];
178 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
180 int max_devs
= if_max_devs
[type
];
181 return max_devs
? index
/ max_devs
: 0;
184 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
186 int max_devs
= if_max_devs
[type
];
187 return max_devs
? index
% max_devs
: index
;
190 QemuOpts
*drive_def(const char *optstr
)
192 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
195 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
200 opts
= drive_def(optstr
);
204 if (type
!= IF_DEFAULT
) {
205 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
208 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
211 qemu_opt_set(opts
, "file", file
, &error_abort
);
215 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
220 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
221 dinfo
= blk_legacy_dinfo(blk
);
222 if (dinfo
&& dinfo
->type
== type
223 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
231 void drive_check_orphaned(void)
236 bool orphans
= false;
238 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
239 dinfo
= blk_legacy_dinfo(blk
);
240 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
241 dinfo
->type
!= IF_NONE
) {
243 qemu_opts_loc_restore(dinfo
->opts
);
244 error_report("machine type does not support"
245 " if=%s,bus=%d,unit=%d",
246 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
257 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
259 return drive_get(type
,
260 drive_index_to_bus_id(type
, index
),
261 drive_index_to_unit_id(type
, index
));
264 int drive_get_max_bus(BlockInterfaceType type
)
271 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
272 dinfo
= blk_legacy_dinfo(blk
);
273 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
274 max_bus
= dinfo
->bus
;
280 /* Get a block device. This should only be used for single-drive devices
281 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
283 DriveInfo
*drive_get_next(BlockInterfaceType type
)
285 static int next_block_unit
[IF_COUNT
];
287 return drive_get(type
, 0, next_block_unit
[type
]++);
290 static void bdrv_format_print(void *opaque
, const char *name
)
292 error_printf(" %s", name
);
297 BlockDriverState
*bs
;
300 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
302 if (!strcmp(buf
, "ignore")) {
303 return BLOCKDEV_ON_ERROR_IGNORE
;
304 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
305 return BLOCKDEV_ON_ERROR_ENOSPC
;
306 } else if (!strcmp(buf
, "stop")) {
307 return BLOCKDEV_ON_ERROR_STOP
;
308 } else if (!strcmp(buf
, "report")) {
309 return BLOCKDEV_ON_ERROR_REPORT
;
311 error_setg(errp
, "'%s' invalid %s error action",
312 buf
, is_read
? "read" : "write");
317 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
320 const QListEntry
*entry
;
321 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
322 switch (qobject_type(entry
->value
)) {
324 case QTYPE_QSTRING
: {
325 unsigned long long length
;
326 const char *str
= qstring_get_str(qobject_to_qstring(entry
->value
));
327 if (parse_uint_full(str
, &length
, 10) == 0 &&
328 length
> 0 && length
<= UINT_MAX
) {
329 block_acct_add_interval(stats
, (unsigned) length
);
331 error_setg(errp
, "Invalid interval length: %s", str
);
338 int64_t length
= qint_get_int(qobject_to_qint(entry
->value
));
339 if (length
> 0 && length
<= UINT_MAX
) {
340 block_acct_add_interval(stats
, (unsigned) length
);
342 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
349 error_setg(errp
, "The specification of stats-intervals is invalid");
356 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
358 /* All parameters but @opts are optional and may be set to NULL. */
359 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
360 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
361 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
363 Error
*local_error
= NULL
;
367 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
368 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
371 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
372 if (!strcmp(aio
, "native")) {
373 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
374 } else if (!strcmp(aio
, "threads")) {
375 /* this is the default */
377 error_setg(errp
, "invalid aio option");
383 /* disk I/O throttling */
384 if (throttling_group
) {
385 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
389 throttle_config_init(throttle_cfg
);
390 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
391 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
392 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
393 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
394 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
395 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
396 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
397 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
398 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
399 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
400 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
401 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
403 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
404 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
405 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
406 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
407 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
408 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
409 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
410 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
411 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
412 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
413 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
414 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
416 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
417 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
418 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
419 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
420 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
421 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
422 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
423 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
424 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
425 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
426 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
427 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
429 throttle_cfg
->op_size
=
430 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
432 if (!throttle_is_valid(throttle_cfg
, errp
)) {
439 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
440 qemu_opt_get(opts
, "detect-zeroes"),
441 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX
,
442 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
445 error_propagate(errp
, local_error
);
451 /* Takes the ownership of bs_opts */
452 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
457 int on_read_error
, on_write_error
;
458 bool account_invalid
, account_failed
;
459 bool writethrough
, read_only
;
461 BlockDriverState
*bs
;
466 QDict
*interval_dict
= NULL
;
467 QList
*interval_list
= NULL
;
469 BlockdevDetectZeroesOptions detect_zeroes
=
470 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
471 const char *throttling_group
= NULL
;
473 /* Check common options by copying from bs_opts to opts, all other options
474 * stay in bs_opts for processing by bdrv_open(). */
475 id
= qdict_get_try_str(bs_opts
, "id");
476 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
478 error_propagate(errp
, error
);
482 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
484 error_propagate(errp
, error
);
489 qdict_del(bs_opts
, "id");
492 /* extract parameters */
493 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
495 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
496 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
498 writethrough
= !qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true);
500 id
= qemu_opts_id(opts
);
502 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
503 qdict_array_split(interval_dict
, &interval_list
);
505 if (qdict_size(interval_dict
) != 0) {
506 error_setg(errp
, "Invalid option stats-intervals.%s",
507 qdict_first(interval_dict
)->key
);
511 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
512 &detect_zeroes
, &error
);
514 error_propagate(errp
, error
);
518 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
519 if (is_help_option(buf
)) {
520 error_printf("Supported formats:");
521 bdrv_iterate_format(bdrv_format_print
, NULL
);
526 if (qdict_haskey(bs_opts
, "driver")) {
527 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
530 qdict_put_str(bs_opts
, "driver", buf
);
533 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
534 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
535 on_write_error
= parse_block_error_action(buf
, 0, &error
);
537 error_propagate(errp
, error
);
542 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
543 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
544 on_read_error
= parse_block_error_action(buf
, 1, &error
);
546 error_propagate(errp
, error
);
552 bdrv_flags
|= BDRV_O_SNAPSHOT
;
555 read_only
= qemu_opt_get_bool(opts
, BDRV_OPT_READ_ONLY
, false);
558 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
559 BlockBackendRootState
*blk_rs
;
561 blk
= blk_new(0, BLK_PERM_ALL
);
562 blk_rs
= blk_get_root_state(blk
);
563 blk_rs
->open_flags
= bdrv_flags
;
564 blk_rs
->read_only
= read_only
;
565 blk_rs
->detect_zeroes
= detect_zeroes
;
569 if (file
&& !*file
) {
573 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
574 * with other callers) rather than what we want as the real defaults.
575 * Apply the defaults here instead. */
576 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
577 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
578 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
,
579 read_only
? "on" : "off");
580 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
582 if (runstate_check(RUN_STATE_INMIGRATE
)) {
583 bdrv_flags
|= BDRV_O_INACTIVE
;
586 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
592 bs
->detect_zeroes
= detect_zeroes
;
594 if (bdrv_key_required(bs
)) {
598 block_acct_init(blk_get_stats(blk
), account_invalid
, account_failed
);
600 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
607 /* disk I/O throttling */
608 if (throttle_enabled(&cfg
)) {
609 if (!throttling_group
) {
610 throttling_group
= id
;
612 blk_io_limits_enable(blk
, throttling_group
);
613 blk_set_io_limits(blk
, &cfg
);
616 blk_set_enable_write_cache(blk
, !writethrough
);
617 blk_set_on_error(blk
, on_read_error
, on_write_error
);
619 if (!monitor_add_blk(blk
, id
, errp
)) {
627 QDECREF(interval_dict
);
628 QDECREF(interval_list
);
633 QDECREF(interval_dict
);
634 QDECREF(interval_list
);
640 /* Takes the ownership of bs_opts */
641 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
645 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
646 * with other callers) rather than what we want as the real defaults.
647 * Apply the defaults here instead. */
648 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
649 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
650 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
, "off");
652 if (runstate_check(RUN_STATE_INMIGRATE
)) {
653 bdrv_flags
|= BDRV_O_INACTIVE
;
656 return bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
659 void blockdev_close_all_bdrv_states(void)
661 BlockDriverState
*bs
, *next_bs
;
663 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
664 AioContext
*ctx
= bdrv_get_aio_context(bs
);
666 aio_context_acquire(ctx
);
668 aio_context_release(ctx
);
672 /* Iterates over the list of monitor-owned BlockDriverStates */
673 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
675 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
676 : QTAILQ_FIRST(&monitor_bdrv_states
);
679 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
684 value
= qemu_opt_get(opts
, from
);
686 if (qemu_opt_find(opts
, to
)) {
687 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
688 "same time", to
, from
);
693 /* rename all items in opts */
694 while ((value
= qemu_opt_get(opts
, from
))) {
695 qemu_opt_set(opts
, to
, value
, &error_abort
);
696 qemu_opt_unset(opts
, from
);
700 QemuOptsList qemu_legacy_drive_opts
= {
702 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
706 .type
= QEMU_OPT_NUMBER
,
707 .help
= "bus number",
710 .type
= QEMU_OPT_NUMBER
,
711 .help
= "unit number (i.e. lun for scsi)",
714 .type
= QEMU_OPT_NUMBER
,
715 .help
= "index number",
718 .type
= QEMU_OPT_STRING
,
719 .help
= "media type (disk, cdrom)",
722 .type
= QEMU_OPT_STRING
,
723 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
726 .type
= QEMU_OPT_NUMBER
,
727 .help
= "number of cylinders (ide disk geometry)",
730 .type
= QEMU_OPT_NUMBER
,
731 .help
= "number of heads (ide disk geometry)",
734 .type
= QEMU_OPT_NUMBER
,
735 .help
= "number of sectors (ide disk geometry)",
738 .type
= QEMU_OPT_STRING
,
739 .help
= "chs translation (auto, lba, none)",
742 .type
= QEMU_OPT_BOOL
,
743 .help
= "(deprecated, ignored)",
746 .type
= QEMU_OPT_STRING
,
747 .help
= "pci address (virtio only)",
750 .type
= QEMU_OPT_STRING
,
751 .help
= "disk serial number",
754 .type
= QEMU_OPT_STRING
,
758 /* Options that are passed on, but have special semantics with -drive */
760 .name
= BDRV_OPT_READ_ONLY
,
761 .type
= QEMU_OPT_BOOL
,
762 .help
= "open drive file as read-only",
765 .type
= QEMU_OPT_STRING
,
766 .help
= "read error action",
769 .type
= QEMU_OPT_STRING
,
770 .help
= "write error action",
772 .name
= "copy-on-read",
773 .type
= QEMU_OPT_BOOL
,
774 .help
= "copy read data from backing file into image file",
777 { /* end of list */ }
781 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
785 DriveInfo
*dinfo
= NULL
;
787 QemuOpts
*legacy_opts
;
788 DriveMediaType media
= MEDIA_DISK
;
789 BlockInterfaceType type
;
790 int cyls
, heads
, secs
, translation
;
791 int max_devs
, bus_id
, unit_id
, index
;
793 const char *werror
, *rerror
;
794 bool read_only
= false;
797 const char *filename
;
798 Error
*local_err
= NULL
;
801 /* Change legacy command line options into QMP ones */
802 static const struct {
806 { "iops", "throttling.iops-total" },
807 { "iops_rd", "throttling.iops-read" },
808 { "iops_wr", "throttling.iops-write" },
810 { "bps", "throttling.bps-total" },
811 { "bps_rd", "throttling.bps-read" },
812 { "bps_wr", "throttling.bps-write" },
814 { "iops_max", "throttling.iops-total-max" },
815 { "iops_rd_max", "throttling.iops-read-max" },
816 { "iops_wr_max", "throttling.iops-write-max" },
818 { "bps_max", "throttling.bps-total-max" },
819 { "bps_rd_max", "throttling.bps-read-max" },
820 { "bps_wr_max", "throttling.bps-write-max" },
822 { "iops_size", "throttling.iops-size" },
824 { "group", "throttling.group" },
826 { "readonly", BDRV_OPT_READ_ONLY
},
829 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
830 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
833 error_report_err(local_err
);
838 value
= qemu_opt_get(all_opts
, "cache");
843 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
844 error_report("invalid cache option");
848 /* Specific options take precedence */
849 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
850 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
851 !writethrough
, &error_abort
);
853 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
854 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
855 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
857 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
858 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
859 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
861 qemu_opt_unset(all_opts
, "cache");
864 /* Get a QDict for processing the options */
865 bs_opts
= qdict_new();
866 qemu_opts_to_qdict(all_opts
, bs_opts
);
868 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
870 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
872 error_report_err(local_err
);
876 /* Deprecated option boot=[on|off] */
877 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
878 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
879 "ignored. Future versions will reject this parameter. Please "
880 "update your scripts.\n");
884 value
= qemu_opt_get(legacy_opts
, "media");
886 if (!strcmp(value
, "disk")) {
888 } else if (!strcmp(value
, "cdrom")) {
892 error_report("'%s' invalid media", value
);
897 /* copy-on-read is disabled with a warning for read-only devices */
898 read_only
|= qemu_opt_get_bool(legacy_opts
, BDRV_OPT_READ_ONLY
, false);
899 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
901 if (read_only
&& copy_on_read
) {
902 error_report("warning: disabling copy-on-read on read-only drive");
903 copy_on_read
= false;
906 qdict_put_str(bs_opts
, BDRV_OPT_READ_ONLY
, read_only
? "on" : "off");
907 qdict_put_str(bs_opts
, "copy-on-read", copy_on_read
? "on" : "off");
909 /* Controller type */
910 value
= qemu_opt_get(legacy_opts
, "if");
913 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
916 if (type
== IF_COUNT
) {
917 error_report("unsupported bus type '%s'", value
);
921 type
= block_default_type
;
925 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
926 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
927 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
929 if (cyls
|| heads
|| secs
) {
931 error_report("invalid physical cyls number");
935 error_report("invalid physical heads number");
939 error_report("invalid physical secs number");
944 translation
= BIOS_ATA_TRANSLATION_AUTO
;
945 value
= qemu_opt_get(legacy_opts
, "trans");
948 error_report("'%s' trans must be used with cyls, heads and secs",
952 if (!strcmp(value
, "none")) {
953 translation
= BIOS_ATA_TRANSLATION_NONE
;
954 } else if (!strcmp(value
, "lba")) {
955 translation
= BIOS_ATA_TRANSLATION_LBA
;
956 } else if (!strcmp(value
, "large")) {
957 translation
= BIOS_ATA_TRANSLATION_LARGE
;
958 } else if (!strcmp(value
, "rechs")) {
959 translation
= BIOS_ATA_TRANSLATION_RECHS
;
960 } else if (!strcmp(value
, "auto")) {
961 translation
= BIOS_ATA_TRANSLATION_AUTO
;
963 error_report("'%s' invalid translation type", value
);
968 if (media
== MEDIA_CDROM
) {
969 if (cyls
|| secs
|| heads
) {
970 error_report("CHS can't be set with media=cdrom");
975 /* Device address specified by bus/unit or index.
976 * If none was specified, try to find the first free one. */
977 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
978 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
979 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
981 max_devs
= if_max_devs
[type
];
984 if (bus_id
!= 0 || unit_id
!= -1) {
985 error_report("index cannot be used with bus and unit");
988 bus_id
= drive_index_to_bus_id(type
, index
);
989 unit_id
= drive_index_to_unit_id(type
, index
);
994 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
996 if (max_devs
&& unit_id
>= max_devs
) {
1003 if (max_devs
&& unit_id
>= max_devs
) {
1004 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1008 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1009 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1010 bus_id
, unit_id
, index
);
1015 serial
= qemu_opt_get(legacy_opts
, "serial");
1017 /* no id supplied -> create one */
1018 if (qemu_opts_id(all_opts
) == NULL
) {
1020 const char *mediastr
= "";
1021 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1022 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1025 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1028 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1031 qdict_put_str(bs_opts
, "id", new_id
);
1035 /* Add virtio block device */
1036 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1037 if (devaddr
&& type
!= IF_VIRTIO
) {
1038 error_report("addr is not supported by this bus type");
1042 if (type
== IF_VIRTIO
) {
1044 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1046 if (arch_type
== QEMU_ARCH_S390X
) {
1047 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1049 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1051 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1054 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1058 filename
= qemu_opt_get(legacy_opts
, "file");
1060 /* Check werror/rerror compatibility with if=... */
1061 werror
= qemu_opt_get(legacy_opts
, "werror");
1062 if (werror
!= NULL
) {
1063 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1065 error_report("werror is not supported by this bus type");
1068 qdict_put_str(bs_opts
, "werror", werror
);
1071 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1072 if (rerror
!= NULL
) {
1073 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1075 error_report("rerror is not supported by this bus type");
1078 qdict_put_str(bs_opts
, "rerror", rerror
);
1081 /* Actual block device init: Functionality shared with blockdev-add */
1082 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1086 error_report_err(local_err
);
1093 /* Create legacy DriveInfo */
1094 dinfo
= g_malloc0(sizeof(*dinfo
));
1095 dinfo
->opts
= all_opts
;
1098 dinfo
->heads
= heads
;
1100 dinfo
->trans
= translation
;
1103 dinfo
->bus
= bus_id
;
1104 dinfo
->unit
= unit_id
;
1105 dinfo
->devaddr
= devaddr
;
1106 dinfo
->serial
= g_strdup(serial
);
1108 blk_set_legacy_dinfo(blk
, dinfo
);
1115 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1122 qemu_opts_del(legacy_opts
);
1127 static BlockDriverState
*qmp_get_root_bs(const char *name
, Error
**errp
)
1129 BlockDriverState
*bs
;
1131 bs
= bdrv_lookup_bs(name
, name
, errp
);
1136 if (!bdrv_is_root_node(bs
)) {
1137 error_setg(errp
, "Need a root block node");
1141 if (!bdrv_is_inserted(bs
)) {
1142 error_setg(errp
, "Device has no medium");
1149 static BlockBackend
*qmp_get_blk(const char *blk_name
, const char *qdev_id
,
1154 if (!blk_name
== !qdev_id
) {
1155 error_setg(errp
, "Need exactly one of 'device' and 'id'");
1160 blk
= blk_by_qdev_id(qdev_id
, errp
);
1162 blk
= blk_by_name(blk_name
);
1164 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1165 "Device '%s' not found", blk_name
);
1172 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1174 const char *device
= qdict_get_str(qdict
, "device");
1178 if (!strcmp(device
, "all")) {
1179 ret
= blk_commit_all();
1181 BlockDriverState
*bs
;
1182 AioContext
*aio_context
;
1184 blk
= blk_by_name(device
);
1186 monitor_printf(mon
, "Device '%s' not found\n", device
);
1189 if (!blk_is_available(blk
)) {
1190 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1195 aio_context
= bdrv_get_aio_context(bs
);
1196 aio_context_acquire(aio_context
);
1198 ret
= bdrv_commit(bs
);
1200 aio_context_release(aio_context
);
1203 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1208 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1210 TransactionActionList list
;
1212 list
.value
= action
;
1214 qmp_transaction(&list
, false, NULL
, errp
);
1217 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1218 bool has_node_name
, const char *node_name
,
1219 const char *snapshot_file
,
1220 bool has_snapshot_node_name
,
1221 const char *snapshot_node_name
,
1222 bool has_format
, const char *format
,
1223 bool has_mode
, NewImageMode mode
, Error
**errp
)
1225 BlockdevSnapshotSync snapshot
= {
1226 .has_device
= has_device
,
1227 .device
= (char *) device
,
1228 .has_node_name
= has_node_name
,
1229 .node_name
= (char *) node_name
,
1230 .snapshot_file
= (char *) snapshot_file
,
1231 .has_snapshot_node_name
= has_snapshot_node_name
,
1232 .snapshot_node_name
= (char *) snapshot_node_name
,
1233 .has_format
= has_format
,
1234 .format
= (char *) format
,
1235 .has_mode
= has_mode
,
1238 TransactionAction action
= {
1239 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1240 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1242 blockdev_do_action(&action
, errp
);
1245 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1248 BlockdevSnapshot snapshot_data
= {
1249 .node
= (char *) node
,
1250 .overlay
= (char *) overlay
1252 TransactionAction action
= {
1253 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1254 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1256 blockdev_do_action(&action
, errp
);
1259 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1263 BlockdevSnapshotInternal snapshot
= {
1264 .device
= (char *) device
,
1265 .name
= (char *) name
1267 TransactionAction action
= {
1268 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1269 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1271 blockdev_do_action(&action
, errp
);
1274 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1281 BlockDriverState
*bs
;
1282 AioContext
*aio_context
;
1283 QEMUSnapshotInfo sn
;
1284 Error
*local_err
= NULL
;
1285 SnapshotInfo
*info
= NULL
;
1288 bs
= qmp_get_root_bs(device
, errp
);
1292 aio_context
= bdrv_get_aio_context(bs
);
1293 aio_context_acquire(aio_context
);
1304 error_setg(errp
, "Name or id must be provided");
1305 goto out_aio_context
;
1308 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1309 goto out_aio_context
;
1312 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1314 error_propagate(errp
, local_err
);
1315 goto out_aio_context
;
1319 "Snapshot with id '%s' and name '%s' does not exist on "
1321 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1322 goto out_aio_context
;
1325 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1327 error_propagate(errp
, local_err
);
1328 goto out_aio_context
;
1331 aio_context_release(aio_context
);
1333 info
= g_new0(SnapshotInfo
, 1);
1334 info
->id
= g_strdup(sn
.id_str
);
1335 info
->name
= g_strdup(sn
.name
);
1336 info
->date_nsec
= sn
.date_nsec
;
1337 info
->date_sec
= sn
.date_sec
;
1338 info
->vm_state_size
= sn
.vm_state_size
;
1339 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1340 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1345 aio_context_release(aio_context
);
1350 * block_dirty_bitmap_lookup:
1351 * Return a dirty bitmap (if present), after validating
1352 * the node reference and bitmap names.
1354 * @node: The name of the BDS node to search for bitmaps
1355 * @name: The name of the bitmap to search for
1356 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1357 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1358 * @errp: Output pointer for error information. Can be NULL.
1360 * @return: A bitmap object on success, or NULL on failure.
1362 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1364 BlockDriverState
**pbs
,
1368 BlockDriverState
*bs
;
1369 BdrvDirtyBitmap
*bitmap
;
1370 AioContext
*aio_context
;
1373 error_setg(errp
, "Node cannot be NULL");
1377 error_setg(errp
, "Bitmap name cannot be NULL");
1380 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1382 error_setg(errp
, "Node '%s' not found", node
);
1386 aio_context
= bdrv_get_aio_context(bs
);
1387 aio_context_acquire(aio_context
);
1389 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1391 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1399 *paio
= aio_context
;
1401 aio_context_release(aio_context
);
1407 aio_context_release(aio_context
);
1411 /* New and old BlockDriverState structs for atomic group operations */
1413 typedef struct BlkActionState BlkActionState
;
1417 * Table of operations that define an Action.
1419 * @instance_size: Size of state struct, in bytes.
1420 * @prepare: Prepare the work, must NOT be NULL.
1421 * @commit: Commit the changes, can be NULL.
1422 * @abort: Abort the changes on fail, can be NULL.
1423 * @clean: Clean up resources after all transaction actions have called
1424 * commit() or abort(). Can be NULL.
1426 * Only prepare() may fail. In a single transaction, only one of commit() or
1427 * abort() will be called. clean() will always be called if it is present.
1429 typedef struct BlkActionOps
{
1430 size_t instance_size
;
1431 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1432 void (*commit
)(BlkActionState
*common
);
1433 void (*abort
)(BlkActionState
*common
);
1434 void (*clean
)(BlkActionState
*common
);
1439 * Describes one Action's state within a Transaction.
1441 * @action: QAPI-defined enum identifying which Action to perform.
1442 * @ops: Table of ActionOps this Action can perform.
1443 * @block_job_txn: Transaction which this action belongs to.
1444 * @entry: List membership for all Actions in this Transaction.
1446 * This structure must be arranged as first member in a subclassed type,
1447 * assuming that the compiler will also arrange it to the same offsets as the
1450 struct BlkActionState
{
1451 TransactionAction
*action
;
1452 const BlkActionOps
*ops
;
1453 BlockJobTxn
*block_job_txn
;
1454 TransactionProperties
*txn_props
;
1455 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1458 /* internal snapshot private data */
1459 typedef struct InternalSnapshotState
{
1460 BlkActionState common
;
1461 BlockDriverState
*bs
;
1462 AioContext
*aio_context
;
1463 QEMUSnapshotInfo sn
;
1465 } InternalSnapshotState
;
1468 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1470 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1472 "Action '%s' does not support Transaction property "
1473 "completion-mode = %s",
1474 TransactionActionKind_lookup
[s
->action
->type
],
1475 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1481 static void internal_snapshot_prepare(BlkActionState
*common
,
1484 Error
*local_err
= NULL
;
1487 BlockDriverState
*bs
;
1488 QEMUSnapshotInfo old_sn
, *sn
;
1491 BlockdevSnapshotInternal
*internal
;
1492 InternalSnapshotState
*state
;
1495 g_assert(common
->action
->type
==
1496 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1497 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1498 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1500 /* 1. parse input */
1501 device
= internal
->device
;
1502 name
= internal
->name
;
1504 /* 2. check for validation */
1505 if (action_check_completion_mode(common
, errp
) < 0) {
1509 bs
= qmp_get_root_bs(device
, errp
);
1514 /* AioContext is released in .clean() */
1515 state
->aio_context
= bdrv_get_aio_context(bs
);
1516 aio_context_acquire(state
->aio_context
);
1519 bdrv_drained_begin(bs
);
1521 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1525 if (bdrv_is_read_only(bs
)) {
1526 error_setg(errp
, "Device '%s' is read only", device
);
1530 if (!bdrv_can_snapshot(bs
)) {
1531 error_setg(errp
, "Block format '%s' used by device '%s' "
1532 "does not support internal snapshots",
1533 bs
->drv
->format_name
, device
);
1537 if (!strlen(name
)) {
1538 error_setg(errp
, "Name is empty");
1542 /* check whether a snapshot with name exist */
1543 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1546 error_propagate(errp
, local_err
);
1550 "Snapshot with name '%s' already exists on device '%s'",
1555 /* 3. take the snapshot */
1557 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1558 qemu_gettimeofday(&tv
);
1559 sn
->date_sec
= tv
.tv_sec
;
1560 sn
->date_nsec
= tv
.tv_usec
* 1000;
1561 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1563 ret1
= bdrv_snapshot_create(bs
, sn
);
1565 error_setg_errno(errp
, -ret1
,
1566 "Failed to create snapshot '%s' on device '%s'",
1571 /* 4. succeed, mark a snapshot is created */
1572 state
->created
= true;
1575 static void internal_snapshot_abort(BlkActionState
*common
)
1577 InternalSnapshotState
*state
=
1578 DO_UPCAST(InternalSnapshotState
, common
, common
);
1579 BlockDriverState
*bs
= state
->bs
;
1580 QEMUSnapshotInfo
*sn
= &state
->sn
;
1581 Error
*local_error
= NULL
;
1583 if (!state
->created
) {
1587 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1588 error_reportf_err(local_error
,
1589 "Failed to delete snapshot with id '%s' and "
1590 "name '%s' on device '%s' in abort: ",
1591 sn
->id_str
, sn
->name
,
1592 bdrv_get_device_name(bs
));
1596 static void internal_snapshot_clean(BlkActionState
*common
)
1598 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1601 if (state
->aio_context
) {
1603 bdrv_drained_end(state
->bs
);
1605 aio_context_release(state
->aio_context
);
1609 /* external snapshot private data */
1610 typedef struct ExternalSnapshotState
{
1611 BlkActionState common
;
1612 BlockDriverState
*old_bs
;
1613 BlockDriverState
*new_bs
;
1614 AioContext
*aio_context
;
1615 bool overlay_appended
;
1616 } ExternalSnapshotState
;
1618 static void external_snapshot_prepare(BlkActionState
*common
,
1622 QDict
*options
= NULL
;
1623 Error
*local_err
= NULL
;
1624 /* Device and node name of the image to generate the snapshot from */
1626 const char *node_name
;
1627 /* Reference to the new image (for 'blockdev-snapshot') */
1628 const char *snapshot_ref
;
1629 /* File name of the new image (for 'blockdev-snapshot-sync') */
1630 const char *new_image_file
;
1631 ExternalSnapshotState
*state
=
1632 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1633 TransactionAction
*action
= common
->action
;
1635 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1636 * purpose but a different set of parameters */
1637 switch (action
->type
) {
1638 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1640 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1642 node_name
= s
->node
;
1643 new_image_file
= NULL
;
1644 snapshot_ref
= s
->overlay
;
1647 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1649 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1650 device
= s
->has_device
? s
->device
: NULL
;
1651 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1652 new_image_file
= s
->snapshot_file
;
1653 snapshot_ref
= NULL
;
1657 g_assert_not_reached();
1660 /* start processing */
1661 if (action_check_completion_mode(common
, errp
) < 0) {
1665 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1666 if (!state
->old_bs
) {
1670 /* Acquire AioContext now so any threads operating on old_bs stop */
1671 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1672 aio_context_acquire(state
->aio_context
);
1673 bdrv_drained_begin(state
->old_bs
);
1675 if (!bdrv_is_inserted(state
->old_bs
)) {
1676 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1680 if (bdrv_op_is_blocked(state
->old_bs
,
1681 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1685 if (!bdrv_is_read_only(state
->old_bs
)) {
1686 if (bdrv_flush(state
->old_bs
)) {
1687 error_setg(errp
, QERR_IO_ERROR
);
1692 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1693 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1697 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1698 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1699 const char *format
= s
->has_format
? s
->format
: "qcow2";
1700 enum NewImageMode mode
;
1701 const char *snapshot_node_name
=
1702 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1704 if (node_name
&& !snapshot_node_name
) {
1705 error_setg(errp
, "New snapshot node name missing");
1709 if (snapshot_node_name
&&
1710 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1711 error_setg(errp
, "New snapshot node name already in use");
1715 flags
= state
->old_bs
->open_flags
;
1716 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
| BDRV_O_COPY_ON_READ
);
1718 /* create new image w/backing file */
1719 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1720 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1721 int64_t size
= bdrv_getlength(state
->old_bs
);
1723 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1726 bdrv_img_create(new_image_file
, format
,
1727 state
->old_bs
->filename
,
1728 state
->old_bs
->drv
->format_name
,
1729 NULL
, size
, flags
, false, &local_err
);
1731 error_propagate(errp
, local_err
);
1736 options
= qdict_new();
1737 if (s
->has_snapshot_node_name
) {
1738 qdict_put_str(options
, "node-name", snapshot_node_name
);
1740 qdict_put_str(options
, "driver", format
);
1742 flags
|= BDRV_O_NO_BACKING
;
1745 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1747 /* We will manually add the backing_hd field to the bs later */
1748 if (!state
->new_bs
) {
1752 if (bdrv_has_blk(state
->new_bs
)) {
1753 error_setg(errp
, "The snapshot is already in use");
1757 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1762 if (state
->new_bs
->backing
!= NULL
) {
1763 error_setg(errp
, "The snapshot already has a backing image");
1767 if (!state
->new_bs
->drv
->supports_backing
) {
1768 error_setg(errp
, "The snapshot does not support backing images");
1772 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1774 /* This removes our old bs and adds the new bs. This is an operation that
1775 * can fail, so we need to do it in .prepare; undoing it for abort is
1776 * always possible. */
1777 bdrv_ref(state
->new_bs
);
1778 bdrv_append(state
->new_bs
, state
->old_bs
, &local_err
);
1780 error_propagate(errp
, local_err
);
1783 state
->overlay_appended
= true;
1786 static void external_snapshot_commit(BlkActionState
*common
)
1788 ExternalSnapshotState
*state
=
1789 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1791 /* We don't need (or want) to use the transactional
1792 * bdrv_reopen_multiple() across all the entries at once, because we
1793 * don't want to abort all of them if one of them fails the reopen */
1794 if (!state
->old_bs
->copy_on_read
) {
1795 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1800 static void external_snapshot_abort(BlkActionState
*common
)
1802 ExternalSnapshotState
*state
=
1803 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1804 if (state
->new_bs
) {
1805 if (state
->overlay_appended
) {
1806 bdrv_replace_node(state
->new_bs
, state
->old_bs
, &error_abort
);
1811 static void external_snapshot_clean(BlkActionState
*common
)
1813 ExternalSnapshotState
*state
=
1814 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1815 if (state
->aio_context
) {
1816 bdrv_drained_end(state
->old_bs
);
1817 aio_context_release(state
->aio_context
);
1818 bdrv_unref(state
->new_bs
);
1822 typedef struct DriveBackupState
{
1823 BlkActionState common
;
1824 BlockDriverState
*bs
;
1825 AioContext
*aio_context
;
1829 static BlockJob
*do_drive_backup(DriveBackup
*backup
, BlockJobTxn
*txn
,
1832 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1834 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1835 BlockDriverState
*bs
;
1836 DriveBackup
*backup
;
1837 Error
*local_err
= NULL
;
1839 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1840 backup
= common
->action
->u
.drive_backup
.data
;
1842 bs
= qmp_get_root_bs(backup
->device
, errp
);
1847 /* AioContext is released in .clean() */
1848 state
->aio_context
= bdrv_get_aio_context(bs
);
1849 aio_context_acquire(state
->aio_context
);
1850 bdrv_drained_begin(bs
);
1853 state
->job
= do_drive_backup(backup
, common
->block_job_txn
, &local_err
);
1855 error_propagate(errp
, local_err
);
1860 static void drive_backup_commit(BlkActionState
*common
)
1862 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1864 block_job_start(state
->job
);
1867 static void drive_backup_abort(BlkActionState
*common
)
1869 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1872 block_job_cancel_sync(state
->job
);
1876 static void drive_backup_clean(BlkActionState
*common
)
1878 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1880 if (state
->aio_context
) {
1881 bdrv_drained_end(state
->bs
);
1882 aio_context_release(state
->aio_context
);
1886 typedef struct BlockdevBackupState
{
1887 BlkActionState common
;
1888 BlockDriverState
*bs
;
1890 AioContext
*aio_context
;
1891 } BlockdevBackupState
;
1893 static BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, BlockJobTxn
*txn
,
1896 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1898 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1899 BlockdevBackup
*backup
;
1900 BlockDriverState
*bs
, *target
;
1901 Error
*local_err
= NULL
;
1903 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1904 backup
= common
->action
->u
.blockdev_backup
.data
;
1906 bs
= qmp_get_root_bs(backup
->device
, errp
);
1911 target
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
1916 /* AioContext is released in .clean() */
1917 state
->aio_context
= bdrv_get_aio_context(bs
);
1918 if (state
->aio_context
!= bdrv_get_aio_context(target
)) {
1919 state
->aio_context
= NULL
;
1920 error_setg(errp
, "Backup between two IO threads is not implemented");
1923 aio_context_acquire(state
->aio_context
);
1925 bdrv_drained_begin(state
->bs
);
1927 state
->job
= do_blockdev_backup(backup
, common
->block_job_txn
, &local_err
);
1929 error_propagate(errp
, local_err
);
1934 static void blockdev_backup_commit(BlkActionState
*common
)
1936 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1938 block_job_start(state
->job
);
1941 static void blockdev_backup_abort(BlkActionState
*common
)
1943 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1946 block_job_cancel_sync(state
->job
);
1950 static void blockdev_backup_clean(BlkActionState
*common
)
1952 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1954 if (state
->aio_context
) {
1955 bdrv_drained_end(state
->bs
);
1956 aio_context_release(state
->aio_context
);
1960 typedef struct BlockDirtyBitmapState
{
1961 BlkActionState common
;
1962 BdrvDirtyBitmap
*bitmap
;
1963 BlockDriverState
*bs
;
1964 AioContext
*aio_context
;
1967 } BlockDirtyBitmapState
;
1969 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
1972 Error
*local_err
= NULL
;
1973 BlockDirtyBitmapAdd
*action
;
1974 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1977 if (action_check_completion_mode(common
, errp
) < 0) {
1981 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
1982 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1983 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
1984 action
->has_granularity
, action
->granularity
,
1988 state
->prepared
= true;
1990 error_propagate(errp
, local_err
);
1994 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
1996 BlockDirtyBitmapAdd
*action
;
1997 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2000 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2001 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2002 * then the node reference and bitmap name must have been valid.
2004 if (state
->prepared
) {
2005 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2009 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2012 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2014 BlockDirtyBitmap
*action
;
2016 if (action_check_completion_mode(common
, errp
) < 0) {
2020 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2021 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2024 &state
->aio_context
,
2026 if (!state
->bitmap
) {
2030 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2031 error_setg(errp
, "Cannot modify a frozen bitmap");
2033 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2034 error_setg(errp
, "Cannot clear a disabled bitmap");
2038 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2039 /* AioContext is released in .clean() */
2042 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2044 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2047 if (state
->backup
) {
2048 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2052 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2054 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2057 hbitmap_free(state
->backup
);
2060 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2062 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2065 if (state
->aio_context
) {
2066 aio_context_release(state
->aio_context
);
2070 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2072 error_setg(errp
, "Transaction aborted using Abort action");
2075 static void abort_commit(BlkActionState
*common
)
2077 g_assert_not_reached(); /* this action never succeeds */
2080 static const BlkActionOps actions
[] = {
2081 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2082 .instance_size
= sizeof(ExternalSnapshotState
),
2083 .prepare
= external_snapshot_prepare
,
2084 .commit
= external_snapshot_commit
,
2085 .abort
= external_snapshot_abort
,
2086 .clean
= external_snapshot_clean
,
2088 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2089 .instance_size
= sizeof(ExternalSnapshotState
),
2090 .prepare
= external_snapshot_prepare
,
2091 .commit
= external_snapshot_commit
,
2092 .abort
= external_snapshot_abort
,
2093 .clean
= external_snapshot_clean
,
2095 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2096 .instance_size
= sizeof(DriveBackupState
),
2097 .prepare
= drive_backup_prepare
,
2098 .commit
= drive_backup_commit
,
2099 .abort
= drive_backup_abort
,
2100 .clean
= drive_backup_clean
,
2102 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2103 .instance_size
= sizeof(BlockdevBackupState
),
2104 .prepare
= blockdev_backup_prepare
,
2105 .commit
= blockdev_backup_commit
,
2106 .abort
= blockdev_backup_abort
,
2107 .clean
= blockdev_backup_clean
,
2109 [TRANSACTION_ACTION_KIND_ABORT
] = {
2110 .instance_size
= sizeof(BlkActionState
),
2111 .prepare
= abort_prepare
,
2112 .commit
= abort_commit
,
2114 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2115 .instance_size
= sizeof(InternalSnapshotState
),
2116 .prepare
= internal_snapshot_prepare
,
2117 .abort
= internal_snapshot_abort
,
2118 .clean
= internal_snapshot_clean
,
2120 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2121 .instance_size
= sizeof(BlockDirtyBitmapState
),
2122 .prepare
= block_dirty_bitmap_add_prepare
,
2123 .abort
= block_dirty_bitmap_add_abort
,
2125 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2126 .instance_size
= sizeof(BlockDirtyBitmapState
),
2127 .prepare
= block_dirty_bitmap_clear_prepare
,
2128 .commit
= block_dirty_bitmap_clear_commit
,
2129 .abort
= block_dirty_bitmap_clear_abort
,
2130 .clean
= block_dirty_bitmap_clear_clean
,
2135 * Allocate a TransactionProperties structure if necessary, and fill
2136 * that structure with desired defaults if they are unset.
2138 static TransactionProperties
*get_transaction_properties(
2139 TransactionProperties
*props
)
2142 props
= g_new0(TransactionProperties
, 1);
2145 if (!props
->has_completion_mode
) {
2146 props
->has_completion_mode
= true;
2147 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2154 * 'Atomic' group operations. The operations are performed as a set, and if
2155 * any fail then we roll back all operations in the group.
2157 void qmp_transaction(TransactionActionList
*dev_list
,
2159 struct TransactionProperties
*props
,
2162 TransactionActionList
*dev_entry
= dev_list
;
2163 BlockJobTxn
*block_job_txn
= NULL
;
2164 BlkActionState
*state
, *next
;
2165 Error
*local_err
= NULL
;
2167 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2168 QSIMPLEQ_INIT(&snap_bdrv_states
);
2170 /* Does this transaction get canceled as a group on failure?
2171 * If not, we don't really need to make a BlockJobTxn.
2173 props
= get_transaction_properties(props
);
2174 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2175 block_job_txn
= block_job_txn_new();
2178 /* drain all i/o before any operations */
2181 /* We don't do anything in this loop that commits us to the operations */
2182 while (NULL
!= dev_entry
) {
2183 TransactionAction
*dev_info
= NULL
;
2184 const BlkActionOps
*ops
;
2186 dev_info
= dev_entry
->value
;
2187 dev_entry
= dev_entry
->next
;
2189 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2191 ops
= &actions
[dev_info
->type
];
2192 assert(ops
->instance_size
> 0);
2194 state
= g_malloc0(ops
->instance_size
);
2196 state
->action
= dev_info
;
2197 state
->block_job_txn
= block_job_txn
;
2198 state
->txn_props
= props
;
2199 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2201 state
->ops
->prepare(state
, &local_err
);
2203 error_propagate(errp
, local_err
);
2204 goto delete_and_fail
;
2208 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2209 if (state
->ops
->commit
) {
2210 state
->ops
->commit(state
);
2218 /* failure, and it is all-or-none; roll back all operations */
2219 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2220 if (state
->ops
->abort
) {
2221 state
->ops
->abort(state
);
2225 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2226 if (state
->ops
->clean
) {
2227 state
->ops
->clean(state
);
2232 qapi_free_TransactionProperties(props
);
2234 block_job_txn_unref(block_job_txn
);
2237 void qmp_eject(bool has_device
, const char *device
,
2238 bool has_id
, const char *id
,
2239 bool has_force
, bool force
, Error
**errp
)
2241 Error
*local_err
= NULL
;
2248 rc
= do_open_tray(has_device
? device
: NULL
,
2251 if (rc
&& rc
!= -ENOSYS
) {
2252 error_propagate(errp
, local_err
);
2255 error_free(local_err
);
2257 qmp_x_blockdev_remove_medium(has_device
, device
, has_id
, id
, errp
);
2260 void qmp_block_passwd(bool has_device
, const char *device
,
2261 bool has_node_name
, const char *node_name
,
2262 const char *password
, Error
**errp
)
2264 Error
*local_err
= NULL
;
2265 BlockDriverState
*bs
;
2266 AioContext
*aio_context
;
2268 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2269 has_node_name
? node_name
: NULL
,
2272 error_propagate(errp
, local_err
);
2276 aio_context
= bdrv_get_aio_context(bs
);
2277 aio_context_acquire(aio_context
);
2279 bdrv_add_key(bs
, password
, errp
);
2281 aio_context_release(aio_context
);
2285 * Attempt to open the tray of @device.
2286 * If @force, ignore its tray lock.
2287 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2288 * On error, store an error through @errp and return -errno.
2289 * If @device does not exist, return -ENODEV.
2290 * If it has no removable media, return -ENOTSUP.
2291 * If it has no tray, return -ENOSYS.
2292 * If the guest was asked to open the tray, return -EINPROGRESS.
2295 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
2296 bool force
, Error
**errp
)
2299 const char *device
= qdev_id
?: blk_name
;
2302 blk
= qmp_get_blk(blk_name
, qdev_id
, errp
);
2307 if (!blk_dev_has_removable_media(blk
)) {
2308 error_setg(errp
, "Device '%s' is not removable", device
);
2312 if (!blk_dev_has_tray(blk
)) {
2313 error_setg(errp
, "Device '%s' does not have a tray", device
);
2317 if (blk_dev_is_tray_open(blk
)) {
2321 locked
= blk_dev_is_medium_locked(blk
);
2323 blk_dev_eject_request(blk
, force
);
2326 if (!locked
|| force
) {
2327 blk_dev_change_media_cb(blk
, false, &error_abort
);
2330 if (locked
&& !force
) {
2331 error_setg(errp
, "Device '%s' is locked and force was not specified, "
2332 "wait for tray to open and try again", device
);
2333 return -EINPROGRESS
;
2339 void qmp_blockdev_open_tray(bool has_device
, const char *device
,
2340 bool has_id
, const char *id
,
2341 bool has_force
, bool force
,
2344 Error
*local_err
= NULL
;
2350 rc
= do_open_tray(has_device
? device
: NULL
,
2353 if (rc
&& rc
!= -ENOSYS
&& rc
!= -EINPROGRESS
) {
2354 error_propagate(errp
, local_err
);
2357 error_free(local_err
);
2360 void qmp_blockdev_close_tray(bool has_device
, const char *device
,
2361 bool has_id
, const char *id
,
2365 Error
*local_err
= NULL
;
2367 device
= has_device
? device
: NULL
;
2368 id
= has_id
? id
: NULL
;
2370 blk
= qmp_get_blk(device
, id
, errp
);
2375 if (!blk_dev_has_removable_media(blk
)) {
2376 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2380 if (!blk_dev_has_tray(blk
)) {
2381 /* Ignore this command on tray-less devices */
2385 if (!blk_dev_is_tray_open(blk
)) {
2389 blk_dev_change_media_cb(blk
, true, &local_err
);
2391 error_propagate(errp
, local_err
);
2396 void qmp_x_blockdev_remove_medium(bool has_device
, const char *device
,
2397 bool has_id
, const char *id
, Error
**errp
)
2400 BlockDriverState
*bs
;
2401 AioContext
*aio_context
;
2402 bool has_attached_device
;
2404 device
= has_device
? device
: NULL
;
2405 id
= has_id
? id
: NULL
;
2407 blk
= qmp_get_blk(device
, id
, errp
);
2412 /* For BBs without a device, we can exchange the BDS tree at will */
2413 has_attached_device
= blk_get_attached_dev(blk
);
2415 if (has_attached_device
&& !blk_dev_has_removable_media(blk
)) {
2416 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2420 if (has_attached_device
&& blk_dev_has_tray(blk
) &&
2421 !blk_dev_is_tray_open(blk
))
2423 error_setg(errp
, "Tray of device '%s' is not open", device
?: id
);
2432 aio_context
= bdrv_get_aio_context(bs
);
2433 aio_context_acquire(aio_context
);
2435 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2441 if (!blk_dev_has_tray(blk
)) {
2442 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2443 * called at all); therefore, the medium needs to be ejected here.
2444 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2445 * value passed here (i.e. false). */
2446 blk_dev_change_media_cb(blk
, false, &error_abort
);
2450 aio_context_release(aio_context
);
2453 static void qmp_blockdev_insert_anon_medium(BlockBackend
*blk
,
2454 BlockDriverState
*bs
, Error
**errp
)
2456 Error
*local_err
= NULL
;
2460 /* For BBs without a device, we can exchange the BDS tree at will */
2461 has_device
= blk_get_attached_dev(blk
);
2463 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2464 error_setg(errp
, "Device is not removable");
2468 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2469 error_setg(errp
, "Tray of the device is not open");
2474 error_setg(errp
, "There already is a medium in the device");
2478 ret
= blk_insert_bs(blk
, bs
, errp
);
2483 if (!blk_dev_has_tray(blk
)) {
2484 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2485 * called at all); therefore, the medium needs to be pushed into the
2487 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2488 * value passed here (i.e. true). */
2489 blk_dev_change_media_cb(blk
, true, &local_err
);
2491 error_propagate(errp
, local_err
);
2498 void qmp_x_blockdev_insert_medium(bool has_device
, const char *device
,
2499 bool has_id
, const char *id
,
2500 const char *node_name
, Error
**errp
)
2503 BlockDriverState
*bs
;
2505 blk
= qmp_get_blk(has_device
? device
: NULL
,
2512 bs
= bdrv_find_node(node_name
);
2514 error_setg(errp
, "Node '%s' not found", node_name
);
2518 if (bdrv_has_blk(bs
)) {
2519 error_setg(errp
, "Node '%s' is already in use", node_name
);
2523 qmp_blockdev_insert_anon_medium(blk
, bs
, errp
);
2526 void qmp_blockdev_change_medium(bool has_device
, const char *device
,
2527 bool has_id
, const char *id
,
2528 const char *filename
,
2529 bool has_format
, const char *format
,
2531 BlockdevChangeReadOnlyMode read_only
,
2535 BlockDriverState
*medium_bs
= NULL
;
2539 QDict
*options
= NULL
;
2542 blk
= qmp_get_blk(has_device
? device
: NULL
,
2550 blk_update_root_state(blk
);
2553 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2554 bdrv_flags
&= ~(BDRV_O_TEMPORARY
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
|
2557 if (!has_read_only
) {
2558 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2561 switch (read_only
) {
2562 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2565 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2566 bdrv_flags
&= ~BDRV_O_RDWR
;
2569 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2570 bdrv_flags
|= BDRV_O_RDWR
;
2577 options
= qdict_new();
2578 detect_zeroes
= blk_get_detect_zeroes_from_root_state(blk
);
2579 qdict_put_str(options
, "detect-zeroes", detect_zeroes
? "on" : "off");
2582 qdict_put_str(options
, "driver", format
);
2585 medium_bs
= bdrv_open(filename
, NULL
, options
, bdrv_flags
, errp
);
2590 bdrv_add_key(medium_bs
, NULL
, &err
);
2592 error_propagate(errp
, err
);
2596 rc
= do_open_tray(has_device
? device
: NULL
,
2599 if (rc
&& rc
!= -ENOSYS
) {
2600 error_propagate(errp
, err
);
2606 qmp_x_blockdev_remove_medium(has_device
, device
, has_id
, id
, &err
);
2608 error_propagate(errp
, err
);
2612 qmp_blockdev_insert_anon_medium(blk
, medium_bs
, &err
);
2614 error_propagate(errp
, err
);
2618 qmp_blockdev_close_tray(has_device
, device
, has_id
, id
, errp
);
2621 /* If the medium has been inserted, the device has its own reference, so
2622 * ours must be relinquished; and if it has not been inserted successfully,
2623 * the reference must be relinquished anyway */
2624 bdrv_unref(medium_bs
);
2627 /* throttling disk I/O limits */
2628 void qmp_block_set_io_throttle(BlockIOThrottle
*arg
, Error
**errp
)
2631 BlockDriverState
*bs
;
2633 AioContext
*aio_context
;
2635 blk
= qmp_get_blk(arg
->has_device
? arg
->device
: NULL
,
2636 arg
->has_id
? arg
->id
: NULL
,
2642 aio_context
= blk_get_aio_context(blk
);
2643 aio_context_acquire(aio_context
);
2647 error_setg(errp
, "Device has no medium");
2651 throttle_config_init(&cfg
);
2652 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= arg
->bps
;
2653 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= arg
->bps_rd
;
2654 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= arg
->bps_wr
;
2656 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= arg
->iops
;
2657 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= arg
->iops_rd
;
2658 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= arg
->iops_wr
;
2660 if (arg
->has_bps_max
) {
2661 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= arg
->bps_max
;
2663 if (arg
->has_bps_rd_max
) {
2664 cfg
.buckets
[THROTTLE_BPS_READ
].max
= arg
->bps_rd_max
;
2666 if (arg
->has_bps_wr_max
) {
2667 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= arg
->bps_wr_max
;
2669 if (arg
->has_iops_max
) {
2670 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= arg
->iops_max
;
2672 if (arg
->has_iops_rd_max
) {
2673 cfg
.buckets
[THROTTLE_OPS_READ
].max
= arg
->iops_rd_max
;
2675 if (arg
->has_iops_wr_max
) {
2676 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= arg
->iops_wr_max
;
2679 if (arg
->has_bps_max_length
) {
2680 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
= arg
->bps_max_length
;
2682 if (arg
->has_bps_rd_max_length
) {
2683 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
= arg
->bps_rd_max_length
;
2685 if (arg
->has_bps_wr_max_length
) {
2686 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
= arg
->bps_wr_max_length
;
2688 if (arg
->has_iops_max_length
) {
2689 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
= arg
->iops_max_length
;
2691 if (arg
->has_iops_rd_max_length
) {
2692 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
= arg
->iops_rd_max_length
;
2694 if (arg
->has_iops_wr_max_length
) {
2695 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
= arg
->iops_wr_max_length
;
2698 if (arg
->has_iops_size
) {
2699 cfg
.op_size
= arg
->iops_size
;
2702 if (!throttle_is_valid(&cfg
, errp
)) {
2706 if (throttle_enabled(&cfg
)) {
2707 /* Enable I/O limits if they're not enabled yet, otherwise
2708 * just update the throttling group. */
2709 if (!blk_get_public(blk
)->throttle_state
) {
2710 blk_io_limits_enable(blk
,
2711 arg
->has_group
? arg
->group
:
2712 arg
->has_device
? arg
->device
:
2714 } else if (arg
->has_group
) {
2715 blk_io_limits_update_group(blk
, arg
->group
);
2717 /* Set the new throttling configuration */
2718 blk_set_io_limits(blk
, &cfg
);
2719 } else if (blk_get_public(blk
)->throttle_state
) {
2720 /* If all throttling settings are set to 0, disable I/O limits */
2721 blk_io_limits_disable(blk
);
2725 aio_context_release(aio_context
);
2728 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2729 bool has_granularity
, uint32_t granularity
,
2732 AioContext
*aio_context
;
2733 BlockDriverState
*bs
;
2735 if (!name
|| name
[0] == '\0') {
2736 error_setg(errp
, "Bitmap name cannot be empty");
2740 bs
= bdrv_lookup_bs(node
, node
, errp
);
2745 aio_context
= bdrv_get_aio_context(bs
);
2746 aio_context_acquire(aio_context
);
2748 if (has_granularity
) {
2749 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2750 error_setg(errp
, "Granularity must be power of 2 "
2751 "and at least 512");
2755 /* Default to cluster size, if available: */
2756 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2759 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2762 aio_context_release(aio_context
);
2765 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2768 AioContext
*aio_context
;
2769 BlockDriverState
*bs
;
2770 BdrvDirtyBitmap
*bitmap
;
2772 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2773 if (!bitmap
|| !bs
) {
2777 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2779 "Bitmap '%s' is currently frozen and cannot be removed",
2783 bdrv_dirty_bitmap_make_anon(bitmap
);
2784 bdrv_release_dirty_bitmap(bs
, bitmap
);
2787 aio_context_release(aio_context
);
2791 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2792 * immediately after a full backup operation.
2794 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2797 AioContext
*aio_context
;
2798 BdrvDirtyBitmap
*bitmap
;
2799 BlockDriverState
*bs
;
2801 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2802 if (!bitmap
|| !bs
) {
2806 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2808 "Bitmap '%s' is currently frozen and cannot be modified",
2811 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2813 "Bitmap '%s' is currently disabled and cannot be cleared",
2818 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2821 aio_context_release(aio_context
);
2824 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2826 const char *id
= qdict_get_str(qdict
, "id");
2828 BlockDriverState
*bs
;
2829 AioContext
*aio_context
;
2830 Error
*local_err
= NULL
;
2832 bs
= bdrv_find_node(id
);
2834 qmp_blockdev_del(id
, &local_err
);
2836 error_report_err(local_err
);
2841 blk
= blk_by_name(id
);
2843 error_report("Device '%s' not found", id
);
2847 if (!blk_legacy_dinfo(blk
)) {
2848 error_report("Deleting device added with blockdev-add"
2849 " is not supported");
2853 aio_context
= blk_get_aio_context(blk
);
2854 aio_context_acquire(aio_context
);
2858 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2859 error_report_err(local_err
);
2860 aio_context_release(aio_context
);
2867 /* Make the BlockBackend and the attached BlockDriverState anonymous */
2868 monitor_remove_blk(blk
);
2870 /* If this BlockBackend has a device attached to it, its refcount will be
2871 * decremented when the device is removed; otherwise we have to do so here.
2873 if (blk_get_attached_dev(blk
)) {
2874 /* Further I/O must not pause the guest */
2875 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2876 BLOCKDEV_ON_ERROR_REPORT
);
2881 aio_context_release(aio_context
);
2884 void qmp_block_resize(bool has_device
, const char *device
,
2885 bool has_node_name
, const char *node_name
,
2886 int64_t size
, Error
**errp
)
2888 Error
*local_err
= NULL
;
2889 BlockBackend
*blk
= NULL
;
2890 BlockDriverState
*bs
;
2891 AioContext
*aio_context
;
2894 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2895 has_node_name
? node_name
: NULL
,
2898 error_propagate(errp
, local_err
);
2902 aio_context
= bdrv_get_aio_context(bs
);
2903 aio_context_acquire(aio_context
);
2905 if (!bdrv_is_first_non_filter(bs
)) {
2906 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2911 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2915 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2916 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2920 blk
= blk_new(BLK_PERM_RESIZE
, BLK_PERM_ALL
);
2921 ret
= blk_insert_bs(blk
, bs
, errp
);
2926 bdrv_drained_begin(bs
);
2927 ret
= blk_truncate(blk
, size
, errp
);
2928 bdrv_drained_end(bs
);
2932 aio_context_release(aio_context
);
2935 void qmp_block_stream(bool has_job_id
, const char *job_id
, const char *device
,
2936 bool has_base
, const char *base
,
2937 bool has_base_node
, const char *base_node
,
2938 bool has_backing_file
, const char *backing_file
,
2939 bool has_speed
, int64_t speed
,
2940 bool has_on_error
, BlockdevOnError on_error
,
2943 BlockDriverState
*bs
, *iter
;
2944 BlockDriverState
*base_bs
= NULL
;
2945 AioContext
*aio_context
;
2946 Error
*local_err
= NULL
;
2947 const char *base_name
= NULL
;
2949 if (!has_on_error
) {
2950 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2953 bs
= bdrv_lookup_bs(device
, device
, errp
);
2958 aio_context
= bdrv_get_aio_context(bs
);
2959 aio_context_acquire(aio_context
);
2961 if (has_base
&& has_base_node
) {
2962 error_setg(errp
, "'base' and 'base-node' cannot be specified "
2963 "at the same time");
2968 base_bs
= bdrv_find_backing_image(bs
, base
);
2969 if (base_bs
== NULL
) {
2970 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2973 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2977 if (has_base_node
) {
2978 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
2982 if (bs
== base_bs
|| !bdrv_chain_contains(bs
, base_bs
)) {
2983 error_setg(errp
, "Node '%s' is not a backing image of '%s'",
2987 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2988 base_name
= base_bs
->filename
;
2991 /* Check for op blockers in the whole chain between bs and base */
2992 for (iter
= bs
; iter
&& iter
!= base_bs
; iter
= backing_bs(iter
)) {
2993 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2998 /* if we are streaming the entire chain, the result will have no backing
2999 * file, and specifying one is therefore an error */
3000 if (base_bs
== NULL
&& has_backing_file
) {
3001 error_setg(errp
, "backing file specified, but streaming the "
3006 /* backing_file string overrides base bs filename */
3007 base_name
= has_backing_file
? backing_file
: base_name
;
3009 stream_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, base_name
,
3010 has_speed
? speed
: 0, on_error
, &local_err
);
3012 error_propagate(errp
, local_err
);
3016 trace_qmp_block_stream(bs
, bs
->job
);
3019 aio_context_release(aio_context
);
3022 void qmp_block_commit(bool has_job_id
, const char *job_id
, const char *device
,
3023 bool has_base
, const char *base
,
3024 bool has_top
, const char *top
,
3025 bool has_backing_file
, const char *backing_file
,
3026 bool has_speed
, int64_t speed
,
3027 bool has_filter_node_name
, const char *filter_node_name
,
3030 BlockDriverState
*bs
;
3031 BlockDriverState
*iter
;
3032 BlockDriverState
*base_bs
, *top_bs
;
3033 AioContext
*aio_context
;
3034 Error
*local_err
= NULL
;
3035 /* This will be part of the QMP command, if/when the
3036 * BlockdevOnError change for blkmirror makes it in
3038 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3043 if (!has_filter_node_name
) {
3044 filter_node_name
= NULL
;
3048 * libvirt relies on the DeviceNotFound error class in order to probe for
3049 * live commit feature versions; for this to work, we must make sure to
3050 * perform the device lookup before any generic errors that may occur in a
3051 * scenario in which all optional arguments are omitted. */
3052 bs
= qmp_get_root_bs(device
, &local_err
);
3054 bs
= bdrv_lookup_bs(device
, device
, NULL
);
3056 error_free(local_err
);
3057 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3058 "Device '%s' not found", device
);
3060 error_propagate(errp
, local_err
);
3065 aio_context
= bdrv_get_aio_context(bs
);
3066 aio_context_acquire(aio_context
);
3068 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3072 /* default top_bs is the active layer */
3075 if (has_top
&& top
) {
3076 if (strcmp(bs
->filename
, top
) != 0) {
3077 top_bs
= bdrv_find_backing_image(bs
, top
);
3081 if (top_bs
== NULL
) {
3082 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3086 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3088 if (has_base
&& base
) {
3089 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3091 base_bs
= bdrv_find_base(top_bs
);
3094 if (base_bs
== NULL
) {
3095 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3099 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3101 for (iter
= top_bs
; iter
!= backing_bs(base_bs
); iter
= backing_bs(iter
)) {
3102 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3107 /* Do not allow attempts to commit an image into itself */
3108 if (top_bs
== base_bs
) {
3109 error_setg(errp
, "cannot commit an image into itself");
3114 if (has_backing_file
) {
3115 error_setg(errp
, "'backing-file' specified,"
3116 " but 'top' is the active layer");
3119 commit_active_start(has_job_id
? job_id
: NULL
, bs
, base_bs
,
3120 BLOCK_JOB_DEFAULT
, speed
, on_error
,
3121 filter_node_name
, NULL
, NULL
, false, &local_err
);
3123 BlockDriverState
*overlay_bs
= bdrv_find_overlay(bs
, top_bs
);
3124 if (bdrv_op_is_blocked(overlay_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3127 commit_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, top_bs
, speed
,
3128 on_error
, has_backing_file
? backing_file
: NULL
,
3129 filter_node_name
, &local_err
);
3131 if (local_err
!= NULL
) {
3132 error_propagate(errp
, local_err
);
3137 aio_context_release(aio_context
);
3140 static BlockJob
*do_drive_backup(DriveBackup
*backup
, BlockJobTxn
*txn
,
3143 BlockDriverState
*bs
;
3144 BlockDriverState
*target_bs
;
3145 BlockDriverState
*source
= NULL
;
3146 BlockJob
*job
= NULL
;
3147 BdrvDirtyBitmap
*bmap
= NULL
;
3148 AioContext
*aio_context
;
3149 QDict
*options
= NULL
;
3150 Error
*local_err
= NULL
;
3153 bool set_backing_hd
= false;
3155 if (!backup
->has_speed
) {
3158 if (!backup
->has_on_source_error
) {
3159 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3161 if (!backup
->has_on_target_error
) {
3162 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3164 if (!backup
->has_mode
) {
3165 backup
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3167 if (!backup
->has_job_id
) {
3168 backup
->job_id
= NULL
;
3170 if (!backup
->has_compress
) {
3171 backup
->compress
= false;
3174 bs
= qmp_get_root_bs(backup
->device
, errp
);
3179 aio_context
= bdrv_get_aio_context(bs
);
3180 aio_context_acquire(aio_context
);
3182 if (!backup
->has_format
) {
3183 backup
->format
= backup
->mode
== NEW_IMAGE_MODE_EXISTING
?
3184 NULL
: (char*) bs
->drv
->format_name
;
3187 /* Early check to avoid creating target */
3188 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3192 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3194 /* See if we have a backing HD we can use to create our new image
3196 if (backup
->sync
== MIRROR_SYNC_MODE_TOP
) {
3197 source
= backing_bs(bs
);
3199 backup
->sync
= MIRROR_SYNC_MODE_FULL
;
3202 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
3204 flags
|= BDRV_O_NO_BACKING
;
3205 set_backing_hd
= true;
3208 size
= bdrv_getlength(bs
);
3210 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3214 if (backup
->mode
!= NEW_IMAGE_MODE_EXISTING
) {
3215 assert(backup
->format
);
3217 bdrv_img_create(backup
->target
, backup
->format
, source
->filename
,
3218 source
->drv
->format_name
, NULL
,
3219 size
, flags
, false, &local_err
);
3221 bdrv_img_create(backup
->target
, backup
->format
, NULL
, NULL
, NULL
,
3222 size
, flags
, false, &local_err
);
3227 error_propagate(errp
, local_err
);
3231 if (backup
->format
) {
3233 options
= qdict_new();
3235 qdict_put_str(options
, "driver", backup
->format
);
3238 target_bs
= bdrv_open(backup
->target
, NULL
, options
, flags
, errp
);
3243 bdrv_set_aio_context(target_bs
, aio_context
);
3245 if (set_backing_hd
) {
3246 bdrv_set_backing_hd(target_bs
, source
, &local_err
);
3248 bdrv_unref(target_bs
);
3253 if (backup
->has_bitmap
) {
3254 bmap
= bdrv_find_dirty_bitmap(bs
, backup
->bitmap
);
3256 error_setg(errp
, "Bitmap '%s' could not be found", backup
->bitmap
);
3257 bdrv_unref(target_bs
);
3262 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3263 backup
->sync
, bmap
, backup
->compress
,
3264 backup
->on_source_error
, backup
->on_target_error
,
3265 BLOCK_JOB_DEFAULT
, NULL
, NULL
, txn
, &local_err
);
3266 bdrv_unref(target_bs
);
3267 if (local_err
!= NULL
) {
3268 error_propagate(errp
, local_err
);
3273 aio_context_release(aio_context
);
3277 void qmp_drive_backup(DriveBackup
*arg
, Error
**errp
)
3281 job
= do_drive_backup(arg
, NULL
, errp
);
3283 block_job_start(job
);
3287 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3289 return bdrv_named_nodes_list(errp
);
3292 BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, BlockJobTxn
*txn
,
3295 BlockDriverState
*bs
;
3296 BlockDriverState
*target_bs
;
3297 Error
*local_err
= NULL
;
3298 AioContext
*aio_context
;
3299 BlockJob
*job
= NULL
;
3301 if (!backup
->has_speed
) {
3304 if (!backup
->has_on_source_error
) {
3305 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3307 if (!backup
->has_on_target_error
) {
3308 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3310 if (!backup
->has_job_id
) {
3311 backup
->job_id
= NULL
;
3313 if (!backup
->has_compress
) {
3314 backup
->compress
= false;
3317 bs
= qmp_get_root_bs(backup
->device
, errp
);
3322 aio_context
= bdrv_get_aio_context(bs
);
3323 aio_context_acquire(aio_context
);
3325 target_bs
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
3330 if (bdrv_get_aio_context(target_bs
) != aio_context
) {
3331 if (!bdrv_has_blk(target_bs
)) {
3332 /* The target BDS is not attached, we can safely move it to another
3334 bdrv_set_aio_context(target_bs
, aio_context
);
3336 error_setg(errp
, "Target is attached to a different thread from "
3341 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3342 backup
->sync
, NULL
, backup
->compress
,
3343 backup
->on_source_error
, backup
->on_target_error
,
3344 BLOCK_JOB_DEFAULT
, NULL
, NULL
, txn
, &local_err
);
3345 if (local_err
!= NULL
) {
3346 error_propagate(errp
, local_err
);
3349 aio_context_release(aio_context
);
3353 void qmp_blockdev_backup(BlockdevBackup
*arg
, Error
**errp
)
3356 job
= do_blockdev_backup(arg
, NULL
, errp
);
3358 block_job_start(job
);
3362 /* Parameter check and block job starting for drive mirroring.
3363 * Caller should hold @device and @target's aio context (must be the same).
3365 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
3366 BlockDriverState
*target
,
3367 bool has_replaces
, const char *replaces
,
3368 enum MirrorSyncMode sync
,
3369 BlockMirrorBackingMode backing_mode
,
3370 bool has_speed
, int64_t speed
,
3371 bool has_granularity
, uint32_t granularity
,
3372 bool has_buf_size
, int64_t buf_size
,
3373 bool has_on_source_error
,
3374 BlockdevOnError on_source_error
,
3375 bool has_on_target_error
,
3376 BlockdevOnError on_target_error
,
3377 bool has_unmap
, bool unmap
,
3378 bool has_filter_node_name
,
3379 const char *filter_node_name
,
3386 if (!has_on_source_error
) {
3387 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3389 if (!has_on_target_error
) {
3390 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3392 if (!has_granularity
) {
3395 if (!has_buf_size
) {
3401 if (!has_filter_node_name
) {
3402 filter_node_name
= NULL
;
3405 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3406 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3407 "a value in range [512B, 64MB]");
3410 if (granularity
& (granularity
- 1)) {
3411 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3416 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3419 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3423 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3424 sync
= MIRROR_SYNC_MODE_FULL
;
3427 /* pass the node name to replace to mirror start since it's loose coupling
3428 * and will allow to check whether the node still exist at mirror completion
3430 mirror_start(job_id
, bs
, target
,
3431 has_replaces
? replaces
: NULL
,
3432 speed
, granularity
, buf_size
, sync
, backing_mode
,
3433 on_source_error
, on_target_error
, unmap
, filter_node_name
,
3437 void qmp_drive_mirror(DriveMirror
*arg
, Error
**errp
)
3439 BlockDriverState
*bs
;
3440 BlockDriverState
*source
, *target_bs
;
3441 AioContext
*aio_context
;
3442 BlockMirrorBackingMode backing_mode
;
3443 Error
*local_err
= NULL
;
3444 QDict
*options
= NULL
;
3447 const char *format
= arg
->format
;
3449 bs
= qmp_get_root_bs(arg
->device
, errp
);
3454 aio_context
= bdrv_get_aio_context(bs
);
3455 aio_context_acquire(aio_context
);
3457 if (!arg
->has_mode
) {
3458 arg
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3461 if (!arg
->has_format
) {
3462 format
= (arg
->mode
== NEW_IMAGE_MODE_EXISTING
3463 ? NULL
: bs
->drv
->format_name
);
3466 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3467 source
= backing_bs(bs
);
3468 if (!source
&& arg
->sync
== MIRROR_SYNC_MODE_TOP
) {
3469 arg
->sync
= MIRROR_SYNC_MODE_FULL
;
3471 if (arg
->sync
== MIRROR_SYNC_MODE_NONE
) {
3475 size
= bdrv_getlength(bs
);
3477 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3481 if (arg
->has_replaces
) {
3482 BlockDriverState
*to_replace_bs
;
3483 AioContext
*replace_aio_context
;
3484 int64_t replace_size
;
3486 if (!arg
->has_node_name
) {
3487 error_setg(errp
, "a node-name must be provided when replacing a"
3488 " named node of the graph");
3492 to_replace_bs
= check_to_replace_node(bs
, arg
->replaces
, &local_err
);
3494 if (!to_replace_bs
) {
3495 error_propagate(errp
, local_err
);
3499 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3500 aio_context_acquire(replace_aio_context
);
3501 replace_size
= bdrv_getlength(to_replace_bs
);
3502 aio_context_release(replace_aio_context
);
3504 if (size
!= replace_size
) {
3505 error_setg(errp
, "cannot replace image with a mirror image of "
3511 if (arg
->mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3512 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3514 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3517 if ((arg
->sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3518 && arg
->mode
!= NEW_IMAGE_MODE_EXISTING
)
3520 /* create new image w/o backing file */
3522 bdrv_img_create(arg
->target
, format
,
3523 NULL
, NULL
, NULL
, size
, flags
, false, &local_err
);
3525 switch (arg
->mode
) {
3526 case NEW_IMAGE_MODE_EXISTING
:
3528 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3529 /* create new image with backing file */
3530 bdrv_img_create(arg
->target
, format
,
3532 source
->drv
->format_name
,
3533 NULL
, size
, flags
, false, &local_err
);
3541 error_propagate(errp
, local_err
);
3545 options
= qdict_new();
3546 if (arg
->has_node_name
) {
3547 qdict_put_str(options
, "node-name", arg
->node_name
);
3550 qdict_put_str(options
, "driver", format
);
3553 /* Mirroring takes care of copy-on-write using the source's backing
3556 target_bs
= bdrv_open(arg
->target
, NULL
, options
,
3557 flags
| BDRV_O_NO_BACKING
, errp
);
3562 bdrv_set_aio_context(target_bs
, aio_context
);
3564 blockdev_mirror_common(arg
->has_job_id
? arg
->job_id
: NULL
, bs
, target_bs
,
3565 arg
->has_replaces
, arg
->replaces
, arg
->sync
,
3566 backing_mode
, arg
->has_speed
, arg
->speed
,
3567 arg
->has_granularity
, arg
->granularity
,
3568 arg
->has_buf_size
, arg
->buf_size
,
3569 arg
->has_on_source_error
, arg
->on_source_error
,
3570 arg
->has_on_target_error
, arg
->on_target_error
,
3571 arg
->has_unmap
, arg
->unmap
,
3574 bdrv_unref(target_bs
);
3575 error_propagate(errp
, local_err
);
3577 aio_context_release(aio_context
);
3580 void qmp_blockdev_mirror(bool has_job_id
, const char *job_id
,
3581 const char *device
, const char *target
,
3582 bool has_replaces
, const char *replaces
,
3583 MirrorSyncMode sync
,
3584 bool has_speed
, int64_t speed
,
3585 bool has_granularity
, uint32_t granularity
,
3586 bool has_buf_size
, int64_t buf_size
,
3587 bool has_on_source_error
,
3588 BlockdevOnError on_source_error
,
3589 bool has_on_target_error
,
3590 BlockdevOnError on_target_error
,
3591 bool has_filter_node_name
,
3592 const char *filter_node_name
,
3595 BlockDriverState
*bs
;
3596 BlockDriverState
*target_bs
;
3597 AioContext
*aio_context
;
3598 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3599 Error
*local_err
= NULL
;
3601 bs
= qmp_get_root_bs(device
, errp
);
3606 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3611 aio_context
= bdrv_get_aio_context(bs
);
3612 aio_context_acquire(aio_context
);
3614 bdrv_set_aio_context(target_bs
, aio_context
);
3616 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3617 has_replaces
, replaces
, sync
, backing_mode
,
3619 has_granularity
, granularity
,
3620 has_buf_size
, buf_size
,
3621 has_on_source_error
, on_source_error
,
3622 has_on_target_error
, on_target_error
,
3624 has_filter_node_name
, filter_node_name
,
3626 error_propagate(errp
, local_err
);
3628 aio_context_release(aio_context
);
3631 /* Get a block job using its ID and acquire its AioContext */
3632 static BlockJob
*find_block_job(const char *id
, AioContext
**aio_context
,
3639 *aio_context
= NULL
;
3641 job
= block_job_get(id
);
3644 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3645 "Block job '%s' not found", id
);
3649 *aio_context
= blk_get_aio_context(job
->blk
);
3650 aio_context_acquire(*aio_context
);
3655 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3657 AioContext
*aio_context
;
3658 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3664 block_job_set_speed(job
, speed
, errp
);
3665 aio_context_release(aio_context
);
3668 void qmp_block_job_cancel(const char *device
,
3669 bool has_force
, bool force
, Error
**errp
)
3671 AioContext
*aio_context
;
3672 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3682 if (block_job_user_paused(job
) && !force
) {
3683 error_setg(errp
, "The block job for device '%s' is currently paused",
3688 trace_qmp_block_job_cancel(job
);
3689 block_job_cancel(job
);
3691 aio_context_release(aio_context
);
3694 void qmp_block_job_pause(const char *device
, Error
**errp
)
3696 AioContext
*aio_context
;
3697 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3699 if (!job
|| block_job_user_paused(job
)) {
3703 trace_qmp_block_job_pause(job
);
3704 block_job_user_pause(job
);
3705 aio_context_release(aio_context
);
3708 void qmp_block_job_resume(const char *device
, Error
**errp
)
3710 AioContext
*aio_context
;
3711 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3713 if (!job
|| !block_job_user_paused(job
)) {
3717 trace_qmp_block_job_resume(job
);
3718 block_job_user_resume(job
);
3719 aio_context_release(aio_context
);
3722 void qmp_block_job_complete(const char *device
, Error
**errp
)
3724 AioContext
*aio_context
;
3725 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3731 trace_qmp_block_job_complete(job
);
3732 block_job_complete(job
, errp
);
3733 aio_context_release(aio_context
);
3736 void qmp_change_backing_file(const char *device
,
3737 const char *image_node_name
,
3738 const char *backing_file
,
3741 BlockDriverState
*bs
= NULL
;
3742 AioContext
*aio_context
;
3743 BlockDriverState
*image_bs
= NULL
;
3744 Error
*local_err
= NULL
;
3749 bs
= qmp_get_root_bs(device
, errp
);
3754 aio_context
= bdrv_get_aio_context(bs
);
3755 aio_context_acquire(aio_context
);
3757 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3759 error_propagate(errp
, local_err
);
3764 error_setg(errp
, "image file not found");
3768 if (bdrv_find_base(image_bs
) == image_bs
) {
3769 error_setg(errp
, "not allowing backing file change on an image "
3770 "without a backing file");
3774 /* even though we are not necessarily operating on bs, we need it to
3775 * determine if block ops are currently prohibited on the chain */
3776 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3780 /* final sanity check */
3781 if (!bdrv_chain_contains(bs
, image_bs
)) {
3782 error_setg(errp
, "'%s' and image file are not in the same chain",
3787 /* if not r/w, reopen to make r/w */
3788 open_flags
= image_bs
->open_flags
;
3789 ro
= bdrv_is_read_only(image_bs
);
3792 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3794 error_propagate(errp
, local_err
);
3799 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3800 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3803 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3805 /* don't exit here, so we can try to restore open flags if
3810 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3811 error_propagate(errp
, local_err
);
3815 aio_context_release(aio_context
);
3818 void hmp_drive_add_node(Monitor
*mon
, const char *optstr
)
3822 Error
*local_err
= NULL
;
3824 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, optstr
, false);
3829 qdict
= qemu_opts_to_qdict(opts
, NULL
);
3831 if (!qdict_get_try_str(qdict
, "node-name")) {
3833 error_report("'node-name' needs to be specified");
3837 BlockDriverState
*bs
= bds_tree_init(qdict
, &local_err
);
3839 error_report_err(local_err
);
3843 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3846 qemu_opts_del(opts
);
3849 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3851 BlockDriverState
*bs
;
3853 Visitor
*v
= qobject_output_visitor_new(&obj
);
3855 Error
*local_err
= NULL
;
3857 visit_type_BlockdevOptions(v
, NULL
, &options
, &local_err
);
3859 error_propagate(errp
, local_err
);
3863 visit_complete(v
, &obj
);
3864 qdict
= qobject_to_qdict(obj
);
3866 qdict_flatten(qdict
);
3868 if (!qdict_get_try_str(qdict
, "node-name")) {
3869 error_setg(errp
, "'node-name' must be specified for the root node");
3873 bs
= bds_tree_init(qdict
, errp
);
3878 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3880 if (bs
&& bdrv_key_required(bs
)) {
3881 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3883 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3891 void qmp_blockdev_del(const char *node_name
, Error
**errp
)
3893 AioContext
*aio_context
;
3894 BlockDriverState
*bs
;
3896 bs
= bdrv_find_node(node_name
);
3898 error_setg(errp
, "Cannot find node %s", node_name
);
3901 if (bdrv_has_blk(bs
)) {
3902 error_setg(errp
, "Node %s is in use", node_name
);
3905 aio_context
= bdrv_get_aio_context(bs
);
3906 aio_context_acquire(aio_context
);
3908 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3912 if (!bs
->monitor_list
.tqe_prev
) {
3913 error_setg(errp
, "Node %s is not owned by the monitor",
3918 if (bs
->refcnt
> 1) {
3919 error_setg(errp
, "Block device %s is in use",
3920 bdrv_get_device_or_node_name(bs
));
3924 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3928 aio_context_release(aio_context
);
3931 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
3932 const char *child_name
)
3936 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
3937 if (strcmp(child
->name
, child_name
) == 0) {
3945 void qmp_x_blockdev_change(const char *parent
, bool has_child
,
3946 const char *child
, bool has_node
,
3947 const char *node
, Error
**errp
)
3949 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
3952 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
3957 if (has_child
== has_node
) {
3959 error_setg(errp
, "The parameters child and node are in conflict");
3961 error_setg(errp
, "Either child or node must be specified");
3967 p_child
= bdrv_find_child(parent_bs
, child
);
3969 error_setg(errp
, "Node '%s' does not have child '%s'",
3973 bdrv_del_child(parent_bs
, p_child
, errp
);
3977 new_bs
= bdrv_find_node(node
);
3979 error_setg(errp
, "Node '%s' not found", node
);
3982 bdrv_add_child(parent_bs
, new_bs
, errp
);
3986 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3988 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3991 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
3992 BlockJobInfoList
*elem
;
3993 AioContext
*aio_context
;
3995 if (block_job_is_internal(job
)) {
3998 elem
= g_new0(BlockJobInfoList
, 1);
3999 aio_context
= blk_get_aio_context(job
->blk
);
4000 aio_context_acquire(aio_context
);
4001 elem
->value
= block_job_query(job
, errp
);
4002 aio_context_release(aio_context
);
4005 qapi_free_BlockJobInfoList(head
);
4009 p_next
= &elem
->next
;
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
= "host AIO implementation (threads, native)",
4028 .name
= BDRV_OPT_CACHE_WB
,
4029 .type
= QEMU_OPT_BOOL
,
4030 .help
= "Enable writeback mode",
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
= BDRV_OPT_READ_ONLY
,
4045 .type
= QEMU_OPT_BOOL
,
4046 .help
= "open drive file as read-only",
4052 .name
= "throttling.group",
4053 .type
= QEMU_OPT_STRING
,
4054 .help
= "name of the block throttling group",
4056 .name
= "copy-on-read",
4057 .type
= QEMU_OPT_BOOL
,
4058 .help
= "copy read data from backing file into image file",
4060 .name
= "detect-zeroes",
4061 .type
= QEMU_OPT_STRING
,
4062 .help
= "try to optimize zero writes (off, on, unmap)",
4064 .name
= "stats-account-invalid",
4065 .type
= QEMU_OPT_BOOL
,
4066 .help
= "whether to account for invalid I/O operations "
4067 "in the statistics",
4069 .name
= "stats-account-failed",
4070 .type
= QEMU_OPT_BOOL
,
4071 .help
= "whether to account for failed I/O operations "
4072 "in the statistics",
4074 { /* end of list */ }
4078 QemuOptsList qemu_drive_opts
= {
4080 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4083 * no elements => accept any params
4084 * validation will happen later
4086 { /* end of list */ }