]> git.proxmox.com Git - qemu.git/blame - blockdev.c
block: Attach non-qdev devices as well
[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 17#include "block_int.h"
666daa68 18
c9b62a7e 19static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
666daa68 20
1960966d
MA
21static const char *const if_name[IF_COUNT] = {
22 [IF_NONE] = "none",
23 [IF_IDE] = "ide",
24 [IF_SCSI] = "scsi",
25 [IF_FLOPPY] = "floppy",
26 [IF_PFLASH] = "pflash",
27 [IF_MTD] = "mtd",
28 [IF_SD] = "sd",
29 [IF_VIRTIO] = "virtio",
30 [IF_XEN] = "xen",
31};
32
33static const int if_max_devs[IF_COUNT] = {
27d6bf40
MA
34 /*
35 * Do not change these numbers! They govern how drive option
36 * index maps to unit and bus. That mapping is ABI.
37 *
38 * All controllers used to imlement if=T drives need to support
39 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
40 * Otherwise, some index values map to "impossible" bus, unit
41 * values.
42 *
43 * For instance, if you change [IF_SCSI] to 255, -drive
44 * if=scsi,index=12 no longer means bus=1,unit=5, but
45 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
46 * the drive can't be set up. Regression.
47 */
48 [IF_IDE] = 2,
49 [IF_SCSI] = 7,
1960966d
MA
50};
51
14bafc54
MA
52/*
53 * We automatically delete the drive when a device using it gets
54 * unplugged. Questionable feature, but we can't just drop it.
55 * Device models call blockdev_mark_auto_del() to schedule the
56 * automatic deletion, and generic qdev code calls blockdev_auto_del()
57 * when deletion is actually safe.
58 */
59void blockdev_mark_auto_del(BlockDriverState *bs)
60{
61 DriveInfo *dinfo = drive_get_by_blockdev(bs);
62
0fc0f1fa
RH
63 if (dinfo) {
64 dinfo->auto_del = 1;
65 }
14bafc54
MA
66}
67
68void blockdev_auto_del(BlockDriverState *bs)
69{
70 DriveInfo *dinfo = drive_get_by_blockdev(bs);
71
0fc0f1fa 72 if (dinfo && dinfo->auto_del) {
84fb3925 73 drive_put_ref(dinfo);
14bafc54
MA
74 }
75}
76
505a7fb1
MA
77static int drive_index_to_bus_id(BlockInterfaceType type, int index)
78{
79 int max_devs = if_max_devs[type];
80 return max_devs ? index / max_devs : 0;
81}
82
83static int drive_index_to_unit_id(BlockInterfaceType type, int index)
84{
85 int max_devs = if_max_devs[type];
86 return max_devs ? index % max_devs : index;
87}
88
2292ddae
MA
89QemuOpts *drive_def(const char *optstr)
90{
91 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
92}
93
94QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
5645b0f4 95 const char *optstr)
666daa68 96{
666daa68 97 QemuOpts *opts;
2292ddae 98 char buf[32];
666daa68 99
2292ddae 100 opts = drive_def(optstr);
666daa68
MA
101 if (!opts) {
102 return NULL;
103 }
2292ddae
MA
104 if (type != IF_DEFAULT) {
105 qemu_opt_set(opts, "if", if_name[type]);
106 }
107 if (index >= 0) {
108 snprintf(buf, sizeof(buf), "%d", index);
109 qemu_opt_set(opts, "index", buf);
110 }
666daa68
MA
111 if (file)
112 qemu_opt_set(opts, "file", file);
113 return opts;
114}
115
116DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
117{
118 DriveInfo *dinfo;
119
120 /* seek interface, bus and unit */
121
122 QTAILQ_FOREACH(dinfo, &drives, next) {
123 if (dinfo->type == type &&
124 dinfo->bus == bus &&
125 dinfo->unit == unit)
126 return dinfo;
127 }
128
129 return NULL;
130}
131
f1bd51ac
MA
132DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
133{
134 return drive_get(type,
135 drive_index_to_bus_id(type, index),
136 drive_index_to_unit_id(type, index));
137}
138
666daa68
MA
139int drive_get_max_bus(BlockInterfaceType type)
140{
141 int max_bus;
142 DriveInfo *dinfo;
143
144 max_bus = -1;
145 QTAILQ_FOREACH(dinfo, &drives, next) {
146 if(dinfo->type == type &&
147 dinfo->bus > max_bus)
148 max_bus = dinfo->bus;
149 }
150 return max_bus;
151}
152
13839974
MA
153/* Get a block device. This should only be used for single-drive devices
154 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
155 appropriate bus. */
156DriveInfo *drive_get_next(BlockInterfaceType type)
157{
158 static int next_block_unit[IF_COUNT];
159
160 return drive_get(type, 0, next_block_unit[type]++);
161}
162
e4700e59 163DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
666daa68
MA
164{
165 DriveInfo *dinfo;
166
167 QTAILQ_FOREACH(dinfo, &drives, next) {
e4700e59
MA
168 if (dinfo->bdrv == bs) {
169 return dinfo;
170 }
666daa68 171 }
e4700e59 172 return NULL;
666daa68
MA
173}
174
666daa68
MA
175static void bdrv_format_print(void *opaque, const char *name)
176{
807105a7 177 error_printf(" %s", name);
666daa68
MA
178}
179
84fb3925 180static void drive_uninit(DriveInfo *dinfo)
666daa68
MA
181{
182 qemu_opts_del(dinfo->opts);
183 bdrv_delete(dinfo->bdrv);
7267c094 184 g_free(dinfo->id);
666daa68 185 QTAILQ_REMOVE(&drives, dinfo, next);
7267c094 186 g_free(dinfo);
666daa68
MA
187}
188
84fb3925
MT
189void drive_put_ref(DriveInfo *dinfo)
190{
191 assert(dinfo->refcount);
192 if (--dinfo->refcount == 0) {
193 drive_uninit(dinfo);
194 }
195}
196
197void drive_get_ref(DriveInfo *dinfo)
198{
199 dinfo->refcount++;
200}
201
666daa68
MA
202static int parse_block_error_action(const char *buf, int is_read)
203{
204 if (!strcmp(buf, "ignore")) {
205 return BLOCK_ERR_IGNORE;
206 } else if (!is_read && !strcmp(buf, "enospc")) {
207 return BLOCK_ERR_STOP_ENOSPC;
208 } else if (!strcmp(buf, "stop")) {
209 return BLOCK_ERR_STOP_ANY;
210 } else if (!strcmp(buf, "report")) {
211 return BLOCK_ERR_REPORT;
212 } else {
807105a7
MA
213 error_report("'%s' invalid %s error action",
214 buf, is_read ? "read" : "write");
666daa68
MA
215 return -1;
216 }
217}
218
319ae529 219DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
666daa68
MA
220{
221 const char *buf;
222 const char *file = NULL;
223 char devname[128];
224 const char *serial;
225 const char *mediastr = "";
226 BlockInterfaceType type;
227 enum { MEDIA_DISK, MEDIA_CDROM } media;
228 int bus_id, unit_id;
229 int cyls, heads, secs, translation;
230 BlockDriver *drv = NULL;
231 int max_devs;
232 int index;
233 int ro = 0;
234 int bdrv_flags = 0;
235 int on_read_error, on_write_error;
236 const char *devaddr;
237 DriveInfo *dinfo;
238 int snapshot = 0;
239 int ret;
240
666daa68 241 translation = BIOS_ATA_TRANSLATION_AUTO;
666daa68
MA
242 media = MEDIA_DISK;
243
244 /* extract parameters */
245 bus_id = qemu_opt_get_number(opts, "bus", 0);
246 unit_id = qemu_opt_get_number(opts, "unit", -1);
247 index = qemu_opt_get_number(opts, "index", -1);
248
249 cyls = qemu_opt_get_number(opts, "cyls", 0);
250 heads = qemu_opt_get_number(opts, "heads", 0);
251 secs = qemu_opt_get_number(opts, "secs", 0);
252
253 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
254 ro = qemu_opt_get_bool(opts, "readonly", 0);
255
256 file = qemu_opt_get(opts, "file");
257 serial = qemu_opt_get(opts, "serial");
258
259 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
260 pstrcpy(devname, sizeof(devname), buf);
1960966d
MA
261 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
262 ;
263 if (type == IF_COUNT) {
807105a7 264 error_report("unsupported bus type '%s'", buf);
666daa68
MA
265 return NULL;
266 }
2d3999fe
LC
267 } else {
268 type = default_to_scsi ? IF_SCSI : IF_IDE;
269 pstrcpy(devname, sizeof(devname), if_name[type]);
666daa68 270 }
2d3999fe 271
1960966d 272 max_devs = if_max_devs[type];
666daa68
MA
273
274 if (cyls || heads || secs) {
275 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
807105a7 276 error_report("invalid physical cyls number");
666daa68
MA
277 return NULL;
278 }
279 if (heads < 1 || (type == IF_IDE && heads > 16)) {
807105a7 280 error_report("invalid physical heads number");
666daa68
MA
281 return NULL;
282 }
283 if (secs < 1 || (type == IF_IDE && secs > 63)) {
807105a7 284 error_report("invalid physical secs number");
666daa68
MA
285 return NULL;
286 }
287 }
288
289 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
290 if (!cyls) {
e4080f9b 291 error_report("'%s' trans must be used with cyls, heads and secs",
807105a7 292 buf);
666daa68
MA
293 return NULL;
294 }
295 if (!strcmp(buf, "none"))
296 translation = BIOS_ATA_TRANSLATION_NONE;
297 else if (!strcmp(buf, "lba"))
298 translation = BIOS_ATA_TRANSLATION_LBA;
299 else if (!strcmp(buf, "auto"))
300 translation = BIOS_ATA_TRANSLATION_AUTO;
301 else {
807105a7 302 error_report("'%s' invalid translation type", buf);
666daa68
MA
303 return NULL;
304 }
305 }
306
307 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
308 if (!strcmp(buf, "disk")) {
309 media = MEDIA_DISK;
310 } else if (!strcmp(buf, "cdrom")) {
311 if (cyls || secs || heads) {
e7ff8f0e 312 error_report("CHS can't be set with media=%s", buf);
666daa68
MA
313 return NULL;
314 }
315 media = MEDIA_CDROM;
316 } else {
807105a7 317 error_report("'%s' invalid media", buf);
666daa68
MA
318 return NULL;
319 }
320 }
321
322 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
c3993cdc
SH
323 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
324 error_report("invalid cache option");
325 return NULL;
666daa68
MA
326 }
327 }
328
329#ifdef CONFIG_LINUX_AIO
330 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
331 if (!strcmp(buf, "native")) {
332 bdrv_flags |= BDRV_O_NATIVE_AIO;
333 } else if (!strcmp(buf, "threads")) {
334 /* this is the default */
335 } else {
807105a7 336 error_report("invalid aio option");
666daa68
MA
337 return NULL;
338 }
339 }
340#endif
341
342 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
343 if (strcmp(buf, "?") == 0) {
807105a7
MA
344 error_printf("Supported formats:");
345 bdrv_iterate_format(bdrv_format_print, NULL);
346 error_printf("\n");
347 return NULL;
666daa68
MA
348 }
349 drv = bdrv_find_whitelisted_format(buf);
350 if (!drv) {
807105a7 351 error_report("'%s' invalid format", buf);
666daa68
MA
352 return NULL;
353 }
354 }
355
356 on_write_error = BLOCK_ERR_STOP_ENOSPC;
357 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
358 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
807105a7 359 error_report("werror is not supported by this bus type");
666daa68
MA
360 return NULL;
361 }
362
363 on_write_error = parse_block_error_action(buf, 0);
364 if (on_write_error < 0) {
365 return NULL;
366 }
367 }
368
369 on_read_error = BLOCK_ERR_REPORT;
370 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
5dba48a8 371 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
807105a7 372 error_report("rerror is not supported by this bus type");
666daa68
MA
373 return NULL;
374 }
375
376 on_read_error = parse_block_error_action(buf, 1);
377 if (on_read_error < 0) {
378 return NULL;
379 }
380 }
381
382 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
383 if (type != IF_VIRTIO) {
807105a7 384 error_report("addr is not supported by this bus type");
666daa68
MA
385 return NULL;
386 }
387 }
388
389 /* compute bus and unit according index */
390
391 if (index != -1) {
392 if (bus_id != 0 || unit_id != -1) {
807105a7 393 error_report("index cannot be used with bus and unit");
666daa68
MA
394 return NULL;
395 }
505a7fb1
MA
396 bus_id = drive_index_to_bus_id(type, index);
397 unit_id = drive_index_to_unit_id(type, index);
666daa68
MA
398 }
399
400 /* if user doesn't specify a unit_id,
401 * try to find the first free
402 */
403
404 if (unit_id == -1) {
405 unit_id = 0;
406 while (drive_get(type, bus_id, unit_id) != NULL) {
407 unit_id++;
408 if (max_devs && unit_id >= max_devs) {
409 unit_id -= max_devs;
410 bus_id++;
411 }
412 }
413 }
414
415 /* check unit id */
416
417 if (max_devs && unit_id >= max_devs) {
807105a7
MA
418 error_report("unit %d too big (max is %d)",
419 unit_id, max_devs - 1);
666daa68
MA
420 return NULL;
421 }
422
423 /*
4e5d9b57 424 * catch multiple definitions
666daa68
MA
425 */
426
427 if (drive_get(type, bus_id, unit_id) != NULL) {
4e5d9b57
MA
428 error_report("drive with bus=%d, unit=%d (index=%d) exists",
429 bus_id, unit_id, index);
666daa68
MA
430 return NULL;
431 }
432
433 /* init */
434
7267c094 435 dinfo = g_malloc0(sizeof(*dinfo));
666daa68 436 if ((buf = qemu_opts_id(opts)) != NULL) {
7267c094 437 dinfo->id = g_strdup(buf);
666daa68
MA
438 } else {
439 /* no id supplied -> create one */
7267c094 440 dinfo->id = g_malloc0(32);
666daa68
MA
441 if (type == IF_IDE || type == IF_SCSI)
442 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
443 if (max_devs)
444 snprintf(dinfo->id, 32, "%s%i%s%i",
445 devname, bus_id, mediastr, unit_id);
446 else
447 snprintf(dinfo->id, 32, "%s%s%i",
448 devname, mediastr, unit_id);
449 }
450 dinfo->bdrv = bdrv_new(dinfo->id);
451 dinfo->devaddr = devaddr;
452 dinfo->type = type;
453 dinfo->bus = bus_id;
454 dinfo->unit = unit_id;
666daa68 455 dinfo->opts = opts;
84fb3925 456 dinfo->refcount = 1;
666daa68 457 if (serial)
653dbec7 458 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
666daa68
MA
459 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
460
abd7f68d
MA
461 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
462
666daa68
MA
463 switch(type) {
464 case IF_IDE:
465 case IF_SCSI:
466 case IF_XEN:
467 case IF_NONE:
468 switch(media) {
469 case MEDIA_DISK:
470 if (cyls != 0) {
471 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
472 bdrv_set_translation_hint(dinfo->bdrv, translation);
473 }
474 break;
475 case MEDIA_CDROM:
8d278467 476 bdrv_set_removable(dinfo->bdrv, 1);
95b5edcd 477 dinfo->media_cd = 1;
666daa68
MA
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:
8d278467 485 bdrv_set_removable(dinfo->bdrv, 1);
666daa68
MA
486 break;
487 case IF_PFLASH:
488 case IF_MTD:
489 break;
490 case IF_VIRTIO:
491 /* add virtio block device */
3329f07b 492 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
29f82b37 493 qemu_opt_set(opts, "driver", "virtio-blk");
666daa68
MA
494 qemu_opt_set(opts, "drive", dinfo->id);
495 if (devaddr)
496 qemu_opt_set(opts, "addr", devaddr);
497 break;
2292ddae 498 default:
666daa68
MA
499 abort();
500 }
dd5b0d71 501 if (!file || !*file) {
319ae529 502 return dinfo;
666daa68
MA
503 }
504 if (snapshot) {
505 /* always use cache=unsafe with snapshot */
506 bdrv_flags &= ~BDRV_O_CACHE_MASK;
507 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
508 }
509
510 if (media == MEDIA_CDROM) {
511 /* CDROM is fine for any interface, don't check. */
512 ro = 1;
513 } else if (ro == 1) {
514 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
807105a7 515 error_report("readonly not supported by this bus type");
a9ae2bff 516 goto err;
666daa68
MA
517 }
518 }
519
520 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
521
522 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
523 if (ret < 0) {
807105a7
MA
524 error_report("could not open disk image %s: %s",
525 file, strerror(-ret));
a9ae2bff 526 goto err;
666daa68
MA
527 }
528
529 if (bdrv_key_required(dinfo->bdrv))
530 autostart = 0;
666daa68 531 return dinfo;
a9ae2bff
MA
532
533err:
534 bdrv_delete(dinfo->bdrv);
7267c094 535 g_free(dinfo->id);
a9ae2bff 536 QTAILQ_REMOVE(&drives, dinfo, next);
7267c094 537 g_free(dinfo);
a9ae2bff 538 return NULL;
666daa68
MA
539}
540
541void do_commit(Monitor *mon, const QDict *qdict)
542{
666daa68 543 const char *device = qdict_get_str(qdict, "device");
6ab4b5ab 544 BlockDriverState *bs;
666daa68 545
6ab4b5ab
MA
546 if (!strcmp(device, "all")) {
547 bdrv_commit_all();
548 } else {
549 bs = bdrv_find(device);
ac59eb95
MA
550 if (!bs) {
551 qerror_report(QERR_DEVICE_NOT_FOUND, device);
552 return;
6ab4b5ab 553 }
ac59eb95 554 bdrv_commit(bs);
666daa68
MA
555 }
556}
557
f8882568
JS
558int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
559{
560 const char *device = qdict_get_str(qdict, "device");
d967b2f1 561 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
f8882568
JS
562 const char *format = qdict_get_try_str(qdict, "format");
563 BlockDriverState *bs;
52f9a172 564 BlockDriver *drv, *old_drv, *proto_drv;
f8882568
JS
565 int ret = 0;
566 int flags;
52f9a172 567 char old_filename[1024];
f8882568 568
c90f1b32 569 if (!filename) {
d967b2f1 570 qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
c90f1b32
JS
571 ret = -1;
572 goto out;
573 }
574
f8882568
JS
575 bs = bdrv_find(device);
576 if (!bs) {
577 qerror_report(QERR_DEVICE_NOT_FOUND, device);
578 ret = -1;
579 goto out;
580 }
581
52f9a172
JS
582 pstrcpy(old_filename, sizeof(old_filename), bs->filename);
583
584 old_drv = bs->drv;
585 flags = bs->open_flags;
586
f8882568
JS
587 if (!format) {
588 format = "qcow2";
589 }
590
591 drv = bdrv_find_format(format);
592 if (!drv) {
593 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
594 ret = -1;
595 goto out;
596 }
597
598 proto_drv = bdrv_find_protocol(filename);
599 if (!proto_drv) {
600 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
601 ret = -1;
602 goto out;
603 }
604
605 ret = bdrv_img_create(filename, format, bs->filename,
52f9a172 606 bs->drv->format_name, NULL, -1, flags);
f8882568
JS
607 if (ret) {
608 goto out;
609 }
610
611 qemu_aio_flush();
612 bdrv_flush(bs);
613
f8882568
JS
614 bdrv_close(bs);
615 ret = bdrv_open(bs, filename, flags, drv);
616 /*
52f9a172
JS
617 * If reopening the image file we just created fails, fall back
618 * and try to re-open the original image. If that fails too, we
619 * are in serious trouble.
f8882568
JS
620 */
621 if (ret != 0) {
52f9a172
JS
622 ret = bdrv_open(bs, old_filename, flags, old_drv);
623 if (ret != 0) {
624 qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
625 } else {
626 qerror_report(QERR_OPEN_FILE_FAILED, filename);
627 }
f8882568
JS
628 }
629out:
630 if (ret) {
631 ret = -1;
632 }
633
634 return ret;
635}
636
666daa68
MA
637static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
638{
ea8f942f
MA
639 if (!bdrv_is_removable(bs)) {
640 qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
641 return -1;
642 }
643 if (!force && bdrv_is_locked(bs)) {
644 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
645 return -1;
666daa68 646 }
3b5276b5 647 bdrv_close(bs);
666daa68
MA
648 return 0;
649}
650
651int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
652{
653 BlockDriverState *bs;
eb159d13 654 int force = qdict_get_try_bool(qdict, "force", 0);
666daa68
MA
655 const char *filename = qdict_get_str(qdict, "device");
656
657 bs = bdrv_find(filename);
658 if (!bs) {
659 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
660 return -1;
661 }
662 return eject_device(mon, bs, force);
663}
664
665int do_block_set_passwd(Monitor *mon, const QDict *qdict,
666 QObject **ret_data)
667{
668 BlockDriverState *bs;
669 int err;
670
671 bs = bdrv_find(qdict_get_str(qdict, "device"));
672 if (!bs) {
673 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
674 return -1;
675 }
676
677 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
678 if (err == -EINVAL) {
679 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
680 return -1;
681 } else if (err < 0) {
682 qerror_report(QERR_INVALID_PASSWORD);
683 return -1;
684 }
685
686 return 0;
687}
688
689int do_change_block(Monitor *mon, const char *device,
690 const char *filename, const char *fmt)
691{
692 BlockDriverState *bs;
693 BlockDriver *drv = NULL;
694 int bdrv_flags;
695
696 bs = bdrv_find(device);
697 if (!bs) {
698 qerror_report(QERR_DEVICE_NOT_FOUND, device);
699 return -1;
700 }
701 if (fmt) {
702 drv = bdrv_find_whitelisted_format(fmt);
703 if (!drv) {
704 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
705 return -1;
706 }
707 }
708 if (eject_device(mon, bs, 0) < 0) {
709 return -1;
710 }
528f7663 711 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
199630b6 712 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
666daa68
MA
713 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
714 qerror_report(QERR_OPEN_FILE_FAILED, filename);
715 return -1;
716 }
717 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
718}
9063f814
RH
719
720int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
721{
722 const char *id = qdict_get_str(qdict, "id");
723 BlockDriverState *bs;
9063f814
RH
724
725 bs = bdrv_find(id);
726 if (!bs) {
727 qerror_report(QERR_DEVICE_NOT_FOUND, id);
728 return -1;
729 }
8591675f
MT
730 if (bdrv_in_use(bs)) {
731 qerror_report(QERR_DEVICE_IN_USE, id);
732 return -1;
733 }
9063f814
RH
734
735 /* quiesce block driver; prevent further io */
736 qemu_aio_flush();
737 bdrv_flush(bs);
738 bdrv_close(bs);
739
fa879d62 740 /* if we have a device attached to this BlockDriverState
d22b2f41
RH
741 * then we need to make the drive anonymous until the device
742 * can be removed. If this is a drive with no device backing
743 * then we can just get rid of the block driver state right here.
744 */
fa879d62 745 if (bdrv_get_attached_dev(bs)) {
d22b2f41
RH
746 bdrv_make_anon(bs);
747 } else {
748 drive_uninit(drive_get_by_blockdev(bs));
9063f814
RH
749 }
750
9063f814
RH
751 return 0;
752}
6d4a2b3a
CH
753
754/*
755 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
756 * existing QERR_ macro mess is cleaned up. A good example for better
757 * error reports can be found in the qemu-img resize code.
758 */
759int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
760{
761 const char *device = qdict_get_str(qdict, "device");
762 int64_t size = qdict_get_int(qdict, "size");
763 BlockDriverState *bs;
764
765 bs = bdrv_find(device);
766 if (!bs) {
767 qerror_report(QERR_DEVICE_NOT_FOUND, device);
768 return -1;
769 }
770
771 if (size < 0) {
772 qerror_report(QERR_UNDEFINED_ERROR);
773 return -1;
774 }
775
776 if (bdrv_truncate(bs, size)) {
777 qerror_report(QERR_UNDEFINED_ERROR);
778 return -1;
779 }
780
781 return 0;
782}