]> git.proxmox.com Git - mirror_qemu.git/blame - blockdev.c
block: Suppress unhelpful extra errors in bdrv_img_create()
[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
MA
189 if (type != IF_DEFAULT) {
190 qemu_opt_set(opts, "if", if_name[type]);
191 }
192 if (index >= 0) {
193 snprintf(buf, sizeof(buf), "%d", index);
194 qemu_opt_set(opts, "index", buf);
195 }
666daa68
MA
196 if (file)
197 qemu_opt_set(opts, "file", file);
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))) {
57975222
KW
587 qemu_opt_set(opts, to, value);
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
KW
937 if (arch_type == QEMU_ARCH_S390X) {
938 qemu_opt_set(devopts, "driver", "virtio-blk-s390");
939 } else {
940 qemu_opt_set(devopts, "driver", "virtio-blk-pci");
941 }
942 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"));
943 if (devaddr) {
944 qemu_opt_set(devopts, "addr", devaddr);
945 }
946 }
947
d095b465
HR
948 filename = qemu_opt_get(legacy_opts, "file");
949
ee13ed1c
KW
950 /* Check werror/rerror compatibility with if=... */
951 werror = qemu_opt_get(legacy_opts, "werror");
952 if (werror != NULL) {
953 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
954 type != IF_NONE) {
955 error_report("werror is not supported by this bus type");
956 goto fail;
957 }
958 qdict_put(bs_opts, "werror", qstring_from_str(werror));
959 }
960
961 rerror = qemu_opt_get(legacy_opts, "rerror");
962 if (rerror != NULL) {
963 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
964 type != IF_NONE) {
965 error_report("rerror is not supported by this bus type");
966 goto fail;
967 }
968 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
969 }
970
2d246f01 971 /* Actual block device init: Functionality shared with blockdev-add */
18e46a03 972 blk = blockdev_init(filename, bs_opts, &local_err);
3cb0e25c 973 bs_opts = NULL;
18e46a03 974 if (!blk) {
84d18f06 975 if (local_err) {
565f65d2 976 error_report_err(local_err);
b681072d 977 }
2d246f01 978 goto fail;
b681072d 979 } else {
84d18f06 980 assert(!local_err);
2d246f01
KW
981 }
982
26f8b3a8
MA
983 /* Create legacy DriveInfo */
984 dinfo = g_malloc0(sizeof(*dinfo));
f298d071 985 dinfo->opts = all_opts;
2d246f01 986
b41a7338
KW
987 dinfo->cyls = cyls;
988 dinfo->heads = heads;
989 dinfo->secs = secs;
990 dinfo->trans = translation;
991
ee13ed1c 992 dinfo->type = type;
87a899c5
KW
993 dinfo->bus = bus_id;
994 dinfo->unit = unit_id;
394c7d4d 995 dinfo->devaddr = devaddr;
bcf83158
KW
996 dinfo->serial = g_strdup(serial);
997
26f8b3a8
MA
998 blk_set_legacy_dinfo(blk, dinfo);
999
e34ef046
KW
1000 switch(type) {
1001 case IF_IDE:
1002 case IF_SCSI:
1003 case IF_XEN:
1004 case IF_NONE:
1005 dinfo->media_cd = media == MEDIA_CDROM;
1006 break;
1007 default:
1008 break;
1009 }
1010
2d246f01 1011fail:
33cb7dc8 1012 qemu_opts_del(legacy_opts);
3cb0e25c 1013 QDECREF(bs_opts);
2d246f01 1014 return dinfo;
57975222
KW
1015}
1016
3e5a50d6 1017void hmp_commit(Monitor *mon, const QDict *qdict)
666daa68 1018{
666daa68 1019 const char *device = qdict_get_str(qdict, "device");
6ab4b5ab 1020 BlockDriverState *bs;
e8877497 1021 int ret;
666daa68 1022
6ab4b5ab 1023 if (!strcmp(device, "all")) {
e8877497 1024 ret = bdrv_commit_all();
6ab4b5ab
MA
1025 } else {
1026 bs = bdrv_find(device);
ac59eb95 1027 if (!bs) {
58513bde 1028 monitor_printf(mon, "Device '%s' not found\n", device);
ac59eb95 1029 return;
6ab4b5ab 1030 }
2d3735d3 1031 ret = bdrv_commit(bs);
58513bde
JC
1032 }
1033 if (ret < 0) {
1034 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1035 strerror(-ret));
666daa68
MA
1036 }
1037}
1038
6cc2a415
PB
1039static void blockdev_do_action(int kind, void *data, Error **errp)
1040{
c8a83e85
KW
1041 TransactionAction action;
1042 TransactionActionList list;
6cc2a415
PB
1043
1044 action.kind = kind;
1045 action.data = data;
1046 list.value = &action;
1047 list.next = NULL;
1048 qmp_transaction(&list, errp);
1049}
1050
0901f67e
BC
1051void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1052 bool has_node_name, const char *node_name,
1053 const char *snapshot_file,
1054 bool has_snapshot_node_name,
1055 const char *snapshot_node_name,
6106e249 1056 bool has_format, const char *format,
0901f67e 1057 bool has_mode, NewImageMode mode, Error **errp)
f8882568 1058{
6cc2a415 1059 BlockdevSnapshot snapshot = {
0901f67e 1060 .has_device = has_device,
6cc2a415 1061 .device = (char *) device,
0901f67e
BC
1062 .has_node_name = has_node_name,
1063 .node_name = (char *) node_name,
6cc2a415 1064 .snapshot_file = (char *) snapshot_file,
0901f67e
BC
1065 .has_snapshot_node_name = has_snapshot_node_name,
1066 .snapshot_node_name = (char *) snapshot_node_name,
6cc2a415
PB
1067 .has_format = has_format,
1068 .format = (char *) format,
1069 .has_mode = has_mode,
1070 .mode = mode,
1071 };
c8a83e85
KW
1072 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1073 &snapshot, errp);
f8882568
JS
1074}
1075
f323bc9e
WX
1076void qmp_blockdev_snapshot_internal_sync(const char *device,
1077 const char *name,
1078 Error **errp)
1079{
1080 BlockdevSnapshotInternal snapshot = {
1081 .device = (char *) device,
1082 .name = (char *) name
1083 };
1084
1085 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1086 &snapshot, errp);
1087}
1088
44e3e053
WX
1089SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1090 bool has_id,
1091 const char *id,
1092 bool has_name,
1093 const char *name,
1094 Error **errp)
1095{
1096 BlockDriverState *bs = bdrv_find(device);
4ef3982a 1097 AioContext *aio_context;
44e3e053
WX
1098 QEMUSnapshotInfo sn;
1099 Error *local_err = NULL;
1100 SnapshotInfo *info = NULL;
1101 int ret;
1102
1103 if (!bs) {
1104 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1105 return NULL;
1106 }
1107
1108 if (!has_id) {
1109 id = NULL;
1110 }
1111
1112 if (!has_name) {
1113 name = NULL;
1114 }
1115
1116 if (!id && !name) {
1117 error_setg(errp, "Name or id must be provided");
1118 return NULL;
1119 }
1120
4ef3982a
SH
1121 aio_context = bdrv_get_aio_context(bs);
1122 aio_context_acquire(aio_context);
1123
0b928854
SH
1124 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1125 goto out_aio_context;
1126 }
1127
44e3e053 1128 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
84d18f06 1129 if (local_err) {
44e3e053 1130 error_propagate(errp, local_err);
4ef3982a 1131 goto out_aio_context;
44e3e053
WX
1132 }
1133 if (!ret) {
1134 error_setg(errp,
1135 "Snapshot with id '%s' and name '%s' does not exist on "
1136 "device '%s'",
1137 STR_OR_NULL(id), STR_OR_NULL(name), device);
4ef3982a 1138 goto out_aio_context;
44e3e053
WX
1139 }
1140
1141 bdrv_snapshot_delete(bs, id, name, &local_err);
84d18f06 1142 if (local_err) {
44e3e053 1143 error_propagate(errp, local_err);
4ef3982a 1144 goto out_aio_context;
44e3e053
WX
1145 }
1146
4ef3982a
SH
1147 aio_context_release(aio_context);
1148
5839e53b 1149 info = g_new0(SnapshotInfo, 1);
44e3e053
WX
1150 info->id = g_strdup(sn.id_str);
1151 info->name = g_strdup(sn.name);
1152 info->date_nsec = sn.date_nsec;
1153 info->date_sec = sn.date_sec;
1154 info->vm_state_size = sn.vm_state_size;
1155 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1156 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1157
1158 return info;
4ef3982a
SH
1159
1160out_aio_context:
1161 aio_context_release(aio_context);
1162 return NULL;
44e3e053 1163}
8802d1fd 1164
b756b9ce 1165/* New and old BlockDriverState structs for atomic group operations */
ba0c86a3 1166
ba5d6ab6 1167typedef struct BlkTransactionState BlkTransactionState;
ba0c86a3
WX
1168
1169/* Only prepare() may fail. In a single transaction, only one of commit() or
1170 abort() will be called, clean() will always be called if it present. */
1171typedef struct BdrvActionOps {
1172 /* Size of state struct, in bytes. */
1173 size_t instance_size;
1174 /* Prepare the work, must NOT be NULL. */
ba5d6ab6 1175 void (*prepare)(BlkTransactionState *common, Error **errp);
f9ea81e8 1176 /* Commit the changes, can be NULL. */
ba5d6ab6 1177 void (*commit)(BlkTransactionState *common);
ba0c86a3 1178 /* Abort the changes on fail, can be NULL. */
ba5d6ab6 1179 void (*abort)(BlkTransactionState *common);
ba0c86a3 1180 /* Clean up resource in the end, can be NULL. */
ba5d6ab6 1181 void (*clean)(BlkTransactionState *common);
ba0c86a3
WX
1182} BdrvActionOps;
1183
1184/*
1185 * This structure must be arranged as first member in child type, assuming
1186 * that compiler will also arrange it to the same address with parent instance.
1187 * Later it will be used in free().
1188 */
ba5d6ab6 1189struct BlkTransactionState {
c8a83e85 1190 TransactionAction *action;
ba0c86a3 1191 const BdrvActionOps *ops;
ba5d6ab6 1192 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
ba0c86a3
WX
1193};
1194
bbe86010
WX
1195/* internal snapshot private data */
1196typedef struct InternalSnapshotState {
1197 BlkTransactionState common;
1198 BlockDriverState *bs;
5d6e96ef 1199 AioContext *aio_context;
bbe86010
WX
1200 QEMUSnapshotInfo sn;
1201} InternalSnapshotState;
1202
1203static void internal_snapshot_prepare(BlkTransactionState *common,
1204 Error **errp)
1205{
f70edf99 1206 Error *local_err = NULL;
bbe86010
WX
1207 const char *device;
1208 const char *name;
1209 BlockDriverState *bs;
1210 QEMUSnapshotInfo old_sn, *sn;
1211 bool ret;
1212 qemu_timeval tv;
1213 BlockdevSnapshotInternal *internal;
1214 InternalSnapshotState *state;
1215 int ret1;
1216
1217 g_assert(common->action->kind ==
1218 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1219 internal = common->action->blockdev_snapshot_internal_sync;
1220 state = DO_UPCAST(InternalSnapshotState, common, common);
1221
1222 /* 1. parse input */
1223 device = internal->device;
1224 name = internal->name;
1225
1226 /* 2. check for validation */
1227 bs = bdrv_find(device);
1228 if (!bs) {
1229 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1230 return;
1231 }
1232
5d6e96ef
SH
1233 /* AioContext is released in .clean() */
1234 state->aio_context = bdrv_get_aio_context(bs);
1235 aio_context_acquire(state->aio_context);
1236
bbe86010
WX
1237 if (!bdrv_is_inserted(bs)) {
1238 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1239 return;
1240 }
1241
3dc7ca3c
SH
1242 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1243 return;
1244 }
1245
bbe86010
WX
1246 if (bdrv_is_read_only(bs)) {
1247 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1248 return;
1249 }
1250
1251 if (!bdrv_can_snapshot(bs)) {
1252 error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
1253 bs->drv->format_name, device, "internal snapshot");
1254 return;
1255 }
1256
1257 if (!strlen(name)) {
1258 error_setg(errp, "Name is empty");
1259 return;
1260 }
1261
1262 /* check whether a snapshot with name exist */
f70edf99
MA
1263 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1264 &local_err);
1265 if (local_err) {
1266 error_propagate(errp, local_err);
bbe86010
WX
1267 return;
1268 } else if (ret) {
1269 error_setg(errp,
1270 "Snapshot with name '%s' already exists on device '%s'",
1271 name, device);
1272 return;
1273 }
1274
1275 /* 3. take the snapshot */
1276 sn = &state->sn;
1277 pstrcpy(sn->name, sizeof(sn->name), name);
1278 qemu_gettimeofday(&tv);
1279 sn->date_sec = tv.tv_sec;
1280 sn->date_nsec = tv.tv_usec * 1000;
1281 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1282
1283 ret1 = bdrv_snapshot_create(bs, sn);
1284 if (ret1 < 0) {
1285 error_setg_errno(errp, -ret1,
1286 "Failed to create snapshot '%s' on device '%s'",
1287 name, device);
1288 return;
1289 }
1290
1291 /* 4. succeed, mark a snapshot is created */
1292 state->bs = bs;
1293}
1294
1295static void internal_snapshot_abort(BlkTransactionState *common)
1296{
1297 InternalSnapshotState *state =
1298 DO_UPCAST(InternalSnapshotState, common, common);
1299 BlockDriverState *bs = state->bs;
1300 QEMUSnapshotInfo *sn = &state->sn;
1301 Error *local_error = NULL;
1302
1303 if (!bs) {
1304 return;
1305 }
1306
1307 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1308 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1309 "device '%s' in abort: %s",
1310 sn->id_str,
1311 sn->name,
1312 bdrv_get_device_name(bs),
1313 error_get_pretty(local_error));
1314 error_free(local_error);
1315 }
1316}
1317
5d6e96ef
SH
1318static void internal_snapshot_clean(BlkTransactionState *common)
1319{
1320 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1321 common, common);
1322
1323 if (state->aio_context) {
1324 aio_context_release(state->aio_context);
1325 }
1326}
1327
ba0c86a3 1328/* external snapshot private data */
ba5d6ab6
SH
1329typedef struct ExternalSnapshotState {
1330 BlkTransactionState common;
8802d1fd
JC
1331 BlockDriverState *old_bs;
1332 BlockDriverState *new_bs;
5d6e96ef 1333 AioContext *aio_context;
ba5d6ab6 1334} ExternalSnapshotState;
8802d1fd 1335
ba5d6ab6 1336static void external_snapshot_prepare(BlkTransactionState *common,
9b9877ee
WX
1337 Error **errp)
1338{
9b9877ee
WX
1339 BlockDriver *drv;
1340 int flags, ret;
0901f67e 1341 QDict *options = NULL;
9b9877ee 1342 Error *local_err = NULL;
0901f67e 1343 bool has_device = false;
e2a31e87 1344 const char *device;
0901f67e
BC
1345 bool has_node_name = false;
1346 const char *node_name;
1347 bool has_snapshot_node_name = false;
1348 const char *snapshot_node_name;
e2a31e87
WX
1349 const char *new_image_file;
1350 const char *format = "qcow2";
1351 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
ba5d6ab6
SH
1352 ExternalSnapshotState *state =
1353 DO_UPCAST(ExternalSnapshotState, common, common);
c8a83e85 1354 TransactionAction *action = common->action;
9b9877ee 1355
e2a31e87 1356 /* get parameters */
c8a83e85 1357 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
e2a31e87 1358
0901f67e 1359 has_device = action->blockdev_snapshot_sync->has_device;
e2a31e87 1360 device = action->blockdev_snapshot_sync->device;
0901f67e
BC
1361 has_node_name = action->blockdev_snapshot_sync->has_node_name;
1362 node_name = action->blockdev_snapshot_sync->node_name;
1363 has_snapshot_node_name =
1364 action->blockdev_snapshot_sync->has_snapshot_node_name;
1365 snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name;
1366
e2a31e87
WX
1367 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1368 if (action->blockdev_snapshot_sync->has_format) {
1369 format = action->blockdev_snapshot_sync->format;
1370 }
1371 if (action->blockdev_snapshot_sync->has_mode) {
1372 mode = action->blockdev_snapshot_sync->mode;
1373 }
1374
1375 /* start processing */
9b9877ee
WX
1376 drv = bdrv_find_format(format);
1377 if (!drv) {
1378 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1379 return;
1380 }
1381
0901f67e
BC
1382 state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
1383 has_node_name ? node_name : NULL,
1384 &local_err);
84d18f06 1385 if (local_err) {
0901f67e
BC
1386 error_propagate(errp, local_err);
1387 return;
1388 }
1389
1390 if (has_node_name && !has_snapshot_node_name) {
1391 error_setg(errp, "New snapshot node name missing");
1392 return;
1393 }
1394
1395 if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) {
1396 error_setg(errp, "New snapshot node name already existing");
9b9877ee
WX
1397 return;
1398 }
1399
5d6e96ef
SH
1400 /* Acquire AioContext now so any threads operating on old_bs stop */
1401 state->aio_context = bdrv_get_aio_context(state->old_bs);
1402 aio_context_acquire(state->aio_context);
1403
ba5d6ab6 1404 if (!bdrv_is_inserted(state->old_bs)) {
9b9877ee
WX
1405 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1406 return;
1407 }
1408
3718d8ab
FZ
1409 if (bdrv_op_is_blocked(state->old_bs,
1410 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
9b9877ee
WX
1411 return;
1412 }
1413
ba5d6ab6
SH
1414 if (!bdrv_is_read_only(state->old_bs)) {
1415 if (bdrv_flush(state->old_bs)) {
9b9877ee
WX
1416 error_set(errp, QERR_IO_ERROR);
1417 return;
1418 }
1419 }
1420
212a5a8f 1421 if (!bdrv_is_first_non_filter(state->old_bs)) {
f6186f49
BC
1422 error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
1423 return;
1424 }
1425
ba5d6ab6 1426 flags = state->old_bs->open_flags;
9b9877ee 1427
9b9877ee
WX
1428 /* create new image w/backing file */
1429 if (mode != NEW_IMAGE_MODE_EXISTING) {
1430 bdrv_img_create(new_image_file, format,
ba5d6ab6
SH
1431 state->old_bs->filename,
1432 state->old_bs->drv->format_name,
9b9877ee 1433 NULL, -1, flags, &local_err, false);
84d18f06 1434 if (local_err) {
9b9877ee
WX
1435 error_propagate(errp, local_err);
1436 return;
1437 }
1438 }
1439
0901f67e
BC
1440 if (has_snapshot_node_name) {
1441 options = qdict_new();
1442 qdict_put(options, "node-name",
1443 qstring_from_str(snapshot_node_name));
1444 }
1445
9b9877ee
WX
1446 /* TODO Inherit bs->options or only take explicit options with an
1447 * extended QMP command? */
f67503e5 1448 assert(state->new_bs == NULL);
ddf5636d 1449 ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
34b5d2c6 1450 flags | BDRV_O_NO_BACKING, drv, &local_err);
f67503e5 1451 /* We will manually add the backing_hd field to the bs later */
9b9877ee 1452 if (ret != 0) {
34b5d2c6 1453 error_propagate(errp, local_err);
9b9877ee
WX
1454 }
1455}
1456
ba5d6ab6 1457static void external_snapshot_commit(BlkTransactionState *common)
3b0047e8 1458{
ba5d6ab6
SH
1459 ExternalSnapshotState *state =
1460 DO_UPCAST(ExternalSnapshotState, common, common);
ba0c86a3 1461
5d6e96ef
SH
1462 bdrv_set_aio_context(state->new_bs, state->aio_context);
1463
ba5d6ab6
SH
1464 /* This removes our old bs and adds the new bs */
1465 bdrv_append(state->new_bs, state->old_bs);
3b0047e8
WX
1466 /* We don't need (or want) to use the transactional
1467 * bdrv_reopen_multiple() across all the entries at once, because we
1468 * don't want to abort all of them if one of them fails the reopen */
ba5d6ab6 1469 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
3b0047e8 1470 NULL);
5d6e96ef
SH
1471
1472 aio_context_release(state->aio_context);
3b0047e8
WX
1473}
1474
ba5d6ab6 1475static void external_snapshot_abort(BlkTransactionState *common)
96b86bf7 1476{
ba5d6ab6
SH
1477 ExternalSnapshotState *state =
1478 DO_UPCAST(ExternalSnapshotState, common, common);
1479 if (state->new_bs) {
4f6fd349 1480 bdrv_unref(state->new_bs);
96b86bf7 1481 }
5d6e96ef
SH
1482 if (state->aio_context) {
1483 aio_context_release(state->aio_context);
1484 }
96b86bf7
WX
1485}
1486
3037f364
SH
1487typedef struct DriveBackupState {
1488 BlkTransactionState common;
1489 BlockDriverState *bs;
5d6e96ef 1490 AioContext *aio_context;
3037f364
SH
1491 BlockJob *job;
1492} DriveBackupState;
1493
1494static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1495{
1496 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
5d6e96ef 1497 BlockDriverState *bs;
3037f364
SH
1498 DriveBackup *backup;
1499 Error *local_err = NULL;
1500
1501 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1502 backup = common->action->drive_backup;
1503
5d6e96ef
SH
1504 bs = bdrv_find(backup->device);
1505 if (!bs) {
1506 error_set(errp, QERR_DEVICE_NOT_FOUND, backup->device);
1507 return;
1508 }
1509
1510 /* AioContext is released in .clean() */
1511 state->aio_context = bdrv_get_aio_context(bs);
1512 aio_context_acquire(state->aio_context);
1513
3037f364
SH
1514 qmp_drive_backup(backup->device, backup->target,
1515 backup->has_format, backup->format,
b53169ea 1516 backup->sync,
3037f364
SH
1517 backup->has_mode, backup->mode,
1518 backup->has_speed, backup->speed,
1519 backup->has_on_source_error, backup->on_source_error,
1520 backup->has_on_target_error, backup->on_target_error,
1521 &local_err);
84d18f06 1522 if (local_err) {
3037f364 1523 error_propagate(errp, local_err);
3037f364
SH
1524 return;
1525 }
1526
5d6e96ef 1527 state->bs = bs;
3037f364
SH
1528 state->job = state->bs->job;
1529}
1530
1531static void drive_backup_abort(BlkTransactionState *common)
1532{
1533 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1534 BlockDriverState *bs = state->bs;
1535
1536 /* Only cancel if it's the job we started */
1537 if (bs && bs->job && bs->job == state->job) {
1538 block_job_cancel_sync(bs->job);
1539 }
1540}
1541
5d6e96ef
SH
1542static void drive_backup_clean(BlkTransactionState *common)
1543{
1544 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1545
1546 if (state->aio_context) {
1547 aio_context_release(state->aio_context);
1548 }
1549}
1550
bd8baecd
FZ
1551typedef struct BlockdevBackupState {
1552 BlkTransactionState common;
1553 BlockDriverState *bs;
1554 BlockJob *job;
1555 AioContext *aio_context;
1556} BlockdevBackupState;
1557
1558static void blockdev_backup_prepare(BlkTransactionState *common, Error **errp)
1559{
1560 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1561 BlockdevBackup *backup;
1562 BlockDriverState *bs, *target;
1563 Error *local_err = NULL;
1564
1565 assert(common->action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1566 backup = common->action->blockdev_backup;
1567
1568 bs = bdrv_find(backup->device);
1569 if (!bs) {
1570 error_set(errp, QERR_DEVICE_NOT_FOUND, backup->device);
1571 return;
1572 }
1573
1574 target = bdrv_find(backup->target);
1575 if (!target) {
1576 error_set(errp, QERR_DEVICE_NOT_FOUND, backup->target);
1577 return;
1578 }
1579
1580 /* AioContext is released in .clean() */
1581 state->aio_context = bdrv_get_aio_context(bs);
1582 if (state->aio_context != bdrv_get_aio_context(target)) {
1583 state->aio_context = NULL;
1584 error_setg(errp, "Backup between two IO threads is not implemented");
1585 return;
1586 }
1587 aio_context_acquire(state->aio_context);
1588
1589 qmp_blockdev_backup(backup->device, backup->target,
1590 backup->sync,
1591 backup->has_speed, backup->speed,
1592 backup->has_on_source_error, backup->on_source_error,
1593 backup->has_on_target_error, backup->on_target_error,
1594 &local_err);
1595 if (local_err) {
1596 error_propagate(errp, local_err);
1597 return;
1598 }
1599
1600 state->bs = bs;
1601 state->job = state->bs->job;
1602}
1603
1604static void blockdev_backup_abort(BlkTransactionState *common)
1605{
1606 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1607 BlockDriverState *bs = state->bs;
1608
1609 /* Only cancel if it's the job we started */
1610 if (bs && bs->job && bs->job == state->job) {
1611 block_job_cancel_sync(bs->job);
1612 }
1613}
1614
1615static void blockdev_backup_clean(BlkTransactionState *common)
1616{
1617 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1618
1619 if (state->aio_context) {
1620 aio_context_release(state->aio_context);
1621 }
1622}
1623
78b18b78
SH
1624static void abort_prepare(BlkTransactionState *common, Error **errp)
1625{
1626 error_setg(errp, "Transaction aborted using Abort action");
1627}
1628
1629static void abort_commit(BlkTransactionState *common)
1630{
dfc6f865 1631 g_assert_not_reached(); /* this action never succeeds */
78b18b78
SH
1632}
1633
ba0c86a3 1634static const BdrvActionOps actions[] = {
c8a83e85 1635 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
ba5d6ab6 1636 .instance_size = sizeof(ExternalSnapshotState),
ba0c86a3
WX
1637 .prepare = external_snapshot_prepare,
1638 .commit = external_snapshot_commit,
1639 .abort = external_snapshot_abort,
1640 },
3037f364
SH
1641 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1642 .instance_size = sizeof(DriveBackupState),
1643 .prepare = drive_backup_prepare,
1644 .abort = drive_backup_abort,
5d6e96ef 1645 .clean = drive_backup_clean,
3037f364 1646 },
bd8baecd
FZ
1647 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
1648 .instance_size = sizeof(BlockdevBackupState),
1649 .prepare = blockdev_backup_prepare,
1650 .abort = blockdev_backup_abort,
1651 .clean = blockdev_backup_clean,
1652 },
78b18b78
SH
1653 [TRANSACTION_ACTION_KIND_ABORT] = {
1654 .instance_size = sizeof(BlkTransactionState),
1655 .prepare = abort_prepare,
1656 .commit = abort_commit,
1657 },
bbe86010
WX
1658 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1659 .instance_size = sizeof(InternalSnapshotState),
1660 .prepare = internal_snapshot_prepare,
1661 .abort = internal_snapshot_abort,
5d6e96ef 1662 .clean = internal_snapshot_clean,
bbe86010 1663 },
ba0c86a3
WX
1664};
1665
8802d1fd 1666/*
b756b9ce
SH
1667 * 'Atomic' group operations. The operations are performed as a set, and if
1668 * any fail then we roll back all operations in the group.
8802d1fd 1669 */
c8a83e85 1670void qmp_transaction(TransactionActionList *dev_list, Error **errp)
8802d1fd 1671{
c8a83e85 1672 TransactionActionList *dev_entry = dev_list;
ba5d6ab6 1673 BlkTransactionState *state, *next;
43e17041 1674 Error *local_err = NULL;
8802d1fd 1675
ba5d6ab6 1676 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
8802d1fd
JC
1677 QSIMPLEQ_INIT(&snap_bdrv_states);
1678
b756b9ce 1679 /* drain all i/o before any operations */
8802d1fd
JC
1680 bdrv_drain_all();
1681
b756b9ce 1682 /* We don't do anything in this loop that commits us to the operations */
8802d1fd 1683 while (NULL != dev_entry) {
c8a83e85 1684 TransactionAction *dev_info = NULL;
ba0c86a3 1685 const BdrvActionOps *ops;
52e7c241 1686
8802d1fd
JC
1687 dev_info = dev_entry->value;
1688 dev_entry = dev_entry->next;
1689
ba0c86a3
WX
1690 assert(dev_info->kind < ARRAY_SIZE(actions));
1691
1692 ops = &actions[dev_info->kind];
aa3fe714
HR
1693 assert(ops->instance_size > 0);
1694
ba5d6ab6
SH
1695 state = g_malloc0(ops->instance_size);
1696 state->ops = ops;
1697 state->action = dev_info;
1698 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
8802d1fd 1699
ba5d6ab6 1700 state->ops->prepare(state, &local_err);
84d18f06 1701 if (local_err) {
ba0c86a3
WX
1702 error_propagate(errp, local_err);
1703 goto delete_and_fail;
8802d1fd 1704 }
8802d1fd
JC
1705 }
1706
ba5d6ab6 1707 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
f9ea81e8
SH
1708 if (state->ops->commit) {
1709 state->ops->commit(state);
1710 }
8802d1fd
JC
1711 }
1712
1713 /* success */
1714 goto exit;
1715
1716delete_and_fail:
b756b9ce 1717 /* failure, and it is all-or-none; roll back all operations */
ba5d6ab6
SH
1718 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1719 if (state->ops->abort) {
1720 state->ops->abort(state);
ba0c86a3 1721 }
8802d1fd
JC
1722 }
1723exit:
ba5d6ab6
SH
1724 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1725 if (state->ops->clean) {
1726 state->ops->clean(state);
ba0c86a3 1727 }
ba5d6ab6 1728 g_free(state);
8802d1fd 1729 }
8802d1fd
JC
1730}
1731
1732
6007cdd4 1733static void eject_device(BlockBackend *blk, int force, Error **errp)
666daa68 1734{
6007cdd4 1735 BlockDriverState *bs = blk_bs(blk);
e3442099
SH
1736 AioContext *aio_context;
1737
1738 aio_context = bdrv_get_aio_context(bs);
1739 aio_context_acquire(aio_context);
6007cdd4 1740
3718d8ab 1741 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
e3442099 1742 goto out;
2d3735d3 1743 }
a7f53e26 1744 if (!blk_dev_has_removable_media(blk)) {
f231b88d
CR
1745 error_setg(errp, "Device '%s' is not removable",
1746 bdrv_get_device_name(bs));
e3442099 1747 goto out;
ea8f942f 1748 }
92d48558 1749
a7f53e26
MA
1750 if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) {
1751 blk_dev_eject_request(blk, force);
025ccaa7 1752 if (!force) {
f231b88d
CR
1753 error_setg(errp, "Device '%s' is locked",
1754 bdrv_get_device_name(bs));
e3442099 1755 goto out;
025ccaa7 1756 }
666daa68 1757 }
92d48558 1758
3b5276b5 1759 bdrv_close(bs);
e3442099
SH
1760
1761out:
1762 aio_context_release(aio_context);
666daa68
MA
1763}
1764
c245b6a3 1765void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
666daa68 1766{
6007cdd4 1767 BlockBackend *blk;
666daa68 1768
6007cdd4
MA
1769 blk = blk_by_name(device);
1770 if (!blk) {
c245b6a3
LC
1771 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1772 return;
92d48558
LC
1773 }
1774
6007cdd4 1775 eject_device(blk, force, errp);
666daa68
MA
1776}
1777
12d3ba82
BC
1778void qmp_block_passwd(bool has_device, const char *device,
1779 bool has_node_name, const char *node_name,
1780 const char *password, Error **errp)
666daa68 1781{
12d3ba82 1782 Error *local_err = NULL;
666daa68 1783 BlockDriverState *bs;
e3442099 1784 AioContext *aio_context;
666daa68 1785
12d3ba82
BC
1786 bs = bdrv_lookup_bs(has_device ? device : NULL,
1787 has_node_name ? node_name : NULL,
1788 &local_err);
84d18f06 1789 if (local_err) {
12d3ba82 1790 error_propagate(errp, local_err);
a4dea8a9 1791 return;
666daa68
MA
1792 }
1793
e3442099
SH
1794 aio_context = bdrv_get_aio_context(bs);
1795 aio_context_acquire(aio_context);
1796
4d2855a3 1797 bdrv_add_key(bs, password, errp);
e3442099 1798
e3442099 1799 aio_context_release(aio_context);
666daa68
MA
1800}
1801
e3442099 1802/* Assumes AioContext is held */
333a96ec
LC
1803static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1804 int bdrv_flags, BlockDriver *drv,
1805 const char *password, Error **errp)
1806{
34b5d2c6 1807 Error *local_err = NULL;
0eef407c
LC
1808 int ret;
1809
ddf5636d 1810 ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err);
0eef407c 1811 if (ret < 0) {
34b5d2c6 1812 error_propagate(errp, local_err);
333a96ec
LC
1813 return;
1814 }
1815
4d2855a3 1816 bdrv_add_key(bs, password, errp);
333a96ec
LC
1817}
1818
1819void qmp_change_blockdev(const char *device, const char *filename,
314f7ea7 1820 const char *format, Error **errp)
666daa68 1821{
6007cdd4 1822 BlockBackend *blk;
666daa68 1823 BlockDriverState *bs;
e3442099 1824 AioContext *aio_context;
666daa68
MA
1825 BlockDriver *drv = NULL;
1826 int bdrv_flags;
92d48558 1827 Error *err = NULL;
666daa68 1828
6007cdd4
MA
1829 blk = blk_by_name(device);
1830 if (!blk) {
333a96ec
LC
1831 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1832 return;
666daa68 1833 }
6007cdd4 1834 bs = blk_bs(blk);
333a96ec 1835
e3442099
SH
1836 aio_context = bdrv_get_aio_context(bs);
1837 aio_context_acquire(aio_context);
1838
333a96ec 1839 if (format) {
b64ec4e4 1840 drv = bdrv_find_whitelisted_format(format, bs->read_only);
666daa68 1841 if (!drv) {
333a96ec 1842 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
e3442099 1843 goto out;
666daa68
MA
1844 }
1845 }
333a96ec 1846
6007cdd4 1847 eject_device(blk, 0, &err);
84d18f06 1848 if (err) {
333a96ec 1849 error_propagate(errp, err);
e3442099 1850 goto out;
666daa68 1851 }
333a96ec 1852
528f7663 1853 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
199630b6 1854 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
333a96ec
LC
1855
1856 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
e3442099
SH
1857
1858out:
1859 aio_context_release(aio_context);
666daa68 1860}
9063f814 1861
727f005e 1862/* throttling disk I/O limits */
80047da5 1863void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
3e9fab69
BC
1864 int64_t bps_wr,
1865 int64_t iops,
1866 int64_t iops_rd,
1867 int64_t iops_wr,
1868 bool has_bps_max,
1869 int64_t bps_max,
1870 bool has_bps_rd_max,
1871 int64_t bps_rd_max,
1872 bool has_bps_wr_max,
1873 int64_t bps_wr_max,
1874 bool has_iops_max,
1875 int64_t iops_max,
1876 bool has_iops_rd_max,
1877 int64_t iops_rd_max,
1878 bool has_iops_wr_max,
2024c1df
BC
1879 int64_t iops_wr_max,
1880 bool has_iops_size,
1881 int64_t iops_size, Error **errp)
727f005e 1882{
cc0681c4 1883 ThrottleConfig cfg;
727f005e 1884 BlockDriverState *bs;
b15446fd 1885 AioContext *aio_context;
727f005e 1886
80047da5 1887 bs = bdrv_find(device);
727f005e 1888 if (!bs) {
80047da5
LC
1889 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1890 return;
727f005e
ZYW
1891 }
1892
cc0681c4
BC
1893 memset(&cfg, 0, sizeof(cfg));
1894 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1895 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
1896 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
1897
1898 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1899 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
1900 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
1901
3e9fab69
BC
1902 if (has_bps_max) {
1903 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
1904 }
1905 if (has_bps_rd_max) {
1906 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
1907 }
1908 if (has_bps_wr_max) {
1909 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
1910 }
1911 if (has_iops_max) {
1912 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
1913 }
1914 if (has_iops_rd_max) {
1915 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
1916 }
1917 if (has_iops_wr_max) {
1918 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
1919 }
727f005e 1920
2024c1df
BC
1921 if (has_iops_size) {
1922 cfg.op_size = iops_size;
1923 }
cc0681c4
BC
1924
1925 if (!check_throttle_config(&cfg, errp)) {
80047da5 1926 return;
727f005e
ZYW
1927 }
1928
b15446fd
SH
1929 aio_context = bdrv_get_aio_context(bs);
1930 aio_context_acquire(aio_context);
1931
cc0681c4 1932 if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
727f005e 1933 bdrv_io_limits_enable(bs);
cc0681c4 1934 } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
727f005e 1935 bdrv_io_limits_disable(bs);
cc0681c4
BC
1936 }
1937
1938 if (bs->io_limits_enabled) {
1939 bdrv_set_io_limits(bs, &cfg);
727f005e 1940 }
b15446fd
SH
1941
1942 aio_context_release(aio_context);
727f005e
ZYW
1943}
1944
3e5a50d6 1945int hmp_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
9063f814
RH
1946{
1947 const char *id = qdict_get_str(qdict, "id");
7e7d56d9 1948 BlockBackend *blk;
9063f814 1949 BlockDriverState *bs;
8ad4202b 1950 AioContext *aio_context;
3718d8ab 1951 Error *local_err = NULL;
9063f814 1952
7e7d56d9
MA
1953 blk = blk_by_name(id);
1954 if (!blk) {
b1422f20 1955 error_report("Device '%s' not found", id);
9063f814
RH
1956 return -1;
1957 }
7e7d56d9 1958 bs = blk_bs(blk);
8ad4202b 1959
26f8b3a8 1960 if (!blk_legacy_dinfo(blk)) {
48f364dd
MA
1961 error_report("Deleting device added with blockdev-add"
1962 " is not supported");
1963 return -1;
1964 }
1965
8ad4202b
SH
1966 aio_context = bdrv_get_aio_context(bs);
1967 aio_context_acquire(aio_context);
1968
3718d8ab 1969 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
565f65d2 1970 error_report_err(local_err);
8ad4202b 1971 aio_context_release(aio_context);
8591675f
MT
1972 return -1;
1973 }
9063f814
RH
1974
1975 /* quiesce block driver; prevent further io */
922453bc 1976 bdrv_drain_all();
9063f814
RH
1977 bdrv_flush(bs);
1978 bdrv_close(bs);
1979
fa879d62 1980 /* if we have a device attached to this BlockDriverState
d22b2f41
RH
1981 * then we need to make the drive anonymous until the device
1982 * can be removed. If this is a drive with no device backing
1983 * then we can just get rid of the block driver state right here.
1984 */
a7f53e26 1985 if (blk_get_attached_dev(blk)) {
3e5a50d6 1986 blk_hide_on_behalf_of_hmp_drive_del(blk);
293c51a6
SH
1987 /* Further I/O must not pause the guest */
1988 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
1989 BLOCKDEV_ON_ERROR_REPORT);
d22b2f41 1990 } else {
b9fe8a7a 1991 blk_unref(blk);
9063f814
RH
1992 }
1993
8ad4202b 1994 aio_context_release(aio_context);
9063f814
RH
1995 return 0;
1996}
6d4a2b3a 1997
3b1dbd11
BC
1998void qmp_block_resize(bool has_device, const char *device,
1999 bool has_node_name, const char *node_name,
2000 int64_t size, Error **errp)
6d4a2b3a 2001{
3b1dbd11 2002 Error *local_err = NULL;
6d4a2b3a 2003 BlockDriverState *bs;
927e0e76 2004 AioContext *aio_context;
8732901e 2005 int ret;
6d4a2b3a 2006
3b1dbd11
BC
2007 bs = bdrv_lookup_bs(has_device ? device : NULL,
2008 has_node_name ? node_name : NULL,
2009 &local_err);
84d18f06 2010 if (local_err) {
3b1dbd11
BC
2011 error_propagate(errp, local_err);
2012 return;
2013 }
2014
927e0e76
SH
2015 aio_context = bdrv_get_aio_context(bs);
2016 aio_context_acquire(aio_context);
2017
3b1dbd11
BC
2018 if (!bdrv_is_first_non_filter(bs)) {
2019 error_set(errp, QERR_FEATURE_DISABLED, "resize");
927e0e76 2020 goto out;
6d4a2b3a
CH
2021 }
2022
2023 if (size < 0) {
939a1cc3 2024 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
927e0e76 2025 goto out;
6d4a2b3a
CH
2026 }
2027
9c75e168
JC
2028 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2029 error_set(errp, QERR_DEVICE_IN_USE, device);
927e0e76 2030 goto out;
9c75e168
JC
2031 }
2032
92b7a08d
PL
2033 /* complete all in-flight operations before resizing the device */
2034 bdrv_drain_all();
2035
8732901e
KW
2036 ret = bdrv_truncate(bs, size);
2037 switch (ret) {
939a1cc3
SH
2038 case 0:
2039 break;
2040 case -ENOMEDIUM:
2041 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2042 break;
2043 case -ENOTSUP:
2044 error_set(errp, QERR_UNSUPPORTED);
2045 break;
2046 case -EACCES:
2047 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
2048 break;
2049 case -EBUSY:
2050 error_set(errp, QERR_DEVICE_IN_USE, device);
2051 break;
2052 default:
8732901e 2053 error_setg_errno(errp, -ret, "Could not resize");
939a1cc3 2054 break;
6d4a2b3a 2055 }
927e0e76
SH
2056
2057out:
2058 aio_context_release(aio_context);
6d4a2b3a 2059}
12bd451f 2060
9abf2dba 2061static void block_job_cb(void *opaque, int ret)
12bd451f 2062{
723c5d93
SH
2063 /* Note that this function may be executed from another AioContext besides
2064 * the QEMU main loop. If you need to access anything that assumes the
2065 * QEMU global mutex, use a BH or introduce a mutex.
2066 */
2067
12bd451f 2068 BlockDriverState *bs = opaque;
bcada37b 2069 const char *msg = NULL;
12bd451f 2070
9abf2dba 2071 trace_block_job_cb(bs, bs->job, ret);
12bd451f
SH
2072
2073 assert(bs->job);
bcada37b 2074
12bd451f 2075 if (ret < 0) {
bcada37b 2076 msg = strerror(-ret);
12bd451f
SH
2077 }
2078
370521a1 2079 if (block_job_is_cancelled(bs->job)) {
bcada37b 2080 block_job_event_cancelled(bs->job);
370521a1 2081 } else {
bcada37b 2082 block_job_event_completed(bs->job, msg);
370521a1 2083 }
aa398a5c 2084
fa510ebf 2085 bdrv_put_ref_bh_schedule(bs);
12bd451f
SH
2086}
2087
13d8cc51
JC
2088void qmp_block_stream(const char *device,
2089 bool has_base, const char *base,
2090 bool has_backing_file, const char *backing_file,
2091 bool has_speed, int64_t speed,
1d809098
PB
2092 bool has_on_error, BlockdevOnError on_error,
2093 Error **errp)
12bd451f
SH
2094{
2095 BlockDriverState *bs;
c8c3080f 2096 BlockDriverState *base_bs = NULL;
f3e69beb 2097 AioContext *aio_context;
fd7f8c65 2098 Error *local_err = NULL;
13d8cc51 2099 const char *base_name = NULL;
12bd451f 2100
1d809098
PB
2101 if (!has_on_error) {
2102 on_error = BLOCKDEV_ON_ERROR_REPORT;
2103 }
2104
12bd451f
SH
2105 bs = bdrv_find(device);
2106 if (!bs) {
2107 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2108 return;
2109 }
2110
f3e69beb
SH
2111 aio_context = bdrv_get_aio_context(bs);
2112 aio_context_acquire(aio_context);
2113
628ff683 2114 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
f3e69beb 2115 goto out;
628ff683
FZ
2116 }
2117
13d8cc51 2118 if (has_base) {
c8c3080f
MT
2119 base_bs = bdrv_find_backing_image(bs, base);
2120 if (base_bs == NULL) {
2121 error_set(errp, QERR_BASE_NOT_FOUND, base);
f3e69beb 2122 goto out;
c8c3080f 2123 }
f3e69beb 2124 assert(bdrv_get_aio_context(base_bs) == aio_context);
13d8cc51 2125 base_name = base;
12bd451f
SH
2126 }
2127
13d8cc51
JC
2128 /* if we are streaming the entire chain, the result will have no backing
2129 * file, and specifying one is therefore an error */
2130 if (base_bs == NULL && has_backing_file) {
2131 error_setg(errp, "backing file specified, but streaming the "
2132 "entire chain");
f3e69beb 2133 goto out;
13d8cc51
JC
2134 }
2135
2136 /* backing_file string overrides base bs filename */
2137 base_name = has_backing_file ? backing_file : base_name;
2138
2139 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
1d809098 2140 on_error, block_job_cb, bs, &local_err);
84d18f06 2141 if (local_err) {
fd7f8c65 2142 error_propagate(errp, local_err);
f3e69beb 2143 goto out;
12bd451f
SH
2144 }
2145
2146 trace_qmp_block_stream(bs, bs->job);
f3e69beb
SH
2147
2148out:
2149 aio_context_release(aio_context);
12bd451f 2150}
2d47c6e9 2151
ed61fc10 2152void qmp_block_commit(const char *device,
7676e2c5
JC
2153 bool has_base, const char *base,
2154 bool has_top, const char *top,
54e26900 2155 bool has_backing_file, const char *backing_file,
ed61fc10
JC
2156 bool has_speed, int64_t speed,
2157 Error **errp)
2158{
2159 BlockDriverState *bs;
2160 BlockDriverState *base_bs, *top_bs;
9e85cd5c 2161 AioContext *aio_context;
ed61fc10
JC
2162 Error *local_err = NULL;
2163 /* This will be part of the QMP command, if/when the
2164 * BlockdevOnError change for blkmirror makes it in
2165 */
92aa5c6d 2166 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
ed61fc10 2167
54504663
HR
2168 if (!has_speed) {
2169 speed = 0;
2170 }
2171
7676e2c5
JC
2172 /* Important Note:
2173 * libvirt relies on the DeviceNotFound error class in order to probe for
2174 * live commit feature versions; for this to work, we must make sure to
2175 * perform the device lookup before any generic errors that may occur in a
2176 * scenario in which all optional arguments are omitted. */
ed61fc10
JC
2177 bs = bdrv_find(device);
2178 if (!bs) {
2179 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2180 return;
628ff683
FZ
2181 }
2182
9e85cd5c
SH
2183 aio_context = bdrv_get_aio_context(bs);
2184 aio_context_acquire(aio_context);
2185
2186 /* drain all i/o before commits */
2187 bdrv_drain_all();
2188
bb00021d 2189 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
9e85cd5c 2190 goto out;
ed61fc10 2191 }
ed61fc10
JC
2192
2193 /* default top_bs is the active layer */
2194 top_bs = bs;
2195
7676e2c5 2196 if (has_top && top) {
ed61fc10
JC
2197 if (strcmp(bs->filename, top) != 0) {
2198 top_bs = bdrv_find_backing_image(bs, top);
2199 }
2200 }
2201
2202 if (top_bs == NULL) {
2203 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
9e85cd5c 2204 goto out;
ed61fc10
JC
2205 }
2206
9e85cd5c
SH
2207 assert(bdrv_get_aio_context(top_bs) == aio_context);
2208
d5208c45
JC
2209 if (has_base && base) {
2210 base_bs = bdrv_find_backing_image(top_bs, base);
2211 } else {
2212 base_bs = bdrv_find_base(top_bs);
2213 }
2214
2215 if (base_bs == NULL) {
2216 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
9e85cd5c 2217 goto out;
d5208c45
JC
2218 }
2219
9e85cd5c
SH
2220 assert(bdrv_get_aio_context(base_bs) == aio_context);
2221
bb00021d
FZ
2222 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2223 goto out;
2224 }
2225
7676e2c5
JC
2226 /* Do not allow attempts to commit an image into itself */
2227 if (top_bs == base_bs) {
2228 error_setg(errp, "cannot commit an image into itself");
9e85cd5c 2229 goto out;
7676e2c5
JC
2230 }
2231
20a63d2c 2232 if (top_bs == bs) {
54e26900
JC
2233 if (has_backing_file) {
2234 error_setg(errp, "'backing-file' specified,"
2235 " but 'top' is the active layer");
9e85cd5c 2236 goto out;
54e26900 2237 }
20a63d2c
FZ
2238 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
2239 bs, &local_err);
2240 } else {
2241 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
54e26900 2242 has_backing_file ? backing_file : NULL, &local_err);
20a63d2c 2243 }
ed61fc10
JC
2244 if (local_err != NULL) {
2245 error_propagate(errp, local_err);
9e85cd5c 2246 goto out;
ed61fc10 2247 }
9e85cd5c
SH
2248
2249out:
2250 aio_context_release(aio_context);
ed61fc10
JC
2251}
2252
99a9addf
SH
2253void qmp_drive_backup(const char *device, const char *target,
2254 bool has_format, const char *format,
b53169ea 2255 enum MirrorSyncMode sync,
99a9addf
SH
2256 bool has_mode, enum NewImageMode mode,
2257 bool has_speed, int64_t speed,
2258 bool has_on_source_error, BlockdevOnError on_source_error,
2259 bool has_on_target_error, BlockdevOnError on_target_error,
2260 Error **errp)
2261{
2262 BlockDriverState *bs;
2263 BlockDriverState *target_bs;
fc5d3f84 2264 BlockDriverState *source = NULL;
761731b1 2265 AioContext *aio_context;
99a9addf
SH
2266 BlockDriver *drv = NULL;
2267 Error *local_err = NULL;
2268 int flags;
2269 int64_t size;
2270 int ret;
2271
2272 if (!has_speed) {
2273 speed = 0;
2274 }
2275 if (!has_on_source_error) {
2276 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2277 }
2278 if (!has_on_target_error) {
2279 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2280 }
2281 if (!has_mode) {
2282 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2283 }
2284
2285 bs = bdrv_find(device);
2286 if (!bs) {
2287 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2288 return;
2289 }
2290
761731b1
SH
2291 aio_context = bdrv_get_aio_context(bs);
2292 aio_context_acquire(aio_context);
2293
c29c1dd3
FZ
2294 /* Although backup_run has this check too, we need to use bs->drv below, so
2295 * do an early check redundantly. */
99a9addf
SH
2296 if (!bdrv_is_inserted(bs)) {
2297 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
761731b1 2298 goto out;
99a9addf
SH
2299 }
2300
2301 if (!has_format) {
2302 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2303 }
2304 if (format) {
2305 drv = bdrv_find_format(format);
2306 if (!drv) {
2307 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
761731b1 2308 goto out;
99a9addf
SH
2309 }
2310 }
2311
c29c1dd3 2312 /* Early check to avoid creating target */
3718d8ab 2313 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
761731b1 2314 goto out;
99a9addf
SH
2315 }
2316
2317 flags = bs->open_flags | BDRV_O_RDWR;
2318
fc5d3f84
IM
2319 /* See if we have a backing HD we can use to create our new image
2320 * on top of. */
2321 if (sync == MIRROR_SYNC_MODE_TOP) {
2322 source = bs->backing_hd;
2323 if (!source) {
2324 sync = MIRROR_SYNC_MODE_FULL;
2325 }
2326 }
2327 if (sync == MIRROR_SYNC_MODE_NONE) {
2328 source = bs;
2329 }
2330
99a9addf
SH
2331 size = bdrv_getlength(bs);
2332 if (size < 0) {
2333 error_setg_errno(errp, -size, "bdrv_getlength failed");
761731b1 2334 goto out;
99a9addf
SH
2335 }
2336
2337 if (mode != NEW_IMAGE_MODE_EXISTING) {
2338 assert(format && drv);
fc5d3f84
IM
2339 if (source) {
2340 bdrv_img_create(target, format, source->filename,
2341 source->drv->format_name, NULL,
2342 size, flags, &local_err, false);
2343 } else {
2344 bdrv_img_create(target, format, NULL, NULL, NULL,
2345 size, flags, &local_err, false);
2346 }
99a9addf
SH
2347 }
2348
84d18f06 2349 if (local_err) {
99a9addf 2350 error_propagate(errp, local_err);
761731b1 2351 goto out;
99a9addf
SH
2352 }
2353
f67503e5 2354 target_bs = NULL;
ddf5636d 2355 ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err);
99a9addf 2356 if (ret < 0) {
34b5d2c6 2357 error_propagate(errp, local_err);
761731b1 2358 goto out;
99a9addf
SH
2359 }
2360
761731b1
SH
2361 bdrv_set_aio_context(target_bs, aio_context);
2362
fc5d3f84 2363 backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
99a9addf
SH
2364 block_job_cb, bs, &local_err);
2365 if (local_err != NULL) {
4f6fd349 2366 bdrv_unref(target_bs);
99a9addf 2367 error_propagate(errp, local_err);
761731b1 2368 goto out;
99a9addf 2369 }
761731b1
SH
2370
2371out:
2372 aio_context_release(aio_context);
99a9addf
SH
2373}
2374
c13163fb
BC
2375BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
2376{
2377 return bdrv_named_nodes_list();
2378}
2379
c29c1dd3
FZ
2380void qmp_blockdev_backup(const char *device, const char *target,
2381 enum MirrorSyncMode sync,
2382 bool has_speed, int64_t speed,
2383 bool has_on_source_error,
2384 BlockdevOnError on_source_error,
2385 bool has_on_target_error,
2386 BlockdevOnError on_target_error,
2387 Error **errp)
2388{
2389 BlockDriverState *bs;
2390 BlockDriverState *target_bs;
2391 Error *local_err = NULL;
2392 AioContext *aio_context;
2393
2394 if (!has_speed) {
2395 speed = 0;
2396 }
2397 if (!has_on_source_error) {
2398 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2399 }
2400 if (!has_on_target_error) {
2401 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2402 }
2403
2404 bs = bdrv_find(device);
2405 if (!bs) {
2406 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2407 return;
2408 }
2409
2410 aio_context = bdrv_get_aio_context(bs);
2411 aio_context_acquire(aio_context);
2412
2413 target_bs = bdrv_find(target);
2414 if (!target_bs) {
2415 error_set(errp, QERR_DEVICE_NOT_FOUND, target);
2416 goto out;
2417 }
2418
2419 bdrv_ref(target_bs);
2420 bdrv_set_aio_context(target_bs, aio_context);
2421 backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
2422 block_job_cb, bs, &local_err);
2423 if (local_err != NULL) {
2424 bdrv_unref(target_bs);
2425 error_propagate(errp, local_err);
2426 }
2427out:
2428 aio_context_release(aio_context);
2429}
2430
08e4ed6c
PB
2431#define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2432
d9b902db
PB
2433void qmp_drive_mirror(const char *device, const char *target,
2434 bool has_format, const char *format,
4c828dc6 2435 bool has_node_name, const char *node_name,
09158f00 2436 bool has_replaces, const char *replaces,
d9b902db
PB
2437 enum MirrorSyncMode sync,
2438 bool has_mode, enum NewImageMode mode,
b952b558 2439 bool has_speed, int64_t speed,
eee13dfe 2440 bool has_granularity, uint32_t granularity,
08e4ed6c 2441 bool has_buf_size, int64_t buf_size,
b952b558
PB
2442 bool has_on_source_error, BlockdevOnError on_source_error,
2443 bool has_on_target_error, BlockdevOnError on_target_error,
2444 Error **errp)
d9b902db 2445{
d9b902db
PB
2446 BlockDriverState *bs;
2447 BlockDriverState *source, *target_bs;
5a7e7a0b 2448 AioContext *aio_context;
d9b902db
PB
2449 BlockDriver *drv = NULL;
2450 Error *local_err = NULL;
4c828dc6 2451 QDict *options = NULL;
d9b902db 2452 int flags;
ac3c5d83 2453 int64_t size;
d9b902db
PB
2454 int ret;
2455
2456 if (!has_speed) {
2457 speed = 0;
2458 }
b952b558
PB
2459 if (!has_on_source_error) {
2460 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2461 }
2462 if (!has_on_target_error) {
2463 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2464 }
d9b902db
PB
2465 if (!has_mode) {
2466 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2467 }
eee13dfe
PB
2468 if (!has_granularity) {
2469 granularity = 0;
2470 }
08e4ed6c
PB
2471 if (!has_buf_size) {
2472 buf_size = DEFAULT_MIRROR_BUF_SIZE;
2473 }
2474
eee13dfe 2475 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3cbbe9fd
SH
2476 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2477 "a value in range [512B, 64MB]");
eee13dfe
PB
2478 return;
2479 }
2480 if (granularity & (granularity - 1)) {
3cbbe9fd 2481 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2");
eee13dfe
PB
2482 return;
2483 }
d9b902db
PB
2484
2485 bs = bdrv_find(device);
2486 if (!bs) {
2487 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2488 return;
2489 }
2490
5a7e7a0b
SH
2491 aio_context = bdrv_get_aio_context(bs);
2492 aio_context_acquire(aio_context);
2493
d9b902db
PB
2494 if (!bdrv_is_inserted(bs)) {
2495 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
5a7e7a0b 2496 goto out;
d9b902db
PB
2497 }
2498
2499 if (!has_format) {
2500 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2501 }
2502 if (format) {
2503 drv = bdrv_find_format(format);
2504 if (!drv) {
2505 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
5a7e7a0b 2506 goto out;
d9b902db
PB
2507 }
2508 }
2509
3718d8ab 2510 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
5a7e7a0b 2511 goto out;
d9b902db
PB
2512 }
2513
2514 flags = bs->open_flags | BDRV_O_RDWR;
2515 source = bs->backing_hd;
2516 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
2517 sync = MIRROR_SYNC_MODE_FULL;
2518 }
117e0c82
HR
2519 if (sync == MIRROR_SYNC_MODE_NONE) {
2520 source = bs;
2521 }
d9b902db 2522
ac3c5d83
SH
2523 size = bdrv_getlength(bs);
2524 if (size < 0) {
2525 error_setg_errno(errp, -size, "bdrv_getlength failed");
5a7e7a0b 2526 goto out;
ac3c5d83
SH
2527 }
2528
09158f00
BC
2529 if (has_replaces) {
2530 BlockDriverState *to_replace_bs;
5a7e7a0b
SH
2531 AioContext *replace_aio_context;
2532 int64_t replace_size;
09158f00
BC
2533
2534 if (!has_node_name) {
2535 error_setg(errp, "a node-name must be provided when replacing a"
2536 " named node of the graph");
5a7e7a0b 2537 goto out;
09158f00
BC
2538 }
2539
2540 to_replace_bs = check_to_replace_node(replaces, &local_err);
2541
2542 if (!to_replace_bs) {
2543 error_propagate(errp, local_err);
5a7e7a0b 2544 goto out;
09158f00
BC
2545 }
2546
5a7e7a0b
SH
2547 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
2548 aio_context_acquire(replace_aio_context);
2549 replace_size = bdrv_getlength(to_replace_bs);
2550 aio_context_release(replace_aio_context);
2551
2552 if (size != replace_size) {
09158f00
BC
2553 error_setg(errp, "cannot replace image with a mirror image of "
2554 "different size");
5a7e7a0b 2555 goto out;
09158f00
BC
2556 }
2557 }
2558
14526864
HR
2559 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
2560 && mode != NEW_IMAGE_MODE_EXISTING)
2561 {
d9b902db
PB
2562 /* create new image w/o backing file */
2563 assert(format && drv);
cf8f2426 2564 bdrv_img_create(target, format,
f382d43a 2565 NULL, NULL, NULL, size, flags, &local_err, false);
d9b902db
PB
2566 } else {
2567 switch (mode) {
2568 case NEW_IMAGE_MODE_EXISTING:
d9b902db
PB
2569 break;
2570 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2571 /* create new image with backing file */
cf8f2426
LC
2572 bdrv_img_create(target, format,
2573 source->filename,
2574 source->drv->format_name,
f382d43a 2575 NULL, size, flags, &local_err, false);
d9b902db
PB
2576 break;
2577 default:
2578 abort();
2579 }
2580 }
2581
84d18f06 2582 if (local_err) {
cf8f2426 2583 error_propagate(errp, local_err);
5a7e7a0b 2584 goto out;
d9b902db
PB
2585 }
2586
4c828dc6
BC
2587 if (has_node_name) {
2588 options = qdict_new();
2589 qdict_put(options, "node-name", qstring_from_str(node_name));
2590 }
2591
b812f671
PB
2592 /* Mirroring takes care of copy-on-write using the source's backing
2593 * file.
2594 */
f67503e5 2595 target_bs = NULL;
4c828dc6
BC
2596 ret = bdrv_open(&target_bs, target, NULL, options,
2597 flags | BDRV_O_NO_BACKING, drv, &local_err);
d9b902db 2598 if (ret < 0) {
34b5d2c6 2599 error_propagate(errp, local_err);
5a7e7a0b 2600 goto out;
d9b902db
PB
2601 }
2602
5a7e7a0b
SH
2603 bdrv_set_aio_context(target_bs, aio_context);
2604
09158f00
BC
2605 /* pass the node name to replace to mirror start since it's loose coupling
2606 * and will allow to check whether the node still exist at mirror completion
2607 */
2608 mirror_start(bs, target_bs,
2609 has_replaces ? replaces : NULL,
2610 speed, granularity, buf_size, sync,
eee13dfe 2611 on_source_error, on_target_error,
b952b558 2612 block_job_cb, bs, &local_err);
d9b902db 2613 if (local_err != NULL) {
4f6fd349 2614 bdrv_unref(target_bs);
d9b902db 2615 error_propagate(errp, local_err);
5a7e7a0b 2616 goto out;
d9b902db 2617 }
5a7e7a0b
SH
2618
2619out:
2620 aio_context_release(aio_context);
d9b902db
PB
2621}
2622
3d948cdf 2623/* Get the block job for a given device name and acquire its AioContext */
24d6bffe
MA
2624static BlockJob *find_block_job(const char *device, AioContext **aio_context,
2625 Error **errp)
2d47c6e9
SH
2626{
2627 BlockDriverState *bs;
2628
2629 bs = bdrv_find(device);
3d948cdf
SH
2630 if (!bs) {
2631 goto notfound;
2632 }
2633
2634 *aio_context = bdrv_get_aio_context(bs);
2635 aio_context_acquire(*aio_context);
2636
2637 if (!bs->job) {
2638 aio_context_release(*aio_context);
2639 goto notfound;
2d47c6e9 2640 }
3d948cdf 2641
2d47c6e9 2642 return bs->job;
3d948cdf
SH
2643
2644notfound:
2e3a0266
MA
2645 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
2646 "No active block job on device '%s'", device);
3d948cdf
SH
2647 *aio_context = NULL;
2648 return NULL;
2d47c6e9
SH
2649}
2650
882ec7ce 2651void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2d47c6e9 2652{
3d948cdf 2653 AioContext *aio_context;
24d6bffe 2654 BlockJob *job = find_block_job(device, &aio_context, errp);
2d47c6e9
SH
2655
2656 if (!job) {
2d47c6e9
SH
2657 return;
2658 }
2659
882ec7ce 2660 block_job_set_speed(job, speed, errp);
3d948cdf 2661 aio_context_release(aio_context);
2d47c6e9 2662}
370521a1 2663
6e37fb81
PB
2664void qmp_block_job_cancel(const char *device,
2665 bool has_force, bool force, Error **errp)
370521a1 2666{
3d948cdf 2667 AioContext *aio_context;
24d6bffe 2668 BlockJob *job = find_block_job(device, &aio_context, errp);
6e37fb81 2669
370521a1 2670 if (!job) {
370521a1
SH
2671 return;
2672 }
3d948cdf
SH
2673
2674 if (!has_force) {
2675 force = false;
2676 }
2677
6e37fb81 2678 if (job->paused && !force) {
f231b88d
CR
2679 error_setg(errp, "The block job for device '%s' is currently paused",
2680 device);
3d948cdf 2681 goto out;
8acc72a4 2682 }
370521a1
SH
2683
2684 trace_qmp_block_job_cancel(job);
2685 block_job_cancel(job);
3d948cdf
SH
2686out:
2687 aio_context_release(aio_context);
370521a1 2688}
fb5458cd 2689
6e37fb81
PB
2690void qmp_block_job_pause(const char *device, Error **errp)
2691{
3d948cdf 2692 AioContext *aio_context;
24d6bffe 2693 BlockJob *job = find_block_job(device, &aio_context, errp);
6e37fb81
PB
2694
2695 if (!job) {
6e37fb81
PB
2696 return;
2697 }
2698
2699 trace_qmp_block_job_pause(job);
2700 block_job_pause(job);
3d948cdf 2701 aio_context_release(aio_context);
6e37fb81
PB
2702}
2703
2704void qmp_block_job_resume(const char *device, Error **errp)
2705{
3d948cdf 2706 AioContext *aio_context;
24d6bffe 2707 BlockJob *job = find_block_job(device, &aio_context, errp);
6e37fb81
PB
2708
2709 if (!job) {
6e37fb81
PB
2710 return;
2711 }
2712
2713 trace_qmp_block_job_resume(job);
2714 block_job_resume(job);
3d948cdf 2715 aio_context_release(aio_context);
6e37fb81
PB
2716}
2717
aeae883b
PB
2718void qmp_block_job_complete(const char *device, Error **errp)
2719{
3d948cdf 2720 AioContext *aio_context;
24d6bffe 2721 BlockJob *job = find_block_job(device, &aio_context, errp);
aeae883b
PB
2722
2723 if (!job) {
aeae883b
PB
2724 return;
2725 }
2726
2727 trace_qmp_block_job_complete(job);
2728 block_job_complete(job, errp);
3d948cdf 2729 aio_context_release(aio_context);
aeae883b
PB
2730}
2731
fa40e656
JC
2732void qmp_change_backing_file(const char *device,
2733 const char *image_node_name,
2734 const char *backing_file,
2735 Error **errp)
2736{
2737 BlockDriverState *bs = NULL;
729962f6 2738 AioContext *aio_context;
fa40e656
JC
2739 BlockDriverState *image_bs = NULL;
2740 Error *local_err = NULL;
2741 bool ro;
2742 int open_flags;
2743 int ret;
2744
2745 /* find the top layer BDS of the chain */
2746 bs = bdrv_find(device);
2747 if (!bs) {
2748 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2749 return;
2750 }
2751
729962f6
SH
2752 aio_context = bdrv_get_aio_context(bs);
2753 aio_context_acquire(aio_context);
2754
fa40e656
JC
2755 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
2756 if (local_err) {
2757 error_propagate(errp, local_err);
729962f6 2758 goto out;
fa40e656
JC
2759 }
2760
2761 if (!image_bs) {
2762 error_setg(errp, "image file not found");
729962f6 2763 goto out;
fa40e656
JC
2764 }
2765
2766 if (bdrv_find_base(image_bs) == image_bs) {
2767 error_setg(errp, "not allowing backing file change on an image "
2768 "without a backing file");
729962f6 2769 goto out;
fa40e656
JC
2770 }
2771
2772 /* even though we are not necessarily operating on bs, we need it to
2773 * determine if block ops are currently prohibited on the chain */
2774 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
729962f6 2775 goto out;
fa40e656
JC
2776 }
2777
2778 /* final sanity check */
2779 if (!bdrv_chain_contains(bs, image_bs)) {
2780 error_setg(errp, "'%s' and image file are not in the same chain",
2781 device);
729962f6 2782 goto out;
fa40e656
JC
2783 }
2784
2785 /* if not r/w, reopen to make r/w */
2786 open_flags = image_bs->open_flags;
2787 ro = bdrv_is_read_only(image_bs);
2788
2789 if (ro) {
2790 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
2791 if (local_err) {
2792 error_propagate(errp, local_err);
729962f6 2793 goto out;
fa40e656
JC
2794 }
2795 }
2796
2797 ret = bdrv_change_backing_file(image_bs, backing_file,
2798 image_bs->drv ? image_bs->drv->format_name : "");
2799
2800 if (ret < 0) {
2801 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
2802 backing_file);
2803 /* don't exit here, so we can try to restore open flags if
2804 * appropriate */
2805 }
2806
2807 if (ro) {
2808 bdrv_reopen(image_bs, open_flags, &local_err);
2809 if (local_err) {
2810 error_propagate(errp, local_err); /* will preserve prior errp */
2811 }
2812 }
729962f6
SH
2813
2814out:
2815 aio_context_release(aio_context);
fa40e656
JC
2816}
2817
d26c9a15
KW
2818void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
2819{
2820 QmpOutputVisitor *ov = qmp_output_visitor_new();
18e46a03 2821 BlockBackend *blk;
d26c9a15
KW
2822 QObject *obj;
2823 QDict *qdict;
d26c9a15
KW
2824 Error *local_err = NULL;
2825
2826 /* Require an ID in the top level */
2827 if (!options->has_id) {
2828 error_setg(errp, "Block device needs an ID");
2829 goto fail;
2830 }
2831
60e19e06 2832 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
d26c9a15 2833 * cache.direct=false instead of silently switching to aio=threads, except
60e19e06 2834 * when called from drive_new().
d26c9a15
KW
2835 *
2836 * For now, simply forbidding the combination for all drivers will do. */
2837 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
c6e0bd9b
KW
2838 bool direct = options->has_cache &&
2839 options->cache->has_direct &&
2840 options->cache->direct;
2841 if (!direct) {
d26c9a15
KW
2842 error_setg(errp, "aio=native requires cache.direct=true");
2843 goto fail;
2844 }
2845 }
2846
2847 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
2848 &options, NULL, &local_err);
84d18f06 2849 if (local_err) {
d26c9a15
KW
2850 error_propagate(errp, local_err);
2851 goto fail;
2852 }
2853
2854 obj = qmp_output_get_qobject(ov);
2855 qdict = qobject_to_qdict(obj);
2856
2857 qdict_flatten(qdict);
2858
18e46a03 2859 blk = blockdev_init(NULL, qdict, &local_err);
84d18f06 2860 if (local_err) {
b681072d 2861 error_propagate(errp, local_err);
d26c9a15
KW
2862 goto fail;
2863 }
2864
18e46a03 2865 if (bdrv_key_required(blk_bs(blk))) {
18e46a03 2866 blk_unref(blk);
8ae8e904
KW
2867 error_setg(errp, "blockdev-add doesn't support encrypted devices");
2868 goto fail;
2869 }
2870
d26c9a15
KW
2871fail:
2872 qmp_output_visitor_cleanup(ov);
2873}
2874
fea68bb6 2875BlockJobInfoList *qmp_query_block_jobs(Error **errp)
fb5458cd 2876{
fea68bb6
MA
2877 BlockJobInfoList *head = NULL, **p_next = &head;
2878 BlockDriverState *bs;
fb5458cd 2879
fea68bb6 2880 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
69691e72
SH
2881 AioContext *aio_context = bdrv_get_aio_context(bs);
2882
2883 aio_context_acquire(aio_context);
2884
fea68bb6
MA
2885 if (bs->job) {
2886 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
2887 elem->value = block_job_query(bs->job);
2888 *p_next = elem;
2889 p_next = &elem->next;
2890 }
69691e72
SH
2891
2892 aio_context_release(aio_context);
fb5458cd 2893 }
fb5458cd 2894
fea68bb6 2895 return head;
fb5458cd 2896}
4d454574 2897
0006383e 2898QemuOptsList qemu_common_drive_opts = {
4d454574 2899 .name = "drive",
0006383e 2900 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4d454574
PB
2901 .desc = {
2902 {
4d454574
PB
2903 .name = "snapshot",
2904 .type = QEMU_OPT_BOOL,
2905 .help = "enable/disable snapshot mode",
a9384aff
PB
2906 },{
2907 .name = "discard",
2908 .type = QEMU_OPT_STRING,
2909 .help = "discard operation (ignore/off, unmap/on)",
4d454574 2910 },{
29c4e2b5
KW
2911 .name = "cache.writeback",
2912 .type = QEMU_OPT_BOOL,
2913 .help = "enables writeback mode for any caches",
2914 },{
2915 .name = "cache.direct",
2916 .type = QEMU_OPT_BOOL,
2917 .help = "enables use of O_DIRECT (bypass the host page cache)",
2918 },{
2919 .name = "cache.no-flush",
2920 .type = QEMU_OPT_BOOL,
2921 .help = "ignore any flush requests for the device",
4d454574
PB
2922 },{
2923 .name = "aio",
2924 .type = QEMU_OPT_STRING,
2925 .help = "host AIO implementation (threads, native)",
2926 },{
2927 .name = "format",
2928 .type = QEMU_OPT_STRING,
2929 .help = "disk format (raw, qcow2, ...)",
4d454574
PB
2930 },{
2931 .name = "rerror",
2932 .type = QEMU_OPT_STRING,
2933 .help = "read error action",
2934 },{
2935 .name = "werror",
2936 .type = QEMU_OPT_STRING,
2937 .help = "write error action",
4d454574 2938 },{
0f227a94 2939 .name = "read-only",
4d454574
PB
2940 .type = QEMU_OPT_BOOL,
2941 .help = "open drive file as read-only",
2942 },{
57975222 2943 .name = "throttling.iops-total",
4d454574
PB
2944 .type = QEMU_OPT_NUMBER,
2945 .help = "limit total I/O operations per second",
2946 },{
57975222 2947 .name = "throttling.iops-read",
4d454574
PB
2948 .type = QEMU_OPT_NUMBER,
2949 .help = "limit read operations per second",
2950 },{
57975222 2951 .name = "throttling.iops-write",
4d454574
PB
2952 .type = QEMU_OPT_NUMBER,
2953 .help = "limit write operations per second",
2954 },{
57975222 2955 .name = "throttling.bps-total",
4d454574
PB
2956 .type = QEMU_OPT_NUMBER,
2957 .help = "limit total bytes per second",
2958 },{
57975222 2959 .name = "throttling.bps-read",
4d454574
PB
2960 .type = QEMU_OPT_NUMBER,
2961 .help = "limit read bytes per second",
2962 },{
57975222 2963 .name = "throttling.bps-write",
4d454574
PB
2964 .type = QEMU_OPT_NUMBER,
2965 .help = "limit write bytes per second",
3e9fab69
BC
2966 },{
2967 .name = "throttling.iops-total-max",
2968 .type = QEMU_OPT_NUMBER,
2969 .help = "I/O operations burst",
2970 },{
2971 .name = "throttling.iops-read-max",
2972 .type = QEMU_OPT_NUMBER,
2973 .help = "I/O operations read burst",
2974 },{
2975 .name = "throttling.iops-write-max",
2976 .type = QEMU_OPT_NUMBER,
2977 .help = "I/O operations write burst",
2978 },{
2979 .name = "throttling.bps-total-max",
2980 .type = QEMU_OPT_NUMBER,
2981 .help = "total bytes burst",
2982 },{
2983 .name = "throttling.bps-read-max",
2984 .type = QEMU_OPT_NUMBER,
2985 .help = "total bytes read burst",
2986 },{
2987 .name = "throttling.bps-write-max",
2988 .type = QEMU_OPT_NUMBER,
2989 .help = "total bytes write burst",
2024c1df
BC
2990 },{
2991 .name = "throttling.iops-size",
2992 .type = QEMU_OPT_NUMBER,
2993 .help = "when limiting by iops max size of an I/O in bytes",
4d454574
PB
2994 },{
2995 .name = "copy-on-read",
2996 .type = QEMU_OPT_BOOL,
2997 .help = "copy read data from backing file into image file",
465bee1d
PL
2998 },{
2999 .name = "detect-zeroes",
3000 .type = QEMU_OPT_STRING,
3001 .help = "try to optimize zero writes (off, on, unmap)",
4d454574
PB
3002 },
3003 { /* end of list */ }
3004 },
3005};
0006383e
KW
3006
3007QemuOptsList qemu_drive_opts = {
3008 .name = "drive",
3009 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3010 .desc = {
492fdc6f
KW
3011 /*
3012 * no elements => accept any params
3013 * validation will happen later
3014 */
0006383e
KW
3015 { /* end of list */ }
3016 },
3017};