kvm: init mp_state
[qemu.git] / blockdev.c
blob3b8c6067c7930abb7d9448dffca840ed85bc9512
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 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
20 QemuOpts *drive_add(const char *file, const char *fmt, ...)
22 va_list ap;
23 char optstr[1024];
24 QemuOpts *opts;
26 va_start(ap, fmt);
27 vsnprintf(optstr, sizeof(optstr), fmt, ap);
28 va_end(ap);
30 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
31 if (!opts) {
32 return NULL;
34 if (file)
35 qemu_opt_set(opts, "file", file);
36 return opts;
39 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
41 DriveInfo *dinfo;
43 /* seek interface, bus and unit */
45 QTAILQ_FOREACH(dinfo, &drives, next) {
46 if (dinfo->type == type &&
47 dinfo->bus == bus &&
48 dinfo->unit == unit)
49 return dinfo;
52 return NULL;
55 DriveInfo *drive_get_by_id(const char *id)
57 DriveInfo *dinfo;
59 QTAILQ_FOREACH(dinfo, &drives, next) {
60 if (strcmp(id, dinfo->id))
61 continue;
62 return dinfo;
64 return NULL;
67 int drive_get_max_bus(BlockInterfaceType type)
69 int max_bus;
70 DriveInfo *dinfo;
72 max_bus = -1;
73 QTAILQ_FOREACH(dinfo, &drives, next) {
74 if(dinfo->type == type &&
75 dinfo->bus > max_bus)
76 max_bus = dinfo->bus;
78 return max_bus;
81 const char *drive_get_serial(BlockDriverState *bdrv)
83 DriveInfo *dinfo;
85 QTAILQ_FOREACH(dinfo, &drives, next) {
86 if (dinfo->bdrv == bdrv)
87 return dinfo->serial;
90 return "\0";
93 static void bdrv_format_print(void *opaque, const char *name)
95 fprintf(stderr, " %s", name);
98 void drive_uninit(DriveInfo *dinfo)
100 qemu_opts_del(dinfo->opts);
101 bdrv_delete(dinfo->bdrv);
102 QTAILQ_REMOVE(&drives, dinfo, next);
103 qemu_free(dinfo);
106 static int parse_block_error_action(const char *buf, int is_read)
108 if (!strcmp(buf, "ignore")) {
109 return BLOCK_ERR_IGNORE;
110 } else if (!is_read && !strcmp(buf, "enospc")) {
111 return BLOCK_ERR_STOP_ENOSPC;
112 } else if (!strcmp(buf, "stop")) {
113 return BLOCK_ERR_STOP_ANY;
114 } else if (!strcmp(buf, "report")) {
115 return BLOCK_ERR_REPORT;
116 } else {
117 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
118 buf, is_read ? "read" : "write");
119 return -1;
123 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
125 const char *buf;
126 const char *file = NULL;
127 char devname[128];
128 const char *serial;
129 const char *mediastr = "";
130 BlockInterfaceType type;
131 enum { MEDIA_DISK, MEDIA_CDROM } media;
132 int bus_id, unit_id;
133 int cyls, heads, secs, translation;
134 BlockDriver *drv = NULL;
135 int max_devs;
136 int index;
137 int ro = 0;
138 int bdrv_flags = 0;
139 int on_read_error, on_write_error;
140 const char *devaddr;
141 DriveInfo *dinfo;
142 int snapshot = 0;
143 int ret;
145 *fatal_error = 1;
147 translation = BIOS_ATA_TRANSLATION_AUTO;
149 if (default_to_scsi) {
150 type = IF_SCSI;
151 max_devs = MAX_SCSI_DEVS;
152 pstrcpy(devname, sizeof(devname), "scsi");
153 } else {
154 type = IF_IDE;
155 max_devs = MAX_IDE_DEVS;
156 pstrcpy(devname, sizeof(devname), "ide");
158 media = MEDIA_DISK;
160 /* extract parameters */
161 bus_id = qemu_opt_get_number(opts, "bus", 0);
162 unit_id = qemu_opt_get_number(opts, "unit", -1);
163 index = qemu_opt_get_number(opts, "index", -1);
165 cyls = qemu_opt_get_number(opts, "cyls", 0);
166 heads = qemu_opt_get_number(opts, "heads", 0);
167 secs = qemu_opt_get_number(opts, "secs", 0);
169 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
170 ro = qemu_opt_get_bool(opts, "readonly", 0);
172 file = qemu_opt_get(opts, "file");
173 serial = qemu_opt_get(opts, "serial");
175 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
176 pstrcpy(devname, sizeof(devname), buf);
177 if (!strcmp(buf, "ide")) {
178 type = IF_IDE;
179 max_devs = MAX_IDE_DEVS;
180 } else if (!strcmp(buf, "scsi")) {
181 type = IF_SCSI;
182 max_devs = MAX_SCSI_DEVS;
183 } else if (!strcmp(buf, "floppy")) {
184 type = IF_FLOPPY;
185 max_devs = 0;
186 } else if (!strcmp(buf, "pflash")) {
187 type = IF_PFLASH;
188 max_devs = 0;
189 } else if (!strcmp(buf, "mtd")) {
190 type = IF_MTD;
191 max_devs = 0;
192 } else if (!strcmp(buf, "sd")) {
193 type = IF_SD;
194 max_devs = 0;
195 } else if (!strcmp(buf, "virtio")) {
196 type = IF_VIRTIO;
197 max_devs = 0;
198 } else if (!strcmp(buf, "xen")) {
199 type = IF_XEN;
200 max_devs = 0;
201 } else if (!strcmp(buf, "none")) {
202 type = IF_NONE;
203 max_devs = 0;
204 } else {
205 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
206 return NULL;
210 if (cyls || heads || secs) {
211 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
212 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
213 return NULL;
215 if (heads < 1 || (type == IF_IDE && heads > 16)) {
216 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
217 return NULL;
219 if (secs < 1 || (type == IF_IDE && secs > 63)) {
220 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
221 return NULL;
225 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
226 if (!cyls) {
227 fprintf(stderr,
228 "qemu: '%s' trans must be used with cyls,heads and secs\n",
229 buf);
230 return NULL;
232 if (!strcmp(buf, "none"))
233 translation = BIOS_ATA_TRANSLATION_NONE;
234 else if (!strcmp(buf, "lba"))
235 translation = BIOS_ATA_TRANSLATION_LBA;
236 else if (!strcmp(buf, "auto"))
237 translation = BIOS_ATA_TRANSLATION_AUTO;
238 else {
239 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
240 return NULL;
244 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
245 if (!strcmp(buf, "disk")) {
246 media = MEDIA_DISK;
247 } else if (!strcmp(buf, "cdrom")) {
248 if (cyls || secs || heads) {
249 fprintf(stderr,
250 "qemu: '%s' invalid physical CHS format\n", buf);
251 return NULL;
253 media = MEDIA_CDROM;
254 } else {
255 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
256 return NULL;
260 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
261 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
262 bdrv_flags |= BDRV_O_NOCACHE;
263 } else if (!strcmp(buf, "writeback")) {
264 bdrv_flags |= BDRV_O_CACHE_WB;
265 } else if (!strcmp(buf, "unsafe")) {
266 bdrv_flags |= BDRV_O_CACHE_WB;
267 bdrv_flags |= BDRV_O_NO_FLUSH;
268 } else if (!strcmp(buf, "writethrough")) {
269 /* this is the default */
270 } else {
271 fprintf(stderr, "qemu: invalid cache option\n");
272 return NULL;
276 #ifdef CONFIG_LINUX_AIO
277 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
278 if (!strcmp(buf, "native")) {
279 bdrv_flags |= BDRV_O_NATIVE_AIO;
280 } else if (!strcmp(buf, "threads")) {
281 /* this is the default */
282 } else {
283 fprintf(stderr, "qemu: invalid aio option\n");
284 return NULL;
287 #endif
289 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
290 if (strcmp(buf, "?") == 0) {
291 fprintf(stderr, "qemu: Supported formats:");
292 bdrv_iterate_format(bdrv_format_print, NULL);
293 fprintf(stderr, "\n");
294 return NULL;
296 drv = bdrv_find_whitelisted_format(buf);
297 if (!drv) {
298 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
299 return NULL;
303 on_write_error = BLOCK_ERR_STOP_ENOSPC;
304 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
305 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
306 fprintf(stderr, "werror is no supported by this format\n");
307 return NULL;
310 on_write_error = parse_block_error_action(buf, 0);
311 if (on_write_error < 0) {
312 return NULL;
316 on_read_error = BLOCK_ERR_REPORT;
317 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
318 if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
319 fprintf(stderr, "rerror is no supported by this format\n");
320 return NULL;
323 on_read_error = parse_block_error_action(buf, 1);
324 if (on_read_error < 0) {
325 return NULL;
329 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
330 if (type != IF_VIRTIO) {
331 fprintf(stderr, "addr is not supported\n");
332 return NULL;
336 /* compute bus and unit according index */
338 if (index != -1) {
339 if (bus_id != 0 || unit_id != -1) {
340 fprintf(stderr,
341 "qemu: index cannot be used with bus and unit\n");
342 return NULL;
344 if (max_devs == 0)
346 unit_id = index;
347 bus_id = 0;
348 } else {
349 unit_id = index % max_devs;
350 bus_id = index / max_devs;
354 /* if user doesn't specify a unit_id,
355 * try to find the first free
358 if (unit_id == -1) {
359 unit_id = 0;
360 while (drive_get(type, bus_id, unit_id) != NULL) {
361 unit_id++;
362 if (max_devs && unit_id >= max_devs) {
363 unit_id -= max_devs;
364 bus_id++;
369 /* check unit id */
371 if (max_devs && unit_id >= max_devs) {
372 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
373 unit_id, max_devs - 1);
374 return NULL;
378 * ignore multiple definitions
381 if (drive_get(type, bus_id, unit_id) != NULL) {
382 *fatal_error = 0;
383 return NULL;
386 /* init */
388 dinfo = qemu_mallocz(sizeof(*dinfo));
389 if ((buf = qemu_opts_id(opts)) != NULL) {
390 dinfo->id = qemu_strdup(buf);
391 } else {
392 /* no id supplied -> create one */
393 dinfo->id = qemu_mallocz(32);
394 if (type == IF_IDE || type == IF_SCSI)
395 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
396 if (max_devs)
397 snprintf(dinfo->id, 32, "%s%i%s%i",
398 devname, bus_id, mediastr, unit_id);
399 else
400 snprintf(dinfo->id, 32, "%s%s%i",
401 devname, mediastr, unit_id);
403 dinfo->bdrv = bdrv_new(dinfo->id);
404 dinfo->devaddr = devaddr;
405 dinfo->type = type;
406 dinfo->bus = bus_id;
407 dinfo->unit = unit_id;
408 dinfo->opts = opts;
409 if (serial)
410 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
411 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
413 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
415 switch(type) {
416 case IF_IDE:
417 case IF_SCSI:
418 case IF_XEN:
419 case IF_NONE:
420 switch(media) {
421 case MEDIA_DISK:
422 if (cyls != 0) {
423 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
424 bdrv_set_translation_hint(dinfo->bdrv, translation);
426 break;
427 case MEDIA_CDROM:
428 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
429 break;
431 break;
432 case IF_SD:
433 /* FIXME: This isn't really a floppy, but it's a reasonable
434 approximation. */
435 case IF_FLOPPY:
436 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
437 break;
438 case IF_PFLASH:
439 case IF_MTD:
440 break;
441 case IF_VIRTIO:
442 /* add virtio block device */
443 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
444 qemu_opt_set(opts, "driver", "virtio-blk-pci");
445 qemu_opt_set(opts, "drive", dinfo->id);
446 if (devaddr)
447 qemu_opt_set(opts, "addr", devaddr);
448 break;
449 case IF_COUNT:
450 abort();
452 if (!file || !*file) {
453 *fatal_error = 0;
454 return NULL;
456 if (snapshot) {
457 /* always use cache=unsafe with snapshot */
458 bdrv_flags &= ~BDRV_O_CACHE_MASK;
459 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
462 if (media == MEDIA_CDROM) {
463 /* CDROM is fine for any interface, don't check. */
464 ro = 1;
465 } else if (ro == 1) {
466 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
467 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
468 return NULL;
472 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
474 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
475 if (ret < 0) {
476 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
477 file, strerror(-ret));
478 return NULL;
481 if (bdrv_key_required(dinfo->bdrv))
482 autostart = 0;
483 *fatal_error = 0;
484 return dinfo;
487 void do_commit(Monitor *mon, const QDict *qdict)
489 const char *device = qdict_get_str(qdict, "device");
490 BlockDriverState *bs;
492 if (!strcmp(device, "all")) {
493 bdrv_commit_all();
494 } else {
495 bs = bdrv_find(device);
496 if (!bs) {
497 qerror_report(QERR_DEVICE_NOT_FOUND, device);
498 return;
500 bdrv_commit(bs);
504 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
506 if (!force) {
507 if (!bdrv_is_removable(bs)) {
508 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
509 bdrv_get_device_name(bs));
510 return -1;
512 if (bdrv_is_locked(bs)) {
513 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
514 return -1;
517 bdrv_close(bs);
518 return 0;
521 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
523 BlockDriverState *bs;
524 int force = qdict_get_int(qdict, "force");
525 const char *filename = qdict_get_str(qdict, "device");
527 bs = bdrv_find(filename);
528 if (!bs) {
529 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
530 return -1;
532 return eject_device(mon, bs, force);
535 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
536 QObject **ret_data)
538 BlockDriverState *bs;
539 int err;
541 bs = bdrv_find(qdict_get_str(qdict, "device"));
542 if (!bs) {
543 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
544 return -1;
547 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
548 if (err == -EINVAL) {
549 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
550 return -1;
551 } else if (err < 0) {
552 qerror_report(QERR_INVALID_PASSWORD);
553 return -1;
556 return 0;
559 int do_change_block(Monitor *mon, const char *device,
560 const char *filename, const char *fmt)
562 BlockDriverState *bs;
563 BlockDriver *drv = NULL;
564 int bdrv_flags;
566 bs = bdrv_find(device);
567 if (!bs) {
568 qerror_report(QERR_DEVICE_NOT_FOUND, device);
569 return -1;
571 if (fmt) {
572 drv = bdrv_find_whitelisted_format(fmt);
573 if (!drv) {
574 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
575 return -1;
578 if (eject_device(mon, bs, 0) < 0) {
579 return -1;
581 bdrv_flags = bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM ? 0 : BDRV_O_RDWR;
582 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
583 qerror_report(QERR_OPEN_FILE_FAILED, filename);
584 return -1;
586 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);