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