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 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
171 return drive_get(type
,
172 drive_index_to_bus_id(type
, index
),
173 drive_index_to_unit_id(type
, index
));
176 int drive_get_max_bus(BlockInterfaceType type
)
182 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
183 if(dinfo
->type
== type
&&
184 dinfo
->bus
> max_bus
)
185 max_bus
= dinfo
->bus
;
190 /* Get a block device. This should only be used for single-drive devices
191 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
193 DriveInfo
*drive_get_next(BlockInterfaceType type
)
195 static int next_block_unit
[IF_COUNT
];
197 return drive_get(type
, 0, next_block_unit
[type
]++);
200 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
204 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
205 if (dinfo
->bdrv
== bs
) {
212 static void bdrv_format_print(void *opaque
, const char *name
)
214 error_printf(" %s", name
);
217 void drive_del(DriveInfo
*dinfo
)
219 bdrv_unref(dinfo
->bdrv
);
222 void drive_info_del(DriveInfo
*dinfo
)
228 qemu_opts_del(dinfo
->opts
);
231 QTAILQ_REMOVE(&drives
, dinfo
, next
);
232 g_free(dinfo
->serial
);
238 BlockDriverState
*bs
;
241 static void bdrv_put_ref_bh(void *opaque
)
243 BDRVPutRefBH
*s
= opaque
;
246 qemu_bh_delete(s
->bh
);
251 * Release a BDS reference in a BH
253 * It is not safe to use bdrv_unref() from a callback function when the callers
254 * still need the BlockDriverState. In such cases we schedule a BH to release
257 static void bdrv_put_ref_bh_schedule(BlockDriverState
*bs
)
261 s
= g_new(BDRVPutRefBH
, 1);
262 s
->bh
= qemu_bh_new(bdrv_put_ref_bh
, s
);
264 qemu_bh_schedule(s
->bh
);
267 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
269 if (!strcmp(buf
, "ignore")) {
270 return BLOCKDEV_ON_ERROR_IGNORE
;
271 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
272 return BLOCKDEV_ON_ERROR_ENOSPC
;
273 } else if (!strcmp(buf
, "stop")) {
274 return BLOCKDEV_ON_ERROR_STOP
;
275 } else if (!strcmp(buf
, "report")) {
276 return BLOCKDEV_ON_ERROR_REPORT
;
278 error_setg(errp
, "'%s' invalid %s error action",
279 buf
, is_read
? "read" : "write");
284 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
286 if (throttle_conflicting(cfg
)) {
287 error_setg(errp
, "bps/iops/max total values and read/write values"
288 " cannot be used at the same time");
292 if (!throttle_is_valid(cfg
)) {
293 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
300 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
302 /* Takes the ownership of bs_opts */
303 static DriveInfo
*blockdev_init(const char *file
, QDict
*bs_opts
,
309 int on_read_error
, on_write_error
;
310 BlockDriverState
*bs
;
319 bool has_driver_specific_opts
;
320 BlockdevDetectZeroesOptions detect_zeroes
;
321 BlockDriver
*drv
= NULL
;
323 /* Check common options by copying from bs_opts to opts, all other options
324 * stay in bs_opts for processing by bdrv_open(). */
325 id
= qdict_get_try_str(bs_opts
, "id");
326 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
328 error_propagate(errp
, error
);
332 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
334 error_propagate(errp
, error
);
339 qdict_del(bs_opts
, "id");
342 has_driver_specific_opts
= !!qdict_size(bs_opts
);
344 /* extract parameters */
345 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
346 ro
= qemu_opt_get_bool(opts
, "read-only", 0);
347 copy_on_read
= qemu_opt_get_bool(opts
, "copy-on-read", false);
349 if ((buf
= qemu_opt_get(opts
, "discard")) != NULL
) {
350 if (bdrv_parse_discard_flags(buf
, &bdrv_flags
) != 0) {
351 error_setg(errp
, "invalid discard option");
356 if (qemu_opt_get_bool(opts
, "cache.writeback", true)) {
357 bdrv_flags
|= BDRV_O_CACHE_WB
;
359 if (qemu_opt_get_bool(opts
, "cache.direct", false)) {
360 bdrv_flags
|= BDRV_O_NOCACHE
;
362 if (qemu_opt_get_bool(opts
, "cache.no-flush", false)) {
363 bdrv_flags
|= BDRV_O_NO_FLUSH
;
366 #ifdef CONFIG_LINUX_AIO
367 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
368 if (!strcmp(buf
, "native")) {
369 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
370 } else if (!strcmp(buf
, "threads")) {
371 /* this is the default */
373 error_setg(errp
, "invalid aio option");
379 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
380 if (is_help_option(buf
)) {
381 error_printf("Supported formats:");
382 bdrv_iterate_format(bdrv_format_print
, NULL
);
387 drv
= bdrv_find_format(buf
);
389 error_setg(errp
, "'%s' invalid format", buf
);
394 /* disk I/O throttling */
395 memset(&cfg
, 0, sizeof(cfg
));
396 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
=
397 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
398 cfg
.buckets
[THROTTLE_BPS_READ
].avg
=
399 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
400 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
=
401 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
402 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
=
403 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
404 cfg
.buckets
[THROTTLE_OPS_READ
].avg
=
405 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
406 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
=
407 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
409 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
=
410 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
411 cfg
.buckets
[THROTTLE_BPS_READ
].max
=
412 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
413 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
=
414 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
415 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
=
416 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
417 cfg
.buckets
[THROTTLE_OPS_READ
].max
=
418 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
419 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
=
420 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
422 cfg
.op_size
= qemu_opt_get_number(opts
, "throttling.iops-size", 0);
424 if (!check_throttle_config(&cfg
, &error
)) {
425 error_propagate(errp
, error
);
429 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
430 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
431 on_write_error
= parse_block_error_action(buf
, 0, &error
);
433 error_propagate(errp
, error
);
438 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
439 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
440 on_read_error
= parse_block_error_action(buf
, 1, &error
);
442 error_propagate(errp
, error
);
448 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
449 qemu_opt_get(opts
, "detect-zeroes"),
450 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX
,
451 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
454 error_propagate(errp
, error
);
458 if (detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
459 !(bdrv_flags
& BDRV_O_UNMAP
)) {
460 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
461 "without setting discard operation to unmap");
466 bs
= bdrv_new(qemu_opts_id(opts
), errp
);
470 bs
->open_flags
= snapshot
? BDRV_O_SNAPSHOT
: 0;
472 bs
->detect_zeroes
= detect_zeroes
;
474 bdrv_set_on_error(bs
, on_read_error
, on_write_error
);
476 /* disk I/O throttling */
477 if (throttle_enabled(&cfg
)) {
478 bdrv_io_limits_enable(bs
);
479 bdrv_set_io_limits(bs
, &cfg
);
482 dinfo
= g_malloc0(sizeof(*dinfo
));
483 dinfo
->id
= g_strdup(qemu_opts_id(opts
));
485 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
487 if (!file
|| !*file
) {
488 if (has_driver_specific_opts
) {
497 /* always use cache=unsafe with snapshot */
498 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
499 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
503 bdrv_flags
|= BDRV_O_COPY_ON_READ
;
506 if (runstate_check(RUN_STATE_INMIGRATE
)) {
507 bdrv_flags
|= BDRV_O_INCOMING
;
510 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
513 ret
= bdrv_open(&bs
, file
, NULL
, bs_opts
, bdrv_flags
, drv
, &error
);
514 assert(bs
== dinfo
->bdrv
);
517 error_setg(errp
, "could not open disk image %s: %s",
518 file
?: dinfo
->id
, error_get_pretty(error
));
523 if (bdrv_key_required(bs
)) {
541 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
546 value
= qemu_opt_get(opts
, from
);
548 if (qemu_opt_find(opts
, to
)) {
549 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
550 "same time", to
, from
);
553 qemu_opt_set(opts
, to
, value
);
554 qemu_opt_unset(opts
, from
);
558 QemuOptsList qemu_legacy_drive_opts
= {
560 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
564 .type
= QEMU_OPT_NUMBER
,
565 .help
= "bus number",
568 .type
= QEMU_OPT_NUMBER
,
569 .help
= "unit number (i.e. lun for scsi)",
572 .type
= QEMU_OPT_NUMBER
,
573 .help
= "index number",
576 .type
= QEMU_OPT_STRING
,
577 .help
= "media type (disk, cdrom)",
580 .type
= QEMU_OPT_STRING
,
581 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
584 .type
= QEMU_OPT_NUMBER
,
585 .help
= "number of cylinders (ide disk geometry)",
588 .type
= QEMU_OPT_NUMBER
,
589 .help
= "number of heads (ide disk geometry)",
592 .type
= QEMU_OPT_NUMBER
,
593 .help
= "number of sectors (ide disk geometry)",
596 .type
= QEMU_OPT_STRING
,
597 .help
= "chs translation (auto, lba, none)",
600 .type
= QEMU_OPT_BOOL
,
601 .help
= "(deprecated, ignored)",
604 .type
= QEMU_OPT_STRING
,
605 .help
= "pci address (virtio only)",
608 .type
= QEMU_OPT_STRING
,
609 .help
= "disk serial number",
612 .type
= QEMU_OPT_STRING
,
616 /* Options that are passed on, but have special semantics with -drive */
619 .type
= QEMU_OPT_BOOL
,
620 .help
= "open drive file as read-only",
623 .type
= QEMU_OPT_STRING
,
624 .help
= "read error action",
627 .type
= QEMU_OPT_STRING
,
628 .help
= "write error action",
630 .name
= "copy-on-read",
631 .type
= QEMU_OPT_BOOL
,
632 .help
= "copy read data from backing file into image file",
635 { /* end of list */ }
639 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
642 DriveInfo
*dinfo
= NULL
;
644 QemuOpts
*legacy_opts
;
645 DriveMediaType media
= MEDIA_DISK
;
646 BlockInterfaceType type
;
647 int cyls
, heads
, secs
, translation
;
648 int max_devs
, bus_id
, unit_id
, index
;
650 const char *werror
, *rerror
;
651 bool read_only
= false;
654 const char *filename
;
655 Error
*local_err
= NULL
;
658 /* Change legacy command line options into QMP ones */
659 static const struct {
663 { "iops", "throttling.iops-total" },
664 { "iops_rd", "throttling.iops-read" },
665 { "iops_wr", "throttling.iops-write" },
667 { "bps", "throttling.bps-total" },
668 { "bps_rd", "throttling.bps-read" },
669 { "bps_wr", "throttling.bps-write" },
671 { "iops_max", "throttling.iops-total-max" },
672 { "iops_rd_max", "throttling.iops-read-max" },
673 { "iops_wr_max", "throttling.iops-write-max" },
675 { "bps_max", "throttling.bps-total-max" },
676 { "bps_rd_max", "throttling.bps-read-max" },
677 { "bps_wr_max", "throttling.bps-write-max" },
679 { "iops_size", "throttling.iops-size" },
681 { "readonly", "read-only" },
684 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
685 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
688 error_report("%s", error_get_pretty(local_err
));
689 error_free(local_err
);
694 value
= qemu_opt_get(all_opts
, "cache");
698 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
699 error_report("invalid cache option");
703 /* Specific options take precedence */
704 if (!qemu_opt_get(all_opts
, "cache.writeback")) {
705 qemu_opt_set_bool(all_opts
, "cache.writeback",
706 !!(flags
& BDRV_O_CACHE_WB
));
708 if (!qemu_opt_get(all_opts
, "cache.direct")) {
709 qemu_opt_set_bool(all_opts
, "cache.direct",
710 !!(flags
& BDRV_O_NOCACHE
));
712 if (!qemu_opt_get(all_opts
, "cache.no-flush")) {
713 qemu_opt_set_bool(all_opts
, "cache.no-flush",
714 !!(flags
& BDRV_O_NO_FLUSH
));
716 qemu_opt_unset(all_opts
, "cache");
719 /* Get a QDict for processing the options */
720 bs_opts
= qdict_new();
721 qemu_opts_to_qdict(all_opts
, bs_opts
);
723 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
725 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
727 error_report("%s", error_get_pretty(local_err
));
728 error_free(local_err
);
732 /* Deprecated option boot=[on|off] */
733 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
734 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
735 "ignored. Future versions will reject this parameter. Please "
736 "update your scripts.\n");
740 value
= qemu_opt_get(legacy_opts
, "media");
742 if (!strcmp(value
, "disk")) {
744 } else if (!strcmp(value
, "cdrom")) {
748 error_report("'%s' invalid media", value
);
753 /* copy-on-read is disabled with a warning for read-only devices */
754 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
755 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
757 if (read_only
&& copy_on_read
) {
758 error_report("warning: disabling copy-on-read on read-only drive");
759 copy_on_read
= false;
762 qdict_put(bs_opts
, "read-only",
763 qstring_from_str(read_only
? "on" : "off"));
764 qdict_put(bs_opts
, "copy-on-read",
765 qstring_from_str(copy_on_read
? "on" :"off"));
767 /* Controller type */
768 value
= qemu_opt_get(legacy_opts
, "if");
771 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
774 if (type
== IF_COUNT
) {
775 error_report("unsupported bus type '%s'", value
);
779 type
= block_default_type
;
783 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
784 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
785 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
787 if (cyls
|| heads
|| secs
) {
789 error_report("invalid physical cyls number");
793 error_report("invalid physical heads number");
797 error_report("invalid physical secs number");
802 translation
= BIOS_ATA_TRANSLATION_AUTO
;
803 value
= qemu_opt_get(legacy_opts
, "trans");
806 error_report("'%s' trans must be used with cyls, heads and secs",
810 if (!strcmp(value
, "none")) {
811 translation
= BIOS_ATA_TRANSLATION_NONE
;
812 } else if (!strcmp(value
, "lba")) {
813 translation
= BIOS_ATA_TRANSLATION_LBA
;
814 } else if (!strcmp(value
, "large")) {
815 translation
= BIOS_ATA_TRANSLATION_LARGE
;
816 } else if (!strcmp(value
, "rechs")) {
817 translation
= BIOS_ATA_TRANSLATION_RECHS
;
818 } else if (!strcmp(value
, "auto")) {
819 translation
= BIOS_ATA_TRANSLATION_AUTO
;
821 error_report("'%s' invalid translation type", value
);
826 if (media
== MEDIA_CDROM
) {
827 if (cyls
|| secs
|| heads
) {
828 error_report("CHS can't be set with media=cdrom");
833 /* Device address specified by bus/unit or index.
834 * If none was specified, try to find the first free one. */
835 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
836 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
837 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
839 max_devs
= if_max_devs
[type
];
842 if (bus_id
!= 0 || unit_id
!= -1) {
843 error_report("index cannot be used with bus and unit");
846 bus_id
= drive_index_to_bus_id(type
, index
);
847 unit_id
= drive_index_to_unit_id(type
, index
);
852 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
854 if (max_devs
&& unit_id
>= max_devs
) {
861 if (max_devs
&& unit_id
>= max_devs
) {
862 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
866 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
867 error_report("drive with bus=%d, unit=%d (index=%d) exists",
868 bus_id
, unit_id
, index
);
873 serial
= qemu_opt_get(legacy_opts
, "serial");
875 /* no id supplied -> create one */
876 if (qemu_opts_id(all_opts
) == NULL
) {
878 const char *mediastr
= "";
879 if (type
== IF_IDE
|| type
== IF_SCSI
) {
880 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
883 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
886 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
889 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
893 /* Add virtio block device */
894 devaddr
= qemu_opt_get(legacy_opts
, "addr");
895 if (devaddr
&& type
!= IF_VIRTIO
) {
896 error_report("addr is not supported by this bus type");
900 if (type
== IF_VIRTIO
) {
902 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
904 if (arch_type
== QEMU_ARCH_S390X
) {
905 qemu_opt_set(devopts
, "driver", "virtio-blk-s390");
907 qemu_opt_set(devopts
, "driver", "virtio-blk-pci");
909 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"));
911 qemu_opt_set(devopts
, "addr", devaddr
);
915 filename
= qemu_opt_get(legacy_opts
, "file");
917 /* Check werror/rerror compatibility with if=... */
918 werror
= qemu_opt_get(legacy_opts
, "werror");
919 if (werror
!= NULL
) {
920 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
922 error_report("werror is not supported by this bus type");
925 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
928 rerror
= qemu_opt_get(legacy_opts
, "rerror");
929 if (rerror
!= NULL
) {
930 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
932 error_report("rerror is not supported by this bus type");
935 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
938 /* Actual block device init: Functionality shared with blockdev-add */
939 dinfo
= blockdev_init(filename
, bs_opts
, &local_err
);
943 error_report("%s", error_get_pretty(local_err
));
944 error_free(local_err
);
951 /* Set legacy DriveInfo fields */
952 dinfo
->enable_auto_del
= true;
953 dinfo
->opts
= all_opts
;
956 dinfo
->heads
= heads
;
958 dinfo
->trans
= translation
;
962 dinfo
->unit
= unit_id
;
963 dinfo
->devaddr
= devaddr
;
965 dinfo
->serial
= g_strdup(serial
);
972 dinfo
->media_cd
= media
== MEDIA_CDROM
;
979 qemu_opts_del(legacy_opts
);
984 void do_commit(Monitor
*mon
, const QDict
*qdict
)
986 const char *device
= qdict_get_str(qdict
, "device");
987 BlockDriverState
*bs
;
990 if (!strcmp(device
, "all")) {
991 ret
= bdrv_commit_all();
993 bs
= bdrv_find(device
);
995 monitor_printf(mon
, "Device '%s' not found\n", device
);
998 ret
= bdrv_commit(bs
);
1001 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1006 static void blockdev_do_action(int kind
, void *data
, Error
**errp
)
1008 TransactionAction action
;
1009 TransactionActionList list
;
1013 list
.value
= &action
;
1015 qmp_transaction(&list
, errp
);
1018 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1019 bool has_node_name
, const char *node_name
,
1020 const char *snapshot_file
,
1021 bool has_snapshot_node_name
,
1022 const char *snapshot_node_name
,
1023 bool has_format
, const char *format
,
1024 bool has_mode
, NewImageMode mode
, Error
**errp
)
1026 BlockdevSnapshot snapshot
= {
1027 .has_device
= has_device
,
1028 .device
= (char *) device
,
1029 .has_node_name
= has_node_name
,
1030 .node_name
= (char *) node_name
,
1031 .snapshot_file
= (char *) snapshot_file
,
1032 .has_snapshot_node_name
= has_snapshot_node_name
,
1033 .snapshot_node_name
= (char *) snapshot_node_name
,
1034 .has_format
= has_format
,
1035 .format
= (char *) format
,
1036 .has_mode
= has_mode
,
1039 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1043 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1047 BlockdevSnapshotInternal snapshot
= {
1048 .device
= (char *) device
,
1049 .name
= (char *) name
1052 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1056 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1063 BlockDriverState
*bs
= bdrv_find(device
);
1064 QEMUSnapshotInfo sn
;
1065 Error
*local_err
= NULL
;
1066 SnapshotInfo
*info
= NULL
;
1070 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1083 error_setg(errp
, "Name or id must be provided");
1087 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1089 error_propagate(errp
, local_err
);
1094 "Snapshot with id '%s' and name '%s' does not exist on "
1096 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1100 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1102 error_propagate(errp
, local_err
);
1106 info
= g_new0(SnapshotInfo
, 1);
1107 info
->id
= g_strdup(sn
.id_str
);
1108 info
->name
= g_strdup(sn
.name
);
1109 info
->date_nsec
= sn
.date_nsec
;
1110 info
->date_sec
= sn
.date_sec
;
1111 info
->vm_state_size
= sn
.vm_state_size
;
1112 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1113 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1118 /* New and old BlockDriverState structs for group snapshots */
1120 typedef struct BlkTransactionState BlkTransactionState
;
1122 /* Only prepare() may fail. In a single transaction, only one of commit() or
1123 abort() will be called, clean() will always be called if it present. */
1124 typedef struct BdrvActionOps
{
1125 /* Size of state struct, in bytes. */
1126 size_t instance_size
;
1127 /* Prepare the work, must NOT be NULL. */
1128 void (*prepare
)(BlkTransactionState
*common
, Error
**errp
);
1129 /* Commit the changes, can be NULL. */
1130 void (*commit
)(BlkTransactionState
*common
);
1131 /* Abort the changes on fail, can be NULL. */
1132 void (*abort
)(BlkTransactionState
*common
);
1133 /* Clean up resource in the end, can be NULL. */
1134 void (*clean
)(BlkTransactionState
*common
);
1138 * This structure must be arranged as first member in child type, assuming
1139 * that compiler will also arrange it to the same address with parent instance.
1140 * Later it will be used in free().
1142 struct BlkTransactionState
{
1143 TransactionAction
*action
;
1144 const BdrvActionOps
*ops
;
1145 QSIMPLEQ_ENTRY(BlkTransactionState
) entry
;
1148 /* internal snapshot private data */
1149 typedef struct InternalSnapshotState
{
1150 BlkTransactionState common
;
1151 BlockDriverState
*bs
;
1152 QEMUSnapshotInfo sn
;
1153 } InternalSnapshotState
;
1155 static void internal_snapshot_prepare(BlkTransactionState
*common
,
1158 Error
*local_err
= NULL
;
1161 BlockDriverState
*bs
;
1162 QEMUSnapshotInfo old_sn
, *sn
;
1165 BlockdevSnapshotInternal
*internal
;
1166 InternalSnapshotState
*state
;
1169 g_assert(common
->action
->kind
==
1170 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1171 internal
= common
->action
->blockdev_snapshot_internal_sync
;
1172 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1174 /* 1. parse input */
1175 device
= internal
->device
;
1176 name
= internal
->name
;
1178 /* 2. check for validation */
1179 bs
= bdrv_find(device
);
1181 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1185 if (!bdrv_is_inserted(bs
)) {
1186 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1190 if (bdrv_is_read_only(bs
)) {
1191 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1195 if (!bdrv_can_snapshot(bs
)) {
1196 error_set(errp
, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
1197 bs
->drv
->format_name
, device
, "internal snapshot");
1201 if (!strlen(name
)) {
1202 error_setg(errp
, "Name is empty");
1206 /* check whether a snapshot with name exist */
1207 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1210 error_propagate(errp
, local_err
);
1214 "Snapshot with name '%s' already exists on device '%s'",
1219 /* 3. take the snapshot */
1221 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1222 qemu_gettimeofday(&tv
);
1223 sn
->date_sec
= tv
.tv_sec
;
1224 sn
->date_nsec
= tv
.tv_usec
* 1000;
1225 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1227 ret1
= bdrv_snapshot_create(bs
, sn
);
1229 error_setg_errno(errp
, -ret1
,
1230 "Failed to create snapshot '%s' on device '%s'",
1235 /* 4. succeed, mark a snapshot is created */
1239 static void internal_snapshot_abort(BlkTransactionState
*common
)
1241 InternalSnapshotState
*state
=
1242 DO_UPCAST(InternalSnapshotState
, common
, common
);
1243 BlockDriverState
*bs
= state
->bs
;
1244 QEMUSnapshotInfo
*sn
= &state
->sn
;
1245 Error
*local_error
= NULL
;
1251 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1252 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1253 "device '%s' in abort: %s",
1256 bdrv_get_device_name(bs
),
1257 error_get_pretty(local_error
));
1258 error_free(local_error
);
1262 /* external snapshot private data */
1263 typedef struct ExternalSnapshotState
{
1264 BlkTransactionState common
;
1265 BlockDriverState
*old_bs
;
1266 BlockDriverState
*new_bs
;
1267 } ExternalSnapshotState
;
1269 static void external_snapshot_prepare(BlkTransactionState
*common
,
1274 QDict
*options
= NULL
;
1275 Error
*local_err
= NULL
;
1276 bool has_device
= false;
1278 bool has_node_name
= false;
1279 const char *node_name
;
1280 bool has_snapshot_node_name
= false;
1281 const char *snapshot_node_name
;
1282 const char *new_image_file
;
1283 const char *format
= "qcow2";
1284 enum NewImageMode mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1285 ExternalSnapshotState
*state
=
1286 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1287 TransactionAction
*action
= common
->action
;
1289 /* get parameters */
1290 g_assert(action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
);
1292 has_device
= action
->blockdev_snapshot_sync
->has_device
;
1293 device
= action
->blockdev_snapshot_sync
->device
;
1294 has_node_name
= action
->blockdev_snapshot_sync
->has_node_name
;
1295 node_name
= action
->blockdev_snapshot_sync
->node_name
;
1296 has_snapshot_node_name
=
1297 action
->blockdev_snapshot_sync
->has_snapshot_node_name
;
1298 snapshot_node_name
= action
->blockdev_snapshot_sync
->snapshot_node_name
;
1300 new_image_file
= action
->blockdev_snapshot_sync
->snapshot_file
;
1301 if (action
->blockdev_snapshot_sync
->has_format
) {
1302 format
= action
->blockdev_snapshot_sync
->format
;
1304 if (action
->blockdev_snapshot_sync
->has_mode
) {
1305 mode
= action
->blockdev_snapshot_sync
->mode
;
1308 /* start processing */
1309 drv
= bdrv_find_format(format
);
1311 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1315 state
->old_bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1316 has_node_name
? node_name
: NULL
,
1319 error_propagate(errp
, local_err
);
1323 if (has_node_name
&& !has_snapshot_node_name
) {
1324 error_setg(errp
, "New snapshot node name missing");
1328 if (has_snapshot_node_name
&& bdrv_find_node(snapshot_node_name
)) {
1329 error_setg(errp
, "New snapshot node name already existing");
1333 if (!bdrv_is_inserted(state
->old_bs
)) {
1334 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1338 if (bdrv_op_is_blocked(state
->old_bs
,
1339 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1343 if (!bdrv_is_read_only(state
->old_bs
)) {
1344 if (bdrv_flush(state
->old_bs
)) {
1345 error_set(errp
, QERR_IO_ERROR
);
1350 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1351 error_set(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1355 flags
= state
->old_bs
->open_flags
;
1357 /* create new image w/backing file */
1358 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1359 bdrv_img_create(new_image_file
, format
,
1360 state
->old_bs
->filename
,
1361 state
->old_bs
->drv
->format_name
,
1362 NULL
, -1, flags
, &local_err
, false);
1364 error_propagate(errp
, local_err
);
1369 if (has_snapshot_node_name
) {
1370 options
= qdict_new();
1371 qdict_put(options
, "node-name",
1372 qstring_from_str(snapshot_node_name
));
1375 /* TODO Inherit bs->options or only take explicit options with an
1376 * extended QMP command? */
1377 assert(state
->new_bs
== NULL
);
1378 ret
= bdrv_open(&state
->new_bs
, new_image_file
, NULL
, options
,
1379 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
1380 /* We will manually add the backing_hd field to the bs later */
1382 error_propagate(errp
, local_err
);
1386 static void external_snapshot_commit(BlkTransactionState
*common
)
1388 ExternalSnapshotState
*state
=
1389 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1391 /* This removes our old bs and adds the new bs */
1392 bdrv_append(state
->new_bs
, state
->old_bs
);
1393 /* We don't need (or want) to use the transactional
1394 * bdrv_reopen_multiple() across all the entries at once, because we
1395 * don't want to abort all of them if one of them fails the reopen */
1396 bdrv_reopen(state
->new_bs
, state
->new_bs
->open_flags
& ~BDRV_O_RDWR
,
1400 static void external_snapshot_abort(BlkTransactionState
*common
)
1402 ExternalSnapshotState
*state
=
1403 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1404 if (state
->new_bs
) {
1405 bdrv_unref(state
->new_bs
);
1409 typedef struct DriveBackupState
{
1410 BlkTransactionState common
;
1411 BlockDriverState
*bs
;
1415 static void drive_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1417 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1418 DriveBackup
*backup
;
1419 Error
*local_err
= NULL
;
1421 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1422 backup
= common
->action
->drive_backup
;
1424 qmp_drive_backup(backup
->device
, backup
->target
,
1425 backup
->has_format
, backup
->format
,
1427 backup
->has_mode
, backup
->mode
,
1428 backup
->has_speed
, backup
->speed
,
1429 backup
->has_on_source_error
, backup
->on_source_error
,
1430 backup
->has_on_target_error
, backup
->on_target_error
,
1433 error_propagate(errp
, local_err
);
1439 state
->bs
= bdrv_find(backup
->device
);
1440 state
->job
= state
->bs
->job
;
1443 static void drive_backup_abort(BlkTransactionState
*common
)
1445 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1446 BlockDriverState
*bs
= state
->bs
;
1448 /* Only cancel if it's the job we started */
1449 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1450 block_job_cancel_sync(bs
->job
);
1454 static void abort_prepare(BlkTransactionState
*common
, Error
**errp
)
1456 error_setg(errp
, "Transaction aborted using Abort action");
1459 static void abort_commit(BlkTransactionState
*common
)
1461 g_assert_not_reached(); /* this action never succeeds */
1464 static const BdrvActionOps actions
[] = {
1465 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
1466 .instance_size
= sizeof(ExternalSnapshotState
),
1467 .prepare
= external_snapshot_prepare
,
1468 .commit
= external_snapshot_commit
,
1469 .abort
= external_snapshot_abort
,
1471 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
1472 .instance_size
= sizeof(DriveBackupState
),
1473 .prepare
= drive_backup_prepare
,
1474 .abort
= drive_backup_abort
,
1476 [TRANSACTION_ACTION_KIND_ABORT
] = {
1477 .instance_size
= sizeof(BlkTransactionState
),
1478 .prepare
= abort_prepare
,
1479 .commit
= abort_commit
,
1481 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
1482 .instance_size
= sizeof(InternalSnapshotState
),
1483 .prepare
= internal_snapshot_prepare
,
1484 .abort
= internal_snapshot_abort
,
1489 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1490 * then we do not pivot any of the devices in the group, and abandon the
1493 void qmp_transaction(TransactionActionList
*dev_list
, Error
**errp
)
1495 TransactionActionList
*dev_entry
= dev_list
;
1496 BlkTransactionState
*state
, *next
;
1497 Error
*local_err
= NULL
;
1499 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkTransactionState
) snap_bdrv_states
;
1500 QSIMPLEQ_INIT(&snap_bdrv_states
);
1502 /* drain all i/o before any snapshots */
1505 /* We don't do anything in this loop that commits us to the snapshot */
1506 while (NULL
!= dev_entry
) {
1507 TransactionAction
*dev_info
= NULL
;
1508 const BdrvActionOps
*ops
;
1510 dev_info
= dev_entry
->value
;
1511 dev_entry
= dev_entry
->next
;
1513 assert(dev_info
->kind
< ARRAY_SIZE(actions
));
1515 ops
= &actions
[dev_info
->kind
];
1516 assert(ops
->instance_size
> 0);
1518 state
= g_malloc0(ops
->instance_size
);
1520 state
->action
= dev_info
;
1521 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
1523 state
->ops
->prepare(state
, &local_err
);
1525 error_propagate(errp
, local_err
);
1526 goto delete_and_fail
;
1530 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1531 if (state
->ops
->commit
) {
1532 state
->ops
->commit(state
);
1541 * failure, and it is all-or-none; abandon each new bs, and keep using
1542 * the original bs for all images
1544 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1545 if (state
->ops
->abort
) {
1546 state
->ops
->abort(state
);
1550 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
1551 if (state
->ops
->clean
) {
1552 state
->ops
->clean(state
);
1559 static void eject_device(BlockDriverState
*bs
, int force
, Error
**errp
)
1561 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
1564 if (!bdrv_dev_has_removable_media(bs
)) {
1565 error_setg(errp
, "Device '%s' is not removable",
1566 bdrv_get_device_name(bs
));
1570 if (bdrv_dev_is_medium_locked(bs
) && !bdrv_dev_is_tray_open(bs
)) {
1571 bdrv_dev_eject_request(bs
, force
);
1573 error_setg(errp
, "Device '%s' is locked",
1574 bdrv_get_device_name(bs
));
1582 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
1584 BlockDriverState
*bs
;
1586 bs
= bdrv_find(device
);
1588 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1592 eject_device(bs
, force
, errp
);
1595 void qmp_block_passwd(bool has_device
, const char *device
,
1596 bool has_node_name
, const char *node_name
,
1597 const char *password
, Error
**errp
)
1599 Error
*local_err
= NULL
;
1600 BlockDriverState
*bs
;
1603 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1604 has_node_name
? node_name
: NULL
,
1607 error_propagate(errp
, local_err
);
1611 err
= bdrv_set_key(bs
, password
);
1612 if (err
== -EINVAL
) {
1613 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1615 } else if (err
< 0) {
1616 error_set(errp
, QERR_INVALID_PASSWORD
);
1621 static void qmp_bdrv_open_encrypted(BlockDriverState
*bs
, const char *filename
,
1622 int bdrv_flags
, BlockDriver
*drv
,
1623 const char *password
, Error
**errp
)
1625 Error
*local_err
= NULL
;
1628 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, bdrv_flags
, drv
, &local_err
);
1630 error_propagate(errp
, local_err
);
1634 if (bdrv_key_required(bs
)) {
1636 if (bdrv_set_key(bs
, password
) < 0) {
1637 error_set(errp
, QERR_INVALID_PASSWORD
);
1640 error_set(errp
, QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
),
1641 bdrv_get_encrypted_filename(bs
));
1643 } else if (password
) {
1644 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1648 void qmp_change_blockdev(const char *device
, const char *filename
,
1649 const char *format
, Error
**errp
)
1651 BlockDriverState
*bs
;
1652 BlockDriver
*drv
= NULL
;
1656 bs
= bdrv_find(device
);
1658 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1663 drv
= bdrv_find_whitelisted_format(format
, bs
->read_only
);
1665 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1670 eject_device(bs
, 0, &err
);
1672 error_propagate(errp
, err
);
1676 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
1677 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
1679 qmp_bdrv_open_encrypted(bs
, filename
, bdrv_flags
, drv
, NULL
, errp
);
1682 /* throttling disk I/O limits */
1683 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
1690 bool has_bps_rd_max
,
1692 bool has_bps_wr_max
,
1696 bool has_iops_rd_max
,
1697 int64_t iops_rd_max
,
1698 bool has_iops_wr_max
,
1699 int64_t iops_wr_max
,
1701 int64_t iops_size
, Error
**errp
)
1704 BlockDriverState
*bs
;
1705 AioContext
*aio_context
;
1707 bs
= bdrv_find(device
);
1709 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1713 memset(&cfg
, 0, sizeof(cfg
));
1714 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
1715 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
1716 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
1718 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
1719 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
1720 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
1723 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
1725 if (has_bps_rd_max
) {
1726 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
1728 if (has_bps_wr_max
) {
1729 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
1732 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
1734 if (has_iops_rd_max
) {
1735 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
1737 if (has_iops_wr_max
) {
1738 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
1741 if (has_iops_size
) {
1742 cfg
.op_size
= iops_size
;
1745 if (!check_throttle_config(&cfg
, errp
)) {
1749 aio_context
= bdrv_get_aio_context(bs
);
1750 aio_context_acquire(aio_context
);
1752 if (!bs
->io_limits_enabled
&& throttle_enabled(&cfg
)) {
1753 bdrv_io_limits_enable(bs
);
1754 } else if (bs
->io_limits_enabled
&& !throttle_enabled(&cfg
)) {
1755 bdrv_io_limits_disable(bs
);
1758 if (bs
->io_limits_enabled
) {
1759 bdrv_set_io_limits(bs
, &cfg
);
1762 aio_context_release(aio_context
);
1765 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1767 const char *id
= qdict_get_str(qdict
, "id");
1768 BlockDriverState
*bs
;
1770 AioContext
*aio_context
;
1771 Error
*local_err
= NULL
;
1775 error_report("Device '%s' not found", id
);
1779 dinfo
= drive_get_by_blockdev(bs
);
1780 if (dinfo
&& !dinfo
->enable_auto_del
) {
1781 error_report("Deleting device added with blockdev-add"
1782 " is not supported");
1786 aio_context
= bdrv_get_aio_context(bs
);
1787 aio_context_acquire(aio_context
);
1789 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
1790 error_report("%s", error_get_pretty(local_err
));
1791 error_free(local_err
);
1792 aio_context_release(aio_context
);
1796 /* quiesce block driver; prevent further io */
1801 /* if we have a device attached to this BlockDriverState
1802 * then we need to make the drive anonymous until the device
1803 * can be removed. If this is a drive with no device backing
1804 * then we can just get rid of the block driver state right here.
1806 if (bdrv_get_attached_dev(bs
)) {
1809 /* Further I/O must not pause the guest */
1810 bdrv_set_on_error(bs
, BLOCKDEV_ON_ERROR_REPORT
,
1811 BLOCKDEV_ON_ERROR_REPORT
);
1816 aio_context_release(aio_context
);
1820 void qmp_block_resize(bool has_device
, const char *device
,
1821 bool has_node_name
, const char *node_name
,
1822 int64_t size
, Error
**errp
)
1824 Error
*local_err
= NULL
;
1825 BlockDriverState
*bs
;
1826 AioContext
*aio_context
;
1829 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1830 has_node_name
? node_name
: NULL
,
1833 error_propagate(errp
, local_err
);
1837 aio_context
= bdrv_get_aio_context(bs
);
1838 aio_context_acquire(aio_context
);
1840 if (!bdrv_is_first_non_filter(bs
)) {
1841 error_set(errp
, QERR_FEATURE_DISABLED
, "resize");
1846 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
1850 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
1851 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1855 /* complete all in-flight operations before resizing the device */
1858 ret
= bdrv_truncate(bs
, size
);
1863 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1866 error_set(errp
, QERR_UNSUPPORTED
);
1869 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1872 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1875 error_setg_errno(errp
, -ret
, "Could not resize");
1880 aio_context_release(aio_context
);
1883 static void block_job_cb(void *opaque
, int ret
)
1885 BlockDriverState
*bs
= opaque
;
1886 const char *msg
= NULL
;
1888 trace_block_job_cb(bs
, bs
->job
, ret
);
1893 msg
= strerror(-ret
);
1896 if (block_job_is_cancelled(bs
->job
)) {
1897 block_job_event_cancelled(bs
->job
);
1899 block_job_event_completed(bs
->job
, msg
);
1902 bdrv_put_ref_bh_schedule(bs
);
1905 void qmp_block_stream(const char *device
,
1906 bool has_base
, const char *base
,
1907 bool has_backing_file
, const char *backing_file
,
1908 bool has_speed
, int64_t speed
,
1909 bool has_on_error
, BlockdevOnError on_error
,
1912 BlockDriverState
*bs
;
1913 BlockDriverState
*base_bs
= NULL
;
1914 Error
*local_err
= NULL
;
1915 const char *base_name
= NULL
;
1917 if (!has_on_error
) {
1918 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1921 bs
= bdrv_find(device
);
1923 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1927 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
1932 base_bs
= bdrv_find_backing_image(bs
, base
);
1933 if (base_bs
== NULL
) {
1934 error_set(errp
, QERR_BASE_NOT_FOUND
, base
);
1940 /* if we are streaming the entire chain, the result will have no backing
1941 * file, and specifying one is therefore an error */
1942 if (base_bs
== NULL
&& has_backing_file
) {
1943 error_setg(errp
, "backing file specified, but streaming the "
1948 /* backing_file string overrides base bs filename */
1949 base_name
= has_backing_file
? backing_file
: base_name
;
1951 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
1952 on_error
, block_job_cb
, bs
, &local_err
);
1954 error_propagate(errp
, local_err
);
1958 trace_qmp_block_stream(bs
, bs
->job
);
1961 void qmp_block_commit(const char *device
,
1962 bool has_base
, const char *base
,
1963 bool has_top
, const char *top
,
1964 bool has_backing_file
, const char *backing_file
,
1965 bool has_speed
, int64_t speed
,
1968 BlockDriverState
*bs
;
1969 BlockDriverState
*base_bs
, *top_bs
;
1970 Error
*local_err
= NULL
;
1971 /* This will be part of the QMP command, if/when the
1972 * BlockdevOnError change for blkmirror makes it in
1974 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1980 /* drain all i/o before commits */
1984 * libvirt relies on the DeviceNotFound error class in order to probe for
1985 * live commit feature versions; for this to work, we must make sure to
1986 * perform the device lookup before any generic errors that may occur in a
1987 * scenario in which all optional arguments are omitted. */
1988 bs
= bdrv_find(device
);
1990 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1994 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT
, errp
)) {
1998 /* default top_bs is the active layer */
2001 if (has_top
&& top
) {
2002 if (strcmp(bs
->filename
, top
) != 0) {
2003 top_bs
= bdrv_find_backing_image(bs
, top
);
2007 if (top_bs
== NULL
) {
2008 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2012 if (has_base
&& base
) {
2013 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2015 base_bs
= bdrv_find_base(top_bs
);
2018 if (base_bs
== NULL
) {
2019 error_set(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2023 /* Do not allow attempts to commit an image into itself */
2024 if (top_bs
== base_bs
) {
2025 error_setg(errp
, "cannot commit an image into itself");
2030 if (has_backing_file
) {
2031 error_setg(errp
, "'backing-file' specified,"
2032 " but 'top' is the active layer");
2035 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
2038 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
2039 has_backing_file
? backing_file
: NULL
, &local_err
);
2041 if (local_err
!= NULL
) {
2042 error_propagate(errp
, local_err
);
2047 void qmp_drive_backup(const char *device
, const char *target
,
2048 bool has_format
, const char *format
,
2049 enum MirrorSyncMode sync
,
2050 bool has_mode
, enum NewImageMode mode
,
2051 bool has_speed
, int64_t speed
,
2052 bool has_on_source_error
, BlockdevOnError on_source_error
,
2053 bool has_on_target_error
, BlockdevOnError on_target_error
,
2056 BlockDriverState
*bs
;
2057 BlockDriverState
*target_bs
;
2058 BlockDriverState
*source
= NULL
;
2059 BlockDriver
*drv
= NULL
;
2060 Error
*local_err
= NULL
;
2068 if (!has_on_source_error
) {
2069 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2071 if (!has_on_target_error
) {
2072 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2075 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2078 bs
= bdrv_find(device
);
2080 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2084 if (!bdrv_is_inserted(bs
)) {
2085 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2090 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2093 drv
= bdrv_find_format(format
);
2095 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
2100 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
2104 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2106 /* See if we have a backing HD we can use to create our new image
2108 if (sync
== MIRROR_SYNC_MODE_TOP
) {
2109 source
= bs
->backing_hd
;
2111 sync
= MIRROR_SYNC_MODE_FULL
;
2114 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2118 size
= bdrv_getlength(bs
);
2120 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2124 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
2125 assert(format
&& drv
);
2127 bdrv_img_create(target
, format
, source
->filename
,
2128 source
->drv
->format_name
, NULL
,
2129 size
, flags
, &local_err
, false);
2131 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
2132 size
, flags
, &local_err
, false);
2137 error_propagate(errp
, local_err
);
2142 ret
= bdrv_open(&target_bs
, target
, NULL
, NULL
, flags
, drv
, &local_err
);
2144 error_propagate(errp
, local_err
);
2148 backup_start(bs
, target_bs
, speed
, sync
, on_source_error
, on_target_error
,
2149 block_job_cb
, bs
, &local_err
);
2150 if (local_err
!= NULL
) {
2151 bdrv_unref(target_bs
);
2152 error_propagate(errp
, local_err
);
2157 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
2159 return bdrv_named_nodes_list();
2162 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2164 void qmp_drive_mirror(const char *device
, const char *target
,
2165 bool has_format
, const char *format
,
2166 bool has_node_name
, const char *node_name
,
2167 bool has_replaces
, const char *replaces
,
2168 enum MirrorSyncMode sync
,
2169 bool has_mode
, enum NewImageMode mode
,
2170 bool has_speed
, int64_t speed
,
2171 bool has_granularity
, uint32_t granularity
,
2172 bool has_buf_size
, int64_t buf_size
,
2173 bool has_on_source_error
, BlockdevOnError on_source_error
,
2174 bool has_on_target_error
, BlockdevOnError on_target_error
,
2177 BlockDriverState
*bs
;
2178 BlockDriverState
*source
, *target_bs
;
2179 BlockDriver
*drv
= NULL
;
2180 Error
*local_err
= NULL
;
2181 QDict
*options
= NULL
;
2189 if (!has_on_source_error
) {
2190 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2192 if (!has_on_target_error
) {
2193 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2196 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2198 if (!has_granularity
) {
2201 if (!has_buf_size
) {
2202 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
2205 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
2206 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2207 "a value in range [512B, 64MB]");
2210 if (granularity
& (granularity
- 1)) {
2211 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity", "power of 2");
2215 bs
= bdrv_find(device
);
2217 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2221 if (!bdrv_is_inserted(bs
)) {
2222 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2227 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2230 drv
= bdrv_find_format(format
);
2232 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
2237 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR
, errp
)) {
2241 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2242 source
= bs
->backing_hd
;
2243 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
2244 sync
= MIRROR_SYNC_MODE_FULL
;
2246 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2250 size
= bdrv_getlength(bs
);
2252 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2257 BlockDriverState
*to_replace_bs
;
2259 if (!has_node_name
) {
2260 error_setg(errp
, "a node-name must be provided when replacing a"
2261 " named node of the graph");
2265 to_replace_bs
= check_to_replace_node(replaces
, &local_err
);
2267 if (!to_replace_bs
) {
2268 error_propagate(errp
, local_err
);
2272 if (size
!= bdrv_getlength(to_replace_bs
)) {
2273 error_setg(errp
, "cannot replace image with a mirror image of "
2279 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
2280 && mode
!= NEW_IMAGE_MODE_EXISTING
)
2282 /* create new image w/o backing file */
2283 assert(format
&& drv
);
2284 bdrv_img_create(target
, format
,
2285 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
2288 case NEW_IMAGE_MODE_EXISTING
:
2290 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
2291 /* create new image with backing file */
2292 bdrv_img_create(target
, format
,
2294 source
->drv
->format_name
,
2295 NULL
, size
, flags
, &local_err
, false);
2303 error_propagate(errp
, local_err
);
2307 if (has_node_name
) {
2308 options
= qdict_new();
2309 qdict_put(options
, "node-name", qstring_from_str(node_name
));
2312 /* Mirroring takes care of copy-on-write using the source's backing
2316 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
2317 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
2319 error_propagate(errp
, local_err
);
2323 /* pass the node name to replace to mirror start since it's loose coupling
2324 * and will allow to check whether the node still exist at mirror completion
2326 mirror_start(bs
, target_bs
,
2327 has_replaces
? replaces
: NULL
,
2328 speed
, granularity
, buf_size
, sync
,
2329 on_source_error
, on_target_error
,
2330 block_job_cb
, bs
, &local_err
);
2331 if (local_err
!= NULL
) {
2332 bdrv_unref(target_bs
);
2333 error_propagate(errp
, local_err
);
2338 static BlockJob
*find_block_job(const char *device
)
2340 BlockDriverState
*bs
;
2342 bs
= bdrv_find(device
);
2343 if (!bs
|| !bs
->job
) {
2349 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
2351 BlockJob
*job
= find_block_job(device
);
2354 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2358 block_job_set_speed(job
, speed
, errp
);
2361 void qmp_block_job_cancel(const char *device
,
2362 bool has_force
, bool force
, Error
**errp
)
2364 BlockJob
*job
= find_block_job(device
);
2371 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2374 if (job
->paused
&& !force
) {
2375 error_setg(errp
, "The block job for device '%s' is currently paused",
2380 trace_qmp_block_job_cancel(job
);
2381 block_job_cancel(job
);
2384 void qmp_block_job_pause(const char *device
, Error
**errp
)
2386 BlockJob
*job
= find_block_job(device
);
2389 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2393 trace_qmp_block_job_pause(job
);
2394 block_job_pause(job
);
2397 void qmp_block_job_resume(const char *device
, Error
**errp
)
2399 BlockJob
*job
= find_block_job(device
);
2402 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2406 trace_qmp_block_job_resume(job
);
2407 block_job_resume(job
);
2410 void qmp_block_job_complete(const char *device
, Error
**errp
)
2412 BlockJob
*job
= find_block_job(device
);
2415 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2419 trace_qmp_block_job_complete(job
);
2420 block_job_complete(job
, errp
);
2423 void qmp_change_backing_file(const char *device
,
2424 const char *image_node_name
,
2425 const char *backing_file
,
2428 BlockDriverState
*bs
= NULL
;
2429 BlockDriverState
*image_bs
= NULL
;
2430 Error
*local_err
= NULL
;
2435 /* find the top layer BDS of the chain */
2436 bs
= bdrv_find(device
);
2438 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2442 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
2444 error_propagate(errp
, local_err
);
2449 error_setg(errp
, "image file not found");
2453 if (bdrv_find_base(image_bs
) == image_bs
) {
2454 error_setg(errp
, "not allowing backing file change on an image "
2455 "without a backing file");
2459 /* even though we are not necessarily operating on bs, we need it to
2460 * determine if block ops are currently prohibited on the chain */
2461 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
2465 /* final sanity check */
2466 if (!bdrv_chain_contains(bs
, image_bs
)) {
2467 error_setg(errp
, "'%s' and image file are not in the same chain",
2472 /* if not r/w, reopen to make r/w */
2473 open_flags
= image_bs
->open_flags
;
2474 ro
= bdrv_is_read_only(image_bs
);
2477 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
2479 error_propagate(errp
, local_err
);
2484 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
2485 image_bs
->drv
? image_bs
->drv
->format_name
: "");
2488 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
2490 /* don't exit here, so we can try to restore open flags if
2495 bdrv_reopen(image_bs
, open_flags
, &local_err
);
2497 error_propagate(errp
, local_err
); /* will preserve prior errp */
2502 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
2504 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
2508 Error
*local_err
= NULL
;
2510 /* Require an ID in the top level */
2511 if (!options
->has_id
) {
2512 error_setg(errp
, "Block device needs an ID");
2516 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
2517 * cache.direct=false instead of silently switching to aio=threads, except
2518 * when called from drive_new().
2520 * For now, simply forbidding the combination for all drivers will do. */
2521 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
2522 bool direct
= options
->has_cache
&&
2523 options
->cache
->has_direct
&&
2524 options
->cache
->direct
;
2526 error_setg(errp
, "aio=native requires cache.direct=true");
2531 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
2532 &options
, NULL
, &local_err
);
2534 error_propagate(errp
, local_err
);
2538 obj
= qmp_output_get_qobject(ov
);
2539 qdict
= qobject_to_qdict(obj
);
2541 qdict_flatten(qdict
);
2543 dinfo
= blockdev_init(NULL
, qdict
, &local_err
);
2545 error_propagate(errp
, local_err
);
2549 if (bdrv_key_required(dinfo
->bdrv
)) {
2551 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
2556 qmp_output_visitor_cleanup(ov
);
2559 static void do_qmp_query_block_jobs_one(void *opaque
, BlockDriverState
*bs
)
2561 BlockJobInfoList
**prev
= opaque
;
2562 BlockJob
*job
= bs
->job
;
2565 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
2566 elem
->value
= block_job_query(bs
->job
);
2567 (*prev
)->next
= elem
;
2572 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
2574 /* Dummy is a fake list element for holding the head pointer */
2575 BlockJobInfoList dummy
= {};
2576 BlockJobInfoList
*prev
= &dummy
;
2577 bdrv_iterate(do_qmp_query_block_jobs_one
, &prev
);
2581 QemuOptsList qemu_common_drive_opts
= {
2583 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
2587 .type
= QEMU_OPT_BOOL
,
2588 .help
= "enable/disable snapshot mode",
2591 .type
= QEMU_OPT_STRING
,
2592 .help
= "discard operation (ignore/off, unmap/on)",
2594 .name
= "cache.writeback",
2595 .type
= QEMU_OPT_BOOL
,
2596 .help
= "enables writeback mode for any caches",
2598 .name
= "cache.direct",
2599 .type
= QEMU_OPT_BOOL
,
2600 .help
= "enables use of O_DIRECT (bypass the host page cache)",
2602 .name
= "cache.no-flush",
2603 .type
= QEMU_OPT_BOOL
,
2604 .help
= "ignore any flush requests for the device",
2607 .type
= QEMU_OPT_STRING
,
2608 .help
= "host AIO implementation (threads, native)",
2611 .type
= QEMU_OPT_STRING
,
2612 .help
= "disk format (raw, qcow2, ...)",
2615 .type
= QEMU_OPT_STRING
,
2616 .help
= "read error action",
2619 .type
= QEMU_OPT_STRING
,
2620 .help
= "write error action",
2622 .name
= "read-only",
2623 .type
= QEMU_OPT_BOOL
,
2624 .help
= "open drive file as read-only",
2626 .name
= "throttling.iops-total",
2627 .type
= QEMU_OPT_NUMBER
,
2628 .help
= "limit total I/O operations per second",
2630 .name
= "throttling.iops-read",
2631 .type
= QEMU_OPT_NUMBER
,
2632 .help
= "limit read operations per second",
2634 .name
= "throttling.iops-write",
2635 .type
= QEMU_OPT_NUMBER
,
2636 .help
= "limit write operations per second",
2638 .name
= "throttling.bps-total",
2639 .type
= QEMU_OPT_NUMBER
,
2640 .help
= "limit total bytes per second",
2642 .name
= "throttling.bps-read",
2643 .type
= QEMU_OPT_NUMBER
,
2644 .help
= "limit read bytes per second",
2646 .name
= "throttling.bps-write",
2647 .type
= QEMU_OPT_NUMBER
,
2648 .help
= "limit write bytes per second",
2650 .name
= "throttling.iops-total-max",
2651 .type
= QEMU_OPT_NUMBER
,
2652 .help
= "I/O operations burst",
2654 .name
= "throttling.iops-read-max",
2655 .type
= QEMU_OPT_NUMBER
,
2656 .help
= "I/O operations read burst",
2658 .name
= "throttling.iops-write-max",
2659 .type
= QEMU_OPT_NUMBER
,
2660 .help
= "I/O operations write burst",
2662 .name
= "throttling.bps-total-max",
2663 .type
= QEMU_OPT_NUMBER
,
2664 .help
= "total bytes burst",
2666 .name
= "throttling.bps-read-max",
2667 .type
= QEMU_OPT_NUMBER
,
2668 .help
= "total bytes read burst",
2670 .name
= "throttling.bps-write-max",
2671 .type
= QEMU_OPT_NUMBER
,
2672 .help
= "total bytes write burst",
2674 .name
= "throttling.iops-size",
2675 .type
= QEMU_OPT_NUMBER
,
2676 .help
= "when limiting by iops max size of an I/O in bytes",
2678 .name
= "copy-on-read",
2679 .type
= QEMU_OPT_BOOL
,
2680 .help
= "copy read data from backing file into image file",
2682 .name
= "detect-zeroes",
2683 .type
= QEMU_OPT_STRING
,
2684 .help
= "try to optimize zero writes (off, on, unmap)",
2686 { /* end of list */ }
2690 QemuOptsList qemu_drive_opts
= {
2692 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
2695 * no elements => accept any params
2696 * validation will happen later
2698 { /* end of list */ }