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