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