ac97: IOMMU support
[qemu-kvm/amd-iommu.git] / blockdev.c
blob965a089e84f60edaccef8435084196d0a54bfe33
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 static QTAILQ_HEAD(drivelist, DriveInfo) 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 static void bdrv_format_print(void *opaque, const char *name)
97 fprintf(stderr, " %s", name);
100 void drive_uninit(DriveInfo *dinfo)
102 qemu_opts_del(dinfo->opts);
103 bdrv_delete(dinfo->bdrv);
104 QTAILQ_REMOVE(&drives, dinfo, next);
105 qemu_free(dinfo);
108 static int parse_block_error_action(const char *buf, int is_read)
110 if (!strcmp(buf, "ignore")) {
111 return BLOCK_ERR_IGNORE;
112 } else if (!is_read && !strcmp(buf, "enospc")) {
113 return BLOCK_ERR_STOP_ENOSPC;
114 } else if (!strcmp(buf, "stop")) {
115 return BLOCK_ERR_STOP_ANY;
116 } else if (!strcmp(buf, "report")) {
117 return BLOCK_ERR_REPORT;
118 } else {
119 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
120 buf, is_read ? "read" : "write");
121 return -1;
125 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
127 const char *buf;
128 const char *file = NULL;
129 char devname[128];
130 const char *serial;
131 const char *mediastr = "";
132 BlockInterfaceType type;
133 enum { MEDIA_DISK, MEDIA_CDROM } media;
134 int bus_id, unit_id;
135 int cyls, heads, secs, translation;
136 BlockDriver *drv = NULL;
137 int max_devs;
138 int index;
139 int ro = 0;
140 int bdrv_flags = 0;
141 int on_read_error, on_write_error;
142 const char *devaddr;
143 DriveInfo *dinfo;
144 int is_extboot = 0;
145 int snapshot = 0;
146 int ret;
148 *fatal_error = 1;
150 translation = BIOS_ATA_TRANSLATION_AUTO;
152 if (default_to_scsi) {
153 type = IF_SCSI;
154 max_devs = MAX_SCSI_DEVS;
155 pstrcpy(devname, sizeof(devname), "scsi");
156 } else {
157 type = IF_IDE;
158 max_devs = MAX_IDE_DEVS;
159 pstrcpy(devname, sizeof(devname), "ide");
161 media = MEDIA_DISK;
163 /* extract parameters */
164 bus_id = qemu_opt_get_number(opts, "bus", 0);
165 unit_id = qemu_opt_get_number(opts, "unit", -1);
166 index = qemu_opt_get_number(opts, "index", -1);
168 cyls = qemu_opt_get_number(opts, "cyls", 0);
169 heads = qemu_opt_get_number(opts, "heads", 0);
170 secs = qemu_opt_get_number(opts, "secs", 0);
172 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
173 ro = qemu_opt_get_bool(opts, "readonly", 0);
175 file = qemu_opt_get(opts, "file");
176 serial = qemu_opt_get(opts, "serial");
178 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
179 pstrcpy(devname, sizeof(devname), buf);
180 if (!strcmp(buf, "ide")) {
181 type = IF_IDE;
182 max_devs = MAX_IDE_DEVS;
183 } else if (!strcmp(buf, "scsi")) {
184 type = IF_SCSI;
185 max_devs = MAX_SCSI_DEVS;
186 } else if (!strcmp(buf, "floppy")) {
187 type = IF_FLOPPY;
188 max_devs = 0;
189 } else if (!strcmp(buf, "pflash")) {
190 type = IF_PFLASH;
191 max_devs = 0;
192 } else if (!strcmp(buf, "mtd")) {
193 type = IF_MTD;
194 max_devs = 0;
195 } else if (!strcmp(buf, "sd")) {
196 type = IF_SD;
197 max_devs = 0;
198 } else if (!strcmp(buf, "virtio")) {
199 type = IF_VIRTIO;
200 max_devs = 0;
201 } else if (!strcmp(buf, "xen")) {
202 type = IF_XEN;
203 max_devs = 0;
204 } else if (!strcmp(buf, "none")) {
205 type = IF_NONE;
206 max_devs = 0;
207 } else {
208 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
209 return NULL;
213 if (cyls || heads || secs) {
214 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
215 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
216 return NULL;
218 if (heads < 1 || (type == IF_IDE && heads > 16)) {
219 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
220 return NULL;
222 if (secs < 1 || (type == IF_IDE && secs > 63)) {
223 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
224 return NULL;
228 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
229 if (!cyls) {
230 fprintf(stderr,
231 "qemu: '%s' trans must be used with cyls,heads and secs\n",
232 buf);
233 return NULL;
235 if (!strcmp(buf, "none"))
236 translation = BIOS_ATA_TRANSLATION_NONE;
237 else if (!strcmp(buf, "lba"))
238 translation = BIOS_ATA_TRANSLATION_LBA;
239 else if (!strcmp(buf, "auto"))
240 translation = BIOS_ATA_TRANSLATION_AUTO;
241 else {
242 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
243 return NULL;
247 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
248 if (!strcmp(buf, "disk")) {
249 media = MEDIA_DISK;
250 } else if (!strcmp(buf, "cdrom")) {
251 if (cyls || secs || heads) {
252 fprintf(stderr,
253 "qemu: '%s' invalid physical CHS format\n", buf);
254 return NULL;
256 media = MEDIA_CDROM;
257 } else {
258 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
259 return NULL;
263 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
264 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
265 bdrv_flags |= BDRV_O_NOCACHE;
266 } else if (!strcmp(buf, "writeback")) {
267 bdrv_flags |= BDRV_O_CACHE_WB;
268 } else if (!strcmp(buf, "unsafe")) {
269 bdrv_flags |= BDRV_O_CACHE_WB;
270 bdrv_flags |= BDRV_O_NO_FLUSH;
271 } else if (!strcmp(buf, "writethrough")) {
272 /* this is the default */
273 } else {
274 fprintf(stderr, "qemu: invalid cache option\n");
275 return NULL;
279 #ifdef CONFIG_LINUX_AIO
280 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
281 if (!strcmp(buf, "native")) {
282 bdrv_flags |= BDRV_O_NATIVE_AIO;
283 } else if (!strcmp(buf, "threads")) {
284 /* this is the default */
285 } else {
286 fprintf(stderr, "qemu: invalid aio option\n");
287 return NULL;
290 #endif
292 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
293 if (strcmp(buf, "?") == 0) {
294 fprintf(stderr, "qemu: Supported formats:");
295 bdrv_iterate_format(bdrv_format_print, NULL);
296 fprintf(stderr, "\n");
297 return NULL;
299 drv = bdrv_find_whitelisted_format(buf);
300 if (!drv) {
301 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
302 return NULL;
306 is_extboot = qemu_opt_get_bool(opts, "boot", 0);
307 if (is_extboot && extboot_drive) {
308 fprintf(stderr, "qemu: two bootable drives specified\n");
309 return NULL;
312 on_write_error = BLOCK_ERR_STOP_ENOSPC;
313 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
314 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
315 fprintf(stderr, "werror is no supported by this format\n");
316 return NULL;
319 on_write_error = parse_block_error_action(buf, 0);
320 if (on_write_error < 0) {
321 return NULL;
325 on_read_error = BLOCK_ERR_REPORT;
326 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
327 if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
328 fprintf(stderr, "rerror is no supported by this format\n");
329 return NULL;
332 on_read_error = parse_block_error_action(buf, 1);
333 if (on_read_error < 0) {
334 return NULL;
338 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
339 if (type != IF_VIRTIO) {
340 fprintf(stderr, "addr is not supported\n");
341 return NULL;
345 /* compute bus and unit according index */
347 if (index != -1) {
348 if (bus_id != 0 || unit_id != -1) {
349 fprintf(stderr,
350 "qemu: index cannot be used with bus and unit\n");
351 return NULL;
353 if (max_devs == 0)
355 unit_id = index;
356 bus_id = 0;
357 } else {
358 unit_id = index % max_devs;
359 bus_id = index / max_devs;
363 /* if user doesn't specify a unit_id,
364 * try to find the first free
367 if (unit_id == -1) {
368 unit_id = 0;
369 while (drive_get(type, bus_id, unit_id) != NULL) {
370 unit_id++;
371 if (max_devs && unit_id >= max_devs) {
372 unit_id -= max_devs;
373 bus_id++;
378 /* check unit id */
380 if (max_devs && unit_id >= max_devs) {
381 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
382 unit_id, max_devs - 1);
383 return NULL;
387 * ignore multiple definitions
390 if (drive_get(type, bus_id, unit_id) != NULL) {
391 *fatal_error = 0;
392 return NULL;
395 /* init */
397 dinfo = qemu_mallocz(sizeof(*dinfo));
398 if ((buf = qemu_opts_id(opts)) != NULL) {
399 dinfo->id = qemu_strdup(buf);
400 } else {
401 /* no id supplied -> create one */
402 dinfo->id = qemu_mallocz(32);
403 if (type == IF_IDE || type == IF_SCSI)
404 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
405 if (max_devs)
406 snprintf(dinfo->id, 32, "%s%i%s%i",
407 devname, bus_id, mediastr, unit_id);
408 else
409 snprintf(dinfo->id, 32, "%s%s%i",
410 devname, mediastr, unit_id);
412 dinfo->bdrv = bdrv_new(dinfo->id);
413 dinfo->devaddr = devaddr;
414 dinfo->type = type;
415 dinfo->bus = bus_id;
416 dinfo->unit = unit_id;
417 dinfo->opts = opts;
418 if (serial)
419 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
420 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
422 if (is_extboot) {
423 extboot_drive = dinfo;
426 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
428 switch(type) {
429 case IF_IDE:
430 case IF_SCSI:
431 case IF_XEN:
432 case IF_NONE:
433 switch(media) {
434 case MEDIA_DISK:
435 if (cyls != 0) {
436 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
437 bdrv_set_translation_hint(dinfo->bdrv, translation);
439 break;
440 case MEDIA_CDROM:
441 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
442 break;
444 break;
445 case IF_SD:
446 /* FIXME: This isn't really a floppy, but it's a reasonable
447 approximation. */
448 case IF_FLOPPY:
449 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
450 break;
451 case IF_PFLASH:
452 case IF_MTD:
453 break;
454 case IF_VIRTIO:
455 /* add virtio block device */
456 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
457 qemu_opt_set(opts, "driver", "virtio-blk-pci");
458 qemu_opt_set(opts, "drive", dinfo->id);
459 if (devaddr)
460 qemu_opt_set(opts, "addr", devaddr);
461 break;
462 case IF_COUNT:
463 abort();
465 if (!file || !*file) {
466 *fatal_error = 0;
467 return NULL;
469 if (snapshot) {
470 /* always use cache=unsafe with snapshot */
471 bdrv_flags &= ~BDRV_O_CACHE_MASK;
472 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
475 if (media == MEDIA_CDROM) {
476 /* CDROM is fine for any interface, don't check. */
477 ro = 1;
478 } else if (ro == 1) {
479 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
480 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
481 return NULL;
485 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
487 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
488 if (ret < 0) {
489 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
490 file, strerror(-ret));
491 return NULL;
494 if (bdrv_key_required(dinfo->bdrv))
495 autostart = 0;
496 *fatal_error = 0;
497 return dinfo;
500 void do_commit(Monitor *mon, const QDict *qdict)
502 const char *device = qdict_get_str(qdict, "device");
503 BlockDriverState *bs;
505 if (!strcmp(device, "all")) {
506 bdrv_commit_all();
507 } else {
508 bs = bdrv_find(device);
509 if (!bs) {
510 qerror_report(QERR_DEVICE_NOT_FOUND, device);
511 return;
513 bdrv_commit(bs);
517 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
519 if (!force) {
520 if (!bdrv_is_removable(bs)) {
521 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
522 bdrv_get_device_name(bs));
523 return -1;
525 if (bdrv_is_locked(bs)) {
526 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
527 return -1;
530 bdrv_close(bs);
531 return 0;
534 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
536 BlockDriverState *bs;
537 int force = qdict_get_int(qdict, "force");
538 const char *filename = qdict_get_str(qdict, "device");
540 bs = bdrv_find(filename);
541 if (!bs) {
542 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
543 return -1;
545 return eject_device(mon, bs, force);
548 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
549 QObject **ret_data)
551 BlockDriverState *bs;
552 int err;
554 bs = bdrv_find(qdict_get_str(qdict, "device"));
555 if (!bs) {
556 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
557 return -1;
560 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
561 if (err == -EINVAL) {
562 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
563 return -1;
564 } else if (err < 0) {
565 qerror_report(QERR_INVALID_PASSWORD);
566 return -1;
569 return 0;
572 int do_change_block(Monitor *mon, const char *device,
573 const char *filename, const char *fmt)
575 BlockDriverState *bs;
576 BlockDriver *drv = NULL;
577 int bdrv_flags;
579 bs = bdrv_find(device);
580 if (!bs) {
581 qerror_report(QERR_DEVICE_NOT_FOUND, device);
582 return -1;
584 if (fmt) {
585 drv = bdrv_find_whitelisted_format(fmt);
586 if (!drv) {
587 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
588 return -1;
591 if (eject_device(mon, bs, 0) < 0) {
592 return -1;
594 bdrv_flags = bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM ? 0 : BDRV_O_RDWR;
595 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
596 qerror_report(QERR_OPEN_FILE_FAILED, filename);
597 return -1;
599 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);