]> git.proxmox.com Git - mirror_qemu.git/blame - blockdev.c
blockdev: Keep a copy of DriveInfo.serial
[mirror_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
9c17d615 10#include "sysemu/blockdev.h"
2b584959 11#include "hw/block-common.h"
737e150e 12#include "block/blockjob.h"
83c9089e 13#include "monitor/monitor.h"
7b1b5d19 14#include "qapi/qmp/qerror.h"
1de7afc9
PB
15#include "qemu/option.h"
16#include "qemu/config-file.h"
7b1b5d19 17#include "qapi/qmp/types.h"
9c17d615 18#include "sysemu/sysemu.h"
737e150e 19#include "block/block_int.h"
a4dea8a9 20#include "qmp-commands.h"
12bd451f 21#include "trace.h"
9c17d615 22#include "sysemu/arch_init.h"
666daa68 23
c9b62a7e 24static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
666daa68 25
1960966d
MA
26static const char *const if_name[IF_COUNT] = {
27 [IF_NONE] = "none",
28 [IF_IDE] = "ide",
29 [IF_SCSI] = "scsi",
30 [IF_FLOPPY] = "floppy",
31 [IF_PFLASH] = "pflash",
32 [IF_MTD] = "mtd",
33 [IF_SD] = "sd",
34 [IF_VIRTIO] = "virtio",
35 [IF_XEN] = "xen",
36};
37
38static const int if_max_devs[IF_COUNT] = {
27d6bf40
MA
39 /*
40 * Do not change these numbers! They govern how drive option
41 * index maps to unit and bus. That mapping is ABI.
42 *
43 * All controllers used to imlement if=T drives need to support
44 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
45 * Otherwise, some index values map to "impossible" bus, unit
46 * values.
47 *
48 * For instance, if you change [IF_SCSI] to 255, -drive
49 * if=scsi,index=12 no longer means bus=1,unit=5, but
50 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
51 * the drive can't be set up. Regression.
52 */
53 [IF_IDE] = 2,
54 [IF_SCSI] = 7,
1960966d
MA
55};
56
14bafc54
MA
57/*
58 * We automatically delete the drive when a device using it gets
59 * unplugged. Questionable feature, but we can't just drop it.
60 * Device models call blockdev_mark_auto_del() to schedule the
61 * automatic deletion, and generic qdev code calls blockdev_auto_del()
62 * when deletion is actually safe.
63 */
64void blockdev_mark_auto_del(BlockDriverState *bs)
65{
66 DriveInfo *dinfo = drive_get_by_blockdev(bs);
67
12bde0ee
PB
68 if (bs->job) {
69 block_job_cancel(bs->job);
70 }
0fc0f1fa
RH
71 if (dinfo) {
72 dinfo->auto_del = 1;
73 }
14bafc54
MA
74}
75
76void blockdev_auto_del(BlockDriverState *bs)
77{
78 DriveInfo *dinfo = drive_get_by_blockdev(bs);
79
0fc0f1fa 80 if (dinfo && dinfo->auto_del) {
84fb3925 81 drive_put_ref(dinfo);
14bafc54
MA
82 }
83}
84
505a7fb1
MA
85static int drive_index_to_bus_id(BlockInterfaceType type, int index)
86{
87 int max_devs = if_max_devs[type];
88 return max_devs ? index / max_devs : 0;
89}
90
91static int drive_index_to_unit_id(BlockInterfaceType type, int index)
92{
93 int max_devs = if_max_devs[type];
94 return max_devs ? index % max_devs : index;
95}
96
2292ddae
MA
97QemuOpts *drive_def(const char *optstr)
98{
99 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
100}
101
102QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
5645b0f4 103 const char *optstr)
666daa68 104{
666daa68 105 QemuOpts *opts;
2292ddae 106 char buf[32];
666daa68 107
2292ddae 108 opts = drive_def(optstr);
666daa68
MA
109 if (!opts) {
110 return NULL;
111 }
2292ddae
MA
112 if (type != IF_DEFAULT) {
113 qemu_opt_set(opts, "if", if_name[type]);
114 }
115 if (index >= 0) {
116 snprintf(buf, sizeof(buf), "%d", index);
117 qemu_opt_set(opts, "index", buf);
118 }
666daa68
MA
119 if (file)
120 qemu_opt_set(opts, "file", file);
121 return opts;
122}
123
124DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
125{
126 DriveInfo *dinfo;
127
128 /* seek interface, bus and unit */
129
130 QTAILQ_FOREACH(dinfo, &drives, next) {
131 if (dinfo->type == type &&
132 dinfo->bus == bus &&
133 dinfo->unit == unit)
134 return dinfo;
135 }
136
137 return NULL;
138}
139
f1bd51ac
MA
140DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
141{
142 return drive_get(type,
143 drive_index_to_bus_id(type, index),
144 drive_index_to_unit_id(type, index));
145}
146
666daa68
MA
147int drive_get_max_bus(BlockInterfaceType type)
148{
149 int max_bus;
150 DriveInfo *dinfo;
151
152 max_bus = -1;
153 QTAILQ_FOREACH(dinfo, &drives, next) {
154 if(dinfo->type == type &&
155 dinfo->bus > max_bus)
156 max_bus = dinfo->bus;
157 }
158 return max_bus;
159}
160
13839974
MA
161/* Get a block device. This should only be used for single-drive devices
162 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
163 appropriate bus. */
164DriveInfo *drive_get_next(BlockInterfaceType type)
165{
166 static int next_block_unit[IF_COUNT];
167
168 return drive_get(type, 0, next_block_unit[type]++);
169}
170
e4700e59 171DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
666daa68
MA
172{
173 DriveInfo *dinfo;
174
175 QTAILQ_FOREACH(dinfo, &drives, next) {
e4700e59
MA
176 if (dinfo->bdrv == bs) {
177 return dinfo;
178 }
666daa68 179 }
e4700e59 180 return NULL;
666daa68
MA
181}
182
666daa68
MA
183static void bdrv_format_print(void *opaque, const char *name)
184{
807105a7 185 error_printf(" %s", name);
666daa68
MA
186}
187
84fb3925 188static void drive_uninit(DriveInfo *dinfo)
666daa68
MA
189{
190 qemu_opts_del(dinfo->opts);
191 bdrv_delete(dinfo->bdrv);
7267c094 192 g_free(dinfo->id);
666daa68 193 QTAILQ_REMOVE(&drives, dinfo, next);
bb44619b 194 g_free(dinfo->serial);
7267c094 195 g_free(dinfo);
666daa68
MA
196}
197
84fb3925
MT
198void drive_put_ref(DriveInfo *dinfo)
199{
200 assert(dinfo->refcount);
201 if (--dinfo->refcount == 0) {
202 drive_uninit(dinfo);
203 }
204}
205
206void drive_get_ref(DriveInfo *dinfo)
207{
208 dinfo->refcount++;
209}
210
aa398a5c
SH
211typedef struct {
212 QEMUBH *bh;
213 DriveInfo *dinfo;
214} DrivePutRefBH;
215
216static void drive_put_ref_bh(void *opaque)
217{
218 DrivePutRefBH *s = opaque;
219
220 drive_put_ref(s->dinfo);
221 qemu_bh_delete(s->bh);
222 g_free(s);
223}
224
225/*
226 * Release a drive reference in a BH
227 *
228 * It is not possible to use drive_put_ref() from a callback function when the
229 * callers still need the drive. In such cases we schedule a BH to release the
230 * reference.
231 */
232static void drive_put_ref_bh_schedule(DriveInfo *dinfo)
233{
234 DrivePutRefBH *s;
235
236 s = g_new(DrivePutRefBH, 1);
237 s->bh = qemu_bh_new(drive_put_ref_bh, s);
238 s->dinfo = dinfo;
239 qemu_bh_schedule(s->bh);
240}
241
1ceee0d5 242static int parse_block_error_action(const char *buf, bool is_read)
666daa68
MA
243{
244 if (!strcmp(buf, "ignore")) {
92aa5c6d 245 return BLOCKDEV_ON_ERROR_IGNORE;
666daa68 246 } else if (!is_read && !strcmp(buf, "enospc")) {
92aa5c6d 247 return BLOCKDEV_ON_ERROR_ENOSPC;
666daa68 248 } else if (!strcmp(buf, "stop")) {
92aa5c6d 249 return BLOCKDEV_ON_ERROR_STOP;
666daa68 250 } else if (!strcmp(buf, "report")) {
92aa5c6d 251 return BLOCKDEV_ON_ERROR_REPORT;
666daa68 252 } else {
807105a7
MA
253 error_report("'%s' invalid %s error action",
254 buf, is_read ? "read" : "write");
666daa68
MA
255 return -1;
256 }
257}
258
c546194f 259static bool do_check_io_limits(BlockIOLimit *io_limits, Error **errp)
0563e191
ZYW
260{
261 bool bps_flag;
262 bool iops_flag;
263
264 assert(io_limits);
265
266 bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
267 && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
268 || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
269 iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
270 && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
271 || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
272 if (bps_flag || iops_flag) {
c546194f
SH
273 error_setg(errp, "bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
274 "cannot be used at the same time");
0563e191
ZYW
275 return false;
276 }
277
7d81c141
SH
278 if (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] < 0 ||
279 io_limits->bps[BLOCK_IO_LIMIT_WRITE] < 0 ||
280 io_limits->bps[BLOCK_IO_LIMIT_READ] < 0 ||
281 io_limits->iops[BLOCK_IO_LIMIT_TOTAL] < 0 ||
282 io_limits->iops[BLOCK_IO_LIMIT_WRITE] < 0 ||
283 io_limits->iops[BLOCK_IO_LIMIT_READ] < 0) {
284 error_setg(errp, "bps and iops values must be 0 or greater");
285 return false;
286 }
287
0563e191
ZYW
288 return true;
289}
290
2d0d2837 291DriveInfo *drive_init(QemuOpts *opts, BlockInterfaceType block_default_type)
666daa68
MA
292{
293 const char *buf;
294 const char *file = NULL;
666daa68
MA
295 const char *serial;
296 const char *mediastr = "";
297 BlockInterfaceType type;
298 enum { MEDIA_DISK, MEDIA_CDROM } media;
299 int bus_id, unit_id;
300 int cyls, heads, secs, translation;
301 BlockDriver *drv = NULL;
302 int max_devs;
303 int index;
304 int ro = 0;
305 int bdrv_flags = 0;
306 int on_read_error, on_write_error;
307 const char *devaddr;
308 DriveInfo *dinfo;
0563e191 309 BlockIOLimit io_limits;
666daa68 310 int snapshot = 0;
fb0490f6 311 bool copy_on_read;
666daa68 312 int ret;
c546194f 313 Error *error = NULL;
666daa68 314
666daa68 315 translation = BIOS_ATA_TRANSLATION_AUTO;
666daa68
MA
316 media = MEDIA_DISK;
317
318 /* extract parameters */
319 bus_id = qemu_opt_get_number(opts, "bus", 0);
320 unit_id = qemu_opt_get_number(opts, "unit", -1);
321 index = qemu_opt_get_number(opts, "index", -1);
322
323 cyls = qemu_opt_get_number(opts, "cyls", 0);
324 heads = qemu_opt_get_number(opts, "heads", 0);
325 secs = qemu_opt_get_number(opts, "secs", 0);
326
327 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
328 ro = qemu_opt_get_bool(opts, "readonly", 0);
fb0490f6 329 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
666daa68
MA
330
331 file = qemu_opt_get(opts, "file");
332 serial = qemu_opt_get(opts, "serial");
333
334 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
1960966d
MA
335 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
336 ;
337 if (type == IF_COUNT) {
807105a7 338 error_report("unsupported bus type '%s'", buf);
666daa68
MA
339 return NULL;
340 }
2d3999fe 341 } else {
2d0d2837 342 type = block_default_type;
666daa68 343 }
2d3999fe 344
1960966d 345 max_devs = if_max_devs[type];
666daa68
MA
346
347 if (cyls || heads || secs) {
aaea3f36 348 if (cyls < 1) {
807105a7 349 error_report("invalid physical cyls number");
666daa68
MA
350 return NULL;
351 }
aaea3f36 352 if (heads < 1) {
807105a7 353 error_report("invalid physical heads number");
666daa68
MA
354 return NULL;
355 }
aaea3f36 356 if (secs < 1) {
807105a7 357 error_report("invalid physical secs number");
666daa68
MA
358 return NULL;
359 }
360 }
361
362 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
363 if (!cyls) {
e4080f9b 364 error_report("'%s' trans must be used with cyls, heads and secs",
807105a7 365 buf);
666daa68
MA
366 return NULL;
367 }
368 if (!strcmp(buf, "none"))
369 translation = BIOS_ATA_TRANSLATION_NONE;
370 else if (!strcmp(buf, "lba"))
371 translation = BIOS_ATA_TRANSLATION_LBA;
372 else if (!strcmp(buf, "auto"))
373 translation = BIOS_ATA_TRANSLATION_AUTO;
374 else {
807105a7 375 error_report("'%s' invalid translation type", buf);
666daa68
MA
376 return NULL;
377 }
378 }
379
380 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
381 if (!strcmp(buf, "disk")) {
382 media = MEDIA_DISK;
383 } else if (!strcmp(buf, "cdrom")) {
384 if (cyls || secs || heads) {
e7ff8f0e 385 error_report("CHS can't be set with media=%s", buf);
666daa68
MA
386 return NULL;
387 }
388 media = MEDIA_CDROM;
389 } else {
807105a7 390 error_report("'%s' invalid media", buf);
666daa68
MA
391 return NULL;
392 }
393 }
394
a9384aff
PB
395 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
396 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
397 error_report("invalid discard option");
398 return NULL;
399 }
400 }
401
1f212b9d 402 bdrv_flags |= BDRV_O_CACHE_WB;
666daa68 403 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
c3993cdc
SH
404 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
405 error_report("invalid cache option");
406 return NULL;
666daa68
MA
407 }
408 }
409
410#ifdef CONFIG_LINUX_AIO
411 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
412 if (!strcmp(buf, "native")) {
413 bdrv_flags |= BDRV_O_NATIVE_AIO;
414 } else if (!strcmp(buf, "threads")) {
415 /* this is the default */
416 } else {
807105a7 417 error_report("invalid aio option");
666daa68
MA
418 return NULL;
419 }
420 }
421#endif
422
423 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
c8057f95
PM
424 if (is_help_option(buf)) {
425 error_printf("Supported formats:");
426 bdrv_iterate_format(bdrv_format_print, NULL);
427 error_printf("\n");
428 return NULL;
666daa68
MA
429 }
430 drv = bdrv_find_whitelisted_format(buf);
431 if (!drv) {
807105a7 432 error_report("'%s' invalid format", buf);
666daa68
MA
433 return NULL;
434 }
435 }
436
0563e191
ZYW
437 /* disk I/O throttling */
438 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
439 qemu_opt_get_number(opts, "bps", 0);
440 io_limits.bps[BLOCK_IO_LIMIT_READ] =
441 qemu_opt_get_number(opts, "bps_rd", 0);
442 io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
443 qemu_opt_get_number(opts, "bps_wr", 0);
444 io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
445 qemu_opt_get_number(opts, "iops", 0);
446 io_limits.iops[BLOCK_IO_LIMIT_READ] =
447 qemu_opt_get_number(opts, "iops_rd", 0);
448 io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
449 qemu_opt_get_number(opts, "iops_wr", 0);
450
c546194f
SH
451 if (!do_check_io_limits(&io_limits, &error)) {
452 error_report("%s", error_get_pretty(error));
453 error_free(error);
0563e191
ZYW
454 return NULL;
455 }
456
0d92d17a
JK
457 if (qemu_opt_get(opts, "boot") != NULL) {
458 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
459 "ignored. Future versions will reject this parameter. Please "
460 "update your scripts.\n");
461 }
462
92aa5c6d 463 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
666daa68
MA
464 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
465 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
807105a7 466 error_report("werror is not supported by this bus type");
666daa68
MA
467 return NULL;
468 }
469
470 on_write_error = parse_block_error_action(buf, 0);
471 if (on_write_error < 0) {
472 return NULL;
473 }
474 }
475
92aa5c6d 476 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
666daa68 477 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
5dba48a8 478 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
807105a7 479 error_report("rerror is not supported by this bus type");
666daa68
MA
480 return NULL;
481 }
482
483 on_read_error = parse_block_error_action(buf, 1);
484 if (on_read_error < 0) {
485 return NULL;
486 }
487 }
488
489 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
490 if (type != IF_VIRTIO) {
807105a7 491 error_report("addr is not supported by this bus type");
666daa68
MA
492 return NULL;
493 }
494 }
495
496 /* compute bus and unit according index */
497
498 if (index != -1) {
499 if (bus_id != 0 || unit_id != -1) {
807105a7 500 error_report("index cannot be used with bus and unit");
666daa68
MA
501 return NULL;
502 }
505a7fb1
MA
503 bus_id = drive_index_to_bus_id(type, index);
504 unit_id = drive_index_to_unit_id(type, index);
666daa68
MA
505 }
506
507 /* if user doesn't specify a unit_id,
508 * try to find the first free
509 */
510
511 if (unit_id == -1) {
512 unit_id = 0;
513 while (drive_get(type, bus_id, unit_id) != NULL) {
514 unit_id++;
515 if (max_devs && unit_id >= max_devs) {
516 unit_id -= max_devs;
517 bus_id++;
518 }
519 }
520 }
521
522 /* check unit id */
523
524 if (max_devs && unit_id >= max_devs) {
807105a7
MA
525 error_report("unit %d too big (max is %d)",
526 unit_id, max_devs - 1);
666daa68
MA
527 return NULL;
528 }
529
530 /*
4e5d9b57 531 * catch multiple definitions
666daa68
MA
532 */
533
534 if (drive_get(type, bus_id, unit_id) != NULL) {
4e5d9b57
MA
535 error_report("drive with bus=%d, unit=%d (index=%d) exists",
536 bus_id, unit_id, index);
666daa68
MA
537 return NULL;
538 }
539
540 /* init */
541
7267c094 542 dinfo = g_malloc0(sizeof(*dinfo));
666daa68 543 if ((buf = qemu_opts_id(opts)) != NULL) {
7267c094 544 dinfo->id = g_strdup(buf);
666daa68
MA
545 } else {
546 /* no id supplied -> create one */
7267c094 547 dinfo->id = g_malloc0(32);
666daa68
MA
548 if (type == IF_IDE || type == IF_SCSI)
549 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
550 if (max_devs)
551 snprintf(dinfo->id, 32, "%s%i%s%i",
79d21d5b 552 if_name[type], bus_id, mediastr, unit_id);
666daa68
MA
553 else
554 snprintf(dinfo->id, 32, "%s%s%i",
79d21d5b 555 if_name[type], mediastr, unit_id);
666daa68
MA
556 }
557 dinfo->bdrv = bdrv_new(dinfo->id);
80dd1aae
KS
558 dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
559 dinfo->bdrv->read_only = ro;
666daa68
MA
560 dinfo->devaddr = devaddr;
561 dinfo->type = type;
562 dinfo->bus = bus_id;
563 dinfo->unit = unit_id;
317bb412
MA
564 dinfo->cyls = cyls;
565 dinfo->heads = heads;
566 dinfo->secs = secs;
567 dinfo->trans = translation;
666daa68 568 dinfo->opts = opts;
84fb3925 569 dinfo->refcount = 1;
bb44619b
KW
570 if (serial != NULL) {
571 dinfo->serial = g_strdup(serial);
572 }
666daa68
MA
573 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
574
abd7f68d
MA
575 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
576
0563e191
ZYW
577 /* disk I/O throttling */
578 bdrv_set_io_limits(dinfo->bdrv, &io_limits);
579
666daa68
MA
580 switch(type) {
581 case IF_IDE:
582 case IF_SCSI:
583 case IF_XEN:
584 case IF_NONE:
2b584959 585 dinfo->media_cd = media == MEDIA_CDROM;
666daa68
MA
586 break;
587 case IF_SD:
666daa68 588 case IF_FLOPPY:
666daa68
MA
589 case IF_PFLASH:
590 case IF_MTD:
591 break;
592 case IF_VIRTIO:
593 /* add virtio block device */
e478b448 594 opts = qemu_opts_create_nofail(qemu_find_opts("device"));
eeb9c1b5
AL
595 if (arch_type == QEMU_ARCH_S390X) {
596 qemu_opt_set(opts, "driver", "virtio-blk-s390");
597 } else {
598 qemu_opt_set(opts, "driver", "virtio-blk-pci");
599 }
666daa68
MA
600 qemu_opt_set(opts, "drive", dinfo->id);
601 if (devaddr)
602 qemu_opt_set(opts, "addr", devaddr);
603 break;
2292ddae 604 default:
666daa68
MA
605 abort();
606 }
dd5b0d71 607 if (!file || !*file) {
319ae529 608 return dinfo;
666daa68
MA
609 }
610 if (snapshot) {
611 /* always use cache=unsafe with snapshot */
612 bdrv_flags &= ~BDRV_O_CACHE_MASK;
613 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
614 }
615
fb0490f6
SH
616 if (copy_on_read) {
617 bdrv_flags |= BDRV_O_COPY_ON_READ;
618 }
619
ed9d4205
BC
620 if (runstate_check(RUN_STATE_INMIGRATE)) {
621 bdrv_flags |= BDRV_O_INCOMING;
622 }
623
666daa68
MA
624 if (media == MEDIA_CDROM) {
625 /* CDROM is fine for any interface, don't check. */
626 ro = 1;
627 } else if (ro == 1) {
1e9eb78a
JJ
628 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
629 type != IF_NONE && type != IF_PFLASH) {
807105a7 630 error_report("readonly not supported by this bus type");
a9ae2bff 631 goto err;
666daa68
MA
632 }
633 }
634
635 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
636
04d4abe9
SH
637 if (ro && copy_on_read) {
638 error_report("warning: disabling copy_on_read on readonly drive");
639 }
640
de9c0cec 641 ret = bdrv_open(dinfo->bdrv, file, NULL, bdrv_flags, drv);
666daa68 642 if (ret < 0) {
02582abd
SW
643 if (ret == -EMEDIUMTYPE) {
644 error_report("could not open disk image %s: not in %s format",
645 file, drv->format_name);
646 } else {
647 error_report("could not open disk image %s: %s",
648 file, strerror(-ret));
649 }
a9ae2bff 650 goto err;
666daa68
MA
651 }
652
653 if (bdrv_key_required(dinfo->bdrv))
654 autostart = 0;
666daa68 655 return dinfo;
a9ae2bff
MA
656
657err:
658 bdrv_delete(dinfo->bdrv);
7267c094 659 g_free(dinfo->id);
a9ae2bff 660 QTAILQ_REMOVE(&drives, dinfo, next);
7267c094 661 g_free(dinfo);
a9ae2bff 662 return NULL;
666daa68
MA
663}
664
665void do_commit(Monitor *mon, const QDict *qdict)
666{
666daa68 667 const char *device = qdict_get_str(qdict, "device");
6ab4b5ab 668 BlockDriverState *bs;
e8877497 669 int ret;
666daa68 670
6ab4b5ab 671 if (!strcmp(device, "all")) {
e8877497 672 ret = bdrv_commit_all();
6ab4b5ab
MA
673 } else {
674 bs = bdrv_find(device);
ac59eb95 675 if (!bs) {
58513bde 676 monitor_printf(mon, "Device '%s' not found\n", device);
ac59eb95 677 return;
6ab4b5ab 678 }
2d3735d3 679 ret = bdrv_commit(bs);
58513bde
JC
680 }
681 if (ret < 0) {
682 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
683 strerror(-ret));
666daa68
MA
684 }
685}
686
6cc2a415
PB
687static void blockdev_do_action(int kind, void *data, Error **errp)
688{
689 BlockdevAction action;
690 BlockdevActionList list;
691
692 action.kind = kind;
693 action.data = data;
694 list.value = &action;
695 list.next = NULL;
696 qmp_transaction(&list, errp);
697}
698
6106e249
LC
699void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
700 bool has_format, const char *format,
6cc2a415 701 bool has_mode, enum NewImageMode mode,
6106e249 702 Error **errp)
f8882568 703{
6cc2a415
PB
704 BlockdevSnapshot snapshot = {
705 .device = (char *) device,
706 .snapshot_file = (char *) snapshot_file,
707 .has_format = has_format,
708 .format = (char *) format,
709 .has_mode = has_mode,
710 .mode = mode,
711 };
712 blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot,
713 errp);
f8882568
JS
714}
715
8802d1fd
JC
716
717/* New and old BlockDriverState structs for group snapshots */
52e7c241 718typedef struct BlkTransactionStates {
8802d1fd
JC
719 BlockDriverState *old_bs;
720 BlockDriverState *new_bs;
52e7c241
PB
721 QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
722} BlkTransactionStates;
8802d1fd
JC
723
724/*
725 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
726 * then we do not pivot any of the devices in the group, and abandon the
727 * snapshots
728 */
52e7c241 729void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
8802d1fd
JC
730{
731 int ret = 0;
52e7c241
PB
732 BlockdevActionList *dev_entry = dev_list;
733 BlkTransactionStates *states, *next;
43e17041 734 Error *local_err = NULL;
8802d1fd 735
52e7c241 736 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
8802d1fd
JC
737 QSIMPLEQ_INIT(&snap_bdrv_states);
738
739 /* drain all i/o before any snapshots */
740 bdrv_drain_all();
741
742 /* We don't do anything in this loop that commits us to the snapshot */
743 while (NULL != dev_entry) {
52e7c241
PB
744 BlockdevAction *dev_info = NULL;
745 BlockDriver *proto_drv;
746 BlockDriver *drv;
747 int flags;
bc8b094f
PB
748 enum NewImageMode mode;
749 const char *new_image_file;
52e7c241
PB
750 const char *device;
751 const char *format = "qcow2";
52e7c241 752
8802d1fd
JC
753 dev_info = dev_entry->value;
754 dev_entry = dev_entry->next;
755
52e7c241 756 states = g_malloc0(sizeof(BlkTransactionStates));
8802d1fd
JC
757 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
758
52e7c241
PB
759 switch (dev_info->kind) {
760 case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
761 device = dev_info->blockdev_snapshot_sync->device;
bc8b094f
PB
762 if (!dev_info->blockdev_snapshot_sync->has_mode) {
763 dev_info->blockdev_snapshot_sync->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
764 }
765 new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file;
52e7c241
PB
766 if (dev_info->blockdev_snapshot_sync->has_format) {
767 format = dev_info->blockdev_snapshot_sync->format;
768 }
bc8b094f 769 mode = dev_info->blockdev_snapshot_sync->mode;
52e7c241
PB
770 break;
771 default:
772 abort();
773 }
8802d1fd 774
52e7c241
PB
775 drv = bdrv_find_format(format);
776 if (!drv) {
777 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
778 goto delete_and_fail;
779 }
780
781 states->old_bs = bdrv_find(device);
8802d1fd 782 if (!states->old_bs) {
52e7c241 783 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
8802d1fd
JC
784 goto delete_and_fail;
785 }
786
e86fe18a
PB
787 if (!bdrv_is_inserted(states->old_bs)) {
788 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
789 goto delete_and_fail;
790 }
791
8802d1fd 792 if (bdrv_in_use(states->old_bs)) {
52e7c241 793 error_set(errp, QERR_DEVICE_IN_USE, device);
8802d1fd
JC
794 goto delete_and_fail;
795 }
796
e86fe18a 797 if (!bdrv_is_read_only(states->old_bs)) {
8802d1fd
JC
798 if (bdrv_flush(states->old_bs)) {
799 error_set(errp, QERR_IO_ERROR);
800 goto delete_and_fail;
801 }
802 }
803
8802d1fd
JC
804 flags = states->old_bs->open_flags;
805
52e7c241 806 proto_drv = bdrv_find_protocol(new_image_file);
8802d1fd
JC
807 if (!proto_drv) {
808 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
809 goto delete_and_fail;
810 }
811
812 /* create new image w/backing file */
bc8b094f 813 if (mode != NEW_IMAGE_MODE_EXISTING) {
43e17041
LC
814 bdrv_img_create(new_image_file, format,
815 states->old_bs->filename,
816 states->old_bs->drv->format_name,
f382d43a 817 NULL, -1, flags, &local_err, false);
43e17041
LC
818 if (error_is_set(&local_err)) {
819 error_propagate(errp, local_err);
bc8b094f
PB
820 goto delete_and_fail;
821 }
8802d1fd
JC
822 }
823
824 /* We will manually add the backing_hd field to the bs later */
825 states->new_bs = bdrv_new("");
de9c0cec
KW
826 /* TODO Inherit bs->options or only take explicit options with an
827 * extended QMP command? */
828 ret = bdrv_open(states->new_bs, new_image_file, NULL,
8802d1fd
JC
829 flags | BDRV_O_NO_BACKING, drv);
830 if (ret != 0) {
52e7c241 831 error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
8802d1fd
JC
832 goto delete_and_fail;
833 }
834 }
835
836
837 /* Now we are going to do the actual pivot. Everything up to this point
838 * is reversible, but we are committed at this point */
839 QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
840 /* This removes our old bs from the bdrv_states, and adds the new bs */
841 bdrv_append(states->new_bs, states->old_bs);
870f5681
JC
842 /* We don't need (or want) to use the transactional
843 * bdrv_reopen_multiple() across all the entries at once, because we
844 * don't want to abort all of them if one of them fails the reopen */
845 bdrv_reopen(states->new_bs, states->new_bs->open_flags & ~BDRV_O_RDWR,
846 NULL);
8802d1fd
JC
847 }
848
849 /* success */
850 goto exit;
851
852delete_and_fail:
853 /*
854 * failure, and it is all-or-none; abandon each new bs, and keep using
855 * the original bs for all images
856 */
857 QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
858 if (states->new_bs) {
859 bdrv_delete(states->new_bs);
860 }
861 }
862exit:
622d2419 863 QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
8802d1fd
JC
864 g_free(states);
865 }
8802d1fd
JC
866}
867
868
92d48558 869static void eject_device(BlockDriverState *bs, int force, Error **errp)
666daa68 870{
2d3735d3
SH
871 if (bdrv_in_use(bs)) {
872 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
873 return;
874 }
2c6942fa 875 if (!bdrv_dev_has_removable_media(bs)) {
92d48558
LC
876 error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
877 return;
ea8f942f 878 }
92d48558 879
025ccaa7
PB
880 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
881 bdrv_dev_eject_request(bs, force);
882 if (!force) {
92d48558
LC
883 error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
884 return;
025ccaa7 885 }
666daa68 886 }
92d48558 887
3b5276b5 888 bdrv_close(bs);
666daa68
MA
889}
890
c245b6a3 891void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
666daa68
MA
892{
893 BlockDriverState *bs;
666daa68 894
c245b6a3 895 bs = bdrv_find(device);
666daa68 896 if (!bs) {
c245b6a3
LC
897 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
898 return;
92d48558
LC
899 }
900
c245b6a3 901 eject_device(bs, force, errp);
666daa68
MA
902}
903
a4dea8a9 904void qmp_block_passwd(const char *device, const char *password, Error **errp)
666daa68
MA
905{
906 BlockDriverState *bs;
907 int err;
908
a4dea8a9 909 bs = bdrv_find(device);
666daa68 910 if (!bs) {
a4dea8a9
LC
911 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
912 return;
666daa68
MA
913 }
914
a4dea8a9 915 err = bdrv_set_key(bs, password);
666daa68 916 if (err == -EINVAL) {
a4dea8a9
LC
917 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
918 return;
666daa68 919 } else if (err < 0) {
a4dea8a9
LC
920 error_set(errp, QERR_INVALID_PASSWORD);
921 return;
666daa68 922 }
666daa68
MA
923}
924
333a96ec
LC
925static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
926 int bdrv_flags, BlockDriver *drv,
927 const char *password, Error **errp)
928{
de9c0cec 929 if (bdrv_open(bs, filename, NULL, bdrv_flags, drv) < 0) {
333a96ec
LC
930 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
931 return;
932 }
933
934 if (bdrv_key_required(bs)) {
935 if (password) {
936 if (bdrv_set_key(bs, password) < 0) {
937 error_set(errp, QERR_INVALID_PASSWORD);
938 }
939 } else {
940 error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
941 bdrv_get_encrypted_filename(bs));
942 }
943 } else if (password) {
944 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
945 }
946}
947
948void qmp_change_blockdev(const char *device, const char *filename,
949 bool has_format, const char *format, Error **errp)
666daa68
MA
950{
951 BlockDriverState *bs;
952 BlockDriver *drv = NULL;
953 int bdrv_flags;
92d48558 954 Error *err = NULL;
666daa68
MA
955
956 bs = bdrv_find(device);
957 if (!bs) {
333a96ec
LC
958 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
959 return;
666daa68 960 }
333a96ec
LC
961
962 if (format) {
963 drv = bdrv_find_whitelisted_format(format);
666daa68 964 if (!drv) {
333a96ec
LC
965 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
966 return;
666daa68
MA
967 }
968 }
333a96ec 969
92d48558
LC
970 eject_device(bs, 0, &err);
971 if (error_is_set(&err)) {
333a96ec
LC
972 error_propagate(errp, err);
973 return;
666daa68 974 }
333a96ec 975
528f7663 976 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
199630b6 977 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
333a96ec
LC
978
979 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
666daa68 980}
9063f814 981
727f005e 982/* throttling disk I/O limits */
80047da5
LC
983void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
984 int64_t bps_wr, int64_t iops, int64_t iops_rd,
985 int64_t iops_wr, Error **errp)
727f005e
ZYW
986{
987 BlockIOLimit io_limits;
727f005e
ZYW
988 BlockDriverState *bs;
989
80047da5 990 bs = bdrv_find(device);
727f005e 991 if (!bs) {
80047da5
LC
992 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
993 return;
727f005e
ZYW
994 }
995
80047da5
LC
996 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps;
997 io_limits.bps[BLOCK_IO_LIMIT_READ] = bps_rd;
998 io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr;
999 io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops;
1000 io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd;
1001 io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr;
727f005e 1002
c546194f 1003 if (!do_check_io_limits(&io_limits, errp)) {
80047da5 1004 return;
727f005e
ZYW
1005 }
1006
1007 bs->io_limits = io_limits;
1008 bs->slice_time = BLOCK_IO_SLICE_TIME;
1009
1010 if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
1011 bdrv_io_limits_enable(bs);
1012 } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
1013 bdrv_io_limits_disable(bs);
1014 } else {
1015 if (bs->block_timer) {
1016 qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
1017 }
1018 }
727f005e
ZYW
1019}
1020
9063f814
RH
1021int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1022{
1023 const char *id = qdict_get_str(qdict, "id");
1024 BlockDriverState *bs;
9063f814
RH
1025
1026 bs = bdrv_find(id);
1027 if (!bs) {
1028 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1029 return -1;
1030 }
8591675f
MT
1031 if (bdrv_in_use(bs)) {
1032 qerror_report(QERR_DEVICE_IN_USE, id);
1033 return -1;
1034 }
9063f814
RH
1035
1036 /* quiesce block driver; prevent further io */
922453bc 1037 bdrv_drain_all();
9063f814
RH
1038 bdrv_flush(bs);
1039 bdrv_close(bs);
1040
fa879d62 1041 /* if we have a device attached to this BlockDriverState
d22b2f41
RH
1042 * then we need to make the drive anonymous until the device
1043 * can be removed. If this is a drive with no device backing
1044 * then we can just get rid of the block driver state right here.
1045 */
fa879d62 1046 if (bdrv_get_attached_dev(bs)) {
d22b2f41
RH
1047 bdrv_make_anon(bs);
1048 } else {
1049 drive_uninit(drive_get_by_blockdev(bs));
9063f814
RH
1050 }
1051
9063f814
RH
1052 return 0;
1053}
6d4a2b3a 1054
5e7caacb 1055void qmp_block_resize(const char *device, int64_t size, Error **errp)
6d4a2b3a 1056{
6d4a2b3a
CH
1057 BlockDriverState *bs;
1058
1059 bs = bdrv_find(device);
1060 if (!bs) {
5e7caacb
LC
1061 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1062 return;
6d4a2b3a
CH
1063 }
1064
1065 if (size < 0) {
939a1cc3 1066 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
5e7caacb 1067 return;
6d4a2b3a
CH
1068 }
1069
939a1cc3
SH
1070 switch (bdrv_truncate(bs, size)) {
1071 case 0:
1072 break;
1073 case -ENOMEDIUM:
1074 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1075 break;
1076 case -ENOTSUP:
1077 error_set(errp, QERR_UNSUPPORTED);
1078 break;
1079 case -EACCES:
1080 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1081 break;
1082 case -EBUSY:
1083 error_set(errp, QERR_DEVICE_IN_USE, device);
1084 break;
1085 default:
5e7caacb 1086 error_set(errp, QERR_UNDEFINED_ERROR);
939a1cc3 1087 break;
6d4a2b3a 1088 }
6d4a2b3a 1089}
12bd451f 1090
9abf2dba 1091static void block_job_cb(void *opaque, int ret)
12bd451f
SH
1092{
1093 BlockDriverState *bs = opaque;
1094 QObject *obj;
1095
9abf2dba 1096 trace_block_job_cb(bs, bs->job, ret);
12bd451f
SH
1097
1098 assert(bs->job);
1099 obj = qobject_from_block_job(bs->job);
1100 if (ret < 0) {
1101 QDict *dict = qobject_to_qdict(obj);
1102 qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
1103 }
1104
370521a1
SH
1105 if (block_job_is_cancelled(bs->job)) {
1106 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
1107 } else {
1108 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
1109 }
12bd451f 1110 qobject_decref(obj);
aa398a5c
SH
1111
1112 drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
12bd451f
SH
1113}
1114
1115void qmp_block_stream(const char *device, bool has_base,
1d809098
PB
1116 const char *base, bool has_speed, int64_t speed,
1117 bool has_on_error, BlockdevOnError on_error,
1118 Error **errp)
12bd451f
SH
1119{
1120 BlockDriverState *bs;
c8c3080f 1121 BlockDriverState *base_bs = NULL;
fd7f8c65 1122 Error *local_err = NULL;
12bd451f 1123
1d809098
PB
1124 if (!has_on_error) {
1125 on_error = BLOCKDEV_ON_ERROR_REPORT;
1126 }
1127
12bd451f
SH
1128 bs = bdrv_find(device);
1129 if (!bs) {
1130 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1131 return;
1132 }
1133
12bd451f 1134 if (base) {
c8c3080f
MT
1135 base_bs = bdrv_find_backing_image(bs, base);
1136 if (base_bs == NULL) {
1137 error_set(errp, QERR_BASE_NOT_FOUND, base);
1138 return;
1139 }
12bd451f
SH
1140 }
1141
c83c66c3 1142 stream_start(bs, base_bs, base, has_speed ? speed : 0,
1d809098 1143 on_error, block_job_cb, bs, &local_err);
fd7f8c65
SH
1144 if (error_is_set(&local_err)) {
1145 error_propagate(errp, local_err);
1146 return;
12bd451f
SH
1147 }
1148
aa398a5c
SH
1149 /* Grab a reference so hotplug does not delete the BlockDriverState from
1150 * underneath us.
1151 */
1152 drive_get_ref(drive_get_by_blockdev(bs));
1153
12bd451f
SH
1154 trace_qmp_block_stream(bs, bs->job);
1155}
2d47c6e9 1156
ed61fc10
JC
1157void qmp_block_commit(const char *device,
1158 bool has_base, const char *base, const char *top,
1159 bool has_speed, int64_t speed,
1160 Error **errp)
1161{
1162 BlockDriverState *bs;
1163 BlockDriverState *base_bs, *top_bs;
1164 Error *local_err = NULL;
1165 /* This will be part of the QMP command, if/when the
1166 * BlockdevOnError change for blkmirror makes it in
1167 */
92aa5c6d 1168 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
ed61fc10
JC
1169
1170 /* drain all i/o before commits */
1171 bdrv_drain_all();
1172
1173 bs = bdrv_find(device);
1174 if (!bs) {
1175 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1176 return;
1177 }
ed61fc10
JC
1178
1179 /* default top_bs is the active layer */
1180 top_bs = bs;
1181
1182 if (top) {
1183 if (strcmp(bs->filename, top) != 0) {
1184 top_bs = bdrv_find_backing_image(bs, top);
1185 }
1186 }
1187
1188 if (top_bs == NULL) {
1189 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
1190 return;
1191 }
1192
d5208c45
JC
1193 if (has_base && base) {
1194 base_bs = bdrv_find_backing_image(top_bs, base);
1195 } else {
1196 base_bs = bdrv_find_base(top_bs);
1197 }
1198
1199 if (base_bs == NULL) {
1200 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
1201 return;
1202 }
1203
ed61fc10
JC
1204 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
1205 &local_err);
1206 if (local_err != NULL) {
1207 error_propagate(errp, local_err);
1208 return;
1209 }
1210 /* Grab a reference so hotplug does not delete the BlockDriverState from
1211 * underneath us.
1212 */
1213 drive_get_ref(drive_get_by_blockdev(bs));
1214}
1215
08e4ed6c
PB
1216#define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1217
d9b902db
PB
1218void qmp_drive_mirror(const char *device, const char *target,
1219 bool has_format, const char *format,
1220 enum MirrorSyncMode sync,
1221 bool has_mode, enum NewImageMode mode,
b952b558 1222 bool has_speed, int64_t speed,
eee13dfe 1223 bool has_granularity, uint32_t granularity,
08e4ed6c 1224 bool has_buf_size, int64_t buf_size,
b952b558
PB
1225 bool has_on_source_error, BlockdevOnError on_source_error,
1226 bool has_on_target_error, BlockdevOnError on_target_error,
1227 Error **errp)
d9b902db 1228{
d9b902db
PB
1229 BlockDriverState *bs;
1230 BlockDriverState *source, *target_bs;
1231 BlockDriver *proto_drv;
1232 BlockDriver *drv = NULL;
1233 Error *local_err = NULL;
1234 int flags;
1235 uint64_t size;
1236 int ret;
1237
1238 if (!has_speed) {
1239 speed = 0;
1240 }
b952b558
PB
1241 if (!has_on_source_error) {
1242 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1243 }
1244 if (!has_on_target_error) {
1245 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1246 }
d9b902db
PB
1247 if (!has_mode) {
1248 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1249 }
eee13dfe
PB
1250 if (!has_granularity) {
1251 granularity = 0;
1252 }
08e4ed6c
PB
1253 if (!has_buf_size) {
1254 buf_size = DEFAULT_MIRROR_BUF_SIZE;
1255 }
1256
eee13dfe
PB
1257 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
1258 error_set(errp, QERR_INVALID_PARAMETER, device);
1259 return;
1260 }
1261 if (granularity & (granularity - 1)) {
1262 error_set(errp, QERR_INVALID_PARAMETER, device);
1263 return;
1264 }
d9b902db
PB
1265
1266 bs = bdrv_find(device);
1267 if (!bs) {
1268 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1269 return;
1270 }
1271
1272 if (!bdrv_is_inserted(bs)) {
1273 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1274 return;
1275 }
1276
1277 if (!has_format) {
1278 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1279 }
1280 if (format) {
1281 drv = bdrv_find_format(format);
1282 if (!drv) {
1283 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1284 return;
1285 }
1286 }
1287
1288 if (bdrv_in_use(bs)) {
1289 error_set(errp, QERR_DEVICE_IN_USE, device);
1290 return;
1291 }
1292
1293 flags = bs->open_flags | BDRV_O_RDWR;
1294 source = bs->backing_hd;
1295 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
1296 sync = MIRROR_SYNC_MODE_FULL;
1297 }
1298
1299 proto_drv = bdrv_find_protocol(target);
1300 if (!proto_drv) {
1301 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1302 return;
1303 }
1304
86899072
VI
1305 bdrv_get_geometry(bs, &size);
1306 size *= 512;
d9b902db
PB
1307 if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
1308 /* create new image w/o backing file */
1309 assert(format && drv);
cf8f2426 1310 bdrv_img_create(target, format,
f382d43a 1311 NULL, NULL, NULL, size, flags, &local_err, false);
d9b902db
PB
1312 } else {
1313 switch (mode) {
1314 case NEW_IMAGE_MODE_EXISTING:
1315 ret = 0;
1316 break;
1317 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
1318 /* create new image with backing file */
cf8f2426
LC
1319 bdrv_img_create(target, format,
1320 source->filename,
1321 source->drv->format_name,
f382d43a 1322 NULL, size, flags, &local_err, false);
d9b902db
PB
1323 break;
1324 default:
1325 abort();
1326 }
1327 }
1328
cf8f2426
LC
1329 if (error_is_set(&local_err)) {
1330 error_propagate(errp, local_err);
d9b902db
PB
1331 return;
1332 }
1333
b812f671
PB
1334 /* Mirroring takes care of copy-on-write using the source's backing
1335 * file.
1336 */
d9b902db 1337 target_bs = bdrv_new("");
de9c0cec 1338 ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv);
d9b902db
PB
1339
1340 if (ret < 0) {
1341 bdrv_delete(target_bs);
1342 error_set(errp, QERR_OPEN_FILE_FAILED, target);
1343 return;
1344 }
1345
08e4ed6c 1346 mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
eee13dfe 1347 on_source_error, on_target_error,
b952b558 1348 block_job_cb, bs, &local_err);
d9b902db
PB
1349 if (local_err != NULL) {
1350 bdrv_delete(target_bs);
1351 error_propagate(errp, local_err);
1352 return;
1353 }
1354
1355 /* Grab a reference so hotplug does not delete the BlockDriverState from
1356 * underneath us.
1357 */
1358 drive_get_ref(drive_get_by_blockdev(bs));
1359}
1360
2d47c6e9
SH
1361static BlockJob *find_block_job(const char *device)
1362{
1363 BlockDriverState *bs;
1364
1365 bs = bdrv_find(device);
1366 if (!bs || !bs->job) {
1367 return NULL;
1368 }
1369 return bs->job;
1370}
1371
882ec7ce 1372void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2d47c6e9
SH
1373{
1374 BlockJob *job = find_block_job(device);
1375
1376 if (!job) {
7ef15070 1377 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2d47c6e9
SH
1378 return;
1379 }
1380
882ec7ce 1381 block_job_set_speed(job, speed, errp);
2d47c6e9 1382}
370521a1 1383
6e37fb81
PB
1384void qmp_block_job_cancel(const char *device,
1385 bool has_force, bool force, Error **errp)
370521a1
SH
1386{
1387 BlockJob *job = find_block_job(device);
1388
6e37fb81
PB
1389 if (!has_force) {
1390 force = false;
1391 }
1392
370521a1 1393 if (!job) {
7ef15070 1394 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
370521a1
SH
1395 return;
1396 }
6e37fb81 1397 if (job->paused && !force) {
8acc72a4
PB
1398 error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
1399 return;
1400 }
370521a1
SH
1401
1402 trace_qmp_block_job_cancel(job);
1403 block_job_cancel(job);
1404}
fb5458cd 1405
6e37fb81
PB
1406void qmp_block_job_pause(const char *device, Error **errp)
1407{
1408 BlockJob *job = find_block_job(device);
1409
1410 if (!job) {
1411 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1412 return;
1413 }
1414
1415 trace_qmp_block_job_pause(job);
1416 block_job_pause(job);
1417}
1418
1419void qmp_block_job_resume(const char *device, Error **errp)
1420{
1421 BlockJob *job = find_block_job(device);
1422
1423 if (!job) {
1424 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1425 return;
1426 }
1427
1428 trace_qmp_block_job_resume(job);
1429 block_job_resume(job);
1430}
1431
aeae883b
PB
1432void qmp_block_job_complete(const char *device, Error **errp)
1433{
1434 BlockJob *job = find_block_job(device);
1435
1436 if (!job) {
1437 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1438 return;
1439 }
1440
1441 trace_qmp_block_job_complete(job);
1442 block_job_complete(job, errp);
1443}
1444
fb5458cd
SH
1445static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
1446{
1447 BlockJobInfoList **prev = opaque;
1448 BlockJob *job = bs->job;
1449
1450 if (job) {
30e628b7
PB
1451 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
1452 elem->value = block_job_query(bs->job);
fb5458cd
SH
1453 (*prev)->next = elem;
1454 *prev = elem;
1455 }
1456}
1457
1458BlockJobInfoList *qmp_query_block_jobs(Error **errp)
1459{
1460 /* Dummy is a fake list element for holding the head pointer */
1461 BlockJobInfoList dummy = {};
1462 BlockJobInfoList *prev = &dummy;
1463 bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
1464 return dummy.next;
1465}
4d454574
PB
1466
1467QemuOptsList qemu_drive_opts = {
1468 .name = "drive",
1469 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
1470 .desc = {
1471 {
1472 .name = "bus",
1473 .type = QEMU_OPT_NUMBER,
1474 .help = "bus number",
1475 },{
1476 .name = "unit",
1477 .type = QEMU_OPT_NUMBER,
1478 .help = "unit number (i.e. lun for scsi)",
1479 },{
1480 .name = "if",
1481 .type = QEMU_OPT_STRING,
1482 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1483 },{
1484 .name = "index",
1485 .type = QEMU_OPT_NUMBER,
1486 .help = "index number",
1487 },{
1488 .name = "cyls",
1489 .type = QEMU_OPT_NUMBER,
1490 .help = "number of cylinders (ide disk geometry)",
1491 },{
1492 .name = "heads",
1493 .type = QEMU_OPT_NUMBER,
1494 .help = "number of heads (ide disk geometry)",
1495 },{
1496 .name = "secs",
1497 .type = QEMU_OPT_NUMBER,
1498 .help = "number of sectors (ide disk geometry)",
1499 },{
1500 .name = "trans",
1501 .type = QEMU_OPT_STRING,
1502 .help = "chs translation (auto, lba. none)",
1503 },{
1504 .name = "media",
1505 .type = QEMU_OPT_STRING,
1506 .help = "media type (disk, cdrom)",
1507 },{
1508 .name = "snapshot",
1509 .type = QEMU_OPT_BOOL,
1510 .help = "enable/disable snapshot mode",
1511 },{
1512 .name = "file",
1513 .type = QEMU_OPT_STRING,
1514 .help = "disk image",
a9384aff
PB
1515 },{
1516 .name = "discard",
1517 .type = QEMU_OPT_STRING,
1518 .help = "discard operation (ignore/off, unmap/on)",
4d454574
PB
1519 },{
1520 .name = "cache",
1521 .type = QEMU_OPT_STRING,
1522 .help = "host cache usage (none, writeback, writethrough, "
1523 "directsync, unsafe)",
1524 },{
1525 .name = "aio",
1526 .type = QEMU_OPT_STRING,
1527 .help = "host AIO implementation (threads, native)",
1528 },{
1529 .name = "format",
1530 .type = QEMU_OPT_STRING,
1531 .help = "disk format (raw, qcow2, ...)",
1532 },{
1533 .name = "serial",
1534 .type = QEMU_OPT_STRING,
1535 .help = "disk serial number",
1536 },{
1537 .name = "rerror",
1538 .type = QEMU_OPT_STRING,
1539 .help = "read error action",
1540 },{
1541 .name = "werror",
1542 .type = QEMU_OPT_STRING,
1543 .help = "write error action",
1544 },{
1545 .name = "addr",
1546 .type = QEMU_OPT_STRING,
1547 .help = "pci address (virtio only)",
1548 },{
1549 .name = "readonly",
1550 .type = QEMU_OPT_BOOL,
1551 .help = "open drive file as read-only",
1552 },{
1553 .name = "iops",
1554 .type = QEMU_OPT_NUMBER,
1555 .help = "limit total I/O operations per second",
1556 },{
1557 .name = "iops_rd",
1558 .type = QEMU_OPT_NUMBER,
1559 .help = "limit read operations per second",
1560 },{
1561 .name = "iops_wr",
1562 .type = QEMU_OPT_NUMBER,
1563 .help = "limit write operations per second",
1564 },{
1565 .name = "bps",
1566 .type = QEMU_OPT_NUMBER,
1567 .help = "limit total bytes per second",
1568 },{
1569 .name = "bps_rd",
1570 .type = QEMU_OPT_NUMBER,
1571 .help = "limit read bytes per second",
1572 },{
1573 .name = "bps_wr",
1574 .type = QEMU_OPT_NUMBER,
1575 .help = "limit write bytes per second",
1576 },{
1577 .name = "copy-on-read",
1578 .type = QEMU_OPT_BOOL,
1579 .help = "copy read data from backing file into image file",
1580 },{
1581 .name = "boot",
1582 .type = QEMU_OPT_BOOL,
1583 .help = "(deprecated, ignored)",
1584 },
1585 { /* end of list */ }
1586 },
1587};