2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
9 * This file incorporates work covered by the following copyright and
12 * Copyright (c) 2003-2008 Fabrice Bellard
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "sysemu/block-backend.h"
34 #include "sysemu/blockdev.h"
35 #include "hw/block/block.h"
36 #include "block/blockjob.h"
37 #include "monitor/monitor.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40 #include "qapi/qmp/types.h"
41 #include "qapi-visit.h"
42 #include "qapi/qmp-output-visitor.h"
43 #include "qapi/util.h"
44 #include "sysemu/sysemu.h"
45 #include "block/block_int.h"
46 #include "qmp-commands.h"
48 #include "sysemu/arch_init.h"
50 static QTAILQ_HEAD(drivelist
, DriveInfo
) drives
= QTAILQ_HEAD_INITIALIZER(drives
);
52 static const char *const if_name
[IF_COUNT
] = {
56 [IF_FLOPPY
] = "floppy",
57 [IF_PFLASH
] = "pflash",
60 [IF_VIRTIO
] = "virtio",
64 static int if_max_devs
[IF_COUNT
] = {
66 * Do not change these numbers! They govern how drive option
67 * index maps to unit and bus. That mapping is ABI.
69 * All controllers used to imlement if=T drives need to support
70 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
71 * Otherwise, some index values map to "impossible" bus, unit
74 * For instance, if you change [IF_SCSI] to 255, -drive
75 * if=scsi,index=12 no longer means bus=1,unit=5, but
76 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
77 * the drive can't be set up. Regression.
84 * Boards may call this to offer board-by-board overrides
85 * of the default, global values.
87 void override_max_devs(BlockInterfaceType type
, int max_devs
)
95 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
96 if (dinfo
->type
== type
) {
97 fprintf(stderr
, "Cannot override units-per-bus property of"
98 " the %s interface, because a drive of that type has"
99 " already been added.\n", if_name
[type
]);
100 g_assert_not_reached();
104 if_max_devs
[type
] = max_devs
;
108 * We automatically delete the drive when a device using it gets
109 * unplugged. Questionable feature, but we can't just drop it.
110 * Device models call blockdev_mark_auto_del() to schedule the
111 * automatic deletion, and generic qdev code calls blockdev_auto_del()
112 * when deletion is actually safe.
114 void blockdev_mark_auto_del(BlockDriverState
*bs
)
116 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
118 if (dinfo
&& !dinfo
->enable_auto_del
) {
123 block_job_cancel(bs
->job
);
130 void blockdev_auto_del(BlockDriverState
*bs
)
132 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
134 if (dinfo
&& dinfo
->auto_del
) {
140 * Returns the current mapping of how many units per bus
141 * a particular interface can support.
143 * A positive integer indicates n units per bus.
144 * 0 implies the mapping has not been established.
145 * -1 indicates an invalid BlockInterfaceType was given.
147 int drive_get_max_devs(BlockInterfaceType type
)
149 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
150 return if_max_devs
[type
];
156 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
158 int max_devs
= if_max_devs
[type
];
159 return max_devs
? index
/ max_devs
: 0;
162 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
164 int max_devs
= if_max_devs
[type
];
165 return max_devs
? index
% max_devs
: index
;
168 QemuOpts
*drive_def(const char *optstr
)
170 return qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
173 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
179 opts
= drive_def(optstr
);
183 if (type
!= IF_DEFAULT
) {
184 qemu_opt_set(opts
, "if", if_name
[type
]);
187 snprintf(buf
, sizeof(buf
), "%d", index
);
188 qemu_opt_set(opts
, "index", buf
);
191 qemu_opt_set(opts
, "file", file
);
195 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
199 /* seek interface, bus and unit */
201 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
202 if (dinfo
->type
== type
&&
211 bool drive_check_orphaned(void)
216 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
217 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
218 /* Unless this is a default drive, this may be an oversight. */
219 if (!dinfo
->bdrv
->dev
&& !dinfo
->is_default
&&
220 dinfo
->type
!= IF_NONE
) {
221 fprintf(stderr
, "Warning: Orphaned drive without device: "
222 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
223 dinfo
->id
, dinfo
->bdrv
->filename
, if_name
[dinfo
->type
],
224 dinfo
->bus
, dinfo
->unit
);
232 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
234 return drive_get(type
,
235 drive_index_to_bus_id(type
, index
),
236 drive_index_to_unit_id(type
, index
));
239 int drive_get_max_bus(BlockInterfaceType type
)
245 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
246 if(dinfo
->type
== type
&&
247 dinfo
->bus
> max_bus
)
248 max_bus
= dinfo
->bus
;
253 /* Get a block device. This should only be used for single-drive devices
254 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
256 DriveInfo
*drive_get_next(BlockInterfaceType type
)
258 static int next_block_unit
[IF_COUNT
];
260 return drive_get(type
, 0, next_block_unit
[type
]++);
263 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
267 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
268 if (dinfo
->bdrv
== bs
) {
275 static void bdrv_format_print(void *opaque
, const char *name
)
277 error_printf(" %s", name
);
280 void drive_del(DriveInfo
*dinfo
)
282 BlockBackend
*blk
= blk_by_name(dinfo
->id
);
284 bdrv_unref(dinfo
->bdrv
);
288 void drive_info_del(DriveInfo
*dinfo
)
293 qemu_opts_del(dinfo
->opts
);
295 QTAILQ_REMOVE(&drives
, dinfo
, next
);
296 g_free(dinfo
->serial
);
302 BlockDriverState
*bs
;
305 static void bdrv_put_ref_bh(void *opaque
)
307 BDRVPutRefBH
*s
= opaque
;
310 qemu_bh_delete(s
->bh
);
315 * Release a BDS reference in a BH
317 * It is not safe to use bdrv_unref() from a callback function when the callers
318 * still need the BlockDriverState. In such cases we schedule a BH to release
321 static void bdrv_put_ref_bh_schedule(BlockDriverState
*bs
)
325 s
= g_new(BDRVPutRefBH
, 1);
326 s
->bh
= qemu_bh_new(bdrv_put_ref_bh
, s
);
328 qemu_bh_schedule(s
->bh
);
331 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
333 if (!strcmp(buf
, "ignore")) {
334 return BLOCKDEV_ON_ERROR_IGNORE
;
335 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
336 return BLOCKDEV_ON_ERROR_ENOSPC
;
337 } else if (!strcmp(buf
, "stop")) {
338 return BLOCKDEV_ON_ERROR_STOP
;
339 } else if (!strcmp(buf
, "report")) {
340 return BLOCKDEV_ON_ERROR_REPORT
;
342 error_setg(errp
, "'%s' invalid %s error action",
343 buf
, is_read
? "read" : "write");
348 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
350 if (throttle_conflicting(cfg
)) {
351 error_setg(errp
, "bps/iops/max total values and read/write values"
352 " cannot be used at the same time");
356 if (!throttle_is_valid(cfg
)) {
357 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
364 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
366 /* Takes the ownership of bs_opts */
367 static DriveInfo
*blockdev_init(const char *file
, QDict
*bs_opts
,
373 int on_read_error
, on_write_error
;
375 BlockDriverState
*bs
;
384 bool has_driver_specific_opts
;
385 BlockdevDetectZeroesOptions detect_zeroes
;
386 BlockDriver
*drv
= NULL
;
388 /* Check common options by copying from bs_opts to opts, all other options
389 * stay in bs_opts for processing by bdrv_open(). */
390 id
= qdict_get_try_str(bs_opts
, "id");
391 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
393 error_propagate(errp
, error
);
397 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
399 error_propagate(errp
, error
);
404 qdict_del(bs_opts
, "id");
407 has_driver_specific_opts
= !!qdict_size(bs_opts
);
409 /* extract parameters */
410 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
411 ro
= qemu_opt_get_bool(opts
, "read-only", 0);
412 copy_on_read
= qemu_opt_get_bool(opts
, "copy-on-read", false);
414 if ((buf
= qemu_opt_get(opts
, "discard")) != NULL
) {
415 if (bdrv_parse_discard_flags(buf
, &bdrv_flags
) != 0) {
416 error_setg(errp
, "invalid discard option");
421 if (qemu_opt_get_bool(opts
, "cache.writeback", true)) {
422 bdrv_flags
|= BDRV_O_CACHE_WB
;
424 if (qemu_opt_get_bool(opts
, "cache.direct", false)) {
425 bdrv_flags
|= BDRV_O_NOCACHE
;
427 if (qemu_opt_get_bool(opts
, "cache.no-flush", false)) {
428 bdrv_flags
|= BDRV_O_NO_FLUSH
;
431 #ifdef CONFIG_LINUX_AIO
432 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
433 if (!strcmp(buf
, "native")) {
434 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
435 } else if (!strcmp(buf
, "threads")) {
436 /* this is the default */
438 error_setg(errp
, "invalid aio option");
444 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
445 if (is_help_option(buf
)) {
446 error_printf("Supported formats:");
447 bdrv_iterate_format(bdrv_format_print
, NULL
);
452 drv
= bdrv_find_format(buf
);
454 error_setg(errp
, "'%s' invalid format", buf
);
459 /* disk I/O throttling */
460 memset(&cfg
, 0, sizeof(cfg
));
461 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
=
462 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
463 cfg
.buckets
[THROTTLE_BPS_READ
].avg
=
464 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
465 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
=
466 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
467 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
=
468 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
469 cfg
.buckets
[THROTTLE_OPS_READ
].avg
=
470 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
471 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
=
472 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
474 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
=
475 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
476 cfg
.buckets
[THROTTLE_BPS_READ
].max
=
477 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
478 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
=
479 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
480 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
=
481 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
482 cfg
.buckets
[THROTTLE_OPS_READ
].max
=
483 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
484 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
=
485 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
487 cfg
.op_size
= qemu_opt_get_number(opts
, "throttling.iops-size", 0);
489 if (!check_throttle_config(&cfg
, &error
)) {
490 error_propagate(errp
, error
);
494 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
495 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
496 on_write_error
= parse_block_error_action(buf
, 0, &error
);
498 error_propagate(errp
, error
);
503 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
504 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
505 on_read_error
= parse_block_error_action(buf
, 1, &error
);
507 error_propagate(errp
, error
);
513 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
514 qemu_opt_get(opts
, "detect-zeroes"),
515 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX
,
516 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
519 error_propagate(errp
, error
);
523 if (detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
524 !(bdrv_flags
& BDRV_O_UNMAP
)) {
525 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
526 "without setting discard operation to unmap");
531 blk
= blk_new(qemu_opts_id(opts
), errp
);
535 bs
= bdrv_new_root(qemu_opts_id(opts
), errp
);
539 bs
->open_flags
= snapshot
? BDRV_O_SNAPSHOT
: 0;
541 bs
->detect_zeroes
= detect_zeroes
;
543 bdrv_set_on_error(bs
, on_read_error
, on_write_error
);
545 /* disk I/O throttling */
546 if (throttle_enabled(&cfg
)) {
547 bdrv_io_limits_enable(bs
);
548 bdrv_set_io_limits(bs
, &cfg
);
551 dinfo
= g_malloc0(sizeof(*dinfo
));
552 dinfo
->id
= g_strdup(qemu_opts_id(opts
));
554 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
556 if (!file
|| !*file
) {
557 if (has_driver_specific_opts
) {
566 /* always use cache=unsafe with snapshot */
567 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
568 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
572 bdrv_flags
|= BDRV_O_COPY_ON_READ
;
575 if (runstate_check(RUN_STATE_INMIGRATE
)) {
576 bdrv_flags
|= BDRV_O_INCOMING
;
579 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
582 ret
= bdrv_open(&bs
, file
, NULL
, bs_opts
, bdrv_flags
, drv
, &error
);
583 assert(bs
== dinfo
->bdrv
);
586 error_setg(errp
, "could not open disk image %s: %s",
587 file
?: dinfo
->id
, error_get_pretty(error
));
592 if (bdrv_key_required(bs
)) {
612 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
617 value
= qemu_opt_get(opts
, from
);
619 if (qemu_opt_find(opts
, to
)) {
620 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
621 "same time", to
, from
);
626 /* rename all items in opts */
627 while ((value
= qemu_opt_get(opts
, from
))) {
628 qemu_opt_set(opts
, to
, value
);
629 qemu_opt_unset(opts
, from
);
633 QemuOptsList qemu_legacy_drive_opts
= {
635 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
639 .type
= QEMU_OPT_NUMBER
,
640 .help
= "bus number",
643 .type
= QEMU_OPT_NUMBER
,
644 .help
= "unit number (i.e. lun for scsi)",
647 .type
= QEMU_OPT_NUMBER
,
648 .help
= "index number",
651 .type
= QEMU_OPT_STRING
,
652 .help
= "media type (disk, cdrom)",
655 .type
= QEMU_OPT_STRING
,
656 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
659 .type
= QEMU_OPT_NUMBER
,
660 .help
= "number of cylinders (ide disk geometry)",
663 .type
= QEMU_OPT_NUMBER
,
664 .help
= "number of heads (ide disk geometry)",
667 .type
= QEMU_OPT_NUMBER
,
668 .help
= "number of sectors (ide disk geometry)",
671 .type
= QEMU_OPT_STRING
,
672 .help
= "chs translation (auto, lba, none)",
675 .type
= QEMU_OPT_BOOL
,
676 .help
= "(deprecated, ignored)",
679 .type
= QEMU_OPT_STRING
,
680 .help
= "pci address (virtio only)",
683 .type
= QEMU_OPT_STRING
,
684 .help
= "disk serial number",
687 .type
= QEMU_OPT_STRING
,
691 /* Options that are passed on, but have special semantics with -drive */
694 .type
= QEMU_OPT_BOOL
,
695 .help
= "open drive file as read-only",
698 .type
= QEMU_OPT_STRING
,
699 .help
= "read error action",
702 .type
= QEMU_OPT_STRING
,
703 .help
= "write error action",
705 .name
= "copy-on-read",
706 .type
= QEMU_OPT_BOOL
,
707 .help
= "copy read data from backing file into image file",
710 { /* end of list */ }
714 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
717 DriveInfo
*dinfo
= NULL
;
719 QemuOpts
*legacy_opts
;
720 DriveMediaType media
= MEDIA_DISK
;
721 BlockInterfaceType type
;
722 int cyls
, heads
, secs
, translation
;
723 int max_devs
, bus_id
, unit_id
, index
;
725 const char *werror
, *rerror
;
726 bool read_only
= false;
729 const char *filename
;
730 Error
*local_err
= NULL
;
733 /* Change legacy command line options into QMP ones */
734 static const struct {
738 { "iops", "throttling.iops-total" },
739 { "iops_rd", "throttling.iops-read" },
740 { "iops_wr", "throttling.iops-write" },
742 { "bps", "throttling.bps-total" },
743 { "bps_rd", "throttling.bps-read" },
744 { "bps_wr", "throttling.bps-write" },
746 { "iops_max", "throttling.iops-total-max" },
747 { "iops_rd_max", "throttling.iops-read-max" },
748 { "iops_wr_max", "throttling.iops-write-max" },
750 { "bps_max", "throttling.bps-total-max" },
751 { "bps_rd_max", "throttling.bps-read-max" },
752 { "bps_wr_max", "throttling.bps-write-max" },
754 { "iops_size", "throttling.iops-size" },
756 { "readonly", "read-only" },
759 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
760 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
763 error_report("%s", error_get_pretty(local_err
));
764 error_free(local_err
);
769 value
= qemu_opt_get(all_opts
, "cache");
773 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
774 error_report("invalid cache option");
778 /* Specific options take precedence */
779 if (!qemu_opt_get(all_opts
, "cache.writeback")) {
780 qemu_opt_set_bool(all_opts
, "cache.writeback",
781 !!(flags
& BDRV_O_CACHE_WB
));
783 if (!qemu_opt_get(all_opts
, "cache.direct")) {
784 qemu_opt_set_bool(all_opts
, "cache.direct",
785 !!(flags
& BDRV_O_NOCACHE
));
787 if (!qemu_opt_get(all_opts
, "cache.no-flush")) {
788 qemu_opt_set_bool(all_opts
, "cache.no-flush",
789 !!(flags
& BDRV_O_NO_FLUSH
));
791 qemu_opt_unset(all_opts
, "cache");
794 /* Get a QDict for processing the options */
795 bs_opts
= qdict_new();
796 qemu_opts_to_qdict(all_opts
, bs_opts
);
798 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
800 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
802 error_report("%s", error_get_pretty(local_err
));
803 error_free(local_err
);
807 /* Deprecated option boot=[on|off] */
808 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
809 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
810 "ignored. Future versions will reject this parameter. Please "
811 "update your scripts.\n");
815 value
= qemu_opt_get(legacy_opts
, "media");
817 if (!strcmp(value
, "disk")) {
819 } else if (!strcmp(value
, "cdrom")) {
823 error_report("'%s' invalid media", value
);
828 /* copy-on-read is disabled with a warning for read-only devices */
829 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
830 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
832 if (read_only
&& copy_on_read
) {
833 error_report("warning: disabling copy-on-read on read-only drive");
834 copy_on_read
= false;
837 qdict_put(bs_opts
, "read-only",
838 qstring_from_str(read_only
? "on" : "off"));
839 qdict_put(bs_opts
, "copy-on-read",
840 qstring_from_str(copy_on_read
? "on" :"off"));
842 /* Controller type */
843 value
= qemu_opt_get(legacy_opts
, "if");
846 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
849 if (type
== IF_COUNT
) {
850 error_report("unsupported bus type '%s'", value
);
854 type
= block_default_type
;
858 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
859 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
860 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
862 if (cyls
|| heads
|| secs
) {
864 error_report("invalid physical cyls number");
868 error_report("invalid physical heads number");
872 error_report("invalid physical secs number");
877 translation
= BIOS_ATA_TRANSLATION_AUTO
;
878 value
= qemu_opt_get(legacy_opts
, "trans");
881 error_report("'%s' trans must be used with cyls, heads and secs",
885 if (!strcmp(value
, "none")) {
886 translation
= BIOS_ATA_TRANSLATION_NONE
;
887 } else if (!strcmp(value
, "lba")) {
888 translation
= BIOS_ATA_TRANSLATION_LBA
;
889 } else if (!strcmp(value
, "large")) {
890 translation
= BIOS_ATA_TRANSLATION_LARGE
;
891 } else if (!strcmp(value
, "rechs")) {
892 translation
= BIOS_ATA_TRANSLATION_RECHS
;
893 } else if (!strcmp(value
, "auto")) {
894 translation
= BIOS_ATA_TRANSLATION_AUTO
;
896 error_report("'%s' invalid translation type", value
);
901 if (media
== MEDIA_CDROM
) {
902 if (cyls
|| secs
|| heads
) {
903 error_report("CHS can't be set with media=cdrom");
908 /* Device address specified by bus/unit or index.
909 * If none was specified, try to find the first free one. */
910 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
911 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
912 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
914 max_devs
= if_max_devs
[type
];
917 if (bus_id
!= 0 || unit_id
!= -1) {
918 error_report("index cannot be used with bus and unit");
921 bus_id
= drive_index_to_bus_id(type
, index
);
922 unit_id
= drive_index_to_unit_id(type
, index
);
927 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
929 if (max_devs
&& unit_id
>= max_devs
) {
936 if (max_devs
&& unit_id
>= max_devs
) {
937 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
941 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
942 error_report("drive with bus=%d, unit=%d (index=%d) exists",
943 bus_id
, unit_id
, index
);
948 serial
= qemu_opt_get(legacy_opts
, "serial");
950 /* no id supplied -> create one */
951 if (qemu_opts_id(all_opts
) == NULL
) {
953 const char *mediastr
= "";
954 if (type
== IF_IDE
|| type
== IF_SCSI
) {
955 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
958 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
961 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
964 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
968 /* Add virtio block device */
969 devaddr
= qemu_opt_get(legacy_opts
, "addr");
970 if (devaddr
&& type
!= IF_VIRTIO
) {
971 error_report("addr is not supported by this bus type");
975 if (type
== IF_VIRTIO
) {
977 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
979 if (arch_type
== QEMU_ARCH_S390X
) {
980 qemu_opt_set(devopts
, "driver", "virtio-blk-s390");
982 qemu_opt_set(devopts
, "driver", "virtio-blk-pci");
984 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"));
986 qemu_opt_set(devopts
, "addr", devaddr
);
990 filename
= qemu_opt_get(legacy_opts
, "file");
992 /* Check werror/rerror compatibility with if=... */
993 werror
= qemu_opt_get(legacy_opts
, "werror");
994 if (werror
!= NULL
) {
995 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
997 error_report("werror is not supported by this bus type");
1000 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1003 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1004 if (rerror
!= NULL
) {
1005 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1007 error_report("rerror is not supported by this bus type");
1010 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1013 /* Actual block device init: Functionality shared with blockdev-add */
1014 dinfo
= blockdev_init(filename
, bs_opts
, &local_err
);
1016 if (dinfo
== NULL
) {
1018 error_report("%s", error_get_pretty(local_err
));
1019 error_free(local_err
);
1026 /* Set legacy DriveInfo fields */
1027 dinfo
->enable_auto_del
= true;
1028 dinfo
->opts
= all_opts
;
1031 dinfo
->heads
= heads
;
1033 dinfo
->trans
= translation
;
1036 dinfo
->bus
= bus_id
;
1037 dinfo
->unit
= unit_id
;
1038 dinfo
->devaddr
= devaddr
;
1040 dinfo
->serial
= g_strdup(serial
);
1047 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1054 qemu_opts_del(legacy_opts
);
1059 void do_commit(Monitor
*mon
, const QDict
*qdict
)
1061 const char *device
= qdict_get_str(qdict
, "device");
1062 BlockDriverState
*bs
;
1065 if (!strcmp(device
, "all")) {
1066 ret
= bdrv_commit_all();
1068 bs
= bdrv_find(device
);
1070 monitor_printf(mon
, "Device '%s' not found\n", device
);
1073 ret
= bdrv_commit(bs
);
1076 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1081 static void blockdev_do_action(int kind
, void *data
, Error
**errp
)
1083 TransactionAction action
;
1084 TransactionActionList list
;
1088 list
.value
= &action
;
1090 qmp_transaction(&list
, errp
);
1093 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1094 bool has_node_name
, const char *node_name
,
1095 const char *snapshot_file
,
1096 bool has_snapshot_node_name
,
1097 const char *snapshot_node_name
,
1098 bool has_format
, const char *format
,
1099 bool has_mode
, NewImageMode mode
, Error
**errp
)
1101 BlockdevSnapshot snapshot
= {
1102 .has_device
= has_device
,
1103 .device
= (char *) device
,
1104 .has_node_name
= has_node_name
,
1105 .node_name
= (char *) node_name
,
1106 .snapshot_file
= (char *) snapshot_file
,
1107 .has_snapshot_node_name
= has_snapshot_node_name
,
1108 .snapshot_node_name
= (char *) snapshot_node_name
,
1109 .has_format
= has_format
,
1110 .format
= (char *) format
,
1111 .has_mode
= has_mode
,
1114 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1118 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1122 BlockdevSnapshotInternal snapshot
= {
1123 .device
= (char *) device
,
1124 .name
= (char *) name
1127 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1131 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1138 BlockDriverState
*bs
= bdrv_find(device
);
1139 QEMUSnapshotInfo sn
;
1140 Error
*local_err
= NULL
;
1141 SnapshotInfo
*info
= NULL
;
1145 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1158 error_setg(errp
, "Name or id must be provided");
1162 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1164 error_propagate(errp
, local_err
);
1169 "Snapshot with id '%s' and name '%s' does not exist on "
1171 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1175 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1177 error_propagate(errp
, local_err
);
1181 info
= g_new0(SnapshotInfo
, 1);
1182 info
->id
= g_strdup(sn
.id_str
);
1183 info
->name
= g_strdup(sn
.name
);
1184 info
->date_nsec
= sn
.date_nsec
;
1185 info
->date_sec
= sn
.date_sec
;
1186 info
->vm_state_size
= sn
.vm_state_size
;
1187 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1188 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1193 /* New and old BlockDriverState structs for group snapshots */
1195 typedef struct BlkTransactionState BlkTransactionState
;
1197 /* Only prepare() may fail. In a single transaction, only one of commit() or
1198 abort() will be called, clean() will always be called if it present. */
1199 typedef struct BdrvActionOps
{
1200 /* Size of state struct, in bytes. */
1201 size_t instance_size
;
1202 /* Prepare the work, must NOT be NULL. */
1203 void (*prepare
)(BlkTransactionState
*common
, Error
**errp
);
1204 /* Commit the changes, can be NULL. */
1205 void (*commit
)(BlkTransactionState
*common
);
1206 /* Abort the changes on fail, can be NULL. */
1207 void (*abort
)(BlkTransactionState
*common
);
1208 /* Clean up resource in the end, can be NULL. */
1209 void (*clean
)(BlkTransactionState
*common
);
1213 * This structure must be arranged as first member in child type, assuming
1214 * that compiler will also arrange it to the same address with parent instance.
1215 * Later it will be used in free().
1217 struct BlkTransactionState
{
1218 TransactionAction
*action
;
1219 const BdrvActionOps
*ops
;
1220 QSIMPLEQ_ENTRY(BlkTransactionState
) entry
;
1223 /* internal snapshot private data */
1224 typedef struct InternalSnapshotState
{
1225 BlkTransactionState common
;
1226 BlockDriverState
*bs
;
1227 QEMUSnapshotInfo sn
;
1228 } InternalSnapshotState
;
1230 static void internal_snapshot_prepare(BlkTransactionState
*common
,
1233 Error
*local_err
= NULL
;
1236 BlockDriverState
*bs
;
1237 QEMUSnapshotInfo old_sn
, *sn
;
1240 BlockdevSnapshotInternal
*internal
;
1241 InternalSnapshotState
*state
;
1244 g_assert(common
->action
->kind
==
1245 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1246 internal
= common
->action
->blockdev_snapshot_internal_sync
;
1247 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1249 /* 1. parse input */
1250 device
= internal
->device
;
1251 name
= internal
->name
;
1253 /* 2. check for validation */
1254 bs
= bdrv_find(device
);
1256 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1260 if (!bdrv_is_inserted(bs
)) {
1261 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1265 if (bdrv_is_read_only(bs
)) {
1266 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1270 if (!bdrv_can_snapshot(bs
)) {
1271 error_set(errp
, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
1272 bs
->drv
->format_name
, device
, "internal snapshot");
1276 if (!strlen(name
)) {
1277 error_setg(errp
, "Name is empty");
1281 /* check whether a snapshot with name exist */
1282 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1285 error_propagate(errp
, local_err
);
1289 "Snapshot with name '%s' already exists on device '%s'",
1294 /* 3. take the snapshot */
1296 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1297 qemu_gettimeofday(&tv
);
1298 sn
->date_sec
= tv
.tv_sec
;
1299 sn
->date_nsec
= tv
.tv_usec
* 1000;
1300 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1302 ret1
= bdrv_snapshot_create(bs
, sn
);
1304 error_setg_errno(errp
, -ret1
,
1305 "Failed to create snapshot '%s' on device '%s'",
1310 /* 4. succeed, mark a snapshot is created */
1314 static void internal_snapshot_abort(BlkTransactionState
*common
)
1316 InternalSnapshotState
*state
=
1317 DO_UPCAST(InternalSnapshotState
, common
, common
);
1318 BlockDriverState
*bs
= state
->bs
;
1319 QEMUSnapshotInfo
*sn
= &state
->sn
;
1320 Error
*local_error
= NULL
;
1326 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1327 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1328 "device '%s' in abort: %s",
1331 bdrv_get_device_name(bs
),
1332 error_get_pretty(local_error
));
1333 error_free(local_error
);
1337 /* external snapshot private data */
1338 typedef struct ExternalSnapshotState
{
1339 BlkTransactionState common
;
1340 BlockDriverState
*old_bs
;
1341 BlockDriverState
*new_bs
;
1342 } ExternalSnapshotState
;
1344 static void external_snapshot_prepare(BlkTransactionState
*common
,
1349 QDict
*options
= NULL
;
1350 Error
*local_err
= NULL
;
1351 bool has_device
= false;
1353 bool has_node_name
= false;
1354 const char *node_name
;
1355 bool has_snapshot_node_name
= false;
1356 const char *snapshot_node_name
;
1357 const char *new_image_file
;
1358 const char *format
= "qcow2";
1359 enum NewImageMode mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1360 ExternalSnapshotState
*state
=
1361 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1362 TransactionAction
*action
= common
->action
;
1364 /* get parameters */
1365 g_assert(action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
);
1367 has_device
= action
->blockdev_snapshot_sync
->has_device
;
1368 device
= action
->blockdev_snapshot_sync
->device
;
1369 has_node_name
= action
->blockdev_snapshot_sync
->has_node_name
;
1370 node_name
= action
->blockdev_snapshot_sync
->node_name
;
1371 has_snapshot_node_name
=
1372 action
->blockdev_snapshot_sync
->has_snapshot_node_name
;
1373 snapshot_node_name
= action
->blockdev_snapshot_sync
->snapshot_node_name
;
1375 new_image_file
= action
->blockdev_snapshot_sync
->snapshot_file
;
1376 if (action
->blockdev_snapshot_sync
->has_format
) {
1377 format
= action
->blockdev_snapshot_sync
->format
;
1379 if (action
->blockdev_snapshot_sync
->has_mode
) {
1380 mode
= action
->blockdev_snapshot_sync
->mode
;
1383 /* start processing */
1384 drv
= bdrv_find_format(format
);
1386 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1390 state
->old_bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1391 has_node_name
? node_name
: NULL
,
1394 error_propagate(errp
, local_err
);
1398 if (has_node_name
&& !has_snapshot_node_name
) {
1399 error_setg(errp
, "New snapshot node name missing");
1403 if (has_snapshot_node_name
&& bdrv_find_node(snapshot_node_name
)) {
1404 error_setg(errp
, "New snapshot node name already existing");
1408 if (!bdrv_is_inserted(state
->old_bs
)) {
1409 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1413 if (bdrv_op_is_blocked(state
->old_bs
,
1414 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1418 if (!bdrv_is_read_only(state
->old_bs
)) {
1419 if (bdrv_flush(state
->old_bs
)) {
1420 error_set(errp
, QERR_IO_ERROR
);
1425 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1426 error_set(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1430 flags
= state
->old_bs
->open_flags
;
1432 /* create new image w/backing file */
1433 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1434 bdrv_img_create(new_image_file
, format
,
1435 state
->old_bs
->filename
,
1436 state
->old_bs
->drv
->format_name
,
1437 NULL
, -1, flags
, &local_err
, false);
1439 error_propagate(errp
, local_err
);
1444 if (has_snapshot_node_name
) {
1445 options
= qdict_new();
1446 qdict_put(options
, "node-name",
1447 qstring_from_str(snapshot_node_name
));
1450 /* TODO Inherit bs->options or only take explicit options with an
1451 * extended QMP command? */
1452 assert(state
->new_bs
== NULL
);
1453 ret
= bdrv_open(&state
->new_bs
, new_image_file
, NULL
, options
,
1454 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
1455 /* We will manually add the backing_hd field to the bs later */
1457 error_propagate(errp
, local_err
);
1461 static void external_snapshot_commit(BlkTransactionState
*common
)
1463 ExternalSnapshotState
*state
=
1464 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1466 /* This removes our old bs and adds the new bs */
1467 bdrv_append(state
->new_bs
, state
->old_bs
);
1468 /* We don't need (or want) to use the transactional
1469 * bdrv_reopen_multiple() across all the entries at once, because we
1470 * don't want to abort all of them if one of them fails the reopen */
1471 bdrv_reopen(state
->new_bs
, state
->new_bs
->open_flags
& ~BDRV_O_RDWR
,
1475 static void external_snapshot_abort(BlkTransactionState
*common
)
1477 ExternalSnapshotState
*state
=
1478 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1479 if (state
->new_bs
) {
1480 bdrv_unref(state
->new_bs
);
1484 typedef struct DriveBackupState
{
1485 BlkTransactionState common
;
1486 BlockDriverState
*bs
;
1490 static void drive_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1492 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1493 DriveBackup
*backup
;
1494 Error
*local_err
= NULL
;
1496 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1497 backup
= common
->action
->drive_backup
;
1499 qmp_drive_backup(backup
->device
, backup
->target
,
1500 backup
->has_format
, backup
->format
,
1502 backup
->has_mode
, backup
->mode
,
1503 backup
->has_speed
, backup
->speed
,
1504 backup
->has_on_source_error
, backup
->on_source_error
,
1505 backup
->has_on_target_error
, backup
->on_target_error
,
1508 error_propagate(errp
, local_err
);
1514 state
->bs
= bdrv_find(backup
->device
);
1515 state
->job
= state
->bs
->job
;
1518 static void drive_backup_abort(BlkTransactionState
*common
)
1520 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1521 BlockDriverState
*bs
= state
->bs
;
1523 /* Only cancel if it's the job we started */
1524 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1525 block_job_cancel_sync(bs
->job
);
1529 static void abort_prepare(BlkTransactionState
*common
, Error
**errp
)
1531 error_setg(errp
, "Transaction aborted using Abort action");
1534 static void abort_commit(BlkTransactionState
*common
)
1536 g_assert_not_reached(); /* this action never succeeds */
1539 static const BdrvActionOps actions
[] = {
1540 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
1541 .instance_size
= sizeof(ExternalSnapshotState
),
1542 .prepare
= external_snapshot_prepare
,
1543 .commit
= external_snapshot_commit
,
1544 .abort
= external_snapshot_abort
,
1546 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
1547 .instance_size
= sizeof(DriveBackupState
),
1548 .prepare
= drive_backup_prepare
,
1549 .abort
= drive_backup_abort
,
1551 [TRANSACTION_ACTION_KIND_ABORT
] = {
1552 .instance_size
= sizeof(BlkTransactionState
),
1553 .prepare
= abort_prepare
,
1554 .commit
= abort_commit
,
1556 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
1557 .instance_size
= sizeof(InternalSnapshotState
),
1558 .prepare
= internal_snapshot_prepare
,
1559 .abort
= internal_snapshot_abort
,
1564 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1565 * then we do not pivot any of the devices in the group, and abandon the
1568 void qmp_transaction(TransactionActionList
*dev_list
, Error
**errp
)
1570 TransactionActionList
*dev_entry
= dev_list
;
1571 BlkTransactionState
*state
, *next
;
1572 Error
*local_err
= NULL
;
1574 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkTransactionState
) snap_bdrv_states
;
1575 QSIMPLEQ_INIT(&snap_bdrv_states
);
1577 /* drain all i/o before any snapshots */
1580 /* We don't do anything in this loop that commits us to the snapshot */
1581 while (NULL
!= dev_entry
) {
1582 TransactionAction
*dev_info
= NULL
;
1583 const BdrvActionOps
*ops
;
1585 dev_info
= dev_entry
->value
;
1586 dev_entry
= dev_entry
->next
;
1588 assert(dev_info
->kind
< ARRAY_SIZE(actions
));
1590 ops
= &actions
[dev_info
->kind
];
1591 assert(ops
->instance_size
> 0);
1593 state
= g_malloc0(ops
->instance_size
);
1595 state
->action
= dev_info
;
1596 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
1598 state
->ops
->prepare(state
, &local_err
);
1600 error_propagate(errp
, local_err
);
1601 goto delete_and_fail
;
1605 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1606 if (state
->ops
->commit
) {
1607 state
->ops
->commit(state
);
1616 * failure, and it is all-or-none; abandon each new bs, and keep using
1617 * the original bs for all images
1619 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1620 if (state
->ops
->abort
) {
1621 state
->ops
->abort(state
);
1625 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
1626 if (state
->ops
->clean
) {
1627 state
->ops
->clean(state
);
1634 static void eject_device(BlockDriverState
*bs
, int force
, Error
**errp
)
1636 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
1639 if (!bdrv_dev_has_removable_media(bs
)) {
1640 error_setg(errp
, "Device '%s' is not removable",
1641 bdrv_get_device_name(bs
));
1645 if (bdrv_dev_is_medium_locked(bs
) && !bdrv_dev_is_tray_open(bs
)) {
1646 bdrv_dev_eject_request(bs
, force
);
1648 error_setg(errp
, "Device '%s' is locked",
1649 bdrv_get_device_name(bs
));
1657 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
1659 BlockDriverState
*bs
;
1661 bs
= bdrv_find(device
);
1663 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1667 eject_device(bs
, force
, errp
);
1670 void qmp_block_passwd(bool has_device
, const char *device
,
1671 bool has_node_name
, const char *node_name
,
1672 const char *password
, Error
**errp
)
1674 Error
*local_err
= NULL
;
1675 BlockDriverState
*bs
;
1678 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1679 has_node_name
? node_name
: NULL
,
1682 error_propagate(errp
, local_err
);
1686 err
= bdrv_set_key(bs
, password
);
1687 if (err
== -EINVAL
) {
1688 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1690 } else if (err
< 0) {
1691 error_set(errp
, QERR_INVALID_PASSWORD
);
1696 static void qmp_bdrv_open_encrypted(BlockDriverState
*bs
, const char *filename
,
1697 int bdrv_flags
, BlockDriver
*drv
,
1698 const char *password
, Error
**errp
)
1700 Error
*local_err
= NULL
;
1703 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, bdrv_flags
, drv
, &local_err
);
1705 error_propagate(errp
, local_err
);
1709 if (bdrv_key_required(bs
)) {
1711 if (bdrv_set_key(bs
, password
) < 0) {
1712 error_set(errp
, QERR_INVALID_PASSWORD
);
1715 error_set(errp
, QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
),
1716 bdrv_get_encrypted_filename(bs
));
1718 } else if (password
) {
1719 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1723 void qmp_change_blockdev(const char *device
, const char *filename
,
1724 const char *format
, Error
**errp
)
1726 BlockDriverState
*bs
;
1727 BlockDriver
*drv
= NULL
;
1731 bs
= bdrv_find(device
);
1733 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1738 drv
= bdrv_find_whitelisted_format(format
, bs
->read_only
);
1740 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1745 eject_device(bs
, 0, &err
);
1747 error_propagate(errp
, err
);
1751 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
1752 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
1754 qmp_bdrv_open_encrypted(bs
, filename
, bdrv_flags
, drv
, NULL
, errp
);
1757 /* throttling disk I/O limits */
1758 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
1765 bool has_bps_rd_max
,
1767 bool has_bps_wr_max
,
1771 bool has_iops_rd_max
,
1772 int64_t iops_rd_max
,
1773 bool has_iops_wr_max
,
1774 int64_t iops_wr_max
,
1776 int64_t iops_size
, Error
**errp
)
1779 BlockDriverState
*bs
;
1780 AioContext
*aio_context
;
1782 bs
= bdrv_find(device
);
1784 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1788 memset(&cfg
, 0, sizeof(cfg
));
1789 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
1790 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
1791 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
1793 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
1794 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
1795 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
1798 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
1800 if (has_bps_rd_max
) {
1801 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
1803 if (has_bps_wr_max
) {
1804 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
1807 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
1809 if (has_iops_rd_max
) {
1810 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
1812 if (has_iops_wr_max
) {
1813 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
1816 if (has_iops_size
) {
1817 cfg
.op_size
= iops_size
;
1820 if (!check_throttle_config(&cfg
, errp
)) {
1824 aio_context
= bdrv_get_aio_context(bs
);
1825 aio_context_acquire(aio_context
);
1827 if (!bs
->io_limits_enabled
&& throttle_enabled(&cfg
)) {
1828 bdrv_io_limits_enable(bs
);
1829 } else if (bs
->io_limits_enabled
&& !throttle_enabled(&cfg
)) {
1830 bdrv_io_limits_disable(bs
);
1833 if (bs
->io_limits_enabled
) {
1834 bdrv_set_io_limits(bs
, &cfg
);
1837 aio_context_release(aio_context
);
1840 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1842 const char *id
= qdict_get_str(qdict
, "id");
1843 BlockDriverState
*bs
;
1845 AioContext
*aio_context
;
1846 Error
*local_err
= NULL
;
1850 error_report("Device '%s' not found", id
);
1854 dinfo
= drive_get_by_blockdev(bs
);
1855 if (dinfo
&& !dinfo
->enable_auto_del
) {
1856 error_report("Deleting device added with blockdev-add"
1857 " is not supported");
1861 aio_context
= bdrv_get_aio_context(bs
);
1862 aio_context_acquire(aio_context
);
1864 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
1865 error_report("%s", error_get_pretty(local_err
));
1866 error_free(local_err
);
1867 aio_context_release(aio_context
);
1871 /* quiesce block driver; prevent further io */
1876 /* if we have a device attached to this BlockDriverState
1877 * then we need to make the drive anonymous until the device
1878 * can be removed. If this is a drive with no device backing
1879 * then we can just get rid of the block driver state right here.
1881 if (bdrv_get_attached_dev(bs
)) {
1884 /* Further I/O must not pause the guest */
1885 bdrv_set_on_error(bs
, BLOCKDEV_ON_ERROR_REPORT
,
1886 BLOCKDEV_ON_ERROR_REPORT
);
1891 aio_context_release(aio_context
);
1895 void qmp_block_resize(bool has_device
, const char *device
,
1896 bool has_node_name
, const char *node_name
,
1897 int64_t size
, Error
**errp
)
1899 Error
*local_err
= NULL
;
1900 BlockDriverState
*bs
;
1901 AioContext
*aio_context
;
1904 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1905 has_node_name
? node_name
: NULL
,
1908 error_propagate(errp
, local_err
);
1912 aio_context
= bdrv_get_aio_context(bs
);
1913 aio_context_acquire(aio_context
);
1915 if (!bdrv_is_first_non_filter(bs
)) {
1916 error_set(errp
, QERR_FEATURE_DISABLED
, "resize");
1921 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
1925 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
1926 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1930 /* complete all in-flight operations before resizing the device */
1933 ret
= bdrv_truncate(bs
, size
);
1938 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1941 error_set(errp
, QERR_UNSUPPORTED
);
1944 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1947 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1950 error_setg_errno(errp
, -ret
, "Could not resize");
1955 aio_context_release(aio_context
);
1958 static void block_job_cb(void *opaque
, int ret
)
1960 BlockDriverState
*bs
= opaque
;
1961 const char *msg
= NULL
;
1963 trace_block_job_cb(bs
, bs
->job
, ret
);
1968 msg
= strerror(-ret
);
1971 if (block_job_is_cancelled(bs
->job
)) {
1972 block_job_event_cancelled(bs
->job
);
1974 block_job_event_completed(bs
->job
, msg
);
1977 bdrv_put_ref_bh_schedule(bs
);
1980 void qmp_block_stream(const char *device
,
1981 bool has_base
, const char *base
,
1982 bool has_backing_file
, const char *backing_file
,
1983 bool has_speed
, int64_t speed
,
1984 bool has_on_error
, BlockdevOnError on_error
,
1987 BlockDriverState
*bs
;
1988 BlockDriverState
*base_bs
= NULL
;
1989 Error
*local_err
= NULL
;
1990 const char *base_name
= NULL
;
1992 if (!has_on_error
) {
1993 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1996 bs
= bdrv_find(device
);
1998 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2002 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2007 base_bs
= bdrv_find_backing_image(bs
, base
);
2008 if (base_bs
== NULL
) {
2009 error_set(errp
, QERR_BASE_NOT_FOUND
, base
);
2015 /* if we are streaming the entire chain, the result will have no backing
2016 * file, and specifying one is therefore an error */
2017 if (base_bs
== NULL
&& has_backing_file
) {
2018 error_setg(errp
, "backing file specified, but streaming the "
2023 /* backing_file string overrides base bs filename */
2024 base_name
= has_backing_file
? backing_file
: base_name
;
2026 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
2027 on_error
, block_job_cb
, bs
, &local_err
);
2029 error_propagate(errp
, local_err
);
2033 trace_qmp_block_stream(bs
, bs
->job
);
2036 void qmp_block_commit(const char *device
,
2037 bool has_base
, const char *base
,
2038 bool has_top
, const char *top
,
2039 bool has_backing_file
, const char *backing_file
,
2040 bool has_speed
, int64_t speed
,
2043 BlockDriverState
*bs
;
2044 BlockDriverState
*base_bs
, *top_bs
;
2045 Error
*local_err
= NULL
;
2046 /* This will be part of the QMP command, if/when the
2047 * BlockdevOnError change for blkmirror makes it in
2049 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2055 /* drain all i/o before commits */
2059 * libvirt relies on the DeviceNotFound error class in order to probe for
2060 * live commit feature versions; for this to work, we must make sure to
2061 * perform the device lookup before any generic errors that may occur in a
2062 * scenario in which all optional arguments are omitted. */
2063 bs
= bdrv_find(device
);
2065 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2069 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT
, errp
)) {
2073 /* default top_bs is the active layer */
2076 if (has_top
&& top
) {
2077 if (strcmp(bs
->filename
, top
) != 0) {
2078 top_bs
= bdrv_find_backing_image(bs
, top
);
2082 if (top_bs
== NULL
) {
2083 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2087 if (has_base
&& base
) {
2088 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2090 base_bs
= bdrv_find_base(top_bs
);
2093 if (base_bs
== NULL
) {
2094 error_set(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2098 /* Do not allow attempts to commit an image into itself */
2099 if (top_bs
== base_bs
) {
2100 error_setg(errp
, "cannot commit an image into itself");
2105 if (has_backing_file
) {
2106 error_setg(errp
, "'backing-file' specified,"
2107 " but 'top' is the active layer");
2110 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
2113 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
2114 has_backing_file
? backing_file
: NULL
, &local_err
);
2116 if (local_err
!= NULL
) {
2117 error_propagate(errp
, local_err
);
2122 void qmp_drive_backup(const char *device
, const char *target
,
2123 bool has_format
, const char *format
,
2124 enum MirrorSyncMode sync
,
2125 bool has_mode
, enum NewImageMode mode
,
2126 bool has_speed
, int64_t speed
,
2127 bool has_on_source_error
, BlockdevOnError on_source_error
,
2128 bool has_on_target_error
, BlockdevOnError on_target_error
,
2131 BlockDriverState
*bs
;
2132 BlockDriverState
*target_bs
;
2133 BlockDriverState
*source
= NULL
;
2134 BlockDriver
*drv
= NULL
;
2135 Error
*local_err
= NULL
;
2143 if (!has_on_source_error
) {
2144 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2146 if (!has_on_target_error
) {
2147 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2150 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2153 bs
= bdrv_find(device
);
2155 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2159 if (!bdrv_is_inserted(bs
)) {
2160 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2165 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2168 drv
= bdrv_find_format(format
);
2170 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
2175 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
2179 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2181 /* See if we have a backing HD we can use to create our new image
2183 if (sync
== MIRROR_SYNC_MODE_TOP
) {
2184 source
= bs
->backing_hd
;
2186 sync
= MIRROR_SYNC_MODE_FULL
;
2189 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2193 size
= bdrv_getlength(bs
);
2195 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2199 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
2200 assert(format
&& drv
);
2202 bdrv_img_create(target
, format
, source
->filename
,
2203 source
->drv
->format_name
, NULL
,
2204 size
, flags
, &local_err
, false);
2206 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
2207 size
, flags
, &local_err
, false);
2212 error_propagate(errp
, local_err
);
2217 ret
= bdrv_open(&target_bs
, target
, NULL
, NULL
, flags
, drv
, &local_err
);
2219 error_propagate(errp
, local_err
);
2223 backup_start(bs
, target_bs
, speed
, sync
, on_source_error
, on_target_error
,
2224 block_job_cb
, bs
, &local_err
);
2225 if (local_err
!= NULL
) {
2226 bdrv_unref(target_bs
);
2227 error_propagate(errp
, local_err
);
2232 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
2234 return bdrv_named_nodes_list();
2237 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2239 void qmp_drive_mirror(const char *device
, const char *target
,
2240 bool has_format
, const char *format
,
2241 bool has_node_name
, const char *node_name
,
2242 bool has_replaces
, const char *replaces
,
2243 enum MirrorSyncMode sync
,
2244 bool has_mode
, enum NewImageMode mode
,
2245 bool has_speed
, int64_t speed
,
2246 bool has_granularity
, uint32_t granularity
,
2247 bool has_buf_size
, int64_t buf_size
,
2248 bool has_on_source_error
, BlockdevOnError on_source_error
,
2249 bool has_on_target_error
, BlockdevOnError on_target_error
,
2252 BlockDriverState
*bs
;
2253 BlockDriverState
*source
, *target_bs
;
2254 BlockDriver
*drv
= NULL
;
2255 Error
*local_err
= NULL
;
2256 QDict
*options
= NULL
;
2264 if (!has_on_source_error
) {
2265 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2267 if (!has_on_target_error
) {
2268 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2271 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2273 if (!has_granularity
) {
2276 if (!has_buf_size
) {
2277 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
2280 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
2281 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2282 "a value in range [512B, 64MB]");
2285 if (granularity
& (granularity
- 1)) {
2286 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity", "power of 2");
2290 bs
= bdrv_find(device
);
2292 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2296 if (!bdrv_is_inserted(bs
)) {
2297 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2302 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2305 drv
= bdrv_find_format(format
);
2307 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
2312 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR
, errp
)) {
2316 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2317 source
= bs
->backing_hd
;
2318 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
2319 sync
= MIRROR_SYNC_MODE_FULL
;
2321 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2325 size
= bdrv_getlength(bs
);
2327 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2332 BlockDriverState
*to_replace_bs
;
2334 if (!has_node_name
) {
2335 error_setg(errp
, "a node-name must be provided when replacing a"
2336 " named node of the graph");
2340 to_replace_bs
= check_to_replace_node(replaces
, &local_err
);
2342 if (!to_replace_bs
) {
2343 error_propagate(errp
, local_err
);
2347 if (size
!= bdrv_getlength(to_replace_bs
)) {
2348 error_setg(errp
, "cannot replace image with a mirror image of "
2354 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
2355 && mode
!= NEW_IMAGE_MODE_EXISTING
)
2357 /* create new image w/o backing file */
2358 assert(format
&& drv
);
2359 bdrv_img_create(target
, format
,
2360 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
2363 case NEW_IMAGE_MODE_EXISTING
:
2365 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
2366 /* create new image with backing file */
2367 bdrv_img_create(target
, format
,
2369 source
->drv
->format_name
,
2370 NULL
, size
, flags
, &local_err
, false);
2378 error_propagate(errp
, local_err
);
2382 if (has_node_name
) {
2383 options
= qdict_new();
2384 qdict_put(options
, "node-name", qstring_from_str(node_name
));
2387 /* Mirroring takes care of copy-on-write using the source's backing
2391 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
2392 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
2394 error_propagate(errp
, local_err
);
2398 /* pass the node name to replace to mirror start since it's loose coupling
2399 * and will allow to check whether the node still exist at mirror completion
2401 mirror_start(bs
, target_bs
,
2402 has_replaces
? replaces
: NULL
,
2403 speed
, granularity
, buf_size
, sync
,
2404 on_source_error
, on_target_error
,
2405 block_job_cb
, bs
, &local_err
);
2406 if (local_err
!= NULL
) {
2407 bdrv_unref(target_bs
);
2408 error_propagate(errp
, local_err
);
2413 static BlockJob
*find_block_job(const char *device
)
2415 BlockDriverState
*bs
;
2417 bs
= bdrv_find(device
);
2418 if (!bs
|| !bs
->job
) {
2424 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
2426 BlockJob
*job
= find_block_job(device
);
2429 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2433 block_job_set_speed(job
, speed
, errp
);
2436 void qmp_block_job_cancel(const char *device
,
2437 bool has_force
, bool force
, Error
**errp
)
2439 BlockJob
*job
= find_block_job(device
);
2446 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2449 if (job
->paused
&& !force
) {
2450 error_setg(errp
, "The block job for device '%s' is currently paused",
2455 trace_qmp_block_job_cancel(job
);
2456 block_job_cancel(job
);
2459 void qmp_block_job_pause(const char *device
, Error
**errp
)
2461 BlockJob
*job
= find_block_job(device
);
2464 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2468 trace_qmp_block_job_pause(job
);
2469 block_job_pause(job
);
2472 void qmp_block_job_resume(const char *device
, Error
**errp
)
2474 BlockJob
*job
= find_block_job(device
);
2477 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2481 trace_qmp_block_job_resume(job
);
2482 block_job_resume(job
);
2485 void qmp_block_job_complete(const char *device
, Error
**errp
)
2487 BlockJob
*job
= find_block_job(device
);
2490 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2494 trace_qmp_block_job_complete(job
);
2495 block_job_complete(job
, errp
);
2498 void qmp_change_backing_file(const char *device
,
2499 const char *image_node_name
,
2500 const char *backing_file
,
2503 BlockDriverState
*bs
= NULL
;
2504 BlockDriverState
*image_bs
= NULL
;
2505 Error
*local_err
= NULL
;
2510 /* find the top layer BDS of the chain */
2511 bs
= bdrv_find(device
);
2513 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2517 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
2519 error_propagate(errp
, local_err
);
2524 error_setg(errp
, "image file not found");
2528 if (bdrv_find_base(image_bs
) == image_bs
) {
2529 error_setg(errp
, "not allowing backing file change on an image "
2530 "without a backing file");
2534 /* even though we are not necessarily operating on bs, we need it to
2535 * determine if block ops are currently prohibited on the chain */
2536 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
2540 /* final sanity check */
2541 if (!bdrv_chain_contains(bs
, image_bs
)) {
2542 error_setg(errp
, "'%s' and image file are not in the same chain",
2547 /* if not r/w, reopen to make r/w */
2548 open_flags
= image_bs
->open_flags
;
2549 ro
= bdrv_is_read_only(image_bs
);
2552 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
2554 error_propagate(errp
, local_err
);
2559 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
2560 image_bs
->drv
? image_bs
->drv
->format_name
: "");
2563 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
2565 /* don't exit here, so we can try to restore open flags if
2570 bdrv_reopen(image_bs
, open_flags
, &local_err
);
2572 error_propagate(errp
, local_err
); /* will preserve prior errp */
2577 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
2579 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
2583 Error
*local_err
= NULL
;
2585 /* Require an ID in the top level */
2586 if (!options
->has_id
) {
2587 error_setg(errp
, "Block device needs an ID");
2591 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
2592 * cache.direct=false instead of silently switching to aio=threads, except
2593 * when called from drive_new().
2595 * For now, simply forbidding the combination for all drivers will do. */
2596 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
2597 bool direct
= options
->has_cache
&&
2598 options
->cache
->has_direct
&&
2599 options
->cache
->direct
;
2601 error_setg(errp
, "aio=native requires cache.direct=true");
2606 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
2607 &options
, NULL
, &local_err
);
2609 error_propagate(errp
, local_err
);
2613 obj
= qmp_output_get_qobject(ov
);
2614 qdict
= qobject_to_qdict(obj
);
2616 qdict_flatten(qdict
);
2618 dinfo
= blockdev_init(NULL
, qdict
, &local_err
);
2620 error_propagate(errp
, local_err
);
2624 if (bdrv_key_required(dinfo
->bdrv
)) {
2626 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
2631 qmp_output_visitor_cleanup(ov
);
2634 static void do_qmp_query_block_jobs_one(void *opaque
, BlockDriverState
*bs
)
2636 BlockJobInfoList
**prev
= opaque
;
2637 BlockJob
*job
= bs
->job
;
2640 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
2641 elem
->value
= block_job_query(bs
->job
);
2642 (*prev
)->next
= elem
;
2647 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
2649 /* Dummy is a fake list element for holding the head pointer */
2650 BlockJobInfoList dummy
= {};
2651 BlockJobInfoList
*prev
= &dummy
;
2652 bdrv_iterate(do_qmp_query_block_jobs_one
, &prev
);
2656 QemuOptsList qemu_common_drive_opts
= {
2658 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
2662 .type
= QEMU_OPT_BOOL
,
2663 .help
= "enable/disable snapshot mode",
2666 .type
= QEMU_OPT_STRING
,
2667 .help
= "discard operation (ignore/off, unmap/on)",
2669 .name
= "cache.writeback",
2670 .type
= QEMU_OPT_BOOL
,
2671 .help
= "enables writeback mode for any caches",
2673 .name
= "cache.direct",
2674 .type
= QEMU_OPT_BOOL
,
2675 .help
= "enables use of O_DIRECT (bypass the host page cache)",
2677 .name
= "cache.no-flush",
2678 .type
= QEMU_OPT_BOOL
,
2679 .help
= "ignore any flush requests for the device",
2682 .type
= QEMU_OPT_STRING
,
2683 .help
= "host AIO implementation (threads, native)",
2686 .type
= QEMU_OPT_STRING
,
2687 .help
= "disk format (raw, qcow2, ...)",
2690 .type
= QEMU_OPT_STRING
,
2691 .help
= "read error action",
2694 .type
= QEMU_OPT_STRING
,
2695 .help
= "write error action",
2697 .name
= "read-only",
2698 .type
= QEMU_OPT_BOOL
,
2699 .help
= "open drive file as read-only",
2701 .name
= "throttling.iops-total",
2702 .type
= QEMU_OPT_NUMBER
,
2703 .help
= "limit total I/O operations per second",
2705 .name
= "throttling.iops-read",
2706 .type
= QEMU_OPT_NUMBER
,
2707 .help
= "limit read operations per second",
2709 .name
= "throttling.iops-write",
2710 .type
= QEMU_OPT_NUMBER
,
2711 .help
= "limit write operations per second",
2713 .name
= "throttling.bps-total",
2714 .type
= QEMU_OPT_NUMBER
,
2715 .help
= "limit total bytes per second",
2717 .name
= "throttling.bps-read",
2718 .type
= QEMU_OPT_NUMBER
,
2719 .help
= "limit read bytes per second",
2721 .name
= "throttling.bps-write",
2722 .type
= QEMU_OPT_NUMBER
,
2723 .help
= "limit write bytes per second",
2725 .name
= "throttling.iops-total-max",
2726 .type
= QEMU_OPT_NUMBER
,
2727 .help
= "I/O operations burst",
2729 .name
= "throttling.iops-read-max",
2730 .type
= QEMU_OPT_NUMBER
,
2731 .help
= "I/O operations read burst",
2733 .name
= "throttling.iops-write-max",
2734 .type
= QEMU_OPT_NUMBER
,
2735 .help
= "I/O operations write burst",
2737 .name
= "throttling.bps-total-max",
2738 .type
= QEMU_OPT_NUMBER
,
2739 .help
= "total bytes burst",
2741 .name
= "throttling.bps-read-max",
2742 .type
= QEMU_OPT_NUMBER
,
2743 .help
= "total bytes read burst",
2745 .name
= "throttling.bps-write-max",
2746 .type
= QEMU_OPT_NUMBER
,
2747 .help
= "total bytes write burst",
2749 .name
= "throttling.iops-size",
2750 .type
= QEMU_OPT_NUMBER
,
2751 .help
= "when limiting by iops max size of an I/O in bytes",
2753 .name
= "copy-on-read",
2754 .type
= QEMU_OPT_BOOL
,
2755 .help
= "copy read data from backing file into image file",
2757 .name
= "detect-zeroes",
2758 .type
= QEMU_OPT_STRING
,
2759 .help
= "try to optimize zero writes (off, on, unmap)",
2761 { /* end of list */ }
2765 QemuOptsList qemu_drive_opts
= {
2767 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
2770 * no elements => accept any params
2771 * validation will happen later
2773 { /* end of list */ }