blob: f2a00bd214a0f8953570aab239cffb7a76982988 [file] [log] [blame]
Markus Armbruster666daa62010-06-02 18:48:27 +02001/*
2 * QEMU host block devices
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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 */
9
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"
Ryan Harper9063f812010-11-12 11:07:13 -060017#include "hw/qdev.h"
18#include "block_int.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020019
Markus Armbrusterc9b62a72010-06-02 18:55:22 +020020static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
Markus Armbruster666daa62010-06-02 18:48:27 +020021
Markus Armbruster19609662011-01-28 11:21:39 +010022static const char *const if_name[IF_COUNT] = {
23 [IF_NONE] = "none",
24 [IF_IDE] = "ide",
25 [IF_SCSI] = "scsi",
26 [IF_FLOPPY] = "floppy",
27 [IF_PFLASH] = "pflash",
28 [IF_MTD] = "mtd",
29 [IF_SD] = "sd",
30 [IF_VIRTIO] = "virtio",
31 [IF_XEN] = "xen",
32};
33
34static const int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010035 /*
36 * Do not change these numbers! They govern how drive option
37 * index maps to unit and bus. That mapping is ABI.
38 *
39 * All controllers used to imlement if=T drives need to support
40 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
41 * Otherwise, some index values map to "impossible" bus, unit
42 * values.
43 *
44 * For instance, if you change [IF_SCSI] to 255, -drive
45 * if=scsi,index=12 no longer means bus=1,unit=5, but
46 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
47 * the drive can't be set up. Regression.
48 */
49 [IF_IDE] = 2,
50 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +010051};
52
Markus Armbruster14bafc52010-06-25 08:09:10 +020053/*
54 * We automatically delete the drive when a device using it gets
55 * unplugged. Questionable feature, but we can't just drop it.
56 * Device models call blockdev_mark_auto_del() to schedule the
57 * automatic deletion, and generic qdev code calls blockdev_auto_del()
58 * when deletion is actually safe.
59 */
60void blockdev_mark_auto_del(BlockDriverState *bs)
61{
62 DriveInfo *dinfo = drive_get_by_blockdev(bs);
63
Ryan Harper0fc0f1f2010-12-08 10:05:00 -060064 if (dinfo) {
65 dinfo->auto_del = 1;
66 }
Markus Armbruster14bafc52010-06-25 08:09:10 +020067}
68
69void blockdev_auto_del(BlockDriverState *bs)
70{
71 DriveInfo *dinfo = drive_get_by_blockdev(bs);
72
Ryan Harper0fc0f1f2010-12-08 10:05:00 -060073 if (dinfo && dinfo->auto_del) {
Marcelo Tosatti84fb3922011-01-26 12:12:32 -020074 drive_put_ref(dinfo);
Markus Armbruster14bafc52010-06-25 08:09:10 +020075 }
76}
77
Markus Armbruster505a7fb2011-01-28 11:21:43 +010078static int drive_index_to_bus_id(BlockInterfaceType type, int index)
79{
80 int max_devs = if_max_devs[type];
81 return max_devs ? index / max_devs : 0;
82}
83
84static int drive_index_to_unit_id(BlockInterfaceType type, int index)
85{
86 int max_devs = if_max_devs[type];
87 return max_devs ? index % max_devs : index;
88}
89
Markus Armbruster2292dda2011-01-28 11:21:41 +010090QemuOpts *drive_def(const char *optstr)
91{
92 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
93}
94
95QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +010096 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +020097{
Markus Armbruster666daa62010-06-02 18:48:27 +020098 QemuOpts *opts;
Markus Armbruster2292dda2011-01-28 11:21:41 +010099 char buf[32];
Markus Armbruster666daa62010-06-02 18:48:27 +0200100
Markus Armbruster2292dda2011-01-28 11:21:41 +0100101 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200102 if (!opts) {
103 return NULL;
104 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100105 if (type != IF_DEFAULT) {
106 qemu_opt_set(opts, "if", if_name[type]);
107 }
108 if (index >= 0) {
109 snprintf(buf, sizeof(buf), "%d", index);
110 qemu_opt_set(opts, "index", buf);
111 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200112 if (file)
113 qemu_opt_set(opts, "file", file);
114 return opts;
115}
116
117DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
118{
119 DriveInfo *dinfo;
120
121 /* seek interface, bus and unit */
122
123 QTAILQ_FOREACH(dinfo, &drives, next) {
124 if (dinfo->type == type &&
125 dinfo->bus == bus &&
126 dinfo->unit == unit)
127 return dinfo;
128 }
129
130 return NULL;
131}
132
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100133DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
134{
135 return drive_get(type,
136 drive_index_to_bus_id(type, index),
137 drive_index_to_unit_id(type, index));
138}
139
Markus Armbruster666daa62010-06-02 18:48:27 +0200140int drive_get_max_bus(BlockInterfaceType type)
141{
142 int max_bus;
143 DriveInfo *dinfo;
144
145 max_bus = -1;
146 QTAILQ_FOREACH(dinfo, &drives, next) {
147 if(dinfo->type == type &&
148 dinfo->bus > max_bus)
149 max_bus = dinfo->bus;
150 }
151 return max_bus;
152}
153
Markus Armbruster13839972011-01-28 11:21:37 +0100154/* Get a block device. This should only be used for single-drive devices
155 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
156 appropriate bus. */
157DriveInfo *drive_get_next(BlockInterfaceType type)
158{
159 static int next_block_unit[IF_COUNT];
160
161 return drive_get(type, 0, next_block_unit[type]++);
162}
163
Markus Armbrustere4700e52010-06-24 17:25:32 +0200164DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
Markus Armbruster666daa62010-06-02 18:48:27 +0200165{
166 DriveInfo *dinfo;
167
168 QTAILQ_FOREACH(dinfo, &drives, next) {
Markus Armbrustere4700e52010-06-24 17:25:32 +0200169 if (dinfo->bdrv == bs) {
170 return dinfo;
171 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200172 }
Markus Armbrustere4700e52010-06-24 17:25:32 +0200173 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200174}
175
Markus Armbruster666daa62010-06-02 18:48:27 +0200176static void bdrv_format_print(void *opaque, const char *name)
177{
Markus Armbruster807105a2011-01-17 19:31:27 +0100178 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200179}
180
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200181static void drive_uninit(DriveInfo *dinfo)
Markus Armbruster666daa62010-06-02 18:48:27 +0200182{
183 qemu_opts_del(dinfo->opts);
184 bdrv_delete(dinfo->bdrv);
185 QTAILQ_REMOVE(&drives, dinfo, next);
186 qemu_free(dinfo);
187}
188
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200189void drive_put_ref(DriveInfo *dinfo)
190{
191 assert(dinfo->refcount);
192 if (--dinfo->refcount == 0) {
193 drive_uninit(dinfo);
194 }
195}
196
197void drive_get_ref(DriveInfo *dinfo)
198{
199 dinfo->refcount++;
200}
201
Markus Armbruster666daa62010-06-02 18:48:27 +0200202static int parse_block_error_action(const char *buf, int is_read)
203{
204 if (!strcmp(buf, "ignore")) {
205 return BLOCK_ERR_IGNORE;
206 } else if (!is_read && !strcmp(buf, "enospc")) {
207 return BLOCK_ERR_STOP_ENOSPC;
208 } else if (!strcmp(buf, "stop")) {
209 return BLOCK_ERR_STOP_ANY;
210 } else if (!strcmp(buf, "report")) {
211 return BLOCK_ERR_REPORT;
212 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100213 error_report("'%s' invalid %s error action",
214 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200215 return -1;
216 }
217}
218
Markus Armbruster319ae522011-01-28 11:21:46 +0100219DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
Markus Armbruster666daa62010-06-02 18:48:27 +0200220{
221 const char *buf;
222 const char *file = NULL;
223 char devname[128];
224 const char *serial;
225 const char *mediastr = "";
226 BlockInterfaceType type;
227 enum { MEDIA_DISK, MEDIA_CDROM } media;
228 int bus_id, unit_id;
229 int cyls, heads, secs, translation;
230 BlockDriver *drv = NULL;
231 int max_devs;
232 int index;
233 int ro = 0;
234 int bdrv_flags = 0;
235 int on_read_error, on_write_error;
236 const char *devaddr;
237 DriveInfo *dinfo;
238 int snapshot = 0;
239 int ret;
240
Markus Armbruster666daa62010-06-02 18:48:27 +0200241 translation = BIOS_ATA_TRANSLATION_AUTO;
242
243 if (default_to_scsi) {
244 type = IF_SCSI;
Markus Armbruster666daa62010-06-02 18:48:27 +0200245 pstrcpy(devname, sizeof(devname), "scsi");
246 } else {
247 type = IF_IDE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200248 pstrcpy(devname, sizeof(devname), "ide");
249 }
250 media = MEDIA_DISK;
251
252 /* extract parameters */
253 bus_id = qemu_opt_get_number(opts, "bus", 0);
254 unit_id = qemu_opt_get_number(opts, "unit", -1);
255 index = qemu_opt_get_number(opts, "index", -1);
256
257 cyls = qemu_opt_get_number(opts, "cyls", 0);
258 heads = qemu_opt_get_number(opts, "heads", 0);
259 secs = qemu_opt_get_number(opts, "secs", 0);
260
261 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
262 ro = qemu_opt_get_bool(opts, "readonly", 0);
263
264 file = qemu_opt_get(opts, "file");
265 serial = qemu_opt_get(opts, "serial");
266
267 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
268 pstrcpy(devname, sizeof(devname), buf);
Markus Armbruster19609662011-01-28 11:21:39 +0100269 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
270 ;
271 if (type == IF_COUNT) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100272 error_report("unsupported bus type '%s'", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200273 return NULL;
274 }
275 }
Markus Armbruster19609662011-01-28 11:21:39 +0100276 max_devs = if_max_devs[type];
Markus Armbruster666daa62010-06-02 18:48:27 +0200277
278 if (cyls || heads || secs) {
279 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100280 error_report("invalid physical cyls number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200281 return NULL;
282 }
283 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100284 error_report("invalid physical heads number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200285 return NULL;
286 }
287 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100288 error_report("invalid physical secs number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200289 return NULL;
290 }
291 }
292
293 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
294 if (!cyls) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100295 error_report("'%s' trans must be used with cyls,heads and secs",
296 buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200297 return NULL;
298 }
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;
305 else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100306 error_report("'%s' invalid translation type", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200307 return NULL;
308 }
309 }
310
311 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
312 if (!strcmp(buf, "disk")) {
313 media = MEDIA_DISK;
314 } else if (!strcmp(buf, "cdrom")) {
315 if (cyls || secs || heads) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100316 error_report("'%s' invalid physical CHS format", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200317 return NULL;
318 }
319 media = MEDIA_CDROM;
320 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100321 error_report("'%s' invalid media", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200322 return NULL;
323 }
324 }
325
326 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
327 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
328 bdrv_flags |= BDRV_O_NOCACHE;
329 } else if (!strcmp(buf, "writeback")) {
330 bdrv_flags |= BDRV_O_CACHE_WB;
331 } else if (!strcmp(buf, "unsafe")) {
332 bdrv_flags |= BDRV_O_CACHE_WB;
333 bdrv_flags |= BDRV_O_NO_FLUSH;
334 } else if (!strcmp(buf, "writethrough")) {
335 /* this is the default */
336 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100337 error_report("invalid cache option");
Markus Armbruster666daa62010-06-02 18:48:27 +0200338 return NULL;
339 }
340 }
341
342#ifdef CONFIG_LINUX_AIO
343 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
344 if (!strcmp(buf, "native")) {
345 bdrv_flags |= BDRV_O_NATIVE_AIO;
346 } else if (!strcmp(buf, "threads")) {
347 /* this is the default */
348 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100349 error_report("invalid aio option");
Markus Armbruster666daa62010-06-02 18:48:27 +0200350 return NULL;
351 }
352 }
353#endif
354
355 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
356 if (strcmp(buf, "?") == 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100357 error_printf("Supported formats:");
358 bdrv_iterate_format(bdrv_format_print, NULL);
359 error_printf("\n");
360 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200361 }
362 drv = bdrv_find_whitelisted_format(buf);
363 if (!drv) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100364 error_report("'%s' invalid format", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200365 return NULL;
366 }
367 }
368
369 on_write_error = BLOCK_ERR_STOP_ENOSPC;
370 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
371 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100372 error_report("werror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200373 return NULL;
374 }
375
376 on_write_error = parse_block_error_action(buf, 0);
377 if (on_write_error < 0) {
378 return NULL;
379 }
380 }
381
382 on_read_error = BLOCK_ERR_REPORT;
383 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolf5dba48a2010-10-25 14:52:21 +0200384 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100385 error_report("rerror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200386 return NULL;
387 }
388
389 on_read_error = parse_block_error_action(buf, 1);
390 if (on_read_error < 0) {
391 return NULL;
392 }
393 }
394
395 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
396 if (type != IF_VIRTIO) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100397 error_report("addr is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200398 return NULL;
399 }
400 }
401
402 /* compute bus and unit according index */
403
404 if (index != -1) {
405 if (bus_id != 0 || unit_id != -1) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100406 error_report("index cannot be used with bus and unit");
Markus Armbruster666daa62010-06-02 18:48:27 +0200407 return NULL;
408 }
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100409 bus_id = drive_index_to_bus_id(type, index);
410 unit_id = drive_index_to_unit_id(type, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200411 }
412
413 /* if user doesn't specify a unit_id,
414 * try to find the first free
415 */
416
417 if (unit_id == -1) {
418 unit_id = 0;
419 while (drive_get(type, bus_id, unit_id) != NULL) {
420 unit_id++;
421 if (max_devs && unit_id >= max_devs) {
422 unit_id -= max_devs;
423 bus_id++;
424 }
425 }
426 }
427
428 /* check unit id */
429
430 if (max_devs && unit_id >= max_devs) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100431 error_report("unit %d too big (max is %d)",
432 unit_id, max_devs - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200433 return NULL;
434 }
435
436 /*
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100437 * catch multiple definitions
Markus Armbruster666daa62010-06-02 18:48:27 +0200438 */
439
440 if (drive_get(type, bus_id, unit_id) != NULL) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100441 error_report("drive with bus=%d, unit=%d (index=%d) exists",
442 bus_id, unit_id, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200443 return NULL;
444 }
445
446 /* init */
447
448 dinfo = qemu_mallocz(sizeof(*dinfo));
449 if ((buf = qemu_opts_id(opts)) != NULL) {
450 dinfo->id = qemu_strdup(buf);
451 } else {
452 /* no id supplied -> create one */
453 dinfo->id = qemu_mallocz(32);
454 if (type == IF_IDE || type == IF_SCSI)
455 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
456 if (max_devs)
457 snprintf(dinfo->id, 32, "%s%i%s%i",
458 devname, bus_id, mediastr, unit_id);
459 else
460 snprintf(dinfo->id, 32, "%s%s%i",
461 devname, mediastr, unit_id);
462 }
463 dinfo->bdrv = bdrv_new(dinfo->id);
464 dinfo->devaddr = devaddr;
465 dinfo->type = type;
466 dinfo->bus = bus_id;
467 dinfo->unit = unit_id;
Markus Armbruster666daa62010-06-02 18:48:27 +0200468 dinfo->opts = opts;
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200469 dinfo->refcount = 1;
Markus Armbruster666daa62010-06-02 18:48:27 +0200470 if (serial)
Luiz Capitulino653dbec2010-06-02 17:46:31 -0300471 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200472 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
473
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200474 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
475
Markus Armbruster666daa62010-06-02 18:48:27 +0200476 switch(type) {
477 case IF_IDE:
478 case IF_SCSI:
479 case IF_XEN:
480 case IF_NONE:
481 switch(media) {
482 case MEDIA_DISK:
483 if (cyls != 0) {
484 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
485 bdrv_set_translation_hint(dinfo->bdrv, translation);
486 }
487 break;
488 case MEDIA_CDROM:
489 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
490 break;
491 }
492 break;
493 case IF_SD:
494 /* FIXME: This isn't really a floppy, but it's a reasonable
495 approximation. */
496 case IF_FLOPPY:
497 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
498 break;
499 case IF_PFLASH:
500 case IF_MTD:
501 break;
502 case IF_VIRTIO:
503 /* add virtio block device */
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200504 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200505 qemu_opt_set(opts, "driver", "virtio-blk-pci");
506 qemu_opt_set(opts, "drive", dinfo->id);
507 if (devaddr)
508 qemu_opt_set(opts, "addr", devaddr);
509 break;
Markus Armbruster2292dda2011-01-28 11:21:41 +0100510 default:
Markus Armbruster666daa62010-06-02 18:48:27 +0200511 abort();
512 }
Markus Armbrusterdd5b0d72010-05-11 15:36:46 +0200513 if (!file || !*file) {
Markus Armbruster319ae522011-01-28 11:21:46 +0100514 return dinfo;
Markus Armbruster666daa62010-06-02 18:48:27 +0200515 }
516 if (snapshot) {
517 /* always use cache=unsafe with snapshot */
518 bdrv_flags &= ~BDRV_O_CACHE_MASK;
519 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
520 }
521
522 if (media == MEDIA_CDROM) {
523 /* CDROM is fine for any interface, don't check. */
524 ro = 1;
525 } else if (ro == 1) {
526 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100527 error_report("readonly not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200528 return NULL;
529 }
530 }
531
532 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
533
534 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
535 if (ret < 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100536 error_report("could not open disk image %s: %s",
537 file, strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +0200538 return NULL;
539 }
540
541 if (bdrv_key_required(dinfo->bdrv))
542 autostart = 0;
Markus Armbruster666daa62010-06-02 18:48:27 +0200543 return dinfo;
544}
545
546void do_commit(Monitor *mon, const QDict *qdict)
547{
Markus Armbruster666daa62010-06-02 18:48:27 +0200548 const char *device = qdict_get_str(qdict, "device");
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200549 BlockDriverState *bs;
Markus Armbruster666daa62010-06-02 18:48:27 +0200550
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200551 if (!strcmp(device, "all")) {
552 bdrv_commit_all();
553 } else {
554 bs = bdrv_find(device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200555 if (!bs) {
556 qerror_report(QERR_DEVICE_NOT_FOUND, device);
557 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200558 }
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200559 bdrv_commit(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200560 }
561}
562
Jes Sorensenf8882562010-12-16 13:52:16 +0100563int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
564{
565 const char *device = qdict_get_str(qdict, "device");
566 const char *filename = qdict_get_try_str(qdict, "snapshot_file");
567 const char *format = qdict_get_try_str(qdict, "format");
568 BlockDriverState *bs;
569 BlockDriver *drv, *proto_drv;
570 int ret = 0;
571 int flags;
572
Jes Sorensenc90f1b32011-01-06 17:02:23 +0100573 if (!filename) {
574 qerror_report(QERR_MISSING_PARAMETER, "snapshot_file");
575 ret = -1;
576 goto out;
577 }
578
Jes Sorensenf8882562010-12-16 13:52:16 +0100579 bs = bdrv_find(device);
580 if (!bs) {
581 qerror_report(QERR_DEVICE_NOT_FOUND, device);
582 ret = -1;
583 goto out;
584 }
585
586 if (!format) {
587 format = "qcow2";
588 }
589
590 drv = bdrv_find_format(format);
591 if (!drv) {
592 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
593 ret = -1;
594 goto out;
595 }
596
597 proto_drv = bdrv_find_protocol(filename);
598 if (!proto_drv) {
599 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
600 ret = -1;
601 goto out;
602 }
603
604 ret = bdrv_img_create(filename, format, bs->filename,
605 bs->drv->format_name, NULL, -1, bs->open_flags);
606 if (ret) {
607 goto out;
608 }
609
610 qemu_aio_flush();
611 bdrv_flush(bs);
612
613 flags = bs->open_flags;
614 bdrv_close(bs);
615 ret = bdrv_open(bs, filename, flags, drv);
616 /*
617 * If reopening the image file we just created fails, we really
618 * are in trouble :(
619 */
620 if (ret != 0) {
621 abort();
622 }
623out:
624 if (ret) {
625 ret = -1;
626 }
627
628 return ret;
629}
630
Markus Armbruster666daa62010-06-02 18:48:27 +0200631static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
632{
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300633 if (!force) {
634 if (!bdrv_is_removable(bs)) {
635 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
636 bdrv_get_device_name(bs));
637 return -1;
Markus Armbruster666daa62010-06-02 18:48:27 +0200638 }
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300639 if (bdrv_is_locked(bs)) {
640 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
641 return -1;
642 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200643 }
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300644 bdrv_close(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200645 return 0;
646}
647
648int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
649{
650 BlockDriverState *bs;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -0300651 int force = qdict_get_try_bool(qdict, "force", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200652 const char *filename = qdict_get_str(qdict, "device");
653
654 bs = bdrv_find(filename);
655 if (!bs) {
656 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
657 return -1;
658 }
659 return eject_device(mon, bs, force);
660}
661
662int do_block_set_passwd(Monitor *mon, const QDict *qdict,
663 QObject **ret_data)
664{
665 BlockDriverState *bs;
666 int err;
667
668 bs = bdrv_find(qdict_get_str(qdict, "device"));
669 if (!bs) {
670 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
671 return -1;
672 }
673
674 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
675 if (err == -EINVAL) {
676 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
677 return -1;
678 } else if (err < 0) {
679 qerror_report(QERR_INVALID_PASSWORD);
680 return -1;
681 }
682
683 return 0;
684}
685
686int do_change_block(Monitor *mon, const char *device,
687 const char *filename, const char *fmt)
688{
689 BlockDriverState *bs;
690 BlockDriver *drv = NULL;
691 int bdrv_flags;
692
693 bs = bdrv_find(device);
694 if (!bs) {
695 qerror_report(QERR_DEVICE_NOT_FOUND, device);
696 return -1;
697 }
698 if (fmt) {
699 drv = bdrv_find_whitelisted_format(fmt);
700 if (!drv) {
701 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
702 return -1;
703 }
704 }
705 if (eject_device(mon, bs, 0) < 0) {
706 return -1;
707 }
Markus Armbruster528f7662010-06-25 15:26:48 +0200708 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
Blue Swirl199630b2010-07-25 20:49:34 +0000709 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
Markus Armbruster666daa62010-06-02 18:48:27 +0200710 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
711 qerror_report(QERR_OPEN_FILE_FAILED, filename);
712 return -1;
713 }
714 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
715}
Ryan Harper9063f812010-11-12 11:07:13 -0600716
717int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
718{
719 const char *id = qdict_get_str(qdict, "id");
720 BlockDriverState *bs;
721 BlockDriverState **ptr;
722 Property *prop;
723
724 bs = bdrv_find(id);
725 if (!bs) {
726 qerror_report(QERR_DEVICE_NOT_FOUND, id);
727 return -1;
728 }
729
730 /* quiesce block driver; prevent further io */
731 qemu_aio_flush();
732 bdrv_flush(bs);
733 bdrv_close(bs);
734
735 /* clean up guest state from pointing to host resource by
736 * finding and removing DeviceState "drive" property */
Markus Armbruster850ec112011-01-17 19:31:29 +0100737 if (bs->peer) {
738 for (prop = bs->peer->info->props; prop && prop->name; prop++) {
739 if (prop->info->type == PROP_TYPE_DRIVE) {
740 ptr = qdev_get_prop_ptr(bs->peer, prop);
741 if (*ptr == bs) {
742 bdrv_detach(bs, bs->peer);
743 *ptr = NULL;
744 break;
745 }
Ryan Harper9063f812010-11-12 11:07:13 -0600746 }
747 }
748 }
749
750 /* clean up host side */
751 drive_uninit(drive_get_by_blockdev(bs));
752
753 return 0;
754}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +0100755
756/*
757 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
758 * existing QERR_ macro mess is cleaned up. A good example for better
759 * error reports can be found in the qemu-img resize code.
760 */
761int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
762{
763 const char *device = qdict_get_str(qdict, "device");
764 int64_t size = qdict_get_int(qdict, "size");
765 BlockDriverState *bs;
766
767 bs = bdrv_find(device);
768 if (!bs) {
769 qerror_report(QERR_DEVICE_NOT_FOUND, device);
770 return -1;
771 }
772
773 if (size < 0) {
774 qerror_report(QERR_UNDEFINED_ERROR);
775 return -1;
776 }
777
778 if (bdrv_truncate(bs, size)) {
779 qerror_report(QERR_UNDEFINED_ERROR);
780 return -1;
781 }
782
783 return 0;
784}