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 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
85 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
88 opts
= qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
93 qemu_opt_set(opts
, "file", file
);
97 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
101 /* seek interface, bus and unit */
103 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
104 if (dinfo
->type
== type
&&
113 int drive_get_max_bus(BlockInterfaceType type
)
119 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
120 if(dinfo
->type
== type
&&
121 dinfo
->bus
> max_bus
)
122 max_bus
= dinfo
->bus
;
127 /* Get a block device. This should only be used for single-drive devices
128 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
130 DriveInfo
*drive_get_next(BlockInterfaceType type
)
132 static int next_block_unit
[IF_COUNT
];
134 return drive_get(type
, 0, next_block_unit
[type
]++);
137 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
141 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
142 if (dinfo
->bdrv
== bs
) {
149 static void bdrv_format_print(void *opaque
, const char *name
)
151 error_printf(" %s", name
);
154 void drive_uninit(DriveInfo
*dinfo
)
156 qemu_opts_del(dinfo
->opts
);
157 bdrv_delete(dinfo
->bdrv
);
158 QTAILQ_REMOVE(&drives
, dinfo
, next
);
162 static int parse_block_error_action(const char *buf
, int is_read
)
164 if (!strcmp(buf
, "ignore")) {
165 return BLOCK_ERR_IGNORE
;
166 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
167 return BLOCK_ERR_STOP_ENOSPC
;
168 } else if (!strcmp(buf
, "stop")) {
169 return BLOCK_ERR_STOP_ANY
;
170 } else if (!strcmp(buf
, "report")) {
171 return BLOCK_ERR_REPORT
;
173 error_report("'%s' invalid %s error action",
174 buf
, is_read
? "read" : "write");
179 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
, int *fatal_error
)
182 const char *file
= NULL
;
185 const char *mediastr
= "";
186 BlockInterfaceType type
;
187 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
189 int cyls
, heads
, secs
, translation
;
190 BlockDriver
*drv
= NULL
;
195 int on_read_error
, on_write_error
;
203 translation
= BIOS_ATA_TRANSLATION_AUTO
;
205 if (default_to_scsi
) {
207 pstrcpy(devname
, sizeof(devname
), "scsi");
210 pstrcpy(devname
, sizeof(devname
), "ide");
214 /* extract parameters */
215 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
216 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
217 index
= qemu_opt_get_number(opts
, "index", -1);
219 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
220 heads
= qemu_opt_get_number(opts
, "heads", 0);
221 secs
= qemu_opt_get_number(opts
, "secs", 0);
223 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
224 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
226 file
= qemu_opt_get(opts
, "file");
227 serial
= qemu_opt_get(opts
, "serial");
229 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
230 pstrcpy(devname
, sizeof(devname
), buf
);
231 for (type
= 0; type
< IF_COUNT
&& strcmp(buf
, if_name
[type
]); type
++)
233 if (type
== IF_COUNT
) {
234 error_report("unsupported bus type '%s'", buf
);
238 max_devs
= if_max_devs
[type
];
240 if (cyls
|| heads
|| secs
) {
241 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
242 error_report("invalid physical cyls number");
245 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
246 error_report("invalid physical heads number");
249 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
250 error_report("invalid physical secs number");
255 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
257 error_report("'%s' trans must be used with cyls,heads and secs",
261 if (!strcmp(buf
, "none"))
262 translation
= BIOS_ATA_TRANSLATION_NONE
;
263 else if (!strcmp(buf
, "lba"))
264 translation
= BIOS_ATA_TRANSLATION_LBA
;
265 else if (!strcmp(buf
, "auto"))
266 translation
= BIOS_ATA_TRANSLATION_AUTO
;
268 error_report("'%s' invalid translation type", buf
);
273 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
274 if (!strcmp(buf
, "disk")) {
276 } else if (!strcmp(buf
, "cdrom")) {
277 if (cyls
|| secs
|| heads
) {
278 error_report("'%s' invalid physical CHS format", buf
);
283 error_report("'%s' invalid media", buf
);
288 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
289 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
290 bdrv_flags
|= BDRV_O_NOCACHE
;
291 } else if (!strcmp(buf
, "writeback")) {
292 bdrv_flags
|= BDRV_O_CACHE_WB
;
293 } else if (!strcmp(buf
, "unsafe")) {
294 bdrv_flags
|= BDRV_O_CACHE_WB
;
295 bdrv_flags
|= BDRV_O_NO_FLUSH
;
296 } else if (!strcmp(buf
, "writethrough")) {
297 /* this is the default */
299 error_report("invalid cache option");
304 #ifdef CONFIG_LINUX_AIO
305 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
306 if (!strcmp(buf
, "native")) {
307 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
308 } else if (!strcmp(buf
, "threads")) {
309 /* this is the default */
311 error_report("invalid aio option");
317 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
318 if (strcmp(buf
, "?") == 0) {
319 error_printf("Supported formats:");
320 bdrv_iterate_format(bdrv_format_print
, NULL
);
324 drv
= bdrv_find_whitelisted_format(buf
);
326 error_report("'%s' invalid format", buf
);
331 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
332 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
333 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
334 error_report("werror is not supported by this bus type");
338 on_write_error
= parse_block_error_action(buf
, 0);
339 if (on_write_error
< 0) {
344 on_read_error
= BLOCK_ERR_REPORT
;
345 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
346 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
347 error_report("rerror is not supported by this bus type");
351 on_read_error
= parse_block_error_action(buf
, 1);
352 if (on_read_error
< 0) {
357 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
358 if (type
!= IF_VIRTIO
) {
359 error_report("addr is not supported by this bus type");
364 /* compute bus and unit according index */
367 if (bus_id
!= 0 || unit_id
!= -1) {
368 error_report("index cannot be used with bus and unit");
376 unit_id
= index
% max_devs
;
377 bus_id
= index
/ max_devs
;
381 /* if user doesn't specify a unit_id,
382 * try to find the first free
387 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
389 if (max_devs
&& unit_id
>= max_devs
) {
398 if (max_devs
&& unit_id
>= max_devs
) {
399 error_report("unit %d too big (max is %d)",
400 unit_id
, max_devs
- 1);
405 * ignore multiple definitions
408 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
415 dinfo
= qemu_mallocz(sizeof(*dinfo
));
416 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
417 dinfo
->id
= qemu_strdup(buf
);
419 /* no id supplied -> create one */
420 dinfo
->id
= qemu_mallocz(32);
421 if (type
== IF_IDE
|| type
== IF_SCSI
)
422 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
424 snprintf(dinfo
->id
, 32, "%s%i%s%i",
425 devname
, bus_id
, mediastr
, unit_id
);
427 snprintf(dinfo
->id
, 32, "%s%s%i",
428 devname
, mediastr
, unit_id
);
430 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
431 dinfo
->devaddr
= devaddr
;
434 dinfo
->unit
= unit_id
;
437 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
438 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
440 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
450 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
451 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
455 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
460 /* FIXME: This isn't really a floppy, but it's a reasonable
463 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
469 /* add virtio block device */
470 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
471 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
472 qemu_opt_set(opts
, "drive", dinfo
->id
);
474 qemu_opt_set(opts
, "addr", devaddr
);
479 if (!file
|| !*file
) {
484 /* always use cache=unsafe with snapshot */
485 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
486 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
489 if (media
== MEDIA_CDROM
) {
490 /* CDROM is fine for any interface, don't check. */
492 } else if (ro
== 1) {
493 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
494 error_report("readonly not supported by this bus type");
499 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
501 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
503 error_report("could not open disk image %s: %s",
504 file
, strerror(-ret
));
508 if (bdrv_key_required(dinfo
->bdrv
))
514 void do_commit(Monitor
*mon
, const QDict
*qdict
)
516 const char *device
= qdict_get_str(qdict
, "device");
517 BlockDriverState
*bs
;
519 if (!strcmp(device
, "all")) {
522 bs
= bdrv_find(device
);
524 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
531 int do_snapshot_blkdev(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
533 const char *device
= qdict_get_str(qdict
, "device");
534 const char *filename
= qdict_get_try_str(qdict
, "snapshot_file");
535 const char *format
= qdict_get_try_str(qdict
, "format");
536 BlockDriverState
*bs
;
537 BlockDriver
*drv
, *proto_drv
;
542 qerror_report(QERR_MISSING_PARAMETER
, "snapshot_file");
547 bs
= bdrv_find(device
);
549 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
558 drv
= bdrv_find_format(format
);
560 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
565 proto_drv
= bdrv_find_protocol(filename
);
567 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
572 ret
= bdrv_img_create(filename
, format
, bs
->filename
,
573 bs
->drv
->format_name
, NULL
, -1, bs
->open_flags
);
581 flags
= bs
->open_flags
;
583 ret
= bdrv_open(bs
, filename
, flags
, drv
);
585 * If reopening the image file we just created fails, we really
599 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
602 if (!bdrv_is_removable(bs
)) {
603 qerror_report(QERR_DEVICE_NOT_REMOVABLE
,
604 bdrv_get_device_name(bs
));
607 if (bdrv_is_locked(bs
)) {
608 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
616 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
618 BlockDriverState
*bs
;
619 int force
= qdict_get_try_bool(qdict
, "force", 0);
620 const char *filename
= qdict_get_str(qdict
, "device");
622 bs
= bdrv_find(filename
);
624 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
627 return eject_device(mon
, bs
, force
);
630 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
633 BlockDriverState
*bs
;
636 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
638 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
642 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
643 if (err
== -EINVAL
) {
644 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
646 } else if (err
< 0) {
647 qerror_report(QERR_INVALID_PASSWORD
);
654 int do_change_block(Monitor
*mon
, const char *device
,
655 const char *filename
, const char *fmt
)
657 BlockDriverState
*bs
;
658 BlockDriver
*drv
= NULL
;
661 bs
= bdrv_find(device
);
663 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
667 drv
= bdrv_find_whitelisted_format(fmt
);
669 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
673 if (eject_device(mon
, bs
, 0) < 0) {
676 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
677 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
678 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
679 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
682 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
685 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
687 const char *id
= qdict_get_str(qdict
, "id");
688 BlockDriverState
*bs
;
689 BlockDriverState
**ptr
;
694 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
698 /* quiesce block driver; prevent further io */
703 /* clean up guest state from pointing to host resource by
704 * finding and removing DeviceState "drive" property */
706 for (prop
= bs
->peer
->info
->props
; prop
&& prop
->name
; prop
++) {
707 if (prop
->info
->type
== PROP_TYPE_DRIVE
) {
708 ptr
= qdev_get_prop_ptr(bs
->peer
, prop
);
710 bdrv_detach(bs
, bs
->peer
);
718 /* clean up host side */
719 drive_uninit(drive_get_by_blockdev(bs
));
725 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
726 * existing QERR_ macro mess is cleaned up. A good example for better
727 * error reports can be found in the qemu-img resize code.
729 int do_block_resize(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
731 const char *device
= qdict_get_str(qdict
, "device");
732 int64_t size
= qdict_get_int(qdict
, "size");
733 BlockDriverState
*bs
;
735 bs
= bdrv_find(device
);
737 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
742 qerror_report(QERR_UNDEFINED_ERROR
);
746 if (bdrv_truncate(bs
, size
)) {
747 qerror_report(QERR_UNDEFINED_ERROR
);