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