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 DriveInfo
*extboot_drive
= NULL
;
21 static QTAILQ_HEAD(drivelist
, DriveInfo
) drives
= QTAILQ_HEAD_INITIALIZER(drives
);
23 static const char *const if_name
[IF_COUNT
] = {
27 [IF_FLOPPY
] = "floppy",
28 [IF_PFLASH
] = "pflash",
31 [IF_VIRTIO
] = "virtio",
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
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.
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
);
70 void blockdev_auto_del(BlockDriverState
*bs
)
72 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
74 if (dinfo
&& dinfo
->auto_del
) {
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
,
102 opts
= drive_def(optstr
);
106 if (type
!= IF_DEFAULT
) {
107 qemu_opt_set(opts
, "if", if_name
[type
]);
110 snprintf(buf
, sizeof(buf
), "%d", index
);
111 qemu_opt_set(opts
, "index", buf
);
114 qemu_opt_set(opts
, "file", file
);
118 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
122 /* seek interface, bus and unit */
124 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
125 if (dinfo
->type
== type
&&
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
)
147 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
148 if(dinfo
->type
== type
&&
149 dinfo
->bus
> max_bus
)
150 max_bus
= dinfo
->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
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
)
169 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
170 if (dinfo
->bdrv
== bs
) {
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
);
187 QTAILQ_REMOVE(&drives
, dinfo
, next
);
191 void drive_put_ref(DriveInfo
*dinfo
)
193 assert(dinfo
->refcount
);
194 if (--dinfo
->refcount
== 0) {
199 void drive_get_ref(DriveInfo
*dinfo
)
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
;
215 error_report("'%s' invalid %s error action",
216 buf
, is_read
? "read" : "write");
221 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
)
224 const char *file
= NULL
;
227 const char *mediastr
= "";
228 BlockInterfaceType type
;
229 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
231 int cyls
, heads
, secs
, translation
;
232 BlockDriver
*drv
= NULL
;
237 int on_read_error
, on_write_error
;
244 translation
= BIOS_ATA_TRANSLATION_AUTO
;
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
);
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");
282 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
283 error_report("invalid physical heads number");
286 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
287 error_report("invalid physical secs number");
292 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
294 error_report("'%s' trans must be used with cyls, heads and secs",
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
;
305 error_report("'%s' invalid translation type", buf
);
310 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
311 if (!strcmp(buf
, "disk")) {
313 } else if (!strcmp(buf
, "cdrom")) {
314 if (cyls
|| secs
|| heads
) {
315 error_report("CHS can't be set with media=%s", buf
);
320 error_report("'%s' invalid media", buf
);
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");
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 */
339 error_report("invalid aio option");
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
);
352 drv
= bdrv_find_whitelisted_format(buf
);
354 error_report("'%s' invalid format", buf
);
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");
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");
372 on_write_error
= parse_block_error_action(buf
, 0);
373 if (on_write_error
< 0) {
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");
385 on_read_error
= parse_block_error_action(buf
, 1);
386 if (on_read_error
< 0) {
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");
398 /* compute bus and unit according index */
401 if (bus_id
!= 0 || unit_id
!= -1) {
402 error_report("index cannot be used with bus and unit");
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
415 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
417 if (max_devs
&& unit_id
>= max_devs
) {
426 if (max_devs
&& unit_id
>= max_devs
) {
427 error_report("unit %d too big (max is %d)",
428 unit_id
, max_devs
- 1);
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
);
444 dinfo
= g_malloc0(sizeof(*dinfo
));
445 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
446 dinfo
->id
= g_strdup(buf
);
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";
453 snprintf(dinfo
->id
, 32, "%s%i%s%i",
454 devname
, bus_id
, mediastr
, unit_id
);
456 snprintf(dinfo
->id
, 32, "%s%s%i",
457 devname
, mediastr
, unit_id
);
459 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
460 dinfo
->devaddr
= devaddr
;
463 dinfo
->unit
= unit_id
;
467 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
468 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
471 extboot_drive
= dinfo
;
474 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
484 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
485 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
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
);
504 qemu_opt_set(opts
, "addr", devaddr
);
509 if (!file
|| !*file
) {
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. */
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");
528 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
530 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
532 error_report("could not open disk image %s: %s",
533 file
, strerror(-ret
));
537 if (bdrv_key_required(dinfo
->bdrv
))
542 bdrv_delete(dinfo
->bdrv
);
544 QTAILQ_REMOVE(&drives
, dinfo
, next
);
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")) {
557 bs
= bdrv_find(device
);
559 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
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
;
575 char old_filename
[1024];
578 qerror_report(QERR_MISSING_PARAMETER
, "snapshot-file");
583 bs
= bdrv_find(device
);
585 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
590 pstrcpy(old_filename
, sizeof(old_filename
), bs
->filename
);
593 flags
= bs
->open_flags
;
599 drv
= bdrv_find_format(format
);
601 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
606 proto_drv
= bdrv_find_protocol(filename
);
608 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
613 ret
= bdrv_img_create(filename
, format
, bs
->filename
,
614 bs
->drv
->format_name
, NULL
, -1, flags
);
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.
630 ret
= bdrv_open(bs
, old_filename
, flags
, old_drv
);
632 qerror_report(QERR_OPEN_FILE_FAILED
, old_filename
);
634 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
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
));
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
));
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
);
668 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
671 return eject_device(mon
, bs
, force
);
674 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
677 BlockDriverState
*bs
;
680 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
682 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
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
));
690 } else if (err
< 0) {
691 qerror_report(QERR_INVALID_PASSWORD
);
698 int do_change_block(Monitor
*mon
, const char *device
,
699 const char *filename
, const char *fmt
)
701 BlockDriverState
*bs
;
702 BlockDriver
*drv
= NULL
;
705 bs
= bdrv_find(device
);
707 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
711 drv
= bdrv_find_whitelisted_format(fmt
);
713 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
717 if (eject_device(mon
, bs
, 0) < 0) {
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
);
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
;
736 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
739 if (bdrv_in_use(bs
)) {
740 qerror_report(QERR_DEVICE_IN_USE
, id
);
744 /* quiesce block driver; prevent further io */
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
)) {
757 drive_uninit(drive_get_by_blockdev(bs
));
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
);
776 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
781 qerror_report(QERR_UNDEFINED_ERROR
);
785 if (bdrv_truncate(bs
, size
)) {
786 qerror_report(QERR_UNDEFINED_ERROR
);