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
] = {
35 [IF_IDE
] = MAX_IDE_DEVS
,
36 [IF_SCSI
] = MAX_SCSI_DEVS
,
40 * We automatically delete the drive when a device using it gets
41 * unplugged. Questionable feature, but we can't just drop it.
42 * Device models call blockdev_mark_auto_del() to schedule the
43 * automatic deletion, and generic qdev code calls blockdev_auto_del()
44 * when deletion is actually safe.
46 void blockdev_mark_auto_del(BlockDriverState
*bs
)
48 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
55 void blockdev_auto_del(BlockDriverState
*bs
)
57 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
59 if (dinfo
&& dinfo
->auto_del
) {
64 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
71 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
74 opts
= qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
79 qemu_opt_set(opts
, "file", file
);
83 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
87 /* seek interface, bus and unit */
89 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
90 if (dinfo
->type
== type
&&
99 int drive_get_max_bus(BlockInterfaceType type
)
105 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
106 if(dinfo
->type
== type
&&
107 dinfo
->bus
> max_bus
)
108 max_bus
= dinfo
->bus
;
113 /* Get a block device. This should only be used for single-drive devices
114 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
116 DriveInfo
*drive_get_next(BlockInterfaceType type
)
118 static int next_block_unit
[IF_COUNT
];
120 return drive_get(type
, 0, next_block_unit
[type
]++);
123 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
127 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
128 if (dinfo
->bdrv
== bs
) {
135 static void bdrv_format_print(void *opaque
, const char *name
)
137 error_printf(" %s", name
);
140 void drive_uninit(DriveInfo
*dinfo
)
142 qemu_opts_del(dinfo
->opts
);
143 bdrv_delete(dinfo
->bdrv
);
144 QTAILQ_REMOVE(&drives
, dinfo
, next
);
148 static int parse_block_error_action(const char *buf
, int is_read
)
150 if (!strcmp(buf
, "ignore")) {
151 return BLOCK_ERR_IGNORE
;
152 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
153 return BLOCK_ERR_STOP_ENOSPC
;
154 } else if (!strcmp(buf
, "stop")) {
155 return BLOCK_ERR_STOP_ANY
;
156 } else if (!strcmp(buf
, "report")) {
157 return BLOCK_ERR_REPORT
;
159 error_report("'%s' invalid %s error action",
160 buf
, is_read
? "read" : "write");
165 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
, int *fatal_error
)
168 const char *file
= NULL
;
171 const char *mediastr
= "";
172 BlockInterfaceType type
;
173 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
175 int cyls
, heads
, secs
, translation
;
176 BlockDriver
*drv
= NULL
;
181 int on_read_error
, on_write_error
;
189 translation
= BIOS_ATA_TRANSLATION_AUTO
;
191 if (default_to_scsi
) {
193 pstrcpy(devname
, sizeof(devname
), "scsi");
196 pstrcpy(devname
, sizeof(devname
), "ide");
200 /* extract parameters */
201 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
202 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
203 index
= qemu_opt_get_number(opts
, "index", -1);
205 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
206 heads
= qemu_opt_get_number(opts
, "heads", 0);
207 secs
= qemu_opt_get_number(opts
, "secs", 0);
209 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
210 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
212 file
= qemu_opt_get(opts
, "file");
213 serial
= qemu_opt_get(opts
, "serial");
215 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
216 pstrcpy(devname
, sizeof(devname
), buf
);
217 for (type
= 0; type
< IF_COUNT
&& strcmp(buf
, if_name
[type
]); type
++)
219 if (type
== IF_COUNT
) {
220 error_report("unsupported bus type '%s'", buf
);
224 max_devs
= if_max_devs
[type
];
226 if (cyls
|| heads
|| secs
) {
227 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
228 error_report("invalid physical cyls number");
231 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
232 error_report("invalid physical heads number");
235 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
236 error_report("invalid physical secs number");
241 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
243 error_report("'%s' trans must be used with cyls,heads and secs",
247 if (!strcmp(buf
, "none"))
248 translation
= BIOS_ATA_TRANSLATION_NONE
;
249 else if (!strcmp(buf
, "lba"))
250 translation
= BIOS_ATA_TRANSLATION_LBA
;
251 else if (!strcmp(buf
, "auto"))
252 translation
= BIOS_ATA_TRANSLATION_AUTO
;
254 error_report("'%s' invalid translation type", buf
);
259 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
260 if (!strcmp(buf
, "disk")) {
262 } else if (!strcmp(buf
, "cdrom")) {
263 if (cyls
|| secs
|| heads
) {
264 error_report("'%s' invalid physical CHS format", buf
);
269 error_report("'%s' invalid media", buf
);
274 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
275 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
276 bdrv_flags
|= BDRV_O_NOCACHE
;
277 } else if (!strcmp(buf
, "writeback")) {
278 bdrv_flags
|= BDRV_O_CACHE_WB
;
279 } else if (!strcmp(buf
, "unsafe")) {
280 bdrv_flags
|= BDRV_O_CACHE_WB
;
281 bdrv_flags
|= BDRV_O_NO_FLUSH
;
282 } else if (!strcmp(buf
, "writethrough")) {
283 /* this is the default */
285 error_report("invalid cache option");
290 #ifdef CONFIG_LINUX_AIO
291 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
292 if (!strcmp(buf
, "native")) {
293 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
294 } else if (!strcmp(buf
, "threads")) {
295 /* this is the default */
297 error_report("invalid aio option");
303 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
304 if (strcmp(buf
, "?") == 0) {
305 error_printf("Supported formats:");
306 bdrv_iterate_format(bdrv_format_print
, NULL
);
310 drv
= bdrv_find_whitelisted_format(buf
);
312 error_report("'%s' invalid format", buf
);
317 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
318 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
319 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
320 error_report("werror is not supported by this bus type");
324 on_write_error
= parse_block_error_action(buf
, 0);
325 if (on_write_error
< 0) {
330 on_read_error
= BLOCK_ERR_REPORT
;
331 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
332 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
333 error_report("rerror is not supported by this bus type");
337 on_read_error
= parse_block_error_action(buf
, 1);
338 if (on_read_error
< 0) {
343 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
344 if (type
!= IF_VIRTIO
) {
345 error_report("addr is not supported by this bus type");
350 /* compute bus and unit according index */
353 if (bus_id
!= 0 || unit_id
!= -1) {
354 error_report("index cannot be used with bus and unit");
362 unit_id
= index
% max_devs
;
363 bus_id
= index
/ max_devs
;
367 /* if user doesn't specify a unit_id,
368 * try to find the first free
373 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
375 if (max_devs
&& unit_id
>= max_devs
) {
384 if (max_devs
&& unit_id
>= max_devs
) {
385 error_report("unit %d too big (max is %d)",
386 unit_id
, max_devs
- 1);
391 * ignore multiple definitions
394 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
401 dinfo
= qemu_mallocz(sizeof(*dinfo
));
402 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
403 dinfo
->id
= qemu_strdup(buf
);
405 /* no id supplied -> create one */
406 dinfo
->id
= qemu_mallocz(32);
407 if (type
== IF_IDE
|| type
== IF_SCSI
)
408 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
410 snprintf(dinfo
->id
, 32, "%s%i%s%i",
411 devname
, bus_id
, mediastr
, unit_id
);
413 snprintf(dinfo
->id
, 32, "%s%s%i",
414 devname
, mediastr
, unit_id
);
416 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
417 dinfo
->devaddr
= devaddr
;
420 dinfo
->unit
= unit_id
;
423 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
424 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
426 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
436 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
437 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
441 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
446 /* FIXME: This isn't really a floppy, but it's a reasonable
449 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
455 /* add virtio block device */
456 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
457 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
458 qemu_opt_set(opts
, "drive", dinfo
->id
);
460 qemu_opt_set(opts
, "addr", devaddr
);
465 if (!file
|| !*file
) {
470 /* always use cache=unsafe with snapshot */
471 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
472 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
475 if (media
== MEDIA_CDROM
) {
476 /* CDROM is fine for any interface, don't check. */
478 } else if (ro
== 1) {
479 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
480 error_report("readonly not supported by this bus type");
485 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
487 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
489 error_report("could not open disk image %s: %s",
490 file
, strerror(-ret
));
494 if (bdrv_key_required(dinfo
->bdrv
))
500 void do_commit(Monitor
*mon
, const QDict
*qdict
)
502 const char *device
= qdict_get_str(qdict
, "device");
503 BlockDriverState
*bs
;
505 if (!strcmp(device
, "all")) {
508 bs
= bdrv_find(device
);
510 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
517 int do_snapshot_blkdev(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
519 const char *device
= qdict_get_str(qdict
, "device");
520 const char *filename
= qdict_get_try_str(qdict
, "snapshot_file");
521 const char *format
= qdict_get_try_str(qdict
, "format");
522 BlockDriverState
*bs
;
523 BlockDriver
*drv
, *proto_drv
;
528 qerror_report(QERR_MISSING_PARAMETER
, "snapshot_file");
533 bs
= bdrv_find(device
);
535 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
544 drv
= bdrv_find_format(format
);
546 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
551 proto_drv
= bdrv_find_protocol(filename
);
553 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
558 ret
= bdrv_img_create(filename
, format
, bs
->filename
,
559 bs
->drv
->format_name
, NULL
, -1, bs
->open_flags
);
567 flags
= bs
->open_flags
;
569 ret
= bdrv_open(bs
, filename
, flags
, drv
);
571 * If reopening the image file we just created fails, we really
585 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
588 if (!bdrv_is_removable(bs
)) {
589 qerror_report(QERR_DEVICE_NOT_REMOVABLE
,
590 bdrv_get_device_name(bs
));
593 if (bdrv_is_locked(bs
)) {
594 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
602 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
604 BlockDriverState
*bs
;
605 int force
= qdict_get_try_bool(qdict
, "force", 0);
606 const char *filename
= qdict_get_str(qdict
, "device");
608 bs
= bdrv_find(filename
);
610 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
613 return eject_device(mon
, bs
, force
);
616 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
619 BlockDriverState
*bs
;
622 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
624 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
628 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
629 if (err
== -EINVAL
) {
630 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
632 } else if (err
< 0) {
633 qerror_report(QERR_INVALID_PASSWORD
);
640 int do_change_block(Monitor
*mon
, const char *device
,
641 const char *filename
, const char *fmt
)
643 BlockDriverState
*bs
;
644 BlockDriver
*drv
= NULL
;
647 bs
= bdrv_find(device
);
649 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
653 drv
= bdrv_find_whitelisted_format(fmt
);
655 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
659 if (eject_device(mon
, bs
, 0) < 0) {
662 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
663 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
664 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
665 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
668 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
671 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
673 const char *id
= qdict_get_str(qdict
, "id");
674 BlockDriverState
*bs
;
675 BlockDriverState
**ptr
;
680 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
684 /* quiesce block driver; prevent further io */
689 /* clean up guest state from pointing to host resource by
690 * finding and removing DeviceState "drive" property */
692 for (prop
= bs
->peer
->info
->props
; prop
&& prop
->name
; prop
++) {
693 if (prop
->info
->type
== PROP_TYPE_DRIVE
) {
694 ptr
= qdev_get_prop_ptr(bs
->peer
, prop
);
696 bdrv_detach(bs
, bs
->peer
);
704 /* clean up host side */
705 drive_uninit(drive_get_by_blockdev(bs
));
711 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
712 * existing QERR_ macro mess is cleaned up. A good example for better
713 * error reports can be found in the qemu-img resize code.
715 int do_block_resize(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
717 const char *device
= qdict_get_str(qdict
, "device");
718 int64_t size
= qdict_get_int(qdict
, "size");
719 BlockDriverState
*bs
;
721 bs
= bdrv_find(device
);
723 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
728 qerror_report(QERR_UNDEFINED_ERROR
);
732 if (bdrv_truncate(bs
, size
)) {
733 qerror_report(QERR_UNDEFINED_ERROR
);