blob: d272659ab26f0ba9901802876d368d7127b02f3c [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);
Anthony Liguori7267c092011-08-20 22:09:37 -0500185 g_free(dinfo->id);
Markus Armbruster666daa62010-06-02 18:48:27 +0200186 QTAILQ_REMOVE(&drives, dinfo, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500187 g_free(dinfo);
Markus Armbruster666daa62010-06-02 18:48:27 +0200188}
189
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200190void drive_put_ref(DriveInfo *dinfo)
191{
192 assert(dinfo->refcount);
193 if (--dinfo->refcount == 0) {
194 drive_uninit(dinfo);
195 }
196}
197
198void drive_get_ref(DriveInfo *dinfo)
199{
200 dinfo->refcount++;
201}
202
Markus Armbruster666daa62010-06-02 18:48:27 +0200203static int parse_block_error_action(const char *buf, int is_read)
204{
205 if (!strcmp(buf, "ignore")) {
206 return BLOCK_ERR_IGNORE;
207 } else if (!is_read && !strcmp(buf, "enospc")) {
208 return BLOCK_ERR_STOP_ENOSPC;
209 } else if (!strcmp(buf, "stop")) {
210 return BLOCK_ERR_STOP_ANY;
211 } else if (!strcmp(buf, "report")) {
212 return BLOCK_ERR_REPORT;
213 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100214 error_report("'%s' invalid %s error action",
215 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200216 return -1;
217 }
218}
219
Markus Armbruster319ae522011-01-28 11:21:46 +0100220DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
Markus Armbruster666daa62010-06-02 18:48:27 +0200221{
222 const char *buf;
223 const char *file = NULL;
224 char devname[128];
225 const char *serial;
226 const char *mediastr = "";
227 BlockInterfaceType type;
228 enum { MEDIA_DISK, MEDIA_CDROM } media;
229 int bus_id, unit_id;
230 int cyls, heads, secs, translation;
231 BlockDriver *drv = NULL;
232 int max_devs;
233 int index;
234 int ro = 0;
235 int bdrv_flags = 0;
236 int on_read_error, on_write_error;
237 const char *devaddr;
238 DriveInfo *dinfo;
239 int snapshot = 0;
240 int ret;
241
Markus Armbruster666daa62010-06-02 18:48:27 +0200242 translation = BIOS_ATA_TRANSLATION_AUTO;
Markus Armbruster666daa62010-06-02 18:48:27 +0200243 media = MEDIA_DISK;
244
245 /* extract parameters */
246 bus_id = qemu_opt_get_number(opts, "bus", 0);
247 unit_id = qemu_opt_get_number(opts, "unit", -1);
248 index = qemu_opt_get_number(opts, "index", -1);
249
250 cyls = qemu_opt_get_number(opts, "cyls", 0);
251 heads = qemu_opt_get_number(opts, "heads", 0);
252 secs = qemu_opt_get_number(opts, "secs", 0);
253
254 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
255 ro = qemu_opt_get_bool(opts, "readonly", 0);
256
257 file = qemu_opt_get(opts, "file");
258 serial = qemu_opt_get(opts, "serial");
259
260 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
261 pstrcpy(devname, sizeof(devname), buf);
Markus Armbruster19609662011-01-28 11:21:39 +0100262 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
263 ;
264 if (type == IF_COUNT) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100265 error_report("unsupported bus type '%s'", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200266 return NULL;
267 }
Luiz Capitulino2d3999f2011-07-01 10:46:12 -0300268 } else {
269 type = default_to_scsi ? IF_SCSI : IF_IDE;
270 pstrcpy(devname, sizeof(devname), if_name[type]);
Markus Armbruster666daa62010-06-02 18:48:27 +0200271 }
Luiz Capitulino2d3999f2011-07-01 10:46:12 -0300272
Markus Armbruster19609662011-01-28 11:21:39 +0100273 max_devs = if_max_devs[type];
Markus Armbruster666daa62010-06-02 18:48:27 +0200274
275 if (cyls || heads || secs) {
276 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100277 error_report("invalid physical cyls number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200278 return NULL;
279 }
280 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100281 error_report("invalid physical heads number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200282 return NULL;
283 }
284 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100285 error_report("invalid physical secs number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200286 return NULL;
287 }
288 }
289
290 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
291 if (!cyls) {
Markus Armbrustere4080f92011-06-22 14:03:57 +0200292 error_report("'%s' trans must be used with cyls, heads and secs",
Markus Armbruster807105a2011-01-17 19:31:27 +0100293 buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200294 return NULL;
295 }
296 if (!strcmp(buf, "none"))
297 translation = BIOS_ATA_TRANSLATION_NONE;
298 else if (!strcmp(buf, "lba"))
299 translation = BIOS_ATA_TRANSLATION_LBA;
300 else if (!strcmp(buf, "auto"))
301 translation = BIOS_ATA_TRANSLATION_AUTO;
302 else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100303 error_report("'%s' invalid translation type", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200304 return NULL;
305 }
306 }
307
308 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
309 if (!strcmp(buf, "disk")) {
310 media = MEDIA_DISK;
311 } else if (!strcmp(buf, "cdrom")) {
312 if (cyls || secs || heads) {
Luiz Capitulinoe7ff8f02011-07-01 10:46:13 -0300313 error_report("CHS can't be set with media=%s", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200314 return NULL;
315 }
316 media = MEDIA_CDROM;
317 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100318 error_report("'%s' invalid media", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200319 return NULL;
320 }
321 }
322
323 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
324 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
Christoph Hellwiga6599792011-05-17 18:04:06 +0200325 bdrv_flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
Markus Armbruster666daa62010-06-02 18:48:27 +0200326 } else if (!strcmp(buf, "writeback")) {
327 bdrv_flags |= BDRV_O_CACHE_WB;
328 } else if (!strcmp(buf, "unsafe")) {
329 bdrv_flags |= BDRV_O_CACHE_WB;
330 bdrv_flags |= BDRV_O_NO_FLUSH;
331 } else if (!strcmp(buf, "writethrough")) {
332 /* this is the default */
333 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100334 error_report("invalid cache option");
Markus Armbruster666daa62010-06-02 18:48:27 +0200335 return NULL;
336 }
337 }
338
339#ifdef CONFIG_LINUX_AIO
340 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
341 if (!strcmp(buf, "native")) {
342 bdrv_flags |= BDRV_O_NATIVE_AIO;
343 } else if (!strcmp(buf, "threads")) {
344 /* this is the default */
345 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100346 error_report("invalid aio option");
Markus Armbruster666daa62010-06-02 18:48:27 +0200347 return NULL;
348 }
349 }
350#endif
351
352 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
353 if (strcmp(buf, "?") == 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100354 error_printf("Supported formats:");
355 bdrv_iterate_format(bdrv_format_print, NULL);
356 error_printf("\n");
357 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200358 }
359 drv = bdrv_find_whitelisted_format(buf);
360 if (!drv) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100361 error_report("'%s' invalid format", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200362 return NULL;
363 }
364 }
365
366 on_write_error = BLOCK_ERR_STOP_ENOSPC;
367 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
368 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100369 error_report("werror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200370 return NULL;
371 }
372
373 on_write_error = parse_block_error_action(buf, 0);
374 if (on_write_error < 0) {
375 return NULL;
376 }
377 }
378
379 on_read_error = BLOCK_ERR_REPORT;
380 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolf5dba48a2010-10-25 14:52:21 +0200381 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100382 error_report("rerror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200383 return NULL;
384 }
385
386 on_read_error = parse_block_error_action(buf, 1);
387 if (on_read_error < 0) {
388 return NULL;
389 }
390 }
391
392 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
393 if (type != IF_VIRTIO) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100394 error_report("addr is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200395 return NULL;
396 }
397 }
398
399 /* compute bus and unit according index */
400
401 if (index != -1) {
402 if (bus_id != 0 || unit_id != -1) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100403 error_report("index cannot be used with bus and unit");
Markus Armbruster666daa62010-06-02 18:48:27 +0200404 return NULL;
405 }
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100406 bus_id = drive_index_to_bus_id(type, index);
407 unit_id = drive_index_to_unit_id(type, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200408 }
409
410 /* if user doesn't specify a unit_id,
411 * try to find the first free
412 */
413
414 if (unit_id == -1) {
415 unit_id = 0;
416 while (drive_get(type, bus_id, unit_id) != NULL) {
417 unit_id++;
418 if (max_devs && unit_id >= max_devs) {
419 unit_id -= max_devs;
420 bus_id++;
421 }
422 }
423 }
424
425 /* check unit id */
426
427 if (max_devs && unit_id >= max_devs) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100428 error_report("unit %d too big (max is %d)",
429 unit_id, max_devs - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200430 return NULL;
431 }
432
433 /*
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100434 * catch multiple definitions
Markus Armbruster666daa62010-06-02 18:48:27 +0200435 */
436
437 if (drive_get(type, bus_id, unit_id) != NULL) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100438 error_report("drive with bus=%d, unit=%d (index=%d) exists",
439 bus_id, unit_id, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200440 return NULL;
441 }
442
443 /* init */
444
Anthony Liguori7267c092011-08-20 22:09:37 -0500445 dinfo = g_malloc0(sizeof(*dinfo));
Markus Armbruster666daa62010-06-02 18:48:27 +0200446 if ((buf = qemu_opts_id(opts)) != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500447 dinfo->id = g_strdup(buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200448 } else {
449 /* no id supplied -> create one */
Anthony Liguori7267c092011-08-20 22:09:37 -0500450 dinfo->id = g_malloc0(32);
Markus Armbruster666daa62010-06-02 18:48:27 +0200451 if (type == IF_IDE || type == IF_SCSI)
452 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
453 if (max_devs)
454 snprintf(dinfo->id, 32, "%s%i%s%i",
455 devname, bus_id, mediastr, unit_id);
456 else
457 snprintf(dinfo->id, 32, "%s%s%i",
458 devname, mediastr, unit_id);
459 }
460 dinfo->bdrv = bdrv_new(dinfo->id);
461 dinfo->devaddr = devaddr;
462 dinfo->type = type;
463 dinfo->bus = bus_id;
464 dinfo->unit = unit_id;
Markus Armbruster666daa62010-06-02 18:48:27 +0200465 dinfo->opts = opts;
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200466 dinfo->refcount = 1;
Markus Armbruster666daa62010-06-02 18:48:27 +0200467 if (serial)
Luiz Capitulino653dbec2010-06-02 17:46:31 -0300468 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200469 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
470
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200471 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
472
Markus Armbruster666daa62010-06-02 18:48:27 +0200473 switch(type) {
474 case IF_IDE:
475 case IF_SCSI:
476 case IF_XEN:
477 case IF_NONE:
478 switch(media) {
479 case MEDIA_DISK:
480 if (cyls != 0) {
481 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
482 bdrv_set_translation_hint(dinfo->bdrv, translation);
483 }
484 break;
485 case MEDIA_CDROM:
Markus Armbruster8d278462011-05-16 15:04:57 +0200486 bdrv_set_removable(dinfo->bdrv, 1);
Markus Armbruster95b5edc2011-05-16 15:04:56 +0200487 dinfo->media_cd = 1;
Markus Armbruster666daa62010-06-02 18:48:27 +0200488 break;
489 }
490 break;
491 case IF_SD:
492 /* FIXME: This isn't really a floppy, but it's a reasonable
493 approximation. */
494 case IF_FLOPPY:
Markus Armbruster8d278462011-05-16 15:04:57 +0200495 bdrv_set_removable(dinfo->bdrv, 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200496 break;
497 case IF_PFLASH:
498 case IF_MTD:
499 break;
500 case IF_VIRTIO:
501 /* add virtio block device */
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200502 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Alexander Graf29f82b32011-03-29 15:29:29 +0200503 qemu_opt_set(opts, "driver", "virtio-blk");
Markus Armbruster666daa62010-06-02 18:48:27 +0200504 qemu_opt_set(opts, "drive", dinfo->id);
505 if (devaddr)
506 qemu_opt_set(opts, "addr", devaddr);
507 break;
Markus Armbruster2292dda2011-01-28 11:21:41 +0100508 default:
Markus Armbruster666daa62010-06-02 18:48:27 +0200509 abort();
510 }
Markus Armbrusterdd5b0d72010-05-11 15:36:46 +0200511 if (!file || !*file) {
Markus Armbruster319ae522011-01-28 11:21:46 +0100512 return dinfo;
Markus Armbruster666daa62010-06-02 18:48:27 +0200513 }
514 if (snapshot) {
515 /* always use cache=unsafe with snapshot */
516 bdrv_flags &= ~BDRV_O_CACHE_MASK;
517 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
518 }
519
520 if (media == MEDIA_CDROM) {
521 /* CDROM is fine for any interface, don't check. */
522 ro = 1;
523 } else if (ro == 1) {
524 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100525 error_report("readonly not supported by this bus type");
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100526 goto err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200527 }
528 }
529
530 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
531
532 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
533 if (ret < 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100534 error_report("could not open disk image %s: %s",
535 file, strerror(-ret));
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100536 goto err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200537 }
538
539 if (bdrv_key_required(dinfo->bdrv))
540 autostart = 0;
Markus Armbruster666daa62010-06-02 18:48:27 +0200541 return dinfo;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100542
543err:
544 bdrv_delete(dinfo->bdrv);
Anthony Liguori7267c092011-08-20 22:09:37 -0500545 g_free(dinfo->id);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100546 QTAILQ_REMOVE(&drives, dinfo, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500547 g_free(dinfo);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100548 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200549}
550
551void do_commit(Monitor *mon, const QDict *qdict)
552{
Markus Armbruster666daa62010-06-02 18:48:27 +0200553 const char *device = qdict_get_str(qdict, "device");
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200554 BlockDriverState *bs;
Markus Armbruster666daa62010-06-02 18:48:27 +0200555
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200556 if (!strcmp(device, "all")) {
557 bdrv_commit_all();
558 } else {
559 bs = bdrv_find(device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200560 if (!bs) {
561 qerror_report(QERR_DEVICE_NOT_FOUND, device);
562 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200563 }
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200564 bdrv_commit(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200565 }
566}
567
Jes Sorensenf8882562010-12-16 13:52:16 +0100568int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
569{
570 const char *device = qdict_get_str(qdict, "device");
Jes Sorensend967b2f2011-07-11 20:01:09 +0200571 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
Jes Sorensenf8882562010-12-16 13:52:16 +0100572 const char *format = qdict_get_try_str(qdict, "format");
573 BlockDriverState *bs;
Jes Sorensen52f9a172011-03-09 11:20:30 +0100574 BlockDriver *drv, *old_drv, *proto_drv;
Jes Sorensenf8882562010-12-16 13:52:16 +0100575 int ret = 0;
576 int flags;
Jes Sorensen52f9a172011-03-09 11:20:30 +0100577 char old_filename[1024];
Jes Sorensenf8882562010-12-16 13:52:16 +0100578
Jes Sorensenc90f1b32011-01-06 17:02:23 +0100579 if (!filename) {
Jes Sorensend967b2f2011-07-11 20:01:09 +0200580 qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
Jes Sorensenc90f1b32011-01-06 17:02:23 +0100581 ret = -1;
582 goto out;
583 }
584
Jes Sorensenf8882562010-12-16 13:52:16 +0100585 bs = bdrv_find(device);
586 if (!bs) {
587 qerror_report(QERR_DEVICE_NOT_FOUND, device);
588 ret = -1;
589 goto out;
590 }
591
Jes Sorensen52f9a172011-03-09 11:20:30 +0100592 pstrcpy(old_filename, sizeof(old_filename), bs->filename);
593
594 old_drv = bs->drv;
595 flags = bs->open_flags;
596
Jes Sorensenf8882562010-12-16 13:52:16 +0100597 if (!format) {
598 format = "qcow2";
599 }
600
601 drv = bdrv_find_format(format);
602 if (!drv) {
603 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
604 ret = -1;
605 goto out;
606 }
607
608 proto_drv = bdrv_find_protocol(filename);
609 if (!proto_drv) {
610 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
611 ret = -1;
612 goto out;
613 }
614
615 ret = bdrv_img_create(filename, format, bs->filename,
Jes Sorensen52f9a172011-03-09 11:20:30 +0100616 bs->drv->format_name, NULL, -1, flags);
Jes Sorensenf8882562010-12-16 13:52:16 +0100617 if (ret) {
618 goto out;
619 }
620
621 qemu_aio_flush();
622 bdrv_flush(bs);
623
Jes Sorensenf8882562010-12-16 13:52:16 +0100624 bdrv_close(bs);
625 ret = bdrv_open(bs, filename, flags, drv);
626 /*
Jes Sorensen52f9a172011-03-09 11:20:30 +0100627 * If reopening the image file we just created fails, fall back
628 * and try to re-open the original image. If that fails too, we
629 * are in serious trouble.
Jes Sorensenf8882562010-12-16 13:52:16 +0100630 */
631 if (ret != 0) {
Jes Sorensen52f9a172011-03-09 11:20:30 +0100632 ret = bdrv_open(bs, old_filename, flags, old_drv);
633 if (ret != 0) {
634 qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
635 } else {
636 qerror_report(QERR_OPEN_FILE_FAILED, filename);
637 }
Jes Sorensenf8882562010-12-16 13:52:16 +0100638 }
639out:
640 if (ret) {
641 ret = -1;
642 }
643
644 return ret;
645}
646
Markus Armbruster666daa62010-06-02 18:48:27 +0200647static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
648{
Markus Armbrusterea8f9422011-07-20 18:23:35 +0200649 if (!bdrv_is_removable(bs)) {
650 qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
651 return -1;
652 }
653 if (!force && bdrv_is_locked(bs)) {
654 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
655 return -1;
Markus Armbruster666daa62010-06-02 18:48:27 +0200656 }
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300657 bdrv_close(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200658 return 0;
659}
660
661int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
662{
663 BlockDriverState *bs;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -0300664 int force = qdict_get_try_bool(qdict, "force", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200665 const char *filename = qdict_get_str(qdict, "device");
666
667 bs = bdrv_find(filename);
668 if (!bs) {
669 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
670 return -1;
671 }
672 return eject_device(mon, bs, force);
673}
674
675int do_block_set_passwd(Monitor *mon, const QDict *qdict,
676 QObject **ret_data)
677{
678 BlockDriverState *bs;
679 int err;
680
681 bs = bdrv_find(qdict_get_str(qdict, "device"));
682 if (!bs) {
683 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
684 return -1;
685 }
686
687 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
688 if (err == -EINVAL) {
689 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
690 return -1;
691 } else if (err < 0) {
692 qerror_report(QERR_INVALID_PASSWORD);
693 return -1;
694 }
695
696 return 0;
697}
698
699int do_change_block(Monitor *mon, const char *device,
700 const char *filename, const char *fmt)
701{
702 BlockDriverState *bs;
703 BlockDriver *drv = NULL;
704 int bdrv_flags;
705
706 bs = bdrv_find(device);
707 if (!bs) {
708 qerror_report(QERR_DEVICE_NOT_FOUND, device);
709 return -1;
710 }
711 if (fmt) {
712 drv = bdrv_find_whitelisted_format(fmt);
713 if (!drv) {
714 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
715 return -1;
716 }
717 }
718 if (eject_device(mon, bs, 0) < 0) {
719 return -1;
720 }
Markus Armbruster528f7662010-06-25 15:26:48 +0200721 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
Blue Swirl199630b2010-07-25 20:49:34 +0000722 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
Markus Armbruster666daa62010-06-02 18:48:27 +0200723 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
724 qerror_report(QERR_OPEN_FILE_FAILED, filename);
725 return -1;
726 }
727 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
728}
Ryan Harper9063f812010-11-12 11:07:13 -0600729
730int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
731{
732 const char *id = qdict_get_str(qdict, "id");
733 BlockDriverState *bs;
Ryan Harper9063f812010-11-12 11:07:13 -0600734
735 bs = bdrv_find(id);
736 if (!bs) {
737 qerror_report(QERR_DEVICE_NOT_FOUND, id);
738 return -1;
739 }
Marcelo Tosatti85916752011-01-26 12:12:35 -0200740 if (bdrv_in_use(bs)) {
741 qerror_report(QERR_DEVICE_IN_USE, id);
742 return -1;
743 }
Ryan Harper9063f812010-11-12 11:07:13 -0600744
745 /* quiesce block driver; prevent further io */
746 qemu_aio_flush();
747 bdrv_flush(bs);
748 bdrv_close(bs);
749
Ryan Harperd22b2f42011-03-29 20:51:47 -0500750 /* if we have a device associated with this BlockDriverState (bs->peer)
751 * then we need to make the drive anonymous until the device
752 * can be removed. If this is a drive with no device backing
753 * then we can just get rid of the block driver state right here.
754 */
Markus Armbruster850ec112011-01-17 19:31:29 +0100755 if (bs->peer) {
Ryan Harperd22b2f42011-03-29 20:51:47 -0500756 bdrv_make_anon(bs);
757 } else {
758 drive_uninit(drive_get_by_blockdev(bs));
Ryan Harper9063f812010-11-12 11:07:13 -0600759 }
760
Ryan Harper9063f812010-11-12 11:07:13 -0600761 return 0;
762}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +0100763
764/*
765 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
766 * existing QERR_ macro mess is cleaned up. A good example for better
767 * error reports can be found in the qemu-img resize code.
768 */
769int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
770{
771 const char *device = qdict_get_str(qdict, "device");
772 int64_t size = qdict_get_int(qdict, "size");
773 BlockDriverState *bs;
774
775 bs = bdrv_find(device);
776 if (!bs) {
777 qerror_report(QERR_DEVICE_NOT_FOUND, device);
778 return -1;
779 }
780
781 if (size < 0) {
782 qerror_report(QERR_UNDEFINED_ERROR);
783 return -1;
784 }
785
786 if (bdrv_truncate(bs, size)) {
787 qerror_report(QERR_UNDEFINED_ERROR);
788 return -1;
789 }
790
791 return 0;
792}