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
);
23 * We automatically delete the drive when a device using it gets
24 * unplugged. Questionable feature, but we can't just drop it.
25 * Device models call blockdev_mark_auto_del() to schedule the
26 * automatic deletion, and generic qdev code calls blockdev_auto_del()
27 * when deletion is actually safe.
29 void blockdev_mark_auto_del(BlockDriverState
*bs
)
31 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
38 void blockdev_auto_del(BlockDriverState
*bs
)
40 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
42 if (dinfo
&& dinfo
->auto_del
) {
47 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
54 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
57 opts
= qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
62 qemu_opt_set(opts
, "file", file
);
66 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
70 /* seek interface, bus and unit */
72 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
73 if (dinfo
->type
== type
&&
82 int drive_get_max_bus(BlockInterfaceType type
)
88 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
89 if(dinfo
->type
== type
&&
96 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
100 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
101 if (dinfo
->bdrv
== bs
) {
108 static void bdrv_format_print(void *opaque
, const char *name
)
110 error_printf(" %s", name
);
113 void drive_uninit(DriveInfo
*dinfo
)
115 qemu_opts_del(dinfo
->opts
);
116 bdrv_delete(dinfo
->bdrv
);
117 QTAILQ_REMOVE(&drives
, dinfo
, next
);
121 static int parse_block_error_action(const char *buf
, int is_read
)
123 if (!strcmp(buf
, "ignore")) {
124 return BLOCK_ERR_IGNORE
;
125 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
126 return BLOCK_ERR_STOP_ENOSPC
;
127 } else if (!strcmp(buf
, "stop")) {
128 return BLOCK_ERR_STOP_ANY
;
129 } else if (!strcmp(buf
, "report")) {
130 return BLOCK_ERR_REPORT
;
132 error_report("'%s' invalid %s error action",
133 buf
, is_read
? "read" : "write");
138 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
, int *fatal_error
)
141 const char *file
= NULL
;
144 const char *mediastr
= "";
145 BlockInterfaceType type
;
146 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
148 int cyls
, heads
, secs
, translation
;
149 BlockDriver
*drv
= NULL
;
154 int on_read_error
, on_write_error
;
162 translation
= BIOS_ATA_TRANSLATION_AUTO
;
164 if (default_to_scsi
) {
166 max_devs
= MAX_SCSI_DEVS
;
167 pstrcpy(devname
, sizeof(devname
), "scsi");
170 max_devs
= MAX_IDE_DEVS
;
171 pstrcpy(devname
, sizeof(devname
), "ide");
175 /* extract parameters */
176 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
177 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
178 index
= qemu_opt_get_number(opts
, "index", -1);
180 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
181 heads
= qemu_opt_get_number(opts
, "heads", 0);
182 secs
= qemu_opt_get_number(opts
, "secs", 0);
184 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
185 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
187 file
= qemu_opt_get(opts
, "file");
188 serial
= qemu_opt_get(opts
, "serial");
190 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
191 pstrcpy(devname
, sizeof(devname
), buf
);
192 if (!strcmp(buf
, "ide")) {
194 max_devs
= MAX_IDE_DEVS
;
195 } else if (!strcmp(buf
, "scsi")) {
197 max_devs
= MAX_SCSI_DEVS
;
198 } else if (!strcmp(buf
, "floppy")) {
201 } else if (!strcmp(buf
, "pflash")) {
204 } else if (!strcmp(buf
, "mtd")) {
207 } else if (!strcmp(buf
, "sd")) {
210 } else if (!strcmp(buf
, "virtio")) {
213 } else if (!strcmp(buf
, "xen")) {
216 } else if (!strcmp(buf
, "none")) {
220 error_report("unsupported bus type '%s'", buf
);
225 if (cyls
|| heads
|| secs
) {
226 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
227 error_report("invalid physical cyls number");
230 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
231 error_report("invalid physical heads number");
234 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
235 error_report("invalid physical secs number");
240 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
242 error_report("'%s' trans must be used with cyls,heads and secs",
246 if (!strcmp(buf
, "none"))
247 translation
= BIOS_ATA_TRANSLATION_NONE
;
248 else if (!strcmp(buf
, "lba"))
249 translation
= BIOS_ATA_TRANSLATION_LBA
;
250 else if (!strcmp(buf
, "auto"))
251 translation
= BIOS_ATA_TRANSLATION_AUTO
;
253 error_report("'%s' invalid translation type", buf
);
258 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
259 if (!strcmp(buf
, "disk")) {
261 } else if (!strcmp(buf
, "cdrom")) {
262 if (cyls
|| secs
|| heads
) {
263 error_report("'%s' invalid physical CHS format", buf
);
268 error_report("'%s' invalid media", buf
);
273 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
274 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
275 bdrv_flags
|= BDRV_O_NOCACHE
;
276 } else if (!strcmp(buf
, "writeback")) {
277 bdrv_flags
|= BDRV_O_CACHE_WB
;
278 } else if (!strcmp(buf
, "unsafe")) {
279 bdrv_flags
|= BDRV_O_CACHE_WB
;
280 bdrv_flags
|= BDRV_O_NO_FLUSH
;
281 } else if (!strcmp(buf
, "writethrough")) {
282 /* this is the default */
284 error_report("invalid cache option");
289 #ifdef CONFIG_LINUX_AIO
290 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
291 if (!strcmp(buf
, "native")) {
292 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
293 } else if (!strcmp(buf
, "threads")) {
294 /* this is the default */
296 error_report("invalid aio option");
302 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
303 if (strcmp(buf
, "?") == 0) {
304 error_printf("Supported formats:");
305 bdrv_iterate_format(bdrv_format_print
, NULL
);
309 drv
= bdrv_find_whitelisted_format(buf
);
311 error_report("'%s' invalid format", buf
);
316 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
317 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
318 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
319 error_report("werror is not supported by this bus type");
323 on_write_error
= parse_block_error_action(buf
, 0);
324 if (on_write_error
< 0) {
329 on_read_error
= BLOCK_ERR_REPORT
;
330 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
331 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
332 error_report("rerror is not supported by this bus type");
336 on_read_error
= parse_block_error_action(buf
, 1);
337 if (on_read_error
< 0) {
342 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
343 if (type
!= IF_VIRTIO
) {
344 error_report("addr is not supported by this bus type");
349 /* compute bus and unit according index */
352 if (bus_id
!= 0 || unit_id
!= -1) {
353 error_report("index cannot be used with bus and unit");
361 unit_id
= index
% max_devs
;
362 bus_id
= index
/ max_devs
;
366 /* if user doesn't specify a unit_id,
367 * try to find the first free
372 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
374 if (max_devs
&& unit_id
>= max_devs
) {
383 if (max_devs
&& unit_id
>= max_devs
) {
384 error_report("unit %d too big (max is %d)",
385 unit_id
, max_devs
- 1);
390 * ignore multiple definitions
393 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
400 dinfo
= qemu_mallocz(sizeof(*dinfo
));
401 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
402 dinfo
->id
= qemu_strdup(buf
);
404 /* no id supplied -> create one */
405 dinfo
->id
= qemu_mallocz(32);
406 if (type
== IF_IDE
|| type
== IF_SCSI
)
407 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
409 snprintf(dinfo
->id
, 32, "%s%i%s%i",
410 devname
, bus_id
, mediastr
, unit_id
);
412 snprintf(dinfo
->id
, 32, "%s%s%i",
413 devname
, mediastr
, unit_id
);
415 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
416 dinfo
->devaddr
= devaddr
;
419 dinfo
->unit
= unit_id
;
422 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
423 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
425 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
435 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
436 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
440 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
445 /* FIXME: This isn't really a floppy, but it's a reasonable
448 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
454 /* add virtio block device */
455 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
456 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
457 qemu_opt_set(opts
, "drive", dinfo
->id
);
459 qemu_opt_set(opts
, "addr", devaddr
);
464 if (!file
|| !*file
) {
469 /* always use cache=unsafe with snapshot */
470 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
471 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
474 if (media
== MEDIA_CDROM
) {
475 /* CDROM is fine for any interface, don't check. */
477 } else if (ro
== 1) {
478 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
479 error_report("readonly not supported by this bus type");
484 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
486 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
488 error_report("could not open disk image %s: %s",
489 file
, strerror(-ret
));
493 if (bdrv_key_required(dinfo
->bdrv
))
499 void do_commit(Monitor
*mon
, const QDict
*qdict
)
501 const char *device
= qdict_get_str(qdict
, "device");
502 BlockDriverState
*bs
;
504 if (!strcmp(device
, "all")) {
507 bs
= bdrv_find(device
);
509 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
516 int do_snapshot_blkdev(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
518 const char *device
= qdict_get_str(qdict
, "device");
519 const char *filename
= qdict_get_try_str(qdict
, "snapshot_file");
520 const char *format
= qdict_get_try_str(qdict
, "format");
521 BlockDriverState
*bs
;
522 BlockDriver
*drv
, *proto_drv
;
527 qerror_report(QERR_MISSING_PARAMETER
, "snapshot_file");
532 bs
= bdrv_find(device
);
534 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
543 drv
= bdrv_find_format(format
);
545 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
550 proto_drv
= bdrv_find_protocol(filename
);
552 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
557 ret
= bdrv_img_create(filename
, format
, bs
->filename
,
558 bs
->drv
->format_name
, NULL
, -1, bs
->open_flags
);
566 flags
= bs
->open_flags
;
568 ret
= bdrv_open(bs
, filename
, flags
, drv
);
570 * If reopening the image file we just created fails, we really
584 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
587 if (!bdrv_is_removable(bs
)) {
588 qerror_report(QERR_DEVICE_NOT_REMOVABLE
,
589 bdrv_get_device_name(bs
));
592 if (bdrv_is_locked(bs
)) {
593 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
601 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
603 BlockDriverState
*bs
;
604 int force
= qdict_get_try_bool(qdict
, "force", 0);
605 const char *filename
= qdict_get_str(qdict
, "device");
607 bs
= bdrv_find(filename
);
609 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
612 return eject_device(mon
, bs
, force
);
615 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
618 BlockDriverState
*bs
;
621 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
623 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
627 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
628 if (err
== -EINVAL
) {
629 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
631 } else if (err
< 0) {
632 qerror_report(QERR_INVALID_PASSWORD
);
639 int do_change_block(Monitor
*mon
, const char *device
,
640 const char *filename
, const char *fmt
)
642 BlockDriverState
*bs
;
643 BlockDriver
*drv
= NULL
;
646 bs
= bdrv_find(device
);
648 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
652 drv
= bdrv_find_whitelisted_format(fmt
);
654 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
658 if (eject_device(mon
, bs
, 0) < 0) {
661 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
662 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
663 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
664 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
667 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
670 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
672 const char *id
= qdict_get_str(qdict
, "id");
673 BlockDriverState
*bs
;
674 BlockDriverState
**ptr
;
679 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
683 /* quiesce block driver; prevent further io */
688 /* clean up guest state from pointing to host resource by
689 * finding and removing DeviceState "drive" property */
691 for (prop
= bs
->peer
->info
->props
; prop
&& prop
->name
; prop
++) {
692 if (prop
->info
->type
== PROP_TYPE_DRIVE
) {
693 ptr
= qdev_get_prop_ptr(bs
->peer
, prop
);
695 bdrv_detach(bs
, bs
->peer
);
703 /* clean up host side */
704 drive_uninit(drive_get_by_blockdev(bs
));
710 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
711 * existing QERR_ macro mess is cleaned up. A good example for better
712 * error reports can be found in the qemu-img resize code.
714 int do_block_resize(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
716 const char *device
= qdict_get_str(qdict
, "device");
717 int64_t size
= qdict_get_int(qdict
, "size");
718 BlockDriverState
*bs
;
720 bs
= bdrv_find(device
);
722 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
727 qerror_report(QERR_UNDEFINED_ERROR
);
731 if (bdrv_truncate(bs
, size
)) {
732 qerror_report(QERR_UNDEFINED_ERROR
);