blockdev: Reject multiple definitions for the same drive
[qemu.git] / blockdev.c
blob3f7b560b5ddb9d0f33470e407c6c022eb09326bf
1 /*
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.
8 */
10 #include "block.h"
11 #include "blockdev.h"
12 #include "monitor.h"
13 #include "qerror.h"
14 #include "qemu-option.h"
15 #include "qemu-config.h"
16 #include "sysemu.h"
17 #include "hw/qdev.h"
18 #include "block_int.h"
20 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
22 static const char *const if_name[IF_COUNT] = {
23 [IF_NONE] = "none",
24 [IF_IDE] = "ide",
25 [IF_SCSI] = "scsi",
26 [IF_FLOPPY] = "floppy",
27 [IF_PFLASH] = "pflash",
28 [IF_MTD] = "mtd",
29 [IF_SD] = "sd",
30 [IF_VIRTIO] = "virtio",
31 [IF_XEN] = "xen",
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
42 * values.
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.
49 [IF_IDE] = 2,
50 [IF_SCSI] = 7,
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);
64 if (dinfo) {
65 dinfo->auto_del = 1;
69 void blockdev_auto_del(BlockDriverState *bs)
71 DriveInfo *dinfo = drive_get_by_blockdev(bs);
73 if (dinfo && dinfo->auto_del) {
74 drive_uninit(dinfo);
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,
96 const char *fmt, ...)
98 va_list ap;
99 char optstr[1024];
100 QemuOpts *opts;
101 char buf[32];
103 va_start(ap, fmt);
104 vsnprintf(optstr, sizeof(optstr), fmt, ap);
105 va_end(ap);
107 opts = drive_def(optstr);
108 if (!opts) {
109 return NULL;
111 if (type != IF_DEFAULT) {
112 qemu_opt_set(opts, "if", if_name[type]);
114 if (index >= 0) {
115 snprintf(buf, sizeof(buf), "%d", index);
116 qemu_opt_set(opts, "index", buf);
118 if (file)
119 qemu_opt_set(opts, "file", file);
120 return opts;
123 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
125 DriveInfo *dinfo;
127 /* seek interface, bus and unit */
129 QTAILQ_FOREACH(dinfo, &drives, next) {
130 if (dinfo->type == type &&
131 dinfo->bus == bus &&
132 dinfo->unit == unit)
133 return dinfo;
136 return NULL;
139 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
141 return drive_get(type,
142 drive_index_to_bus_id(type, index),
143 drive_index_to_unit_id(type, index));
146 int drive_get_max_bus(BlockInterfaceType type)
148 int max_bus;
149 DriveInfo *dinfo;
151 max_bus = -1;
152 QTAILQ_FOREACH(dinfo, &drives, next) {
153 if(dinfo->type == type &&
154 dinfo->bus > max_bus)
155 max_bus = dinfo->bus;
157 return max_bus;
160 /* Get a block device. This should only be used for single-drive devices
161 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
162 appropriate bus. */
163 DriveInfo *drive_get_next(BlockInterfaceType type)
165 static int next_block_unit[IF_COUNT];
167 return drive_get(type, 0, next_block_unit[type]++);
170 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
172 DriveInfo *dinfo;
174 QTAILQ_FOREACH(dinfo, &drives, next) {
175 if (dinfo->bdrv == bs) {
176 return dinfo;
179 return NULL;
182 static void bdrv_format_print(void *opaque, const char *name)
184 error_printf(" %s", name);
187 void drive_uninit(DriveInfo *dinfo)
189 qemu_opts_del(dinfo->opts);
190 bdrv_delete(dinfo->bdrv);
191 QTAILQ_REMOVE(&drives, dinfo, next);
192 qemu_free(dinfo);
195 static int parse_block_error_action(const char *buf, int is_read)
197 if (!strcmp(buf, "ignore")) {
198 return BLOCK_ERR_IGNORE;
199 } else if (!is_read && !strcmp(buf, "enospc")) {
200 return BLOCK_ERR_STOP_ENOSPC;
201 } else if (!strcmp(buf, "stop")) {
202 return BLOCK_ERR_STOP_ANY;
203 } else if (!strcmp(buf, "report")) {
204 return BLOCK_ERR_REPORT;
205 } else {
206 error_report("'%s' invalid %s error action",
207 buf, is_read ? "read" : "write");
208 return -1;
212 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
214 const char *buf;
215 const char *file = NULL;
216 char devname[128];
217 const char *serial;
218 const char *mediastr = "";
219 BlockInterfaceType type;
220 enum { MEDIA_DISK, MEDIA_CDROM } media;
221 int bus_id, unit_id;
222 int cyls, heads, secs, translation;
223 BlockDriver *drv = NULL;
224 int max_devs;
225 int index;
226 int ro = 0;
227 int bdrv_flags = 0;
228 int on_read_error, on_write_error;
229 const char *devaddr;
230 DriveInfo *dinfo;
231 int snapshot = 0;
232 int ret;
234 *fatal_error = 1;
236 translation = BIOS_ATA_TRANSLATION_AUTO;
238 if (default_to_scsi) {
239 type = IF_SCSI;
240 pstrcpy(devname, sizeof(devname), "scsi");
241 } else {
242 type = IF_IDE;
243 pstrcpy(devname, sizeof(devname), "ide");
245 media = MEDIA_DISK;
247 /* extract parameters */
248 bus_id = qemu_opt_get_number(opts, "bus", 0);
249 unit_id = qemu_opt_get_number(opts, "unit", -1);
250 index = qemu_opt_get_number(opts, "index", -1);
252 cyls = qemu_opt_get_number(opts, "cyls", 0);
253 heads = qemu_opt_get_number(opts, "heads", 0);
254 secs = qemu_opt_get_number(opts, "secs", 0);
256 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
257 ro = qemu_opt_get_bool(opts, "readonly", 0);
259 file = qemu_opt_get(opts, "file");
260 serial = qemu_opt_get(opts, "serial");
262 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
263 pstrcpy(devname, sizeof(devname), buf);
264 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
266 if (type == IF_COUNT) {
267 error_report("unsupported bus type '%s'", buf);
268 return NULL;
271 max_devs = if_max_devs[type];
273 if (cyls || heads || secs) {
274 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
275 error_report("invalid physical cyls number");
276 return NULL;
278 if (heads < 1 || (type == IF_IDE && heads > 16)) {
279 error_report("invalid physical heads number");
280 return NULL;
282 if (secs < 1 || (type == IF_IDE && secs > 63)) {
283 error_report("invalid physical secs number");
284 return NULL;
288 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
289 if (!cyls) {
290 error_report("'%s' trans must be used with cyls,heads and secs",
291 buf);
292 return NULL;
294 if (!strcmp(buf, "none"))
295 translation = BIOS_ATA_TRANSLATION_NONE;
296 else if (!strcmp(buf, "lba"))
297 translation = BIOS_ATA_TRANSLATION_LBA;
298 else if (!strcmp(buf, "auto"))
299 translation = BIOS_ATA_TRANSLATION_AUTO;
300 else {
301 error_report("'%s' invalid translation type", buf);
302 return NULL;
306 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
307 if (!strcmp(buf, "disk")) {
308 media = MEDIA_DISK;
309 } else if (!strcmp(buf, "cdrom")) {
310 if (cyls || secs || heads) {
311 error_report("'%s' invalid physical CHS format", buf);
312 return NULL;
314 media = MEDIA_CDROM;
315 } else {
316 error_report("'%s' invalid media", buf);
317 return NULL;
321 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
322 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
323 bdrv_flags |= BDRV_O_NOCACHE;
324 } else if (!strcmp(buf, "writeback")) {
325 bdrv_flags |= BDRV_O_CACHE_WB;
326 } else if (!strcmp(buf, "unsafe")) {
327 bdrv_flags |= BDRV_O_CACHE_WB;
328 bdrv_flags |= BDRV_O_NO_FLUSH;
329 } else if (!strcmp(buf, "writethrough")) {
330 /* this is the default */
331 } else {
332 error_report("invalid cache option");
333 return NULL;
337 #ifdef CONFIG_LINUX_AIO
338 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
339 if (!strcmp(buf, "native")) {
340 bdrv_flags |= BDRV_O_NATIVE_AIO;
341 } else if (!strcmp(buf, "threads")) {
342 /* this is the default */
343 } else {
344 error_report("invalid aio option");
345 return NULL;
348 #endif
350 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
351 if (strcmp(buf, "?") == 0) {
352 error_printf("Supported formats:");
353 bdrv_iterate_format(bdrv_format_print, NULL);
354 error_printf("\n");
355 return NULL;
357 drv = bdrv_find_whitelisted_format(buf);
358 if (!drv) {
359 error_report("'%s' invalid format", buf);
360 return NULL;
364 on_write_error = BLOCK_ERR_STOP_ENOSPC;
365 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
366 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
367 error_report("werror is not supported by this bus type");
368 return NULL;
371 on_write_error = parse_block_error_action(buf, 0);
372 if (on_write_error < 0) {
373 return NULL;
377 on_read_error = BLOCK_ERR_REPORT;
378 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
379 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
380 error_report("rerror is not supported by this bus type");
381 return NULL;
384 on_read_error = parse_block_error_action(buf, 1);
385 if (on_read_error < 0) {
386 return NULL;
390 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
391 if (type != IF_VIRTIO) {
392 error_report("addr is not supported by this bus type");
393 return NULL;
397 /* compute bus and unit according index */
399 if (index != -1) {
400 if (bus_id != 0 || unit_id != -1) {
401 error_report("index cannot be used with bus and unit");
402 return NULL;
404 bus_id = drive_index_to_bus_id(type, index);
405 unit_id = drive_index_to_unit_id(type, index);
408 /* if user doesn't specify a unit_id,
409 * try to find the first free
412 if (unit_id == -1) {
413 unit_id = 0;
414 while (drive_get(type, bus_id, unit_id) != NULL) {
415 unit_id++;
416 if (max_devs && unit_id >= max_devs) {
417 unit_id -= max_devs;
418 bus_id++;
423 /* check unit id */
425 if (max_devs && unit_id >= max_devs) {
426 error_report("unit %d too big (max is %d)",
427 unit_id, max_devs - 1);
428 return NULL;
432 * catch multiple definitions
435 if (drive_get(type, bus_id, unit_id) != NULL) {
436 error_report("drive with bus=%d, unit=%d (index=%d) exists",
437 bus_id, unit_id, index);
438 return NULL;
441 /* init */
443 dinfo = qemu_mallocz(sizeof(*dinfo));
444 if ((buf = qemu_opts_id(opts)) != NULL) {
445 dinfo->id = qemu_strdup(buf);
446 } else {
447 /* no id supplied -> create one */
448 dinfo->id = qemu_mallocz(32);
449 if (type == IF_IDE || type == IF_SCSI)
450 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
451 if (max_devs)
452 snprintf(dinfo->id, 32, "%s%i%s%i",
453 devname, bus_id, mediastr, unit_id);
454 else
455 snprintf(dinfo->id, 32, "%s%s%i",
456 devname, mediastr, unit_id);
458 dinfo->bdrv = bdrv_new(dinfo->id);
459 dinfo->devaddr = devaddr;
460 dinfo->type = type;
461 dinfo->bus = bus_id;
462 dinfo->unit = unit_id;
463 dinfo->opts = opts;
464 if (serial)
465 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
466 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
468 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
470 switch(type) {
471 case IF_IDE:
472 case IF_SCSI:
473 case IF_XEN:
474 case IF_NONE:
475 switch(media) {
476 case MEDIA_DISK:
477 if (cyls != 0) {
478 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
479 bdrv_set_translation_hint(dinfo->bdrv, translation);
481 break;
482 case MEDIA_CDROM:
483 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
484 break;
486 break;
487 case IF_SD:
488 /* FIXME: This isn't really a floppy, but it's a reasonable
489 approximation. */
490 case IF_FLOPPY:
491 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
492 break;
493 case IF_PFLASH:
494 case IF_MTD:
495 break;
496 case IF_VIRTIO:
497 /* add virtio block device */
498 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
499 qemu_opt_set(opts, "driver", "virtio-blk-pci");
500 qemu_opt_set(opts, "drive", dinfo->id);
501 if (devaddr)
502 qemu_opt_set(opts, "addr", devaddr);
503 break;
504 default:
505 abort();
507 if (!file || !*file) {
508 *fatal_error = 0;
509 return NULL;
511 if (snapshot) {
512 /* always use cache=unsafe with snapshot */
513 bdrv_flags &= ~BDRV_O_CACHE_MASK;
514 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
517 if (media == MEDIA_CDROM) {
518 /* CDROM is fine for any interface, don't check. */
519 ro = 1;
520 } else if (ro == 1) {
521 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
522 error_report("readonly not supported by this bus type");
523 return NULL;
527 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
529 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
530 if (ret < 0) {
531 error_report("could not open disk image %s: %s",
532 file, strerror(-ret));
533 return NULL;
536 if (bdrv_key_required(dinfo->bdrv))
537 autostart = 0;
538 *fatal_error = 0;
539 return dinfo;
542 void do_commit(Monitor *mon, const QDict *qdict)
544 const char *device = qdict_get_str(qdict, "device");
545 BlockDriverState *bs;
547 if (!strcmp(device, "all")) {
548 bdrv_commit_all();
549 } else {
550 bs = bdrv_find(device);
551 if (!bs) {
552 qerror_report(QERR_DEVICE_NOT_FOUND, device);
553 return;
555 bdrv_commit(bs);
559 int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
561 const char *device = qdict_get_str(qdict, "device");
562 const char *filename = qdict_get_try_str(qdict, "snapshot_file");
563 const char *format = qdict_get_try_str(qdict, "format");
564 BlockDriverState *bs;
565 BlockDriver *drv, *proto_drv;
566 int ret = 0;
567 int flags;
569 if (!filename) {
570 qerror_report(QERR_MISSING_PARAMETER, "snapshot_file");
571 ret = -1;
572 goto out;
575 bs = bdrv_find(device);
576 if (!bs) {
577 qerror_report(QERR_DEVICE_NOT_FOUND, device);
578 ret = -1;
579 goto out;
582 if (!format) {
583 format = "qcow2";
586 drv = bdrv_find_format(format);
587 if (!drv) {
588 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
589 ret = -1;
590 goto out;
593 proto_drv = bdrv_find_protocol(filename);
594 if (!proto_drv) {
595 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
596 ret = -1;
597 goto out;
600 ret = bdrv_img_create(filename, format, bs->filename,
601 bs->drv->format_name, NULL, -1, bs->open_flags);
602 if (ret) {
603 goto out;
606 qemu_aio_flush();
607 bdrv_flush(bs);
609 flags = bs->open_flags;
610 bdrv_close(bs);
611 ret = bdrv_open(bs, filename, flags, drv);
613 * If reopening the image file we just created fails, we really
614 * are in trouble :(
616 if (ret != 0) {
617 abort();
619 out:
620 if (ret) {
621 ret = -1;
624 return ret;
627 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
629 if (!force) {
630 if (!bdrv_is_removable(bs)) {
631 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
632 bdrv_get_device_name(bs));
633 return -1;
635 if (bdrv_is_locked(bs)) {
636 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
637 return -1;
640 bdrv_close(bs);
641 return 0;
644 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
646 BlockDriverState *bs;
647 int force = qdict_get_try_bool(qdict, "force", 0);
648 const char *filename = qdict_get_str(qdict, "device");
650 bs = bdrv_find(filename);
651 if (!bs) {
652 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
653 return -1;
655 return eject_device(mon, bs, force);
658 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
659 QObject **ret_data)
661 BlockDriverState *bs;
662 int err;
664 bs = bdrv_find(qdict_get_str(qdict, "device"));
665 if (!bs) {
666 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
667 return -1;
670 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
671 if (err == -EINVAL) {
672 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
673 return -1;
674 } else if (err < 0) {
675 qerror_report(QERR_INVALID_PASSWORD);
676 return -1;
679 return 0;
682 int do_change_block(Monitor *mon, const char *device,
683 const char *filename, const char *fmt)
685 BlockDriverState *bs;
686 BlockDriver *drv = NULL;
687 int bdrv_flags;
689 bs = bdrv_find(device);
690 if (!bs) {
691 qerror_report(QERR_DEVICE_NOT_FOUND, device);
692 return -1;
694 if (fmt) {
695 drv = bdrv_find_whitelisted_format(fmt);
696 if (!drv) {
697 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
698 return -1;
701 if (eject_device(mon, bs, 0) < 0) {
702 return -1;
704 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
705 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
706 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
707 qerror_report(QERR_OPEN_FILE_FAILED, filename);
708 return -1;
710 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
713 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
715 const char *id = qdict_get_str(qdict, "id");
716 BlockDriverState *bs;
717 BlockDriverState **ptr;
718 Property *prop;
720 bs = bdrv_find(id);
721 if (!bs) {
722 qerror_report(QERR_DEVICE_NOT_FOUND, id);
723 return -1;
726 /* quiesce block driver; prevent further io */
727 qemu_aio_flush();
728 bdrv_flush(bs);
729 bdrv_close(bs);
731 /* clean up guest state from pointing to host resource by
732 * finding and removing DeviceState "drive" property */
733 if (bs->peer) {
734 for (prop = bs->peer->info->props; prop && prop->name; prop++) {
735 if (prop->info->type == PROP_TYPE_DRIVE) {
736 ptr = qdev_get_prop_ptr(bs->peer, prop);
737 if (*ptr == bs) {
738 bdrv_detach(bs, bs->peer);
739 *ptr = NULL;
740 break;
746 /* clean up host side */
747 drive_uninit(drive_get_by_blockdev(bs));
749 return 0;
753 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
754 * existing QERR_ macro mess is cleaned up. A good example for better
755 * error reports can be found in the qemu-img resize code.
757 int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
759 const char *device = qdict_get_str(qdict, "device");
760 int64_t size = qdict_get_int(qdict, "size");
761 BlockDriverState *bs;
763 bs = bdrv_find(device);
764 if (!bs) {
765 qerror_report(QERR_DEVICE_NOT_FOUND, device);
766 return -1;
769 if (size < 0) {
770 qerror_report(QERR_UNDEFINED_ERROR);
771 return -1;
774 if (bdrv_truncate(bs, size)) {
775 qerror_report(QERR_UNDEFINED_ERROR);
776 return -1;
779 return 0;