Fix build from previous commit
[qemu.git] / blockdev.c
blobecfadc138fc34808d7492ac1a47eef964677a641
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_put_ref(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 *optstr)
98 QemuOpts *opts;
99 char buf[32];
101 opts = drive_def(optstr);
102 if (!opts) {
103 return NULL;
105 if (type != IF_DEFAULT) {
106 qemu_opt_set(opts, "if", if_name[type]);
108 if (index >= 0) {
109 snprintf(buf, sizeof(buf), "%d", index);
110 qemu_opt_set(opts, "index", buf);
112 if (file)
113 qemu_opt_set(opts, "file", file);
114 return opts;
117 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
119 DriveInfo *dinfo;
121 /* seek interface, bus and unit */
123 QTAILQ_FOREACH(dinfo, &drives, next) {
124 if (dinfo->type == type &&
125 dinfo->bus == bus &&
126 dinfo->unit == unit)
127 return dinfo;
130 return NULL;
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)
142 int max_bus;
143 DriveInfo *dinfo;
145 max_bus = -1;
146 QTAILQ_FOREACH(dinfo, &drives, next) {
147 if(dinfo->type == type &&
148 dinfo->bus > max_bus)
149 max_bus = dinfo->bus;
151 return max_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
156 appropriate bus. */
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)
166 DriveInfo *dinfo;
168 QTAILQ_FOREACH(dinfo, &drives, next) {
169 if (dinfo->bdrv == bs) {
170 return dinfo;
173 return NULL;
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);
185 QTAILQ_REMOVE(&drives, dinfo, next);
186 qemu_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;
243 if (default_to_scsi) {
244 type = IF_SCSI;
245 pstrcpy(devname, sizeof(devname), "scsi");
246 } else {
247 type = IF_IDE;
248 pstrcpy(devname, sizeof(devname), "ide");
250 media = MEDIA_DISK;
252 /* extract parameters */
253 bus_id = qemu_opt_get_number(opts, "bus", 0);
254 unit_id = qemu_opt_get_number(opts, "unit", -1);
255 index = qemu_opt_get_number(opts, "index", -1);
257 cyls = qemu_opt_get_number(opts, "cyls", 0);
258 heads = qemu_opt_get_number(opts, "heads", 0);
259 secs = qemu_opt_get_number(opts, "secs", 0);
261 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
262 ro = qemu_opt_get_bool(opts, "readonly", 0);
264 file = qemu_opt_get(opts, "file");
265 serial = qemu_opt_get(opts, "serial");
267 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
268 pstrcpy(devname, sizeof(devname), buf);
269 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
271 if (type == IF_COUNT) {
272 error_report("unsupported bus type '%s'", buf);
273 return NULL;
276 max_devs = if_max_devs[type];
278 if (cyls || heads || secs) {
279 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
280 error_report("invalid physical cyls number");
281 return NULL;
283 if (heads < 1 || (type == IF_IDE && heads > 16)) {
284 error_report("invalid physical heads number");
285 return NULL;
287 if (secs < 1 || (type == IF_IDE && secs > 63)) {
288 error_report("invalid physical secs number");
289 return NULL;
293 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
294 if (!cyls) {
295 error_report("'%s' trans must be used with cyls,heads and secs",
296 buf);
297 return NULL;
299 if (!strcmp(buf, "none"))
300 translation = BIOS_ATA_TRANSLATION_NONE;
301 else if (!strcmp(buf, "lba"))
302 translation = BIOS_ATA_TRANSLATION_LBA;
303 else if (!strcmp(buf, "auto"))
304 translation = BIOS_ATA_TRANSLATION_AUTO;
305 else {
306 error_report("'%s' invalid translation type", buf);
307 return NULL;
311 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
312 if (!strcmp(buf, "disk")) {
313 media = MEDIA_DISK;
314 } else if (!strcmp(buf, "cdrom")) {
315 if (cyls || secs || heads) {
316 error_report("'%s' invalid physical CHS format", buf);
317 return NULL;
319 media = MEDIA_CDROM;
320 } else {
321 error_report("'%s' invalid media", buf);
322 return NULL;
326 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
327 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
328 bdrv_flags |= BDRV_O_NOCACHE;
329 } else if (!strcmp(buf, "writeback")) {
330 bdrv_flags |= BDRV_O_CACHE_WB;
331 } else if (!strcmp(buf, "unsafe")) {
332 bdrv_flags |= BDRV_O_CACHE_WB;
333 bdrv_flags |= BDRV_O_NO_FLUSH;
334 } else if (!strcmp(buf, "writethrough")) {
335 /* this is the default */
336 } else {
337 error_report("invalid cache option");
338 return NULL;
342 #ifdef CONFIG_LINUX_AIO
343 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
344 if (!strcmp(buf, "native")) {
345 bdrv_flags |= BDRV_O_NATIVE_AIO;
346 } else if (!strcmp(buf, "threads")) {
347 /* this is the default */
348 } else {
349 error_report("invalid aio option");
350 return NULL;
353 #endif
355 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
356 if (strcmp(buf, "?") == 0) {
357 error_printf("Supported formats:");
358 bdrv_iterate_format(bdrv_format_print, NULL);
359 error_printf("\n");
360 return NULL;
362 drv = bdrv_find_whitelisted_format(buf);
363 if (!drv) {
364 error_report("'%s' invalid format", buf);
365 return NULL;
369 on_write_error = BLOCK_ERR_STOP_ENOSPC;
370 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
371 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
372 error_report("werror is not supported by this bus type");
373 return NULL;
376 on_write_error = parse_block_error_action(buf, 0);
377 if (on_write_error < 0) {
378 return NULL;
382 on_read_error = BLOCK_ERR_REPORT;
383 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
384 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
385 error_report("rerror is not supported by this bus type");
386 return NULL;
389 on_read_error = parse_block_error_action(buf, 1);
390 if (on_read_error < 0) {
391 return NULL;
395 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
396 if (type != IF_VIRTIO) {
397 error_report("addr is not supported by this bus type");
398 return NULL;
402 /* compute bus and unit according index */
404 if (index != -1) {
405 if (bus_id != 0 || unit_id != -1) {
406 error_report("index cannot be used with bus and unit");
407 return NULL;
409 bus_id = drive_index_to_bus_id(type, index);
410 unit_id = drive_index_to_unit_id(type, index);
413 /* if user doesn't specify a unit_id,
414 * try to find the first free
417 if (unit_id == -1) {
418 unit_id = 0;
419 while (drive_get(type, bus_id, unit_id) != NULL) {
420 unit_id++;
421 if (max_devs && unit_id >= max_devs) {
422 unit_id -= max_devs;
423 bus_id++;
428 /* check unit id */
430 if (max_devs && unit_id >= max_devs) {
431 error_report("unit %d too big (max is %d)",
432 unit_id, max_devs - 1);
433 return NULL;
437 * catch multiple definitions
440 if (drive_get(type, bus_id, unit_id) != NULL) {
441 error_report("drive with bus=%d, unit=%d (index=%d) exists",
442 bus_id, unit_id, index);
443 return NULL;
446 /* init */
448 dinfo = qemu_mallocz(sizeof(*dinfo));
449 if ((buf = qemu_opts_id(opts)) != NULL) {
450 dinfo->id = qemu_strdup(buf);
451 } else {
452 /* no id supplied -> create one */
453 dinfo->id = qemu_mallocz(32);
454 if (type == IF_IDE || type == IF_SCSI)
455 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
456 if (max_devs)
457 snprintf(dinfo->id, 32, "%s%i%s%i",
458 devname, bus_id, mediastr, unit_id);
459 else
460 snprintf(dinfo->id, 32, "%s%s%i",
461 devname, mediastr, unit_id);
463 dinfo->bdrv = bdrv_new(dinfo->id);
464 dinfo->devaddr = devaddr;
465 dinfo->type = type;
466 dinfo->bus = bus_id;
467 dinfo->unit = unit_id;
468 dinfo->opts = opts;
469 dinfo->refcount = 1;
470 if (serial)
471 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
472 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
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 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
490 break;
492 break;
493 case IF_SD:
494 /* FIXME: This isn't really a floppy, but it's a reasonable
495 approximation. */
496 case IF_FLOPPY:
497 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
498 break;
499 case IF_PFLASH:
500 case IF_MTD:
501 break;
502 case IF_VIRTIO:
503 /* add virtio block device */
504 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
505 qemu_opt_set(opts, "driver", "virtio-blk-pci");
506 qemu_opt_set(opts, "drive", dinfo->id);
507 if (devaddr)
508 qemu_opt_set(opts, "addr", devaddr);
509 break;
510 default:
511 abort();
513 if (!file || !*file) {
514 return dinfo;
516 if (snapshot) {
517 /* always use cache=unsafe with snapshot */
518 bdrv_flags &= ~BDRV_O_CACHE_MASK;
519 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
522 if (media == MEDIA_CDROM) {
523 /* CDROM is fine for any interface, don't check. */
524 ro = 1;
525 } else if (ro == 1) {
526 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
527 error_report("readonly not supported by this bus type");
528 return NULL;
532 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
534 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
535 if (ret < 0) {
536 error_report("could not open disk image %s: %s",
537 file, strerror(-ret));
538 return NULL;
541 if (bdrv_key_required(dinfo->bdrv))
542 autostart = 0;
543 return dinfo;
546 void do_commit(Monitor *mon, const QDict *qdict)
548 const char *device = qdict_get_str(qdict, "device");
549 BlockDriverState *bs;
551 if (!strcmp(device, "all")) {
552 bdrv_commit_all();
553 } else {
554 bs = bdrv_find(device);
555 if (!bs) {
556 qerror_report(QERR_DEVICE_NOT_FOUND, device);
557 return;
559 bdrv_commit(bs);
563 int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
565 const char *device = qdict_get_str(qdict, "device");
566 const char *filename = qdict_get_try_str(qdict, "snapshot_file");
567 const char *format = qdict_get_try_str(qdict, "format");
568 BlockDriverState *bs;
569 BlockDriver *drv, *proto_drv;
570 int ret = 0;
571 int flags;
573 if (!filename) {
574 qerror_report(QERR_MISSING_PARAMETER, "snapshot_file");
575 ret = -1;
576 goto out;
579 bs = bdrv_find(device);
580 if (!bs) {
581 qerror_report(QERR_DEVICE_NOT_FOUND, device);
582 ret = -1;
583 goto out;
586 if (!format) {
587 format = "qcow2";
590 drv = bdrv_find_format(format);
591 if (!drv) {
592 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
593 ret = -1;
594 goto out;
597 proto_drv = bdrv_find_protocol(filename);
598 if (!proto_drv) {
599 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
600 ret = -1;
601 goto out;
604 ret = bdrv_img_create(filename, format, bs->filename,
605 bs->drv->format_name, NULL, -1, bs->open_flags);
606 if (ret) {
607 goto out;
610 qemu_aio_flush();
611 bdrv_flush(bs);
613 flags = bs->open_flags;
614 bdrv_close(bs);
615 ret = bdrv_open(bs, filename, flags, drv);
617 * If reopening the image file we just created fails, we really
618 * are in trouble :(
620 if (ret != 0) {
621 abort();
623 out:
624 if (ret) {
625 ret = -1;
628 return ret;
631 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
633 if (!force) {
634 if (!bdrv_is_removable(bs)) {
635 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
636 bdrv_get_device_name(bs));
637 return -1;
639 if (bdrv_is_locked(bs)) {
640 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
641 return -1;
644 bdrv_close(bs);
645 return 0;
648 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
650 BlockDriverState *bs;
651 int force = qdict_get_try_bool(qdict, "force", 0);
652 const char *filename = qdict_get_str(qdict, "device");
654 bs = bdrv_find(filename);
655 if (!bs) {
656 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
657 return -1;
659 return eject_device(mon, bs, force);
662 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
663 QObject **ret_data)
665 BlockDriverState *bs;
666 int err;
668 bs = bdrv_find(qdict_get_str(qdict, "device"));
669 if (!bs) {
670 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
671 return -1;
674 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
675 if (err == -EINVAL) {
676 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
677 return -1;
678 } else if (err < 0) {
679 qerror_report(QERR_INVALID_PASSWORD);
680 return -1;
683 return 0;
686 int do_change_block(Monitor *mon, const char *device,
687 const char *filename, const char *fmt)
689 BlockDriverState *bs;
690 BlockDriver *drv = NULL;
691 int bdrv_flags;
693 bs = bdrv_find(device);
694 if (!bs) {
695 qerror_report(QERR_DEVICE_NOT_FOUND, device);
696 return -1;
698 if (fmt) {
699 drv = bdrv_find_whitelisted_format(fmt);
700 if (!drv) {
701 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
702 return -1;
705 if (eject_device(mon, bs, 0) < 0) {
706 return -1;
708 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
709 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
710 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
711 qerror_report(QERR_OPEN_FILE_FAILED, filename);
712 return -1;
714 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
717 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
719 const char *id = qdict_get_str(qdict, "id");
720 BlockDriverState *bs;
721 BlockDriverState **ptr;
722 Property *prop;
724 bs = bdrv_find(id);
725 if (!bs) {
726 qerror_report(QERR_DEVICE_NOT_FOUND, id);
727 return -1;
729 if (bdrv_in_use(bs)) {
730 qerror_report(QERR_DEVICE_IN_USE, id);
731 return -1;
734 /* quiesce block driver; prevent further io */
735 qemu_aio_flush();
736 bdrv_flush(bs);
737 bdrv_close(bs);
739 /* clean up guest state from pointing to host resource by
740 * finding and removing DeviceState "drive" property */
741 if (bs->peer) {
742 for (prop = bs->peer->info->props; prop && prop->name; prop++) {
743 if (prop->info->type == PROP_TYPE_DRIVE) {
744 ptr = qdev_get_prop_ptr(bs->peer, prop);
745 if (*ptr == bs) {
746 bdrv_detach(bs, bs->peer);
747 *ptr = NULL;
748 break;
754 /* clean up host side */
755 drive_uninit(drive_get_by_blockdev(bs));
757 return 0;
761 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
762 * existing QERR_ macro mess is cleaned up. A good example for better
763 * error reports can be found in the qemu-img resize code.
765 int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
767 const char *device = qdict_get_str(qdict, "device");
768 int64_t size = qdict_get_int(qdict, "size");
769 BlockDriverState *bs;
771 bs = bdrv_find(device);
772 if (!bs) {
773 qerror_report(QERR_DEVICE_NOT_FOUND, device);
774 return -1;
777 if (size < 0) {
778 qerror_report(QERR_UNDEFINED_ERROR);
779 return -1;
782 if (bdrv_truncate(bs, size)) {
783 qerror_report(QERR_UNDEFINED_ERROR);
784 return -1;
787 return 0;