]> git.proxmox.com Git - qemu.git/blame - blockdev.c
async: Allow nested qemu_bh_poll calls
[qemu.git] / blockdev.c
CommitLineData
666daa68
MA
1/*
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"
9063f814
RH
17#include "hw/qdev.h"
18#include "block_int.h"
666daa68 19
c9b62a7e 20static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
666daa68 21
1960966d
MA
22static 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] = {
27d6bf40
MA
35 /*
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,
1960966d
MA
51};
52
14bafc54
MA
53/*
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
0fc0f1fa
RH
64 if (dinfo) {
65 dinfo->auto_del = 1;
66 }
14bafc54
MA
67}
68
69void blockdev_auto_del(BlockDriverState *bs)
70{
71 DriveInfo *dinfo = drive_get_by_blockdev(bs);
72
0fc0f1fa 73 if (dinfo && dinfo->auto_del) {
84fb3925 74 drive_put_ref(dinfo);
14bafc54
MA
75 }
76}
77
505a7fb1
MA
78static 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
2292ddae
MA
90QemuOpts *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,
5645b0f4 96 const char *optstr)
666daa68 97{
666daa68 98 QemuOpts *opts;
2292ddae 99 char buf[32];
666daa68 100
2292ddae 101 opts = drive_def(optstr);
666daa68
MA
102 if (!opts) {
103 return NULL;
104 }
2292ddae
MA
105 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 }
666daa68
MA
112 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
f1bd51ac
MA
133DriveInfo *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
666daa68
MA
140int 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
13839974
MA
154/* 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
e4700e59 164DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
666daa68
MA
165{
166 DriveInfo *dinfo;
167
168 QTAILQ_FOREACH(dinfo, &drives, next) {
e4700e59
MA
169 if (dinfo->bdrv == bs) {
170 return dinfo;
171 }
666daa68 172 }
e4700e59 173 return NULL;
666daa68
MA
174}
175
666daa68
MA
176static void bdrv_format_print(void *opaque, const char *name)
177{
807105a7 178 error_printf(" %s", name);
666daa68
MA
179}
180
84fb3925 181static void drive_uninit(DriveInfo *dinfo)
666daa68
MA
182{
183 qemu_opts_del(dinfo->opts);
184 bdrv_delete(dinfo->bdrv);
7267c094 185 g_free(dinfo->id);
666daa68 186 QTAILQ_REMOVE(&drives, dinfo, next);
7267c094 187 g_free(dinfo);
666daa68
MA
188}
189
84fb3925
MT
190void 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
666daa68
MA
203static 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 {
807105a7
MA
214 error_report("'%s' invalid %s error action",
215 buf, is_read ? "read" : "write");
666daa68
MA
216 return -1;
217 }
218}
219
319ae529 220DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
666daa68
MA
221{
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
666daa68 242 translation = BIOS_ATA_TRANSLATION_AUTO;
666daa68
MA
243 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);
1960966d
MA
262 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
263 ;
264 if (type == IF_COUNT) {
807105a7 265 error_report("unsupported bus type '%s'", buf);
666daa68
MA
266 return NULL;
267 }
2d3999fe
LC
268 } else {
269 type = default_to_scsi ? IF_SCSI : IF_IDE;
270 pstrcpy(devname, sizeof(devname), if_name[type]);
666daa68 271 }
2d3999fe 272
1960966d 273 max_devs = if_max_devs[type];
666daa68
MA
274
275 if (cyls || heads || secs) {
276 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
807105a7 277 error_report("invalid physical cyls number");
666daa68
MA
278 return NULL;
279 }
280 if (heads < 1 || (type == IF_IDE && heads > 16)) {
807105a7 281 error_report("invalid physical heads number");
666daa68
MA
282 return NULL;
283 }
284 if (secs < 1 || (type == IF_IDE && secs > 63)) {
807105a7 285 error_report("invalid physical secs number");
666daa68
MA
286 return NULL;
287 }
288 }
289
290 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
291 if (!cyls) {
e4080f9b 292 error_report("'%s' trans must be used with cyls, heads and secs",
807105a7 293 buf);
666daa68
MA
294 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 {
807105a7 303 error_report("'%s' invalid translation type", buf);
666daa68
MA
304 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) {
e7ff8f0e 313 error_report("CHS can't be set with media=%s", buf);
666daa68
MA
314 return NULL;
315 }
316 media = MEDIA_CDROM;
317 } else {
807105a7 318 error_report("'%s' invalid media", buf);
666daa68
MA
319 return NULL;
320 }
321 }
322
323 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
c3993cdc
SH
324 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
325 error_report("invalid cache option");
326 return NULL;
666daa68
MA
327 }
328 }
329
330#ifdef CONFIG_LINUX_AIO
331 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
332 if (!strcmp(buf, "native")) {
333 bdrv_flags |= BDRV_O_NATIVE_AIO;
334 } else if (!strcmp(buf, "threads")) {
335 /* this is the default */
336 } else {
807105a7 337 error_report("invalid aio option");
666daa68
MA
338 return NULL;
339 }
340 }
341#endif
342
343 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
344 if (strcmp(buf, "?") == 0) {
807105a7
MA
345 error_printf("Supported formats:");
346 bdrv_iterate_format(bdrv_format_print, NULL);
347 error_printf("\n");
348 return NULL;
666daa68
MA
349 }
350 drv = bdrv_find_whitelisted_format(buf);
351 if (!drv) {
807105a7 352 error_report("'%s' invalid format", buf);
666daa68
MA
353 return NULL;
354 }
355 }
356
357 on_write_error = BLOCK_ERR_STOP_ENOSPC;
358 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
359 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
807105a7 360 error_report("werror is not supported by this bus type");
666daa68
MA
361 return NULL;
362 }
363
364 on_write_error = parse_block_error_action(buf, 0);
365 if (on_write_error < 0) {
366 return NULL;
367 }
368 }
369
370 on_read_error = BLOCK_ERR_REPORT;
371 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
5dba48a8 372 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
807105a7 373 error_report("rerror is not supported by this bus type");
666daa68
MA
374 return NULL;
375 }
376
377 on_read_error = parse_block_error_action(buf, 1);
378 if (on_read_error < 0) {
379 return NULL;
380 }
381 }
382
383 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
384 if (type != IF_VIRTIO) {
807105a7 385 error_report("addr is not supported by this bus type");
666daa68
MA
386 return NULL;
387 }
388 }
389
390 /* compute bus and unit according index */
391
392 if (index != -1) {
393 if (bus_id != 0 || unit_id != -1) {
807105a7 394 error_report("index cannot be used with bus and unit");
666daa68
MA
395 return NULL;
396 }
505a7fb1
MA
397 bus_id = drive_index_to_bus_id(type, index);
398 unit_id = drive_index_to_unit_id(type, index);
666daa68
MA
399 }
400
401 /* if user doesn't specify a unit_id,
402 * try to find the first free
403 */
404
405 if (unit_id == -1) {
406 unit_id = 0;
407 while (drive_get(type, bus_id, unit_id) != NULL) {
408 unit_id++;
409 if (max_devs && unit_id >= max_devs) {
410 unit_id -= max_devs;
411 bus_id++;
412 }
413 }
414 }
415
416 /* check unit id */
417
418 if (max_devs && unit_id >= max_devs) {
807105a7
MA
419 error_report("unit %d too big (max is %d)",
420 unit_id, max_devs - 1);
666daa68
MA
421 return NULL;
422 }
423
424 /*
4e5d9b57 425 * catch multiple definitions
666daa68
MA
426 */
427
428 if (drive_get(type, bus_id, unit_id) != NULL) {
4e5d9b57
MA
429 error_report("drive with bus=%d, unit=%d (index=%d) exists",
430 bus_id, unit_id, index);
666daa68
MA
431 return NULL;
432 }
433
434 /* init */
435
7267c094 436 dinfo = g_malloc0(sizeof(*dinfo));
666daa68 437 if ((buf = qemu_opts_id(opts)) != NULL) {
7267c094 438 dinfo->id = g_strdup(buf);
666daa68
MA
439 } else {
440 /* no id supplied -> create one */
7267c094 441 dinfo->id = g_malloc0(32);
666daa68
MA
442 if (type == IF_IDE || type == IF_SCSI)
443 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
444 if (max_devs)
445 snprintf(dinfo->id, 32, "%s%i%s%i",
446 devname, bus_id, mediastr, unit_id);
447 else
448 snprintf(dinfo->id, 32, "%s%s%i",
449 devname, mediastr, unit_id);
450 }
451 dinfo->bdrv = bdrv_new(dinfo->id);
452 dinfo->devaddr = devaddr;
453 dinfo->type = type;
454 dinfo->bus = bus_id;
455 dinfo->unit = unit_id;
666daa68 456 dinfo->opts = opts;
84fb3925 457 dinfo->refcount = 1;
666daa68 458 if (serial)
653dbec7 459 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
666daa68
MA
460 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
461
abd7f68d
MA
462 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
463
666daa68
MA
464 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:
8d278467 477 bdrv_set_removable(dinfo->bdrv, 1);
95b5edcd 478 dinfo->media_cd = 1;
666daa68
MA
479 break;
480 }
481 break;
482 case IF_SD:
483 /* FIXME: This isn't really a floppy, but it's a reasonable
484 approximation. */
485 case IF_FLOPPY:
8d278467 486 bdrv_set_removable(dinfo->bdrv, 1);
666daa68
MA
487 break;
488 case IF_PFLASH:
489 case IF_MTD:
490 break;
491 case IF_VIRTIO:
492 /* add virtio block device */
3329f07b 493 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
29f82b37 494 qemu_opt_set(opts, "driver", "virtio-blk");
666daa68
MA
495 qemu_opt_set(opts, "drive", dinfo->id);
496 if (devaddr)
497 qemu_opt_set(opts, "addr", devaddr);
498 break;
2292ddae 499 default:
666daa68
MA
500 abort();
501 }
dd5b0d71 502 if (!file || !*file) {
319ae529 503 return dinfo;
666daa68
MA
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) {
807105a7 516 error_report("readonly not supported by this bus type");
a9ae2bff 517 goto err;
666daa68
MA
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) {
807105a7
MA
525 error_report("could not open disk image %s: %s",
526 file, strerror(-ret));
a9ae2bff 527 goto err;
666daa68
MA
528 }
529
530 if (bdrv_key_required(dinfo->bdrv))
531 autostart = 0;
666daa68 532 return dinfo;
a9ae2bff
MA
533
534err:
535 bdrv_delete(dinfo->bdrv);
7267c094 536 g_free(dinfo->id);
a9ae2bff 537 QTAILQ_REMOVE(&drives, dinfo, next);
7267c094 538 g_free(dinfo);
a9ae2bff 539 return NULL;
666daa68
MA
540}
541
542void do_commit(Monitor *mon, const QDict *qdict)
543{
666daa68 544 const char *device = qdict_get_str(qdict, "device");
6ab4b5ab 545 BlockDriverState *bs;
666daa68 546
6ab4b5ab
MA
547 if (!strcmp(device, "all")) {
548 bdrv_commit_all();
549 } else {
550 bs = bdrv_find(device);
ac59eb95
MA
551 if (!bs) {
552 qerror_report(QERR_DEVICE_NOT_FOUND, device);
553 return;
6ab4b5ab 554 }
ac59eb95 555 bdrv_commit(bs);
666daa68
MA
556 }
557}
558
f8882568
JS
559int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
560{
561 const char *device = qdict_get_str(qdict, "device");
d967b2f1 562 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
f8882568
JS
563 const char *format = qdict_get_try_str(qdict, "format");
564 BlockDriverState *bs;
52f9a172 565 BlockDriver *drv, *old_drv, *proto_drv;
f8882568
JS
566 int ret = 0;
567 int flags;
52f9a172 568 char old_filename[1024];
f8882568 569
c90f1b32 570 if (!filename) {
d967b2f1 571 qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
c90f1b32
JS
572 ret = -1;
573 goto out;
574 }
575
f8882568
JS
576 bs = bdrv_find(device);
577 if (!bs) {
578 qerror_report(QERR_DEVICE_NOT_FOUND, device);
579 ret = -1;
580 goto out;
581 }
582
52f9a172
JS
583 pstrcpy(old_filename, sizeof(old_filename), bs->filename);
584
585 old_drv = bs->drv;
586 flags = bs->open_flags;
587
f8882568
JS
588 if (!format) {
589 format = "qcow2";
590 }
591
592 drv = bdrv_find_format(format);
593 if (!drv) {
594 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
595 ret = -1;
596 goto out;
597 }
598
599 proto_drv = bdrv_find_protocol(filename);
600 if (!proto_drv) {
601 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
602 ret = -1;
603 goto out;
604 }
605
606 ret = bdrv_img_create(filename, format, bs->filename,
52f9a172 607 bs->drv->format_name, NULL, -1, flags);
f8882568
JS
608 if (ret) {
609 goto out;
610 }
611
612 qemu_aio_flush();
613 bdrv_flush(bs);
614
f8882568
JS
615 bdrv_close(bs);
616 ret = bdrv_open(bs, filename, flags, drv);
617 /*
52f9a172
JS
618 * If reopening the image file we just created fails, fall back
619 * and try to re-open the original image. If that fails too, we
620 * are in serious trouble.
f8882568
JS
621 */
622 if (ret != 0) {
52f9a172
JS
623 ret = bdrv_open(bs, old_filename, flags, old_drv);
624 if (ret != 0) {
625 qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
626 } else {
627 qerror_report(QERR_OPEN_FILE_FAILED, filename);
628 }
f8882568
JS
629 }
630out:
631 if (ret) {
632 ret = -1;
633 }
634
635 return ret;
636}
637
666daa68
MA
638static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
639{
ea8f942f
MA
640 if (!bdrv_is_removable(bs)) {
641 qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
642 return -1;
643 }
644 if (!force && bdrv_is_locked(bs)) {
645 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
646 return -1;
666daa68 647 }
3b5276b5 648 bdrv_close(bs);
666daa68
MA
649 return 0;
650}
651
652int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
653{
654 BlockDriverState *bs;
eb159d13 655 int force = qdict_get_try_bool(qdict, "force", 0);
666daa68
MA
656 const char *filename = qdict_get_str(qdict, "device");
657
658 bs = bdrv_find(filename);
659 if (!bs) {
660 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
661 return -1;
662 }
663 return eject_device(mon, bs, force);
664}
665
666int do_block_set_passwd(Monitor *mon, const QDict *qdict,
667 QObject **ret_data)
668{
669 BlockDriverState *bs;
670 int err;
671
672 bs = bdrv_find(qdict_get_str(qdict, "device"));
673 if (!bs) {
674 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
675 return -1;
676 }
677
678 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
679 if (err == -EINVAL) {
680 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
681 return -1;
682 } else if (err < 0) {
683 qerror_report(QERR_INVALID_PASSWORD);
684 return -1;
685 }
686
687 return 0;
688}
689
690int do_change_block(Monitor *mon, const char *device,
691 const char *filename, const char *fmt)
692{
693 BlockDriverState *bs;
694 BlockDriver *drv = NULL;
695 int bdrv_flags;
696
697 bs = bdrv_find(device);
698 if (!bs) {
699 qerror_report(QERR_DEVICE_NOT_FOUND, device);
700 return -1;
701 }
702 if (fmt) {
703 drv = bdrv_find_whitelisted_format(fmt);
704 if (!drv) {
705 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
706 return -1;
707 }
708 }
709 if (eject_device(mon, bs, 0) < 0) {
710 return -1;
711 }
528f7663 712 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
199630b6 713 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
666daa68
MA
714 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
715 qerror_report(QERR_OPEN_FILE_FAILED, filename);
716 return -1;
717 }
718 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
719}
9063f814
RH
720
721int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
722{
723 const char *id = qdict_get_str(qdict, "id");
724 BlockDriverState *bs;
9063f814
RH
725
726 bs = bdrv_find(id);
727 if (!bs) {
728 qerror_report(QERR_DEVICE_NOT_FOUND, id);
729 return -1;
730 }
8591675f
MT
731 if (bdrv_in_use(bs)) {
732 qerror_report(QERR_DEVICE_IN_USE, id);
733 return -1;
734 }
9063f814
RH
735
736 /* quiesce block driver; prevent further io */
737 qemu_aio_flush();
738 bdrv_flush(bs);
739 bdrv_close(bs);
740
d22b2f41
RH
741 /* if we have a device associated with this BlockDriverState (bs->peer)
742 * then we need to make the drive anonymous until the device
743 * can be removed. If this is a drive with no device backing
744 * then we can just get rid of the block driver state right here.
745 */
850ec113 746 if (bs->peer) {
d22b2f41
RH
747 bdrv_make_anon(bs);
748 } else {
749 drive_uninit(drive_get_by_blockdev(bs));
9063f814
RH
750 }
751
9063f814
RH
752 return 0;
753}
6d4a2b3a
CH
754
755/*
756 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
757 * existing QERR_ macro mess is cleaned up. A good example for better
758 * error reports can be found in the qemu-img resize code.
759 */
760int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
761{
762 const char *device = qdict_get_str(qdict, "device");
763 int64_t size = qdict_get_int(qdict, "size");
764 BlockDriverState *bs;
765
766 bs = bdrv_find(device);
767 if (!bs) {
768 qerror_report(QERR_DEVICE_NOT_FOUND, device);
769 return -1;
770 }
771
772 if (size < 0) {
773 qerror_report(QERR_UNDEFINED_ERROR);
774 return -1;
775 }
776
777 if (bdrv_truncate(bs, size)) {
778 qerror_report(QERR_UNDEFINED_ERROR);
779 return -1;
780 }
781
782 return 0;
783}