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/blockdev.h"
34 #include "hw/block/block.h"
35 #include "block/blockjob.h"
36 #include "monitor/monitor.h"
37 #include "qemu/option.h"
38 #include "qemu/config-file.h"
39 #include "qapi/qmp/types.h"
40 #include "qapi-visit.h"
41 #include "qapi/qmp-output-visitor.h"
42 #include "qapi/util.h"
43 #include "sysemu/sysemu.h"
44 #include "block/block_int.h"
45 #include "qmp-commands.h"
47 #include "sysemu/arch_init.h"
49 static QTAILQ_HEAD(drivelist
, DriveInfo
) drives
= QTAILQ_HEAD_INITIALIZER(drives
);
51 static const char *const if_name
[IF_COUNT
] = {
55 [IF_FLOPPY
] = "floppy",
56 [IF_PFLASH
] = "pflash",
59 [IF_VIRTIO
] = "virtio",
63 static const 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 * We automatically delete the drive when a device using it gets
84 * unplugged. Questionable feature, but we can't just drop it.
85 * Device models call blockdev_mark_auto_del() to schedule the
86 * automatic deletion, and generic qdev code calls blockdev_auto_del()
87 * when deletion is actually safe.
89 void blockdev_mark_auto_del(BlockDriverState
*bs
)
91 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
93 if (dinfo
&& !dinfo
->enable_auto_del
) {
98 block_job_cancel(bs
->job
);
105 void blockdev_auto_del(BlockDriverState
*bs
)
107 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
109 if (dinfo
&& dinfo
->auto_del
) {
114 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
116 int max_devs
= if_max_devs
[type
];
117 return max_devs
? index
/ max_devs
: 0;
120 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
122 int max_devs
= if_max_devs
[type
];
123 return max_devs
? index
% max_devs
: index
;
126 QemuOpts
*drive_def(const char *optstr
)
128 return qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
131 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
137 opts
= drive_def(optstr
);
141 if (type
!= IF_DEFAULT
) {
142 qemu_opt_set(opts
, "if", if_name
[type
]);
145 snprintf(buf
, sizeof(buf
), "%d", index
);
146 qemu_opt_set(opts
, "index", buf
);
149 qemu_opt_set(opts
, "file", file
);
153 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
157 /* seek interface, bus and unit */
159 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
160 if (dinfo
->type
== type
&&
169 bool drive_check_orphaned(void)
174 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
175 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
176 /* Unless this is a default drive, this may be an oversight. */
177 if (!dinfo
->bdrv
->dev
&& !dinfo
->is_default
&&
178 dinfo
->type
!= IF_NONE
) {
179 fprintf(stderr
, "Warning: Orphaned drive without device: "
180 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
181 dinfo
->id
, dinfo
->bdrv
->filename
, if_name
[dinfo
->type
],
182 dinfo
->bus
, dinfo
->unit
);
190 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
192 return drive_get(type
,
193 drive_index_to_bus_id(type
, index
),
194 drive_index_to_unit_id(type
, index
));
197 int drive_get_max_bus(BlockInterfaceType type
)
203 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
204 if(dinfo
->type
== type
&&
205 dinfo
->bus
> max_bus
)
206 max_bus
= dinfo
->bus
;
211 /* Get a block device. This should only be used for single-drive devices
212 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
214 DriveInfo
*drive_get_next(BlockInterfaceType type
)
216 static int next_block_unit
[IF_COUNT
];
218 return drive_get(type
, 0, next_block_unit
[type
]++);
221 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
225 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
226 if (dinfo
->bdrv
== bs
) {
233 static void bdrv_format_print(void *opaque
, const char *name
)
235 error_printf(" %s", name
);
238 void drive_del(DriveInfo
*dinfo
)
240 bdrv_unref(dinfo
->bdrv
);
243 void drive_info_del(DriveInfo
*dinfo
)
248 qemu_opts_del(dinfo
->opts
);
250 QTAILQ_REMOVE(&drives
, dinfo
, next
);
251 g_free(dinfo
->serial
);
257 BlockDriverState
*bs
;
260 static void bdrv_put_ref_bh(void *opaque
)
262 BDRVPutRefBH
*s
= opaque
;
265 qemu_bh_delete(s
->bh
);
270 * Release a BDS reference in a BH
272 * It is not safe to use bdrv_unref() from a callback function when the callers
273 * still need the BlockDriverState. In such cases we schedule a BH to release
276 static void bdrv_put_ref_bh_schedule(BlockDriverState
*bs
)
280 s
= g_new(BDRVPutRefBH
, 1);
281 s
->bh
= qemu_bh_new(bdrv_put_ref_bh
, s
);
283 qemu_bh_schedule(s
->bh
);
286 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
288 if (!strcmp(buf
, "ignore")) {
289 return BLOCKDEV_ON_ERROR_IGNORE
;
290 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
291 return BLOCKDEV_ON_ERROR_ENOSPC
;
292 } else if (!strcmp(buf
, "stop")) {
293 return BLOCKDEV_ON_ERROR_STOP
;
294 } else if (!strcmp(buf
, "report")) {
295 return BLOCKDEV_ON_ERROR_REPORT
;
297 error_setg(errp
, "'%s' invalid %s error action",
298 buf
, is_read
? "read" : "write");
303 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
305 if (throttle_conflicting(cfg
)) {
306 error_setg(errp
, "bps/iops/max total values and read/write values"
307 " cannot be used at the same time");
311 if (!throttle_is_valid(cfg
)) {
312 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
319 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
321 /* Takes the ownership of bs_opts */
322 static DriveInfo
*blockdev_init(const char *file
, QDict
*bs_opts
,
328 int on_read_error
, on_write_error
;
329 BlockDriverState
*bs
;
338 bool has_driver_specific_opts
;
339 BlockdevDetectZeroesOptions detect_zeroes
;
340 BlockDriver
*drv
= NULL
;
342 /* Check common options by copying from bs_opts to opts, all other options
343 * stay in bs_opts for processing by bdrv_open(). */
344 id
= qdict_get_try_str(bs_opts
, "id");
345 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
347 error_propagate(errp
, error
);
351 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
353 error_propagate(errp
, error
);
358 qdict_del(bs_opts
, "id");
361 has_driver_specific_opts
= !!qdict_size(bs_opts
);
363 /* extract parameters */
364 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
365 ro
= qemu_opt_get_bool(opts
, "read-only", 0);
366 copy_on_read
= qemu_opt_get_bool(opts
, "copy-on-read", false);
368 if ((buf
= qemu_opt_get(opts
, "discard")) != NULL
) {
369 if (bdrv_parse_discard_flags(buf
, &bdrv_flags
) != 0) {
370 error_setg(errp
, "invalid discard option");
375 if (qemu_opt_get_bool(opts
, "cache.writeback", true)) {
376 bdrv_flags
|= BDRV_O_CACHE_WB
;
378 if (qemu_opt_get_bool(opts
, "cache.direct", false)) {
379 bdrv_flags
|= BDRV_O_NOCACHE
;
381 if (qemu_opt_get_bool(opts
, "cache.no-flush", false)) {
382 bdrv_flags
|= BDRV_O_NO_FLUSH
;
385 #ifdef CONFIG_LINUX_AIO
386 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
387 if (!strcmp(buf
, "native")) {
388 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
389 } else if (!strcmp(buf
, "threads")) {
390 /* this is the default */
392 error_setg(errp
, "invalid aio option");
398 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
399 if (is_help_option(buf
)) {
400 error_printf("Supported formats:");
401 bdrv_iterate_format(bdrv_format_print
, NULL
);
406 drv
= bdrv_find_format(buf
);
408 error_setg(errp
, "'%s' invalid format", buf
);
413 /* disk I/O throttling */
414 memset(&cfg
, 0, sizeof(cfg
));
415 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
=
416 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
417 cfg
.buckets
[THROTTLE_BPS_READ
].avg
=
418 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
419 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
=
420 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
421 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
=
422 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
423 cfg
.buckets
[THROTTLE_OPS_READ
].avg
=
424 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
425 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
=
426 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
428 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
=
429 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
430 cfg
.buckets
[THROTTLE_BPS_READ
].max
=
431 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
432 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
=
433 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
434 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
=
435 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
436 cfg
.buckets
[THROTTLE_OPS_READ
].max
=
437 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
438 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
=
439 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
441 cfg
.op_size
= qemu_opt_get_number(opts
, "throttling.iops-size", 0);
443 if (!check_throttle_config(&cfg
, &error
)) {
444 error_propagate(errp
, error
);
448 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
449 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
450 on_write_error
= parse_block_error_action(buf
, 0, &error
);
452 error_propagate(errp
, error
);
457 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
458 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
459 on_read_error
= parse_block_error_action(buf
, 1, &error
);
461 error_propagate(errp
, error
);
467 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
468 qemu_opt_get(opts
, "detect-zeroes"),
469 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX
,
470 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
473 error_propagate(errp
, error
);
477 if (detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
478 !(bdrv_flags
& BDRV_O_UNMAP
)) {
479 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
480 "without setting discard operation to unmap");
485 bs
= bdrv_new(qemu_opts_id(opts
), errp
);
489 bs
->open_flags
= snapshot
? BDRV_O_SNAPSHOT
: 0;
491 bs
->detect_zeroes
= detect_zeroes
;
493 bdrv_set_on_error(bs
, on_read_error
, on_write_error
);
495 /* disk I/O throttling */
496 if (throttle_enabled(&cfg
)) {
497 bdrv_io_limits_enable(bs
);
498 bdrv_set_io_limits(bs
, &cfg
);
501 dinfo
= g_malloc0(sizeof(*dinfo
));
502 dinfo
->id
= g_strdup(qemu_opts_id(opts
));
504 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
506 if (!file
|| !*file
) {
507 if (has_driver_specific_opts
) {
516 /* always use cache=unsafe with snapshot */
517 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
518 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
522 bdrv_flags
|= BDRV_O_COPY_ON_READ
;
525 if (runstate_check(RUN_STATE_INMIGRATE
)) {
526 bdrv_flags
|= BDRV_O_INCOMING
;
529 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
532 ret
= bdrv_open(&bs
, file
, NULL
, bs_opts
, bdrv_flags
, drv
, &error
);
533 assert(bs
== dinfo
->bdrv
);
536 error_setg(errp
, "could not open disk image %s: %s",
537 file
?: dinfo
->id
, error_get_pretty(error
));
542 if (bdrv_key_required(bs
)) {
560 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
565 value
= qemu_opt_get(opts
, from
);
567 if (qemu_opt_find(opts
, to
)) {
568 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
569 "same time", to
, from
);
574 /* rename all items in opts */
575 while ((value
= qemu_opt_get(opts
, from
))) {
576 qemu_opt_set(opts
, to
, value
);
577 qemu_opt_unset(opts
, from
);
581 QemuOptsList qemu_legacy_drive_opts
= {
583 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
587 .type
= QEMU_OPT_NUMBER
,
588 .help
= "bus number",
591 .type
= QEMU_OPT_NUMBER
,
592 .help
= "unit number (i.e. lun for scsi)",
595 .type
= QEMU_OPT_NUMBER
,
596 .help
= "index number",
599 .type
= QEMU_OPT_STRING
,
600 .help
= "media type (disk, cdrom)",
603 .type
= QEMU_OPT_STRING
,
604 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
607 .type
= QEMU_OPT_NUMBER
,
608 .help
= "number of cylinders (ide disk geometry)",
611 .type
= QEMU_OPT_NUMBER
,
612 .help
= "number of heads (ide disk geometry)",
615 .type
= QEMU_OPT_NUMBER
,
616 .help
= "number of sectors (ide disk geometry)",
619 .type
= QEMU_OPT_STRING
,
620 .help
= "chs translation (auto, lba, none)",
623 .type
= QEMU_OPT_BOOL
,
624 .help
= "(deprecated, ignored)",
627 .type
= QEMU_OPT_STRING
,
628 .help
= "pci address (virtio only)",
631 .type
= QEMU_OPT_STRING
,
632 .help
= "disk serial number",
635 .type
= QEMU_OPT_STRING
,
639 /* Options that are passed on, but have special semantics with -drive */
642 .type
= QEMU_OPT_BOOL
,
643 .help
= "open drive file as read-only",
646 .type
= QEMU_OPT_STRING
,
647 .help
= "read error action",
650 .type
= QEMU_OPT_STRING
,
651 .help
= "write error action",
653 .name
= "copy-on-read",
654 .type
= QEMU_OPT_BOOL
,
655 .help
= "copy read data from backing file into image file",
658 { /* end of list */ }
662 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
665 DriveInfo
*dinfo
= NULL
;
667 QemuOpts
*legacy_opts
;
668 DriveMediaType media
= MEDIA_DISK
;
669 BlockInterfaceType type
;
670 int cyls
, heads
, secs
, translation
;
671 int max_devs
, bus_id
, unit_id
, index
;
673 const char *werror
, *rerror
;
674 bool read_only
= false;
677 const char *filename
;
678 Error
*local_err
= NULL
;
681 /* Change legacy command line options into QMP ones */
682 static const struct {
686 { "iops", "throttling.iops-total" },
687 { "iops_rd", "throttling.iops-read" },
688 { "iops_wr", "throttling.iops-write" },
690 { "bps", "throttling.bps-total" },
691 { "bps_rd", "throttling.bps-read" },
692 { "bps_wr", "throttling.bps-write" },
694 { "iops_max", "throttling.iops-total-max" },
695 { "iops_rd_max", "throttling.iops-read-max" },
696 { "iops_wr_max", "throttling.iops-write-max" },
698 { "bps_max", "throttling.bps-total-max" },
699 { "bps_rd_max", "throttling.bps-read-max" },
700 { "bps_wr_max", "throttling.bps-write-max" },
702 { "iops_size", "throttling.iops-size" },
704 { "readonly", "read-only" },
707 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
708 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
711 error_report("%s", error_get_pretty(local_err
));
712 error_free(local_err
);
717 value
= qemu_opt_get(all_opts
, "cache");
721 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
722 error_report("invalid cache option");
726 /* Specific options take precedence */
727 if (!qemu_opt_get(all_opts
, "cache.writeback")) {
728 qemu_opt_set_bool(all_opts
, "cache.writeback",
729 !!(flags
& BDRV_O_CACHE_WB
));
731 if (!qemu_opt_get(all_opts
, "cache.direct")) {
732 qemu_opt_set_bool(all_opts
, "cache.direct",
733 !!(flags
& BDRV_O_NOCACHE
));
735 if (!qemu_opt_get(all_opts
, "cache.no-flush")) {
736 qemu_opt_set_bool(all_opts
, "cache.no-flush",
737 !!(flags
& BDRV_O_NO_FLUSH
));
739 qemu_opt_unset(all_opts
, "cache");
742 /* Get a QDict for processing the options */
743 bs_opts
= qdict_new();
744 qemu_opts_to_qdict(all_opts
, bs_opts
);
746 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
748 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
750 error_report("%s", error_get_pretty(local_err
));
751 error_free(local_err
);
755 /* Deprecated option boot=[on|off] */
756 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
757 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
758 "ignored. Future versions will reject this parameter. Please "
759 "update your scripts.\n");
763 value
= qemu_opt_get(legacy_opts
, "media");
765 if (!strcmp(value
, "disk")) {
767 } else if (!strcmp(value
, "cdrom")) {
771 error_report("'%s' invalid media", value
);
776 /* copy-on-read is disabled with a warning for read-only devices */
777 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
778 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
780 if (read_only
&& copy_on_read
) {
781 error_report("warning: disabling copy-on-read on read-only drive");
782 copy_on_read
= false;
785 qdict_put(bs_opts
, "read-only",
786 qstring_from_str(read_only
? "on" : "off"));
787 qdict_put(bs_opts
, "copy-on-read",
788 qstring_from_str(copy_on_read
? "on" :"off"));
790 /* Controller type */
791 value
= qemu_opt_get(legacy_opts
, "if");
794 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
797 if (type
== IF_COUNT
) {
798 error_report("unsupported bus type '%s'", value
);
802 type
= block_default_type
;
806 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
807 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
808 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
810 if (cyls
|| heads
|| secs
) {
812 error_report("invalid physical cyls number");
816 error_report("invalid physical heads number");
820 error_report("invalid physical secs number");
825 translation
= BIOS_ATA_TRANSLATION_AUTO
;
826 value
= qemu_opt_get(legacy_opts
, "trans");
829 error_report("'%s' trans must be used with cyls, heads and secs",
833 if (!strcmp(value
, "none")) {
834 translation
= BIOS_ATA_TRANSLATION_NONE
;
835 } else if (!strcmp(value
, "lba")) {
836 translation
= BIOS_ATA_TRANSLATION_LBA
;
837 } else if (!strcmp(value
, "large")) {
838 translation
= BIOS_ATA_TRANSLATION_LARGE
;
839 } else if (!strcmp(value
, "rechs")) {
840 translation
= BIOS_ATA_TRANSLATION_RECHS
;
841 } else if (!strcmp(value
, "auto")) {
842 translation
= BIOS_ATA_TRANSLATION_AUTO
;
844 error_report("'%s' invalid translation type", value
);
849 if (media
== MEDIA_CDROM
) {
850 if (cyls
|| secs
|| heads
) {
851 error_report("CHS can't be set with media=cdrom");
856 /* Device address specified by bus/unit or index.
857 * If none was specified, try to find the first free one. */
858 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
859 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
860 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
862 max_devs
= if_max_devs
[type
];
865 if (bus_id
!= 0 || unit_id
!= -1) {
866 error_report("index cannot be used with bus and unit");
869 bus_id
= drive_index_to_bus_id(type
, index
);
870 unit_id
= drive_index_to_unit_id(type
, index
);
875 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
877 if (max_devs
&& unit_id
>= max_devs
) {
884 if (max_devs
&& unit_id
>= max_devs
) {
885 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
889 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
890 error_report("drive with bus=%d, unit=%d (index=%d) exists",
891 bus_id
, unit_id
, index
);
896 serial
= qemu_opt_get(legacy_opts
, "serial");
898 /* no id supplied -> create one */
899 if (qemu_opts_id(all_opts
) == NULL
) {
901 const char *mediastr
= "";
902 if (type
== IF_IDE
|| type
== IF_SCSI
) {
903 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
906 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
909 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
912 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
916 /* Add virtio block device */
917 devaddr
= qemu_opt_get(legacy_opts
, "addr");
918 if (devaddr
&& type
!= IF_VIRTIO
) {
919 error_report("addr is not supported by this bus type");
923 if (type
== IF_VIRTIO
) {
925 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
927 if (arch_type
== QEMU_ARCH_S390X
) {
928 qemu_opt_set(devopts
, "driver", "virtio-blk-s390");
930 qemu_opt_set(devopts
, "driver", "virtio-blk-pci");
932 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"));
934 qemu_opt_set(devopts
, "addr", devaddr
);
938 filename
= qemu_opt_get(legacy_opts
, "file");
940 /* Check werror/rerror compatibility with if=... */
941 werror
= qemu_opt_get(legacy_opts
, "werror");
942 if (werror
!= NULL
) {
943 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
945 error_report("werror is not supported by this bus type");
948 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
951 rerror
= qemu_opt_get(legacy_opts
, "rerror");
952 if (rerror
!= NULL
) {
953 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
955 error_report("rerror is not supported by this bus type");
958 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
961 /* Actual block device init: Functionality shared with blockdev-add */
962 dinfo
= blockdev_init(filename
, bs_opts
, &local_err
);
966 error_report("%s", error_get_pretty(local_err
));
967 error_free(local_err
);
974 /* Set legacy DriveInfo fields */
975 dinfo
->enable_auto_del
= true;
976 dinfo
->opts
= all_opts
;
979 dinfo
->heads
= heads
;
981 dinfo
->trans
= translation
;
985 dinfo
->unit
= unit_id
;
986 dinfo
->devaddr
= devaddr
;
988 dinfo
->serial
= g_strdup(serial
);
995 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1002 qemu_opts_del(legacy_opts
);
1007 void do_commit(Monitor
*mon
, const QDict
*qdict
)
1009 const char *device
= qdict_get_str(qdict
, "device");
1010 BlockDriverState
*bs
;
1013 if (!strcmp(device
, "all")) {
1014 ret
= bdrv_commit_all();
1016 bs
= bdrv_find(device
);
1018 monitor_printf(mon
, "Device '%s' not found\n", device
);
1021 ret
= bdrv_commit(bs
);
1024 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1029 static void blockdev_do_action(int kind
, void *data
, Error
**errp
)
1031 TransactionAction action
;
1032 TransactionActionList list
;
1036 list
.value
= &action
;
1038 qmp_transaction(&list
, errp
);
1041 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1042 bool has_node_name
, const char *node_name
,
1043 const char *snapshot_file
,
1044 bool has_snapshot_node_name
,
1045 const char *snapshot_node_name
,
1046 bool has_format
, const char *format
,
1047 bool has_mode
, NewImageMode mode
, Error
**errp
)
1049 BlockdevSnapshot snapshot
= {
1050 .has_device
= has_device
,
1051 .device
= (char *) device
,
1052 .has_node_name
= has_node_name
,
1053 .node_name
= (char *) node_name
,
1054 .snapshot_file
= (char *) snapshot_file
,
1055 .has_snapshot_node_name
= has_snapshot_node_name
,
1056 .snapshot_node_name
= (char *) snapshot_node_name
,
1057 .has_format
= has_format
,
1058 .format
= (char *) format
,
1059 .has_mode
= has_mode
,
1062 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1066 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1070 BlockdevSnapshotInternal snapshot
= {
1071 .device
= (char *) device
,
1072 .name
= (char *) name
1075 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1079 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1086 BlockDriverState
*bs
= bdrv_find(device
);
1087 QEMUSnapshotInfo sn
;
1088 Error
*local_err
= NULL
;
1089 SnapshotInfo
*info
= NULL
;
1093 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1106 error_setg(errp
, "Name or id must be provided");
1110 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1112 error_propagate(errp
, local_err
);
1117 "Snapshot with id '%s' and name '%s' does not exist on "
1119 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1123 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1125 error_propagate(errp
, local_err
);
1129 info
= g_new0(SnapshotInfo
, 1);
1130 info
->id
= g_strdup(sn
.id_str
);
1131 info
->name
= g_strdup(sn
.name
);
1132 info
->date_nsec
= sn
.date_nsec
;
1133 info
->date_sec
= sn
.date_sec
;
1134 info
->vm_state_size
= sn
.vm_state_size
;
1135 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1136 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1141 /* New and old BlockDriverState structs for group snapshots */
1143 typedef struct BlkTransactionState BlkTransactionState
;
1145 /* Only prepare() may fail. In a single transaction, only one of commit() or
1146 abort() will be called, clean() will always be called if it present. */
1147 typedef struct BdrvActionOps
{
1148 /* Size of state struct, in bytes. */
1149 size_t instance_size
;
1150 /* Prepare the work, must NOT be NULL. */
1151 void (*prepare
)(BlkTransactionState
*common
, Error
**errp
);
1152 /* Commit the changes, can be NULL. */
1153 void (*commit
)(BlkTransactionState
*common
);
1154 /* Abort the changes on fail, can be NULL. */
1155 void (*abort
)(BlkTransactionState
*common
);
1156 /* Clean up resource in the end, can be NULL. */
1157 void (*clean
)(BlkTransactionState
*common
);
1161 * This structure must be arranged as first member in child type, assuming
1162 * that compiler will also arrange it to the same address with parent instance.
1163 * Later it will be used in free().
1165 struct BlkTransactionState
{
1166 TransactionAction
*action
;
1167 const BdrvActionOps
*ops
;
1168 QSIMPLEQ_ENTRY(BlkTransactionState
) entry
;
1171 /* internal snapshot private data */
1172 typedef struct InternalSnapshotState
{
1173 BlkTransactionState common
;
1174 BlockDriverState
*bs
;
1175 QEMUSnapshotInfo sn
;
1176 } InternalSnapshotState
;
1178 static void internal_snapshot_prepare(BlkTransactionState
*common
,
1181 Error
*local_err
= NULL
;
1184 BlockDriverState
*bs
;
1185 QEMUSnapshotInfo old_sn
, *sn
;
1188 BlockdevSnapshotInternal
*internal
;
1189 InternalSnapshotState
*state
;
1192 g_assert(common
->action
->kind
==
1193 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1194 internal
= common
->action
->blockdev_snapshot_internal_sync
;
1195 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1197 /* 1. parse input */
1198 device
= internal
->device
;
1199 name
= internal
->name
;
1201 /* 2. check for validation */
1202 bs
= bdrv_find(device
);
1204 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1208 if (!bdrv_is_inserted(bs
)) {
1209 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1213 if (bdrv_is_read_only(bs
)) {
1214 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1218 if (!bdrv_can_snapshot(bs
)) {
1219 error_set(errp
, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
1220 bs
->drv
->format_name
, device
, "internal snapshot");
1224 if (!strlen(name
)) {
1225 error_setg(errp
, "Name is empty");
1229 /* check whether a snapshot with name exist */
1230 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1233 error_propagate(errp
, local_err
);
1237 "Snapshot with name '%s' already exists on device '%s'",
1242 /* 3. take the snapshot */
1244 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1245 qemu_gettimeofday(&tv
);
1246 sn
->date_sec
= tv
.tv_sec
;
1247 sn
->date_nsec
= tv
.tv_usec
* 1000;
1248 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1250 ret1
= bdrv_snapshot_create(bs
, sn
);
1252 error_setg_errno(errp
, -ret1
,
1253 "Failed to create snapshot '%s' on device '%s'",
1258 /* 4. succeed, mark a snapshot is created */
1262 static void internal_snapshot_abort(BlkTransactionState
*common
)
1264 InternalSnapshotState
*state
=
1265 DO_UPCAST(InternalSnapshotState
, common
, common
);
1266 BlockDriverState
*bs
= state
->bs
;
1267 QEMUSnapshotInfo
*sn
= &state
->sn
;
1268 Error
*local_error
= NULL
;
1274 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1275 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1276 "device '%s' in abort: %s",
1279 bdrv_get_device_name(bs
),
1280 error_get_pretty(local_error
));
1281 error_free(local_error
);
1285 /* external snapshot private data */
1286 typedef struct ExternalSnapshotState
{
1287 BlkTransactionState common
;
1288 BlockDriverState
*old_bs
;
1289 BlockDriverState
*new_bs
;
1290 } ExternalSnapshotState
;
1292 static void external_snapshot_prepare(BlkTransactionState
*common
,
1297 QDict
*options
= NULL
;
1298 Error
*local_err
= NULL
;
1299 bool has_device
= false;
1301 bool has_node_name
= false;
1302 const char *node_name
;
1303 bool has_snapshot_node_name
= false;
1304 const char *snapshot_node_name
;
1305 const char *new_image_file
;
1306 const char *format
= "qcow2";
1307 enum NewImageMode mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1308 ExternalSnapshotState
*state
=
1309 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1310 TransactionAction
*action
= common
->action
;
1312 /* get parameters */
1313 g_assert(action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
);
1315 has_device
= action
->blockdev_snapshot_sync
->has_device
;
1316 device
= action
->blockdev_snapshot_sync
->device
;
1317 has_node_name
= action
->blockdev_snapshot_sync
->has_node_name
;
1318 node_name
= action
->blockdev_snapshot_sync
->node_name
;
1319 has_snapshot_node_name
=
1320 action
->blockdev_snapshot_sync
->has_snapshot_node_name
;
1321 snapshot_node_name
= action
->blockdev_snapshot_sync
->snapshot_node_name
;
1323 new_image_file
= action
->blockdev_snapshot_sync
->snapshot_file
;
1324 if (action
->blockdev_snapshot_sync
->has_format
) {
1325 format
= action
->blockdev_snapshot_sync
->format
;
1327 if (action
->blockdev_snapshot_sync
->has_mode
) {
1328 mode
= action
->blockdev_snapshot_sync
->mode
;
1331 /* start processing */
1332 drv
= bdrv_find_format(format
);
1334 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1338 state
->old_bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1339 has_node_name
? node_name
: NULL
,
1342 error_propagate(errp
, local_err
);
1346 if (has_node_name
&& !has_snapshot_node_name
) {
1347 error_setg(errp
, "New snapshot node name missing");
1351 if (has_snapshot_node_name
&& bdrv_find_node(snapshot_node_name
)) {
1352 error_setg(errp
, "New snapshot node name already existing");
1356 if (!bdrv_is_inserted(state
->old_bs
)) {
1357 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1361 if (bdrv_op_is_blocked(state
->old_bs
,
1362 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1366 if (!bdrv_is_read_only(state
->old_bs
)) {
1367 if (bdrv_flush(state
->old_bs
)) {
1368 error_set(errp
, QERR_IO_ERROR
);
1373 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1374 error_set(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1378 flags
= state
->old_bs
->open_flags
;
1380 /* create new image w/backing file */
1381 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1382 bdrv_img_create(new_image_file
, format
,
1383 state
->old_bs
->filename
,
1384 state
->old_bs
->drv
->format_name
,
1385 NULL
, -1, flags
, &local_err
, false);
1387 error_propagate(errp
, local_err
);
1392 if (has_snapshot_node_name
) {
1393 options
= qdict_new();
1394 qdict_put(options
, "node-name",
1395 qstring_from_str(snapshot_node_name
));
1398 /* TODO Inherit bs->options or only take explicit options with an
1399 * extended QMP command? */
1400 assert(state
->new_bs
== NULL
);
1401 ret
= bdrv_open(&state
->new_bs
, new_image_file
, NULL
, options
,
1402 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
1403 /* We will manually add the backing_hd field to the bs later */
1405 error_propagate(errp
, local_err
);
1409 static void external_snapshot_commit(BlkTransactionState
*common
)
1411 ExternalSnapshotState
*state
=
1412 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1414 /* This removes our old bs and adds the new bs */
1415 bdrv_append(state
->new_bs
, state
->old_bs
);
1416 /* We don't need (or want) to use the transactional
1417 * bdrv_reopen_multiple() across all the entries at once, because we
1418 * don't want to abort all of them if one of them fails the reopen */
1419 bdrv_reopen(state
->new_bs
, state
->new_bs
->open_flags
& ~BDRV_O_RDWR
,
1423 static void external_snapshot_abort(BlkTransactionState
*common
)
1425 ExternalSnapshotState
*state
=
1426 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1427 if (state
->new_bs
) {
1428 bdrv_unref(state
->new_bs
);
1432 typedef struct DriveBackupState
{
1433 BlkTransactionState common
;
1434 BlockDriverState
*bs
;
1438 static void drive_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1440 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1441 DriveBackup
*backup
;
1442 Error
*local_err
= NULL
;
1444 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1445 backup
= common
->action
->drive_backup
;
1447 qmp_drive_backup(backup
->device
, backup
->target
,
1448 backup
->has_format
, backup
->format
,
1450 backup
->has_mode
, backup
->mode
,
1451 backup
->has_speed
, backup
->speed
,
1452 backup
->has_on_source_error
, backup
->on_source_error
,
1453 backup
->has_on_target_error
, backup
->on_target_error
,
1456 error_propagate(errp
, local_err
);
1462 state
->bs
= bdrv_find(backup
->device
);
1463 state
->job
= state
->bs
->job
;
1466 static void drive_backup_abort(BlkTransactionState
*common
)
1468 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1469 BlockDriverState
*bs
= state
->bs
;
1471 /* Only cancel if it's the job we started */
1472 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1473 block_job_cancel_sync(bs
->job
);
1477 static void abort_prepare(BlkTransactionState
*common
, Error
**errp
)
1479 error_setg(errp
, "Transaction aborted using Abort action");
1482 static void abort_commit(BlkTransactionState
*common
)
1484 g_assert_not_reached(); /* this action never succeeds */
1487 static const BdrvActionOps actions
[] = {
1488 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
1489 .instance_size
= sizeof(ExternalSnapshotState
),
1490 .prepare
= external_snapshot_prepare
,
1491 .commit
= external_snapshot_commit
,
1492 .abort
= external_snapshot_abort
,
1494 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
1495 .instance_size
= sizeof(DriveBackupState
),
1496 .prepare
= drive_backup_prepare
,
1497 .abort
= drive_backup_abort
,
1499 [TRANSACTION_ACTION_KIND_ABORT
] = {
1500 .instance_size
= sizeof(BlkTransactionState
),
1501 .prepare
= abort_prepare
,
1502 .commit
= abort_commit
,
1504 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
1505 .instance_size
= sizeof(InternalSnapshotState
),
1506 .prepare
= internal_snapshot_prepare
,
1507 .abort
= internal_snapshot_abort
,
1512 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1513 * then we do not pivot any of the devices in the group, and abandon the
1516 void qmp_transaction(TransactionActionList
*dev_list
, Error
**errp
)
1518 TransactionActionList
*dev_entry
= dev_list
;
1519 BlkTransactionState
*state
, *next
;
1520 Error
*local_err
= NULL
;
1522 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkTransactionState
) snap_bdrv_states
;
1523 QSIMPLEQ_INIT(&snap_bdrv_states
);
1525 /* drain all i/o before any snapshots */
1528 /* We don't do anything in this loop that commits us to the snapshot */
1529 while (NULL
!= dev_entry
) {
1530 TransactionAction
*dev_info
= NULL
;
1531 const BdrvActionOps
*ops
;
1533 dev_info
= dev_entry
->value
;
1534 dev_entry
= dev_entry
->next
;
1536 assert(dev_info
->kind
< ARRAY_SIZE(actions
));
1538 ops
= &actions
[dev_info
->kind
];
1539 assert(ops
->instance_size
> 0);
1541 state
= g_malloc0(ops
->instance_size
);
1543 state
->action
= dev_info
;
1544 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
1546 state
->ops
->prepare(state
, &local_err
);
1548 error_propagate(errp
, local_err
);
1549 goto delete_and_fail
;
1553 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1554 if (state
->ops
->commit
) {
1555 state
->ops
->commit(state
);
1564 * failure, and it is all-or-none; abandon each new bs, and keep using
1565 * the original bs for all images
1567 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1568 if (state
->ops
->abort
) {
1569 state
->ops
->abort(state
);
1573 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
1574 if (state
->ops
->clean
) {
1575 state
->ops
->clean(state
);
1582 static void eject_device(BlockDriverState
*bs
, int force
, Error
**errp
)
1584 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
1587 if (!bdrv_dev_has_removable_media(bs
)) {
1588 error_setg(errp
, "Device '%s' is not removable",
1589 bdrv_get_device_name(bs
));
1593 if (bdrv_dev_is_medium_locked(bs
) && !bdrv_dev_is_tray_open(bs
)) {
1594 bdrv_dev_eject_request(bs
, force
);
1596 error_setg(errp
, "Device '%s' is locked",
1597 bdrv_get_device_name(bs
));
1605 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
1607 BlockDriverState
*bs
;
1609 bs
= bdrv_find(device
);
1611 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1615 eject_device(bs
, force
, errp
);
1618 void qmp_block_passwd(bool has_device
, const char *device
,
1619 bool has_node_name
, const char *node_name
,
1620 const char *password
, Error
**errp
)
1622 Error
*local_err
= NULL
;
1623 BlockDriverState
*bs
;
1626 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1627 has_node_name
? node_name
: NULL
,
1630 error_propagate(errp
, local_err
);
1634 err
= bdrv_set_key(bs
, password
);
1635 if (err
== -EINVAL
) {
1636 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1638 } else if (err
< 0) {
1639 error_set(errp
, QERR_INVALID_PASSWORD
);
1644 static void qmp_bdrv_open_encrypted(BlockDriverState
*bs
, const char *filename
,
1645 int bdrv_flags
, BlockDriver
*drv
,
1646 const char *password
, Error
**errp
)
1648 Error
*local_err
= NULL
;
1651 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, bdrv_flags
, drv
, &local_err
);
1653 error_propagate(errp
, local_err
);
1657 if (bdrv_key_required(bs
)) {
1659 if (bdrv_set_key(bs
, password
) < 0) {
1660 error_set(errp
, QERR_INVALID_PASSWORD
);
1663 error_set(errp
, QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
),
1664 bdrv_get_encrypted_filename(bs
));
1666 } else if (password
) {
1667 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1671 void qmp_change_blockdev(const char *device
, const char *filename
,
1672 const char *format
, Error
**errp
)
1674 BlockDriverState
*bs
;
1675 BlockDriver
*drv
= NULL
;
1679 bs
= bdrv_find(device
);
1681 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1686 drv
= bdrv_find_whitelisted_format(format
, bs
->read_only
);
1688 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1693 eject_device(bs
, 0, &err
);
1695 error_propagate(errp
, err
);
1699 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
1700 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
1702 qmp_bdrv_open_encrypted(bs
, filename
, bdrv_flags
, drv
, NULL
, errp
);
1705 /* throttling disk I/O limits */
1706 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
1713 bool has_bps_rd_max
,
1715 bool has_bps_wr_max
,
1719 bool has_iops_rd_max
,
1720 int64_t iops_rd_max
,
1721 bool has_iops_wr_max
,
1722 int64_t iops_wr_max
,
1724 int64_t iops_size
, Error
**errp
)
1727 BlockDriverState
*bs
;
1728 AioContext
*aio_context
;
1730 bs
= bdrv_find(device
);
1732 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1736 memset(&cfg
, 0, sizeof(cfg
));
1737 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
1738 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
1739 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
1741 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
1742 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
1743 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
1746 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
1748 if (has_bps_rd_max
) {
1749 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
1751 if (has_bps_wr_max
) {
1752 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
1755 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
1757 if (has_iops_rd_max
) {
1758 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
1760 if (has_iops_wr_max
) {
1761 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
1764 if (has_iops_size
) {
1765 cfg
.op_size
= iops_size
;
1768 if (!check_throttle_config(&cfg
, errp
)) {
1772 aio_context
= bdrv_get_aio_context(bs
);
1773 aio_context_acquire(aio_context
);
1775 if (!bs
->io_limits_enabled
&& throttle_enabled(&cfg
)) {
1776 bdrv_io_limits_enable(bs
);
1777 } else if (bs
->io_limits_enabled
&& !throttle_enabled(&cfg
)) {
1778 bdrv_io_limits_disable(bs
);
1781 if (bs
->io_limits_enabled
) {
1782 bdrv_set_io_limits(bs
, &cfg
);
1785 aio_context_release(aio_context
);
1788 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1790 const char *id
= qdict_get_str(qdict
, "id");
1791 BlockDriverState
*bs
;
1793 AioContext
*aio_context
;
1794 Error
*local_err
= NULL
;
1798 error_report("Device '%s' not found", id
);
1802 dinfo
= drive_get_by_blockdev(bs
);
1803 if (dinfo
&& !dinfo
->enable_auto_del
) {
1804 error_report("Deleting device added with blockdev-add"
1805 " is not supported");
1809 aio_context
= bdrv_get_aio_context(bs
);
1810 aio_context_acquire(aio_context
);
1812 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
1813 error_report("%s", error_get_pretty(local_err
));
1814 error_free(local_err
);
1815 aio_context_release(aio_context
);
1819 /* quiesce block driver; prevent further io */
1824 /* if we have a device attached to this BlockDriverState
1825 * then we need to make the drive anonymous until the device
1826 * can be removed. If this is a drive with no device backing
1827 * then we can just get rid of the block driver state right here.
1829 if (bdrv_get_attached_dev(bs
)) {
1832 /* Further I/O must not pause the guest */
1833 bdrv_set_on_error(bs
, BLOCKDEV_ON_ERROR_REPORT
,
1834 BLOCKDEV_ON_ERROR_REPORT
);
1839 aio_context_release(aio_context
);
1843 void qmp_block_resize(bool has_device
, const char *device
,
1844 bool has_node_name
, const char *node_name
,
1845 int64_t size
, Error
**errp
)
1847 Error
*local_err
= NULL
;
1848 BlockDriverState
*bs
;
1849 AioContext
*aio_context
;
1852 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1853 has_node_name
? node_name
: NULL
,
1856 error_propagate(errp
, local_err
);
1860 aio_context
= bdrv_get_aio_context(bs
);
1861 aio_context_acquire(aio_context
);
1863 if (!bdrv_is_first_non_filter(bs
)) {
1864 error_set(errp
, QERR_FEATURE_DISABLED
, "resize");
1869 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
1873 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
1874 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1878 /* complete all in-flight operations before resizing the device */
1881 ret
= bdrv_truncate(bs
, size
);
1886 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1889 error_set(errp
, QERR_UNSUPPORTED
);
1892 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1895 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1898 error_setg_errno(errp
, -ret
, "Could not resize");
1903 aio_context_release(aio_context
);
1906 static void block_job_cb(void *opaque
, int ret
)
1908 BlockDriverState
*bs
= opaque
;
1909 const char *msg
= NULL
;
1911 trace_block_job_cb(bs
, bs
->job
, ret
);
1916 msg
= strerror(-ret
);
1919 if (block_job_is_cancelled(bs
->job
)) {
1920 block_job_event_cancelled(bs
->job
);
1922 block_job_event_completed(bs
->job
, msg
);
1925 bdrv_put_ref_bh_schedule(bs
);
1928 void qmp_block_stream(const char *device
,
1929 bool has_base
, const char *base
,
1930 bool has_backing_file
, const char *backing_file
,
1931 bool has_speed
, int64_t speed
,
1932 bool has_on_error
, BlockdevOnError on_error
,
1935 BlockDriverState
*bs
;
1936 BlockDriverState
*base_bs
= NULL
;
1937 Error
*local_err
= NULL
;
1938 const char *base_name
= NULL
;
1940 if (!has_on_error
) {
1941 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1944 bs
= bdrv_find(device
);
1946 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1950 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
1955 base_bs
= bdrv_find_backing_image(bs
, base
);
1956 if (base_bs
== NULL
) {
1957 error_set(errp
, QERR_BASE_NOT_FOUND
, base
);
1963 /* if we are streaming the entire chain, the result will have no backing
1964 * file, and specifying one is therefore an error */
1965 if (base_bs
== NULL
&& has_backing_file
) {
1966 error_setg(errp
, "backing file specified, but streaming the "
1971 /* backing_file string overrides base bs filename */
1972 base_name
= has_backing_file
? backing_file
: base_name
;
1974 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
1975 on_error
, block_job_cb
, bs
, &local_err
);
1977 error_propagate(errp
, local_err
);
1981 trace_qmp_block_stream(bs
, bs
->job
);
1984 void qmp_block_commit(const char *device
,
1985 bool has_base
, const char *base
,
1986 bool has_top
, const char *top
,
1987 bool has_backing_file
, const char *backing_file
,
1988 bool has_speed
, int64_t speed
,
1991 BlockDriverState
*bs
;
1992 BlockDriverState
*base_bs
, *top_bs
;
1993 Error
*local_err
= NULL
;
1994 /* This will be part of the QMP command, if/when the
1995 * BlockdevOnError change for blkmirror makes it in
1997 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2003 /* drain all i/o before commits */
2007 * libvirt relies on the DeviceNotFound error class in order to probe for
2008 * live commit feature versions; for this to work, we must make sure to
2009 * perform the device lookup before any generic errors that may occur in a
2010 * scenario in which all optional arguments are omitted. */
2011 bs
= bdrv_find(device
);
2013 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2017 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT
, errp
)) {
2021 /* default top_bs is the active layer */
2024 if (has_top
&& top
) {
2025 if (strcmp(bs
->filename
, top
) != 0) {
2026 top_bs
= bdrv_find_backing_image(bs
, top
);
2030 if (top_bs
== NULL
) {
2031 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2035 if (has_base
&& base
) {
2036 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2038 base_bs
= bdrv_find_base(top_bs
);
2041 if (base_bs
== NULL
) {
2042 error_set(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2046 /* Do not allow attempts to commit an image into itself */
2047 if (top_bs
== base_bs
) {
2048 error_setg(errp
, "cannot commit an image into itself");
2053 if (has_backing_file
) {
2054 error_setg(errp
, "'backing-file' specified,"
2055 " but 'top' is the active layer");
2058 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
2061 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
2062 has_backing_file
? backing_file
: NULL
, &local_err
);
2064 if (local_err
!= NULL
) {
2065 error_propagate(errp
, local_err
);
2070 void qmp_drive_backup(const char *device
, const char *target
,
2071 bool has_format
, const char *format
,
2072 enum MirrorSyncMode sync
,
2073 bool has_mode
, enum NewImageMode mode
,
2074 bool has_speed
, int64_t speed
,
2075 bool has_on_source_error
, BlockdevOnError on_source_error
,
2076 bool has_on_target_error
, BlockdevOnError on_target_error
,
2079 BlockDriverState
*bs
;
2080 BlockDriverState
*target_bs
;
2081 BlockDriverState
*source
= NULL
;
2082 BlockDriver
*drv
= NULL
;
2083 Error
*local_err
= NULL
;
2091 if (!has_on_source_error
) {
2092 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2094 if (!has_on_target_error
) {
2095 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2098 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2101 bs
= bdrv_find(device
);
2103 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2107 if (!bdrv_is_inserted(bs
)) {
2108 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2113 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2116 drv
= bdrv_find_format(format
);
2118 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
2123 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
2127 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2129 /* See if we have a backing HD we can use to create our new image
2131 if (sync
== MIRROR_SYNC_MODE_TOP
) {
2132 source
= bs
->backing_hd
;
2134 sync
= MIRROR_SYNC_MODE_FULL
;
2137 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2141 size
= bdrv_getlength(bs
);
2143 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2147 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
2148 assert(format
&& drv
);
2150 bdrv_img_create(target
, format
, source
->filename
,
2151 source
->drv
->format_name
, NULL
,
2152 size
, flags
, &local_err
, false);
2154 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
2155 size
, flags
, &local_err
, false);
2160 error_propagate(errp
, local_err
);
2165 ret
= bdrv_open(&target_bs
, target
, NULL
, NULL
, flags
, drv
, &local_err
);
2167 error_propagate(errp
, local_err
);
2171 backup_start(bs
, target_bs
, speed
, sync
, on_source_error
, on_target_error
,
2172 block_job_cb
, bs
, &local_err
);
2173 if (local_err
!= NULL
) {
2174 bdrv_unref(target_bs
);
2175 error_propagate(errp
, local_err
);
2180 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
2182 return bdrv_named_nodes_list();
2185 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2187 void qmp_drive_mirror(const char *device
, const char *target
,
2188 bool has_format
, const char *format
,
2189 bool has_node_name
, const char *node_name
,
2190 bool has_replaces
, const char *replaces
,
2191 enum MirrorSyncMode sync
,
2192 bool has_mode
, enum NewImageMode mode
,
2193 bool has_speed
, int64_t speed
,
2194 bool has_granularity
, uint32_t granularity
,
2195 bool has_buf_size
, int64_t buf_size
,
2196 bool has_on_source_error
, BlockdevOnError on_source_error
,
2197 bool has_on_target_error
, BlockdevOnError on_target_error
,
2200 BlockDriverState
*bs
;
2201 BlockDriverState
*source
, *target_bs
;
2202 BlockDriver
*drv
= NULL
;
2203 Error
*local_err
= NULL
;
2204 QDict
*options
= NULL
;
2212 if (!has_on_source_error
) {
2213 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2215 if (!has_on_target_error
) {
2216 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2219 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2221 if (!has_granularity
) {
2224 if (!has_buf_size
) {
2225 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
2228 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
2229 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2230 "a value in range [512B, 64MB]");
2233 if (granularity
& (granularity
- 1)) {
2234 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity", "power of 2");
2238 bs
= bdrv_find(device
);
2240 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2244 if (!bdrv_is_inserted(bs
)) {
2245 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2250 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2253 drv
= bdrv_find_format(format
);
2255 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
2260 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR
, errp
)) {
2264 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2265 source
= bs
->backing_hd
;
2266 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
2267 sync
= MIRROR_SYNC_MODE_FULL
;
2269 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2273 size
= bdrv_getlength(bs
);
2275 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2280 BlockDriverState
*to_replace_bs
;
2282 if (!has_node_name
) {
2283 error_setg(errp
, "a node-name must be provided when replacing a"
2284 " named node of the graph");
2288 to_replace_bs
= check_to_replace_node(replaces
, &local_err
);
2290 if (!to_replace_bs
) {
2291 error_propagate(errp
, local_err
);
2295 if (size
!= bdrv_getlength(to_replace_bs
)) {
2296 error_setg(errp
, "cannot replace image with a mirror image of "
2302 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
2303 && mode
!= NEW_IMAGE_MODE_EXISTING
)
2305 /* create new image w/o backing file */
2306 assert(format
&& drv
);
2307 bdrv_img_create(target
, format
,
2308 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
2311 case NEW_IMAGE_MODE_EXISTING
:
2313 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
2314 /* create new image with backing file */
2315 bdrv_img_create(target
, format
,
2317 source
->drv
->format_name
,
2318 NULL
, size
, flags
, &local_err
, false);
2326 error_propagate(errp
, local_err
);
2330 if (has_node_name
) {
2331 options
= qdict_new();
2332 qdict_put(options
, "node-name", qstring_from_str(node_name
));
2335 /* Mirroring takes care of copy-on-write using the source's backing
2339 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
2340 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
2342 error_propagate(errp
, local_err
);
2346 /* pass the node name to replace to mirror start since it's loose coupling
2347 * and will allow to check whether the node still exist at mirror completion
2349 mirror_start(bs
, target_bs
,
2350 has_replaces
? replaces
: NULL
,
2351 speed
, granularity
, buf_size
, sync
,
2352 on_source_error
, on_target_error
,
2353 block_job_cb
, bs
, &local_err
);
2354 if (local_err
!= NULL
) {
2355 bdrv_unref(target_bs
);
2356 error_propagate(errp
, local_err
);
2361 static BlockJob
*find_block_job(const char *device
)
2363 BlockDriverState
*bs
;
2365 bs
= bdrv_find(device
);
2366 if (!bs
|| !bs
->job
) {
2372 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
2374 BlockJob
*job
= find_block_job(device
);
2377 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2381 block_job_set_speed(job
, speed
, errp
);
2384 void qmp_block_job_cancel(const char *device
,
2385 bool has_force
, bool force
, Error
**errp
)
2387 BlockJob
*job
= find_block_job(device
);
2394 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2397 if (job
->paused
&& !force
) {
2398 error_setg(errp
, "The block job for device '%s' is currently paused",
2403 trace_qmp_block_job_cancel(job
);
2404 block_job_cancel(job
);
2407 void qmp_block_job_pause(const char *device
, Error
**errp
)
2409 BlockJob
*job
= find_block_job(device
);
2412 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2416 trace_qmp_block_job_pause(job
);
2417 block_job_pause(job
);
2420 void qmp_block_job_resume(const char *device
, Error
**errp
)
2422 BlockJob
*job
= find_block_job(device
);
2425 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2429 trace_qmp_block_job_resume(job
);
2430 block_job_resume(job
);
2433 void qmp_block_job_complete(const char *device
, Error
**errp
)
2435 BlockJob
*job
= find_block_job(device
);
2438 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2442 trace_qmp_block_job_complete(job
);
2443 block_job_complete(job
, errp
);
2446 void qmp_change_backing_file(const char *device
,
2447 const char *image_node_name
,
2448 const char *backing_file
,
2451 BlockDriverState
*bs
= NULL
;
2452 BlockDriverState
*image_bs
= NULL
;
2453 Error
*local_err
= NULL
;
2458 /* find the top layer BDS of the chain */
2459 bs
= bdrv_find(device
);
2461 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2465 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
2467 error_propagate(errp
, local_err
);
2472 error_setg(errp
, "image file not found");
2476 if (bdrv_find_base(image_bs
) == image_bs
) {
2477 error_setg(errp
, "not allowing backing file change on an image "
2478 "without a backing file");
2482 /* even though we are not necessarily operating on bs, we need it to
2483 * determine if block ops are currently prohibited on the chain */
2484 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
2488 /* final sanity check */
2489 if (!bdrv_chain_contains(bs
, image_bs
)) {
2490 error_setg(errp
, "'%s' and image file are not in the same chain",
2495 /* if not r/w, reopen to make r/w */
2496 open_flags
= image_bs
->open_flags
;
2497 ro
= bdrv_is_read_only(image_bs
);
2500 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
2502 error_propagate(errp
, local_err
);
2507 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
2508 image_bs
->drv
? image_bs
->drv
->format_name
: "");
2511 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
2513 /* don't exit here, so we can try to restore open flags if
2518 bdrv_reopen(image_bs
, open_flags
, &local_err
);
2520 error_propagate(errp
, local_err
); /* will preserve prior errp */
2525 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
2527 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
2531 Error
*local_err
= NULL
;
2533 /* Require an ID in the top level */
2534 if (!options
->has_id
) {
2535 error_setg(errp
, "Block device needs an ID");
2539 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
2540 * cache.direct=false instead of silently switching to aio=threads, except
2541 * when called from drive_new().
2543 * For now, simply forbidding the combination for all drivers will do. */
2544 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
2545 bool direct
= options
->has_cache
&&
2546 options
->cache
->has_direct
&&
2547 options
->cache
->direct
;
2549 error_setg(errp
, "aio=native requires cache.direct=true");
2554 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
2555 &options
, NULL
, &local_err
);
2557 error_propagate(errp
, local_err
);
2561 obj
= qmp_output_get_qobject(ov
);
2562 qdict
= qobject_to_qdict(obj
);
2564 qdict_flatten(qdict
);
2566 dinfo
= blockdev_init(NULL
, qdict
, &local_err
);
2568 error_propagate(errp
, local_err
);
2572 if (bdrv_key_required(dinfo
->bdrv
)) {
2574 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
2579 qmp_output_visitor_cleanup(ov
);
2582 static void do_qmp_query_block_jobs_one(void *opaque
, BlockDriverState
*bs
)
2584 BlockJobInfoList
**prev
= opaque
;
2585 BlockJob
*job
= bs
->job
;
2588 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
2589 elem
->value
= block_job_query(bs
->job
);
2590 (*prev
)->next
= elem
;
2595 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
2597 /* Dummy is a fake list element for holding the head pointer */
2598 BlockJobInfoList dummy
= {};
2599 BlockJobInfoList
*prev
= &dummy
;
2600 bdrv_iterate(do_qmp_query_block_jobs_one
, &prev
);
2604 QemuOptsList qemu_common_drive_opts
= {
2606 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
2610 .type
= QEMU_OPT_BOOL
,
2611 .help
= "enable/disable snapshot mode",
2614 .type
= QEMU_OPT_STRING
,
2615 .help
= "discard operation (ignore/off, unmap/on)",
2617 .name
= "cache.writeback",
2618 .type
= QEMU_OPT_BOOL
,
2619 .help
= "enables writeback mode for any caches",
2621 .name
= "cache.direct",
2622 .type
= QEMU_OPT_BOOL
,
2623 .help
= "enables use of O_DIRECT (bypass the host page cache)",
2625 .name
= "cache.no-flush",
2626 .type
= QEMU_OPT_BOOL
,
2627 .help
= "ignore any flush requests for the device",
2630 .type
= QEMU_OPT_STRING
,
2631 .help
= "host AIO implementation (threads, native)",
2634 .type
= QEMU_OPT_STRING
,
2635 .help
= "disk format (raw, qcow2, ...)",
2638 .type
= QEMU_OPT_STRING
,
2639 .help
= "read error action",
2642 .type
= QEMU_OPT_STRING
,
2643 .help
= "write error action",
2645 .name
= "read-only",
2646 .type
= QEMU_OPT_BOOL
,
2647 .help
= "open drive file as read-only",
2649 .name
= "throttling.iops-total",
2650 .type
= QEMU_OPT_NUMBER
,
2651 .help
= "limit total I/O operations per second",
2653 .name
= "throttling.iops-read",
2654 .type
= QEMU_OPT_NUMBER
,
2655 .help
= "limit read operations per second",
2657 .name
= "throttling.iops-write",
2658 .type
= QEMU_OPT_NUMBER
,
2659 .help
= "limit write operations per second",
2661 .name
= "throttling.bps-total",
2662 .type
= QEMU_OPT_NUMBER
,
2663 .help
= "limit total bytes per second",
2665 .name
= "throttling.bps-read",
2666 .type
= QEMU_OPT_NUMBER
,
2667 .help
= "limit read bytes per second",
2669 .name
= "throttling.bps-write",
2670 .type
= QEMU_OPT_NUMBER
,
2671 .help
= "limit write bytes per second",
2673 .name
= "throttling.iops-total-max",
2674 .type
= QEMU_OPT_NUMBER
,
2675 .help
= "I/O operations burst",
2677 .name
= "throttling.iops-read-max",
2678 .type
= QEMU_OPT_NUMBER
,
2679 .help
= "I/O operations read burst",
2681 .name
= "throttling.iops-write-max",
2682 .type
= QEMU_OPT_NUMBER
,
2683 .help
= "I/O operations write burst",
2685 .name
= "throttling.bps-total-max",
2686 .type
= QEMU_OPT_NUMBER
,
2687 .help
= "total bytes burst",
2689 .name
= "throttling.bps-read-max",
2690 .type
= QEMU_OPT_NUMBER
,
2691 .help
= "total bytes read burst",
2693 .name
= "throttling.bps-write-max",
2694 .type
= QEMU_OPT_NUMBER
,
2695 .help
= "total bytes write burst",
2697 .name
= "throttling.iops-size",
2698 .type
= QEMU_OPT_NUMBER
,
2699 .help
= "when limiting by iops max size of an I/O in bytes",
2701 .name
= "copy-on-read",
2702 .type
= QEMU_OPT_BOOL
,
2703 .help
= "copy read data from backing file into image file",
2705 .name
= "detect-zeroes",
2706 .type
= QEMU_OPT_STRING
,
2707 .help
= "try to optimize zero writes (off, on, unmap)",
2709 { /* end of list */ }
2713 QemuOptsList qemu_drive_opts
= {
2715 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
2718 * no elements => accept any params
2719 * validation will happen later
2721 { /* end of list */ }