qemu-kvm: Drop redundant cpuid filtering from cpu_x86_cpuid
[qemu-kvm.git] / blockdev.c
blobda74171cb4504b662ae7f00ee4d163fde3d1277f
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 DriveInfo *extboot_drive = NULL;
21 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
23 static const char *const if_name[IF_COUNT] = {
24 [IF_NONE] = "none",
25 [IF_IDE] = "ide",
26 [IF_SCSI] = "scsi",
27 [IF_FLOPPY] = "floppy",
28 [IF_PFLASH] = "pflash",
29 [IF_MTD] = "mtd",
30 [IF_SD] = "sd",
31 [IF_VIRTIO] = "virtio",
32 [IF_XEN] = "xen",
35 static const int if_max_devs[IF_COUNT] = {
37 * Do not change these numbers! They govern how drive option
38 * index maps to unit and bus. That mapping is ABI.
40 * All controllers used to imlement if=T drives need to support
41 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
42 * Otherwise, some index values map to "impossible" bus, unit
43 * values.
45 * For instance, if you change [IF_SCSI] to 255, -drive
46 * if=scsi,index=12 no longer means bus=1,unit=5, but
47 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
48 * the drive can't be set up. Regression.
50 [IF_IDE] = 2,
51 [IF_SCSI] = 7,
55 * We automatically delete the drive when a device using it gets
56 * unplugged. Questionable feature, but we can't just drop it.
57 * Device models call blockdev_mark_auto_del() to schedule the
58 * automatic deletion, and generic qdev code calls blockdev_auto_del()
59 * when deletion is actually safe.
61 void blockdev_mark_auto_del(BlockDriverState *bs)
63 DriveInfo *dinfo = drive_get_by_blockdev(bs);
65 if (dinfo) {
66 dinfo->auto_del = 1;
70 void blockdev_auto_del(BlockDriverState *bs)
72 DriveInfo *dinfo = drive_get_by_blockdev(bs);
74 if (dinfo && dinfo->auto_del) {
75 drive_put_ref(dinfo);
79 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
81 int max_devs = if_max_devs[type];
82 return max_devs ? index / max_devs : 0;
85 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
87 int max_devs = if_max_devs[type];
88 return max_devs ? index % max_devs : index;
91 QemuOpts *drive_def(const char *optstr)
93 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
96 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
97 const char *optstr)
99 QemuOpts *opts;
100 char buf[32];
102 opts = drive_def(optstr);
103 if (!opts) {
104 return NULL;
106 if (type != IF_DEFAULT) {
107 qemu_opt_set(opts, "if", if_name[type]);
109 if (index >= 0) {
110 snprintf(buf, sizeof(buf), "%d", index);
111 qemu_opt_set(opts, "index", buf);
113 if (file)
114 qemu_opt_set(opts, "file", file);
115 return opts;
118 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
120 DriveInfo *dinfo;
122 /* seek interface, bus and unit */
124 QTAILQ_FOREACH(dinfo, &drives, next) {
125 if (dinfo->type == type &&
126 dinfo->bus == bus &&
127 dinfo->unit == unit)
128 return dinfo;
131 return NULL;
134 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
136 return drive_get(type,
137 drive_index_to_bus_id(type, index),
138 drive_index_to_unit_id(type, index));
141 int drive_get_max_bus(BlockInterfaceType type)
143 int max_bus;
144 DriveInfo *dinfo;
146 max_bus = -1;
147 QTAILQ_FOREACH(dinfo, &drives, next) {
148 if(dinfo->type == type &&
149 dinfo->bus > max_bus)
150 max_bus = dinfo->bus;
152 return max_bus;
155 /* Get a block device. This should only be used for single-drive devices
156 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
157 appropriate bus. */
158 DriveInfo *drive_get_next(BlockInterfaceType type)
160 static int next_block_unit[IF_COUNT];
162 return drive_get(type, 0, next_block_unit[type]++);
165 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
167 DriveInfo *dinfo;
169 QTAILQ_FOREACH(dinfo, &drives, next) {
170 if (dinfo->bdrv == bs) {
171 return dinfo;
174 return NULL;
177 static void bdrv_format_print(void *opaque, const char *name)
179 error_printf(" %s", name);
182 static void drive_uninit(DriveInfo *dinfo)
184 qemu_opts_del(dinfo->opts);
185 bdrv_delete(dinfo->bdrv);
186 g_free(dinfo->id);
187 QTAILQ_REMOVE(&drives, dinfo, next);
188 g_free(dinfo);
191 void drive_put_ref(DriveInfo *dinfo)
193 assert(dinfo->refcount);
194 if (--dinfo->refcount == 0) {
195 drive_uninit(dinfo);
199 void drive_get_ref(DriveInfo *dinfo)
201 dinfo->refcount++;
204 static int parse_block_error_action(const char *buf, int is_read)
206 if (!strcmp(buf, "ignore")) {
207 return BLOCK_ERR_IGNORE;
208 } else if (!is_read && !strcmp(buf, "enospc")) {
209 return BLOCK_ERR_STOP_ENOSPC;
210 } else if (!strcmp(buf, "stop")) {
211 return BLOCK_ERR_STOP_ANY;
212 } else if (!strcmp(buf, "report")) {
213 return BLOCK_ERR_REPORT;
214 } else {
215 error_report("'%s' invalid %s error action",
216 buf, is_read ? "read" : "write");
217 return -1;
221 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
223 const char *buf;
224 const char *file = NULL;
225 char devname[128];
226 const char *serial;
227 const char *mediastr = "";
228 BlockInterfaceType type;
229 enum { MEDIA_DISK, MEDIA_CDROM } media;
230 int bus_id, unit_id;
231 int cyls, heads, secs, translation;
232 BlockDriver *drv = NULL;
233 int max_devs;
234 int index;
235 int ro = 0;
236 int bdrv_flags = 0;
237 int on_read_error, on_write_error;
238 const char *devaddr;
239 DriveInfo *dinfo;
240 int is_extboot = 0;
241 int snapshot = 0;
242 int ret;
244 translation = BIOS_ATA_TRANSLATION_AUTO;
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;
270 } else {
271 type = default_to_scsi ? IF_SCSI : IF_IDE;
272 pstrcpy(devname, sizeof(devname), if_name[type]);
275 max_devs = if_max_devs[type];
277 if (cyls || heads || secs) {
278 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
279 error_report("invalid physical cyls number");
280 return NULL;
282 if (heads < 1 || (type == IF_IDE && heads > 16)) {
283 error_report("invalid physical heads number");
284 return NULL;
286 if (secs < 1 || (type == IF_IDE && secs > 63)) {
287 error_report("invalid physical secs number");
288 return NULL;
292 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
293 if (!cyls) {
294 error_report("'%s' trans must be used with cyls, heads and secs",
295 buf);
296 return NULL;
298 if (!strcmp(buf, "none"))
299 translation = BIOS_ATA_TRANSLATION_NONE;
300 else if (!strcmp(buf, "lba"))
301 translation = BIOS_ATA_TRANSLATION_LBA;
302 else if (!strcmp(buf, "auto"))
303 translation = BIOS_ATA_TRANSLATION_AUTO;
304 else {
305 error_report("'%s' invalid translation type", buf);
306 return NULL;
310 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
311 if (!strcmp(buf, "disk")) {
312 media = MEDIA_DISK;
313 } else if (!strcmp(buf, "cdrom")) {
314 if (cyls || secs || heads) {
315 error_report("CHS can't be set with media=%s", buf);
316 return NULL;
318 media = MEDIA_CDROM;
319 } else {
320 error_report("'%s' invalid media", buf);
321 return NULL;
325 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
326 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
327 error_report("invalid cache option");
328 return NULL;
332 #ifdef CONFIG_LINUX_AIO
333 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
334 if (!strcmp(buf, "native")) {
335 bdrv_flags |= BDRV_O_NATIVE_AIO;
336 } else if (!strcmp(buf, "threads")) {
337 /* this is the default */
338 } else {
339 error_report("invalid aio option");
340 return NULL;
343 #endif
345 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
346 if (strcmp(buf, "?") == 0) {
347 error_printf("Supported formats:");
348 bdrv_iterate_format(bdrv_format_print, NULL);
349 error_printf("\n");
350 return NULL;
352 drv = bdrv_find_whitelisted_format(buf);
353 if (!drv) {
354 error_report("'%s' invalid format", buf);
355 return NULL;
359 is_extboot = qemu_opt_get_bool(opts, "boot", 0);
360 if (is_extboot && extboot_drive) {
361 fprintf(stderr, "qemu: two bootable drives specified\n");
362 return NULL;
365 on_write_error = BLOCK_ERR_STOP_ENOSPC;
366 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
367 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
368 error_report("werror is not supported by this bus type");
369 return NULL;
372 on_write_error = parse_block_error_action(buf, 0);
373 if (on_write_error < 0) {
374 return NULL;
378 on_read_error = BLOCK_ERR_REPORT;
379 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
380 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
381 error_report("rerror is not supported by this bus type");
382 return NULL;
385 on_read_error = parse_block_error_action(buf, 1);
386 if (on_read_error < 0) {
387 return NULL;
391 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
392 if (type != IF_VIRTIO) {
393 error_report("addr is not supported by this bus type");
394 return NULL;
398 /* compute bus and unit according index */
400 if (index != -1) {
401 if (bus_id != 0 || unit_id != -1) {
402 error_report("index cannot be used with bus and unit");
403 return NULL;
405 bus_id = drive_index_to_bus_id(type, index);
406 unit_id = drive_index_to_unit_id(type, index);
409 /* if user doesn't specify a unit_id,
410 * try to find the first free
413 if (unit_id == -1) {
414 unit_id = 0;
415 while (drive_get(type, bus_id, unit_id) != NULL) {
416 unit_id++;
417 if (max_devs && unit_id >= max_devs) {
418 unit_id -= max_devs;
419 bus_id++;
424 /* check unit id */
426 if (max_devs && unit_id >= max_devs) {
427 error_report("unit %d too big (max is %d)",
428 unit_id, max_devs - 1);
429 return NULL;
433 * catch multiple definitions
436 if (drive_get(type, bus_id, unit_id) != NULL) {
437 error_report("drive with bus=%d, unit=%d (index=%d) exists",
438 bus_id, unit_id, index);
439 return NULL;
442 /* init */
444 dinfo = g_malloc0(sizeof(*dinfo));
445 if ((buf = qemu_opts_id(opts)) != NULL) {
446 dinfo->id = g_strdup(buf);
447 } else {
448 /* no id supplied -> create one */
449 dinfo->id = g_malloc0(32);
450 if (type == IF_IDE || type == IF_SCSI)
451 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
452 if (max_devs)
453 snprintf(dinfo->id, 32, "%s%i%s%i",
454 devname, bus_id, mediastr, unit_id);
455 else
456 snprintf(dinfo->id, 32, "%s%s%i",
457 devname, mediastr, unit_id);
459 dinfo->bdrv = bdrv_new(dinfo->id);
460 dinfo->devaddr = devaddr;
461 dinfo->type = type;
462 dinfo->bus = bus_id;
463 dinfo->unit = unit_id;
464 dinfo->opts = opts;
465 dinfo->refcount = 1;
466 if (serial)
467 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
468 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
470 if (is_extboot) {
471 extboot_drive = dinfo;
474 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
476 switch(type) {
477 case IF_IDE:
478 case IF_SCSI:
479 case IF_XEN:
480 case IF_NONE:
481 switch(media) {
482 case MEDIA_DISK:
483 if (cyls != 0) {
484 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
485 bdrv_set_translation_hint(dinfo->bdrv, translation);
487 break;
488 case MEDIA_CDROM:
489 dinfo->media_cd = 1;
490 break;
492 break;
493 case IF_SD:
494 case IF_FLOPPY:
495 case IF_PFLASH:
496 case IF_MTD:
497 break;
498 case IF_VIRTIO:
499 /* add virtio block device */
500 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
501 qemu_opt_set(opts, "driver", "virtio-blk");
502 qemu_opt_set(opts, "drive", dinfo->id);
503 if (devaddr)
504 qemu_opt_set(opts, "addr", devaddr);
505 break;
506 default:
507 abort();
509 if (!file || !*file) {
510 return dinfo;
512 if (snapshot) {
513 /* always use cache=unsafe with snapshot */
514 bdrv_flags &= ~BDRV_O_CACHE_MASK;
515 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
518 if (media == MEDIA_CDROM) {
519 /* CDROM is fine for any interface, don't check. */
520 ro = 1;
521 } else if (ro == 1) {
522 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
523 error_report("readonly not supported by this bus type");
524 goto err;
528 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
530 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
531 if (ret < 0) {
532 error_report("could not open disk image %s: %s",
533 file, strerror(-ret));
534 goto err;
537 if (bdrv_key_required(dinfo->bdrv))
538 autostart = 0;
539 return dinfo;
541 err:
542 bdrv_delete(dinfo->bdrv);
543 g_free(dinfo->id);
544 QTAILQ_REMOVE(&drives, dinfo, next);
545 g_free(dinfo);
546 return NULL;
549 void do_commit(Monitor *mon, const QDict *qdict)
551 const char *device = qdict_get_str(qdict, "device");
552 BlockDriverState *bs;
554 if (!strcmp(device, "all")) {
555 bdrv_commit_all();
556 } else {
557 bs = bdrv_find(device);
558 if (!bs) {
559 qerror_report(QERR_DEVICE_NOT_FOUND, device);
560 return;
562 bdrv_commit(bs);
566 int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
568 const char *device = qdict_get_str(qdict, "device");
569 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
570 const char *format = qdict_get_try_str(qdict, "format");
571 BlockDriverState *bs;
572 BlockDriver *drv, *old_drv, *proto_drv;
573 int ret = 0;
574 int flags;
575 char old_filename[1024];
577 if (!filename) {
578 qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
579 ret = -1;
580 goto out;
583 bs = bdrv_find(device);
584 if (!bs) {
585 qerror_report(QERR_DEVICE_NOT_FOUND, device);
586 ret = -1;
587 goto out;
590 pstrcpy(old_filename, sizeof(old_filename), bs->filename);
592 old_drv = bs->drv;
593 flags = bs->open_flags;
595 if (!format) {
596 format = "qcow2";
599 drv = bdrv_find_format(format);
600 if (!drv) {
601 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
602 ret = -1;
603 goto out;
606 proto_drv = bdrv_find_protocol(filename);
607 if (!proto_drv) {
608 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
609 ret = -1;
610 goto out;
613 ret = bdrv_img_create(filename, format, bs->filename,
614 bs->drv->format_name, NULL, -1, flags);
615 if (ret) {
616 goto out;
619 qemu_aio_flush();
620 bdrv_flush(bs);
622 bdrv_close(bs);
623 ret = bdrv_open(bs, filename, flags, drv);
625 * If reopening the image file we just created fails, fall back
626 * and try to re-open the original image. If that fails too, we
627 * are in serious trouble.
629 if (ret != 0) {
630 ret = bdrv_open(bs, old_filename, flags, old_drv);
631 if (ret != 0) {
632 qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
633 } else {
634 qerror_report(QERR_OPEN_FILE_FAILED, filename);
637 out:
638 if (ret) {
639 ret = -1;
642 return ret;
645 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
647 if (!bdrv_dev_has_removable_media(bs)) {
648 qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
649 return -1;
651 if (!force && !bdrv_dev_is_tray_open(bs)
652 && bdrv_dev_is_medium_locked(bs)) {
653 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
654 return -1;
656 bdrv_close(bs);
657 return 0;
660 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
662 BlockDriverState *bs;
663 int force = qdict_get_try_bool(qdict, "force", 0);
664 const char *filename = qdict_get_str(qdict, "device");
666 bs = bdrv_find(filename);
667 if (!bs) {
668 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
669 return -1;
671 return eject_device(mon, bs, force);
674 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
675 QObject **ret_data)
677 BlockDriverState *bs;
678 int err;
680 bs = bdrv_find(qdict_get_str(qdict, "device"));
681 if (!bs) {
682 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
683 return -1;
686 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
687 if (err == -EINVAL) {
688 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
689 return -1;
690 } else if (err < 0) {
691 qerror_report(QERR_INVALID_PASSWORD);
692 return -1;
695 return 0;
698 int do_change_block(Monitor *mon, const char *device,
699 const char *filename, const char *fmt)
701 BlockDriverState *bs;
702 BlockDriver *drv = NULL;
703 int bdrv_flags;
705 bs = bdrv_find(device);
706 if (!bs) {
707 qerror_report(QERR_DEVICE_NOT_FOUND, device);
708 return -1;
710 if (fmt) {
711 drv = bdrv_find_whitelisted_format(fmt);
712 if (!drv) {
713 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
714 return -1;
717 if (eject_device(mon, bs, 0) < 0) {
718 return -1;
720 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
721 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
722 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
723 qerror_report(QERR_OPEN_FILE_FAILED, filename);
724 return -1;
726 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
729 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
731 const char *id = qdict_get_str(qdict, "id");
732 BlockDriverState *bs;
734 bs = bdrv_find(id);
735 if (!bs) {
736 qerror_report(QERR_DEVICE_NOT_FOUND, id);
737 return -1;
739 if (bdrv_in_use(bs)) {
740 qerror_report(QERR_DEVICE_IN_USE, id);
741 return -1;
744 /* quiesce block driver; prevent further io */
745 qemu_aio_flush();
746 bdrv_flush(bs);
747 bdrv_close(bs);
749 /* if we have a device attached to this BlockDriverState
750 * then we need to make the drive anonymous until the device
751 * can be removed. If this is a drive with no device backing
752 * then we can just get rid of the block driver state right here.
754 if (bdrv_get_attached_dev(bs)) {
755 bdrv_make_anon(bs);
756 } else {
757 drive_uninit(drive_get_by_blockdev(bs));
760 return 0;
764 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
765 * existing QERR_ macro mess is cleaned up. A good example for better
766 * error reports can be found in the qemu-img resize code.
768 int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
770 const char *device = qdict_get_str(qdict, "device");
771 int64_t size = qdict_get_int(qdict, "size");
772 BlockDriverState *bs;
774 bs = bdrv_find(device);
775 if (!bs) {
776 qerror_report(QERR_DEVICE_NOT_FOUND, device);
777 return -1;
780 if (size < 0) {
781 qerror_report(QERR_UNDEFINED_ERROR);
782 return -1;
785 if (bdrv_truncate(bs, size)) {
786 qerror_report(QERR_UNDEFINED_ERROR);
787 return -1;
790 return 0;