]> git.proxmox.com Git - mirror_qemu.git/blame - blockdev.c
qerror: Eliminate QERR_DEVICE_NOT_FOUND
[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.
3618a094
MA
8 *
9 * This file incorporates work covered by the following copyright and
10 * permission notice:
11 *
12 * Copyright (c) 2003-2008 Fabrice Bellard
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 * THE SOFTWARE.
666daa68
MA
31 */
32
26f54e9a 33#include "sysemu/block-backend.h"
9c17d615 34#include "sysemu/blockdev.h"
0d09e41a 35#include "hw/block/block.h"
737e150e 36#include "block/blockjob.h"
76f4afb4 37#include "block/throttle-groups.h"
83c9089e 38#include "monitor/monitor.h"
1de7afc9
PB
39#include "qemu/option.h"
40#include "qemu/config-file.h"
7b1b5d19 41#include "qapi/qmp/types.h"
d26c9a15
KW
42#include "qapi-visit.h"
43#include "qapi/qmp-output-visitor.h"
9e7dac7c 44#include "qapi/util.h"
9c17d615 45#include "sysemu/sysemu.h"
737e150e 46#include "block/block_int.h"
a4dea8a9 47#include "qmp-commands.h"
12bd451f 48#include "trace.h"
9c17d615 49#include "sysemu/arch_init.h"
666daa68 50
1960966d
MA
51static const char *const if_name[IF_COUNT] = {
52 [IF_NONE] = "none",
53 [IF_IDE] = "ide",
54 [IF_SCSI] = "scsi",
55 [IF_FLOPPY] = "floppy",
56 [IF_PFLASH] = "pflash",
57 [IF_MTD] = "mtd",
58 [IF_SD] = "sd",
59 [IF_VIRTIO] = "virtio",
60 [IF_XEN] = "xen",
61};
62
21dff8cf 63static int if_max_devs[IF_COUNT] = {
27d6bf40
MA
64 /*
65 * Do not change these numbers! They govern how drive option
66 * index maps to unit and bus. That mapping is ABI.
67 *
68 * All controllers used to imlement if=T drives need to support
69 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
70 * Otherwise, some index values map to "impossible" bus, unit
71 * values.
72 *
73 * For instance, if you change [IF_SCSI] to 255, -drive
74 * if=scsi,index=12 no longer means bus=1,unit=5, but
75 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
76 * the drive can't be set up. Regression.
77 */
78 [IF_IDE] = 2,
79 [IF_SCSI] = 7,
1960966d
MA
80};
81
21dff8cf
JS
82/**
83 * Boards may call this to offer board-by-board overrides
84 * of the default, global values.
85 */
86void override_max_devs(BlockInterfaceType type, int max_devs)
87{
18e46a03 88 BlockBackend *blk;
21dff8cf
JS
89 DriveInfo *dinfo;
90
91 if (max_devs <= 0) {
92 return;
93 }
94
18e46a03
MA
95 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
96 dinfo = blk_legacy_dinfo(blk);
21dff8cf
JS
97 if (dinfo->type == type) {
98 fprintf(stderr, "Cannot override units-per-bus property of"
99 " the %s interface, because a drive of that type has"
100 " already been added.\n", if_name[type]);
101 g_assert_not_reached();
102 }
103 }
104
105 if_max_devs[type] = max_devs;
106}
107
14bafc54
MA
108/*
109 * We automatically delete the drive when a device using it gets
110 * unplugged. Questionable feature, but we can't just drop it.
111 * Device models call blockdev_mark_auto_del() to schedule the
112 * automatic deletion, and generic qdev code calls blockdev_auto_del()
113 * when deletion is actually safe.
114 */
4be74634 115void blockdev_mark_auto_del(BlockBackend *blk)
14bafc54 116{
18e46a03 117 DriveInfo *dinfo = blk_legacy_dinfo(blk);
4be74634 118 BlockDriverState *bs = blk_bs(blk);
91fddb0d 119 AioContext *aio_context;
14bafc54 120
26f8b3a8 121 if (!dinfo) {
2d246f01
KW
122 return;
123 }
124
91fddb0d
SH
125 aio_context = bdrv_get_aio_context(bs);
126 aio_context_acquire(aio_context);
127
12bde0ee
PB
128 if (bs->job) {
129 block_job_cancel(bs->job);
130 }
91fddb0d
SH
131
132 aio_context_release(aio_context);
133
26f8b3a8 134 dinfo->auto_del = 1;
14bafc54
MA
135}
136
4be74634 137void blockdev_auto_del(BlockBackend *blk)
14bafc54 138{
18e46a03 139 DriveInfo *dinfo = blk_legacy_dinfo(blk);
14bafc54 140
0fc0f1fa 141 if (dinfo && dinfo->auto_del) {
b9fe8a7a 142 blk_unref(blk);
14bafc54
MA
143 }
144}
145
d8f94e1b
JS
146/**
147 * Returns the current mapping of how many units per bus
148 * a particular interface can support.
149 *
150 * A positive integer indicates n units per bus.
151 * 0 implies the mapping has not been established.
152 * -1 indicates an invalid BlockInterfaceType was given.
153 */
154int drive_get_max_devs(BlockInterfaceType type)
155{
156 if (type >= IF_IDE && type < IF_COUNT) {
157 return if_max_devs[type];
158 }
159
160 return -1;
161}
162
505a7fb1
MA
163static int drive_index_to_bus_id(BlockInterfaceType type, int index)
164{
165 int max_devs = if_max_devs[type];
166 return max_devs ? index / max_devs : 0;
167}
168
169static int drive_index_to_unit_id(BlockInterfaceType type, int index)
170{
171 int max_devs = if_max_devs[type];
172 return max_devs ? index % max_devs : index;
173}
174
2292ddae
MA
175QemuOpts *drive_def(const char *optstr)
176{
70b94331 177 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
2292ddae
MA
178}
179
180QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
5645b0f4 181 const char *optstr)
666daa68 182{
666daa68
MA
183 QemuOpts *opts;
184
2292ddae 185 opts = drive_def(optstr);
666daa68
MA
186 if (!opts) {
187 return NULL;
188 }
2292ddae 189 if (type != IF_DEFAULT) {
f43e47db 190 qemu_opt_set(opts, "if", if_name[type], &error_abort);
2292ddae
MA
191 }
192 if (index >= 0) {
a8b18f8f 193 qemu_opt_set_number(opts, "index", index, &error_abort);
2292ddae 194 }
666daa68 195 if (file)
f43e47db 196 qemu_opt_set(opts, "file", file, &error_abort);
666daa68
MA
197 return opts;
198}
199
200DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
201{
18e46a03 202 BlockBackend *blk;
666daa68
MA
203 DriveInfo *dinfo;
204
18e46a03
MA
205 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
206 dinfo = blk_legacy_dinfo(blk);
207 if (dinfo && dinfo->type == type
208 && dinfo->bus == bus && dinfo->unit == unit) {
666daa68 209 return dinfo;
18e46a03 210 }
666daa68
MA
211 }
212
213 return NULL;
214}
215
a66c9dc7
JS
216bool drive_check_orphaned(void)
217{
18e46a03 218 BlockBackend *blk;
a66c9dc7
JS
219 DriveInfo *dinfo;
220 bool rs = false;
221
18e46a03
MA
222 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
223 dinfo = blk_legacy_dinfo(blk);
a66c9dc7
JS
224 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
225 /* Unless this is a default drive, this may be an oversight. */
a7f53e26 226 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
a66c9dc7
JS
227 dinfo->type != IF_NONE) {
228 fprintf(stderr, "Warning: Orphaned drive without device: "
229 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
d3aeb1b7 230 blk_name(blk), blk_bs(blk)->filename, if_name[dinfo->type],
a66c9dc7
JS
231 dinfo->bus, dinfo->unit);
232 rs = true;
233 }
234 }
235
236 return rs;
237}
238
f1bd51ac
MA
239DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
240{
241 return drive_get(type,
242 drive_index_to_bus_id(type, index),
243 drive_index_to_unit_id(type, index));
244}
245
666daa68
MA
246int drive_get_max_bus(BlockInterfaceType type)
247{
248 int max_bus;
18e46a03 249 BlockBackend *blk;
666daa68
MA
250 DriveInfo *dinfo;
251
252 max_bus = -1;
18e46a03
MA
253 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
254 dinfo = blk_legacy_dinfo(blk);
255 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
666daa68 256 max_bus = dinfo->bus;
18e46a03 257 }
666daa68
MA
258 }
259 return max_bus;
260}
261
13839974
MA
262/* Get a block device. This should only be used for single-drive devices
263 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
264 appropriate bus. */
265DriveInfo *drive_get_next(BlockInterfaceType type)
266{
267 static int next_block_unit[IF_COUNT];
268
269 return drive_get(type, 0, next_block_unit[type]++);
270}
271
666daa68
MA
272static void bdrv_format_print(void *opaque, const char *name)
273{
807105a7 274 error_printf(" %s", name);
666daa68
MA
275}
276
aa398a5c
SH
277typedef struct {
278 QEMUBH *bh;
fa510ebf
FZ
279 BlockDriverState *bs;
280} BDRVPutRefBH;
aa398a5c 281
fa510ebf 282static void bdrv_put_ref_bh(void *opaque)
aa398a5c 283{
fa510ebf 284 BDRVPutRefBH *s = opaque;
aa398a5c 285
fa510ebf 286 bdrv_unref(s->bs);
aa398a5c
SH
287 qemu_bh_delete(s->bh);
288 g_free(s);
289}
290
291/*
fa510ebf 292 * Release a BDS reference in a BH
aa398a5c 293 *
fa510ebf
FZ
294 * It is not safe to use bdrv_unref() from a callback function when the callers
295 * still need the BlockDriverState. In such cases we schedule a BH to release
296 * the reference.
aa398a5c 297 */
fa510ebf 298static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
aa398a5c 299{
fa510ebf 300 BDRVPutRefBH *s;
aa398a5c 301
fa510ebf
FZ
302 s = g_new(BDRVPutRefBH, 1);
303 s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
304 s->bs = bs;
aa398a5c
SH
305 qemu_bh_schedule(s->bh);
306}
307
b681072d 308static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
666daa68
MA
309{
310 if (!strcmp(buf, "ignore")) {
92aa5c6d 311 return BLOCKDEV_ON_ERROR_IGNORE;
666daa68 312 } else if (!is_read && !strcmp(buf, "enospc")) {
92aa5c6d 313 return BLOCKDEV_ON_ERROR_ENOSPC;
666daa68 314 } else if (!strcmp(buf, "stop")) {
92aa5c6d 315 return BLOCKDEV_ON_ERROR_STOP;
666daa68 316 } else if (!strcmp(buf, "report")) {
92aa5c6d 317 return BLOCKDEV_ON_ERROR_REPORT;
666daa68 318 } else {
b681072d
KW
319 error_setg(errp, "'%s' invalid %s error action",
320 buf, is_read ? "read" : "write");
666daa68
MA
321 return -1;
322 }
323}
324
cc0681c4 325static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
0563e191 326{
cc0681c4
BC
327 if (throttle_conflicting(cfg)) {
328 error_setg(errp, "bps/iops/max total values and read/write values"
329 " cannot be used at the same time");
0563e191
ZYW
330 return false;
331 }
332
cc0681c4
BC
333 if (!throttle_is_valid(cfg)) {
334 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
7d81c141
SH
335 return false;
336 }
337
0563e191
ZYW
338 return true;
339}
340
33cb7dc8
KW
341typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
342
f298d071 343/* Takes the ownership of bs_opts */
18e46a03
MA
344static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
345 Error **errp)
666daa68
MA
346{
347 const char *buf;
666daa68
MA
348 int ro = 0;
349 int bdrv_flags = 0;
350 int on_read_error, on_write_error;
26f54e9a 351 BlockBackend *blk;
a0f1eab1 352 BlockDriverState *bs;
cc0681c4 353 ThrottleConfig cfg;
666daa68 354 int snapshot = 0;
fb0490f6 355 bool copy_on_read;
c546194f 356 Error *error = NULL;
0006383e 357 QemuOpts *opts;
0006383e 358 const char *id;
74fe54f2 359 bool has_driver_specific_opts;
465bee1d 360 BlockdevDetectZeroesOptions detect_zeroes;
76f4afb4 361 const char *throttling_group;
666daa68 362
f298d071
KW
363 /* Check common options by copying from bs_opts to opts, all other options
364 * stay in bs_opts for processing by bdrv_open(). */
365 id = qdict_get_try_str(bs_opts, "id");
0006383e 366 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
84d18f06 367 if (error) {
b681072d 368 error_propagate(errp, error);
6376f952 369 goto err_no_opts;
0006383e
KW
370 }
371
0006383e 372 qemu_opts_absorb_qdict(opts, bs_opts, &error);
84d18f06 373 if (error) {
b681072d 374 error_propagate(errp, error);
ec9c10d2 375 goto early_err;
0006383e
KW
376 }
377
378 if (id) {
379 qdict_del(bs_opts, "id");
380 }
381
74fe54f2
KW
382 has_driver_specific_opts = !!qdict_size(bs_opts);
383
666daa68 384 /* extract parameters */
666daa68 385 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
0f227a94 386 ro = qemu_opt_get_bool(opts, "read-only", 0);
fb0490f6 387 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
666daa68 388
a9384aff
PB
389 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
390 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
b681072d 391 error_setg(errp, "invalid discard option");
ec9c10d2 392 goto early_err;
a9384aff
PB
393 }
394 }
395
54861b92 396 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) {
29c4e2b5
KW
397 bdrv_flags |= BDRV_O_CACHE_WB;
398 }
54861b92 399 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
29c4e2b5
KW
400 bdrv_flags |= BDRV_O_NOCACHE;
401 }
54861b92 402 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
29c4e2b5 403 bdrv_flags |= BDRV_O_NO_FLUSH;
666daa68
MA
404 }
405
406#ifdef CONFIG_LINUX_AIO
407 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
408 if (!strcmp(buf, "native")) {
409 bdrv_flags |= BDRV_O_NATIVE_AIO;
410 } else if (!strcmp(buf, "threads")) {
411 /* this is the default */
412 } else {
b681072d 413 error_setg(errp, "invalid aio option");
ec9c10d2 414 goto early_err;
666daa68
MA
415 }
416 }
417#endif
418
419 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
c8057f95
PM
420 if (is_help_option(buf)) {
421 error_printf("Supported formats:");
422 bdrv_iterate_format(bdrv_format_print, NULL);
423 error_printf("\n");
ec9c10d2 424 goto early_err;
666daa68 425 }
74fe54f2 426
e4342ce5
HR
427 if (qdict_haskey(bs_opts, "driver")) {
428 error_setg(errp, "Cannot specify both 'driver' and 'format'");
ec9c10d2 429 goto early_err;
6db5f5d6 430 }
e4342ce5 431 qdict_put(bs_opts, "driver", qstring_from_str(buf));
666daa68
MA
432 }
433
0563e191 434 /* disk I/O throttling */
cc0681c4
BC
435 memset(&cfg, 0, sizeof(cfg));
436 cfg.buckets[THROTTLE_BPS_TOTAL].avg =
57975222 437 qemu_opt_get_number(opts, "throttling.bps-total", 0);
cc0681c4 438 cfg.buckets[THROTTLE_BPS_READ].avg =
57975222 439 qemu_opt_get_number(opts, "throttling.bps-read", 0);
cc0681c4 440 cfg.buckets[THROTTLE_BPS_WRITE].avg =
57975222 441 qemu_opt_get_number(opts, "throttling.bps-write", 0);
cc0681c4 442 cfg.buckets[THROTTLE_OPS_TOTAL].avg =
57975222 443 qemu_opt_get_number(opts, "throttling.iops-total", 0);
cc0681c4 444 cfg.buckets[THROTTLE_OPS_READ].avg =
57975222 445 qemu_opt_get_number(opts, "throttling.iops-read", 0);
cc0681c4 446 cfg.buckets[THROTTLE_OPS_WRITE].avg =
57975222 447 qemu_opt_get_number(opts, "throttling.iops-write", 0);
0563e191 448
3e9fab69
BC
449 cfg.buckets[THROTTLE_BPS_TOTAL].max =
450 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
451 cfg.buckets[THROTTLE_BPS_READ].max =
452 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
453 cfg.buckets[THROTTLE_BPS_WRITE].max =
454 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
455 cfg.buckets[THROTTLE_OPS_TOTAL].max =
456 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
457 cfg.buckets[THROTTLE_OPS_READ].max =
458 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
459 cfg.buckets[THROTTLE_OPS_WRITE].max =
460 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
cc0681c4 461
2024c1df 462 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
cc0681c4 463
76f4afb4
AG
464 throttling_group = qemu_opt_get(opts, "throttling.group");
465
cc0681c4 466 if (!check_throttle_config(&cfg, &error)) {
b681072d 467 error_propagate(errp, error);
ec9c10d2 468 goto early_err;
0563e191
ZYW
469 }
470
92aa5c6d 471 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
666daa68 472 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
b681072d 473 on_write_error = parse_block_error_action(buf, 0, &error);
84d18f06 474 if (error) {
b681072d 475 error_propagate(errp, error);
ec9c10d2 476 goto early_err;
666daa68
MA
477 }
478 }
479
92aa5c6d 480 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
666daa68 481 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
b681072d 482 on_read_error = parse_block_error_action(buf, 1, &error);
84d18f06 483 if (error) {
b681072d 484 error_propagate(errp, error);
ec9c10d2 485 goto early_err;
666daa68
MA
486 }
487 }
488
465bee1d 489 detect_zeroes =
9e7dac7c
PL
490 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
491 qemu_opt_get(opts, "detect-zeroes"),
492 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
493 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
494 &error);
465bee1d
PL
495 if (error) {
496 error_propagate(errp, error);
497 goto early_err;
498 }
499
500 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
501 !(bdrv_flags & BDRV_O_UNMAP)) {
502 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
503 "without setting discard operation to unmap");
504 goto early_err;
505 }
506
326642bc 507 /* init */
e4342ce5
HR
508 if ((!file || !*file) && !has_driver_specific_opts) {
509 blk = blk_new_with_bs(qemu_opts_id(opts), errp);
510 if (!blk) {
511 goto early_err;
512 }
abd7f68d 513
e4342ce5
HR
514 bs = blk_bs(blk);
515 bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
516 bs->read_only = ro;
0563e191 517
e4342ce5
HR
518 QDECREF(bs_opts);
519 } else {
520 if (file && !*file) {
c2ad1b0c 521 file = NULL;
c2ad1b0c 522 }
666daa68 523
e4342ce5
HR
524 if (snapshot) {
525 /* always use cache=unsafe with snapshot */
526 bdrv_flags &= ~BDRV_O_CACHE_MASK;
527 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
528 }
529
530 if (copy_on_read) {
531 bdrv_flags |= BDRV_O_COPY_ON_READ;
532 }
fb0490f6 533
e4342ce5
HR
534 if (runstate_check(RUN_STATE_INMIGRATE)) {
535 bdrv_flags |= BDRV_O_INCOMING;
536 }
537
538 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
539
540 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
541 errp);
542 if (!blk) {
543 goto err_no_bs_opts;
544 }
545 bs = blk_bs(blk);
ed9d4205
BC
546 }
547
e4342ce5 548 bs->detect_zeroes = detect_zeroes;
666daa68 549
e4342ce5 550 bdrv_set_on_error(bs, on_read_error, on_write_error);
0006383e 551
e4342ce5
HR
552 /* disk I/O throttling */
553 if (throttle_enabled(&cfg)) {
76f4afb4
AG
554 if (!throttling_group) {
555 throttling_group = blk_name(blk);
556 }
557 bdrv_io_limits_enable(bs, throttling_group);
e4342ce5 558 bdrv_set_io_limits(bs, &cfg);
666daa68
MA
559 }
560
a0f1eab1 561 if (bdrv_key_required(bs)) {
666daa68 562 autostart = 0;
a0f1eab1 563 }
0006383e 564
e4342ce5 565err_no_bs_opts:
0006383e 566 qemu_opts_del(opts);
18e46a03 567 return blk;
a9ae2bff 568
ec9c10d2 569early_err:
ec9c10d2 570 qemu_opts_del(opts);
6376f952
MA
571err_no_opts:
572 QDECREF(bs_opts);
a9ae2bff 573 return NULL;
666daa68
MA
574}
575
5abbf0ee
KW
576static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
577 Error **errp)
57975222
KW
578{
579 const char *value;
580
581 value = qemu_opt_get(opts, from);
582 if (value) {
5abbf0ee
KW
583 if (qemu_opt_find(opts, to)) {
584 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
585 "same time", to, from);
586 return;
587 }
20d6cd47
JL
588 }
589
590 /* rename all items in opts */
591 while ((value = qemu_opt_get(opts, from))) {
f43e47db 592 qemu_opt_set(opts, to, value, &error_abort);
57975222
KW
593 qemu_opt_unset(opts, from);
594 }
595}
596
33cb7dc8
KW
597QemuOptsList qemu_legacy_drive_opts = {
598 .name = "drive",
599 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
600 .desc = {
601 {
87a899c5
KW
602 .name = "bus",
603 .type = QEMU_OPT_NUMBER,
604 .help = "bus number",
605 },{
606 .name = "unit",
607 .type = QEMU_OPT_NUMBER,
608 .help = "unit number (i.e. lun for scsi)",
609 },{
610 .name = "index",
611 .type = QEMU_OPT_NUMBER,
612 .help = "index number",
613 },{
33cb7dc8
KW
614 .name = "media",
615 .type = QEMU_OPT_STRING,
616 .help = "media type (disk, cdrom)",
593d464b
KW
617 },{
618 .name = "if",
619 .type = QEMU_OPT_STRING,
620 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
b41a7338
KW
621 },{
622 .name = "cyls",
623 .type = QEMU_OPT_NUMBER,
624 .help = "number of cylinders (ide disk geometry)",
625 },{
626 .name = "heads",
627 .type = QEMU_OPT_NUMBER,
628 .help = "number of heads (ide disk geometry)",
629 },{
630 .name = "secs",
631 .type = QEMU_OPT_NUMBER,
632 .help = "number of sectors (ide disk geometry)",
633 },{
634 .name = "trans",
635 .type = QEMU_OPT_STRING,
636 .help = "chs translation (auto, lba, none)",
26929298
KW
637 },{
638 .name = "boot",
639 .type = QEMU_OPT_BOOL,
640 .help = "(deprecated, ignored)",
394c7d4d
KW
641 },{
642 .name = "addr",
643 .type = QEMU_OPT_STRING,
644 .help = "pci address (virtio only)",
bcf83158
KW
645 },{
646 .name = "serial",
647 .type = QEMU_OPT_STRING,
648 .help = "disk serial number",
d095b465
HR
649 },{
650 .name = "file",
651 .type = QEMU_OPT_STRING,
652 .help = "file name",
33cb7dc8 653 },
0ebd24e0
KW
654
655 /* Options that are passed on, but have special semantics with -drive */
656 {
657 .name = "read-only",
658 .type = QEMU_OPT_BOOL,
659 .help = "open drive file as read-only",
ee13ed1c
KW
660 },{
661 .name = "rerror",
662 .type = QEMU_OPT_STRING,
663 .help = "read error action",
664 },{
665 .name = "werror",
666 .type = QEMU_OPT_STRING,
667 .help = "write error action",
0ebd24e0
KW
668 },{
669 .name = "copy-on-read",
670 .type = QEMU_OPT_BOOL,
671 .help = "copy read data from backing file into image file",
672 },
673
33cb7dc8
KW
674 { /* end of list */ }
675 },
676};
677
60e19e06 678DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
57975222 679{
29c4e2b5 680 const char *value;
18e46a03 681 BlockBackend *blk;
33cb7dc8 682 DriveInfo *dinfo = NULL;
f298d071 683 QDict *bs_opts;
33cb7dc8
KW
684 QemuOpts *legacy_opts;
685 DriveMediaType media = MEDIA_DISK;
593d464b 686 BlockInterfaceType type;
b41a7338 687 int cyls, heads, secs, translation;
87a899c5 688 int max_devs, bus_id, unit_id, index;
394c7d4d 689 const char *devaddr;
ee13ed1c 690 const char *werror, *rerror;
a7fdbcf0
FZ
691 bool read_only = false;
692 bool copy_on_read;
bcf83158 693 const char *serial;
d095b465 694 const char *filename;
33cb7dc8 695 Error *local_err = NULL;
247147fb 696 int i;
29c4e2b5 697
57975222 698 /* Change legacy command line options into QMP ones */
247147fb
KW
699 static const struct {
700 const char *from;
701 const char *to;
702 } opt_renames[] = {
703 { "iops", "throttling.iops-total" },
704 { "iops_rd", "throttling.iops-read" },
705 { "iops_wr", "throttling.iops-write" },
57975222 706
247147fb
KW
707 { "bps", "throttling.bps-total" },
708 { "bps_rd", "throttling.bps-read" },
709 { "bps_wr", "throttling.bps-write" },
57975222 710
247147fb
KW
711 { "iops_max", "throttling.iops-total-max" },
712 { "iops_rd_max", "throttling.iops-read-max" },
713 { "iops_wr_max", "throttling.iops-write-max" },
3e9fab69 714
247147fb
KW
715 { "bps_max", "throttling.bps-total-max" },
716 { "bps_rd_max", "throttling.bps-read-max" },
717 { "bps_wr_max", "throttling.bps-write-max" },
3e9fab69 718
247147fb 719 { "iops_size", "throttling.iops-size" },
2024c1df 720
76f4afb4
AG
721 { "group", "throttling.group" },
722
247147fb
KW
723 { "readonly", "read-only" },
724 };
725
726 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
5abbf0ee
KW
727 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
728 &local_err);
729 if (local_err) {
565f65d2 730 error_report_err(local_err);
5abbf0ee
KW
731 return NULL;
732 }
247147fb 733 }
0f227a94 734
29c4e2b5
KW
735 value = qemu_opt_get(all_opts, "cache");
736 if (value) {
737 int flags = 0;
738
739 if (bdrv_parse_cache_flags(value, &flags) != 0) {
740 error_report("invalid cache option");
741 return NULL;
742 }
743
744 /* Specific options take precedence */
54861b92
KW
745 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
746 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
cccb7967 747 !!(flags & BDRV_O_CACHE_WB), &error_abort);
29c4e2b5 748 }
54861b92
KW
749 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
750 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
cccb7967 751 !!(flags & BDRV_O_NOCACHE), &error_abort);
29c4e2b5 752 }
54861b92
KW
753 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
754 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
cccb7967 755 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
29c4e2b5
KW
756 }
757 qemu_opt_unset(all_opts, "cache");
758 }
759
f298d071
KW
760 /* Get a QDict for processing the options */
761 bs_opts = qdict_new();
762 qemu_opts_to_qdict(all_opts, bs_opts);
763
87ea75d5
PC
764 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
765 &error_abort);
33cb7dc8 766 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
84d18f06 767 if (local_err) {
565f65d2 768 error_report_err(local_err);
33cb7dc8
KW
769 goto fail;
770 }
771
26929298
KW
772 /* Deprecated option boot=[on|off] */
773 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
774 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
775 "ignored. Future versions will reject this parameter. Please "
776 "update your scripts.\n");
777 }
778
33cb7dc8
KW
779 /* Media type */
780 value = qemu_opt_get(legacy_opts, "media");
781 if (value) {
782 if (!strcmp(value, "disk")) {
783 media = MEDIA_DISK;
784 } else if (!strcmp(value, "cdrom")) {
785 media = MEDIA_CDROM;
a7fdbcf0 786 read_only = true;
33cb7dc8
KW
787 } else {
788 error_report("'%s' invalid media", value);
789 goto fail;
790 }
791 }
792
0ebd24e0 793 /* copy-on-read is disabled with a warning for read-only devices */
a7fdbcf0 794 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
0ebd24e0
KW
795 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
796
797 if (read_only && copy_on_read) {
798 error_report("warning: disabling copy-on-read on read-only drive");
799 copy_on_read = false;
800 }
801
802 qdict_put(bs_opts, "read-only",
803 qstring_from_str(read_only ? "on" : "off"));
804 qdict_put(bs_opts, "copy-on-read",
805 qstring_from_str(copy_on_read ? "on" :"off"));
806
593d464b
KW
807 /* Controller type */
808 value = qemu_opt_get(legacy_opts, "if");
809 if (value) {
810 for (type = 0;
811 type < IF_COUNT && strcmp(value, if_name[type]);
812 type++) {
813 }
814 if (type == IF_COUNT) {
815 error_report("unsupported bus type '%s'", value);
816 goto fail;
817 }
818 } else {
819 type = block_default_type;
820 }
821
b41a7338
KW
822 /* Geometry */
823 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
824 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
825 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
826
827 if (cyls || heads || secs) {
828 if (cyls < 1) {
829 error_report("invalid physical cyls number");
830 goto fail;
831 }
832 if (heads < 1) {
833 error_report("invalid physical heads number");
834 goto fail;
835 }
836 if (secs < 1) {
837 error_report("invalid physical secs number");
838 goto fail;
839 }
840 }
841
842 translation = BIOS_ATA_TRANSLATION_AUTO;
843 value = qemu_opt_get(legacy_opts, "trans");
844 if (value != NULL) {
845 if (!cyls) {
846 error_report("'%s' trans must be used with cyls, heads and secs",
847 value);
848 goto fail;
849 }
850 if (!strcmp(value, "none")) {
851 translation = BIOS_ATA_TRANSLATION_NONE;
852 } else if (!strcmp(value, "lba")) {
853 translation = BIOS_ATA_TRANSLATION_LBA;
f31c41ff
PB
854 } else if (!strcmp(value, "large")) {
855 translation = BIOS_ATA_TRANSLATION_LARGE;
856 } else if (!strcmp(value, "rechs")) {
857 translation = BIOS_ATA_TRANSLATION_RECHS;
b41a7338
KW
858 } else if (!strcmp(value, "auto")) {
859 translation = BIOS_ATA_TRANSLATION_AUTO;
860 } else {
861 error_report("'%s' invalid translation type", value);
862 goto fail;
863 }
864 }
865
866 if (media == MEDIA_CDROM) {
867 if (cyls || secs || heads) {
868 error_report("CHS can't be set with media=cdrom");
869 goto fail;
870 }
871 }
872
87a899c5
KW
873 /* Device address specified by bus/unit or index.
874 * If none was specified, try to find the first free one. */
875 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
876 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
877 index = qemu_opt_get_number(legacy_opts, "index", -1);
878
879 max_devs = if_max_devs[type];
880
881 if (index != -1) {
882 if (bus_id != 0 || unit_id != -1) {
883 error_report("index cannot be used with bus and unit");
884 goto fail;
885 }
886 bus_id = drive_index_to_bus_id(type, index);
887 unit_id = drive_index_to_unit_id(type, index);
888 }
889
890 if (unit_id == -1) {
891 unit_id = 0;
892 while (drive_get(type, bus_id, unit_id) != NULL) {
893 unit_id++;
894 if (max_devs && unit_id >= max_devs) {
895 unit_id -= max_devs;
896 bus_id++;
897 }
898 }
899 }
900
901 if (max_devs && unit_id >= max_devs) {
902 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
903 goto fail;
904 }
905
906 if (drive_get(type, bus_id, unit_id) != NULL) {
907 error_report("drive with bus=%d, unit=%d (index=%d) exists",
908 bus_id, unit_id, index);
909 goto fail;
910 }
911
bcf83158
KW
912 /* Serial number */
913 serial = qemu_opt_get(legacy_opts, "serial");
914
87a899c5
KW
915 /* no id supplied -> create one */
916 if (qemu_opts_id(all_opts) == NULL) {
917 char *new_id;
918 const char *mediastr = "";
919 if (type == IF_IDE || type == IF_SCSI) {
920 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
921 }
922 if (max_devs) {
923 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
924 mediastr, unit_id);
925 } else {
926 new_id = g_strdup_printf("%s%s%i", if_name[type],
927 mediastr, unit_id);
928 }
929 qdict_put(bs_opts, "id", qstring_from_str(new_id));
930 g_free(new_id);
931 }
932
394c7d4d
KW
933 /* Add virtio block device */
934 devaddr = qemu_opt_get(legacy_opts, "addr");
935 if (devaddr && type != IF_VIRTIO) {
936 error_report("addr is not supported by this bus type");
937 goto fail;
938 }
939
940 if (type == IF_VIRTIO) {
941 QemuOpts *devopts;
87ea75d5
PC
942 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
943 &error_abort);
394c7d4d 944 if (arch_type == QEMU_ARCH_S390X) {
1f68f1d3 945 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
394c7d4d 946 } else {
f43e47db 947 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
394c7d4d 948 }
f43e47db
MA
949 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
950 &error_abort);
394c7d4d 951 if (devaddr) {
f43e47db 952 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
394c7d4d
KW
953 }
954 }
955
d095b465
HR
956 filename = qemu_opt_get(legacy_opts, "file");
957
ee13ed1c
KW
958 /* Check werror/rerror compatibility with if=... */
959 werror = qemu_opt_get(legacy_opts, "werror");
960 if (werror != NULL) {
961 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
962 type != IF_NONE) {
963 error_report("werror is not supported by this bus type");
964 goto fail;
965 }
966 qdict_put(bs_opts, "werror", qstring_from_str(werror));
967 }
968
969 rerror = qemu_opt_get(legacy_opts, "rerror");
970 if (rerror != NULL) {
971 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
972 type != IF_NONE) {
973 error_report("rerror is not supported by this bus type");
974 goto fail;
975 }
976 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
977 }
978
2d246f01 979 /* Actual block device init: Functionality shared with blockdev-add */
18e46a03 980 blk = blockdev_init(filename, bs_opts, &local_err);
3cb0e25c 981 bs_opts = NULL;
18e46a03 982 if (!blk) {
84d18f06 983 if (local_err) {
565f65d2 984 error_report_err(local_err);
b681072d 985 }
2d246f01 986 goto fail;
b681072d 987 } else {
84d18f06 988 assert(!local_err);
2d246f01
KW
989 }
990
26f8b3a8
MA
991 /* Create legacy DriveInfo */
992 dinfo = g_malloc0(sizeof(*dinfo));
f298d071 993 dinfo->opts = all_opts;
2d246f01 994
b41a7338
KW
995 dinfo->cyls = cyls;
996 dinfo->heads = heads;
997 dinfo->secs = secs;
998 dinfo->trans = translation;
999
ee13ed1c 1000 dinfo->type = type;
87a899c5
KW
1001 dinfo->bus = bus_id;
1002 dinfo->unit = unit_id;
394c7d4d 1003 dinfo->devaddr = devaddr;
bcf83158
KW
1004 dinfo->serial = g_strdup(serial);
1005
26f8b3a8
MA
1006 blk_set_legacy_dinfo(blk, dinfo);
1007
e34ef046
KW
1008 switch(type) {
1009 case IF_IDE:
1010 case IF_SCSI:
1011 case IF_XEN:
1012 case IF_NONE:
1013 dinfo->media_cd = media == MEDIA_CDROM;
1014 break;
1015 default:
1016 break;
1017 }
1018
2d246f01 1019fail:
33cb7dc8 1020 qemu_opts_del(legacy_opts);
3cb0e25c 1021 QDECREF(bs_opts);
2d246f01 1022 return dinfo;
57975222
KW
1023}
1024
3e5a50d6 1025void hmp_commit(Monitor *mon, const QDict *qdict)
666daa68 1026{
666daa68 1027 const char *device = qdict_get_str(qdict, "device");
a0e8544c 1028 BlockBackend *blk;
e8877497 1029 int ret;
666daa68 1030
6ab4b5ab 1031 if (!strcmp(device, "all")) {
e8877497 1032 ret = bdrv_commit_all();
6ab4b5ab 1033 } else {
a0e8544c
FZ
1034 blk = blk_by_name(device);
1035 if (!blk) {
58513bde 1036 monitor_printf(mon, "Device '%s' not found\n", device);
ac59eb95 1037 return;
6ab4b5ab 1038 }
a0e8544c 1039 ret = bdrv_commit(blk_bs(blk));
58513bde
JC
1040 }
1041 if (ret < 0) {
1042 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1043 strerror(-ret));
666daa68
MA
1044 }
1045}
1046
6cc2a415
PB
1047static void blockdev_do_action(int kind, void *data, Error **errp)
1048{
c8a83e85
KW
1049 TransactionAction action;
1050 TransactionActionList list;
6cc2a415
PB
1051
1052 action.kind = kind;
1053 action.data = data;
1054 list.value = &action;
1055 list.next = NULL;
1056 qmp_transaction(&list, errp);
1057}
1058
0901f67e
BC
1059void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1060 bool has_node_name, const char *node_name,
1061 const char *snapshot_file,
1062 bool has_snapshot_node_name,
1063 const char *snapshot_node_name,
6106e249 1064 bool has_format, const char *format,
0901f67e 1065 bool has_mode, NewImageMode mode, Error **errp)
f8882568 1066{
6cc2a415 1067 BlockdevSnapshot snapshot = {
0901f67e 1068 .has_device = has_device,
6cc2a415 1069 .device = (char *) device,
0901f67e
BC
1070 .has_node_name = has_node_name,
1071 .node_name = (char *) node_name,
6cc2a415 1072 .snapshot_file = (char *) snapshot_file,
0901f67e
BC
1073 .has_snapshot_node_name = has_snapshot_node_name,
1074 .snapshot_node_name = (char *) snapshot_node_name,
6cc2a415
PB
1075 .has_format = has_format,
1076 .format = (char *) format,
1077 .has_mode = has_mode,
1078 .mode = mode,
1079 };
c8a83e85
KW
1080 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1081 &snapshot, errp);
f8882568
JS
1082}
1083
f323bc9e
WX
1084void qmp_blockdev_snapshot_internal_sync(const char *device,
1085 const char *name,
1086 Error **errp)
1087{
1088 BlockdevSnapshotInternal snapshot = {
1089 .device = (char *) device,
1090 .name = (char *) name
1091 };
1092
1093 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1094 &snapshot, errp);
1095}
1096
44e3e053
WX
1097SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1098 bool has_id,
1099 const char *id,
1100 bool has_name,
1101 const char *name,
1102 Error **errp)
1103{
a0e8544c
FZ
1104 BlockDriverState *bs;
1105 BlockBackend *blk;
4ef3982a 1106 AioContext *aio_context;
44e3e053
WX
1107 QEMUSnapshotInfo sn;
1108 Error *local_err = NULL;
1109 SnapshotInfo *info = NULL;
1110 int ret;
1111
a0e8544c
FZ
1112 blk = blk_by_name(device);
1113 if (!blk) {
75158ebb
MA
1114 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1115 "Device '%s' not found", device);
44e3e053
WX
1116 return NULL;
1117 }
a0e8544c 1118 bs = blk_bs(blk);
44e3e053
WX
1119
1120 if (!has_id) {
1121 id = NULL;
1122 }
1123
1124 if (!has_name) {
1125 name = NULL;
1126 }
1127
1128 if (!id && !name) {
1129 error_setg(errp, "Name or id must be provided");
1130 return NULL;
1131 }
1132
4ef3982a
SH
1133 aio_context = bdrv_get_aio_context(bs);
1134 aio_context_acquire(aio_context);
1135
0b928854
SH
1136 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1137 goto out_aio_context;
1138 }
1139
44e3e053 1140 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
84d18f06 1141 if (local_err) {
44e3e053 1142 error_propagate(errp, local_err);
4ef3982a 1143 goto out_aio_context;
44e3e053
WX
1144 }
1145 if (!ret) {
1146 error_setg(errp,
1147 "Snapshot with id '%s' and name '%s' does not exist on "
1148 "device '%s'",
1149 STR_OR_NULL(id), STR_OR_NULL(name), device);
4ef3982a 1150 goto out_aio_context;
44e3e053
WX
1151 }
1152
1153 bdrv_snapshot_delete(bs, id, name, &local_err);
84d18f06 1154 if (local_err) {
44e3e053 1155 error_propagate(errp, local_err);
4ef3982a 1156 goto out_aio_context;
44e3e053
WX
1157 }
1158
4ef3982a
SH
1159 aio_context_release(aio_context);
1160
5839e53b 1161 info = g_new0(SnapshotInfo, 1);
44e3e053
WX
1162 info->id = g_strdup(sn.id_str);
1163 info->name = g_strdup(sn.name);
1164 info->date_nsec = sn.date_nsec;
1165 info->date_sec = sn.date_sec;
1166 info->vm_state_size = sn.vm_state_size;
1167 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1168 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1169
1170 return info;
4ef3982a
SH
1171
1172out_aio_context:
1173 aio_context_release(aio_context);
1174 return NULL;
44e3e053 1175}
8802d1fd 1176
341ebc2f
JS
1177/**
1178 * block_dirty_bitmap_lookup:
1179 * Return a dirty bitmap (if present), after validating
1180 * the node reference and bitmap names.
1181 *
1182 * @node: The name of the BDS node to search for bitmaps
1183 * @name: The name of the bitmap to search for
1184 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1185 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1186 * @errp: Output pointer for error information. Can be NULL.
1187 *
1188 * @return: A bitmap object on success, or NULL on failure.
1189 */
1190static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1191 const char *name,
1192 BlockDriverState **pbs,
1193 AioContext **paio,
1194 Error **errp)
1195{
1196 BlockDriverState *bs;
1197 BdrvDirtyBitmap *bitmap;
1198 AioContext *aio_context;
1199
1200 if (!node) {
1201 error_setg(errp, "Node cannot be NULL");
1202 return NULL;
1203 }
1204 if (!name) {
1205 error_setg(errp, "Bitmap name cannot be NULL");
1206 return NULL;
1207 }
1208 bs = bdrv_lookup_bs(node, node, NULL);
1209 if (!bs) {
1210 error_setg(errp, "Node '%s' not found", node);
1211 return NULL;
1212 }
1213
1214 aio_context = bdrv_get_aio_context(bs);
1215 aio_context_acquire(aio_context);
1216
1217 bitmap = bdrv_find_dirty_bitmap(bs, name);
1218 if (!bitmap) {
1219 error_setg(errp, "Dirty bitmap '%s' not found", name);
1220 goto fail;
1221 }
1222
1223 if (pbs) {
1224 *pbs = bs;
1225 }
1226 if (paio) {
1227 *paio = aio_context;
1228 } else {
1229 aio_context_release(aio_context);
1230 }
1231
1232 return bitmap;
1233
1234 fail:
1235 aio_context_release(aio_context);
1236 return NULL;
1237}
1238
b756b9ce 1239/* New and old BlockDriverState structs for atomic group operations */
ba0c86a3 1240
ba5d6ab6 1241typedef struct BlkTransactionState BlkTransactionState;
ba0c86a3
WX
1242
1243/* Only prepare() may fail. In a single transaction, only one of commit() or
1244 abort() will be called, clean() will always be called if it present. */
1245typedef struct BdrvActionOps {
1246 /* Size of state struct, in bytes. */
1247 size_t instance_size;
1248 /* Prepare the work, must NOT be NULL. */
ba5d6ab6 1249 void (*prepare)(BlkTransactionState *common, Error **errp);
f9ea81e8 1250 /* Commit the changes, can be NULL. */
ba5d6ab6 1251 void (*commit)(BlkTransactionState *common);
ba0c86a3 1252 /* Abort the changes on fail, can be NULL. */
ba5d6ab6 1253 void (*abort)(BlkTransactionState *common);
ba0c86a3 1254 /* Clean up resource in the end, can be NULL. */
ba5d6ab6 1255 void (*clean)(BlkTransactionState *common);
ba0c86a3
WX
1256} BdrvActionOps;
1257
1258/*
1259 * This structure must be arranged as first member in child type, assuming
1260 * that compiler will also arrange it to the same address with parent instance.
1261 * Later it will be used in free().
1262 */
ba5d6ab6 1263struct BlkTransactionState {
c8a83e85 1264 TransactionAction *action;
ba0c86a3 1265 const BdrvActionOps *ops;
ba5d6ab6 1266 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
ba0c86a3
WX
1267};
1268
bbe86010
WX
1269/* internal snapshot private data */
1270typedef struct InternalSnapshotState {
1271 BlkTransactionState common;
1272 BlockDriverState *bs;
5d6e96ef 1273 AioContext *aio_context;
bbe86010
WX
1274 QEMUSnapshotInfo sn;
1275} InternalSnapshotState;
1276
1277static void internal_snapshot_prepare(BlkTransactionState *common,
1278 Error **errp)
1279{
f70edf99 1280 Error *local_err = NULL;
bbe86010
WX
1281 const char *device;
1282 const char *name;
a0e8544c 1283 BlockBackend *blk;
bbe86010
WX
1284 BlockDriverState *bs;
1285 QEMUSnapshotInfo old_sn, *sn;
1286 bool ret;
1287 qemu_timeval tv;
1288 BlockdevSnapshotInternal *internal;
1289 InternalSnapshotState *state;
1290 int ret1;
1291
1292 g_assert(common->action->kind ==
1293 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1294 internal = common->action->blockdev_snapshot_internal_sync;
1295 state = DO_UPCAST(InternalSnapshotState, common, common);
1296
1297 /* 1. parse input */
1298 device = internal->device;
1299 name = internal->name;
1300
1301 /* 2. check for validation */
a0e8544c
FZ
1302 blk = blk_by_name(device);
1303 if (!blk) {
75158ebb
MA
1304 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1305 "Device '%s' not found", device);
bbe86010
WX
1306 return;
1307 }
a0e8544c 1308 bs = blk_bs(blk);
bbe86010 1309
5d6e96ef
SH
1310 /* AioContext is released in .clean() */
1311 state->aio_context = bdrv_get_aio_context(bs);
1312 aio_context_acquire(state->aio_context);
1313
bbe86010
WX
1314 if (!bdrv_is_inserted(bs)) {
1315 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1316 return;
1317 }
1318
3dc7ca3c
SH
1319 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1320 return;
1321 }
1322
bbe86010 1323 if (bdrv_is_read_only(bs)) {
81e5f78a 1324 error_setg(errp, "Device '%s' is read only", device);
bbe86010
WX
1325 return;
1326 }
1327
1328 if (!bdrv_can_snapshot(bs)) {
81e5f78a
AG
1329 error_setg(errp, "Block format '%s' used by device '%s' "
1330 "does not support internal snapshots",
1331 bs->drv->format_name, device);
bbe86010
WX
1332 return;
1333 }
1334
1335 if (!strlen(name)) {
1336 error_setg(errp, "Name is empty");
1337 return;
1338 }
1339
1340 /* check whether a snapshot with name exist */
f70edf99
MA
1341 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1342 &local_err);
1343 if (local_err) {
1344 error_propagate(errp, local_err);
bbe86010
WX
1345 return;
1346 } else if (ret) {
1347 error_setg(errp,
1348 "Snapshot with name '%s' already exists on device '%s'",
1349 name, device);
1350 return;
1351 }
1352
1353 /* 3. take the snapshot */
1354 sn = &state->sn;
1355 pstrcpy(sn->name, sizeof(sn->name), name);
1356 qemu_gettimeofday(&tv);
1357 sn->date_sec = tv.tv_sec;
1358 sn->date_nsec = tv.tv_usec * 1000;
1359 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1360
1361 ret1 = bdrv_snapshot_create(bs, sn);
1362 if (ret1 < 0) {
1363 error_setg_errno(errp, -ret1,
1364 "Failed to create snapshot '%s' on device '%s'",
1365 name, device);
1366 return;
1367 }
1368
1369 /* 4. succeed, mark a snapshot is created */
1370 state->bs = bs;
1371}
1372
1373static void internal_snapshot_abort(BlkTransactionState *common)
1374{
1375 InternalSnapshotState *state =
1376 DO_UPCAST(InternalSnapshotState, common, common);
1377 BlockDriverState *bs = state->bs;
1378 QEMUSnapshotInfo *sn = &state->sn;
1379 Error *local_error = NULL;
1380
1381 if (!bs) {
1382 return;
1383 }
1384
1385 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1386 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1387 "device '%s' in abort: %s",
1388 sn->id_str,
1389 sn->name,
1390 bdrv_get_device_name(bs),
1391 error_get_pretty(local_error));
1392 error_free(local_error);
1393 }
1394}
1395
5d6e96ef
SH
1396static void internal_snapshot_clean(BlkTransactionState *common)
1397{
1398 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1399 common, common);
1400
1401 if (state->aio_context) {
1402 aio_context_release(state->aio_context);
1403 }
1404}
1405
ba0c86a3 1406/* external snapshot private data */
ba5d6ab6
SH
1407typedef struct ExternalSnapshotState {
1408 BlkTransactionState common;
8802d1fd
JC
1409 BlockDriverState *old_bs;
1410 BlockDriverState *new_bs;
5d6e96ef 1411 AioContext *aio_context;
ba5d6ab6 1412} ExternalSnapshotState;
8802d1fd 1413
ba5d6ab6 1414static void external_snapshot_prepare(BlkTransactionState *common,
9b9877ee
WX
1415 Error **errp)
1416{
9b9877ee
WX
1417 BlockDriver *drv;
1418 int flags, ret;
0901f67e 1419 QDict *options = NULL;
9b9877ee 1420 Error *local_err = NULL;
0901f67e 1421 bool has_device = false;
e2a31e87 1422 const char *device;
0901f67e
BC
1423 bool has_node_name = false;
1424 const char *node_name;
1425 bool has_snapshot_node_name = false;
1426 const char *snapshot_node_name;
e2a31e87
WX
1427 const char *new_image_file;
1428 const char *format = "qcow2";
1429 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
ba5d6ab6
SH
1430 ExternalSnapshotState *state =
1431 DO_UPCAST(ExternalSnapshotState, common, common);
c8a83e85 1432 TransactionAction *action = common->action;
9b9877ee 1433
e2a31e87 1434 /* get parameters */
c8a83e85 1435 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
e2a31e87 1436
0901f67e 1437 has_device = action->blockdev_snapshot_sync->has_device;
e2a31e87 1438 device = action->blockdev_snapshot_sync->device;
0901f67e
BC
1439 has_node_name = action->blockdev_snapshot_sync->has_node_name;
1440 node_name = action->blockdev_snapshot_sync->node_name;
1441 has_snapshot_node_name =
1442 action->blockdev_snapshot_sync->has_snapshot_node_name;
1443 snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name;
1444
e2a31e87
WX
1445 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1446 if (action->blockdev_snapshot_sync->has_format) {
1447 format = action->blockdev_snapshot_sync->format;
1448 }
1449 if (action->blockdev_snapshot_sync->has_mode) {
1450 mode = action->blockdev_snapshot_sync->mode;
1451 }
1452
1453 /* start processing */
9b9877ee
WX
1454 drv = bdrv_find_format(format);
1455 if (!drv) {
1456 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1457 return;
1458 }
1459
0901f67e
BC
1460 state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
1461 has_node_name ? node_name : NULL,
1462 &local_err);
84d18f06 1463 if (local_err) {
0901f67e
BC
1464 error_propagate(errp, local_err);
1465 return;
1466 }
1467
1468 if (has_node_name && !has_snapshot_node_name) {
1469 error_setg(errp, "New snapshot node name missing");
1470 return;
1471 }
1472
1473 if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) {
1474 error_setg(errp, "New snapshot node name already existing");
9b9877ee
WX
1475 return;
1476 }
1477
5d6e96ef
SH
1478 /* Acquire AioContext now so any threads operating on old_bs stop */
1479 state->aio_context = bdrv_get_aio_context(state->old_bs);
1480 aio_context_acquire(state->aio_context);
1481
ba5d6ab6 1482 if (!bdrv_is_inserted(state->old_bs)) {
9b9877ee
WX
1483 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1484 return;
1485 }
1486
3718d8ab
FZ
1487 if (bdrv_op_is_blocked(state->old_bs,
1488 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
9b9877ee
WX
1489 return;
1490 }
1491
ba5d6ab6
SH
1492 if (!bdrv_is_read_only(state->old_bs)) {
1493 if (bdrv_flush(state->old_bs)) {
9b9877ee
WX
1494 error_set(errp, QERR_IO_ERROR);
1495 return;
1496 }
1497 }
1498
212a5a8f 1499 if (!bdrv_is_first_non_filter(state->old_bs)) {
f6186f49
BC
1500 error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
1501 return;
1502 }
1503
ba5d6ab6 1504 flags = state->old_bs->open_flags;
9b9877ee 1505
9b9877ee
WX
1506 /* create new image w/backing file */
1507 if (mode != NEW_IMAGE_MODE_EXISTING) {
1508 bdrv_img_create(new_image_file, format,
ba5d6ab6
SH
1509 state->old_bs->filename,
1510 state->old_bs->drv->format_name,
9b9877ee 1511 NULL, -1, flags, &local_err, false);
84d18f06 1512 if (local_err) {
9b9877ee
WX
1513 error_propagate(errp, local_err);
1514 return;
1515 }
1516 }
1517
0901f67e
BC
1518 if (has_snapshot_node_name) {
1519 options = qdict_new();
1520 qdict_put(options, "node-name",
1521 qstring_from_str(snapshot_node_name));
1522 }
1523
9b9877ee
WX
1524 /* TODO Inherit bs->options or only take explicit options with an
1525 * extended QMP command? */
f67503e5 1526 assert(state->new_bs == NULL);
ddf5636d 1527 ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
34b5d2c6 1528 flags | BDRV_O_NO_BACKING, drv, &local_err);
f67503e5 1529 /* We will manually add the backing_hd field to the bs later */
9b9877ee 1530 if (ret != 0) {
34b5d2c6 1531 error_propagate(errp, local_err);
9b9877ee
WX
1532 }
1533}
1534
ba5d6ab6 1535static void external_snapshot_commit(BlkTransactionState *common)
3b0047e8 1536{
ba5d6ab6
SH
1537 ExternalSnapshotState *state =
1538 DO_UPCAST(ExternalSnapshotState, common, common);
ba0c86a3 1539
5d6e96ef
SH
1540 bdrv_set_aio_context(state->new_bs, state->aio_context);
1541
ba5d6ab6
SH
1542 /* This removes our old bs and adds the new bs */
1543 bdrv_append(state->new_bs, state->old_bs);
3b0047e8
WX
1544 /* We don't need (or want) to use the transactional
1545 * bdrv_reopen_multiple() across all the entries at once, because we
1546 * don't want to abort all of them if one of them fails the reopen */
ba5d6ab6 1547 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
3b0047e8 1548 NULL);
5d6e96ef
SH
1549
1550 aio_context_release(state->aio_context);
3b0047e8
WX
1551}
1552
ba5d6ab6 1553static void external_snapshot_abort(BlkTransactionState *common)
96b86bf7 1554{
ba5d6ab6
SH
1555 ExternalSnapshotState *state =
1556 DO_UPCAST(ExternalSnapshotState, common, common);
1557 if (state->new_bs) {
4f6fd349 1558 bdrv_unref(state->new_bs);
96b86bf7 1559 }
5d6e96ef
SH
1560 if (state->aio_context) {
1561 aio_context_release(state->aio_context);
1562 }
96b86bf7
WX
1563}
1564
3037f364
SH
1565typedef struct DriveBackupState {
1566 BlkTransactionState common;
1567 BlockDriverState *bs;
5d6e96ef 1568 AioContext *aio_context;
3037f364
SH
1569 BlockJob *job;
1570} DriveBackupState;
1571
1572static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1573{
1574 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
5d6e96ef 1575 BlockDriverState *bs;
a0e8544c 1576 BlockBackend *blk;
3037f364
SH
1577 DriveBackup *backup;
1578 Error *local_err = NULL;
1579
1580 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1581 backup = common->action->drive_backup;
1582
a0e8544c
FZ
1583 blk = blk_by_name(backup->device);
1584 if (!blk) {
75158ebb
MA
1585 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1586 "Device '%s' not found", backup->device);
5d6e96ef
SH
1587 return;
1588 }
a0e8544c 1589 bs = blk_bs(blk);
5d6e96ef
SH
1590
1591 /* AioContext is released in .clean() */
1592 state->aio_context = bdrv_get_aio_context(bs);
1593 aio_context_acquire(state->aio_context);
1594
3037f364
SH
1595 qmp_drive_backup(backup->device, backup->target,
1596 backup->has_format, backup->format,
b53169ea 1597 backup->sync,
3037f364
SH
1598 backup->has_mode, backup->mode,
1599 backup->has_speed, backup->speed,
d58d8453 1600 backup->has_bitmap, backup->bitmap,
3037f364
SH
1601 backup->has_on_source_error, backup->on_source_error,
1602 backup->has_on_target_error, backup->on_target_error,
1603 &local_err);
84d18f06 1604 if (local_err) {
3037f364 1605 error_propagate(errp, local_err);
3037f364
SH
1606 return;
1607 }
1608
5d6e96ef 1609 state->bs = bs;
3037f364
SH
1610 state->job = state->bs->job;
1611}
1612
1613static void drive_backup_abort(BlkTransactionState *common)
1614{
1615 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1616 BlockDriverState *bs = state->bs;
1617
1618 /* Only cancel if it's the job we started */
1619 if (bs && bs->job && bs->job == state->job) {
1620 block_job_cancel_sync(bs->job);
1621 }
1622}
1623
5d6e96ef
SH
1624static void drive_backup_clean(BlkTransactionState *common)
1625{
1626 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1627
1628 if (state->aio_context) {
1629 aio_context_release(state->aio_context);
1630 }
1631}
1632
bd8baecd
FZ
1633typedef struct BlockdevBackupState {
1634 BlkTransactionState common;
1635 BlockDriverState *bs;
1636 BlockJob *job;
1637 AioContext *aio_context;
1638} BlockdevBackupState;
1639
1640static void blockdev_backup_prepare(BlkTransactionState *common, Error **errp)
1641{
1642 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1643 BlockdevBackup *backup;
1644 BlockDriverState *bs, *target;
a0e8544c 1645 BlockBackend *blk;
bd8baecd
FZ
1646 Error *local_err = NULL;
1647
1648 assert(common->action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1649 backup = common->action->blockdev_backup;
1650
a0e8544c
FZ
1651 blk = blk_by_name(backup->device);
1652 if (!blk) {
5b347c54 1653 error_setg(errp, "Device '%s' not found", backup->device);
bd8baecd
FZ
1654 return;
1655 }
a0e8544c 1656 bs = blk_bs(blk);
bd8baecd 1657
a0e8544c
FZ
1658 blk = blk_by_name(backup->target);
1659 if (!blk) {
5b347c54 1660 error_setg(errp, "Device '%s' not found", backup->target);
bd8baecd
FZ
1661 return;
1662 }
a0e8544c 1663 target = blk_bs(blk);
bd8baecd
FZ
1664
1665 /* AioContext is released in .clean() */
1666 state->aio_context = bdrv_get_aio_context(bs);
1667 if (state->aio_context != bdrv_get_aio_context(target)) {
1668 state->aio_context = NULL;
1669 error_setg(errp, "Backup between two IO threads is not implemented");
1670 return;
1671 }
1672 aio_context_acquire(state->aio_context);
1673
1674 qmp_blockdev_backup(backup->device, backup->target,
1675 backup->sync,
1676 backup->has_speed, backup->speed,
1677 backup->has_on_source_error, backup->on_source_error,
1678 backup->has_on_target_error, backup->on_target_error,
1679 &local_err);
1680 if (local_err) {
1681 error_propagate(errp, local_err);
1682 return;
1683 }
1684
1685 state->bs = bs;
1686 state->job = state->bs->job;
1687}
1688
1689static void blockdev_backup_abort(BlkTransactionState *common)
1690{
1691 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1692 BlockDriverState *bs = state->bs;
1693
1694 /* Only cancel if it's the job we started */
1695 if (bs && bs->job && bs->job == state->job) {
1696 block_job_cancel_sync(bs->job);
1697 }
1698}
1699
1700static void blockdev_backup_clean(BlkTransactionState *common)
1701{
1702 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1703
1704 if (state->aio_context) {
1705 aio_context_release(state->aio_context);
1706 }
1707}
1708
78b18b78
SH
1709static void abort_prepare(BlkTransactionState *common, Error **errp)
1710{
1711 error_setg(errp, "Transaction aborted using Abort action");
1712}
1713
1714static void abort_commit(BlkTransactionState *common)
1715{
dfc6f865 1716 g_assert_not_reached(); /* this action never succeeds */
78b18b78
SH
1717}
1718
ba0c86a3 1719static const BdrvActionOps actions[] = {
c8a83e85 1720 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
ba5d6ab6 1721 .instance_size = sizeof(ExternalSnapshotState),
ba0c86a3
WX
1722 .prepare = external_snapshot_prepare,
1723 .commit = external_snapshot_commit,
1724 .abort = external_snapshot_abort,
1725 },
3037f364
SH
1726 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1727 .instance_size = sizeof(DriveBackupState),
1728 .prepare = drive_backup_prepare,
1729 .abort = drive_backup_abort,
5d6e96ef 1730 .clean = drive_backup_clean,
3037f364 1731 },
bd8baecd
FZ
1732 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
1733 .instance_size = sizeof(BlockdevBackupState),
1734 .prepare = blockdev_backup_prepare,
1735 .abort = blockdev_backup_abort,
1736 .clean = blockdev_backup_clean,
1737 },
78b18b78
SH
1738 [TRANSACTION_ACTION_KIND_ABORT] = {
1739 .instance_size = sizeof(BlkTransactionState),
1740 .prepare = abort_prepare,
1741 .commit = abort_commit,
1742 },
bbe86010
WX
1743 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1744 .instance_size = sizeof(InternalSnapshotState),
1745 .prepare = internal_snapshot_prepare,
1746 .abort = internal_snapshot_abort,
5d6e96ef 1747 .clean = internal_snapshot_clean,
bbe86010 1748 },
ba0c86a3
WX
1749};
1750
8802d1fd 1751/*
b756b9ce
SH
1752 * 'Atomic' group operations. The operations are performed as a set, and if
1753 * any fail then we roll back all operations in the group.
8802d1fd 1754 */
c8a83e85 1755void qmp_transaction(TransactionActionList *dev_list, Error **errp)
8802d1fd 1756{
c8a83e85 1757 TransactionActionList *dev_entry = dev_list;
ba5d6ab6 1758 BlkTransactionState *state, *next;
43e17041 1759 Error *local_err = NULL;
8802d1fd 1760
ba5d6ab6 1761 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
8802d1fd
JC
1762 QSIMPLEQ_INIT(&snap_bdrv_states);
1763
b756b9ce 1764 /* drain all i/o before any operations */
8802d1fd
JC
1765 bdrv_drain_all();
1766
b756b9ce 1767 /* We don't do anything in this loop that commits us to the operations */
8802d1fd 1768 while (NULL != dev_entry) {
c8a83e85 1769 TransactionAction *dev_info = NULL;
ba0c86a3 1770 const BdrvActionOps *ops;
52e7c241 1771
8802d1fd
JC
1772 dev_info = dev_entry->value;
1773 dev_entry = dev_entry->next;
1774
ba0c86a3
WX
1775 assert(dev_info->kind < ARRAY_SIZE(actions));
1776
1777 ops = &actions[dev_info->kind];
aa3fe714
HR
1778 assert(ops->instance_size > 0);
1779
ba5d6ab6
SH
1780 state = g_malloc0(ops->instance_size);
1781 state->ops = ops;
1782 state->action = dev_info;
1783 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
8802d1fd 1784
ba5d6ab6 1785 state->ops->prepare(state, &local_err);
84d18f06 1786 if (local_err) {
ba0c86a3
WX
1787 error_propagate(errp, local_err);
1788 goto delete_and_fail;
8802d1fd 1789 }
8802d1fd
JC
1790 }
1791
ba5d6ab6 1792 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
f9ea81e8
SH
1793 if (state->ops->commit) {
1794 state->ops->commit(state);
1795 }
8802d1fd
JC
1796 }
1797
1798 /* success */
1799 goto exit;
1800
1801delete_and_fail:
b756b9ce 1802 /* failure, and it is all-or-none; roll back all operations */
ba5d6ab6
SH
1803 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1804 if (state->ops->abort) {
1805 state->ops->abort(state);
ba0c86a3 1806 }
8802d1fd
JC
1807 }
1808exit:
ba5d6ab6
SH
1809 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1810 if (state->ops->clean) {
1811 state->ops->clean(state);
ba0c86a3 1812 }
ba5d6ab6 1813 g_free(state);
8802d1fd 1814 }
8802d1fd
JC
1815}
1816
1817
6007cdd4 1818static void eject_device(BlockBackend *blk, int force, Error **errp)
666daa68 1819{
6007cdd4 1820 BlockDriverState *bs = blk_bs(blk);
e3442099
SH
1821 AioContext *aio_context;
1822
1823 aio_context = bdrv_get_aio_context(bs);
1824 aio_context_acquire(aio_context);
6007cdd4 1825
3718d8ab 1826 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
e3442099 1827 goto out;
2d3735d3 1828 }
a7f53e26 1829 if (!blk_dev_has_removable_media(blk)) {
f231b88d
CR
1830 error_setg(errp, "Device '%s' is not removable",
1831 bdrv_get_device_name(bs));
e3442099 1832 goto out;
ea8f942f 1833 }
92d48558 1834
a7f53e26
MA
1835 if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) {
1836 blk_dev_eject_request(blk, force);
025ccaa7 1837 if (!force) {
f231b88d
CR
1838 error_setg(errp, "Device '%s' is locked",
1839 bdrv_get_device_name(bs));
e3442099 1840 goto out;
025ccaa7 1841 }
666daa68 1842 }
92d48558 1843
3b5276b5 1844 bdrv_close(bs);
e3442099
SH
1845
1846out:
1847 aio_context_release(aio_context);
666daa68
MA
1848}
1849
c245b6a3 1850void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
666daa68 1851{
6007cdd4 1852 BlockBackend *blk;
666daa68 1853
6007cdd4
MA
1854 blk = blk_by_name(device);
1855 if (!blk) {
75158ebb
MA
1856 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1857 "Device '%s' not found", device);
c245b6a3 1858 return;
92d48558
LC
1859 }
1860
6007cdd4 1861 eject_device(blk, force, errp);
666daa68
MA
1862}
1863
12d3ba82
BC
1864void qmp_block_passwd(bool has_device, const char *device,
1865 bool has_node_name, const char *node_name,
1866 const char *password, Error **errp)
666daa68 1867{
12d3ba82 1868 Error *local_err = NULL;
666daa68 1869 BlockDriverState *bs;
e3442099 1870 AioContext *aio_context;
666daa68 1871
12d3ba82
BC
1872 bs = bdrv_lookup_bs(has_device ? device : NULL,
1873 has_node_name ? node_name : NULL,
1874 &local_err);
84d18f06 1875 if (local_err) {
12d3ba82 1876 error_propagate(errp, local_err);
a4dea8a9 1877 return;
666daa68
MA
1878 }
1879
e3442099
SH
1880 aio_context = bdrv_get_aio_context(bs);
1881 aio_context_acquire(aio_context);
1882
4d2855a3 1883 bdrv_add_key(bs, password, errp);
e3442099 1884
e3442099 1885 aio_context_release(aio_context);
666daa68
MA
1886}
1887
e3442099 1888/* Assumes AioContext is held */
333a96ec
LC
1889static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1890 int bdrv_flags, BlockDriver *drv,
1891 const char *password, Error **errp)
1892{
34b5d2c6 1893 Error *local_err = NULL;
0eef407c
LC
1894 int ret;
1895
ddf5636d 1896 ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err);
0eef407c 1897 if (ret < 0) {
34b5d2c6 1898 error_propagate(errp, local_err);
333a96ec
LC
1899 return;
1900 }
1901
4d2855a3 1902 bdrv_add_key(bs, password, errp);
333a96ec
LC
1903}
1904
1905void qmp_change_blockdev(const char *device, const char *filename,
314f7ea7 1906 const char *format, Error **errp)
666daa68 1907{
6007cdd4 1908 BlockBackend *blk;
666daa68 1909 BlockDriverState *bs;
e3442099 1910 AioContext *aio_context;
666daa68
MA
1911 BlockDriver *drv = NULL;
1912 int bdrv_flags;
92d48558 1913 Error *err = NULL;
666daa68 1914
6007cdd4
MA
1915 blk = blk_by_name(device);
1916 if (!blk) {
75158ebb
MA
1917 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1918 "Device '%s' not found", device);
333a96ec 1919 return;
666daa68 1920 }
6007cdd4 1921 bs = blk_bs(blk);
333a96ec 1922
e3442099
SH
1923 aio_context = bdrv_get_aio_context(bs);
1924 aio_context_acquire(aio_context);
1925
333a96ec 1926 if (format) {
b64ec4e4 1927 drv = bdrv_find_whitelisted_format(format, bs->read_only);
666daa68 1928 if (!drv) {
333a96ec 1929 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
e3442099 1930 goto out;
666daa68
MA
1931 }
1932 }
333a96ec 1933
6007cdd4 1934 eject_device(blk, 0, &err);
84d18f06 1935 if (err) {
333a96ec 1936 error_propagate(errp, err);
e3442099 1937 goto out;
666daa68 1938 }
333a96ec 1939
528f7663 1940 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
199630b6 1941 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
333a96ec
LC
1942
1943 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
e3442099
SH
1944
1945out:
1946 aio_context_release(aio_context);
666daa68 1947}
9063f814 1948
727f005e 1949/* throttling disk I/O limits */
80047da5 1950void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
3e9fab69
BC
1951 int64_t bps_wr,
1952 int64_t iops,
1953 int64_t iops_rd,
1954 int64_t iops_wr,
1955 bool has_bps_max,
1956 int64_t bps_max,
1957 bool has_bps_rd_max,
1958 int64_t bps_rd_max,
1959 bool has_bps_wr_max,
1960 int64_t bps_wr_max,
1961 bool has_iops_max,
1962 int64_t iops_max,
1963 bool has_iops_rd_max,
1964 int64_t iops_rd_max,
1965 bool has_iops_wr_max,
2024c1df
BC
1966 int64_t iops_wr_max,
1967 bool has_iops_size,
76f4afb4
AG
1968 int64_t iops_size,
1969 bool has_group,
1970 const char *group, Error **errp)
727f005e 1971{
cc0681c4 1972 ThrottleConfig cfg;
727f005e 1973 BlockDriverState *bs;
a0e8544c 1974 BlockBackend *blk;
b15446fd 1975 AioContext *aio_context;
727f005e 1976
a0e8544c
FZ
1977 blk = blk_by_name(device);
1978 if (!blk) {
75158ebb
MA
1979 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1980 "Device '%s' not found", device);
80047da5 1981 return;
727f005e 1982 }
a0e8544c 1983 bs = blk_bs(blk);
727f005e 1984
cc0681c4
BC
1985 memset(&cfg, 0, sizeof(cfg));
1986 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1987 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
1988 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
1989
1990 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1991 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
1992 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
1993
3e9fab69
BC
1994 if (has_bps_max) {
1995 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
1996 }
1997 if (has_bps_rd_max) {
1998 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
1999 }
2000 if (has_bps_wr_max) {
2001 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2002 }
2003 if (has_iops_max) {
2004 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2005 }
2006 if (has_iops_rd_max) {
2007 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2008 }
2009 if (has_iops_wr_max) {
2010 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2011 }
727f005e 2012
2024c1df
BC
2013 if (has_iops_size) {
2014 cfg.op_size = iops_size;
2015 }
cc0681c4
BC
2016
2017 if (!check_throttle_config(&cfg, errp)) {
80047da5 2018 return;
727f005e
ZYW
2019 }
2020
b15446fd
SH
2021 aio_context = bdrv_get_aio_context(bs);
2022 aio_context_acquire(aio_context);
2023
76f4afb4
AG
2024 if (throttle_enabled(&cfg)) {
2025 /* Enable I/O limits if they're not enabled yet, otherwise
2026 * just update the throttling group. */
2027 if (!bs->io_limits_enabled) {
2028 bdrv_io_limits_enable(bs, has_group ? group : device);
2029 } else if (has_group) {
2030 bdrv_io_limits_update_group(bs, group);
2031 }
2032 /* Set the new throttling configuration */
cc0681c4 2033 bdrv_set_io_limits(bs, &cfg);
76f4afb4
AG
2034 } else if (bs->io_limits_enabled) {
2035 /* If all throttling settings are set to 0, disable I/O limits */
2036 bdrv_io_limits_disable(bs);
727f005e 2037 }
b15446fd
SH
2038
2039 aio_context_release(aio_context);
727f005e
ZYW
2040}
2041
341ebc2f
JS
2042void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2043 bool has_granularity, uint32_t granularity,
2044 Error **errp)
2045{
2046 AioContext *aio_context;
2047 BlockDriverState *bs;
2048
2049 if (!name || name[0] == '\0') {
2050 error_setg(errp, "Bitmap name cannot be empty");
2051 return;
2052 }
2053
2054 bs = bdrv_lookup_bs(node, node, errp);
2055 if (!bs) {
2056 return;
2057 }
2058
2059 aio_context = bdrv_get_aio_context(bs);
2060 aio_context_acquire(aio_context);
2061
2062 if (has_granularity) {
2063 if (granularity < 512 || !is_power_of_2(granularity)) {
2064 error_setg(errp, "Granularity must be power of 2 "
2065 "and at least 512");
2066 goto out;
2067 }
2068 } else {
2069 /* Default to cluster size, if available: */
2070 granularity = bdrv_get_default_bitmap_granularity(bs);
2071 }
2072
2073 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2074
2075 out:
2076 aio_context_release(aio_context);
2077}
2078
2079void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2080 Error **errp)
2081{
2082 AioContext *aio_context;
2083 BlockDriverState *bs;
2084 BdrvDirtyBitmap *bitmap;
2085
2086 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2087 if (!bitmap || !bs) {
2088 return;
2089 }
2090
9bd2b08f
JS
2091 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2092 error_setg(errp,
2093 "Bitmap '%s' is currently frozen and cannot be removed",
2094 name);
2095 goto out;
2096 }
20dca810 2097 bdrv_dirty_bitmap_make_anon(bitmap);
341ebc2f
JS
2098 bdrv_release_dirty_bitmap(bs, bitmap);
2099
9bd2b08f 2100 out:
341ebc2f
JS
2101 aio_context_release(aio_context);
2102}
2103
e74e6b78
JS
2104/**
2105 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2106 * immediately after a full backup operation.
2107 */
2108void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2109 Error **errp)
2110{
2111 AioContext *aio_context;
2112 BdrvDirtyBitmap *bitmap;
2113 BlockDriverState *bs;
2114
2115 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2116 if (!bitmap || !bs) {
2117 return;
2118 }
2119
2120 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2121 error_setg(errp,
2122 "Bitmap '%s' is currently frozen and cannot be modified",
2123 name);
2124 goto out;
2125 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2126 error_setg(errp,
2127 "Bitmap '%s' is currently disabled and cannot be cleared",
2128 name);
2129 goto out;
2130 }
2131
2132 bdrv_clear_dirty_bitmap(bitmap);
2133
2134 out:
2135 aio_context_release(aio_context);
2136}
2137
072ebe6b 2138void hmp_drive_del(Monitor *mon, const QDict *qdict)
9063f814
RH
2139{
2140 const char *id = qdict_get_str(qdict, "id");
7e7d56d9 2141 BlockBackend *blk;
9063f814 2142 BlockDriverState *bs;
8ad4202b 2143 AioContext *aio_context;
3718d8ab 2144 Error *local_err = NULL;
9063f814 2145
7e7d56d9
MA
2146 blk = blk_by_name(id);
2147 if (!blk) {
b1422f20 2148 error_report("Device '%s' not found", id);
072ebe6b 2149 return;
9063f814 2150 }
7e7d56d9 2151 bs = blk_bs(blk);
8ad4202b 2152
26f8b3a8 2153 if (!blk_legacy_dinfo(blk)) {
48f364dd
MA
2154 error_report("Deleting device added with blockdev-add"
2155 " is not supported");
072ebe6b 2156 return;
48f364dd
MA
2157 }
2158
8ad4202b
SH
2159 aio_context = bdrv_get_aio_context(bs);
2160 aio_context_acquire(aio_context);
2161
3718d8ab 2162 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
565f65d2 2163 error_report_err(local_err);
8ad4202b 2164 aio_context_release(aio_context);
072ebe6b 2165 return;
8591675f 2166 }
9063f814
RH
2167
2168 /* quiesce block driver; prevent further io */
922453bc 2169 bdrv_drain_all();
9063f814
RH
2170 bdrv_flush(bs);
2171 bdrv_close(bs);
2172
fa879d62 2173 /* if we have a device attached to this BlockDriverState
d22b2f41
RH
2174 * then we need to make the drive anonymous until the device
2175 * can be removed. If this is a drive with no device backing
2176 * then we can just get rid of the block driver state right here.
2177 */
a7f53e26 2178 if (blk_get_attached_dev(blk)) {
3e5a50d6 2179 blk_hide_on_behalf_of_hmp_drive_del(blk);
293c51a6
SH
2180 /* Further I/O must not pause the guest */
2181 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
2182 BLOCKDEV_ON_ERROR_REPORT);
d22b2f41 2183 } else {
b9fe8a7a 2184 blk_unref(blk);
9063f814
RH
2185 }
2186
8ad4202b 2187 aio_context_release(aio_context);
9063f814 2188}
6d4a2b3a 2189
3b1dbd11
BC
2190void qmp_block_resize(bool has_device, const char *device,
2191 bool has_node_name, const char *node_name,
2192 int64_t size, Error **errp)
6d4a2b3a 2193{
3b1dbd11 2194 Error *local_err = NULL;
6d4a2b3a 2195 BlockDriverState *bs;
927e0e76 2196 AioContext *aio_context;
8732901e 2197 int ret;
6d4a2b3a 2198
3b1dbd11
BC
2199 bs = bdrv_lookup_bs(has_device ? device : NULL,
2200 has_node_name ? node_name : NULL,
2201 &local_err);
84d18f06 2202 if (local_err) {
3b1dbd11
BC
2203 error_propagate(errp, local_err);
2204 return;
2205 }
2206
927e0e76
SH
2207 aio_context = bdrv_get_aio_context(bs);
2208 aio_context_acquire(aio_context);
2209
3b1dbd11
BC
2210 if (!bdrv_is_first_non_filter(bs)) {
2211 error_set(errp, QERR_FEATURE_DISABLED, "resize");
927e0e76 2212 goto out;
6d4a2b3a
CH
2213 }
2214
2215 if (size < 0) {
939a1cc3 2216 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
927e0e76 2217 goto out;
6d4a2b3a
CH
2218 }
2219
9c75e168
JC
2220 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2221 error_set(errp, QERR_DEVICE_IN_USE, device);
927e0e76 2222 goto out;
9c75e168
JC
2223 }
2224
92b7a08d
PL
2225 /* complete all in-flight operations before resizing the device */
2226 bdrv_drain_all();
2227
8732901e
KW
2228 ret = bdrv_truncate(bs, size);
2229 switch (ret) {
939a1cc3
SH
2230 case 0:
2231 break;
2232 case -ENOMEDIUM:
2233 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2234 break;
2235 case -ENOTSUP:
2236 error_set(errp, QERR_UNSUPPORTED);
2237 break;
2238 case -EACCES:
81e5f78a 2239 error_setg(errp, "Device '%s' is read only", device);
939a1cc3
SH
2240 break;
2241 case -EBUSY:
2242 error_set(errp, QERR_DEVICE_IN_USE, device);
2243 break;
2244 default:
8732901e 2245 error_setg_errno(errp, -ret, "Could not resize");
939a1cc3 2246 break;
6d4a2b3a 2247 }
927e0e76
SH
2248
2249out:
2250 aio_context_release(aio_context);
6d4a2b3a 2251}
12bd451f 2252
9abf2dba 2253static void block_job_cb(void *opaque, int ret)
12bd451f 2254{
723c5d93
SH
2255 /* Note that this function may be executed from another AioContext besides
2256 * the QEMU main loop. If you need to access anything that assumes the
2257 * QEMU global mutex, use a BH or introduce a mutex.
2258 */
2259
12bd451f 2260 BlockDriverState *bs = opaque;
bcada37b 2261 const char *msg = NULL;
12bd451f 2262
9abf2dba 2263 trace_block_job_cb(bs, bs->job, ret);
12bd451f
SH
2264
2265 assert(bs->job);
bcada37b 2266
12bd451f 2267 if (ret < 0) {
bcada37b 2268 msg = strerror(-ret);
12bd451f
SH
2269 }
2270
370521a1 2271 if (block_job_is_cancelled(bs->job)) {
bcada37b 2272 block_job_event_cancelled(bs->job);
370521a1 2273 } else {
bcada37b 2274 block_job_event_completed(bs->job, msg);
370521a1 2275 }
aa398a5c 2276
fa510ebf 2277 bdrv_put_ref_bh_schedule(bs);
12bd451f
SH
2278}
2279
13d8cc51
JC
2280void qmp_block_stream(const char *device,
2281 bool has_base, const char *base,
2282 bool has_backing_file, const char *backing_file,
2283 bool has_speed, int64_t speed,
1d809098
PB
2284 bool has_on_error, BlockdevOnError on_error,
2285 Error **errp)
12bd451f 2286{
a0e8544c 2287 BlockBackend *blk;
12bd451f 2288 BlockDriverState *bs;
c8c3080f 2289 BlockDriverState *base_bs = NULL;
f3e69beb 2290 AioContext *aio_context;
fd7f8c65 2291 Error *local_err = NULL;
13d8cc51 2292 const char *base_name = NULL;
12bd451f 2293
1d809098
PB
2294 if (!has_on_error) {
2295 on_error = BLOCKDEV_ON_ERROR_REPORT;
2296 }
2297
a0e8544c
FZ
2298 blk = blk_by_name(device);
2299 if (!blk) {
75158ebb
MA
2300 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2301 "Device '%s' not found", device);
12bd451f
SH
2302 return;
2303 }
a0e8544c 2304 bs = blk_bs(blk);
12bd451f 2305
f3e69beb
SH
2306 aio_context = bdrv_get_aio_context(bs);
2307 aio_context_acquire(aio_context);
2308
628ff683 2309 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
f3e69beb 2310 goto out;
628ff683
FZ
2311 }
2312
13d8cc51 2313 if (has_base) {
c8c3080f
MT
2314 base_bs = bdrv_find_backing_image(bs, base);
2315 if (base_bs == NULL) {
2316 error_set(errp, QERR_BASE_NOT_FOUND, base);
f3e69beb 2317 goto out;
c8c3080f 2318 }
f3e69beb 2319 assert(bdrv_get_aio_context(base_bs) == aio_context);
13d8cc51 2320 base_name = base;
12bd451f
SH
2321 }
2322
13d8cc51
JC
2323 /* if we are streaming the entire chain, the result will have no backing
2324 * file, and specifying one is therefore an error */
2325 if (base_bs == NULL && has_backing_file) {
2326 error_setg(errp, "backing file specified, but streaming the "
2327 "entire chain");
f3e69beb 2328 goto out;
13d8cc51
JC
2329 }
2330
2331 /* backing_file string overrides base bs filename */
2332 base_name = has_backing_file ? backing_file : base_name;
2333
2334 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
1d809098 2335 on_error, block_job_cb, bs, &local_err);
84d18f06 2336 if (local_err) {
fd7f8c65 2337 error_propagate(errp, local_err);
f3e69beb 2338 goto out;
12bd451f
SH
2339 }
2340
2341 trace_qmp_block_stream(bs, bs->job);
f3e69beb
SH
2342
2343out:
2344 aio_context_release(aio_context);
12bd451f 2345}
2d47c6e9 2346
ed61fc10 2347void qmp_block_commit(const char *device,
7676e2c5
JC
2348 bool has_base, const char *base,
2349 bool has_top, const char *top,
54e26900 2350 bool has_backing_file, const char *backing_file,
ed61fc10
JC
2351 bool has_speed, int64_t speed,
2352 Error **errp)
2353{
a0e8544c 2354 BlockBackend *blk;
ed61fc10
JC
2355 BlockDriverState *bs;
2356 BlockDriverState *base_bs, *top_bs;
9e85cd5c 2357 AioContext *aio_context;
ed61fc10
JC
2358 Error *local_err = NULL;
2359 /* This will be part of the QMP command, if/when the
2360 * BlockdevOnError change for blkmirror makes it in
2361 */
92aa5c6d 2362 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
ed61fc10 2363
54504663
HR
2364 if (!has_speed) {
2365 speed = 0;
2366 }
2367
7676e2c5
JC
2368 /* Important Note:
2369 * libvirt relies on the DeviceNotFound error class in order to probe for
2370 * live commit feature versions; for this to work, we must make sure to
2371 * perform the device lookup before any generic errors that may occur in a
2372 * scenario in which all optional arguments are omitted. */
a0e8544c
FZ
2373 blk = blk_by_name(device);
2374 if (!blk) {
75158ebb
MA
2375 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2376 "Device '%s' not found", device);
ed61fc10 2377 return;
628ff683 2378 }
a0e8544c 2379 bs = blk_bs(blk);
628ff683 2380
9e85cd5c
SH
2381 aio_context = bdrv_get_aio_context(bs);
2382 aio_context_acquire(aio_context);
2383
2384 /* drain all i/o before commits */
2385 bdrv_drain_all();
2386
bb00021d 2387 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
9e85cd5c 2388 goto out;
ed61fc10 2389 }
ed61fc10
JC
2390
2391 /* default top_bs is the active layer */
2392 top_bs = bs;
2393
7676e2c5 2394 if (has_top && top) {
ed61fc10
JC
2395 if (strcmp(bs->filename, top) != 0) {
2396 top_bs = bdrv_find_backing_image(bs, top);
2397 }
2398 }
2399
2400 if (top_bs == NULL) {
2401 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
9e85cd5c 2402 goto out;
ed61fc10
JC
2403 }
2404
9e85cd5c
SH
2405 assert(bdrv_get_aio_context(top_bs) == aio_context);
2406
d5208c45
JC
2407 if (has_base && base) {
2408 base_bs = bdrv_find_backing_image(top_bs, base);
2409 } else {
2410 base_bs = bdrv_find_base(top_bs);
2411 }
2412
2413 if (base_bs == NULL) {
2414 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
9e85cd5c 2415 goto out;
d5208c45
JC
2416 }
2417
9e85cd5c
SH
2418 assert(bdrv_get_aio_context(base_bs) == aio_context);
2419
bb00021d
FZ
2420 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2421 goto out;
2422 }
2423
7676e2c5
JC
2424 /* Do not allow attempts to commit an image into itself */
2425 if (top_bs == base_bs) {
2426 error_setg(errp, "cannot commit an image into itself");
9e85cd5c 2427 goto out;
7676e2c5
JC
2428 }
2429
20a63d2c 2430 if (top_bs == bs) {
54e26900
JC
2431 if (has_backing_file) {
2432 error_setg(errp, "'backing-file' specified,"
2433 " but 'top' is the active layer");
9e85cd5c 2434 goto out;
54e26900 2435 }
20a63d2c
FZ
2436 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
2437 bs, &local_err);
2438 } else {
2439 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
54e26900 2440 has_backing_file ? backing_file : NULL, &local_err);
20a63d2c 2441 }
ed61fc10
JC
2442 if (local_err != NULL) {
2443 error_propagate(errp, local_err);
9e85cd5c 2444 goto out;
ed61fc10 2445 }
9e85cd5c
SH
2446
2447out:
2448 aio_context_release(aio_context);
ed61fc10
JC
2449}
2450
99a9addf
SH
2451void qmp_drive_backup(const char *device, const char *target,
2452 bool has_format, const char *format,
b53169ea 2453 enum MirrorSyncMode sync,
99a9addf
SH
2454 bool has_mode, enum NewImageMode mode,
2455 bool has_speed, int64_t speed,
d58d8453 2456 bool has_bitmap, const char *bitmap,
99a9addf
SH
2457 bool has_on_source_error, BlockdevOnError on_source_error,
2458 bool has_on_target_error, BlockdevOnError on_target_error,
2459 Error **errp)
2460{
a0e8544c 2461 BlockBackend *blk;
99a9addf
SH
2462 BlockDriverState *bs;
2463 BlockDriverState *target_bs;
fc5d3f84 2464 BlockDriverState *source = NULL;
d58d8453 2465 BdrvDirtyBitmap *bmap = NULL;
761731b1 2466 AioContext *aio_context;
99a9addf
SH
2467 BlockDriver *drv = NULL;
2468 Error *local_err = NULL;
2469 int flags;
2470 int64_t size;
2471 int ret;
2472
2473 if (!has_speed) {
2474 speed = 0;
2475 }
2476 if (!has_on_source_error) {
2477 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2478 }
2479 if (!has_on_target_error) {
2480 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2481 }
2482 if (!has_mode) {
2483 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2484 }
2485
a0e8544c
FZ
2486 blk = blk_by_name(device);
2487 if (!blk) {
75158ebb
MA
2488 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2489 "Device '%s' not found", device);
99a9addf
SH
2490 return;
2491 }
a0e8544c 2492 bs = blk_bs(blk);
99a9addf 2493
761731b1
SH
2494 aio_context = bdrv_get_aio_context(bs);
2495 aio_context_acquire(aio_context);
2496
c29c1dd3
FZ
2497 /* Although backup_run has this check too, we need to use bs->drv below, so
2498 * do an early check redundantly. */
99a9addf
SH
2499 if (!bdrv_is_inserted(bs)) {
2500 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
761731b1 2501 goto out;
99a9addf
SH
2502 }
2503
2504 if (!has_format) {
2505 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2506 }
2507 if (format) {
2508 drv = bdrv_find_format(format);
2509 if (!drv) {
2510 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
761731b1 2511 goto out;
99a9addf
SH
2512 }
2513 }
2514
c29c1dd3 2515 /* Early check to avoid creating target */
3718d8ab 2516 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
761731b1 2517 goto out;
99a9addf
SH
2518 }
2519
2520 flags = bs->open_flags | BDRV_O_RDWR;
2521
fc5d3f84
IM
2522 /* See if we have a backing HD we can use to create our new image
2523 * on top of. */
2524 if (sync == MIRROR_SYNC_MODE_TOP) {
2525 source = bs->backing_hd;
2526 if (!source) {
2527 sync = MIRROR_SYNC_MODE_FULL;
2528 }
2529 }
2530 if (sync == MIRROR_SYNC_MODE_NONE) {
2531 source = bs;
2532 }
2533
99a9addf
SH
2534 size = bdrv_getlength(bs);
2535 if (size < 0) {
2536 error_setg_errno(errp, -size, "bdrv_getlength failed");
761731b1 2537 goto out;
99a9addf
SH
2538 }
2539
2540 if (mode != NEW_IMAGE_MODE_EXISTING) {
2541 assert(format && drv);
fc5d3f84
IM
2542 if (source) {
2543 bdrv_img_create(target, format, source->filename,
2544 source->drv->format_name, NULL,
2545 size, flags, &local_err, false);
2546 } else {
2547 bdrv_img_create(target, format, NULL, NULL, NULL,
2548 size, flags, &local_err, false);
2549 }
99a9addf
SH
2550 }
2551
84d18f06 2552 if (local_err) {
99a9addf 2553 error_propagate(errp, local_err);
761731b1 2554 goto out;
99a9addf
SH
2555 }
2556
f67503e5 2557 target_bs = NULL;
ddf5636d 2558 ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err);
99a9addf 2559 if (ret < 0) {
34b5d2c6 2560 error_propagate(errp, local_err);
761731b1 2561 goto out;
99a9addf
SH
2562 }
2563
761731b1
SH
2564 bdrv_set_aio_context(target_bs, aio_context);
2565
d58d8453
JS
2566 if (has_bitmap) {
2567 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
2568 if (!bmap) {
2569 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
2570 goto out;
2571 }
2572 }
2573
2574 backup_start(bs, target_bs, speed, sync, bmap,
2575 on_source_error, on_target_error,
99a9addf
SH
2576 block_job_cb, bs, &local_err);
2577 if (local_err != NULL) {
4f6fd349 2578 bdrv_unref(target_bs);
99a9addf 2579 error_propagate(errp, local_err);
761731b1 2580 goto out;
99a9addf 2581 }
761731b1
SH
2582
2583out:
2584 aio_context_release(aio_context);
99a9addf
SH
2585}
2586
c13163fb
BC
2587BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
2588{
d5a8ee60 2589 return bdrv_named_nodes_list(errp);
c13163fb
BC
2590}
2591
c29c1dd3
FZ
2592void qmp_blockdev_backup(const char *device, const char *target,
2593 enum MirrorSyncMode sync,
2594 bool has_speed, int64_t speed,
2595 bool has_on_source_error,
2596 BlockdevOnError on_source_error,
2597 bool has_on_target_error,
2598 BlockdevOnError on_target_error,
2599 Error **errp)
2600{
a0e8544c 2601 BlockBackend *blk;
c29c1dd3
FZ
2602 BlockDriverState *bs;
2603 BlockDriverState *target_bs;
2604 Error *local_err = NULL;
2605 AioContext *aio_context;
2606
2607 if (!has_speed) {
2608 speed = 0;
2609 }
2610 if (!has_on_source_error) {
2611 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2612 }
2613 if (!has_on_target_error) {
2614 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2615 }
2616
a0e8544c
FZ
2617 blk = blk_by_name(device);
2618 if (!blk) {
5b347c54 2619 error_setg(errp, "Device '%s' not found", device);
c29c1dd3
FZ
2620 return;
2621 }
a0e8544c 2622 bs = blk_bs(blk);
c29c1dd3
FZ
2623
2624 aio_context = bdrv_get_aio_context(bs);
2625 aio_context_acquire(aio_context);
2626
a0e8544c
FZ
2627 blk = blk_by_name(target);
2628 if (!blk) {
5b347c54 2629 error_setg(errp, "Device '%s' not found", target);
c29c1dd3
FZ
2630 goto out;
2631 }
a0e8544c 2632 target_bs = blk_bs(blk);
c29c1dd3
FZ
2633
2634 bdrv_ref(target_bs);
2635 bdrv_set_aio_context(target_bs, aio_context);
d58d8453
JS
2636 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
2637 on_target_error, block_job_cb, bs, &local_err);
c29c1dd3
FZ
2638 if (local_err != NULL) {
2639 bdrv_unref(target_bs);
2640 error_propagate(errp, local_err);
2641 }
2642out:
2643 aio_context_release(aio_context);
2644}
2645
08e4ed6c
PB
2646#define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2647
d9b902db
PB
2648void qmp_drive_mirror(const char *device, const char *target,
2649 bool has_format, const char *format,
4c828dc6 2650 bool has_node_name, const char *node_name,
09158f00 2651 bool has_replaces, const char *replaces,
d9b902db
PB
2652 enum MirrorSyncMode sync,
2653 bool has_mode, enum NewImageMode mode,
b952b558 2654 bool has_speed, int64_t speed,
eee13dfe 2655 bool has_granularity, uint32_t granularity,
08e4ed6c 2656 bool has_buf_size, int64_t buf_size,
b952b558
PB
2657 bool has_on_source_error, BlockdevOnError on_source_error,
2658 bool has_on_target_error, BlockdevOnError on_target_error,
2659 Error **errp)
d9b902db 2660{
a0e8544c 2661 BlockBackend *blk;
d9b902db
PB
2662 BlockDriverState *bs;
2663 BlockDriverState *source, *target_bs;
5a7e7a0b 2664 AioContext *aio_context;
d9b902db
PB
2665 BlockDriver *drv = NULL;
2666 Error *local_err = NULL;
4c828dc6 2667 QDict *options = NULL;
d9b902db 2668 int flags;
ac3c5d83 2669 int64_t size;
d9b902db
PB
2670 int ret;
2671
2672 if (!has_speed) {
2673 speed = 0;
2674 }
b952b558
PB
2675 if (!has_on_source_error) {
2676 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2677 }
2678 if (!has_on_target_error) {
2679 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2680 }
d9b902db
PB
2681 if (!has_mode) {
2682 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2683 }
eee13dfe
PB
2684 if (!has_granularity) {
2685 granularity = 0;
2686 }
08e4ed6c
PB
2687 if (!has_buf_size) {
2688 buf_size = DEFAULT_MIRROR_BUF_SIZE;
2689 }
2690
eee13dfe 2691 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3cbbe9fd
SH
2692 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2693 "a value in range [512B, 64MB]");
eee13dfe
PB
2694 return;
2695 }
2696 if (granularity & (granularity - 1)) {
3cbbe9fd 2697 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2");
eee13dfe
PB
2698 return;
2699 }
d9b902db 2700
a0e8544c
FZ
2701 blk = blk_by_name(device);
2702 if (!blk) {
75158ebb
MA
2703 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2704 "Device '%s' not found", device);
d9b902db
PB
2705 return;
2706 }
a0e8544c 2707 bs = blk_bs(blk);
d9b902db 2708
5a7e7a0b
SH
2709 aio_context = bdrv_get_aio_context(bs);
2710 aio_context_acquire(aio_context);
2711
d9b902db
PB
2712 if (!bdrv_is_inserted(bs)) {
2713 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
5a7e7a0b 2714 goto out;
d9b902db
PB
2715 }
2716
2717 if (!has_format) {
2718 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2719 }
2720 if (format) {
2721 drv = bdrv_find_format(format);
2722 if (!drv) {
2723 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
5a7e7a0b 2724 goto out;
d9b902db
PB
2725 }
2726 }
2727
3718d8ab 2728 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
5a7e7a0b 2729 goto out;
d9b902db
PB
2730 }
2731
2732 flags = bs->open_flags | BDRV_O_RDWR;
2733 source = bs->backing_hd;
2734 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
2735 sync = MIRROR_SYNC_MODE_FULL;
2736 }
117e0c82
HR
2737 if (sync == MIRROR_SYNC_MODE_NONE) {
2738 source = bs;
2739 }
d9b902db 2740
ac3c5d83
SH
2741 size = bdrv_getlength(bs);
2742 if (size < 0) {
2743 error_setg_errno(errp, -size, "bdrv_getlength failed");
5a7e7a0b 2744 goto out;
ac3c5d83
SH
2745 }
2746
09158f00
BC
2747 if (has_replaces) {
2748 BlockDriverState *to_replace_bs;
5a7e7a0b
SH
2749 AioContext *replace_aio_context;
2750 int64_t replace_size;
09158f00
BC
2751
2752 if (!has_node_name) {
2753 error_setg(errp, "a node-name must be provided when replacing a"
2754 " named node of the graph");
5a7e7a0b 2755 goto out;
09158f00
BC
2756 }
2757
2758 to_replace_bs = check_to_replace_node(replaces, &local_err);
2759
2760 if (!to_replace_bs) {
2761 error_propagate(errp, local_err);
5a7e7a0b 2762 goto out;
09158f00
BC
2763 }
2764
5a7e7a0b
SH
2765 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
2766 aio_context_acquire(replace_aio_context);
2767 replace_size = bdrv_getlength(to_replace_bs);
2768 aio_context_release(replace_aio_context);
2769
2770 if (size != replace_size) {
09158f00
BC
2771 error_setg(errp, "cannot replace image with a mirror image of "
2772 "different size");
5a7e7a0b 2773 goto out;
09158f00
BC
2774 }
2775 }
2776
14526864
HR
2777 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
2778 && mode != NEW_IMAGE_MODE_EXISTING)
2779 {
d9b902db
PB
2780 /* create new image w/o backing file */
2781 assert(format && drv);
cf8f2426 2782 bdrv_img_create(target, format,
f382d43a 2783 NULL, NULL, NULL, size, flags, &local_err, false);
d9b902db
PB
2784 } else {
2785 switch (mode) {
2786 case NEW_IMAGE_MODE_EXISTING:
d9b902db
PB
2787 break;
2788 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2789 /* create new image with backing file */
cf8f2426
LC
2790 bdrv_img_create(target, format,
2791 source->filename,
2792 source->drv->format_name,
f382d43a 2793 NULL, size, flags, &local_err, false);
d9b902db
PB
2794 break;
2795 default:
2796 abort();
2797 }
2798 }
2799
84d18f06 2800 if (local_err) {
cf8f2426 2801 error_propagate(errp, local_err);
5a7e7a0b 2802 goto out;
d9b902db
PB
2803 }
2804
4c828dc6
BC
2805 if (has_node_name) {
2806 options = qdict_new();
2807 qdict_put(options, "node-name", qstring_from_str(node_name));
2808 }
2809
b812f671
PB
2810 /* Mirroring takes care of copy-on-write using the source's backing
2811 * file.
2812 */
f67503e5 2813 target_bs = NULL;
4c828dc6
BC
2814 ret = bdrv_open(&target_bs, target, NULL, options,
2815 flags | BDRV_O_NO_BACKING, drv, &local_err);
d9b902db 2816 if (ret < 0) {
34b5d2c6 2817 error_propagate(errp, local_err);
5a7e7a0b 2818 goto out;
d9b902db
PB
2819 }
2820
5a7e7a0b
SH
2821 bdrv_set_aio_context(target_bs, aio_context);
2822
09158f00
BC
2823 /* pass the node name to replace to mirror start since it's loose coupling
2824 * and will allow to check whether the node still exist at mirror completion
2825 */
2826 mirror_start(bs, target_bs,
2827 has_replaces ? replaces : NULL,
2828 speed, granularity, buf_size, sync,
eee13dfe 2829 on_source_error, on_target_error,
b952b558 2830 block_job_cb, bs, &local_err);
d9b902db 2831 if (local_err != NULL) {
4f6fd349 2832 bdrv_unref(target_bs);
d9b902db 2833 error_propagate(errp, local_err);
5a7e7a0b 2834 goto out;
d9b902db 2835 }
5a7e7a0b
SH
2836
2837out:
2838 aio_context_release(aio_context);
d9b902db
PB
2839}
2840
3d948cdf 2841/* Get the block job for a given device name and acquire its AioContext */
24d6bffe
MA
2842static BlockJob *find_block_job(const char *device, AioContext **aio_context,
2843 Error **errp)
2d47c6e9 2844{
a0e8544c 2845 BlockBackend *blk;
2d47c6e9
SH
2846 BlockDriverState *bs;
2847
a0e8544c
FZ
2848 blk = blk_by_name(device);
2849 if (!blk) {
3d948cdf
SH
2850 goto notfound;
2851 }
a0e8544c 2852 bs = blk_bs(blk);
3d948cdf
SH
2853
2854 *aio_context = bdrv_get_aio_context(bs);
2855 aio_context_acquire(*aio_context);
2856
2857 if (!bs->job) {
2858 aio_context_release(*aio_context);
2859 goto notfound;
2d47c6e9 2860 }
3d948cdf 2861
2d47c6e9 2862 return bs->job;
3d948cdf
SH
2863
2864notfound:
2e3a0266
MA
2865 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
2866 "No active block job on device '%s'", device);
3d948cdf
SH
2867 *aio_context = NULL;
2868 return NULL;
2d47c6e9
SH
2869}
2870
882ec7ce 2871void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2d47c6e9 2872{
3d948cdf 2873 AioContext *aio_context;
24d6bffe 2874 BlockJob *job = find_block_job(device, &aio_context, errp);
2d47c6e9
SH
2875
2876 if (!job) {
2d47c6e9
SH
2877 return;
2878 }
2879
882ec7ce 2880 block_job_set_speed(job, speed, errp);
3d948cdf 2881 aio_context_release(aio_context);
2d47c6e9 2882}
370521a1 2883
6e37fb81
PB
2884void qmp_block_job_cancel(const char *device,
2885 bool has_force, bool force, Error **errp)
370521a1 2886{
3d948cdf 2887 AioContext *aio_context;
24d6bffe 2888 BlockJob *job = find_block_job(device, &aio_context, errp);
6e37fb81 2889
370521a1 2890 if (!job) {
370521a1
SH
2891 return;
2892 }
3d948cdf
SH
2893
2894 if (!has_force) {
2895 force = false;
2896 }
2897
751ebd76 2898 if (job->user_paused && !force) {
f231b88d
CR
2899 error_setg(errp, "The block job for device '%s' is currently paused",
2900 device);
3d948cdf 2901 goto out;
8acc72a4 2902 }
370521a1
SH
2903
2904 trace_qmp_block_job_cancel(job);
2905 block_job_cancel(job);
3d948cdf
SH
2906out:
2907 aio_context_release(aio_context);
370521a1 2908}
fb5458cd 2909
6e37fb81
PB
2910void qmp_block_job_pause(const char *device, Error **errp)
2911{
3d948cdf 2912 AioContext *aio_context;
24d6bffe 2913 BlockJob *job = find_block_job(device, &aio_context, errp);
6e37fb81 2914
751ebd76 2915 if (!job || job->user_paused) {
6e37fb81
PB
2916 return;
2917 }
2918
751ebd76 2919 job->user_paused = true;
6e37fb81
PB
2920 trace_qmp_block_job_pause(job);
2921 block_job_pause(job);
3d948cdf 2922 aio_context_release(aio_context);
6e37fb81
PB
2923}
2924
2925void qmp_block_job_resume(const char *device, Error **errp)
2926{
3d948cdf 2927 AioContext *aio_context;
24d6bffe 2928 BlockJob *job = find_block_job(device, &aio_context, errp);
6e37fb81 2929
751ebd76 2930 if (!job || !job->user_paused) {
6e37fb81
PB
2931 return;
2932 }
2933
751ebd76 2934 job->user_paused = false;
6e37fb81
PB
2935 trace_qmp_block_job_resume(job);
2936 block_job_resume(job);
3d948cdf 2937 aio_context_release(aio_context);
6e37fb81
PB
2938}
2939
aeae883b
PB
2940void qmp_block_job_complete(const char *device, Error **errp)
2941{
3d948cdf 2942 AioContext *aio_context;
24d6bffe 2943 BlockJob *job = find_block_job(device, &aio_context, errp);
aeae883b
PB
2944
2945 if (!job) {
aeae883b
PB
2946 return;
2947 }
2948
2949 trace_qmp_block_job_complete(job);
2950 block_job_complete(job, errp);
3d948cdf 2951 aio_context_release(aio_context);
aeae883b
PB
2952}
2953
fa40e656
JC
2954void qmp_change_backing_file(const char *device,
2955 const char *image_node_name,
2956 const char *backing_file,
2957 Error **errp)
2958{
a0e8544c 2959 BlockBackend *blk;
fa40e656 2960 BlockDriverState *bs = NULL;
729962f6 2961 AioContext *aio_context;
fa40e656
JC
2962 BlockDriverState *image_bs = NULL;
2963 Error *local_err = NULL;
2964 bool ro;
2965 int open_flags;
2966 int ret;
2967
a0e8544c
FZ
2968 blk = blk_by_name(device);
2969 if (!blk) {
75158ebb
MA
2970 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2971 "Device '%s' not found", device);
fa40e656
JC
2972 return;
2973 }
a0e8544c 2974 bs = blk_bs(blk);
fa40e656 2975
729962f6
SH
2976 aio_context = bdrv_get_aio_context(bs);
2977 aio_context_acquire(aio_context);
2978
fa40e656
JC
2979 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
2980 if (local_err) {
2981 error_propagate(errp, local_err);
729962f6 2982 goto out;
fa40e656
JC
2983 }
2984
2985 if (!image_bs) {
2986 error_setg(errp, "image file not found");
729962f6 2987 goto out;
fa40e656
JC
2988 }
2989
2990 if (bdrv_find_base(image_bs) == image_bs) {
2991 error_setg(errp, "not allowing backing file change on an image "
2992 "without a backing file");
729962f6 2993 goto out;
fa40e656
JC
2994 }
2995
2996 /* even though we are not necessarily operating on bs, we need it to
2997 * determine if block ops are currently prohibited on the chain */
2998 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
729962f6 2999 goto out;
fa40e656
JC
3000 }
3001
3002 /* final sanity check */
3003 if (!bdrv_chain_contains(bs, image_bs)) {
3004 error_setg(errp, "'%s' and image file are not in the same chain",
3005 device);
729962f6 3006 goto out;
fa40e656
JC
3007 }
3008
3009 /* if not r/w, reopen to make r/w */
3010 open_flags = image_bs->open_flags;
3011 ro = bdrv_is_read_only(image_bs);
3012
3013 if (ro) {
3014 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3015 if (local_err) {
3016 error_propagate(errp, local_err);
729962f6 3017 goto out;
fa40e656
JC
3018 }
3019 }
3020
3021 ret = bdrv_change_backing_file(image_bs, backing_file,
3022 image_bs->drv ? image_bs->drv->format_name : "");
3023
3024 if (ret < 0) {
3025 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3026 backing_file);
3027 /* don't exit here, so we can try to restore open flags if
3028 * appropriate */
3029 }
3030
3031 if (ro) {
3032 bdrv_reopen(image_bs, open_flags, &local_err);
3033 if (local_err) {
3034 error_propagate(errp, local_err); /* will preserve prior errp */
3035 }
3036 }
729962f6
SH
3037
3038out:
3039 aio_context_release(aio_context);
fa40e656
JC
3040}
3041
d26c9a15
KW
3042void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3043{
3044 QmpOutputVisitor *ov = qmp_output_visitor_new();
18e46a03 3045 BlockBackend *blk;
d26c9a15
KW
3046 QObject *obj;
3047 QDict *qdict;
d26c9a15
KW
3048 Error *local_err = NULL;
3049
3050 /* Require an ID in the top level */
3051 if (!options->has_id) {
3052 error_setg(errp, "Block device needs an ID");
3053 goto fail;
3054 }
3055
60e19e06 3056 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
d26c9a15 3057 * cache.direct=false instead of silently switching to aio=threads, except
60e19e06 3058 * when called from drive_new().
d26c9a15
KW
3059 *
3060 * For now, simply forbidding the combination for all drivers will do. */
3061 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
c6e0bd9b
KW
3062 bool direct = options->has_cache &&
3063 options->cache->has_direct &&
3064 options->cache->direct;
3065 if (!direct) {
d26c9a15
KW
3066 error_setg(errp, "aio=native requires cache.direct=true");
3067 goto fail;
3068 }
3069 }
3070
3071 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
3072 &options, NULL, &local_err);
84d18f06 3073 if (local_err) {
d26c9a15
KW
3074 error_propagate(errp, local_err);
3075 goto fail;
3076 }
3077
3078 obj = qmp_output_get_qobject(ov);
3079 qdict = qobject_to_qdict(obj);
3080
3081 qdict_flatten(qdict);
3082
18e46a03 3083 blk = blockdev_init(NULL, qdict, &local_err);
84d18f06 3084 if (local_err) {
b681072d 3085 error_propagate(errp, local_err);
d26c9a15
KW
3086 goto fail;
3087 }
3088
18e46a03 3089 if (bdrv_key_required(blk_bs(blk))) {
18e46a03 3090 blk_unref(blk);
8ae8e904
KW
3091 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3092 goto fail;
3093 }
3094
d26c9a15
KW
3095fail:
3096 qmp_output_visitor_cleanup(ov);
3097}
3098
fea68bb6 3099BlockJobInfoList *qmp_query_block_jobs(Error **errp)
fb5458cd 3100{
fea68bb6
MA
3101 BlockJobInfoList *head = NULL, **p_next = &head;
3102 BlockDriverState *bs;
fb5458cd 3103
fea68bb6 3104 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
69691e72
SH
3105 AioContext *aio_context = bdrv_get_aio_context(bs);
3106
3107 aio_context_acquire(aio_context);
3108
fea68bb6
MA
3109 if (bs->job) {
3110 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3111 elem->value = block_job_query(bs->job);
3112 *p_next = elem;
3113 p_next = &elem->next;
3114 }
69691e72
SH
3115
3116 aio_context_release(aio_context);
fb5458cd 3117 }
fb5458cd 3118
fea68bb6 3119 return head;
fb5458cd 3120}
4d454574 3121
0006383e 3122QemuOptsList qemu_common_drive_opts = {
4d454574 3123 .name = "drive",
0006383e 3124 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4d454574
PB
3125 .desc = {
3126 {
4d454574
PB
3127 .name = "snapshot",
3128 .type = QEMU_OPT_BOOL,
3129 .help = "enable/disable snapshot mode",
a9384aff
PB
3130 },{
3131 .name = "discard",
3132 .type = QEMU_OPT_STRING,
3133 .help = "discard operation (ignore/off, unmap/on)",
4d454574 3134 },{
54861b92 3135 .name = BDRV_OPT_CACHE_WB,
29c4e2b5
KW
3136 .type = QEMU_OPT_BOOL,
3137 .help = "enables writeback mode for any caches",
3138 },{
54861b92 3139 .name = BDRV_OPT_CACHE_DIRECT,
29c4e2b5
KW
3140 .type = QEMU_OPT_BOOL,
3141 .help = "enables use of O_DIRECT (bypass the host page cache)",
3142 },{
54861b92 3143 .name = BDRV_OPT_CACHE_NO_FLUSH,
29c4e2b5
KW
3144 .type = QEMU_OPT_BOOL,
3145 .help = "ignore any flush requests for the device",
4d454574
PB
3146 },{
3147 .name = "aio",
3148 .type = QEMU_OPT_STRING,
3149 .help = "host AIO implementation (threads, native)",
3150 },{
3151 .name = "format",
3152 .type = QEMU_OPT_STRING,
3153 .help = "disk format (raw, qcow2, ...)",
4d454574
PB
3154 },{
3155 .name = "rerror",
3156 .type = QEMU_OPT_STRING,
3157 .help = "read error action",
3158 },{
3159 .name = "werror",
3160 .type = QEMU_OPT_STRING,
3161 .help = "write error action",
4d454574 3162 },{
0f227a94 3163 .name = "read-only",
4d454574
PB
3164 .type = QEMU_OPT_BOOL,
3165 .help = "open drive file as read-only",
3166 },{
57975222 3167 .name = "throttling.iops-total",
4d454574
PB
3168 .type = QEMU_OPT_NUMBER,
3169 .help = "limit total I/O operations per second",
3170 },{
57975222 3171 .name = "throttling.iops-read",
4d454574
PB
3172 .type = QEMU_OPT_NUMBER,
3173 .help = "limit read operations per second",
3174 },{
57975222 3175 .name = "throttling.iops-write",
4d454574
PB
3176 .type = QEMU_OPT_NUMBER,
3177 .help = "limit write operations per second",
3178 },{
57975222 3179 .name = "throttling.bps-total",
4d454574
PB
3180 .type = QEMU_OPT_NUMBER,
3181 .help = "limit total bytes per second",
3182 },{
57975222 3183 .name = "throttling.bps-read",
4d454574
PB
3184 .type = QEMU_OPT_NUMBER,
3185 .help = "limit read bytes per second",
3186 },{
57975222 3187 .name = "throttling.bps-write",
4d454574
PB
3188 .type = QEMU_OPT_NUMBER,
3189 .help = "limit write bytes per second",
3e9fab69
BC
3190 },{
3191 .name = "throttling.iops-total-max",
3192 .type = QEMU_OPT_NUMBER,
3193 .help = "I/O operations burst",
3194 },{
3195 .name = "throttling.iops-read-max",
3196 .type = QEMU_OPT_NUMBER,
3197 .help = "I/O operations read burst",
3198 },{
3199 .name = "throttling.iops-write-max",
3200 .type = QEMU_OPT_NUMBER,
3201 .help = "I/O operations write burst",
3202 },{
3203 .name = "throttling.bps-total-max",
3204 .type = QEMU_OPT_NUMBER,
3205 .help = "total bytes burst",
3206 },{
3207 .name = "throttling.bps-read-max",
3208 .type = QEMU_OPT_NUMBER,
3209 .help = "total bytes read burst",
3210 },{
3211 .name = "throttling.bps-write-max",
3212 .type = QEMU_OPT_NUMBER,
3213 .help = "total bytes write burst",
2024c1df
BC
3214 },{
3215 .name = "throttling.iops-size",
3216 .type = QEMU_OPT_NUMBER,
3217 .help = "when limiting by iops max size of an I/O in bytes",
76f4afb4
AG
3218 },{
3219 .name = "throttling.group",
3220 .type = QEMU_OPT_STRING,
3221 .help = "name of the block throttling group",
4d454574
PB
3222 },{
3223 .name = "copy-on-read",
3224 .type = QEMU_OPT_BOOL,
3225 .help = "copy read data from backing file into image file",
465bee1d
PL
3226 },{
3227 .name = "detect-zeroes",
3228 .type = QEMU_OPT_STRING,
3229 .help = "try to optimize zero writes (off, on, unmap)",
4d454574
PB
3230 },
3231 { /* end of list */ }
3232 },
3233};
0006383e
KW
3234
3235QemuOptsList qemu_drive_opts = {
3236 .name = "drive",
3237 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3238 .desc = {
492fdc6f
KW
3239 /*
3240 * no elements => accept any params
3241 * validation will happen later
3242 */
0006383e
KW
3243 { /* end of list */ }
3244 },
3245};