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
)
221 qemu_opts_del(dinfo
->opts
);
224 bdrv_unref(dinfo
->bdrv
);
226 QTAILQ_REMOVE(&drives
, dinfo
, next
);
227 g_free(dinfo
->serial
);
231 void drive_put_ref(DriveInfo
*dinfo
)
233 assert(dinfo
->refcount
);
234 if (--dinfo
->refcount
== 0) {
239 void drive_get_ref(DriveInfo
*dinfo
)
246 BlockDriverState
*bs
;
249 static void bdrv_put_ref_bh(void *opaque
)
251 BDRVPutRefBH
*s
= opaque
;
254 qemu_bh_delete(s
->bh
);
259 * Release a BDS reference in a BH
261 * It is not safe to use bdrv_unref() from a callback function when the callers
262 * still need the BlockDriverState. In such cases we schedule a BH to release
265 static void bdrv_put_ref_bh_schedule(BlockDriverState
*bs
)
269 s
= g_new(BDRVPutRefBH
, 1);
270 s
->bh
= qemu_bh_new(bdrv_put_ref_bh
, s
);
272 qemu_bh_schedule(s
->bh
);
275 static int parse_block_error_action(const char *buf
, bool is_read
)
277 if (!strcmp(buf
, "ignore")) {
278 return BLOCKDEV_ON_ERROR_IGNORE
;
279 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
280 return BLOCKDEV_ON_ERROR_ENOSPC
;
281 } else if (!strcmp(buf
, "stop")) {
282 return BLOCKDEV_ON_ERROR_STOP
;
283 } else if (!strcmp(buf
, "report")) {
284 return BLOCKDEV_ON_ERROR_REPORT
;
286 error_report("'%s' invalid %s error action",
287 buf
, is_read
? "read" : "write");
292 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
294 if (throttle_conflicting(cfg
)) {
295 error_setg(errp
, "bps/iops/max total values and read/write values"
296 " cannot be used at the same time");
300 if (!throttle_is_valid(cfg
)) {
301 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
308 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
310 /* Takes the ownership of bs_opts */
311 static DriveInfo
*blockdev_init(QDict
*bs_opts
,
312 BlockInterfaceType type
,
313 DriveMediaType media
)
316 const char *file
= NULL
;
318 const char *mediastr
= "";
324 int on_read_error
, on_write_error
;
334 bool has_driver_specific_opts
;
335 BlockDriver
*drv
= NULL
;
337 /* Check common options by copying from bs_opts to opts, all other options
338 * stay in bs_opts for processing by bdrv_open(). */
339 id
= qdict_get_try_str(bs_opts
, "id");
340 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
341 if (error_is_set(&error
)) {
342 qerror_report_err(error
);
347 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
348 if (error_is_set(&error
)) {
349 qerror_report_err(error
);
355 qdict_del(bs_opts
, "id");
358 has_driver_specific_opts
= !!qdict_size(bs_opts
);
360 /* extract parameters */
361 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
362 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
363 index
= qemu_opt_get_number(opts
, "index", -1);
365 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
366 ro
= qemu_opt_get_bool(opts
, "read-only", 0);
367 copy_on_read
= qemu_opt_get_bool(opts
, "copy-on-read", false);
369 file
= qemu_opt_get(opts
, "file");
370 serial
= qemu_opt_get(opts
, "serial");
372 max_devs
= if_max_devs
[type
];
374 if ((buf
= qemu_opt_get(opts
, "discard")) != NULL
) {
375 if (bdrv_parse_discard_flags(buf
, &bdrv_flags
) != 0) {
376 error_report("invalid discard option");
381 if (qemu_opt_get_bool(opts
, "cache.writeback", true)) {
382 bdrv_flags
|= BDRV_O_CACHE_WB
;
384 if (qemu_opt_get_bool(opts
, "cache.direct", false)) {
385 bdrv_flags
|= BDRV_O_NOCACHE
;
387 if (qemu_opt_get_bool(opts
, "cache.no-flush", false)) {
388 bdrv_flags
|= BDRV_O_NO_FLUSH
;
391 #ifdef CONFIG_LINUX_AIO
392 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
393 if (!strcmp(buf
, "native")) {
394 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
395 } else if (!strcmp(buf
, "threads")) {
396 /* this is the default */
398 error_report("invalid aio option");
404 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
405 if (is_help_option(buf
)) {
406 error_printf("Supported formats:");
407 bdrv_iterate_format(bdrv_format_print
, NULL
);
412 drv
= bdrv_find_format(buf
);
414 error_report("'%s' invalid format", buf
);
419 /* disk I/O throttling */
420 memset(&cfg
, 0, sizeof(cfg
));
421 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
=
422 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
423 cfg
.buckets
[THROTTLE_BPS_READ
].avg
=
424 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
425 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
=
426 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
427 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
=
428 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
429 cfg
.buckets
[THROTTLE_OPS_READ
].avg
=
430 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
431 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
=
432 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
434 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
=
435 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
436 cfg
.buckets
[THROTTLE_BPS_READ
].max
=
437 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
438 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
=
439 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
440 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
=
441 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
442 cfg
.buckets
[THROTTLE_OPS_READ
].max
=
443 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
444 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
=
445 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
447 cfg
.op_size
= qemu_opt_get_number(opts
, "throttling.iops-size", 0);
449 if (!check_throttle_config(&cfg
, &error
)) {
450 error_report("%s", error_get_pretty(error
));
455 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
456 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
457 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
458 error_report("werror is not supported by this bus type");
462 on_write_error
= parse_block_error_action(buf
, 0);
463 if (on_write_error
< 0) {
468 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
469 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
470 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
471 error_report("rerror is not supported by this bus type");
475 on_read_error
= parse_block_error_action(buf
, 1);
476 if (on_read_error
< 0) {
481 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
482 if (type
!= IF_VIRTIO
) {
483 error_report("addr is not supported by this bus type");
488 /* compute bus and unit according index */
491 if (bus_id
!= 0 || unit_id
!= -1) {
492 error_report("index cannot be used with bus and unit");
495 bus_id
= drive_index_to_bus_id(type
, index
);
496 unit_id
= drive_index_to_unit_id(type
, index
);
499 /* if user doesn't specify a unit_id,
500 * try to find the first free
505 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
507 if (max_devs
&& unit_id
>= max_devs
) {
516 if (max_devs
&& unit_id
>= max_devs
) {
517 error_report("unit %d too big (max is %d)",
518 unit_id
, max_devs
- 1);
523 * catch multiple definitions
526 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
527 error_report("drive with bus=%d, unit=%d (index=%d) exists",
528 bus_id
, unit_id
, index
);
532 /* no id supplied -> create one */
533 if (qemu_opts_id(opts
) == NULL
) {
535 if (type
== IF_IDE
|| type
== IF_SCSI
) {
536 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
539 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
542 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
545 qemu_opts_set_id(opts
, new_id
);
549 dinfo
= g_malloc0(sizeof(*dinfo
));
550 dinfo
->id
= g_strdup(qemu_opts_id(opts
));
551 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
552 dinfo
->bdrv
->open_flags
= snapshot
? BDRV_O_SNAPSHOT
: 0;
553 dinfo
->bdrv
->read_only
= ro
;
554 dinfo
->devaddr
= devaddr
;
557 dinfo
->unit
= unit_id
;
559 if (serial
!= NULL
) {
560 dinfo
->serial
= g_strdup(serial
);
562 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
564 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
566 /* disk I/O throttling */
567 if (throttle_enabled(&cfg
)) {
568 bdrv_io_limits_enable(dinfo
->bdrv
);
569 bdrv_set_io_limits(dinfo
->bdrv
, &cfg
);
577 dinfo
->media_cd
= media
== MEDIA_CDROM
;
586 /* add virtio block device */
588 devopts
= qemu_opts_create_nofail(qemu_find_opts("device"));
589 if (arch_type
== QEMU_ARCH_S390X
) {
590 qemu_opt_set(devopts
, "driver", "virtio-blk-s390");
592 qemu_opt_set(devopts
, "driver", "virtio-blk-pci");
594 qemu_opt_set(devopts
, "drive", dinfo
->id
);
596 qemu_opt_set(devopts
, "addr", devaddr
);
602 if (!file
|| !*file
) {
603 if (has_driver_specific_opts
) {
610 /* always use cache=unsafe with snapshot */
611 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
612 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
616 bdrv_flags
|= BDRV_O_COPY_ON_READ
;
619 if (runstate_check(RUN_STATE_INMIGRATE
)) {
620 bdrv_flags
|= BDRV_O_INCOMING
;
623 if (media
== MEDIA_CDROM
) {
624 /* CDROM is fine for any interface, don't check. */
626 } else if (ro
== 1) {
627 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&&
628 type
!= IF_NONE
&& type
!= IF_PFLASH
) {
629 error_report("read-only not supported by this bus type");
634 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
636 if (ro
&& copy_on_read
) {
637 error_report("warning: disabling copy_on_read on read-only drive");
641 ret
= bdrv_open(dinfo
->bdrv
, file
, bs_opts
, bdrv_flags
, drv
, &error
);
644 error_report("could not open disk image %s: %s",
645 file
?: dinfo
->id
, error_get_pretty(error
));
649 if (bdrv_key_required(dinfo
->bdrv
))
660 bdrv_unref(dinfo
->bdrv
);
662 QTAILQ_REMOVE(&drives
, dinfo
, next
);
667 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
)
671 value
= qemu_opt_get(opts
, from
);
673 qemu_opt_set(opts
, to
, value
);
674 qemu_opt_unset(opts
, from
);
678 QemuOptsList qemu_legacy_drive_opts
= {
680 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
684 .type
= QEMU_OPT_STRING
,
685 .help
= "media type (disk, cdrom)",
688 .type
= QEMU_OPT_STRING
,
689 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
692 .type
= QEMU_OPT_NUMBER
,
693 .help
= "number of cylinders (ide disk geometry)",
696 .type
= QEMU_OPT_NUMBER
,
697 .help
= "number of heads (ide disk geometry)",
700 .type
= QEMU_OPT_NUMBER
,
701 .help
= "number of sectors (ide disk geometry)",
704 .type
= QEMU_OPT_STRING
,
705 .help
= "chs translation (auto, lba, none)",
708 .type
= QEMU_OPT_BOOL
,
709 .help
= "(deprecated, ignored)",
711 { /* end of list */ }
715 DriveInfo
*drive_init(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
718 DriveInfo
*dinfo
= NULL
;
720 QemuOpts
*legacy_opts
;
721 DriveMediaType media
= MEDIA_DISK
;
722 BlockInterfaceType type
;
723 int cyls
, heads
, secs
, translation
;
724 Error
*local_err
= NULL
;
726 /* Change legacy command line options into QMP ones */
727 qemu_opt_rename(all_opts
, "iops", "throttling.iops-total");
728 qemu_opt_rename(all_opts
, "iops_rd", "throttling.iops-read");
729 qemu_opt_rename(all_opts
, "iops_wr", "throttling.iops-write");
731 qemu_opt_rename(all_opts
, "bps", "throttling.bps-total");
732 qemu_opt_rename(all_opts
, "bps_rd", "throttling.bps-read");
733 qemu_opt_rename(all_opts
, "bps_wr", "throttling.bps-write");
735 qemu_opt_rename(all_opts
, "iops_max", "throttling.iops-total-max");
736 qemu_opt_rename(all_opts
, "iops_rd_max", "throttling.iops-read-max");
737 qemu_opt_rename(all_opts
, "iops_wr_max", "throttling.iops-write-max");
739 qemu_opt_rename(all_opts
, "bps_max", "throttling.bps-total-max");
740 qemu_opt_rename(all_opts
, "bps_rd_max", "throttling.bps-read-max");
741 qemu_opt_rename(all_opts
, "bps_wr_max", "throttling.bps-write-max");
743 qemu_opt_rename(all_opts
,
744 "iops_size", "throttling.iops-size");
746 qemu_opt_rename(all_opts
, "readonly", "read-only");
748 value
= qemu_opt_get(all_opts
, "cache");
752 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
753 error_report("invalid cache option");
757 /* Specific options take precedence */
758 if (!qemu_opt_get(all_opts
, "cache.writeback")) {
759 qemu_opt_set_bool(all_opts
, "cache.writeback",
760 !!(flags
& BDRV_O_CACHE_WB
));
762 if (!qemu_opt_get(all_opts
, "cache.direct")) {
763 qemu_opt_set_bool(all_opts
, "cache.direct",
764 !!(flags
& BDRV_O_NOCACHE
));
766 if (!qemu_opt_get(all_opts
, "cache.no-flush")) {
767 qemu_opt_set_bool(all_opts
, "cache.no-flush",
768 !!(flags
& BDRV_O_NO_FLUSH
));
770 qemu_opt_unset(all_opts
, "cache");
773 /* Get a QDict for processing the options */
774 bs_opts
= qdict_new();
775 qemu_opts_to_qdict(all_opts
, bs_opts
);
777 legacy_opts
= qemu_opts_create_nofail(&qemu_legacy_drive_opts
);
778 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
779 if (error_is_set(&local_err
)) {
780 qerror_report_err(local_err
);
781 error_free(local_err
);
785 /* Deprecated option boot=[on|off] */
786 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
787 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
788 "ignored. Future versions will reject this parameter. Please "
789 "update your scripts.\n");
793 value
= qemu_opt_get(legacy_opts
, "media");
795 if (!strcmp(value
, "disk")) {
797 } else if (!strcmp(value
, "cdrom")) {
800 error_report("'%s' invalid media", value
);
805 /* Controller type */
806 value
= qemu_opt_get(legacy_opts
, "if");
809 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
812 if (type
== IF_COUNT
) {
813 error_report("unsupported bus type '%s'", value
);
817 type
= block_default_type
;
821 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
822 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
823 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
825 if (cyls
|| heads
|| secs
) {
827 error_report("invalid physical cyls number");
831 error_report("invalid physical heads number");
835 error_report("invalid physical secs number");
840 translation
= BIOS_ATA_TRANSLATION_AUTO
;
841 value
= qemu_opt_get(legacy_opts
, "trans");
844 error_report("'%s' trans must be used with cyls, heads and secs",
848 if (!strcmp(value
, "none")) {
849 translation
= BIOS_ATA_TRANSLATION_NONE
;
850 } else if (!strcmp(value
, "lba")) {
851 translation
= BIOS_ATA_TRANSLATION_LBA
;
852 } else if (!strcmp(value
, "auto")) {
853 translation
= BIOS_ATA_TRANSLATION_AUTO
;
855 error_report("'%s' invalid translation type", value
);
860 if (media
== MEDIA_CDROM
) {
861 if (cyls
|| secs
|| heads
) {
862 error_report("CHS can't be set with media=cdrom");
867 /* Actual block device init: Functionality shared with blockdev-add */
868 dinfo
= blockdev_init(bs_opts
, type
, media
);
873 /* Set legacy DriveInfo fields */
874 dinfo
->enable_auto_del
= true;
875 dinfo
->opts
= all_opts
;
878 dinfo
->heads
= heads
;
880 dinfo
->trans
= translation
;
883 qemu_opts_del(legacy_opts
);
887 void do_commit(Monitor
*mon
, const QDict
*qdict
)
889 const char *device
= qdict_get_str(qdict
, "device");
890 BlockDriverState
*bs
;
893 if (!strcmp(device
, "all")) {
894 ret
= bdrv_commit_all();
896 bs
= bdrv_find(device
);
898 monitor_printf(mon
, "Device '%s' not found\n", device
);
901 ret
= bdrv_commit(bs
);
904 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
909 static void blockdev_do_action(int kind
, void *data
, Error
**errp
)
911 TransactionAction action
;
912 TransactionActionList list
;
916 list
.value
= &action
;
918 qmp_transaction(&list
, errp
);
921 void qmp_blockdev_snapshot_sync(const char *device
, const char *snapshot_file
,
922 bool has_format
, const char *format
,
923 bool has_mode
, enum NewImageMode mode
,
926 BlockdevSnapshot snapshot
= {
927 .device
= (char *) device
,
928 .snapshot_file
= (char *) snapshot_file
,
929 .has_format
= has_format
,
930 .format
= (char *) format
,
931 .has_mode
= has_mode
,
934 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
938 void qmp_blockdev_snapshot_internal_sync(const char *device
,
942 BlockdevSnapshotInternal snapshot
= {
943 .device
= (char *) device
,
944 .name
= (char *) name
947 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
951 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
958 BlockDriverState
*bs
= bdrv_find(device
);
960 Error
*local_err
= NULL
;
961 SnapshotInfo
*info
= NULL
;
965 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
978 error_setg(errp
, "Name or id must be provided");
982 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
983 if (error_is_set(&local_err
)) {
984 error_propagate(errp
, local_err
);
989 "Snapshot with id '%s' and name '%s' does not exist on "
991 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
995 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
996 if (error_is_set(&local_err
)) {
997 error_propagate(errp
, local_err
);
1001 info
= g_malloc0(sizeof(SnapshotInfo
));
1002 info
->id
= g_strdup(sn
.id_str
);
1003 info
->name
= g_strdup(sn
.name
);
1004 info
->date_nsec
= sn
.date_nsec
;
1005 info
->date_sec
= sn
.date_sec
;
1006 info
->vm_state_size
= sn
.vm_state_size
;
1007 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1008 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1013 /* New and old BlockDriverState structs for group snapshots */
1015 typedef struct BlkTransactionState BlkTransactionState
;
1017 /* Only prepare() may fail. In a single transaction, only one of commit() or
1018 abort() will be called, clean() will always be called if it present. */
1019 typedef struct BdrvActionOps
{
1020 /* Size of state struct, in bytes. */
1021 size_t instance_size
;
1022 /* Prepare the work, must NOT be NULL. */
1023 void (*prepare
)(BlkTransactionState
*common
, Error
**errp
);
1024 /* Commit the changes, can be NULL. */
1025 void (*commit
)(BlkTransactionState
*common
);
1026 /* Abort the changes on fail, can be NULL. */
1027 void (*abort
)(BlkTransactionState
*common
);
1028 /* Clean up resource in the end, can be NULL. */
1029 void (*clean
)(BlkTransactionState
*common
);
1033 * This structure must be arranged as first member in child type, assuming
1034 * that compiler will also arrange it to the same address with parent instance.
1035 * Later it will be used in free().
1037 struct BlkTransactionState
{
1038 TransactionAction
*action
;
1039 const BdrvActionOps
*ops
;
1040 QSIMPLEQ_ENTRY(BlkTransactionState
) entry
;
1043 /* internal snapshot private data */
1044 typedef struct InternalSnapshotState
{
1045 BlkTransactionState common
;
1046 BlockDriverState
*bs
;
1047 QEMUSnapshotInfo sn
;
1048 } InternalSnapshotState
;
1050 static void internal_snapshot_prepare(BlkTransactionState
*common
,
1055 BlockDriverState
*bs
;
1056 QEMUSnapshotInfo old_sn
, *sn
;
1059 BlockdevSnapshotInternal
*internal
;
1060 InternalSnapshotState
*state
;
1063 g_assert(common
->action
->kind
==
1064 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1065 internal
= common
->action
->blockdev_snapshot_internal_sync
;
1066 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1068 /* 1. parse input */
1069 device
= internal
->device
;
1070 name
= internal
->name
;
1072 /* 2. check for validation */
1073 bs
= bdrv_find(device
);
1075 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1079 if (!bdrv_is_inserted(bs
)) {
1080 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1084 if (bdrv_is_read_only(bs
)) {
1085 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1089 if (!bdrv_can_snapshot(bs
)) {
1090 error_set(errp
, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
1091 bs
->drv
->format_name
, device
, "internal snapshot");
1095 if (!strlen(name
)) {
1096 error_setg(errp
, "Name is empty");
1100 /* check whether a snapshot with name exist */
1101 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
, errp
);
1102 if (error_is_set(errp
)) {
1106 "Snapshot with name '%s' already exists on device '%s'",
1111 /* 3. take the snapshot */
1113 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1114 qemu_gettimeofday(&tv
);
1115 sn
->date_sec
= tv
.tv_sec
;
1116 sn
->date_nsec
= tv
.tv_usec
* 1000;
1117 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1119 ret1
= bdrv_snapshot_create(bs
, sn
);
1121 error_setg_errno(errp
, -ret1
,
1122 "Failed to create snapshot '%s' on device '%s'",
1127 /* 4. succeed, mark a snapshot is created */
1131 static void internal_snapshot_abort(BlkTransactionState
*common
)
1133 InternalSnapshotState
*state
=
1134 DO_UPCAST(InternalSnapshotState
, common
, common
);
1135 BlockDriverState
*bs
= state
->bs
;
1136 QEMUSnapshotInfo
*sn
= &state
->sn
;
1137 Error
*local_error
= NULL
;
1143 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1144 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1145 "device '%s' in abort: %s",
1148 bdrv_get_device_name(bs
),
1149 error_get_pretty(local_error
));
1150 error_free(local_error
);
1154 /* external snapshot private data */
1155 typedef struct ExternalSnapshotState
{
1156 BlkTransactionState common
;
1157 BlockDriverState
*old_bs
;
1158 BlockDriverState
*new_bs
;
1159 } ExternalSnapshotState
;
1161 static void external_snapshot_prepare(BlkTransactionState
*common
,
1166 Error
*local_err
= NULL
;
1168 const char *new_image_file
;
1169 const char *format
= "qcow2";
1170 enum NewImageMode mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1171 ExternalSnapshotState
*state
=
1172 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1173 TransactionAction
*action
= common
->action
;
1175 /* get parameters */
1176 g_assert(action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
);
1178 device
= action
->blockdev_snapshot_sync
->device
;
1179 new_image_file
= action
->blockdev_snapshot_sync
->snapshot_file
;
1180 if (action
->blockdev_snapshot_sync
->has_format
) {
1181 format
= action
->blockdev_snapshot_sync
->format
;
1183 if (action
->blockdev_snapshot_sync
->has_mode
) {
1184 mode
= action
->blockdev_snapshot_sync
->mode
;
1187 /* start processing */
1188 drv
= bdrv_find_format(format
);
1190 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1194 state
->old_bs
= bdrv_find(device
);
1195 if (!state
->old_bs
) {
1196 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1200 if (!bdrv_is_inserted(state
->old_bs
)) {
1201 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1205 if (bdrv_in_use(state
->old_bs
)) {
1206 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1210 if (!bdrv_is_read_only(state
->old_bs
)) {
1211 if (bdrv_flush(state
->old_bs
)) {
1212 error_set(errp
, QERR_IO_ERROR
);
1217 if (bdrv_check_ext_snapshot(state
->old_bs
) != EXT_SNAPSHOT_ALLOWED
) {
1218 error_set(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1222 flags
= state
->old_bs
->open_flags
;
1224 /* create new image w/backing file */
1225 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1226 bdrv_img_create(new_image_file
, format
,
1227 state
->old_bs
->filename
,
1228 state
->old_bs
->drv
->format_name
,
1229 NULL
, -1, flags
, &local_err
, false);
1230 if (error_is_set(&local_err
)) {
1231 error_propagate(errp
, local_err
);
1236 /* We will manually add the backing_hd field to the bs later */
1237 state
->new_bs
= bdrv_new("");
1238 /* TODO Inherit bs->options or only take explicit options with an
1239 * extended QMP command? */
1240 ret
= bdrv_open(state
->new_bs
, new_image_file
, NULL
,
1241 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
1243 error_propagate(errp
, local_err
);
1247 static void external_snapshot_commit(BlkTransactionState
*common
)
1249 ExternalSnapshotState
*state
=
1250 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1252 /* This removes our old bs and adds the new bs */
1253 bdrv_append(state
->new_bs
, state
->old_bs
);
1254 /* We don't need (or want) to use the transactional
1255 * bdrv_reopen_multiple() across all the entries at once, because we
1256 * don't want to abort all of them if one of them fails the reopen */
1257 bdrv_reopen(state
->new_bs
, state
->new_bs
->open_flags
& ~BDRV_O_RDWR
,
1261 static void external_snapshot_abort(BlkTransactionState
*common
)
1263 ExternalSnapshotState
*state
=
1264 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1265 if (state
->new_bs
) {
1266 bdrv_unref(state
->new_bs
);
1270 typedef struct DriveBackupState
{
1271 BlkTransactionState common
;
1272 BlockDriverState
*bs
;
1276 static void drive_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1278 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1279 DriveBackup
*backup
;
1280 Error
*local_err
= NULL
;
1282 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1283 backup
= common
->action
->drive_backup
;
1285 qmp_drive_backup(backup
->device
, backup
->target
,
1286 backup
->has_format
, backup
->format
,
1288 backup
->has_mode
, backup
->mode
,
1289 backup
->has_speed
, backup
->speed
,
1290 backup
->has_on_source_error
, backup
->on_source_error
,
1291 backup
->has_on_target_error
, backup
->on_target_error
,
1293 if (error_is_set(&local_err
)) {
1294 error_propagate(errp
, local_err
);
1300 state
->bs
= bdrv_find(backup
->device
);
1301 state
->job
= state
->bs
->job
;
1304 static void drive_backup_abort(BlkTransactionState
*common
)
1306 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1307 BlockDriverState
*bs
= state
->bs
;
1309 /* Only cancel if it's the job we started */
1310 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1311 block_job_cancel_sync(bs
->job
);
1315 static void abort_prepare(BlkTransactionState
*common
, Error
**errp
)
1317 error_setg(errp
, "Transaction aborted using Abort action");
1320 static void abort_commit(BlkTransactionState
*common
)
1322 g_assert_not_reached(); /* this action never succeeds */
1325 static const BdrvActionOps actions
[] = {
1326 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
1327 .instance_size
= sizeof(ExternalSnapshotState
),
1328 .prepare
= external_snapshot_prepare
,
1329 .commit
= external_snapshot_commit
,
1330 .abort
= external_snapshot_abort
,
1332 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
1333 .instance_size
= sizeof(DriveBackupState
),
1334 .prepare
= drive_backup_prepare
,
1335 .abort
= drive_backup_abort
,
1337 [TRANSACTION_ACTION_KIND_ABORT
] = {
1338 .instance_size
= sizeof(BlkTransactionState
),
1339 .prepare
= abort_prepare
,
1340 .commit
= abort_commit
,
1342 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
1343 .instance_size
= sizeof(InternalSnapshotState
),
1344 .prepare
= internal_snapshot_prepare
,
1345 .abort
= internal_snapshot_abort
,
1350 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1351 * then we do not pivot any of the devices in the group, and abandon the
1354 void qmp_transaction(TransactionActionList
*dev_list
, Error
**errp
)
1356 TransactionActionList
*dev_entry
= dev_list
;
1357 BlkTransactionState
*state
, *next
;
1358 Error
*local_err
= NULL
;
1360 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkTransactionState
) snap_bdrv_states
;
1361 QSIMPLEQ_INIT(&snap_bdrv_states
);
1363 /* drain all i/o before any snapshots */
1366 /* We don't do anything in this loop that commits us to the snapshot */
1367 while (NULL
!= dev_entry
) {
1368 TransactionAction
*dev_info
= NULL
;
1369 const BdrvActionOps
*ops
;
1371 dev_info
= dev_entry
->value
;
1372 dev_entry
= dev_entry
->next
;
1374 assert(dev_info
->kind
< ARRAY_SIZE(actions
));
1376 ops
= &actions
[dev_info
->kind
];
1377 assert(ops
->instance_size
> 0);
1379 state
= g_malloc0(ops
->instance_size
);
1381 state
->action
= dev_info
;
1382 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
1384 state
->ops
->prepare(state
, &local_err
);
1385 if (error_is_set(&local_err
)) {
1386 error_propagate(errp
, local_err
);
1387 goto delete_and_fail
;
1391 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1392 if (state
->ops
->commit
) {
1393 state
->ops
->commit(state
);
1402 * failure, and it is all-or-none; abandon each new bs, and keep using
1403 * the original bs for all images
1405 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1406 if (state
->ops
->abort
) {
1407 state
->ops
->abort(state
);
1411 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
1412 if (state
->ops
->clean
) {
1413 state
->ops
->clean(state
);
1420 static void eject_device(BlockDriverState
*bs
, int force
, Error
**errp
)
1422 if (bdrv_in_use(bs
)) {
1423 error_set(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
1426 if (!bdrv_dev_has_removable_media(bs
)) {
1427 error_set(errp
, QERR_DEVICE_NOT_REMOVABLE
, bdrv_get_device_name(bs
));
1431 if (bdrv_dev_is_medium_locked(bs
) && !bdrv_dev_is_tray_open(bs
)) {
1432 bdrv_dev_eject_request(bs
, force
);
1434 error_set(errp
, QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
1442 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
1444 BlockDriverState
*bs
;
1446 bs
= bdrv_find(device
);
1448 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1452 eject_device(bs
, force
, errp
);
1455 void qmp_block_passwd(const char *device
, const char *password
, Error
**errp
)
1457 BlockDriverState
*bs
;
1460 bs
= bdrv_find(device
);
1462 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1466 err
= bdrv_set_key(bs
, password
);
1467 if (err
== -EINVAL
) {
1468 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1470 } else if (err
< 0) {
1471 error_set(errp
, QERR_INVALID_PASSWORD
);
1476 static void qmp_bdrv_open_encrypted(BlockDriverState
*bs
, const char *filename
,
1477 int bdrv_flags
, BlockDriver
*drv
,
1478 const char *password
, Error
**errp
)
1480 Error
*local_err
= NULL
;
1483 ret
= bdrv_open(bs
, filename
, NULL
, bdrv_flags
, drv
, &local_err
);
1485 error_propagate(errp
, local_err
);
1489 if (bdrv_key_required(bs
)) {
1491 if (bdrv_set_key(bs
, password
) < 0) {
1492 error_set(errp
, QERR_INVALID_PASSWORD
);
1495 error_set(errp
, QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
),
1496 bdrv_get_encrypted_filename(bs
));
1498 } else if (password
) {
1499 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1503 void qmp_change_blockdev(const char *device
, const char *filename
,
1504 bool has_format
, const char *format
, Error
**errp
)
1506 BlockDriverState
*bs
;
1507 BlockDriver
*drv
= NULL
;
1511 bs
= bdrv_find(device
);
1513 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1518 drv
= bdrv_find_whitelisted_format(format
, bs
->read_only
);
1520 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1525 eject_device(bs
, 0, &err
);
1526 if (error_is_set(&err
)) {
1527 error_propagate(errp
, err
);
1531 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
1532 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
1534 qmp_bdrv_open_encrypted(bs
, filename
, bdrv_flags
, drv
, NULL
, errp
);
1537 /* throttling disk I/O limits */
1538 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
1545 bool has_bps_rd_max
,
1547 bool has_bps_wr_max
,
1551 bool has_iops_rd_max
,
1552 int64_t iops_rd_max
,
1553 bool has_iops_wr_max
,
1554 int64_t iops_wr_max
,
1556 int64_t iops_size
, Error
**errp
)
1559 BlockDriverState
*bs
;
1561 bs
= bdrv_find(device
);
1563 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1567 memset(&cfg
, 0, sizeof(cfg
));
1568 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
1569 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
1570 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
1572 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
1573 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
1574 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
1577 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
1579 if (has_bps_rd_max
) {
1580 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
1582 if (has_bps_wr_max
) {
1583 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
1586 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
1588 if (has_iops_rd_max
) {
1589 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
1591 if (has_iops_wr_max
) {
1592 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
1595 if (has_iops_size
) {
1596 cfg
.op_size
= iops_size
;
1599 if (!check_throttle_config(&cfg
, errp
)) {
1603 if (!bs
->io_limits_enabled
&& throttle_enabled(&cfg
)) {
1604 bdrv_io_limits_enable(bs
);
1605 } else if (bs
->io_limits_enabled
&& !throttle_enabled(&cfg
)) {
1606 bdrv_io_limits_disable(bs
);
1609 if (bs
->io_limits_enabled
) {
1610 bdrv_set_io_limits(bs
, &cfg
);
1614 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1616 const char *id
= qdict_get_str(qdict
, "id");
1617 BlockDriverState
*bs
;
1621 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1624 if (bdrv_in_use(bs
)) {
1625 qerror_report(QERR_DEVICE_IN_USE
, id
);
1629 /* quiesce block driver; prevent further io */
1634 /* if we have a device attached to this BlockDriverState
1635 * then we need to make the drive anonymous until the device
1636 * can be removed. If this is a drive with no device backing
1637 * then we can just get rid of the block driver state right here.
1639 if (bdrv_get_attached_dev(bs
)) {
1642 /* Further I/O must not pause the guest */
1643 bdrv_set_on_error(bs
, BLOCKDEV_ON_ERROR_REPORT
,
1644 BLOCKDEV_ON_ERROR_REPORT
);
1646 drive_uninit(drive_get_by_blockdev(bs
));
1652 void qmp_block_resize(const char *device
, int64_t size
, Error
**errp
)
1654 BlockDriverState
*bs
;
1657 bs
= bdrv_find(device
);
1659 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1664 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
1668 /* complete all in-flight operations before resizing the device */
1671 ret
= bdrv_truncate(bs
, size
);
1676 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1679 error_set(errp
, QERR_UNSUPPORTED
);
1682 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1685 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1688 error_setg_errno(errp
, -ret
, "Could not resize");
1693 static void block_job_cb(void *opaque
, int ret
)
1695 BlockDriverState
*bs
= opaque
;
1698 trace_block_job_cb(bs
, bs
->job
, ret
);
1701 obj
= qobject_from_block_job(bs
->job
);
1703 QDict
*dict
= qobject_to_qdict(obj
);
1704 qdict_put(dict
, "error", qstring_from_str(strerror(-ret
)));
1707 if (block_job_is_cancelled(bs
->job
)) {
1708 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED
, obj
);
1710 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED
, obj
);
1712 qobject_decref(obj
);
1714 bdrv_put_ref_bh_schedule(bs
);
1717 void qmp_block_stream(const char *device
, bool has_base
,
1718 const char *base
, bool has_speed
, int64_t speed
,
1719 bool has_on_error
, BlockdevOnError on_error
,
1722 BlockDriverState
*bs
;
1723 BlockDriverState
*base_bs
= NULL
;
1724 Error
*local_err
= NULL
;
1726 if (!has_on_error
) {
1727 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1730 bs
= bdrv_find(device
);
1732 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1737 base_bs
= bdrv_find_backing_image(bs
, base
);
1738 if (base_bs
== NULL
) {
1739 error_set(errp
, QERR_BASE_NOT_FOUND
, base
);
1744 stream_start(bs
, base_bs
, base
, has_speed
? speed
: 0,
1745 on_error
, block_job_cb
, bs
, &local_err
);
1746 if (error_is_set(&local_err
)) {
1747 error_propagate(errp
, local_err
);
1751 trace_qmp_block_stream(bs
, bs
->job
);
1754 void qmp_block_commit(const char *device
,
1755 bool has_base
, const char *base
, const char *top
,
1756 bool has_speed
, int64_t speed
,
1759 BlockDriverState
*bs
;
1760 BlockDriverState
*base_bs
, *top_bs
;
1761 Error
*local_err
= NULL
;
1762 /* This will be part of the QMP command, if/when the
1763 * BlockdevOnError change for blkmirror makes it in
1765 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1767 /* drain all i/o before commits */
1770 bs
= bdrv_find(device
);
1772 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1776 /* default top_bs is the active layer */
1780 if (strcmp(bs
->filename
, top
) != 0) {
1781 top_bs
= bdrv_find_backing_image(bs
, top
);
1785 if (top_bs
== NULL
) {
1786 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
1790 if (has_base
&& base
) {
1791 base_bs
= bdrv_find_backing_image(top_bs
, base
);
1793 base_bs
= bdrv_find_base(top_bs
);
1796 if (base_bs
== NULL
) {
1797 error_set(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
1801 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
1803 if (local_err
!= NULL
) {
1804 error_propagate(errp
, local_err
);
1809 void qmp_drive_backup(const char *device
, const char *target
,
1810 bool has_format
, const char *format
,
1811 enum MirrorSyncMode sync
,
1812 bool has_mode
, enum NewImageMode mode
,
1813 bool has_speed
, int64_t speed
,
1814 bool has_on_source_error
, BlockdevOnError on_source_error
,
1815 bool has_on_target_error
, BlockdevOnError on_target_error
,
1818 BlockDriverState
*bs
;
1819 BlockDriverState
*target_bs
;
1820 BlockDriverState
*source
= NULL
;
1821 BlockDriver
*drv
= NULL
;
1822 Error
*local_err
= NULL
;
1830 if (!has_on_source_error
) {
1831 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
1833 if (!has_on_target_error
) {
1834 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
1837 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1840 bs
= bdrv_find(device
);
1842 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1846 if (!bdrv_is_inserted(bs
)) {
1847 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1852 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
1855 drv
= bdrv_find_format(format
);
1857 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1862 if (bdrv_in_use(bs
)) {
1863 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1867 flags
= bs
->open_flags
| BDRV_O_RDWR
;
1869 /* See if we have a backing HD we can use to create our new image
1871 if (sync
== MIRROR_SYNC_MODE_TOP
) {
1872 source
= bs
->backing_hd
;
1874 sync
= MIRROR_SYNC_MODE_FULL
;
1877 if (sync
== MIRROR_SYNC_MODE_NONE
) {
1881 size
= bdrv_getlength(bs
);
1883 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1887 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1888 assert(format
&& drv
);
1890 bdrv_img_create(target
, format
, source
->filename
,
1891 source
->drv
->format_name
, NULL
,
1892 size
, flags
, &local_err
, false);
1894 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
1895 size
, flags
, &local_err
, false);
1899 if (error_is_set(&local_err
)) {
1900 error_propagate(errp
, local_err
);
1904 target_bs
= bdrv_new("");
1905 ret
= bdrv_open(target_bs
, target
, NULL
, flags
, drv
, &local_err
);
1907 bdrv_unref(target_bs
);
1908 error_propagate(errp
, local_err
);
1912 backup_start(bs
, target_bs
, speed
, sync
, on_source_error
, on_target_error
,
1913 block_job_cb
, bs
, &local_err
);
1914 if (local_err
!= NULL
) {
1915 bdrv_unref(target_bs
);
1916 error_propagate(errp
, local_err
);
1921 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1923 void qmp_drive_mirror(const char *device
, const char *target
,
1924 bool has_format
, const char *format
,
1925 enum MirrorSyncMode sync
,
1926 bool has_mode
, enum NewImageMode mode
,
1927 bool has_speed
, int64_t speed
,
1928 bool has_granularity
, uint32_t granularity
,
1929 bool has_buf_size
, int64_t buf_size
,
1930 bool has_on_source_error
, BlockdevOnError on_source_error
,
1931 bool has_on_target_error
, BlockdevOnError on_target_error
,
1934 BlockDriverState
*bs
;
1935 BlockDriverState
*source
, *target_bs
;
1936 BlockDriver
*drv
= NULL
;
1937 Error
*local_err
= NULL
;
1945 if (!has_on_source_error
) {
1946 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
1948 if (!has_on_target_error
) {
1949 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
1952 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1954 if (!has_granularity
) {
1957 if (!has_buf_size
) {
1958 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
1961 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
1962 error_set(errp
, QERR_INVALID_PARAMETER
, device
);
1965 if (granularity
& (granularity
- 1)) {
1966 error_set(errp
, QERR_INVALID_PARAMETER
, device
);
1970 bs
= bdrv_find(device
);
1972 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1976 if (!bdrv_is_inserted(bs
)) {
1977 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1982 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
1985 drv
= bdrv_find_format(format
);
1987 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1992 if (bdrv_in_use(bs
)) {
1993 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1997 flags
= bs
->open_flags
| BDRV_O_RDWR
;
1998 source
= bs
->backing_hd
;
1999 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
2000 sync
= MIRROR_SYNC_MODE_FULL
;
2003 size
= bdrv_getlength(bs
);
2005 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2009 if (sync
== MIRROR_SYNC_MODE_FULL
&& mode
!= NEW_IMAGE_MODE_EXISTING
) {
2010 /* create new image w/o backing file */
2011 assert(format
&& drv
);
2012 bdrv_img_create(target
, format
,
2013 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
2016 case NEW_IMAGE_MODE_EXISTING
:
2018 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
2019 /* create new image with backing file */
2020 bdrv_img_create(target
, format
,
2022 source
->drv
->format_name
,
2023 NULL
, size
, flags
, &local_err
, false);
2030 if (error_is_set(&local_err
)) {
2031 error_propagate(errp
, local_err
);
2035 /* Mirroring takes care of copy-on-write using the source's backing
2038 target_bs
= bdrv_new("");
2039 ret
= bdrv_open(target_bs
, target
, NULL
, flags
| BDRV_O_NO_BACKING
, drv
,
2042 bdrv_unref(target_bs
);
2043 error_propagate(errp
, local_err
);
2047 mirror_start(bs
, target_bs
, speed
, granularity
, buf_size
, sync
,
2048 on_source_error
, on_target_error
,
2049 block_job_cb
, bs
, &local_err
);
2050 if (local_err
!= NULL
) {
2051 bdrv_unref(target_bs
);
2052 error_propagate(errp
, local_err
);
2057 static BlockJob
*find_block_job(const char *device
)
2059 BlockDriverState
*bs
;
2061 bs
= bdrv_find(device
);
2062 if (!bs
|| !bs
->job
) {
2068 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
2070 BlockJob
*job
= find_block_job(device
);
2073 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2077 block_job_set_speed(job
, speed
, errp
);
2080 void qmp_block_job_cancel(const char *device
,
2081 bool has_force
, bool force
, Error
**errp
)
2083 BlockJob
*job
= find_block_job(device
);
2090 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2093 if (job
->paused
&& !force
) {
2094 error_set(errp
, QERR_BLOCK_JOB_PAUSED
, device
);
2098 trace_qmp_block_job_cancel(job
);
2099 block_job_cancel(job
);
2102 void qmp_block_job_pause(const char *device
, Error
**errp
)
2104 BlockJob
*job
= find_block_job(device
);
2107 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2111 trace_qmp_block_job_pause(job
);
2112 block_job_pause(job
);
2115 void qmp_block_job_resume(const char *device
, Error
**errp
)
2117 BlockJob
*job
= find_block_job(device
);
2120 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2124 trace_qmp_block_job_resume(job
);
2125 block_job_resume(job
);
2128 void qmp_block_job_complete(const char *device
, Error
**errp
)
2130 BlockJob
*job
= find_block_job(device
);
2133 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
2137 trace_qmp_block_job_complete(job
);
2138 block_job_complete(job
, errp
);
2141 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
2143 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
2147 Error
*local_err
= NULL
;
2149 /* Require an ID in the top level */
2150 if (!options
->has_id
) {
2151 error_setg(errp
, "Block device needs an ID");
2155 /* TODO Sort it out in raw-posix and drive_init: Reject aio=native with
2156 * cache.direct=false instead of silently switching to aio=threads, except
2157 * if called from drive_init.
2159 * For now, simply forbidding the combination for all drivers will do. */
2160 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
2161 bool direct
= options
->cache
->has_direct
&& options
->cache
->direct
;
2162 if (!options
->has_cache
&& !direct
) {
2163 error_setg(errp
, "aio=native requires cache.direct=true");
2168 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
2169 &options
, NULL
, &local_err
);
2170 if (error_is_set(&local_err
)) {
2171 error_propagate(errp
, local_err
);
2175 obj
= qmp_output_get_qobject(ov
);
2176 qdict
= qobject_to_qdict(obj
);
2178 qdict_flatten(qdict
);
2180 dinfo
= blockdev_init(qdict
, IF_NONE
, MEDIA_DISK
);
2182 error_setg(errp
, "Could not open image");
2187 qmp_output_visitor_cleanup(ov
);
2190 static void do_qmp_query_block_jobs_one(void *opaque
, BlockDriverState
*bs
)
2192 BlockJobInfoList
**prev
= opaque
;
2193 BlockJob
*job
= bs
->job
;
2196 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
2197 elem
->value
= block_job_query(bs
->job
);
2198 (*prev
)->next
= elem
;
2203 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
2205 /* Dummy is a fake list element for holding the head pointer */
2206 BlockJobInfoList dummy
= {};
2207 BlockJobInfoList
*prev
= &dummy
;
2208 bdrv_iterate(do_qmp_query_block_jobs_one
, &prev
);
2212 QemuOptsList qemu_common_drive_opts
= {
2214 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
2218 .type
= QEMU_OPT_NUMBER
,
2219 .help
= "bus number",
2222 .type
= QEMU_OPT_NUMBER
,
2223 .help
= "unit number (i.e. lun for scsi)",
2226 .type
= QEMU_OPT_NUMBER
,
2227 .help
= "index number",
2230 .type
= QEMU_OPT_BOOL
,
2231 .help
= "enable/disable snapshot mode",
2234 .type
= QEMU_OPT_STRING
,
2235 .help
= "disk image",
2238 .type
= QEMU_OPT_STRING
,
2239 .help
= "discard operation (ignore/off, unmap/on)",
2241 .name
= "cache.writeback",
2242 .type
= QEMU_OPT_BOOL
,
2243 .help
= "enables writeback mode for any caches",
2245 .name
= "cache.direct",
2246 .type
= QEMU_OPT_BOOL
,
2247 .help
= "enables use of O_DIRECT (bypass the host page cache)",
2249 .name
= "cache.no-flush",
2250 .type
= QEMU_OPT_BOOL
,
2251 .help
= "ignore any flush requests for the device",
2254 .type
= QEMU_OPT_STRING
,
2255 .help
= "host AIO implementation (threads, native)",
2258 .type
= QEMU_OPT_STRING
,
2259 .help
= "disk format (raw, qcow2, ...)",
2262 .type
= QEMU_OPT_STRING
,
2263 .help
= "disk serial number",
2266 .type
= QEMU_OPT_STRING
,
2267 .help
= "read error action",
2270 .type
= QEMU_OPT_STRING
,
2271 .help
= "write error action",
2274 .type
= QEMU_OPT_STRING
,
2275 .help
= "pci address (virtio only)",
2277 .name
= "read-only",
2278 .type
= QEMU_OPT_BOOL
,
2279 .help
= "open drive file as read-only",
2281 .name
= "throttling.iops-total",
2282 .type
= QEMU_OPT_NUMBER
,
2283 .help
= "limit total I/O operations per second",
2285 .name
= "throttling.iops-read",
2286 .type
= QEMU_OPT_NUMBER
,
2287 .help
= "limit read operations per second",
2289 .name
= "throttling.iops-write",
2290 .type
= QEMU_OPT_NUMBER
,
2291 .help
= "limit write operations per second",
2293 .name
= "throttling.bps-total",
2294 .type
= QEMU_OPT_NUMBER
,
2295 .help
= "limit total bytes per second",
2297 .name
= "throttling.bps-read",
2298 .type
= QEMU_OPT_NUMBER
,
2299 .help
= "limit read bytes per second",
2301 .name
= "throttling.bps-write",
2302 .type
= QEMU_OPT_NUMBER
,
2303 .help
= "limit write bytes per second",
2305 .name
= "throttling.iops-total-max",
2306 .type
= QEMU_OPT_NUMBER
,
2307 .help
= "I/O operations burst",
2309 .name
= "throttling.iops-read-max",
2310 .type
= QEMU_OPT_NUMBER
,
2311 .help
= "I/O operations read burst",
2313 .name
= "throttling.iops-write-max",
2314 .type
= QEMU_OPT_NUMBER
,
2315 .help
= "I/O operations write burst",
2317 .name
= "throttling.bps-total-max",
2318 .type
= QEMU_OPT_NUMBER
,
2319 .help
= "total bytes burst",
2321 .name
= "throttling.bps-read-max",
2322 .type
= QEMU_OPT_NUMBER
,
2323 .help
= "total bytes read burst",
2325 .name
= "throttling.bps-write-max",
2326 .type
= QEMU_OPT_NUMBER
,
2327 .help
= "total bytes write burst",
2329 .name
= "throttling.iops-size",
2330 .type
= QEMU_OPT_NUMBER
,
2331 .help
= "when limiting by iops max size of an I/O in bytes",
2333 .name
= "copy-on-read",
2334 .type
= QEMU_OPT_BOOL
,
2335 .help
= "copy read data from backing file into image file",
2337 { /* end of list */ }
2341 QemuOptsList qemu_drive_opts
= {
2343 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
2346 * no elements => accept any params
2347 * validation will happen later
2349 { /* end of list */ }