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