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