block: User BdrvChild callback for device name
[qemu.git] / blockdev.c
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.
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.
31 */
32
33 #include "qemu/osdep.h"
34 #include "sysemu/block-backend.h"
35 #include "sysemu/blockdev.h"
36 #include "hw/block/block.h"
37 #include "block/blockjob.h"
38 #include "block/throttle-groups.h"
39 #include "monitor/monitor.h"
40 #include "qemu/error-report.h"
41 #include "qemu/option.h"
42 #include "qemu/config-file.h"
43 #include "qapi/qmp/types.h"
44 #include "qapi-visit.h"
45 #include "qapi/qmp/qerror.h"
46 #include "qapi/qmp-output-visitor.h"
47 #include "qapi/util.h"
48 #include "sysemu/sysemu.h"
49 #include "block/block_int.h"
50 #include "qmp-commands.h"
51 #include "trace.h"
52 #include "sysemu/arch_init.h"
53 #include "qemu/cutils.h"
54 #include "qemu/help_option.h"
55
56 static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
57 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
58
59 static const char *const if_name[IF_COUNT] = {
60 [IF_NONE] = "none",
61 [IF_IDE] = "ide",
62 [IF_SCSI] = "scsi",
63 [IF_FLOPPY] = "floppy",
64 [IF_PFLASH] = "pflash",
65 [IF_MTD] = "mtd",
66 [IF_SD] = "sd",
67 [IF_VIRTIO] = "virtio",
68 [IF_XEN] = "xen",
69 };
70
71 static int if_max_devs[IF_COUNT] = {
72 /*
73 * Do not change these numbers! They govern how drive option
74 * index maps to unit and bus. That mapping is ABI.
75 *
76 * All controllers used to implement if=T drives need to support
77 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
78 * Otherwise, some index values map to "impossible" bus, unit
79 * values.
80 *
81 * For instance, if you change [IF_SCSI] to 255, -drive
82 * if=scsi,index=12 no longer means bus=1,unit=5, but
83 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
84 * the drive can't be set up. Regression.
85 */
86 [IF_IDE] = 2,
87 [IF_SCSI] = 7,
88 };
89
90 /**
91 * Boards may call this to offer board-by-board overrides
92 * of the default, global values.
93 */
94 void override_max_devs(BlockInterfaceType type, int max_devs)
95 {
96 BlockBackend *blk;
97 DriveInfo *dinfo;
98
99 if (max_devs <= 0) {
100 return;
101 }
102
103 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
104 dinfo = blk_legacy_dinfo(blk);
105 if (dinfo->type == type) {
106 fprintf(stderr, "Cannot override units-per-bus property of"
107 " the %s interface, because a drive of that type has"
108 " already been added.\n", if_name[type]);
109 g_assert_not_reached();
110 }
111 }
112
113 if_max_devs[type] = max_devs;
114 }
115
116 /*
117 * We automatically delete the drive when a device using it gets
118 * unplugged. Questionable feature, but we can't just drop it.
119 * Device models call blockdev_mark_auto_del() to schedule the
120 * automatic deletion, and generic qdev code calls blockdev_auto_del()
121 * when deletion is actually safe.
122 */
123 void blockdev_mark_auto_del(BlockBackend *blk)
124 {
125 DriveInfo *dinfo = blk_legacy_dinfo(blk);
126 BlockDriverState *bs = blk_bs(blk);
127 AioContext *aio_context;
128
129 if (!dinfo) {
130 return;
131 }
132
133 if (bs) {
134 aio_context = bdrv_get_aio_context(bs);
135 aio_context_acquire(aio_context);
136
137 if (bs->job) {
138 block_job_cancel(bs->job);
139 }
140
141 aio_context_release(aio_context);
142 }
143
144 dinfo->auto_del = 1;
145 }
146
147 void blockdev_auto_del(BlockBackend *blk)
148 {
149 DriveInfo *dinfo = blk_legacy_dinfo(blk);
150
151 if (dinfo && dinfo->auto_del) {
152 monitor_remove_blk(blk);
153 blk_unref(blk);
154 }
155 }
156
157 /**
158 * Returns the current mapping of how many units per bus
159 * a particular interface can support.
160 *
161 * A positive integer indicates n units per bus.
162 * 0 implies the mapping has not been established.
163 * -1 indicates an invalid BlockInterfaceType was given.
164 */
165 int drive_get_max_devs(BlockInterfaceType type)
166 {
167 if (type >= IF_IDE && type < IF_COUNT) {
168 return if_max_devs[type];
169 }
170
171 return -1;
172 }
173
174 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
175 {
176 int max_devs = if_max_devs[type];
177 return max_devs ? index / max_devs : 0;
178 }
179
180 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
181 {
182 int max_devs = if_max_devs[type];
183 return max_devs ? index % max_devs : index;
184 }
185
186 QemuOpts *drive_def(const char *optstr)
187 {
188 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
189 }
190
191 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
192 const char *optstr)
193 {
194 QemuOpts *opts;
195
196 opts = drive_def(optstr);
197 if (!opts) {
198 return NULL;
199 }
200 if (type != IF_DEFAULT) {
201 qemu_opt_set(opts, "if", if_name[type], &error_abort);
202 }
203 if (index >= 0) {
204 qemu_opt_set_number(opts, "index", index, &error_abort);
205 }
206 if (file)
207 qemu_opt_set(opts, "file", file, &error_abort);
208 return opts;
209 }
210
211 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
212 {
213 BlockBackend *blk;
214 DriveInfo *dinfo;
215
216 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
217 dinfo = blk_legacy_dinfo(blk);
218 if (dinfo && dinfo->type == type
219 && dinfo->bus == bus && dinfo->unit == unit) {
220 return dinfo;
221 }
222 }
223
224 return NULL;
225 }
226
227 bool drive_check_orphaned(void)
228 {
229 BlockBackend *blk;
230 DriveInfo *dinfo;
231 bool rs = false;
232
233 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
234 dinfo = blk_legacy_dinfo(blk);
235 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
236 /* Unless this is a default drive, this may be an oversight. */
237 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
238 dinfo->type != IF_NONE) {
239 fprintf(stderr, "Warning: Orphaned drive without device: "
240 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
241 blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
242 if_name[dinfo->type], dinfo->bus, dinfo->unit);
243 rs = true;
244 }
245 }
246
247 return rs;
248 }
249
250 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
251 {
252 return drive_get(type,
253 drive_index_to_bus_id(type, index),
254 drive_index_to_unit_id(type, index));
255 }
256
257 int drive_get_max_bus(BlockInterfaceType type)
258 {
259 int max_bus;
260 BlockBackend *blk;
261 DriveInfo *dinfo;
262
263 max_bus = -1;
264 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
265 dinfo = blk_legacy_dinfo(blk);
266 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
267 max_bus = dinfo->bus;
268 }
269 }
270 return max_bus;
271 }
272
273 /* Get a block device. This should only be used for single-drive devices
274 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
275 appropriate bus. */
276 DriveInfo *drive_get_next(BlockInterfaceType type)
277 {
278 static int next_block_unit[IF_COUNT];
279
280 return drive_get(type, 0, next_block_unit[type]++);
281 }
282
283 static void bdrv_format_print(void *opaque, const char *name)
284 {
285 error_printf(" %s", name);
286 }
287
288 typedef struct {
289 QEMUBH *bh;
290 BlockDriverState *bs;
291 } BDRVPutRefBH;
292
293 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
294 {
295 if (!strcmp(buf, "ignore")) {
296 return BLOCKDEV_ON_ERROR_IGNORE;
297 } else if (!is_read && !strcmp(buf, "enospc")) {
298 return BLOCKDEV_ON_ERROR_ENOSPC;
299 } else if (!strcmp(buf, "stop")) {
300 return BLOCKDEV_ON_ERROR_STOP;
301 } else if (!strcmp(buf, "report")) {
302 return BLOCKDEV_ON_ERROR_REPORT;
303 } else {
304 error_setg(errp, "'%s' invalid %s error action",
305 buf, is_read ? "read" : "write");
306 return -1;
307 }
308 }
309
310 static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
311 Error **errp)
312 {
313 const QListEntry *entry;
314 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
315 switch (qobject_type(entry->value)) {
316
317 case QTYPE_QSTRING: {
318 unsigned long long length;
319 const char *str = qstring_get_str(qobject_to_qstring(entry->value));
320 if (parse_uint_full(str, &length, 10) == 0 &&
321 length > 0 && length <= UINT_MAX) {
322 block_acct_add_interval(stats, (unsigned) length);
323 } else {
324 error_setg(errp, "Invalid interval length: %s", str);
325 return false;
326 }
327 break;
328 }
329
330 case QTYPE_QINT: {
331 int64_t length = qint_get_int(qobject_to_qint(entry->value));
332 if (length > 0 && length <= UINT_MAX) {
333 block_acct_add_interval(stats, (unsigned) length);
334 } else {
335 error_setg(errp, "Invalid interval length: %" PRId64, length);
336 return false;
337 }
338 break;
339 }
340
341 default:
342 error_setg(errp, "The specification of stats-intervals is invalid");
343 return false;
344 }
345 }
346 return true;
347 }
348
349 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
350
351 /* All parameters but @opts are optional and may be set to NULL. */
352 static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
353 const char **throttling_group, ThrottleConfig *throttle_cfg,
354 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
355 {
356 const char *discard;
357 Error *local_error = NULL;
358 const char *aio;
359
360 if (bdrv_flags) {
361 if (!qemu_opt_get_bool(opts, "read-only", false)) {
362 *bdrv_flags |= BDRV_O_RDWR;
363 }
364 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
365 *bdrv_flags |= BDRV_O_COPY_ON_READ;
366 }
367
368 if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
369 if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
370 error_setg(errp, "Invalid discard option");
371 return;
372 }
373 }
374
375 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
376 if (!strcmp(aio, "native")) {
377 *bdrv_flags |= BDRV_O_NATIVE_AIO;
378 } else if (!strcmp(aio, "threads")) {
379 /* this is the default */
380 } else {
381 error_setg(errp, "invalid aio option");
382 return;
383 }
384 }
385 }
386
387 /* disk I/O throttling */
388 if (throttling_group) {
389 *throttling_group = qemu_opt_get(opts, "throttling.group");
390 }
391
392 if (throttle_cfg) {
393 throttle_config_init(throttle_cfg);
394 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
395 qemu_opt_get_number(opts, "throttling.bps-total", 0);
396 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
397 qemu_opt_get_number(opts, "throttling.bps-read", 0);
398 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
399 qemu_opt_get_number(opts, "throttling.bps-write", 0);
400 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
401 qemu_opt_get_number(opts, "throttling.iops-total", 0);
402 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
403 qemu_opt_get_number(opts, "throttling.iops-read", 0);
404 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
405 qemu_opt_get_number(opts, "throttling.iops-write", 0);
406
407 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
408 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
409 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
410 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
411 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
412 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
413 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
414 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
415 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
416 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
417 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
418 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
419
420 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
421 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
422 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
423 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
424 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
425 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
426 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
427 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
428 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
429 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
430 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
431 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
432
433 throttle_cfg->op_size =
434 qemu_opt_get_number(opts, "throttling.iops-size", 0);
435
436 if (!throttle_is_valid(throttle_cfg, errp)) {
437 return;
438 }
439 }
440
441 if (detect_zeroes) {
442 *detect_zeroes =
443 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
444 qemu_opt_get(opts, "detect-zeroes"),
445 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
446 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
447 &local_error);
448 if (local_error) {
449 error_propagate(errp, local_error);
450 return;
451 }
452
453 if (bdrv_flags &&
454 *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
455 !(*bdrv_flags & BDRV_O_UNMAP))
456 {
457 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
458 "without setting discard operation to unmap");
459 return;
460 }
461 }
462 }
463
464 /* Takes the ownership of bs_opts */
465 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
466 Error **errp)
467 {
468 const char *buf;
469 int bdrv_flags = 0;
470 int on_read_error, on_write_error;
471 bool account_invalid, account_failed;
472 bool writethrough;
473 BlockBackend *blk;
474 BlockDriverState *bs;
475 ThrottleConfig cfg;
476 int snapshot = 0;
477 Error *error = NULL;
478 QemuOpts *opts;
479 QDict *interval_dict = NULL;
480 QList *interval_list = NULL;
481 const char *id;
482 BlockdevDetectZeroesOptions detect_zeroes =
483 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
484 const char *throttling_group = NULL;
485
486 /* Check common options by copying from bs_opts to opts, all other options
487 * stay in bs_opts for processing by bdrv_open(). */
488 id = qdict_get_try_str(bs_opts, "id");
489 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
490 if (error) {
491 error_propagate(errp, error);
492 goto err_no_opts;
493 }
494
495 qemu_opts_absorb_qdict(opts, bs_opts, &error);
496 if (error) {
497 error_propagate(errp, error);
498 goto early_err;
499 }
500
501 if (id) {
502 qdict_del(bs_opts, "id");
503 }
504
505 /* extract parameters */
506 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
507
508 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
509 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
510
511 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
512
513 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
514 qdict_array_split(interval_dict, &interval_list);
515
516 if (qdict_size(interval_dict) != 0) {
517 error_setg(errp, "Invalid option stats-intervals.%s",
518 qdict_first(interval_dict)->key);
519 goto early_err;
520 }
521
522 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
523 &detect_zeroes, &error);
524 if (error) {
525 error_propagate(errp, error);
526 goto early_err;
527 }
528
529 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
530 if (is_help_option(buf)) {
531 error_printf("Supported formats:");
532 bdrv_iterate_format(bdrv_format_print, NULL);
533 error_printf("\n");
534 goto early_err;
535 }
536
537 if (qdict_haskey(bs_opts, "driver")) {
538 error_setg(errp, "Cannot specify both 'driver' and 'format'");
539 goto early_err;
540 }
541 qdict_put(bs_opts, "driver", qstring_from_str(buf));
542 }
543
544 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
545 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
546 on_write_error = parse_block_error_action(buf, 0, &error);
547 if (error) {
548 error_propagate(errp, error);
549 goto early_err;
550 }
551 }
552
553 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
554 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
555 on_read_error = parse_block_error_action(buf, 1, &error);
556 if (error) {
557 error_propagate(errp, error);
558 goto early_err;
559 }
560 }
561
562 if (snapshot) {
563 bdrv_flags |= BDRV_O_SNAPSHOT;
564 }
565
566 /* init */
567 if ((!file || !*file) && !qdict_size(bs_opts)) {
568 BlockBackendRootState *blk_rs;
569
570 blk = blk_new(errp);
571 if (!blk) {
572 goto early_err;
573 }
574
575 blk_rs = blk_get_root_state(blk);
576 blk_rs->open_flags = bdrv_flags;
577 blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR);
578 blk_rs->detect_zeroes = detect_zeroes;
579
580 QDECREF(bs_opts);
581 } else {
582 if (file && !*file) {
583 file = NULL;
584 }
585
586 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
587 * with other callers) rather than what we want as the real defaults.
588 * Apply the defaults here instead. */
589 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
590 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
591 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
592
593 if (runstate_check(RUN_STATE_INMIGRATE)) {
594 bdrv_flags |= BDRV_O_INACTIVE;
595 }
596
597 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
598 if (!blk) {
599 goto err_no_bs_opts;
600 }
601 bs = blk_bs(blk);
602
603 bs->detect_zeroes = detect_zeroes;
604
605 if (bdrv_key_required(bs)) {
606 autostart = 0;
607 }
608
609 block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
610
611 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
612 blk_unref(blk);
613 blk = NULL;
614 goto err_no_bs_opts;
615 }
616 }
617
618 /* disk I/O throttling */
619 if (throttle_enabled(&cfg)) {
620 if (!throttling_group) {
621 throttling_group = blk_name(blk);
622 }
623 blk_io_limits_enable(blk, throttling_group);
624 blk_set_io_limits(blk, &cfg);
625 }
626
627 blk_set_enable_write_cache(blk, !writethrough);
628 blk_set_on_error(blk, on_read_error, on_write_error);
629
630 if (!monitor_add_blk(blk, qemu_opts_id(opts), errp)) {
631 blk_unref(blk);
632 blk = NULL;
633 goto err_no_bs_opts;
634 }
635
636 err_no_bs_opts:
637 qemu_opts_del(opts);
638 QDECREF(interval_dict);
639 QDECREF(interval_list);
640 return blk;
641
642 early_err:
643 qemu_opts_del(opts);
644 QDECREF(interval_dict);
645 QDECREF(interval_list);
646 err_no_opts:
647 QDECREF(bs_opts);
648 return NULL;
649 }
650
651 static QemuOptsList qemu_root_bds_opts;
652
653 /* Takes the ownership of bs_opts */
654 static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
655 {
656 BlockDriverState *bs;
657 QemuOpts *opts;
658 Error *local_error = NULL;
659 BlockdevDetectZeroesOptions detect_zeroes;
660 int ret;
661 int bdrv_flags = 0;
662
663 opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
664 if (!opts) {
665 goto fail;
666 }
667
668 qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
669 if (local_error) {
670 error_propagate(errp, local_error);
671 goto fail;
672 }
673
674 extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
675 &detect_zeroes, &local_error);
676 if (local_error) {
677 error_propagate(errp, local_error);
678 goto fail;
679 }
680
681 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
682 * with other callers) rather than what we want as the real defaults.
683 * Apply the defaults here instead. */
684 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
685 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
686
687 if (runstate_check(RUN_STATE_INMIGRATE)) {
688 bdrv_flags |= BDRV_O_INACTIVE;
689 }
690
691 bs = NULL;
692 ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp);
693 if (ret < 0) {
694 goto fail_no_bs_opts;
695 }
696
697 bs->detect_zeroes = detect_zeroes;
698
699 fail_no_bs_opts:
700 qemu_opts_del(opts);
701 return bs;
702
703 fail:
704 qemu_opts_del(opts);
705 QDECREF(bs_opts);
706 return NULL;
707 }
708
709 void blockdev_close_all_bdrv_states(void)
710 {
711 BlockDriverState *bs, *next_bs;
712
713 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
714 AioContext *ctx = bdrv_get_aio_context(bs);
715
716 aio_context_acquire(ctx);
717 bdrv_unref(bs);
718 aio_context_release(ctx);
719 }
720 }
721
722 /* Iterates over the list of monitor-owned BlockDriverStates */
723 BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
724 {
725 return bs ? QTAILQ_NEXT(bs, monitor_list)
726 : QTAILQ_FIRST(&monitor_bdrv_states);
727 }
728
729 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
730 Error **errp)
731 {
732 const char *value;
733
734 value = qemu_opt_get(opts, from);
735 if (value) {
736 if (qemu_opt_find(opts, to)) {
737 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
738 "same time", to, from);
739 return;
740 }
741 }
742
743 /* rename all items in opts */
744 while ((value = qemu_opt_get(opts, from))) {
745 qemu_opt_set(opts, to, value, &error_abort);
746 qemu_opt_unset(opts, from);
747 }
748 }
749
750 QemuOptsList qemu_legacy_drive_opts = {
751 .name = "drive",
752 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
753 .desc = {
754 {
755 .name = "bus",
756 .type = QEMU_OPT_NUMBER,
757 .help = "bus number",
758 },{
759 .name = "unit",
760 .type = QEMU_OPT_NUMBER,
761 .help = "unit number (i.e. lun for scsi)",
762 },{
763 .name = "index",
764 .type = QEMU_OPT_NUMBER,
765 .help = "index number",
766 },{
767 .name = "media",
768 .type = QEMU_OPT_STRING,
769 .help = "media type (disk, cdrom)",
770 },{
771 .name = "if",
772 .type = QEMU_OPT_STRING,
773 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
774 },{
775 .name = "cyls",
776 .type = QEMU_OPT_NUMBER,
777 .help = "number of cylinders (ide disk geometry)",
778 },{
779 .name = "heads",
780 .type = QEMU_OPT_NUMBER,
781 .help = "number of heads (ide disk geometry)",
782 },{
783 .name = "secs",
784 .type = QEMU_OPT_NUMBER,
785 .help = "number of sectors (ide disk geometry)",
786 },{
787 .name = "trans",
788 .type = QEMU_OPT_STRING,
789 .help = "chs translation (auto, lba, none)",
790 },{
791 .name = "boot",
792 .type = QEMU_OPT_BOOL,
793 .help = "(deprecated, ignored)",
794 },{
795 .name = "addr",
796 .type = QEMU_OPT_STRING,
797 .help = "pci address (virtio only)",
798 },{
799 .name = "serial",
800 .type = QEMU_OPT_STRING,
801 .help = "disk serial number",
802 },{
803 .name = "file",
804 .type = QEMU_OPT_STRING,
805 .help = "file name",
806 },
807
808 /* Options that are passed on, but have special semantics with -drive */
809 {
810 .name = "read-only",
811 .type = QEMU_OPT_BOOL,
812 .help = "open drive file as read-only",
813 },{
814 .name = "rerror",
815 .type = QEMU_OPT_STRING,
816 .help = "read error action",
817 },{
818 .name = "werror",
819 .type = QEMU_OPT_STRING,
820 .help = "write error action",
821 },{
822 .name = "copy-on-read",
823 .type = QEMU_OPT_BOOL,
824 .help = "copy read data from backing file into image file",
825 },
826
827 { /* end of list */ }
828 },
829 };
830
831 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
832 {
833 const char *value;
834 BlockBackend *blk;
835 DriveInfo *dinfo = NULL;
836 QDict *bs_opts;
837 QemuOpts *legacy_opts;
838 DriveMediaType media = MEDIA_DISK;
839 BlockInterfaceType type;
840 int cyls, heads, secs, translation;
841 int max_devs, bus_id, unit_id, index;
842 const char *devaddr;
843 const char *werror, *rerror;
844 bool read_only = false;
845 bool copy_on_read;
846 const char *serial;
847 const char *filename;
848 Error *local_err = NULL;
849 int i;
850
851 /* Change legacy command line options into QMP ones */
852 static const struct {
853 const char *from;
854 const char *to;
855 } opt_renames[] = {
856 { "iops", "throttling.iops-total" },
857 { "iops_rd", "throttling.iops-read" },
858 { "iops_wr", "throttling.iops-write" },
859
860 { "bps", "throttling.bps-total" },
861 { "bps_rd", "throttling.bps-read" },
862 { "bps_wr", "throttling.bps-write" },
863
864 { "iops_max", "throttling.iops-total-max" },
865 { "iops_rd_max", "throttling.iops-read-max" },
866 { "iops_wr_max", "throttling.iops-write-max" },
867
868 { "bps_max", "throttling.bps-total-max" },
869 { "bps_rd_max", "throttling.bps-read-max" },
870 { "bps_wr_max", "throttling.bps-write-max" },
871
872 { "iops_size", "throttling.iops-size" },
873
874 { "group", "throttling.group" },
875
876 { "readonly", "read-only" },
877 };
878
879 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
880 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
881 &local_err);
882 if (local_err) {
883 error_report_err(local_err);
884 return NULL;
885 }
886 }
887
888 value = qemu_opt_get(all_opts, "cache");
889 if (value) {
890 int flags = 0;
891 bool writethrough;
892
893 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
894 error_report("invalid cache option");
895 return NULL;
896 }
897
898 /* Specific options take precedence */
899 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
900 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
901 !writethrough, &error_abort);
902 }
903 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
904 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
905 !!(flags & BDRV_O_NOCACHE), &error_abort);
906 }
907 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
908 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
909 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
910 }
911 qemu_opt_unset(all_opts, "cache");
912 }
913
914 /* Get a QDict for processing the options */
915 bs_opts = qdict_new();
916 qemu_opts_to_qdict(all_opts, bs_opts);
917
918 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
919 &error_abort);
920 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
921 if (local_err) {
922 error_report_err(local_err);
923 goto fail;
924 }
925
926 /* Deprecated option boot=[on|off] */
927 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
928 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
929 "ignored. Future versions will reject this parameter. Please "
930 "update your scripts.\n");
931 }
932
933 /* Media type */
934 value = qemu_opt_get(legacy_opts, "media");
935 if (value) {
936 if (!strcmp(value, "disk")) {
937 media = MEDIA_DISK;
938 } else if (!strcmp(value, "cdrom")) {
939 media = MEDIA_CDROM;
940 read_only = true;
941 } else {
942 error_report("'%s' invalid media", value);
943 goto fail;
944 }
945 }
946
947 /* copy-on-read is disabled with a warning for read-only devices */
948 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
949 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
950
951 if (read_only && copy_on_read) {
952 error_report("warning: disabling copy-on-read on read-only drive");
953 copy_on_read = false;
954 }
955
956 qdict_put(bs_opts, "read-only",
957 qstring_from_str(read_only ? "on" : "off"));
958 qdict_put(bs_opts, "copy-on-read",
959 qstring_from_str(copy_on_read ? "on" :"off"));
960
961 /* Controller type */
962 value = qemu_opt_get(legacy_opts, "if");
963 if (value) {
964 for (type = 0;
965 type < IF_COUNT && strcmp(value, if_name[type]);
966 type++) {
967 }
968 if (type == IF_COUNT) {
969 error_report("unsupported bus type '%s'", value);
970 goto fail;
971 }
972 } else {
973 type = block_default_type;
974 }
975
976 /* Geometry */
977 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
978 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
979 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
980
981 if (cyls || heads || secs) {
982 if (cyls < 1) {
983 error_report("invalid physical cyls number");
984 goto fail;
985 }
986 if (heads < 1) {
987 error_report("invalid physical heads number");
988 goto fail;
989 }
990 if (secs < 1) {
991 error_report("invalid physical secs number");
992 goto fail;
993 }
994 }
995
996 translation = BIOS_ATA_TRANSLATION_AUTO;
997 value = qemu_opt_get(legacy_opts, "trans");
998 if (value != NULL) {
999 if (!cyls) {
1000 error_report("'%s' trans must be used with cyls, heads and secs",
1001 value);
1002 goto fail;
1003 }
1004 if (!strcmp(value, "none")) {
1005 translation = BIOS_ATA_TRANSLATION_NONE;
1006 } else if (!strcmp(value, "lba")) {
1007 translation = BIOS_ATA_TRANSLATION_LBA;
1008 } else if (!strcmp(value, "large")) {
1009 translation = BIOS_ATA_TRANSLATION_LARGE;
1010 } else if (!strcmp(value, "rechs")) {
1011 translation = BIOS_ATA_TRANSLATION_RECHS;
1012 } else if (!strcmp(value, "auto")) {
1013 translation = BIOS_ATA_TRANSLATION_AUTO;
1014 } else {
1015 error_report("'%s' invalid translation type", value);
1016 goto fail;
1017 }
1018 }
1019
1020 if (media == MEDIA_CDROM) {
1021 if (cyls || secs || heads) {
1022 error_report("CHS can't be set with media=cdrom");
1023 goto fail;
1024 }
1025 }
1026
1027 /* Device address specified by bus/unit or index.
1028 * If none was specified, try to find the first free one. */
1029 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
1030 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
1031 index = qemu_opt_get_number(legacy_opts, "index", -1);
1032
1033 max_devs = if_max_devs[type];
1034
1035 if (index != -1) {
1036 if (bus_id != 0 || unit_id != -1) {
1037 error_report("index cannot be used with bus and unit");
1038 goto fail;
1039 }
1040 bus_id = drive_index_to_bus_id(type, index);
1041 unit_id = drive_index_to_unit_id(type, index);
1042 }
1043
1044 if (unit_id == -1) {
1045 unit_id = 0;
1046 while (drive_get(type, bus_id, unit_id) != NULL) {
1047 unit_id++;
1048 if (max_devs && unit_id >= max_devs) {
1049 unit_id -= max_devs;
1050 bus_id++;
1051 }
1052 }
1053 }
1054
1055 if (max_devs && unit_id >= max_devs) {
1056 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1057 goto fail;
1058 }
1059
1060 if (drive_get(type, bus_id, unit_id) != NULL) {
1061 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1062 bus_id, unit_id, index);
1063 goto fail;
1064 }
1065
1066 /* Serial number */
1067 serial = qemu_opt_get(legacy_opts, "serial");
1068
1069 /* no id supplied -> create one */
1070 if (qemu_opts_id(all_opts) == NULL) {
1071 char *new_id;
1072 const char *mediastr = "";
1073 if (type == IF_IDE || type == IF_SCSI) {
1074 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1075 }
1076 if (max_devs) {
1077 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1078 mediastr, unit_id);
1079 } else {
1080 new_id = g_strdup_printf("%s%s%i", if_name[type],
1081 mediastr, unit_id);
1082 }
1083 qdict_put(bs_opts, "id", qstring_from_str(new_id));
1084 g_free(new_id);
1085 }
1086
1087 /* Add virtio block device */
1088 devaddr = qemu_opt_get(legacy_opts, "addr");
1089 if (devaddr && type != IF_VIRTIO) {
1090 error_report("addr is not supported by this bus type");
1091 goto fail;
1092 }
1093
1094 if (type == IF_VIRTIO) {
1095 QemuOpts *devopts;
1096 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1097 &error_abort);
1098 if (arch_type == QEMU_ARCH_S390X) {
1099 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
1100 } else {
1101 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
1102 }
1103 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1104 &error_abort);
1105 if (devaddr) {
1106 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
1107 }
1108 }
1109
1110 filename = qemu_opt_get(legacy_opts, "file");
1111
1112 /* Check werror/rerror compatibility with if=... */
1113 werror = qemu_opt_get(legacy_opts, "werror");
1114 if (werror != NULL) {
1115 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1116 type != IF_NONE) {
1117 error_report("werror is not supported by this bus type");
1118 goto fail;
1119 }
1120 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1121 }
1122
1123 rerror = qemu_opt_get(legacy_opts, "rerror");
1124 if (rerror != NULL) {
1125 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1126 type != IF_NONE) {
1127 error_report("rerror is not supported by this bus type");
1128 goto fail;
1129 }
1130 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1131 }
1132
1133 /* Actual block device init: Functionality shared with blockdev-add */
1134 blk = blockdev_init(filename, bs_opts, &local_err);
1135 bs_opts = NULL;
1136 if (!blk) {
1137 if (local_err) {
1138 error_report_err(local_err);
1139 }
1140 goto fail;
1141 } else {
1142 assert(!local_err);
1143 }
1144
1145 /* Create legacy DriveInfo */
1146 dinfo = g_malloc0(sizeof(*dinfo));
1147 dinfo->opts = all_opts;
1148
1149 dinfo->cyls = cyls;
1150 dinfo->heads = heads;
1151 dinfo->secs = secs;
1152 dinfo->trans = translation;
1153
1154 dinfo->type = type;
1155 dinfo->bus = bus_id;
1156 dinfo->unit = unit_id;
1157 dinfo->devaddr = devaddr;
1158 dinfo->serial = g_strdup(serial);
1159
1160 blk_set_legacy_dinfo(blk, dinfo);
1161
1162 switch(type) {
1163 case IF_IDE:
1164 case IF_SCSI:
1165 case IF_XEN:
1166 case IF_NONE:
1167 dinfo->media_cd = media == MEDIA_CDROM;
1168 break;
1169 default:
1170 break;
1171 }
1172
1173 fail:
1174 qemu_opts_del(legacy_opts);
1175 QDECREF(bs_opts);
1176 return dinfo;
1177 }
1178
1179 void hmp_commit(Monitor *mon, const QDict *qdict)
1180 {
1181 const char *device = qdict_get_str(qdict, "device");
1182 BlockBackend *blk;
1183 int ret;
1184
1185 if (!strcmp(device, "all")) {
1186 ret = blk_commit_all();
1187 } else {
1188 BlockDriverState *bs;
1189 AioContext *aio_context;
1190
1191 blk = blk_by_name(device);
1192 if (!blk) {
1193 monitor_printf(mon, "Device '%s' not found\n", device);
1194 return;
1195 }
1196 if (!blk_is_available(blk)) {
1197 monitor_printf(mon, "Device '%s' has no medium\n", device);
1198 return;
1199 }
1200
1201 bs = blk_bs(blk);
1202 aio_context = bdrv_get_aio_context(bs);
1203 aio_context_acquire(aio_context);
1204
1205 ret = bdrv_commit(bs);
1206
1207 aio_context_release(aio_context);
1208 }
1209 if (ret < 0) {
1210 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1211 strerror(-ret));
1212 }
1213 }
1214
1215 static void blockdev_do_action(TransactionAction *action, Error **errp)
1216 {
1217 TransactionActionList list;
1218
1219 list.value = action;
1220 list.next = NULL;
1221 qmp_transaction(&list, false, NULL, errp);
1222 }
1223
1224 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1225 bool has_node_name, const char *node_name,
1226 const char *snapshot_file,
1227 bool has_snapshot_node_name,
1228 const char *snapshot_node_name,
1229 bool has_format, const char *format,
1230 bool has_mode, NewImageMode mode, Error **errp)
1231 {
1232 BlockdevSnapshotSync snapshot = {
1233 .has_device = has_device,
1234 .device = (char *) device,
1235 .has_node_name = has_node_name,
1236 .node_name = (char *) node_name,
1237 .snapshot_file = (char *) snapshot_file,
1238 .has_snapshot_node_name = has_snapshot_node_name,
1239 .snapshot_node_name = (char *) snapshot_node_name,
1240 .has_format = has_format,
1241 .format = (char *) format,
1242 .has_mode = has_mode,
1243 .mode = mode,
1244 };
1245 TransactionAction action = {
1246 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1247 .u.blockdev_snapshot_sync.data = &snapshot,
1248 };
1249 blockdev_do_action(&action, errp);
1250 }
1251
1252 void qmp_blockdev_snapshot(const char *node, const char *overlay,
1253 Error **errp)
1254 {
1255 BlockdevSnapshot snapshot_data = {
1256 .node = (char *) node,
1257 .overlay = (char *) overlay
1258 };
1259 TransactionAction action = {
1260 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
1261 .u.blockdev_snapshot.data = &snapshot_data,
1262 };
1263 blockdev_do_action(&action, errp);
1264 }
1265
1266 void qmp_blockdev_snapshot_internal_sync(const char *device,
1267 const char *name,
1268 Error **errp)
1269 {
1270 BlockdevSnapshotInternal snapshot = {
1271 .device = (char *) device,
1272 .name = (char *) name
1273 };
1274 TransactionAction action = {
1275 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1276 .u.blockdev_snapshot_internal_sync.data = &snapshot,
1277 };
1278 blockdev_do_action(&action, errp);
1279 }
1280
1281 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1282 bool has_id,
1283 const char *id,
1284 bool has_name,
1285 const char *name,
1286 Error **errp)
1287 {
1288 BlockDriverState *bs;
1289 BlockBackend *blk;
1290 AioContext *aio_context;
1291 QEMUSnapshotInfo sn;
1292 Error *local_err = NULL;
1293 SnapshotInfo *info = NULL;
1294 int ret;
1295
1296 blk = blk_by_name(device);
1297 if (!blk) {
1298 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1299 "Device '%s' not found", device);
1300 return NULL;
1301 }
1302
1303 aio_context = blk_get_aio_context(blk);
1304 aio_context_acquire(aio_context);
1305
1306 if (!has_id) {
1307 id = NULL;
1308 }
1309
1310 if (!has_name) {
1311 name = NULL;
1312 }
1313
1314 if (!id && !name) {
1315 error_setg(errp, "Name or id must be provided");
1316 goto out_aio_context;
1317 }
1318
1319 if (!blk_is_available(blk)) {
1320 error_setg(errp, "Device '%s' has no medium", device);
1321 goto out_aio_context;
1322 }
1323 bs = blk_bs(blk);
1324
1325 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1326 goto out_aio_context;
1327 }
1328
1329 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1330 if (local_err) {
1331 error_propagate(errp, local_err);
1332 goto out_aio_context;
1333 }
1334 if (!ret) {
1335 error_setg(errp,
1336 "Snapshot with id '%s' and name '%s' does not exist on "
1337 "device '%s'",
1338 STR_OR_NULL(id), STR_OR_NULL(name), device);
1339 goto out_aio_context;
1340 }
1341
1342 bdrv_snapshot_delete(bs, id, name, &local_err);
1343 if (local_err) {
1344 error_propagate(errp, local_err);
1345 goto out_aio_context;
1346 }
1347
1348 aio_context_release(aio_context);
1349
1350 info = g_new0(SnapshotInfo, 1);
1351 info->id = g_strdup(sn.id_str);
1352 info->name = g_strdup(sn.name);
1353 info->date_nsec = sn.date_nsec;
1354 info->date_sec = sn.date_sec;
1355 info->vm_state_size = sn.vm_state_size;
1356 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1357 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1358
1359 return info;
1360
1361 out_aio_context:
1362 aio_context_release(aio_context);
1363 return NULL;
1364 }
1365
1366 /**
1367 * block_dirty_bitmap_lookup:
1368 * Return a dirty bitmap (if present), after validating
1369 * the node reference and bitmap names.
1370 *
1371 * @node: The name of the BDS node to search for bitmaps
1372 * @name: The name of the bitmap to search for
1373 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1374 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1375 * @errp: Output pointer for error information. Can be NULL.
1376 *
1377 * @return: A bitmap object on success, or NULL on failure.
1378 */
1379 static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1380 const char *name,
1381 BlockDriverState **pbs,
1382 AioContext **paio,
1383 Error **errp)
1384 {
1385 BlockDriverState *bs;
1386 BdrvDirtyBitmap *bitmap;
1387 AioContext *aio_context;
1388
1389 if (!node) {
1390 error_setg(errp, "Node cannot be NULL");
1391 return NULL;
1392 }
1393 if (!name) {
1394 error_setg(errp, "Bitmap name cannot be NULL");
1395 return NULL;
1396 }
1397 bs = bdrv_lookup_bs(node, node, NULL);
1398 if (!bs) {
1399 error_setg(errp, "Node '%s' not found", node);
1400 return NULL;
1401 }
1402
1403 aio_context = bdrv_get_aio_context(bs);
1404 aio_context_acquire(aio_context);
1405
1406 bitmap = bdrv_find_dirty_bitmap(bs, name);
1407 if (!bitmap) {
1408 error_setg(errp, "Dirty bitmap '%s' not found", name);
1409 goto fail;
1410 }
1411
1412 if (pbs) {
1413 *pbs = bs;
1414 }
1415 if (paio) {
1416 *paio = aio_context;
1417 } else {
1418 aio_context_release(aio_context);
1419 }
1420
1421 return bitmap;
1422
1423 fail:
1424 aio_context_release(aio_context);
1425 return NULL;
1426 }
1427
1428 /* New and old BlockDriverState structs for atomic group operations */
1429
1430 typedef struct BlkActionState BlkActionState;
1431
1432 /**
1433 * BlkActionOps:
1434 * Table of operations that define an Action.
1435 *
1436 * @instance_size: Size of state struct, in bytes.
1437 * @prepare: Prepare the work, must NOT be NULL.
1438 * @commit: Commit the changes, can be NULL.
1439 * @abort: Abort the changes on fail, can be NULL.
1440 * @clean: Clean up resources after all transaction actions have called
1441 * commit() or abort(). Can be NULL.
1442 *
1443 * Only prepare() may fail. In a single transaction, only one of commit() or
1444 * abort() will be called. clean() will always be called if it is present.
1445 */
1446 typedef struct BlkActionOps {
1447 size_t instance_size;
1448 void (*prepare)(BlkActionState *common, Error **errp);
1449 void (*commit)(BlkActionState *common);
1450 void (*abort)(BlkActionState *common);
1451 void (*clean)(BlkActionState *common);
1452 } BlkActionOps;
1453
1454 /**
1455 * BlkActionState:
1456 * Describes one Action's state within a Transaction.
1457 *
1458 * @action: QAPI-defined enum identifying which Action to perform.
1459 * @ops: Table of ActionOps this Action can perform.
1460 * @block_job_txn: Transaction which this action belongs to.
1461 * @entry: List membership for all Actions in this Transaction.
1462 *
1463 * This structure must be arranged as first member in a subclassed type,
1464 * assuming that the compiler will also arrange it to the same offsets as the
1465 * base class.
1466 */
1467 struct BlkActionState {
1468 TransactionAction *action;
1469 const BlkActionOps *ops;
1470 BlockJobTxn *block_job_txn;
1471 TransactionProperties *txn_props;
1472 QSIMPLEQ_ENTRY(BlkActionState) entry;
1473 };
1474
1475 /* internal snapshot private data */
1476 typedef struct InternalSnapshotState {
1477 BlkActionState common;
1478 BlockDriverState *bs;
1479 AioContext *aio_context;
1480 QEMUSnapshotInfo sn;
1481 bool created;
1482 } InternalSnapshotState;
1483
1484
1485 static int action_check_completion_mode(BlkActionState *s, Error **errp)
1486 {
1487 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1488 error_setg(errp,
1489 "Action '%s' does not support Transaction property "
1490 "completion-mode = %s",
1491 TransactionActionKind_lookup[s->action->type],
1492 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1493 return -1;
1494 }
1495 return 0;
1496 }
1497
1498 static void internal_snapshot_prepare(BlkActionState *common,
1499 Error **errp)
1500 {
1501 Error *local_err = NULL;
1502 const char *device;
1503 const char *name;
1504 BlockBackend *blk;
1505 BlockDriverState *bs;
1506 QEMUSnapshotInfo old_sn, *sn;
1507 bool ret;
1508 qemu_timeval tv;
1509 BlockdevSnapshotInternal *internal;
1510 InternalSnapshotState *state;
1511 int ret1;
1512
1513 g_assert(common->action->type ==
1514 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1515 internal = common->action->u.blockdev_snapshot_internal_sync.data;
1516 state = DO_UPCAST(InternalSnapshotState, common, common);
1517
1518 /* 1. parse input */
1519 device = internal->device;
1520 name = internal->name;
1521
1522 /* 2. check for validation */
1523 if (action_check_completion_mode(common, errp) < 0) {
1524 return;
1525 }
1526
1527 blk = blk_by_name(device);
1528 if (!blk) {
1529 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1530 "Device '%s' not found", device);
1531 return;
1532 }
1533
1534 /* AioContext is released in .clean() */
1535 state->aio_context = blk_get_aio_context(blk);
1536 aio_context_acquire(state->aio_context);
1537
1538 if (!blk_is_available(blk)) {
1539 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1540 return;
1541 }
1542 bs = blk_bs(blk);
1543
1544 state->bs = bs;
1545 bdrv_drained_begin(bs);
1546
1547 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1548 return;
1549 }
1550
1551 if (bdrv_is_read_only(bs)) {
1552 error_setg(errp, "Device '%s' is read only", device);
1553 return;
1554 }
1555
1556 if (!bdrv_can_snapshot(bs)) {
1557 error_setg(errp, "Block format '%s' used by device '%s' "
1558 "does not support internal snapshots",
1559 bs->drv->format_name, device);
1560 return;
1561 }
1562
1563 if (!strlen(name)) {
1564 error_setg(errp, "Name is empty");
1565 return;
1566 }
1567
1568 /* check whether a snapshot with name exist */
1569 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1570 &local_err);
1571 if (local_err) {
1572 error_propagate(errp, local_err);
1573 return;
1574 } else if (ret) {
1575 error_setg(errp,
1576 "Snapshot with name '%s' already exists on device '%s'",
1577 name, device);
1578 return;
1579 }
1580
1581 /* 3. take the snapshot */
1582 sn = &state->sn;
1583 pstrcpy(sn->name, sizeof(sn->name), name);
1584 qemu_gettimeofday(&tv);
1585 sn->date_sec = tv.tv_sec;
1586 sn->date_nsec = tv.tv_usec * 1000;
1587 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1588
1589 ret1 = bdrv_snapshot_create(bs, sn);
1590 if (ret1 < 0) {
1591 error_setg_errno(errp, -ret1,
1592 "Failed to create snapshot '%s' on device '%s'",
1593 name, device);
1594 return;
1595 }
1596
1597 /* 4. succeed, mark a snapshot is created */
1598 state->created = true;
1599 }
1600
1601 static void internal_snapshot_abort(BlkActionState *common)
1602 {
1603 InternalSnapshotState *state =
1604 DO_UPCAST(InternalSnapshotState, common, common);
1605 BlockDriverState *bs = state->bs;
1606 QEMUSnapshotInfo *sn = &state->sn;
1607 Error *local_error = NULL;
1608
1609 if (!state->created) {
1610 return;
1611 }
1612
1613 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1614 error_reportf_err(local_error,
1615 "Failed to delete snapshot with id '%s' and "
1616 "name '%s' on device '%s' in abort: ",
1617 sn->id_str, sn->name,
1618 bdrv_get_device_name(bs));
1619 }
1620 }
1621
1622 static void internal_snapshot_clean(BlkActionState *common)
1623 {
1624 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1625 common, common);
1626
1627 if (state->aio_context) {
1628 if (state->bs) {
1629 bdrv_drained_end(state->bs);
1630 }
1631 aio_context_release(state->aio_context);
1632 }
1633 }
1634
1635 /* external snapshot private data */
1636 typedef struct ExternalSnapshotState {
1637 BlkActionState common;
1638 BlockDriverState *old_bs;
1639 BlockDriverState *new_bs;
1640 AioContext *aio_context;
1641 } ExternalSnapshotState;
1642
1643 static void external_snapshot_prepare(BlkActionState *common,
1644 Error **errp)
1645 {
1646 int flags = 0, ret;
1647 QDict *options = NULL;
1648 Error *local_err = NULL;
1649 /* Device and node name of the image to generate the snapshot from */
1650 const char *device;
1651 const char *node_name;
1652 /* Reference to the new image (for 'blockdev-snapshot') */
1653 const char *snapshot_ref;
1654 /* File name of the new image (for 'blockdev-snapshot-sync') */
1655 const char *new_image_file;
1656 ExternalSnapshotState *state =
1657 DO_UPCAST(ExternalSnapshotState, common, common);
1658 TransactionAction *action = common->action;
1659
1660 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1661 * purpose but a different set of parameters */
1662 switch (action->type) {
1663 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1664 {
1665 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
1666 device = s->node;
1667 node_name = s->node;
1668 new_image_file = NULL;
1669 snapshot_ref = s->overlay;
1670 }
1671 break;
1672 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1673 {
1674 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
1675 device = s->has_device ? s->device : NULL;
1676 node_name = s->has_node_name ? s->node_name : NULL;
1677 new_image_file = s->snapshot_file;
1678 snapshot_ref = NULL;
1679 }
1680 break;
1681 default:
1682 g_assert_not_reached();
1683 }
1684
1685 /* start processing */
1686 if (action_check_completion_mode(common, errp) < 0) {
1687 return;
1688 }
1689
1690 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1691 if (!state->old_bs) {
1692 return;
1693 }
1694
1695 /* Acquire AioContext now so any threads operating on old_bs stop */
1696 state->aio_context = bdrv_get_aio_context(state->old_bs);
1697 aio_context_acquire(state->aio_context);
1698 bdrv_drained_begin(state->old_bs);
1699
1700 if (!bdrv_is_inserted(state->old_bs)) {
1701 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1702 return;
1703 }
1704
1705 if (bdrv_op_is_blocked(state->old_bs,
1706 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1707 return;
1708 }
1709
1710 if (!bdrv_is_read_only(state->old_bs)) {
1711 if (bdrv_flush(state->old_bs)) {
1712 error_setg(errp, QERR_IO_ERROR);
1713 return;
1714 }
1715 }
1716
1717 if (!bdrv_is_first_non_filter(state->old_bs)) {
1718 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1719 return;
1720 }
1721
1722 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
1723 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
1724 const char *format = s->has_format ? s->format : "qcow2";
1725 enum NewImageMode mode;
1726 const char *snapshot_node_name =
1727 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
1728
1729 if (node_name && !snapshot_node_name) {
1730 error_setg(errp, "New snapshot node name missing");
1731 return;
1732 }
1733
1734 if (snapshot_node_name &&
1735 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1736 error_setg(errp, "New snapshot node name already in use");
1737 return;
1738 }
1739
1740 flags = state->old_bs->open_flags;
1741 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
1742
1743 /* create new image w/backing file */
1744 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1745 if (mode != NEW_IMAGE_MODE_EXISTING) {
1746 int64_t size = bdrv_getlength(state->old_bs);
1747 if (size < 0) {
1748 error_setg_errno(errp, -size, "bdrv_getlength failed");
1749 return;
1750 }
1751 bdrv_img_create(new_image_file, format,
1752 state->old_bs->filename,
1753 state->old_bs->drv->format_name,
1754 NULL, size, flags, &local_err, false);
1755 if (local_err) {
1756 error_propagate(errp, local_err);
1757 return;
1758 }
1759 }
1760
1761 options = qdict_new();
1762 if (s->has_snapshot_node_name) {
1763 qdict_put(options, "node-name",
1764 qstring_from_str(snapshot_node_name));
1765 }
1766 qdict_put(options, "driver", qstring_from_str(format));
1767
1768 flags |= BDRV_O_NO_BACKING;
1769 }
1770
1771 assert(state->new_bs == NULL);
1772 ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options,
1773 flags, errp);
1774 /* We will manually add the backing_hd field to the bs later */
1775 if (ret != 0) {
1776 return;
1777 }
1778
1779 if (state->new_bs->blk != NULL) {
1780 error_setg(errp, "The snapshot is already in use by %s",
1781 blk_name(state->new_bs->blk));
1782 return;
1783 }
1784
1785 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1786 errp)) {
1787 return;
1788 }
1789
1790 if (state->new_bs->backing != NULL) {
1791 error_setg(errp, "The snapshot already has a backing image");
1792 return;
1793 }
1794
1795 if (!state->new_bs->drv->supports_backing) {
1796 error_setg(errp, "The snapshot does not support backing images");
1797 }
1798 }
1799
1800 static void external_snapshot_commit(BlkActionState *common)
1801 {
1802 ExternalSnapshotState *state =
1803 DO_UPCAST(ExternalSnapshotState, common, common);
1804
1805 bdrv_set_aio_context(state->new_bs, state->aio_context);
1806
1807 /* This removes our old bs and adds the new bs */
1808 bdrv_append(state->new_bs, state->old_bs);
1809 /* We don't need (or want) to use the transactional
1810 * bdrv_reopen_multiple() across all the entries at once, because we
1811 * don't want to abort all of them if one of them fails the reopen */
1812 if (!state->old_bs->copy_on_read) {
1813 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1814 NULL);
1815 }
1816 }
1817
1818 static void external_snapshot_abort(BlkActionState *common)
1819 {
1820 ExternalSnapshotState *state =
1821 DO_UPCAST(ExternalSnapshotState, common, common);
1822 if (state->new_bs) {
1823 bdrv_unref(state->new_bs);
1824 }
1825 }
1826
1827 static void external_snapshot_clean(BlkActionState *common)
1828 {
1829 ExternalSnapshotState *state =
1830 DO_UPCAST(ExternalSnapshotState, common, common);
1831 if (state->aio_context) {
1832 bdrv_drained_end(state->old_bs);
1833 aio_context_release(state->aio_context);
1834 }
1835 }
1836
1837 typedef struct DriveBackupState {
1838 BlkActionState common;
1839 BlockDriverState *bs;
1840 AioContext *aio_context;
1841 BlockJob *job;
1842 } DriveBackupState;
1843
1844 static void do_drive_backup(const char *device, const char *target,
1845 bool has_format, const char *format,
1846 enum MirrorSyncMode sync,
1847 bool has_mode, enum NewImageMode mode,
1848 bool has_speed, int64_t speed,
1849 bool has_bitmap, const char *bitmap,
1850 bool has_on_source_error,
1851 BlockdevOnError on_source_error,
1852 bool has_on_target_error,
1853 BlockdevOnError on_target_error,
1854 BlockJobTxn *txn, Error **errp);
1855
1856 static void drive_backup_prepare(BlkActionState *common, Error **errp)
1857 {
1858 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1859 BlockBackend *blk;
1860 DriveBackup *backup;
1861 Error *local_err = NULL;
1862
1863 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1864 backup = common->action->u.drive_backup.data;
1865
1866 blk = blk_by_name(backup->device);
1867 if (!blk) {
1868 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1869 "Device '%s' not found", backup->device);
1870 return;
1871 }
1872
1873 if (!blk_is_available(blk)) {
1874 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1875 return;
1876 }
1877
1878 /* AioContext is released in .clean() */
1879 state->aio_context = blk_get_aio_context(blk);
1880 aio_context_acquire(state->aio_context);
1881 bdrv_drained_begin(blk_bs(blk));
1882 state->bs = blk_bs(blk);
1883
1884 do_drive_backup(backup->device, backup->target,
1885 backup->has_format, backup->format,
1886 backup->sync,
1887 backup->has_mode, backup->mode,
1888 backup->has_speed, backup->speed,
1889 backup->has_bitmap, backup->bitmap,
1890 backup->has_on_source_error, backup->on_source_error,
1891 backup->has_on_target_error, backup->on_target_error,
1892 common->block_job_txn, &local_err);
1893 if (local_err) {
1894 error_propagate(errp, local_err);
1895 return;
1896 }
1897
1898 state->job = state->bs->job;
1899 }
1900
1901 static void drive_backup_abort(BlkActionState *common)
1902 {
1903 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1904 BlockDriverState *bs = state->bs;
1905
1906 /* Only cancel if it's the job we started */
1907 if (bs && bs->job && bs->job == state->job) {
1908 block_job_cancel_sync(bs->job);
1909 }
1910 }
1911
1912 static void drive_backup_clean(BlkActionState *common)
1913 {
1914 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1915
1916 if (state->aio_context) {
1917 bdrv_drained_end(state->bs);
1918 aio_context_release(state->aio_context);
1919 }
1920 }
1921
1922 typedef struct BlockdevBackupState {
1923 BlkActionState common;
1924 BlockDriverState *bs;
1925 BlockJob *job;
1926 AioContext *aio_context;
1927 } BlockdevBackupState;
1928
1929 static void do_blockdev_backup(const char *device, const char *target,
1930 enum MirrorSyncMode sync,
1931 bool has_speed, int64_t speed,
1932 bool has_on_source_error,
1933 BlockdevOnError on_source_error,
1934 bool has_on_target_error,
1935 BlockdevOnError on_target_error,
1936 BlockJobTxn *txn, Error **errp);
1937
1938 static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1939 {
1940 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1941 BlockdevBackup *backup;
1942 BlockBackend *blk, *target;
1943 Error *local_err = NULL;
1944
1945 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1946 backup = common->action->u.blockdev_backup.data;
1947
1948 blk = blk_by_name(backup->device);
1949 if (!blk) {
1950 error_setg(errp, "Device '%s' not found", backup->device);
1951 return;
1952 }
1953
1954 if (!blk_is_available(blk)) {
1955 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1956 return;
1957 }
1958
1959 target = blk_by_name(backup->target);
1960 if (!target) {
1961 error_setg(errp, "Device '%s' not found", backup->target);
1962 return;
1963 }
1964
1965 /* AioContext is released in .clean() */
1966 state->aio_context = blk_get_aio_context(blk);
1967 if (state->aio_context != blk_get_aio_context(target)) {
1968 state->aio_context = NULL;
1969 error_setg(errp, "Backup between two IO threads is not implemented");
1970 return;
1971 }
1972 aio_context_acquire(state->aio_context);
1973 state->bs = blk_bs(blk);
1974 bdrv_drained_begin(state->bs);
1975
1976 do_blockdev_backup(backup->device, backup->target,
1977 backup->sync,
1978 backup->has_speed, backup->speed,
1979 backup->has_on_source_error, backup->on_source_error,
1980 backup->has_on_target_error, backup->on_target_error,
1981 common->block_job_txn, &local_err);
1982 if (local_err) {
1983 error_propagate(errp, local_err);
1984 return;
1985 }
1986
1987 state->job = state->bs->job;
1988 }
1989
1990 static void blockdev_backup_abort(BlkActionState *common)
1991 {
1992 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1993 BlockDriverState *bs = state->bs;
1994
1995 /* Only cancel if it's the job we started */
1996 if (bs && bs->job && bs->job == state->job) {
1997 block_job_cancel_sync(bs->job);
1998 }
1999 }
2000
2001 static void blockdev_backup_clean(BlkActionState *common)
2002 {
2003 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
2004
2005 if (state->aio_context) {
2006 bdrv_drained_end(state->bs);
2007 aio_context_release(state->aio_context);
2008 }
2009 }
2010
2011 typedef struct BlockDirtyBitmapState {
2012 BlkActionState common;
2013 BdrvDirtyBitmap *bitmap;
2014 BlockDriverState *bs;
2015 AioContext *aio_context;
2016 HBitmap *backup;
2017 bool prepared;
2018 } BlockDirtyBitmapState;
2019
2020 static void block_dirty_bitmap_add_prepare(BlkActionState *common,
2021 Error **errp)
2022 {
2023 Error *local_err = NULL;
2024 BlockDirtyBitmapAdd *action;
2025 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2026 common, common);
2027
2028 if (action_check_completion_mode(common, errp) < 0) {
2029 return;
2030 }
2031
2032 action = common->action->u.block_dirty_bitmap_add.data;
2033 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2034 qmp_block_dirty_bitmap_add(action->node, action->name,
2035 action->has_granularity, action->granularity,
2036 &local_err);
2037
2038 if (!local_err) {
2039 state->prepared = true;
2040 } else {
2041 error_propagate(errp, local_err);
2042 }
2043 }
2044
2045 static void block_dirty_bitmap_add_abort(BlkActionState *common)
2046 {
2047 BlockDirtyBitmapAdd *action;
2048 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2049 common, common);
2050
2051 action = common->action->u.block_dirty_bitmap_add.data;
2052 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2053 * then the node reference and bitmap name must have been valid.
2054 */
2055 if (state->prepared) {
2056 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2057 }
2058 }
2059
2060 static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
2061 Error **errp)
2062 {
2063 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2064 common, common);
2065 BlockDirtyBitmap *action;
2066
2067 if (action_check_completion_mode(common, errp) < 0) {
2068 return;
2069 }
2070
2071 action = common->action->u.block_dirty_bitmap_clear.data;
2072 state->bitmap = block_dirty_bitmap_lookup(action->node,
2073 action->name,
2074 &state->bs,
2075 &state->aio_context,
2076 errp);
2077 if (!state->bitmap) {
2078 return;
2079 }
2080
2081 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2082 error_setg(errp, "Cannot modify a frozen bitmap");
2083 return;
2084 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2085 error_setg(errp, "Cannot clear a disabled bitmap");
2086 return;
2087 }
2088
2089 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2090 /* AioContext is released in .clean() */
2091 }
2092
2093 static void block_dirty_bitmap_clear_abort(BlkActionState *common)
2094 {
2095 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2096 common, common);
2097
2098 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2099 }
2100
2101 static void block_dirty_bitmap_clear_commit(BlkActionState *common)
2102 {
2103 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2104 common, common);
2105
2106 hbitmap_free(state->backup);
2107 }
2108
2109 static void block_dirty_bitmap_clear_clean(BlkActionState *common)
2110 {
2111 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2112 common, common);
2113
2114 if (state->aio_context) {
2115 aio_context_release(state->aio_context);
2116 }
2117 }
2118
2119 static void abort_prepare(BlkActionState *common, Error **errp)
2120 {
2121 error_setg(errp, "Transaction aborted using Abort action");
2122 }
2123
2124 static void abort_commit(BlkActionState *common)
2125 {
2126 g_assert_not_reached(); /* this action never succeeds */
2127 }
2128
2129 static const BlkActionOps actions[] = {
2130 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2131 .instance_size = sizeof(ExternalSnapshotState),
2132 .prepare = external_snapshot_prepare,
2133 .commit = external_snapshot_commit,
2134 .abort = external_snapshot_abort,
2135 .clean = external_snapshot_clean,
2136 },
2137 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2138 .instance_size = sizeof(ExternalSnapshotState),
2139 .prepare = external_snapshot_prepare,
2140 .commit = external_snapshot_commit,
2141 .abort = external_snapshot_abort,
2142 .clean = external_snapshot_clean,
2143 },
2144 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2145 .instance_size = sizeof(DriveBackupState),
2146 .prepare = drive_backup_prepare,
2147 .abort = drive_backup_abort,
2148 .clean = drive_backup_clean,
2149 },
2150 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2151 .instance_size = sizeof(BlockdevBackupState),
2152 .prepare = blockdev_backup_prepare,
2153 .abort = blockdev_backup_abort,
2154 .clean = blockdev_backup_clean,
2155 },
2156 [TRANSACTION_ACTION_KIND_ABORT] = {
2157 .instance_size = sizeof(BlkActionState),
2158 .prepare = abort_prepare,
2159 .commit = abort_commit,
2160 },
2161 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2162 .instance_size = sizeof(InternalSnapshotState),
2163 .prepare = internal_snapshot_prepare,
2164 .abort = internal_snapshot_abort,
2165 .clean = internal_snapshot_clean,
2166 },
2167 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2168 .instance_size = sizeof(BlockDirtyBitmapState),
2169 .prepare = block_dirty_bitmap_add_prepare,
2170 .abort = block_dirty_bitmap_add_abort,
2171 },
2172 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2173 .instance_size = sizeof(BlockDirtyBitmapState),
2174 .prepare = block_dirty_bitmap_clear_prepare,
2175 .commit = block_dirty_bitmap_clear_commit,
2176 .abort = block_dirty_bitmap_clear_abort,
2177 .clean = block_dirty_bitmap_clear_clean,
2178 }
2179 };
2180
2181 /**
2182 * Allocate a TransactionProperties structure if necessary, and fill
2183 * that structure with desired defaults if they are unset.
2184 */
2185 static TransactionProperties *get_transaction_properties(
2186 TransactionProperties *props)
2187 {
2188 if (!props) {
2189 props = g_new0(TransactionProperties, 1);
2190 }
2191
2192 if (!props->has_completion_mode) {
2193 props->has_completion_mode = true;
2194 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2195 }
2196
2197 return props;
2198 }
2199
2200 /*
2201 * 'Atomic' group operations. The operations are performed as a set, and if
2202 * any fail then we roll back all operations in the group.
2203 */
2204 void qmp_transaction(TransactionActionList *dev_list,
2205 bool has_props,
2206 struct TransactionProperties *props,
2207 Error **errp)
2208 {
2209 TransactionActionList *dev_entry = dev_list;
2210 BlockJobTxn *block_job_txn = NULL;
2211 BlkActionState *state, *next;
2212 Error *local_err = NULL;
2213
2214 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
2215 QSIMPLEQ_INIT(&snap_bdrv_states);
2216
2217 /* Does this transaction get canceled as a group on failure?
2218 * If not, we don't really need to make a BlockJobTxn.
2219 */
2220 props = get_transaction_properties(props);
2221 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2222 block_job_txn = block_job_txn_new();
2223 }
2224
2225 /* drain all i/o before any operations */
2226 bdrv_drain_all();
2227
2228 /* We don't do anything in this loop that commits us to the operations */
2229 while (NULL != dev_entry) {
2230 TransactionAction *dev_info = NULL;
2231 const BlkActionOps *ops;
2232
2233 dev_info = dev_entry->value;
2234 dev_entry = dev_entry->next;
2235
2236 assert(dev_info->type < ARRAY_SIZE(actions));
2237
2238 ops = &actions[dev_info->type];
2239 assert(ops->instance_size > 0);
2240
2241 state = g_malloc0(ops->instance_size);
2242 state->ops = ops;
2243 state->action = dev_info;
2244 state->block_job_txn = block_job_txn;
2245 state->txn_props = props;
2246 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
2247
2248 state->ops->prepare(state, &local_err);
2249 if (local_err) {
2250 error_propagate(errp, local_err);
2251 goto delete_and_fail;
2252 }
2253 }
2254
2255 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2256 if (state->ops->commit) {
2257 state->ops->commit(state);
2258 }
2259 }
2260
2261 /* success */
2262 goto exit;
2263
2264 delete_and_fail:
2265 /* failure, and it is all-or-none; roll back all operations */
2266 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2267 if (state->ops->abort) {
2268 state->ops->abort(state);
2269 }
2270 }
2271 exit:
2272 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2273 if (state->ops->clean) {
2274 state->ops->clean(state);
2275 }
2276 g_free(state);
2277 }
2278 if (!has_props) {
2279 qapi_free_TransactionProperties(props);
2280 }
2281 block_job_txn_unref(block_job_txn);
2282 }
2283
2284 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
2285 {
2286 Error *local_err = NULL;
2287
2288 qmp_blockdev_open_tray(device, has_force, force, &local_err);
2289 if (local_err) {
2290 error_propagate(errp, local_err);
2291 return;
2292 }
2293
2294 qmp_x_blockdev_remove_medium(device, errp);
2295 }
2296
2297 void qmp_block_passwd(bool has_device, const char *device,
2298 bool has_node_name, const char *node_name,
2299 const char *password, Error **errp)
2300 {
2301 Error *local_err = NULL;
2302 BlockDriverState *bs;
2303 AioContext *aio_context;
2304
2305 bs = bdrv_lookup_bs(has_device ? device : NULL,
2306 has_node_name ? node_name : NULL,
2307 &local_err);
2308 if (local_err) {
2309 error_propagate(errp, local_err);
2310 return;
2311 }
2312
2313 aio_context = bdrv_get_aio_context(bs);
2314 aio_context_acquire(aio_context);
2315
2316 bdrv_add_key(bs, password, errp);
2317
2318 aio_context_release(aio_context);
2319 }
2320
2321 void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
2322 Error **errp)
2323 {
2324 BlockBackend *blk;
2325 bool locked;
2326
2327 if (!has_force) {
2328 force = false;
2329 }
2330
2331 blk = blk_by_name(device);
2332 if (!blk) {
2333 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2334 "Device '%s' not found", device);
2335 return;
2336 }
2337
2338 if (!blk_dev_has_removable_media(blk)) {
2339 error_setg(errp, "Device '%s' is not removable", device);
2340 return;
2341 }
2342
2343 if (!blk_dev_has_tray(blk)) {
2344 /* Ignore this command on tray-less devices */
2345 return;
2346 }
2347
2348 if (blk_dev_is_tray_open(blk)) {
2349 return;
2350 }
2351
2352 locked = blk_dev_is_medium_locked(blk);
2353 if (locked) {
2354 blk_dev_eject_request(blk, force);
2355 }
2356
2357 if (!locked || force) {
2358 blk_dev_change_media_cb(blk, false);
2359 }
2360 }
2361
2362 void qmp_blockdev_close_tray(const char *device, Error **errp)
2363 {
2364 BlockBackend *blk;
2365
2366 blk = blk_by_name(device);
2367 if (!blk) {
2368 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2369 "Device '%s' not found", device);
2370 return;
2371 }
2372
2373 if (!blk_dev_has_removable_media(blk)) {
2374 error_setg(errp, "Device '%s' is not removable", device);
2375 return;
2376 }
2377
2378 if (!blk_dev_has_tray(blk)) {
2379 /* Ignore this command on tray-less devices */
2380 return;
2381 }
2382
2383 if (!blk_dev_is_tray_open(blk)) {
2384 return;
2385 }
2386
2387 blk_dev_change_media_cb(blk, true);
2388 }
2389
2390 void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
2391 {
2392 BlockBackend *blk;
2393 BlockDriverState *bs;
2394 AioContext *aio_context;
2395 bool has_device;
2396
2397 blk = blk_by_name(device);
2398 if (!blk) {
2399 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2400 "Device '%s' not found", device);
2401 return;
2402 }
2403
2404 /* For BBs without a device, we can exchange the BDS tree at will */
2405 has_device = blk_get_attached_dev(blk);
2406
2407 if (has_device && !blk_dev_has_removable_media(blk)) {
2408 error_setg(errp, "Device '%s' is not removable", device);
2409 return;
2410 }
2411
2412 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2413 error_setg(errp, "Tray of device '%s' is not open", device);
2414 return;
2415 }
2416
2417 bs = blk_bs(blk);
2418 if (!bs) {
2419 return;
2420 }
2421
2422 aio_context = bdrv_get_aio_context(bs);
2423 aio_context_acquire(aio_context);
2424
2425 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2426 goto out;
2427 }
2428
2429 blk_remove_bs(blk);
2430
2431 if (!blk_dev_has_tray(blk)) {
2432 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2433 * called at all); therefore, the medium needs to be ejected here.
2434 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2435 * value passed here (i.e. false). */
2436 blk_dev_change_media_cb(blk, false);
2437 }
2438
2439 out:
2440 aio_context_release(aio_context);
2441 }
2442
2443 static void qmp_blockdev_insert_anon_medium(const char *device,
2444 BlockDriverState *bs, Error **errp)
2445 {
2446 BlockBackend *blk;
2447 bool has_device;
2448
2449 blk = blk_by_name(device);
2450 if (!blk) {
2451 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2452 "Device '%s' not found", device);
2453 return;
2454 }
2455
2456 /* For BBs without a device, we can exchange the BDS tree at will */
2457 has_device = blk_get_attached_dev(blk);
2458
2459 if (has_device && !blk_dev_has_removable_media(blk)) {
2460 error_setg(errp, "Device '%s' is not removable", device);
2461 return;
2462 }
2463
2464 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2465 error_setg(errp, "Tray of device '%s' is not open", device);
2466 return;
2467 }
2468
2469 if (blk_bs(blk)) {
2470 error_setg(errp, "There already is a medium in device '%s'", device);
2471 return;
2472 }
2473
2474 blk_insert_bs(blk, bs);
2475
2476 if (!blk_dev_has_tray(blk)) {
2477 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2478 * called at all); therefore, the medium needs to be pushed into the
2479 * slot here.
2480 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2481 * value passed here (i.e. true). */
2482 blk_dev_change_media_cb(blk, true);
2483 }
2484 }
2485
2486 void qmp_x_blockdev_insert_medium(const char *device, const char *node_name,
2487 Error **errp)
2488 {
2489 BlockDriverState *bs;
2490
2491 bs = bdrv_find_node(node_name);
2492 if (!bs) {
2493 error_setg(errp, "Node '%s' not found", node_name);
2494 return;
2495 }
2496
2497 if (bs->blk) {
2498 error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
2499 blk_name(bs->blk));
2500 return;
2501 }
2502
2503 qmp_blockdev_insert_anon_medium(device, bs, errp);
2504 }
2505
2506 void qmp_blockdev_change_medium(const char *device, const char *filename,
2507 bool has_format, const char *format,
2508 bool has_read_only,
2509 BlockdevChangeReadOnlyMode read_only,
2510 Error **errp)
2511 {
2512 BlockBackend *blk;
2513 BlockDriverState *medium_bs = NULL;
2514 int bdrv_flags, ret;
2515 QDict *options = NULL;
2516 Error *err = NULL;
2517
2518 blk = blk_by_name(device);
2519 if (!blk) {
2520 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2521 "Device '%s' not found", device);
2522 goto fail;
2523 }
2524
2525 if (blk_bs(blk)) {
2526 blk_update_root_state(blk);
2527 }
2528
2529 bdrv_flags = blk_get_open_flags_from_root_state(blk);
2530 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2531 BDRV_O_PROTOCOL);
2532
2533 if (!has_read_only) {
2534 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2535 }
2536
2537 switch (read_only) {
2538 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2539 break;
2540
2541 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2542 bdrv_flags &= ~BDRV_O_RDWR;
2543 break;
2544
2545 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2546 bdrv_flags |= BDRV_O_RDWR;
2547 break;
2548
2549 default:
2550 abort();
2551 }
2552
2553 if (has_format) {
2554 options = qdict_new();
2555 qdict_put(options, "driver", qstring_from_str(format));
2556 }
2557
2558 assert(!medium_bs);
2559 ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2560 if (ret < 0) {
2561 goto fail;
2562 }
2563
2564 bdrv_add_key(medium_bs, NULL, &err);
2565 if (err) {
2566 error_propagate(errp, err);
2567 goto fail;
2568 }
2569
2570 qmp_blockdev_open_tray(device, false, false, &err);
2571 if (err) {
2572 error_propagate(errp, err);
2573 goto fail;
2574 }
2575
2576 qmp_x_blockdev_remove_medium(device, &err);
2577 if (err) {
2578 error_propagate(errp, err);
2579 goto fail;
2580 }
2581
2582 qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2583 if (err) {
2584 error_propagate(errp, err);
2585 goto fail;
2586 }
2587
2588 blk_apply_root_state(blk, medium_bs);
2589
2590 qmp_blockdev_close_tray(device, errp);
2591
2592 fail:
2593 /* If the medium has been inserted, the device has its own reference, so
2594 * ours must be relinquished; and if it has not been inserted successfully,
2595 * the reference must be relinquished anyway */
2596 bdrv_unref(medium_bs);
2597 }
2598
2599 /* throttling disk I/O limits */
2600 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
2601 int64_t bps_wr,
2602 int64_t iops,
2603 int64_t iops_rd,
2604 int64_t iops_wr,
2605 bool has_bps_max,
2606 int64_t bps_max,
2607 bool has_bps_rd_max,
2608 int64_t bps_rd_max,
2609 bool has_bps_wr_max,
2610 int64_t bps_wr_max,
2611 bool has_iops_max,
2612 int64_t iops_max,
2613 bool has_iops_rd_max,
2614 int64_t iops_rd_max,
2615 bool has_iops_wr_max,
2616 int64_t iops_wr_max,
2617 bool has_bps_max_length,
2618 int64_t bps_max_length,
2619 bool has_bps_rd_max_length,
2620 int64_t bps_rd_max_length,
2621 bool has_bps_wr_max_length,
2622 int64_t bps_wr_max_length,
2623 bool has_iops_max_length,
2624 int64_t iops_max_length,
2625 bool has_iops_rd_max_length,
2626 int64_t iops_rd_max_length,
2627 bool has_iops_wr_max_length,
2628 int64_t iops_wr_max_length,
2629 bool has_iops_size,
2630 int64_t iops_size,
2631 bool has_group,
2632 const char *group, Error **errp)
2633 {
2634 ThrottleConfig cfg;
2635 BlockDriverState *bs;
2636 BlockBackend *blk;
2637 AioContext *aio_context;
2638
2639 blk = blk_by_name(device);
2640 if (!blk) {
2641 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2642 "Device '%s' not found", device);
2643 return;
2644 }
2645
2646 aio_context = blk_get_aio_context(blk);
2647 aio_context_acquire(aio_context);
2648
2649 bs = blk_bs(blk);
2650 if (!bs) {
2651 error_setg(errp, "Device '%s' has no medium", device);
2652 goto out;
2653 }
2654
2655 throttle_config_init(&cfg);
2656 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2657 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
2658 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
2659
2660 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2661 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
2662 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2663
2664 if (has_bps_max) {
2665 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
2666 }
2667 if (has_bps_rd_max) {
2668 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
2669 }
2670 if (has_bps_wr_max) {
2671 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2672 }
2673 if (has_iops_max) {
2674 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2675 }
2676 if (has_iops_rd_max) {
2677 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2678 }
2679 if (has_iops_wr_max) {
2680 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2681 }
2682
2683 if (has_bps_max_length) {
2684 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = bps_max_length;
2685 }
2686 if (has_bps_rd_max_length) {
2687 cfg.buckets[THROTTLE_BPS_READ].burst_length = bps_rd_max_length;
2688 }
2689 if (has_bps_wr_max_length) {
2690 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = bps_wr_max_length;
2691 }
2692 if (has_iops_max_length) {
2693 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = iops_max_length;
2694 }
2695 if (has_iops_rd_max_length) {
2696 cfg.buckets[THROTTLE_OPS_READ].burst_length = iops_rd_max_length;
2697 }
2698 if (has_iops_wr_max_length) {
2699 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = iops_wr_max_length;
2700 }
2701
2702 if (has_iops_size) {
2703 cfg.op_size = iops_size;
2704 }
2705
2706 if (!throttle_is_valid(&cfg, errp)) {
2707 goto out;
2708 }
2709
2710 if (throttle_enabled(&cfg)) {
2711 /* Enable I/O limits if they're not enabled yet, otherwise
2712 * just update the throttling group. */
2713 if (!blk_get_public(blk)->throttle_state) {
2714 blk_io_limits_enable(blk, has_group ? group : device);
2715 } else if (has_group) {
2716 blk_io_limits_update_group(blk, group);
2717 }
2718 /* Set the new throttling configuration */
2719 blk_set_io_limits(blk, &cfg);
2720 } else if (blk_get_public(blk)->throttle_state) {
2721 /* If all throttling settings are set to 0, disable I/O limits */
2722 blk_io_limits_disable(blk);
2723 }
2724
2725 out:
2726 aio_context_release(aio_context);
2727 }
2728
2729 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2730 bool has_granularity, uint32_t granularity,
2731 Error **errp)
2732 {
2733 AioContext *aio_context;
2734 BlockDriverState *bs;
2735
2736 if (!name || name[0] == '\0') {
2737 error_setg(errp, "Bitmap name cannot be empty");
2738 return;
2739 }
2740
2741 bs = bdrv_lookup_bs(node, node, errp);
2742 if (!bs) {
2743 return;
2744 }
2745
2746 aio_context = bdrv_get_aio_context(bs);
2747 aio_context_acquire(aio_context);
2748
2749 if (has_granularity) {
2750 if (granularity < 512 || !is_power_of_2(granularity)) {
2751 error_setg(errp, "Granularity must be power of 2 "
2752 "and at least 512");
2753 goto out;
2754 }
2755 } else {
2756 /* Default to cluster size, if available: */
2757 granularity = bdrv_get_default_bitmap_granularity(bs);
2758 }
2759
2760 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2761
2762 out:
2763 aio_context_release(aio_context);
2764 }
2765
2766 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2767 Error **errp)
2768 {
2769 AioContext *aio_context;
2770 BlockDriverState *bs;
2771 BdrvDirtyBitmap *bitmap;
2772
2773 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2774 if (!bitmap || !bs) {
2775 return;
2776 }
2777
2778 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2779 error_setg(errp,
2780 "Bitmap '%s' is currently frozen and cannot be removed",
2781 name);
2782 goto out;
2783 }
2784 bdrv_dirty_bitmap_make_anon(bitmap);
2785 bdrv_release_dirty_bitmap(bs, bitmap);
2786
2787 out:
2788 aio_context_release(aio_context);
2789 }
2790
2791 /**
2792 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2793 * immediately after a full backup operation.
2794 */
2795 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2796 Error **errp)
2797 {
2798 AioContext *aio_context;
2799 BdrvDirtyBitmap *bitmap;
2800 BlockDriverState *bs;
2801
2802 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2803 if (!bitmap || !bs) {
2804 return;
2805 }
2806
2807 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2808 error_setg(errp,
2809 "Bitmap '%s' is currently frozen and cannot be modified",
2810 name);
2811 goto out;
2812 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2813 error_setg(errp,
2814 "Bitmap '%s' is currently disabled and cannot be cleared",
2815 name);
2816 goto out;
2817 }
2818
2819 bdrv_clear_dirty_bitmap(bitmap, NULL);
2820
2821 out:
2822 aio_context_release(aio_context);
2823 }
2824
2825 void hmp_drive_del(Monitor *mon, const QDict *qdict)
2826 {
2827 const char *id = qdict_get_str(qdict, "id");
2828 BlockBackend *blk;
2829 BlockDriverState *bs;
2830 AioContext *aio_context;
2831 Error *local_err = NULL;
2832
2833 bs = bdrv_find_node(id);
2834 if (bs) {
2835 qmp_x_blockdev_del(false, NULL, true, id, &local_err);
2836 if (local_err) {
2837 error_report_err(local_err);
2838 }
2839 return;
2840 }
2841
2842 blk = blk_by_name(id);
2843 if (!blk) {
2844 error_report("Device '%s' not found", id);
2845 return;
2846 }
2847
2848 if (!blk_legacy_dinfo(blk)) {
2849 error_report("Deleting device added with blockdev-add"
2850 " is not supported");
2851 return;
2852 }
2853
2854 aio_context = blk_get_aio_context(blk);
2855 aio_context_acquire(aio_context);
2856
2857 bs = blk_bs(blk);
2858 if (bs) {
2859 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2860 error_report_err(local_err);
2861 aio_context_release(aio_context);
2862 return;
2863 }
2864
2865 blk_remove_bs(blk);
2866 }
2867
2868 /* Make the BlockBackend and the attached BlockDriverState anonymous */
2869 monitor_remove_blk(blk);
2870
2871 /* If this BlockBackend has a device attached to it, its refcount will be
2872 * decremented when the device is removed; otherwise we have to do so here.
2873 */
2874 if (blk_get_attached_dev(blk)) {
2875 /* Further I/O must not pause the guest */
2876 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2877 BLOCKDEV_ON_ERROR_REPORT);
2878 } else {
2879 blk_unref(blk);
2880 }
2881
2882 aio_context_release(aio_context);
2883 }
2884
2885 void qmp_block_resize(bool has_device, const char *device,
2886 bool has_node_name, const char *node_name,
2887 int64_t size, Error **errp)
2888 {
2889 Error *local_err = NULL;
2890 BlockDriverState *bs;
2891 AioContext *aio_context;
2892 int ret;
2893
2894 bs = bdrv_lookup_bs(has_device ? device : NULL,
2895 has_node_name ? node_name : NULL,
2896 &local_err);
2897 if (local_err) {
2898 error_propagate(errp, local_err);
2899 return;
2900 }
2901
2902 aio_context = bdrv_get_aio_context(bs);
2903 aio_context_acquire(aio_context);
2904
2905 if (!bdrv_is_first_non_filter(bs)) {
2906 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2907 goto out;
2908 }
2909
2910 if (size < 0) {
2911 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2912 goto out;
2913 }
2914
2915 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2916 error_setg(errp, QERR_DEVICE_IN_USE, device);
2917 goto out;
2918 }
2919
2920 /* complete all in-flight operations before resizing the device */
2921 bdrv_drain_all();
2922
2923 ret = bdrv_truncate(bs, size);
2924 switch (ret) {
2925 case 0:
2926 break;
2927 case -ENOMEDIUM:
2928 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2929 break;
2930 case -ENOTSUP:
2931 error_setg(errp, QERR_UNSUPPORTED);
2932 break;
2933 case -EACCES:
2934 error_setg(errp, "Device '%s' is read only", device);
2935 break;
2936 case -EBUSY:
2937 error_setg(errp, QERR_DEVICE_IN_USE, device);
2938 break;
2939 default:
2940 error_setg_errno(errp, -ret, "Could not resize");
2941 break;
2942 }
2943
2944 out:
2945 aio_context_release(aio_context);
2946 }
2947
2948 static void block_job_cb(void *opaque, int ret)
2949 {
2950 /* Note that this function may be executed from another AioContext besides
2951 * the QEMU main loop. If you need to access anything that assumes the
2952 * QEMU global mutex, use a BH or introduce a mutex.
2953 */
2954
2955 BlockDriverState *bs = opaque;
2956 const char *msg = NULL;
2957
2958 trace_block_job_cb(bs, bs->job, ret);
2959
2960 assert(bs->job);
2961
2962 if (ret < 0) {
2963 msg = strerror(-ret);
2964 }
2965
2966 if (block_job_is_cancelled(bs->job)) {
2967 block_job_event_cancelled(bs->job);
2968 } else {
2969 block_job_event_completed(bs->job, msg);
2970 }
2971 }
2972
2973 void qmp_block_stream(const char *device,
2974 bool has_base, const char *base,
2975 bool has_backing_file, const char *backing_file,
2976 bool has_speed, int64_t speed,
2977 bool has_on_error, BlockdevOnError on_error,
2978 Error **errp)
2979 {
2980 BlockBackend *blk;
2981 BlockDriverState *bs;
2982 BlockDriverState *base_bs = NULL;
2983 AioContext *aio_context;
2984 Error *local_err = NULL;
2985 const char *base_name = NULL;
2986
2987 if (!has_on_error) {
2988 on_error = BLOCKDEV_ON_ERROR_REPORT;
2989 }
2990
2991 blk = blk_by_name(device);
2992 if (!blk) {
2993 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2994 "Device '%s' not found", device);
2995 return;
2996 }
2997
2998 aio_context = blk_get_aio_context(blk);
2999 aio_context_acquire(aio_context);
3000
3001 if (!blk_is_available(blk)) {
3002 error_setg(errp, "Device '%s' has no medium", device);
3003 goto out;
3004 }
3005 bs = blk_bs(blk);
3006
3007 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
3008 goto out;
3009 }
3010
3011 if (has_base) {
3012 base_bs = bdrv_find_backing_image(bs, base);
3013 if (base_bs == NULL) {
3014 error_setg(errp, QERR_BASE_NOT_FOUND, base);
3015 goto out;
3016 }
3017 assert(bdrv_get_aio_context(base_bs) == aio_context);
3018 base_name = base;
3019 }
3020
3021 /* if we are streaming the entire chain, the result will have no backing
3022 * file, and specifying one is therefore an error */
3023 if (base_bs == NULL && has_backing_file) {
3024 error_setg(errp, "backing file specified, but streaming the "
3025 "entire chain");
3026 goto out;
3027 }
3028
3029 /* backing_file string overrides base bs filename */
3030 base_name = has_backing_file ? backing_file : base_name;
3031
3032 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
3033 on_error, block_job_cb, bs, &local_err);
3034 if (local_err) {
3035 error_propagate(errp, local_err);
3036 goto out;
3037 }
3038
3039 trace_qmp_block_stream(bs, bs->job);
3040
3041 out:
3042 aio_context_release(aio_context);
3043 }
3044
3045 void qmp_block_commit(const char *device,
3046 bool has_base, const char *base,
3047 bool has_top, const char *top,
3048 bool has_backing_file, const char *backing_file,
3049 bool has_speed, int64_t speed,
3050 Error **errp)
3051 {
3052 BlockBackend *blk;
3053 BlockDriverState *bs;
3054 BlockDriverState *base_bs, *top_bs;
3055 AioContext *aio_context;
3056 Error *local_err = NULL;
3057 /* This will be part of the QMP command, if/when the
3058 * BlockdevOnError change for blkmirror makes it in
3059 */
3060 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
3061
3062 if (!has_speed) {
3063 speed = 0;
3064 }
3065
3066 /* Important Note:
3067 * libvirt relies on the DeviceNotFound error class in order to probe for
3068 * live commit feature versions; for this to work, we must make sure to
3069 * perform the device lookup before any generic errors that may occur in a
3070 * scenario in which all optional arguments are omitted. */
3071 blk = blk_by_name(device);
3072 if (!blk) {
3073 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3074 "Device '%s' not found", device);
3075 return;
3076 }
3077
3078 aio_context = blk_get_aio_context(blk);
3079 aio_context_acquire(aio_context);
3080
3081 if (!blk_is_available(blk)) {
3082 error_setg(errp, "Device '%s' has no medium", device);
3083 goto out;
3084 }
3085 bs = blk_bs(blk);
3086
3087 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
3088 goto out;
3089 }
3090
3091 /* default top_bs is the active layer */
3092 top_bs = bs;
3093
3094 if (has_top && top) {
3095 if (strcmp(bs->filename, top) != 0) {
3096 top_bs = bdrv_find_backing_image(bs, top);
3097 }
3098 }
3099
3100 if (top_bs == NULL) {
3101 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
3102 goto out;
3103 }
3104
3105 assert(bdrv_get_aio_context(top_bs) == aio_context);
3106
3107 if (has_base && base) {
3108 base_bs = bdrv_find_backing_image(top_bs, base);
3109 } else {
3110 base_bs = bdrv_find_base(top_bs);
3111 }
3112
3113 if (base_bs == NULL) {
3114 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
3115 goto out;
3116 }
3117
3118 assert(bdrv_get_aio_context(base_bs) == aio_context);
3119
3120 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3121 goto out;
3122 }
3123
3124 /* Do not allow attempts to commit an image into itself */
3125 if (top_bs == base_bs) {
3126 error_setg(errp, "cannot commit an image into itself");
3127 goto out;
3128 }
3129
3130 if (top_bs == bs) {
3131 if (has_backing_file) {
3132 error_setg(errp, "'backing-file' specified,"
3133 " but 'top' is the active layer");
3134 goto out;
3135 }
3136 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
3137 bs, &local_err);
3138 } else {
3139 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
3140 has_backing_file ? backing_file : NULL, &local_err);
3141 }
3142 if (local_err != NULL) {
3143 error_propagate(errp, local_err);
3144 goto out;
3145 }
3146
3147 out:
3148 aio_context_release(aio_context);
3149 }
3150
3151 static void do_drive_backup(const char *device, const char *target,
3152 bool has_format, const char *format,
3153 enum MirrorSyncMode sync,
3154 bool has_mode, enum NewImageMode mode,
3155 bool has_speed, int64_t speed,
3156 bool has_bitmap, const char *bitmap,
3157 bool has_on_source_error,
3158 BlockdevOnError on_source_error,
3159 bool has_on_target_error,
3160 BlockdevOnError on_target_error,
3161 BlockJobTxn *txn, Error **errp)
3162 {
3163 BlockBackend *blk;
3164 BlockDriverState *bs;
3165 BlockDriverState *target_bs;
3166 BlockDriverState *source = NULL;
3167 BdrvDirtyBitmap *bmap = NULL;
3168 AioContext *aio_context;
3169 QDict *options = NULL;
3170 Error *local_err = NULL;
3171 int flags;
3172 int64_t size;
3173 int ret;
3174
3175 if (!has_speed) {
3176 speed = 0;
3177 }
3178 if (!has_on_source_error) {
3179 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3180 }
3181 if (!has_on_target_error) {
3182 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3183 }
3184 if (!has_mode) {
3185 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3186 }
3187
3188 blk = blk_by_name(device);
3189 if (!blk) {
3190 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3191 "Device '%s' not found", device);
3192 return;
3193 }
3194
3195 aio_context = blk_get_aio_context(blk);
3196 aio_context_acquire(aio_context);
3197
3198 /* Although backup_run has this check too, we need to use bs->drv below, so
3199 * do an early check redundantly. */
3200 if (!blk_is_available(blk)) {
3201 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3202 goto out;
3203 }
3204 bs = blk_bs(blk);
3205
3206 if (!has_format) {
3207 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3208 }
3209
3210 /* Early check to avoid creating target */
3211 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
3212 goto out;
3213 }
3214
3215 flags = bs->open_flags | BDRV_O_RDWR;
3216
3217 /* See if we have a backing HD we can use to create our new image
3218 * on top of. */
3219 if (sync == MIRROR_SYNC_MODE_TOP) {
3220 source = backing_bs(bs);
3221 if (!source) {
3222 sync = MIRROR_SYNC_MODE_FULL;
3223 }
3224 }
3225 if (sync == MIRROR_SYNC_MODE_NONE) {
3226 source = bs;
3227 }
3228
3229 size = bdrv_getlength(bs);
3230 if (size < 0) {
3231 error_setg_errno(errp, -size, "bdrv_getlength failed");
3232 goto out;
3233 }
3234
3235 if (mode != NEW_IMAGE_MODE_EXISTING) {
3236 assert(format);
3237 if (source) {
3238 bdrv_img_create(target, format, source->filename,
3239 source->drv->format_name, NULL,
3240 size, flags, &local_err, false);
3241 } else {
3242 bdrv_img_create(target, format, NULL, NULL, NULL,
3243 size, flags, &local_err, false);
3244 }
3245 }
3246
3247 if (local_err) {
3248 error_propagate(errp, local_err);
3249 goto out;
3250 }
3251
3252 if (format) {
3253 options = qdict_new();
3254 qdict_put(options, "driver", qstring_from_str(format));
3255 }
3256
3257 target_bs = NULL;
3258 ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
3259 if (ret < 0) {
3260 error_propagate(errp, local_err);
3261 goto out;
3262 }
3263
3264 bdrv_set_aio_context(target_bs, aio_context);
3265
3266 if (has_bitmap) {
3267 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3268 if (!bmap) {
3269 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
3270 bdrv_unref(target_bs);
3271 goto out;
3272 }
3273 }
3274
3275 backup_start(bs, target_bs, speed, sync, bmap,
3276 on_source_error, on_target_error,
3277 block_job_cb, bs, txn, &local_err);
3278 if (local_err != NULL) {
3279 bdrv_unref(target_bs);
3280 error_propagate(errp, local_err);
3281 goto out;
3282 }
3283
3284 out:
3285 aio_context_release(aio_context);
3286 }
3287
3288 void qmp_drive_backup(const char *device, const char *target,
3289 bool has_format, const char *format,
3290 enum MirrorSyncMode sync,
3291 bool has_mode, enum NewImageMode mode,
3292 bool has_speed, int64_t speed,
3293 bool has_bitmap, const char *bitmap,
3294 bool has_on_source_error, BlockdevOnError on_source_error,
3295 bool has_on_target_error, BlockdevOnError on_target_error,
3296 Error **errp)
3297 {
3298 return do_drive_backup(device, target, has_format, format, sync,
3299 has_mode, mode, has_speed, speed,
3300 has_bitmap, bitmap,
3301 has_on_source_error, on_source_error,
3302 has_on_target_error, on_target_error,
3303 NULL, errp);
3304 }
3305
3306 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3307 {
3308 return bdrv_named_nodes_list(errp);
3309 }
3310
3311 void do_blockdev_backup(const char *device, const char *target,
3312 enum MirrorSyncMode sync,
3313 bool has_speed, int64_t speed,
3314 bool has_on_source_error,
3315 BlockdevOnError on_source_error,
3316 bool has_on_target_error,
3317 BlockdevOnError on_target_error,
3318 BlockJobTxn *txn, Error **errp)
3319 {
3320 BlockBackend *blk, *target_blk;
3321 BlockDriverState *bs;
3322 BlockDriverState *target_bs;
3323 Error *local_err = NULL;
3324 AioContext *aio_context;
3325
3326 if (!has_speed) {
3327 speed = 0;
3328 }
3329 if (!has_on_source_error) {
3330 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3331 }
3332 if (!has_on_target_error) {
3333 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3334 }
3335
3336 blk = blk_by_name(device);
3337 if (!blk) {
3338 error_setg(errp, "Device '%s' not found", device);
3339 return;
3340 }
3341
3342 aio_context = blk_get_aio_context(blk);
3343 aio_context_acquire(aio_context);
3344
3345 if (!blk_is_available(blk)) {
3346 error_setg(errp, "Device '%s' has no medium", device);
3347 goto out;
3348 }
3349 bs = blk_bs(blk);
3350
3351 target_blk = blk_by_name(target);
3352 if (!target_blk) {
3353 error_setg(errp, "Device '%s' not found", target);
3354 goto out;
3355 }
3356
3357 if (!blk_is_available(target_blk)) {
3358 error_setg(errp, "Device '%s' has no medium", target);
3359 goto out;
3360 }
3361 target_bs = blk_bs(target_blk);
3362
3363 bdrv_ref(target_bs);
3364 bdrv_set_aio_context(target_bs, aio_context);
3365 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
3366 on_target_error, block_job_cb, bs, txn, &local_err);
3367 if (local_err != NULL) {
3368 bdrv_unref(target_bs);
3369 error_propagate(errp, local_err);
3370 }
3371 out:
3372 aio_context_release(aio_context);
3373 }
3374
3375 void qmp_blockdev_backup(const char *device, const char *target,
3376 enum MirrorSyncMode sync,
3377 bool has_speed, int64_t speed,
3378 bool has_on_source_error,
3379 BlockdevOnError on_source_error,
3380 bool has_on_target_error,
3381 BlockdevOnError on_target_error,
3382 Error **errp)
3383 {
3384 do_blockdev_backup(device, target, sync, has_speed, speed,
3385 has_on_source_error, on_source_error,
3386 has_on_target_error, on_target_error,
3387 NULL, errp);
3388 }
3389
3390 /* Parameter check and block job starting for drive mirroring.
3391 * Caller should hold @device and @target's aio context (must be the same).
3392 **/
3393 static void blockdev_mirror_common(BlockDriverState *bs,
3394 BlockDriverState *target,
3395 bool has_replaces, const char *replaces,
3396 enum MirrorSyncMode sync,
3397 bool has_speed, int64_t speed,
3398 bool has_granularity, uint32_t granularity,
3399 bool has_buf_size, int64_t buf_size,
3400 bool has_on_source_error,
3401 BlockdevOnError on_source_error,
3402 bool has_on_target_error,
3403 BlockdevOnError on_target_error,
3404 bool has_unmap, bool unmap,
3405 Error **errp)
3406 {
3407
3408 if (!has_speed) {
3409 speed = 0;
3410 }
3411 if (!has_on_source_error) {
3412 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3413 }
3414 if (!has_on_target_error) {
3415 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3416 }
3417 if (!has_granularity) {
3418 granularity = 0;
3419 }
3420 if (!has_buf_size) {
3421 buf_size = 0;
3422 }
3423 if (!has_unmap) {
3424 unmap = true;
3425 }
3426
3427 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3428 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3429 "a value in range [512B, 64MB]");
3430 return;
3431 }
3432 if (granularity & (granularity - 1)) {
3433 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3434 "power of 2");
3435 return;
3436 }
3437
3438 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3439 return;
3440 }
3441 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3442 return;
3443 }
3444 if (target->blk) {
3445 error_setg(errp, "Cannot mirror to an attached block device");
3446 return;
3447 }
3448
3449 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3450 sync = MIRROR_SYNC_MODE_FULL;
3451 }
3452
3453 /* pass the node name to replace to mirror start since it's loose coupling
3454 * and will allow to check whether the node still exist at mirror completion
3455 */
3456 mirror_start(bs, target,
3457 has_replaces ? replaces : NULL,
3458 speed, granularity, buf_size, sync,
3459 on_source_error, on_target_error, unmap,
3460 block_job_cb, bs, errp);
3461 }
3462
3463 void qmp_drive_mirror(const char *device, const char *target,
3464 bool has_format, const char *format,
3465 bool has_node_name, const char *node_name,
3466 bool has_replaces, const char *replaces,
3467 enum MirrorSyncMode sync,
3468 bool has_mode, enum NewImageMode mode,
3469 bool has_speed, int64_t speed,
3470 bool has_granularity, uint32_t granularity,
3471 bool has_buf_size, int64_t buf_size,
3472 bool has_on_source_error, BlockdevOnError on_source_error,
3473 bool has_on_target_error, BlockdevOnError on_target_error,
3474 bool has_unmap, bool unmap,
3475 Error **errp)
3476 {
3477 BlockDriverState *bs;
3478 BlockBackend *blk;
3479 BlockDriverState *source, *target_bs;
3480 AioContext *aio_context;
3481 Error *local_err = NULL;
3482 QDict *options = NULL;