blob: 4b2145c25b31aaedc232332a25ed61fc153f3ff2 [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) {
Markus Armbruster14bafc52010-06-25 08:09:10 +020074 drive_uninit(dinfo);
75 }
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
181void drive_uninit(DriveInfo *dinfo)
182{
183 qemu_opts_del(dinfo->opts);
184 bdrv_delete(dinfo->bdrv);
185 QTAILQ_REMOVE(&drives, dinfo, next);
186 qemu_free(dinfo);
187}
188
189static int parse_block_error_action(const char *buf, int is_read)
190{
191 if (!strcmp(buf, "ignore")) {
192 return BLOCK_ERR_IGNORE;
193 } else if (!is_read && !strcmp(buf, "enospc")) {
194 return BLOCK_ERR_STOP_ENOSPC;
195 } else if (!strcmp(buf, "stop")) {
196 return BLOCK_ERR_STOP_ANY;
197 } else if (!strcmp(buf, "report")) {
198 return BLOCK_ERR_REPORT;
199 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100200 error_report("'%s' invalid %s error action",
201 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200202 return -1;
203 }
204}
205
206DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
207{
208 const char *buf;
209 const char *file = NULL;
210 char devname[128];
211 const char *serial;
212 const char *mediastr = "";
213 BlockInterfaceType type;
214 enum { MEDIA_DISK, MEDIA_CDROM } media;
215 int bus_id, unit_id;
216 int cyls, heads, secs, translation;
217 BlockDriver *drv = NULL;
218 int max_devs;
219 int index;
220 int ro = 0;
221 int bdrv_flags = 0;
222 int on_read_error, on_write_error;
223 const char *devaddr;
224 DriveInfo *dinfo;
225 int snapshot = 0;
226 int ret;
227
228 *fatal_error = 1;
229
230 translation = BIOS_ATA_TRANSLATION_AUTO;
231
232 if (default_to_scsi) {
233 type = IF_SCSI;
Markus Armbruster666daa62010-06-02 18:48:27 +0200234 pstrcpy(devname, sizeof(devname), "scsi");
235 } else {
236 type = IF_IDE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200237 pstrcpy(devname, sizeof(devname), "ide");
238 }
239 media = MEDIA_DISK;
240
241 /* extract parameters */
242 bus_id = qemu_opt_get_number(opts, "bus", 0);
243 unit_id = qemu_opt_get_number(opts, "unit", -1);
244 index = qemu_opt_get_number(opts, "index", -1);
245
246 cyls = qemu_opt_get_number(opts, "cyls", 0);
247 heads = qemu_opt_get_number(opts, "heads", 0);
248 secs = qemu_opt_get_number(opts, "secs", 0);
249
250 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
251 ro = qemu_opt_get_bool(opts, "readonly", 0);
252
253 file = qemu_opt_get(opts, "file");
254 serial = qemu_opt_get(opts, "serial");
255
256 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
257 pstrcpy(devname, sizeof(devname), buf);
Markus Armbruster19609662011-01-28 11:21:39 +0100258 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
259 ;
260 if (type == IF_COUNT) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100261 error_report("unsupported bus type '%s'", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200262 return NULL;
263 }
264 }
Markus Armbruster19609662011-01-28 11:21:39 +0100265 max_devs = if_max_devs[type];
Markus Armbruster666daa62010-06-02 18:48:27 +0200266
267 if (cyls || heads || secs) {
268 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100269 error_report("invalid physical cyls number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200270 return NULL;
271 }
272 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100273 error_report("invalid physical heads number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200274 return NULL;
275 }
276 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100277 error_report("invalid physical secs number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200278 return NULL;
279 }
280 }
281
282 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
283 if (!cyls) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100284 error_report("'%s' trans must be used with cyls,heads and secs",
285 buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200286 return NULL;
287 }
288 if (!strcmp(buf, "none"))
289 translation = BIOS_ATA_TRANSLATION_NONE;
290 else if (!strcmp(buf, "lba"))
291 translation = BIOS_ATA_TRANSLATION_LBA;
292 else if (!strcmp(buf, "auto"))
293 translation = BIOS_ATA_TRANSLATION_AUTO;
294 else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100295 error_report("'%s' invalid translation type", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200296 return NULL;
297 }
298 }
299
300 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
301 if (!strcmp(buf, "disk")) {
302 media = MEDIA_DISK;
303 } else if (!strcmp(buf, "cdrom")) {
304 if (cyls || secs || heads) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100305 error_report("'%s' invalid physical CHS format", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200306 return NULL;
307 }
308 media = MEDIA_CDROM;
309 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100310 error_report("'%s' invalid media", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200311 return NULL;
312 }
313 }
314
315 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
316 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
317 bdrv_flags |= BDRV_O_NOCACHE;
318 } else if (!strcmp(buf, "writeback")) {
319 bdrv_flags |= BDRV_O_CACHE_WB;
320 } else if (!strcmp(buf, "unsafe")) {
321 bdrv_flags |= BDRV_O_CACHE_WB;
322 bdrv_flags |= BDRV_O_NO_FLUSH;
323 } else if (!strcmp(buf, "writethrough")) {
324 /* this is the default */
325 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100326 error_report("invalid cache option");
Markus Armbruster666daa62010-06-02 18:48:27 +0200327 return NULL;
328 }
329 }
330
331#ifdef CONFIG_LINUX_AIO
332 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
333 if (!strcmp(buf, "native")) {
334 bdrv_flags |= BDRV_O_NATIVE_AIO;
335 } else if (!strcmp(buf, "threads")) {
336 /* this is the default */
337 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100338 error_report("invalid aio option");
Markus Armbruster666daa62010-06-02 18:48:27 +0200339 return NULL;
340 }
341 }
342#endif
343
344 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
345 if (strcmp(buf, "?") == 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100346 error_printf("Supported formats:");
347 bdrv_iterate_format(bdrv_format_print, NULL);
348 error_printf("\n");
349 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200350 }
351 drv = bdrv_find_whitelisted_format(buf);
352 if (!drv) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100353 error_report("'%s' invalid format", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200354 return NULL;
355 }
356 }
357
358 on_write_error = BLOCK_ERR_STOP_ENOSPC;
359 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
360 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100361 error_report("werror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200362 return NULL;
363 }
364
365 on_write_error = parse_block_error_action(buf, 0);
366 if (on_write_error < 0) {
367 return NULL;
368 }
369 }
370
371 on_read_error = BLOCK_ERR_REPORT;
372 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolf5dba48a2010-10-25 14:52:21 +0200373 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100374 error_report("rerror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200375 return NULL;
376 }
377
378 on_read_error = parse_block_error_action(buf, 1);
379 if (on_read_error < 0) {
380 return NULL;
381 }
382 }
383
384 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
385 if (type != IF_VIRTIO) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100386 error_report("addr is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200387 return NULL;
388 }
389 }
390
391 /* compute bus and unit according index */
392
393 if (index != -1) {
394 if (bus_id != 0 || unit_id != -1) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100395 error_report("index cannot be used with bus and unit");
Markus Armbruster666daa62010-06-02 18:48:27 +0200396 return NULL;
397 }
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100398 bus_id = drive_index_to_bus_id(type, index);
399 unit_id = drive_index_to_unit_id(type, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200400 }
401
402 /* if user doesn't specify a unit_id,
403 * try to find the first free
404 */
405
406 if (unit_id == -1) {
407 unit_id = 0;
408 while (drive_get(type, bus_id, unit_id) != NULL) {
409 unit_id++;
410 if (max_devs && unit_id >= max_devs) {
411 unit_id -= max_devs;
412 bus_id++;
413 }
414 }
415 }
416
417 /* check unit id */
418
419 if (max_devs && unit_id >= max_devs) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100420 error_report("unit %d too big (max is %d)",
421 unit_id, max_devs - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200422 return NULL;
423 }
424
425 /*
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100426 * catch multiple definitions
Markus Armbruster666daa62010-06-02 18:48:27 +0200427 */
428
429 if (drive_get(type, bus_id, unit_id) != NULL) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100430 error_report("drive with bus=%d, unit=%d (index=%d) exists",
431 bus_id, unit_id, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200432 return NULL;
433 }
434
435 /* init */
436
437 dinfo = qemu_mallocz(sizeof(*dinfo));
438 if ((buf = qemu_opts_id(opts)) != NULL) {
439 dinfo->id = qemu_strdup(buf);
440 } else {
441 /* no id supplied -> create one */
442 dinfo->id = qemu_mallocz(32);
443 if (type == IF_IDE || type == IF_SCSI)
444 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
445 if (max_devs)
446 snprintf(dinfo->id, 32, "%s%i%s%i",
447 devname, bus_id, mediastr, unit_id);
448 else
449 snprintf(dinfo->id, 32, "%s%s%i",
450 devname, mediastr, unit_id);
451 }
452 dinfo->bdrv = bdrv_new(dinfo->id);
453 dinfo->devaddr = devaddr;
454 dinfo->type = type;
455 dinfo->bus = bus_id;
456 dinfo->unit = unit_id;
Markus Armbruster666daa62010-06-02 18:48:27 +0200457 dinfo->opts = opts;
458 if (serial)
Luiz Capitulino653dbec2010-06-02 17:46:31 -0300459 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200460 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
461
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200462 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
463
Markus Armbruster666daa62010-06-02 18:48:27 +0200464 switch(type) {
465 case IF_IDE:
466 case IF_SCSI:
467 case IF_XEN:
468 case IF_NONE:
469 switch(media) {
470 case MEDIA_DISK:
471 if (cyls != 0) {
472 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
473 bdrv_set_translation_hint(dinfo->bdrv, translation);
474 }
475 break;
476 case MEDIA_CDROM:
477 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
478 break;
479 }
480 break;
481 case IF_SD:
482 /* FIXME: This isn't really a floppy, but it's a reasonable
483 approximation. */
484 case IF_FLOPPY:
485 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
486 break;
487 case IF_PFLASH:
488 case IF_MTD:
489 break;
490 case IF_VIRTIO:
491 /* add virtio block device */
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200492 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200493 qemu_opt_set(opts, "driver", "virtio-blk-pci");
494 qemu_opt_set(opts, "drive", dinfo->id);
495 if (devaddr)
496 qemu_opt_set(opts, "addr", devaddr);
497 break;
Markus Armbruster2292dda2011-01-28 11:21:41 +0100498 default:
Markus Armbruster666daa62010-06-02 18:48:27 +0200499 abort();
500 }
Markus Armbrusterdd5b0d72010-05-11 15:36:46 +0200501 if (!file || !*file) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200502 *fatal_error = 0;
503 return NULL;
504 }
505 if (snapshot) {
506 /* always use cache=unsafe with snapshot */
507 bdrv_flags &= ~BDRV_O_CACHE_MASK;
508 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
509 }
510
511 if (media == MEDIA_CDROM) {
512 /* CDROM is fine for any interface, don't check. */
513 ro = 1;
514 } else if (ro == 1) {
515 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100516 error_report("readonly not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200517 return NULL;
518 }
519 }
520
521 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
522
523 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
524 if (ret < 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100525 error_report("could not open disk image %s: %s",
526 file, strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +0200527 return NULL;
528 }
529
530 if (bdrv_key_required(dinfo->bdrv))
531 autostart = 0;
532 *fatal_error = 0;
533 return dinfo;
534}
535
536void do_commit(Monitor *mon, const QDict *qdict)
537{
Markus Armbruster666daa62010-06-02 18:48:27 +0200538 const char *device = qdict_get_str(qdict, "device");
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200539 BlockDriverState *bs;
Markus Armbruster666daa62010-06-02 18:48:27 +0200540
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200541 if (!strcmp(device, "all")) {
542 bdrv_commit_all();
543 } else {
544 bs = bdrv_find(device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200545 if (!bs) {
546 qerror_report(QERR_DEVICE_NOT_FOUND, device);
547 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200548 }
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200549 bdrv_commit(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200550 }
551}
552
Jes Sorensenf8882562010-12-16 13:52:16 +0100553int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
554{
555 const char *device = qdict_get_str(qdict, "device");
556 const char *filename = qdict_get_try_str(qdict, "snapshot_file");
557 const char *format = qdict_get_try_str(qdict, "format");
558 BlockDriverState *bs;
559 BlockDriver *drv, *proto_drv;
560 int ret = 0;
561 int flags;
562
Jes Sorensenc90f1b32011-01-06 17:02:23 +0100563 if (!filename) {
564 qerror_report(QERR_MISSING_PARAMETER, "snapshot_file");
565 ret = -1;
566 goto out;
567 }
568
Jes Sorensenf8882562010-12-16 13:52:16 +0100569 bs = bdrv_find(device);
570 if (!bs) {
571 qerror_report(QERR_DEVICE_NOT_FOUND, device);
572 ret = -1;
573 goto out;
574 }
575
576 if (!format) {
577 format = "qcow2";
578 }
579
580 drv = bdrv_find_format(format);
581 if (!drv) {
582 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
583 ret = -1;
584 goto out;
585 }
586
587 proto_drv = bdrv_find_protocol(filename);
588 if (!proto_drv) {
589 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
590 ret = -1;
591 goto out;
592 }
593
594 ret = bdrv_img_create(filename, format, bs->filename,
595 bs->drv->format_name, NULL, -1, bs->open_flags);
596 if (ret) {
597 goto out;
598 }
599
600 qemu_aio_flush();
601 bdrv_flush(bs);
602
603 flags = bs->open_flags;
604 bdrv_close(bs);
605 ret = bdrv_open(bs, filename, flags, drv);
606 /*
607 * If reopening the image file we just created fails, we really
608 * are in trouble :(
609 */
610 if (ret != 0) {
611 abort();
612 }
613out:
614 if (ret) {
615 ret = -1;
616 }
617
618 return ret;
619}
620
Markus Armbruster666daa62010-06-02 18:48:27 +0200621static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
622{
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300623 if (!force) {
624 if (!bdrv_is_removable(bs)) {
625 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
626 bdrv_get_device_name(bs));
627 return -1;
Markus Armbruster666daa62010-06-02 18:48:27 +0200628 }
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300629 if (bdrv_is_locked(bs)) {
630 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
631 return -1;
632 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200633 }
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300634 bdrv_close(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200635 return 0;
636}
637
638int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
639{
640 BlockDriverState *bs;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -0300641 int force = qdict_get_try_bool(qdict, "force", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200642 const char *filename = qdict_get_str(qdict, "device");
643
644 bs = bdrv_find(filename);
645 if (!bs) {
646 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
647 return -1;
648 }
649 return eject_device(mon, bs, force);
650}
651
652int do_block_set_passwd(Monitor *mon, const QDict *qdict,
653 QObject **ret_data)
654{
655 BlockDriverState *bs;
656 int err;
657
658 bs = bdrv_find(qdict_get_str(qdict, "device"));
659 if (!bs) {
660 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
661 return -1;
662 }
663
664 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
665 if (err == -EINVAL) {
666 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
667 return -1;
668 } else if (err < 0) {
669 qerror_report(QERR_INVALID_PASSWORD);
670 return -1;
671 }
672
673 return 0;
674}
675
676int do_change_block(Monitor *mon, const char *device,
677 const char *filename, const char *fmt)
678{
679 BlockDriverState *bs;
680 BlockDriver *drv = NULL;
681 int bdrv_flags;
682
683 bs = bdrv_find(device);
684 if (!bs) {
685 qerror_report(QERR_DEVICE_NOT_FOUND, device);
686 return -1;
687 }
688 if (fmt) {
689 drv = bdrv_find_whitelisted_format(fmt);
690 if (!drv) {
691 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
692 return -1;
693 }
694 }
695 if (eject_device(mon, bs, 0) < 0) {
696 return -1;
697 }
Markus Armbruster528f7662010-06-25 15:26:48 +0200698 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
Blue Swirl199630b2010-07-25 20:49:34 +0000699 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
Markus Armbruster666daa62010-06-02 18:48:27 +0200700 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
701 qerror_report(QERR_OPEN_FILE_FAILED, filename);
702 return -1;
703 }
704 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
705}
Ryan Harper9063f812010-11-12 11:07:13 -0600706
707int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
708{
709 const char *id = qdict_get_str(qdict, "id");
710 BlockDriverState *bs;
711 BlockDriverState **ptr;
712 Property *prop;
713
714 bs = bdrv_find(id);
715 if (!bs) {
716 qerror_report(QERR_DEVICE_NOT_FOUND, id);
717 return -1;
718 }
719
720 /* quiesce block driver; prevent further io */
721 qemu_aio_flush();
722 bdrv_flush(bs);
723 bdrv_close(bs);
724
725 /* clean up guest state from pointing to host resource by
726 * finding and removing DeviceState "drive" property */
Markus Armbruster850ec112011-01-17 19:31:29 +0100727 if (bs->peer) {
728 for (prop = bs->peer->info->props; prop && prop->name; prop++) {
729 if (prop->info->type == PROP_TYPE_DRIVE) {
730 ptr = qdev_get_prop_ptr(bs->peer, prop);
731 if (*ptr == bs) {
732 bdrv_detach(bs, bs->peer);
733 *ptr = NULL;
734 break;
735 }
Ryan Harper9063f812010-11-12 11:07:13 -0600736 }
737 }
738 }
739
740 /* clean up host side */
741 drive_uninit(drive_get_by_blockdev(bs));
742
743 return 0;
744}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +0100745
746/*
747 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
748 * existing QERR_ macro mess is cleaned up. A good example for better
749 * error reports can be found in the qemu-img resize code.
750 */
751int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
752{
753 const char *device = qdict_get_str(qdict, "device");
754 int64_t size = qdict_get_int(qdict, "size");
755 BlockDriverState *bs;
756
757 bs = bdrv_find(device);
758 if (!bs) {
759 qerror_report(QERR_DEVICE_NOT_FOUND, device);
760 return -1;
761 }
762
763 if (size < 0) {
764 qerror_report(QERR_UNDEFINED_ERROR);
765 return -1;
766 }
767
768 if (bdrv_truncate(bs, size)) {
769 qerror_report(QERR_UNDEFINED_ERROR);
770 return -1;
771 }
772
773 return 0;
774}