blob: 01228f6200c0a9708ed42a639483a07a2121c7e9 [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,
96 const char *fmt, ...)
Markus Armbruster666daa62010-06-02 18:48:27 +020097{
98 va_list ap;
99 char optstr[1024];
100 QemuOpts *opts;
Markus Armbruster2292dda2011-01-28 11:21:41 +0100101 char buf[32];
Markus Armbruster666daa62010-06-02 18:48:27 +0200102
103 va_start(ap, fmt);
104 vsnprintf(optstr, sizeof(optstr), fmt, ap);
105 va_end(ap);
106
Markus Armbruster2292dda2011-01-28 11:21:41 +0100107 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200108 if (!opts) {
109 return NULL;
110 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100111 if (type != IF_DEFAULT) {
112 qemu_opt_set(opts, "if", if_name[type]);
113 }
114 if (index >= 0) {
115 snprintf(buf, sizeof(buf), "%d", index);
116 qemu_opt_set(opts, "index", buf);
117 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200118 if (file)
119 qemu_opt_set(opts, "file", file);
120 return opts;
121}
122
123DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
124{
125 DriveInfo *dinfo;
126
127 /* seek interface, bus and unit */
128
129 QTAILQ_FOREACH(dinfo, &drives, next) {
130 if (dinfo->type == type &&
131 dinfo->bus == bus &&
132 dinfo->unit == unit)
133 return dinfo;
134 }
135
136 return NULL;
137}
138
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100139DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
140{
141 return drive_get(type,
142 drive_index_to_bus_id(type, index),
143 drive_index_to_unit_id(type, index));
144}
145
Markus Armbruster666daa62010-06-02 18:48:27 +0200146int drive_get_max_bus(BlockInterfaceType type)
147{
148 int max_bus;
149 DriveInfo *dinfo;
150
151 max_bus = -1;
152 QTAILQ_FOREACH(dinfo, &drives, next) {
153 if(dinfo->type == type &&
154 dinfo->bus > max_bus)
155 max_bus = dinfo->bus;
156 }
157 return max_bus;
158}
159
Markus Armbruster13839972011-01-28 11:21:37 +0100160/* Get a block device. This should only be used for single-drive devices
161 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
162 appropriate bus. */
163DriveInfo *drive_get_next(BlockInterfaceType type)
164{
165 static int next_block_unit[IF_COUNT];
166
167 return drive_get(type, 0, next_block_unit[type]++);
168}
169
Markus Armbrustere4700e52010-06-24 17:25:32 +0200170DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
Markus Armbruster666daa62010-06-02 18:48:27 +0200171{
172 DriveInfo *dinfo;
173
174 QTAILQ_FOREACH(dinfo, &drives, next) {
Markus Armbrustere4700e52010-06-24 17:25:32 +0200175 if (dinfo->bdrv == bs) {
176 return dinfo;
177 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200178 }
Markus Armbrustere4700e52010-06-24 17:25:32 +0200179 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200180}
181
Markus Armbruster666daa62010-06-02 18:48:27 +0200182static void bdrv_format_print(void *opaque, const char *name)
183{
Markus Armbruster807105a2011-01-17 19:31:27 +0100184 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200185}
186
187void drive_uninit(DriveInfo *dinfo)
188{
189 qemu_opts_del(dinfo->opts);
190 bdrv_delete(dinfo->bdrv);
191 QTAILQ_REMOVE(&drives, dinfo, next);
192 qemu_free(dinfo);
193}
194
195static int parse_block_error_action(const char *buf, int is_read)
196{
197 if (!strcmp(buf, "ignore")) {
198 return BLOCK_ERR_IGNORE;
199 } else if (!is_read && !strcmp(buf, "enospc")) {
200 return BLOCK_ERR_STOP_ENOSPC;
201 } else if (!strcmp(buf, "stop")) {
202 return BLOCK_ERR_STOP_ANY;
203 } else if (!strcmp(buf, "report")) {
204 return BLOCK_ERR_REPORT;
205 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100206 error_report("'%s' invalid %s error action",
207 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200208 return -1;
209 }
210}
211
212DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
213{
214 const char *buf;
215 const char *file = NULL;
216 char devname[128];
217 const char *serial;
218 const char *mediastr = "";
219 BlockInterfaceType type;
220 enum { MEDIA_DISK, MEDIA_CDROM } media;
221 int bus_id, unit_id;
222 int cyls, heads, secs, translation;
223 BlockDriver *drv = NULL;
224 int max_devs;
225 int index;
226 int ro = 0;
227 int bdrv_flags = 0;
228 int on_read_error, on_write_error;
229 const char *devaddr;
230 DriveInfo *dinfo;
231 int snapshot = 0;
232 int ret;
233
234 *fatal_error = 1;
235
236 translation = BIOS_ATA_TRANSLATION_AUTO;
237
238 if (default_to_scsi) {
239 type = IF_SCSI;
Markus Armbruster666daa62010-06-02 18:48:27 +0200240 pstrcpy(devname, sizeof(devname), "scsi");
241 } else {
242 type = IF_IDE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200243 pstrcpy(devname, sizeof(devname), "ide");
244 }
245 media = MEDIA_DISK;
246
247 /* extract parameters */
248 bus_id = qemu_opt_get_number(opts, "bus", 0);
249 unit_id = qemu_opt_get_number(opts, "unit", -1);
250 index = qemu_opt_get_number(opts, "index", -1);
251
252 cyls = qemu_opt_get_number(opts, "cyls", 0);
253 heads = qemu_opt_get_number(opts, "heads", 0);
254 secs = qemu_opt_get_number(opts, "secs", 0);
255
256 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
257 ro = qemu_opt_get_bool(opts, "readonly", 0);
258
259 file = qemu_opt_get(opts, "file");
260 serial = qemu_opt_get(opts, "serial");
261
262 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
263 pstrcpy(devname, sizeof(devname), buf);
Markus Armbruster19609662011-01-28 11:21:39 +0100264 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
265 ;
266 if (type == IF_COUNT) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100267 error_report("unsupported bus type '%s'", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200268 return NULL;
269 }
270 }
Markus Armbruster19609662011-01-28 11:21:39 +0100271 max_devs = if_max_devs[type];
Markus Armbruster666daa62010-06-02 18:48:27 +0200272
273 if (cyls || heads || secs) {
274 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100275 error_report("invalid physical cyls number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200276 return NULL;
277 }
278 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100279 error_report("invalid physical heads number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200280 return NULL;
281 }
282 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100283 error_report("invalid physical secs number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200284 return NULL;
285 }
286 }
287
288 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
289 if (!cyls) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100290 error_report("'%s' trans must be used with cyls,heads and secs",
291 buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200292 return NULL;
293 }
294 if (!strcmp(buf, "none"))
295 translation = BIOS_ATA_TRANSLATION_NONE;
296 else if (!strcmp(buf, "lba"))
297 translation = BIOS_ATA_TRANSLATION_LBA;
298 else if (!strcmp(buf, "auto"))
299 translation = BIOS_ATA_TRANSLATION_AUTO;
300 else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100301 error_report("'%s' invalid translation type", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200302 return NULL;
303 }
304 }
305
306 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
307 if (!strcmp(buf, "disk")) {
308 media = MEDIA_DISK;
309 } else if (!strcmp(buf, "cdrom")) {
310 if (cyls || secs || heads) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100311 error_report("'%s' invalid physical CHS format", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200312 return NULL;
313 }
314 media = MEDIA_CDROM;
315 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100316 error_report("'%s' invalid media", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200317 return NULL;
318 }
319 }
320
321 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
322 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
323 bdrv_flags |= BDRV_O_NOCACHE;
324 } else if (!strcmp(buf, "writeback")) {
325 bdrv_flags |= BDRV_O_CACHE_WB;
326 } else if (!strcmp(buf, "unsafe")) {
327 bdrv_flags |= BDRV_O_CACHE_WB;
328 bdrv_flags |= BDRV_O_NO_FLUSH;
329 } else if (!strcmp(buf, "writethrough")) {
330 /* this is the default */
331 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100332 error_report("invalid cache option");
Markus Armbruster666daa62010-06-02 18:48:27 +0200333 return NULL;
334 }
335 }
336
337#ifdef CONFIG_LINUX_AIO
338 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
339 if (!strcmp(buf, "native")) {
340 bdrv_flags |= BDRV_O_NATIVE_AIO;
341 } else if (!strcmp(buf, "threads")) {
342 /* this is the default */
343 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100344 error_report("invalid aio option");
Markus Armbruster666daa62010-06-02 18:48:27 +0200345 return NULL;
346 }
347 }
348#endif
349
350 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
351 if (strcmp(buf, "?") == 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100352 error_printf("Supported formats:");
353 bdrv_iterate_format(bdrv_format_print, NULL);
354 error_printf("\n");
355 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200356 }
357 drv = bdrv_find_whitelisted_format(buf);
358 if (!drv) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100359 error_report("'%s' invalid format", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200360 return NULL;
361 }
362 }
363
364 on_write_error = BLOCK_ERR_STOP_ENOSPC;
365 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
366 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100367 error_report("werror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200368 return NULL;
369 }
370
371 on_write_error = parse_block_error_action(buf, 0);
372 if (on_write_error < 0) {
373 return NULL;
374 }
375 }
376
377 on_read_error = BLOCK_ERR_REPORT;
378 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolf5dba48a2010-10-25 14:52:21 +0200379 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100380 error_report("rerror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200381 return NULL;
382 }
383
384 on_read_error = parse_block_error_action(buf, 1);
385 if (on_read_error < 0) {
386 return NULL;
387 }
388 }
389
390 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
391 if (type != IF_VIRTIO) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100392 error_report("addr is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200393 return NULL;
394 }
395 }
396
397 /* compute bus and unit according index */
398
399 if (index != -1) {
400 if (bus_id != 0 || unit_id != -1) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100401 error_report("index cannot be used with bus and unit");
Markus Armbruster666daa62010-06-02 18:48:27 +0200402 return NULL;
403 }
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100404 bus_id = drive_index_to_bus_id(type, index);
405 unit_id = drive_index_to_unit_id(type, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200406 }
407
408 /* if user doesn't specify a unit_id,
409 * try to find the first free
410 */
411
412 if (unit_id == -1) {
413 unit_id = 0;
414 while (drive_get(type, bus_id, unit_id) != NULL) {
415 unit_id++;
416 if (max_devs && unit_id >= max_devs) {
417 unit_id -= max_devs;
418 bus_id++;
419 }
420 }
421 }
422
423 /* check unit id */
424
425 if (max_devs && unit_id >= max_devs) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100426 error_report("unit %d too big (max is %d)",
427 unit_id, max_devs - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200428 return NULL;
429 }
430
431 /*
432 * ignore multiple definitions
433 */
434
435 if (drive_get(type, bus_id, unit_id) != NULL) {
436 *fatal_error = 0;
437 return NULL;
438 }
439
440 /* init */
441
442 dinfo = qemu_mallocz(sizeof(*dinfo));
443 if ((buf = qemu_opts_id(opts)) != NULL) {
444 dinfo->id = qemu_strdup(buf);
445 } else {
446 /* no id supplied -> create one */
447 dinfo->id = qemu_mallocz(32);
448 if (type == IF_IDE || type == IF_SCSI)
449 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
450 if (max_devs)
451 snprintf(dinfo->id, 32, "%s%i%s%i",
452 devname, bus_id, mediastr, unit_id);
453 else
454 snprintf(dinfo->id, 32, "%s%s%i",
455 devname, mediastr, unit_id);
456 }
457 dinfo->bdrv = bdrv_new(dinfo->id);
458 dinfo->devaddr = devaddr;
459 dinfo->type = type;
460 dinfo->bus = bus_id;
461 dinfo->unit = unit_id;
Markus Armbruster666daa62010-06-02 18:48:27 +0200462 dinfo->opts = opts;
463 if (serial)
Luiz Capitulino653dbec2010-06-02 17:46:31 -0300464 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200465 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
466
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200467 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
468
Markus Armbruster666daa62010-06-02 18:48:27 +0200469 switch(type) {
470 case IF_IDE:
471 case IF_SCSI:
472 case IF_XEN:
473 case IF_NONE:
474 switch(media) {
475 case MEDIA_DISK:
476 if (cyls != 0) {
477 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
478 bdrv_set_translation_hint(dinfo->bdrv, translation);
479 }
480 break;
481 case MEDIA_CDROM:
482 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
483 break;
484 }
485 break;
486 case IF_SD:
487 /* FIXME: This isn't really a floppy, but it's a reasonable
488 approximation. */
489 case IF_FLOPPY:
490 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
491 break;
492 case IF_PFLASH:
493 case IF_MTD:
494 break;
495 case IF_VIRTIO:
496 /* add virtio block device */
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200497 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200498 qemu_opt_set(opts, "driver", "virtio-blk-pci");
499 qemu_opt_set(opts, "drive", dinfo->id);
500 if (devaddr)
501 qemu_opt_set(opts, "addr", devaddr);
502 break;
Markus Armbruster2292dda2011-01-28 11:21:41 +0100503 default:
Markus Armbruster666daa62010-06-02 18:48:27 +0200504 abort();
505 }
Markus Armbrusterdd5b0d72010-05-11 15:36:46 +0200506 if (!file || !*file) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200507 *fatal_error = 0;
508 return NULL;
509 }
510 if (snapshot) {
511 /* always use cache=unsafe with snapshot */
512 bdrv_flags &= ~BDRV_O_CACHE_MASK;
513 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
514 }
515
516 if (media == MEDIA_CDROM) {
517 /* CDROM is fine for any interface, don't check. */
518 ro = 1;
519 } else if (ro == 1) {
520 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100521 error_report("readonly not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200522 return NULL;
523 }
524 }
525
526 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
527
528 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
529 if (ret < 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100530 error_report("could not open disk image %s: %s",
531 file, strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +0200532 return NULL;
533 }
534
535 if (bdrv_key_required(dinfo->bdrv))
536 autostart = 0;
537 *fatal_error = 0;
538 return dinfo;
539}
540
541void do_commit(Monitor *mon, const QDict *qdict)
542{
Markus Armbruster666daa62010-06-02 18:48:27 +0200543 const char *device = qdict_get_str(qdict, "device");
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200544 BlockDriverState *bs;
Markus Armbruster666daa62010-06-02 18:48:27 +0200545
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200546 if (!strcmp(device, "all")) {
547 bdrv_commit_all();
548 } else {
549 bs = bdrv_find(device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200550 if (!bs) {
551 qerror_report(QERR_DEVICE_NOT_FOUND, device);
552 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200553 }
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200554 bdrv_commit(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200555 }
556}
557
Jes Sorensenf8882562010-12-16 13:52:16 +0100558int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
559{
560 const char *device = qdict_get_str(qdict, "device");
561 const char *filename = qdict_get_try_str(qdict, "snapshot_file");
562 const char *format = qdict_get_try_str(qdict, "format");
563 BlockDriverState *bs;
564 BlockDriver *drv, *proto_drv;
565 int ret = 0;
566 int flags;
567
Jes Sorensenc90f1b32011-01-06 17:02:23 +0100568 if (!filename) {
569 qerror_report(QERR_MISSING_PARAMETER, "snapshot_file");
570 ret = -1;
571 goto out;
572 }
573
Jes Sorensenf8882562010-12-16 13:52:16 +0100574 bs = bdrv_find(device);
575 if (!bs) {
576 qerror_report(QERR_DEVICE_NOT_FOUND, device);
577 ret = -1;
578 goto out;
579 }
580
581 if (!format) {
582 format = "qcow2";
583 }
584
585 drv = bdrv_find_format(format);
586 if (!drv) {
587 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
588 ret = -1;
589 goto out;
590 }
591
592 proto_drv = bdrv_find_protocol(filename);
593 if (!proto_drv) {
594 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
595 ret = -1;
596 goto out;
597 }
598
599 ret = bdrv_img_create(filename, format, bs->filename,
600 bs->drv->format_name, NULL, -1, bs->open_flags);
601 if (ret) {
602 goto out;
603 }
604
605 qemu_aio_flush();
606 bdrv_flush(bs);
607
608 flags = bs->open_flags;
609 bdrv_close(bs);
610 ret = bdrv_open(bs, filename, flags, drv);
611 /*
612 * If reopening the image file we just created fails, we really
613 * are in trouble :(
614 */
615 if (ret != 0) {
616 abort();
617 }
618out:
619 if (ret) {
620 ret = -1;
621 }
622
623 return ret;
624}
625
Markus Armbruster666daa62010-06-02 18:48:27 +0200626static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
627{
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300628 if (!force) {
629 if (!bdrv_is_removable(bs)) {
630 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
631 bdrv_get_device_name(bs));
632 return -1;
Markus Armbruster666daa62010-06-02 18:48:27 +0200633 }
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300634 if (bdrv_is_locked(bs)) {
635 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
636 return -1;
637 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200638 }
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300639 bdrv_close(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200640 return 0;
641}
642
643int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
644{
645 BlockDriverState *bs;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -0300646 int force = qdict_get_try_bool(qdict, "force", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200647 const char *filename = qdict_get_str(qdict, "device");
648
649 bs = bdrv_find(filename);
650 if (!bs) {
651 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
652 return -1;
653 }
654 return eject_device(mon, bs, force);
655}
656
657int do_block_set_passwd(Monitor *mon, const QDict *qdict,
658 QObject **ret_data)
659{
660 BlockDriverState *bs;
661 int err;
662
663 bs = bdrv_find(qdict_get_str(qdict, "device"));
664 if (!bs) {
665 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
666 return -1;
667 }
668
669 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
670 if (err == -EINVAL) {
671 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
672 return -1;
673 } else if (err < 0) {
674 qerror_report(QERR_INVALID_PASSWORD);
675 return -1;
676 }
677
678 return 0;
679}
680
681int do_change_block(Monitor *mon, const char *device,
682 const char *filename, const char *fmt)
683{
684 BlockDriverState *bs;
685 BlockDriver *drv = NULL;
686 int bdrv_flags;
687
688 bs = bdrv_find(device);
689 if (!bs) {
690 qerror_report(QERR_DEVICE_NOT_FOUND, device);
691 return -1;
692 }
693 if (fmt) {
694 drv = bdrv_find_whitelisted_format(fmt);
695 if (!drv) {
696 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
697 return -1;
698 }
699 }
700 if (eject_device(mon, bs, 0) < 0) {
701 return -1;
702 }
Markus Armbruster528f7662010-06-25 15:26:48 +0200703 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
Blue Swirl199630b2010-07-25 20:49:34 +0000704 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
Markus Armbruster666daa62010-06-02 18:48:27 +0200705 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
706 qerror_report(QERR_OPEN_FILE_FAILED, filename);
707 return -1;
708 }
709 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
710}
Ryan Harper9063f812010-11-12 11:07:13 -0600711
712int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
713{
714 const char *id = qdict_get_str(qdict, "id");
715 BlockDriverState *bs;
716 BlockDriverState **ptr;
717 Property *prop;
718
719 bs = bdrv_find(id);
720 if (!bs) {
721 qerror_report(QERR_DEVICE_NOT_FOUND, id);
722 return -1;
723 }
724
725 /* quiesce block driver; prevent further io */
726 qemu_aio_flush();
727 bdrv_flush(bs);
728 bdrv_close(bs);
729
730 /* clean up guest state from pointing to host resource by
731 * finding and removing DeviceState "drive" property */
Markus Armbruster850ec112011-01-17 19:31:29 +0100732 if (bs->peer) {
733 for (prop = bs->peer->info->props; prop && prop->name; prop++) {
734 if (prop->info->type == PROP_TYPE_DRIVE) {
735 ptr = qdev_get_prop_ptr(bs->peer, prop);
736 if (*ptr == bs) {
737 bdrv_detach(bs, bs->peer);
738 *ptr = NULL;
739 break;
740 }
Ryan Harper9063f812010-11-12 11:07:13 -0600741 }
742 }
743 }
744
745 /* clean up host side */
746 drive_uninit(drive_get_by_blockdev(bs));
747
748 return 0;
749}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +0100750
751/*
752 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
753 * existing QERR_ macro mess is cleaned up. A good example for better
754 * error reports can be found in the qemu-img resize code.
755 */
756int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
757{
758 const char *device = qdict_get_str(qdict, "device");
759 int64_t size = qdict_get_int(qdict, "size");
760 BlockDriverState *bs;
761
762 bs = bdrv_find(device);
763 if (!bs) {
764 qerror_report(QERR_DEVICE_NOT_FOUND, device);
765 return -1;
766 }
767
768 if (size < 0) {
769 qerror_report(QERR_UNDEFINED_ERROR);
770 return -1;
771 }
772
773 if (bdrv_truncate(bs, size)) {
774 qerror_report(QERR_UNDEFINED_ERROR);
775 return -1;
776 }
777
778 return 0;
779}