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)
227 qemu_opts_del(dinfo->opts);
229 QTAILQ_REMOVE(&drives, dinfo, next);
230 g_free(dinfo->serial);
236 BlockDriverState *bs;
239 static void bdrv_put_ref_bh(void *opaque)
241 BDRVPutRefBH *s = opaque;
244 qemu_bh_delete(s->bh);
249 * Release a BDS reference in a BH
251 * It is not safe to use bdrv_unref() from a callback function when the callers
252 * still need the BlockDriverState. In such cases we schedule a BH to release
255 static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
259 s = g_new(BDRVPutRefBH, 1);
260 s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
262 qemu_bh_schedule(s->bh);
265 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
267 if (!strcmp(buf, "ignore")) {
268 return BLOCKDEV_ON_ERROR_IGNORE;
269 } else if (!is_read && !strcmp(buf, "enospc")) {
270 return BLOCKDEV_ON_ERROR_ENOSPC;
271 } else if (!strcmp(buf, "stop")) {
272 return BLOCKDEV_ON_ERROR_STOP;
273 } else if (!strcmp(buf, "report")) {
274 return BLOCKDEV_ON_ERROR_REPORT;
276 error_setg(errp, "'%s' invalid %s error action",
277 buf, is_read ? "read" : "write");
282 static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
284 if (throttle_conflicting(cfg)) {
285 error_setg(errp, "bps/iops/max total values and read/write values"
286 " cannot be used at the same time");
290 if (!throttle_is_valid(cfg)) {
291 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
298 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
300 /* Takes the ownership of bs_opts */
301 static DriveInfo *blockdev_init(const char *file, QDict *bs_opts,
307 int on_read_error, on_write_error;
308 BlockDriverState *bs;
317 bool has_driver_specific_opts;
318 BlockdevDetectZeroesOptions detect_zeroes;
319 BlockDriver *drv = NULL;
321 /* Check common options by copying from bs_opts to opts, all other options
322 * stay in bs_opts for processing by bdrv_open(). */
323 id = qdict_get_try_str(bs_opts, "id");
324 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
326 error_propagate(errp, error);
330 qemu_opts_absorb_qdict(opts, bs_opts, &error);
332 error_propagate(errp, error);
337 qdict_del(bs_opts, "id");
340 has_driver_specific_opts = !!qdict_size(bs_opts);
342 /* extract parameters */
343 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
344 ro = qemu_opt_get_bool(opts, "read-only", 0);
345 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
347 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
348 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
349 error_setg(errp, "invalid discard option");
354 if (qemu_opt_get_bool(opts, "cache.writeback", true)) {
355 bdrv_flags |= BDRV_O_CACHE_WB;
357 if (qemu_opt_get_bool(opts, "cache.direct", false)) {
358 bdrv_flags |= BDRV_O_NOCACHE;
360 if (qemu_opt_get_bool(opts, "cache.no-flush", false)) {
361 bdrv_flags |= BDRV_O_NO_FLUSH;
364 #ifdef CONFIG_LINUX_AIO
365 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
366 if (!strcmp(buf, "native")) {
367 bdrv_flags |= BDRV_O_NATIVE_AIO;
368 } else if (!strcmp(buf, "threads")) {
369 /* this is the default */
371 error_setg(errp, "invalid aio option");
377 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
378 if (is_help_option(buf)) {
379 error_printf("Supported formats:");
380 bdrv_iterate_format(bdrv_format_print, NULL);
385 drv = bdrv_find_format(buf);
387 error_setg(errp, "'%s' invalid format", buf);
392 /* disk I/O throttling */
393 memset(&cfg, 0, sizeof(cfg));
394 cfg.buckets[THROTTLE_BPS_TOTAL].avg =
395 qemu_opt_get_number(opts, "throttling.bps-total", 0);
396 cfg.buckets[THROTTLE_BPS_READ].avg =
397 qemu_opt_get_number(opts, "throttling.bps-read", 0);
398 cfg.buckets[THROTTLE_BPS_WRITE].avg =
399 qemu_opt_get_number(opts, "throttling.bps-write", 0);
400 cfg.buckets[THROTTLE_OPS_TOTAL].avg =
401 qemu_opt_get_number(opts, "throttling.iops-total", 0);
402 cfg.buckets[THROTTLE_OPS_READ].avg =
403 qemu_opt_get_number(opts, "throttling.iops-read", 0);
404 cfg.buckets[THROTTLE_OPS_WRITE].avg =
405 qemu_opt_get_number(opts, "throttling.iops-write", 0);
407 cfg.buckets[THROTTLE_BPS_TOTAL].max =
408 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
409 cfg.buckets[THROTTLE_BPS_READ].max =
410 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
411 cfg.buckets[THROTTLE_BPS_WRITE].max =
412 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
413 cfg.buckets[THROTTLE_OPS_TOTAL].max =
414 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
415 cfg.buckets[THROTTLE_OPS_READ].max =
416 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
417 cfg.buckets[THROTTLE_OPS_WRITE].max =
418 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
420 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
422 if (!check_throttle_config(&cfg, &error)) {
423 error_propagate(errp, error);
427 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
428 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
429 on_write_error = parse_block_error_action(buf, 0, &error);
431 error_propagate(errp, error);
436 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
437 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
438 on_read_error = parse_block_error_action(buf, 1, &error);
440 error_propagate(errp, error);
446 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
447 qemu_opt_get(opts, "detect-zeroes"),
448 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
449 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
452 error_propagate(errp, error);
456 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
457 !(bdrv_flags & BDRV_O_UNMAP)) {
458 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
459 "without setting discard operation to unmap");
464 bs = bdrv_new(qemu_opts_id(opts), errp);
468 bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
470 bs->detect_zeroes = detect_zeroes;
472 bdrv_set_on_error(bs, on_read_error, on_write_error);
474 /* disk I/O throttling */
475 if (throttle_enabled(&cfg)) {
476 bdrv_io_limits_enable(bs);
477 bdrv_set_io_limits(bs, &cfg);
480 dinfo = g_malloc0(sizeof(*dinfo));
481 dinfo->id = g_strdup(qemu_opts_id(opts));
483 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
485 if (!file || !*file) {
486 if (has_driver_specific_opts) {
495 /* always use cache=unsafe with snapshot */
496 bdrv_flags &= ~BDRV_O_CACHE_MASK;
497 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
501 bdrv_flags |= BDRV_O_COPY_ON_READ;
504 if (runstate_check(RUN_STATE_INMIGRATE)) {
505 bdrv_flags |= BDRV_O_INCOMING;
508 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
511 ret = bdrv_open(&bs, file, NULL, bs_opts, bdrv_flags, drv, &error);
512 assert(bs == dinfo->bdrv);
515 error_setg(errp, "could not open disk image %s: %s",
516 file ?: dinfo->id, error_get_pretty(error));
521 if (bdrv_key_required(bs)) {
539 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
544 value = qemu_opt_get(opts, from);
546 if (qemu_opt_find(opts, to)) {
547 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
548 "same time", to, from);
551 qemu_opt_set(opts, to, value);
552 qemu_opt_unset(opts, from);
556 QemuOptsList qemu_legacy_drive_opts = {
558 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
562 .type = QEMU_OPT_NUMBER,
563 .help = "bus number",
566 .type = QEMU_OPT_NUMBER,
567 .help = "unit number (i.e. lun for scsi)",
570 .type = QEMU_OPT_NUMBER,
571 .help = "index number",
574 .type = QEMU_OPT_STRING,
575 .help = "media type (disk, cdrom)",
578 .type = QEMU_OPT_STRING,
579 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
582 .type = QEMU_OPT_NUMBER,
583 .help = "number of cylinders (ide disk geometry)",
586 .type = QEMU_OPT_NUMBER,
587 .help = "number of heads (ide disk geometry)",
590 .type = QEMU_OPT_NUMBER,
591 .help = "number of sectors (ide disk geometry)",
594 .type = QEMU_OPT_STRING,
595 .help = "chs translation (auto, lba, none)",
598 .type = QEMU_OPT_BOOL,
599 .help = "(deprecated, ignored)",
602 .type = QEMU_OPT_STRING,
603 .help = "pci address (virtio only)",
606 .type = QEMU_OPT_STRING,
607 .help = "disk serial number",
610 .type = QEMU_OPT_STRING,
614 /* Options that are passed on, but have special semantics with -drive */
617 .type = QEMU_OPT_BOOL,
618 .help = "open drive file as read-only",
621 .type = QEMU_OPT_STRING,
622 .help = "read error action",
625 .type = QEMU_OPT_STRING,
626 .help = "write error action",
628 .name = "copy-on-read",
629 .type = QEMU_OPT_BOOL,
630 .help = "copy read data from backing file into image file",
633 { /* end of list */ }
637 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
640 DriveInfo *dinfo = NULL;
642 QemuOpts *legacy_opts;
643 DriveMediaType media = MEDIA_DISK;
644 BlockInterfaceType type;
645 int cyls, heads, secs, translation;
646 int max_devs, bus_id, unit_id, index;
648 const char *werror, *rerror;
649 bool read_only = false;
652 const char *filename;
653 Error *local_err = NULL;
656 /* Change legacy command line options into QMP ones */
657 static const struct {
661 { "iops", "throttling.iops-total" },
662 { "iops_rd", "throttling.iops-read" },
663 { "iops_wr", "throttling.iops-write" },
665 { "bps", "throttling.bps-total" },
666 { "bps_rd", "throttling.bps-read" },
667 { "bps_wr", "throttling.bps-write" },
669 { "iops_max", "throttling.iops-total-max" },
670 { "iops_rd_max", "throttling.iops-read-max" },
671 { "iops_wr_max", "throttling.iops-write-max" },
673 { "bps_max", "throttling.bps-total-max" },
674 { "bps_rd_max", "throttling.bps-read-max" },
675 { "bps_wr_max", "throttling.bps-write-max" },
677 { "iops_size", "throttling.iops-size" },
679 { "readonly", "read-only" },
682 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
683 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
686 error_report("%s", error_get_pretty(local_err));
687 error_free(local_err);
692 value = qemu_opt_get(all_opts, "cache");
696 if (bdrv_parse_cache_flags(value, &flags) != 0) {
697 error_report("invalid cache option");
701 /* Specific options take precedence */
702 if (!qemu_opt_get(all_opts, "cache.writeback")) {
703 qemu_opt_set_bool(all_opts, "cache.writeback",
704 !!(flags & BDRV_O_CACHE_WB));
706 if (!qemu_opt_get(all_opts, "cache.direct")) {
707 qemu_opt_set_bool(all_opts, "cache.direct",
708 !!(flags & BDRV_O_NOCACHE));
710 if (!qemu_opt_get(all_opts, "cache.no-flush")) {
711 qemu_opt_set_bool(all_opts, "cache.no-flush",
712 !!(flags & BDRV_O_NO_FLUSH));
714 qemu_opt_unset(all_opts, "cache");
717 /* Get a QDict for processing the options */
718 bs_opts = qdict_new();
719 qemu_opts_to_qdict(all_opts, bs_opts);
721 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
723 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
725 error_report("%s", error_get_pretty(local_err));
726 error_free(local_err);
730 /* Deprecated option boot=[on|off] */
731 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
732 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
733 "ignored. Future versions will reject this parameter. Please "
734 "update your scripts.\n");
738 value = qemu_opt_get(legacy_opts, "media");
740 if (!strcmp(value, "disk")) {
742 } else if (!strcmp(value, "cdrom")) {
746 error_report("'%s' invalid media", value);
751 /* copy-on-read is disabled with a warning for read-only devices */
752 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
753 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
755 if (read_only && copy_on_read) {
756 error_report("warning: disabling copy-on-read on read-only drive");
757 copy_on_read = false;
760 qdict_put(bs_opts, "read-only",
761 qstring_from_str(read_only ? "on" : "off"));
762 qdict_put(bs_opts, "copy-on-read",
763 qstring_from_str(copy_on_read ? "on" :"off"));
765 /* Controller type */
766 value = qemu_opt_get(legacy_opts, "if");
769 type < IF_COUNT && strcmp(value, if_name[type]);
772 if (type == IF_COUNT) {
773 error_report("unsupported bus type '%s'", value);
777 type = block_default_type;
781 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
782 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
783 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
785 if (cyls || heads || secs) {
787 error_report("invalid physical cyls number");
791 error_report("invalid physical heads number");
795 error_report("invalid physical secs number");
800 translation = BIOS_ATA_TRANSLATION_AUTO;
801 value = qemu_opt_get(legacy_opts, "trans");
804 error_report("'%s' trans must be used with cyls, heads and secs",
808 if (!strcmp(value, "none")) {
809 translation = BIOS_ATA_TRANSLATION_NONE;
810 } else if (!strcmp(value, "lba")) {
811 translation = BIOS_ATA_TRANSLATION_LBA;
812 } else if (!strcmp(value, "large")) {
813 translation = BIOS_ATA_TRANSLATION_LARGE;
814 } else if (!strcmp(value, "rechs")) {
815 translation = BIOS_ATA_TRANSLATION_RECHS;
816 } else if (!strcmp(value, "auto")) {
817 translation = BIOS_ATA_TRANSLATION_AUTO;
819 error_report("'%s' invalid translation type", value);
824 if (media == MEDIA_CDROM) {
825 if (cyls || secs || heads) {
826 error_report("CHS can't be set with media=cdrom");
831 /* Device address specified by bus/unit or index.
832 * If none was specified, try to find the first free one. */
833 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
834 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
835 index = qemu_opt_get_number(legacy_opts, "index", -1);
837 max_devs = if_max_devs[type];
840 if (bus_id != 0 || unit_id != -1) {
841 error_report("index cannot be used with bus and unit");
844 bus_id = drive_index_to_bus_id(type, index);
845 unit_id = drive_index_to_unit_id(type, index);
850 while (drive_get(type, bus_id, unit_id) != NULL) {
852 if (max_devs && unit_id >= max_devs) {
859 if (max_devs && unit_id >= max_devs) {
860 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
864 if (drive_get(type, bus_id, unit_id) != NULL) {
865 error_report("drive with bus=%d, unit=%d (index=%d) exists",
866 bus_id, unit_id, index);
871 serial = qemu_opt_get(legacy_opts, "serial");
873 /* no id supplied -> create one */
874 if (qemu_opts_id(all_opts) == NULL) {
876 const char *mediastr = "";
877 if (type == IF_IDE || type == IF_SCSI) {
878 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
881 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
884 new_id = g_strdup_printf("%s%s%i", if_name[type],
887 qdict_put(bs_opts, "id", qstring_from_str(new_id));
891 /* Add virtio block device */
892 devaddr = qemu_opt_get(legacy_opts, "addr");
893 if (devaddr && type != IF_VIRTIO) {
894 error_report("addr is not supported by this bus type");
898 if (type == IF_VIRTIO) {
900 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
902 if (arch_type == QEMU_ARCH_S390X) {
903 qemu_opt_set(devopts, "driver", "virtio-blk-s390");
905 qemu_opt_set(devopts, "driver", "virtio-blk-pci");
907 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"));
909 qemu_opt_set(devopts, "addr", devaddr);
913 filename = qemu_opt_get(legacy_opts, "file");
915 /* Check werror/rerror compatibility with if=... */
916 werror = qemu_opt_get(legacy_opts, "werror");
917 if (werror != NULL) {
918 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
920 error_report("werror is not supported by this bus type");
923 qdict_put(bs_opts, "werror", qstring_from_str(werror));
926 rerror = qemu_opt_get(legacy_opts, "rerror");
927 if (rerror != NULL) {
928 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
930 error_report("rerror is not supported by this bus type");
933 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
936 /* Actual block device init: Functionality shared with blockdev-add */
937 dinfo = blockdev_init(filename, bs_opts, &local_err);
941 error_report("%s", error_get_pretty(local_err));
942 error_free(local_err);
949 /* Set legacy DriveInfo fields */
950 dinfo->enable_auto_del = true;
951 dinfo->opts = all_opts;
954 dinfo->heads = heads;
956 dinfo->trans = translation;
960 dinfo->unit = unit_id;
961 dinfo->devaddr = devaddr;
963 dinfo->serial = g_strdup(serial);
970 dinfo->media_cd = media == MEDIA_CDROM;
977 qemu_opts_del(legacy_opts);
982 void do_commit(Monitor *mon, const QDict *qdict)
984 const char *device = qdict_get_str(qdict, "device");
985 BlockDriverState *bs;
988 if (!strcmp(device, "all")) {
989 ret = bdrv_commit_all();
991 bs = bdrv_find(device);
993 monitor_printf(mon, "Device '%s' not found\n", device);
996 ret = bdrv_commit(bs);
999 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1004 static void blockdev_do_action(int kind, void *data, Error **errp)
1006 TransactionAction action;
1007 TransactionActionList list;
1011 list.value = &action;
1013 qmp_transaction(&list, errp);
1016 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1017 bool has_node_name, const char *node_name,
1018 const char *snapshot_file,
1019 bool has_snapshot_node_name,
1020 const char *snapshot_node_name,
1021 bool has_format, const char *format,
1022 bool has_mode, NewImageMode mode, Error **errp)
1024 BlockdevSnapshot snapshot = {
1025 .has_device = has_device,
1026 .device = (char *) device,
1027 .has_node_name = has_node_name,
1028 .node_name = (char *) node_name,
1029 .snapshot_file = (char *) snapshot_file,
1030 .has_snapshot_node_name = has_snapshot_node_name,
1031 .snapshot_node_name = (char *) snapshot_node_name,
1032 .has_format = has_format,
1033 .format = (char *) format,
1034 .has_mode = has_mode,
1037 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1041 void qmp_blockdev_snapshot_internal_sync(const char *device,
1045 BlockdevSnapshotInternal snapshot = {
1046 .device = (char *) device,
1047 .name = (char *) name
1050 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1054 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1061 BlockDriverState *bs = bdrv_find(device);
1062 QEMUSnapshotInfo sn;
1063 Error *local_err = NULL;
1064 SnapshotInfo *info = NULL;
1068 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1081 error_setg(errp, "Name or id must be provided");
1085 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1087 error_propagate(errp, local_err);
1092 "Snapshot with id '%s' and name '%s' does not exist on "
1094 STR_OR_NULL(id), STR_OR_NULL(name), device);
1098 bdrv_snapshot_delete(bs, id, name, &local_err);
1100 error_propagate(errp, local_err);
1104 info = g_new0(SnapshotInfo, 1);
1105 info->id = g_strdup(sn.id_str);
1106 info->name = g_strdup(sn.name);
1107 info->date_nsec = sn.date_nsec;
1108 info->date_sec = sn.date_sec;
1109 info->vm_state_size = sn.vm_state_size;
1110 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1111 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1116 /* New and old BlockDriverState structs for group snapshots */
1118 typedef struct BlkTransactionState BlkTransactionState;
1120 /* Only prepare() may fail. In a single transaction, only one of commit() or
1121 abort() will be called, clean() will always be called if it present. */
1122 typedef struct BdrvActionOps {
1123 /* Size of state struct, in bytes. */
1124 size_t instance_size;
1125 /* Prepare the work, must NOT be NULL. */
1126 void (*prepare)(BlkTransactionState *common, Error **errp);
1127 /* Commit the changes, can be NULL. */
1128 void (*commit)(BlkTransactionState *common);
1129 /* Abort the changes on fail, can be NULL. */
1130 void (*abort)(BlkTransactionState *common);
1131 /* Clean up resource in the end, can be NULL. */
1132 void (*clean)(BlkTransactionState *common);
1136 * This structure must be arranged as first member in child type, assuming
1137 * that compiler will also arrange it to the same address with parent instance.
1138 * Later it will be used in free().
1140 struct BlkTransactionState {
1141 TransactionAction *action;
1142 const BdrvActionOps *ops;
1143 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
1146 /* internal snapshot private data */
1147 typedef struct InternalSnapshotState {
1148 BlkTransactionState common;
1149 BlockDriverState *bs;
1150 QEMUSnapshotInfo sn;
1151 } InternalSnapshotState;
1153 static void internal_snapshot_prepare(BlkTransactionState *common,
1156 Error *local_err = NULL;
1159 BlockDriverState *bs;
1160 QEMUSnapshotInfo old_sn, *sn;
1163 BlockdevSnapshotInternal *internal;
1164 InternalSnapshotState *state;
1167 g_assert(common->action->kind ==
1168 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1169 internal = common->action->blockdev_snapshot_internal_sync;
1170 state = DO_UPCAST(InternalSnapshotState, common, common);
1172 /* 1. parse input */
1173 device = internal->device;
1174 name = internal->name;
1176 /* 2. check for validation */
1177 bs = bdrv_find(device);
1179 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1183 if (!bdrv_is_inserted(bs)) {
1184 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1188 if (bdrv_is_read_only(bs)) {
1189 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1193 if (!bdrv_can_snapshot(bs)) {
1194 error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
1195 bs->drv->format_name, device, "internal snapshot");
1199 if (!strlen(name)) {
1200 error_setg(errp, "Name is empty");
1204 /* check whether a snapshot with name exist */
1205 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1208 error_propagate(errp, local_err);
1212 "Snapshot with name '%s' already exists on device '%s'",
1217 /* 3. take the snapshot */
1219 pstrcpy(sn->name, sizeof(sn->name), name);
1220 qemu_gettimeofday(&tv);
1221 sn->date_sec = tv.tv_sec;
1222 sn->date_nsec = tv.tv_usec * 1000;
1223 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1225 ret1 = bdrv_snapshot_create(bs, sn);
1227 error_setg_errno(errp, -ret1,
1228 "Failed to create snapshot '%s' on device '%s'",
1233 /* 4. succeed, mark a snapshot is created */
1237 static void internal_snapshot_abort(BlkTransactionState *common)
1239 InternalSnapshotState *state =
1240 DO_UPCAST(InternalSnapshotState, common, common);
1241 BlockDriverState *bs = state->bs;
1242 QEMUSnapshotInfo *sn = &state->sn;
1243 Error *local_error = NULL;
1249 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1250 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1251 "device '%s' in abort: %s",
1254 bdrv_get_device_name(bs),
1255 error_get_pretty(local_error));
1256 error_free(local_error);
1260 /* external snapshot private data */
1261 typedef struct ExternalSnapshotState {
1262 BlkTransactionState common;
1263 BlockDriverState *old_bs;
1264 BlockDriverState *new_bs;
1265 } ExternalSnapshotState;
1267 static void external_snapshot_prepare(BlkTransactionState *common,
1272 QDict *options = NULL;
1273 Error *local_err = NULL;
1274 bool has_device = false;
1276 bool has_node_name = false;
1277 const char *node_name;
1278 bool has_snapshot_node_name = false;
1279 const char *snapshot_node_name;
1280 const char *new_image_file;
1281 const char *format = "qcow2";
1282 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1283 ExternalSnapshotState *state =
1284 DO_UPCAST(ExternalSnapshotState, common, common);
1285 TransactionAction *action = common->action;
1287 /* get parameters */
1288 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
1290 has_device = action->blockdev_snapshot_sync->has_device;
1291 device = action->blockdev_snapshot_sync->device;
1292 has_node_name = action->blockdev_snapshot_sync->has_node_name;
1293 node_name = action->blockdev_snapshot_sync->node_name;
1294 has_snapshot_node_name =
1295 action->blockdev_snapshot_sync->has_snapshot_node_name;
1296 snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name;
1298 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1299 if (action->blockdev_snapshot_sync->has_format) {
1300 format = action->blockdev_snapshot_sync->format;
1302 if (action->blockdev_snapshot_sync->has_mode) {
1303 mode = action->blockdev_snapshot_sync->mode;
1306 /* start processing */
1307 drv = bdrv_find_format(format);
1309 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1313 state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
1314 has_node_name ? node_name : NULL,
1317 error_propagate(errp, local_err);
1321 if (has_node_name && !has_snapshot_node_name) {
1322 error_setg(errp, "New snapshot node name missing");
1326 if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) {
1327 error_setg(errp, "New snapshot node name already existing");
1331 if (!bdrv_is_inserted(state->old_bs)) {
1332 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1336 if (bdrv_op_is_blocked(state->old_bs,
1337 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1341 if (!bdrv_is_read_only(state->old_bs)) {
1342 if (bdrv_flush(state->old_bs)) {
1343 error_set(errp, QERR_IO_ERROR);
1348 if (!bdrv_is_first_non_filter(state->old_bs)) {
1349 error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
1353 flags = state->old_bs->open_flags;
1355 /* create new image w/backing file */
1356 if (mode != NEW_IMAGE_MODE_EXISTING) {
1357 bdrv_img_create(new_image_file, format,
1358 state->old_bs->filename,
1359 state->old_bs->drv->format_name,
1360 NULL, -1, flags, &local_err, false);
1362 error_propagate(errp, local_err);
1367 if (has_snapshot_node_name) {
1368 options = qdict_new();
1369 qdict_put(options, "node-name",
1370 qstring_from_str(snapshot_node_name));
1373 /* TODO Inherit bs->options or only take explicit options with an
1374 * extended QMP command? */
1375 assert(state->new_bs == NULL);
1376 ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
1377 flags | BDRV_O_NO_BACKING, drv, &local_err);
1378 /* We will manually add the backing_hd field to the bs later */
1380 error_propagate(errp, local_err);
1384 static void external_snapshot_commit(BlkTransactionState *common)
1386 ExternalSnapshotState *state =
1387 DO_UPCAST(ExternalSnapshotState, common, common);
1389 /* This removes our old bs and adds the new bs */
1390 bdrv_append(state->new_bs, state->old_bs);
1391 /* We don't need (or want) to use the transactional
1392 * bdrv_reopen_multiple() across all the entries at once, because we
1393 * don't want to abort all of them if one of them fails the reopen */
1394 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
1398 static void external_snapshot_abort(BlkTransactionState *common)
1400 ExternalSnapshotState *state =
1401 DO_UPCAST(ExternalSnapshotState, common, common);
1402 if (state->new_bs) {
1403 bdrv_unref(state->new_bs);
1407 typedef struct DriveBackupState {
1408 BlkTransactionState common;
1409 BlockDriverState *bs;
1413 static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1415 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1416 DriveBackup *backup;
1417 Error *local_err = NULL;
1419 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1420 backup = common->action->drive_backup;
1422 qmp_drive_backup(backup->device, backup->target,
1423 backup->has_format, backup->format,
1425 backup->has_mode, backup->mode,
1426 backup->has_speed, backup->speed,
1427 backup->has_on_source_error, backup->on_source_error,
1428 backup->has_on_target_error, backup->on_target_error,
1431 error_propagate(errp, local_err);
1437 state->bs = bdrv_find(backup->device);
1438 state->job = state->bs->job;
1441 static void drive_backup_abort(BlkTransactionState *common)
1443 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1444 BlockDriverState *bs = state->bs;
1446 /* Only cancel if it's the job we started */
1447 if (bs && bs->job && bs->job == state->job) {
1448 block_job_cancel_sync(bs->job);
1452 static void abort_prepare(BlkTransactionState *common, Error **errp)
1454 error_setg(errp, "Transaction aborted using Abort action");
1457 static void abort_commit(BlkTransactionState *common)
1459 g_assert_not_reached(); /* this action never succeeds */
1462 static const BdrvActionOps actions[] = {
1463 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1464 .instance_size = sizeof(ExternalSnapshotState),
1465 .prepare = external_snapshot_prepare,
1466 .commit = external_snapshot_commit,
1467 .abort = external_snapshot_abort,
1469 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1470 .instance_size = sizeof(DriveBackupState),
1471 .prepare = drive_backup_prepare,
1472 .abort = drive_backup_abort,
1474 [TRANSACTION_ACTION_KIND_ABORT] = {
1475 .instance_size = sizeof(BlkTransactionState),
1476 .prepare = abort_prepare,
1477 .commit = abort_commit,
1479 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1480 .instance_size = sizeof(InternalSnapshotState),
1481 .prepare = internal_snapshot_prepare,
1482 .abort = internal_snapshot_abort,
1487 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1488 * then we do not pivot any of the devices in the group, and abandon the
1491 void qmp_transaction(TransactionActionList *dev_list, Error **errp)
1493 TransactionActionList *dev_entry = dev_list;
1494 BlkTransactionState *state, *next;
1495 Error *local_err = NULL;
1497 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
1498 QSIMPLEQ_INIT(&snap_bdrv_states);
1500 /* drain all i/o before any snapshots */
1503 /* We don't do anything in this loop that commits us to the snapshot */
1504 while (NULL != dev_entry) {
1505 TransactionAction *dev_info = NULL;
1506 const BdrvActionOps *ops;
1508 dev_info = dev_entry->value;
1509 dev_entry = dev_entry->next;
1511 assert(dev_info->kind < ARRAY_SIZE(actions));
1513 ops = &actions[dev_info->kind];
1514 assert(ops->instance_size > 0);
1516 state = g_malloc0(ops->instance_size);
1518 state->action = dev_info;
1519 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
1521 state->ops->prepare(state, &local_err);
1523 error_propagate(errp, local_err);
1524 goto delete_and_fail;
1528 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1529 if (state->ops->commit) {
1530 state->ops->commit(state);
1539 * failure, and it is all-or-none; abandon each new bs, and keep using
1540 * the original bs for all images
1542 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1543 if (state->ops->abort) {
1544 state->ops->abort(state);
1548 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1549 if (state->ops->clean) {
1550 state->ops->clean(state);
1557 static void eject_device(BlockDriverState *bs, int force, Error **errp)
1559 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
1562 if (!bdrv_dev_has_removable_media(bs)) {
1563 error_setg(errp, "Device '%s' is not removable",
1564 bdrv_get_device_name(bs));
1568 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
1569 bdrv_dev_eject_request(bs, force);
1571 error_setg(errp, "Device '%s' is locked",
1572 bdrv_get_device_name(bs));
1580 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1582 BlockDriverState *bs;
1584 bs = bdrv_find(device);
1586 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1590 eject_device(bs, force, errp);
1593 void qmp_block_passwd(bool has_device, const char *device,
1594 bool has_node_name, const char *node_name,
1595 const char *password, Error **errp)
1597 Error *local_err = NULL;
1598 BlockDriverState *bs;
1601 bs = bdrv_lookup_bs(has_device ? device : NULL,
1602 has_node_name ? node_name : NULL,
1605 error_propagate(errp, local_err);
1609 err = bdrv_set_key(bs, password);
1610 if (err == -EINVAL) {
1611 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1613 } else if (err < 0) {
1614 error_set(errp, QERR_INVALID_PASSWORD);
1619 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1620 int bdrv_flags, BlockDriver *drv,
1621 const char *password, Error **errp)
1623 Error *local_err = NULL;
1626 ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err);
1628 error_propagate(errp, local_err);
1632 if (bdrv_key_required(bs)) {
1634 if (bdrv_set_key(bs, password) < 0) {
1635 error_set(errp, QERR_INVALID_PASSWORD);
1638 error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
1639 bdrv_get_encrypted_filename(bs));
1641 } else if (password) {
1642 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1646 void qmp_change_blockdev(const char *device, const char *filename,
1647 const char *format, Error **errp)
1649 BlockDriverState *bs;
1650 BlockDriver *drv = NULL;
1654 bs = bdrv_find(device);
1656 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1661 drv = bdrv_find_whitelisted_format(format, bs->read_only);
1663 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1668 eject_device(bs, 0, &err);
1670 error_propagate(errp, err);
1674 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1675 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1677 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1680 /* throttling disk I/O limits */
1681 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1688 bool has_bps_rd_max,
1690 bool has_bps_wr_max,
1694 bool has_iops_rd_max,
1695 int64_t iops_rd_max,
1696 bool has_iops_wr_max,
1697 int64_t iops_wr_max,
1699 int64_t iops_size, Error **errp)
1702 BlockDriverState *bs;
1703 AioContext *aio_context;
1705 bs = bdrv_find(device);
1707 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1711 memset(&cfg, 0, sizeof(cfg));
1712 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1713 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
1714 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
1716 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1717 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
1718 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
1721 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
1723 if (has_bps_rd_max) {
1724 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
1726 if (has_bps_wr_max) {
1727 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
1730 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
1732 if (has_iops_rd_max) {
1733 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
1735 if (has_iops_wr_max) {
1736 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
1739 if (has_iops_size) {
1740 cfg.op_size = iops_size;
1743 if (!check_throttle_config(&cfg, errp)) {
1747 aio_context = bdrv_get_aio_context(bs);
1748 aio_context_acquire(aio_context);
1750 if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
1751 bdrv_io_limits_enable(bs);
1752 } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
1753 bdrv_io_limits_disable(bs);
1756 if (bs->io_limits_enabled) {
1757 bdrv_set_io_limits(bs, &cfg);
1760 aio_context_release(aio_context);
1763 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1765 const char *id = qdict_get_str(qdict, "id");
1766 BlockDriverState *bs;
1768 AioContext *aio_context;
1769 Error *local_err = NULL;
1773 error_report("Device '%s' not found", id);
1777 dinfo = drive_get_by_blockdev(bs);
1778 if (dinfo && !dinfo->enable_auto_del) {
1779 error_report("Deleting device added with blockdev-add"
1780 " is not supported");
1784 aio_context = bdrv_get_aio_context(bs);
1785 aio_context_acquire(aio_context);
1787 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
1788 error_report("%s", error_get_pretty(local_err));
1789 error_free(local_err);
1790 aio_context_release(aio_context);
1794 /* quiesce block driver; prevent further io */
1799 /* if we have a device attached to this BlockDriverState
1800 * then we need to make the drive anonymous until the device
1801 * can be removed. If this is a drive with no device backing
1802 * then we can just get rid of the block driver state right here.
1804 if (bdrv_get_attached_dev(bs)) {
1807 /* Further I/O must not pause the guest */
1808 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
1809 BLOCKDEV_ON_ERROR_REPORT);
1814 aio_context_release(aio_context);
1818 void qmp_block_resize(bool has_device, const char *device,
1819 bool has_node_name, const char *node_name,
1820 int64_t size, Error **errp)
1822 Error *local_err = NULL;
1823 BlockDriverState *bs;
1824 AioContext *aio_context;
1827 bs = bdrv_lookup_bs(has_device ? device : NULL,
1828 has_node_name ? node_name : NULL,
1831 error_propagate(errp, local_err);
1835 aio_context = bdrv_get_aio_context(bs);
1836 aio_context_acquire(aio_context);
1838 if (!bdrv_is_first_non_filter(bs)) {
1839 error_set(errp, QERR_FEATURE_DISABLED, "resize");
1844 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
1848 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
1849 error_set(errp, QERR_DEVICE_IN_USE, device);
1853 /* complete all in-flight operations before resizing the device */
1856 ret = bdrv_truncate(bs, size);
1861 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1864 error_set(errp, QERR_UNSUPPORTED);
1867 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1870 error_set(errp, QERR_DEVICE_IN_USE, device);
1873 error_setg_errno(errp, -ret, "Could not resize");
1878 aio_context_release(aio_context);
1881 static void block_job_cb(void *opaque, int ret)
1883 BlockDriverState *bs = opaque;
1884 const char *msg = NULL;
1886 trace_block_job_cb(bs, bs->job, ret);
1891 msg = strerror(-ret);
1894 if (block_job_is_cancelled(bs->job)) {
1895 block_job_event_cancelled(bs->job);
1897 block_job_event_completed(bs->job, msg);
1900 bdrv_put_ref_bh_schedule(bs);
1903 void qmp_block_stream(const char *device,
1904 bool has_base, const char *base,
1905 bool has_backing_file, const char *backing_file,
1906 bool has_speed, int64_t speed,
1907 bool has_on_error, BlockdevOnError on_error,
1910 BlockDriverState *bs;
1911 BlockDriverState *base_bs = NULL;
1912 Error *local_err = NULL;
1913 const char *base_name = NULL;
1915 if (!has_on_error) {
1916 on_error = BLOCKDEV_ON_ERROR_REPORT;
1919 bs = bdrv_find(device);
1921 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1925 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
1930 base_bs = bdrv_find_backing_image(bs, base);
1931 if (base_bs == NULL) {
1932 error_set(errp, QERR_BASE_NOT_FOUND, base);
1938 /* if we are streaming the entire chain, the result will have no backing
1939 * file, and specifying one is therefore an error */
1940 if (base_bs == NULL && has_backing_file) {
1941 error_setg(errp, "backing file specified, but streaming the "
1946 /* backing_file string overrides base bs filename */
1947 base_name = has_backing_file ? backing_file : base_name;
1949 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
1950 on_error, block_job_cb, bs, &local_err);
1952 error_propagate(errp, local_err);
1956 trace_qmp_block_stream(bs, bs->job);
1959 void qmp_block_commit(const char *device,
1960 bool has_base, const char *base,
1961 bool has_top, const char *top,
1962 bool has_backing_file, const char *backing_file,
1963 bool has_speed, int64_t speed,
1966 BlockDriverState *bs;
1967 BlockDriverState *base_bs, *top_bs;
1968 Error *local_err = NULL;
1969 /* This will be part of the QMP command, if/when the
1970 * BlockdevOnError change for blkmirror makes it in
1972 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
1978 /* drain all i/o before commits */
1982 * libvirt relies on the DeviceNotFound error class in order to probe for
1983 * live commit feature versions; for this to work, we must make sure to
1984 * perform the device lookup before any generic errors that may occur in a
1985 * scenario in which all optional arguments are omitted. */
1986 bs = bdrv_find(device);
1988 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1992 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT, errp)) {
1996 /* default top_bs is the active layer */
1999 if (has_top && top) {
2000 if (strcmp(bs->filename, top) != 0) {
2001 top_bs = bdrv_find_backing_image(bs, top);
2005 if (top_bs == NULL) {
2006 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
2010 if (has_base && base) {
2011 base_bs = bdrv_find_backing_image(top_bs, base);
2013 base_bs = bdrv_find_base(top_bs);
2016 if (base_bs == NULL) {
2017 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
2021 /* Do not allow attempts to commit an image into itself */
2022 if (top_bs == base_bs) {
2023 error_setg(errp, "cannot commit an image into itself");
2028 if (has_backing_file) {
2029 error_setg(errp, "'backing-file' specified,"
2030 " but 'top' is the active layer");
2033 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
2036 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
2037 has_backing_file ? backing_file : NULL, &local_err);
2039 if (local_err != NULL) {
2040 error_propagate(errp, local_err);
2045 void qmp_drive_backup(const char *device, const char *target,
2046 bool has_format, const char *format,
2047 enum MirrorSyncMode sync,
2048 bool has_mode, enum NewImageMode mode,
2049 bool has_speed, int64_t speed,
2050 bool has_on_source_error, BlockdevOnError on_source_error,
2051 bool has_on_target_error, BlockdevOnError on_target_error,
2054 BlockDriverState *bs;
2055 BlockDriverState *target_bs;
2056 BlockDriverState *source = NULL;
2057 BlockDriver *drv = NULL;
2058 Error *local_err = NULL;
2066 if (!has_on_source_error) {
2067 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2069 if (!has_on_target_error) {
2070 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2073 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2076 bs = bdrv_find(device);
2078 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2082 if (!bdrv_is_inserted(bs)) {
2083 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2088 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2091 drv = bdrv_find_format(format);
2093 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
2098 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
2102 flags = bs->open_flags | BDRV_O_RDWR;
2104 /* See if we have a backing HD we can use to create our new image
2106 if (sync == MIRROR_SYNC_MODE_TOP) {
2107 source = bs->backing_hd;
2109 sync = MIRROR_SYNC_MODE_FULL;
2112 if (sync == MIRROR_SYNC_MODE_NONE) {
2116 size = bdrv_getlength(bs);
2118 error_setg_errno(errp, -size, "bdrv_getlength failed");
2122 if (mode != NEW_IMAGE_MODE_EXISTING) {
2123 assert(format && drv);
2125 bdrv_img_create(target, format, source->filename,
2126 source->drv->format_name, NULL,
2127 size, flags, &local_err, false);
2129 bdrv_img_create(target, format, NULL, NULL, NULL,
2130 size, flags, &local_err, false);
2135 error_propagate(errp, local_err);
2140 ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err);
2142 error_propagate(errp, local_err);
2146 backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
2147 block_job_cb, bs, &local_err);
2148 if (local_err != NULL) {
2149 bdrv_unref(target_bs);
2150 error_propagate(errp, local_err);
2155 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
2157 return bdrv_named_nodes_list();
2160 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2162 void qmp_drive_mirror(const char *device, const char *target,
2163 bool has_format, const char *format,
2164 bool has_node_name, const char *node_name,
2165 bool has_replaces, const char *replaces,
2166 enum MirrorSyncMode sync,
2167 bool has_mode, enum NewImageMode mode,
2168 bool has_speed, int64_t speed,
2169 bool has_granularity, uint32_t granularity,
2170 bool has_buf_size, int64_t buf_size,
2171 bool has_on_source_error, BlockdevOnError on_source_error,
2172 bool has_on_target_error, BlockdevOnError on_target_error,
2175 BlockDriverState *bs;
2176 BlockDriverState *source, *target_bs;
2177 BlockDriver *drv = NULL;
2178 Error *local_err = NULL;
2179 QDict *options = NULL;
2187 if (!has_on_source_error) {
2188 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2190 if (!has_on_target_error) {
2191 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2194 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2196 if (!has_granularity) {
2199 if (!has_buf_size) {
2200 buf_size = DEFAULT_MIRROR_BUF_SIZE;
2203 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
2204 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2205 "a value in range [512B, 64MB]");
2208 if (granularity & (granularity - 1)) {
2209 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2");
2213 bs = bdrv_find(device);
2215 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2219 if (!bdrv_is_inserted(bs)) {
2220 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2225 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2228 drv = bdrv_find_format(format);
2230 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
2235 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
2239 flags = bs->open_flags | BDRV_O_RDWR;
2240 source = bs->backing_hd;
2241 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
2242 sync = MIRROR_SYNC_MODE_FULL;
2244 if (sync == MIRROR_SYNC_MODE_NONE) {
2248 size = bdrv_getlength(bs);
2250 error_setg_errno(errp, -size, "bdrv_getlength failed");
2255 BlockDriverState *to_replace_bs;
2257 if (!has_node_name) {
2258 error_setg(errp, "a node-name must be provided when replacing a"
2259 " named node of the graph");
2263 to_replace_bs = check_to_replace_node(replaces, &local_err);
2265 if (!to_replace_bs) {
2266 error_propagate(errp, local_err);
2270 if (size != bdrv_getlength(to_replace_bs)) {
2271 error_setg(errp, "cannot replace image with a mirror image of "
2277 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
2278 && mode != NEW_IMAGE_MODE_EXISTING)
2280 /* create new image w/o backing file */
2281 assert(format && drv);
2282 bdrv_img_create(target, format,
2283 NULL, NULL, NULL, size, flags, &local_err, false);
2286 case NEW_IMAGE_MODE_EXISTING:
2288 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2289 /* create new image with backing file */
2290 bdrv_img_create(target, format,
2292 source->drv->format_name,
2293 NULL, size, flags, &local_err, false);
2301 error_propagate(errp, local_err);
2305 if (has_node_name) {
2306 options = qdict_new();
2307 qdict_put(options, "node-name", qstring_from_str(node_name));
2310 /* Mirroring takes care of copy-on-write using the source's backing
2314 ret = bdrv_open(&target_bs, target, NULL, options,
2315 flags | BDRV_O_NO_BACKING, drv, &local_err);
2317 error_propagate(errp, local_err);
2321 /* pass the node name to replace to mirror start since it's loose coupling
2322 * and will allow to check whether the node still exist at mirror completion
2324 mirror_start(bs, target_bs,
2325 has_replaces ? replaces : NULL,
2326 speed, granularity, buf_size, sync,
2327 on_source_error, on_target_error,
2328 block_job_cb, bs, &local_err);
2329 if (local_err != NULL) {
2330 bdrv_unref(target_bs);
2331 error_propagate(errp, local_err);
2336 static BlockJob *find_block_job(const char *device)
2338 BlockDriverState *bs;
2340 bs = bdrv_find(device);
2341 if (!bs || !bs->job) {
2347 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2349 BlockJob *job = find_block_job(device);
2352 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2356 block_job_set_speed(job, speed, errp);
2359 void qmp_block_job_cancel(const char *device,
2360 bool has_force, bool force, Error **errp)
2362 BlockJob *job = find_block_job(device);
2369 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2372 if (job->paused && !force) {
2373 error_setg(errp, "The block job for device '%s' is currently paused",
2378 trace_qmp_block_job_cancel(job);
2379 block_job_cancel(job);
2382 void qmp_block_job_pause(const char *device, Error **errp)
2384 BlockJob *job = find_block_job(device);
2387 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2391 trace_qmp_block_job_pause(job);
2392 block_job_pause(job);
2395 void qmp_block_job_resume(const char *device, Error **errp)
2397 BlockJob *job = find_block_job(device);
2400 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2404 trace_qmp_block_job_resume(job);
2405 block_job_resume(job);
2408 void qmp_block_job_complete(const char *device, Error **errp)
2410 BlockJob *job = find_block_job(device);
2413 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2417 trace_qmp_block_job_complete(job);
2418 block_job_complete(job, errp);
2421 void qmp_change_backing_file(const char *device,
2422 const char *image_node_name,
2423 const char *backing_file,
2426 BlockDriverState *bs = NULL;
2427 BlockDriverState *image_bs = NULL;
2428 Error *local_err = NULL;
2433 /* find the top layer BDS of the chain */
2434 bs = bdrv_find(device);
2436 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2440 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
2442 error_propagate(errp, local_err);
2447 error_setg(errp, "image file not found");
2451 if (bdrv_find_base(image_bs) == image_bs) {
2452 error_setg(errp, "not allowing backing file change on an image "
2453 "without a backing file");
2457 /* even though we are not necessarily operating on bs, we need it to
2458 * determine if block ops are currently prohibited on the chain */
2459 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
2463 /* final sanity check */
2464 if (!bdrv_chain_contains(bs, image_bs)) {
2465 error_setg(errp, "'%s' and image file are not in the same chain",
2470 /* if not r/w, reopen to make r/w */
2471 open_flags = image_bs->open_flags;
2472 ro = bdrv_is_read_only(image_bs);
2475 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
2477 error_propagate(errp, local_err);
2482 ret = bdrv_change_backing_file(image_bs, backing_file,
2483 image_bs->drv ? image_bs->drv->format_name : "");
2486 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
2488 /* don't exit here, so we can try to restore open flags if
2493 bdrv_reopen(image_bs, open_flags, &local_err);
2495 error_propagate(errp, local_err); /* will preserve prior errp */
2500 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
2502 QmpOutputVisitor *ov = qmp_output_visitor_new();
2506 Error *local_err = NULL;
2508 /* Require an ID in the top level */
2509 if (!options->has_id) {
2510 error_setg(errp, "Block device needs an ID");
2514 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
2515 * cache.direct=false instead of silently switching to aio=threads, except
2516 * when called from drive_new().
2518 * For now, simply forbidding the combination for all drivers will do. */
2519 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
2520 bool direct = options->has_cache &&
2521 options->cache->has_direct &&
2522 options->cache->direct;
2524 error_setg(errp, "aio=native requires cache.direct=true");
2529 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
2530 &options, NULL, &local_err);
2532 error_propagate(errp, local_err);
2536 obj = qmp_output_get_qobject(ov);
2537 qdict = qobject_to_qdict(obj);
2539 qdict_flatten(qdict);
2541 dinfo = blockdev_init(NULL, qdict, &local_err);
2543 error_propagate(errp, local_err);
2547 if (bdrv_key_required(dinfo->bdrv)) {
2549 error_setg(errp, "blockdev-add doesn't support encrypted devices");
2554 qmp_output_visitor_cleanup(ov);
2557 static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
2559 BlockJobInfoList **prev = opaque;
2560 BlockJob *job = bs->job;
2563 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
2564 elem->value = block_job_query(bs->job);
2565 (*prev)->next = elem;
2570 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
2572 /* Dummy is a fake list element for holding the head pointer */
2573 BlockJobInfoList dummy = {};
2574 BlockJobInfoList *prev = &dummy;
2575 bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
2579 QemuOptsList qemu_common_drive_opts = {
2581 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
2585 .type = QEMU_OPT_BOOL,
2586 .help = "enable/disable snapshot mode",
2589 .type = QEMU_OPT_STRING,
2590 .help = "discard operation (ignore/off, unmap/on)",
2592 .name = "cache.writeback",
2593 .type = QEMU_OPT_BOOL,
2594 .help = "enables writeback mode for any caches",
2596 .name = "cache.direct",
2597 .type = QEMU_OPT_BOOL,
2598 .help = "enables use of O_DIRECT (bypass the host page cache)",
2600 .name = "cache.no-flush",
2601 .type = QEMU_OPT_BOOL,
2602 .help = "ignore any flush requests for the device",
2605 .type = QEMU_OPT_STRING,
2606 .help = "host AIO implementation (threads, native)",
2609 .type = QEMU_OPT_STRING,
2610 .help = "disk format (raw, qcow2, ...)",
2613 .type = QEMU_OPT_STRING,
2614 .help = "read error action",
2617 .type = QEMU_OPT_STRING,
2618 .help = "write error action",
2620 .name = "read-only",
2621 .type = QEMU_OPT_BOOL,
2622 .help = "open drive file as read-only",
2624 .name = "throttling.iops-total",
2625 .type = QEMU_OPT_NUMBER,
2626 .help = "limit total I/O operations per second",
2628 .name = "throttling.iops-read",
2629 .type = QEMU_OPT_NUMBER,
2630 .help = "limit read operations per second",
2632 .name = "throttling.iops-write",
2633 .type = QEMU_OPT_NUMBER,
2634 .help = "limit write operations per second",
2636 .name = "throttling.bps-total",
2637 .type = QEMU_OPT_NUMBER,
2638 .help = "limit total bytes per second",
2640 .name = "throttling.bps-read",
2641 .type = QEMU_OPT_NUMBER,
2642 .help = "limit read bytes per second",
2644 .name = "throttling.bps-write",
2645 .type = QEMU_OPT_NUMBER,
2646 .help = "limit write bytes per second",
2648 .name = "throttling.iops-total-max",
2649 .type = QEMU_OPT_NUMBER,
2650 .help = "I/O operations burst",
2652 .name = "throttling.iops-read-max",
2653 .type = QEMU_OPT_NUMBER,
2654 .help = "I/O operations read burst",
2656 .name = "throttling.iops-write-max",
2657 .type = QEMU_OPT_NUMBER,
2658 .help = "I/O operations write burst",
2660 .name = "throttling.bps-total-max",
2661 .type = QEMU_OPT_NUMBER,
2662 .help = "total bytes burst",
2664 .name = "throttling.bps-read-max",
2665 .type = QEMU_OPT_NUMBER,
2666 .help = "total bytes read burst",
2668 .name = "throttling.bps-write-max",
2669 .type = QEMU_OPT_NUMBER,
2670 .help = "total bytes write burst",
2672 .name = "throttling.iops-size",
2673 .type = QEMU_OPT_NUMBER,
2674 .help = "when limiting by iops max size of an I/O in bytes",
2676 .name = "copy-on-read",
2677 .type = QEMU_OPT_BOOL,
2678 .help = "copy read data from backing file into image file",
2680 .name = "detect-zeroes",
2681 .type = QEMU_OPT_STRING,
2682 .help = "try to optimize zero writes (off, on, unmap)",
2684 { /* end of list */ }
2688 QemuOptsList qemu_drive_opts = {
2690 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
2693 * no elements => accept any params
2694 * validation will happen later
2696 { /* end of list */ }