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.
14 #include "qemu-option.h"
15 #include "qemu-config.h"
17 #include "block_int.h"
18 #include "qmp-commands.h"
20 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
22 static const char *const if_name[IF_COUNT] = {
26 [IF_FLOPPY] = "floppy",
27 [IF_PFLASH] = "pflash",
30 [IF_VIRTIO] = "virtio",
34 static const int if_max_devs[IF_COUNT] = {
36 * Do not change these numbers! They govern how drive option
37 * index maps to unit and bus. That mapping is ABI.
39 * All controllers used to imlement if=T drives need to support
40 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
41 * Otherwise, some index values map to "impossible" bus, unit
44 * For instance, if you change [IF_SCSI] to 255, -drive
45 * if=scsi,index=12 no longer means bus=1,unit=5, but
46 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
47 * the drive can't be set up. Regression.
54 * We automatically delete the drive when a device using it gets
55 * unplugged. Questionable feature, but we can't just drop it.
56 * Device models call blockdev_mark_auto_del() to schedule the
57 * automatic deletion, and generic qdev code calls blockdev_auto_del()
58 * when deletion is actually safe.
60 void blockdev_mark_auto_del(BlockDriverState *bs)
62 DriveInfo *dinfo = drive_get_by_blockdev(bs);
69 void blockdev_auto_del(BlockDriverState *bs)
71 DriveInfo *dinfo = drive_get_by_blockdev(bs);
73 if (dinfo && dinfo->auto_del) {
78 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
80 int max_devs = if_max_devs[type];
81 return max_devs ? index / max_devs : 0;
84 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
86 int max_devs = if_max_devs[type];
87 return max_devs ? index % max_devs : index;
90 QemuOpts *drive_def(const char *optstr)
92 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
95 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
101 opts = drive_def(optstr);
105 if (type != IF_DEFAULT) {
106 qemu_opt_set(opts, "if", if_name[type]);
109 snprintf(buf, sizeof(buf), "%d", index);
110 qemu_opt_set(opts, "index", buf);
113 qemu_opt_set(opts, "file", file);
117 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
121 /* seek interface, bus and unit */
123 QTAILQ_FOREACH(dinfo, &drives, next) {
124 if (dinfo->type == type &&
133 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
135 return drive_get(type,
136 drive_index_to_bus_id(type, index),
137 drive_index_to_unit_id(type, index));
140 int drive_get_max_bus(BlockInterfaceType type)
146 QTAILQ_FOREACH(dinfo, &drives, next) {
147 if(dinfo->type == type &&
148 dinfo->bus > max_bus)
149 max_bus = dinfo->bus;
154 /* Get a block device. This should only be used for single-drive devices
155 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
157 DriveInfo *drive_get_next(BlockInterfaceType type)
159 static int next_block_unit[IF_COUNT];
161 return drive_get(type, 0, next_block_unit[type]++);
164 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
168 QTAILQ_FOREACH(dinfo, &drives, next) {
169 if (dinfo->bdrv == bs) {
176 static void bdrv_format_print(void *opaque, const char *name)
178 error_printf(" %s", name);
181 static void drive_uninit(DriveInfo *dinfo)
183 qemu_opts_del(dinfo->opts);
184 bdrv_delete(dinfo->bdrv);
186 QTAILQ_REMOVE(&drives, dinfo, next);
190 void drive_put_ref(DriveInfo *dinfo)
192 assert(dinfo->refcount);
193 if (--dinfo->refcount == 0) {
198 void drive_get_ref(DriveInfo *dinfo)
203 static int parse_block_error_action(const char *buf, int is_read)
205 if (!strcmp(buf, "ignore")) {
206 return BLOCK_ERR_IGNORE;
207 } else if (!is_read && !strcmp(buf, "enospc")) {
208 return BLOCK_ERR_STOP_ENOSPC;
209 } else if (!strcmp(buf, "stop")) {
210 return BLOCK_ERR_STOP_ANY;
211 } else if (!strcmp(buf, "report")) {
212 return BLOCK_ERR_REPORT;
214 error_report("'%s' invalid %s error action",
215 buf, is_read ? "read" : "write");
220 static bool do_check_io_limits(BlockIOLimit *io_limits)
227 bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
228 && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
229 || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
230 iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
231 && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
232 || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
233 if (bps_flag || iops_flag) {
240 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
243 const char *file = NULL;
246 const char *mediastr = "";
247 BlockInterfaceType type;
248 enum { MEDIA_DISK, MEDIA_CDROM } media;
250 int cyls, heads, secs, translation;
251 BlockDriver *drv = NULL;
256 int on_read_error, on_write_error;
259 BlockIOLimit io_limits;
264 translation = BIOS_ATA_TRANSLATION_AUTO;
267 /* extract parameters */
268 bus_id = qemu_opt_get_number(opts, "bus", 0);
269 unit_id = qemu_opt_get_number(opts, "unit", -1);
270 index = qemu_opt_get_number(opts, "index", -1);
272 cyls = qemu_opt_get_number(opts, "cyls", 0);
273 heads = qemu_opt_get_number(opts, "heads", 0);
274 secs = qemu_opt_get_number(opts, "secs", 0);
276 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
277 ro = qemu_opt_get_bool(opts, "readonly", 0);
278 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
280 file = qemu_opt_get(opts, "file");
281 serial = qemu_opt_get(opts, "serial");
283 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
284 pstrcpy(devname, sizeof(devname), buf);
285 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
287 if (type == IF_COUNT) {
288 error_report("unsupported bus type '%s'", buf);
292 type = default_to_scsi ? IF_SCSI : IF_IDE;
293 pstrcpy(devname, sizeof(devname), if_name[type]);
296 max_devs = if_max_devs[type];
298 if (cyls || heads || secs) {
299 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
300 error_report("invalid physical cyls number");
303 if (heads < 1 || (type == IF_IDE && heads > 16)) {
304 error_report("invalid physical heads number");
307 if (secs < 1 || (type == IF_IDE && secs > 63)) {
308 error_report("invalid physical secs number");
313 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
315 error_report("'%s' trans must be used with cyls, heads and secs",
319 if (!strcmp(buf, "none"))
320 translation = BIOS_ATA_TRANSLATION_NONE;
321 else if (!strcmp(buf, "lba"))
322 translation = BIOS_ATA_TRANSLATION_LBA;
323 else if (!strcmp(buf, "auto"))
324 translation = BIOS_ATA_TRANSLATION_AUTO;
326 error_report("'%s' invalid translation type", buf);
331 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
332 if (!strcmp(buf, "disk")) {
334 } else if (!strcmp(buf, "cdrom")) {
335 if (cyls || secs || heads) {
336 error_report("CHS can't be set with media=%s", buf);
341 error_report("'%s' invalid media", buf);
346 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
347 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
348 error_report("invalid cache option");
353 #ifdef CONFIG_LINUX_AIO
354 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
355 if (!strcmp(buf, "native")) {
356 bdrv_flags |= BDRV_O_NATIVE_AIO;
357 } else if (!strcmp(buf, "threads")) {
358 /* this is the default */
360 error_report("invalid aio option");
366 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
367 if (strcmp(buf, "?") == 0) {
368 error_printf("Supported formats:");
369 bdrv_iterate_format(bdrv_format_print, NULL);
373 drv = bdrv_find_whitelisted_format(buf);
375 error_report("'%s' invalid format", buf);
380 /* disk I/O throttling */
381 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
382 qemu_opt_get_number(opts, "bps", 0);
383 io_limits.bps[BLOCK_IO_LIMIT_READ] =
384 qemu_opt_get_number(opts, "bps_rd", 0);
385 io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
386 qemu_opt_get_number(opts, "bps_wr", 0);
387 io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
388 qemu_opt_get_number(opts, "iops", 0);
389 io_limits.iops[BLOCK_IO_LIMIT_READ] =
390 qemu_opt_get_number(opts, "iops_rd", 0);
391 io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
392 qemu_opt_get_number(opts, "iops_wr", 0);
394 if (!do_check_io_limits(&io_limits)) {
395 error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
396 "cannot be used at the same time");
400 on_write_error = BLOCK_ERR_STOP_ENOSPC;
401 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
402 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
403 error_report("werror is not supported by this bus type");
407 on_write_error = parse_block_error_action(buf, 0);
408 if (on_write_error < 0) {
413 on_read_error = BLOCK_ERR_REPORT;
414 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
415 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
416 error_report("rerror is not supported by this bus type");
420 on_read_error = parse_block_error_action(buf, 1);
421 if (on_read_error < 0) {
426 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
427 if (type != IF_VIRTIO) {
428 error_report("addr is not supported by this bus type");
433 /* compute bus and unit according index */
436 if (bus_id != 0 || unit_id != -1) {
437 error_report("index cannot be used with bus and unit");
440 bus_id = drive_index_to_bus_id(type, index);
441 unit_id = drive_index_to_unit_id(type, index);
444 /* if user doesn't specify a unit_id,
445 * try to find the first free
450 while (drive_get(type, bus_id, unit_id) != NULL) {
452 if (max_devs && unit_id >= max_devs) {
461 if (max_devs && unit_id >= max_devs) {
462 error_report("unit %d too big (max is %d)",
463 unit_id, max_devs - 1);
468 * catch multiple definitions
471 if (drive_get(type, bus_id, unit_id) != NULL) {
472 error_report("drive with bus=%d, unit=%d (index=%d) exists",
473 bus_id, unit_id, index);
479 dinfo = g_malloc0(sizeof(*dinfo));
480 if ((buf = qemu_opts_id(opts)) != NULL) {
481 dinfo->id = g_strdup(buf);
483 /* no id supplied -> create one */
484 dinfo->id = g_malloc0(32);
485 if (type == IF_IDE || type == IF_SCSI)
486 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
488 snprintf(dinfo->id, 32, "%s%i%s%i",
489 devname, bus_id, mediastr, unit_id);
491 snprintf(dinfo->id, 32, "%s%s%i",
492 devname, mediastr, unit_id);
494 dinfo->bdrv = bdrv_new(dinfo->id);
495 dinfo->devaddr = devaddr;
498 dinfo->unit = unit_id;
502 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
503 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
505 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
507 /* disk I/O throttling */
508 bdrv_set_io_limits(dinfo->bdrv, &io_limits);
518 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
519 bdrv_set_translation_hint(dinfo->bdrv, translation);
533 /* add virtio block device */
534 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
535 qemu_opt_set(opts, "driver", "virtio-blk");
536 qemu_opt_set(opts, "drive", dinfo->id);
538 qemu_opt_set(opts, "addr", devaddr);
543 if (!file || !*file) {
547 /* always use cache=unsafe with snapshot */
548 bdrv_flags &= ~BDRV_O_CACHE_MASK;
549 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
553 bdrv_flags |= BDRV_O_COPY_ON_READ;
556 if (media == MEDIA_CDROM) {
557 /* CDROM is fine for any interface, don't check. */
559 } else if (ro == 1) {
560 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
561 error_report("readonly not supported by this bus type");
566 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
568 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
570 error_report("could not open disk image %s: %s",
571 file, strerror(-ret));
575 if (bdrv_key_required(dinfo->bdrv))
580 bdrv_delete(dinfo->bdrv);
582 QTAILQ_REMOVE(&drives, dinfo, next);
587 void do_commit(Monitor *mon, const QDict *qdict)
589 const char *device = qdict_get_str(qdict, "device");
590 BlockDriverState *bs;
592 if (!strcmp(device, "all")) {
595 bs = bdrv_find(device);
597 qerror_report(QERR_DEVICE_NOT_FOUND, device);
604 int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
606 const char *device = qdict_get_str(qdict, "device");
607 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
608 const char *format = qdict_get_try_str(qdict, "format");
609 BlockDriverState *bs;
610 BlockDriver *drv, *old_drv, *proto_drv;
613 char old_filename[1024];
616 qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
621 bs = bdrv_find(device);
623 qerror_report(QERR_DEVICE_NOT_FOUND, device);
628 pstrcpy(old_filename, sizeof(old_filename), bs->filename);
631 flags = bs->open_flags;
637 drv = bdrv_find_format(format);
639 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
644 proto_drv = bdrv_find_protocol(filename);
646 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
651 ret = bdrv_img_create(filename, format, bs->filename,
652 bs->drv->format_name, NULL, -1, flags);
661 ret = bdrv_open(bs, filename, flags, drv);
663 * If reopening the image file we just created fails, fall back
664 * and try to re-open the original image. If that fails too, we
665 * are in serious trouble.
668 ret = bdrv_open(bs, old_filename, flags, old_drv);
670 qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
672 qerror_report(QERR_OPEN_FILE_FAILED, filename);
683 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
685 if (!bdrv_dev_has_removable_media(bs)) {
686 qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
689 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
690 bdrv_dev_eject_request(bs, force);
692 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
700 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
702 BlockDriverState *bs;
703 int force = qdict_get_try_bool(qdict, "force", 0);
704 const char *filename = qdict_get_str(qdict, "device");
706 bs = bdrv_find(filename);
708 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
711 return eject_device(mon, bs, force);
714 void qmp_block_passwd(const char *device, const char *password, Error **errp)
716 BlockDriverState *bs;
719 bs = bdrv_find(device);
721 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
725 err = bdrv_set_key(bs, password);
726 if (err == -EINVAL) {
727 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
729 } else if (err < 0) {
730 error_set(errp, QERR_INVALID_PASSWORD);
735 int do_change_block(Monitor *mon, const char *device,
736 const char *filename, const char *fmt)
738 BlockDriverState *bs;
739 BlockDriver *drv = NULL;
742 bs = bdrv_find(device);
744 qerror_report(QERR_DEVICE_NOT_FOUND, device);
748 drv = bdrv_find_whitelisted_format(fmt);
750 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
754 if (eject_device(mon, bs, 0) < 0) {
757 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
758 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
759 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
760 qerror_report(QERR_OPEN_FILE_FAILED, filename);
763 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
766 /* throttling disk I/O limits */
767 int do_block_set_io_throttle(Monitor *mon,
768 const QDict *qdict, QObject **ret_data)
770 BlockIOLimit io_limits;
771 const char *devname = qdict_get_str(qdict, "device");
772 BlockDriverState *bs;
774 io_limits.bps[BLOCK_IO_LIMIT_TOTAL]
775 = qdict_get_try_int(qdict, "bps", -1);
776 io_limits.bps[BLOCK_IO_LIMIT_READ]
777 = qdict_get_try_int(qdict, "bps_rd", -1);
778 io_limits.bps[BLOCK_IO_LIMIT_WRITE]
779 = qdict_get_try_int(qdict, "bps_wr", -1);
780 io_limits.iops[BLOCK_IO_LIMIT_TOTAL]
781 = qdict_get_try_int(qdict, "iops", -1);
782 io_limits.iops[BLOCK_IO_LIMIT_READ]
783 = qdict_get_try_int(qdict, "iops_rd", -1);
784 io_limits.iops[BLOCK_IO_LIMIT_WRITE]
785 = qdict_get_try_int(qdict, "iops_wr", -1);
787 bs = bdrv_find(devname);
789 qerror_report(QERR_DEVICE_NOT_FOUND, devname);
793 if ((io_limits.bps[BLOCK_IO_LIMIT_TOTAL] == -1)
794 || (io_limits.bps[BLOCK_IO_LIMIT_READ] == -1)
795 || (io_limits.bps[BLOCK_IO_LIMIT_WRITE] == -1)
796 || (io_limits.iops[BLOCK_IO_LIMIT_TOTAL] == -1)
797 || (io_limits.iops[BLOCK_IO_LIMIT_READ] == -1)
798 || (io_limits.iops[BLOCK_IO_LIMIT_WRITE] == -1)) {
799 qerror_report(QERR_MISSING_PARAMETER,
800 "bps/bps_rd/bps_wr/iops/iops_rd/iops_wr");
804 if (!do_check_io_limits(&io_limits)) {
805 qerror_report(QERR_INVALID_PARAMETER_COMBINATION);
809 bs->io_limits = io_limits;
810 bs->slice_time = BLOCK_IO_SLICE_TIME;
812 if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
813 bdrv_io_limits_enable(bs);
814 } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
815 bdrv_io_limits_disable(bs);
817 if (bs->block_timer) {
818 qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
825 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
827 const char *id = qdict_get_str(qdict, "id");
828 BlockDriverState *bs;
832 qerror_report(QERR_DEVICE_NOT_FOUND, id);
835 if (bdrv_in_use(bs)) {
836 qerror_report(QERR_DEVICE_IN_USE, id);
840 /* quiesce block driver; prevent further io */
845 /* if we have a device attached to this BlockDriverState
846 * then we need to make the drive anonymous until the device
847 * can be removed. If this is a drive with no device backing
848 * then we can just get rid of the block driver state right here.
850 if (bdrv_get_attached_dev(bs)) {
853 drive_uninit(drive_get_by_blockdev(bs));
860 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
861 * existing QERR_ macro mess is cleaned up. A good example for better
862 * error reports can be found in the qemu-img resize code.
864 int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
866 const char *device = qdict_get_str(qdict, "device");
867 int64_t size = qdict_get_int(qdict, "size");
868 BlockDriverState *bs;
870 bs = bdrv_find(device);
872 qerror_report(QERR_DEVICE_NOT_FOUND, device);
877 qerror_report(QERR_UNDEFINED_ERROR);
881 if (bdrv_truncate(bs, size)) {
882 qerror_report(QERR_UNDEFINED_ERROR);