2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
9 * This file incorporates work covered by the following copyright and
12 * Copyright (c) 2003-2008 Fabrice Bellard
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "qemu/osdep.h"
34 #include "sysemu/block-backend.h"
35 #include "sysemu/blockdev.h"
36 #include "hw/block/block.h"
37 #include "block/blockjob.h"
38 #include "block/throttle-groups.h"
39 #include "monitor/monitor.h"
40 #include "qemu/error-report.h"
41 #include "qemu/option.h"
42 #include "qemu/config-file.h"
43 #include "qapi/qmp/types.h"
44 #include "qapi-visit.h"
45 #include "qapi/qmp/qerror.h"
46 #include "qapi/qmp-output-visitor.h"
47 #include "qapi/util.h"
48 #include "sysemu/sysemu.h"
49 #include "block/block_int.h"
50 #include "qmp-commands.h"
52 #include "sysemu/arch_init.h"
54 static QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
55 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
57 static const char *const if_name
[IF_COUNT
] = {
61 [IF_FLOPPY
] = "floppy",
62 [IF_PFLASH
] = "pflash",
65 [IF_VIRTIO
] = "virtio",
69 static int if_max_devs
[IF_COUNT
] = {
71 * Do not change these numbers! They govern how drive option
72 * index maps to unit and bus. That mapping is ABI.
74 * All controllers used to imlement if=T drives need to support
75 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
76 * Otherwise, some index values map to "impossible" bus, unit
79 * For instance, if you change [IF_SCSI] to 255, -drive
80 * if=scsi,index=12 no longer means bus=1,unit=5, but
81 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
82 * the drive can't be set up. Regression.
89 * Boards may call this to offer board-by-board overrides
90 * of the default, global values.
92 void override_max_devs(BlockInterfaceType type
, int max_devs
)
101 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
102 dinfo
= blk_legacy_dinfo(blk
);
103 if (dinfo
->type
== type
) {
104 fprintf(stderr
, "Cannot override units-per-bus property of"
105 " the %s interface, because a drive of that type has"
106 " already been added.\n", if_name
[type
]);
107 g_assert_not_reached();
111 if_max_devs
[type
] = max_devs
;
115 * We automatically delete the drive when a device using it gets
116 * unplugged. Questionable feature, but we can't just drop it.
117 * Device models call blockdev_mark_auto_del() to schedule the
118 * automatic deletion, and generic qdev code calls blockdev_auto_del()
119 * when deletion is actually safe.
121 void blockdev_mark_auto_del(BlockBackend
*blk
)
123 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
124 BlockDriverState
*bs
= blk_bs(blk
);
125 AioContext
*aio_context
;
132 aio_context
= bdrv_get_aio_context(bs
);
133 aio_context_acquire(aio_context
);
136 block_job_cancel(bs
->job
);
139 aio_context_release(aio_context
);
145 void blockdev_auto_del(BlockBackend
*blk
)
147 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
149 if (dinfo
&& dinfo
->auto_del
) {
155 * Returns the current mapping of how many units per bus
156 * a particular interface can support.
158 * A positive integer indicates n units per bus.
159 * 0 implies the mapping has not been established.
160 * -1 indicates an invalid BlockInterfaceType was given.
162 int drive_get_max_devs(BlockInterfaceType type
)
164 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
165 return if_max_devs
[type
];
171 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
173 int max_devs
= if_max_devs
[type
];
174 return max_devs
? index
/ max_devs
: 0;
177 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
179 int max_devs
= if_max_devs
[type
];
180 return max_devs
? index
% max_devs
: index
;
183 QemuOpts
*drive_def(const char *optstr
)
185 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
188 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
193 opts
= drive_def(optstr
);
197 if (type
!= IF_DEFAULT
) {
198 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
201 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
204 qemu_opt_set(opts
, "file", file
, &error_abort
);
208 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
213 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
214 dinfo
= blk_legacy_dinfo(blk
);
215 if (dinfo
&& dinfo
->type
== type
216 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
224 bool drive_check_orphaned(void)
230 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
231 dinfo
= blk_legacy_dinfo(blk
);
232 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
233 /* Unless this is a default drive, this may be an oversight. */
234 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
235 dinfo
->type
!= IF_NONE
) {
236 fprintf(stderr
, "Warning: Orphaned drive without device: "
237 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
238 blk_name(blk
), blk_bs(blk
) ? blk_bs(blk
)->filename
: "",
239 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
247 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
249 return drive_get(type
,
250 drive_index_to_bus_id(type
, index
),
251 drive_index_to_unit_id(type
, index
));
254 int drive_get_max_bus(BlockInterfaceType type
)
261 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
262 dinfo
= blk_legacy_dinfo(blk
);
263 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
264 max_bus
= dinfo
->bus
;
270 /* Get a block device. This should only be used for single-drive devices
271 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
273 DriveInfo
*drive_get_next(BlockInterfaceType type
)
275 static int next_block_unit
[IF_COUNT
];
277 return drive_get(type
, 0, next_block_unit
[type
]++);
280 static void bdrv_format_print(void *opaque
, const char *name
)
282 error_printf(" %s", name
);
287 BlockDriverState
*bs
;
290 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
292 if (!strcmp(buf
, "ignore")) {
293 return BLOCKDEV_ON_ERROR_IGNORE
;
294 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
295 return BLOCKDEV_ON_ERROR_ENOSPC
;
296 } else if (!strcmp(buf
, "stop")) {
297 return BLOCKDEV_ON_ERROR_STOP
;
298 } else if (!strcmp(buf
, "report")) {
299 return BLOCKDEV_ON_ERROR_REPORT
;
301 error_setg(errp
, "'%s' invalid %s error action",
302 buf
, is_read
? "read" : "write");
307 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
310 const QListEntry
*entry
;
311 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
312 switch (qobject_type(entry
->value
)) {
314 case QTYPE_QSTRING
: {
315 unsigned long long length
;
316 const char *str
= qstring_get_str(qobject_to_qstring(entry
->value
));
317 if (parse_uint_full(str
, &length
, 10) == 0 &&
318 length
> 0 && length
<= UINT_MAX
) {
319 block_acct_add_interval(stats
, (unsigned) length
);
321 error_setg(errp
, "Invalid interval length: %s", str
);
328 int64_t length
= qint_get_int(qobject_to_qint(entry
->value
));
329 if (length
> 0 && length
<= UINT_MAX
) {
330 block_acct_add_interval(stats
, (unsigned) length
);
332 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
339 error_setg(errp
, "The specification of stats-intervals is invalid");
346 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
348 if (throttle_conflicting(cfg
, errp
)) {
352 if (!throttle_is_valid(cfg
, errp
)) {
356 if (throttle_max_is_missing_limit(cfg
, errp
)) {
363 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
365 /* All parameters but @opts are optional and may be set to NULL. */
366 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
367 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
368 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
371 Error
*local_error
= NULL
;
375 if (!qemu_opt_get_bool(opts
, "read-only", false)) {
376 *bdrv_flags
|= BDRV_O_RDWR
;
378 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
379 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
382 if ((discard
= qemu_opt_get(opts
, "discard")) != NULL
) {
383 if (bdrv_parse_discard_flags(discard
, bdrv_flags
) != 0) {
384 error_setg(errp
, "Invalid discard option");
389 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
390 if (!strcmp(aio
, "native")) {
391 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
392 } else if (!strcmp(aio
, "threads")) {
393 /* this is the default */
395 error_setg(errp
, "invalid aio option");
401 /* disk I/O throttling */
402 if (throttling_group
) {
403 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
407 memset(throttle_cfg
, 0, sizeof(*throttle_cfg
));
408 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
409 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
410 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
411 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
412 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
413 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
414 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
415 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
416 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
417 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
418 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
419 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
421 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
422 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
423 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
424 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
425 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
426 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
427 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
428 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
429 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
430 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
431 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
432 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
434 throttle_cfg
->op_size
=
435 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
437 if (!check_throttle_config(throttle_cfg
, errp
)) {
444 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
445 qemu_opt_get(opts
, "detect-zeroes"),
446 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX
,
447 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
450 error_propagate(errp
, local_error
);
455 *detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
456 !(*bdrv_flags
& BDRV_O_UNMAP
))
458 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
459 "without setting discard operation to unmap");
465 /* Takes the ownership of bs_opts */
466 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
471 int on_read_error
, on_write_error
;
472 bool account_invalid
, account_failed
;
474 BlockDriverState
*bs
;
479 QDict
*interval_dict
= NULL
;
480 QList
*interval_list
= NULL
;
482 BlockdevDetectZeroesOptions detect_zeroes
=
483 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
484 const char *throttling_group
= NULL
;
486 /* Check common options by copying from bs_opts to opts, all other options
487 * stay in bs_opts for processing by bdrv_open(). */
488 id
= qdict_get_try_str(bs_opts
, "id");
489 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
491 error_propagate(errp
, error
);
495 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
497 error_propagate(errp
, error
);
502 qdict_del(bs_opts
, "id");
505 /* extract parameters */
506 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
508 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
509 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
511 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
512 qdict_array_split(interval_dict
, &interval_list
);
514 if (qdict_size(interval_dict
) != 0) {
515 error_setg(errp
, "Invalid option stats-intervals.%s",
516 qdict_first(interval_dict
)->key
);
520 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
521 &detect_zeroes
, &error
);
523 error_propagate(errp
, error
);
527 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
528 if (is_help_option(buf
)) {
529 error_printf("Supported formats:");
530 bdrv_iterate_format(bdrv_format_print
, NULL
);
535 if (qdict_haskey(bs_opts
, "driver")) {
536 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
539 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
542 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
543 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
544 on_write_error
= parse_block_error_action(buf
, 0, &error
);
546 error_propagate(errp
, error
);
551 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
552 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
553 on_read_error
= parse_block_error_action(buf
, 1, &error
);
555 error_propagate(errp
, error
);
561 bdrv_flags
|= BDRV_O_SNAPSHOT
;
565 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
566 BlockBackendRootState
*blk_rs
;
568 blk
= blk_new(qemu_opts_id(opts
), errp
);
573 blk_rs
= blk_get_root_state(blk
);
574 blk_rs
->open_flags
= bdrv_flags
;
575 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
576 blk_rs
->detect_zeroes
= detect_zeroes
;
578 if (throttle_enabled(&cfg
)) {
579 if (!throttling_group
) {
580 throttling_group
= blk_name(blk
);
582 blk_rs
->throttle_group
= g_strdup(throttling_group
);
583 blk_rs
->throttle_state
= throttle_group_incref(throttling_group
);
584 blk_rs
->throttle_state
->cfg
= cfg
;
589 if (file
&& !*file
) {
593 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
594 * with other callers) rather than what we want as the real defaults.
595 * Apply the defaults here instead. */
596 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_WB
, "on");
597 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
598 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
601 /* always use cache=unsafe with snapshot */
602 qdict_put(bs_opts
, BDRV_OPT_CACHE_WB
, qstring_from_str("on"));
603 qdict_put(bs_opts
, BDRV_OPT_CACHE_DIRECT
, qstring_from_str("off"));
604 qdict_put(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, qstring_from_str("on"));
607 if (runstate_check(RUN_STATE_INMIGRATE
)) {
608 bdrv_flags
|= BDRV_O_INACTIVE
;
611 blk
= blk_new_open(qemu_opts_id(opts
), file
, NULL
, bs_opts
, bdrv_flags
,
618 bs
->detect_zeroes
= detect_zeroes
;
620 /* disk I/O throttling */
621 if (throttle_enabled(&cfg
)) {
622 if (!throttling_group
) {
623 throttling_group
= blk_name(blk
);
625 bdrv_io_limits_enable(bs
, throttling_group
);
626 bdrv_set_io_limits(bs
, &cfg
);
629 if (bdrv_key_required(bs
)) {
633 block_acct_init(blk_get_stats(blk
), account_invalid
, account_failed
);
635 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
642 blk_set_on_error(blk
, on_read_error
, on_write_error
);
646 QDECREF(interval_dict
);
647 QDECREF(interval_list
);
652 QDECREF(interval_dict
);
653 QDECREF(interval_list
);
659 static QemuOptsList qemu_root_bds_opts
;
661 /* Takes the ownership of bs_opts */
662 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
664 BlockDriverState
*bs
;
666 Error
*local_error
= NULL
;
667 BlockdevDetectZeroesOptions detect_zeroes
;
671 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
676 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
678 error_propagate(errp
, local_error
);
682 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
683 &detect_zeroes
, &local_error
);
685 error_propagate(errp
, local_error
);
689 if (runstate_check(RUN_STATE_INMIGRATE
)) {
690 bdrv_flags
|= BDRV_O_INACTIVE
;
694 ret
= bdrv_open(&bs
, NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
696 goto fail_no_bs_opts
;
699 bs
->detect_zeroes
= detect_zeroes
;
711 void blockdev_close_all_bdrv_states(void)
713 BlockDriverState
*bs
, *next_bs
;
715 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
716 AioContext
*ctx
= bdrv_get_aio_context(bs
);
718 aio_context_acquire(ctx
);
720 aio_context_release(ctx
);
724 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
729 value
= qemu_opt_get(opts
, from
);
731 if (qemu_opt_find(opts
, to
)) {
732 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
733 "same time", to
, from
);
738 /* rename all items in opts */
739 while ((value
= qemu_opt_get(opts
, from
))) {
740 qemu_opt_set(opts
, to
, value
, &error_abort
);
741 qemu_opt_unset(opts
, from
);
745 QemuOptsList qemu_legacy_drive_opts
= {
747 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
751 .type
= QEMU_OPT_NUMBER
,
752 .help
= "bus number",
755 .type
= QEMU_OPT_NUMBER
,
756 .help
= "unit number (i.e. lun for scsi)",
759 .type
= QEMU_OPT_NUMBER
,
760 .help
= "index number",
763 .type
= QEMU_OPT_STRING
,
764 .help
= "media type (disk, cdrom)",
767 .type
= QEMU_OPT_STRING
,
768 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
771 .type
= QEMU_OPT_NUMBER
,
772 .help
= "number of cylinders (ide disk geometry)",
775 .type
= QEMU_OPT_NUMBER
,
776 .help
= "number of heads (ide disk geometry)",
779 .type
= QEMU_OPT_NUMBER
,
780 .help
= "number of sectors (ide disk geometry)",
783 .type
= QEMU_OPT_STRING
,
784 .help
= "chs translation (auto, lba, none)",
787 .type
= QEMU_OPT_BOOL
,
788 .help
= "(deprecated, ignored)",
791 .type
= QEMU_OPT_STRING
,
792 .help
= "pci address (virtio only)",
795 .type
= QEMU_OPT_STRING
,
796 .help
= "disk serial number",
799 .type
= QEMU_OPT_STRING
,
803 /* Options that are passed on, but have special semantics with -drive */
806 .type
= QEMU_OPT_BOOL
,
807 .help
= "open drive file as read-only",
810 .type
= QEMU_OPT_STRING
,
811 .help
= "read error action",
814 .type
= QEMU_OPT_STRING
,
815 .help
= "write error action",
817 .name
= "copy-on-read",
818 .type
= QEMU_OPT_BOOL
,
819 .help
= "copy read data from backing file into image file",
822 { /* end of list */ }
826 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
830 DriveInfo
*dinfo
= NULL
;
832 QemuOpts
*legacy_opts
;
833 DriveMediaType media
= MEDIA_DISK
;
834 BlockInterfaceType type
;
835 int cyls
, heads
, secs
, translation
;
836 int max_devs
, bus_id
, unit_id
, index
;
838 const char *werror
, *rerror
;
839 bool read_only
= false;
842 const char *filename
;
843 Error
*local_err
= NULL
;
846 /* Change legacy command line options into QMP ones */
847 static const struct {
851 { "iops", "throttling.iops-total" },
852 { "iops_rd", "throttling.iops-read" },
853 { "iops_wr", "throttling.iops-write" },
855 { "bps", "throttling.bps-total" },
856 { "bps_rd", "throttling.bps-read" },
857 { "bps_wr", "throttling.bps-write" },
859 { "iops_max", "throttling.iops-total-max" },
860 { "iops_rd_max", "throttling.iops-read-max" },
861 { "iops_wr_max", "throttling.iops-write-max" },
863 { "bps_max", "throttling.bps-total-max" },
864 { "bps_rd_max", "throttling.bps-read-max" },
865 { "bps_wr_max", "throttling.bps-write-max" },
867 { "iops_size", "throttling.iops-size" },
869 { "group", "throttling.group" },
871 { "readonly", "read-only" },
874 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
875 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
878 error_report_err(local_err
);
883 value
= qemu_opt_get(all_opts
, "cache");
887 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
888 error_report("invalid cache option");
892 /* Specific options take precedence */
893 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
894 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
895 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
897 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
898 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
899 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
901 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
902 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
903 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
905 qemu_opt_unset(all_opts
, "cache");
908 /* Get a QDict for processing the options */
909 bs_opts
= qdict_new();
910 qemu_opts_to_qdict(all_opts
, bs_opts
);
912 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
914 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
916 error_report_err(local_err
);
920 /* Deprecated option boot=[on|off] */
921 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
922 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
923 "ignored. Future versions will reject this parameter. Please "
924 "update your scripts.\n");
928 value
= qemu_opt_get(legacy_opts
, "media");
930 if (!strcmp(value
, "disk")) {
932 } else if (!strcmp(value
, "cdrom")) {
936 error_report("'%s' invalid media", value
);
941 /* copy-on-read is disabled with a warning for read-only devices */
942 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
943 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
945 if (read_only
&& copy_on_read
) {
946 error_report("warning: disabling copy-on-read on read-only drive");
947 copy_on_read
= false;
950 qdict_put(bs_opts
, "read-only",
951 qstring_from_str(read_only
? "on" : "off"));
952 qdict_put(bs_opts
, "copy-on-read",
953 qstring_from_str(copy_on_read
? "on" :"off"));
955 /* Controller type */
956 value
= qemu_opt_get(legacy_opts
, "if");
959 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
962 if (type
== IF_COUNT
) {
963 error_report("unsupported bus type '%s'", value
);
967 type
= block_default_type
;
971 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
972 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
973 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
975 if (cyls
|| heads
|| secs
) {
977 error_report("invalid physical cyls number");
981 error_report("invalid physical heads number");
985 error_report("invalid physical secs number");
990 translation
= BIOS_ATA_TRANSLATION_AUTO
;
991 value
= qemu_opt_get(legacy_opts
, "trans");
994 error_report("'%s' trans must be used with cyls, heads and secs",
998 if (!strcmp(value
, "none")) {
999 translation
= BIOS_ATA_TRANSLATION_NONE
;
1000 } else if (!strcmp(value
, "lba")) {
1001 translation
= BIOS_ATA_TRANSLATION_LBA
;
1002 } else if (!strcmp(value
, "large")) {
1003 translation
= BIOS_ATA_TRANSLATION_LARGE
;
1004 } else if (!strcmp(value
, "rechs")) {
1005 translation
= BIOS_ATA_TRANSLATION_RECHS
;
1006 } else if (!strcmp(value
, "auto")) {
1007 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1009 error_report("'%s' invalid translation type", value
);
1014 if (media
== MEDIA_CDROM
) {
1015 if (cyls
|| secs
|| heads
) {
1016 error_report("CHS can't be set with media=cdrom");
1021 /* Device address specified by bus/unit or index.
1022 * If none was specified, try to find the first free one. */
1023 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
1024 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
1025 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
1027 max_devs
= if_max_devs
[type
];
1030 if (bus_id
!= 0 || unit_id
!= -1) {
1031 error_report("index cannot be used with bus and unit");
1034 bus_id
= drive_index_to_bus_id(type
, index
);
1035 unit_id
= drive_index_to_unit_id(type
, index
);
1038 if (unit_id
== -1) {
1040 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1042 if (max_devs
&& unit_id
>= max_devs
) {
1043 unit_id
-= max_devs
;
1049 if (max_devs
&& unit_id
>= max_devs
) {
1050 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1054 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1055 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1056 bus_id
, unit_id
, index
);
1061 serial
= qemu_opt_get(legacy_opts
, "serial");
1063 /* no id supplied -> create one */
1064 if (qemu_opts_id(all_opts
) == NULL
) {
1066 const char *mediastr
= "";
1067 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1068 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1071 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1074 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1077 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1081 /* Add virtio block device */
1082 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1083 if (devaddr
&& type
!= IF_VIRTIO
) {
1084 error_report("addr is not supported by this bus type");
1088 if (type
== IF_VIRTIO
) {
1090 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1092 if (arch_type
== QEMU_ARCH_S390X
) {
1093 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1095 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1097 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1100 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1104 filename
= qemu_opt_get(legacy_opts
, "file");
1106 /* Check werror/rerror compatibility with if=... */
1107 werror
= qemu_opt_get(legacy_opts
, "werror");
1108 if (werror
!= NULL
) {
1109 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1111 error_report("werror is not supported by this bus type");
1114 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1117 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1118 if (rerror
!= NULL
) {
1119 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1121 error_report("rerror is not supported by this bus type");
1124 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1127 /* Actual block device init: Functionality shared with blockdev-add */
1128 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1132 error_report_err(local_err
);
1139 /* Create legacy DriveInfo */
1140 dinfo
= g_malloc0(sizeof(*dinfo
));
1141 dinfo
->opts
= all_opts
;
1144 dinfo
->heads
= heads
;
1146 dinfo
->trans
= translation
;
1149 dinfo
->bus
= bus_id
;
1150 dinfo
->unit
= unit_id
;
1151 dinfo
->devaddr
= devaddr
;
1152 dinfo
->serial
= g_strdup(serial
);
1154 blk_set_legacy_dinfo(blk
, dinfo
);
1161 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1168 qemu_opts_del(legacy_opts
);
1173 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1175 const char *device
= qdict_get_str(qdict
, "device");
1179 if (!strcmp(device
, "all")) {
1180 ret
= bdrv_commit_all();
1182 BlockDriverState
*bs
;
1183 AioContext
*aio_context
;
1185 blk
= blk_by_name(device
);
1187 monitor_printf(mon
, "Device '%s' not found\n", device
);
1190 if (!blk_is_available(blk
)) {
1191 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1196 aio_context
= bdrv_get_aio_context(bs
);
1197 aio_context_acquire(aio_context
);
1199 ret
= bdrv_commit(bs
);
1201 aio_context_release(aio_context
);
1204 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1209 static void blockdev_do_action(TransactionActionKind type
, void *data
,
1212 TransactionAction action
;
1213 TransactionActionList list
;
1216 action
.u
.data
= data
;
1217 list
.value
= &action
;
1219 qmp_transaction(&list
, false, NULL
, errp
);
1222 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1223 bool has_node_name
, const char *node_name
,
1224 const char *snapshot_file
,
1225 bool has_snapshot_node_name
,
1226 const char *snapshot_node_name
,
1227 bool has_format
, const char *format
,
1228 bool has_mode
, NewImageMode mode
, Error
**errp
)
1230 BlockdevSnapshotSync snapshot
= {
1231 .has_device
= has_device
,
1232 .device
= (char *) device
,
1233 .has_node_name
= has_node_name
,
1234 .node_name
= (char *) node_name
,
1235 .snapshot_file
= (char *) snapshot_file
,
1236 .has_snapshot_node_name
= has_snapshot_node_name
,
1237 .snapshot_node_name
= (char *) snapshot_node_name
,
1238 .has_format
= has_format
,
1239 .format
= (char *) format
,
1240 .has_mode
= has_mode
,
1243 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1247 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1250 BlockdevSnapshot snapshot_data
= {
1251 .node
= (char *) node
,
1252 .overlay
= (char *) overlay
1255 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1256 &snapshot_data
, errp
);
1259 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1263 BlockdevSnapshotInternal snapshot
= {
1264 .device
= (char *) device
,
1265 .name
= (char *) name
1268 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1272 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1279 BlockDriverState
*bs
;
1281 AioContext
*aio_context
;
1282 QEMUSnapshotInfo sn
;
1283 Error
*local_err
= NULL
;
1284 SnapshotInfo
*info
= NULL
;
1287 blk
= blk_by_name(device
);
1289 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1290 "Device '%s' not found", device
);
1294 aio_context
= blk_get_aio_context(blk
);
1295 aio_context_acquire(aio_context
);
1306 error_setg(errp
, "Name or id must be provided");
1307 goto out_aio_context
;
1310 if (!blk_is_available(blk
)) {
1311 error_setg(errp
, "Device '%s' has no medium", device
);
1312 goto out_aio_context
;
1316 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1317 goto out_aio_context
;
1320 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1322 error_propagate(errp
, local_err
);
1323 goto out_aio_context
;
1327 "Snapshot with id '%s' and name '%s' does not exist on "
1329 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1330 goto out_aio_context
;
1333 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1335 error_propagate(errp
, local_err
);
1336 goto out_aio_context
;
1339 aio_context_release(aio_context
);
1341 info
= g_new0(SnapshotInfo
, 1);
1342 info
->id
= g_strdup(sn
.id_str
);
1343 info
->name
= g_strdup(sn
.name
);
1344 info
->date_nsec
= sn
.date_nsec
;
1345 info
->date_sec
= sn
.date_sec
;
1346 info
->vm_state_size
= sn
.vm_state_size
;
1347 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1348 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1353 aio_context_release(aio_context
);
1358 * block_dirty_bitmap_lookup:
1359 * Return a dirty bitmap (if present), after validating
1360 * the node reference and bitmap names.
1362 * @node: The name of the BDS node to search for bitmaps
1363 * @name: The name of the bitmap to search for
1364 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1365 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1366 * @errp: Output pointer for error information. Can be NULL.
1368 * @return: A bitmap object on success, or NULL on failure.
1370 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1372 BlockDriverState
**pbs
,
1376 BlockDriverState
*bs
;
1377 BdrvDirtyBitmap
*bitmap
;
1378 AioContext
*aio_context
;
1381 error_setg(errp
, "Node cannot be NULL");
1385 error_setg(errp
, "Bitmap name cannot be NULL");
1388 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1390 error_setg(errp
, "Node '%s' not found", node
);
1394 aio_context
= bdrv_get_aio_context(bs
);
1395 aio_context_acquire(aio_context
);
1397 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1399 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1407 *paio
= aio_context
;
1409 aio_context_release(aio_context
);
1415 aio_context_release(aio_context
);
1419 /* New and old BlockDriverState structs for atomic group operations */
1421 typedef struct BlkActionState BlkActionState
;
1425 * Table of operations that define an Action.
1427 * @instance_size: Size of state struct, in bytes.
1428 * @prepare: Prepare the work, must NOT be NULL.
1429 * @commit: Commit the changes, can be NULL.
1430 * @abort: Abort the changes on fail, can be NULL.
1431 * @clean: Clean up resources after all transaction actions have called
1432 * commit() or abort(). Can be NULL.
1434 * Only prepare() may fail. In a single transaction, only one of commit() or
1435 * abort() will be called. clean() will always be called if it is present.
1437 typedef struct BlkActionOps
{
1438 size_t instance_size
;
1439 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1440 void (*commit
)(BlkActionState
*common
);
1441 void (*abort
)(BlkActionState
*common
);
1442 void (*clean
)(BlkActionState
*common
);
1447 * Describes one Action's state within a Transaction.
1449 * @action: QAPI-defined enum identifying which Action to perform.
1450 * @ops: Table of ActionOps this Action can perform.
1451 * @block_job_txn: Transaction which this action belongs to.
1452 * @entry: List membership for all Actions in this Transaction.
1454 * This structure must be arranged as first member in a subclassed type,
1455 * assuming that the compiler will also arrange it to the same offsets as the
1458 struct BlkActionState
{
1459 TransactionAction
*action
;
1460 const BlkActionOps
*ops
;
1461 BlockJobTxn
*block_job_txn
;
1462 TransactionProperties
*txn_props
;
1463 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1466 /* internal snapshot private data */
1467 typedef struct InternalSnapshotState
{
1468 BlkActionState common
;
1469 BlockDriverState
*bs
;
1470 AioContext
*aio_context
;
1471 QEMUSnapshotInfo sn
;
1473 } InternalSnapshotState
;
1476 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1478 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1480 "Action '%s' does not support Transaction property "
1481 "completion-mode = %s",
1482 TransactionActionKind_lookup
[s
->action
->type
],
1483 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1489 static void internal_snapshot_prepare(BlkActionState
*common
,
1492 Error
*local_err
= NULL
;
1496 BlockDriverState
*bs
;
1497 QEMUSnapshotInfo old_sn
, *sn
;
1500 BlockdevSnapshotInternal
*internal
;
1501 InternalSnapshotState
*state
;
1504 g_assert(common
->action
->type
==
1505 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1506 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
;
1507 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1509 /* 1. parse input */
1510 device
= internal
->device
;
1511 name
= internal
->name
;
1513 /* 2. check for validation */
1514 if (action_check_completion_mode(common
, errp
) < 0) {
1518 blk
= blk_by_name(device
);
1520 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1521 "Device '%s' not found", device
);
1525 /* AioContext is released in .clean() */
1526 state
->aio_context
= blk_get_aio_context(blk
);
1527 aio_context_acquire(state
->aio_context
);
1529 if (!blk_is_available(blk
)) {
1530 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1536 bdrv_drained_begin(bs
);
1538 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1542 if (bdrv_is_read_only(bs
)) {
1543 error_setg(errp
, "Device '%s' is read only", device
);
1547 if (!bdrv_can_snapshot(bs
)) {
1548 error_setg(errp
, "Block format '%s' used by device '%s' "
1549 "does not support internal snapshots",
1550 bs
->drv
->format_name
, device
);
1554 if (!strlen(name
)) {
1555 error_setg(errp
, "Name is empty");
1559 /* check whether a snapshot with name exist */
1560 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1563 error_propagate(errp
, local_err
);
1567 "Snapshot with name '%s' already exists on device '%s'",
1572 /* 3. take the snapshot */
1574 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1575 qemu_gettimeofday(&tv
);
1576 sn
->date_sec
= tv
.tv_sec
;
1577 sn
->date_nsec
= tv
.tv_usec
* 1000;
1578 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1580 ret1
= bdrv_snapshot_create(bs
, sn
);
1582 error_setg_errno(errp
, -ret1
,
1583 "Failed to create snapshot '%s' on device '%s'",
1588 /* 4. succeed, mark a snapshot is created */
1589 state
->created
= true;
1592 static void internal_snapshot_abort(BlkActionState
*common
)
1594 InternalSnapshotState
*state
=
1595 DO_UPCAST(InternalSnapshotState
, common
, common
);
1596 BlockDriverState
*bs
= state
->bs
;
1597 QEMUSnapshotInfo
*sn
= &state
->sn
;
1598 Error
*local_error
= NULL
;
1600 if (!state
->created
) {
1604 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1605 error_reportf_err(local_error
,
1606 "Failed to delete snapshot with id '%s' and "
1607 "name '%s' on device '%s' in abort: ",
1608 sn
->id_str
, sn
->name
,
1609 bdrv_get_device_name(bs
));
1613 static void internal_snapshot_clean(BlkActionState
*common
)
1615 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1618 if (state
->aio_context
) {
1620 bdrv_drained_end(state
->bs
);
1622 aio_context_release(state
->aio_context
);
1626 /* external snapshot private data */
1627 typedef struct ExternalSnapshotState
{
1628 BlkActionState common
;
1629 BlockDriverState
*old_bs
;
1630 BlockDriverState
*new_bs
;
1631 AioContext
*aio_context
;
1632 } ExternalSnapshotState
;
1634 static void external_snapshot_prepare(BlkActionState
*common
,
1638 QDict
*options
= NULL
;
1639 Error
*local_err
= NULL
;
1640 /* Device and node name of the image to generate the snapshot from */
1642 const char *node_name
;
1643 /* Reference to the new image (for 'blockdev-snapshot') */
1644 const char *snapshot_ref
;
1645 /* File name of the new image (for 'blockdev-snapshot-sync') */
1646 const char *new_image_file
;
1647 ExternalSnapshotState
*state
=
1648 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1649 TransactionAction
*action
= common
->action
;
1651 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1652 * purpose but a different set of parameters */
1653 switch (action
->type
) {
1654 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1656 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
;
1658 node_name
= s
->node
;
1659 new_image_file
= NULL
;
1660 snapshot_ref
= s
->overlay
;
1663 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1665 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1666 device
= s
->has_device
? s
->device
: NULL
;
1667 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1668 new_image_file
= s
->snapshot_file
;
1669 snapshot_ref
= NULL
;
1673 g_assert_not_reached();
1676 /* start processing */
1677 if (action_check_completion_mode(common
, errp
) < 0) {
1681 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1682 if (!state
->old_bs
) {
1686 /* Acquire AioContext now so any threads operating on old_bs stop */
1687 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1688 aio_context_acquire(state
->aio_context
);
1689 bdrv_drained_begin(state
->old_bs
);
1691 if (!bdrv_is_inserted(state
->old_bs
)) {
1692 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1696 if (bdrv_op_is_blocked(state
->old_bs
,
1697 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1701 if (!bdrv_is_read_only(state
->old_bs
)) {
1702 if (bdrv_flush(state
->old_bs
)) {
1703 error_setg(errp
, QERR_IO_ERROR
);
1708 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1709 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1713 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1714 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1715 const char *format
= s
->has_format
? s
->format
: "qcow2";
1716 enum NewImageMode mode
;
1717 const char *snapshot_node_name
=
1718 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1720 if (node_name
&& !snapshot_node_name
) {
1721 error_setg(errp
, "New snapshot node name missing");
1725 if (snapshot_node_name
&&
1726 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1727 error_setg(errp
, "New snapshot node name already in use");
1731 flags
= state
->old_bs
->open_flags
;
1733 /* create new image w/backing file */
1734 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1735 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1736 bdrv_img_create(new_image_file
, format
,
1737 state
->old_bs
->filename
,
1738 state
->old_bs
->drv
->format_name
,
1739 NULL
, -1, flags
, &local_err
, false);
1741 error_propagate(errp
, local_err
);
1746 options
= qdict_new();
1747 if (s
->has_snapshot_node_name
) {
1748 qdict_put(options
, "node-name",
1749 qstring_from_str(snapshot_node_name
));
1751 qdict_put(options
, "driver", qstring_from_str(format
));
1753 flags
|= BDRV_O_NO_BACKING
;
1756 assert(state
->new_bs
== NULL
);
1757 ret
= bdrv_open(&state
->new_bs
, new_image_file
, snapshot_ref
, options
,
1759 /* We will manually add the backing_hd field to the bs later */
1764 if (state
->new_bs
->blk
!= NULL
) {
1765 error_setg(errp
, "The snapshot is already in use by %s",
1766 blk_name(state
->new_bs
->blk
));
1770 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1775 if (state
->new_bs
->backing
!= NULL
) {
1776 error_setg(errp
, "The snapshot already has a backing image");
1780 if (!state
->new_bs
->drv
->supports_backing
) {
1781 error_setg(errp
, "The snapshot does not support backing images");
1785 static void external_snapshot_commit(BlkActionState
*common
)
1787 ExternalSnapshotState
*state
=
1788 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1790 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1792 /* This removes our old bs and adds the new bs */
1793 bdrv_append(state
->new_bs
, state
->old_bs
);
1794 /* We don't need (or want) to use the transactional
1795 * bdrv_reopen_multiple() across all the entries at once, because we
1796 * don't want to abort all of them if one of them fails the reopen */
1797 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1801 static void external_snapshot_abort(BlkActionState
*common
)
1803 ExternalSnapshotState
*state
=
1804 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1805 if (state
->new_bs
) {
1806 bdrv_unref(state
->new_bs
);
1810 static void external_snapshot_clean(BlkActionState
*common
)
1812 ExternalSnapshotState
*state
=
1813 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1814 if (state
->aio_context
) {
1815 bdrv_drained_end(state
->old_bs
);
1816 aio_context_release(state
->aio_context
);
1820 typedef struct DriveBackupState
{
1821 BlkActionState common
;
1822 BlockDriverState
*bs
;
1823 AioContext
*aio_context
;
1827 static void do_drive_backup(const char *device
, const char *target
,
1828 bool has_format
, const char *format
,
1829 enum MirrorSyncMode sync
,
1830 bool has_mode
, enum NewImageMode mode
,
1831 bool has_speed
, int64_t speed
,
1832 bool has_bitmap
, const char *bitmap
,
1833 bool has_on_source_error
,
1834 BlockdevOnError on_source_error
,
1835 bool has_on_target_error
,
1836 BlockdevOnError on_target_error
,
1837 BlockJobTxn
*txn
, Error
**errp
);
1839 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1841 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1843 DriveBackup
*backup
;
1844 Error
*local_err
= NULL
;
1846 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1847 backup
= common
->action
->u
.drive_backup
;
1849 blk
= blk_by_name(backup
->device
);
1851 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1852 "Device '%s' not found", backup
->device
);
1856 if (!blk_is_available(blk
)) {
1857 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1861 /* AioContext is released in .clean() */
1862 state
->aio_context
= blk_get_aio_context(blk
);
1863 aio_context_acquire(state
->aio_context
);
1864 bdrv_drained_begin(blk_bs(blk
));
1865 state
->bs
= blk_bs(blk
);
1867 do_drive_backup(backup
->device
, backup
->target
,
1868 backup
->has_format
, backup
->format
,
1870 backup
->has_mode
, backup
->mode
,
1871 backup
->has_speed
, backup
->speed
,
1872 backup
->has_bitmap
, backup
->bitmap
,
1873 backup
->has_on_source_error
, backup
->on_source_error
,
1874 backup
->has_on_target_error
, backup
->on_target_error
,
1875 common
->block_job_txn
, &local_err
);
1877 error_propagate(errp
, local_err
);
1881 state
->job
= state
->bs
->job
;
1884 static void drive_backup_abort(BlkActionState
*common
)
1886 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1887 BlockDriverState
*bs
= state
->bs
;
1889 /* Only cancel if it's the job we started */
1890 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1891 block_job_cancel_sync(bs
->job
);
1895 static void drive_backup_clean(BlkActionState
*common
)
1897 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1899 if (state
->aio_context
) {
1900 bdrv_drained_end(state
->bs
);
1901 aio_context_release(state
->aio_context
);
1905 typedef struct BlockdevBackupState
{
1906 BlkActionState common
;
1907 BlockDriverState
*bs
;
1909 AioContext
*aio_context
;
1910 } BlockdevBackupState
;
1912 static void do_blockdev_backup(const char *device
, const char *target
,
1913 enum MirrorSyncMode sync
,
1914 bool has_speed
, int64_t speed
,
1915 bool has_on_source_error
,
1916 BlockdevOnError on_source_error
,
1917 bool has_on_target_error
,
1918 BlockdevOnError on_target_error
,
1919 BlockJobTxn
*txn
, Error
**errp
);
1921 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1923 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1924 BlockdevBackup
*backup
;
1925 BlockBackend
*blk
, *target
;
1926 Error
*local_err
= NULL
;
1928 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1929 backup
= common
->action
->u
.blockdev_backup
;
1931 blk
= blk_by_name(backup
->device
);
1933 error_setg(errp
, "Device '%s' not found", backup
->device
);
1937 if (!blk_is_available(blk
)) {
1938 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1942 target
= blk_by_name(backup
->target
);
1944 error_setg(errp
, "Device '%s' not found", backup
->target
);
1948 /* AioContext is released in .clean() */
1949 state
->aio_context
= blk_get_aio_context(blk
);
1950 if (state
->aio_context
!= blk_get_aio_context(target
)) {
1951 state
->aio_context
= NULL
;
1952 error_setg(errp
, "Backup between two IO threads is not implemented");
1955 aio_context_acquire(state
->aio_context
);
1956 state
->bs
= blk_bs(blk
);
1957 bdrv_drained_begin(state
->bs
);
1959 do_blockdev_backup(backup
->device
, backup
->target
,
1961 backup
->has_speed
, backup
->speed
,
1962 backup
->has_on_source_error
, backup
->on_source_error
,
1963 backup
->has_on_target_error
, backup
->on_target_error
,
1964 common
->block_job_txn
, &local_err
);
1966 error_propagate(errp
, local_err
);
1970 state
->job
= state
->bs
->job
;
1973 static void blockdev_backup_abort(BlkActionState
*common
)
1975 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1976 BlockDriverState
*bs
= state
->bs
;
1978 /* Only cancel if it's the job we started */
1979 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1980 block_job_cancel_sync(bs
->job
);
1984 static void blockdev_backup_clean(BlkActionState
*common
)
1986 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1988 if (state
->aio_context
) {
1989 bdrv_drained_end(state
->bs
);
1990 aio_context_release(state
->aio_context
);
1994 typedef struct BlockDirtyBitmapState
{
1995 BlkActionState common
;
1996 BdrvDirtyBitmap
*bitmap
;
1997 BlockDriverState
*bs
;
1998 AioContext
*aio_context
;
2001 } BlockDirtyBitmapState
;
2003 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
2006 Error
*local_err
= NULL
;
2007 BlockDirtyBitmapAdd
*action
;
2008 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2011 if (action_check_completion_mode(common
, errp
) < 0) {
2015 action
= common
->action
->u
.block_dirty_bitmap_add
;
2016 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2017 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2018 action
->has_granularity
, action
->granularity
,
2022 state
->prepared
= true;
2024 error_propagate(errp
, local_err
);
2028 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2030 BlockDirtyBitmapAdd
*action
;
2031 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2034 action
= common
->action
->u
.block_dirty_bitmap_add
;
2035 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2036 * then the node reference and bitmap name must have been valid.
2038 if (state
->prepared
) {
2039 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2043 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2046 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2048 BlockDirtyBitmap
*action
;
2050 if (action_check_completion_mode(common
, errp
) < 0) {
2054 action
= common
->action
->u
.block_dirty_bitmap_clear
;
2055 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2058 &state
->aio_context
,
2060 if (!state
->bitmap
) {
2064 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2065 error_setg(errp
, "Cannot modify a frozen bitmap");
2067 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2068 error_setg(errp
, "Cannot clear a disabled bitmap");
2072 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2073 /* AioContext is released in .clean() */
2076 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2078 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2081 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2084 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2086 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2089 hbitmap_free(state
->backup
);
2092 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2094 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2097 if (state
->aio_context
) {
2098 aio_context_release(state
->aio_context
);
2102 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2104 error_setg(errp
, "Transaction aborted using Abort action");
2107 static void abort_commit(BlkActionState
*common
)
2109 g_assert_not_reached(); /* this action never succeeds */
2112 static const BlkActionOps actions
[] = {
2113 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2114 .instance_size
= sizeof(ExternalSnapshotState
),
2115 .prepare
= external_snapshot_prepare
,
2116 .commit
= external_snapshot_commit
,
2117 .abort
= external_snapshot_abort
,
2118 .clean
= external_snapshot_clean
,
2120 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2121 .instance_size
= sizeof(ExternalSnapshotState
),
2122 .prepare
= external_snapshot_prepare
,
2123 .commit
= external_snapshot_commit
,
2124 .abort
= external_snapshot_abort
,
2125 .clean
= external_snapshot_clean
,
2127 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2128 .instance_size
= sizeof(DriveBackupState
),
2129 .prepare
= drive_backup_prepare
,
2130 .abort
= drive_backup_abort
,
2131 .clean
= drive_backup_clean
,
2133 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2134 .instance_size
= sizeof(BlockdevBackupState
),
2135 .prepare
= blockdev_backup_prepare
,
2136 .abort
= blockdev_backup_abort
,
2137 .clean
= blockdev_backup_clean
,
2139 [TRANSACTION_ACTION_KIND_ABORT
] = {
2140 .instance_size
= sizeof(BlkActionState
),
2141 .prepare
= abort_prepare
,
2142 .commit
= abort_commit
,
2144 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2145 .instance_size
= sizeof(InternalSnapshotState
),
2146 .prepare
= internal_snapshot_prepare
,
2147 .abort
= internal_snapshot_abort
,
2148 .clean
= internal_snapshot_clean
,
2150 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2151 .instance_size
= sizeof(BlockDirtyBitmapState
),
2152 .prepare
= block_dirty_bitmap_add_prepare
,
2153 .abort
= block_dirty_bitmap_add_abort
,
2155 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2156 .instance_size
= sizeof(BlockDirtyBitmapState
),
2157 .prepare
= block_dirty_bitmap_clear_prepare
,
2158 .commit
= block_dirty_bitmap_clear_commit
,
2159 .abort
= block_dirty_bitmap_clear_abort
,
2160 .clean
= block_dirty_bitmap_clear_clean
,
2165 * Allocate a TransactionProperties structure if necessary, and fill
2166 * that structure with desired defaults if they are unset.
2168 static TransactionProperties
*get_transaction_properties(
2169 TransactionProperties
*props
)
2172 props
= g_new0(TransactionProperties
, 1);
2175 if (!props
->has_completion_mode
) {
2176 props
->has_completion_mode
= true;
2177 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2184 * 'Atomic' group operations. The operations are performed as a set, and if
2185 * any fail then we roll back all operations in the group.
2187 void qmp_transaction(TransactionActionList
*dev_list
,
2189 struct TransactionProperties
*props
,
2192 TransactionActionList
*dev_entry
= dev_list
;
2193 BlockJobTxn
*block_job_txn
= NULL
;
2194 BlkActionState
*state
, *next
;
2195 Error
*local_err
= NULL
;
2197 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2198 QSIMPLEQ_INIT(&snap_bdrv_states
);
2200 /* Does this transaction get canceled as a group on failure?
2201 * If not, we don't really need to make a BlockJobTxn.
2203 props
= get_transaction_properties(props
);
2204 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2205 block_job_txn
= block_job_txn_new();
2208 /* drain all i/o before any operations */
2211 /* We don't do anything in this loop that commits us to the operations */
2212 while (NULL
!= dev_entry
) {
2213 TransactionAction
*dev_info
= NULL
;
2214 const BlkActionOps
*ops
;
2216 dev_info
= dev_entry
->value
;
2217 dev_entry
= dev_entry
->next
;
2219 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2221 ops
= &actions
[dev_info
->type
];
2222 assert(ops
->instance_size
> 0);
2224 state
= g_malloc0(ops
->instance_size
);
2226 state
->action
= dev_info
;
2227 state
->block_job_txn
= block_job_txn
;
2228 state
->txn_props
= props
;
2229 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2231 state
->ops
->prepare(state
, &local_err
);
2233 error_propagate(errp
, local_err
);
2234 goto delete_and_fail
;
2238 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2239 if (state
->ops
->commit
) {
2240 state
->ops
->commit(state
);
2248 /* failure, and it is all-or-none; roll back all operations */
2249 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2250 if (state
->ops
->abort
) {
2251 state
->ops
->abort(state
);
2255 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2256 if (state
->ops
->clean
) {
2257 state
->ops
->clean(state
);
2262 qapi_free_TransactionProperties(props
);
2264 block_job_txn_unref(block_job_txn
);
2267 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
2269 Error
*local_err
= NULL
;
2271 qmp_blockdev_open_tray(device
, has_force
, force
, &local_err
);
2273 error_propagate(errp
, local_err
);
2277 qmp_x_blockdev_remove_medium(device
, errp
);
2280 void qmp_block_passwd(bool has_device
, const char *device
,
2281 bool has_node_name
, const char *node_name
,
2282 const char *password
, Error
**errp
)
2284 Error
*local_err
= NULL
;
2285 BlockDriverState
*bs
;
2286 AioContext
*aio_context
;
2288 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2289 has_node_name
? node_name
: NULL
,
2292 error_propagate(errp
, local_err
);
2296 aio_context
= bdrv_get_aio_context(bs
);
2297 aio_context_acquire(aio_context
);
2299 bdrv_add_key(bs
, password
, errp
);
2301 aio_context_release(aio_context
);
2304 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2314 blk
= blk_by_name(device
);
2316 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2317 "Device '%s' not found", device
);
2321 if (!blk_dev_has_removable_media(blk
)) {
2322 error_setg(errp
, "Device '%s' is not removable", device
);
2326 if (!blk_dev_has_tray(blk
)) {
2327 /* Ignore this command on tray-less devices */
2331 if (blk_dev_is_tray_open(blk
)) {
2335 locked
= blk_dev_is_medium_locked(blk
);
2337 blk_dev_eject_request(blk
, force
);
2340 if (!locked
|| force
) {
2341 blk_dev_change_media_cb(blk
, false);
2345 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2349 blk
= blk_by_name(device
);
2351 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2352 "Device '%s' not found", device
);
2356 if (!blk_dev_has_removable_media(blk
)) {
2357 error_setg(errp
, "Device '%s' is not removable", device
);
2361 if (!blk_dev_has_tray(blk
)) {
2362 /* Ignore this command on tray-less devices */
2366 if (!blk_dev_is_tray_open(blk
)) {
2370 blk_dev_change_media_cb(blk
, true);
2373 void qmp_x_blockdev_remove_medium(const char *device
, Error
**errp
)
2376 BlockDriverState
*bs
;
2377 AioContext
*aio_context
;
2380 blk
= blk_by_name(device
);
2382 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2383 "Device '%s' not found", device
);
2387 /* For BBs without a device, we can exchange the BDS tree at will */
2388 has_device
= blk_get_attached_dev(blk
);
2390 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2391 error_setg(errp
, "Device '%s' is not removable", device
);
2395 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2396 error_setg(errp
, "Tray of device '%s' is not open", device
);
2405 aio_context
= bdrv_get_aio_context(bs
);
2406 aio_context_acquire(aio_context
);
2408 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2412 /* This follows the convention established by bdrv_make_anon() */
2413 if (bs
->device_list
.tqe_prev
) {
2414 bdrv_device_remove(bs
);
2419 if (!blk_dev_has_tray(blk
)) {
2420 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2421 * called at all); therefore, the medium needs to be ejected here.
2422 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2423 * value passed here (i.e. false). */
2424 blk_dev_change_media_cb(blk
, false);
2428 aio_context_release(aio_context
);
2431 static void qmp_blockdev_insert_anon_medium(const char *device
,
2432 BlockDriverState
*bs
, Error
**errp
)
2437 blk
= blk_by_name(device
);
2439 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2440 "Device '%s' not found", device
);
2444 /* For BBs without a device, we can exchange the BDS tree at will */
2445 has_device
= blk_get_attached_dev(blk
);
2447 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2448 error_setg(errp
, "Device '%s' is not removable", device
);
2452 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2453 error_setg(errp
, "Tray of device '%s' is not open", device
);
2458 error_setg(errp
, "There already is a medium in device '%s'", device
);
2462 blk_insert_bs(blk
, bs
);
2464 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, device_list
);
2466 if (!blk_dev_has_tray(blk
)) {
2467 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2468 * called at all); therefore, the medium needs to be pushed into the
2470 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2471 * value passed here (i.e. true). */
2472 blk_dev_change_media_cb(blk
, true);
2476 void qmp_x_blockdev_insert_medium(const char *device
, const char *node_name
,
2479 BlockDriverState
*bs
;
2481 bs
= bdrv_find_node(node_name
);
2483 error_setg(errp
, "Node '%s' not found", node_name
);
2488 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2493 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2496 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2497 bool has_format
, const char *format
,
2499 BlockdevChangeReadOnlyMode read_only
,
2503 BlockDriverState
*medium_bs
= NULL
;
2504 int bdrv_flags
, ret
;
2505 QDict
*options
= NULL
;
2508 blk
= blk_by_name(device
);
2510 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2511 "Device '%s' not found", device
);
2516 blk_update_root_state(blk
);
2519 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2521 if (!has_read_only
) {
2522 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2525 switch (read_only
) {
2526 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2529 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2530 bdrv_flags
&= ~BDRV_O_RDWR
;
2533 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2534 bdrv_flags
|= BDRV_O_RDWR
;
2542 options
= qdict_new();
2543 qdict_put(options
, "driver", qstring_from_str(format
));
2547 ret
= bdrv_open(&medium_bs
, filename
, NULL
, options
, bdrv_flags
, errp
);
2552 blk_apply_root_state(blk
, medium_bs
);
2554 bdrv_add_key(medium_bs
, NULL
, &err
);
2556 error_propagate(errp
, err
);
2560 qmp_blockdev_open_tray(device
, false, false, &err
);
2562 error_propagate(errp
, err
);
2566 qmp_x_blockdev_remove_medium(device
, &err
);
2568 error_propagate(errp
, err
);
2572 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2574 error_propagate(errp
, err
);
2578 qmp_blockdev_close_tray(device
, errp
);
2581 /* If the medium has been inserted, the device has its own reference, so
2582 * ours must be relinquished; and if it has not been inserted successfully,
2583 * the reference must be relinquished anyway */
2584 bdrv_unref(medium_bs
);
2587 /* throttling disk I/O limits */
2588 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
2595 bool has_bps_rd_max
,
2597 bool has_bps_wr_max
,
2601 bool has_iops_rd_max
,
2602 int64_t iops_rd_max
,
2603 bool has_iops_wr_max
,
2604 int64_t iops_wr_max
,
2608 const char *group
, Error
**errp
)
2611 BlockDriverState
*bs
;
2613 AioContext
*aio_context
;
2615 blk
= blk_by_name(device
);
2617 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2618 "Device '%s' not found", device
);
2622 aio_context
= blk_get_aio_context(blk
);
2623 aio_context_acquire(aio_context
);
2627 error_setg(errp
, "Device '%s' has no medium", device
);
2631 memset(&cfg
, 0, sizeof(cfg
));
2632 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
2633 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
2634 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
2636 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
2637 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
2638 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
2641 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
2643 if (has_bps_rd_max
) {
2644 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2646 if (has_bps_wr_max
) {
2647 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2650 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2652 if (has_iops_rd_max
) {
2653 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2655 if (has_iops_wr_max
) {
2656 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2659 if (has_iops_size
) {
2660 cfg
.op_size
= iops_size
;
2663 if (!check_throttle_config(&cfg
, errp
)) {
2667 if (throttle_enabled(&cfg
)) {
2668 /* Enable I/O limits if they're not enabled yet, otherwise
2669 * just update the throttling group. */
2670 if (!bs
->throttle_state
) {
2671 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2672 } else if (has_group
) {
2673 bdrv_io_limits_update_group(bs
, group
);
2675 /* Set the new throttling configuration */
2676 bdrv_set_io_limits(bs
, &cfg
);
2677 } else if (bs
->throttle_state
) {
2678 /* If all throttling settings are set to 0, disable I/O limits */
2679 bdrv_io_limits_disable(bs
);
2683 aio_context_release(aio_context
);
2686 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2687 bool has_granularity
, uint32_t granularity
,
2690 AioContext
*aio_context
;
2691 BlockDriverState
*bs
;
2693 if (!name
|| name
[0] == '\0') {
2694 error_setg(errp
, "Bitmap name cannot be empty");
2698 bs
= bdrv_lookup_bs(node
, node
, errp
);
2703 aio_context
= bdrv_get_aio_context(bs
);
2704 aio_context_acquire(aio_context
);
2706 if (has_granularity
) {
2707 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2708 error_setg(errp
, "Granularity must be power of 2 "
2709 "and at least 512");
2713 /* Default to cluster size, if available: */
2714 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2717 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2720 aio_context_release(aio_context
);
2723 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2726 AioContext
*aio_context
;
2727 BlockDriverState
*bs
;
2728 BdrvDirtyBitmap
*bitmap
;
2730 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2731 if (!bitmap
|| !bs
) {
2735 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2737 "Bitmap '%s' is currently frozen and cannot be removed",
2741 bdrv_dirty_bitmap_make_anon(bitmap
);
2742 bdrv_release_dirty_bitmap(bs
, bitmap
);
2745 aio_context_release(aio_context
);
2749 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2750 * immediately after a full backup operation.
2752 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2755 AioContext
*aio_context
;
2756 BdrvDirtyBitmap
*bitmap
;
2757 BlockDriverState
*bs
;
2759 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2760 if (!bitmap
|| !bs
) {
2764 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2766 "Bitmap '%s' is currently frozen and cannot be modified",
2769 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2771 "Bitmap '%s' is currently disabled and cannot be cleared",
2776 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2779 aio_context_release(aio_context
);
2782 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2784 const char *id
= qdict_get_str(qdict
, "id");
2786 BlockDriverState
*bs
;
2787 AioContext
*aio_context
;
2788 Error
*local_err
= NULL
;
2790 blk
= blk_by_name(id
);
2792 error_report("Device '%s' not found", id
);
2796 if (!blk_legacy_dinfo(blk
)) {
2797 error_report("Deleting device added with blockdev-add"
2798 " is not supported");
2802 aio_context
= blk_get_aio_context(blk
);
2803 aio_context_acquire(aio_context
);
2807 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2808 error_report_err(local_err
);
2809 aio_context_release(aio_context
);
2816 /* if we have a device attached to this BlockDriverState
2817 * then we need to make the drive anonymous until the device
2818 * can be removed. If this is a drive with no device backing
2819 * then we can just get rid of the block driver state right here.
2821 if (blk_get_attached_dev(blk
)) {
2822 blk_hide_on_behalf_of_hmp_drive_del(blk
);
2823 /* Further I/O must not pause the guest */
2824 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2825 BLOCKDEV_ON_ERROR_REPORT
);
2830 aio_context_release(aio_context
);
2833 void qmp_block_resize(bool has_device
, const char *device
,
2834 bool has_node_name
, const char *node_name
,
2835 int64_t size
, Error
**errp
)
2837 Error
*local_err
= NULL
;
2838 BlockDriverState
*bs
;
2839 AioContext
*aio_context
;
2842 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2843 has_node_name
? node_name
: NULL
,
2846 error_propagate(errp
, local_err
);
2850 aio_context
= bdrv_get_aio_context(bs
);
2851 aio_context_acquire(aio_context
);
2853 if (!bdrv_is_first_non_filter(bs
)) {
2854 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2859 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2863 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2864 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2868 /* complete all in-flight operations before resizing the device */
2871 ret
= bdrv_truncate(bs
, size
);
2876 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2879 error_setg(errp
, QERR_UNSUPPORTED
);
2882 error_setg(errp
, "Device '%s' is read only", device
);
2885 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2888 error_setg_errno(errp
, -ret
, "Could not resize");
2893 aio_context_release(aio_context
);
2896 static void block_job_cb(void *opaque
, int ret
)
2898 /* Note that this function may be executed from another AioContext besides
2899 * the QEMU main loop. If you need to access anything that assumes the
2900 * QEMU global mutex, use a BH or introduce a mutex.
2903 BlockDriverState
*bs
= opaque
;
2904 const char *msg
= NULL
;
2906 trace_block_job_cb(bs
, bs
->job
, ret
);
2911 msg
= strerror(-ret
);
2914 if (block_job_is_cancelled(bs
->job
)) {
2915 block_job_event_cancelled(bs
->job
);
2917 block_job_event_completed(bs
->job
, msg
);
2921 void qmp_block_stream(const char *device
,
2922 bool has_base
, const char *base
,
2923 bool has_backing_file
, const char *backing_file
,
2924 bool has_speed
, int64_t speed
,
2925 bool has_on_error
, BlockdevOnError on_error
,
2929 BlockDriverState
*bs
;
2930 BlockDriverState
*base_bs
= NULL
;
2931 AioContext
*aio_context
;
2932 Error
*local_err
= NULL
;
2933 const char *base_name
= NULL
;
2935 if (!has_on_error
) {
2936 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2939 blk
= blk_by_name(device
);
2941 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2942 "Device '%s' not found", device
);
2946 aio_context
= blk_get_aio_context(blk
);
2947 aio_context_acquire(aio_context
);
2949 if (!blk_is_available(blk
)) {
2950 error_setg(errp
, "Device '%s' has no medium", device
);
2955 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2960 base_bs
= bdrv_find_backing_image(bs
, base
);
2961 if (base_bs
== NULL
) {
2962 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2965 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2969 /* if we are streaming the entire chain, the result will have no backing
2970 * file, and specifying one is therefore an error */
2971 if (base_bs
== NULL
&& has_backing_file
) {
2972 error_setg(errp
, "backing file specified, but streaming the "
2977 /* backing_file string overrides base bs filename */
2978 base_name
= has_backing_file
? backing_file
: base_name
;
2980 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
2981 on_error
, block_job_cb
, bs
, &local_err
);
2983 error_propagate(errp
, local_err
);
2987 trace_qmp_block_stream(bs
, bs
->job
);
2990 aio_context_release(aio_context
);
2993 void qmp_block_commit(const char *device
,
2994 bool has_base
, const char *base
,
2995 bool has_top
, const char *top
,
2996 bool has_backing_file
, const char *backing_file
,
2997 bool has_speed
, int64_t speed
,
3001 BlockDriverState
*bs
;
3002 BlockDriverState
*base_bs
, *top_bs
;
3003 AioContext
*aio_context
;
3004 Error
*local_err
= NULL
;
3005 /* This will be part of the QMP command, if/when the
3006 * BlockdevOnError change for blkmirror makes it in
3008 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3015 * libvirt relies on the DeviceNotFound error class in order to probe for
3016 * live commit feature versions; for this to work, we must make sure to
3017 * perform the device lookup before any generic errors that may occur in a
3018 * scenario in which all optional arguments are omitted. */
3019 blk
= blk_by_name(device
);
3021 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3022 "Device '%s' not found", device
);
3026 aio_context
= blk_get_aio_context(blk
);
3027 aio_context_acquire(aio_context
);
3029 if (!blk_is_available(blk
)) {
3030 error_setg(errp
, "Device '%s' has no medium", device
);
3035 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3039 /* default top_bs is the active layer */
3042 if (has_top
&& top
) {
3043 if (strcmp(bs
->filename
, top
) != 0) {
3044 top_bs
= bdrv_find_backing_image(bs
, top
);
3048 if (top_bs
== NULL
) {
3049 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3053 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3055 if (has_base
&& base
) {
3056 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3058 base_bs
= bdrv_find_base(top_bs
);
3061 if (base_bs
== NULL
) {
3062 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3066 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3068 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3072 /* Do not allow attempts to commit an image into itself */
3073 if (top_bs
== base_bs
) {
3074 error_setg(errp
, "cannot commit an image into itself");
3079 if (has_backing_file
) {
3080 error_setg(errp
, "'backing-file' specified,"
3081 " but 'top' is the active layer");
3084 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
3087 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
3088 has_backing_file
? backing_file
: NULL
, &local_err
);
3090 if (local_err
!= NULL
) {
3091 error_propagate(errp
, local_err
);
3096 aio_context_release(aio_context
);
3099 static void do_drive_backup(const char *device
, const char *target
,
3100 bool has_format
, const char *format
,
3101 enum MirrorSyncMode sync
,
3102 bool has_mode
, enum NewImageMode mode
,
3103 bool has_speed
, int64_t speed
,
3104 bool has_bitmap
, const char *bitmap
,
3105 bool has_on_source_error
,
3106 BlockdevOnError on_source_error
,
3107 bool has_on_target_error
,
3108 BlockdevOnError on_target_error
,
3109 BlockJobTxn
*txn
, Error
**errp
)
3112 BlockDriverState
*bs
;
3113 BlockDriverState
*target_bs
;
3114 BlockDriverState
*source
= NULL
;
3115 BdrvDirtyBitmap
*bmap
= NULL
;
3116 AioContext
*aio_context
;
3117 QDict
*options
= NULL
;
3118 Error
*local_err
= NULL
;
3126 if (!has_on_source_error
) {
3127 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3129 if (!has_on_target_error
) {
3130 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3133 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3136 blk
= blk_by_name(device
);
3138 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3139 "Device '%s' not found", device
);
3143 aio_context
= blk_get_aio_context(blk
);
3144 aio_context_acquire(aio_context
);
3146 /* Although backup_run has this check too, we need to use bs->drv below, so
3147 * do an early check redundantly. */
3148 if (!blk_is_available(blk
)) {
3149 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3155 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3158 /* Early check to avoid creating target */
3159 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3163 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3165 /* See if we have a backing HD we can use to create our new image
3167 if (sync
== MIRROR_SYNC_MODE_TOP
) {
3168 source
= backing_bs(bs
);
3170 sync
= MIRROR_SYNC_MODE_FULL
;
3173 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3177 size
= bdrv_getlength(bs
);
3179 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3183 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
3186 bdrv_img_create(target
, format
, source
->filename
,
3187 source
->drv
->format_name
, NULL
,
3188 size
, flags
, &local_err
, false);
3190 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
3191 size
, flags
, &local_err
, false);
3196 error_propagate(errp
, local_err
);
3201 options
= qdict_new();
3202 qdict_put(options
, "driver", qstring_from_str(format
));
3206 ret
= bdrv_open(&target_bs
, target
, NULL
, options
, flags
, &local_err
);
3208 error_propagate(errp
, local_err
);
3212 bdrv_set_aio_context(target_bs
, aio_context
);
3215 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
3217 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
3218 bdrv_unref(target_bs
);
3223 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
3224 on_source_error
, on_target_error
,
3225 block_job_cb
, bs
, txn
, &local_err
);
3226 if (local_err
!= NULL
) {
3227 bdrv_unref(target_bs
);
3228 error_propagate(errp
, local_err
);
3233 aio_context_release(aio_context
);
3236 void qmp_drive_backup(const char *device
, const char *target
,
3237 bool has_format
, const char *format
,
3238 enum MirrorSyncMode sync
,
3239 bool has_mode
, enum NewImageMode mode
,
3240 bool has_speed
, int64_t speed
,
3241 bool has_bitmap
, const char *bitmap
,
3242 bool has_on_source_error
, BlockdevOnError on_source_error
,
3243 bool has_on_target_error
, BlockdevOnError on_target_error
,
3246 return do_drive_backup(device
, target
, has_format
, format
, sync
,
3247 has_mode
, mode
, has_speed
, speed
,
3249 has_on_source_error
, on_source_error
,
3250 has_on_target_error
, on_target_error
,
3254 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3256 return bdrv_named_nodes_list(errp
);
3259 void do_blockdev_backup(const char *device
, const char *target
,
3260 enum MirrorSyncMode sync
,
3261 bool has_speed
, int64_t speed
,
3262 bool has_on_source_error
,
3263 BlockdevOnError on_source_error
,
3264 bool has_on_target_error
,
3265 BlockdevOnError on_target_error
,
3266 BlockJobTxn
*txn
, Error
**errp
)
3268 BlockBackend
*blk
, *target_blk
;
3269 BlockDriverState
*bs
;
3270 BlockDriverState
*target_bs
;
3271 Error
*local_err
= NULL
;
3272 AioContext
*aio_context
;
3277 if (!has_on_source_error
) {
3278 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3280 if (!has_on_target_error
) {
3281 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3284 blk
= blk_by_name(device
);
3286 error_setg(errp
, "Device '%s' not found", device
);
3290 aio_context
= blk_get_aio_context(blk
);
3291 aio_context_acquire(aio_context
);
3293 if (!blk_is_available(blk
)) {
3294 error_setg(errp
, "Device '%s' has no medium", device
);
3299 target_blk
= blk_by_name(target
);
3301 error_setg(errp
, "Device '%s' not found", target
);
3305 if (!blk_is_available(target_blk
)) {
3306 error_setg(errp
, "Device '%s' has no medium", target
);
3309 target_bs
= blk_bs(target_blk
);
3311 bdrv_ref(target_bs
);
3312 bdrv_set_aio_context(target_bs
, aio_context
);
3313 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
3314 on_target_error
, block_job_cb
, bs
, txn
, &local_err
);
3315 if (local_err
!= NULL
) {
3316 bdrv_unref(target_bs
);
3317 error_propagate(errp
, local_err
);
3320 aio_context_release(aio_context
);
3323 void qmp_blockdev_backup(const char *device
, const char *target
,
3324 enum MirrorSyncMode sync
,
3325 bool has_speed
, int64_t speed
,
3326 bool has_on_source_error
,
3327 BlockdevOnError on_source_error
,
3328 bool has_on_target_error
,
3329 BlockdevOnError on_target_error
,
3332 do_blockdev_backup(device
, target
, sync
, has_speed
, speed
,
3333 has_on_source_error
, on_source_error
,
3334 has_on_target_error
, on_target_error
,
3338 /* Parameter check and block job starting for drive mirroring.
3339 * Caller should hold @device and @target's aio context (must be the same).
3341 static void blockdev_mirror_common(BlockDriverState
*bs
,
3342 BlockDriverState
*target
,
3343 bool has_replaces
, const char *replaces
,
3344 enum MirrorSyncMode sync
,
3345 bool has_speed
, int64_t speed
,
3346 bool has_granularity
, uint32_t granularity
,
3347 bool has_buf_size
, int64_t buf_size
,
3348 bool has_on_source_error
,
3349 BlockdevOnError on_source_error
,
3350 bool has_on_target_error
,
3351 BlockdevOnError on_target_error
,
3352 bool has_unmap
, bool unmap
,
3359 if (!has_on_source_error
) {
3360 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3362 if (!has_on_target_error
) {
3363 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3365 if (!has_granularity
) {
3368 if (!has_buf_size
) {
3375 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3376 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3377 "a value in range [512B, 64MB]");
3380 if (granularity
& (granularity
- 1)) {
3381 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3386 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3389 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3393 error_setg(errp
, "Cannot mirror to an attached block device");
3397 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3398 sync
= MIRROR_SYNC_MODE_FULL
;
3401 /* pass the node name to replace to mirror start since it's loose coupling
3402 * and will allow to check whether the node still exist at mirror completion
3404 mirror_start(bs
, target
,
3405 has_replaces
? replaces
: NULL
,
3406 speed
, granularity
, buf_size
, sync
,
3407 on_source_error
, on_target_error
, unmap
,
3408 block_job_cb
, bs
, errp
);
3411 void qmp_drive_mirror(const char *device
, const char *target
,
3412 bool has_format
, const char *format
,
3413 bool has_node_name
, const char *node_name
,
3414 bool has_replaces
, const char *replaces
,
3415 enum MirrorSyncMode sync
,
3416 bool has_mode
, enum NewImageMode mode
,
3417 bool has_speed
, int64_t speed
,
3418 bool has_granularity
, uint32_t granularity
,
3419 bool has_buf_size
, int64_t buf_size
,
3420 bool has_on_source_error
, BlockdevOnError on_source_error
,
3421 bool has_on_target_error
, BlockdevOnError on_target_error
,
3422 bool has_unmap
, bool unmap
,
3425 BlockDriverState
*bs
;
3427 BlockDriverState
*source
, *target_bs
;
3428 AioContext
*aio_context
;
3429 Error
*local_err
= NULL
;
3430 QDict
*options
= NULL
;
3435 blk
= blk_by_name(device
);
3437 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3438 "Device '%s' not found", device
);
3442 aio_context
= blk_get_aio_context(blk
);
3443 aio_context_acquire(aio_context
);
3445 if (!blk_is_available(blk
)) {
3446 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3451 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3455 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3458 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3459 source
= backing_bs(bs
);
3460 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3461 sync
= MIRROR_SYNC_MODE_FULL
;
3463 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3467 size
= bdrv_getlength(bs
);
3469 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3474 BlockDriverState
*to_replace_bs
;
3475 AioContext
*replace_aio_context
;
3476 int64_t replace_size
;
3478 if (!has_node_name
) {
3479 error_setg(errp
, "a node-name must be provided when replacing a"
3480 " named node of the graph");
3484 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
3486 if (!to_replace_bs
) {
3487 error_propagate(errp
, local_err
);
3491 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3492 aio_context_acquire(replace_aio_context
);
3493 replace_size
= bdrv_getlength(to_replace_bs
);
3494 aio_context_release(replace_aio_context
);
3496 if (size
!= replace_size
) {
3497 error_setg(errp
, "cannot replace image with a mirror image of "
3503 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3504 && mode
!= NEW_IMAGE_MODE_EXISTING
)
3506 /* create new image w/o backing file */
3508 bdrv_img_create(target
, format
,
3509 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3512 case NEW_IMAGE_MODE_EXISTING
:
3514 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3515 /* create new image with backing file */
3516 bdrv_img_create(target
, format
,
3518 source
->drv
->format_name
,
3519 NULL
, size
, flags
, &local_err
, false);
3527 error_propagate(errp
, local_err
);
3531 options
= qdict_new();
3532 if (has_node_name
) {
3533 qdict_put(options
, "node-name", qstring_from_str(node_name
));
3536 qdict_put(options
, "driver", qstring_from_str(format
));
3539 /* Mirroring takes care of copy-on-write using the source's backing
3543 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
3544 flags
| BDRV_O_NO_BACKING
, &local_err
);
3546 error_propagate(errp
, local_err
);
3550 bdrv_set_aio_context(target_bs
, aio_context
);
3552 blockdev_mirror_common(bs
, target_bs
,
3553 has_replaces
, replaces
, sync
,
3555 has_granularity
, granularity
,
3556 has_buf_size
, buf_size
,
3557 has_on_source_error
, on_source_error
,
3558 has_on_target_error
, on_target_error
,
3562 error_propagate(errp
, local_err
);
3563 bdrv_unref(target_bs
);
3566 aio_context_release(aio_context
);
3569 void qmp_blockdev_mirror(const char *device
, const char *target
,
3570 bool has_replaces
, const char *replaces
,
3571 MirrorSyncMode sync
,
3572 bool has_speed
, int64_t speed
,
3573 bool has_granularity
, uint32_t granularity
,
3574 bool has_buf_size
, int64_t buf_size
,
3575 bool has_on_source_error
,
3576 BlockdevOnError on_source_error
,
3577 bool has_on_target_error
,
3578 BlockdevOnError on_target_error
,
3581 BlockDriverState
*bs
;
3583 BlockDriverState
*target_bs
;
3584 AioContext
*aio_context
;
3585 Error
*local_err
= NULL
;
3587 blk
= blk_by_name(device
);
3589 error_setg(errp
, "Device '%s' not found", device
);
3595 error_setg(errp
, "Device '%s' has no media", device
);
3599 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3604 aio_context
= bdrv_get_aio_context(bs
);
3605 aio_context_acquire(aio_context
);
3607 bdrv_ref(target_bs
);
3608 bdrv_set_aio_context(target_bs
, aio_context
);
3610 blockdev_mirror_common(bs
, target_bs
,
3611 has_replaces
, replaces
, sync
,
3613 has_granularity
, granularity
,
3614 has_buf_size
, buf_size
,
3615 has_on_source_error
, on_source_error
,
3616 has_on_target_error
, on_target_error
,
3620 error_propagate(errp
, local_err
);
3621 bdrv_unref(target_bs
);
3624 aio_context_release(aio_context
);
3627 /* Get the block job for a given device name and acquire its AioContext */
3628 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
3632 BlockDriverState
*bs
;
3634 *aio_context
= NULL
;
3636 blk
= blk_by_name(device
);
3641 *aio_context
= blk_get_aio_context(blk
);
3642 aio_context_acquire(*aio_context
);
3644 if (!blk_is_available(blk
)) {
3656 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3657 "No active block job on device '%s'", device
);
3659 aio_context_release(*aio_context
);
3660 *aio_context
= NULL
;
3665 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3667 AioContext
*aio_context
;
3668 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3674 block_job_set_speed(job
, speed
, errp
);
3675 aio_context_release(aio_context
);
3678 void qmp_block_job_cancel(const char *device
,
3679 bool has_force
, bool force
, Error
**errp
)
3681 AioContext
*aio_context
;
3682 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3692 if (job
->user_paused
&& !force
) {
3693 error_setg(errp
, "The block job for device '%s' is currently paused",
3698 trace_qmp_block_job_cancel(job
);
3699 block_job_cancel(job
);
3701 aio_context_release(aio_context
);
3704 void qmp_block_job_pause(const char *device
, Error
**errp
)
3706 AioContext
*aio_context
;
3707 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3709 if (!job
|| job
->user_paused
) {
3713 job
->user_paused
= true;
3714 trace_qmp_block_job_pause(job
);
3715 block_job_pause(job
);
3716 aio_context_release(aio_context
);
3719 void qmp_block_job_resume(const char *device
, Error
**errp
)
3721 AioContext
*aio_context
;
3722 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3724 if (!job
|| !job
->user_paused
) {
3728 job
->user_paused
= false;
3729 trace_qmp_block_job_resume(job
);
3730 block_job_resume(job
);
3731 aio_context_release(aio_context
);
3734 void qmp_block_job_complete(const char *device
, Error
**errp
)
3736 AioContext
*aio_context
;
3737 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3743 trace_qmp_block_job_complete(job
);
3744 block_job_complete(job
, errp
);
3745 aio_context_release(aio_context
);
3748 void qmp_change_backing_file(const char *device
,
3749 const char *image_node_name
,
3750 const char *backing_file
,
3754 BlockDriverState
*bs
= NULL
;
3755 AioContext
*aio_context
;
3756 BlockDriverState
*image_bs
= NULL
;
3757 Error
*local_err
= NULL
;
3762 blk
= blk_by_name(device
);
3764 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3765 "Device '%s' not found", device
);
3769 aio_context
= blk_get_aio_context(blk
);
3770 aio_context_acquire(aio_context
);
3772 if (!blk_is_available(blk
)) {
3773 error_setg(errp
, "Device '%s' has no medium", device
);
3778 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3780 error_propagate(errp
, local_err
);
3785 error_setg(errp
, "image file not found");
3789 if (bdrv_find_base(image_bs
) == image_bs
) {
3790 error_setg(errp
, "not allowing backing file change on an image "
3791 "without a backing file");
3795 /* even though we are not necessarily operating on bs, we need it to
3796 * determine if block ops are currently prohibited on the chain */
3797 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3801 /* final sanity check */
3802 if (!bdrv_chain_contains(bs
, image_bs
)) {
3803 error_setg(errp
, "'%s' and image file are not in the same chain",
3808 /* if not r/w, reopen to make r/w */
3809 open_flags
= image_bs
->open_flags
;
3810 ro
= bdrv_is_read_only(image_bs
);
3813 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3815 error_propagate(errp
, local_err
);
3820 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3821 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3824 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3826 /* don't exit here, so we can try to restore open flags if
3831 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3833 error_propagate(errp
, local_err
); /* will preserve prior errp */
3838 aio_context_release(aio_context
);
3841 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3843 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3844 BlockDriverState
*bs
;
3845 BlockBackend
*blk
= NULL
;
3848 Error
*local_err
= NULL
;
3850 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3851 * cache.direct=false instead of silently switching to aio=threads, except
3852 * when called from drive_new().
3854 * For now, simply forbidding the combination for all drivers will do. */
3855 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3856 bool direct
= options
->has_cache
&&
3857 options
->cache
->has_direct
&&
3858 options
->cache
->direct
;
3860 error_setg(errp
, "aio=native requires cache.direct=true");
3865 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
), NULL
, &options
,
3868 error_propagate(errp
, local_err
);
3872 obj
= qmp_output_get_qobject(ov
);
3873 qdict
= qobject_to_qdict(obj
);
3875 qdict_flatten(qdict
);
3877 if (options
->has_id
) {
3878 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3880 error_propagate(errp
, local_err
);
3886 if (!qdict_get_try_str(qdict
, "node-name")) {
3887 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3892 bs
= bds_tree_init(qdict
, errp
);
3897 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3900 if (bs
&& bdrv_key_required(bs
)) {
3904 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3907 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3912 qmp_output_visitor_cleanup(ov
);
3915 void qmp_x_blockdev_del(bool has_id
, const char *id
,
3916 bool has_node_name
, const char *node_name
, Error
**errp
)
3918 AioContext
*aio_context
;
3920 BlockDriverState
*bs
;
3922 if (has_id
&& has_node_name
) {
3923 error_setg(errp
, "Only one of id and node-name must be specified");
3925 } else if (!has_id
&& !has_node_name
) {
3926 error_setg(errp
, "No block device specified");
3931 blk
= blk_by_name(id
);
3933 error_setg(errp
, "Cannot find block backend %s", id
);
3936 if (blk_get_refcnt(blk
) > 1) {
3937 error_setg(errp
, "Block backend %s is in use", id
);
3941 aio_context
= blk_get_aio_context(blk
);
3943 bs
= bdrv_find_node(node_name
);
3945 error_setg(errp
, "Cannot find node %s", node_name
);
3950 error_setg(errp
, "Node %s is in use by %s",
3951 node_name
, blk_name(blk
));
3954 aio_context
= bdrv_get_aio_context(bs
);
3957 aio_context_acquire(aio_context
);
3960 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3964 if (!blk
&& !bs
->monitor_list
.tqe_prev
) {
3965 error_setg(errp
, "Node %s is not owned by the monitor",
3970 if (bs
->refcnt
> 1) {
3971 error_setg(errp
, "Block device %s is in use",
3972 bdrv_get_device_or_node_name(bs
));
3980 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3985 aio_context_release(aio_context
);
3988 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3990 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3991 BlockDriverState
*bs
;
3993 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3994 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
3996 aio_context_acquire(aio_context
);
3999 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
4000 elem
->value
= block_job_query(bs
->job
);
4002 p_next
= &elem
->next
;
4005 aio_context_release(aio_context
);
4011 QemuOptsList qemu_common_drive_opts
= {
4013 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4017 .type
= QEMU_OPT_BOOL
,
4018 .help
= "enable/disable snapshot mode",
4021 .type
= QEMU_OPT_STRING
,
4022 .help
= "discard operation (ignore/off, unmap/on)",
4025 .type
= QEMU_OPT_STRING
,
4026 .help
= "host AIO implementation (threads, native)",
4029 .type
= QEMU_OPT_STRING
,
4030 .help
= "disk format (raw, qcow2, ...)",
4033 .type
= QEMU_OPT_STRING
,
4034 .help
= "read error action",
4037 .type
= QEMU_OPT_STRING
,
4038 .help
= "write error action",
4040 .name
= "read-only",
4041 .type
= QEMU_OPT_BOOL
,
4042 .help
= "open drive file as read-only",
4044 .name
= "throttling.iops-total",
4045 .type
= QEMU_OPT_NUMBER
,
4046 .help
= "limit total I/O operations per second",
4048 .name
= "throttling.iops-read",
4049 .type
= QEMU_OPT_NUMBER
,
4050 .help
= "limit read operations per second",
4052 .name
= "throttling.iops-write",
4053 .type
= QEMU_OPT_NUMBER
,
4054 .help
= "limit write operations per second",
4056 .name
= "throttling.bps-total",
4057 .type
= QEMU_OPT_NUMBER
,
4058 .help
= "limit total bytes per second",
4060 .name
= "throttling.bps-read",
4061 .type
= QEMU_OPT_NUMBER
,
4062 .help
= "limit read bytes per second",
4064 .name
= "throttling.bps-write",
4065 .type
= QEMU_OPT_NUMBER
,
4066 .help
= "limit write bytes per second",
4068 .name
= "throttling.iops-total-max",
4069 .type
= QEMU_OPT_NUMBER
,
4070 .help
= "I/O operations burst",
4072 .name
= "throttling.iops-read-max",
4073 .type
= QEMU_OPT_NUMBER
,
4074 .help
= "I/O operations read burst",
4076 .name
= "throttling.iops-write-max",
4077 .type
= QEMU_OPT_NUMBER
,
4078 .help
= "I/O operations write burst",
4080 .name
= "throttling.bps-total-max",
4081 .type
= QEMU_OPT_NUMBER
,
4082 .help
= "total bytes burst",
4084 .name
= "throttling.bps-read-max",
4085 .type
= QEMU_OPT_NUMBER
,
4086 .help
= "total bytes read burst",
4088 .name
= "throttling.bps-write-max",
4089 .type
= QEMU_OPT_NUMBER
,
4090 .help
= "total bytes write burst",
4092 .name
= "throttling.iops-size",
4093 .type
= QEMU_OPT_NUMBER
,
4094 .help
= "when limiting by iops max size of an I/O in bytes",
4096 .name
= "throttling.group",
4097 .type
= QEMU_OPT_STRING
,
4098 .help
= "name of the block throttling group",
4100 .name
= "copy-on-read",
4101 .type
= QEMU_OPT_BOOL
,
4102 .help
= "copy read data from backing file into image file",
4104 .name
= "detect-zeroes",
4105 .type
= QEMU_OPT_STRING
,
4106 .help
= "try to optimize zero writes (off, on, unmap)",
4108 .name
= "stats-account-invalid",
4109 .type
= QEMU_OPT_BOOL
,
4110 .help
= "whether to account for invalid I/O operations "
4111 "in the statistics",
4113 .name
= "stats-account-failed",
4114 .type
= QEMU_OPT_BOOL
,
4115 .help
= "whether to account for failed I/O operations "
4116 "in the statistics",
4118 { /* end of list */ }
4122 static QemuOptsList qemu_root_bds_opts
= {
4124 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4128 .type
= QEMU_OPT_STRING
,
4129 .help
= "discard operation (ignore/off, unmap/on)",
4132 .type
= QEMU_OPT_STRING
,
4133 .help
= "host AIO implementation (threads, native)",
4135 .name
= "read-only",
4136 .type
= QEMU_OPT_BOOL
,
4137 .help
= "open drive file as read-only",
4139 .name
= "copy-on-read",
4140 .type
= QEMU_OPT_BOOL
,
4141 .help
= "copy read data from backing file into image file",
4143 .name
= "detect-zeroes",
4144 .type
= QEMU_OPT_STRING
,
4145 .help
= "try to optimize zero writes (off, on, unmap)",
4147 { /* end of list */ }
4151 QemuOptsList qemu_drive_opts
= {
4153 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4156 * no elements => accept any params
4157 * validation will happen later
4159 { /* end of list */ }