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"
19 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
21 static const char *const if_name[IF_COUNT] = {
25 [IF_FLOPPY] = "floppy",
26 [IF_PFLASH] = "pflash",
29 [IF_VIRTIO] = "virtio",
33 static const int if_max_devs[IF_COUNT] = {
35 * Do not change these numbers! They govern how drive option
36 * index maps to unit and bus. That mapping is ABI.
38 * All controllers used to imlement if=T drives need to support
39 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
40 * Otherwise, some index values map to "impossible" bus, unit
43 * For instance, if you change [IF_SCSI] to 255, -drive
44 * if=scsi,index=12 no longer means bus=1,unit=5, but
45 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
46 * the drive can't be set up. Regression.
53 * We automatically delete the drive when a device using it gets
54 * unplugged. Questionable feature, but we can't just drop it.
55 * Device models call blockdev_mark_auto_del() to schedule the
56 * automatic deletion, and generic qdev code calls blockdev_auto_del()
57 * when deletion is actually safe.
59 void blockdev_mark_auto_del(BlockDriverState *bs)
61 DriveInfo *dinfo = drive_get_by_blockdev(bs);
68 void blockdev_auto_del(BlockDriverState *bs)
70 DriveInfo *dinfo = drive_get_by_blockdev(bs);
72 if (dinfo && dinfo->auto_del) {
77 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
79 int max_devs = if_max_devs[type];
80 return max_devs ? index / max_devs : 0;
83 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
85 int max_devs = if_max_devs[type];
86 return max_devs ? index % max_devs : index;
89 QemuOpts *drive_def(const char *optstr)
91 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
94 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
100 opts = drive_def(optstr);
104 if (type != IF_DEFAULT) {
105 qemu_opt_set(opts, "if", if_name[type]);
108 snprintf(buf, sizeof(buf), "%d", index);
109 qemu_opt_set(opts, "index", buf);
112 qemu_opt_set(opts, "file", file);
116 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
120 /* seek interface, bus and unit */
122 QTAILQ_FOREACH(dinfo, &drives, next) {
123 if (dinfo->type == type &&
132 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
134 return drive_get(type,
135 drive_index_to_bus_id(type, index),
136 drive_index_to_unit_id(type, index));
139 int drive_get_max_bus(BlockInterfaceType type)
145 QTAILQ_FOREACH(dinfo, &drives, next) {
146 if(dinfo->type == type &&
147 dinfo->bus > max_bus)
148 max_bus = dinfo->bus;
153 /* Get a block device. This should only be used for single-drive devices
154 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
156 DriveInfo *drive_get_next(BlockInterfaceType type)
158 static int next_block_unit[IF_COUNT];
160 return drive_get(type, 0, next_block_unit[type]++);
163 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
167 QTAILQ_FOREACH(dinfo, &drives, next) {
168 if (dinfo->bdrv == bs) {
175 static void bdrv_format_print(void *opaque, const char *name)
177 error_printf(" %s", name);
180 static void drive_uninit(DriveInfo *dinfo)
182 qemu_opts_del(dinfo->opts);
183 bdrv_delete(dinfo->bdrv);
185 QTAILQ_REMOVE(&drives, dinfo, next);
189 void drive_put_ref(DriveInfo *dinfo)
191 assert(dinfo->refcount);
192 if (--dinfo->refcount == 0) {
197 void drive_get_ref(DriveInfo *dinfo)
202 static int parse_block_error_action(const char *buf, int is_read)
204 if (!strcmp(buf, "ignore")) {
205 return BLOCK_ERR_IGNORE;
206 } else if (!is_read && !strcmp(buf, "enospc")) {
207 return BLOCK_ERR_STOP_ENOSPC;
208 } else if (!strcmp(buf, "stop")) {
209 return BLOCK_ERR_STOP_ANY;
210 } else if (!strcmp(buf, "report")) {
211 return BLOCK_ERR_REPORT;
213 error_report("'%s' invalid %s error action",
214 buf, is_read ? "read" : "write");
219 static bool do_check_io_limits(BlockIOLimit *io_limits)
226 bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
227 && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
228 || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
229 iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
230 && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
231 || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
232 if (bps_flag || iops_flag) {
239 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
242 const char *file = NULL;
245 const char *mediastr = "";
246 BlockInterfaceType type;
247 enum { MEDIA_DISK, MEDIA_CDROM } media;
249 int cyls, heads, secs, translation;
250 BlockDriver *drv = NULL;
255 int on_read_error, on_write_error;
258 BlockIOLimit io_limits;
262 translation = BIOS_ATA_TRANSLATION_AUTO;
265 /* extract parameters */
266 bus_id = qemu_opt_get_number(opts, "bus", 0);
267 unit_id = qemu_opt_get_number(opts, "unit", -1);
268 index = qemu_opt_get_number(opts, "index", -1);
270 cyls = qemu_opt_get_number(opts, "cyls", 0);
271 heads = qemu_opt_get_number(opts, "heads", 0);
272 secs = qemu_opt_get_number(opts, "secs", 0);
274 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
275 ro = qemu_opt_get_bool(opts, "readonly", 0);
277 file = qemu_opt_get(opts, "file");
278 serial = qemu_opt_get(opts, "serial");
280 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
281 pstrcpy(devname, sizeof(devname), buf);
282 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
284 if (type == IF_COUNT) {
285 error_report("unsupported bus type '%s'", buf);
289 type = default_to_scsi ? IF_SCSI : IF_IDE;
290 pstrcpy(devname, sizeof(devname), if_name[type]);
293 max_devs = if_max_devs[type];
295 if (cyls || heads || secs) {
296 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
297 error_report("invalid physical cyls number");
300 if (heads < 1 || (type == IF_IDE && heads > 16)) {
301 error_report("invalid physical heads number");
304 if (secs < 1 || (type == IF_IDE && secs > 63)) {
305 error_report("invalid physical secs number");
310 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
312 error_report("'%s' trans must be used with cyls, heads and secs",
316 if (!strcmp(buf, "none"))
317 translation = BIOS_ATA_TRANSLATION_NONE;
318 else if (!strcmp(buf, "lba"))
319 translation = BIOS_ATA_TRANSLATION_LBA;
320 else if (!strcmp(buf, "auto"))
321 translation = BIOS_ATA_TRANSLATION_AUTO;
323 error_report("'%s' invalid translation type", buf);
328 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
329 if (!strcmp(buf, "disk")) {
331 } else if (!strcmp(buf, "cdrom")) {
332 if (cyls || secs || heads) {
333 error_report("CHS can't be set with media=%s", buf);
338 error_report("'%s' invalid media", buf);
343 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
344 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
345 error_report("invalid cache option");
350 #ifdef CONFIG_LINUX_AIO
351 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
352 if (!strcmp(buf, "native")) {
353 bdrv_flags |= BDRV_O_NATIVE_AIO;
354 } else if (!strcmp(buf, "threads")) {
355 /* this is the default */
357 error_report("invalid aio option");
363 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
364 if (strcmp(buf, "?") == 0) {
365 error_printf("Supported formats:");
366 bdrv_iterate_format(bdrv_format_print, NULL);
370 drv = bdrv_find_whitelisted_format(buf);
372 error_report("'%s' invalid format", buf);
377 /* disk I/O throttling */
378 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
379 qemu_opt_get_number(opts, "bps", 0);
380 io_limits.bps[BLOCK_IO_LIMIT_READ] =
381 qemu_opt_get_number(opts, "bps_rd", 0);
382 io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
383 qemu_opt_get_number(opts, "bps_wr", 0);
384 io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
385 qemu_opt_get_number(opts, "iops", 0);
386 io_limits.iops[BLOCK_IO_LIMIT_READ] =
387 qemu_opt_get_number(opts, "iops_rd", 0);
388 io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
389 qemu_opt_get_number(opts, "iops_wr", 0);
391 if (!do_check_io_limits(&io_limits)) {
392 error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
393 "cannot be used at the same time");
397 on_write_error = BLOCK_ERR_STOP_ENOSPC;
398 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
399 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
400 error_report("werror is not supported by this bus type");
404 on_write_error = parse_block_error_action(buf, 0);
405 if (on_write_error < 0) {
410 on_read_error = BLOCK_ERR_REPORT;
411 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
412 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
413 error_report("rerror is not supported by this bus type");
417 on_read_error = parse_block_error_action(buf, 1);
418 if (on_read_error < 0) {
423 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
424 if (type != IF_VIRTIO) {
425 error_report("addr is not supported by this bus type");
430 /* compute bus and unit according index */
433 if (bus_id != 0 || unit_id != -1) {
434 error_report("index cannot be used with bus and unit");
437 bus_id = drive_index_to_bus_id(type, index);
438 unit_id = drive_index_to_unit_id(type, index);
441 /* if user doesn't specify a unit_id,
442 * try to find the first free
447 while (drive_get(type, bus_id, unit_id) != NULL) {
449 if (max_devs && unit_id >= max_devs) {
458 if (max_devs && unit_id >= max_devs) {
459 error_report("unit %d too big (max is %d)",
460 unit_id, max_devs - 1);
465 * catch multiple definitions
468 if (drive_get(type, bus_id, unit_id) != NULL) {
469 error_report("drive with bus=%d, unit=%d (index=%d) exists",
470 bus_id, unit_id, index);
476 dinfo = g_malloc0(sizeof(*dinfo));
477 if ((buf = qemu_opts_id(opts)) != NULL) {
478 dinfo->id = g_strdup(buf);
480 /* no id supplied -> create one */
481 dinfo->id = g_malloc0(32);
482 if (type == IF_IDE || type == IF_SCSI)
483 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
485 snprintf(dinfo->id, 32, "%s%i%s%i",
486 devname, bus_id, mediastr, unit_id);
488 snprintf(dinfo->id, 32, "%s%s%i",
489 devname, mediastr, unit_id);
491 dinfo->bdrv = bdrv_new(dinfo->id);
492 dinfo->devaddr = devaddr;
495 dinfo->unit = unit_id;
499 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
500 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
502 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
504 /* disk I/O throttling */
505 bdrv_set_io_limits(dinfo->bdrv, &io_limits);
515 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
516 bdrv_set_translation_hint(dinfo->bdrv, translation);
530 /* add virtio block device */
531 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
532 qemu_opt_set(opts, "driver", "virtio-blk");
533 qemu_opt_set(opts, "drive", dinfo->id);
535 qemu_opt_set(opts, "addr", devaddr);
540 if (!file || !*file) {
544 /* always use cache=unsafe with snapshot */
545 bdrv_flags &= ~BDRV_O_CACHE_MASK;
546 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
549 if (media == MEDIA_CDROM) {
550 /* CDROM is fine for any interface, don't check. */
552 } else if (ro == 1) {
553 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
554 error_report("readonly not supported by this bus type");
559 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
561 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
563 error_report("could not open disk image %s: %s",
564 file, strerror(-ret));
568 if (bdrv_key_required(dinfo->bdrv))
573 bdrv_delete(dinfo->bdrv);
575 QTAILQ_REMOVE(&drives, dinfo, next);
580 void do_commit(Monitor *mon, const QDict *qdict)
582 const char *device = qdict_get_str(qdict, "device");
583 BlockDriverState *bs;
585 if (!strcmp(device, "all")) {
588 bs = bdrv_find(device);
590 qerror_report(QERR_DEVICE_NOT_FOUND, device);
597 int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
599 const char *device = qdict_get_str(qdict, "device");
600 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
601 const char *format = qdict_get_try_str(qdict, "format");
602 BlockDriverState *bs;
603 BlockDriver *drv, *old_drv, *proto_drv;
606 char old_filename[1024];
609 qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
614 bs = bdrv_find(device);
616 qerror_report(QERR_DEVICE_NOT_FOUND, device);
621 pstrcpy(old_filename, sizeof(old_filename), bs->filename);
624 flags = bs->open_flags;
630 drv = bdrv_find_format(format);
632 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
637 proto_drv = bdrv_find_protocol(filename);
639 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
644 ret = bdrv_img_create(filename, format, bs->filename,
645 bs->drv->format_name, NULL, -1, flags);
654 ret = bdrv_open(bs, filename, flags, drv);
656 * If reopening the image file we just created fails, fall back
657 * and try to re-open the original image. If that fails too, we
658 * are in serious trouble.
661 ret = bdrv_open(bs, old_filename, flags, old_drv);
663 qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
665 qerror_report(QERR_OPEN_FILE_FAILED, filename);
676 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
678 if (!bdrv_dev_has_removable_media(bs)) {
679 qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
682 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
683 bdrv_dev_eject_request(bs, force);
685 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
693 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
695 BlockDriverState *bs;
696 int force = qdict_get_try_bool(qdict, "force", 0);
697 const char *filename = qdict_get_str(qdict, "device");
699 bs = bdrv_find(filename);
701 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
704 return eject_device(mon, bs, force);
707 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
710 BlockDriverState *bs;
713 bs = bdrv_find(qdict_get_str(qdict, "device"));
715 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
719 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
720 if (err == -EINVAL) {
721 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
723 } else if (err < 0) {
724 qerror_report(QERR_INVALID_PASSWORD);
731 int do_change_block(Monitor *mon, const char *device,
732 const char *filename, const char *fmt)
734 BlockDriverState *bs;
735 BlockDriver *drv = NULL;
738 bs = bdrv_find(device);
740 qerror_report(QERR_DEVICE_NOT_FOUND, device);
744 drv = bdrv_find_whitelisted_format(fmt);
746 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
750 if (eject_device(mon, bs, 0) < 0) {
753 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
754 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
755 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
756 qerror_report(QERR_OPEN_FILE_FAILED, filename);
759 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
762 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
764 const char *id = qdict_get_str(qdict, "id");
765 BlockDriverState *bs;
769 qerror_report(QERR_DEVICE_NOT_FOUND, id);
772 if (bdrv_in_use(bs)) {
773 qerror_report(QERR_DEVICE_IN_USE, id);
777 /* quiesce block driver; prevent further io */
782 /* if we have a device attached to this BlockDriverState
783 * then we need to make the drive anonymous until the device
784 * can be removed. If this is a drive with no device backing
785 * then we can just get rid of the block driver state right here.
787 if (bdrv_get_attached_dev(bs)) {
790 drive_uninit(drive_get_by_blockdev(bs));
797 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
798 * existing QERR_ macro mess is cleaned up. A good example for better
799 * error reports can be found in the qemu-img resize code.
801 int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
803 const char *device = qdict_get_str(qdict, "device");
804 int64_t size = qdict_get_int(qdict, "size");
805 BlockDriverState *bs;
807 bs = bdrv_find(device);
809 qerror_report(QERR_DEVICE_NOT_FOUND, device);
814 qerror_report(QERR_UNDEFINED_ERROR);
818 if (bdrv_truncate(bs, size)) {
819 qerror_report(QERR_UNDEFINED_ERROR);