pci: Remove capability specific handlers
[qemu-kvm/stefanha.git] / blockdev.c
blobaf0a99e8c7ec6929db27ba9dd6518d6036f738a7
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);
23 * We automatically delete the drive when a device using it gets
24 * unplugged. Questionable feature, but we can't just drop it.
25 * Device models call blockdev_mark_auto_del() to schedule the
26 * automatic deletion, and generic qdev code calls blockdev_auto_del()
27 * when deletion is actually safe.
29 void blockdev_mark_auto_del(BlockDriverState *bs)
31 DriveInfo *dinfo = drive_get_by_blockdev(bs);
33 dinfo->auto_del = 1;
36 void blockdev_auto_del(BlockDriverState *bs)
38 DriveInfo *dinfo = drive_get_by_blockdev(bs);
40 if (dinfo->auto_del) {
41 drive_uninit(dinfo);
45 QemuOpts *drive_add(const char *file, const char *fmt, ...)
47 va_list ap;
48 char optstr[1024];
49 QemuOpts *opts;
51 va_start(ap, fmt);
52 vsnprintf(optstr, sizeof(optstr), fmt, ap);
53 va_end(ap);
55 opts = qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
56 if (!opts) {
57 return NULL;
59 if (file)
60 qemu_opt_set(opts, "file", file);
61 return opts;
64 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
66 DriveInfo *dinfo;
68 /* seek interface, bus and unit */
70 QTAILQ_FOREACH(dinfo, &drives, next) {
71 if (dinfo->type == type &&
72 dinfo->bus == bus &&
73 dinfo->unit == unit)
74 return dinfo;
77 return NULL;
80 int drive_get_max_bus(BlockInterfaceType type)
82 int max_bus;
83 DriveInfo *dinfo;
85 max_bus = -1;
86 QTAILQ_FOREACH(dinfo, &drives, next) {
87 if(dinfo->type == type &&
88 dinfo->bus > max_bus)
89 max_bus = dinfo->bus;
91 return max_bus;
94 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
96 DriveInfo *dinfo;
98 QTAILQ_FOREACH(dinfo, &drives, next) {
99 if (dinfo->bdrv == bs) {
100 return dinfo;
103 return NULL;
106 static void bdrv_format_print(void *opaque, const char *name)
108 fprintf(stderr, " %s", name);
111 void drive_uninit(DriveInfo *dinfo)
113 qemu_opts_del(dinfo->opts);
114 bdrv_delete(dinfo->bdrv);
115 QTAILQ_REMOVE(&drives, dinfo, next);
116 qemu_free(dinfo);
119 static int parse_block_error_action(const char *buf, int is_read)
121 if (!strcmp(buf, "ignore")) {
122 return BLOCK_ERR_IGNORE;
123 } else if (!is_read && !strcmp(buf, "enospc")) {
124 return BLOCK_ERR_STOP_ENOSPC;
125 } else if (!strcmp(buf, "stop")) {
126 return BLOCK_ERR_STOP_ANY;
127 } else if (!strcmp(buf, "report")) {
128 return BLOCK_ERR_REPORT;
129 } else {
130 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
131 buf, is_read ? "read" : "write");
132 return -1;
136 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
138 const char *buf;
139 const char *file = NULL;
140 char devname[128];
141 const char *serial;
142 const char *mediastr = "";
143 BlockInterfaceType type;
144 enum { MEDIA_DISK, MEDIA_CDROM } media;
145 int bus_id, unit_id;
146 int cyls, heads, secs, translation;
147 BlockDriver *drv = NULL;
148 int max_devs;
149 int index;
150 int ro = 0;
151 int bdrv_flags = 0;
152 int on_read_error, on_write_error;
153 const char *devaddr;
154 DriveInfo *dinfo;
155 int is_extboot = 0;
156 int snapshot = 0;
157 int ret;
159 *fatal_error = 1;
161 translation = BIOS_ATA_TRANSLATION_AUTO;
163 if (default_to_scsi) {
164 type = IF_SCSI;
165 max_devs = MAX_SCSI_DEVS;
166 pstrcpy(devname, sizeof(devname), "scsi");
167 } else {
168 type = IF_IDE;
169 max_devs = MAX_IDE_DEVS;
170 pstrcpy(devname, sizeof(devname), "ide");
172 media = MEDIA_DISK;
174 /* extract parameters */
175 bus_id = qemu_opt_get_number(opts, "bus", 0);
176 unit_id = qemu_opt_get_number(opts, "unit", -1);
177 index = qemu_opt_get_number(opts, "index", -1);
179 cyls = qemu_opt_get_number(opts, "cyls", 0);
180 heads = qemu_opt_get_number(opts, "heads", 0);
181 secs = qemu_opt_get_number(opts, "secs", 0);
183 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
184 ro = qemu_opt_get_bool(opts, "readonly", 0);
186 file = qemu_opt_get(opts, "file");
187 serial = qemu_opt_get(opts, "serial");
189 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
190 pstrcpy(devname, sizeof(devname), buf);
191 if (!strcmp(buf, "ide")) {
192 type = IF_IDE;
193 max_devs = MAX_IDE_DEVS;
194 } else if (!strcmp(buf, "scsi")) {
195 type = IF_SCSI;
196 max_devs = MAX_SCSI_DEVS;
197 } else if (!strcmp(buf, "floppy")) {
198 type = IF_FLOPPY;
199 max_devs = 0;
200 } else if (!strcmp(buf, "pflash")) {
201 type = IF_PFLASH;
202 max_devs = 0;
203 } else if (!strcmp(buf, "mtd")) {
204 type = IF_MTD;
205 max_devs = 0;
206 } else if (!strcmp(buf, "sd")) {
207 type = IF_SD;
208 max_devs = 0;
209 } else if (!strcmp(buf, "virtio")) {
210 type = IF_VIRTIO;
211 max_devs = 0;
212 } else if (!strcmp(buf, "xen")) {
213 type = IF_XEN;
214 max_devs = 0;
215 } else if (!strcmp(buf, "none")) {
216 type = IF_NONE;
217 max_devs = 0;
218 } else {
219 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
220 return NULL;
224 if (cyls || heads || secs) {
225 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
226 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
227 return NULL;
229 if (heads < 1 || (type == IF_IDE && heads > 16)) {
230 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
231 return NULL;
233 if (secs < 1 || (type == IF_IDE && secs > 63)) {
234 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
235 return NULL;
239 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
240 if (!cyls) {
241 fprintf(stderr,
242 "qemu: '%s' trans must be used with cyls,heads and secs\n",
243 buf);
244 return NULL;
246 if (!strcmp(buf, "none"))
247 translation = BIOS_ATA_TRANSLATION_NONE;
248 else if (!strcmp(buf, "lba"))
249 translation = BIOS_ATA_TRANSLATION_LBA;
250 else if (!strcmp(buf, "auto"))
251 translation = BIOS_ATA_TRANSLATION_AUTO;
252 else {
253 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
254 return NULL;
258 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
259 if (!strcmp(buf, "disk")) {
260 media = MEDIA_DISK;
261 } else if (!strcmp(buf, "cdrom")) {
262 if (cyls || secs || heads) {
263 fprintf(stderr,
264 "qemu: '%s' invalid physical CHS format\n", buf);
265 return NULL;
267 media = MEDIA_CDROM;
268 } else {
269 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
270 return NULL;
274 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
275 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
276 bdrv_flags |= BDRV_O_NOCACHE;
277 } else if (!strcmp(buf, "writeback")) {
278 bdrv_flags |= BDRV_O_CACHE_WB;
279 } else if (!strcmp(buf, "unsafe")) {
280 bdrv_flags |= BDRV_O_CACHE_WB;
281 bdrv_flags |= BDRV_O_NO_FLUSH;
282 } else if (!strcmp(buf, "writethrough")) {
283 /* this is the default */
284 } else {
285 fprintf(stderr, "qemu: invalid cache option\n");
286 return NULL;
290 #ifdef CONFIG_LINUX_AIO
291 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
292 if (!strcmp(buf, "native")) {
293 bdrv_flags |= BDRV_O_NATIVE_AIO;
294 } else if (!strcmp(buf, "threads")) {
295 /* this is the default */
296 } else {
297 fprintf(stderr, "qemu: invalid aio option\n");
298 return NULL;
301 #endif
303 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
304 if (strcmp(buf, "?") == 0) {
305 fprintf(stderr, "qemu: Supported formats:");
306 bdrv_iterate_format(bdrv_format_print, NULL);
307 fprintf(stderr, "\n");
308 return NULL;
310 drv = bdrv_find_whitelisted_format(buf);
311 if (!drv) {
312 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
313 return NULL;
317 is_extboot = qemu_opt_get_bool(opts, "boot", 0);
318 if (is_extboot && extboot_drive) {
319 fprintf(stderr, "qemu: two bootable drives specified\n");
320 return NULL;
323 on_write_error = BLOCK_ERR_STOP_ENOSPC;
324 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
325 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
326 fprintf(stderr, "werror is not supported by this format\n");
327 return NULL;
330 on_write_error = parse_block_error_action(buf, 0);
331 if (on_write_error < 0) {
332 return NULL;
336 on_read_error = BLOCK_ERR_REPORT;
337 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
338 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
339 fprintf(stderr, "rerror is not supported by this format\n");
340 return NULL;
343 on_read_error = parse_block_error_action(buf, 1);
344 if (on_read_error < 0) {
345 return NULL;
349 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
350 if (type != IF_VIRTIO) {
351 fprintf(stderr, "addr is not supported\n");
352 return NULL;
356 /* compute bus and unit according index */
358 if (index != -1) {
359 if (bus_id != 0 || unit_id != -1) {
360 fprintf(stderr,
361 "qemu: index cannot be used with bus and unit\n");
362 return NULL;
364 if (max_devs == 0)
366 unit_id = index;
367 bus_id = 0;
368 } else {
369 unit_id = index % max_devs;
370 bus_id = index / max_devs;
374 /* if user doesn't specify a unit_id,
375 * try to find the first free
378 if (unit_id == -1) {
379 unit_id = 0;
380 while (drive_get(type, bus_id, unit_id) != NULL) {
381 unit_id++;
382 if (max_devs && unit_id >= max_devs) {
383 unit_id -= max_devs;
384 bus_id++;
389 /* check unit id */
391 if (max_devs && unit_id >= max_devs) {
392 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
393 unit_id, max_devs - 1);
394 return NULL;
398 * ignore multiple definitions
401 if (drive_get(type, bus_id, unit_id) != NULL) {
402 *fatal_error = 0;
403 return NULL;
406 /* init */
408 dinfo = qemu_mallocz(sizeof(*dinfo));
409 if ((buf = qemu_opts_id(opts)) != NULL) {
410 dinfo->id = qemu_strdup(buf);
411 } else {
412 /* no id supplied -> create one */
413 dinfo->id = qemu_mallocz(32);
414 if (type == IF_IDE || type == IF_SCSI)
415 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
416 if (max_devs)
417 snprintf(dinfo->id, 32, "%s%i%s%i",
418 devname, bus_id, mediastr, unit_id);
419 else
420 snprintf(dinfo->id, 32, "%s%s%i",
421 devname, mediastr, unit_id);
423 dinfo->bdrv = bdrv_new(dinfo->id);
424 dinfo->devaddr = devaddr;
425 dinfo->type = type;
426 dinfo->bus = bus_id;
427 dinfo->unit = unit_id;
428 dinfo->opts = opts;
429 if (serial)
430 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
431 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
433 if (is_extboot) {
434 extboot_drive = dinfo;
437 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
439 switch(type) {
440 case IF_IDE:
441 case IF_SCSI:
442 case IF_XEN:
443 case IF_NONE:
444 switch(media) {
445 case MEDIA_DISK:
446 if (cyls != 0) {
447 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
448 bdrv_set_translation_hint(dinfo->bdrv, translation);
450 break;
451 case MEDIA_CDROM:
452 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
453 break;
455 break;
456 case IF_SD:
457 /* FIXME: This isn't really a floppy, but it's a reasonable
458 approximation. */
459 case IF_FLOPPY:
460 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
461 break;
462 case IF_PFLASH:
463 case IF_MTD:
464 break;
465 case IF_VIRTIO:
466 /* add virtio block device */
467 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
468 qemu_opt_set(opts, "driver", "virtio-blk-pci");
469 qemu_opt_set(opts, "drive", dinfo->id);
470 if (devaddr)
471 qemu_opt_set(opts, "addr", devaddr);
472 break;
473 case IF_COUNT:
474 abort();
476 if (!file || !*file) {
477 *fatal_error = 0;
478 return NULL;
480 if (snapshot) {
481 /* always use cache=unsafe with snapshot */
482 bdrv_flags &= ~BDRV_O_CACHE_MASK;
483 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
486 if (media == MEDIA_CDROM) {
487 /* CDROM is fine for any interface, don't check. */
488 ro = 1;
489 } else if (ro == 1) {
490 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
491 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
492 return NULL;
496 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
498 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
499 if (ret < 0) {
500 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
501 file, strerror(-ret));
502 return NULL;
505 if (bdrv_key_required(dinfo->bdrv))
506 autostart = 0;
507 *fatal_error = 0;
508 return dinfo;
511 void do_commit(Monitor *mon, const QDict *qdict)
513 const char *device = qdict_get_str(qdict, "device");
514 BlockDriverState *bs;
516 if (!strcmp(device, "all")) {
517 bdrv_commit_all();
518 } else {
519 bs = bdrv_find(device);
520 if (!bs) {
521 qerror_report(QERR_DEVICE_NOT_FOUND, device);
522 return;
524 bdrv_commit(bs);
528 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
530 if (!force) {
531 if (!bdrv_is_removable(bs)) {
532 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
533 bdrv_get_device_name(bs));
534 return -1;
536 if (bdrv_is_locked(bs)) {
537 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
538 return -1;
541 bdrv_close(bs);
542 return 0;
545 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
547 BlockDriverState *bs;
548 int force = qdict_get_try_bool(qdict, "force", 0);
549 const char *filename = qdict_get_str(qdict, "device");
551 bs = bdrv_find(filename);
552 if (!bs) {
553 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
554 return -1;
556 return eject_device(mon, bs, force);
559 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
560 QObject **ret_data)
562 BlockDriverState *bs;
563 int err;
565 bs = bdrv_find(qdict_get_str(qdict, "device"));
566 if (!bs) {
567 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
568 return -1;
571 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
572 if (err == -EINVAL) {
573 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
574 return -1;
575 } else if (err < 0) {
576 qerror_report(QERR_INVALID_PASSWORD);
577 return -1;
580 return 0;
583 int do_change_block(Monitor *mon, const char *device,
584 const char *filename, const char *fmt)
586 BlockDriverState *bs;
587 BlockDriver *drv = NULL;
588 int bdrv_flags;
590 bs = bdrv_find(device);
591 if (!bs) {
592 qerror_report(QERR_DEVICE_NOT_FOUND, device);
593 return -1;
595 if (fmt) {
596 drv = bdrv_find_whitelisted_format(fmt);
597 if (!drv) {
598 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
599 return -1;
602 if (eject_device(mon, bs, 0) < 0) {
603 return -1;
605 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
606 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
607 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
608 qerror_report(QERR_OPEN_FILE_FAILED, filename);
609 return -1;
611 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);