2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
9 * This file incorporates work covered by the following copyright and
12 * Copyright (c) 2003-2008 Fabrice Bellard
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "sysemu/block-backend.h"
34 #include "sysemu/blockdev.h"
35 #include "hw/block/block.h"
36 #include "block/blockjob.h"
37 #include "block/throttle-groups.h"
38 #include "monitor/monitor.h"
39 #include "qemu/error-report.h"
40 #include "qemu/option.h"
41 #include "qemu/config-file.h"
42 #include "qapi/qmp/types.h"
43 #include "qapi-visit.h"
44 #include "qapi/qmp/qerror.h"
45 #include "qapi/qmp-output-visitor.h"
46 #include "qapi/util.h"
47 #include "sysemu/sysemu.h"
48 #include "block/block_int.h"
49 #include "qmp-commands.h"
51 #include "sysemu/arch_init.h"
53 static const char *const if_name
[IF_COUNT
] = {
57 [IF_FLOPPY
] = "floppy",
58 [IF_PFLASH
] = "pflash",
61 [IF_VIRTIO
] = "virtio",
65 static int if_max_devs
[IF_COUNT
] = {
67 * Do not change these numbers! They govern how drive option
68 * index maps to unit and bus. That mapping is ABI.
70 * All controllers used to imlement if=T drives need to support
71 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
72 * Otherwise, some index values map to "impossible" bus, unit
75 * For instance, if you change [IF_SCSI] to 255, -drive
76 * if=scsi,index=12 no longer means bus=1,unit=5, but
77 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
78 * the drive can't be set up. Regression.
85 * Boards may call this to offer board-by-board overrides
86 * of the default, global values.
88 void override_max_devs(BlockInterfaceType type
, int max_devs
)
97 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
98 dinfo
= blk_legacy_dinfo(blk
);
99 if (dinfo
->type
== type
) {
100 fprintf(stderr
, "Cannot override units-per-bus property of"
101 " the %s interface, because a drive of that type has"
102 " already been added.\n", if_name
[type
]);
103 g_assert_not_reached();
107 if_max_devs
[type
] = max_devs
;
111 * We automatically delete the drive when a device using it gets
112 * unplugged. Questionable feature, but we can't just drop it.
113 * Device models call blockdev_mark_auto_del() to schedule the
114 * automatic deletion, and generic qdev code calls blockdev_auto_del()
115 * when deletion is actually safe.
117 void blockdev_mark_auto_del(BlockBackend
*blk
)
119 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
120 BlockDriverState
*bs
= blk_bs(blk
);
121 AioContext
*aio_context
;
127 aio_context
= bdrv_get_aio_context(bs
);
128 aio_context_acquire(aio_context
);
131 block_job_cancel(bs
->job
);
134 aio_context_release(aio_context
);
139 void blockdev_auto_del(BlockBackend
*blk
)
141 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
143 if (dinfo
&& dinfo
->auto_del
) {
149 * Returns the current mapping of how many units per bus
150 * a particular interface can support.
152 * A positive integer indicates n units per bus.
153 * 0 implies the mapping has not been established.
154 * -1 indicates an invalid BlockInterfaceType was given.
156 int drive_get_max_devs(BlockInterfaceType type
)
158 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
159 return if_max_devs
[type
];
165 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
167 int max_devs
= if_max_devs
[type
];
168 return max_devs
? index
/ max_devs
: 0;
171 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
173 int max_devs
= if_max_devs
[type
];
174 return max_devs
? index
% max_devs
: index
;
177 QemuOpts
*drive_def(const char *optstr
)
179 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
182 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
187 opts
= drive_def(optstr
);
191 if (type
!= IF_DEFAULT
) {
192 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
195 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
198 qemu_opt_set(opts
, "file", file
, &error_abort
);
202 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
207 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
208 dinfo
= blk_legacy_dinfo(blk
);
209 if (dinfo
&& dinfo
->type
== type
210 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
218 bool drive_check_orphaned(void)
224 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
225 dinfo
= blk_legacy_dinfo(blk
);
226 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
227 /* Unless this is a default drive, this may be an oversight. */
228 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
229 dinfo
->type
!= IF_NONE
) {
230 fprintf(stderr
, "Warning: Orphaned drive without device: "
231 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
232 blk_name(blk
), blk_bs(blk
)->filename
, if_name
[dinfo
->type
],
233 dinfo
->bus
, dinfo
->unit
);
241 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
243 return drive_get(type
,
244 drive_index_to_bus_id(type
, index
),
245 drive_index_to_unit_id(type
, index
));
248 int drive_get_max_bus(BlockInterfaceType type
)
255 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
256 dinfo
= blk_legacy_dinfo(blk
);
257 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
258 max_bus
= dinfo
->bus
;
264 /* Get a block device. This should only be used for single-drive devices
265 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
267 DriveInfo
*drive_get_next(BlockInterfaceType type
)
269 static int next_block_unit
[IF_COUNT
];
271 return drive_get(type
, 0, next_block_unit
[type
]++);
274 static void bdrv_format_print(void *opaque
, const char *name
)
276 error_printf(" %s", name
);
281 BlockDriverState
*bs
;
284 static void bdrv_put_ref_bh(void *opaque
)
286 BDRVPutRefBH
*s
= opaque
;
289 qemu_bh_delete(s
->bh
);
294 * Release a BDS reference in a BH
296 * It is not safe to use bdrv_unref() from a callback function when the callers
297 * still need the BlockDriverState. In such cases we schedule a BH to release
300 static void bdrv_put_ref_bh_schedule(BlockDriverState
*bs
)
304 s
= g_new(BDRVPutRefBH
, 1);
305 s
->bh
= qemu_bh_new(bdrv_put_ref_bh
, s
);
307 qemu_bh_schedule(s
->bh
);
310 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
312 if (!strcmp(buf
, "ignore")) {
313 return BLOCKDEV_ON_ERROR_IGNORE
;
314 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
315 return BLOCKDEV_ON_ERROR_ENOSPC
;
316 } else if (!strcmp(buf
, "stop")) {
317 return BLOCKDEV_ON_ERROR_STOP
;
318 } else if (!strcmp(buf
, "report")) {
319 return BLOCKDEV_ON_ERROR_REPORT
;
321 error_setg(errp
, "'%s' invalid %s error action",
322 buf
, is_read
? "read" : "write");
327 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
329 if (throttle_conflicting(cfg
)) {
330 error_setg(errp
, "bps/iops/max total values and read/write values"
331 " cannot be used at the same time");
335 if (!throttle_is_valid(cfg
)) {
336 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
340 if (throttle_max_is_missing_limit(cfg
)) {
341 error_setg(errp
, "bps_max/iops_max require corresponding"
349 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
351 /* Takes the ownership of bs_opts */
352 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
358 int on_read_error
, on_write_error
;
360 BlockDriverState
*bs
;
367 bool has_driver_specific_opts
;
368 BlockdevDetectZeroesOptions detect_zeroes
;
369 const char *throttling_group
;
371 /* Check common options by copying from bs_opts to opts, all other options
372 * stay in bs_opts for processing by bdrv_open(). */
373 id
= qdict_get_try_str(bs_opts
, "id");
374 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
376 error_propagate(errp
, error
);
380 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
382 error_propagate(errp
, error
);
387 qdict_del(bs_opts
, "id");
390 has_driver_specific_opts
= !!qdict_size(bs_opts
);
392 /* extract parameters */
393 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
394 ro
= qemu_opt_get_bool(opts
, "read-only", 0);
395 copy_on_read
= qemu_opt_get_bool(opts
, "copy-on-read", false);
397 if ((buf
= qemu_opt_get(opts
, "discard")) != NULL
) {
398 if (bdrv_parse_discard_flags(buf
, &bdrv_flags
) != 0) {
399 error_setg(errp
, "invalid discard option");
404 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true)) {
405 bdrv_flags
|= BDRV_O_CACHE_WB
;
407 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_DIRECT
, false)) {
408 bdrv_flags
|= BDRV_O_NOCACHE
;
410 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_NO_FLUSH
, false)) {
411 bdrv_flags
|= BDRV_O_NO_FLUSH
;
414 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
415 if (!strcmp(buf
, "native")) {
416 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
417 } else if (!strcmp(buf
, "threads")) {
418 /* this is the default */
420 error_setg(errp
, "invalid aio option");
425 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
426 if (is_help_option(buf
)) {
427 error_printf("Supported formats:");
428 bdrv_iterate_format(bdrv_format_print
, NULL
);
433 if (qdict_haskey(bs_opts
, "driver")) {
434 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
437 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
440 /* disk I/O throttling */
441 memset(&cfg
, 0, sizeof(cfg
));
442 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
=
443 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
444 cfg
.buckets
[THROTTLE_BPS_READ
].avg
=
445 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
446 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
=
447 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
448 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
=
449 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
450 cfg
.buckets
[THROTTLE_OPS_READ
].avg
=
451 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
452 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
=
453 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
455 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
=
456 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
457 cfg
.buckets
[THROTTLE_BPS_READ
].max
=
458 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
459 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
=
460 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
461 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
=
462 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
463 cfg
.buckets
[THROTTLE_OPS_READ
].max
=
464 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
465 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
=
466 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
468 cfg
.op_size
= qemu_opt_get_number(opts
, "throttling.iops-size", 0);
470 throttling_group
= qemu_opt_get(opts
, "throttling.group");
472 if (!check_throttle_config(&cfg
, &error
)) {
473 error_propagate(errp
, error
);
477 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
478 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
479 on_write_error
= parse_block_error_action(buf
, 0, &error
);
481 error_propagate(errp
, error
);
486 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
487 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
488 on_read_error
= parse_block_error_action(buf
, 1, &error
);
490 error_propagate(errp
, error
);
496 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
497 qemu_opt_get(opts
, "detect-zeroes"),
498 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX
,
499 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
502 error_propagate(errp
, error
);
506 if (detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
507 !(bdrv_flags
& BDRV_O_UNMAP
)) {
508 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
509 "without setting discard operation to unmap");
514 if ((!file
|| !*file
) && !has_driver_specific_opts
) {
515 blk
= blk_new_with_bs(qemu_opts_id(opts
), errp
);
521 bs
->open_flags
= snapshot
? BDRV_O_SNAPSHOT
: 0;
526 if (file
&& !*file
) {
531 /* always use cache=unsafe with snapshot */
532 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
533 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
537 bdrv_flags
|= BDRV_O_COPY_ON_READ
;
540 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
542 blk
= blk_new_open(qemu_opts_id(opts
), file
, NULL
, bs_opts
, bdrv_flags
,
550 bs
->detect_zeroes
= detect_zeroes
;
552 blk_set_on_error(blk
, on_read_error
, on_write_error
);
554 /* disk I/O throttling */
555 if (throttle_enabled(&cfg
)) {
556 if (!throttling_group
) {
557 throttling_group
= blk_name(blk
);
559 bdrv_io_limits_enable(bs
, throttling_group
);
560 bdrv_set_io_limits(bs
, &cfg
);
563 if (bdrv_key_required(bs
)) {
578 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
583 value
= qemu_opt_get(opts
, from
);
585 if (qemu_opt_find(opts
, to
)) {
586 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
587 "same time", to
, from
);
592 /* rename all items in opts */
593 while ((value
= qemu_opt_get(opts
, from
))) {
594 qemu_opt_set(opts
, to
, value
, &error_abort
);
595 qemu_opt_unset(opts
, from
);
599 QemuOptsList qemu_legacy_drive_opts
= {
601 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
605 .type
= QEMU_OPT_NUMBER
,
606 .help
= "bus number",
609 .type
= QEMU_OPT_NUMBER
,
610 .help
= "unit number (i.e. lun for scsi)",
613 .type
= QEMU_OPT_NUMBER
,
614 .help
= "index number",
617 .type
= QEMU_OPT_STRING
,
618 .help
= "media type (disk, cdrom)",
621 .type
= QEMU_OPT_STRING
,
622 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
625 .type
= QEMU_OPT_NUMBER
,
626 .help
= "number of cylinders (ide disk geometry)",
629 .type
= QEMU_OPT_NUMBER
,
630 .help
= "number of heads (ide disk geometry)",
633 .type
= QEMU_OPT_NUMBER
,
634 .help
= "number of sectors (ide disk geometry)",
637 .type
= QEMU_OPT_STRING
,
638 .help
= "chs translation (auto, lba, none)",
641 .type
= QEMU_OPT_BOOL
,
642 .help
= "(deprecated, ignored)",
645 .type
= QEMU_OPT_STRING
,
646 .help
= "pci address (virtio only)",
649 .type
= QEMU_OPT_STRING
,
650 .help
= "disk serial number",
653 .type
= QEMU_OPT_STRING
,
657 /* Options that are passed on, but have special semantics with -drive */
660 .type
= QEMU_OPT_BOOL
,
661 .help
= "open drive file as read-only",
664 .type
= QEMU_OPT_STRING
,
665 .help
= "read error action",
668 .type
= QEMU_OPT_STRING
,
669 .help
= "write error action",
671 .name
= "copy-on-read",
672 .type
= QEMU_OPT_BOOL
,
673 .help
= "copy read data from backing file into image file",
676 { /* end of list */ }
680 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
684 DriveInfo
*dinfo
= NULL
;
686 QemuOpts
*legacy_opts
;
687 DriveMediaType media
= MEDIA_DISK
;
688 BlockInterfaceType type
;
689 int cyls
, heads
, secs
, translation
;
690 int max_devs
, bus_id
, unit_id
, index
;
692 const char *werror
, *rerror
;
693 bool read_only
= false;
696 const char *filename
;
697 Error
*local_err
= NULL
;
700 /* Change legacy command line options into QMP ones */
701 static const struct {
705 { "iops", "throttling.iops-total" },
706 { "iops_rd", "throttling.iops-read" },
707 { "iops_wr", "throttling.iops-write" },
709 { "bps", "throttling.bps-total" },
710 { "bps_rd", "throttling.bps-read" },
711 { "bps_wr", "throttling.bps-write" },
713 { "iops_max", "throttling.iops-total-max" },
714 { "iops_rd_max", "throttling.iops-read-max" },
715 { "iops_wr_max", "throttling.iops-write-max" },
717 { "bps_max", "throttling.bps-total-max" },
718 { "bps_rd_max", "throttling.bps-read-max" },
719 { "bps_wr_max", "throttling.bps-write-max" },
721 { "iops_size", "throttling.iops-size" },
723 { "group", "throttling.group" },
725 { "readonly", "read-only" },
728 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
729 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
732 error_report_err(local_err
);
737 value
= qemu_opt_get(all_opts
, "cache");
741 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
742 error_report("invalid cache option");
746 /* Specific options take precedence */
747 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
748 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
749 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
751 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
752 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
753 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
755 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
756 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
757 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
759 qemu_opt_unset(all_opts
, "cache");
762 /* Get a QDict for processing the options */
763 bs_opts
= qdict_new();
764 qemu_opts_to_qdict(all_opts
, bs_opts
);
766 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
768 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
770 error_report_err(local_err
);
774 /* Deprecated option boot=[on|off] */
775 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
776 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
777 "ignored. Future versions will reject this parameter. Please "
778 "update your scripts.\n");
782 value
= qemu_opt_get(legacy_opts
, "media");
784 if (!strcmp(value
, "disk")) {
786 } else if (!strcmp(value
, "cdrom")) {
790 error_report("'%s' invalid media", value
);
795 /* copy-on-read is disabled with a warning for read-only devices */
796 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
797 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
799 if (read_only
&& copy_on_read
) {
800 error_report("warning: disabling copy-on-read on read-only drive");
801 copy_on_read
= false;
804 qdict_put(bs_opts
, "read-only",
805 qstring_from_str(read_only
? "on" : "off"));
806 qdict_put(bs_opts
, "copy-on-read",
807 qstring_from_str(copy_on_read
? "on" :"off"));
809 /* Controller type */
810 value
= qemu_opt_get(legacy_opts
, "if");
813 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
816 if (type
== IF_COUNT
) {
817 error_report("unsupported bus type '%s'", value
);
821 type
= block_default_type
;
825 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
826 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
827 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
829 if (cyls
|| heads
|| secs
) {
831 error_report("invalid physical cyls number");
835 error_report("invalid physical heads number");
839 error_report("invalid physical secs number");
844 translation
= BIOS_ATA_TRANSLATION_AUTO
;
845 value
= qemu_opt_get(legacy_opts
, "trans");
848 error_report("'%s' trans must be used with cyls, heads and secs",
852 if (!strcmp(value
, "none")) {
853 translation
= BIOS_ATA_TRANSLATION_NONE
;
854 } else if (!strcmp(value
, "lba")) {
855 translation
= BIOS_ATA_TRANSLATION_LBA
;
856 } else if (!strcmp(value
, "large")) {
857 translation
= BIOS_ATA_TRANSLATION_LARGE
;
858 } else if (!strcmp(value
, "rechs")) {
859 translation
= BIOS_ATA_TRANSLATION_RECHS
;
860 } else if (!strcmp(value
, "auto")) {
861 translation
= BIOS_ATA_TRANSLATION_AUTO
;
863 error_report("'%s' invalid translation type", value
);
868 if (media
== MEDIA_CDROM
) {
869 if (cyls
|| secs
|| heads
) {
870 error_report("CHS can't be set with media=cdrom");
875 /* Device address specified by bus/unit or index.
876 * If none was specified, try to find the first free one. */
877 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
878 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
879 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
881 max_devs
= if_max_devs
[type
];
884 if (bus_id
!= 0 || unit_id
!= -1) {
885 error_report("index cannot be used with bus and unit");
888 bus_id
= drive_index_to_bus_id(type
, index
);
889 unit_id
= drive_index_to_unit_id(type
, index
);
894 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
896 if (max_devs
&& unit_id
>= max_devs
) {
903 if (max_devs
&& unit_id
>= max_devs
) {
904 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
908 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
909 error_report("drive with bus=%d, unit=%d (index=%d) exists",
910 bus_id
, unit_id
, index
);
915 serial
= qemu_opt_get(legacy_opts
, "serial");
917 /* no id supplied -> create one */
918 if (qemu_opts_id(all_opts
) == NULL
) {
920 const char *mediastr
= "";
921 if (type
== IF_IDE
|| type
== IF_SCSI
) {
922 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
925 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
928 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
931 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
935 /* Add virtio block device */
936 devaddr
= qemu_opt_get(legacy_opts
, "addr");
937 if (devaddr
&& type
!= IF_VIRTIO
) {
938 error_report("addr is not supported by this bus type");
942 if (type
== IF_VIRTIO
) {
944 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
946 if (arch_type
== QEMU_ARCH_S390X
) {
947 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
949 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
951 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
954 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
958 filename
= qemu_opt_get(legacy_opts
, "file");
960 /* Check werror/rerror compatibility with if=... */
961 werror
= qemu_opt_get(legacy_opts
, "werror");
962 if (werror
!= NULL
) {
963 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
965 error_report("werror is not supported by this bus type");
968 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
971 rerror
= qemu_opt_get(legacy_opts
, "rerror");
972 if (rerror
!= NULL
) {
973 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
975 error_report("rerror is not supported by this bus type");
978 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
981 /* Actual block device init: Functionality shared with blockdev-add */
982 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
986 error_report_err(local_err
);
993 /* Create legacy DriveInfo */
994 dinfo
= g_malloc0(sizeof(*dinfo
));
995 dinfo
->opts
= all_opts
;
998 dinfo
->heads
= heads
;
1000 dinfo
->trans
= translation
;
1003 dinfo
->bus
= bus_id
;
1004 dinfo
->unit
= unit_id
;
1005 dinfo
->devaddr
= devaddr
;
1006 dinfo
->serial
= g_strdup(serial
);
1008 blk_set_legacy_dinfo(blk
, dinfo
);
1015 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1022 qemu_opts_del(legacy_opts
);
1027 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1029 const char *device
= qdict_get_str(qdict
, "device");
1033 if (!strcmp(device
, "all")) {
1034 ret
= bdrv_commit_all();
1036 blk
= blk_by_name(device
);
1038 monitor_printf(mon
, "Device '%s' not found\n", device
);
1041 ret
= bdrv_commit(blk_bs(blk
));
1044 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1049 static void blockdev_do_action(int kind
, void *data
, Error
**errp
)
1051 TransactionAction action
;
1052 TransactionActionList list
;
1056 list
.value
= &action
;
1058 qmp_transaction(&list
, errp
);
1061 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1062 bool has_node_name
, const char *node_name
,
1063 const char *snapshot_file
,
1064 bool has_snapshot_node_name
,
1065 const char *snapshot_node_name
,
1066 bool has_format
, const char *format
,
1067 bool has_mode
, NewImageMode mode
, Error
**errp
)
1069 BlockdevSnapshot snapshot
= {
1070 .has_device
= has_device
,
1071 .device
= (char *) device
,
1072 .has_node_name
= has_node_name
,
1073 .node_name
= (char *) node_name
,
1074 .snapshot_file
= (char *) snapshot_file
,
1075 .has_snapshot_node_name
= has_snapshot_node_name
,
1076 .snapshot_node_name
= (char *) snapshot_node_name
,
1077 .has_format
= has_format
,
1078 .format
= (char *) format
,
1079 .has_mode
= has_mode
,
1082 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1086 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1090 BlockdevSnapshotInternal snapshot
= {
1091 .device
= (char *) device
,
1092 .name
= (char *) name
1095 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1099 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1106 BlockDriverState
*bs
;
1108 AioContext
*aio_context
;
1109 QEMUSnapshotInfo sn
;
1110 Error
*local_err
= NULL
;
1111 SnapshotInfo
*info
= NULL
;
1114 blk
= blk_by_name(device
);
1116 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1117 "Device '%s' not found", device
);
1131 error_setg(errp
, "Name or id must be provided");
1135 aio_context
= bdrv_get_aio_context(bs
);
1136 aio_context_acquire(aio_context
);
1138 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1139 goto out_aio_context
;
1142 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1144 error_propagate(errp
, local_err
);
1145 goto out_aio_context
;
1149 "Snapshot with id '%s' and name '%s' does not exist on "
1151 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1152 goto out_aio_context
;
1155 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1157 error_propagate(errp
, local_err
);
1158 goto out_aio_context
;
1161 aio_context_release(aio_context
);
1163 info
= g_new0(SnapshotInfo
, 1);
1164 info
->id
= g_strdup(sn
.id_str
);
1165 info
->name
= g_strdup(sn
.name
);
1166 info
->date_nsec
= sn
.date_nsec
;
1167 info
->date_sec
= sn
.date_sec
;
1168 info
->vm_state_size
= sn
.vm_state_size
;
1169 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1170 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1175 aio_context_release(aio_context
);
1180 * block_dirty_bitmap_lookup:
1181 * Return a dirty bitmap (if present), after validating
1182 * the node reference and bitmap names.
1184 * @node: The name of the BDS node to search for bitmaps
1185 * @name: The name of the bitmap to search for
1186 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1187 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1188 * @errp: Output pointer for error information. Can be NULL.
1190 * @return: A bitmap object on success, or NULL on failure.
1192 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1194 BlockDriverState
**pbs
,
1198 BlockDriverState
*bs
;
1199 BdrvDirtyBitmap
*bitmap
;
1200 AioContext
*aio_context
;
1203 error_setg(errp
, "Node cannot be NULL");
1207 error_setg(errp
, "Bitmap name cannot be NULL");
1210 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1212 error_setg(errp
, "Node '%s' not found", node
);
1216 aio_context
= bdrv_get_aio_context(bs
);
1217 aio_context_acquire(aio_context
);
1219 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1221 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1229 *paio
= aio_context
;
1231 aio_context_release(aio_context
);
1237 aio_context_release(aio_context
);
1241 /* New and old BlockDriverState structs for atomic group operations */
1243 typedef struct BlkTransactionState BlkTransactionState
;
1245 /* Only prepare() may fail. In a single transaction, only one of commit() or
1246 abort() will be called, clean() will always be called if it present. */
1247 typedef struct BdrvActionOps
{
1248 /* Size of state struct, in bytes. */
1249 size_t instance_size
;
1250 /* Prepare the work, must NOT be NULL. */
1251 void (*prepare
)(BlkTransactionState
*common
, Error
**errp
);
1252 /* Commit the changes, can be NULL. */
1253 void (*commit
)(BlkTransactionState
*common
);
1254 /* Abort the changes on fail, can be NULL. */
1255 void (*abort
)(BlkTransactionState
*common
);
1256 /* Clean up resource in the end, can be NULL. */
1257 void (*clean
)(BlkTransactionState
*common
);
1261 * This structure must be arranged as first member in child type, assuming
1262 * that compiler will also arrange it to the same address with parent instance.
1263 * Later it will be used in free().
1265 struct BlkTransactionState
{
1266 TransactionAction
*action
;
1267 const BdrvActionOps
*ops
;
1268 QSIMPLEQ_ENTRY(BlkTransactionState
) entry
;
1271 /* internal snapshot private data */
1272 typedef struct InternalSnapshotState
{
1273 BlkTransactionState common
;
1274 BlockDriverState
*bs
;
1275 AioContext
*aio_context
;
1276 QEMUSnapshotInfo sn
;
1277 } InternalSnapshotState
;
1279 static void internal_snapshot_prepare(BlkTransactionState
*common
,
1282 Error
*local_err
= NULL
;
1286 BlockDriverState
*bs
;
1287 QEMUSnapshotInfo old_sn
, *sn
;
1290 BlockdevSnapshotInternal
*internal
;
1291 InternalSnapshotState
*state
;
1294 g_assert(common
->action
->kind
==
1295 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1296 internal
= common
->action
->blockdev_snapshot_internal_sync
;
1297 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1299 /* 1. parse input */
1300 device
= internal
->device
;
1301 name
= internal
->name
;
1303 /* 2. check for validation */
1304 blk
= blk_by_name(device
);
1306 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1307 "Device '%s' not found", device
);
1312 /* AioContext is released in .clean() */
1313 state
->aio_context
= bdrv_get_aio_context(bs
);
1314 aio_context_acquire(state
->aio_context
);
1316 if (!bdrv_is_inserted(bs
)) {
1317 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1321 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1325 if (bdrv_is_read_only(bs
)) {
1326 error_setg(errp
, "Device '%s' is read only", device
);
1330 if (!bdrv_can_snapshot(bs
)) {
1331 error_setg(errp
, "Block format '%s' used by device '%s' "
1332 "does not support internal snapshots",
1333 bs
->drv
->format_name
, device
);
1337 if (!strlen(name
)) {
1338 error_setg(errp
, "Name is empty");
1342 /* check whether a snapshot with name exist */
1343 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1346 error_propagate(errp
, local_err
);
1350 "Snapshot with name '%s' already exists on device '%s'",
1355 /* 3. take the snapshot */
1357 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1358 qemu_gettimeofday(&tv
);
1359 sn
->date_sec
= tv
.tv_sec
;
1360 sn
->date_nsec
= tv
.tv_usec
* 1000;
1361 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1363 ret1
= bdrv_snapshot_create(bs
, sn
);
1365 error_setg_errno(errp
, -ret1
,
1366 "Failed to create snapshot '%s' on device '%s'",
1371 /* 4. succeed, mark a snapshot is created */
1375 static void internal_snapshot_abort(BlkTransactionState
*common
)
1377 InternalSnapshotState
*state
=
1378 DO_UPCAST(InternalSnapshotState
, common
, common
);
1379 BlockDriverState
*bs
= state
->bs
;
1380 QEMUSnapshotInfo
*sn
= &state
->sn
;
1381 Error
*local_error
= NULL
;
1387 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1388 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1389 "device '%s' in abort: %s",
1392 bdrv_get_device_name(bs
),
1393 error_get_pretty(local_error
));
1394 error_free(local_error
);
1398 static void internal_snapshot_clean(BlkTransactionState
*common
)
1400 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1403 if (state
->aio_context
) {
1404 aio_context_release(state
->aio_context
);
1408 /* external snapshot private data */
1409 typedef struct ExternalSnapshotState
{
1410 BlkTransactionState common
;
1411 BlockDriverState
*old_bs
;
1412 BlockDriverState
*new_bs
;
1413 AioContext
*aio_context
;
1414 } ExternalSnapshotState
;
1416 static void external_snapshot_prepare(BlkTransactionState
*common
,
1421 Error
*local_err
= NULL
;
1422 bool has_device
= false;
1424 bool has_node_name
= false;
1425 const char *node_name
;
1426 bool has_snapshot_node_name
= false;
1427 const char *snapshot_node_name
;
1428 const char *new_image_file
;
1429 const char *format
= "qcow2";
1430 enum NewImageMode mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1431 ExternalSnapshotState
*state
=
1432 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1433 TransactionAction
*action
= common
->action
;
1435 /* get parameters */
1436 g_assert(action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
);
1438 has_device
= action
->blockdev_snapshot_sync
->has_device
;
1439 device
= action
->blockdev_snapshot_sync
->device
;
1440 has_node_name
= action
->blockdev_snapshot_sync
->has_node_name
;
1441 node_name
= action
->blockdev_snapshot_sync
->node_name
;
1442 has_snapshot_node_name
=
1443 action
->blockdev_snapshot_sync
->has_snapshot_node_name
;
1444 snapshot_node_name
= action
->blockdev_snapshot_sync
->snapshot_node_name
;
1446 new_image_file
= action
->blockdev_snapshot_sync
->snapshot_file
;
1447 if (action
->blockdev_snapshot_sync
->has_format
) {
1448 format
= action
->blockdev_snapshot_sync
->format
;
1450 if (action
->blockdev_snapshot_sync
->has_mode
) {
1451 mode
= action
->blockdev_snapshot_sync
->mode
;
1454 /* start processing */
1455 state
->old_bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1456 has_node_name
? node_name
: NULL
,
1459 error_propagate(errp
, local_err
);
1463 if (has_node_name
&& !has_snapshot_node_name
) {
1464 error_setg(errp
, "New snapshot node name missing");
1468 if (has_snapshot_node_name
&& bdrv_find_node(snapshot_node_name
)) {
1469 error_setg(errp
, "New snapshot node name already existing");
1473 /* Acquire AioContext now so any threads operating on old_bs stop */
1474 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1475 aio_context_acquire(state
->aio_context
);
1477 if (!bdrv_is_inserted(state
->old_bs
)) {
1478 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1482 if (bdrv_op_is_blocked(state
->old_bs
,
1483 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1487 if (!bdrv_is_read_only(state
->old_bs
)) {
1488 if (bdrv_flush(state
->old_bs
)) {
1489 error_setg(errp
, QERR_IO_ERROR
);
1494 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1495 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1499 flags
= state
->old_bs
->open_flags
;
1501 /* create new image w/backing file */
1502 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1503 bdrv_img_create(new_image_file
, format
,
1504 state
->old_bs
->filename
,
1505 state
->old_bs
->drv
->format_name
,
1506 NULL
, -1, flags
, &local_err
, false);
1508 error_propagate(errp
, local_err
);
1513 options
= qdict_new();
1514 if (has_snapshot_node_name
) {
1515 qdict_put(options
, "node-name",
1516 qstring_from_str(snapshot_node_name
));
1518 qdict_put(options
, "driver", qstring_from_str(format
));
1520 /* TODO Inherit bs->options or only take explicit options with an
1521 * extended QMP command? */
1522 assert(state
->new_bs
== NULL
);
1523 ret
= bdrv_open(&state
->new_bs
, new_image_file
, NULL
, options
,
1524 flags
| BDRV_O_NO_BACKING
, &local_err
);
1525 /* We will manually add the backing_hd field to the bs later */
1527 error_propagate(errp
, local_err
);
1531 static void external_snapshot_commit(BlkTransactionState
*common
)
1533 ExternalSnapshotState
*state
=
1534 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1536 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1538 /* This removes our old bs and adds the new bs */
1539 bdrv_append(state
->new_bs
, state
->old_bs
);
1540 /* We don't need (or want) to use the transactional
1541 * bdrv_reopen_multiple() across all the entries at once, because we
1542 * don't want to abort all of them if one of them fails the reopen */
1543 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1546 aio_context_release(state
->aio_context
);
1549 static void external_snapshot_abort(BlkTransactionState
*common
)
1551 ExternalSnapshotState
*state
=
1552 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1553 if (state
->new_bs
) {
1554 bdrv_unref(state
->new_bs
);
1556 if (state
->aio_context
) {
1557 aio_context_release(state
->aio_context
);
1561 typedef struct DriveBackupState
{
1562 BlkTransactionState common
;
1563 BlockDriverState
*bs
;
1564 AioContext
*aio_context
;
1568 static void drive_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1570 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1571 BlockDriverState
*bs
;
1573 DriveBackup
*backup
;
1574 Error
*local_err
= NULL
;
1576 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1577 backup
= common
->action
->drive_backup
;
1579 blk
= blk_by_name(backup
->device
);
1581 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1582 "Device '%s' not found", backup
->device
);
1587 /* AioContext is released in .clean() */
1588 state
->aio_context
= bdrv_get_aio_context(bs
);
1589 aio_context_acquire(state
->aio_context
);
1591 qmp_drive_backup(backup
->device
, backup
->target
,
1592 backup
->has_format
, backup
->format
,
1594 backup
->has_mode
, backup
->mode
,
1595 backup
->has_speed
, backup
->speed
,
1596 backup
->has_bitmap
, backup
->bitmap
,
1597 backup
->has_on_source_error
, backup
->on_source_error
,
1598 backup
->has_on_target_error
, backup
->on_target_error
,
1601 error_propagate(errp
, local_err
);
1606 state
->job
= state
->bs
->job
;
1609 static void drive_backup_abort(BlkTransactionState
*common
)
1611 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1612 BlockDriverState
*bs
= state
->bs
;
1614 /* Only cancel if it's the job we started */
1615 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1616 block_job_cancel_sync(bs
->job
);
1620 static void drive_backup_clean(BlkTransactionState
*common
)
1622 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1624 if (state
->aio_context
) {
1625 aio_context_release(state
->aio_context
);
1629 typedef struct BlockdevBackupState
{
1630 BlkTransactionState common
;
1631 BlockDriverState
*bs
;
1633 AioContext
*aio_context
;
1634 } BlockdevBackupState
;
1636 static void blockdev_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1638 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1639 BlockdevBackup
*backup
;
1640 BlockDriverState
*bs
, *target
;
1642 Error
*local_err
= NULL
;
1644 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1645 backup
= common
->action
->blockdev_backup
;
1647 blk
= blk_by_name(backup
->device
);
1649 error_setg(errp
, "Device '%s' not found", backup
->device
);
1654 blk
= blk_by_name(backup
->target
);
1656 error_setg(errp
, "Device '%s' not found", backup
->target
);
1659 target
= blk_bs(blk
);
1661 /* AioContext is released in .clean() */
1662 state
->aio_context
= bdrv_get_aio_context(bs
);
1663 if (state
->aio_context
!= bdrv_get_aio_context(target
)) {
1664 state
->aio_context
= NULL
;
1665 error_setg(errp
, "Backup between two IO threads is not implemented");
1668 aio_context_acquire(state
->aio_context
);
1670 qmp_blockdev_backup(backup
->device
, backup
->target
,
1672 backup
->has_speed
, backup
->speed
,
1673 backup
->has_on_source_error
, backup
->on_source_error
,
1674 backup
->has_on_target_error
, backup
->on_target_error
,
1677 error_propagate(errp
, local_err
);
1682 state
->job
= state
->bs
->job
;
1685 static void blockdev_backup_abort(BlkTransactionState
*common
)
1687 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1688 BlockDriverState
*bs
= state
->bs
;
1690 /* Only cancel if it's the job we started */
1691 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1692 block_job_cancel_sync(bs
->job
);
1696 static void blockdev_backup_clean(BlkTransactionState
*common
)
1698 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1700 if (state
->aio_context
) {
1701 aio_context_release(state
->aio_context
);
1705 static void abort_prepare(BlkTransactionState
*common
, Error
**errp
)
1707 error_setg(errp
, "Transaction aborted using Abort action");
1710 static void abort_commit(BlkTransactionState
*common
)
1712 g_assert_not_reached(); /* this action never succeeds */
1715 static const BdrvActionOps actions
[] = {
1716 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
1717 .instance_size
= sizeof(ExternalSnapshotState
),
1718 .prepare
= external_snapshot_prepare
,
1719 .commit
= external_snapshot_commit
,
1720 .abort
= external_snapshot_abort
,
1722 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
1723 .instance_size
= sizeof(DriveBackupState
),
1724 .prepare
= drive_backup_prepare
,
1725 .abort
= drive_backup_abort
,
1726 .clean
= drive_backup_clean
,
1728 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
1729 .instance_size
= sizeof(BlockdevBackupState
),
1730 .prepare
= blockdev_backup_prepare
,
1731 .abort
= blockdev_backup_abort
,
1732 .clean
= blockdev_backup_clean
,
1734 [TRANSACTION_ACTION_KIND_ABORT
] = {
1735 .instance_size
= sizeof(BlkTransactionState
),
1736 .prepare
= abort_prepare
,
1737 .commit
= abort_commit
,
1739 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
1740 .instance_size
= sizeof(InternalSnapshotState
),
1741 .prepare
= internal_snapshot_prepare
,
1742 .abort
= internal_snapshot_abort
,
1743 .clean
= internal_snapshot_clean
,
1748 * 'Atomic' group operations. The operations are performed as a set, and if
1749 * any fail then we roll back all operations in the group.
1751 void qmp_transaction(TransactionActionList
*dev_list
, Error
**errp
)
1753 TransactionActionList
*dev_entry
= dev_list
;
1754 BlkTransactionState
*state
, *next
;
1755 Error
*local_err
= NULL
;
1757 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkTransactionState
) snap_bdrv_states
;
1758 QSIMPLEQ_INIT(&snap_bdrv_states
);
1760 /* drain all i/o before any operations */
1763 /* We don't do anything in this loop that commits us to the operations */
1764 while (NULL
!= dev_entry
) {
1765 TransactionAction
*dev_info
= NULL
;
1766 const BdrvActionOps
*ops
;
1768 dev_info
= dev_entry
->value
;
1769 dev_entry
= dev_entry
->next
;
1771 assert(dev_info
->kind
< ARRAY_SIZE(actions
));
1773 ops
= &actions
[dev_info
->kind
];
1774 assert(ops
->instance_size
> 0);
1776 state
= g_malloc0(ops
->instance_size
);
1778 state
->action
= dev_info
;
1779 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
1781 state
->ops
->prepare(state
, &local_err
);
1783 error_propagate(errp
, local_err
);
1784 goto delete_and_fail
;
1788 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1789 if (state
->ops
->commit
) {
1790 state
->ops
->commit(state
);
1798 /* failure, and it is all-or-none; roll back all operations */
1799 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1800 if (state
->ops
->abort
) {
1801 state
->ops
->abort(state
);
1805 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
1806 if (state
->ops
->clean
) {
1807 state
->ops
->clean(state
);
1814 static void eject_device(BlockBackend
*blk
, int force
, Error
**errp
)
1816 BlockDriverState
*bs
= blk_bs(blk
);
1817 AioContext
*aio_context
;
1819 aio_context
= bdrv_get_aio_context(bs
);
1820 aio_context_acquire(aio_context
);
1822 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
1825 if (!blk_dev_has_removable_media(blk
)) {
1826 error_setg(errp
, "Device '%s' is not removable",
1827 bdrv_get_device_name(bs
));
1831 if (blk_dev_is_medium_locked(blk
) && !blk_dev_is_tray_open(blk
)) {
1832 blk_dev_eject_request(blk
, force
);
1834 error_setg(errp
, "Device '%s' is locked",
1835 bdrv_get_device_name(bs
));
1843 aio_context_release(aio_context
);
1846 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
1850 blk
= blk_by_name(device
);
1852 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1853 "Device '%s' not found", device
);
1857 eject_device(blk
, force
, errp
);
1860 void qmp_block_passwd(bool has_device
, const char *device
,
1861 bool has_node_name
, const char *node_name
,
1862 const char *password
, Error
**errp
)
1864 Error
*local_err
= NULL
;
1865 BlockDriverState
*bs
;
1866 AioContext
*aio_context
;
1868 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1869 has_node_name
? node_name
: NULL
,
1872 error_propagate(errp
, local_err
);
1876 aio_context
= bdrv_get_aio_context(bs
);
1877 aio_context_acquire(aio_context
);
1879 bdrv_add_key(bs
, password
, errp
);
1881 aio_context_release(aio_context
);
1884 /* Assumes AioContext is held */
1885 static void qmp_bdrv_open_encrypted(BlockDriverState
*bs
, const char *filename
,
1886 int bdrv_flags
, const char *format
,
1887 const char *password
, Error
**errp
)
1889 Error
*local_err
= NULL
;
1890 QDict
*options
= NULL
;
1894 options
= qdict_new();
1895 qdict_put(options
, "driver", qstring_from_str(format
));
1898 ret
= bdrv_open(&bs
, filename
, NULL
, options
, bdrv_flags
, &local_err
);
1900 error_propagate(errp
, local_err
);
1904 bdrv_add_key(bs
, password
, errp
);
1907 void qmp_change_blockdev(const char *device
, const char *filename
,
1908 const char *format
, Error
**errp
)
1911 BlockDriverState
*bs
;
1912 AioContext
*aio_context
;
1916 blk
= blk_by_name(device
);
1918 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1919 "Device '%s' not found", device
);
1924 aio_context
= bdrv_get_aio_context(bs
);
1925 aio_context_acquire(aio_context
);
1927 eject_device(blk
, 0, &err
);
1929 error_propagate(errp
, err
);
1933 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
1934 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
1936 qmp_bdrv_open_encrypted(bs
, filename
, bdrv_flags
, format
, NULL
, errp
);
1939 aio_context_release(aio_context
);
1942 /* throttling disk I/O limits */
1943 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
1950 bool has_bps_rd_max
,
1952 bool has_bps_wr_max
,
1956 bool has_iops_rd_max
,
1957 int64_t iops_rd_max
,
1958 bool has_iops_wr_max
,
1959 int64_t iops_wr_max
,
1963 const char *group
, Error
**errp
)
1966 BlockDriverState
*bs
;
1968 AioContext
*aio_context
;
1970 blk
= blk_by_name(device
);
1972 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1973 "Device '%s' not found", device
);
1978 memset(&cfg
, 0, sizeof(cfg
));
1979 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
1980 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
1981 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
1983 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
1984 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
1985 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
1988 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
1990 if (has_bps_rd_max
) {
1991 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
1993 if (has_bps_wr_max
) {
1994 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
1997 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
1999 if (has_iops_rd_max
) {
2000 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2002 if (has_iops_wr_max
) {
2003 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2006 if (has_iops_size
) {
2007 cfg
.op_size
= iops_size
;
2010 if (!check_throttle_config(&cfg
, errp
)) {
2014 aio_context
= bdrv_get_aio_context(bs
);
2015 aio_context_acquire(aio_context
);
2017 if (throttle_enabled(&cfg
)) {
2018 /* Enable I/O limits if they're not enabled yet, otherwise
2019 * just update the throttling group. */
2020 if (!bs
->io_limits_enabled
) {
2021 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2022 } else if (has_group
) {
2023 bdrv_io_limits_update_group(bs
, group
);
2025 /* Set the new throttling configuration */
2026 bdrv_set_io_limits(bs
, &cfg
);
2027 } else if (bs
->io_limits_enabled
) {
2028 /* If all throttling settings are set to 0, disable I/O limits */
2029 bdrv_io_limits_disable(bs
);
2032 aio_context_release(aio_context
);
2035 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2036 bool has_granularity
, uint32_t granularity
,
2039 AioContext
*aio_context
;
2040 BlockDriverState
*bs
;
2042 if (!name
|| name
[0] == '\0') {
2043 error_setg(errp
, "Bitmap name cannot be empty");
2047 bs
= bdrv_lookup_bs(node
, node
, errp
);
2052 aio_context
= bdrv_get_aio_context(bs
);
2053 aio_context_acquire(aio_context
);
2055 if (has_granularity
) {
2056 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2057 error_setg(errp
, "Granularity must be power of 2 "
2058 "and at least 512");
2062 /* Default to cluster size, if available: */
2063 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2066 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2069 aio_context_release(aio_context
);
2072 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2075 AioContext
*aio_context
;
2076 BlockDriverState
*bs
;
2077 BdrvDirtyBitmap
*bitmap
;
2079 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2080 if (!bitmap
|| !bs
) {
2084 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2086 "Bitmap '%s' is currently frozen and cannot be removed",
2090 bdrv_dirty_bitmap_make_anon(bitmap
);
2091 bdrv_release_dirty_bitmap(bs
, bitmap
);
2094 aio_context_release(aio_context
);
2098 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2099 * immediately after a full backup operation.
2101 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2104 AioContext
*aio_context
;
2105 BdrvDirtyBitmap
*bitmap
;
2106 BlockDriverState
*bs
;
2108 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2109 if (!bitmap
|| !bs
) {
2113 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2115 "Bitmap '%s' is currently frozen and cannot be modified",
2118 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2120 "Bitmap '%s' is currently disabled and cannot be cleared",
2125 bdrv_clear_dirty_bitmap(bitmap
);
2128 aio_context_release(aio_context
);
2131 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2133 const char *id
= qdict_get_str(qdict
, "id");
2135 BlockDriverState
*bs
;
2136 AioContext
*aio_context
;
2137 Error
*local_err
= NULL
;
2139 blk
= blk_by_name(id
);
2141 error_report("Device '%s' not found", id
);
2146 if (!blk_legacy_dinfo(blk
)) {
2147 error_report("Deleting device added with blockdev-add"
2148 " is not supported");
2152 aio_context
= bdrv_get_aio_context(bs
);
2153 aio_context_acquire(aio_context
);
2155 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2156 error_report_err(local_err
);
2157 aio_context_release(aio_context
);
2163 /* if we have a device attached to this BlockDriverState
2164 * then we need to make the drive anonymous until the device
2165 * can be removed. If this is a drive with no device backing
2166 * then we can just get rid of the block driver state right here.
2168 if (blk_get_attached_dev(blk
)) {
2169 blk_hide_on_behalf_of_hmp_drive_del(blk
);
2170 /* Further I/O must not pause the guest */
2171 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2172 BLOCKDEV_ON_ERROR_REPORT
);
2177 aio_context_release(aio_context
);
2180 void qmp_block_resize(bool has_device
, const char *device
,
2181 bool has_node_name
, const char *node_name
,
2182 int64_t size
, Error
**errp
)
2184 Error
*local_err
= NULL
;
2185 BlockDriverState
*bs
;
2186 AioContext
*aio_context
;
2189 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2190 has_node_name
? node_name
: NULL
,
2193 error_propagate(errp
, local_err
);
2197 aio_context
= bdrv_get_aio_context(bs
);
2198 aio_context_acquire(aio_context
);
2200 if (!bdrv_is_first_non_filter(bs
)) {
2201 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2206 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2210 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2211 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2215 /* complete all in-flight operations before resizing the device */
2218 ret
= bdrv_truncate(bs
, size
);
2223 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2226 error_setg(errp
, QERR_UNSUPPORTED
);
2229 error_setg(errp
, "Device '%s' is read only", device
);
2232 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2235 error_setg_errno(errp
, -ret
, "Could not resize");
2240 aio_context_release(aio_context
);
2243 static void block_job_cb(void *opaque
, int ret
)
2245 /* Note that this function may be executed from another AioContext besides
2246 * the QEMU main loop. If you need to access anything that assumes the
2247 * QEMU global mutex, use a BH or introduce a mutex.
2250 BlockDriverState
*bs
= opaque
;
2251 const char *msg
= NULL
;
2253 trace_block_job_cb(bs
, bs
->job
, ret
);
2258 msg
= strerror(-ret
);
2261 if (block_job_is_cancelled(bs
->job
)) {
2262 block_job_event_cancelled(bs
->job
);
2264 block_job_event_completed(bs
->job
, msg
);
2267 bdrv_put_ref_bh_schedule(bs
);
2270 void qmp_block_stream(const char *device
,
2271 bool has_base
, const char *base
,
2272 bool has_backing_file
, const char *backing_file
,
2273 bool has_speed
, int64_t speed
,
2274 bool has_on_error
, BlockdevOnError on_error
,
2278 BlockDriverState
*bs
;
2279 BlockDriverState
*base_bs
= NULL
;
2280 AioContext
*aio_context
;
2281 Error
*local_err
= NULL
;
2282 const char *base_name
= NULL
;
2284 if (!has_on_error
) {
2285 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2288 blk
= blk_by_name(device
);
2290 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2291 "Device '%s' not found", device
);
2296 aio_context
= bdrv_get_aio_context(bs
);
2297 aio_context_acquire(aio_context
);
2299 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2304 base_bs
= bdrv_find_backing_image(bs
, base
);
2305 if (base_bs
== NULL
) {
2306 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2309 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2313 /* if we are streaming the entire chain, the result will have no backing
2314 * file, and specifying one is therefore an error */
2315 if (base_bs
== NULL
&& has_backing_file
) {
2316 error_setg(errp
, "backing file specified, but streaming the "
2321 /* backing_file string overrides base bs filename */
2322 base_name
= has_backing_file
? backing_file
: base_name
;
2324 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
2325 on_error
, block_job_cb
, bs
, &local_err
);
2327 error_propagate(errp
, local_err
);
2331 trace_qmp_block_stream(bs
, bs
->job
);
2334 aio_context_release(aio_context
);
2337 void qmp_block_commit(const char *device
,
2338 bool has_base
, const char *base
,
2339 bool has_top
, const char *top
,
2340 bool has_backing_file
, const char *backing_file
,
2341 bool has_speed
, int64_t speed
,
2345 BlockDriverState
*bs
;
2346 BlockDriverState
*base_bs
, *top_bs
;
2347 AioContext
*aio_context
;
2348 Error
*local_err
= NULL
;
2349 /* This will be part of the QMP command, if/when the
2350 * BlockdevOnError change for blkmirror makes it in
2352 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2359 * libvirt relies on the DeviceNotFound error class in order to probe for
2360 * live commit feature versions; for this to work, we must make sure to
2361 * perform the device lookup before any generic errors that may occur in a
2362 * scenario in which all optional arguments are omitted. */
2363 blk
= blk_by_name(device
);
2365 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2366 "Device '%s' not found", device
);
2371 aio_context
= bdrv_get_aio_context(bs
);
2372 aio_context_acquire(aio_context
);
2374 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2378 /* default top_bs is the active layer */
2381 if (has_top
&& top
) {
2382 if (strcmp(bs
->filename
, top
) != 0) {
2383 top_bs
= bdrv_find_backing_image(bs
, top
);
2387 if (top_bs
== NULL
) {
2388 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2392 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
2394 if (has_base
&& base
) {
2395 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2397 base_bs
= bdrv_find_base(top_bs
);
2400 if (base_bs
== NULL
) {
2401 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2405 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2407 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2411 /* Do not allow attempts to commit an image into itself */
2412 if (top_bs
== base_bs
) {
2413 error_setg(errp
, "cannot commit an image into itself");
2418 if (has_backing_file
) {
2419 error_setg(errp
, "'backing-file' specified,"
2420 " but 'top' is the active layer");
2423 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
2426 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
2427 has_backing_file
? backing_file
: NULL
, &local_err
);
2429 if (local_err
!= NULL
) {
2430 error_propagate(errp
, local_err
);
2435 aio_context_release(aio_context
);
2438 void qmp_drive_backup(const char *device
, const char *target
,
2439 bool has_format
, const char *format
,
2440 enum MirrorSyncMode sync
,
2441 bool has_mode
, enum NewImageMode mode
,
2442 bool has_speed
, int64_t speed
,
2443 bool has_bitmap
, const char *bitmap
,
2444 bool has_on_source_error
, BlockdevOnError on_source_error
,
2445 bool has_on_target_error
, BlockdevOnError on_target_error
,
2449 BlockDriverState
*bs
;
2450 BlockDriverState
*target_bs
;
2451 BlockDriverState
*source
= NULL
;
2452 BdrvDirtyBitmap
*bmap
= NULL
;
2453 AioContext
*aio_context
;
2454 QDict
*options
= NULL
;
2455 Error
*local_err
= NULL
;
2463 if (!has_on_source_error
) {
2464 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2466 if (!has_on_target_error
) {
2467 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2470 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2473 blk
= blk_by_name(device
);
2475 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2476 "Device '%s' not found", device
);
2481 aio_context
= bdrv_get_aio_context(bs
);
2482 aio_context_acquire(aio_context
);
2484 /* Although backup_run has this check too, we need to use bs->drv below, so
2485 * do an early check redundantly. */
2486 if (!bdrv_is_inserted(bs
)) {
2487 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2492 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2495 /* Early check to avoid creating target */
2496 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
2500 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2502 /* See if we have a backing HD we can use to create our new image
2504 if (sync
== MIRROR_SYNC_MODE_TOP
) {
2505 source
= backing_bs(bs
);
2507 sync
= MIRROR_SYNC_MODE_FULL
;
2510 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2514 size
= bdrv_getlength(bs
);
2516 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2520 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
2523 bdrv_img_create(target
, format
, source
->filename
,
2524 source
->drv
->format_name
, NULL
,
2525 size
, flags
, &local_err
, false);
2527 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
2528 size
, flags
, &local_err
, false);
2533 error_propagate(errp
, local_err
);
2538 options
= qdict_new();
2539 qdict_put(options
, "driver", qstring_from_str(format
));
2543 ret
= bdrv_open(&target_bs
, target
, NULL
, options
, flags
, &local_err
);
2545 error_propagate(errp
, local_err
);
2549 bdrv_set_aio_context(target_bs
, aio_context
);
2552 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
2554 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
2559 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
2560 on_source_error
, on_target_error
,
2561 block_job_cb
, bs
, &local_err
);
2562 if (local_err
!= NULL
) {
2563 bdrv_unref(target_bs
);
2564 error_propagate(errp
, local_err
);
2569 aio_context_release(aio_context
);
2572 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
2574 return bdrv_named_nodes_list(errp
);
2577 void qmp_blockdev_backup(const char *device
, const char *target
,
2578 enum MirrorSyncMode sync
,
2579 bool has_speed
, int64_t speed
,
2580 bool has_on_source_error
,
2581 BlockdevOnError on_source_error
,
2582 bool has_on_target_error
,
2583 BlockdevOnError on_target_error
,
2587 BlockDriverState
*bs
;
2588 BlockDriverState
*target_bs
;
2589 Error
*local_err
= NULL
;
2590 AioContext
*aio_context
;
2595 if (!has_on_source_error
) {
2596 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2598 if (!has_on_target_error
) {
2599 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2602 blk
= blk_by_name(device
);
2604 error_setg(errp
, "Device '%s' not found", device
);
2609 aio_context
= bdrv_get_aio_context(bs
);
2610 aio_context_acquire(aio_context
);
2612 blk
= blk_by_name(target
);
2614 error_setg(errp
, "Device '%s' not found", target
);
2617 target_bs
= blk_bs(blk
);
2619 bdrv_ref(target_bs
);
2620 bdrv_set_aio_context(target_bs
, aio_context
);
2621 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
2622 on_target_error
, block_job_cb
, bs
, &local_err
);
2623 if (local_err
!= NULL
) {
2624 bdrv_unref(target_bs
);
2625 error_propagate(errp
, local_err
);
2628 aio_context_release(aio_context
);
2631 void qmp_drive_mirror(const char *device
, const char *target
,
2632 bool has_format
, const char *format
,
2633 bool has_node_name
, const char *node_name
,
2634 bool has_replaces
, const char *replaces
,
2635 enum MirrorSyncMode sync
,
2636 bool has_mode
, enum NewImageMode mode
,
2637 bool has_speed
, int64_t speed
,
2638 bool has_granularity
, uint32_t granularity
,
2639 bool has_buf_size
, int64_t buf_size
,
2640 bool has_on_source_error
, BlockdevOnError on_source_error
,
2641 bool has_on_target_error
, BlockdevOnError on_target_error
,
2642 bool has_unmap
, bool unmap
,
2646 BlockDriverState
*bs
;
2647 BlockDriverState
*source
, *target_bs
;
2648 AioContext
*aio_context
;
2649 Error
*local_err
= NULL
;
2658 if (!has_on_source_error
) {
2659 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2661 if (!has_on_target_error
) {
2662 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2665 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2667 if (!has_granularity
) {
2670 if (!has_buf_size
) {
2677 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
2678 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2679 "a value in range [512B, 64MB]");
2682 if (granularity
& (granularity
- 1)) {
2683 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2688 blk
= blk_by_name(device
);
2690 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2691 "Device '%s' not found", device
);
2696 aio_context
= bdrv_get_aio_context(bs
);
2697 aio_context_acquire(aio_context
);
2699 if (!bdrv_is_inserted(bs
)) {
2700 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2705 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2708 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR
, errp
)) {
2712 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2713 source
= backing_bs(bs
);
2714 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
2715 sync
= MIRROR_SYNC_MODE_FULL
;
2717 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2721 size
= bdrv_getlength(bs
);
2723 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2728 BlockDriverState
*to_replace_bs
;
2729 AioContext
*replace_aio_context
;
2730 int64_t replace_size
;
2732 if (!has_node_name
) {
2733 error_setg(errp
, "a node-name must be provided when replacing a"
2734 " named node of the graph");
2738 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
2740 if (!to_replace_bs
) {
2741 error_propagate(errp
, local_err
);
2745 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
2746 aio_context_acquire(replace_aio_context
);
2747 replace_size
= bdrv_getlength(to_replace_bs
);
2748 aio_context_release(replace_aio_context
);
2750 if (size
!= replace_size
) {
2751 error_setg(errp
, "cannot replace image with a mirror image of "
2757 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
2758 && mode
!= NEW_IMAGE_MODE_EXISTING
)
2760 /* create new image w/o backing file */
2762 bdrv_img_create(target
, format
,
2763 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
2766 case NEW_IMAGE_MODE_EXISTING
:
2768 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
2769 /* create new image with backing file */
2770 bdrv_img_create(target
, format
,
2772 source
->drv
->format_name
,
2773 NULL
, size
, flags
, &local_err
, false);
2781 error_propagate(errp
, local_err
);
2785 options
= qdict_new();
2786 if (has_node_name
) {
2787 qdict_put(options
, "node-name", qstring_from_str(node_name
));
2790 qdict_put(options
, "driver", qstring_from_str(format
));
2793 /* Mirroring takes care of copy-on-write using the source's backing
2797 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
2798 flags
| BDRV_O_NO_BACKING
, &local_err
);
2800 error_propagate(errp
, local_err
);
2804 bdrv_set_aio_context(target_bs
, aio_context
);
2806 /* pass the node name to replace to mirror start since it's loose coupling
2807 * and will allow to check whether the node still exist at mirror completion
2809 mirror_start(bs
, target_bs
,
2810 has_replaces
? replaces
: NULL
,
2811 speed
, granularity
, buf_size
, sync
,
2812 on_source_error
, on_target_error
,
2814 block_job_cb
, bs
, &local_err
);
2815 if (local_err
!= NULL
) {
2816 bdrv_unref(target_bs
);
2817 error_propagate(errp
, local_err
);
2822 aio_context_release(aio_context
);
2825 /* Get the block job for a given device name and acquire its AioContext */
2826 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
2830 BlockDriverState
*bs
;
2832 blk
= blk_by_name(device
);
2838 *aio_context
= bdrv_get_aio_context(bs
);
2839 aio_context_acquire(*aio_context
);
2842 aio_context_release(*aio_context
);
2849 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
2850 "No active block job on device '%s'", device
);
2851 *aio_context
= NULL
;
2855 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
2857 AioContext
*aio_context
;
2858 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2864 block_job_set_speed(job
, speed
, errp
);
2865 aio_context_release(aio_context
);
2868 void qmp_block_job_cancel(const char *device
,
2869 bool has_force
, bool force
, Error
**errp
)
2871 AioContext
*aio_context
;
2872 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2882 if (job
->user_paused
&& !force
) {
2883 error_setg(errp
, "The block job for device '%s' is currently paused",
2888 trace_qmp_block_job_cancel(job
);
2889 block_job_cancel(job
);
2891 aio_context_release(aio_context
);
2894 void qmp_block_job_pause(const char *device
, Error
**errp
)
2896 AioContext
*aio_context
;
2897 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2899 if (!job
|| job
->user_paused
) {
2903 job
->user_paused
= true;
2904 trace_qmp_block_job_pause(job
);
2905 block_job_pause(job
);
2906 aio_context_release(aio_context
);
2909 void qmp_block_job_resume(const char *device
, Error
**errp
)
2911 AioContext
*aio_context
;
2912 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2914 if (!job
|| !job
->user_paused
) {
2918 job
->user_paused
= false;
2919 trace_qmp_block_job_resume(job
);
2920 block_job_resume(job
);
2921 aio_context_release(aio_context
);
2924 void qmp_block_job_complete(const char *device
, Error
**errp
)
2926 AioContext
*aio_context
;
2927 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2933 trace_qmp_block_job_complete(job
);
2934 block_job_complete(job
, errp
);
2935 aio_context_release(aio_context
);
2938 void qmp_change_backing_file(const char *device
,
2939 const char *image_node_name
,
2940 const char *backing_file
,
2944 BlockDriverState
*bs
= NULL
;
2945 AioContext
*aio_context
;
2946 BlockDriverState
*image_bs
= NULL
;
2947 Error
*local_err
= NULL
;
2952 blk
= blk_by_name(device
);
2954 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2955 "Device '%s' not found", device
);
2960 aio_context
= bdrv_get_aio_context(bs
);
2961 aio_context_acquire(aio_context
);
2963 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
2965 error_propagate(errp
, local_err
);
2970 error_setg(errp
, "image file not found");
2974 if (bdrv_find_base(image_bs
) == image_bs
) {
2975 error_setg(errp
, "not allowing backing file change on an image "
2976 "without a backing file");
2980 /* even though we are not necessarily operating on bs, we need it to
2981 * determine if block ops are currently prohibited on the chain */
2982 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
2986 /* final sanity check */
2987 if (!bdrv_chain_contains(bs
, image_bs
)) {
2988 error_setg(errp
, "'%s' and image file are not in the same chain",
2993 /* if not r/w, reopen to make r/w */
2994 open_flags
= image_bs
->open_flags
;
2995 ro
= bdrv_is_read_only(image_bs
);
2998 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3000 error_propagate(errp
, local_err
);
3005 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3006 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3009 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3011 /* don't exit here, so we can try to restore open flags if
3016 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3018 error_propagate(errp
, local_err
); /* will preserve prior errp */
3023 aio_context_release(aio_context
);
3026 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3028 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3029 BlockDriverState
*bs
;
3030 BlockBackend
*blk
= NULL
;
3033 Error
*local_err
= NULL
;
3035 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3036 * cache.direct=false instead of silently switching to aio=threads, except
3037 * when called from drive_new().
3039 * For now, simply forbidding the combination for all drivers will do. */
3040 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3041 bool direct
= options
->has_cache
&&
3042 options
->cache
->has_direct
&&
3043 options
->cache
->direct
;
3045 error_setg(errp
, "aio=native requires cache.direct=true");
3050 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
3051 &options
, NULL
, &local_err
);
3053 error_propagate(errp
, local_err
);
3057 obj
= qmp_output_get_qobject(ov
);
3058 qdict
= qobject_to_qdict(obj
);
3060 qdict_flatten(qdict
);
3062 if (options
->has_id
) {
3063 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3065 error_propagate(errp
, local_err
);
3073 if (!qdict_get_try_str(qdict
, "node-name")) {
3074 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3080 ret
= bdrv_open(&bs
, NULL
, NULL
, qdict
, BDRV_O_RDWR
| BDRV_O_CACHE_WB
,
3087 if (bs
&& bdrv_key_required(bs
)) {
3093 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3098 qmp_output_visitor_cleanup(ov
);
3101 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3103 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3104 BlockDriverState
*bs
;
3106 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3107 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
3109 aio_context_acquire(aio_context
);
3112 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
3113 elem
->value
= block_job_query(bs
->job
);
3115 p_next
= &elem
->next
;
3118 aio_context_release(aio_context
);
3124 QemuOptsList qemu_common_drive_opts
= {
3126 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3130 .type
= QEMU_OPT_BOOL
,
3131 .help
= "enable/disable snapshot mode",
3134 .type
= QEMU_OPT_STRING
,
3135 .help
= "discard operation (ignore/off, unmap/on)",
3137 .name
= BDRV_OPT_CACHE_WB
,
3138 .type
= QEMU_OPT_BOOL
,
3139 .help
= "enables writeback mode for any caches",
3141 .name
= BDRV_OPT_CACHE_DIRECT
,
3142 .type
= QEMU_OPT_BOOL
,
3143 .help
= "enables use of O_DIRECT (bypass the host page cache)",
3145 .name
= BDRV_OPT_CACHE_NO_FLUSH
,
3146 .type
= QEMU_OPT_BOOL
,
3147 .help
= "ignore any flush requests for the device",
3150 .type
= QEMU_OPT_STRING
,
3151 .help
= "host AIO implementation (threads, native)",
3154 .type
= QEMU_OPT_STRING
,
3155 .help
= "disk format (raw, qcow2, ...)",
3158 .type
= QEMU_OPT_STRING
,
3159 .help
= "read error action",
3162 .type
= QEMU_OPT_STRING
,
3163 .help
= "write error action",
3165 .name
= "read-only",
3166 .type
= QEMU_OPT_BOOL
,
3167 .help
= "open drive file as read-only",
3169 .name
= "throttling.iops-total",
3170 .type
= QEMU_OPT_NUMBER
,
3171 .help
= "limit total I/O operations per second",
3173 .name
= "throttling.iops-read",
3174 .type
= QEMU_OPT_NUMBER
,
3175 .help
= "limit read operations per second",
3177 .name
= "throttling.iops-write",
3178 .type
= QEMU_OPT_NUMBER
,
3179 .help
= "limit write operations per second",
3181 .name
= "throttling.bps-total",
3182 .type
= QEMU_OPT_NUMBER
,
3183 .help
= "limit total bytes per second",
3185 .name
= "throttling.bps-read",
3186 .type
= QEMU_OPT_NUMBER
,
3187 .help
= "limit read bytes per second",
3189 .name
= "throttling.bps-write",
3190 .type
= QEMU_OPT_NUMBER
,
3191 .help
= "limit write bytes per second",
3193 .name
= "throttling.iops-total-max",
3194 .type
= QEMU_OPT_NUMBER
,
3195 .help
= "I/O operations burst",
3197 .name
= "throttling.iops-read-max",
3198 .type
= QEMU_OPT_NUMBER
,
3199 .help
= "I/O operations read burst",
3201 .name
= "throttling.iops-write-max",
3202 .type
= QEMU_OPT_NUMBER
,
3203 .help
= "I/O operations write burst",
3205 .name
= "throttling.bps-total-max",
3206 .type
= QEMU_OPT_NUMBER
,
3207 .help
= "total bytes burst",
3209 .name
= "throttling.bps-read-max",
3210 .type
= QEMU_OPT_NUMBER
,
3211 .help
= "total bytes read burst",
3213 .name
= "throttling.bps-write-max",
3214 .type
= QEMU_OPT_NUMBER
,
3215 .help
= "total bytes write burst",
3217 .name
= "throttling.iops-size",
3218 .type
= QEMU_OPT_NUMBER
,
3219 .help
= "when limiting by iops max size of an I/O in bytes",
3221 .name
= "throttling.group",
3222 .type
= QEMU_OPT_STRING
,
3223 .help
= "name of the block throttling group",
3225 .name
= "copy-on-read",
3226 .type
= QEMU_OPT_BOOL
,
3227 .help
= "copy read data from backing file into image file",
3229 .name
= "detect-zeroes",
3230 .type
= QEMU_OPT_STRING
,
3231 .help
= "try to optimize zero writes (off, on, unmap)",
3233 { /* end of list */ }
3237 QemuOptsList qemu_drive_opts
= {
3239 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
3242 * no elements => accept any params
3243 * validation will happen later
3245 { /* end of list */ }