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 DriveInfo
*extboot_drive
= NULL
;
22 static QTAILQ_HEAD(drivelist
, DriveInfo
) drives
= QTAILQ_HEAD_INITIALIZER(drives
);
24 static const char *const if_name
[IF_COUNT
] = {
28 [IF_FLOPPY
] = "floppy",
29 [IF_PFLASH
] = "pflash",
32 [IF_VIRTIO
] = "virtio",
36 static const int if_max_devs
[IF_COUNT
] = {
38 * Do not change these numbers! They govern how drive option
39 * index maps to unit and bus. That mapping is ABI.
41 * All controllers used to imlement if=T drives need to support
42 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
43 * Otherwise, some index values map to "impossible" bus, unit
46 * For instance, if you change [IF_SCSI] to 255, -drive
47 * if=scsi,index=12 no longer means bus=1,unit=5, but
48 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
49 * the drive can't be set up. Regression.
56 * We automatically delete the drive when a device using it gets
57 * unplugged. Questionable feature, but we can't just drop it.
58 * Device models call blockdev_mark_auto_del() to schedule the
59 * automatic deletion, and generic qdev code calls blockdev_auto_del()
60 * when deletion is actually safe.
62 void blockdev_mark_auto_del(BlockDriverState
*bs
)
64 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
71 void blockdev_auto_del(BlockDriverState
*bs
)
73 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
75 if (dinfo
&& dinfo
->auto_del
) {
80 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
82 int max_devs
= if_max_devs
[type
];
83 return max_devs
? index
/ max_devs
: 0;
86 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
88 int max_devs
= if_max_devs
[type
];
89 return max_devs
? index
% max_devs
: index
;
92 QemuOpts
*drive_def(const char *optstr
)
94 return qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
97 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
103 opts
= drive_def(optstr
);
107 if (type
!= IF_DEFAULT
) {
108 qemu_opt_set(opts
, "if", if_name
[type
]);
111 snprintf(buf
, sizeof(buf
), "%d", index
);
112 qemu_opt_set(opts
, "index", buf
);
115 qemu_opt_set(opts
, "file", file
);
119 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
123 /* seek interface, bus and unit */
125 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
126 if (dinfo
->type
== type
&&
135 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
137 return drive_get(type
,
138 drive_index_to_bus_id(type
, index
),
139 drive_index_to_unit_id(type
, index
));
142 int drive_get_max_bus(BlockInterfaceType type
)
148 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
149 if(dinfo
->type
== type
&&
150 dinfo
->bus
> max_bus
)
151 max_bus
= dinfo
->bus
;
156 /* Get a block device. This should only be used for single-drive devices
157 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
159 DriveInfo
*drive_get_next(BlockInterfaceType type
)
161 static int next_block_unit
[IF_COUNT
];
163 return drive_get(type
, 0, next_block_unit
[type
]++);
166 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
170 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
171 if (dinfo
->bdrv
== bs
) {
178 static void bdrv_format_print(void *opaque
, const char *name
)
180 error_printf(" %s", name
);
183 static void drive_uninit(DriveInfo
*dinfo
)
185 qemu_opts_del(dinfo
->opts
);
186 bdrv_delete(dinfo
->bdrv
);
188 QTAILQ_REMOVE(&drives
, dinfo
, next
);
192 void drive_put_ref(DriveInfo
*dinfo
)
194 assert(dinfo
->refcount
);
195 if (--dinfo
->refcount
== 0) {
200 void drive_get_ref(DriveInfo
*dinfo
)
205 static int parse_block_error_action(const char *buf
, int is_read
)
207 if (!strcmp(buf
, "ignore")) {
208 return BLOCK_ERR_IGNORE
;
209 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
210 return BLOCK_ERR_STOP_ENOSPC
;
211 } else if (!strcmp(buf
, "stop")) {
212 return BLOCK_ERR_STOP_ANY
;
213 } else if (!strcmp(buf
, "report")) {
214 return BLOCK_ERR_REPORT
;
216 error_report("'%s' invalid %s error action",
217 buf
, is_read
? "read" : "write");
222 DriveInfo
*drive_init(QemuOpts
*opts
, int default_to_scsi
)
225 const char *file
= NULL
;
228 const char *mediastr
= "";
229 BlockInterfaceType type
;
230 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
232 int cyls
, heads
, secs
, translation
;
233 BlockDriver
*drv
= NULL
;
238 int on_read_error
, on_write_error
;
245 translation
= BIOS_ATA_TRANSLATION_AUTO
;
248 /* extract parameters */
249 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
250 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
251 index
= qemu_opt_get_number(opts
, "index", -1);
253 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
254 heads
= qemu_opt_get_number(opts
, "heads", 0);
255 secs
= qemu_opt_get_number(opts
, "secs", 0);
257 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
258 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
260 file
= qemu_opt_get(opts
, "file");
261 serial
= qemu_opt_get(opts
, "serial");
263 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
264 pstrcpy(devname
, sizeof(devname
), buf
);
265 for (type
= 0; type
< IF_COUNT
&& strcmp(buf
, if_name
[type
]); type
++)
267 if (type
== IF_COUNT
) {
268 error_report("unsupported bus type '%s'", buf
);
272 type
= default_to_scsi
? IF_SCSI
: IF_IDE
;
273 pstrcpy(devname
, sizeof(devname
), if_name
[type
]);
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("CHS can't be set with media=%s", buf
);
321 error_report("'%s' invalid media", buf
);
326 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
327 if (bdrv_parse_cache_flags(buf
, &bdrv_flags
) != 0) {
328 error_report("invalid cache option");
333 #ifdef CONFIG_LINUX_AIO
334 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
335 if (!strcmp(buf
, "native")) {
336 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
337 } else if (!strcmp(buf
, "threads")) {
338 /* this is the default */
340 error_report("invalid aio option");
346 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
347 if (strcmp(buf
, "?") == 0) {
348 error_printf("Supported formats:");
349 bdrv_iterate_format(bdrv_format_print
, NULL
);
353 drv
= bdrv_find_whitelisted_format(buf
);
355 error_report("'%s' invalid format", buf
);
360 is_extboot
= qemu_opt_get_bool(opts
, "boot", 0);
361 if (is_extboot
&& extboot_drive
) {
362 fprintf(stderr
, "qemu: two bootable drives specified\n");
366 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
367 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
368 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
369 error_report("werror is not supported by this bus type");
373 on_write_error
= parse_block_error_action(buf
, 0);
374 if (on_write_error
< 0) {
379 on_read_error
= BLOCK_ERR_REPORT
;
380 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
381 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
382 error_report("rerror is not supported by this bus type");
386 on_read_error
= parse_block_error_action(buf
, 1);
387 if (on_read_error
< 0) {
392 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
393 if (type
!= IF_VIRTIO
) {
394 error_report("addr is not supported by this bus type");
399 /* compute bus and unit according index */
402 if (bus_id
!= 0 || unit_id
!= -1) {
403 error_report("index cannot be used with bus and unit");
406 bus_id
= drive_index_to_bus_id(type
, index
);
407 unit_id
= drive_index_to_unit_id(type
, index
);
410 /* if user doesn't specify a unit_id,
411 * try to find the first free
416 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
418 if (max_devs
&& unit_id
>= max_devs
) {
427 if (max_devs
&& unit_id
>= max_devs
) {
428 error_report("unit %d too big (max is %d)",
429 unit_id
, max_devs
- 1);
434 * catch multiple definitions
437 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
438 error_report("drive with bus=%d, unit=%d (index=%d) exists",
439 bus_id
, unit_id
, index
);
445 dinfo
= g_malloc0(sizeof(*dinfo
));
446 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
447 dinfo
->id
= g_strdup(buf
);
449 /* no id supplied -> create one */
450 dinfo
->id
= g_malloc0(32);
451 if (type
== IF_IDE
|| type
== IF_SCSI
)
452 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
454 snprintf(dinfo
->id
, 32, "%s%i%s%i",
455 devname
, bus_id
, mediastr
, unit_id
);
457 snprintf(dinfo
->id
, 32, "%s%s%i",
458 devname
, mediastr
, unit_id
);
460 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
461 dinfo
->devaddr
= devaddr
;
464 dinfo
->unit
= unit_id
;
468 strncpy(dinfo
->serial
, serial
, sizeof(dinfo
->serial
) - 1);
469 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
472 extboot_drive
= dinfo
;
475 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
485 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
486 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
490 bdrv_set_removable(dinfo
->bdrv
, 1);
496 /* FIXME: This isn't really a floppy, but it's a reasonable
499 bdrv_set_removable(dinfo
->bdrv
, 1);
505 /* add virtio block device */
506 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
507 qemu_opt_set(opts
, "driver", "virtio-blk");
508 qemu_opt_set(opts
, "drive", dinfo
->id
);
510 qemu_opt_set(opts
, "addr", devaddr
);
515 if (!file
|| !*file
) {
519 /* always use cache=unsafe with snapshot */
520 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
521 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
524 if (media
== MEDIA_CDROM
) {
525 /* CDROM is fine for any interface, don't check. */
527 } else if (ro
== 1) {
528 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
529 error_report("readonly not supported by this bus type");
534 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
536 ret
= bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
);
538 error_report("could not open disk image %s: %s",
539 file
, strerror(-ret
));
543 if (bdrv_key_required(dinfo
->bdrv
))
548 bdrv_delete(dinfo
->bdrv
);
550 QTAILQ_REMOVE(&drives
, dinfo
, next
);
555 void do_commit(Monitor
*mon
, const QDict
*qdict
)
557 const char *device
= qdict_get_str(qdict
, "device");
558 BlockDriverState
*bs
;
560 if (!strcmp(device
, "all")) {
563 bs
= bdrv_find(device
);
565 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
572 int do_snapshot_blkdev(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
574 const char *device
= qdict_get_str(qdict
, "device");
575 const char *filename
= qdict_get_try_str(qdict
, "snapshot-file");
576 const char *format
= qdict_get_try_str(qdict
, "format");
577 BlockDriverState
*bs
;
578 BlockDriver
*drv
, *old_drv
, *proto_drv
;
581 char old_filename
[1024];
584 qerror_report(QERR_MISSING_PARAMETER
, "snapshot-file");
589 bs
= bdrv_find(device
);
591 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
596 pstrcpy(old_filename
, sizeof(old_filename
), bs
->filename
);
599 flags
= bs
->open_flags
;
605 drv
= bdrv_find_format(format
);
607 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
612 proto_drv
= bdrv_find_protocol(filename
);
614 qerror_report(QERR_INVALID_BLOCK_FORMAT
, format
);
619 ret
= bdrv_img_create(filename
, format
, bs
->filename
,
620 bs
->drv
->format_name
, NULL
, -1, flags
);
629 ret
= bdrv_open(bs
, filename
, flags
, drv
);
631 * If reopening the image file we just created fails, fall back
632 * and try to re-open the original image. If that fails too, we
633 * are in serious trouble.
636 ret
= bdrv_open(bs
, old_filename
, flags
, old_drv
);
638 qerror_report(QERR_OPEN_FILE_FAILED
, old_filename
);
640 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
651 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
653 if (!bdrv_is_removable(bs
)) {
654 qerror_report(QERR_DEVICE_NOT_REMOVABLE
, bdrv_get_device_name(bs
));
657 if (!force
&& bdrv_is_locked(bs
)) {
658 qerror_report(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
665 int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
667 BlockDriverState
*bs
;
668 int force
= qdict_get_try_bool(qdict
, "force", 0);
669 const char *filename
= qdict_get_str(qdict
, "device");
671 bs
= bdrv_find(filename
);
673 qerror_report(QERR_DEVICE_NOT_FOUND
, filename
);
676 return eject_device(mon
, bs
, force
);
679 int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
682 BlockDriverState
*bs
;
685 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
687 qerror_report(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
691 err
= bdrv_set_key(bs
, qdict_get_str(qdict
, "password"));
692 if (err
== -EINVAL
) {
693 qerror_report(QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
695 } else if (err
< 0) {
696 qerror_report(QERR_INVALID_PASSWORD
);
703 int do_change_block(Monitor
*mon
, const char *device
,
704 const char *filename
, const char *fmt
)
706 BlockDriverState
*bs
;
707 BlockDriver
*drv
= NULL
;
710 bs
= bdrv_find(device
);
712 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
716 drv
= bdrv_find_whitelisted_format(fmt
);
718 qerror_report(QERR_INVALID_BLOCK_FORMAT
, fmt
);
722 if (eject_device(mon
, bs
, 0) < 0) {
725 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
726 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
727 if (bdrv_open(bs
, filename
, bdrv_flags
, drv
) < 0) {
728 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
731 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
734 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
736 const char *id
= qdict_get_str(qdict
, "id");
737 BlockDriverState
*bs
;
741 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
744 if (bdrv_in_use(bs
)) {
745 qerror_report(QERR_DEVICE_IN_USE
, id
);
749 /* quiesce block driver; prevent further io */
754 /* if we have a device associated with this BlockDriverState (bs->peer)
755 * then we need to make the drive anonymous until the device
756 * can be removed. If this is a drive with no device backing
757 * then we can just get rid of the block driver state right here.
762 drive_uninit(drive_get_by_blockdev(bs
));
769 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
770 * existing QERR_ macro mess is cleaned up. A good example for better
771 * error reports can be found in the qemu-img resize code.
773 int do_block_resize(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
775 const char *device
= qdict_get_str(qdict
, "device");
776 int64_t size
= qdict_get_int(qdict
, "size");
777 BlockDriverState
*bs
;
779 bs
= bdrv_find(device
);
781 qerror_report(QERR_DEVICE_NOT_FOUND
, device
);
786 qerror_report(QERR_UNDEFINED_ERROR
);
790 if (bdrv_truncate(bs
, size
)) {
791 qerror_report(QERR_UNDEFINED_ERROR
);