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