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
);
186 QTAILQ_REMOVE(&drives
, dinfo
, next
);
190 void drive_put_ref(DriveInfo
*dinfo
)
192 assert(dinfo
->refcount
);
193 if (--dinfo
->refcount
== 0) {
198 void drive_get_ref(DriveInfo
*dinfo
)
203 static int parse_block_error_action(const char *buf
, int is_read
)
205 if (!strcmp(buf
, "ignore")) {
206 return BLOCK_ERR_IGNORE
;
207 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
208 return BLOCK_ERR_STOP_ENOSPC
;
209 } else if (!strcmp(buf
, "stop")) {
210 return BLOCK_ERR_STOP_ANY
;
211 } else if (!strcmp(buf
, "report")) {
212 return BLOCK_ERR_REPORT
;
214 error_report("'%s' invalid %s error action",
215 buf
, is_read
? "read" : "write");
220 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
)
223 const char *file
= NULL
;
226 const char *mediastr
= "";
227 BlockInterfaceType type
;
228 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
230 int cyls
, heads
, secs
, translation
;
231 BlockDriver
*drv
= NULL
;
236 int on_read_error
, on_write_error
;
242 translation
= BIOS_ATA_TRANSLATION_AUTO
;
245 /* extract parameters */
246 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
247 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
248 index
= qemu_opt_get_number(opts
, "index", -1);
250 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
251 heads
= qemu_opt_get_number(opts
, "heads", 0);
252 secs
= qemu_opt_get_number(opts
, "secs", 0);
254 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
255 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
257 file
= qemu_opt_get(opts
, "file");
258 serial
= qemu_opt_get(opts
, "serial");
260 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
261 pstrcpy(devname
, sizeof(devname
), buf
);
262 for (type
= 0; type
< IF_COUNT
&& strcmp(buf
, if_name
[type
]); type
++)
264 if (type
== IF_COUNT
) {
265 error_report("unsupported bus type '%s'", buf
);
269 type
= default_to_scsi
? IF_SCSI
: IF_IDE
;
270 pstrcpy(devname
, sizeof(devname
), if_name
[type
]);
273 max_devs
= if_max_devs
[type
];
275 if (cyls
|| heads
|| secs
) {
276 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
277 error_report("invalid physical cyls number");
280 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
281 error_report("invalid physical heads number");
284 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
285 error_report("invalid physical secs number");
290 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
292 error_report("'%s' trans must be used with cyls, heads and secs",
296 if (!strcmp(buf
, "none"))
297 translation
= BIOS_ATA_TRANSLATION_NONE
;
298 else if (!strcmp(buf
, "lba"))
299 translation
= BIOS_ATA_TRANSLATION_LBA
;
300 else if (!strcmp(buf
, "auto"))
301 translation
= BIOS_ATA_TRANSLATION_AUTO
;
303 error_report("'%s' invalid translation type", buf
);
308 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
309 if (!strcmp(buf
, "disk")) {
311 } else if (!strcmp(buf
, "cdrom")) {
312 if (cyls
|| secs
|| heads
) {
313 error_report("CHS can't be set with media=%s", buf
);
318 error_report("'%s' invalid media", buf
);
323 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
324 if (bdrv_parse_cache_flags(buf
, &bdrv_flags
) != 0) {
325 error_report("invalid cache option");
330 #ifdef CONFIG_LINUX_AIO
331 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
332 if (!strcmp(buf
, "native")) {
333 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
334 } else if (!strcmp(buf
, "threads")) {
335 /* this is the default */
337 error_report("invalid aio option");
343 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
344 if (strcmp(buf
, "?") == 0) {
345 error_printf("Supported formats:");
346 bdrv_iterate_format(bdrv_format_print
, NULL
);
350 drv
= bdrv_find_whitelisted_format(buf
);
352 error_report("'%s' invalid format", buf
);
357 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
358 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
359 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
360 error_report("werror is not supported by this bus type");
364 on_write_error
= parse_block_error_action(buf
, 0);
365 if (on_write_error
< 0) {
370 on_read_error
= BLOCK_ERR_REPORT
;
371 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
372 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
373 error_report("rerror is not supported by this bus type");
377 on_read_error
= parse_block_error_action(buf
, 1);
378 if (on_read_error
< 0) {
383 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
384 if (type
!= IF_VIRTIO
) {
385 error_report("addr is not supported by this bus type");
390 /* compute bus and unit according index */
393 if (bus_id
!= 0 || unit_id
!= -1) {
394 error_report("index cannot be used with bus and unit");
397 bus_id
= drive_index_to_bus_id(type
, index
);
398 unit_id
= drive_index_to_unit_id(type
, index
);
401 /* if user doesn't specify a unit_id,
402 * try to find the first free
407 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
409 if (max_devs
&& unit_id
>= max_devs
) {
418 if (max_devs
&& unit_id
>= max_devs
) {
419 error_report("unit %d too big (max is %d)",
420 unit_id
, max_devs
- 1);
425 * catch multiple definitions
428 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
429 error_report("drive with bus=%d, unit=%d (index=%d) exists",
430 bus_id
, unit_id
, index
);
436 dinfo
= g_malloc0(sizeof(*dinfo
));
437 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
438 dinfo
->id
= g_strdup(buf
);
440 /* no id supplied -> create one */
441 dinfo
->id
= g_malloc0(32);
442 if (type
== IF_IDE
|| type
== IF_SCSI
)
443 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
445 snprintf(dinfo
->id
, 32, "%s%i%s%i",
446 devname
, bus_id
, mediastr
, unit_id
);
448 snprintf(dinfo
->id
, 32, "%s%s%i",
449 devname
, mediastr
, unit_id
);
451 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
452 dinfo
->devaddr
= devaddr
;
455 dinfo
->unit
= unit_id
;
459 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
460 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
462 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
472 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
473 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
477 bdrv_set_removable(dinfo
->bdrv
, 1);
483 /* FIXME: This isn't really a floppy, but it's a reasonable
486 bdrv_set_removable(dinfo
->bdrv
, 1);
492 /* add virtio block device */
493 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
494 qemu_opt_set(opts
, "driver", "virtio-blk");
495 qemu_opt_set(opts
, "drive", dinfo
->id
);
497 qemu_opt_set(opts
, "addr", devaddr
);
502 if (!file
|| !*file
) {
506 /* always use cache=unsafe with snapshot */
507 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
508 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
511 if (media
== MEDIA_CDROM
) {
512 /* CDROM is fine for any interface, don't check. */
514 } else if (ro
== 1) {
515 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
516 error_report("readonly not supported by this bus type");
521 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
523 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
525 error_report("could not open disk image %s: %s",
526 file
, strerror(-ret
));
530 if (bdrv_key_required(dinfo
->bdrv
))
535 bdrv_delete(dinfo
->bdrv
);
537 QTAILQ_REMOVE(&drives
, dinfo
, next
);
542 void do_commit(Monitor
*mon
, const QDict
*qdict
)
544 const char *device
= qdict_get_str(qdict
, "device");
545 BlockDriverState
*bs
;
547 if (!strcmp(device
, "all")) {
550 bs
= bdrv_find(device
);
552 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
559 int do_snapshot_blkdev(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
561 const char *device
= qdict_get_str(qdict
, "device");
562 const char *filename
= qdict_get_try_str(qdict
, "snapshot-file");
563 const char *format
= qdict_get_try_str(qdict
, "format");
564 BlockDriverState
*bs
;
565 BlockDriver
*drv
, *old_drv
, *proto_drv
;
568 char old_filename
[1024];
571 qerror_report(QERR_MISSING_PARAMETER
, "snapshot-file");
576 bs
= bdrv_find(device
);
578 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
583 pstrcpy(old_filename
, sizeof(old_filename
), bs
->filename
);
586 flags
= bs
->open_flags
;
592 drv
= bdrv_find_format(format
);
594 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
599 proto_drv
= bdrv_find_protocol(filename
);
601 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
606 ret
= bdrv_img_create(filename
, format
, bs
->filename
,
607 bs
->drv
->format_name
, NULL
, -1, flags
);
616 ret
= bdrv_open(bs
, filename
, flags
, drv
);
618 * If reopening the image file we just created fails, fall back
619 * and try to re-open the original image. If that fails too, we
620 * are in serious trouble.
623 ret
= bdrv_open(bs
, old_filename
, flags
, old_drv
);
625 qerror_report(QERR_OPEN_FILE_FAILED
, old_filename
);
627 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
638 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
640 if (!bdrv_is_removable(bs
)) {
641 qerror_report(QERR_DEVICE_NOT_REMOVABLE
, bdrv_get_device_name(bs
));
644 if (!force
&& bdrv_is_locked(bs
)) {
645 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
652 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
654 BlockDriverState
*bs
;
655 int force
= qdict_get_try_bool(qdict
, "force", 0);
656 const char *filename
= qdict_get_str(qdict
, "device");
658 bs
= bdrv_find(filename
);
660 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
663 return eject_device(mon
, bs
, force
);
666 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
669 BlockDriverState
*bs
;
672 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
674 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
678 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
679 if (err
== -EINVAL
) {
680 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
682 } else if (err
< 0) {
683 qerror_report(QERR_INVALID_PASSWORD
);
690 int do_change_block(Monitor
*mon
, const char *device
,
691 const char *filename
, const char *fmt
)
693 BlockDriverState
*bs
;
694 BlockDriver
*drv
= NULL
;
697 bs
= bdrv_find(device
);
699 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
703 drv
= bdrv_find_whitelisted_format(fmt
);
705 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
709 if (eject_device(mon
, bs
, 0) < 0) {
712 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
713 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
714 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
715 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
718 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
721 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
723 const char *id
= qdict_get_str(qdict
, "id");
724 BlockDriverState
*bs
;
728 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
731 if (bdrv_in_use(bs
)) {
732 qerror_report(QERR_DEVICE_IN_USE
, id
);
736 /* quiesce block driver; prevent further io */
741 /* if we have a device associated with this BlockDriverState (bs->peer)
742 * then we need to make the drive anonymous until the device
743 * can be removed. If this is a drive with no device backing
744 * then we can just get rid of the block driver state right here.
749 drive_uninit(drive_get_by_blockdev(bs
));
756 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
757 * existing QERR_ macro mess is cleaned up. A good example for better
758 * error reports can be found in the qemu-img resize code.
760 int do_block_resize(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
762 const char *device
= qdict_get_str(qdict
, "device");
763 int64_t size
= qdict_get_int(qdict
, "size");
764 BlockDriverState
*bs
;
766 bs
= bdrv_find(device
);
768 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
773 qerror_report(QERR_UNDEFINED_ERROR
);
777 if (bdrv_truncate(bs
, size
)) {
778 qerror_report(QERR_UNDEFINED_ERROR
);