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"
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
] = {
26 [IF_FLOPPY
] = "floppy",
27 [IF_PFLASH
] = "pflash",
30 [IF_VIRTIO
] = "virtio",
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
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.
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
);
69 void blockdev_auto_del(BlockDriverState
*bs
)
71 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
73 if (dinfo
&& dinfo
->auto_del
) {
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
,
104 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
107 opts
= drive_def(optstr
);
111 if (type
!= IF_DEFAULT
) {
112 qemu_opt_set(opts
, "if", if_name
[type
]);
115 snprintf(buf
, sizeof(buf
), "%d", index
);
116 qemu_opt_set(opts
, "index", buf
);
119 qemu_opt_set(opts
, "file", file
);
123 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
127 /* seek interface, bus and unit */
129 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
130 if (dinfo
->type
== type
&&
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
)
152 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
153 if(dinfo
->type
== type
&&
154 dinfo
->bus
> max_bus
)
155 max_bus
= dinfo
->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
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
)
174 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
175 if (dinfo
->bdrv
== bs
) {
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
);
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
;
206 error_report("'%s' invalid %s error action",
207 buf
, is_read
? "read" : "write");
212 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
, int *fatal_error
)
215 const char *file
= NULL
;
218 const char *mediastr
= "";
219 BlockInterfaceType type
;
220 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
222 int cyls
, heads
, secs
, translation
;
223 BlockDriver
*drv
= NULL
;
228 int on_read_error
, on_write_error
;
236 translation
= BIOS_ATA_TRANSLATION_AUTO
;
238 if (default_to_scsi
) {
240 pstrcpy(devname
, sizeof(devname
), "scsi");
243 pstrcpy(devname
, sizeof(devname
), "ide");
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 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");
278 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
279 error_report("invalid physical heads number");
282 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
283 error_report("invalid physical secs number");
288 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
290 error_report("'%s' trans must be used with cyls,heads and secs",
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
;
301 error_report("'%s' invalid translation type", buf
);
306 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
307 if (!strcmp(buf
, "disk")) {
309 } else if (!strcmp(buf
, "cdrom")) {
310 if (cyls
|| secs
|| heads
) {
311 error_report("'%s' invalid physical CHS format", buf
);
316 error_report("'%s' invalid media", buf
);
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 */
332 error_report("invalid cache option");
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 */
344 error_report("invalid aio option");
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
);
357 drv
= bdrv_find_whitelisted_format(buf
);
359 error_report("'%s' invalid format", buf
);
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");
371 on_write_error
= parse_block_error_action(buf
, 0);
372 if (on_write_error
< 0) {
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");
384 on_read_error
= parse_block_error_action(buf
, 1);
385 if (on_read_error
< 0) {
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");
397 /* compute bus and unit according index */
400 if (bus_id
!= 0 || unit_id
!= -1) {
401 error_report("index cannot be used with bus and unit");
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
414 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
416 if (max_devs
&& unit_id
>= max_devs
) {
425 if (max_devs
&& unit_id
>= max_devs
) {
426 error_report("unit %d too big (max is %d)",
427 unit_id
, max_devs
- 1);
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
);
443 dinfo
= qemu_mallocz(sizeof(*dinfo
));
444 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
445 dinfo
->id
= qemu_strdup(buf
);
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";
452 snprintf(dinfo
->id
, 32, "%s%i%s%i",
453 devname
, bus_id
, mediastr
, unit_id
);
455 snprintf(dinfo
->id
, 32, "%s%s%i",
456 devname
, mediastr
, unit_id
);
458 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
459 dinfo
->devaddr
= devaddr
;
462 dinfo
->unit
= unit_id
;
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
);
478 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
479 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
483 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
488 /* FIXME: This isn't really a floppy, but it's a reasonable
491 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
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
);
502 qemu_opt_set(opts
, "addr", devaddr
);
507 if (!file
|| !*file
) {
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. */
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");
527 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
529 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
531 error_report("could not open disk image %s: %s",
532 file
, strerror(-ret
));
536 if (bdrv_key_required(dinfo
->bdrv
))
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")) {
550 bs
= bdrv_find(device
);
552 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
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
;
570 qerror_report(QERR_MISSING_PARAMETER
, "snapshot_file");
575 bs
= bdrv_find(device
);
577 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
586 drv
= bdrv_find_format(format
);
588 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
593 proto_drv
= bdrv_find_protocol(filename
);
595 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
600 ret
= bdrv_img_create(filename
, format
, bs
->filename
,
601 bs
->drv
->format_name
, NULL
, -1, bs
->open_flags
);
609 flags
= bs
->open_flags
;
611 ret
= bdrv_open(bs
, filename
, flags
, drv
);
613 * If reopening the image file we just created fails, we really
627 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
630 if (!bdrv_is_removable(bs
)) {
631 qerror_report(QERR_DEVICE_NOT_REMOVABLE
,
632 bdrv_get_device_name(bs
));
635 if (bdrv_is_locked(bs
)) {
636 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
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
);
652 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
655 return eject_device(mon
, bs
, force
);
658 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
661 BlockDriverState
*bs
;
664 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
666 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
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
));
674 } else if (err
< 0) {
675 qerror_report(QERR_INVALID_PASSWORD
);
682 int do_change_block(Monitor
*mon
, const char *device
,
683 const char *filename
, const char *fmt
)
685 BlockDriverState
*bs
;
686 BlockDriver
*drv
= NULL
;
689 bs
= bdrv_find(device
);
691 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
695 drv
= bdrv_find_whitelisted_format(fmt
);
697 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
701 if (eject_device(mon
, bs
, 0) < 0) {
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
);
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
;
722 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
726 /* quiesce block driver; prevent further io */
731 /* clean up guest state from pointing to host resource by
732 * finding and removing DeviceState "drive" property */
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
);
738 bdrv_detach(bs
, bs
->peer
);
746 /* clean up host side */
747 drive_uninit(drive_get_by_blockdev(bs
));
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
);
765 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
770 qerror_report(QERR_UNDEFINED_ERROR
);
774 if (bdrv_truncate(bs
, size
)) {
775 qerror_report(QERR_UNDEFINED_ERROR
);