]> git.proxmox.com Git - mirror_qemu.git/blame - blockdev.c
block: Fix snapshot=on cache modes
[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
d38ea87a 33#include "qemu/osdep.h"
26f54e9a 34#include "sysemu/block-backend.h"
9c17d615 35#include "sysemu/blockdev.h"
0d09e41a 36#include "hw/block/block.h"
737e150e 37#include "block/blockjob.h"
76f4afb4 38#include "block/throttle-groups.h"
83c9089e 39#include "monitor/monitor.h"
d49b6836 40#include "qemu/error-report.h"
1de7afc9
PB
41#include "qemu/option.h"
42#include "qemu/config-file.h"
7b1b5d19 43#include "qapi/qmp/types.h"
d26c9a15 44#include "qapi-visit.h"
cc7a8ea7 45#include "qapi/qmp/qerror.h"
d26c9a15 46#include "qapi/qmp-output-visitor.h"
9e7dac7c 47#include "qapi/util.h"
9c17d615 48#include "sysemu/sysemu.h"
737e150e 49#include "block/block_int.h"
a4dea8a9 50#include "qmp-commands.h"
12bd451f 51#include "trace.h"
9c17d615 52#include "sysemu/arch_init.h"
666daa68 53
9c4218e9
HR
54static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
55 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
56
1960966d
MA
57static const char *const if_name[IF_COUNT] = {
58 [IF_NONE] = "none",
59 [IF_IDE] = "ide",
60 [IF_SCSI] = "scsi",
61 [IF_FLOPPY] = "floppy",
62 [IF_PFLASH] = "pflash",
63 [IF_MTD] = "mtd",
64 [IF_SD] = "sd",
65 [IF_VIRTIO] = "virtio",
66 [IF_XEN] = "xen",
67};
68
21dff8cf 69static int if_max_devs[IF_COUNT] = {
27d6bf40
MA
70 /*
71 * Do not change these numbers! They govern how drive option
72 * index maps to unit and bus. That mapping is ABI.
73 *
74 * All controllers used to imlement if=T drives need to support
75 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
76 * Otherwise, some index values map to "impossible" bus, unit
77 * values.
78 *
79 * For instance, if you change [IF_SCSI] to 255, -drive
80 * if=scsi,index=12 no longer means bus=1,unit=5, but
81 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
82 * the drive can't be set up. Regression.
83 */
84 [IF_IDE] = 2,
85 [IF_SCSI] = 7,
1960966d
MA
86};
87
21dff8cf
JS
88/**
89 * Boards may call this to offer board-by-board overrides
90 * of the default, global values.
91 */
92void override_max_devs(BlockInterfaceType type, int max_devs)
93{
18e46a03 94 BlockBackend *blk;
21dff8cf
JS
95 DriveInfo *dinfo;
96
97 if (max_devs <= 0) {
98 return;
99 }
100
18e46a03
MA
101 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
102 dinfo = blk_legacy_dinfo(blk);
21dff8cf
JS
103 if (dinfo->type == type) {
104 fprintf(stderr, "Cannot override units-per-bus property of"
105 " the %s interface, because a drive of that type has"
106 " already been added.\n", if_name[type]);
107 g_assert_not_reached();
108 }
109 }
110
111 if_max_devs[type] = max_devs;
112}
113
14bafc54
MA
114/*
115 * We automatically delete the drive when a device using it gets
116 * unplugged. Questionable feature, but we can't just drop it.
117 * Device models call blockdev_mark_auto_del() to schedule the
118 * automatic deletion, and generic qdev code calls blockdev_auto_del()
119 * when deletion is actually safe.
120 */
4be74634 121void blockdev_mark_auto_del(BlockBackend *blk)
14bafc54 122{
18e46a03 123 DriveInfo *dinfo = blk_legacy_dinfo(blk);
4be74634 124 BlockDriverState *bs = blk_bs(blk);
91fddb0d 125 AioContext *aio_context;
14bafc54 126
26f8b3a8 127 if (!dinfo) {
2d246f01
KW
128 return;
129 }
130
5433c24f
HR
131 if (bs) {
132 aio_context = bdrv_get_aio_context(bs);
133 aio_context_acquire(aio_context);
91fddb0d 134
5433c24f
HR
135 if (bs->job) {
136 block_job_cancel(bs->job);
137 }
91fddb0d 138
5433c24f
HR
139 aio_context_release(aio_context);
140 }
91fddb0d 141
26f8b3a8 142 dinfo->auto_del = 1;
14bafc54
MA
143}
144
4be74634 145void blockdev_auto_del(BlockBackend *blk)
14bafc54 146{
18e46a03 147 DriveInfo *dinfo = blk_legacy_dinfo(blk);
14bafc54 148
0fc0f1fa 149 if (dinfo && dinfo->auto_del) {
b9fe8a7a 150 blk_unref(blk);
14bafc54
MA
151 }
152}
153
d8f94e1b
JS
154/**
155 * Returns the current mapping of how many units per bus
156 * a particular interface can support.
157 *
158 * A positive integer indicates n units per bus.
159 * 0 implies the mapping has not been established.
160 * -1 indicates an invalid BlockInterfaceType was given.
161 */
162int drive_get_max_devs(BlockInterfaceType type)
163{
164 if (type >= IF_IDE && type < IF_COUNT) {
165 return if_max_devs[type];
166 }
167
168 return -1;
169}
170
505a7fb1
MA
171static int drive_index_to_bus_id(BlockInterfaceType type, int index)
172{
173 int max_devs = if_max_devs[type];
174 return max_devs ? index / max_devs : 0;
175}
176
177static int drive_index_to_unit_id(BlockInterfaceType type, int index)
178{
179 int max_devs = if_max_devs[type];
180 return max_devs ? index % max_devs : index;
181}
182
2292ddae
MA
183QemuOpts *drive_def(const char *optstr)
184{
70b94331 185 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
2292ddae
MA
186}
187
188QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
5645b0f4 189 const char *optstr)
666daa68 190{
666daa68
MA
191 QemuOpts *opts;
192
2292ddae 193 opts = drive_def(optstr);
666daa68
MA
194 if (!opts) {
195 return NULL;
196 }
2292ddae 197 if (type != IF_DEFAULT) {
f43e47db 198 qemu_opt_set(opts, "if", if_name[type], &error_abort);
2292ddae
MA
199 }
200 if (index >= 0) {
a8b18f8f 201 qemu_opt_set_number(opts, "index", index, &error_abort);
2292ddae 202 }
666daa68 203 if (file)
f43e47db 204 qemu_opt_set(opts, "file", file, &error_abort);
666daa68
MA
205 return opts;
206}
207
208DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
209{
18e46a03 210 BlockBackend *blk;
666daa68
MA
211 DriveInfo *dinfo;
212
18e46a03
MA
213 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
214 dinfo = blk_legacy_dinfo(blk);
215 if (dinfo && dinfo->type == type
216 && dinfo->bus == bus && dinfo->unit == unit) {
666daa68 217 return dinfo;
18e46a03 218 }
666daa68
MA
219 }
220
221 return NULL;
222}
223
a66c9dc7
JS
224bool drive_check_orphaned(void)
225{
18e46a03 226 BlockBackend *blk;
a66c9dc7
JS
227 DriveInfo *dinfo;
228 bool rs = false;
229
18e46a03
MA
230 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
231 dinfo = blk_legacy_dinfo(blk);
a66c9dc7
JS
232 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
233 /* Unless this is a default drive, this may be an oversight. */
a7f53e26 234 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
a66c9dc7
JS
235 dinfo->type != IF_NONE) {
236 fprintf(stderr, "Warning: Orphaned drive without device: "
237 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
5433c24f
HR
238 blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
239 if_name[dinfo->type], dinfo->bus, dinfo->unit);
a66c9dc7
JS
240 rs = true;
241 }
242 }
243
244 return rs;
245}
246
f1bd51ac
MA
247DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
248{
249 return drive_get(type,
250 drive_index_to_bus_id(type, index),
251 drive_index_to_unit_id(type, index));
252}
253
666daa68
MA
254int drive_get_max_bus(BlockInterfaceType type)
255{
256 int max_bus;
18e46a03 257 BlockBackend *blk;
666daa68
MA
258 DriveInfo *dinfo;
259
260 max_bus = -1;
18e46a03
MA
261 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
262 dinfo = blk_legacy_dinfo(blk);
263 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
666daa68 264 max_bus = dinfo->bus;
18e46a03 265 }
666daa68
MA
266 }
267 return max_bus;
268}
269
13839974
MA
270/* Get a block device. This should only be used for single-drive devices
271 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
272 appropriate bus. */
273DriveInfo *drive_get_next(BlockInterfaceType type)
274{
275 static int next_block_unit[IF_COUNT];
276
277 return drive_get(type, 0, next_block_unit[type]++);
278}
279
666daa68
MA
280static void bdrv_format_print(void *opaque, const char *name)
281{
807105a7 282 error_printf(" %s", name);
666daa68
MA
283}
284
aa398a5c
SH
285typedef struct {
286 QEMUBH *bh;
fa510ebf
FZ
287 BlockDriverState *bs;
288} BDRVPutRefBH;
aa398a5c 289
b681072d 290static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
666daa68
MA
291{
292 if (!strcmp(buf, "ignore")) {
92aa5c6d 293 return BLOCKDEV_ON_ERROR_IGNORE;
666daa68 294 } else if (!is_read && !strcmp(buf, "enospc")) {
92aa5c6d 295 return BLOCKDEV_ON_ERROR_ENOSPC;
666daa68 296 } else if (!strcmp(buf, "stop")) {
92aa5c6d 297 return BLOCKDEV_ON_ERROR_STOP;
666daa68 298 } else if (!strcmp(buf, "report")) {
92aa5c6d 299 return BLOCKDEV_ON_ERROR_REPORT;
666daa68 300 } else {
b681072d
KW
301 error_setg(errp, "'%s' invalid %s error action",
302 buf, is_read ? "read" : "write");
666daa68
MA
303 return -1;
304 }
305}
306
40119eff
AG
307static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
308 Error **errp)
309{
310 const QListEntry *entry;
311 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
312 switch (qobject_type(entry->value)) {
313
314 case QTYPE_QSTRING: {
315 unsigned long long length;
316 const char *str = qstring_get_str(qobject_to_qstring(entry->value));
317 if (parse_uint_full(str, &length, 10) == 0 &&
318 length > 0 && length <= UINT_MAX) {
319 block_acct_add_interval(stats, (unsigned) length);
320 } else {
321 error_setg(errp, "Invalid interval length: %s", str);
322 return false;
323 }
324 break;
325 }
326
327 case QTYPE_QINT: {
328 int64_t length = qint_get_int(qobject_to_qint(entry->value));
329 if (length > 0 && length <= UINT_MAX) {
330 block_acct_add_interval(stats, (unsigned) length);
331 } else {
332 error_setg(errp, "Invalid interval length: %" PRId64, length);
333 return false;
334 }
335 break;
336 }
337
338 default:
339 error_setg(errp, "The specification of stats-intervals is invalid");
340 return false;
341 }
342 }
343 return true;
344}
345
33cb7dc8
KW
346typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
347
fbf8175e
HR
348/* All parameters but @opts are optional and may be set to NULL. */
349static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
350 const char **throttling_group, ThrottleConfig *throttle_cfg,
351 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
352{
353 const char *discard;
354 Error *local_error = NULL;
355 const char *aio;
356
357 if (bdrv_flags) {
358 if (!qemu_opt_get_bool(opts, "read-only", false)) {
359 *bdrv_flags |= BDRV_O_RDWR;
360 }
361 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
362 *bdrv_flags |= BDRV_O_COPY_ON_READ;
363 }
364
365 if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
366 if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
367 error_setg(errp, "Invalid discard option");
368 return;
369 }
370 }
371
fbf8175e
HR
372 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
373 if (!strcmp(aio, "native")) {
374 *bdrv_flags |= BDRV_O_NATIVE_AIO;
375 } else if (!strcmp(aio, "threads")) {
376 /* this is the default */
377 } else {
378 error_setg(errp, "invalid aio option");
379 return;
380 }
381 }
382 }
383
384 /* disk I/O throttling */
385 if (throttling_group) {
386 *throttling_group = qemu_opt_get(opts, "throttling.group");
387 }
388
389 if (throttle_cfg) {
1588ab5d 390 throttle_config_init(throttle_cfg);
fbf8175e
HR
391 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
392 qemu_opt_get_number(opts, "throttling.bps-total", 0);
393 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
394 qemu_opt_get_number(opts, "throttling.bps-read", 0);
395 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
396 qemu_opt_get_number(opts, "throttling.bps-write", 0);
397 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
398 qemu_opt_get_number(opts, "throttling.iops-total", 0);
399 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
400 qemu_opt_get_number(opts, "throttling.iops-read", 0);
401 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
402 qemu_opt_get_number(opts, "throttling.iops-write", 0);
403
404 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
405 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
406 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
407 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
408 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
409 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
410 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
411 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
412 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
413 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
414 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
415 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
416
8a0fc18d
AG
417 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
418 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
419 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
420 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
421 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
422 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
423 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
424 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
425 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
426 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
427 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
428 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
429
fbf8175e
HR
430 throttle_cfg->op_size =
431 qemu_opt_get_number(opts, "throttling.iops-size", 0);
432
d5851089 433 if (!throttle_is_valid(throttle_cfg, errp)) {
fbf8175e
HR
434 return;
435 }
436 }
437
438 if (detect_zeroes) {
439 *detect_zeroes =
440 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
441 qemu_opt_get(opts, "detect-zeroes"),
7fb1cf16 442 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
fbf8175e
HR
443 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
444 &local_error);
445 if (local_error) {
446 error_propagate(errp, local_error);
447 return;
448 }
449
450 if (bdrv_flags &&
451 *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
452 !(*bdrv_flags & BDRV_O_UNMAP))
453 {
454 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
455 "without setting discard operation to unmap");
456 return;
457 }
458 }
459}
460
f298d071 461/* Takes the ownership of bs_opts */
18e46a03
MA
462static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
463 Error **errp)
666daa68
MA
464{
465 const char *buf;
666daa68
MA
466 int bdrv_flags = 0;
467 int on_read_error, on_write_error;
362e9299 468 bool account_invalid, account_failed;
26f54e9a 469 BlockBackend *blk;
a0f1eab1 470 BlockDriverState *bs;
cc0681c4 471 ThrottleConfig cfg;
666daa68 472 int snapshot = 0;
c546194f 473 Error *error = NULL;
0006383e 474 QemuOpts *opts;
40119eff
AG
475 QDict *interval_dict = NULL;
476 QList *interval_list = NULL;
0006383e 477 const char *id;
fbf8175e
HR
478 BlockdevDetectZeroesOptions detect_zeroes =
479 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
480 const char *throttling_group = NULL;
666daa68 481
f298d071
KW
482 /* Check common options by copying from bs_opts to opts, all other options
483 * stay in bs_opts for processing by bdrv_open(). */
484 id = qdict_get_try_str(bs_opts, "id");
0006383e 485 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
84d18f06 486 if (error) {
b681072d 487 error_propagate(errp, error);
6376f952 488 goto err_no_opts;
0006383e
KW
489 }
490
0006383e 491 qemu_opts_absorb_qdict(opts, bs_opts, &error);
84d18f06 492 if (error) {
b681072d 493 error_propagate(errp, error);
ec9c10d2 494 goto early_err;
0006383e
KW
495 }
496
497 if (id) {
498 qdict_del(bs_opts, "id");
499 }
500
666daa68 501 /* extract parameters */
666daa68 502 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
666daa68 503
362e9299
AG
504 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
505 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
506
40119eff
AG
507 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
508 qdict_array_split(interval_dict, &interval_list);
509
510 if (qdict_size(interval_dict) != 0) {
511 error_setg(errp, "Invalid option stats-intervals.%s",
512 qdict_first(interval_dict)->key);
513 goto early_err;
514 }
2be5506f 515
fbf8175e
HR
516 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
517 &detect_zeroes, &error);
518 if (error) {
519 error_propagate(errp, error);
520 goto early_err;
666daa68 521 }
666daa68
MA
522
523 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
c8057f95
PM
524 if (is_help_option(buf)) {
525 error_printf("Supported formats:");
526 bdrv_iterate_format(bdrv_format_print, NULL);
527 error_printf("\n");
ec9c10d2 528 goto early_err;
666daa68 529 }
74fe54f2 530
e4342ce5
HR
531 if (qdict_haskey(bs_opts, "driver")) {
532 error_setg(errp, "Cannot specify both 'driver' and 'format'");
ec9c10d2 533 goto early_err;
6db5f5d6 534 }
e4342ce5 535 qdict_put(bs_opts, "driver", qstring_from_str(buf));
666daa68
MA
536 }
537
92aa5c6d 538 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
666daa68 539 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
b681072d 540 on_write_error = parse_block_error_action(buf, 0, &error);
84d18f06 541 if (error) {
b681072d 542 error_propagate(errp, error);
ec9c10d2 543 goto early_err;
666daa68
MA
544 }
545 }
546
92aa5c6d 547 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
666daa68 548 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
b681072d 549 on_read_error = parse_block_error_action(buf, 1, &error);
84d18f06 550 if (error) {
b681072d 551 error_propagate(errp, error);
ec9c10d2 552 goto early_err;
666daa68
MA
553 }
554 }
555
5ec18f8c 556 if (snapshot) {
91a097e7 557 bdrv_flags |= BDRV_O_SNAPSHOT;
5ec18f8c
HR
558 }
559
326642bc 560 /* init */
39c4ae94 561 if ((!file || !*file) && !qdict_size(bs_opts)) {
5ec18f8c
HR
562 BlockBackendRootState *blk_rs;
563
564 blk = blk_new(qemu_opts_id(opts), errp);
e4342ce5
HR
565 if (!blk) {
566 goto early_err;
567 }
abd7f68d 568
5ec18f8c
HR
569 blk_rs = blk_get_root_state(blk);
570 blk_rs->open_flags = bdrv_flags;
fbf8175e 571 blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR);
5ec18f8c
HR
572 blk_rs->detect_zeroes = detect_zeroes;
573
574 if (throttle_enabled(&cfg)) {
575 if (!throttling_group) {
576 throttling_group = blk_name(blk);
577 }
578 blk_rs->throttle_group = g_strdup(throttling_group);
579 blk_rs->throttle_state = throttle_group_incref(throttling_group);
580 blk_rs->throttle_state->cfg = cfg;
581 }
0563e191 582
e4342ce5
HR
583 QDECREF(bs_opts);
584 } else {
585 if (file && !*file) {
c2ad1b0c 586 file = NULL;
c2ad1b0c 587 }
666daa68 588
91a097e7
KW
589 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
590 * with other callers) rather than what we want as the real defaults.
591 * Apply the defaults here instead. */
592 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_WB, "on");
593 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
594 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
595
12d5ee3a
KW
596 if (runstate_check(RUN_STATE_INMIGRATE)) {
597 bdrv_flags |= BDRV_O_INACTIVE;
598 }
599
e4342ce5
HR
600 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
601 errp);
602 if (!blk) {
603 goto err_no_bs_opts;
604 }
605 bs = blk_bs(blk);
ed9d4205 606
5ec18f8c 607 bs->detect_zeroes = detect_zeroes;
666daa68 608
5ec18f8c
HR
609 /* disk I/O throttling */
610 if (throttle_enabled(&cfg)) {
611 if (!throttling_group) {
612 throttling_group = blk_name(blk);
613 }
614 bdrv_io_limits_enable(bs, throttling_group);
615 bdrv_set_io_limits(bs, &cfg);
616 }
0006383e 617
5ec18f8c
HR
618 if (bdrv_key_required(bs)) {
619 autostart = 0;
76f4afb4 620 }
362e9299
AG
621
622 block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
2be5506f 623
40119eff
AG
624 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
625 blk_unref(blk);
626 blk = NULL;
627 goto err_no_bs_opts;
2be5506f 628 }
666daa68
MA
629 }
630
5ec18f8c 631 blk_set_on_error(blk, on_read_error, on_write_error);
0006383e 632
e4342ce5 633err_no_bs_opts:
0006383e 634 qemu_opts_del(opts);
40119eff
AG
635 QDECREF(interval_dict);
636 QDECREF(interval_list);
18e46a03 637 return blk;
a9ae2bff 638
ec9c10d2 639early_err:
ec9c10d2 640 qemu_opts_del(opts);
40119eff
AG
641 QDECREF(interval_dict);
642 QDECREF(interval_list);
6376f952
MA
643err_no_opts:
644 QDECREF(bs_opts);
a9ae2bff 645 return NULL;
666daa68
MA
646}
647
bd745e23
HR
648static QemuOptsList qemu_root_bds_opts;
649
650/* Takes the ownership of bs_opts */
651static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
652{
653 BlockDriverState *bs;
654 QemuOpts *opts;
655 Error *local_error = NULL;
656 BlockdevDetectZeroesOptions detect_zeroes;
657 int ret;
658 int bdrv_flags = 0;
659
660 opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
661 if (!opts) {
662 goto fail;
663 }
664
665 qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
666 if (local_error) {
667 error_propagate(errp, local_error);
668 goto fail;
669 }
670
671 extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
672 &detect_zeroes, &local_error);
673 if (local_error) {
674 error_propagate(errp, local_error);
675 goto fail;
676 }
677
12d5ee3a
KW
678 if (runstate_check(RUN_STATE_INMIGRATE)) {
679 bdrv_flags |= BDRV_O_INACTIVE;
680 }
681
bd745e23
HR
682 bs = NULL;
683 ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp);
684 if (ret < 0) {
685 goto fail_no_bs_opts;
686 }
687
688 bs->detect_zeroes = detect_zeroes;
689
690fail_no_bs_opts:
691 qemu_opts_del(opts);
692 return bs;
693
694fail:
695 qemu_opts_del(opts);
696 QDECREF(bs_opts);
697 return NULL;
698}
699
9c4218e9
HR
700void blockdev_close_all_bdrv_states(void)
701{
702 BlockDriverState *bs, *next_bs;
703
704 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
705 AioContext *ctx = bdrv_get_aio_context(bs);
706
707 aio_context_acquire(ctx);
708 bdrv_unref(bs);
709 aio_context_release(ctx);
710 }
711}
712
5abbf0ee
KW
713static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
714 Error **errp)
57975222
KW
715{
716 const char *value;
717
718 value = qemu_opt_get(opts, from);
719 if (value) {
5abbf0ee
KW
720 if (qemu_opt_find(opts, to)) {
721 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
722 "same time", to, from);
723 return;
724 }
20d6cd47
JL
725 }
726
727 /* rename all items in opts */
728 while ((value = qemu_opt_get(opts, from))) {
f43e47db 729 qemu_opt_set(opts, to, value, &error_abort);
57975222
KW
730 qemu_opt_unset(opts, from);
731 }
732}
733
33cb7dc8
KW
734QemuOptsList qemu_legacy_drive_opts = {
735 .name = "drive",
736 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
737 .desc = {
738 {
87a899c5
KW
739 .name = "bus",
740 .type = QEMU_OPT_NUMBER,
741 .help = "bus number",
742 },{
743 .name = "unit",
744 .type = QEMU_OPT_NUMBER,
745 .help = "unit number (i.e. lun for scsi)",
746 },{
747 .name = "index",
748 .type = QEMU_OPT_NUMBER,
749 .help = "index number",
750 },{
33cb7dc8
KW
751 .name = "media",
752 .type = QEMU_OPT_STRING,
753 .help = "media type (disk, cdrom)",
593d464b
KW
754 },{
755 .name = "if",
756 .type = QEMU_OPT_STRING,
757 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
b41a7338
KW
758 },{
759 .name = "cyls",
760 .type = QEMU_OPT_NUMBER,
761 .help = "number of cylinders (ide disk geometry)",
762 },{
763 .name = "heads",
764 .type = QEMU_OPT_NUMBER,
765 .help = "number of heads (ide disk geometry)",
766 },{
767 .name = "secs",
768 .type = QEMU_OPT_NUMBER,
769 .help = "number of sectors (ide disk geometry)",
770 },{
771 .name = "trans",
772 .type = QEMU_OPT_STRING,
773 .help = "chs translation (auto, lba, none)",
26929298
KW
774 },{
775 .name = "boot",
776 .type = QEMU_OPT_BOOL,
777 .help = "(deprecated, ignored)",
394c7d4d
KW
778 },{
779 .name = "addr",
780 .type = QEMU_OPT_STRING,
781 .help = "pci address (virtio only)",
bcf83158
KW
782 },{
783 .name = "serial",
784 .type = QEMU_OPT_STRING,
785 .help = "disk serial number",
d095b465
HR
786 },{
787 .name = "file",
788 .type = QEMU_OPT_STRING,
789 .help = "file name",
33cb7dc8 790 },
0ebd24e0
KW
791
792 /* Options that are passed on, but have special semantics with -drive */
793 {
794 .name = "read-only",
795 .type = QEMU_OPT_BOOL,
796 .help = "open drive file as read-only",
ee13ed1c
KW
797 },{
798 .name = "rerror",
799 .type = QEMU_OPT_STRING,
800 .help = "read error action",
801 },{
802 .name = "werror",
803 .type = QEMU_OPT_STRING,
804 .help = "write error action",
0ebd24e0
KW
805 },{
806 .name = "copy-on-read",
807 .type = QEMU_OPT_BOOL,
808 .help = "copy read data from backing file into image file",
809 },
810
33cb7dc8
KW
811 { /* end of list */ }
812 },
813};
814
60e19e06 815DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
57975222 816{
29c4e2b5 817 const char *value;
18e46a03 818 BlockBackend *blk;
33cb7dc8 819 DriveInfo *dinfo = NULL;
f298d071 820 QDict *bs_opts;
33cb7dc8
KW
821 QemuOpts *legacy_opts;
822 DriveMediaType media = MEDIA_DISK;
593d464b 823 BlockInterfaceType type;
b41a7338 824 int cyls, heads, secs, translation;
87a899c5 825 int max_devs, bus_id, unit_id, index;
394c7d4d 826 const char *devaddr;
ee13ed1c 827 const char *werror, *rerror;
a7fdbcf0
FZ
828 bool read_only = false;
829 bool copy_on_read;
bcf83158 830 const char *serial;
d095b465 831 const char *filename;
33cb7dc8 832 Error *local_err = NULL;
247147fb 833 int i;
29c4e2b5 834
57975222 835 /* Change legacy command line options into QMP ones */
247147fb
KW
836 static const struct {
837 const char *from;
838 const char *to;
839 } opt_renames[] = {
840 { "iops", "throttling.iops-total" },
841 { "iops_rd", "throttling.iops-read" },
842 { "iops_wr", "throttling.iops-write" },
57975222 843
247147fb
KW
844 { "bps", "throttling.bps-total" },
845 { "bps_rd", "throttling.bps-read" },
846 { "bps_wr", "throttling.bps-write" },
57975222 847
247147fb
KW
848 { "iops_max", "throttling.iops-total-max" },
849 { "iops_rd_max", "throttling.iops-read-max" },
850 { "iops_wr_max", "throttling.iops-write-max" },
3e9fab69 851
247147fb
KW
852 { "bps_max", "throttling.bps-total-max" },
853 { "bps_rd_max", "throttling.bps-read-max" },
854 { "bps_wr_max", "throttling.bps-write-max" },
3e9fab69 855
247147fb 856 { "iops_size", "throttling.iops-size" },
2024c1df 857
76f4afb4
AG
858 { "group", "throttling.group" },
859
247147fb
KW
860 { "readonly", "read-only" },
861 };
862
863 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
5abbf0ee
KW
864 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
865 &local_err);
866 if (local_err) {
565f65d2 867 error_report_err(local_err);
5abbf0ee
KW
868 return NULL;
869 }
247147fb 870 }
0f227a94 871
29c4e2b5
KW
872 value = qemu_opt_get(all_opts, "cache");
873 if (value) {
874 int flags = 0;
875
876 if (bdrv_parse_cache_flags(value, &flags) != 0) {
877 error_report("invalid cache option");
878 return NULL;
879 }
880
881 /* Specific options take precedence */
54861b92
KW
882 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
883 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
cccb7967 884 !!(flags & BDRV_O_CACHE_WB), &error_abort);
29c4e2b5 885 }
54861b92
KW
886 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
887 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
cccb7967 888 !!(flags & BDRV_O_NOCACHE), &error_abort);
29c4e2b5 889 }
54861b92
KW
890 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
891 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
cccb7967 892 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
29c4e2b5
KW
893 }
894 qemu_opt_unset(all_opts, "cache");
895 }
896
f298d071
KW
897 /* Get a QDict for processing the options */
898 bs_opts = qdict_new();
899 qemu_opts_to_qdict(all_opts, bs_opts);
900
87ea75d5
PC
901 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
902 &error_abort);
33cb7dc8 903 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
84d18f06 904 if (local_err) {
565f65d2 905 error_report_err(local_err);
33cb7dc8
KW
906 goto fail;
907 }
908
26929298
KW
909 /* Deprecated option boot=[on|off] */
910 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
911 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
912 "ignored. Future versions will reject this parameter. Please "
913 "update your scripts.\n");
914 }
915
33cb7dc8
KW
916 /* Media type */
917 value = qemu_opt_get(legacy_opts, "media");
918 if (value) {
919 if (!strcmp(value, "disk")) {
920 media = MEDIA_DISK;
921 } else if (!strcmp(value, "cdrom")) {
922 media = MEDIA_CDROM;
a7fdbcf0 923 read_only = true;
33cb7dc8
KW
924 } else {
925 error_report("'%s' invalid media", value);
926 goto fail;
927 }
928 }
929
0ebd24e0 930 /* copy-on-read is disabled with a warning for read-only devices */
a7fdbcf0 931 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
0ebd24e0
KW
932 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
933
934 if (read_only && copy_on_read) {
935 error_report("warning: disabling copy-on-read on read-only drive");
936 copy_on_read = false;
937 }
938
939 qdict_put(bs_opts, "read-only",
940 qstring_from_str(read_only ? "on" : "off"));
941 qdict_put(bs_opts, "copy-on-read",
942 qstring_from_str(copy_on_read ? "on" :"off"));
943
593d464b
KW
944 /* Controller type */
945 value = qemu_opt_get(legacy_opts, "if");
946 if (value) {
947 for (type = 0;
948 type < IF_COUNT && strcmp(value, if_name[type]);
949 type++) {
950 }
951 if (type == IF_COUNT) {
952 error_report("unsupported bus type '%s'", value);
953 goto fail;
954 }
955 } else {
956 type = block_default_type;
957 }
958
b41a7338
KW
959 /* Geometry */
960 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
961 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
962 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
963
964 if (cyls || heads || secs) {
965 if (cyls < 1) {
966 error_report("invalid physical cyls number");
967 goto fail;
968 }
969 if (heads < 1) {
970 error_report("invalid physical heads number");
971 goto fail;
972 }
973 if (secs < 1) {
974 error_report("invalid physical secs number");
975 goto fail;
976 }
977 }
978
979 translation = BIOS_ATA_TRANSLATION_AUTO;
980 value = qemu_opt_get(legacy_opts, "trans");
981 if (value != NULL) {
982 if (!cyls) {
983 error_report("'%s' trans must be used with cyls, heads and secs",
984 value);
985 goto fail;
986 }
987 if (!strcmp(value, "none")) {
988 translation = BIOS_ATA_TRANSLATION_NONE;
989 } else if (!strcmp(value, "lba")) {
990 translation = BIOS_ATA_TRANSLATION_LBA;
f31c41ff
PB
991 } else if (!strcmp(value, "large")) {
992 translation = BIOS_ATA_TRANSLATION_LARGE;
993 } else if (!strcmp(value, "rechs")) {
994 translation = BIOS_ATA_TRANSLATION_RECHS;
b41a7338
KW
995 } else if (!strcmp(value, "auto")) {
996 translation = BIOS_ATA_TRANSLATION_AUTO;
997 } else {
998 error_report("'%s' invalid translation type", value);
999 goto fail;
1000 }
1001 }
1002
1003 if (media == MEDIA_CDROM) {
1004 if (cyls || secs || heads) {
1005 error_report("CHS can't be set with media=cdrom");
1006 goto fail;
1007 }
1008 }
1009
87a899c5
KW
1010 /* Device address specified by bus/unit or index.
1011 * If none was specified, try to find the first free one. */
1012 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
1013 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
1014 index = qemu_opt_get_number(legacy_opts, "index", -1);
1015
1016 max_devs = if_max_devs[type];
1017
1018 if (index != -1) {
1019 if (bus_id != 0 || unit_id != -1) {
1020 error_report("index cannot be used with bus and unit");
1021 goto fail;
1022 }
1023 bus_id = drive_index_to_bus_id(type, index);
1024 unit_id = drive_index_to_unit_id(type, index);
1025 }
1026
1027 if (unit_id == -1) {
1028 unit_id = 0;
1029 while (drive_get(type, bus_id, unit_id) != NULL) {
1030 unit_id++;
1031 if (max_devs && unit_id >= max_devs) {
1032 unit_id -= max_devs;
1033 bus_id++;
1034 }
1035 }
1036 }
1037
1038 if (max_devs && unit_id >= max_devs) {
1039 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1040 goto fail;
1041 }
1042
1043 if (drive_get(type, bus_id, unit_id) != NULL) {
1044 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1045 bus_id, unit_id, index);
1046 goto fail;
1047 }
1048
bcf83158
KW
1049 /* Serial number */
1050 serial = qemu_opt_get(legacy_opts, "serial");
1051
87a899c5
KW
1052 /* no id supplied -> create one */
1053 if (qemu_opts_id(all_opts) == NULL) {
1054 char *new_id;
1055 const char *mediastr = "";
1056 if (type == IF_IDE || type == IF_SCSI) {
1057 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1058 }
1059 if (max_devs) {
1060 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1061 mediastr, unit_id);
1062 } else {
1063 new_id = g_strdup_printf("%s%s%i", if_name[type],
1064 mediastr, unit_id);
1065 }
1066 qdict_put(bs_opts, "id", qstring_from_str(new_id));
1067 g_free(new_id);
1068 }
1069
394c7d4d
KW
1070 /* Add virtio block device */
1071 devaddr = qemu_opt_get(legacy_opts, "addr");
1072 if (devaddr && type != IF_VIRTIO) {
1073 error_report("addr is not supported by this bus type");
1074 goto fail;
1075 }
1076
1077 if (type == IF_VIRTIO) {
1078 QemuOpts *devopts;
87ea75d5
PC
1079 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1080 &error_abort);
394c7d4d 1081 if (arch_type == QEMU_ARCH_S390X) {
1f68f1d3 1082 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
394c7d4d 1083 } else {
f43e47db 1084 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
394c7d4d 1085 }
f43e47db
MA
1086 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1087 &error_abort);
394c7d4d 1088 if (devaddr) {
f43e47db 1089 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
394c7d4d
KW
1090 }
1091 }
1092
d095b465
HR
1093 filename = qemu_opt_get(legacy_opts, "file");
1094
ee13ed1c
KW
1095 /* Check werror/rerror compatibility with if=... */
1096 werror = qemu_opt_get(legacy_opts, "werror");
1097 if (werror != NULL) {
1098 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1099 type != IF_NONE) {
1100 error_report("werror is not supported by this bus type");
1101 goto fail;
1102 }
1103 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1104 }
1105
1106 rerror = qemu_opt_get(legacy_opts, "rerror");
1107 if (rerror != NULL) {
1108 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1109 type != IF_NONE) {
1110 error_report("rerror is not supported by this bus type");
1111 goto fail;
1112 }
1113 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1114 }
1115
2d246f01 1116 /* Actual block device init: Functionality shared with blockdev-add */
18e46a03 1117 blk = blockdev_init(filename, bs_opts, &local_err);
3cb0e25c 1118 bs_opts = NULL;
18e46a03 1119 if (!blk) {
84d18f06 1120 if (local_err) {
565f65d2 1121 error_report_err(local_err);
b681072d 1122 }
2d246f01 1123 goto fail;
b681072d 1124 } else {
84d18f06 1125 assert(!local_err);
2d246f01
KW
1126 }
1127
26f8b3a8
MA
1128 /* Create legacy DriveInfo */
1129 dinfo = g_malloc0(sizeof(*dinfo));
f298d071 1130 dinfo->opts = all_opts;
2d246f01 1131
b41a7338
KW
1132 dinfo->cyls = cyls;
1133 dinfo->heads = heads;
1134 dinfo->secs = secs;
1135 dinfo->trans = translation;
1136
ee13ed1c 1137 dinfo->type = type;
87a899c5
KW
1138 dinfo->bus = bus_id;
1139 dinfo->unit = unit_id;
394c7d4d 1140 dinfo->devaddr = devaddr;
bcf83158
KW
1141 dinfo->serial = g_strdup(serial);
1142
26f8b3a8
MA
1143 blk_set_legacy_dinfo(blk, dinfo);
1144
e34ef046
KW
1145 switch(type) {
1146 case IF_IDE:
1147 case IF_SCSI:
1148 case IF_XEN:
1149 case IF_NONE:
1150 dinfo->media_cd = media == MEDIA_CDROM;
1151 break;
1152 default:
1153 break;
1154 }
1155
2d246f01 1156fail:
33cb7dc8 1157 qemu_opts_del(legacy_opts);
3cb0e25c 1158 QDECREF(bs_opts);
2d246f01 1159 return dinfo;
57975222
KW
1160}
1161
3e5a50d6 1162void hmp_commit(Monitor *mon, const QDict *qdict)
666daa68 1163{
666daa68 1164 const char *device = qdict_get_str(qdict, "device");
a0e8544c 1165 BlockBackend *blk;
e8877497 1166 int ret;
666daa68 1167
6ab4b5ab 1168 if (!strcmp(device, "all")) {
e8877497 1169 ret = bdrv_commit_all();
6ab4b5ab 1170 } else {
84aa0140
SH
1171 BlockDriverState *bs;
1172 AioContext *aio_context;
1173
a0e8544c
FZ
1174 blk = blk_by_name(device);
1175 if (!blk) {
58513bde 1176 monitor_printf(mon, "Device '%s' not found\n", device);
ac59eb95 1177 return;
6ab4b5ab 1178 }
5433c24f
HR
1179 if (!blk_is_available(blk)) {
1180 monitor_printf(mon, "Device '%s' has no medium\n", device);
1181 return;
1182 }
84aa0140
SH
1183
1184 bs = blk_bs(blk);
1185 aio_context = bdrv_get_aio_context(bs);
1186 aio_context_acquire(aio_context);
1187
1188 ret = bdrv_commit(bs);
1189
1190 aio_context_release(aio_context);
58513bde
JC
1191 }
1192 if (ret < 0) {
1193 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1194 strerror(-ret));
666daa68
MA
1195 }
1196}
1197
10f75907 1198static void blockdev_do_action(TransactionAction *action, Error **errp)
6cc2a415 1199{
c8a83e85 1200 TransactionActionList list;
6cc2a415 1201
10f75907 1202 list.value = action;
6cc2a415 1203 list.next = NULL;
94d16a64 1204 qmp_transaction(&list, false, NULL, errp);
6cc2a415
PB
1205}
1206
0901f67e
BC
1207void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1208 bool has_node_name, const char *node_name,
1209 const char *snapshot_file,
1210 bool has_snapshot_node_name,
1211 const char *snapshot_node_name,
6106e249 1212 bool has_format, const char *format,
0901f67e 1213 bool has_mode, NewImageMode mode, Error **errp)
f8882568 1214{
a911e6ae 1215 BlockdevSnapshotSync snapshot = {
0901f67e 1216 .has_device = has_device,
6cc2a415 1217 .device = (char *) device,
0901f67e
BC
1218 .has_node_name = has_node_name,
1219 .node_name = (char *) node_name,
6cc2a415 1220 .snapshot_file = (char *) snapshot_file,
0901f67e
BC
1221 .has_snapshot_node_name = has_snapshot_node_name,
1222 .snapshot_node_name = (char *) snapshot_node_name,
6cc2a415
PB
1223 .has_format = has_format,
1224 .format = (char *) format,
1225 .has_mode = has_mode,
1226 .mode = mode,
1227 };
10f75907
EB
1228 TransactionAction action = {
1229 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1230 .u.blockdev_snapshot_sync = &snapshot,
1231 };
1232 blockdev_do_action(&action, errp);
f8882568
JS
1233}
1234
43de7e2d
AG
1235void qmp_blockdev_snapshot(const char *node, const char *overlay,
1236 Error **errp)
1237{
1238 BlockdevSnapshot snapshot_data = {
1239 .node = (char *) node,
1240 .overlay = (char *) overlay
1241 };
10f75907
EB
1242 TransactionAction action = {
1243 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
1244 .u.blockdev_snapshot = &snapshot_data,
1245 };
1246 blockdev_do_action(&action, errp);
43de7e2d
AG
1247}
1248
f323bc9e
WX
1249void qmp_blockdev_snapshot_internal_sync(const char *device,
1250 const char *name,
1251 Error **errp)
1252{
1253 BlockdevSnapshotInternal snapshot = {
1254 .device = (char *) device,
1255 .name = (char *) name
1256 };
10f75907
EB
1257 TransactionAction action = {
1258 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1259 .u.blockdev_snapshot_internal_sync = &snapshot,
1260 };
1261 blockdev_do_action(&action, errp);
f323bc9e
WX
1262}
1263
44e3e053
WX
1264SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1265 bool has_id,
1266 const char *id,
1267 bool has_name,
1268 const char *name,
1269 Error **errp)
1270{
a0e8544c
FZ
1271 BlockDriverState *bs;
1272 BlockBackend *blk;
4ef3982a 1273 AioContext *aio_context;
44e3e053
WX
1274 QEMUSnapshotInfo sn;
1275 Error *local_err = NULL;
1276 SnapshotInfo *info = NULL;
1277 int ret;
1278
a0e8544c
FZ
1279 blk = blk_by_name(device);
1280 if (!blk) {
75158ebb
MA
1281 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1282 "Device '%s' not found", device);
44e3e053
WX
1283 return NULL;
1284 }
5433c24f
HR
1285
1286 aio_context = blk_get_aio_context(blk);
1287 aio_context_acquire(aio_context);
44e3e053
WX
1288
1289 if (!has_id) {
1290 id = NULL;
1291 }
1292
1293 if (!has_name) {
1294 name = NULL;
1295 }
1296
1297 if (!id && !name) {
1298 error_setg(errp, "Name or id must be provided");
5433c24f 1299 goto out_aio_context;
44e3e053
WX
1300 }
1301
5433c24f
HR
1302 if (!blk_is_available(blk)) {
1303 error_setg(errp, "Device '%s' has no medium", device);
1304 goto out_aio_context;
1305 }
1306 bs = blk_bs(blk);
4ef3982a 1307
0b928854
SH
1308 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1309 goto out_aio_context;
1310 }
1311
44e3e053 1312 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
84d18f06 1313 if (local_err) {
44e3e053 1314 error_propagate(errp, local_err);
4ef3982a 1315 goto out_aio_context;
44e3e053
WX
1316 }
1317 if (!ret) {
1318 error_setg(errp,
1319 "Snapshot with id '%s' and name '%s' does not exist on "
1320 "device '%s'",
1321 STR_OR_NULL(id), STR_OR_NULL(name), device);
4ef3982a 1322 goto out_aio_context;
44e3e053
WX
1323 }
1324
1325 bdrv_snapshot_delete(bs, id, name, &local_err);
84d18f06 1326 if (local_err) {
44e3e053 1327 error_propagate(errp, local_err);
4ef3982a 1328 goto out_aio_context;
44e3e053
WX
1329 }
1330
4ef3982a
SH
1331 aio_context_release(aio_context);
1332
5839e53b 1333 info = g_new0(SnapshotInfo, 1);
44e3e053
WX
1334 info->id = g_strdup(sn.id_str);
1335 info->name = g_strdup(sn.name);
1336 info->date_nsec = sn.date_nsec;
1337 info->date_sec = sn.date_sec;
1338 info->vm_state_size = sn.vm_state_size;
1339 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1340 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1341
1342 return info;
4ef3982a
SH
1343
1344out_aio_context:
1345 aio_context_release(aio_context);
1346 return NULL;
44e3e053 1347}
8802d1fd 1348
341ebc2f
JS
1349/**
1350 * block_dirty_bitmap_lookup:
1351 * Return a dirty bitmap (if present), after validating
1352 * the node reference and bitmap names.
1353 *
1354 * @node: The name of the BDS node to search for bitmaps
1355 * @name: The name of the bitmap to search for
1356 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1357 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1358 * @errp: Output pointer for error information. Can be NULL.
1359 *
1360 * @return: A bitmap object on success, or NULL on failure.
1361 */
1362static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1363 const char *name,
1364 BlockDriverState **pbs,
1365 AioContext **paio,
1366 Error **errp)
1367{
1368 BlockDriverState *bs;
1369 BdrvDirtyBitmap *bitmap;
1370 AioContext *aio_context;
1371
1372 if (!node) {
1373 error_setg(errp, "Node cannot be NULL");
1374 return NULL;
1375 }
1376 if (!name) {
1377 error_setg(errp, "Bitmap name cannot be NULL");
1378 return NULL;
1379 }
1380 bs = bdrv_lookup_bs(node, node, NULL);
1381 if (!bs) {
1382 error_setg(errp, "Node '%s' not found", node);
1383 return NULL;
1384 }
1385
1386 aio_context = bdrv_get_aio_context(bs);
1387 aio_context_acquire(aio_context);
1388
1389 bitmap = bdrv_find_dirty_bitmap(bs, name);
1390 if (!bitmap) {
1391 error_setg(errp, "Dirty bitmap '%s' not found", name);
1392 goto fail;
1393 }
1394
1395 if (pbs) {
1396 *pbs = bs;
1397 }
1398 if (paio) {
1399 *paio = aio_context;
1400 } else {
1401 aio_context_release(aio_context);
1402 }
1403
1404 return bitmap;
1405
1406 fail:
1407 aio_context_release(aio_context);
1408 return NULL;
1409}
1410
b756b9ce 1411/* New and old BlockDriverState structs for atomic group operations */
ba0c86a3 1412
50f43f0f 1413typedef struct BlkActionState BlkActionState;
ba0c86a3 1414
50f43f0f
JS
1415/**
1416 * BlkActionOps:
1417 * Table of operations that define an Action.
1418 *
1419 * @instance_size: Size of state struct, in bytes.
1420 * @prepare: Prepare the work, must NOT be NULL.
1421 * @commit: Commit the changes, can be NULL.
1422 * @abort: Abort the changes on fail, can be NULL.
1423 * @clean: Clean up resources after all transaction actions have called
1424 * commit() or abort(). Can be NULL.
1425 *
1426 * Only prepare() may fail. In a single transaction, only one of commit() or
1427 * abort() will be called. clean() will always be called if it is present.
1428 */
1429typedef struct BlkActionOps {
ba0c86a3 1430 size_t instance_size;
50f43f0f
JS
1431 void (*prepare)(BlkActionState *common, Error **errp);
1432 void (*commit)(BlkActionState *common);
1433 void (*abort)(BlkActionState *common);
1434 void (*clean)(BlkActionState *common);
1435} BlkActionOps;
ba0c86a3 1436
50f43f0f
JS
1437/**
1438 * BlkActionState:
1439 * Describes one Action's state within a Transaction.
1440 *
1441 * @action: QAPI-defined enum identifying which Action to perform.
1442 * @ops: Table of ActionOps this Action can perform.
94d16a64 1443 * @block_job_txn: Transaction which this action belongs to.
50f43f0f
JS
1444 * @entry: List membership for all Actions in this Transaction.
1445 *
1446 * This structure must be arranged as first member in a subclassed type,
1447 * assuming that the compiler will also arrange it to the same offsets as the
1448 * base class.
ba0c86a3 1449 */
50f43f0f 1450struct BlkActionState {
c8a83e85 1451 TransactionAction *action;
50f43f0f 1452 const BlkActionOps *ops;
94d16a64
JS
1453 BlockJobTxn *block_job_txn;
1454 TransactionProperties *txn_props;
50f43f0f 1455 QSIMPLEQ_ENTRY(BlkActionState) entry;
ba0c86a3
WX
1456};
1457
bbe86010
WX
1458/* internal snapshot private data */
1459typedef struct InternalSnapshotState {
50f43f0f 1460 BlkActionState common;
bbe86010 1461 BlockDriverState *bs;
5d6e96ef 1462 AioContext *aio_context;
bbe86010 1463 QEMUSnapshotInfo sn;
507306cc 1464 bool created;
bbe86010
WX
1465} InternalSnapshotState;
1466
94d16a64
JS
1467
1468static int action_check_completion_mode(BlkActionState *s, Error **errp)
1469{
1470 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1471 error_setg(errp,
1472 "Action '%s' does not support Transaction property "
1473 "completion-mode = %s",
1474 TransactionActionKind_lookup[s->action->type],
1475 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1476 return -1;
1477 }
1478 return 0;
1479}
1480
50f43f0f 1481static void internal_snapshot_prepare(BlkActionState *common,
bbe86010
WX
1482 Error **errp)
1483{
f70edf99 1484 Error *local_err = NULL;
bbe86010
WX
1485 const char *device;
1486 const char *name;
a0e8544c 1487 BlockBackend *blk;
bbe86010
WX
1488 BlockDriverState *bs;
1489 QEMUSnapshotInfo old_sn, *sn;
1490 bool ret;
1491 qemu_timeval tv;
1492 BlockdevSnapshotInternal *internal;
1493 InternalSnapshotState *state;
1494 int ret1;
1495
6a8f9661 1496 g_assert(common->action->type ==
bbe86010 1497 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
6a8f9661 1498 internal = common->action->u.blockdev_snapshot_internal_sync;
bbe86010
WX
1499 state = DO_UPCAST(InternalSnapshotState, common, common);
1500
1501 /* 1. parse input */
1502 device = internal->device;
1503 name = internal->name;
1504
1505 /* 2. check for validation */
94d16a64
JS
1506 if (action_check_completion_mode(common, errp) < 0) {
1507 return;
1508 }
1509
a0e8544c
FZ
1510 blk = blk_by_name(device);
1511 if (!blk) {
75158ebb
MA
1512 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1513 "Device '%s' not found", device);
bbe86010
WX
1514 return;
1515 }
1516
5d6e96ef 1517 /* AioContext is released in .clean() */
5433c24f 1518 state->aio_context = blk_get_aio_context(blk);
5d6e96ef
SH
1519 aio_context_acquire(state->aio_context);
1520
5433c24f 1521 if (!blk_is_available(blk)) {
c6bd8c70 1522 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
bbe86010
WX
1523 return;
1524 }
5433c24f 1525 bs = blk_bs(blk);
bbe86010 1526
507306cc
FZ
1527 state->bs = bs;
1528 bdrv_drained_begin(bs);
1529
3dc7ca3c
SH
1530 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1531 return;
1532 }
1533
bbe86010 1534 if (bdrv_is_read_only(bs)) {
81e5f78a 1535 error_setg(errp, "Device '%s' is read only", device);
bbe86010
WX
1536 return;
1537 }
1538
1539 if (!bdrv_can_snapshot(bs)) {
81e5f78a
AG
1540 error_setg(errp, "Block format '%s' used by device '%s' "
1541 "does not support internal snapshots",
1542 bs->drv->format_name, device);
bbe86010
WX
1543 return;
1544 }
1545
1546 if (!strlen(name)) {
1547 error_setg(errp, "Name is empty");
1548 return;
1549 }
1550
1551 /* check whether a snapshot with name exist */
f70edf99
MA
1552 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1553 &local_err);
1554 if (local_err) {
1555 error_propagate(errp, local_err);
bbe86010
WX
1556 return;
1557 } else if (ret) {
1558 error_setg(errp,
1559 "Snapshot with name '%s' already exists on device '%s'",
1560 name, device);
1561 return;
1562 }
1563
1564 /* 3. take the snapshot */
1565 sn = &state->sn;
1566 pstrcpy(sn->name, sizeof(sn->name), name);
1567 qemu_gettimeofday(&tv);
1568 sn->date_sec = tv.tv_sec;
1569 sn->date_nsec = tv.tv_usec * 1000;
1570 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1571
1572 ret1 = bdrv_snapshot_create(bs, sn);
1573 if (ret1 < 0) {
1574 error_setg_errno(errp, -ret1,
1575 "Failed to create snapshot '%s' on device '%s'",
1576 name, device);
1577 return;
1578 }
1579
1580 /* 4. succeed, mark a snapshot is created */
507306cc 1581 state->created = true;
bbe86010
WX
1582}
1583
50f43f0f 1584static void internal_snapshot_abort(BlkActionState *common)
bbe86010
WX
1585{
1586 InternalSnapshotState *state =
1587 DO_UPCAST(InternalSnapshotState, common, common);
1588 BlockDriverState *bs = state->bs;
1589 QEMUSnapshotInfo *sn = &state->sn;
1590 Error *local_error = NULL;
1591
507306cc 1592 if (!state->created) {
bbe86010
WX
1593 return;
1594 }
1595
1596 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
c29b77f9
MA
1597 error_reportf_err(local_error,
1598 "Failed to delete snapshot with id '%s' and "
1599 "name '%s' on device '%s' in abort: ",
1600 sn->id_str, sn->name,
1601 bdrv_get_device_name(bs));
bbe86010
WX
1602 }
1603}
1604
50f43f0f 1605static void internal_snapshot_clean(BlkActionState *common)
5d6e96ef
SH
1606{
1607 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1608 common, common);
1609
1610 if (state->aio_context) {
507306cc
FZ
1611 if (state->bs) {
1612 bdrv_drained_end(state->bs);
1613 }
5d6e96ef
SH
1614 aio_context_release(state->aio_context);
1615 }
1616}
1617
ba0c86a3 1618/* external snapshot private data */
ba5d6ab6 1619typedef struct ExternalSnapshotState {
50f43f0f 1620 BlkActionState common;
8802d1fd
JC
1621 BlockDriverState *old_bs;
1622 BlockDriverState *new_bs;
5d6e96ef 1623 AioContext *aio_context;
ba5d6ab6 1624} ExternalSnapshotState;
8802d1fd 1625
50f43f0f 1626static void external_snapshot_prepare(BlkActionState *common,
9b9877ee
WX
1627 Error **errp)
1628{
43de7e2d
AG
1629 int flags = 0, ret;
1630 QDict *options = NULL;
9b9877ee 1631 Error *local_err = NULL;
43de7e2d 1632 /* Device and node name of the image to generate the snapshot from */
e2a31e87 1633 const char *device;
0901f67e 1634 const char *node_name;
43de7e2d
AG
1635 /* Reference to the new image (for 'blockdev-snapshot') */
1636 const char *snapshot_ref;
1637 /* File name of the new image (for 'blockdev-snapshot-sync') */
e2a31e87 1638 const char *new_image_file;
ba5d6ab6
SH
1639 ExternalSnapshotState *state =
1640 DO_UPCAST(ExternalSnapshotState, common, common);
c8a83e85 1641 TransactionAction *action = common->action;
9b9877ee 1642
43de7e2d
AG
1643 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1644 * purpose but a different set of parameters */
1645 switch (action->type) {
1646 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1647 {
1648 BlockdevSnapshot *s = action->u.blockdev_snapshot;
1649 device = s->node;
1650 node_name = s->node;
1651 new_image_file = NULL;
1652 snapshot_ref = s->overlay;
1653 }
1654 break;
1655 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1656 {
1657 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1658 device = s->has_device ? s->device : NULL;
1659 node_name = s->has_node_name ? s->node_name : NULL;
1660 new_image_file = s->snapshot_file;
1661 snapshot_ref = NULL;
1662 }
1663 break;
1664 default:
1665 g_assert_not_reached();
e2a31e87
WX
1666 }
1667
1668 /* start processing */
94d16a64
JS
1669 if (action_check_completion_mode(common, errp) < 0) {
1670 return;
1671 }
1672
43de7e2d
AG
1673 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1674 if (!state->old_bs) {
9b9877ee
WX
1675 return;
1676 }
1677
5d6e96ef
SH
1678 /* Acquire AioContext now so any threads operating on old_bs stop */
1679 state->aio_context = bdrv_get_aio_context(state->old_bs);
1680 aio_context_acquire(state->aio_context);
da763e83 1681 bdrv_drained_begin(state->old_bs);
5d6e96ef 1682
ba5d6ab6 1683 if (!bdrv_is_inserted(state->old_bs)) {
c6bd8c70 1684 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
9b9877ee
WX
1685 return;
1686 }
1687
3718d8ab
FZ
1688 if (bdrv_op_is_blocked(state->old_bs,
1689 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
9b9877ee
WX
1690 return;
1691 }
1692
ba5d6ab6
SH
1693 if (!bdrv_is_read_only(state->old_bs)) {
1694 if (bdrv_flush(state->old_bs)) {
c6bd8c70 1695 error_setg(errp, QERR_IO_ERROR);
9b9877ee
WX
1696 return;
1697 }
1698 }
1699
212a5a8f 1700 if (!bdrv_is_first_non_filter(state->old_bs)) {
c6bd8c70 1701 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
f6186f49
BC
1702 return;
1703 }
1704
43de7e2d
AG
1705 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
1706 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1707 const char *format = s->has_format ? s->format : "qcow2";
1708 enum NewImageMode mode;
1709 const char *snapshot_node_name =
1710 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
9b9877ee 1711
43de7e2d
AG
1712 if (node_name && !snapshot_node_name) {
1713 error_setg(errp, "New snapshot node name missing");
9b9877ee
WX
1714 return;
1715 }
9b9877ee 1716
43de7e2d
AG
1717 if (snapshot_node_name &&
1718 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1719 error_setg(errp, "New snapshot node name already in use");
1720 return;
1721 }
1722
1723 flags = state->old_bs->open_flags;
1724
1725 /* create new image w/backing file */
1726 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1727 if (mode != NEW_IMAGE_MODE_EXISTING) {
f86b8b58
KW
1728 int64_t size = bdrv_getlength(state->old_bs);
1729 if (size < 0) {
1730 error_setg_errno(errp, -size, "bdrv_getlength failed");
1731 return;
1732 }
43de7e2d
AG
1733 bdrv_img_create(new_image_file, format,
1734 state->old_bs->filename,
1735 state->old_bs->drv->format_name,
f86b8b58 1736 NULL, size, flags, &local_err, false);
43de7e2d
AG
1737 if (local_err) {
1738 error_propagate(errp, local_err);
1739 return;
1740 }
1741 }
1742
1743 options = qdict_new();
1744 if (s->has_snapshot_node_name) {
1745 qdict_put(options, "node-name",
1746 qstring_from_str(snapshot_node_name));
1747 }
1748 qdict_put(options, "driver", qstring_from_str(format));
1749
1750 flags |= BDRV_O_NO_BACKING;
0901f67e
BC
1751 }
1752
f67503e5 1753 assert(state->new_bs == NULL);
43de7e2d
AG
1754 ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options,
1755 flags, errp);
f67503e5 1756 /* We will manually add the backing_hd field to the bs later */
9b9877ee 1757 if (ret != 0) {
43de7e2d
AG
1758 return;
1759 }
1760
1761 if (state->new_bs->blk != NULL) {
1762 error_setg(errp, "The snapshot is already in use by %s",
1763 blk_name(state->new_bs->blk));
1764 return;
1765 }
1766
1767 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1768 errp)) {
1769 return;
1770 }
1771
1772 if (state->new_bs->backing != NULL) {
1773 error_setg(errp, "The snapshot already has a backing image");
08b24cfe
AG
1774 return;
1775 }
1776
1777 if (!state->new_bs->drv->supports_backing) {
1778 error_setg(errp, "The snapshot does not support backing images");
9b9877ee
WX
1779 }
1780}
1781
50f43f0f 1782static void external_snapshot_commit(BlkActionState *common)
3b0047e8 1783{
ba5d6ab6
SH
1784 ExternalSnapshotState *state =
1785 DO_UPCAST(ExternalSnapshotState, common, common);
ba0c86a3 1786
5d6e96ef
SH
1787 bdrv_set_aio_context(state->new_bs, state->aio_context);
1788
ba5d6ab6
SH
1789 /* This removes our old bs and adds the new bs */
1790 bdrv_append(state->new_bs, state->old_bs);
3b0047e8
WX
1791 /* We don't need (or want) to use the transactional
1792 * bdrv_reopen_multiple() across all the entries at once, because we
1793 * don't want to abort all of them if one of them fails the reopen */
dd62f1ca 1794 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
3b0047e8
WX
1795 NULL);
1796}
1797
50f43f0f 1798static void external_snapshot_abort(BlkActionState *common)
96b86bf7 1799{
ba5d6ab6
SH
1800 ExternalSnapshotState *state =
1801 DO_UPCAST(ExternalSnapshotState, common, common);
1802 if (state->new_bs) {
4f6fd349 1803 bdrv_unref(state->new_bs);
96b86bf7 1804 }
da763e83
FZ
1805}
1806
50f43f0f 1807static void external_snapshot_clean(BlkActionState *common)
da763e83
FZ
1808{
1809 ExternalSnapshotState *state =
1810 DO_UPCAST(ExternalSnapshotState, common, common);
5d6e96ef 1811 if (state->aio_context) {
da763e83 1812 bdrv_drained_end(state->old_bs);
5d6e96ef
SH
1813 aio_context_release(state->aio_context);
1814 }
96b86bf7
WX
1815}
1816
3037f364 1817typedef struct DriveBackupState {
50f43f0f 1818 BlkActionState common;
3037f364 1819 BlockDriverState *bs;
5d6e96ef 1820 AioContext *aio_context;
3037f364
SH
1821 BlockJob *job;
1822} DriveBackupState;
1823
78f51fde
JS
1824static void do_drive_backup(const char *device, const char *target,
1825 bool has_format, const char *format,
1826 enum MirrorSyncMode sync,
1827 bool has_mode, enum NewImageMode mode,
1828 bool has_speed, int64_t speed,
1829 bool has_bitmap, const char *bitmap,
1830 bool has_on_source_error,
1831 BlockdevOnError on_source_error,
1832 bool has_on_target_error,
1833 BlockdevOnError on_target_error,
1834 BlockJobTxn *txn, Error **errp);
1835
50f43f0f 1836static void drive_backup_prepare(BlkActionState *common, Error **errp)
3037f364
SH
1837{
1838 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
a0e8544c 1839 BlockBackend *blk;
3037f364
SH
1840 DriveBackup *backup;
1841 Error *local_err = NULL;
1842
6a8f9661
EB
1843 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1844 backup = common->action->u.drive_backup;
3037f364 1845
a0e8544c
FZ
1846 blk = blk_by_name(backup->device);
1847 if (!blk) {
75158ebb
MA
1848 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1849 "Device '%s' not found", backup->device);
5d6e96ef
SH
1850 return;
1851 }
1852
1fdd4b7b
FZ
1853 if (!blk_is_available(blk)) {
1854 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1855 return;
1856 }
1857
5d6e96ef 1858 /* AioContext is released in .clean() */
5433c24f 1859 state->aio_context = blk_get_aio_context(blk);
5d6e96ef 1860 aio_context_acquire(state->aio_context);
1fdd4b7b
FZ
1861 bdrv_drained_begin(blk_bs(blk));
1862 state->bs = blk_bs(blk);
5d6e96ef 1863
78f51fde
JS
1864 do_drive_backup(backup->device, backup->target,
1865 backup->has_format, backup->format,
1866 backup->sync,
1867 backup->has_mode, backup->mode,
1868 backup->has_speed, backup->speed,
1869 backup->has_bitmap, backup->bitmap,
1870 backup->has_on_source_error, backup->on_source_error,
1871 backup->has_on_target_error, backup->on_target_error,
94d16a64 1872 common->block_job_txn, &local_err);
84d18f06 1873 if (local_err) {
3037f364 1874 error_propagate(errp, local_err);
3037f364
SH
1875 return;
1876 }
1877
3037f364
SH
1878 state->job = state->bs->job;
1879}
1880
50f43f0f 1881static void drive_backup_abort(BlkActionState *common)
3037f364
SH
1882{
1883 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1884 BlockDriverState *bs = state->bs;
1885
1886 /* Only cancel if it's the job we started */
1887 if (bs && bs->job && bs->job == state->job) {
1888 block_job_cancel_sync(bs->job);
1889 }
1890}
1891
50f43f0f 1892static void drive_backup_clean(BlkActionState *common)
5d6e96ef
SH
1893{
1894 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1895
1896 if (state->aio_context) {
1fdd4b7b 1897 bdrv_drained_end(state->bs);
5d6e96ef
SH
1898 aio_context_release(state->aio_context);
1899 }
1900}
1901
bd8baecd 1902typedef struct BlockdevBackupState {
50f43f0f 1903 BlkActionState common;
bd8baecd
FZ
1904 BlockDriverState *bs;
1905 BlockJob *job;
1906 AioContext *aio_context;
1907} BlockdevBackupState;
1908
78f51fde
JS
1909static void do_blockdev_backup(const char *device, const char *target,
1910 enum MirrorSyncMode sync,
1911 bool has_speed, int64_t speed,
1912 bool has_on_source_error,
1913 BlockdevOnError on_source_error,
1914 bool has_on_target_error,
1915 BlockdevOnError on_target_error,
1916 BlockJobTxn *txn, Error **errp);
1917
50f43f0f 1918static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
bd8baecd
FZ
1919{
1920 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1921 BlockdevBackup *backup;
5433c24f 1922 BlockBackend *blk, *target;
bd8baecd
FZ
1923 Error *local_err = NULL;
1924
6a8f9661
EB
1925 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1926 backup = common->action->u.blockdev_backup;
bd8baecd 1927
a0e8544c
FZ
1928 blk = blk_by_name(backup->device);
1929 if (!blk) {
5b347c54 1930 error_setg(errp, "Device '%s' not found", backup->device);
bd8baecd
FZ
1931 return;
1932 }
1933
ff52bf36
FZ
1934 if (!blk_is_available(blk)) {
1935 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1936 return;
1937 }
1938
5433c24f
HR
1939 target = blk_by_name(backup->target);
1940 if (!target) {
5b347c54 1941 error_setg(errp, "Device '%s' not found", backup->target);
bd8baecd
FZ
1942 return;
1943 }
1944
1945 /* AioContext is released in .clean() */
5433c24f
HR
1946 state->aio_context = blk_get_aio_context(blk);
1947 if (state->aio_context != blk_get_aio_context(target)) {
bd8baecd
FZ
1948 state->aio_context = NULL;
1949 error_setg(errp, "Backup between two IO threads is not implemented");
1950 return;
1951 }
1952 aio_context_acquire(state->aio_context);
ff52bf36
FZ
1953 state->bs = blk_bs(blk);
1954 bdrv_drained_begin(state->bs);
bd8baecd 1955
78f51fde
JS
1956 do_blockdev_backup(backup->device, backup->target,
1957 backup->sync,
1958 backup->has_speed, backup->speed,
1959 backup->has_on_source_error, backup->on_source_error,
1960 backup->has_on_target_error, backup->on_target_error,
94d16a64 1961 common->block_job_txn, &local_err);
bd8baecd
FZ
1962 if (local_err) {
1963 error_propagate(errp, local_err);
1964 return;
1965 }
1966
bd8baecd
FZ
1967 state->job = state->bs->job;
1968}
1969
50f43f0f 1970static void blockdev_backup_abort(BlkActionState *common)
bd8baecd
FZ
1971{
1972 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1973 BlockDriverState *bs = state->bs;
1974
1975 /* Only cancel if it's the job we started */
1976 if (bs && bs->job && bs->job == state->job) {
1977 block_job_cancel_sync(bs->job);
1978 }
1979}
1980
50f43f0f 1981static void blockdev_backup_clean(BlkActionState *common)
bd8baecd
FZ
1982{
1983 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1984
1985 if (state->aio_context) {
ff52bf36 1986 bdrv_drained_end(state->bs);
bd8baecd
FZ
1987 aio_context_release(state->aio_context);
1988 }
1989}
1990
df9a681d 1991typedef struct BlockDirtyBitmapState {
50f43f0f 1992 BlkActionState common;
df9a681d
FZ
1993 BdrvDirtyBitmap *bitmap;
1994 BlockDriverState *bs;
1995 AioContext *aio_context;
1996 HBitmap *backup;
1997 bool prepared;
1998} BlockDirtyBitmapState;
1999
50f43f0f 2000static void block_dirty_bitmap_add_prepare(BlkActionState *common,
df9a681d
FZ
2001 Error **errp)
2002{
2003 Error *local_err = NULL;
2004 BlockDirtyBitmapAdd *action;
2005 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2006 common, common);
2007
94d16a64
JS
2008 if (action_check_completion_mode(common, errp) < 0) {
2009 return;
2010 }
2011
50f43f0f 2012 action = common->action->u.block_dirty_bitmap_add;
df9a681d
FZ
2013 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2014 qmp_block_dirty_bitmap_add(action->node, action->name,
2015 action->has_granularity, action->granularity,
2016 &local_err);
2017
2018 if (!local_err) {
2019 state->prepared = true;
2020 } else {
2021 error_propagate(errp, local_err);
2022 }
2023}
2024
50f43f0f 2025static void block_dirty_bitmap_add_abort(BlkActionState *common)
df9a681d
FZ
2026{
2027 BlockDirtyBitmapAdd *action;
2028 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2029 common, common);
2030
50f43f0f 2031 action = common->action->u.block_dirty_bitmap_add;
df9a681d
FZ
2032 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2033 * then the node reference and bitmap name must have been valid.
2034 */
2035 if (state->prepared) {
2036 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2037 }
2038}
2039
50f43f0f 2040static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
df9a681d
FZ
2041 Error **errp)
2042{
2043 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2044 common, common);
2045 BlockDirtyBitmap *action;
2046
94d16a64
JS
2047 if (action_check_completion_mode(common, errp) < 0) {
2048 return;
2049 }
2050
50f43f0f 2051 action = common->action->u.block_dirty_bitmap_clear;
df9a681d
FZ
2052 state->bitmap = block_dirty_bitmap_lookup(action->node,
2053 action->name,
2054 &state->bs,
2055 &state->aio_context,
2056 errp);
2057 if (!state->bitmap) {
2058 return;
2059 }
2060
2061 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2062 error_setg(errp, "Cannot modify a frozen bitmap");
2063 return;
2064 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2065 error_setg(errp, "Cannot clear a disabled bitmap");
2066 return;
2067 }
2068
2069 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2070 /* AioContext is released in .clean() */
2071}
2072
50f43f0f 2073static void block_dirty_bitmap_clear_abort(BlkActionState *common)
df9a681d
FZ
2074{
2075 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2076 common, common);
2077
2078 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2079}
2080
50f43f0f 2081static void block_dirty_bitmap_clear_commit(BlkActionState *common)
df9a681d
FZ
2082{
2083 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2084 common, common);
2085
2086 hbitmap_free(state->backup);
2087}
2088
50f43f0f 2089static void block_dirty_bitmap_clear_clean(BlkActionState *common)
df9a681d
FZ
2090{
2091 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2092 common, common);
2093
2094 if (state->aio_context) {
2095 aio_context_release(state->aio_context);
2096 }
2097}
2098
50f43f0f 2099static void abort_prepare(BlkActionState *common, Error **errp)
78b18b78
SH
2100{
2101 error_setg(errp, "Transaction aborted using Abort action");
2102}
2103
50f43f0f 2104static void abort_commit(BlkActionState *common)
78b18b78 2105{
dfc6f865 2106 g_assert_not_reached(); /* this action never succeeds */
78b18b78
SH
2107}
2108
50f43f0f 2109static const BlkActionOps actions[] = {
43de7e2d
AG
2110 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2111 .instance_size = sizeof(ExternalSnapshotState),
2112 .prepare = external_snapshot_prepare,
2113 .commit = external_snapshot_commit,
2114 .abort = external_snapshot_abort,
4ad6f3db 2115 .clean = external_snapshot_clean,
43de7e2d 2116 },
c8a83e85 2117 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
ba5d6ab6 2118 .instance_size = sizeof(ExternalSnapshotState),
ba0c86a3
WX
2119 .prepare = external_snapshot_prepare,
2120 .commit = external_snapshot_commit,
2121 .abort = external_snapshot_abort,
da763e83 2122 .clean = external_snapshot_clean,
ba0c86a3 2123 },
3037f364
SH
2124 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2125 .instance_size = sizeof(DriveBackupState),
2126 .prepare = drive_backup_prepare,
2127 .abort = drive_backup_abort,
5d6e96ef 2128 .clean = drive_backup_clean,
3037f364 2129 },
bd8baecd
FZ
2130 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2131 .instance_size = sizeof(BlockdevBackupState),
2132 .prepare = blockdev_backup_prepare,
2133 .abort = blockdev_backup_abort,
2134 .clean = blockdev_backup_clean,
2135 },
78b18b78 2136 [TRANSACTION_ACTION_KIND_ABORT] = {
50f43f0f 2137 .instance_size = sizeof(BlkActionState),
78b18b78
SH
2138 .prepare = abort_prepare,
2139 .commit = abort_commit,
2140 },
bbe86010
WX
2141 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2142 .instance_size = sizeof(InternalSnapshotState),
2143 .prepare = internal_snapshot_prepare,
2144 .abort = internal_snapshot_abort,
5d6e96ef 2145 .clean = internal_snapshot_clean,
bbe86010 2146 },
df9a681d
FZ
2147 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2148 .instance_size = sizeof(BlockDirtyBitmapState),
2149 .prepare = block_dirty_bitmap_add_prepare,
2150 .abort = block_dirty_bitmap_add_abort,
2151 },
2152 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2153 .instance_size = sizeof(BlockDirtyBitmapState),
2154 .prepare = block_dirty_bitmap_clear_prepare,
2155 .commit = block_dirty_bitmap_clear_commit,
2156 .abort = block_dirty_bitmap_clear_abort,
2157 .clean = block_dirty_bitmap_clear_clean,
2158 }
ba0c86a3
WX
2159};
2160
94d16a64
JS
2161/**
2162 * Allocate a TransactionProperties structure if necessary, and fill
2163 * that structure with desired defaults if they are unset.
2164 */
2165static TransactionProperties *get_transaction_properties(
2166 TransactionProperties *props)
2167{
2168 if (!props) {
2169 props = g_new0(TransactionProperties, 1);
2170 }
2171
2172 if (!props->has_completion_mode) {
2173 props->has_completion_mode = true;
2174 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2175 }
2176
2177 return props;
2178}
2179
8802d1fd 2180/*
b756b9ce
SH
2181 * 'Atomic' group operations. The operations are performed as a set, and if
2182 * any fail then we roll back all operations in the group.
8802d1fd 2183 */
94d16a64
JS
2184void qmp_transaction(TransactionActionList *dev_list,
2185 bool has_props,
2186 struct TransactionProperties *props,
2187 Error **errp)
8802d1fd 2188{
c8a83e85 2189 TransactionActionList *dev_entry = dev_list;
94d16a64 2190 BlockJobTxn *block_job_txn = NULL;
50f43f0f 2191 BlkActionState *state, *next;
43e17041 2192 Error *local_err = NULL;
8802d1fd 2193
50f43f0f 2194 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
8802d1fd
JC
2195 QSIMPLEQ_INIT(&snap_bdrv_states);
2196
94d16a64
JS
2197 /* Does this transaction get canceled as a group on failure?
2198 * If not, we don't really need to make a BlockJobTxn.
2199 */
2200 props = get_transaction_properties(props);
2201 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2202 block_job_txn = block_job_txn_new();
2203 }
2204
b756b9ce 2205 /* drain all i/o before any operations */
8802d1fd
JC
2206 bdrv_drain_all();
2207
b756b9ce 2208 /* We don't do anything in this loop that commits us to the operations */
8802d1fd 2209 while (NULL != dev_entry) {
c8a83e85 2210 TransactionAction *dev_info = NULL;
50f43f0f 2211 const BlkActionOps *ops;
52e7c241 2212
8802d1fd
JC
2213 dev_info = dev_entry->value;
2214 dev_entry = dev_entry->next;
2215
6a8f9661 2216 assert(dev_info->type < ARRAY_SIZE(actions));
ba0c86a3 2217
6a8f9661 2218 ops = &actions[dev_info->type];
aa3fe714
HR
2219 assert(ops->instance_size > 0);
2220
ba5d6ab6
SH
2221 state = g_malloc0(ops->instance_size);
2222 state->ops = ops;
2223 state->action = dev_info;
94d16a64
JS
2224 state->block_job_txn = block_job_txn;
2225 state->txn_props = props;
ba5d6ab6 2226 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
8802d1fd 2227
ba5d6ab6 2228 state->ops->prepare(state, &local_err);
84d18f06 2229 if (local_err) {
ba0c86a3
WX
2230 error_propagate(errp, local_err);
2231 goto delete_and_fail;
8802d1fd 2232 }
8802d1fd
JC
2233 }
2234
ba5d6ab6 2235 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
f9ea81e8
SH
2236 if (state->ops->commit) {
2237 state->ops->commit(state);
2238 }
8802d1fd
JC
2239 }
2240
2241 /* success */
2242 goto exit;
2243
2244delete_and_fail:
b756b9ce 2245 /* failure, and it is all-or-none; roll back all operations */
ba5d6ab6
SH
2246 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2247 if (state->ops->abort) {
2248 state->ops->abort(state);
ba0c86a3 2249 }
8802d1fd
JC
2250 }
2251exit:
ba5d6ab6
SH
2252 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2253 if (state->ops->clean) {
2254 state->ops->clean(state);
ba0c86a3 2255 }
ba5d6ab6 2256 g_free(state);
8802d1fd 2257 }
94d16a64
JS
2258 if (!has_props) {
2259 qapi_free_TransactionProperties(props);
2260 }
2261 block_job_txn_unref(block_job_txn);
8802d1fd
JC
2262}
2263
c245b6a3 2264void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
666daa68 2265{
38f54bd1 2266 Error *local_err = NULL;
666daa68 2267
38f54bd1
HR
2268 qmp_blockdev_open_tray(device, has_force, force, &local_err);
2269 if (local_err) {
2270 error_propagate(errp, local_err);
c245b6a3 2271 return;
92d48558
LC
2272 }
2273
6e0abc25 2274 qmp_x_blockdev_remove_medium(device, errp);
666daa68
MA
2275}
2276
12d3ba82
BC
2277void qmp_block_passwd(bool has_device, const char *device,
2278 bool has_node_name, const char *node_name,
2279 const char *password, Error **errp)
666daa68 2280{
12d3ba82 2281 Error *local_err = NULL;
666daa68 2282 BlockDriverState *bs;
e3442099 2283 AioContext *aio_context;
666daa68 2284
12d3ba82
BC
2285 bs = bdrv_lookup_bs(has_device ? device : NULL,
2286 has_node_name ? node_name : NULL,
2287 &local_err);
84d18f06 2288 if (local_err) {
12d3ba82 2289 error_propagate(errp, local_err);
a4dea8a9 2290 return;
666daa68
MA
2291 }
2292
e3442099
SH
2293 aio_context = bdrv_get_aio_context(bs);
2294 aio_context_acquire(aio_context);
2295
4d2855a3 2296 bdrv_add_key(bs, password, errp);
e3442099 2297
e3442099 2298 aio_context_release(aio_context);
666daa68
MA
2299}
2300
7d8a9f71
HR
2301void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
2302 Error **errp)
2303{
2304 BlockBackend *blk;
2305 bool locked;
2306
2307 if (!has_force) {
2308 force = false;
2309 }
2310
2311 blk = blk_by_name(device);
2312 if (!blk) {
2313 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2314 "Device '%s' not found", device);
2315 return;
2316 }
2317
2318 if (!blk_dev_has_removable_media(blk)) {
2319 error_setg(errp, "Device '%s' is not removable", device);
2320 return;
2321 }
2322
12c7ec87
HR
2323 if (!blk_dev_has_tray(blk)) {
2324 /* Ignore this command on tray-less devices */
2325 return;
2326 }
2327
7d8a9f71
HR
2328 if (blk_dev_is_tray_open(blk)) {
2329 return;
2330 }
2331
2332 locked = blk_dev_is_medium_locked(blk);
2333 if (locked) {
2334 blk_dev_eject_request(blk, force);
2335 }
2336
2337 if (!locked || force) {
2338 blk_dev_change_media_cb(blk, false);
2339 }
2340}
2341
abaaf59d
HR
2342void qmp_blockdev_close_tray(const char *device, Error **errp)
2343{
2344 BlockBackend *blk;
2345
2346 blk = blk_by_name(device);
2347 if (!blk) {
2348 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2349 "Device '%s' not found", device);
2350 return;
2351 }
2352
2353 if (!blk_dev_has_removable_media(blk)) {
2354 error_setg(errp, "Device '%s' is not removable", device);
2355 return;
2356 }
2357
12c7ec87
HR
2358 if (!blk_dev_has_tray(blk)) {
2359 /* Ignore this command on tray-less devices */
2360 return;
2361 }
2362
abaaf59d
HR
2363 if (!blk_dev_is_tray_open(blk)) {
2364 return;
2365 }
2366
2367 blk_dev_change_media_cb(blk, true);
2368}
2369
6e0abc25 2370void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
2814f672
HR
2371{
2372 BlockBackend *blk;
2373 BlockDriverState *bs;
2374 AioContext *aio_context;
2375 bool has_device;
2376
2377 blk = blk_by_name(device);
2378 if (!blk) {
2379 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2380 "Device '%s' not found", device);
2381 return;
2382 }
2383
2384 /* For BBs without a device, we can exchange the BDS tree at will */
2385 has_device = blk_get_attached_dev(blk);
2386
2387 if (has_device && !blk_dev_has_removable_media(blk)) {
2388 error_setg(errp, "Device '%s' is not removable", device);
2389 return;
2390 }
2391
12c7ec87 2392 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2814f672
HR
2393 error_setg(errp, "Tray of device '%s' is not open", device);
2394 return;
2395 }
2396
2397 bs = blk_bs(blk);
2398 if (!bs) {
2399 return;
2400 }
2401
2402 aio_context = bdrv_get_aio_context(bs);
2403 aio_context_acquire(aio_context);
2404
2405 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2406 goto out;
2407 }
2408
2409 /* This follows the convention established by bdrv_make_anon() */
2410 if (bs->device_list.tqe_prev) {
f8aa905a 2411 bdrv_device_remove(bs);
2814f672
HR
2412 }
2413
2414 blk_remove_bs(blk);
2415
12c7ec87
HR
2416 if (!blk_dev_has_tray(blk)) {
2417 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2418 * called at all); therefore, the medium needs to be ejected here.
2419 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2420 * value passed here (i.e. false). */
2421 blk_dev_change_media_cb(blk, false);
2422 }
2423
2814f672
HR
2424out:
2425 aio_context_release(aio_context);
2426}
2427
d1299882
HR
2428static void qmp_blockdev_insert_anon_medium(const char *device,
2429 BlockDriverState *bs, Error **errp)
2430{
2431 BlockBackend *blk;
2432 bool has_device;
2433
2434 blk = blk_by_name(device);
2435 if (!blk) {
2436 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2437 "Device '%s' not found", device);
2438 return;
2439 }
2440
2441 /* For BBs without a device, we can exchange the BDS tree at will */
2442 has_device = blk_get_attached_dev(blk);
2443
2444 if (has_device && !blk_dev_has_removable_media(blk)) {
2445 error_setg(errp, "Device '%s' is not removable", device);
2446 return;
2447 }
2448
12c7ec87 2449 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
d1299882
HR
2450 error_setg(errp, "Tray of device '%s' is not open", device);
2451 return;
2452 }
2453
2454 if (blk_bs(blk)) {
2455 error_setg(errp, "There already is a medium in device '%s'", device);
2456 return;
2457 }
2458
2459 blk_insert_bs(blk, bs);
2460
2461 QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
12c7ec87
HR
2462
2463 if (!blk_dev_has_tray(blk)) {
2464 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2465 * called at all); therefore, the medium needs to be pushed into the
2466 * slot here.
2467 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2468 * value passed here (i.e. true). */
2469 blk_dev_change_media_cb(blk, true);
2470 }
d1299882
HR
2471}
2472
6e0abc25
HR
2473void qmp_x_blockdev_insert_medium(const char *device, const char *node_name,
2474 Error **errp)
d1299882
HR
2475{
2476 BlockDriverState *bs;
2477
2478 bs = bdrv_find_node(node_name);
2479 if (!bs) {
2480 error_setg(errp, "Node '%s' not found", node_name);
2481 return;
2482 }
2483
2484 if (bs->blk) {
2485 error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
2486 blk_name(bs->blk));
2487 return;
2488 }
2489
2490 qmp_blockdev_insert_anon_medium(device, bs, errp);
2491}
2492
24fb4133
HR
2493void qmp_blockdev_change_medium(const char *device, const char *filename,
2494 bool has_format, const char *format,
39ff43d9
HR
2495 bool has_read_only,
2496 BlockdevChangeReadOnlyMode read_only,
24fb4133 2497 Error **errp)
de2c6c05
HR
2498{
2499 BlockBackend *blk;
2500 BlockDriverState *medium_bs = NULL;
2501 int bdrv_flags, ret;
2502 QDict *options = NULL;
2503 Error *err = NULL;
2504
2505 blk = blk_by_name(device);
2506 if (!blk) {
2507 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2508 "Device '%s' not found", device);
2509 goto fail;
2510 }
2511
2512 if (blk_bs(blk)) {
2513 blk_update_root_state(blk);
2514 }
2515
2516 bdrv_flags = blk_get_open_flags_from_root_state(blk);
156abc2f
AM
2517 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2518 BDRV_O_PROTOCOL);
de2c6c05 2519
39ff43d9
HR
2520 if (!has_read_only) {
2521 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2522 }
2523
2524 switch (read_only) {
2525 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2526 break;
2527
2528 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2529 bdrv_flags &= ~BDRV_O_RDWR;
2530 break;
2531
2532 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2533 bdrv_flags |= BDRV_O_RDWR;
2534 break;
2535
2536 default:
2537 abort();
2538 }
2539
24fb4133 2540 if (has_format) {
de2c6c05
HR
2541 options = qdict_new();
2542 qdict_put(options, "driver", qstring_from_str(format));
2543 }
2544
2545 assert(!medium_bs);
2546 ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2547 if (ret < 0) {
2548 goto fail;
2549 }
2550
2551 blk_apply_root_state(blk, medium_bs);
2552
2553 bdrv_add_key(medium_bs, NULL, &err);
2554 if (err) {
2555 error_propagate(errp, err);
2556 goto fail;
2557 }
2558
2559 qmp_blockdev_open_tray(device, false, false, &err);
2560 if (err) {
2561 error_propagate(errp, err);
2562 goto fail;
2563 }
2564
6e0abc25 2565 qmp_x_blockdev_remove_medium(device, &err);
de2c6c05
HR
2566 if (err) {
2567 error_propagate(errp, err);
2568 goto fail;
2569 }
2570
2571 qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2572 if (err) {
2573 error_propagate(errp, err);
2574 goto fail;
2575 }
2576
2577 qmp_blockdev_close_tray(device, errp);
2578
2579fail:
2580 /* If the medium has been inserted, the device has its own reference, so
2581 * ours must be relinquished; and if it has not been inserted successfully,
2582 * the reference must be relinquished anyway */
2583 bdrv_unref(medium_bs);
2584}
2585
727f005e 2586/* throttling disk I/O limits */
80047da5 2587void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
3e9fab69
BC
2588 int64_t bps_wr,
2589 int64_t iops,
2590 int64_t iops_rd,
2591 int64_t iops_wr,
2592 bool has_bps_max,
2593 int64_t bps_max,
2594 bool has_bps_rd_max,
2595 int64_t bps_rd_max,
2596 bool has_bps_wr_max,
2597 int64_t bps_wr_max,
2598 bool has_iops_max,
2599 int64_t iops_max,
2600 bool has_iops_rd_max,
2601 int64_t iops_rd_max,
2602 bool has_iops_wr_max,
2024c1df 2603 int64_t iops_wr_max,
dce13204
AG
2604 bool has_bps_max_length,
2605 int64_t bps_max_length,
2606 bool has_bps_rd_max_length,
2607 int64_t bps_rd_max_length,
2608 bool has_bps_wr_max_length,
2609 int64_t bps_wr_max_length,
2610 bool has_iops_max_length,
2611 int64_t iops_max_length,
2612 bool has_iops_rd_max_length,
2613 int64_t iops_rd_max_length,
2614 bool has_iops_wr_max_length,
2615 int64_t iops_wr_max_length,
2024c1df 2616 bool has_iops_size,
76f4afb4
AG
2617 int64_t iops_size,
2618 bool has_group,
2619 const char *group, Error **errp)
727f005e 2620{
cc0681c4 2621 ThrottleConfig cfg;
727f005e 2622 BlockDriverState *bs;
a0e8544c 2623 BlockBackend *blk;
b15446fd 2624 AioContext *aio_context;
727f005e 2625
a0e8544c
FZ
2626 blk = blk_by_name(device);
2627 if (!blk) {
75158ebb
MA
2628 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2629 "Device '%s' not found", device);
80047da5 2630 return;
727f005e 2631 }
5433c24f
HR
2632
2633 aio_context = blk_get_aio_context(blk);
2634 aio_context_acquire(aio_context);
2635
a0e8544c 2636 bs = blk_bs(blk);
5433c24f
HR
2637 if (!bs) {
2638 error_setg(errp, "Device '%s' has no medium", device);
2639 goto out;
2640 }
727f005e 2641
1588ab5d 2642 throttle_config_init(&cfg);
cc0681c4
BC
2643 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2644 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
2645 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
2646
2647 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2648 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
2649 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2650
3e9fab69
BC
2651 if (has_bps_max) {
2652 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
2653 }
2654 if (has_bps_rd_max) {
2655 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
2656 }
2657 if (has_bps_wr_max) {
2658 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2659 }
2660 if (has_iops_max) {
2661 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2662 }
2663 if (has_iops_rd_max) {
2664 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2665 }
2666 if (has_iops_wr_max) {
2667 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2668 }
727f005e 2669
dce13204
AG
2670 if (has_bps_max_length) {
2671 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = bps_max_length;
2672 }
2673 if (has_bps_rd_max_length) {
2674 cfg.buckets[THROTTLE_BPS_READ].burst_length = bps_rd_max_length;
2675 }
2676 if (has_bps_wr_max_length) {
2677 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = bps_wr_max_length;
2678 }
2679 if (has_iops_max_length) {
2680 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = iops_max_length;
2681 }
2682 if (has_iops_rd_max_length) {
2683 cfg.buckets[THROTTLE_OPS_READ].burst_length = iops_rd_max_length;
2684 }
2685 if (has_iops_wr_max_length) {
2686 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = iops_wr_max_length;
2687 }
2688
2024c1df
BC
2689 if (has_iops_size) {
2690 cfg.op_size = iops_size;
2691 }
cc0681c4 2692
d5851089 2693 if (!throttle_is_valid(&cfg, errp)) {
5433c24f 2694 goto out;
727f005e
ZYW
2695 }
2696
76f4afb4
AG
2697 if (throttle_enabled(&cfg)) {
2698 /* Enable I/O limits if they're not enabled yet, otherwise
2699 * just update the throttling group. */
a0d64a61 2700 if (!bs->throttle_state) {
76f4afb4
AG
2701 bdrv_io_limits_enable(bs, has_group ? group : device);
2702 } else if (has_group) {
2703 bdrv_io_limits_update_group(bs, group);
2704 }
2705 /* Set the new throttling configuration */
cc0681c4 2706 bdrv_set_io_limits(bs, &cfg);
a0d64a61 2707 } else if (bs->throttle_state) {
76f4afb4
AG
2708 /* If all throttling settings are set to 0, disable I/O limits */
2709 bdrv_io_limits_disable(bs);
727f005e 2710 }
b15446fd 2711
5433c24f 2712out:
b15446fd 2713 aio_context_release(aio_context);
727f005e
ZYW
2714}
2715
341ebc2f
JS
2716void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2717 bool has_granularity, uint32_t granularity,
2718 Error **errp)
2719{
2720 AioContext *aio_context;
2721 BlockDriverState *bs;
2722
2723 if (!name || name[0] == '\0') {
2724 error_setg(errp, "Bitmap name cannot be empty");
2725 return;
2726 }
2727
2728 bs = bdrv_lookup_bs(node, node, errp);
2729 if (!bs) {
2730 return;
2731 }
2732
2733 aio_context = bdrv_get_aio_context(bs);
2734 aio_context_acquire(aio_context);
2735
2736 if (has_granularity) {
2737 if (granularity < 512 || !is_power_of_2(granularity)) {
2738 error_setg(errp, "Granularity must be power of 2 "
2739 "and at least 512");
2740 goto out;
2741 }
2742 } else {
2743 /* Default to cluster size, if available: */
2744 granularity = bdrv_get_default_bitmap_granularity(bs);
2745 }
2746
2747 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2748
2749 out:
2750 aio_context_release(aio_context);
2751}
2752
2753void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2754 Error **errp)
2755{
2756 AioContext *aio_context;
2757 BlockDriverState *bs;
2758 BdrvDirtyBitmap *bitmap;
2759
2760 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2761 if (!bitmap || !bs) {
2762 return;
2763 }
2764
9bd2b08f
JS
2765 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2766 error_setg(errp,
2767 "Bitmap '%s' is currently frozen and cannot be removed",
2768 name);
2769 goto out;
2770 }
20dca810 2771 bdrv_dirty_bitmap_make_anon(bitmap);
341ebc2f
JS
2772 bdrv_release_dirty_bitmap(bs, bitmap);
2773
9bd2b08f 2774 out:
341ebc2f
JS
2775 aio_context_release(aio_context);
2776}
2777
e74e6b78
JS
2778/**
2779 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2780 * immediately after a full backup operation.
2781 */
2782void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2783 Error **errp)
2784{
2785 AioContext *aio_context;
2786 BdrvDirtyBitmap *bitmap;
2787 BlockDriverState *bs;
2788
2789 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2790 if (!bitmap || !bs) {
2791 return;
2792 }
2793
2794 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2795 error_setg(errp,
2796 "Bitmap '%s' is currently frozen and cannot be modified",
2797 name);
2798 goto out;
2799 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2800 error_setg(errp,
2801 "Bitmap '%s' is currently disabled and cannot be cleared",
2802 name);
2803 goto out;
2804 }
2805
df9a681d 2806 bdrv_clear_dirty_bitmap(bitmap, NULL);
e74e6b78
JS
2807
2808 out:
2809 aio_context_release(aio_context);
2810}
2811
072ebe6b 2812void hmp_drive_del(Monitor *mon, const QDict *qdict)
9063f814
RH
2813{
2814 const char *id = qdict_get_str(qdict, "id");
7e7d56d9 2815 BlockBackend *blk;
9063f814 2816 BlockDriverState *bs;
8ad4202b 2817 AioContext *aio_context;
3718d8ab 2818 Error *local_err = NULL;
9063f814 2819
7e7d56d9
MA
2820 blk = blk_by_name(id);
2821 if (!blk) {
b1422f20 2822 error_report("Device '%s' not found", id);
072ebe6b 2823 return;
9063f814 2824 }
8ad4202b 2825
26f8b3a8 2826 if (!blk_legacy_dinfo(blk)) {
48f364dd
MA
2827 error_report("Deleting device added with blockdev-add"
2828 " is not supported");
072ebe6b 2829 return;
48f364dd
MA
2830 }
2831
5433c24f 2832 aio_context = blk_get_aio_context(blk);
8ad4202b
SH
2833 aio_context_acquire(aio_context);
2834
5433c24f
HR
2835 bs = blk_bs(blk);
2836 if (bs) {
2837 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2838 error_report_err(local_err);
2839 aio_context_release(aio_context);
2840 return;
2841 }
9063f814 2842
938abd43 2843 blk_remove_bs(blk);
5433c24f 2844 }
9063f814 2845
fa879d62 2846 /* if we have a device attached to this BlockDriverState
d22b2f41
RH
2847 * then we need to make the drive anonymous until the device
2848 * can be removed. If this is a drive with no device backing
2849 * then we can just get rid of the block driver state right here.
2850 */
a7f53e26 2851 if (blk_get_attached_dev(blk)) {
3e5a50d6 2852 blk_hide_on_behalf_of_hmp_drive_del(blk);
293c51a6 2853 /* Further I/O must not pause the guest */
373340b2
HR
2854 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2855 BLOCKDEV_ON_ERROR_REPORT);
d22b2f41 2856 } else {
b9fe8a7a 2857 blk_unref(blk);
9063f814
RH
2858 }
2859
8ad4202b 2860 aio_context_release(aio_context);
9063f814 2861}
6d4a2b3a 2862
3b1dbd11
BC
2863void qmp_block_resize(bool has_device, const char *device,
2864 bool has_node_name, const char *node_name,
2865 int64_t size, Error **errp)
6d4a2b3a 2866{
3b1dbd11 2867 Error *local_err = NULL;
6d4a2b3a 2868 BlockDriverState *bs;
927e0e76 2869 AioContext *aio_context;
8732901e 2870 int ret;
6d4a2b3a 2871
3b1dbd11
BC
2872 bs = bdrv_lookup_bs(has_device ? device : NULL,
2873 has_node_name ? node_name : NULL,
2874 &local_err);
84d18f06 2875 if (local_err) {
3b1dbd11
BC
2876 error_propagate(errp, local_err);
2877 return;
2878 }
2879
927e0e76
SH
2880 aio_context = bdrv_get_aio_context(bs);
2881 aio_context_acquire(aio_context);
2882
3b1dbd11 2883 if (!bdrv_is_first_non_filter(bs)) {
c6bd8c70 2884 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
927e0e76 2885 goto out;
6d4a2b3a
CH
2886 }
2887
2888 if (size < 0) {
c6bd8c70 2889 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
927e0e76 2890 goto out;
6d4a2b3a
CH
2891 }
2892
9c75e168 2893 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
c6bd8c70 2894 error_setg(errp, QERR_DEVICE_IN_USE, device);
927e0e76 2895 goto out;
9c75e168
JC
2896 }
2897
92b7a08d
PL
2898 /* complete all in-flight operations before resizing the device */
2899 bdrv_drain_all();
2900
8732901e
KW
2901 ret = bdrv_truncate(bs, size);
2902 switch (ret) {
939a1cc3
SH
2903 case 0:
2904 break;
2905 case -ENOMEDIUM:
c6bd8c70 2906 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
939a1cc3
SH
2907 break;
2908 case -ENOTSUP:
c6bd8c70 2909 error_setg(errp, QERR_UNSUPPORTED);
939a1cc3
SH
2910 break;
2911 case -EACCES:
81e5f78a 2912 error_setg(errp, "Device '%s' is read only", device);
939a1cc3
SH
2913 break;
2914 case -EBUSY:
c6bd8c70 2915 error_setg(errp, QERR_DEVICE_IN_USE, device);
939a1cc3
SH
2916 break;
2917 default:
8732901e 2918 error_setg_errno(errp, -ret, "Could not resize");
939a1cc3 2919 break;
6d4a2b3a 2920 }
927e0e76
SH
2921
2922out:
2923 aio_context_release(aio_context);
6d4a2b3a 2924}
12bd451f 2925
9abf2dba 2926static void block_job_cb(void *opaque, int ret)
12bd451f 2927{
723c5d93
SH
2928 /* Note that this function may be executed from another AioContext besides
2929 * the QEMU main loop. If you need to access anything that assumes the
2930 * QEMU global mutex, use a BH or introduce a mutex.
2931 */
2932
12bd451f 2933 BlockDriverState *bs = opaque;
bcada37b 2934 const char *msg = NULL;
12bd451f 2935
9abf2dba 2936 trace_block_job_cb(bs, bs->job, ret);
12bd451f
SH
2937
2938 assert(bs->job);
bcada37b 2939
12bd451f 2940 if (ret < 0) {
bcada37b 2941 msg = strerror(-ret);
12bd451f
SH
2942 }
2943
370521a1 2944 if (block_job_is_cancelled(bs->job)) {
bcada37b 2945 block_job_event_cancelled(bs->job);
370521a1 2946 } else {
bcada37b 2947 block_job_event_completed(bs->job, msg);
370521a1 2948 }
12bd451f
SH
2949}
2950
13d8cc51
JC
2951void qmp_block_stream(const char *device,
2952 bool has_base, const char *base,
2953 bool has_backing_file, const char *backing_file,
2954 bool has_speed, int64_t speed,
1d809098
PB
2955 bool has_on_error, BlockdevOnError on_error,
2956 Error **errp)
12bd451f 2957{
a0e8544c 2958 BlockBackend *blk;
12bd451f 2959 BlockDriverState *bs;
c8c3080f 2960 BlockDriverState *base_bs = NULL;
f3e69beb 2961 AioContext *aio_context;
fd7f8c65 2962 Error *local_err = NULL;
13d8cc51 2963 const char *base_name = NULL;
12bd451f 2964
1d809098
PB
2965 if (!has_on_error) {
2966 on_error = BLOCKDEV_ON_ERROR_REPORT;
2967 }
2968
a0e8544c
FZ
2969 blk = blk_by_name(device);
2970 if (!blk) {
75158ebb
MA
2971 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2972 "Device '%s' not found", device);
12bd451f
SH
2973 return;
2974 }
2975
5433c24f 2976 aio_context = blk_get_aio_context(blk);
f3e69beb
SH
2977 aio_context_acquire(aio_context);
2978
5433c24f
HR
2979 if (!blk_is_available(blk)) {
2980 error_setg(errp, "Device '%s' has no medium", device);
2981 goto out;
2982 }
2983 bs = blk_bs(blk);
2984
628ff683 2985 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
f3e69beb 2986 goto out;
628ff683
FZ
2987 }
2988
13d8cc51 2989 if (has_base) {
c8c3080f
MT
2990 base_bs = bdrv_find_backing_image(bs, base);
2991 if (base_bs == NULL) {
c6bd8c70 2992 error_setg(errp, QERR_BASE_NOT_FOUND, base);
f3e69beb 2993 goto out;
c8c3080f 2994 }
f3e69beb 2995 assert(bdrv_get_aio_context(base_bs) == aio_context);
13d8cc51 2996 base_name = base;
12bd451f
SH
2997 }
2998
13d8cc51
JC
2999 /* if we are streaming the entire chain, the result will have no backing
3000 * file, and specifying one is therefore an error */
3001 if (base_bs == NULL && has_backing_file) {
3002 error_setg(errp, "backing file specified, but streaming the "
3003 "entire chain");
f3e69beb 3004 goto out;
13d8cc51
JC
3005 }
3006
3007 /* backing_file string overrides base bs filename */
3008 base_name = has_backing_file ? backing_file : base_name;
3009
3010 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
1d809098 3011 on_error, block_job_cb, bs, &local_err);
84d18f06 3012 if (local_err) {
fd7f8c65 3013 error_propagate(errp, local_err);
f3e69beb 3014 goto out;
12bd451f
SH
3015 }
3016
3017 trace_qmp_block_stream(bs, bs->job);
f3e69beb
SH
3018
3019out:
3020 aio_context_release(aio_context);
12bd451f 3021}
2d47c6e9 3022
ed61fc10 3023void qmp_block_commit(const char *device,
7676e2c5
JC
3024 bool has_base, const char *base,
3025 bool has_top, const char *top,
54e26900 3026 bool has_backing_file, const char *backing_file,
ed61fc10
JC
3027 bool has_speed, int64_t speed,
3028 Error **errp)
3029{
a0e8544c 3030 BlockBackend *blk;
ed61fc10
JC
3031 BlockDriverState *bs;
3032 BlockDriverState *base_bs, *top_bs;
9e85cd5c 3033 AioContext *aio_context;
ed61fc10
JC
3034 Error *local_err = NULL;
3035 /* This will be part of the QMP command, if/when the
3036 * BlockdevOnError change for blkmirror makes it in
3037 */
92aa5c6d 3038 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
ed61fc10 3039
54504663
HR
3040 if (!has_speed) {
3041 speed = 0;
3042 }
3043
7676e2c5
JC
3044 /* Important Note:
3045 * libvirt relies on the DeviceNotFound error class in order to probe for
3046 * live commit feature versions; for this to work, we must make sure to
3047 * perform the device lookup before any generic errors that may occur in a
3048 * scenario in which all optional arguments are omitted. */
a0e8544c
FZ
3049 blk = blk_by_name(device);
3050 if (!blk) {
75158ebb
MA
3051 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3052 "Device '%s' not found", device);
ed61fc10 3053 return;
628ff683
FZ
3054 }
3055
5433c24f 3056 aio_context = blk_get_aio_context(blk);
9e85cd5c
SH
3057 aio_context_acquire(aio_context);
3058
5433c24f
HR
3059 if (!blk_is_available(blk)) {
3060 error_setg(errp, "Device '%s' has no medium", device);
3061 goto out;
3062 }
3063 bs = blk_bs(blk);
3064
bb00021d 3065 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
9e85cd5c 3066 goto out;
ed61fc10 3067 }
ed61fc10
JC
3068
3069 /* default top_bs is the active layer */
3070 top_bs = bs;
3071
7676e2c5 3072 if (has_top && top) {
ed61fc10
JC
3073 if (strcmp(bs->filename, top) != 0) {
3074 top_bs = bdrv_find_backing_image(bs, top);
3075 }
3076 }
3077
3078 if (top_bs == NULL) {
3079 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
9e85cd5c 3080 goto out;
ed61fc10
JC
3081 }
3082
9e85cd5c
SH
3083 assert(bdrv_get_aio_context(top_bs) == aio_context);
3084
d5208c45
JC
3085 if (has_base && base) {
3086 base_bs = bdrv_find_backing_image(top_bs, base);
3087 } else {
3088 base_bs = bdrv_find_base(top_bs);
3089 }
3090
3091 if (base_bs == NULL) {
c6bd8c70 3092 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
9e85cd5c 3093 goto out;
d5208c45
JC
3094 }
3095
9e85cd5c
SH
3096 assert(bdrv_get_aio_context(base_bs) == aio_context);
3097
bb00021d
FZ
3098 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3099 goto out;
3100 }
3101
7676e2c5
JC
3102 /* Do not allow attempts to commit an image into itself */
3103 if (top_bs == base_bs) {
3104 error_setg(errp, "cannot commit an image into itself");
9e85cd5c 3105 goto out;
7676e2c5
JC
3106 }
3107
20a63d2c 3108 if (top_bs == bs) {
54e26900
JC
3109 if (has_backing_file) {
3110 error_setg(errp, "'backing-file' specified,"
3111 " but 'top' is the active layer");
9e85cd5c 3112 goto out;
54e26900 3113 }
20a63d2c
FZ
3114 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
3115 bs, &local_err);
3116 } else {
3117 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
54e26900 3118 has_backing_file ? backing_file : NULL, &local_err);
20a63d2c 3119 }
ed61fc10
JC
3120 if (local_err != NULL) {
3121 error_propagate(errp, local_err);
9e85cd5c 3122 goto out;
ed61fc10 3123 }
9e85cd5c
SH
3124
3125out:
3126 aio_context_release(aio_context);
ed61fc10
JC
3127}
3128
78f51fde
JS
3129static void do_drive_backup(const char *device, const char *target,
3130 bool has_format, const char *format,
3131 enum MirrorSyncMode sync,
3132 bool has_mode, enum NewImageMode mode,
3133 bool has_speed, int64_t speed,
3134 bool has_bitmap, const char *bitmap,
3135 bool has_on_source_error,
3136 BlockdevOnError on_source_error,
3137 bool has_on_target_error,
3138 BlockdevOnError on_target_error,
3139 BlockJobTxn *txn, Error **errp)
99a9addf 3140{
a0e8544c 3141 BlockBackend *blk;
99a9addf
SH
3142 BlockDriverState *bs;
3143 BlockDriverState *target_bs;
fc5d3f84 3144 BlockDriverState *source = NULL;
d58d8453 3145 BdrvDirtyBitmap *bmap = NULL;
761731b1 3146 AioContext *aio_context;
e6641719 3147 QDict *options = NULL;
99a9addf
SH
3148 Error *local_err = NULL;
3149 int flags;
3150 int64_t size;
3151 int ret;
3152
3153 if (!has_speed) {
3154 speed = 0;
3155 }
3156 if (!has_on_source_error) {
3157 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3158 }
3159 if (!has_on_target_error) {
3160 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3161 }
3162 if (!has_mode) {
3163 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3164 }
3165
a0e8544c
FZ
3166 blk = blk_by_name(device);
3167 if (!blk) {
75158ebb
MA
3168 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3169 "Device '%s' not found", device);
99a9addf
SH
3170 return;
3171 }
3172
5433c24f 3173 aio_context = blk_get_aio_context(blk);
761731b1
SH
3174 aio_context_acquire(aio_context);
3175
c29c1dd3
FZ
3176 /* Although backup_run has this check too, we need to use bs->drv below, so
3177 * do an early check redundantly. */
5433c24f 3178 if (!blk_is_available(blk)) {
c6bd8c70 3179 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
761731b1 3180 goto out;
99a9addf 3181 }
5433c24f 3182 bs = blk_bs(blk);
99a9addf
SH
3183
3184 if (!has_format) {
3185 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3186 }
99a9addf 3187
c29c1dd3 3188 /* Early check to avoid creating target */
3718d8ab 3189 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
761731b1 3190 goto out;
99a9addf
SH
3191 }
3192
3193 flags = bs->open_flags | BDRV_O_RDWR;
3194
fc5d3f84
IM
3195 /* See if we have a backing HD we can use to create our new image
3196 * on top of. */
3197 if (sync == MIRROR_SYNC_MODE_TOP) {
760e0063 3198 source = backing_bs(bs);
fc5d3f84
IM
3199 if (!source) {
3200 sync = MIRROR_SYNC_MODE_FULL;
3201 }
3202 }
3203 if (sync == MIRROR_SYNC_MODE_NONE) {
3204 source = bs;
3205 }
3206
99a9addf
SH
3207 size = bdrv_getlength(bs);
3208 if (size < 0) {
3209 error_setg_errno(errp, -size, "bdrv_getlength failed");
761731b1 3210 goto out;
99a9addf
SH
3211 }
3212
3213 if (mode != NEW_IMAGE_MODE_EXISTING) {
e6641719 3214 assert(format);
fc5d3f84
IM
3215 if (source) {
3216 bdrv_img_create(target, format, source->filename,
3217 source->drv->format_name, NULL,
3218 size, flags, &local_err, false);
3219 } else {
3220 bdrv_img_create(target, format, NULL, NULL, NULL,
3221 size, flags, &local_err, false);
3222 }
99a9addf
SH
3223 }
3224
84d18f06 3225 if (local_err) {
99a9addf 3226 error_propagate(errp, local_err);
761731b1 3227 goto out;
99a9addf
SH
3228 }
3229
e6641719
HR
3230 if (format) {
3231 options = qdict_new();
3232 qdict_put(options, "driver", qstring_from_str(format));
3233 }
3234
f67503e5 3235 target_bs = NULL;
6ebf9aa2 3236 ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
99a9addf 3237 if (ret < 0) {
34b5d2c6 3238 error_propagate(errp, local_err);
761731b1 3239 goto out;
99a9addf
SH
3240 }
3241
761731b1
SH
3242 bdrv_set_aio_context(target_bs, aio_context);
3243
d58d8453
JS
3244 if (has_bitmap) {
3245 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3246 if (!bmap) {
3247 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
0702d3d8 3248 bdrv_unref(target_bs);
d58d8453
JS
3249 goto out;
3250 }
3251 }
3252
3253 backup_start(bs, target_bs, speed, sync, bmap,
3254 on_source_error, on_target_error,
78f51fde 3255 block_job_cb, bs, txn, &local_err);
99a9addf 3256 if (local_err != NULL) {
4f6fd349 3257 bdrv_unref(target_bs);
99a9addf 3258 error_propagate(errp, local_err);
761731b1 3259 goto out;
99a9addf 3260 }
761731b1
SH
3261
3262out:
3263 aio_context_release(aio_context);
99a9addf
SH
3264}
3265
78f51fde
JS
3266void qmp_drive_backup(const char *device, const char *target,
3267 bool has_format, const char *format,
3268 enum MirrorSyncMode sync,
3269 bool has_mode, enum NewImageMode mode,
3270 bool has_speed, int64_t speed,
3271 bool has_bitmap, const char *bitmap,
3272 bool has_on_source_error, BlockdevOnError on_source_error,
3273 bool has_on_target_error, BlockdevOnError on_target_error,
3274 Error **errp)
3275{
3276 return do_drive_backup(device, target, has_format, format, sync,
3277 has_mode, mode, has_speed, speed,
3278 has_bitmap, bitmap,
3279 has_on_source_error, on_source_error,
3280 has_on_target_error, on_target_error,
3281 NULL, errp);
3282}
3283
c13163fb
BC
3284BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3285{
d5a8ee60 3286 return bdrv_named_nodes_list(errp);
c13163fb
BC
3287}
3288
78f51fde 3289void do_blockdev_backup(const char *device, const char *target,
c29c1dd3
FZ
3290 enum MirrorSyncMode sync,
3291 bool has_speed, int64_t speed,
3292 bool has_on_source_error,
3293 BlockdevOnError on_source_error,
3294 bool has_on_target_error,
3295 BlockdevOnError on_target_error,
78f51fde 3296 BlockJobTxn *txn, Error **errp)
c29c1dd3 3297{
5433c24f 3298 BlockBackend *blk, *target_blk;
c29c1dd3
FZ
3299 BlockDriverState *bs;
3300 BlockDriverState *target_bs;
3301 Error *local_err = NULL;
3302 AioContext *aio_context;
3303
3304 if (!has_speed) {
3305 speed = 0;
3306 }
3307 if (!has_on_source_error) {
3308 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3309 }
3310 if (!has_on_target_error) {
3311 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3312 }
3313
a0e8544c
FZ
3314 blk = blk_by_name(device);
3315 if (!blk) {
5b347c54 3316 error_setg(errp, "Device '%s' not found", device);
c29c1dd3
FZ
3317 return;
3318 }
3319
5433c24f 3320 aio_context = blk_get_aio_context(blk);
c29c1dd3
FZ
3321 aio_context_acquire(aio_context);
3322
5433c24f
HR
3323 if (!blk_is_available(blk)) {
3324 error_setg(errp, "Device '%s' has no medium", device);
3325 goto out;
3326 }
3327 bs = blk_bs(blk);
3328
3329 target_blk = blk_by_name(target);
3330 if (!target_blk) {
5b347c54 3331 error_setg(errp, "Device '%s' not found", target);
c29c1dd3
FZ
3332 goto out;
3333 }
5433c24f
HR
3334
3335 if (!blk_is_available(target_blk)) {
3336 error_setg(errp, "Device '%s' has no medium", target);
3337 goto out;
3338 }
3339 target_bs = blk_bs(target_blk);
c29c1dd3
FZ
3340
3341 bdrv_ref(target_bs);
3342 bdrv_set_aio_context(target_bs, aio_context);
d58d8453 3343 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
78f51fde 3344 on_target_error, block_job_cb, bs, txn, &local_err);
c29c1dd3
FZ
3345 if (local_err != NULL) {
3346 bdrv_unref(target_bs);
3347 error_propagate(errp, local_err);
3348 }
3349out:
3350 aio_context_release(aio_context);
3351}
3352
78f51fde
JS
3353void qmp_blockdev_backup(const char *device, const char *target,
3354 enum MirrorSyncMode sync,
3355 bool has_speed, int64_t speed,
3356 bool has_on_source_error,
3357 BlockdevOnError on_source_error,
3358 bool has_on_target_error,
3359 BlockdevOnError on_target_error,
3360 Error **errp)
3361{
3362 do_blockdev_backup(device, target, sync, has_speed, speed,
3363 has_on_source_error, on_source_error,
3364 has_on_target_error, on_target_error,
3365 NULL, errp);
3366}
3367
4193cdd7
FZ
3368/* Parameter check and block job starting for drive mirroring.
3369 * Caller should hold @device and @target's aio context (must be the same).
3370 **/
3371static void blockdev_mirror_common(BlockDriverState *bs,
3372 BlockDriverState *target,
3373 bool has_replaces, const char *replaces,
3374 enum MirrorSyncMode sync,
3375 bool has_speed, int64_t speed,
3376 bool has_granularity, uint32_t granularity,
3377 bool has_buf_size, int64_t buf_size,
3378 bool has_on_source_error,
3379 BlockdevOnError on_source_error,
3380 bool has_on_target_error,
3381 BlockdevOnError on_target_error,
3382 bool has_unmap, bool unmap,
3383 Error **errp)
d9b902db 3384{
d9b902db
PB
3385
3386 if (!has_speed) {
3387 speed = 0;
3388 }
b952b558
PB
3389 if (!has_on_source_error) {
3390 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3391 }
3392 if (!has_on_target_error) {
3393 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3394 }
eee13dfe
PB
3395 if (!has_granularity) {
3396 granularity = 0;
3397 }
08e4ed6c 3398 if (!has_buf_size) {
48ac0a4d 3399 buf_size = 0;
08e4ed6c 3400 }
0fc9f8ea
FZ
3401 if (!has_unmap) {
3402 unmap = true;
3403 }
08e4ed6c 3404
eee13dfe 3405 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
c6bd8c70
MA
3406 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3407 "a value in range [512B, 64MB]");
eee13dfe
PB
3408 return;
3409 }
3410 if (granularity & (granularity - 1)) {
c6bd8c70
MA
3411 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3412 "power of 2");
eee13dfe
PB
3413 return;
3414 }
d9b902db 3415
4193cdd7
FZ
3416 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3417 return;
3418 }
e40e5027
FZ
3419 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3420 return;
3421 }
df92562e
FZ
3422 if (target->blk) {
3423 error_setg(errp, "Cannot mirror to an attached block device");
3424 return;
3425 }
4193cdd7
FZ
3426
3427 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3428 sync = MIRROR_SYNC_MODE_FULL;
3429 }
3430
3431 /* pass the node name to replace to mirror start since it's loose coupling
3432 * and will allow to check whether the node still exist at mirror completion
3433 */
3434 mirror_start(bs, target,
3435 has_replaces ? replaces : NULL,
3436 speed, granularity, buf_size, sync,
3437 on_source_error, on_target_error, unmap,
3438 block_job_cb, bs, errp);
3439}
3440
3441void qmp_drive_mirror(const char *device, const char *target,
3442 bool has_format, const char *format,
3443 bool has_node_name, const char *node_name,
3444 bool has_replaces, const char *replaces,
3445 enum MirrorSyncMode sync,
3446 bool has_mode, enum NewImageMode mode,
3447 bool has_speed, int64_t speed,
3448 bool has_granularity, uint32_t granularity,
3449 bool has_buf_size, int64_t buf_size,
3450 bool has_on_source_error, BlockdevOnError on_source_error,
3451 bool has_on_target_error, BlockdevOnError on_target_error,
3452 bool has_unmap, bool unmap,
3453 Error **errp)
3454{
3455 BlockDriverState *bs;
3456 BlockBackend *blk;
3457 BlockDriverState *source, *target_bs;
3458 AioContext *aio_context;
3459 Error *local_err = NULL;
3460 QDict *options = NULL;
3461 int flags;
3462 int64_t size;
3463 int ret;
3464
a0e8544c
FZ
3465 blk = blk_by_name(device);
3466 if (!blk) {
75158ebb
MA
3467 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3468 "Device '%s' not found", device);
d9b902db
PB
3469 return;
3470 }
3471
5433c24f 3472 aio_context = blk_get_aio_context(blk);
5a7e7a0b
SH
3473 aio_context_acquire(aio_context);
3474
5433c24f 3475 if (!blk_is_available(blk)) {
c6bd8c70 3476 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
5a7e7a0b 3477 goto out;
d9b902db 3478 }
5433c24f 3479 bs = blk_bs(blk);
4193cdd7
FZ
3480 if (!has_mode) {
3481 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3482 }
d9b902db
PB
3483
3484 if (!has_format) {
3485 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3486 }
d9b902db 3487
d9b902db 3488 flags = bs->open_flags | BDRV_O_RDWR;
760e0063 3489 source = backing_bs(bs);
d9b902db
PB
3490 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
3491 sync = MIRROR_SYNC_MODE_FULL;
3492 }
117e0c82
HR
3493 if (sync == MIRROR_SYNC_MODE_NONE) {
3494 source = bs;
3495 }
d9b902db 3496
ac3c5d83
SH
3497 size = bdrv_getlength(bs);
3498 if (size < 0) {
3499 error_setg_errno(errp, -size, "bdrv_getlength failed");
5a7e7a0b 3500 goto out;
ac3c5d83
SH
3501 }
3502
09158f00
BC
3503 if (has_replaces) {
3504 BlockDriverState *to_replace_bs;
5a7e7a0b
SH
3505 AioContext *replace_aio_context;
3506 int64_t replace_size;
09158f00
BC
3507
3508 if (!has_node_name) {
3509 error_setg(errp, "a node-name must be provided when replacing a"
3510 " named node of the graph");
5a7e7a0b 3511 goto out;
09158f00
BC
3512 }
3513
e12f3784 3514 to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
09158f00
BC
3515
3516 if (!to_replace_bs) {
3517 error_propagate(errp, local_err);
5a7e7a0b 3518 goto out;
09158f00
BC
3519 }
3520
5a7e7a0b
SH
3521 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3522 aio_context_acquire(replace_aio_context);
3523 replace_size = bdrv_getlength(to_replace_bs);
3524 aio_context_release(replace_aio_context);
3525
3526 if (size != replace_size) {
09158f00
BC
3527 error_setg(errp, "cannot replace image with a mirror image of "
3528 "different size");
5a7e7a0b 3529 goto out;
09158f00
BC
3530 }
3531 }
3532
14526864
HR
3533 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
3534 && mode != NEW_IMAGE_MODE_EXISTING)
3535 {
d9b902db 3536 /* create new image w/o backing file */
e6641719 3537 assert(format);
cf8f2426 3538 bdrv_img_create(target, format,
f382d43a 3539 NULL, NULL, NULL, size, flags, &local_err, false);
d9b902db
PB
3540 } else {
3541 switch (mode) {
3542 case NEW_IMAGE_MODE_EXISTING:
d9b902db
PB
3543 break;
3544 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3545 /* create new image with backing file */
cf8f2426
LC
3546 bdrv_img_create(target, format,
3547 source->filename,
3548 source->drv->format_name,
f382d43a 3549 NULL, size, flags, &local_err, false);
d9b902db
PB
3550 break;
3551 default:
3552 abort();
3553 }
3554 }
3555
84d18f06 3556 if (local_err) {
cf8f2426 3557 error_propagate(errp, local_err);
5a7e7a0b 3558 goto out;
d9b902db
PB
3559 }
3560
e6641719 3561 options = qdict_new();
4c828dc6 3562 if (has_node_name) {
4c828dc6
BC
3563 qdict_put(options, "node-name", qstring_from_str(node_name));
3564 }
e6641719
HR
3565 if (format) {
3566 qdict_put(options, "driver", qstring_from_str(format));
3567 }
4c828dc6 3568
b812f671
PB
3569 /* Mirroring takes care of copy-on-write using the source's backing
3570 * file.
3571 */
f67503e5 3572 target_bs = NULL;
4c828dc6 3573 ret = bdrv_open(&target_bs, target, NULL, options,
6ebf9aa2 3574 flags | BDRV_O_NO_BACKING, &local_err);
d9b902db 3575 if (ret < 0) {
34b5d2c6 3576 error_propagate(errp, local_err);
5a7e7a0b 3577 goto out;
d9b902db
PB
3578 }
3579
5a7e7a0b
SH
3580 bdrv_set_aio_context(target_bs, aio_context);
3581
4193cdd7
FZ
3582 blockdev_mirror_common(bs, target_bs,
3583 has_replaces, replaces, sync,
3584 has_speed, speed,
3585 has_granularity, granularity,
3586 has_buf_size, buf_size,
3587 has_on_source_error, on_source_error,
3588 has_on_target_error, on_target_error,
3589 has_unmap, unmap,
3590 &local_err);
3591 if (local_err) {
d9b902db 3592 error_propagate(errp, local_err);
4193cdd7 3593 bdrv_unref(target_bs);
d9b902db 3594 }
5a7e7a0b
SH
3595out:
3596 aio_context_release(aio_context);
d9b902db
PB
3597}
3598
df92562e
FZ
3599void qmp_blockdev_mirror(const char *device, const char *target,
3600 bool has_replaces, const char *replaces,
3601 MirrorSyncMode sync,
3602 bool has_speed, int64_t speed,
3603 bool has_granularity, uint32_t granularity,
3604 bool has_buf_size, int64_t buf_size,
3605 bool has_on_source_error,
3606 BlockdevOnError on_source_error,
3607 bool has_on_target_error,
3608 BlockdevOnError on_target_error,
3609 Error **errp)
3610{
3611 BlockDriverState *bs;
3612 BlockBackend *blk;
3613 BlockDriverState *target_bs;
3614 AioContext *aio_context;
3615 Error *local_err = NULL;
3616
3617 blk = blk_by_name(device);
3618 if (!blk) {
3619 error_setg(errp, "Device '%s' not found", device);
3620 return;
3621 }
3622 bs = blk_bs(blk);
3623
3624 if (!bs) {
3625 error_setg(errp, "Device '%s' has no media", device);
3626 return;
3627 }
3628
3629 target_bs = bdrv_lookup_bs(target, target, errp);
3630 if (!target_bs) {
3631 return;
3632 }
3633
3634 aio_context = bdrv_get_aio_context(bs);
3635 aio_context_acquire(aio_context);
3636
3637 bdrv_ref(target_bs);
3638 bdrv_set_aio_context(target_bs, aio_context);
3639
3640 blockdev_mirror_common(bs, target_bs,
3641 has_replaces, replaces, sync,
3642 has_speed, speed,
3643 has_granularity, granularity,
3644 has_buf_size, buf_size,
3645 has_on_source_error, on_source_error,
3646 has_on_target_error, on_target_error,
3647 true, true,
3648 &local_err);
3649 if (local_err) {
3650 error_propagate(errp, local_err);
3651 bdrv_unref(target_bs);
3652 }
3653
3654 aio_context_release(aio_context);
3655}
3656
3d948cdf 3657/* Get the block job for a given device name and acquire its AioContext */
24d6bffe
MA
3658static BlockJob *find_block_job(const char *device, AioContext **aio_context,
3659 Error **errp)
2d47c6e9 3660{
a0e8544c 3661 BlockBackend *blk;
2d47c6e9
SH
3662 BlockDriverState *bs;
3663
5433c24f
HR
3664 *aio_context = NULL;
3665
a0e8544c
FZ
3666 blk = blk_by_name(device);
3667 if (!blk) {
3d948cdf
SH
3668 goto notfound;
3669 }
3670
5433c24f 3671 *aio_context = blk_get_aio_context(blk);
3d948cdf
SH
3672 aio_context_acquire(*aio_context);
3673
5433c24f
HR
3674 if (!blk_is_available(blk)) {
3675 goto notfound;
3676 }
3677 bs = blk_bs(blk);
3678
3d948cdf 3679 if (!bs->job) {
3d948cdf 3680 goto notfound;
2d47c6e9 3681 }
3d948cdf 3682
2d47c6e9 3683 return bs->job;
3d948cdf
SH
3684
3685notfound:
2e3a0266
MA
3686 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3687 "No active block job on device '%s'", device);
5433c24f
HR
3688 if (*aio_context) {
3689 aio_context_release(*aio_context);
3690 *aio_context = NULL;
3691 }
3d948cdf 3692 return NULL;
2d47c6e9
SH
3693}
3694
882ec7ce 3695void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2d47c6e9 3696{
3d948cdf 3697 AioContext *aio_context;
24d6bffe 3698 BlockJob *job = find_block_job(device, &aio_context, errp);
2d47c6e9
SH
3699
3700 if (!job) {
2d47c6e9
SH
3701 return;
3702 }
3703
882ec7ce 3704 block_job_set_speed(job, speed, errp);
3d948cdf 3705 aio_context_release(aio_context);
2d47c6e9 3706}
370521a1 3707
6e37fb81
PB
3708void qmp_block_job_cancel(const char *device,
3709 bool has_force, bool force, Error **errp)
370521a1 3710{
3d948cdf 3711 AioContext *aio_context;
24d6bffe 3712 BlockJob *job = find_block_job(device, &aio_context, errp);
6e37fb81 3713
370521a1 3714 if (!job) {
370521a1
SH
3715 return;
3716 }
3d948cdf
SH
3717
3718 if (!has_force) {
3719 force = false;
3720 }
3721
751ebd76 3722 if (job->user_paused && !force) {
f231b88d
CR
3723 error_setg(errp, "The block job for device '%s' is currently paused",
3724 device);
3d948cdf 3725 goto out;
8acc72a4 3726 }
370521a1
SH
3727
3728 trace_qmp_block_job_cancel(job);
3729 block_job_cancel(job);
3d948cdf
SH
3730out:
3731 aio_context_release(aio_context);
370521a1 3732}
fb5458cd 3733
6e37fb81
PB
3734void qmp_block_job_pause(const char *device, Error **errp)
3735{
3d948cdf 3736 AioContext *aio_context;
24d6bffe 3737 BlockJob *job = find_block_job(device, &aio_context, errp);
6e37fb81 3738
751ebd76 3739 if (!job || job->user_paused) {
6e37fb81
PB
3740 return;
3741 }
3742
751ebd76 3743 job->user_paused = true;
6e37fb81
PB
3744 trace_qmp_block_job_pause(job);
3745 block_job_pause(job);
3d948cdf 3746 aio_context_release(aio_context);
6e37fb81
PB
3747}
3748
3749void qmp_block_job_resume(const char *device, Error **errp)
3750{
3d948cdf 3751 AioContext *aio_context;
24d6bffe 3752 BlockJob *job = find_block_job(device, &aio_context, errp);
6e37fb81 3753
751ebd76 3754 if (!job || !job->user_paused) {
6e37fb81
PB
3755 return;
3756 }
3757
751ebd76 3758 job->user_paused = false;
6e37fb81
PB
3759 trace_qmp_block_job_resume(job);
3760 block_job_resume(job);
3d948cdf 3761 aio_context_release(aio_context);
6e37fb81
PB
3762}
3763
aeae883b
PB
3764void qmp_block_job_complete(const char *device, Error **errp)
3765{
3d948cdf 3766 AioContext *aio_context;
24d6bffe 3767 BlockJob *job = find_block_job(device, &aio_context, errp);
aeae883b
PB
3768
3769 if (!job) {
aeae883b
PB
3770 return;
3771 }
3772
3773 trace_qmp_block_job_complete(job);
3774 block_job_complete(job, errp);
3d948cdf 3775 aio_context_release(aio_context);
aeae883b
PB
3776}
3777
fa40e656
JC
3778void qmp_change_backing_file(const char *device,
3779 const char *image_node_name,
3780 const char *backing_file,
3781 Error **errp)
3782{
a0e8544c 3783 BlockBackend *blk;
fa40e656 3784 BlockDriverState *bs = NULL;
729962f6 3785 AioContext *aio_context;
fa40e656
JC
3786 BlockDriverState *image_bs = NULL;
3787 Error *local_err = NULL;
3788 bool ro;
3789 int open_flags;
3790 int ret;
3791
a0e8544c
FZ
3792 blk = blk_by_name(device);
3793 if (!blk) {
75158ebb
MA
3794 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3795 "Device '%s' not found", device);
fa40e656
JC
3796 return;
3797 }
3798
5433c24f 3799 aio_context = blk_get_aio_context(blk);
729962f6
SH
3800 aio_context_acquire(aio_context);
3801
5433c24f
HR
3802 if (!blk_is_available(blk)) {
3803 error_setg(errp, "Device '%s' has no medium", device);
3804 goto out;
3805 }
3806 bs = blk_bs(blk);
3807
fa40e656
JC
3808 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3809 if (local_err) {
3810 error_propagate(errp, local_err);
729962f6 3811 goto out;
fa40e656
JC
3812 }
3813
3814 if (!image_bs) {
3815 error_setg(errp, "image file not found");
729962f6 3816 goto out;
fa40e656
JC
3817 }
3818
3819 if (bdrv_find_base(image_bs) == image_bs) {
3820 error_setg(errp, "not allowing backing file change on an image "
3821 "without a backing file");
729962f6 3822 goto out;
fa40e656
JC
3823 }
3824
3825 /* even though we are not necessarily operating on bs, we need it to
3826 * determine if block ops are currently prohibited on the chain */
3827 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
729962f6 3828 goto out;
fa40e656
JC
3829 }
3830
3831 /* final sanity check */
3832 if (!bdrv_chain_contains(bs, image_bs)) {
3833 error_setg(errp, "'%s' and image file are not in the same chain",
3834 device);
729962f6 3835 goto out;
fa40e656
JC
3836 }
3837
3838 /* if not r/w, reopen to make r/w */
3839 open_flags = image_bs->open_flags;
3840 ro = bdrv_is_read_only(image_bs);
3841
3842 if (ro) {
3843 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3844 if (local_err) {
3845 error_propagate(errp, local_err);
729962f6 3846 goto out;
fa40e656
JC
3847 }
3848 }
3849
3850 ret = bdrv_change_backing_file(image_bs, backing_file,
3851 image_bs->drv ? image_bs->drv->format_name : "");
3852
3853 if (ret < 0) {
3854 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3855 backing_file);
3856 /* don't exit here, so we can try to restore open flags if
3857 * appropriate */
3858 }
3859
3860 if (ro) {
3861 bdrv_reopen(image_bs, open_flags, &local_err);
3862 if (local_err) {
3863 error_propagate(errp, local_err); /* will preserve prior errp */
3864 }
3865 }
729962f6
SH
3866
3867out:
3868 aio_context_release(aio_context);
fa40e656
JC
3869}
3870
d26c9a15
KW
3871void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3872{
3873 QmpOutputVisitor *ov = qmp_output_visitor_new();
be4b67bc
HR
3874 BlockDriverState *bs;
3875 BlockBackend *blk = NULL;
d26c9a15
KW
3876 QObject *obj;
3877 QDict *qdict;
d26c9a15
KW
3878 Error *local_err = NULL;
3879
60e19e06 3880 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
d26c9a15 3881 * cache.direct=false instead of silently switching to aio=threads, except
60e19e06 3882 * when called from drive_new().
d26c9a15
KW
3883 *
3884 * For now, simply forbidding the combination for all drivers will do. */
3885 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
c6e0bd9b
KW
3886 bool direct = options->has_cache &&
3887 options->cache->has_direct &&
3888 options->cache->direct;
3889 if (!direct) {
d26c9a15
KW
3890 error_setg(errp, "aio=native requires cache.direct=true");
3891 goto fail;
3892 }
3893 }
3894
51e72bc1
EB
3895 visit_type_BlockdevOptions(qmp_output_get_visitor(ov), NULL, &options,
3896 &local_err);
84d18f06 3897 if (local_err) {
d26c9a15
KW
3898 error_propagate(errp, local_err);
3899 goto fail;
3900 }
3901
3902 obj = qmp_output_get_qobject(ov);
3903 qdict = qobject_to_qdict(obj);
3904
3905 qdict_flatten(qdict);
3906
be4b67bc
HR
3907 if (options->has_id) {
3908 blk = blockdev_init(NULL, qdict, &local_err);
3909 if (local_err) {
3910 error_propagate(errp, local_err);
3911 goto fail;
3912 }
3913
3914 bs = blk_bs(blk);
3915 } else {
be4b67bc
HR
3916 if (!qdict_get_try_str(qdict, "node-name")) {
3917 error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3918 "the root node");
3919 goto fail;
3920 }
3921
bd745e23
HR
3922 bs = bds_tree_init(qdict, errp);
3923 if (!bs) {
be4b67bc
HR
3924 goto fail;
3925 }
9c4218e9
HR
3926
3927 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
d26c9a15
KW
3928 }
3929
be4b67bc
HR
3930 if (bs && bdrv_key_required(bs)) {
3931 if (blk) {
3932 blk_unref(blk);
3933 } else {
9c4218e9 3934 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
be4b67bc
HR
3935 bdrv_unref(bs);
3936 }
8ae8e904
KW
3937 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3938 goto fail;
3939 }
3940
d26c9a15
KW
3941fail:
3942 qmp_output_visitor_cleanup(ov);
3943}
3944
81b936ae
AG
3945void qmp_x_blockdev_del(bool has_id, const char *id,
3946 bool has_node_name, const char *node_name, Error **errp)
3947{
3948 AioContext *aio_context;
3949 BlockBackend *blk;
3950 BlockDriverState *bs;
3951
3952 if (has_id && has_node_name) {
3953 error_setg(errp, "Only one of id and node-name must be specified");
3954 return;
3955 } else if (!has_id && !has_node_name) {
3956 error_setg(errp, "No block device specified");
3957 return;
3958 }
3959
3960 if (has_id) {
3961 blk = blk_by_name(id);
3962 if (!blk) {
3963 error_setg(errp, "Cannot find block backend %s", id);
3964 return;
3965 }
3966 if (blk_get_refcnt(blk) > 1) {
3967 error_setg(errp, "Block backend %s is in use", id);
3968 return;
3969 }
3970 bs = blk_bs(blk);
3971 aio_context = blk_get_aio_context(blk);
3972 } else {
3973 bs = bdrv_find_node(node_name);
3974 if (!bs) {
3975 error_setg(errp, "Cannot find node %s", node_name);
3976 return;
3977 }
3978 blk = bs->blk;
3979 if (blk) {
3980 error_setg(errp, "Node %s is in use by %s",
3981 node_name, blk_name(blk));
3982 return;
3983 }
3984 aio_context = bdrv_get_aio_context(bs);
3985 }
3986
3987 aio_context_acquire(aio_context);
3988
3989 if (bs) {
3990 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3991 goto out;
3992 }
3993
9c4218e9
HR
3994 if (!blk && !bs->monitor_list.tqe_prev) {
3995 error_setg(errp, "Node %s is not owned by the monitor",
3996 bs->node_name);
3997 goto out;
3998 }
3999
4000 if (bs->refcnt > 1) {
81b936ae
AG
4001 error_setg(errp, "Block device %s is in use",
4002 bdrv_get_device_or_node_name(bs));
4003 goto out;
4004 }
4005 }
4006
4007 if (blk) {
4008 blk_unref(blk);
4009 } else {
9c4218e9 4010 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
81b936ae
AG
4011 bdrv_unref(bs);
4012 }
4013
4014out:
4015 aio_context_release(aio_context);
4016}
4017
fea68bb6 4018BlockJobInfoList *qmp_query_block_jobs(Error **errp)
fb5458cd 4019{
fea68bb6
MA
4020 BlockJobInfoList *head = NULL, **p_next = &head;
4021 BlockDriverState *bs;
fb5458cd 4022
fea68bb6 4023 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
69691e72
SH
4024 AioContext *aio_context = bdrv_get_aio_context(bs);
4025
4026 aio_context_acquire(aio_context);
4027
fea68bb6
MA
4028 if (bs->job) {
4029 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
4030 elem->value = block_job_query(bs->job);
4031 *p_next = elem;
4032 p_next = &elem->next;
4033 }
69691e72
SH
4034
4035 aio_context_release(aio_context);
fb5458cd 4036 }
fb5458cd 4037
fea68bb6 4038 return head;
fb5458cd 4039}
4d454574 4040
0006383e 4041QemuOptsList qemu_common_drive_opts = {
4d454574 4042 .name = "drive",
0006383e 4043 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4d454574
PB
4044 .desc = {
4045 {
4d454574
PB
4046 .name = "snapshot",
4047 .type = QEMU_OPT_BOOL,
4048 .help = "enable/disable snapshot mode",
a9384aff
PB
4049 },{
4050 .name = "discard",
4051 .type = QEMU_OPT_STRING,
4052 .help = "discard operation (ignore/off, unmap/on)",
4d454574
PB
4053 },{
4054 .name = "aio",
4055 .type = QEMU_OPT_STRING,
4056 .help = "host AIO implementation (threads, native)",
4057 },{
4058 .name = "format",
4059 .type = QEMU_OPT_STRING,
4060 .help = "disk format (raw, qcow2, ...)",
4d454574
PB
4061 },{
4062 .name = "rerror",
4063 .type = QEMU_OPT_STRING,
4064 .help = "read error action",
4065 },{
4066 .name = "werror",
4067 .type = QEMU_OPT_STRING,
4068 .help = "write error action",
4d454574 4069 },{
0f227a94 4070 .name = "read-only",
4d454574
PB
4071 .type = QEMU_OPT_BOOL,
4072 .help = "open drive file as read-only",
4073 },{
57975222 4074 .name = "throttling.iops-total",
4d454574
PB
4075 .type = QEMU_OPT_NUMBER,
4076 .help = "limit total I/O operations per second",
4077 },{
57975222 4078 .name = "throttling.iops-read",
4d454574
PB
4079 .type = QEMU_OPT_NUMBER,
4080 .help = "limit read operations per second",
4081 },{
57975222 4082 .name = "throttling.iops-write",
4d454574
PB
4083 .type = QEMU_OPT_NUMBER,
4084 .help = "limit write operations per second",
4085 },{
57975222 4086 .name = "throttling.bps-total",
4d454574
PB
4087 .type = QEMU_OPT_NUMBER,
4088 .help = "limit total bytes per second",
4089 },{
57975222 4090 .name = "throttling.bps-read",
4d454574
PB
4091 .type = QEMU_OPT_NUMBER,
4092 .help = "limit read bytes per second",
4093 },{
57975222 4094 .name = "throttling.bps-write",
4d454574
PB
4095 .type = QEMU_OPT_NUMBER,
4096 .help = "limit write bytes per second",
3e9fab69
BC
4097 },{
4098 .name = "throttling.iops-total-max",
4099 .type = QEMU_OPT_NUMBER,
4100 .help = "I/O operations burst",
4101 },{
4102 .name = "throttling.iops-read-max",
4103 .type = QEMU_OPT_NUMBER,
4104 .help = "I/O operations read burst",
4105 },{
4106 .name = "throttling.iops-write-max",
4107 .type = QEMU_OPT_NUMBER,
4108 .help = "I/O operations write burst",
4109 },{
4110 .name = "throttling.bps-total-max",
4111 .type = QEMU_OPT_NUMBER,
4112 .help = "total bytes burst",
4113 },{
4114 .name = "throttling.bps-read-max",
4115 .type = QEMU_OPT_NUMBER,
4116 .help = "total bytes read burst",
4117 },{
4118 .name = "throttling.bps-write-max",
4119 .type = QEMU_OPT_NUMBER,
4120 .help = "total bytes write burst",
8a0fc18d
AG
4121 },{
4122 .name = "throttling.iops-total-max-length",
4123 .type = QEMU_OPT_NUMBER,
4124 .help = "length of the iops-total-max burst period, in seconds",
4125 },{
4126 .name = "throttling.iops-read-max-length",
4127 .type = QEMU_OPT_NUMBER,
4128 .help = "length of the iops-read-max burst period, in seconds",
4129 },{
4130 .name = "throttling.iops-write-max-length",
4131 .type = QEMU_OPT_NUMBER,
4132 .help = "length of the iops-write-max burst period, in seconds",
4133 },{
4134 .name = "throttling.bps-total-max-length",
4135 .type = QEMU_OPT_NUMBER,
4136 .help = "length of the bps-total-max burst period, in seconds",
4137 },{
4138 .name = "throttling.bps-read-max-length",
4139 .type = QEMU_OPT_NUMBER,
4140 .help = "length of the bps-read-max burst period, in seconds",
4141 },{
4142 .name = "throttling.bps-write-max-length",
4143 .type = QEMU_OPT_NUMBER,
4144 .help = "length of the bps-write-max burst period, in seconds",
2024c1df
BC
4145 },{
4146 .name = "throttling.iops-size",
4147 .type = QEMU_OPT_NUMBER,
4148 .help = "when limiting by iops max size of an I/O in bytes",
76f4afb4
AG
4149 },{
4150 .name = "throttling.group",
4151 .type = QEMU_OPT_STRING,
4152 .help = "name of the block throttling group",
4d454574
PB
4153 },{
4154 .name = "copy-on-read",
4155 .type = QEMU_OPT_BOOL,
4156 .help = "copy read data from backing file into image file",
465bee1d
PL
4157 },{
4158 .name = "detect-zeroes",
4159 .type = QEMU_OPT_STRING,
4160 .help = "try to optimize zero writes (off, on, unmap)",
362e9299
AG
4161 },{
4162 .name = "stats-account-invalid",
4163 .type = QEMU_OPT_BOOL,
4164 .help = "whether to account for invalid I/O operations "
4165 "in the statistics",
4166 },{
4167 .name = "stats-account-failed",
4168 .type = QEMU_OPT_BOOL,
4169 .help = "whether to account for failed I/O operations "
4170 "in the statistics",
4d454574
PB
4171 },
4172 { /* end of list */ }
4173 },
4174};
0006383e 4175
bd745e23
HR
4176static QemuOptsList qemu_root_bds_opts = {
4177 .name = "root-bds",
4178 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4179 .desc = {
4180 {
4181 .name = "discard",
4182 .type = QEMU_OPT_STRING,
4183 .help = "discard operation (ignore/off, unmap/on)",
bd745e23
HR
4184 },{
4185 .name = "aio",
4186 .type = QEMU_OPT_STRING,
4187 .help = "host AIO implementation (threads, native)",
4188 },{
4189 .name = "read-only",
4190 .type = QEMU_OPT_BOOL,
4191 .help = "open drive file as read-only",
4192 },{
4193 .name = "copy-on-read",
4194 .type = QEMU_OPT_BOOL,
4195 .help = "copy read data from backing file into image file",
4196 },{
4197 .name = "detect-zeroes",
4198 .type = QEMU_OPT_STRING,
4199 .help = "try to optimize zero writes (off, on, unmap)",
4200 },
4201 { /* end of list */ }
4202 },
4203};
4204
0006383e
KW
4205QemuOptsList qemu_drive_opts = {
4206 .name = "drive",
4207 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4208 .desc = {
492fdc6f
KW
4209 /*
4210 * no elements => accept any params
4211 * validation will happen later
4212 */
0006383e
KW
4213 { /* end of list */ }
4214 },
4215};