make qemu-kvm option handling target-independent
[qemu-kvm.git] / blockdev.c
blob9678915b15b8d2259c0d03b75ee76424af16177b
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 "block_int.h"
19 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
21 static const char *const if_name[IF_COUNT] = {
22 [IF_NONE] = "none",
23 [IF_IDE] = "ide",
24 [IF_SCSI] = "scsi",
25 [IF_FLOPPY] = "floppy",
26 [IF_PFLASH] = "pflash",
27 [IF_MTD] = "mtd",
28 [IF_SD] = "sd",
29 [IF_VIRTIO] = "virtio",
30 [IF_XEN] = "xen",
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
41 * values.
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.
48 [IF_IDE] = 2,
49 [IF_SCSI] = 7,
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);
63 if (dinfo) {
64 dinfo->auto_del = 1;
68 void blockdev_auto_del(BlockDriverState *bs)
70 DriveInfo *dinfo = drive_get_by_blockdev(bs);
72 if (dinfo && dinfo->auto_del) {
73 drive_put_ref(dinfo);
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,
95 const char *optstr)
97 QemuOpts *opts;
98 char buf[32];
100 opts = drive_def(optstr);
101 if (!opts) {
102 return NULL;
104 if (type != IF_DEFAULT) {
105 qemu_opt_set(opts, "if", if_name[type]);
107 if (index >= 0) {
108 snprintf(buf, sizeof(buf), "%d", index);
109 qemu_opt_set(opts, "index", buf);
111 if (file)
112 qemu_opt_set(opts, "file", file);
113 return opts;
116 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
118 DriveInfo *dinfo;
120 /* seek interface, bus and unit */
122 QTAILQ_FOREACH(dinfo, &drives, next) {
123 if (dinfo->type == type &&
124 dinfo->bus == bus &&
125 dinfo->unit == unit)
126 return dinfo;
129 return NULL;
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)
141 int max_bus;
142 DriveInfo *dinfo;
144 max_bus = -1;
145 QTAILQ_FOREACH(dinfo, &drives, next) {
146 if(dinfo->type == type &&
147 dinfo->bus > max_bus)
148 max_bus = dinfo->bus;
150 return max_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
155 appropriate bus. */
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)
165 DriveInfo *dinfo;
167 QTAILQ_FOREACH(dinfo, &drives, next) {
168 if (dinfo->bdrv == bs) {
169 return dinfo;
172 return NULL;
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);
184 g_free(dinfo->id);
185 QTAILQ_REMOVE(&drives, dinfo, next);
186 g_free(dinfo);
189 void drive_put_ref(DriveInfo *dinfo)
191 assert(dinfo->refcount);
192 if (--dinfo->refcount == 0) {
193 drive_uninit(dinfo);
197 void drive_get_ref(DriveInfo *dinfo)
199 dinfo->refcount++;
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;
212 } else {
213 error_report("'%s' invalid %s error action",
214 buf, is_read ? "read" : "write");
215 return -1;
219 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
221 const char *buf;
222 const char *file = NULL;
223 char devname[128];
224 const char *serial;
225 const char *mediastr = "";
226 BlockInterfaceType type;
227 enum { MEDIA_DISK, MEDIA_CDROM } media;
228 int bus_id, unit_id;
229 int cyls, heads, secs, translation;
230 BlockDriver *drv = NULL;
231 int max_devs;
232 int index;
233 int ro = 0;
234 int bdrv_flags = 0;
235 int on_read_error, on_write_error;
236 const char *devaddr;
237 DriveInfo *dinfo;
238 int snapshot = 0;
239 int ret;
241 translation = BIOS_ATA_TRANSLATION_AUTO;
242 media = MEDIA_DISK;
244 /* extract parameters */
245 bus_id = qemu_opt_get_number(opts, "bus", 0);
246 unit_id = qemu_opt_get_number(opts, "unit", -1);
247 index = qemu_opt_get_number(opts, "index", -1);
249 cyls = qemu_opt_get_number(opts, "cyls", 0);
250 heads = qemu_opt_get_number(opts, "heads", 0);
251 secs = qemu_opt_get_number(opts, "secs", 0);
253 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
254 ro = qemu_opt_get_bool(opts, "readonly", 0);
256 file = qemu_opt_get(opts, "file");
257 serial = qemu_opt_get(opts, "serial");
259 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
260 pstrcpy(devname, sizeof(devname), buf);
261 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
263 if (type == IF_COUNT) {
264 error_report("unsupported bus type '%s'", buf);
265 return NULL;
267 } else {
268 type = default_to_scsi ? IF_SCSI : IF_IDE;
269 pstrcpy(devname, sizeof(devname), if_name[type]);
272 max_devs = if_max_devs[type];
274 if (cyls || heads || secs) {
275 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
276 error_report("invalid physical cyls number");
277 return NULL;
279 if (heads < 1 || (type == IF_IDE && heads > 16)) {
280 error_report("invalid physical heads number");
281 return NULL;
283 if (secs < 1 || (type == IF_IDE && secs > 63)) {
284 error_report("invalid physical secs number");
285 return NULL;
289 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
290 if (!cyls) {
291 error_report("'%s' trans must be used with cyls, heads and secs",
292 buf);
293 return NULL;
295 if (!strcmp(buf, "none"))
296 translation = BIOS_ATA_TRANSLATION_NONE;
297 else if (!strcmp(buf, "lba"))
298 translation = BIOS_ATA_TRANSLATION_LBA;
299 else if (!strcmp(buf, "auto"))
300 translation = BIOS_ATA_TRANSLATION_AUTO;
301 else {
302 error_report("'%s' invalid translation type", buf);
303 return NULL;
307 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
308 if (!strcmp(buf, "disk")) {
309 media = MEDIA_DISK;
310 } else if (!strcmp(buf, "cdrom")) {
311 if (cyls || secs || heads) {
312 error_report("CHS can't be set with media=%s", buf);
313 return NULL;
315 media = MEDIA_CDROM;
316 } else {
317 error_report("'%s' invalid media", buf);
318 return NULL;
322 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
323 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
324 error_report("invalid cache option");
325 return NULL;
329 #ifdef CONFIG_LINUX_AIO
330 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
331 if (!strcmp(buf, "native")) {
332 bdrv_flags |= BDRV_O_NATIVE_AIO;
333 } else if (!strcmp(buf, "threads")) {
334 /* this is the default */
335 } else {
336 error_report("invalid aio option");
337 return NULL;
340 #endif
342 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
343 if (strcmp(buf, "?") == 0) {
344 error_printf("Supported formats:");
345 bdrv_iterate_format(bdrv_format_print, NULL);
346 error_printf("\n");
347 return NULL;
349 drv = bdrv_find_whitelisted_format(buf);
350 if (!drv) {
351 error_report("'%s' invalid format", buf);
352 return NULL;
356 if (qemu_opt_get(opts, "boot") != NULL) {
357 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
358 "ignored. Future versions will reject this parameter. Please "
359 "update your scripts.\n");
362 on_write_error = BLOCK_ERR_STOP_ENOSPC;
363 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
364 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
365 error_report("werror is not supported by this bus type");
366 return NULL;
369 on_write_error = parse_block_error_action(buf, 0);
370 if (on_write_error < 0) {
371 return NULL;
375 on_read_error = BLOCK_ERR_REPORT;
376 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
377 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
378 error_report("rerror is not supported by this bus type");
379 return NULL;
382 on_read_error = parse_block_error_action(buf, 1);
383 if (on_read_error < 0) {
384 return NULL;
388 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
389 if (type != IF_VIRTIO) {
390 error_report("addr is not supported by this bus type");
391 return NULL;
395 /* compute bus and unit according index */
397 if (index != -1) {
398 if (bus_id != 0 || unit_id != -1) {
399 error_report("index cannot be used with bus and unit");
400 return NULL;
402 bus_id = drive_index_to_bus_id(type, index);
403 unit_id = drive_index_to_unit_id(type, index);
406 /* if user doesn't specify a unit_id,
407 * try to find the first free
410 if (unit_id == -1) {
411 unit_id = 0;
412 while (drive_get(type, bus_id, unit_id) != NULL) {
413 unit_id++;
414 if (max_devs && unit_id >= max_devs) {
415 unit_id -= max_devs;
416 bus_id++;
421 /* check unit id */
423 if (max_devs && unit_id >= max_devs) {
424 error_report("unit %d too big (max is %d)",
425 unit_id, max_devs - 1);
426 return NULL;
430 * catch multiple definitions
433 if (drive_get(type, bus_id, unit_id) != NULL) {
434 error_report("drive with bus=%d, unit=%d (index=%d) exists",
435 bus_id, unit_id, index);
436 return NULL;
439 /* init */
441 dinfo = g_malloc0(sizeof(*dinfo));
442 if ((buf = qemu_opts_id(opts)) != NULL) {
443 dinfo->id = g_strdup(buf);
444 } else {
445 /* no id supplied -> create one */
446 dinfo->id = g_malloc0(32);
447 if (type == IF_IDE || type == IF_SCSI)
448 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
449 if (max_devs)
450 snprintf(dinfo->id, 32, "%s%i%s%i",
451 devname, bus_id, mediastr, unit_id);
452 else
453 snprintf(dinfo->id, 32, "%s%s%i",
454 devname, mediastr, unit_id);
456 dinfo->bdrv = bdrv_new(dinfo->id);
457 dinfo->devaddr = devaddr;
458 dinfo->type = type;
459 dinfo->bus = bus_id;
460 dinfo->unit = unit_id;
461 dinfo->opts = opts;
462 dinfo->refcount = 1;
463 if (serial)
464 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
465 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
467 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
469 switch(type) {
470 case IF_IDE:
471 case IF_SCSI:
472 case IF_XEN:
473 case IF_NONE:
474 switch(media) {
475 case MEDIA_DISK:
476 if (cyls != 0) {
477 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
478 bdrv_set_translation_hint(dinfo->bdrv, translation);
480 break;
481 case MEDIA_CDROM:
482 dinfo->media_cd = 1;
483 break;
485 break;
486 case IF_SD:
487 case IF_FLOPPY:
488 case IF_PFLASH:
489 case IF_MTD:
490 break;
491 case IF_VIRTIO:
492 /* add virtio block device */
493 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
494 qemu_opt_set(opts, "driver", "virtio-blk");
495 qemu_opt_set(opts, "drive", dinfo->id);
496 if (devaddr)
497 qemu_opt_set(opts, "addr", devaddr);
498 break;
499 default:
500 abort();
502 if (!file || !*file) {
503 return dinfo;
505 if (snapshot) {
506 /* always use cache=unsafe with snapshot */
507 bdrv_flags &= ~BDRV_O_CACHE_MASK;
508 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
511 if (media == MEDIA_CDROM) {
512 /* CDROM is fine for any interface, don't check. */
513 ro = 1;
514 } else if (ro == 1) {
515 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
516 error_report("readonly not supported by this bus type");
517 goto err;
521 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
523 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
524 if (ret < 0) {
525 error_report("could not open disk image %s: %s",
526 file, strerror(-ret));
527 goto err;
530 if (bdrv_key_required(dinfo->bdrv))
531 autostart = 0;
532 return dinfo;
534 err:
535 bdrv_delete(dinfo->bdrv);
536 g_free(dinfo->id);
537 QTAILQ_REMOVE(&drives, dinfo, next);
538 g_free(dinfo);
539 return NULL;
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, *old_drv, *proto_drv;
566 int ret = 0;
567 int flags;
568 char old_filename[1024];
570 if (!filename) {
571 qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
572 ret = -1;
573 goto out;
576 bs = bdrv_find(device);
577 if (!bs) {
578 qerror_report(QERR_DEVICE_NOT_FOUND, device);
579 ret = -1;
580 goto out;
583 pstrcpy(old_filename, sizeof(old_filename), bs->filename);
585 old_drv = bs->drv;
586 flags = bs->open_flags;
588 if (!format) {
589 format = "qcow2";
592 drv = bdrv_find_format(format);
593 if (!drv) {
594 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
595 ret = -1;
596 goto out;
599 proto_drv = bdrv_find_protocol(filename);
600 if (!proto_drv) {
601 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
602 ret = -1;
603 goto out;
606 ret = bdrv_img_create(filename, format, bs->filename,
607 bs->drv->format_name, NULL, -1, flags);
608 if (ret) {
609 goto out;
612 qemu_aio_flush();
613 bdrv_flush(bs);
615 bdrv_close(bs);
616 ret = bdrv_open(bs, filename, flags, drv);
618 * If reopening the image file we just created fails, fall back
619 * and try to re-open the original image. If that fails too, we
620 * are in serious trouble.
622 if (ret != 0) {
623 ret = bdrv_open(bs, old_filename, flags, old_drv);
624 if (ret != 0) {
625 qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
626 } else {
627 qerror_report(QERR_OPEN_FILE_FAILED, filename);
630 out:
631 if (ret) {
632 ret = -1;
635 return ret;
638 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
640 if (!bdrv_dev_has_removable_media(bs)) {
641 qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
642 return -1;
644 if (!force && !bdrv_dev_is_tray_open(bs)
645 && bdrv_dev_is_medium_locked(bs)) {
646 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
647 return -1;
649 bdrv_close(bs);
650 return 0;
653 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
655 BlockDriverState *bs;
656 int force = qdict_get_try_bool(qdict, "force", 0);
657 const char *filename = qdict_get_str(qdict, "device");
659 bs = bdrv_find(filename);
660 if (!bs) {
661 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
662 return -1;
664 return eject_device(mon, bs, force);
667 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
668 QObject **ret_data)
670 BlockDriverState *bs;
671 int err;
673 bs = bdrv_find(qdict_get_str(qdict, "device"));
674 if (!bs) {
675 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
676 return -1;
679 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
680 if (err == -EINVAL) {
681 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
682 return -1;
683 } else if (err < 0) {
684 qerror_report(QERR_INVALID_PASSWORD);
685 return -1;
688 return 0;
691 int do_change_block(Monitor *mon, const char *device,
692 const char *filename, const char *fmt)
694 BlockDriverState *bs;
695 BlockDriver *drv = NULL;
696 int bdrv_flags;
698 bs = bdrv_find(device);
699 if (!bs) {
700 qerror_report(QERR_DEVICE_NOT_FOUND, device);
701 return -1;
703 if (fmt) {
704 drv = bdrv_find_whitelisted_format(fmt);
705 if (!drv) {
706 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
707 return -1;
710 if (eject_device(mon, bs, 0) < 0) {
711 return -1;
713 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
714 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
715 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
716 qerror_report(QERR_OPEN_FILE_FAILED, filename);
717 return -1;
719 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
722 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
724 const char *id = qdict_get_str(qdict, "id");
725 BlockDriverState *bs;
727 bs = bdrv_find(id);
728 if (!bs) {
729 qerror_report(QERR_DEVICE_NOT_FOUND, id);
730 return -1;
732 if (bdrv_in_use(bs)) {
733 qerror_report(QERR_DEVICE_IN_USE, id);
734 return -1;
737 /* quiesce block driver; prevent further io */
738 qemu_aio_flush();
739 bdrv_flush(bs);
740 bdrv_close(bs);
742 /* if we have a device attached to this BlockDriverState
743 * then we need to make the drive anonymous until the device
744 * can be removed. If this is a drive with no device backing
745 * then we can just get rid of the block driver state right here.
747 if (bdrv_get_attached_dev(bs)) {
748 bdrv_make_anon(bs);
749 } else {
750 drive_uninit(drive_get_by_blockdev(bs));
753 return 0;
757 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
758 * existing QERR_ macro mess is cleaned up. A good example for better
759 * error reports can be found in the qemu-img resize code.
761 int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
763 const char *device = qdict_get_str(qdict, "device");
764 int64_t size = qdict_get_int(qdict, "size");
765 BlockDriverState *bs;
767 bs = bdrv_find(device);
768 if (!bs) {
769 qerror_report(QERR_DEVICE_NOT_FOUND, device);
770 return -1;
773 if (size < 0) {
774 qerror_report(QERR_UNDEFINED_ERROR);
775 return -1;
778 if (bdrv_truncate(bs, size)) {
779 qerror_report(QERR_UNDEFINED_ERROR);
780 return -1;
783 return 0;