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.
10 #include "sysemu/blockdev.h"
11 #include "hw/block-common.h"
12 #include "block/blockjob.h"
13 #include "monitor/monitor.h"
14 #include "qapi/qmp/qerror.h"
15 #include "qemu/option.h"
16 #include "qemu/config-file.h"
17 #include "qapi/qmp/types.h"
18 #include "sysemu/sysemu.h"
19 #include "block/block_int.h"
20 #include "qmp-commands.h"
22 #include "sysemu/arch_init.h"
24 static QTAILQ_HEAD(drivelist
, DriveInfo
) drives
= QTAILQ_HEAD_INITIALIZER(drives
);
25 extern QemuOptsList qemu_common_drive_opts
;
27 static const char *const if_name
[IF_COUNT
] = {
31 [IF_FLOPPY
] = "floppy",
32 [IF_PFLASH
] = "pflash",
35 [IF_VIRTIO
] = "virtio",
39 static const int if_max_devs
[IF_COUNT
] = {
41 * Do not change these numbers! They govern how drive option
42 * index maps to unit and bus. That mapping is ABI.
44 * All controllers used to imlement if=T drives need to support
45 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
46 * Otherwise, some index values map to "impossible" bus, unit
49 * For instance, if you change [IF_SCSI] to 255, -drive
50 * if=scsi,index=12 no longer means bus=1,unit=5, but
51 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
52 * the drive can't be set up. Regression.
59 * We automatically delete the drive when a device using it gets
60 * unplugged. Questionable feature, but we can't just drop it.
61 * Device models call blockdev_mark_auto_del() to schedule the
62 * automatic deletion, and generic qdev code calls blockdev_auto_del()
63 * when deletion is actually safe.
65 void blockdev_mark_auto_del(BlockDriverState
*bs
)
67 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
70 block_job_cancel(bs
->job
);
77 void blockdev_auto_del(BlockDriverState
*bs
)
79 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
81 if (dinfo
&& dinfo
->auto_del
) {
86 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
88 int max_devs
= if_max_devs
[type
];
89 return max_devs
? index
/ max_devs
: 0;
92 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
94 int max_devs
= if_max_devs
[type
];
95 return max_devs
? index
% max_devs
: index
;
98 QemuOpts
*drive_def(const char *optstr
)
100 return qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
103 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
109 opts
= drive_def(optstr
);
113 if (type
!= IF_DEFAULT
) {
114 qemu_opt_set(opts
, "if", if_name
[type
]);
117 snprintf(buf
, sizeof(buf
), "%d", index
);
118 qemu_opt_set(opts
, "index", buf
);
121 qemu_opt_set(opts
, "file", file
);
125 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
129 /* seek interface, bus and unit */
131 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
132 if (dinfo
->type
== type
&&
141 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
143 return drive_get(type
,
144 drive_index_to_bus_id(type
, index
),
145 drive_index_to_unit_id(type
, index
));
148 int drive_get_max_bus(BlockInterfaceType type
)
154 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
155 if(dinfo
->type
== type
&&
156 dinfo
->bus
> max_bus
)
157 max_bus
= dinfo
->bus
;
162 /* Get a block device. This should only be used for single-drive devices
163 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
165 DriveInfo
*drive_get_next(BlockInterfaceType type
)
167 static int next_block_unit
[IF_COUNT
];
169 return drive_get(type
, 0, next_block_unit
[type
]++);
172 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
176 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
177 if (dinfo
->bdrv
== bs
) {
184 static void bdrv_format_print(void *opaque
, const char *name
)
186 error_printf(" %s", name
);
189 static void drive_uninit(DriveInfo
*dinfo
)
191 qemu_opts_del(dinfo
->opts
);
192 bdrv_delete(dinfo
->bdrv
);
194 QTAILQ_REMOVE(&drives
, dinfo
, next
);
195 g_free(dinfo
->serial
);
199 void drive_put_ref(DriveInfo
*dinfo
)
201 assert(dinfo
->refcount
);
202 if (--dinfo
->refcount
== 0) {
207 void drive_get_ref(DriveInfo
*dinfo
)
217 static void drive_put_ref_bh(void *opaque
)
219 DrivePutRefBH
*s
= opaque
;
221 drive_put_ref(s
->dinfo
);
222 qemu_bh_delete(s
->bh
);
227 * Release a drive reference in a BH
229 * It is not possible to use drive_put_ref() from a callback function when the
230 * callers still need the drive. In such cases we schedule a BH to release the
233 static void drive_put_ref_bh_schedule(DriveInfo
*dinfo
)
237 s
= g_new(DrivePutRefBH
, 1);
238 s
->bh
= qemu_bh_new(drive_put_ref_bh
, s
);
240 qemu_bh_schedule(s
->bh
);
243 static int parse_block_error_action(const char *buf
, bool is_read
)
245 if (!strcmp(buf
, "ignore")) {
246 return BLOCKDEV_ON_ERROR_IGNORE
;
247 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
248 return BLOCKDEV_ON_ERROR_ENOSPC
;
249 } else if (!strcmp(buf
, "stop")) {
250 return BLOCKDEV_ON_ERROR_STOP
;
251 } else if (!strcmp(buf
, "report")) {
252 return BLOCKDEV_ON_ERROR_REPORT
;
254 error_report("'%s' invalid %s error action",
255 buf
, is_read
? "read" : "write");
260 static bool do_check_io_limits(BlockIOLimit
*io_limits
, Error
**errp
)
267 bps_flag
= (io_limits
->bps
[BLOCK_IO_LIMIT_TOTAL
] != 0)
268 && ((io_limits
->bps
[BLOCK_IO_LIMIT_READ
] != 0)
269 || (io_limits
->bps
[BLOCK_IO_LIMIT_WRITE
] != 0));
270 iops_flag
= (io_limits
->iops
[BLOCK_IO_LIMIT_TOTAL
] != 0)
271 && ((io_limits
->iops
[BLOCK_IO_LIMIT_READ
] != 0)
272 || (io_limits
->iops
[BLOCK_IO_LIMIT_WRITE
] != 0));
273 if (bps_flag
|| iops_flag
) {
274 error_setg(errp
, "bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
275 "cannot be used at the same time");
279 if (io_limits
->bps
[BLOCK_IO_LIMIT_TOTAL
] < 0 ||
280 io_limits
->bps
[BLOCK_IO_LIMIT_WRITE
] < 0 ||
281 io_limits
->bps
[BLOCK_IO_LIMIT_READ
] < 0 ||
282 io_limits
->iops
[BLOCK_IO_LIMIT_TOTAL
] < 0 ||
283 io_limits
->iops
[BLOCK_IO_LIMIT_WRITE
] < 0 ||
284 io_limits
->iops
[BLOCK_IO_LIMIT_READ
] < 0) {
285 error_setg(errp
, "bps and iops values must be 0 or greater");
292 DriveInfo
*drive_init(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
295 const char *file
= NULL
;
297 const char *mediastr
= "";
298 BlockInterfaceType type
;
299 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
301 int cyls
, heads
, secs
, translation
;
302 BlockDriver
*drv
= NULL
;
307 int on_read_error
, on_write_error
;
310 BlockIOLimit io_limits
;
319 translation
= BIOS_ATA_TRANSLATION_AUTO
;
322 /* Check common options by copying from all_opts to opts, all other options
323 * are stored in bs_opts. */
324 id
= qemu_opts_id(all_opts
);
325 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
326 if (error_is_set(&error
)) {
327 qerror_report_err(error
);
332 bs_opts
= qdict_new();
333 qemu_opts_to_qdict(all_opts
, bs_opts
);
334 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
335 if (error_is_set(&error
)) {
336 qerror_report_err(error
);
342 qdict_del(bs_opts
, "id");
345 /* extract parameters */
346 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
347 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
348 index
= qemu_opt_get_number(opts
, "index", -1);
350 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
351 heads
= qemu_opt_get_number(opts
, "heads", 0);
352 secs
= qemu_opt_get_number(opts
, "secs", 0);
354 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
355 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
356 copy_on_read
= qemu_opt_get_bool(opts
, "copy-on-read", false);
358 file
= qemu_opt_get(opts
, "file");
359 serial
= qemu_opt_get(opts
, "serial");
361 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
362 for (type
= 0; type
< IF_COUNT
&& strcmp(buf
, if_name
[type
]); type
++)
364 if (type
== IF_COUNT
) {
365 error_report("unsupported bus type '%s'", buf
);
369 type
= block_default_type
;
372 max_devs
= if_max_devs
[type
];
374 if (cyls
|| heads
|| secs
) {
376 error_report("invalid physical cyls number");
380 error_report("invalid physical heads number");
384 error_report("invalid physical secs number");
389 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
391 error_report("'%s' trans must be used with cyls, heads and secs",
395 if (!strcmp(buf
, "none"))
396 translation
= BIOS_ATA_TRANSLATION_NONE
;
397 else if (!strcmp(buf
, "lba"))
398 translation
= BIOS_ATA_TRANSLATION_LBA
;
399 else if (!strcmp(buf
, "auto"))
400 translation
= BIOS_ATA_TRANSLATION_AUTO
;
402 error_report("'%s' invalid translation type", buf
);
407 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
408 if (!strcmp(buf
, "disk")) {
410 } else if (!strcmp(buf
, "cdrom")) {
411 if (cyls
|| secs
|| heads
) {
412 error_report("CHS can't be set with media=%s", buf
);
417 error_report("'%s' invalid media", buf
);
422 if ((buf
= qemu_opt_get(opts
, "discard")) != NULL
) {
423 if (bdrv_parse_discard_flags(buf
, &bdrv_flags
) != 0) {
424 error_report("invalid discard option");
429 bdrv_flags
|= BDRV_O_CACHE_WB
;
430 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
431 if (bdrv_parse_cache_flags(buf
, &bdrv_flags
) != 0) {
432 error_report("invalid cache option");
437 #ifdef CONFIG_LINUX_AIO
438 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
439 if (!strcmp(buf
, "native")) {
440 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
441 } else if (!strcmp(buf
, "threads")) {
442 /* this is the default */
444 error_report("invalid aio option");
450 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
451 if (is_help_option(buf
)) {
452 error_printf("Supported formats:");
453 bdrv_iterate_format(bdrv_format_print
, NULL
);
457 drv
= bdrv_find_whitelisted_format(buf
);
459 error_report("'%s' invalid format", buf
);
464 /* disk I/O throttling */
465 io_limits
.bps
[BLOCK_IO_LIMIT_TOTAL
] =
466 qemu_opt_get_number(opts
, "bps", 0);
467 io_limits
.bps
[BLOCK_IO_LIMIT_READ
] =
468 qemu_opt_get_number(opts
, "bps_rd", 0);
469 io_limits
.bps
[BLOCK_IO_LIMIT_WRITE
] =
470 qemu_opt_get_number(opts
, "bps_wr", 0);
471 io_limits
.iops
[BLOCK_IO_LIMIT_TOTAL
] =
472 qemu_opt_get_number(opts
, "iops", 0);
473 io_limits
.iops
[BLOCK_IO_LIMIT_READ
] =
474 qemu_opt_get_number(opts
, "iops_rd", 0);
475 io_limits
.iops
[BLOCK_IO_LIMIT_WRITE
] =
476 qemu_opt_get_number(opts
, "iops_wr", 0);
478 if (!do_check_io_limits(&io_limits
, &error
)) {
479 error_report("%s", error_get_pretty(error
));
484 if (qemu_opt_get(opts
, "boot") != NULL
) {
485 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
486 "ignored. Future versions will reject this parameter. Please "
487 "update your scripts.\n");
490 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
491 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
492 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
493 error_report("werror is not supported by this bus type");
497 on_write_error
= parse_block_error_action(buf
, 0);
498 if (on_write_error
< 0) {
503 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
504 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
505 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
506 error_report("rerror is not supported by this bus type");
510 on_read_error
= parse_block_error_action(buf
, 1);
511 if (on_read_error
< 0) {
516 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
517 if (type
!= IF_VIRTIO
) {
518 error_report("addr is not supported by this bus type");
523 /* compute bus and unit according index */
526 if (bus_id
!= 0 || unit_id
!= -1) {
527 error_report("index cannot be used with bus and unit");
530 bus_id
= drive_index_to_bus_id(type
, index
);
531 unit_id
= drive_index_to_unit_id(type
, index
);
534 /* if user doesn't specify a unit_id,
535 * try to find the first free
540 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
542 if (max_devs
&& unit_id
>= max_devs
) {
551 if (max_devs
&& unit_id
>= max_devs
) {
552 error_report("unit %d too big (max is %d)",
553 unit_id
, max_devs
- 1);
558 * catch multiple definitions
561 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
562 error_report("drive with bus=%d, unit=%d (index=%d) exists",
563 bus_id
, unit_id
, index
);
569 dinfo
= g_malloc0(sizeof(*dinfo
));
570 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
571 dinfo
->id
= g_strdup(buf
);
573 /* no id supplied -> create one */
574 dinfo
->id
= g_malloc0(32);
575 if (type
== IF_IDE
|| type
== IF_SCSI
)
576 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
578 snprintf(dinfo
->id
, 32, "%s%i%s%i",
579 if_name
[type
], bus_id
, mediastr
, unit_id
);
581 snprintf(dinfo
->id
, 32, "%s%s%i",
582 if_name
[type
], mediastr
, unit_id
);
584 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
585 dinfo
->bdrv
->open_flags
= snapshot
? BDRV_O_SNAPSHOT
: 0;
586 dinfo
->bdrv
->read_only
= ro
;
587 dinfo
->devaddr
= devaddr
;
590 dinfo
->unit
= unit_id
;
592 dinfo
->heads
= heads
;
594 dinfo
->trans
= translation
;
595 dinfo
->opts
= all_opts
;
597 if (serial
!= NULL
) {
598 dinfo
->serial
= g_strdup(serial
);
600 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
602 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
604 /* disk I/O throttling */
605 bdrv_set_io_limits(dinfo
->bdrv
, &io_limits
);
612 dinfo
->media_cd
= media
== MEDIA_CDROM
;
621 /* add virtio block device */
623 devopts
= qemu_opts_create_nofail(qemu_find_opts("device"));
624 if (arch_type
== QEMU_ARCH_S390X
) {
625 qemu_opt_set(devopts
, "driver", "virtio-blk-s390");
627 qemu_opt_set(devopts
, "driver", "virtio-blk-pci");
629 qemu_opt_set(devopts
, "drive", dinfo
->id
);
631 qemu_opt_set(devopts
, "addr", devaddr
);
637 if (!file
|| !*file
) {
641 /* always use cache=unsafe with snapshot */
642 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
643 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
647 bdrv_flags
|= BDRV_O_COPY_ON_READ
;
650 if (runstate_check(RUN_STATE_INMIGRATE
)) {
651 bdrv_flags
|= BDRV_O_INCOMING
;
654 if (media
== MEDIA_CDROM
) {
655 /* CDROM is fine for any interface, don't check. */
657 } else if (ro
== 1) {
658 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&&
659 type
!= IF_NONE
&& type
!= IF_PFLASH
) {
660 error_report("readonly not supported by this bus type");
665 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
667 if (ro
&& copy_on_read
) {
668 error_report("warning: disabling copy_on_read on readonly drive");
671 ret
= bdrv_open(dinfo
->bdrv
, file
, bs_opts
, bdrv_flags
, drv
);
675 if (ret
== -EMEDIUMTYPE
) {
676 error_report("could not open disk image %s: not in %s format",
677 file
, drv
->format_name
);
679 error_report("could not open disk image %s: %s",
680 file
, strerror(-ret
));
685 if (bdrv_key_required(dinfo
->bdrv
))
695 bdrv_delete(dinfo
->bdrv
);
697 QTAILQ_REMOVE(&drives
, dinfo
, next
);
702 void do_commit(Monitor
*mon
, const QDict
*qdict
)
704 const char *device
= qdict_get_str(qdict
, "device");
705 BlockDriverState
*bs
;
708 if (!strcmp(device
, "all")) {
709 ret
= bdrv_commit_all();
711 bs
= bdrv_find(device
);
713 monitor_printf(mon
, "Device '%s' not found\n", device
);
716 ret
= bdrv_commit(bs
);
719 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
724 static void blockdev_do_action(int kind
, void *data
, Error
**errp
)
726 BlockdevAction action
;
727 BlockdevActionList list
;
731 list
.value
= &action
;
733 qmp_transaction(&list
, errp
);
736 void qmp_blockdev_snapshot_sync(const char *device
, const char *snapshot_file
,
737 bool has_format
, const char *format
,
738 bool has_mode
, enum NewImageMode mode
,
741 BlockdevSnapshot snapshot
= {
742 .device
= (char *) device
,
743 .snapshot_file
= (char *) snapshot_file
,
744 .has_format
= has_format
,
745 .format
= (char *) format
,
746 .has_mode
= has_mode
,
749 blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
, &snapshot
,
754 /* New and old BlockDriverState structs for group snapshots */
755 typedef struct BlkTransactionStates
{
756 BlockDriverState
*old_bs
;
757 BlockDriverState
*new_bs
;
758 QSIMPLEQ_ENTRY(BlkTransactionStates
) entry
;
759 } BlkTransactionStates
;
762 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
763 * then we do not pivot any of the devices in the group, and abandon the
766 void qmp_transaction(BlockdevActionList
*dev_list
, Error
**errp
)
769 BlockdevActionList
*dev_entry
= dev_list
;
770 BlkTransactionStates
*states
, *next
;
771 Error
*local_err
= NULL
;
773 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkTransactionStates
) snap_bdrv_states
;
774 QSIMPLEQ_INIT(&snap_bdrv_states
);
776 /* drain all i/o before any snapshots */
779 /* We don't do anything in this loop that commits us to the snapshot */
780 while (NULL
!= dev_entry
) {
781 BlockdevAction
*dev_info
= NULL
;
782 BlockDriver
*proto_drv
;
785 enum NewImageMode mode
;
786 const char *new_image_file
;
788 const char *format
= "qcow2";
790 dev_info
= dev_entry
->value
;
791 dev_entry
= dev_entry
->next
;
793 states
= g_malloc0(sizeof(BlkTransactionStates
));
794 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, states
, entry
);
796 switch (dev_info
->kind
) {
797 case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
798 device
= dev_info
->blockdev_snapshot_sync
->device
;
799 if (!dev_info
->blockdev_snapshot_sync
->has_mode
) {
800 dev_info
->blockdev_snapshot_sync
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
802 new_image_file
= dev_info
->blockdev_snapshot_sync
->snapshot_file
;
803 if (dev_info
->blockdev_snapshot_sync
->has_format
) {
804 format
= dev_info
->blockdev_snapshot_sync
->format
;
806 mode
= dev_info
->blockdev_snapshot_sync
->mode
;
812 drv
= bdrv_find_format(format
);
814 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
815 goto delete_and_fail
;
818 states
->old_bs
= bdrv_find(device
);
819 if (!states
->old_bs
) {
820 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
821 goto delete_and_fail
;
824 if (!bdrv_is_inserted(states
->old_bs
)) {
825 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
826 goto delete_and_fail
;
829 if (bdrv_in_use(states
->old_bs
)) {
830 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
831 goto delete_and_fail
;
834 if (!bdrv_is_read_only(states
->old_bs
)) {
835 if (bdrv_flush(states
->old_bs
)) {
836 error_set(errp
, QERR_IO_ERROR
);
837 goto delete_and_fail
;
841 flags
= states
->old_bs
->open_flags
;
843 proto_drv
= bdrv_find_protocol(new_image_file
);
845 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
846 goto delete_and_fail
;
849 /* create new image w/backing file */
850 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
851 bdrv_img_create(new_image_file
, format
,
852 states
->old_bs
->filename
,
853 states
->old_bs
->drv
->format_name
,
854 NULL
, -1, flags
, &local_err
, false);
855 if (error_is_set(&local_err
)) {
856 error_propagate(errp
, local_err
);
857 goto delete_and_fail
;
861 /* We will manually add the backing_hd field to the bs later */
862 states
->new_bs
= bdrv_new("");
863 /* TODO Inherit bs->options or only take explicit options with an
864 * extended QMP command? */
865 ret
= bdrv_open(states
->new_bs
, new_image_file
, NULL
,
866 flags
| BDRV_O_NO_BACKING
, drv
);
868 error_set(errp
, QERR_OPEN_FILE_FAILED
, new_image_file
);
869 goto delete_and_fail
;
874 /* Now we are going to do the actual pivot. Everything up to this point
875 * is reversible, but we are committed at this point */
876 QSIMPLEQ_FOREACH(states
, &snap_bdrv_states
, entry
) {
877 /* This removes our old bs from the bdrv_states, and adds the new bs */
878 bdrv_append(states
->new_bs
, states
->old_bs
);
879 /* We don't need (or want) to use the transactional
880 * bdrv_reopen_multiple() across all the entries at once, because we
881 * don't want to abort all of them if one of them fails the reopen */
882 bdrv_reopen(states
->new_bs
, states
->new_bs
->open_flags
& ~BDRV_O_RDWR
,
891 * failure, and it is all-or-none; abandon each new bs, and keep using
892 * the original bs for all images
894 QSIMPLEQ_FOREACH(states
, &snap_bdrv_states
, entry
) {
895 if (states
->new_bs
) {
896 bdrv_delete(states
->new_bs
);
900 QSIMPLEQ_FOREACH_SAFE(states
, &snap_bdrv_states
, entry
, next
) {
906 static void eject_device(BlockDriverState
*bs
, int force
, Error
**errp
)
908 if (bdrv_in_use(bs
)) {
909 error_set(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
912 if (!bdrv_dev_has_removable_media(bs
)) {
913 error_set(errp
, QERR_DEVICE_NOT_REMOVABLE
, bdrv_get_device_name(bs
));
917 if (bdrv_dev_is_medium_locked(bs
) && !bdrv_dev_is_tray_open(bs
)) {
918 bdrv_dev_eject_request(bs
, force
);
920 error_set(errp
, QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
928 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
930 BlockDriverState
*bs
;
932 bs
= bdrv_find(device
);
934 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
938 eject_device(bs
, force
, errp
);
941 void qmp_block_passwd(const char *device
, const char *password
, Error
**errp
)
943 BlockDriverState
*bs
;
946 bs
= bdrv_find(device
);
948 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
952 err
= bdrv_set_key(bs
, password
);
953 if (err
== -EINVAL
) {
954 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
956 } else if (err
< 0) {
957 error_set(errp
, QERR_INVALID_PASSWORD
);
962 static void qmp_bdrv_open_encrypted(BlockDriverState
*bs
, const char *filename
,
963 int bdrv_flags
, BlockDriver
*drv
,
964 const char *password
, Error
**errp
)
966 if (bdrv_open(bs
, filename
, NULL
, bdrv_flags
, drv
) < 0) {
967 error_set(errp
, QERR_OPEN_FILE_FAILED
, filename
);
971 if (bdrv_key_required(bs
)) {
973 if (bdrv_set_key(bs
, password
) < 0) {
974 error_set(errp
, QERR_INVALID_PASSWORD
);
977 error_set(errp
, QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
),
978 bdrv_get_encrypted_filename(bs
));
980 } else if (password
) {
981 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
985 void qmp_change_blockdev(const char *device
, const char *filename
,
986 bool has_format
, const char *format
, Error
**errp
)
988 BlockDriverState
*bs
;
989 BlockDriver
*drv
= NULL
;
993 bs
= bdrv_find(device
);
995 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1000 drv
= bdrv_find_whitelisted_format(format
);
1002 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1007 eject_device(bs
, 0, &err
);
1008 if (error_is_set(&err
)) {
1009 error_propagate(errp
, err
);
1013 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
1014 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
1016 qmp_bdrv_open_encrypted(bs
, filename
, bdrv_flags
, drv
, NULL
, errp
);
1019 /* throttling disk I/O limits */
1020 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
1021 int64_t bps_wr
, int64_t iops
, int64_t iops_rd
,
1022 int64_t iops_wr
, Error
**errp
)
1024 BlockIOLimit io_limits
;
1025 BlockDriverState
*bs
;
1027 bs
= bdrv_find(device
);
1029 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1033 io_limits
.bps
[BLOCK_IO_LIMIT_TOTAL
] = bps
;
1034 io_limits
.bps
[BLOCK_IO_LIMIT_READ
] = bps_rd
;
1035 io_limits
.bps
[BLOCK_IO_LIMIT_WRITE
] = bps_wr
;
1036 io_limits
.iops
[BLOCK_IO_LIMIT_TOTAL
]= iops
;
1037 io_limits
.iops
[BLOCK_IO_LIMIT_READ
] = iops_rd
;
1038 io_limits
.iops
[BLOCK_IO_LIMIT_WRITE
]= iops_wr
;
1040 if (!do_check_io_limits(&io_limits
, errp
)) {
1044 bs
->io_limits
= io_limits
;
1045 bs
->slice_time
= BLOCK_IO_SLICE_TIME
;
1047 if (!bs
->io_limits_enabled
&& bdrv_io_limits_enabled(bs
)) {
1048 bdrv_io_limits_enable(bs
);
1049 } else if (bs
->io_limits_enabled
&& !bdrv_io_limits_enabled(bs
)) {
1050 bdrv_io_limits_disable(bs
);
1052 if (bs
->block_timer
) {
1053 qemu_mod_timer(bs
->block_timer
, qemu_get_clock_ns(vm_clock
));
1058 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1060 const char *id
= qdict_get_str(qdict
, "id");
1061 BlockDriverState
*bs
;
1065 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1068 if (bdrv_in_use(bs
)) {
1069 qerror_report(QERR_DEVICE_IN_USE
, id
);
1073 /* quiesce block driver; prevent further io */
1078 /* if we have a device attached to this BlockDriverState
1079 * then we need to make the drive anonymous until the device
1080 * can be removed. If this is a drive with no device backing
1081 * then we can just get rid of the block driver state right here.
1083 if (bdrv_get_attached_dev(bs
)) {
1086 drive_uninit(drive_get_by_blockdev(bs
));
1092 void qmp_block_resize(const char *device
, int64_t size
, Error
**errp
)
1094 BlockDriverState
*bs
;
1096 bs
= bdrv_find(device
);
1098 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1103 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
1107 switch (bdrv_truncate(bs
, size
)) {
1111 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1114 error_set(errp
, QERR_UNSUPPORTED
);
1117 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1120 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1123 error_set(errp
, QERR_UNDEFINED_ERROR
);
1128 static void block_job_cb(void *opaque
, int ret
)
1130 BlockDriverState
*bs
= opaque
;
1133 trace_block_job_cb(bs
, bs
->job
, ret
);
1136 obj
= qobject_from_block_job(bs
->job
);
1138 QDict
*dict
= qobject_to_qdict(obj
);
1139 qdict_put(dict
, "error", qstring_from_str(strerror(-ret
)));
1142 if (block_job_is_cancelled(bs
->job
)) {
1143 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED
, obj
);
1145 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED
, obj
);
1147 qobject_decref(obj
);
1149 drive_put_ref_bh_schedule(drive_get_by_blockdev(bs
));
1152 void qmp_block_stream(const char *device
, bool has_base
,
1153 const char *base
, bool has_speed
, int64_t speed
,
1154 bool has_on_error
, BlockdevOnError on_error
,
1157 BlockDriverState
*bs
;
1158 BlockDriverState
*base_bs
= NULL
;
1159 Error
*local_err
= NULL
;
1161 if (!has_on_error
) {
1162 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1165 bs
= bdrv_find(device
);
1167 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1172 base_bs
= bdrv_find_backing_image(bs
, base
);
1173 if (base_bs
== NULL
) {
1174 error_set(errp
, QERR_BASE_NOT_FOUND
, base
);
1179 stream_start(bs
, base_bs
, base
, has_speed
? speed
: 0,
1180 on_error
, block_job_cb
, bs
, &local_err
);
1181 if (error_is_set(&local_err
)) {
1182 error_propagate(errp
, local_err
);
1186 /* Grab a reference so hotplug does not delete the BlockDriverState from
1189 drive_get_ref(drive_get_by_blockdev(bs
));
1191 trace_qmp_block_stream(bs
, bs
->job
);
1194 void qmp_block_commit(const char *device
,
1195 bool has_base
, const char *base
, const char *top
,
1196 bool has_speed
, int64_t speed
,
1199 BlockDriverState
*bs
;
1200 BlockDriverState
*base_bs
, *top_bs
;
1201 Error
*local_err
= NULL
;
1202 /* This will be part of the QMP command, if/when the
1203 * BlockdevOnError change for blkmirror makes it in
1205 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1207 /* drain all i/o before commits */
1210 bs
= bdrv_find(device
);
1212 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1216 /* default top_bs is the active layer */
1220 if (strcmp(bs
->filename
, top
) != 0) {
1221 top_bs
= bdrv_find_backing_image(bs
, top
);
1225 if (top_bs
== NULL
) {
1226 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
1230 if (has_base
&& base
) {
1231 base_bs
= bdrv_find_backing_image(top_bs
, base
);
1233 base_bs
= bdrv_find_base(top_bs
);
1236 if (base_bs
== NULL
) {
1237 error_set(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
1241 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
1243 if (local_err
!= NULL
) {
1244 error_propagate(errp
, local_err
);
1247 /* Grab a reference so hotplug does not delete the BlockDriverState from
1250 drive_get_ref(drive_get_by_blockdev(bs
));
1253 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1255 void qmp_drive_mirror(const char *device
, const char *target
,
1256 bool has_format
, const char *format
,
1257 enum MirrorSyncMode sync
,
1258 bool has_mode
, enum NewImageMode mode
,
1259 bool has_speed
, int64_t speed
,
1260 bool has_granularity
, uint32_t granularity
,
1261 bool has_buf_size
, int64_t buf_size
,
1262 bool has_on_source_error
, BlockdevOnError on_source_error
,
1263 bool has_on_target_error
, BlockdevOnError on_target_error
,
1266 BlockDriverState
*bs
;
1267 BlockDriverState
*source
, *target_bs
;
1268 BlockDriver
*proto_drv
;
1269 BlockDriver
*drv
= NULL
;
1270 Error
*local_err
= NULL
;
1278 if (!has_on_source_error
) {
1279 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
1281 if (!has_on_target_error
) {
1282 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
1285 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1287 if (!has_granularity
) {
1290 if (!has_buf_size
) {
1291 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
1294 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
1295 error_set(errp
, QERR_INVALID_PARAMETER
, device
);
1298 if (granularity
& (granularity
- 1)) {
1299 error_set(errp
, QERR_INVALID_PARAMETER
, device
);
1303 bs
= bdrv_find(device
);
1305 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1309 if (!bdrv_is_inserted(bs
)) {
1310 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1315 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
1318 drv
= bdrv_find_format(format
);
1320 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1325 if (bdrv_in_use(bs
)) {
1326 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1330 flags
= bs
->open_flags
| BDRV_O_RDWR
;
1331 source
= bs
->backing_hd
;
1332 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
1333 sync
= MIRROR_SYNC_MODE_FULL
;
1336 proto_drv
= bdrv_find_protocol(target
);
1338 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1342 bdrv_get_geometry(bs
, &size
);
1344 if (sync
== MIRROR_SYNC_MODE_FULL
&& mode
!= NEW_IMAGE_MODE_EXISTING
) {
1345 /* create new image w/o backing file */
1346 assert(format
&& drv
);
1347 bdrv_img_create(target
, format
,
1348 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
1351 case NEW_IMAGE_MODE_EXISTING
:
1354 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
1355 /* create new image with backing file */
1356 bdrv_img_create(target
, format
,
1358 source
->drv
->format_name
,
1359 NULL
, size
, flags
, &local_err
, false);
1366 if (error_is_set(&local_err
)) {
1367 error_propagate(errp
, local_err
);
1371 /* Mirroring takes care of copy-on-write using the source's backing
1374 target_bs
= bdrv_new("");
1375 ret
= bdrv_open(target_bs
, target
, NULL
, flags
| BDRV_O_NO_BACKING
, drv
);
1378 bdrv_delete(target_bs
);
1379 error_set(errp
, QERR_OPEN_FILE_FAILED
, target
);
1383 mirror_start(bs
, target_bs
, speed
, granularity
, buf_size
, sync
,
1384 on_source_error
, on_target_error
,
1385 block_job_cb
, bs
, &local_err
);
1386 if (local_err
!= NULL
) {
1387 bdrv_delete(target_bs
);
1388 error_propagate(errp
, local_err
);
1392 /* Grab a reference so hotplug does not delete the BlockDriverState from
1395 drive_get_ref(drive_get_by_blockdev(bs
));
1398 static BlockJob
*find_block_job(const char *device
)
1400 BlockDriverState
*bs
;
1402 bs
= bdrv_find(device
);
1403 if (!bs
|| !bs
->job
) {
1409 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
1411 BlockJob
*job
= find_block_job(device
);
1414 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
1418 block_job_set_speed(job
, speed
, errp
);
1421 void qmp_block_job_cancel(const char *device
,
1422 bool has_force
, bool force
, Error
**errp
)
1424 BlockJob
*job
= find_block_job(device
);
1431 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
1434 if (job
->paused
&& !force
) {
1435 error_set(errp
, QERR_BLOCK_JOB_PAUSED
, device
);
1439 trace_qmp_block_job_cancel(job
);
1440 block_job_cancel(job
);
1443 void qmp_block_job_pause(const char *device
, Error
**errp
)
1445 BlockJob
*job
= find_block_job(device
);
1448 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
1452 trace_qmp_block_job_pause(job
);
1453 block_job_pause(job
);
1456 void qmp_block_job_resume(const char *device
, Error
**errp
)
1458 BlockJob
*job
= find_block_job(device
);
1461 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
1465 trace_qmp_block_job_resume(job
);
1466 block_job_resume(job
);
1469 void qmp_block_job_complete(const char *device
, Error
**errp
)
1471 BlockJob
*job
= find_block_job(device
);
1474 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
1478 trace_qmp_block_job_complete(job
);
1479 block_job_complete(job
, errp
);
1482 static void do_qmp_query_block_jobs_one(void *opaque
, BlockDriverState
*bs
)
1484 BlockJobInfoList
**prev
= opaque
;
1485 BlockJob
*job
= bs
->job
;
1488 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
1489 elem
->value
= block_job_query(bs
->job
);
1490 (*prev
)->next
= elem
;
1495 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
1497 /* Dummy is a fake list element for holding the head pointer */
1498 BlockJobInfoList dummy
= {};
1499 BlockJobInfoList
*prev
= &dummy
;
1500 bdrv_iterate(do_qmp_query_block_jobs_one
, &prev
);
1504 QemuOptsList qemu_common_drive_opts
= {
1506 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
1510 .type
= QEMU_OPT_NUMBER
,
1511 .help
= "bus number",
1514 .type
= QEMU_OPT_NUMBER
,
1515 .help
= "unit number (i.e. lun for scsi)",
1518 .type
= QEMU_OPT_STRING
,
1519 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1522 .type
= QEMU_OPT_NUMBER
,
1523 .help
= "index number",
1526 .type
= QEMU_OPT_NUMBER
,
1527 .help
= "number of cylinders (ide disk geometry)",
1530 .type
= QEMU_OPT_NUMBER
,
1531 .help
= "number of heads (ide disk geometry)",
1534 .type
= QEMU_OPT_NUMBER
,
1535 .help
= "number of sectors (ide disk geometry)",
1538 .type
= QEMU_OPT_STRING
,
1539 .help
= "chs translation (auto, lba. none)",
1542 .type
= QEMU_OPT_STRING
,
1543 .help
= "media type (disk, cdrom)",
1546 .type
= QEMU_OPT_BOOL
,
1547 .help
= "enable/disable snapshot mode",
1550 .type
= QEMU_OPT_STRING
,
1551 .help
= "disk image",
1554 .type
= QEMU_OPT_STRING
,
1555 .help
= "discard operation (ignore/off, unmap/on)",
1558 .type
= QEMU_OPT_STRING
,
1559 .help
= "host cache usage (none, writeback, writethrough, "
1560 "directsync, unsafe)",
1563 .type
= QEMU_OPT_STRING
,
1564 .help
= "host AIO implementation (threads, native)",
1567 .type
= QEMU_OPT_STRING
,
1568 .help
= "disk format (raw, qcow2, ...)",
1571 .type
= QEMU_OPT_STRING
,
1572 .help
= "disk serial number",
1575 .type
= QEMU_OPT_STRING
,
1576 .help
= "read error action",
1579 .type
= QEMU_OPT_STRING
,
1580 .help
= "write error action",
1583 .type
= QEMU_OPT_STRING
,
1584 .help
= "pci address (virtio only)",
1587 .type
= QEMU_OPT_BOOL
,
1588 .help
= "open drive file as read-only",
1591 .type
= QEMU_OPT_NUMBER
,
1592 .help
= "limit total I/O operations per second",
1595 .type
= QEMU_OPT_NUMBER
,
1596 .help
= "limit read operations per second",
1599 .type
= QEMU_OPT_NUMBER
,
1600 .help
= "limit write operations per second",
1603 .type
= QEMU_OPT_NUMBER
,
1604 .help
= "limit total bytes per second",
1607 .type
= QEMU_OPT_NUMBER
,
1608 .help
= "limit read bytes per second",
1611 .type
= QEMU_OPT_NUMBER
,
1612 .help
= "limit write bytes per second",
1614 .name
= "copy-on-read",
1615 .type
= QEMU_OPT_BOOL
,
1616 .help
= "copy read data from backing file into image file",
1619 .type
= QEMU_OPT_BOOL
,
1620 .help
= "(deprecated, ignored)",
1622 { /* end of list */ }
1626 QemuOptsList qemu_drive_opts
= {
1628 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
1631 * no elements => accept any params
1632 * validation will happen later
1634 { /* end of list */ }