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
,
101 opts
= drive_def(optstr
);
105 if (type
!= IF_DEFAULT
) {
106 qemu_opt_set(opts
, "if", if_name
[type
]);
109 snprintf(buf
, sizeof(buf
), "%d", index
);
110 qemu_opt_set(opts
, "index", buf
);
113 qemu_opt_set(opts
, "file", file
);
117 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
121 /* seek interface, bus and unit */
123 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
124 if (dinfo
->type
== type
&&
133 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
135 return drive_get(type
,
136 drive_index_to_bus_id(type
, index
),
137 drive_index_to_unit_id(type
, index
));
140 int drive_get_max_bus(BlockInterfaceType type
)
146 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
147 if(dinfo
->type
== type
&&
148 dinfo
->bus
> max_bus
)
149 max_bus
= dinfo
->bus
;
154 /* Get a block device. This should only be used for single-drive devices
155 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
157 DriveInfo
*drive_get_next(BlockInterfaceType type
)
159 static int next_block_unit
[IF_COUNT
];
161 return drive_get(type
, 0, next_block_unit
[type
]++);
164 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
168 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
169 if (dinfo
->bdrv
== bs
) {
176 static void bdrv_format_print(void *opaque
, const char *name
)
178 error_printf(" %s", name
);
181 static void drive_uninit(DriveInfo
*dinfo
)
183 qemu_opts_del(dinfo
->opts
);
184 bdrv_delete(dinfo
->bdrv
);
185 QTAILQ_REMOVE(&drives
, dinfo
, next
);
189 void drive_put_ref(DriveInfo
*dinfo
)
191 assert(dinfo
->refcount
);
192 if (--dinfo
->refcount
== 0) {
197 void drive_get_ref(DriveInfo
*dinfo
)
202 static int parse_block_error_action(const char *buf
, int is_read
)
204 if (!strcmp(buf
, "ignore")) {
205 return BLOCK_ERR_IGNORE
;
206 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
207 return BLOCK_ERR_STOP_ENOSPC
;
208 } else if (!strcmp(buf
, "stop")) {
209 return BLOCK_ERR_STOP_ANY
;
210 } else if (!strcmp(buf
, "report")) {
211 return BLOCK_ERR_REPORT
;
213 error_report("'%s' invalid %s error action",
214 buf
, is_read
? "read" : "write");
219 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
)
222 const char *file
= NULL
;
225 const char *mediastr
= "";
226 BlockInterfaceType type
;
227 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
229 int cyls
, heads
, secs
, translation
;
230 BlockDriver
*drv
= NULL
;
235 int on_read_error
, on_write_error
;
241 translation
= BIOS_ATA_TRANSLATION_AUTO
;
243 if (default_to_scsi
) {
245 pstrcpy(devname
, sizeof(devname
), "scsi");
248 pstrcpy(devname
, sizeof(devname
), "ide");
252 /* extract parameters */
253 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
254 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
255 index
= qemu_opt_get_number(opts
, "index", -1);
257 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
258 heads
= qemu_opt_get_number(opts
, "heads", 0);
259 secs
= qemu_opt_get_number(opts
, "secs", 0);
261 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
262 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
264 file
= qemu_opt_get(opts
, "file");
265 serial
= qemu_opt_get(opts
, "serial");
267 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
268 pstrcpy(devname
, sizeof(devname
), buf
);
269 for (type
= 0; type
< IF_COUNT
&& strcmp(buf
, if_name
[type
]); type
++)
271 if (type
== IF_COUNT
) {
272 error_report("unsupported bus type '%s'", buf
);
276 max_devs
= if_max_devs
[type
];
278 if (cyls
|| heads
|| secs
) {
279 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
280 error_report("invalid physical cyls number");
283 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
284 error_report("invalid physical heads number");
287 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
288 error_report("invalid physical secs number");
293 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
295 error_report("'%s' trans must be used with cyls,heads and secs",
299 if (!strcmp(buf
, "none"))
300 translation
= BIOS_ATA_TRANSLATION_NONE
;
301 else if (!strcmp(buf
, "lba"))
302 translation
= BIOS_ATA_TRANSLATION_LBA
;
303 else if (!strcmp(buf
, "auto"))
304 translation
= BIOS_ATA_TRANSLATION_AUTO
;
306 error_report("'%s' invalid translation type", buf
);
311 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
312 if (!strcmp(buf
, "disk")) {
314 } else if (!strcmp(buf
, "cdrom")) {
315 if (cyls
|| secs
|| heads
) {
316 error_report("'%s' invalid physical CHS format", buf
);
321 error_report("'%s' invalid media", buf
);
326 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
327 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
328 bdrv_flags
|= BDRV_O_NOCACHE
;
329 } else if (!strcmp(buf
, "writeback")) {
330 bdrv_flags
|= BDRV_O_CACHE_WB
;
331 } else if (!strcmp(buf
, "unsafe")) {
332 bdrv_flags
|= BDRV_O_CACHE_WB
;
333 bdrv_flags
|= BDRV_O_NO_FLUSH
;
334 } else if (!strcmp(buf
, "writethrough")) {
335 /* this is the default */
337 error_report("invalid cache option");
342 #ifdef CONFIG_LINUX_AIO
343 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
344 if (!strcmp(buf
, "native")) {
345 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
346 } else if (!strcmp(buf
, "threads")) {
347 /* this is the default */
349 error_report("invalid aio option");
355 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
356 if (strcmp(buf
, "?") == 0) {
357 error_printf("Supported formats:");
358 bdrv_iterate_format(bdrv_format_print
, NULL
);
362 drv
= bdrv_find_whitelisted_format(buf
);
364 error_report("'%s' invalid format", buf
);
369 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
370 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
371 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
372 error_report("werror is not supported by this bus type");
376 on_write_error
= parse_block_error_action(buf
, 0);
377 if (on_write_error
< 0) {
382 on_read_error
= BLOCK_ERR_REPORT
;
383 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
384 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
385 error_report("rerror is not supported by this bus type");
389 on_read_error
= parse_block_error_action(buf
, 1);
390 if (on_read_error
< 0) {
395 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
396 if (type
!= IF_VIRTIO
) {
397 error_report("addr is not supported by this bus type");
402 /* compute bus and unit according index */
405 if (bus_id
!= 0 || unit_id
!= -1) {
406 error_report("index cannot be used with bus and unit");
409 bus_id
= drive_index_to_bus_id(type
, index
);
410 unit_id
= drive_index_to_unit_id(type
, index
);
413 /* if user doesn't specify a unit_id,
414 * try to find the first free
419 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
421 if (max_devs
&& unit_id
>= max_devs
) {
430 if (max_devs
&& unit_id
>= max_devs
) {
431 error_report("unit %d too big (max is %d)",
432 unit_id
, max_devs
- 1);
437 * catch multiple definitions
440 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
441 error_report("drive with bus=%d, unit=%d (index=%d) exists",
442 bus_id
, unit_id
, index
);
448 dinfo
= qemu_mallocz(sizeof(*dinfo
));
449 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
450 dinfo
->id
= qemu_strdup(buf
);
452 /* no id supplied -> create one */
453 dinfo
->id
= qemu_mallocz(32);
454 if (type
== IF_IDE
|| type
== IF_SCSI
)
455 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
457 snprintf(dinfo
->id
, 32, "%s%i%s%i",
458 devname
, bus_id
, mediastr
, unit_id
);
460 snprintf(dinfo
->id
, 32, "%s%s%i",
461 devname
, mediastr
, unit_id
);
463 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
464 dinfo
->devaddr
= devaddr
;
467 dinfo
->unit
= unit_id
;
471 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
472 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
474 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
484 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
485 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
489 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
494 /* FIXME: This isn't really a floppy, but it's a reasonable
497 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
503 /* add virtio block device */
504 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
505 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
506 qemu_opt_set(opts
, "drive", dinfo
->id
);
508 qemu_opt_set(opts
, "addr", devaddr
);
513 if (!file
|| !*file
) {
517 /* always use cache=unsafe with snapshot */
518 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
519 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
522 if (media
== MEDIA_CDROM
) {
523 /* CDROM is fine for any interface, don't check. */
525 } else if (ro
== 1) {
526 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
527 error_report("readonly not supported by this bus type");
532 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
534 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
536 error_report("could not open disk image %s: %s",
537 file
, strerror(-ret
));
541 if (bdrv_key_required(dinfo
->bdrv
))
546 void do_commit(Monitor
*mon
, const QDict
*qdict
)
548 const char *device
= qdict_get_str(qdict
, "device");
549 BlockDriverState
*bs
;
551 if (!strcmp(device
, "all")) {
554 bs
= bdrv_find(device
);
556 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
563 int do_snapshot_blkdev(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
565 const char *device
= qdict_get_str(qdict
, "device");
566 const char *filename
= qdict_get_try_str(qdict
, "snapshot_file");
567 const char *format
= qdict_get_try_str(qdict
, "format");
568 BlockDriverState
*bs
;
569 BlockDriver
*drv
, *proto_drv
;
574 qerror_report(QERR_MISSING_PARAMETER
, "snapshot_file");
579 bs
= bdrv_find(device
);
581 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
590 drv
= bdrv_find_format(format
);
592 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
597 proto_drv
= bdrv_find_protocol(filename
);
599 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
604 ret
= bdrv_img_create(filename
, format
, bs
->filename
,
605 bs
->drv
->format_name
, NULL
, -1, bs
->open_flags
);
613 flags
= bs
->open_flags
;
615 ret
= bdrv_open(bs
, filename
, flags
, drv
);
617 * If reopening the image file we just created fails, we really
631 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
634 if (!bdrv_is_removable(bs
)) {
635 qerror_report(QERR_DEVICE_NOT_REMOVABLE
,
636 bdrv_get_device_name(bs
));
639 if (bdrv_is_locked(bs
)) {
640 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
648 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
650 BlockDriverState
*bs
;
651 int force
= qdict_get_try_bool(qdict
, "force", 0);
652 const char *filename
= qdict_get_str(qdict
, "device");
654 bs
= bdrv_find(filename
);
656 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
659 return eject_device(mon
, bs
, force
);
662 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
665 BlockDriverState
*bs
;
668 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
670 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
674 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
675 if (err
== -EINVAL
) {
676 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
678 } else if (err
< 0) {
679 qerror_report(QERR_INVALID_PASSWORD
);
686 int do_change_block(Monitor
*mon
, const char *device
,
687 const char *filename
, const char *fmt
)
689 BlockDriverState
*bs
;
690 BlockDriver
*drv
= NULL
;
693 bs
= bdrv_find(device
);
695 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
699 drv
= bdrv_find_whitelisted_format(fmt
);
701 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
705 if (eject_device(mon
, bs
, 0) < 0) {
708 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
709 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
710 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
711 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
714 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
717 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
719 const char *id
= qdict_get_str(qdict
, "id");
720 BlockDriverState
*bs
;
721 BlockDriverState
**ptr
;
726 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
729 if (bdrv_in_use(bs
)) {
730 qerror_report(QERR_DEVICE_IN_USE
, id
);
734 /* quiesce block driver; prevent further io */
739 /* clean up guest state from pointing to host resource by
740 * finding and removing DeviceState "drive" property */
742 for (prop
= bs
->peer
->info
->props
; prop
&& prop
->name
; prop
++) {
743 if (prop
->info
->type
== PROP_TYPE_DRIVE
) {
744 ptr
= qdev_get_prop_ptr(bs
->peer
, prop
);
746 bdrv_detach(bs
, bs
->peer
);
754 /* clean up host side */
755 drive_uninit(drive_get_by_blockdev(bs
));
761 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
762 * existing QERR_ macro mess is cleaned up. A good example for better
763 * error reports can be found in the qemu-img resize code.
765 int do_block_resize(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
767 const char *device
= qdict_get_str(qdict
, "device");
768 int64_t size
= qdict_get_int(qdict
, "size");
769 BlockDriverState
*bs
;
771 bs
= bdrv_find(device
);
773 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
778 qerror_report(QERR_UNDEFINED_ERROR
);
782 if (bdrv_truncate(bs
, size
)) {
783 qerror_report(QERR_UNDEFINED_ERROR
);