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 "qapi/qmp/qerror.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 "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
);
50 extern QemuOptsList qemu_common_drive_opts
;
52 static const char *const if_name
[IF_COUNT
] = {
56 [IF_FLOPPY
] = "floppy",
57 [IF_PFLASH
] = "pflash",
60 [IF_VIRTIO
] = "virtio",
64 static const 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 * We automatically delete the drive when a device using it gets
85 * unplugged. Questionable feature, but we can't just drop it.
86 * Device models call blockdev_mark_auto_del() to schedule the
87 * automatic deletion, and generic qdev code calls blockdev_auto_del()
88 * when deletion is actually safe.
90 void blockdev_mark_auto_del(BlockDriverState
*bs
)
92 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
94 if (dinfo
&& !dinfo
->enable_auto_del
) {
99 block_job_cancel(bs
->job
);
106 void blockdev_auto_del(BlockDriverState
*bs
)
108 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
110 if (dinfo
&& dinfo
->auto_del
) {
111 drive_put_ref(dinfo
);
115 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
117 int max_devs
= if_max_devs
[type
];
118 return max_devs
? index
/ max_devs
: 0;
121 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
123 int max_devs
= if_max_devs
[type
];
124 return max_devs
? index
% max_devs
: index
;
127 QemuOpts
*drive_def(const char *optstr
)
129 return qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
132 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
138 opts
= drive_def(optstr
);
142 if (type
!= IF_DEFAULT
) {
143 qemu_opt_set(opts
, "if", if_name
[type
]);
146 snprintf(buf
, sizeof(buf
), "%d", index
);
147 qemu_opt_set(opts
, "index", buf
);
150 qemu_opt_set(opts
, "file", file
);
154 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
158 /* seek interface, bus and unit */
160 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
161 if (dinfo
->type
== type
&&
170 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
172 return drive_get(type
,
173 drive_index_to_bus_id(type
, index
),
174 drive_index_to_unit_id(type
, index
));
177 int drive_get_max_bus(BlockInterfaceType type
)
183 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
184 if(dinfo
->type
== type
&&
185 dinfo
->bus
> max_bus
)
186 max_bus
= dinfo
->bus
;
191 /* Get a block device. This should only be used for single-drive devices
192 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
194 DriveInfo
*drive_get_next(BlockInterfaceType type
)
196 static int next_block_unit
[IF_COUNT
];
198 return drive_get(type
, 0, next_block_unit
[type
]++);
201 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
205 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
206 if (dinfo
->bdrv
== bs
) {
213 static void bdrv_format_print(void *opaque
, const char *name
)
215 error_printf(" %s", name
);
218 static void drive_uninit(DriveInfo
*dinfo
)
220 qemu_opts_del(dinfo
->opts
);
221 bdrv_unref(dinfo
->bdrv
);
223 QTAILQ_REMOVE(&drives
, dinfo
, next
);
224 g_free(dinfo
->serial
);
228 void drive_put_ref(DriveInfo
*dinfo
)
230 assert(dinfo
->refcount
);
231 if (--dinfo
->refcount
== 0) {
236 void drive_get_ref(DriveInfo
*dinfo
)
243 BlockDriverState
*bs
;
246 static void bdrv_put_ref_bh(void *opaque
)
248 BDRVPutRefBH
*s
= opaque
;
251 qemu_bh_delete(s
->bh
);
256 * Release a BDS reference in a BH
258 * It is not safe to use bdrv_unref() from a callback function when the callers
259 * still need the BlockDriverState. In such cases we schedule a BH to release
262 static void bdrv_put_ref_bh_schedule(BlockDriverState
*bs
)
266 s
= g_new(BDRVPutRefBH
, 1);
267 s
->bh
= qemu_bh_new(bdrv_put_ref_bh
, s
);
269 qemu_bh_schedule(s
->bh
);
272 static int parse_block_error_action(const char *buf
, bool is_read
)
274 if (!strcmp(buf
, "ignore")) {
275 return BLOCKDEV_ON_ERROR_IGNORE
;
276 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
277 return BLOCKDEV_ON_ERROR_ENOSPC
;
278 } else if (!strcmp(buf
, "stop")) {
279 return BLOCKDEV_ON_ERROR_STOP
;
280 } else if (!strcmp(buf
, "report")) {
281 return BLOCKDEV_ON_ERROR_REPORT
;
283 error_report("'%s' invalid %s error action",
284 buf
, is_read
? "read" : "write");
289 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
291 if (throttle_conflicting(cfg
)) {
292 error_setg(errp
, "bps/iops/max total values and read/write values"
293 " cannot be used at the same time");
297 if (!throttle_is_valid(cfg
)) {
298 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
305 static DriveInfo
*blockdev_init(QemuOpts
*all_opts
,
306 BlockInterfaceType block_default_type
)
309 const char *file
= NULL
;
311 const char *mediastr
= "";
312 BlockInterfaceType type
;
313 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
315 int cyls
, heads
, secs
, translation
;
320 int on_read_error
, on_write_error
;
331 bool has_driver_specific_opts
;
332 BlockDriver
*drv
= NULL
;
334 translation
= BIOS_ATA_TRANSLATION_AUTO
;
337 /* Check common options by copying from all_opts to opts, all other options
338 * are stored in bs_opts. */
339 id
= qemu_opts_id(all_opts
);
340 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
341 if (error_is_set(&error
)) {
342 qerror_report_err(error
);
347 bs_opts
= qdict_new();
348 qemu_opts_to_qdict(all_opts
, bs_opts
);
349 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
350 if (error_is_set(&error
)) {
351 qerror_report_err(error
);
357 qdict_del(bs_opts
, "id");
360 has_driver_specific_opts
= !!qdict_size(bs_opts
);
362 /* extract parameters */
363 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
364 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
365 index
= qemu_opt_get_number(opts
, "index", -1);
367 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
368 heads
= qemu_opt_get_number(opts
, "heads", 0);
369 secs
= qemu_opt_get_number(opts
, "secs", 0);
371 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
372 ro
= qemu_opt_get_bool(opts
, "read-only", 0);
373 copy_on_read
= qemu_opt_get_bool(opts
, "copy-on-read", false);
375 file
= qemu_opt_get(opts
, "file");
376 serial
= qemu_opt_get(opts
, "serial");
378 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
379 for (type
= 0; type
< IF_COUNT
&& strcmp(buf
, if_name
[type
]); type
++)
381 if (type
== IF_COUNT
) {
382 error_report("unsupported bus type '%s'", buf
);
386 type
= block_default_type
;
389 max_devs
= if_max_devs
[type
];
391 if (cyls
|| heads
|| secs
) {
393 error_report("invalid physical cyls number");
397 error_report("invalid physical heads number");
401 error_report("invalid physical secs number");
406 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
408 error_report("'%s' trans must be used with cyls, heads and secs",
412 if (!strcmp(buf
, "none"))
413 translation
= BIOS_ATA_TRANSLATION_NONE
;
414 else if (!strcmp(buf
, "lba"))
415 translation
= BIOS_ATA_TRANSLATION_LBA
;
416 else if (!strcmp(buf
, "auto"))
417 translation
= BIOS_ATA_TRANSLATION_AUTO
;
419 error_report("'%s' invalid translation type", buf
);
424 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
425 if (!strcmp(buf
, "disk")) {
427 } else if (!strcmp(buf
, "cdrom")) {
428 if (cyls
|| secs
|| heads
) {
429 error_report("CHS can't be set with media=%s", buf
);
434 error_report("'%s' invalid media", buf
);
439 if ((buf
= qemu_opt_get(opts
, "discard")) != NULL
) {
440 if (bdrv_parse_discard_flags(buf
, &bdrv_flags
) != 0) {
441 error_report("invalid discard option");
446 if (qemu_opt_get_bool(opts
, "cache.writeback", true)) {
447 bdrv_flags
|= BDRV_O_CACHE_WB
;
449 if (qemu_opt_get_bool(opts
, "cache.direct", false)) {
450 bdrv_flags
|= BDRV_O_NOCACHE
;
452 if (qemu_opt_get_bool(opts
, "cache.no-flush", false)) {
453 bdrv_flags
|= BDRV_O_NO_FLUSH
;
456 #ifdef CONFIG_LINUX_AIO
457 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
458 if (!strcmp(buf
, "native")) {
459 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
460 } else if (!strcmp(buf
, "threads")) {
461 /* this is the default */
463 error_report("invalid aio option");
469 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
470 if (is_help_option(buf
)) {
471 error_printf("Supported formats:");
472 bdrv_iterate_format(bdrv_format_print
, NULL
);
477 drv
= bdrv_find_format(buf
);
479 error_report("'%s' invalid format", buf
);
484 /* disk I/O throttling */
485 memset(&cfg
, 0, sizeof(cfg
));
486 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
=
487 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
488 cfg
.buckets
[THROTTLE_BPS_READ
].avg
=
489 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
490 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
=
491 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
492 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
=
493 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
494 cfg
.buckets
[THROTTLE_OPS_READ
].avg
=
495 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
496 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
=
497 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
499 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
=
500 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
501 cfg
.buckets
[THROTTLE_BPS_READ
].max
=
502 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
503 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
=
504 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
505 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
=
506 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
507 cfg
.buckets
[THROTTLE_OPS_READ
].max
=
508 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
509 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
=
510 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
512 cfg
.op_size
= qemu_opt_get_number(opts
, "throttling.iops-size", 0);
514 if (!check_throttle_config(&cfg
, &error
)) {
515 error_report("%s", error_get_pretty(error
));
520 if (qemu_opt_get(opts
, "boot") != NULL
) {
521 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
522 "ignored. Future versions will reject this parameter. Please "
523 "update your scripts.\n");
526 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
527 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
528 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
529 error_report("werror is not supported by this bus type");
533 on_write_error
= parse_block_error_action(buf
, 0);
534 if (on_write_error
< 0) {
539 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
540 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
541 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
542 error_report("rerror is not supported by this bus type");
546 on_read_error
= parse_block_error_action(buf
, 1);
547 if (on_read_error
< 0) {
552 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
553 if (type
!= IF_VIRTIO
) {
554 error_report("addr is not supported by this bus type");
559 /* compute bus and unit according index */
562 if (bus_id
!= 0 || unit_id
!= -1) {
563 error_report("index cannot be used with bus and unit");
566 bus_id
= drive_index_to_bus_id(type
, index
);
567 unit_id
= drive_index_to_unit_id(type
, index
);
570 /* if user doesn't specify a unit_id,
571 * try to find the first free
576 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
578 if (max_devs
&& unit_id
>= max_devs
) {
587 if (max_devs
&& unit_id
>= max_devs
) {
588 error_report("unit %d too big (max is %d)",
589 unit_id
, max_devs
- 1);
594 * catch multiple definitions
597 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
598 error_report("drive with bus=%d, unit=%d (index=%d) exists",
599 bus_id
, unit_id
, index
);
605 dinfo
= g_malloc0(sizeof(*dinfo
));
606 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
607 dinfo
->id
= g_strdup(buf
);
609 /* no id supplied -> create one */
610 dinfo
->id
= g_malloc0(32);
611 if (type
== IF_IDE
|| type
== IF_SCSI
)
612 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
614 snprintf(dinfo
->id
, 32, "%s%i%s%i",
615 if_name
[type
], bus_id
, mediastr
, unit_id
);
617 snprintf(dinfo
->id
, 32, "%s%s%i",
618 if_name
[type
], mediastr
, unit_id
);
620 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
621 dinfo
->bdrv
->open_flags
= snapshot
? BDRV_O_SNAPSHOT
: 0;
622 dinfo
->bdrv
->read_only
= ro
;
623 dinfo
->devaddr
= devaddr
;
626 dinfo
->unit
= unit_id
;
628 dinfo
->heads
= heads
;
630 dinfo
->trans
= translation
;
631 dinfo
->opts
= all_opts
;
633 if (serial
!= NULL
) {
634 dinfo
->serial
= g_strdup(serial
);
636 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
638 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
640 /* disk I/O throttling */
641 if (throttle_enabled(&cfg
)) {
642 bdrv_io_limits_enable(dinfo
->bdrv
);
643 bdrv_set_io_limits(dinfo
->bdrv
, &cfg
);
651 dinfo
->media_cd
= media
== MEDIA_CDROM
;
660 /* add virtio block device */
662 devopts
= qemu_opts_create_nofail(qemu_find_opts("device"));
663 if (arch_type
== QEMU_ARCH_S390X
) {
664 qemu_opt_set(devopts
, "driver", "virtio-blk-s390");
666 qemu_opt_set(devopts
, "driver", "virtio-blk-pci");
668 qemu_opt_set(devopts
, "drive", dinfo
->id
);
670 qemu_opt_set(devopts
, "addr", devaddr
);
676 if (!file
|| !*file
) {
677 if (has_driver_specific_opts
) {
684 /* always use cache=unsafe with snapshot */
685 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
686 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
690 bdrv_flags
|= BDRV_O_COPY_ON_READ
;
693 if (runstate_check(RUN_STATE_INMIGRATE
)) {
694 bdrv_flags
|= BDRV_O_INCOMING
;
697 if (media
== MEDIA_CDROM
) {
698 /* CDROM is fine for any interface, don't check. */
700 } else if (ro
== 1) {
701 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&&
702 type
!= IF_NONE
&& type
!= IF_PFLASH
) {
703 error_report("read-only not supported by this bus type");
708 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
710 if (ro
&& copy_on_read
) {
711 error_report("warning: disabling copy_on_read on read-only drive");
715 ret
= bdrv_open(dinfo
->bdrv
, file
, bs_opts
, bdrv_flags
, drv
, &error
);
718 error_report("could not open disk image %s: %s",
719 file
?: dinfo
->id
, error_get_pretty(error
));
723 if (bdrv_key_required(dinfo
->bdrv
))
734 bdrv_unref(dinfo
->bdrv
);
736 QTAILQ_REMOVE(&drives
, dinfo
, next
);
741 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
)
745 value
= qemu_opt_get(opts
, from
);
747 qemu_opt_set(opts
, to
, value
);
748 qemu_opt_unset(opts
, from
);
752 DriveInfo
*drive_init(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
757 /* Change legacy command line options into QMP ones */
758 qemu_opt_rename(all_opts
, "iops", "throttling.iops-total");
759 qemu_opt_rename(all_opts
, "iops_rd", "throttling.iops-read");
760 qemu_opt_rename(all_opts
, "iops_wr", "throttling.iops-write");
762 qemu_opt_rename(all_opts
, "bps", "throttling.bps-total");
763 qemu_opt_rename(all_opts
, "bps_rd", "throttling.bps-read");
764 qemu_opt_rename(all_opts
, "bps_wr", "throttling.bps-write");
766 qemu_opt_rename(all_opts
, "iops_max", "throttling.iops-total-max");
767 qemu_opt_rename(all_opts
, "iops_rd_max", "throttling.iops-read-max");
768 qemu_opt_rename(all_opts
, "iops_wr_max", "throttling.iops-write-max");
770 qemu_opt_rename(all_opts
, "bps_max", "throttling.bps-total-max");
771 qemu_opt_rename(all_opts
, "bps_rd_max", "throttling.bps-read-max");
772 qemu_opt_rename(all_opts
, "bps_wr_max", "throttling.bps-write-max");
774 qemu_opt_rename(all_opts
,
775 "iops_size", "throttling.iops-size");
777 qemu_opt_rename(all_opts
, "readonly", "read-only");
779 value
= qemu_opt_get(all_opts
, "cache");
783 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
784 error_report("invalid cache option");
788 /* Specific options take precedence */
789 if (!qemu_opt_get(all_opts
, "cache.writeback")) {
790 qemu_opt_set_bool(all_opts
, "cache.writeback",
791 !!(flags
& BDRV_O_CACHE_WB
));
793 if (!qemu_opt_get(all_opts
, "cache.direct")) {
794 qemu_opt_set_bool(all_opts
, "cache.direct",
795 !!(flags
& BDRV_O_NOCACHE
));
797 if (!qemu_opt_get(all_opts
, "cache.no-flush")) {
798 qemu_opt_set_bool(all_opts
, "cache.no-flush",
799 !!(flags
& BDRV_O_NO_FLUSH
));
801 qemu_opt_unset(all_opts
, "cache");
804 /* Actual block device init: Functionality shared with blockdev-add */
805 dinfo
= blockdev_init(all_opts
, block_default_type
);
810 /* Set legacy DriveInfo fields */
811 dinfo
->enable_auto_del
= true;
817 void do_commit(Monitor
*mon
, const QDict
*qdict
)
819 const char *device
= qdict_get_str(qdict
, "device");
820 BlockDriverState
*bs
;
823 if (!strcmp(device
, "all")) {
824 ret
= bdrv_commit_all();
826 bs
= bdrv_find(device
);
828 monitor_printf(mon
, "Device '%s' not found\n", device
);
831 ret
= bdrv_commit(bs
);
834 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
839 static void blockdev_do_action(int kind
, void *data
, Error
**errp
)
841 TransactionAction action
;
842 TransactionActionList list
;
846 list
.value
= &action
;
848 qmp_transaction(&list
, errp
);
851 void qmp_blockdev_snapshot_sync(const char *device
, const char *snapshot_file
,
852 bool has_format
, const char *format
,
853 bool has_mode
, enum NewImageMode mode
,
856 BlockdevSnapshot snapshot
= {
857 .device
= (char *) device
,
858 .snapshot_file
= (char *) snapshot_file
,
859 .has_format
= has_format
,
860 .format
= (char *) format
,
861 .has_mode
= has_mode
,
864 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
868 void qmp_blockdev_snapshot_internal_sync(const char *device
,
872 BlockdevSnapshotInternal snapshot
= {
873 .device
= (char *) device
,
874 .name
= (char *) name
877 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
881 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
888 BlockDriverState
*bs
= bdrv_find(device
);
890 Error
*local_err
= NULL
;
891 SnapshotInfo
*info
= NULL
;
895 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
908 error_setg(errp
, "Name or id must be provided");
912 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
913 if (error_is_set(&local_err
)) {
914 error_propagate(errp
, local_err
);
919 "Snapshot with id '%s' and name '%s' does not exist on "
921 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
925 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
926 if (error_is_set(&local_err
)) {
927 error_propagate(errp
, local_err
);
931 info
= g_malloc0(sizeof(SnapshotInfo
));
932 info
->id
= g_strdup(sn
.id_str
);
933 info
->name
= g_strdup(sn
.name
);
934 info
->date_nsec
= sn
.date_nsec
;
935 info
->date_sec
= sn
.date_sec
;
936 info
->vm_state_size
= sn
.vm_state_size
;
937 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
938 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
943 /* New and old BlockDriverState structs for group snapshots */
945 typedef struct BlkTransactionState BlkTransactionState
;
947 /* Only prepare() may fail. In a single transaction, only one of commit() or
948 abort() will be called, clean() will always be called if it present. */
949 typedef struct BdrvActionOps
{
950 /* Size of state struct, in bytes. */
951 size_t instance_size
;
952 /* Prepare the work, must NOT be NULL. */
953 void (*prepare
)(BlkTransactionState
*common
, Error
**errp
);
954 /* Commit the changes, can be NULL. */
955 void (*commit
)(BlkTransactionState
*common
);
956 /* Abort the changes on fail, can be NULL. */
957 void (*abort
)(BlkTransactionState
*common
);
958 /* Clean up resource in the end, can be NULL. */
959 void (*clean
)(BlkTransactionState
*common
);
963 * This structure must be arranged as first member in child type, assuming
964 * that compiler will also arrange it to the same address with parent instance.
965 * Later it will be used in free().
967 struct BlkTransactionState
{
968 TransactionAction
*action
;
969 const BdrvActionOps
*ops
;
970 QSIMPLEQ_ENTRY(BlkTransactionState
) entry
;
973 /* internal snapshot private data */
974 typedef struct InternalSnapshotState
{
975 BlkTransactionState common
;
976 BlockDriverState
*bs
;
978 } InternalSnapshotState
;
980 static void internal_snapshot_prepare(BlkTransactionState
*common
,
985 BlockDriverState
*bs
;
986 QEMUSnapshotInfo old_sn
, *sn
;
989 BlockdevSnapshotInternal
*internal
;
990 InternalSnapshotState
*state
;
993 g_assert(common
->action
->kind
==
994 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
995 internal
= common
->action
->blockdev_snapshot_internal_sync
;
996 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
999 device
= internal
->device
;
1000 name
= internal
->name
;
1002 /* 2. check for validation */
1003 bs
= bdrv_find(device
);
1005 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1009 if (!bdrv_is_inserted(bs
)) {
1010 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1014 if (bdrv_is_read_only(bs
)) {
1015 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1019 if (!bdrv_can_snapshot(bs
)) {
1020 error_set(errp
, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
1021 bs
->drv
->format_name
, device
, "internal snapshot");
1025 if (!strlen(name
)) {
1026 error_setg(errp
, "Name is empty");
1030 /* check whether a snapshot with name exist */
1031 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
, errp
);
1032 if (error_is_set(errp
)) {
1036 "Snapshot with name '%s' already exists on device '%s'",
1041 /* 3. take the snapshot */
1043 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1044 qemu_gettimeofday(&tv
);
1045 sn
->date_sec
= tv
.tv_sec
;
1046 sn
->date_nsec
= tv
.tv_usec
* 1000;
1047 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1049 ret1
= bdrv_snapshot_create(bs
, sn
);
1051 error_setg_errno(errp
, -ret1
,
1052 "Failed to create snapshot '%s' on device '%s'",
1057 /* 4. succeed, mark a snapshot is created */
1061 static void internal_snapshot_abort(BlkTransactionState
*common
)
1063 InternalSnapshotState
*state
=
1064 DO_UPCAST(InternalSnapshotState
, common
, common
);
1065 BlockDriverState
*bs
= state
->bs
;
1066 QEMUSnapshotInfo
*sn
= &state
->sn
;
1067 Error
*local_error
= NULL
;
1073 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1074 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1075 "device '%s' in abort: %s",
1078 bdrv_get_device_name(bs
),
1079 error_get_pretty(local_error
));
1080 error_free(local_error
);
1084 /* external snapshot private data */
1085 typedef struct ExternalSnapshotState
{
1086 BlkTransactionState common
;
1087 BlockDriverState
*old_bs
;
1088 BlockDriverState
*new_bs
;
1089 } ExternalSnapshotState
;
1091 static void external_snapshot_prepare(BlkTransactionState
*common
,
1096 Error
*local_err
= NULL
;
1098 const char *new_image_file
;
1099 const char *format
= "qcow2";
1100 enum NewImageMode mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1101 ExternalSnapshotState
*state
=
1102 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1103 TransactionAction
*action
= common
->action
;
1105 /* get parameters */
1106 g_assert(action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
);
1108 device
= action
->blockdev_snapshot_sync
->device
;
1109 new_image_file
= action
->blockdev_snapshot_sync
->snapshot_file
;
1110 if (action
->blockdev_snapshot_sync
->has_format
) {
1111 format
= action
->blockdev_snapshot_sync
->format
;
1113 if (action
->blockdev_snapshot_sync
->has_mode
) {
1114 mode
= action
->blockdev_snapshot_sync
->mode
;
1117 /* start processing */
1118 drv
= bdrv_find_format(format
);
1120 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1124 state
->old_bs
= bdrv_find(device
);
1125 if (!state
->old_bs
) {
1126 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1130 if (!bdrv_is_inserted(state
->old_bs
)) {
1131 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1135 if (bdrv_in_use(state
->old_bs
)) {
1136 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1140 if (!bdrv_is_read_only(state
->old_bs
)) {
1141 if (bdrv_flush(state
->old_bs
)) {
1142 error_set(errp
, QERR_IO_ERROR
);
1147 if (bdrv_check_ext_snapshot(state
->old_bs
) != EXT_SNAPSHOT_ALLOWED
) {
1148 error_set(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1152 flags
= state
->old_bs
->open_flags
;
1154 /* create new image w/backing file */
1155 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1156 bdrv_img_create(new_image_file
, format
,
1157 state
->old_bs
->filename
,
1158 state
->old_bs
->drv
->format_name
,
1159 NULL
, -1, flags
, &local_err
, false);
1160 if (error_is_set(&local_err
)) {
1161 error_propagate(errp
, local_err
);
1166 /* We will manually add the backing_hd field to the bs later */
1167 state
->new_bs
= bdrv_new("");
1168 /* TODO Inherit bs->options or only take explicit options with an
1169 * extended QMP command? */
1170 ret
= bdrv_open(state
->new_bs
, new_image_file
, NULL
,
1171 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
1173 error_propagate(errp
, local_err
);
1177 static void external_snapshot_commit(BlkTransactionState
*common
)
1179 ExternalSnapshotState
*state
=
1180 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1182 /* This removes our old bs and adds the new bs */
1183 bdrv_append(state
->new_bs
, state
->old_bs
);
1184 /* We don't need (or want) to use the transactional
1185 * bdrv_reopen_multiple() across all the entries at once, because we
1186 * don't want to abort all of them if one of them fails the reopen */
1187 bdrv_reopen(state
->new_bs
, state
->new_bs
->open_flags
& ~BDRV_O_RDWR
,
1191 static void external_snapshot_abort(BlkTransactionState
*common
)
1193 ExternalSnapshotState
*state
=
1194 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1195 if (state
->new_bs
) {
1196 bdrv_unref(state
->new_bs
);
1200 typedef struct DriveBackupState
{
1201 BlkTransactionState common
;
1202 BlockDriverState
*bs
;
1206 static void drive_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1208 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1209 DriveBackup
*backup
;
1210 Error
*local_err
= NULL
;
1212 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1213 backup
= common
->action
->drive_backup
;
1215 qmp_drive_backup(backup
->device
, backup
->target
,
1216 backup
->has_format
, backup
->format
,
1218 backup
->has_mode
, backup
->mode
,
1219 backup
->has_speed
, backup
->speed
,
1220 backup
->has_on_source_error
, backup
->on_source_error
,
1221 backup
->has_on_target_error
, backup
->on_target_error
,
1223 if (error_is_set(&local_err
)) {
1224 error_propagate(errp
, local_err
);
1230 state
->bs
= bdrv_find(backup
->device
);
1231 state
->job
= state
->bs
->job
;
1234 static void drive_backup_abort(BlkTransactionState
*common
)
1236 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1237 BlockDriverState
*bs
= state
->bs
;
1239 /* Only cancel if it's the job we started */
1240 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1241 block_job_cancel_sync(bs
->job
);
1245 static void abort_prepare(BlkTransactionState
*common
, Error
**errp
)
1247 error_setg(errp
, "Transaction aborted using Abort action");
1250 static void abort_commit(BlkTransactionState
*common
)
1252 g_assert_not_reached(); /* this action never succeeds */
1255 static const BdrvActionOps actions
[] = {
1256 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
1257 .instance_size
= sizeof(ExternalSnapshotState
),
1258 .prepare
= external_snapshot_prepare
,
1259 .commit
= external_snapshot_commit
,
1260 .abort
= external_snapshot_abort
,
1262 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
1263 .instance_size
= sizeof(DriveBackupState
),
1264 .prepare
= drive_backup_prepare
,
1265 .abort
= drive_backup_abort
,
1267 [TRANSACTION_ACTION_KIND_ABORT
] = {
1268 .instance_size
= sizeof(BlkTransactionState
),
1269 .prepare
= abort_prepare
,
1270 .commit
= abort_commit
,
1272 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
1273 .instance_size
= sizeof(InternalSnapshotState
),
1274 .prepare
= internal_snapshot_prepare
,
1275 .abort
= internal_snapshot_abort
,
1280 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1281 * then we do not pivot any of the devices in the group, and abandon the
1284 void qmp_transaction(TransactionActionList
*dev_list
, Error
**errp
)
1286 TransactionActionList
*dev_entry
= dev_list
;
1287 BlkTransactionState
*state
, *next
;
1288 Error
*local_err
= NULL
;
1290 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkTransactionState
) snap_bdrv_states
;
1291 QSIMPLEQ_INIT(&snap_bdrv_states
);
1293 /* drain all i/o before any snapshots */
1296 /* We don't do anything in this loop that commits us to the snapshot */
1297 while (NULL
!= dev_entry
) {
1298 TransactionAction
*dev_info
= NULL
;
1299 const BdrvActionOps
*ops
;
1301 dev_info
= dev_entry
->value
;
1302 dev_entry
= dev_entry
->next
;
1304 assert(dev_info
->kind
< ARRAY_SIZE(actions
));
1306 ops
= &actions
[dev_info
->kind
];
1307 assert(ops
->instance_size
> 0);
1309 state
= g_malloc0(ops
->instance_size
);
1311 state
->action
= dev_info
;
1312 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
1314 state
->ops
->prepare(state
, &local_err
);
1315 if (error_is_set(&local_err
)) {
1316 error_propagate(errp
, local_err
);
1317 goto delete_and_fail
;
1321 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1322 if (state
->ops
->commit
) {
1323 state
->ops
->commit(state
);
1332 * failure, and it is all-or-none; abandon each new bs, and keep using
1333 * the original bs for all images
1335 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1336 if (state
->ops
->abort
) {
1337 state
->ops
->abort(state
);
1341 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
1342 if (state
->ops
->clean
) {
1343 state
->ops
->clean(state
);
1350 static void eject_device(BlockDriverState
*bs
, int force
, Error
**errp
)
1352 if (bdrv_in_use(bs
)) {
1353 error_set(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
1356 if (!bdrv_dev_has_removable_media(bs
)) {
1357 error_set(errp
, QERR_DEVICE_NOT_REMOVABLE
, bdrv_get_device_name(bs
));
1361 if (bdrv_dev_is_medium_locked(bs
) && !bdrv_dev_is_tray_open(bs
)) {
1362 bdrv_dev_eject_request(bs
, force
);
1364 error_set(errp
, QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
1372 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
1374 BlockDriverState
*bs
;
1376 bs
= bdrv_find(device
);
1378 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1382 eject_device(bs
, force
, errp
);
1385 void qmp_block_passwd(const char *device
, const char *password
, Error
**errp
)
1387 BlockDriverState
*bs
;
1390 bs
= bdrv_find(device
);
1392 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1396 err
= bdrv_set_key(bs
, password
);
1397 if (err
== -EINVAL
) {
1398 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1400 } else if (err
< 0) {
1401 error_set(errp
, QERR_INVALID_PASSWORD
);
1406 static void qmp_bdrv_open_encrypted(BlockDriverState
*bs
, const char *filename
,
1407 int bdrv_flags
, BlockDriver
*drv
,
1408 const char *password
, Error
**errp
)
1410 Error
*local_err
= NULL
;
1413 ret
= bdrv_open(bs
, filename
, NULL
, bdrv_flags
, drv
, &local_err
);
1415 error_propagate(errp
, local_err
);
1419 if (bdrv_key_required(bs
)) {
1421 if (bdrv_set_key(bs
, password
) < 0) {
1422 error_set(errp
, QERR_INVALID_PASSWORD
);
1425 error_set(errp
, QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
),
1426 bdrv_get_encrypted_filename(bs
));
1428 } else if (password
) {
1429 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1433 void qmp_change_blockdev(const char *device
, const char *filename
,
1434 bool has_format
, const char *format
, Error
**errp
)
1436 BlockDriverState
*bs
;
1437 BlockDriver
*drv
= NULL
;
1441 bs
= bdrv_find(device
);
1443 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1448 drv
= bdrv_find_whitelisted_format(format
, bs
->read_only
);
1450 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1455 eject_device(bs
, 0, &err
);
1456 if (error_is_set(&err
)) {
1457 error_propagate(errp
, err
);
1461 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
1462 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
1464 qmp_bdrv_open_encrypted(bs
, filename
, bdrv_flags
, drv
, NULL
, errp
);
1467 /* throttling disk I/O limits */
1468 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
1475 bool has_bps_rd_max
,
1477 bool has_bps_wr_max
,
1481 bool has_iops_rd_max
,
1482 int64_t iops_rd_max
,
1483 bool has_iops_wr_max
,
1484 int64_t iops_wr_max
,
1486 int64_t iops_size
, Error
**errp
)
1489 BlockDriverState
*bs
;
1491 bs
= bdrv_find(device
);
1493 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1497 memset(&cfg
, 0, sizeof(cfg
));
1498 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
1499 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
1500 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
1502 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
1503 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
1504 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
1507 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
1509 if (has_bps_rd_max
) {
1510 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
1512 if (has_bps_wr_max
) {
1513 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
1516 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
1518 if (has_iops_rd_max
) {
1519 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
1521 if (has_iops_wr_max
) {
1522 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
1525 if (has_iops_size
) {
1526 cfg
.op_size
= iops_size
;
1529 if (!check_throttle_config(&cfg
, errp
)) {
1533 if (!bs
->io_limits_enabled
&& throttle_enabled(&cfg
)) {
1534 bdrv_io_limits_enable(bs
);
1535 } else if (bs
->io_limits_enabled
&& !throttle_enabled(&cfg
)) {
1536 bdrv_io_limits_disable(bs
);
1539 if (bs
->io_limits_enabled
) {
1540 bdrv_set_io_limits(bs
, &cfg
);
1544 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1546 const char *id
= qdict_get_str(qdict
, "id");
1547 BlockDriverState
*bs
;
1551 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1554 if (bdrv_in_use(bs
)) {
1555 qerror_report(QERR_DEVICE_IN_USE
, id
);
1559 /* quiesce block driver; prevent further io */
1564 /* if we have a device attached to this BlockDriverState
1565 * then we need to make the drive anonymous until the device
1566 * can be removed. If this is a drive with no device backing
1567 * then we can just get rid of the block driver state right here.
1569 if (bdrv_get_attached_dev(bs
)) {
1572 /* Further I/O must not pause the guest */
1573 bdrv_set_on_error(bs
, BLOCKDEV_ON_ERROR_REPORT
,
1574 BLOCKDEV_ON_ERROR_REPORT
);
1576 drive_uninit(drive_get_by_blockdev(bs
));
1582 void qmp_block_resize(const char *device
, int64_t size
, Error
**errp
)
1584 BlockDriverState
*bs
;
1587 bs
= bdrv_find(device
);
1589 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1594 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
1598 /* complete all in-flight operations before resizing the device */
1601 ret
= bdrv_truncate(bs
, size
);
1606 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1609 error_set(errp
, QERR_UNSUPPORTED
);
1612 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1615 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1618 error_setg_errno(errp
, -ret
, "Could not resize");
1623 static void block_job_cb(void *opaque
, int ret
)
1625 BlockDriverState
*bs
= opaque
;
1628 trace_block_job_cb(bs
, bs
->job
, ret
);
1631 obj
= qobject_from_block_job(bs
->job
);
1633 QDict
*dict
= qobject_to_qdict(obj
);
1634 qdict_put(dict
, "error", qstring_from_str(strerror(-ret
)));
1637 if (block_job_is_cancelled(bs
->job
)) {
1638 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED
, obj
);
1640 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED
, obj
);
1642 qobject_decref(obj
);
1644 bdrv_put_ref_bh_schedule(bs
);
1647 void qmp_block_stream(const char *device
, bool has_base
,
1648 const char *base
, bool has_speed
, int64_t speed
,
1649 bool has_on_error
, BlockdevOnError on_error
,
1652 BlockDriverState
*bs
;
1653 BlockDriverState
*base_bs
= NULL
;
1654 Error
*local_err
= NULL
;
1656 if (!has_on_error
) {
1657 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1660 bs
= bdrv_find(device
);
1662 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1667 base_bs
= bdrv_find_backing_image(bs
, base
);
1668 if (base_bs
== NULL
) {
1669 error_set(errp
, QERR_BASE_NOT_FOUND
, base
);
1674 stream_start(bs
, base_bs
, base
, has_speed
? speed
: 0,
1675 on_error
, block_job_cb
, bs
, &local_err
);
1676 if (error_is_set(&local_err
)) {
1677 error_propagate(errp
, local_err
);
1681 trace_qmp_block_stream(bs
, bs
->job
);
1684 void qmp_block_commit(const char *device
,
1685 bool has_base
, const char *base
, const char *top
,
1686 bool has_speed
, int64_t speed
,
1689 BlockDriverState
*bs
;
1690 BlockDriverState
*base_bs
, *top_bs
;
1691 Error
*local_err
= NULL
;
1692 /* This will be part of the QMP command, if/when the
1693 * BlockdevOnError change for blkmirror makes it in
1695 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1697 /* drain all i/o before commits */
1700 bs
= bdrv_find(device
);
1702 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1706 /* default top_bs is the active layer */
1710 if (strcmp(bs
->filename
, top
) != 0) {
1711 top_bs
= bdrv_find_backing_image(bs
, top
);
1715 if (top_bs
== NULL
) {
1716 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
1720 if (has_base
&& base
) {
1721 base_bs
= bdrv_find_backing_image(top_bs
, base
);
1723 base_bs
= bdrv_find_base(top_bs
);
1726 if (base_bs
== NULL
) {
1727 error_set(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
1731 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
1733 if (local_err
!= NULL
) {
1734 error_propagate(errp
, local_err
);
1739 void qmp_drive_backup(const char *device
, const char *target
,
1740 bool has_format
, const char *format
,
1741 enum MirrorSyncMode sync
,
1742 bool has_mode
, enum NewImageMode mode
,
1743 bool has_speed
, int64_t speed
,
1744 bool has_on_source_error
, BlockdevOnError on_source_error
,
1745 bool has_on_target_error
, BlockdevOnError on_target_error
,
1748 BlockDriverState
*bs
;
1749 BlockDriverState
*target_bs
;
1750 BlockDriverState
*source
= NULL
;
1751 BlockDriver
*drv
= NULL
;
1752 Error
*local_err
= NULL
;
1760 if (!has_on_source_error
) {
1761 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
1763 if (!has_on_target_error
) {
1764 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
1767 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1770 bs
= bdrv_find(device
);
1772 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1776 if (!bdrv_is_inserted(bs
)) {
1777 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1782 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
1785 drv
= bdrv_find_format(format
);
1787 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1792 if (bdrv_in_use(bs
)) {
1793 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1797 flags
= bs
->open_flags
| BDRV_O_RDWR
;
1799 /* See if we have a backing HD we can use to create our new image
1801 if (sync
== MIRROR_SYNC_MODE_TOP
) {
1802 source
= bs
->backing_hd
;
1804 sync
= MIRROR_SYNC_MODE_FULL
;
1807 if (sync
== MIRROR_SYNC_MODE_NONE
) {
1811 size
= bdrv_getlength(bs
);
1813 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1817 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1818 assert(format
&& drv
);
1820 bdrv_img_create(target
, format
, source
->filename
,
1821 source
->drv
->format_name
, NULL
,
1822 size
, flags
, &local_err
, false);
1824 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
1825 size
, flags
, &local_err
, false);
1829 if (error_is_set(&local_err
)) {
1830 error_propagate(errp
, local_err
);
1834 target_bs
= bdrv_new("");
1835 ret
= bdrv_open(target_bs
, target
, NULL
, flags
, drv
, &local_err
);
1837 bdrv_unref(target_bs
);
1838 error_propagate(errp
, local_err
);
1842 backup_start(bs
, target_bs
, speed
, sync
, on_source_error
, on_target_error
,
1843 block_job_cb
, bs
, &local_err
);
1844 if (local_err
!= NULL
) {
1845 bdrv_unref(target_bs
);
1846 error_propagate(errp
, local_err
);
1851 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1853 void qmp_drive_mirror(const char *device
, const char *target
,
1854 bool has_format
, const char *format
,
1855 enum MirrorSyncMode sync
,
1856 bool has_mode
, enum NewImageMode mode
,
1857 bool has_speed
, int64_t speed
,
1858 bool has_granularity
, uint32_t granularity
,
1859 bool has_buf_size
, int64_t buf_size
,
1860 bool has_on_source_error
, BlockdevOnError on_source_error
,
1861 bool has_on_target_error
, BlockdevOnError on_target_error
,
1864 BlockDriverState
*bs
;
1865 BlockDriverState
*source
, *target_bs
;
1866 BlockDriver
*drv
= NULL
;
1867 Error
*local_err
= NULL
;
1875 if (!has_on_source_error
) {
1876 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
1878 if (!has_on_target_error
) {
1879 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
1882 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1884 if (!has_granularity
) {
1887 if (!has_buf_size
) {
1888 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
1891 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
1892 error_set(errp
, QERR_INVALID_PARAMETER
, device
);
1895 if (granularity
& (granularity
- 1)) {
1896 error_set(errp
, QERR_INVALID_PARAMETER
, device
);
1900 bs
= bdrv_find(device
);
1902 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1906 if (!bdrv_is_inserted(bs
)) {
1907 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1912 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
1915 drv
= bdrv_find_format(format
);
1917 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1922 if (bdrv_in_use(bs
)) {
1923 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1927 flags
= bs
->open_flags
| BDRV_O_RDWR
;
1928 source
= bs
->backing_hd
;
1929 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
1930 sync
= MIRROR_SYNC_MODE_FULL
;
1933 size
= bdrv_getlength(bs
);
1935 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1939 if (sync
== MIRROR_SYNC_MODE_FULL
&& mode
!= NEW_IMAGE_MODE_EXISTING
) {
1940 /* create new image w/o backing file */
1941 assert(format
&& drv
);
1942 bdrv_img_create(target
, format
,
1943 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
1946 case NEW_IMAGE_MODE_EXISTING
:
1948 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
1949 /* create new image with backing file */
1950 bdrv_img_create(target
, format
,
1952 source
->drv
->format_name
,
1953 NULL
, size
, flags
, &local_err
, false);
1960 if (error_is_set(&local_err
)) {
1961 error_propagate(errp
, local_err
);
1965 /* Mirroring takes care of copy-on-write using the source's backing
1968 target_bs
= bdrv_new("");
1969 ret
= bdrv_open(target_bs
, target
, NULL
, flags
| BDRV_O_NO_BACKING
, drv
,
1972 bdrv_unref(target_bs
);
1973 error_propagate(errp
, local_err
);
1977 mirror_start(bs
, target_bs
, speed
, granularity
, buf_size
, sync
,
1978 on_source_error
, on_target_error
,
1979 block_job_cb
, bs
, &local_err
);
1980 if (local_err
!= NULL
) {
1981 bdrv_unref(target_bs
);
1982 error_propagate(errp
, local_err
);
1987 static BlockJob
*find_block_job(const char *device
)
1989 BlockDriverState
*bs
;
1991 bs
= bdrv_find(device
);
1992 if (!bs
|| !bs
->job
) {
1998 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
2000 BlockJob
*job
= find_block_job(device
);
2003 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2007 block_job_set_speed(job
, speed
, errp
);
2010 void qmp_block_job_cancel(const char *device
,
2011 bool has_force
, bool force
, Error
**errp
)
2013 BlockJob
*job
= find_block_job(device
);
2020 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2023 if (job
->paused
&& !force
) {
2024 error_set(errp
, QERR_BLOCK_JOB_PAUSED
, device
);
2028 trace_qmp_block_job_cancel(job
);
2029 block_job_cancel(job
);
2032 void qmp_block_job_pause(const char *device
, Error
**errp
)
2034 BlockJob
*job
= find_block_job(device
);
2037 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2041 trace_qmp_block_job_pause(job
);
2042 block_job_pause(job
);
2045 void qmp_block_job_resume(const char *device
, Error
**errp
)
2047 BlockJob
*job
= find_block_job(device
);
2050 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2054 trace_qmp_block_job_resume(job
);
2055 block_job_resume(job
);
2058 void qmp_block_job_complete(const char *device
, Error
**errp
)
2060 BlockJob
*job
= find_block_job(device
);
2063 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2067 trace_qmp_block_job_complete(job
);
2068 block_job_complete(job
, errp
);
2071 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
2073 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
2077 Error
*local_err
= NULL
;
2079 /* Require an ID in the top level */
2080 if (!options
->has_id
) {
2081 error_setg(errp
, "Block device needs an ID");
2085 /* TODO Sort it out in raw-posix and drive_init: Reject aio=native with
2086 * cache.direct=false instead of silently switching to aio=threads, except
2087 * if called from drive_init.
2089 * For now, simply forbidding the combination for all drivers will do. */
2090 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
2091 bool direct
= options
->cache
->has_direct
&& options
->cache
->direct
;
2092 if (!options
->has_cache
&& !direct
) {
2093 error_setg(errp
, "aio=native requires cache.direct=true");
2098 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
2099 &options
, NULL
, &local_err
);
2100 if (error_is_set(&local_err
)) {
2101 error_propagate(errp
, local_err
);
2105 obj
= qmp_output_get_qobject(ov
);
2106 qdict
= qobject_to_qdict(obj
);
2108 qdict_flatten(qdict
);
2110 QemuOpts
*opts
= qemu_opts_from_qdict(&qemu_drive_opts
, qdict
, &local_err
);
2111 if (error_is_set(&local_err
)) {
2112 error_propagate(errp
, local_err
);
2116 dinfo
= blockdev_init(opts
, IF_NONE
);
2118 error_setg(errp
, "Could not open image");
2123 qmp_output_visitor_cleanup(ov
);
2126 static void do_qmp_query_block_jobs_one(void *opaque
, BlockDriverState
*bs
)
2128 BlockJobInfoList
**prev
= opaque
;
2129 BlockJob
*job
= bs
->job
;
2132 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
2133 elem
->value
= block_job_query(bs
->job
);
2134 (*prev
)->next
= elem
;
2139 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
2141 /* Dummy is a fake list element for holding the head pointer */
2142 BlockJobInfoList dummy
= {};
2143 BlockJobInfoList
*prev
= &dummy
;
2144 bdrv_iterate(do_qmp_query_block_jobs_one
, &prev
);
2148 QemuOptsList qemu_common_drive_opts
= {
2150 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
2154 .type
= QEMU_OPT_NUMBER
,
2155 .help
= "bus number",
2158 .type
= QEMU_OPT_NUMBER
,
2159 .help
= "unit number (i.e. lun for scsi)",
2162 .type
= QEMU_OPT_STRING
,
2163 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
2166 .type
= QEMU_OPT_NUMBER
,
2167 .help
= "index number",
2170 .type
= QEMU_OPT_NUMBER
,
2171 .help
= "number of cylinders (ide disk geometry)",
2174 .type
= QEMU_OPT_NUMBER
,
2175 .help
= "number of heads (ide disk geometry)",
2178 .type
= QEMU_OPT_NUMBER
,
2179 .help
= "number of sectors (ide disk geometry)",
2182 .type
= QEMU_OPT_STRING
,
2183 .help
= "chs translation (auto, lba. none)",
2186 .type
= QEMU_OPT_STRING
,
2187 .help
= "media type (disk, cdrom)",
2190 .type
= QEMU_OPT_BOOL
,
2191 .help
= "enable/disable snapshot mode",
2194 .type
= QEMU_OPT_STRING
,
2195 .help
= "disk image",
2198 .type
= QEMU_OPT_STRING
,
2199 .help
= "discard operation (ignore/off, unmap/on)",
2201 .name
= "cache.writeback",
2202 .type
= QEMU_OPT_BOOL
,
2203 .help
= "enables writeback mode for any caches",
2205 .name
= "cache.direct",
2206 .type
= QEMU_OPT_BOOL
,
2207 .help
= "enables use of O_DIRECT (bypass the host page cache)",
2209 .name
= "cache.no-flush",
2210 .type
= QEMU_OPT_BOOL
,
2211 .help
= "ignore any flush requests for the device",
2214 .type
= QEMU_OPT_STRING
,
2215 .help
= "host AIO implementation (threads, native)",
2218 .type
= QEMU_OPT_STRING
,
2219 .help
= "disk format (raw, qcow2, ...)",
2222 .type
= QEMU_OPT_STRING
,
2223 .help
= "disk serial number",
2226 .type
= QEMU_OPT_STRING
,
2227 .help
= "read error action",
2230 .type
= QEMU_OPT_STRING
,
2231 .help
= "write error action",
2234 .type
= QEMU_OPT_STRING
,
2235 .help
= "pci address (virtio only)",
2237 .name
= "read-only",
2238 .type
= QEMU_OPT_BOOL
,
2239 .help
= "open drive file as read-only",
2241 .name
= "throttling.iops-total",
2242 .type
= QEMU_OPT_NUMBER
,
2243 .help
= "limit total I/O operations per second",
2245 .name
= "throttling.iops-read",
2246 .type
= QEMU_OPT_NUMBER
,
2247 .help
= "limit read operations per second",
2249 .name
= "throttling.iops-write",
2250 .type
= QEMU_OPT_NUMBER
,
2251 .help
= "limit write operations per second",
2253 .name
= "throttling.bps-total",
2254 .type
= QEMU_OPT_NUMBER
,
2255 .help
= "limit total bytes per second",
2257 .name
= "throttling.bps-read",
2258 .type
= QEMU_OPT_NUMBER
,
2259 .help
= "limit read bytes per second",
2261 .name
= "throttling.bps-write",
2262 .type
= QEMU_OPT_NUMBER
,
2263 .help
= "limit write bytes per second",
2265 .name
= "throttling.iops-total-max",
2266 .type
= QEMU_OPT_NUMBER
,
2267 .help
= "I/O operations burst",
2269 .name
= "throttling.iops-read-max",
2270 .type
= QEMU_OPT_NUMBER
,
2271 .help
= "I/O operations read burst",
2273 .name
= "throttling.iops-write-max",
2274 .type
= QEMU_OPT_NUMBER
,
2275 .help
= "I/O operations write burst",
2277 .name
= "throttling.bps-total-max",
2278 .type
= QEMU_OPT_NUMBER
,
2279 .help
= "total bytes burst",
2281 .name
= "throttling.bps-read-max",
2282 .type
= QEMU_OPT_NUMBER
,
2283 .help
= "total bytes read burst",
2285 .name
= "throttling.bps-write-max",
2286 .type
= QEMU_OPT_NUMBER
,
2287 .help
= "total bytes write burst",
2289 .name
= "throttling.iops-size",
2290 .type
= QEMU_OPT_NUMBER
,
2291 .help
= "when limiting by iops max size of an I/O in bytes",
2293 .name
= "copy-on-read",
2294 .type
= QEMU_OPT_BOOL
,
2295 .help
= "copy read data from backing file into image file",
2298 .type
= QEMU_OPT_BOOL
,
2299 .help
= "(deprecated, ignored)",
2301 { /* end of list */ }
2305 QemuOptsList qemu_drive_opts
= {
2307 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
2310 * no elements => accept any params
2311 * validation will happen later
2313 { /* end of list */ }