2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
9 * This file incorporates work covered by the following copyright and
12 * Copyright (c) 2003-2008 Fabrice Bellard
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "sysemu/block-backend.h"
34 #include "sysemu/blockdev.h"
35 #include "hw/block/block.h"
36 #include "block/blockjob.h"
37 #include "block/throttle-groups.h"
38 #include "monitor/monitor.h"
39 #include "qemu/option.h"
40 #include "qemu/config-file.h"
41 #include "qapi/qmp/types.h"
42 #include "qapi-visit.h"
43 #include "qapi/qmp-output-visitor.h"
44 #include "qapi/util.h"
45 #include "sysemu/sysemu.h"
46 #include "block/block_int.h"
47 #include "qmp-commands.h"
49 #include "sysemu/arch_init.h"
51 static const char *const if_name
[IF_COUNT
] = {
55 [IF_FLOPPY
] = "floppy",
56 [IF_PFLASH
] = "pflash",
59 [IF_VIRTIO
] = "virtio",
63 static int if_max_devs
[IF_COUNT
] = {
65 * Do not change these numbers! They govern how drive option
66 * index maps to unit and bus. That mapping is ABI.
68 * All controllers used to imlement if=T drives need to support
69 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
70 * Otherwise, some index values map to "impossible" bus, unit
73 * For instance, if you change [IF_SCSI] to 255, -drive
74 * if=scsi,index=12 no longer means bus=1,unit=5, but
75 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
76 * the drive can't be set up. Regression.
83 * Boards may call this to offer board-by-board overrides
84 * of the default, global values.
86 void override_max_devs(BlockInterfaceType type
, int max_devs
)
95 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
96 dinfo
= blk_legacy_dinfo(blk
);
97 if (dinfo
->type
== type
) {
98 fprintf(stderr
, "Cannot override units-per-bus property of"
99 " the %s interface, because a drive of that type has"
100 " already been added.\n", if_name
[type
]);
101 g_assert_not_reached();
105 if_max_devs
[type
] = max_devs
;
109 * We automatically delete the drive when a device using it gets
110 * unplugged. Questionable feature, but we can't just drop it.
111 * Device models call blockdev_mark_auto_del() to schedule the
112 * automatic deletion, and generic qdev code calls blockdev_auto_del()
113 * when deletion is actually safe.
115 void blockdev_mark_auto_del(BlockBackend
*blk
)
117 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
118 BlockDriverState
*bs
= blk_bs(blk
);
119 AioContext
*aio_context
;
125 aio_context
= bdrv_get_aio_context(bs
);
126 aio_context_acquire(aio_context
);
129 block_job_cancel(bs
->job
);
132 aio_context_release(aio_context
);
137 void blockdev_auto_del(BlockBackend
*blk
)
139 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
141 if (dinfo
&& dinfo
->auto_del
) {
147 * Returns the current mapping of how many units per bus
148 * a particular interface can support.
150 * A positive integer indicates n units per bus.
151 * 0 implies the mapping has not been established.
152 * -1 indicates an invalid BlockInterfaceType was given.
154 int drive_get_max_devs(BlockInterfaceType type
)
156 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
157 return if_max_devs
[type
];
163 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
165 int max_devs
= if_max_devs
[type
];
166 return max_devs
? index
/ max_devs
: 0;
169 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
171 int max_devs
= if_max_devs
[type
];
172 return max_devs
? index
% max_devs
: index
;
175 QemuOpts
*drive_def(const char *optstr
)
177 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
180 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
185 opts
= drive_def(optstr
);
189 if (type
!= IF_DEFAULT
) {
190 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
193 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
196 qemu_opt_set(opts
, "file", file
, &error_abort
);
200 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
205 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
206 dinfo
= blk_legacy_dinfo(blk
);
207 if (dinfo
&& dinfo
->type
== type
208 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
216 bool drive_check_orphaned(void)
222 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
223 dinfo
= blk_legacy_dinfo(blk
);
224 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
225 /* Unless this is a default drive, this may be an oversight. */
226 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
227 dinfo
->type
!= IF_NONE
) {
228 fprintf(stderr
, "Warning: Orphaned drive without device: "
229 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
230 blk_name(blk
), blk_bs(blk
)->filename
, if_name
[dinfo
->type
],
231 dinfo
->bus
, dinfo
->unit
);
239 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
241 return drive_get(type
,
242 drive_index_to_bus_id(type
, index
),
243 drive_index_to_unit_id(type
, index
));
246 int drive_get_max_bus(BlockInterfaceType type
)
253 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
254 dinfo
= blk_legacy_dinfo(blk
);
255 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
256 max_bus
= dinfo
->bus
;
262 /* Get a block device. This should only be used for single-drive devices
263 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
265 DriveInfo
*drive_get_next(BlockInterfaceType type
)
267 static int next_block_unit
[IF_COUNT
];
269 return drive_get(type
, 0, next_block_unit
[type
]++);
272 static void bdrv_format_print(void *opaque
, const char *name
)
274 error_printf(" %s", name
);
279 BlockDriverState
*bs
;
282 static void bdrv_put_ref_bh(void *opaque
)
284 BDRVPutRefBH
*s
= opaque
;
287 qemu_bh_delete(s
->bh
);
292 * Release a BDS reference in a BH
294 * It is not safe to use bdrv_unref() from a callback function when the callers
295 * still need the BlockDriverState. In such cases we schedule a BH to release
298 static void bdrv_put_ref_bh_schedule(BlockDriverState
*bs
)
302 s
= g_new(BDRVPutRefBH
, 1);
303 s
->bh
= qemu_bh_new(bdrv_put_ref_bh
, s
);
305 qemu_bh_schedule(s
->bh
);
308 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
310 if (!strcmp(buf
, "ignore")) {
311 return BLOCKDEV_ON_ERROR_IGNORE
;
312 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
313 return BLOCKDEV_ON_ERROR_ENOSPC
;
314 } else if (!strcmp(buf
, "stop")) {
315 return BLOCKDEV_ON_ERROR_STOP
;
316 } else if (!strcmp(buf
, "report")) {
317 return BLOCKDEV_ON_ERROR_REPORT
;
319 error_setg(errp
, "'%s' invalid %s error action",
320 buf
, is_read
? "read" : "write");
325 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
327 if (throttle_conflicting(cfg
)) {
328 error_setg(errp
, "bps/iops/max total values and read/write values"
329 " cannot be used at the same time");
333 if (!throttle_is_valid(cfg
)) {
334 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
341 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
343 /* Takes the ownership of bs_opts */
344 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
350 int on_read_error
, on_write_error
;
352 BlockDriverState
*bs
;
359 bool has_driver_specific_opts
;
360 BlockdevDetectZeroesOptions detect_zeroes
;
361 const char *throttling_group
;
363 /* Check common options by copying from bs_opts to opts, all other options
364 * stay in bs_opts for processing by bdrv_open(). */
365 id
= qdict_get_try_str(bs_opts
, "id");
366 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
368 error_propagate(errp
, error
);
372 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
374 error_propagate(errp
, error
);
379 qdict_del(bs_opts
, "id");
382 has_driver_specific_opts
= !!qdict_size(bs_opts
);
384 /* extract parameters */
385 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
386 ro
= qemu_opt_get_bool(opts
, "read-only", 0);
387 copy_on_read
= qemu_opt_get_bool(opts
, "copy-on-read", false);
389 if ((buf
= qemu_opt_get(opts
, "discard")) != NULL
) {
390 if (bdrv_parse_discard_flags(buf
, &bdrv_flags
) != 0) {
391 error_setg(errp
, "invalid discard option");
396 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true)) {
397 bdrv_flags
|= BDRV_O_CACHE_WB
;
399 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_DIRECT
, false)) {
400 bdrv_flags
|= BDRV_O_NOCACHE
;
402 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_NO_FLUSH
, false)) {
403 bdrv_flags
|= BDRV_O_NO_FLUSH
;
406 #ifdef CONFIG_LINUX_AIO
407 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
408 if (!strcmp(buf
, "native")) {
409 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
410 } else if (!strcmp(buf
, "threads")) {
411 /* this is the default */
413 error_setg(errp
, "invalid aio option");
419 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
420 if (is_help_option(buf
)) {
421 error_printf("Supported formats:");
422 bdrv_iterate_format(bdrv_format_print
, NULL
);
427 if (qdict_haskey(bs_opts
, "driver")) {
428 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
431 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
434 /* disk I/O throttling */
435 memset(&cfg
, 0, sizeof(cfg
));
436 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
=
437 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
438 cfg
.buckets
[THROTTLE_BPS_READ
].avg
=
439 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
440 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
=
441 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
442 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
=
443 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
444 cfg
.buckets
[THROTTLE_OPS_READ
].avg
=
445 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
446 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
=
447 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
449 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
=
450 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
451 cfg
.buckets
[THROTTLE_BPS_READ
].max
=
452 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
453 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
=
454 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
455 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
=
456 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
457 cfg
.buckets
[THROTTLE_OPS_READ
].max
=
458 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
459 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
=
460 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
462 cfg
.op_size
= qemu_opt_get_number(opts
, "throttling.iops-size", 0);
464 throttling_group
= qemu_opt_get(opts
, "throttling.group");
466 if (!check_throttle_config(&cfg
, &error
)) {
467 error_propagate(errp
, error
);
471 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
472 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
473 on_write_error
= parse_block_error_action(buf
, 0, &error
);
475 error_propagate(errp
, error
);
480 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
481 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
482 on_read_error
= parse_block_error_action(buf
, 1, &error
);
484 error_propagate(errp
, error
);
490 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
491 qemu_opt_get(opts
, "detect-zeroes"),
492 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX
,
493 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
496 error_propagate(errp
, error
);
500 if (detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
501 !(bdrv_flags
& BDRV_O_UNMAP
)) {
502 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
503 "without setting discard operation to unmap");
508 if ((!file
|| !*file
) && !has_driver_specific_opts
) {
509 blk
= blk_new_with_bs(qemu_opts_id(opts
), errp
);
515 bs
->open_flags
= snapshot
? BDRV_O_SNAPSHOT
: 0;
520 if (file
&& !*file
) {
525 /* always use cache=unsafe with snapshot */
526 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
527 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
531 bdrv_flags
|= BDRV_O_COPY_ON_READ
;
534 if (runstate_check(RUN_STATE_INMIGRATE
)) {
535 bdrv_flags
|= BDRV_O_INCOMING
;
538 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
540 blk
= blk_new_open(qemu_opts_id(opts
), file
, NULL
, bs_opts
, bdrv_flags
,
548 bs
->detect_zeroes
= detect_zeroes
;
550 bdrv_set_on_error(bs
, on_read_error
, on_write_error
);
552 /* disk I/O throttling */
553 if (throttle_enabled(&cfg
)) {
554 if (!throttling_group
) {
555 throttling_group
= blk_name(blk
);
557 bdrv_io_limits_enable(bs
, throttling_group
);
558 bdrv_set_io_limits(bs
, &cfg
);
561 if (bdrv_key_required(bs
)) {
576 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
581 value
= qemu_opt_get(opts
, from
);
583 if (qemu_opt_find(opts
, to
)) {
584 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
585 "same time", to
, from
);
590 /* rename all items in opts */
591 while ((value
= qemu_opt_get(opts
, from
))) {
592 qemu_opt_set(opts
, to
, value
, &error_abort
);
593 qemu_opt_unset(opts
, from
);
597 QemuOptsList qemu_legacy_drive_opts
= {
599 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
603 .type
= QEMU_OPT_NUMBER
,
604 .help
= "bus number",
607 .type
= QEMU_OPT_NUMBER
,
608 .help
= "unit number (i.e. lun for scsi)",
611 .type
= QEMU_OPT_NUMBER
,
612 .help
= "index number",
615 .type
= QEMU_OPT_STRING
,
616 .help
= "media type (disk, cdrom)",
619 .type
= QEMU_OPT_STRING
,
620 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
623 .type
= QEMU_OPT_NUMBER
,
624 .help
= "number of cylinders (ide disk geometry)",
627 .type
= QEMU_OPT_NUMBER
,
628 .help
= "number of heads (ide disk geometry)",
631 .type
= QEMU_OPT_NUMBER
,
632 .help
= "number of sectors (ide disk geometry)",
635 .type
= QEMU_OPT_STRING
,
636 .help
= "chs translation (auto, lba, none)",
639 .type
= QEMU_OPT_BOOL
,
640 .help
= "(deprecated, ignored)",
643 .type
= QEMU_OPT_STRING
,
644 .help
= "pci address (virtio only)",
647 .type
= QEMU_OPT_STRING
,
648 .help
= "disk serial number",
651 .type
= QEMU_OPT_STRING
,
655 /* Options that are passed on, but have special semantics with -drive */
658 .type
= QEMU_OPT_BOOL
,
659 .help
= "open drive file as read-only",
662 .type
= QEMU_OPT_STRING
,
663 .help
= "read error action",
666 .type
= QEMU_OPT_STRING
,
667 .help
= "write error action",
669 .name
= "copy-on-read",
670 .type
= QEMU_OPT_BOOL
,
671 .help
= "copy read data from backing file into image file",
674 { /* end of list */ }
678 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
682 DriveInfo
*dinfo
= NULL
;
684 QemuOpts
*legacy_opts
;
685 DriveMediaType media
= MEDIA_DISK
;
686 BlockInterfaceType type
;
687 int cyls
, heads
, secs
, translation
;
688 int max_devs
, bus_id
, unit_id
, index
;
690 const char *werror
, *rerror
;
691 bool read_only
= false;
694 const char *filename
;
695 Error
*local_err
= NULL
;
698 /* Change legacy command line options into QMP ones */
699 static const struct {
703 { "iops", "throttling.iops-total" },
704 { "iops_rd", "throttling.iops-read" },
705 { "iops_wr", "throttling.iops-write" },
707 { "bps", "throttling.bps-total" },
708 { "bps_rd", "throttling.bps-read" },
709 { "bps_wr", "throttling.bps-write" },
711 { "iops_max", "throttling.iops-total-max" },
712 { "iops_rd_max", "throttling.iops-read-max" },
713 { "iops_wr_max", "throttling.iops-write-max" },
715 { "bps_max", "throttling.bps-total-max" },
716 { "bps_rd_max", "throttling.bps-read-max" },
717 { "bps_wr_max", "throttling.bps-write-max" },
719 { "iops_size", "throttling.iops-size" },
721 { "group", "throttling.group" },
723 { "readonly", "read-only" },
726 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
727 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
730 error_report_err(local_err
);
735 value
= qemu_opt_get(all_opts
, "cache");
739 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
740 error_report("invalid cache option");
744 /* Specific options take precedence */
745 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
746 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
747 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
749 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
750 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
751 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
753 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
754 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
755 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
757 qemu_opt_unset(all_opts
, "cache");
760 /* Get a QDict for processing the options */
761 bs_opts
= qdict_new();
762 qemu_opts_to_qdict(all_opts
, bs_opts
);
764 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
766 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
768 error_report_err(local_err
);
772 /* Deprecated option boot=[on|off] */
773 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
774 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
775 "ignored. Future versions will reject this parameter. Please "
776 "update your scripts.\n");
780 value
= qemu_opt_get(legacy_opts
, "media");
782 if (!strcmp(value
, "disk")) {
784 } else if (!strcmp(value
, "cdrom")) {
788 error_report("'%s' invalid media", value
);
793 /* copy-on-read is disabled with a warning for read-only devices */
794 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
795 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
797 if (read_only
&& copy_on_read
) {
798 error_report("warning: disabling copy-on-read on read-only drive");
799 copy_on_read
= false;
802 qdict_put(bs_opts
, "read-only",
803 qstring_from_str(read_only
? "on" : "off"));
804 qdict_put(bs_opts
, "copy-on-read",
805 qstring_from_str(copy_on_read
? "on" :"off"));
807 /* Controller type */
808 value
= qemu_opt_get(legacy_opts
, "if");
811 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
814 if (type
== IF_COUNT
) {
815 error_report("unsupported bus type '%s'", value
);
819 type
= block_default_type
;
823 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
824 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
825 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
827 if (cyls
|| heads
|| secs
) {
829 error_report("invalid physical cyls number");
833 error_report("invalid physical heads number");
837 error_report("invalid physical secs number");
842 translation
= BIOS_ATA_TRANSLATION_AUTO
;
843 value
= qemu_opt_get(legacy_opts
, "trans");
846 error_report("'%s' trans must be used with cyls, heads and secs",
850 if (!strcmp(value
, "none")) {
851 translation
= BIOS_ATA_TRANSLATION_NONE
;
852 } else if (!strcmp(value
, "lba")) {
853 translation
= BIOS_ATA_TRANSLATION_LBA
;
854 } else if (!strcmp(value
, "large")) {
855 translation
= BIOS_ATA_TRANSLATION_LARGE
;
856 } else if (!strcmp(value
, "rechs")) {
857 translation
= BIOS_ATA_TRANSLATION_RECHS
;
858 } else if (!strcmp(value
, "auto")) {
859 translation
= BIOS_ATA_TRANSLATION_AUTO
;
861 error_report("'%s' invalid translation type", value
);
866 if (media
== MEDIA_CDROM
) {
867 if (cyls
|| secs
|| heads
) {
868 error_report("CHS can't be set with media=cdrom");
873 /* Device address specified by bus/unit or index.
874 * If none was specified, try to find the first free one. */
875 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
876 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
877 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
879 max_devs
= if_max_devs
[type
];
882 if (bus_id
!= 0 || unit_id
!= -1) {
883 error_report("index cannot be used with bus and unit");
886 bus_id
= drive_index_to_bus_id(type
, index
);
887 unit_id
= drive_index_to_unit_id(type
, index
);
892 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
894 if (max_devs
&& unit_id
>= max_devs
) {
901 if (max_devs
&& unit_id
>= max_devs
) {
902 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
906 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
907 error_report("drive with bus=%d, unit=%d (index=%d) exists",
908 bus_id
, unit_id
, index
);
913 serial
= qemu_opt_get(legacy_opts
, "serial");
915 /* no id supplied -> create one */
916 if (qemu_opts_id(all_opts
) == NULL
) {
918 const char *mediastr
= "";
919 if (type
== IF_IDE
|| type
== IF_SCSI
) {
920 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
923 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
926 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
929 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
933 /* Add virtio block device */
934 devaddr
= qemu_opt_get(legacy_opts
, "addr");
935 if (devaddr
&& type
!= IF_VIRTIO
) {
936 error_report("addr is not supported by this bus type");
940 if (type
== IF_VIRTIO
) {
942 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
944 if (arch_type
== QEMU_ARCH_S390X
) {
945 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
947 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
949 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
952 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
956 filename
= qemu_opt_get(legacy_opts
, "file");
958 /* Check werror/rerror compatibility with if=... */
959 werror
= qemu_opt_get(legacy_opts
, "werror");
960 if (werror
!= NULL
) {
961 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
963 error_report("werror is not supported by this bus type");
966 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
969 rerror
= qemu_opt_get(legacy_opts
, "rerror");
970 if (rerror
!= NULL
) {
971 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
973 error_report("rerror is not supported by this bus type");
976 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
979 /* Actual block device init: Functionality shared with blockdev-add */
980 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
984 error_report_err(local_err
);
991 /* Create legacy DriveInfo */
992 dinfo
= g_malloc0(sizeof(*dinfo
));
993 dinfo
->opts
= all_opts
;
996 dinfo
->heads
= heads
;
998 dinfo
->trans
= translation
;
1001 dinfo
->bus
= bus_id
;
1002 dinfo
->unit
= unit_id
;
1003 dinfo
->devaddr
= devaddr
;
1004 dinfo
->serial
= g_strdup(serial
);
1006 blk_set_legacy_dinfo(blk
, dinfo
);
1013 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1020 qemu_opts_del(legacy_opts
);
1025 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1027 const char *device
= qdict_get_str(qdict
, "device");
1031 if (!strcmp(device
, "all")) {
1032 ret
= bdrv_commit_all();
1034 blk
= blk_by_name(device
);
1036 monitor_printf(mon
, "Device '%s' not found\n", device
);
1039 ret
= bdrv_commit(blk_bs(blk
));
1042 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1047 static void blockdev_do_action(int kind
, void *data
, Error
**errp
)
1049 TransactionAction action
;
1050 TransactionActionList list
;
1054 list
.value
= &action
;
1056 qmp_transaction(&list
, errp
);
1059 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1060 bool has_node_name
, const char *node_name
,
1061 const char *snapshot_file
,
1062 bool has_snapshot_node_name
,
1063 const char *snapshot_node_name
,
1064 bool has_format
, const char *format
,
1065 bool has_mode
, NewImageMode mode
, Error
**errp
)
1067 BlockdevSnapshot snapshot
= {
1068 .has_device
= has_device
,
1069 .device
= (char *) device
,
1070 .has_node_name
= has_node_name
,
1071 .node_name
= (char *) node_name
,
1072 .snapshot_file
= (char *) snapshot_file
,
1073 .has_snapshot_node_name
= has_snapshot_node_name
,
1074 .snapshot_node_name
= (char *) snapshot_node_name
,
1075 .has_format
= has_format
,
1076 .format
= (char *) format
,
1077 .has_mode
= has_mode
,
1080 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1084 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1088 BlockdevSnapshotInternal snapshot
= {
1089 .device
= (char *) device
,
1090 .name
= (char *) name
1093 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1097 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1104 BlockDriverState
*bs
;
1106 AioContext
*aio_context
;
1107 QEMUSnapshotInfo sn
;
1108 Error
*local_err
= NULL
;
1109 SnapshotInfo
*info
= NULL
;
1112 blk
= blk_by_name(device
);
1114 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1115 "Device '%s' not found", device
);
1129 error_setg(errp
, "Name or id must be provided");
1133 aio_context
= bdrv_get_aio_context(bs
);
1134 aio_context_acquire(aio_context
);
1136 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1137 goto out_aio_context
;
1140 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1142 error_propagate(errp
, local_err
);
1143 goto out_aio_context
;
1147 "Snapshot with id '%s' and name '%s' does not exist on "
1149 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1150 goto out_aio_context
;
1153 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1155 error_propagate(errp
, local_err
);
1156 goto out_aio_context
;
1159 aio_context_release(aio_context
);
1161 info
= g_new0(SnapshotInfo
, 1);
1162 info
->id
= g_strdup(sn
.id_str
);
1163 info
->name
= g_strdup(sn
.name
);
1164 info
->date_nsec
= sn
.date_nsec
;
1165 info
->date_sec
= sn
.date_sec
;
1166 info
->vm_state_size
= sn
.vm_state_size
;
1167 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1168 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1173 aio_context_release(aio_context
);
1178 * block_dirty_bitmap_lookup:
1179 * Return a dirty bitmap (if present), after validating
1180 * the node reference and bitmap names.
1182 * @node: The name of the BDS node to search for bitmaps
1183 * @name: The name of the bitmap to search for
1184 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1185 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1186 * @errp: Output pointer for error information. Can be NULL.
1188 * @return: A bitmap object on success, or NULL on failure.
1190 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1192 BlockDriverState
**pbs
,
1196 BlockDriverState
*bs
;
1197 BdrvDirtyBitmap
*bitmap
;
1198 AioContext
*aio_context
;
1201 error_setg(errp
, "Node cannot be NULL");
1205 error_setg(errp
, "Bitmap name cannot be NULL");
1208 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1210 error_setg(errp
, "Node '%s' not found", node
);
1214 aio_context
= bdrv_get_aio_context(bs
);
1215 aio_context_acquire(aio_context
);
1217 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1219 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1227 *paio
= aio_context
;
1229 aio_context_release(aio_context
);
1235 aio_context_release(aio_context
);
1239 /* New and old BlockDriverState structs for atomic group operations */
1241 typedef struct BlkTransactionState BlkTransactionState
;
1243 /* Only prepare() may fail. In a single transaction, only one of commit() or
1244 abort() will be called, clean() will always be called if it present. */
1245 typedef struct BdrvActionOps
{
1246 /* Size of state struct, in bytes. */
1247 size_t instance_size
;
1248 /* Prepare the work, must NOT be NULL. */
1249 void (*prepare
)(BlkTransactionState
*common
, Error
**errp
);
1250 /* Commit the changes, can be NULL. */
1251 void (*commit
)(BlkTransactionState
*common
);
1252 /* Abort the changes on fail, can be NULL. */
1253 void (*abort
)(BlkTransactionState
*common
);
1254 /* Clean up resource in the end, can be NULL. */
1255 void (*clean
)(BlkTransactionState
*common
);
1259 * This structure must be arranged as first member in child type, assuming
1260 * that compiler will also arrange it to the same address with parent instance.
1261 * Later it will be used in free().
1263 struct BlkTransactionState
{
1264 TransactionAction
*action
;
1265 const BdrvActionOps
*ops
;
1266 QSIMPLEQ_ENTRY(BlkTransactionState
) entry
;
1269 /* internal snapshot private data */
1270 typedef struct InternalSnapshotState
{
1271 BlkTransactionState common
;
1272 BlockDriverState
*bs
;
1273 AioContext
*aio_context
;
1274 QEMUSnapshotInfo sn
;
1275 } InternalSnapshotState
;
1277 static void internal_snapshot_prepare(BlkTransactionState
*common
,
1280 Error
*local_err
= NULL
;
1284 BlockDriverState
*bs
;
1285 QEMUSnapshotInfo old_sn
, *sn
;
1288 BlockdevSnapshotInternal
*internal
;
1289 InternalSnapshotState
*state
;
1292 g_assert(common
->action
->kind
==
1293 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1294 internal
= common
->action
->blockdev_snapshot_internal_sync
;
1295 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1297 /* 1. parse input */
1298 device
= internal
->device
;
1299 name
= internal
->name
;
1301 /* 2. check for validation */
1302 blk
= blk_by_name(device
);
1304 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1305 "Device '%s' not found", device
);
1310 /* AioContext is released in .clean() */
1311 state
->aio_context
= bdrv_get_aio_context(bs
);
1312 aio_context_acquire(state
->aio_context
);
1314 if (!bdrv_is_inserted(bs
)) {
1315 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1319 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1323 if (bdrv_is_read_only(bs
)) {
1324 error_setg(errp
, "Device '%s' is read only", device
);
1328 if (!bdrv_can_snapshot(bs
)) {
1329 error_setg(errp
, "Block format '%s' used by device '%s' "
1330 "does not support internal snapshots",
1331 bs
->drv
->format_name
, device
);
1335 if (!strlen(name
)) {
1336 error_setg(errp
, "Name is empty");
1340 /* check whether a snapshot with name exist */
1341 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1344 error_propagate(errp
, local_err
);
1348 "Snapshot with name '%s' already exists on device '%s'",
1353 /* 3. take the snapshot */
1355 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1356 qemu_gettimeofday(&tv
);
1357 sn
->date_sec
= tv
.tv_sec
;
1358 sn
->date_nsec
= tv
.tv_usec
* 1000;
1359 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1361 ret1
= bdrv_snapshot_create(bs
, sn
);
1363 error_setg_errno(errp
, -ret1
,
1364 "Failed to create snapshot '%s' on device '%s'",
1369 /* 4. succeed, mark a snapshot is created */
1373 static void internal_snapshot_abort(BlkTransactionState
*common
)
1375 InternalSnapshotState
*state
=
1376 DO_UPCAST(InternalSnapshotState
, common
, common
);
1377 BlockDriverState
*bs
= state
->bs
;
1378 QEMUSnapshotInfo
*sn
= &state
->sn
;
1379 Error
*local_error
= NULL
;
1385 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1386 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1387 "device '%s' in abort: %s",
1390 bdrv_get_device_name(bs
),
1391 error_get_pretty(local_error
));
1392 error_free(local_error
);
1396 static void internal_snapshot_clean(BlkTransactionState
*common
)
1398 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1401 if (state
->aio_context
) {
1402 aio_context_release(state
->aio_context
);
1406 /* external snapshot private data */
1407 typedef struct ExternalSnapshotState
{
1408 BlkTransactionState common
;
1409 BlockDriverState
*old_bs
;
1410 BlockDriverState
*new_bs
;
1411 AioContext
*aio_context
;
1412 } ExternalSnapshotState
;
1414 static void external_snapshot_prepare(BlkTransactionState
*common
,
1419 QDict
*options
= NULL
;
1420 Error
*local_err
= NULL
;
1421 bool has_device
= false;
1423 bool has_node_name
= false;
1424 const char *node_name
;
1425 bool has_snapshot_node_name
= false;
1426 const char *snapshot_node_name
;
1427 const char *new_image_file
;
1428 const char *format
= "qcow2";
1429 enum NewImageMode mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1430 ExternalSnapshotState
*state
=
1431 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1432 TransactionAction
*action
= common
->action
;
1434 /* get parameters */
1435 g_assert(action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
);
1437 has_device
= action
->blockdev_snapshot_sync
->has_device
;
1438 device
= action
->blockdev_snapshot_sync
->device
;
1439 has_node_name
= action
->blockdev_snapshot_sync
->has_node_name
;
1440 node_name
= action
->blockdev_snapshot_sync
->node_name
;
1441 has_snapshot_node_name
=
1442 action
->blockdev_snapshot_sync
->has_snapshot_node_name
;
1443 snapshot_node_name
= action
->blockdev_snapshot_sync
->snapshot_node_name
;
1445 new_image_file
= action
->blockdev_snapshot_sync
->snapshot_file
;
1446 if (action
->blockdev_snapshot_sync
->has_format
) {
1447 format
= action
->blockdev_snapshot_sync
->format
;
1449 if (action
->blockdev_snapshot_sync
->has_mode
) {
1450 mode
= action
->blockdev_snapshot_sync
->mode
;
1453 /* start processing */
1454 drv
= bdrv_find_format(format
);
1456 error_setg(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1460 state
->old_bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1461 has_node_name
? node_name
: NULL
,
1464 error_propagate(errp
, local_err
);
1468 if (has_node_name
&& !has_snapshot_node_name
) {
1469 error_setg(errp
, "New snapshot node name missing");
1473 if (has_snapshot_node_name
&& bdrv_find_node(snapshot_node_name
)) {
1474 error_setg(errp
, "New snapshot node name already existing");
1478 /* Acquire AioContext now so any threads operating on old_bs stop */
1479 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1480 aio_context_acquire(state
->aio_context
);
1482 if (!bdrv_is_inserted(state
->old_bs
)) {
1483 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1487 if (bdrv_op_is_blocked(state
->old_bs
,
1488 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1492 if (!bdrv_is_read_only(state
->old_bs
)) {
1493 if (bdrv_flush(state
->old_bs
)) {
1494 error_setg(errp
, QERR_IO_ERROR
);
1499 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1500 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1504 flags
= state
->old_bs
->open_flags
;
1506 /* create new image w/backing file */
1507 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1508 bdrv_img_create(new_image_file
, format
,
1509 state
->old_bs
->filename
,
1510 state
->old_bs
->drv
->format_name
,
1511 NULL
, -1, flags
, &local_err
, false);
1513 error_propagate(errp
, local_err
);
1518 if (has_snapshot_node_name
) {
1519 options
= qdict_new();
1520 qdict_put(options
, "node-name",
1521 qstring_from_str(snapshot_node_name
));
1524 /* TODO Inherit bs->options or only take explicit options with an
1525 * extended QMP command? */
1526 assert(state
->new_bs
== NULL
);
1527 ret
= bdrv_open(&state
->new_bs
, new_image_file
, NULL
, options
,
1528 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
1529 /* We will manually add the backing_hd field to the bs later */
1531 error_propagate(errp
, local_err
);
1535 static void external_snapshot_commit(BlkTransactionState
*common
)
1537 ExternalSnapshotState
*state
=
1538 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1540 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1542 /* This removes our old bs and adds the new bs */
1543 bdrv_append(state
->new_bs
, state
->old_bs
);
1544 /* We don't need (or want) to use the transactional
1545 * bdrv_reopen_multiple() across all the entries at once, because we
1546 * don't want to abort all of them if one of them fails the reopen */
1547 bdrv_reopen(state
->new_bs
, state
->new_bs
->open_flags
& ~BDRV_O_RDWR
,
1550 aio_context_release(state
->aio_context
);
1553 static void external_snapshot_abort(BlkTransactionState
*common
)
1555 ExternalSnapshotState
*state
=
1556 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1557 if (state
->new_bs
) {
1558 bdrv_unref(state
->new_bs
);
1560 if (state
->aio_context
) {
1561 aio_context_release(state
->aio_context
);
1565 typedef struct DriveBackupState
{
1566 BlkTransactionState common
;
1567 BlockDriverState
*bs
;
1568 AioContext
*aio_context
;
1572 static void drive_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1574 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1575 BlockDriverState
*bs
;
1577 DriveBackup
*backup
;
1578 Error
*local_err
= NULL
;
1580 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1581 backup
= common
->action
->drive_backup
;
1583 blk
= blk_by_name(backup
->device
);
1585 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1586 "Device '%s' not found", backup
->device
);
1591 /* AioContext is released in .clean() */
1592 state
->aio_context
= bdrv_get_aio_context(bs
);
1593 aio_context_acquire(state
->aio_context
);
1595 qmp_drive_backup(backup
->device
, backup
->target
,
1596 backup
->has_format
, backup
->format
,
1598 backup
->has_mode
, backup
->mode
,
1599 backup
->has_speed
, backup
->speed
,
1600 backup
->has_bitmap
, backup
->bitmap
,
1601 backup
->has_on_source_error
, backup
->on_source_error
,
1602 backup
->has_on_target_error
, backup
->on_target_error
,
1605 error_propagate(errp
, local_err
);
1610 state
->job
= state
->bs
->job
;
1613 static void drive_backup_abort(BlkTransactionState
*common
)
1615 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1616 BlockDriverState
*bs
= state
->bs
;
1618 /* Only cancel if it's the job we started */
1619 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1620 block_job_cancel_sync(bs
->job
);
1624 static void drive_backup_clean(BlkTransactionState
*common
)
1626 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1628 if (state
->aio_context
) {
1629 aio_context_release(state
->aio_context
);
1633 typedef struct BlockdevBackupState
{
1634 BlkTransactionState common
;
1635 BlockDriverState
*bs
;
1637 AioContext
*aio_context
;
1638 } BlockdevBackupState
;
1640 static void blockdev_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1642 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1643 BlockdevBackup
*backup
;
1644 BlockDriverState
*bs
, *target
;
1646 Error
*local_err
= NULL
;
1648 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1649 backup
= common
->action
->blockdev_backup
;
1651 blk
= blk_by_name(backup
->device
);
1653 error_setg(errp
, "Device '%s' not found", backup
->device
);
1658 blk
= blk_by_name(backup
->target
);
1660 error_setg(errp
, "Device '%s' not found", backup
->target
);
1663 target
= blk_bs(blk
);
1665 /* AioContext is released in .clean() */
1666 state
->aio_context
= bdrv_get_aio_context(bs
);
1667 if (state
->aio_context
!= bdrv_get_aio_context(target
)) {
1668 state
->aio_context
= NULL
;
1669 error_setg(errp
, "Backup between two IO threads is not implemented");
1672 aio_context_acquire(state
->aio_context
);
1674 qmp_blockdev_backup(backup
->device
, backup
->target
,
1676 backup
->has_speed
, backup
->speed
,
1677 backup
->has_on_source_error
, backup
->on_source_error
,
1678 backup
->has_on_target_error
, backup
->on_target_error
,
1681 error_propagate(errp
, local_err
);
1686 state
->job
= state
->bs
->job
;
1689 static void blockdev_backup_abort(BlkTransactionState
*common
)
1691 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1692 BlockDriverState
*bs
= state
->bs
;
1694 /* Only cancel if it's the job we started */
1695 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1696 block_job_cancel_sync(bs
->job
);
1700 static void blockdev_backup_clean(BlkTransactionState
*common
)
1702 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1704 if (state
->aio_context
) {
1705 aio_context_release(state
->aio_context
);
1709 static void abort_prepare(BlkTransactionState
*common
, Error
**errp
)
1711 error_setg(errp
, "Transaction aborted using Abort action");
1714 static void abort_commit(BlkTransactionState
*common
)
1716 g_assert_not_reached(); /* this action never succeeds */
1719 static const BdrvActionOps actions
[] = {
1720 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
1721 .instance_size
= sizeof(ExternalSnapshotState
),
1722 .prepare
= external_snapshot_prepare
,
1723 .commit
= external_snapshot_commit
,
1724 .abort
= external_snapshot_abort
,
1726 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
1727 .instance_size
= sizeof(DriveBackupState
),
1728 .prepare
= drive_backup_prepare
,
1729 .abort
= drive_backup_abort
,
1730 .clean
= drive_backup_clean
,
1732 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
1733 .instance_size
= sizeof(BlockdevBackupState
),
1734 .prepare
= blockdev_backup_prepare
,
1735 .abort
= blockdev_backup_abort
,
1736 .clean
= blockdev_backup_clean
,
1738 [TRANSACTION_ACTION_KIND_ABORT
] = {
1739 .instance_size
= sizeof(BlkTransactionState
),
1740 .prepare
= abort_prepare
,
1741 .commit
= abort_commit
,
1743 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
1744 .instance_size
= sizeof(InternalSnapshotState
),
1745 .prepare
= internal_snapshot_prepare
,
1746 .abort
= internal_snapshot_abort
,
1747 .clean
= internal_snapshot_clean
,
1752 * 'Atomic' group operations. The operations are performed as a set, and if
1753 * any fail then we roll back all operations in the group.
1755 void qmp_transaction(TransactionActionList
*dev_list
, Error
**errp
)
1757 TransactionActionList
*dev_entry
= dev_list
;
1758 BlkTransactionState
*state
, *next
;
1759 Error
*local_err
= NULL
;
1761 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkTransactionState
) snap_bdrv_states
;
1762 QSIMPLEQ_INIT(&snap_bdrv_states
);
1764 /* drain all i/o before any operations */
1767 /* We don't do anything in this loop that commits us to the operations */
1768 while (NULL
!= dev_entry
) {
1769 TransactionAction
*dev_info
= NULL
;
1770 const BdrvActionOps
*ops
;
1772 dev_info
= dev_entry
->value
;
1773 dev_entry
= dev_entry
->next
;
1775 assert(dev_info
->kind
< ARRAY_SIZE(actions
));
1777 ops
= &actions
[dev_info
->kind
];
1778 assert(ops
->instance_size
> 0);
1780 state
= g_malloc0(ops
->instance_size
);
1782 state
->action
= dev_info
;
1783 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
1785 state
->ops
->prepare(state
, &local_err
);
1787 error_propagate(errp
, local_err
);
1788 goto delete_and_fail
;
1792 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1793 if (state
->ops
->commit
) {
1794 state
->ops
->commit(state
);
1802 /* failure, and it is all-or-none; roll back all operations */
1803 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1804 if (state
->ops
->abort
) {
1805 state
->ops
->abort(state
);
1809 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
1810 if (state
->ops
->clean
) {
1811 state
->ops
->clean(state
);
1818 static void eject_device(BlockBackend
*blk
, int force
, Error
**errp
)
1820 BlockDriverState
*bs
= blk_bs(blk
);
1821 AioContext
*aio_context
;
1823 aio_context
= bdrv_get_aio_context(bs
);
1824 aio_context_acquire(aio_context
);
1826 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
1829 if (!blk_dev_has_removable_media(blk
)) {
1830 error_setg(errp
, "Device '%s' is not removable",
1831 bdrv_get_device_name(bs
));
1835 if (blk_dev_is_medium_locked(blk
) && !blk_dev_is_tray_open(blk
)) {
1836 blk_dev_eject_request(blk
, force
);
1838 error_setg(errp
, "Device '%s' is locked",
1839 bdrv_get_device_name(bs
));
1847 aio_context_release(aio_context
);
1850 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
1854 blk
= blk_by_name(device
);
1856 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1857 "Device '%s' not found", device
);
1861 eject_device(blk
, force
, errp
);
1864 void qmp_block_passwd(bool has_device
, const char *device
,
1865 bool has_node_name
, const char *node_name
,
1866 const char *password
, Error
**errp
)
1868 Error
*local_err
= NULL
;
1869 BlockDriverState
*bs
;
1870 AioContext
*aio_context
;
1872 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1873 has_node_name
? node_name
: NULL
,
1876 error_propagate(errp
, local_err
);
1880 aio_context
= bdrv_get_aio_context(bs
);
1881 aio_context_acquire(aio_context
);
1883 bdrv_add_key(bs
, password
, errp
);
1885 aio_context_release(aio_context
);
1888 /* Assumes AioContext is held */
1889 static void qmp_bdrv_open_encrypted(BlockDriverState
*bs
, const char *filename
,
1890 int bdrv_flags
, BlockDriver
*drv
,
1891 const char *password
, Error
**errp
)
1893 Error
*local_err
= NULL
;
1896 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, bdrv_flags
, drv
, &local_err
);
1898 error_propagate(errp
, local_err
);
1902 bdrv_add_key(bs
, password
, errp
);
1905 void qmp_change_blockdev(const char *device
, const char *filename
,
1906 const char *format
, Error
**errp
)
1909 BlockDriverState
*bs
;
1910 AioContext
*aio_context
;
1911 BlockDriver
*drv
= NULL
;
1915 blk
= blk_by_name(device
);
1917 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1918 "Device '%s' not found", device
);
1923 aio_context
= bdrv_get_aio_context(bs
);
1924 aio_context_acquire(aio_context
);
1927 drv
= bdrv_find_whitelisted_format(format
, bs
->read_only
);
1929 error_setg(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1934 eject_device(blk
, 0, &err
);
1936 error_propagate(errp
, err
);
1940 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
1941 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
1943 qmp_bdrv_open_encrypted(bs
, filename
, bdrv_flags
, drv
, NULL
, errp
);
1946 aio_context_release(aio_context
);
1949 /* throttling disk I/O limits */
1950 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
1957 bool has_bps_rd_max
,
1959 bool has_bps_wr_max
,
1963 bool has_iops_rd_max
,
1964 int64_t iops_rd_max
,
1965 bool has_iops_wr_max
,
1966 int64_t iops_wr_max
,
1970 const char *group
, Error
**errp
)
1973 BlockDriverState
*bs
;
1975 AioContext
*aio_context
;
1977 blk
= blk_by_name(device
);
1979 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1980 "Device '%s' not found", device
);
1985 memset(&cfg
, 0, sizeof(cfg
));
1986 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
1987 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
1988 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
1990 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
1991 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
1992 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
1995 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
1997 if (has_bps_rd_max
) {
1998 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2000 if (has_bps_wr_max
) {
2001 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2004 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2006 if (has_iops_rd_max
) {
2007 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2009 if (has_iops_wr_max
) {
2010 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2013 if (has_iops_size
) {
2014 cfg
.op_size
= iops_size
;
2017 if (!check_throttle_config(&cfg
, errp
)) {
2021 aio_context
= bdrv_get_aio_context(bs
);
2022 aio_context_acquire(aio_context
);
2024 if (throttle_enabled(&cfg
)) {
2025 /* Enable I/O limits if they're not enabled yet, otherwise
2026 * just update the throttling group. */
2027 if (!bs
->io_limits_enabled
) {
2028 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2029 } else if (has_group
) {
2030 bdrv_io_limits_update_group(bs
, group
);
2032 /* Set the new throttling configuration */
2033 bdrv_set_io_limits(bs
, &cfg
);
2034 } else if (bs
->io_limits_enabled
) {
2035 /* If all throttling settings are set to 0, disable I/O limits */
2036 bdrv_io_limits_disable(bs
);
2039 aio_context_release(aio_context
);
2042 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2043 bool has_granularity
, uint32_t granularity
,
2046 AioContext
*aio_context
;
2047 BlockDriverState
*bs
;
2049 if (!name
|| name
[0] == '\0') {
2050 error_setg(errp
, "Bitmap name cannot be empty");
2054 bs
= bdrv_lookup_bs(node
, node
, errp
);
2059 aio_context
= bdrv_get_aio_context(bs
);
2060 aio_context_acquire(aio_context
);
2062 if (has_granularity
) {
2063 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2064 error_setg(errp
, "Granularity must be power of 2 "
2065 "and at least 512");
2069 /* Default to cluster size, if available: */
2070 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2073 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2076 aio_context_release(aio_context
);
2079 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2082 AioContext
*aio_context
;
2083 BlockDriverState
*bs
;
2084 BdrvDirtyBitmap
*bitmap
;
2086 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2087 if (!bitmap
|| !bs
) {
2091 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2093 "Bitmap '%s' is currently frozen and cannot be removed",
2097 bdrv_dirty_bitmap_make_anon(bitmap
);
2098 bdrv_release_dirty_bitmap(bs
, bitmap
);
2101 aio_context_release(aio_context
);
2105 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2106 * immediately after a full backup operation.
2108 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2111 AioContext
*aio_context
;
2112 BdrvDirtyBitmap
*bitmap
;
2113 BlockDriverState
*bs
;
2115 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2116 if (!bitmap
|| !bs
) {
2120 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2122 "Bitmap '%s' is currently frozen and cannot be modified",
2125 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2127 "Bitmap '%s' is currently disabled and cannot be cleared",
2132 bdrv_clear_dirty_bitmap(bitmap
);
2135 aio_context_release(aio_context
);
2138 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2140 const char *id
= qdict_get_str(qdict
, "id");
2142 BlockDriverState
*bs
;
2143 AioContext
*aio_context
;
2144 Error
*local_err
= NULL
;
2146 blk
= blk_by_name(id
);
2148 error_report("Device '%s' not found", id
);
2153 if (!blk_legacy_dinfo(blk
)) {
2154 error_report("Deleting device added with blockdev-add"
2155 " is not supported");
2159 aio_context
= bdrv_get_aio_context(bs
);
2160 aio_context_acquire(aio_context
);
2162 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2163 error_report_err(local_err
);
2164 aio_context_release(aio_context
);
2168 /* quiesce block driver; prevent further io */
2173 /* if we have a device attached to this BlockDriverState
2174 * then we need to make the drive anonymous until the device
2175 * can be removed. If this is a drive with no device backing
2176 * then we can just get rid of the block driver state right here.
2178 if (blk_get_attached_dev(blk
)) {
2179 blk_hide_on_behalf_of_hmp_drive_del(blk
);
2180 /* Further I/O must not pause the guest */
2181 bdrv_set_on_error(bs
, BLOCKDEV_ON_ERROR_REPORT
,
2182 BLOCKDEV_ON_ERROR_REPORT
);
2187 aio_context_release(aio_context
);
2190 void qmp_block_resize(bool has_device
, const char *device
,
2191 bool has_node_name
, const char *node_name
,
2192 int64_t size
, Error
**errp
)
2194 Error
*local_err
= NULL
;
2195 BlockDriverState
*bs
;
2196 AioContext
*aio_context
;
2199 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2200 has_node_name
? node_name
: NULL
,
2203 error_propagate(errp
, local_err
);
2207 aio_context
= bdrv_get_aio_context(bs
);
2208 aio_context_acquire(aio_context
);
2210 if (!bdrv_is_first_non_filter(bs
)) {
2211 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2216 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2220 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2221 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2225 /* complete all in-flight operations before resizing the device */
2228 ret
= bdrv_truncate(bs
, size
);
2233 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2236 error_setg(errp
, QERR_UNSUPPORTED
);
2239 error_setg(errp
, "Device '%s' is read only", device
);
2242 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2245 error_setg_errno(errp
, -ret
, "Could not resize");
2250 aio_context_release(aio_context
);
2253 static void block_job_cb(void *opaque
, int ret
)
2255 /* Note that this function may be executed from another AioContext besides
2256 * the QEMU main loop. If you need to access anything that assumes the
2257 * QEMU global mutex, use a BH or introduce a mutex.
2260 BlockDriverState
*bs
= opaque
;
2261 const char *msg
= NULL
;
2263 trace_block_job_cb(bs
, bs
->job
, ret
);
2268 msg
= strerror(-ret
);
2271 if (block_job_is_cancelled(bs
->job
)) {
2272 block_job_event_cancelled(bs
->job
);
2274 block_job_event_completed(bs
->job
, msg
);
2277 bdrv_put_ref_bh_schedule(bs
);
2280 void qmp_block_stream(const char *device
,
2281 bool has_base
, const char *base
,
2282 bool has_backing_file
, const char *backing_file
,
2283 bool has_speed
, int64_t speed
,
2284 bool has_on_error
, BlockdevOnError on_error
,
2288 BlockDriverState
*bs
;
2289 BlockDriverState
*base_bs
= NULL
;
2290 AioContext
*aio_context
;
2291 Error
*local_err
= NULL
;
2292 const char *base_name
= NULL
;
2294 if (!has_on_error
) {
2295 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2298 blk
= blk_by_name(device
);
2300 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2301 "Device '%s' not found", device
);
2306 aio_context
= bdrv_get_aio_context(bs
);
2307 aio_context_acquire(aio_context
);
2309 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2314 base_bs
= bdrv_find_backing_image(bs
, base
);
2315 if (base_bs
== NULL
) {
2316 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2319 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2323 /* if we are streaming the entire chain, the result will have no backing
2324 * file, and specifying one is therefore an error */
2325 if (base_bs
== NULL
&& has_backing_file
) {
2326 error_setg(errp
, "backing file specified, but streaming the "
2331 /* backing_file string overrides base bs filename */
2332 base_name
= has_backing_file
? backing_file
: base_name
;
2334 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
2335 on_error
, block_job_cb
, bs
, &local_err
);
2337 error_propagate(errp
, local_err
);
2341 trace_qmp_block_stream(bs
, bs
->job
);
2344 aio_context_release(aio_context
);
2347 void qmp_block_commit(const char *device
,
2348 bool has_base
, const char *base
,
2349 bool has_top
, const char *top
,
2350 bool has_backing_file
, const char *backing_file
,
2351 bool has_speed
, int64_t speed
,
2355 BlockDriverState
*bs
;
2356 BlockDriverState
*base_bs
, *top_bs
;
2357 AioContext
*aio_context
;
2358 Error
*local_err
= NULL
;
2359 /* This will be part of the QMP command, if/when the
2360 * BlockdevOnError change for blkmirror makes it in
2362 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2369 * libvirt relies on the DeviceNotFound error class in order to probe for
2370 * live commit feature versions; for this to work, we must make sure to
2371 * perform the device lookup before any generic errors that may occur in a
2372 * scenario in which all optional arguments are omitted. */
2373 blk
= blk_by_name(device
);
2375 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2376 "Device '%s' not found", device
);
2381 aio_context
= bdrv_get_aio_context(bs
);
2382 aio_context_acquire(aio_context
);
2384 /* drain all i/o before commits */
2387 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2391 /* default top_bs is the active layer */
2394 if (has_top
&& top
) {
2395 if (strcmp(bs
->filename
, top
) != 0) {
2396 top_bs
= bdrv_find_backing_image(bs
, top
);
2400 if (top_bs
== NULL
) {
2401 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2405 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
2407 if (has_base
&& base
) {
2408 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2410 base_bs
= bdrv_find_base(top_bs
);
2413 if (base_bs
== NULL
) {
2414 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2418 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2420 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2424 /* Do not allow attempts to commit an image into itself */
2425 if (top_bs
== base_bs
) {
2426 error_setg(errp
, "cannot commit an image into itself");
2431 if (has_backing_file
) {
2432 error_setg(errp
, "'backing-file' specified,"
2433 " but 'top' is the active layer");
2436 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
2439 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
2440 has_backing_file
? backing_file
: NULL
, &local_err
);
2442 if (local_err
!= NULL
) {
2443 error_propagate(errp
, local_err
);
2448 aio_context_release(aio_context
);
2451 void qmp_drive_backup(const char *device
, const char *target
,
2452 bool has_format
, const char *format
,
2453 enum MirrorSyncMode sync
,
2454 bool has_mode
, enum NewImageMode mode
,
2455 bool has_speed
, int64_t speed
,
2456 bool has_bitmap
, const char *bitmap
,
2457 bool has_on_source_error
, BlockdevOnError on_source_error
,
2458 bool has_on_target_error
, BlockdevOnError on_target_error
,
2462 BlockDriverState
*bs
;
2463 BlockDriverState
*target_bs
;
2464 BlockDriverState
*source
= NULL
;
2465 BdrvDirtyBitmap
*bmap
= NULL
;
2466 AioContext
*aio_context
;
2467 BlockDriver
*drv
= NULL
;
2468 Error
*local_err
= NULL
;
2476 if (!has_on_source_error
) {
2477 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2479 if (!has_on_target_error
) {
2480 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2483 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2486 blk
= blk_by_name(device
);
2488 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2489 "Device '%s' not found", device
);
2494 aio_context
= bdrv_get_aio_context(bs
);
2495 aio_context_acquire(aio_context
);
2497 /* Although backup_run has this check too, we need to use bs->drv below, so
2498 * do an early check redundantly. */
2499 if (!bdrv_is_inserted(bs
)) {
2500 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2505 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2508 drv
= bdrv_find_format(format
);
2510 error_setg(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
2515 /* Early check to avoid creating target */
2516 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
2520 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2522 /* See if we have a backing HD we can use to create our new image
2524 if (sync
== MIRROR_SYNC_MODE_TOP
) {
2525 source
= bs
->backing_hd
;
2527 sync
= MIRROR_SYNC_MODE_FULL
;
2530 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2534 size
= bdrv_getlength(bs
);
2536 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2540 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
2541 assert(format
&& drv
);
2543 bdrv_img_create(target
, format
, source
->filename
,
2544 source
->drv
->format_name
, NULL
,
2545 size
, flags
, &local_err
, false);
2547 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
2548 size
, flags
, &local_err
, false);
2553 error_propagate(errp
, local_err
);
2558 ret
= bdrv_open(&target_bs
, target
, NULL
, NULL
, flags
, drv
, &local_err
);
2560 error_propagate(errp
, local_err
);
2564 bdrv_set_aio_context(target_bs
, aio_context
);
2567 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
2569 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
2574 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
2575 on_source_error
, on_target_error
,
2576 block_job_cb
, bs
, &local_err
);
2577 if (local_err
!= NULL
) {
2578 bdrv_unref(target_bs
);
2579 error_propagate(errp
, local_err
);
2584 aio_context_release(aio_context
);
2587 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
2589 return bdrv_named_nodes_list(errp
);
2592 void qmp_blockdev_backup(const char *device
, const char *target
,
2593 enum MirrorSyncMode sync
,
2594 bool has_speed
, int64_t speed
,
2595 bool has_on_source_error
,
2596 BlockdevOnError on_source_error
,
2597 bool has_on_target_error
,
2598 BlockdevOnError on_target_error
,
2602 BlockDriverState
*bs
;
2603 BlockDriverState
*target_bs
;
2604 Error
*local_err
= NULL
;
2605 AioContext
*aio_context
;
2610 if (!has_on_source_error
) {
2611 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2613 if (!has_on_target_error
) {
2614 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2617 blk
= blk_by_name(device
);
2619 error_setg(errp
, "Device '%s' not found", device
);
2624 aio_context
= bdrv_get_aio_context(bs
);
2625 aio_context_acquire(aio_context
);
2627 blk
= blk_by_name(target
);
2629 error_setg(errp
, "Device '%s' not found", target
);
2632 target_bs
= blk_bs(blk
);
2634 bdrv_ref(target_bs
);
2635 bdrv_set_aio_context(target_bs
, aio_context
);
2636 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
2637 on_target_error
, block_job_cb
, bs
, &local_err
);
2638 if (local_err
!= NULL
) {
2639 bdrv_unref(target_bs
);
2640 error_propagate(errp
, local_err
);
2643 aio_context_release(aio_context
);
2646 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2648 void qmp_drive_mirror(const char *device
, const char *target
,
2649 bool has_format
, const char *format
,
2650 bool has_node_name
, const char *node_name
,
2651 bool has_replaces
, const char *replaces
,
2652 enum MirrorSyncMode sync
,
2653 bool has_mode
, enum NewImageMode mode
,
2654 bool has_speed
, int64_t speed
,
2655 bool has_granularity
, uint32_t granularity
,
2656 bool has_buf_size
, int64_t buf_size
,
2657 bool has_on_source_error
, BlockdevOnError on_source_error
,
2658 bool has_on_target_error
, BlockdevOnError on_target_error
,
2662 BlockDriverState
*bs
;
2663 BlockDriverState
*source
, *target_bs
;
2664 AioContext
*aio_context
;
2665 BlockDriver
*drv
= NULL
;
2666 Error
*local_err
= NULL
;
2667 QDict
*options
= NULL
;
2675 if (!has_on_source_error
) {
2676 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2678 if (!has_on_target_error
) {
2679 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2682 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2684 if (!has_granularity
) {
2687 if (!has_buf_size
) {
2688 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
2691 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
2692 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2693 "a value in range [512B, 64MB]");
2696 if (granularity
& (granularity
- 1)) {
2697 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2702 blk
= blk_by_name(device
);
2704 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2705 "Device '%s' not found", device
);
2710 aio_context
= bdrv_get_aio_context(bs
);
2711 aio_context_acquire(aio_context
);
2713 if (!bdrv_is_inserted(bs
)) {
2714 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2719 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2722 drv
= bdrv_find_format(format
);
2724 error_setg(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
2729 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR
, errp
)) {
2733 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2734 source
= bs
->backing_hd
;
2735 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
2736 sync
= MIRROR_SYNC_MODE_FULL
;
2738 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2742 size
= bdrv_getlength(bs
);
2744 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2749 BlockDriverState
*to_replace_bs
;
2750 AioContext
*replace_aio_context
;
2751 int64_t replace_size
;
2753 if (!has_node_name
) {
2754 error_setg(errp
, "a node-name must be provided when replacing a"
2755 " named node of the graph");
2759 to_replace_bs
= check_to_replace_node(replaces
, &local_err
);
2761 if (!to_replace_bs
) {
2762 error_propagate(errp
, local_err
);
2766 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
2767 aio_context_acquire(replace_aio_context
);
2768 replace_size
= bdrv_getlength(to_replace_bs
);
2769 aio_context_release(replace_aio_context
);
2771 if (size
!= replace_size
) {
2772 error_setg(errp
, "cannot replace image with a mirror image of "
2778 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
2779 && mode
!= NEW_IMAGE_MODE_EXISTING
)
2781 /* create new image w/o backing file */
2782 assert(format
&& drv
);
2783 bdrv_img_create(target
, format
,
2784 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
2787 case NEW_IMAGE_MODE_EXISTING
:
2789 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
2790 /* create new image with backing file */
2791 bdrv_img_create(target
, format
,
2793 source
->drv
->format_name
,
2794 NULL
, size
, flags
, &local_err
, false);
2802 error_propagate(errp
, local_err
);
2806 if (has_node_name
) {
2807 options
= qdict_new();
2808 qdict_put(options
, "node-name", qstring_from_str(node_name
));
2811 /* Mirroring takes care of copy-on-write using the source's backing
2815 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
2816 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
2818 error_propagate(errp
, local_err
);
2822 bdrv_set_aio_context(target_bs
, aio_context
);
2824 /* pass the node name to replace to mirror start since it's loose coupling
2825 * and will allow to check whether the node still exist at mirror completion
2827 mirror_start(bs
, target_bs
,
2828 has_replaces
? replaces
: NULL
,
2829 speed
, granularity
, buf_size
, sync
,
2830 on_source_error
, on_target_error
,
2831 block_job_cb
, bs
, &local_err
);
2832 if (local_err
!= NULL
) {
2833 bdrv_unref(target_bs
);
2834 error_propagate(errp
, local_err
);
2839 aio_context_release(aio_context
);
2842 /* Get the block job for a given device name and acquire its AioContext */
2843 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
2847 BlockDriverState
*bs
;
2849 blk
= blk_by_name(device
);
2855 *aio_context
= bdrv_get_aio_context(bs
);
2856 aio_context_acquire(*aio_context
);
2859 aio_context_release(*aio_context
);
2866 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
2867 "No active block job on device '%s'", device
);
2868 *aio_context
= NULL
;
2872 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
2874 AioContext
*aio_context
;
2875 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2881 block_job_set_speed(job
, speed
, errp
);
2882 aio_context_release(aio_context
);
2885 void qmp_block_job_cancel(const char *device
,
2886 bool has_force
, bool force
, Error
**errp
)
2888 AioContext
*aio_context
;
2889 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2899 if (job
->user_paused
&& !force
) {
2900 error_setg(errp
, "The block job for device '%s' is currently paused",
2905 trace_qmp_block_job_cancel(job
);
2906 block_job_cancel(job
);
2908 aio_context_release(aio_context
);
2911 void qmp_block_job_pause(const char *device
, Error
**errp
)
2913 AioContext
*aio_context
;
2914 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2916 if (!job
|| job
->user_paused
) {
2920 job
->user_paused
= true;
2921 trace_qmp_block_job_pause(job
);
2922 block_job_pause(job
);
2923 aio_context_release(aio_context
);
2926 void qmp_block_job_resume(const char *device
, Error
**errp
)
2928 AioContext
*aio_context
;
2929 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2931 if (!job
|| !job
->user_paused
) {
2935 job
->user_paused
= false;
2936 trace_qmp_block_job_resume(job
);
2937 block_job_resume(job
);
2938 aio_context_release(aio_context
);
2941 void qmp_block_job_complete(const char *device
, Error
**errp
)
2943 AioContext
*aio_context
;
2944 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2950 trace_qmp_block_job_complete(job
);
2951 block_job_complete(job
, errp
);
2952 aio_context_release(aio_context
);
2955 void qmp_change_backing_file(const char *device
,
2956 const char *image_node_name
,
2957 const char *backing_file
,
2961 BlockDriverState
*bs
= NULL
;
2962 AioContext
*aio_context
;
2963 BlockDriverState
*image_bs
= NULL
;
2964 Error
*local_err
= NULL
;
2969 blk
= blk_by_name(device
);
2971 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2972 "Device '%s' not found", device
);
2977 aio_context
= bdrv_get_aio_context(bs
);
2978 aio_context_acquire(aio_context
);
2980 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
2982 error_propagate(errp
, local_err
);
2987 error_setg(errp
, "image file not found");
2991 if (bdrv_find_base(image_bs
) == image_bs
) {
2992 error_setg(errp
, "not allowing backing file change on an image "
2993 "without a backing file");
2997 /* even though we are not necessarily operating on bs, we need it to
2998 * determine if block ops are currently prohibited on the chain */
2999 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3003 /* final sanity check */
3004 if (!bdrv_chain_contains(bs
, image_bs
)) {
3005 error_setg(errp
, "'%s' and image file are not in the same chain",
3010 /* if not r/w, reopen to make r/w */
3011 open_flags
= image_bs
->open_flags
;
3012 ro
= bdrv_is_read_only(image_bs
);
3015 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3017 error_propagate(errp
, local_err
);
3022 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3023 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3026 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3028 /* don't exit here, so we can try to restore open flags if
3033 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3035 error_propagate(errp
, local_err
); /* will preserve prior errp */
3040 aio_context_release(aio_context
);
3043 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3045 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3049 Error
*local_err
= NULL
;
3051 /* Require an ID in the top level */
3052 if (!options
->has_id
) {
3053 error_setg(errp
, "Block device needs an ID");
3057 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3058 * cache.direct=false instead of silently switching to aio=threads, except
3059 * when called from drive_new().
3061 * For now, simply forbidding the combination for all drivers will do. */
3062 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3063 bool direct
= options
->has_cache
&&
3064 options
->cache
->has_direct
&&
3065 options
->cache
->direct
;
3067 error_setg(errp
, "aio=native requires cache.direct=true");
3072 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
3073 &options
, NULL
, &local_err
);
3075 error_propagate(errp
, local_err
);
3079 obj
= qmp_output_get_qobject(ov
);
3080 qdict
= qobject_to_qdict(obj
);
3082 qdict_flatten(qdict
);
3084 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3086 error_propagate(errp
, local_err
);
3090 if (bdrv_key_required(blk_bs(blk
))) {
3092 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3097 qmp_output_visitor_cleanup(ov
);
3100 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3102 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3103 BlockDriverState
*bs
;
3105 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3106 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
3108 aio_context_acquire(aio_context
);
3111 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
3112 elem
->value
= block_job_query(bs
->job
);
3114 p_next
= &elem
->next
;
3117 aio_context_release(aio_context
);
3123 QemuOptsList qemu_common_drive_opts
= {
3125 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3129 .type
= QEMU_OPT_BOOL
,
3130 .help
= "enable/disable snapshot mode",
3133 .type
= QEMU_OPT_STRING
,
3134 .help
= "discard operation (ignore/off, unmap/on)",
3136 .name
= BDRV_OPT_CACHE_WB
,
3137 .type
= QEMU_OPT_BOOL
,
3138 .help
= "enables writeback mode for any caches",
3140 .name
= BDRV_OPT_CACHE_DIRECT
,
3141 .type
= QEMU_OPT_BOOL
,
3142 .help
= "enables use of O_DIRECT (bypass the host page cache)",
3144 .name
= BDRV_OPT_CACHE_NO_FLUSH
,
3145 .type
= QEMU_OPT_BOOL
,
3146 .help
= "ignore any flush requests for the device",
3149 .type
= QEMU_OPT_STRING
,
3150 .help
= "host AIO implementation (threads, native)",
3153 .type
= QEMU_OPT_STRING
,
3154 .help
= "disk format (raw, qcow2, ...)",
3157 .type
= QEMU_OPT_STRING
,
3158 .help
= "read error action",
3161 .type
= QEMU_OPT_STRING
,
3162 .help
= "write error action",
3164 .name
= "read-only",
3165 .type
= QEMU_OPT_BOOL
,
3166 .help
= "open drive file as read-only",
3168 .name
= "throttling.iops-total",
3169 .type
= QEMU_OPT_NUMBER
,
3170 .help
= "limit total I/O operations per second",
3172 .name
= "throttling.iops-read",
3173 .type
= QEMU_OPT_NUMBER
,
3174 .help
= "limit read operations per second",
3176 .name
= "throttling.iops-write",
3177 .type
= QEMU_OPT_NUMBER
,
3178 .help
= "limit write operations per second",
3180 .name
= "throttling.bps-total",
3181 .type
= QEMU_OPT_NUMBER
,
3182 .help
= "limit total bytes per second",
3184 .name
= "throttling.bps-read",
3185 .type
= QEMU_OPT_NUMBER
,
3186 .help
= "limit read bytes per second",
3188 .name
= "throttling.bps-write",
3189 .type
= QEMU_OPT_NUMBER
,
3190 .help
= "limit write bytes per second",
3192 .name
= "throttling.iops-total-max",
3193 .type
= QEMU_OPT_NUMBER
,
3194 .help
= "I/O operations burst",
3196 .name
= "throttling.iops-read-max",
3197 .type
= QEMU_OPT_NUMBER
,
3198 .help
= "I/O operations read burst",
3200 .name
= "throttling.iops-write-max",
3201 .type
= QEMU_OPT_NUMBER
,
3202 .help
= "I/O operations write burst",
3204 .name
= "throttling.bps-total-max",
3205 .type
= QEMU_OPT_NUMBER
,
3206 .help
= "total bytes burst",
3208 .name
= "throttling.bps-read-max",
3209 .type
= QEMU_OPT_NUMBER
,
3210 .help
= "total bytes read burst",
3212 .name
= "throttling.bps-write-max",
3213 .type
= QEMU_OPT_NUMBER
,
3214 .help
= "total bytes write burst",
3216 .name
= "throttling.iops-size",
3217 .type
= QEMU_OPT_NUMBER
,
3218 .help
= "when limiting by iops max size of an I/O in bytes",
3220 .name
= "throttling.group",
3221 .type
= QEMU_OPT_STRING
,
3222 .help
= "name of the block throttling group",
3224 .name
= "copy-on-read",
3225 .type
= QEMU_OPT_BOOL
,
3226 .help
= "copy read data from backing file into image file",
3228 .name
= "detect-zeroes",
3229 .type
= QEMU_OPT_STRING
,
3230 .help
= "try to optimize zero writes (off, on, unmap)",
3232 { /* end of list */ }
3236 QemuOptsList qemu_drive_opts
= {
3238 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
3241 * no elements => accept any params
3242 * validation will happen later
3244 { /* end of list */ }