Merge commit '666daa68234b5b1758652633cab07d5ca6046a5b' into upstream-merge
[qemu-kvm/amd-iommu.git] / blockdev.c
blob2d8954909d054f5b0f99fe81747b4ad96a76857d
1 /*
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.
8 */
10 #include "block.h"
11 #include "blockdev.h"
12 #include "monitor.h"
13 #include "qerror.h"
14 #include "qemu-option.h"
15 #include "qemu-config.h"
16 #include "sysemu.h"
18 DriveInfo *extboot_drive = NULL;
20 struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
22 QemuOpts *drive_add(const char *file, const char *fmt, ...)
24 va_list ap;
25 char optstr[1024];
26 QemuOpts *opts;
28 va_start(ap, fmt);
29 vsnprintf(optstr, sizeof(optstr), fmt, ap);
30 va_end(ap);
32 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
33 if (!opts) {
34 return NULL;
36 if (file)
37 qemu_opt_set(opts, "file", file);
38 return opts;
41 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
43 DriveInfo *dinfo;
45 /* seek interface, bus and unit */
47 QTAILQ_FOREACH(dinfo, &drives, next) {
48 if (dinfo->type == type &&
49 dinfo->bus == bus &&
50 dinfo->unit == unit)
51 return dinfo;
54 return NULL;
57 DriveInfo *drive_get_by_id(const char *id)
59 DriveInfo *dinfo;
61 QTAILQ_FOREACH(dinfo, &drives, next) {
62 if (strcmp(id, dinfo->id))
63 continue;
64 return dinfo;
66 return NULL;
69 int drive_get_max_bus(BlockInterfaceType type)
71 int max_bus;
72 DriveInfo *dinfo;
74 max_bus = -1;
75 QTAILQ_FOREACH(dinfo, &drives, next) {
76 if(dinfo->type == type &&
77 dinfo->bus > max_bus)
78 max_bus = dinfo->bus;
80 return max_bus;
83 const char *drive_get_serial(BlockDriverState *bdrv)
85 DriveInfo *dinfo;
87 QTAILQ_FOREACH(dinfo, &drives, next) {
88 if (dinfo->bdrv == bdrv)
89 return dinfo->serial;
92 return "\0";
95 BlockInterfaceErrorAction drive_get_on_error(
96 BlockDriverState *bdrv, int is_read)
98 DriveInfo *dinfo;
100 QTAILQ_FOREACH(dinfo, &drives, next) {
101 if (dinfo->bdrv == bdrv)
102 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
105 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
108 static void bdrv_format_print(void *opaque, const char *name)
110 fprintf(stderr, " %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);
118 qemu_free(dinfo);
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;
131 } else {
132 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
133 buf, is_read ? "read" : "write");
134 return -1;
138 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
140 const char *buf;
141 const char *file = NULL;
142 char devname[128];
143 const char *serial;
144 const char *mediastr = "";
145 BlockInterfaceType type;
146 enum { MEDIA_DISK, MEDIA_CDROM } media;
147 int bus_id, unit_id;
148 int cyls, heads, secs, translation;
149 BlockDriver *drv = NULL;
150 int max_devs;
151 int index;
152 int ro = 0;
153 int bdrv_flags = 0;
154 int on_read_error, on_write_error;
155 const char *devaddr;
156 DriveInfo *dinfo;
157 int snapshot = 0;
158 int ret;
160 *fatal_error = 1;
162 translation = BIOS_ATA_TRANSLATION_AUTO;
164 if (default_to_scsi) {
165 type = IF_SCSI;
166 max_devs = MAX_SCSI_DEVS;
167 pstrcpy(devname, sizeof(devname), "scsi");
168 } else {
169 type = IF_IDE;
170 max_devs = MAX_IDE_DEVS;
171 pstrcpy(devname, sizeof(devname), "ide");
173 media = MEDIA_DISK;
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")) {
193 type = IF_IDE;
194 max_devs = MAX_IDE_DEVS;
195 } else if (!strcmp(buf, "scsi")) {
196 type = IF_SCSI;
197 max_devs = MAX_SCSI_DEVS;
198 } else if (!strcmp(buf, "floppy")) {
199 type = IF_FLOPPY;
200 max_devs = 0;
201 } else if (!strcmp(buf, "pflash")) {
202 type = IF_PFLASH;
203 max_devs = 0;
204 } else if (!strcmp(buf, "mtd")) {
205 type = IF_MTD;
206 max_devs = 0;
207 } else if (!strcmp(buf, "sd")) {
208 type = IF_SD;
209 max_devs = 0;
210 } else if (!strcmp(buf, "virtio")) {
211 type = IF_VIRTIO;
212 max_devs = 0;
213 } else if (!strcmp(buf, "xen")) {
214 type = IF_XEN;
215 max_devs = 0;
216 } else if (!strcmp(buf, "none")) {
217 type = IF_NONE;
218 max_devs = 0;
219 } else {
220 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
221 return NULL;
225 if (cyls || heads || secs) {
226 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
227 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
228 return NULL;
230 if (heads < 1 || (type == IF_IDE && heads > 16)) {
231 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
232 return NULL;
234 if (secs < 1 || (type == IF_IDE && secs > 63)) {
235 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
236 return NULL;
240 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
241 if (!cyls) {
242 fprintf(stderr,
243 "qemu: '%s' trans must be used with cyls,heads and secs\n",
244 buf);
245 return NULL;
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;
253 else {
254 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
255 return NULL;
259 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
260 if (!strcmp(buf, "disk")) {
261 media = MEDIA_DISK;
262 } else if (!strcmp(buf, "cdrom")) {
263 if (cyls || secs || heads) {
264 fprintf(stderr,
265 "qemu: '%s' invalid physical CHS format\n", buf);
266 return NULL;
268 media = MEDIA_CDROM;
269 } else {
270 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
271 return NULL;
275 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
276 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
277 bdrv_flags |= BDRV_O_NOCACHE;
278 } else if (!strcmp(buf, "writeback")) {
279 bdrv_flags |= BDRV_O_CACHE_WB;
280 } else if (!strcmp(buf, "unsafe")) {
281 bdrv_flags |= BDRV_O_CACHE_WB;
282 bdrv_flags |= BDRV_O_NO_FLUSH;
283 } else if (!strcmp(buf, "writethrough")) {
284 /* this is the default */
285 } else {
286 fprintf(stderr, "qemu: invalid cache option\n");
287 return NULL;
291 #ifdef CONFIG_LINUX_AIO
292 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
293 if (!strcmp(buf, "native")) {
294 bdrv_flags |= BDRV_O_NATIVE_AIO;
295 } else if (!strcmp(buf, "threads")) {
296 /* this is the default */
297 } else {
298 fprintf(stderr, "qemu: invalid aio option\n");
299 return NULL;
302 #endif
304 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
305 if (strcmp(buf, "?") == 0) {
306 fprintf(stderr, "qemu: Supported formats:");
307 bdrv_iterate_format(bdrv_format_print, NULL);
308 fprintf(stderr, "\n");
309 return NULL;
311 drv = bdrv_find_whitelisted_format(buf);
312 if (!drv) {
313 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
314 return NULL;
318 on_write_error = BLOCK_ERR_STOP_ENOSPC;
319 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
320 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
321 fprintf(stderr, "werror is no supported by this format\n");
322 return NULL;
325 on_write_error = parse_block_error_action(buf, 0);
326 if (on_write_error < 0) {
327 return NULL;
331 on_read_error = BLOCK_ERR_REPORT;
332 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
333 if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
334 fprintf(stderr, "rerror is no supported by this format\n");
335 return NULL;
338 on_read_error = parse_block_error_action(buf, 1);
339 if (on_read_error < 0) {
340 return NULL;
344 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
345 if (type != IF_VIRTIO) {
346 fprintf(stderr, "addr is not supported\n");
347 return NULL;
351 /* compute bus and unit according index */
353 if (index != -1) {
354 if (bus_id != 0 || unit_id != -1) {
355 fprintf(stderr,
356 "qemu: index cannot be used with bus and unit\n");
357 return NULL;
359 if (max_devs == 0)
361 unit_id = index;
362 bus_id = 0;
363 } else {
364 unit_id = index % max_devs;
365 bus_id = index / max_devs;
369 /* if user doesn't specify a unit_id,
370 * try to find the first free
373 if (unit_id == -1) {
374 unit_id = 0;
375 while (drive_get(type, bus_id, unit_id) != NULL) {
376 unit_id++;
377 if (max_devs && unit_id >= max_devs) {
378 unit_id -= max_devs;
379 bus_id++;
384 /* check unit id */
386 if (max_devs && unit_id >= max_devs) {
387 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
388 unit_id, max_devs - 1);
389 return NULL;
393 * ignore multiple definitions
396 if (drive_get(type, bus_id, unit_id) != NULL) {
397 *fatal_error = 0;
398 return NULL;
401 /* init */
403 dinfo = qemu_mallocz(sizeof(*dinfo));
404 if ((buf = qemu_opts_id(opts)) != NULL) {
405 dinfo->id = qemu_strdup(buf);
406 } else {
407 /* no id supplied -> create one */
408 dinfo->id = qemu_mallocz(32);
409 if (type == IF_IDE || type == IF_SCSI)
410 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
411 if (max_devs)
412 snprintf(dinfo->id, 32, "%s%i%s%i",
413 devname, bus_id, mediastr, unit_id);
414 else
415 snprintf(dinfo->id, 32, "%s%s%i",
416 devname, mediastr, unit_id);
418 dinfo->bdrv = bdrv_new(dinfo->id);
419 dinfo->devaddr = devaddr;
420 dinfo->type = type;
421 dinfo->bus = bus_id;
422 dinfo->unit = unit_id;
423 dinfo->on_read_error = on_read_error;
424 dinfo->on_write_error = on_write_error;
425 dinfo->opts = opts;
426 if (serial)
427 strncpy(dinfo->serial, serial, sizeof(serial));
428 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
430 switch(type) {
431 case IF_IDE:
432 case IF_SCSI:
433 case IF_XEN:
434 case IF_NONE:
435 switch(media) {
436 case MEDIA_DISK:
437 if (cyls != 0) {
438 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
439 bdrv_set_translation_hint(dinfo->bdrv, translation);
441 break;
442 case MEDIA_CDROM:
443 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
444 break;
446 break;
447 case IF_SD:
448 /* FIXME: This isn't really a floppy, but it's a reasonable
449 approximation. */
450 case IF_FLOPPY:
451 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
452 break;
453 case IF_PFLASH:
454 case IF_MTD:
455 break;
456 case IF_VIRTIO:
457 /* add virtio block device */
458 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
459 qemu_opt_set(opts, "driver", "virtio-blk-pci");
460 qemu_opt_set(opts, "drive", dinfo->id);
461 if (devaddr)
462 qemu_opt_set(opts, "addr", devaddr);
463 break;
464 case IF_COUNT:
465 abort();
467 if (!file) {
468 *fatal_error = 0;
469 return NULL;
471 if (snapshot) {
472 /* always use cache=unsafe with snapshot */
473 bdrv_flags &= ~BDRV_O_CACHE_MASK;
474 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
477 if (media == MEDIA_CDROM) {
478 /* CDROM is fine for any interface, don't check. */
479 ro = 1;
480 } else if (ro == 1) {
481 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
482 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
483 return NULL;
487 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
489 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
490 if (ret < 0) {
491 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
492 file, strerror(-ret));
493 return NULL;
496 if (bdrv_key_required(dinfo->bdrv))
497 autostart = 0;
498 *fatal_error = 0;
499 return dinfo;
502 void do_commit(Monitor *mon, const QDict *qdict)
504 int all_devices;
505 DriveInfo *dinfo;
506 const char *device = qdict_get_str(qdict, "device");
508 all_devices = !strcmp(device, "all");
509 QTAILQ_FOREACH(dinfo, &drives, next) {
510 if (!all_devices)
511 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
512 continue;
513 bdrv_commit(dinfo->bdrv);
517 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
519 if (bdrv_is_inserted(bs)) {
520 if (!force) {
521 if (!bdrv_is_removable(bs)) {
522 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
523 bdrv_get_device_name(bs));
524 return -1;
526 if (bdrv_is_locked(bs)) {
527 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
528 return -1;
531 bdrv_close(bs);
533 return 0;
536 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
538 BlockDriverState *bs;
539 int force = qdict_get_int(qdict, "force");
540 const char *filename = qdict_get_str(qdict, "device");
542 bs = bdrv_find(filename);
543 if (!bs) {
544 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
545 return -1;
547 return eject_device(mon, bs, force);
550 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
551 QObject **ret_data)
553 BlockDriverState *bs;
554 int err;
556 bs = bdrv_find(qdict_get_str(qdict, "device"));
557 if (!bs) {
558 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
559 return -1;
562 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
563 if (err == -EINVAL) {
564 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
565 return -1;
566 } else if (err < 0) {
567 qerror_report(QERR_INVALID_PASSWORD);
568 return -1;
571 return 0;
574 int do_change_block(Monitor *mon, const char *device,
575 const char *filename, const char *fmt)
577 BlockDriverState *bs;
578 BlockDriver *drv = NULL;
579 int bdrv_flags;
581 bs = bdrv_find(device);
582 if (!bs) {
583 qerror_report(QERR_DEVICE_NOT_FOUND, device);
584 return -1;
586 if (fmt) {
587 drv = bdrv_find_whitelisted_format(fmt);
588 if (!drv) {
589 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
590 return -1;
593 if (eject_device(mon, bs, 0) < 0) {
594 return -1;
596 bdrv_flags = bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM ? 0 : BDRV_O_RDWR;
597 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
598 qerror_report(QERR_OPEN_FILE_FAILED, filename);
599 return -1;
601 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);