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