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_def(const char *optstr
)
80 return qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
83 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
92 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
95 opts
= drive_def(optstr
);
99 if (type
!= IF_DEFAULT
) {
100 qemu_opt_set(opts
, "if", if_name
[type
]);
103 snprintf(buf
, sizeof(buf
), "%d", index
);
104 qemu_opt_set(opts
, "index", buf
);
107 qemu_opt_set(opts
, "file", file
);
111 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
115 /* seek interface, bus and unit */
117 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
118 if (dinfo
->type
== type
&&
127 int drive_get_max_bus(BlockInterfaceType type
)
133 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
134 if(dinfo
->type
== type
&&
135 dinfo
->bus
> max_bus
)
136 max_bus
= dinfo
->bus
;
141 /* Get a block device. This should only be used for single-drive devices
142 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
144 DriveInfo
*drive_get_next(BlockInterfaceType type
)
146 static int next_block_unit
[IF_COUNT
];
148 return drive_get(type
, 0, next_block_unit
[type
]++);
151 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
155 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
156 if (dinfo
->bdrv
== bs
) {
163 static void bdrv_format_print(void *opaque
, const char *name
)
165 error_printf(" %s", name
);
168 void drive_uninit(DriveInfo
*dinfo
)
170 qemu_opts_del(dinfo
->opts
);
171 bdrv_delete(dinfo
->bdrv
);
172 QTAILQ_REMOVE(&drives
, dinfo
, next
);
176 static int parse_block_error_action(const char *buf
, int is_read
)
178 if (!strcmp(buf
, "ignore")) {
179 return BLOCK_ERR_IGNORE
;
180 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
181 return BLOCK_ERR_STOP_ENOSPC
;
182 } else if (!strcmp(buf
, "stop")) {
183 return BLOCK_ERR_STOP_ANY
;
184 } else if (!strcmp(buf
, "report")) {
185 return BLOCK_ERR_REPORT
;
187 error_report("'%s' invalid %s error action",
188 buf
, is_read
? "read" : "write");
193 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
, int *fatal_error
)
196 const char *file
= NULL
;
199 const char *mediastr
= "";
200 BlockInterfaceType type
;
201 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
203 int cyls
, heads
, secs
, translation
;
204 BlockDriver
*drv
= NULL
;
209 int on_read_error
, on_write_error
;
217 translation
= BIOS_ATA_TRANSLATION_AUTO
;
219 if (default_to_scsi
) {
221 pstrcpy(devname
, sizeof(devname
), "scsi");
224 pstrcpy(devname
, sizeof(devname
), "ide");
228 /* extract parameters */
229 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
230 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
231 index
= qemu_opt_get_number(opts
, "index", -1);
233 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
234 heads
= qemu_opt_get_number(opts
, "heads", 0);
235 secs
= qemu_opt_get_number(opts
, "secs", 0);
237 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
238 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
240 file
= qemu_opt_get(opts
, "file");
241 serial
= qemu_opt_get(opts
, "serial");
243 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
244 pstrcpy(devname
, sizeof(devname
), buf
);
245 for (type
= 0; type
< IF_COUNT
&& strcmp(buf
, if_name
[type
]); type
++)
247 if (type
== IF_COUNT
) {
248 error_report("unsupported bus type '%s'", buf
);
252 max_devs
= if_max_devs
[type
];
254 if (cyls
|| heads
|| secs
) {
255 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
256 error_report("invalid physical cyls number");
259 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
260 error_report("invalid physical heads number");
263 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
264 error_report("invalid physical secs number");
269 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
271 error_report("'%s' trans must be used with cyls,heads and secs",
275 if (!strcmp(buf
, "none"))
276 translation
= BIOS_ATA_TRANSLATION_NONE
;
277 else if (!strcmp(buf
, "lba"))
278 translation
= BIOS_ATA_TRANSLATION_LBA
;
279 else if (!strcmp(buf
, "auto"))
280 translation
= BIOS_ATA_TRANSLATION_AUTO
;
282 error_report("'%s' invalid translation type", buf
);
287 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
288 if (!strcmp(buf
, "disk")) {
290 } else if (!strcmp(buf
, "cdrom")) {
291 if (cyls
|| secs
|| heads
) {
292 error_report("'%s' invalid physical CHS format", buf
);
297 error_report("'%s' invalid media", buf
);
302 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
303 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
304 bdrv_flags
|= BDRV_O_NOCACHE
;
305 } else if (!strcmp(buf
, "writeback")) {
306 bdrv_flags
|= BDRV_O_CACHE_WB
;
307 } else if (!strcmp(buf
, "unsafe")) {
308 bdrv_flags
|= BDRV_O_CACHE_WB
;
309 bdrv_flags
|= BDRV_O_NO_FLUSH
;
310 } else if (!strcmp(buf
, "writethrough")) {
311 /* this is the default */
313 error_report("invalid cache option");
318 #ifdef CONFIG_LINUX_AIO
319 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
320 if (!strcmp(buf
, "native")) {
321 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
322 } else if (!strcmp(buf
, "threads")) {
323 /* this is the default */
325 error_report("invalid aio option");
331 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
332 if (strcmp(buf
, "?") == 0) {
333 error_printf("Supported formats:");
334 bdrv_iterate_format(bdrv_format_print
, NULL
);
338 drv
= bdrv_find_whitelisted_format(buf
);
340 error_report("'%s' invalid format", buf
);
345 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
346 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
347 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
348 error_report("werror is not supported by this bus type");
352 on_write_error
= parse_block_error_action(buf
, 0);
353 if (on_write_error
< 0) {
358 on_read_error
= BLOCK_ERR_REPORT
;
359 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
360 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
361 error_report("rerror is not supported by this bus type");
365 on_read_error
= parse_block_error_action(buf
, 1);
366 if (on_read_error
< 0) {
371 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
372 if (type
!= IF_VIRTIO
) {
373 error_report("addr is not supported by this bus type");
378 /* compute bus and unit according index */
381 if (bus_id
!= 0 || unit_id
!= -1) {
382 error_report("index cannot be used with bus and unit");
390 unit_id
= index
% max_devs
;
391 bus_id
= index
/ max_devs
;
395 /* if user doesn't specify a unit_id,
396 * try to find the first free
401 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
403 if (max_devs
&& unit_id
>= max_devs
) {
412 if (max_devs
&& unit_id
>= max_devs
) {
413 error_report("unit %d too big (max is %d)",
414 unit_id
, max_devs
- 1);
419 * ignore multiple definitions
422 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
429 dinfo
= qemu_mallocz(sizeof(*dinfo
));
430 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
431 dinfo
->id
= qemu_strdup(buf
);
433 /* no id supplied -> create one */
434 dinfo
->id
= qemu_mallocz(32);
435 if (type
== IF_IDE
|| type
== IF_SCSI
)
436 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
438 snprintf(dinfo
->id
, 32, "%s%i%s%i",
439 devname
, bus_id
, mediastr
, unit_id
);
441 snprintf(dinfo
->id
, 32, "%s%s%i",
442 devname
, mediastr
, unit_id
);
444 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
445 dinfo
->devaddr
= devaddr
;
448 dinfo
->unit
= unit_id
;
451 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
452 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
454 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
464 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
465 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
469 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
474 /* FIXME: This isn't really a floppy, but it's a reasonable
477 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
483 /* add virtio block device */
484 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
485 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
486 qemu_opt_set(opts
, "drive", dinfo
->id
);
488 qemu_opt_set(opts
, "addr", devaddr
);
493 if (!file
|| !*file
) {
498 /* always use cache=unsafe with snapshot */
499 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
500 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
503 if (media
== MEDIA_CDROM
) {
504 /* CDROM is fine for any interface, don't check. */
506 } else if (ro
== 1) {
507 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
508 error_report("readonly not supported by this bus type");
513 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
515 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
517 error_report("could not open disk image %s: %s",
518 file
, strerror(-ret
));
522 if (bdrv_key_required(dinfo
->bdrv
))
528 void do_commit(Monitor
*mon
, const QDict
*qdict
)
530 const char *device
= qdict_get_str(qdict
, "device");
531 BlockDriverState
*bs
;
533 if (!strcmp(device
, "all")) {
536 bs
= bdrv_find(device
);
538 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
545 int do_snapshot_blkdev(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
547 const char *device
= qdict_get_str(qdict
, "device");
548 const char *filename
= qdict_get_try_str(qdict
, "snapshot_file");
549 const char *format
= qdict_get_try_str(qdict
, "format");
550 BlockDriverState
*bs
;
551 BlockDriver
*drv
, *proto_drv
;
556 qerror_report(QERR_MISSING_PARAMETER
, "snapshot_file");
561 bs
= bdrv_find(device
);
563 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
572 drv
= bdrv_find_format(format
);
574 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
579 proto_drv
= bdrv_find_protocol(filename
);
581 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
586 ret
= bdrv_img_create(filename
, format
, bs
->filename
,
587 bs
->drv
->format_name
, NULL
, -1, bs
->open_flags
);
595 flags
= bs
->open_flags
;
597 ret
= bdrv_open(bs
, filename
, flags
, drv
);
599 * If reopening the image file we just created fails, we really
613 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
616 if (!bdrv_is_removable(bs
)) {
617 qerror_report(QERR_DEVICE_NOT_REMOVABLE
,
618 bdrv_get_device_name(bs
));
621 if (bdrv_is_locked(bs
)) {
622 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
630 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
632 BlockDriverState
*bs
;
633 int force
= qdict_get_try_bool(qdict
, "force", 0);
634 const char *filename
= qdict_get_str(qdict
, "device");
636 bs
= bdrv_find(filename
);
638 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
641 return eject_device(mon
, bs
, force
);
644 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
647 BlockDriverState
*bs
;
650 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
652 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
656 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
657 if (err
== -EINVAL
) {
658 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
660 } else if (err
< 0) {
661 qerror_report(QERR_INVALID_PASSWORD
);
668 int do_change_block(Monitor
*mon
, const char *device
,
669 const char *filename
, const char *fmt
)
671 BlockDriverState
*bs
;
672 BlockDriver
*drv
= NULL
;
675 bs
= bdrv_find(device
);
677 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
681 drv
= bdrv_find_whitelisted_format(fmt
);
683 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
687 if (eject_device(mon
, bs
, 0) < 0) {
690 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
691 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
692 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
693 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
696 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
699 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
701 const char *id
= qdict_get_str(qdict
, "id");
702 BlockDriverState
*bs
;
703 BlockDriverState
**ptr
;
708 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
712 /* quiesce block driver; prevent further io */
717 /* clean up guest state from pointing to host resource by
718 * finding and removing DeviceState "drive" property */
720 for (prop
= bs
->peer
->info
->props
; prop
&& prop
->name
; prop
++) {
721 if (prop
->info
->type
== PROP_TYPE_DRIVE
) {
722 ptr
= qdev_get_prop_ptr(bs
->peer
, prop
);
724 bdrv_detach(bs
, bs
->peer
);
732 /* clean up host side */
733 drive_uninit(drive_get_by_blockdev(bs
));
739 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
740 * existing QERR_ macro mess is cleaned up. A good example for better
741 * error reports can be found in the qemu-img resize code.
743 int do_block_resize(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
745 const char *device
= qdict_get_str(qdict
, "device");
746 int64_t size
= qdict_get_int(qdict
, "size");
747 BlockDriverState
*bs
;
749 bs
= bdrv_find(device
);
751 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
756 qerror_report(QERR_UNDEFINED_ERROR
);
760 if (bdrv_truncate(bs
, size
)) {
761 qerror_report(QERR_UNDEFINED_ERROR
);