qcow2: Add subcluster support to handle_alloc_space()
[qemu.git] / blockdev.c
blob3848a9c8ab86fcb2d1913d5448c786ffbca48679
1 /*
2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
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.
9 * This file incorporates work covered by the following copyright and
10 * permission notice:
12 * Copyright (c) 2003-2008 Fabrice Bellard
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:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
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.
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/qdict.h"
39 #include "block/throttle-groups.h"
40 #include "monitor/monitor.h"
41 #include "qemu/error-report.h"
42 #include "qemu/option.h"
43 #include "qemu/qemu-print.h"
44 #include "qemu/config-file.h"
45 #include "qapi/qapi-commands-block.h"
46 #include "qapi/qapi-commands-transaction.h"
47 #include "qapi/qapi-visit-block-core.h"
48 #include "qapi/qmp/qdict.h"
49 #include "qapi/qmp/qnum.h"
50 #include "qapi/qmp/qstring.h"
51 #include "qapi/error.h"
52 #include "qapi/qmp/qerror.h"
53 #include "qapi/qmp/qlist.h"
54 #include "qapi/qobject-output-visitor.h"
55 #include "sysemu/sysemu.h"
56 #include "sysemu/iothread.h"
57 #include "block/block_int.h"
58 #include "block/trace.h"
59 #include "sysemu/arch_init.h"
60 #include "sysemu/qtest.h"
61 #include "sysemu/runstate.h"
62 #include "qemu/cutils.h"
63 #include "qemu/help_option.h"
64 #include "qemu/main-loop.h"
65 #include "qemu/throttle-options.h"
67 QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
68 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
70 void bdrv_set_monitor_owned(BlockDriverState *bs)
72 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
75 static const char *const if_name[IF_COUNT] = {
76 [IF_NONE] = "none",
77 [IF_IDE] = "ide",
78 [IF_SCSI] = "scsi",
79 [IF_FLOPPY] = "floppy",
80 [IF_PFLASH] = "pflash",
81 [IF_MTD] = "mtd",
82 [IF_SD] = "sd",
83 [IF_VIRTIO] = "virtio",
84 [IF_XEN] = "xen",
87 static int if_max_devs[IF_COUNT] = {
89 * Do not change these numbers! They govern how drive option
90 * index maps to unit and bus. That mapping is ABI.
92 * All controllers used to implement if=T drives need to support
93 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
94 * Otherwise, some index values map to "impossible" bus, unit
95 * values.
97 * For instance, if you change [IF_SCSI] to 255, -drive
98 * if=scsi,index=12 no longer means bus=1,unit=5, but
99 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
100 * the drive can't be set up. Regression.
102 [IF_IDE] = 2,
103 [IF_SCSI] = 7,
107 * Boards may call this to offer board-by-board overrides
108 * of the default, global values.
110 void override_max_devs(BlockInterfaceType type, int max_devs)
112 BlockBackend *blk;
113 DriveInfo *dinfo;
115 if (max_devs <= 0) {
116 return;
119 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
120 dinfo = blk_legacy_dinfo(blk);
121 if (dinfo->type == type) {
122 fprintf(stderr, "Cannot override units-per-bus property of"
123 " the %s interface, because a drive of that type has"
124 " already been added.\n", if_name[type]);
125 g_assert_not_reached();
129 if_max_devs[type] = max_devs;
133 * We automatically delete the drive when a device using it gets
134 * unplugged. Questionable feature, but we can't just drop it.
135 * Device models call blockdev_mark_auto_del() to schedule the
136 * automatic deletion, and generic qdev code calls blockdev_auto_del()
137 * when deletion is actually safe.
139 void blockdev_mark_auto_del(BlockBackend *blk)
141 DriveInfo *dinfo = blk_legacy_dinfo(blk);
142 BlockJob *job;
144 if (!dinfo) {
145 return;
148 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
149 if (block_job_has_bdrv(job, blk_bs(blk))) {
150 AioContext *aio_context = job->job.aio_context;
151 aio_context_acquire(aio_context);
153 job_cancel(&job->job, false);
155 aio_context_release(aio_context);
159 dinfo->auto_del = 1;
162 void blockdev_auto_del(BlockBackend *blk)
164 DriveInfo *dinfo = blk_legacy_dinfo(blk);
166 if (dinfo && dinfo->auto_del) {
167 monitor_remove_blk(blk);
168 blk_unref(blk);
173 * Returns the current mapping of how many units per bus
174 * a particular interface can support.
176 * A positive integer indicates n units per bus.
177 * 0 implies the mapping has not been established.
178 * -1 indicates an invalid BlockInterfaceType was given.
180 int drive_get_max_devs(BlockInterfaceType type)
182 if (type >= IF_IDE && type < IF_COUNT) {
183 return if_max_devs[type];
186 return -1;
189 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
191 int max_devs = if_max_devs[type];
192 return max_devs ? index / max_devs : 0;
195 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
197 int max_devs = if_max_devs[type];
198 return max_devs ? index % max_devs : index;
201 QemuOpts *drive_def(const char *optstr)
203 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
206 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
207 const char *optstr)
209 QemuOpts *opts;
211 opts = drive_def(optstr);
212 if (!opts) {
213 return NULL;
215 if (type != IF_DEFAULT) {
216 qemu_opt_set(opts, "if", if_name[type], &error_abort);
218 if (index >= 0) {
219 qemu_opt_set_number(opts, "index", index, &error_abort);
221 if (file)
222 qemu_opt_set(opts, "file", file, &error_abort);
223 return opts;
226 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
228 BlockBackend *blk;
229 DriveInfo *dinfo;
231 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
232 dinfo = blk_legacy_dinfo(blk);
233 if (dinfo && dinfo->type == type
234 && dinfo->bus == bus && dinfo->unit == unit) {
235 return dinfo;
239 return NULL;
242 void drive_mark_claimed_by_board(void)
244 BlockBackend *blk;
245 DriveInfo *dinfo;
247 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
248 dinfo = blk_legacy_dinfo(blk);
249 if (dinfo && blk_get_attached_dev(blk)) {
250 dinfo->claimed_by_board = true;
255 void drive_check_orphaned(void)
257 BlockBackend *blk;
258 DriveInfo *dinfo;
259 Location loc;
260 bool orphans = false;
262 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
263 dinfo = blk_legacy_dinfo(blk);
264 if (dinfo->is_default || dinfo->type == IF_NONE) {
265 continue;
267 if (!blk_get_attached_dev(blk)) {
268 loc_push_none(&loc);
269 qemu_opts_loc_restore(dinfo->opts);
270 error_report("machine type does not support"
271 " if=%s,bus=%d,unit=%d",
272 if_name[dinfo->type], dinfo->bus, dinfo->unit);
273 loc_pop(&loc);
274 orphans = true;
275 continue;
277 if (!dinfo->claimed_by_board && dinfo->type != IF_VIRTIO) {
278 loc_push_none(&loc);
279 qemu_opts_loc_restore(dinfo->opts);
280 warn_report("bogus if=%s is deprecated, use if=none",
281 if_name[dinfo->type]);
282 loc_pop(&loc);
286 if (orphans) {
287 exit(1);
291 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
293 return drive_get(type,
294 drive_index_to_bus_id(type, index),
295 drive_index_to_unit_id(type, index));
298 int drive_get_max_bus(BlockInterfaceType type)
300 int max_bus;
301 BlockBackend *blk;
302 DriveInfo *dinfo;
304 max_bus = -1;
305 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
306 dinfo = blk_legacy_dinfo(blk);
307 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
308 max_bus = dinfo->bus;
311 return max_bus;
314 /* Get a block device. This should only be used for single-drive devices
315 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
316 appropriate bus. */
317 DriveInfo *drive_get_next(BlockInterfaceType type)
319 static int next_block_unit[IF_COUNT];
321 return drive_get(type, 0, next_block_unit[type]++);
324 static void bdrv_format_print(void *opaque, const char *name)
326 qemu_printf(" %s", name);
329 typedef struct {
330 QEMUBH *bh;
331 BlockDriverState *bs;
332 } BDRVPutRefBH;
334 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
336 if (!strcmp(buf, "ignore")) {
337 return BLOCKDEV_ON_ERROR_IGNORE;
338 } else if (!is_read && !strcmp(buf, "enospc")) {
339 return BLOCKDEV_ON_ERROR_ENOSPC;
340 } else if (!strcmp(buf, "stop")) {
341 return BLOCKDEV_ON_ERROR_STOP;
342 } else if (!strcmp(buf, "report")) {
343 return BLOCKDEV_ON_ERROR_REPORT;
344 } else {
345 error_setg(errp, "'%s' invalid %s error action",
346 buf, is_read ? "read" : "write");
347 return -1;
351 static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
352 Error **errp)
354 const QListEntry *entry;
355 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
356 switch (qobject_type(entry->value)) {
358 case QTYPE_QSTRING: {
359 unsigned long long length;
360 const char *str = qstring_get_str(qobject_to(QString,
361 entry->value));
362 if (parse_uint_full(str, &length, 10) == 0 &&
363 length > 0 && length <= UINT_MAX) {
364 block_acct_add_interval(stats, (unsigned) length);
365 } else {
366 error_setg(errp, "Invalid interval length: %s", str);
367 return false;
369 break;
372 case QTYPE_QNUM: {
373 int64_t length = qnum_get_int(qobject_to(QNum, entry->value));
375 if (length > 0 && length <= UINT_MAX) {
376 block_acct_add_interval(stats, (unsigned) length);
377 } else {
378 error_setg(errp, "Invalid interval length: %" PRId64, length);
379 return false;
381 break;
384 default:
385 error_setg(errp, "The specification of stats-intervals is invalid");
386 return false;
389 return true;
392 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
394 /* All parameters but @opts are optional and may be set to NULL. */
395 static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
396 const char **throttling_group, ThrottleConfig *throttle_cfg,
397 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
399 Error *local_error = NULL;
400 const char *aio;
402 if (bdrv_flags) {
403 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
404 *bdrv_flags |= BDRV_O_COPY_ON_READ;
407 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
408 if (bdrv_parse_aio(aio, bdrv_flags) < 0) {
409 error_setg(errp, "invalid aio option");
410 return;
415 /* disk I/O throttling */
416 if (throttling_group) {
417 *throttling_group = qemu_opt_get(opts, "throttling.group");
420 if (throttle_cfg) {
421 throttle_config_init(throttle_cfg);
422 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
423 qemu_opt_get_number(opts, "throttling.bps-total", 0);
424 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
425 qemu_opt_get_number(opts, "throttling.bps-read", 0);
426 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
427 qemu_opt_get_number(opts, "throttling.bps-write", 0);
428 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
429 qemu_opt_get_number(opts, "throttling.iops-total", 0);
430 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
431 qemu_opt_get_number(opts, "throttling.iops-read", 0);
432 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
433 qemu_opt_get_number(opts, "throttling.iops-write", 0);
435 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
436 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
437 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
438 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
439 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
440 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
441 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
442 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
443 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
444 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
445 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
446 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
448 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
449 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
450 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
451 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
452 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
453 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
454 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
455 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
456 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
457 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
458 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
459 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
461 throttle_cfg->op_size =
462 qemu_opt_get_number(opts, "throttling.iops-size", 0);
464 if (!throttle_is_valid(throttle_cfg, errp)) {
465 return;
469 if (detect_zeroes) {
470 *detect_zeroes =
471 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
472 qemu_opt_get(opts, "detect-zeroes"),
473 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
474 &local_error);
475 if (local_error) {
476 error_propagate(errp, local_error);
477 return;
482 /* Takes the ownership of bs_opts */
483 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
484 Error **errp)
486 const char *buf;
487 int bdrv_flags = 0;
488 int on_read_error, on_write_error;
489 bool account_invalid, account_failed;
490 bool writethrough, read_only;
491 BlockBackend *blk;
492 BlockDriverState *bs;
493 ThrottleConfig cfg;
494 int snapshot = 0;
495 Error *error = NULL;
496 QemuOpts *opts;
497 QDict *interval_dict = NULL;
498 QList *interval_list = NULL;
499 const char *id;
500 BlockdevDetectZeroesOptions detect_zeroes =
501 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
502 const char *throttling_group = NULL;
504 /* Check common options by copying from bs_opts to opts, all other options
505 * stay in bs_opts for processing by bdrv_open(). */
506 id = qdict_get_try_str(bs_opts, "id");
507 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, errp);
508 if (!opts) {
509 goto err_no_opts;
512 if (!qemu_opts_absorb_qdict(opts, bs_opts, errp)) {
513 goto early_err;
516 if (id) {
517 qdict_del(bs_opts, "id");
520 /* extract parameters */
521 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
523 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
524 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
526 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
528 id = qemu_opts_id(opts);
530 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
531 qdict_array_split(interval_dict, &interval_list);
533 if (qdict_size(interval_dict) != 0) {
534 error_setg(errp, "Invalid option stats-intervals.%s",
535 qdict_first(interval_dict)->key);
536 goto early_err;
539 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
540 &detect_zeroes, &error);
541 if (error) {
542 error_propagate(errp, error);
543 goto early_err;
546 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
547 if (is_help_option(buf)) {
548 qemu_printf("Supported formats:");
549 bdrv_iterate_format(bdrv_format_print, NULL, false);
550 qemu_printf("\nSupported formats (read-only):");
551 bdrv_iterate_format(bdrv_format_print, NULL, true);
552 qemu_printf("\n");
553 goto early_err;
556 if (qdict_haskey(bs_opts, "driver")) {
557 error_setg(errp, "Cannot specify both 'driver' and 'format'");
558 goto early_err;
560 qdict_put_str(bs_opts, "driver", buf);
563 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
564 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
565 on_write_error = parse_block_error_action(buf, 0, &error);
566 if (error) {
567 error_propagate(errp, error);
568 goto early_err;
572 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
573 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
574 on_read_error = parse_block_error_action(buf, 1, &error);
575 if (error) {
576 error_propagate(errp, error);
577 goto early_err;
581 if (snapshot) {
582 bdrv_flags |= BDRV_O_SNAPSHOT;
585 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
587 /* init */
588 if ((!file || !*file) && !qdict_size(bs_opts)) {
589 BlockBackendRootState *blk_rs;
591 blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
592 blk_rs = blk_get_root_state(blk);
593 blk_rs->open_flags = bdrv_flags;
594 blk_rs->read_only = read_only;
595 blk_rs->detect_zeroes = detect_zeroes;
597 qobject_unref(bs_opts);
598 } else {
599 if (file && !*file) {
600 file = NULL;
603 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
604 * with other callers) rather than what we want as the real defaults.
605 * Apply the defaults here instead. */
606 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
607 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
608 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
609 read_only ? "on" : "off");
610 qdict_set_default_str(bs_opts, BDRV_OPT_AUTO_READ_ONLY, "on");
611 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
613 if (runstate_check(RUN_STATE_INMIGRATE)) {
614 bdrv_flags |= BDRV_O_INACTIVE;
617 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
618 if (!blk) {
619 goto err_no_bs_opts;
621 bs = blk_bs(blk);
623 bs->detect_zeroes = detect_zeroes;
625 block_acct_setup(blk_get_stats(blk), account_invalid, account_failed);
627 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
628 blk_unref(blk);
629 blk = NULL;
630 goto err_no_bs_opts;
634 /* disk I/O throttling */
635 if (throttle_enabled(&cfg)) {
636 if (!throttling_group) {
637 throttling_group = id;
639 blk_io_limits_enable(blk, throttling_group);
640 blk_set_io_limits(blk, &cfg);
643 blk_set_enable_write_cache(blk, !writethrough);
644 blk_set_on_error(blk, on_read_error, on_write_error);
646 if (!monitor_add_blk(blk, id, errp)) {
647 blk_unref(blk);
648 blk = NULL;
649 goto err_no_bs_opts;
652 err_no_bs_opts:
653 qemu_opts_del(opts);
654 qobject_unref(interval_dict);
655 qobject_unref(interval_list);
656 return blk;
658 early_err:
659 qemu_opts_del(opts);
660 qobject_unref(interval_dict);
661 qobject_unref(interval_list);
662 err_no_opts:
663 qobject_unref(bs_opts);
664 return NULL;
667 /* Takes the ownership of bs_opts */
668 BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
670 int bdrv_flags = 0;
672 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
673 * with other callers) rather than what we want as the real defaults.
674 * Apply the defaults here instead. */
675 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
676 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
677 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
679 if (runstate_check(RUN_STATE_INMIGRATE)) {
680 bdrv_flags |= BDRV_O_INACTIVE;
683 return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
686 void blockdev_close_all_bdrv_states(void)
688 BlockDriverState *bs, *next_bs;
690 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
691 AioContext *ctx = bdrv_get_aio_context(bs);
693 aio_context_acquire(ctx);
694 bdrv_unref(bs);
695 aio_context_release(ctx);
699 /* Iterates over the list of monitor-owned BlockDriverStates */
700 BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
702 return bs ? QTAILQ_NEXT(bs, monitor_list)
703 : QTAILQ_FIRST(&monitor_bdrv_states);
706 static bool qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
707 Error **errp)
709 const char *value;
711 value = qemu_opt_get(opts, from);
712 if (value) {
713 if (qemu_opt_find(opts, to)) {
714 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
715 "same time", to, from);
716 return false;
720 /* rename all items in opts */
721 while ((value = qemu_opt_get(opts, from))) {
722 qemu_opt_set(opts, to, value, &error_abort);
723 qemu_opt_unset(opts, from);
725 return true;
728 QemuOptsList qemu_legacy_drive_opts = {
729 .name = "drive",
730 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
731 .desc = {
733 .name = "bus",
734 .type = QEMU_OPT_NUMBER,
735 .help = "bus number",
737 .name = "unit",
738 .type = QEMU_OPT_NUMBER,
739 .help = "unit number (i.e. lun for scsi)",
741 .name = "index",
742 .type = QEMU_OPT_NUMBER,
743 .help = "index number",
745 .name = "media",
746 .type = QEMU_OPT_STRING,
747 .help = "media type (disk, cdrom)",
749 .name = "if",
750 .type = QEMU_OPT_STRING,
751 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
753 .name = "file",
754 .type = QEMU_OPT_STRING,
755 .help = "file name",
758 /* Options that are passed on, but have special semantics with -drive */
760 .name = BDRV_OPT_READ_ONLY,
761 .type = QEMU_OPT_BOOL,
762 .help = "open drive file as read-only",
764 .name = "rerror",
765 .type = QEMU_OPT_STRING,
766 .help = "read error action",
768 .name = "werror",
769 .type = QEMU_OPT_STRING,
770 .help = "write error action",
772 .name = "copy-on-read",
773 .type = QEMU_OPT_BOOL,
774 .help = "copy read data from backing file into image file",
777 { /* end of list */ }
781 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type,
782 Error **errp)
784 const char *value;
785 BlockBackend *blk;
786 DriveInfo *dinfo = NULL;
787 QDict *bs_opts;
788 QemuOpts *legacy_opts;
789 DriveMediaType media = MEDIA_DISK;
790 BlockInterfaceType type;
791 int max_devs, bus_id, unit_id, index;
792 const char *werror, *rerror;
793 bool read_only = false;
794 bool copy_on_read;
795 const char *filename;
796 int i;
798 /* Change legacy command line options into QMP ones */
799 static const struct {
800 const char *from;
801 const char *to;
802 } opt_renames[] = {
803 { "iops", "throttling.iops-total" },
804 { "iops_rd", "throttling.iops-read" },
805 { "iops_wr", "throttling.iops-write" },
807 { "bps", "throttling.bps-total" },
808 { "bps_rd", "throttling.bps-read" },
809 { "bps_wr", "throttling.bps-write" },
811 { "iops_max", "throttling.iops-total-max" },
812 { "iops_rd_max", "throttling.iops-read-max" },
813 { "iops_wr_max", "throttling.iops-write-max" },
815 { "bps_max", "throttling.bps-total-max" },
816 { "bps_rd_max", "throttling.bps-read-max" },
817 { "bps_wr_max", "throttling.bps-write-max" },
819 { "iops_size", "throttling.iops-size" },
821 { "group", "throttling.group" },
823 { "readonly", BDRV_OPT_READ_ONLY },
826 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
827 if (!qemu_opt_rename(all_opts, opt_renames[i].from,
828 opt_renames[i].to, errp)) {
829 return NULL;
833 value = qemu_opt_get(all_opts, "cache");
834 if (value) {
835 int flags = 0;
836 bool writethrough;
838 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
839 error_setg(errp, "invalid cache option");
840 return NULL;
843 /* Specific options take precedence */
844 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
845 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
846 !writethrough, &error_abort);
848 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
849 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
850 !!(flags & BDRV_O_NOCACHE), &error_abort);
852 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
853 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
854 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
856 qemu_opt_unset(all_opts, "cache");
859 /* Get a QDict for processing the options */
860 bs_opts = qdict_new();
861 qemu_opts_to_qdict(all_opts, bs_opts);
863 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
864 &error_abort);
865 if (!qemu_opts_absorb_qdict(legacy_opts, bs_opts, errp)) {
866 goto fail;
869 /* Media type */
870 value = qemu_opt_get(legacy_opts, "media");
871 if (value) {
872 if (!strcmp(value, "disk")) {
873 media = MEDIA_DISK;
874 } else if (!strcmp(value, "cdrom")) {
875 media = MEDIA_CDROM;
876 read_only = true;
877 } else {
878 error_setg(errp, "'%s' invalid media", value);
879 goto fail;
883 /* copy-on-read is disabled with a warning for read-only devices */
884 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
885 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
887 if (read_only && copy_on_read) {
888 warn_report("disabling copy-on-read on read-only drive");
889 copy_on_read = false;
892 qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off");
893 qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off");
895 /* Controller type */
896 value = qemu_opt_get(legacy_opts, "if");
897 if (value) {
898 for (type = 0;
899 type < IF_COUNT && strcmp(value, if_name[type]);
900 type++) {
902 if (type == IF_COUNT) {
903 error_setg(errp, "unsupported bus type '%s'", value);
904 goto fail;
906 } else {
907 type = block_default_type;
910 /* Device address specified by bus/unit or index.
911 * If none was specified, try to find the first free one. */
912 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
913 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
914 index = qemu_opt_get_number(legacy_opts, "index", -1);
916 max_devs = if_max_devs[type];
918 if (index != -1) {
919 if (bus_id != 0 || unit_id != -1) {
920 error_setg(errp, "index cannot be used with bus and unit");
921 goto fail;
923 bus_id = drive_index_to_bus_id(type, index);
924 unit_id = drive_index_to_unit_id(type, index);
927 if (unit_id == -1) {
928 unit_id = 0;
929 while (drive_get(type, bus_id, unit_id) != NULL) {
930 unit_id++;
931 if (max_devs && unit_id >= max_devs) {
932 unit_id -= max_devs;
933 bus_id++;
938 if (max_devs && unit_id >= max_devs) {
939 error_setg(errp, "unit %d too big (max is %d)", unit_id, max_devs - 1);
940 goto fail;
943 if (drive_get(type, bus_id, unit_id) != NULL) {
944 error_setg(errp, "drive with bus=%d, unit=%d (index=%d) exists",
945 bus_id, unit_id, index);
946 goto fail;
949 /* no id supplied -> create one */
950 if (qemu_opts_id(all_opts) == NULL) {
951 char *new_id;
952 const char *mediastr = "";
953 if (type == IF_IDE || type == IF_SCSI) {
954 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
956 if (max_devs) {
957 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
958 mediastr, unit_id);
959 } else {
960 new_id = g_strdup_printf("%s%s%i", if_name[type],
961 mediastr, unit_id);
963 qdict_put_str(bs_opts, "id", new_id);
964 g_free(new_id);
967 /* Add virtio block device */
968 if (type == IF_VIRTIO) {
969 QemuOpts *devopts;
970 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
971 &error_abort);
972 if (arch_type == QEMU_ARCH_S390X) {
973 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
974 } else {
975 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
977 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
978 &error_abort);
981 filename = qemu_opt_get(legacy_opts, "file");
983 /* Check werror/rerror compatibility with if=... */
984 werror = qemu_opt_get(legacy_opts, "werror");
985 if (werror != NULL) {
986 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
987 type != IF_NONE) {
988 error_setg(errp, "werror is not supported by this bus type");
989 goto fail;
991 qdict_put_str(bs_opts, "werror", werror);
994 rerror = qemu_opt_get(legacy_opts, "rerror");
995 if (rerror != NULL) {
996 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
997 type != IF_NONE) {
998 error_setg(errp, "rerror is not supported by this bus type");
999 goto fail;
1001 qdict_put_str(bs_opts, "rerror", rerror);
1004 /* Actual block device init: Functionality shared with blockdev-add */
1005 blk = blockdev_init(filename, bs_opts, errp);
1006 bs_opts = NULL;
1007 if (!blk) {
1008 goto fail;
1011 /* Create legacy DriveInfo */
1012 dinfo = g_malloc0(sizeof(*dinfo));
1013 dinfo->opts = all_opts;
1015 dinfo->type = type;
1016 dinfo->bus = bus_id;
1017 dinfo->unit = unit_id;
1019 blk_set_legacy_dinfo(blk, dinfo);
1021 switch(type) {
1022 case IF_IDE:
1023 case IF_SCSI:
1024 case IF_XEN:
1025 case IF_NONE:
1026 dinfo->media_cd = media == MEDIA_CDROM;
1027 break;
1028 default:
1029 break;
1032 fail:
1033 qemu_opts_del(legacy_opts);
1034 qobject_unref(bs_opts);
1035 return dinfo;
1038 static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1040 BlockDriverState *bs;
1042 bs = bdrv_lookup_bs(name, name, errp);
1043 if (bs == NULL) {
1044 return NULL;
1047 if (!bdrv_is_root_node(bs)) {
1048 error_setg(errp, "Need a root block node");
1049 return NULL;
1052 if (!bdrv_is_inserted(bs)) {
1053 error_setg(errp, "Device has no medium");
1054 return NULL;
1057 return bs;
1060 static void blockdev_do_action(TransactionAction *action, Error **errp)
1062 TransactionActionList list;
1064 list.value = action;
1065 list.next = NULL;
1066 qmp_transaction(&list, false, NULL, errp);
1069 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1070 bool has_node_name, const char *node_name,
1071 const char *snapshot_file,
1072 bool has_snapshot_node_name,
1073 const char *snapshot_node_name,
1074 bool has_format, const char *format,
1075 bool has_mode, NewImageMode mode, Error **errp)
1077 BlockdevSnapshotSync snapshot = {
1078 .has_device = has_device,
1079 .device = (char *) device,
1080 .has_node_name = has_node_name,
1081 .node_name = (char *) node_name,
1082 .snapshot_file = (char *) snapshot_file,
1083 .has_snapshot_node_name = has_snapshot_node_name,
1084 .snapshot_node_name = (char *) snapshot_node_name,
1085 .has_format = has_format,
1086 .format = (char *) format,
1087 .has_mode = has_mode,
1088 .mode = mode,
1090 TransactionAction action = {
1091 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1092 .u.blockdev_snapshot_sync.data = &snapshot,
1094 blockdev_do_action(&action, errp);
1097 void qmp_blockdev_snapshot(const char *node, const char *overlay,
1098 Error **errp)
1100 BlockdevSnapshot snapshot_data = {
1101 .node = (char *) node,
1102 .overlay = (char *) overlay
1104 TransactionAction action = {
1105 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
1106 .u.blockdev_snapshot.data = &snapshot_data,
1108 blockdev_do_action(&action, errp);
1111 void qmp_blockdev_snapshot_internal_sync(const char *device,
1112 const char *name,
1113 Error **errp)
1115 BlockdevSnapshotInternal snapshot = {
1116 .device = (char *) device,
1117 .name = (char *) name
1119 TransactionAction action = {
1120 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1121 .u.blockdev_snapshot_internal_sync.data = &snapshot,
1123 blockdev_do_action(&action, errp);
1126 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1127 bool has_id,
1128 const char *id,
1129 bool has_name,
1130 const char *name,
1131 Error **errp)
1133 BlockDriverState *bs;
1134 AioContext *aio_context;
1135 QEMUSnapshotInfo sn;
1136 Error *local_err = NULL;
1137 SnapshotInfo *info = NULL;
1138 int ret;
1140 bs = qmp_get_root_bs(device, errp);
1141 if (!bs) {
1142 return NULL;
1144 aio_context = bdrv_get_aio_context(bs);
1145 aio_context_acquire(aio_context);
1147 if (!has_id) {
1148 id = NULL;
1151 if (!has_name) {
1152 name = NULL;
1155 if (!id && !name) {
1156 error_setg(errp, "Name or id must be provided");
1157 goto out_aio_context;
1160 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1161 goto out_aio_context;
1164 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1165 if (local_err) {
1166 error_propagate(errp, local_err);
1167 goto out_aio_context;
1169 if (!ret) {
1170 error_setg(errp,
1171 "Snapshot with id '%s' and name '%s' does not exist on "
1172 "device '%s'",
1173 STR_OR_NULL(id), STR_OR_NULL(name), device);
1174 goto out_aio_context;
1177 bdrv_snapshot_delete(bs, id, name, &local_err);
1178 if (local_err) {
1179 error_propagate(errp, local_err);
1180 goto out_aio_context;
1183 aio_context_release(aio_context);
1185 info = g_new0(SnapshotInfo, 1);
1186 info->id = g_strdup(sn.id_str);
1187 info->name = g_strdup(sn.name);
1188 info->date_nsec = sn.date_nsec;
1189 info->date_sec = sn.date_sec;
1190 info->vm_state_size = sn.vm_state_size;
1191 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1192 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1194 return info;
1196 out_aio_context:
1197 aio_context_release(aio_context);
1198 return NULL;
1201 /* New and old BlockDriverState structs for atomic group operations */
1203 typedef struct BlkActionState BlkActionState;
1206 * BlkActionOps:
1207 * Table of operations that define an Action.
1209 * @instance_size: Size of state struct, in bytes.
1210 * @prepare: Prepare the work, must NOT be NULL.
1211 * @commit: Commit the changes, can be NULL.
1212 * @abort: Abort the changes on fail, can be NULL.
1213 * @clean: Clean up resources after all transaction actions have called
1214 * commit() or abort(). Can be NULL.
1216 * Only prepare() may fail. In a single transaction, only one of commit() or
1217 * abort() will be called. clean() will always be called if it is present.
1219 typedef struct BlkActionOps {
1220 size_t instance_size;
1221 void (*prepare)(BlkActionState *common, Error **errp);
1222 void (*commit)(BlkActionState *common);
1223 void (*abort)(BlkActionState *common);
1224 void (*clean)(BlkActionState *common);
1225 } BlkActionOps;
1228 * BlkActionState:
1229 * Describes one Action's state within a Transaction.
1231 * @action: QAPI-defined enum identifying which Action to perform.
1232 * @ops: Table of ActionOps this Action can perform.
1233 * @block_job_txn: Transaction which this action belongs to.
1234 * @entry: List membership for all Actions in this Transaction.
1236 * This structure must be arranged as first member in a subclassed type,
1237 * assuming that the compiler will also arrange it to the same offsets as the
1238 * base class.
1240 struct BlkActionState {
1241 TransactionAction *action;
1242 const BlkActionOps *ops;
1243 JobTxn *block_job_txn;
1244 TransactionProperties *txn_props;
1245 QTAILQ_ENTRY(BlkActionState) entry;
1248 /* internal snapshot private data */
1249 typedef struct InternalSnapshotState {
1250 BlkActionState common;
1251 BlockDriverState *bs;
1252 QEMUSnapshotInfo sn;
1253 bool created;
1254 } InternalSnapshotState;
1257 static int action_check_completion_mode(BlkActionState *s, Error **errp)
1259 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1260 error_setg(errp,
1261 "Action '%s' does not support Transaction property "
1262 "completion-mode = %s",
1263 TransactionActionKind_str(s->action->type),
1264 ActionCompletionMode_str(s->txn_props->completion_mode));
1265 return -1;
1267 return 0;
1270 static void internal_snapshot_prepare(BlkActionState *common,
1271 Error **errp)
1273 Error *local_err = NULL;
1274 const char *device;
1275 const char *name;
1276 BlockDriverState *bs;
1277 QEMUSnapshotInfo old_sn, *sn;
1278 bool ret;
1279 qemu_timeval tv;
1280 BlockdevSnapshotInternal *internal;
1281 InternalSnapshotState *state;
1282 AioContext *aio_context;
1283 int ret1;
1285 g_assert(common->action->type ==
1286 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1287 internal = common->action->u.blockdev_snapshot_internal_sync.data;
1288 state = DO_UPCAST(InternalSnapshotState, common, common);
1290 /* 1. parse input */
1291 device = internal->device;
1292 name = internal->name;
1294 /* 2. check for validation */
1295 if (action_check_completion_mode(common, errp) < 0) {
1296 return;
1299 bs = qmp_get_root_bs(device, errp);
1300 if (!bs) {
1301 return;
1304 aio_context = bdrv_get_aio_context(bs);
1305 aio_context_acquire(aio_context);
1307 state->bs = bs;
1309 /* Paired with .clean() */
1310 bdrv_drained_begin(bs);
1312 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1313 goto out;
1316 if (bdrv_is_read_only(bs)) {
1317 error_setg(errp, "Device '%s' is read only", device);
1318 goto out;
1321 if (!bdrv_can_snapshot(bs)) {
1322 error_setg(errp, "Block format '%s' used by device '%s' "
1323 "does not support internal snapshots",
1324 bs->drv->format_name, device);
1325 goto out;
1328 if (!strlen(name)) {
1329 error_setg(errp, "Name is empty");
1330 goto out;
1333 /* check whether a snapshot with name exist */
1334 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1335 &local_err);
1336 if (local_err) {
1337 error_propagate(errp, local_err);
1338 goto out;
1339 } else if (ret) {
1340 error_setg(errp,
1341 "Snapshot with name '%s' already exists on device '%s'",
1342 name, device);
1343 goto out;
1346 /* 3. take the snapshot */
1347 sn = &state->sn;
1348 pstrcpy(sn->name, sizeof(sn->name), name);
1349 qemu_gettimeofday(&tv);
1350 sn->date_sec = tv.tv_sec;
1351 sn->date_nsec = tv.tv_usec * 1000;
1352 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1354 ret1 = bdrv_snapshot_create(bs, sn);
1355 if (ret1 < 0) {
1356 error_setg_errno(errp, -ret1,
1357 "Failed to create snapshot '%s' on device '%s'",
1358 name, device);
1359 goto out;
1362 /* 4. succeed, mark a snapshot is created */
1363 state->created = true;
1365 out:
1366 aio_context_release(aio_context);
1369 static void internal_snapshot_abort(BlkActionState *common)
1371 InternalSnapshotState *state =
1372 DO_UPCAST(InternalSnapshotState, common, common);
1373 BlockDriverState *bs = state->bs;
1374 QEMUSnapshotInfo *sn = &state->sn;
1375 AioContext *aio_context;
1376 Error *local_error = NULL;
1378 if (!state->created) {
1379 return;
1382 aio_context = bdrv_get_aio_context(state->bs);
1383 aio_context_acquire(aio_context);
1385 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1386 error_reportf_err(local_error,
1387 "Failed to delete snapshot with id '%s' and "
1388 "name '%s' on device '%s' in abort: ",
1389 sn->id_str, sn->name,
1390 bdrv_get_device_name(bs));
1393 aio_context_release(aio_context);
1396 static void internal_snapshot_clean(BlkActionState *common)
1398 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1399 common, common);
1400 AioContext *aio_context;
1402 if (!state->bs) {
1403 return;
1406 aio_context = bdrv_get_aio_context(state->bs);
1407 aio_context_acquire(aio_context);
1409 bdrv_drained_end(state->bs);
1411 aio_context_release(aio_context);
1414 /* external snapshot private data */
1415 typedef struct ExternalSnapshotState {
1416 BlkActionState common;
1417 BlockDriverState *old_bs;
1418 BlockDriverState *new_bs;
1419 bool overlay_appended;
1420 } ExternalSnapshotState;
1422 static void external_snapshot_prepare(BlkActionState *common,
1423 Error **errp)
1425 int flags = 0;
1426 QDict *options = NULL;
1427 Error *local_err = NULL;
1428 /* Device and node name of the image to generate the snapshot from */
1429 const char *device;
1430 const char *node_name;
1431 /* Reference to the new image (for 'blockdev-snapshot') */
1432 const char *snapshot_ref;
1433 /* File name of the new image (for 'blockdev-snapshot-sync') */
1434 const char *new_image_file;
1435 ExternalSnapshotState *state =
1436 DO_UPCAST(ExternalSnapshotState, common, common);
1437 TransactionAction *action = common->action;
1438 AioContext *aio_context;
1439 uint64_t perm, shared;
1441 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1442 * purpose but a different set of parameters */
1443 switch (action->type) {
1444 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1446 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
1447 device = s->node;
1448 node_name = s->node;
1449 new_image_file = NULL;
1450 snapshot_ref = s->overlay;
1452 break;
1453 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1455 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
1456 device = s->has_device ? s->device : NULL;
1457 node_name = s->has_node_name ? s->node_name : NULL;
1458 new_image_file = s->snapshot_file;
1459 snapshot_ref = NULL;
1461 break;
1462 default:
1463 g_assert_not_reached();
1466 /* start processing */
1467 if (action_check_completion_mode(common, errp) < 0) {
1468 return;
1471 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1472 if (!state->old_bs) {
1473 return;
1476 aio_context = bdrv_get_aio_context(state->old_bs);
1477 aio_context_acquire(aio_context);
1479 /* Paired with .clean() */
1480 bdrv_drained_begin(state->old_bs);
1482 if (!bdrv_is_inserted(state->old_bs)) {
1483 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1484 goto out;
1487 if (bdrv_op_is_blocked(state->old_bs,
1488 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1489 goto out;
1492 if (!bdrv_is_read_only(state->old_bs)) {
1493 if (bdrv_flush(state->old_bs)) {
1494 error_setg(errp, QERR_IO_ERROR);
1495 goto out;
1499 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
1500 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
1501 const char *format = s->has_format ? s->format : "qcow2";
1502 enum NewImageMode mode;
1503 const char *snapshot_node_name =
1504 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
1506 if (node_name && !snapshot_node_name) {
1507 error_setg(errp, "New overlay node name missing");
1508 goto out;
1511 if (snapshot_node_name &&
1512 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1513 error_setg(errp, "New overlay node name already in use");
1514 goto out;
1517 flags = state->old_bs->open_flags;
1518 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ);
1519 flags |= BDRV_O_NO_BACKING;
1521 /* create new image w/backing file */
1522 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1523 if (mode != NEW_IMAGE_MODE_EXISTING) {
1524 int64_t size = bdrv_getlength(state->old_bs);
1525 if (size < 0) {
1526 error_setg_errno(errp, -size, "bdrv_getlength failed");
1527 goto out;
1529 bdrv_refresh_filename(state->old_bs);
1530 bdrv_img_create(new_image_file, format,
1531 state->old_bs->filename,
1532 state->old_bs->drv->format_name,
1533 NULL, size, flags, false, &local_err);
1534 if (local_err) {
1535 error_propagate(errp, local_err);
1536 goto out;
1540 options = qdict_new();
1541 if (snapshot_node_name) {
1542 qdict_put_str(options, "node-name", snapshot_node_name);
1544 qdict_put_str(options, "driver", format);
1547 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1548 errp);
1549 /* We will manually add the backing_hd field to the bs later */
1550 if (!state->new_bs) {
1551 goto out;
1555 * Allow attaching a backing file to an overlay that's already in use only
1556 * if the parents don't assume that they are already seeing a valid image.
1557 * (Specifically, allow it as a mirror target, which is write-only access.)
1559 bdrv_get_cumulative_perm(state->new_bs, &perm, &shared);
1560 if (perm & BLK_PERM_CONSISTENT_READ) {
1561 error_setg(errp, "The overlay is already in use");
1562 goto out;
1565 if (state->new_bs->backing != NULL) {
1566 error_setg(errp, "The overlay already has a backing image");
1567 goto out;
1570 if (!state->new_bs->drv->supports_backing) {
1571 error_setg(errp, "The overlay does not support backing images");
1572 goto out;
1575 /* This removes our old bs and adds the new bs. This is an operation that
1576 * can fail, so we need to do it in .prepare; undoing it for abort is
1577 * always possible. */
1578 bdrv_ref(state->new_bs);
1579 bdrv_append(state->new_bs, state->old_bs, &local_err);
1580 if (local_err) {
1581 error_propagate(errp, local_err);
1582 goto out;
1584 state->overlay_appended = true;
1586 out:
1587 aio_context_release(aio_context);
1590 static void external_snapshot_commit(BlkActionState *common)
1592 ExternalSnapshotState *state =
1593 DO_UPCAST(ExternalSnapshotState, common, common);
1594 AioContext *aio_context;
1596 aio_context = bdrv_get_aio_context(state->old_bs);
1597 aio_context_acquire(aio_context);
1599 /* We don't need (or want) to use the transactional
1600 * bdrv_reopen_multiple() across all the entries at once, because we
1601 * don't want to abort all of them if one of them fails the reopen */
1602 if (!atomic_read(&state->old_bs->copy_on_read)) {
1603 bdrv_reopen_set_read_only(state->old_bs, true, NULL);
1606 aio_context_release(aio_context);
1609 static void external_snapshot_abort(BlkActionState *common)
1611 ExternalSnapshotState *state =
1612 DO_UPCAST(ExternalSnapshotState, common, common);
1613 if (state->new_bs) {
1614 if (state->overlay_appended) {
1615 AioContext *aio_context;
1616 AioContext *tmp_context;
1617 int ret;
1619 aio_context = bdrv_get_aio_context(state->old_bs);
1620 aio_context_acquire(aio_context);
1622 bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd()
1623 close state->old_bs; we need it */
1624 bdrv_set_backing_hd(state->new_bs, NULL, &error_abort);
1627 * The call to bdrv_set_backing_hd() above returns state->old_bs to
1628 * the main AioContext. As we're still going to be using it, return
1629 * it to the AioContext it was before.
1631 tmp_context = bdrv_get_aio_context(state->old_bs);
1632 if (aio_context != tmp_context) {
1633 aio_context_release(aio_context);
1634 aio_context_acquire(tmp_context);
1636 ret = bdrv_try_set_aio_context(state->old_bs,
1637 aio_context, NULL);
1638 assert(ret == 0);
1640 aio_context_release(tmp_context);
1641 aio_context_acquire(aio_context);
1644 bdrv_replace_node(state->new_bs, state->old_bs, &error_abort);
1645 bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */
1647 aio_context_release(aio_context);
1652 static void external_snapshot_clean(BlkActionState *common)
1654 ExternalSnapshotState *state =
1655 DO_UPCAST(ExternalSnapshotState, common, common);
1656 AioContext *aio_context;
1658 if (!state->old_bs) {
1659 return;
1662 aio_context = bdrv_get_aio_context(state->old_bs);
1663 aio_context_acquire(aio_context);
1665 bdrv_drained_end(state->old_bs);
1666 bdrv_unref(state->new_bs);
1668 aio_context_release(aio_context);
1671 typedef struct DriveBackupState {
1672 BlkActionState common;
1673 BlockDriverState *bs;
1674 BlockJob *job;
1675 } DriveBackupState;
1677 static BlockJob *do_backup_common(BackupCommon *backup,
1678 BlockDriverState *bs,
1679 BlockDriverState *target_bs,
1680 AioContext *aio_context,
1681 JobTxn *txn, Error **errp);
1683 static void drive_backup_prepare(BlkActionState *common, Error **errp)
1685 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1686 DriveBackup *backup;
1687 BlockDriverState *bs;
1688 BlockDriverState *target_bs;
1689 BlockDriverState *source = NULL;
1690 AioContext *aio_context;
1691 AioContext *old_context;
1692 QDict *options;
1693 Error *local_err = NULL;
1694 int flags;
1695 int64_t size;
1696 bool set_backing_hd = false;
1697 int ret;
1699 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1700 backup = common->action->u.drive_backup.data;
1702 if (!backup->has_mode) {
1703 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1706 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
1707 if (!bs) {
1708 return;
1711 if (!bs->drv) {
1712 error_setg(errp, "Device has no medium");
1713 return;
1716 aio_context = bdrv_get_aio_context(bs);
1717 aio_context_acquire(aio_context);
1719 /* Paired with .clean() */
1720 bdrv_drained_begin(bs);
1722 if (!backup->has_format) {
1723 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
1724 NULL : (char *) bs->drv->format_name;
1727 /* Early check to avoid creating target */
1728 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
1729 goto out;
1732 flags = bs->open_flags | BDRV_O_RDWR;
1735 * See if we have a backing HD we can use to create our new image
1736 * on top of.
1738 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
1739 source = backing_bs(bs);
1740 if (!source) {
1741 backup->sync = MIRROR_SYNC_MODE_FULL;
1744 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
1745 source = bs;
1746 flags |= BDRV_O_NO_BACKING;
1747 set_backing_hd = true;
1750 size = bdrv_getlength(bs);
1751 if (size < 0) {
1752 error_setg_errno(errp, -size, "bdrv_getlength failed");
1753 goto out;
1756 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
1757 assert(backup->format);
1758 if (source) {
1759 bdrv_refresh_filename(source);
1760 bdrv_img_create(backup->target, backup->format, source->filename,
1761 source->drv->format_name, NULL,
1762 size, flags, false, &local_err);
1763 } else {
1764 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
1765 size, flags, false, &local_err);
1769 if (local_err) {
1770 error_propagate(errp, local_err);
1771 goto out;
1774 options = qdict_new();
1775 qdict_put_str(options, "discard", "unmap");
1776 qdict_put_str(options, "detect-zeroes", "unmap");
1777 if (backup->format) {
1778 qdict_put_str(options, "driver", backup->format);
1781 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
1782 if (!target_bs) {
1783 goto out;
1786 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1787 old_context = bdrv_get_aio_context(target_bs);
1788 aio_context_release(aio_context);
1789 aio_context_acquire(old_context);
1791 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
1792 if (ret < 0) {
1793 bdrv_unref(target_bs);
1794 aio_context_release(old_context);
1795 return;
1798 aio_context_release(old_context);
1799 aio_context_acquire(aio_context);
1801 if (set_backing_hd) {
1802 bdrv_set_backing_hd(target_bs, source, &local_err);
1803 if (local_err) {
1804 goto unref;
1808 state->bs = bs;
1810 state->job = do_backup_common(qapi_DriveBackup_base(backup),
1811 bs, target_bs, aio_context,
1812 common->block_job_txn, errp);
1814 unref:
1815 bdrv_unref(target_bs);
1816 out:
1817 aio_context_release(aio_context);
1820 static void drive_backup_commit(BlkActionState *common)
1822 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1823 AioContext *aio_context;
1825 aio_context = bdrv_get_aio_context(state->bs);
1826 aio_context_acquire(aio_context);
1828 assert(state->job);
1829 job_start(&state->job->job);
1831 aio_context_release(aio_context);
1834 static void drive_backup_abort(BlkActionState *common)
1836 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1838 if (state->job) {
1839 AioContext *aio_context;
1841 aio_context = bdrv_get_aio_context(state->bs);
1842 aio_context_acquire(aio_context);
1844 job_cancel_sync(&state->job->job);
1846 aio_context_release(aio_context);
1850 static void drive_backup_clean(BlkActionState *common)
1852 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1853 AioContext *aio_context;
1855 if (!state->bs) {
1856 return;
1859 aio_context = bdrv_get_aio_context(state->bs);
1860 aio_context_acquire(aio_context);
1862 bdrv_drained_end(state->bs);
1864 aio_context_release(aio_context);
1867 typedef struct BlockdevBackupState {
1868 BlkActionState common;
1869 BlockDriverState *bs;
1870 BlockJob *job;
1871 } BlockdevBackupState;
1873 static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1875 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1876 BlockdevBackup *backup;
1877 BlockDriverState *bs;
1878 BlockDriverState *target_bs;
1879 AioContext *aio_context;
1880 AioContext *old_context;
1881 int ret;
1883 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1884 backup = common->action->u.blockdev_backup.data;
1886 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
1887 if (!bs) {
1888 return;
1891 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
1892 if (!target_bs) {
1893 return;
1896 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1897 aio_context = bdrv_get_aio_context(bs);
1898 old_context = bdrv_get_aio_context(target_bs);
1899 aio_context_acquire(old_context);
1901 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
1902 if (ret < 0) {
1903 aio_context_release(old_context);
1904 return;
1907 aio_context_release(old_context);
1908 aio_context_acquire(aio_context);
1909 state->bs = bs;
1911 /* Paired with .clean() */
1912 bdrv_drained_begin(state->bs);
1914 state->job = do_backup_common(qapi_BlockdevBackup_base(backup),
1915 bs, target_bs, aio_context,
1916 common->block_job_txn, errp);
1918 aio_context_release(aio_context);
1921 static void blockdev_backup_commit(BlkActionState *common)
1923 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1924 AioContext *aio_context;
1926 aio_context = bdrv_get_aio_context(state->bs);
1927 aio_context_acquire(aio_context);
1929 assert(state->job);
1930 job_start(&state->job->job);
1932 aio_context_release(aio_context);
1935 static void blockdev_backup_abort(BlkActionState *common)
1937 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1939 if (state->job) {
1940 AioContext *aio_context;
1942 aio_context = bdrv_get_aio_context(state->bs);
1943 aio_context_acquire(aio_context);
1945 job_cancel_sync(&state->job->job);
1947 aio_context_release(aio_context);
1951 static void blockdev_backup_clean(BlkActionState *common)
1953 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1954 AioContext *aio_context;
1956 if (!state->bs) {
1957 return;
1960 aio_context = bdrv_get_aio_context(state->bs);
1961 aio_context_acquire(aio_context);
1963 bdrv_drained_end(state->bs);
1965 aio_context_release(aio_context);
1968 typedef struct BlockDirtyBitmapState {
1969 BlkActionState common;
1970 BdrvDirtyBitmap *bitmap;
1971 BlockDriverState *bs;
1972 HBitmap *backup;
1973 bool prepared;
1974 bool was_enabled;
1975 } BlockDirtyBitmapState;
1977 static void block_dirty_bitmap_add_prepare(BlkActionState *common,
1978 Error **errp)
1980 Error *local_err = NULL;
1981 BlockDirtyBitmapAdd *action;
1982 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1983 common, common);
1985 if (action_check_completion_mode(common, errp) < 0) {
1986 return;
1989 action = common->action->u.block_dirty_bitmap_add.data;
1990 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1991 qmp_block_dirty_bitmap_add(action->node, action->name,
1992 action->has_granularity, action->granularity,
1993 action->has_persistent, action->persistent,
1994 action->has_disabled, action->disabled,
1995 &local_err);
1997 if (!local_err) {
1998 state->prepared = true;
1999 } else {
2000 error_propagate(errp, local_err);
2004 static void block_dirty_bitmap_add_abort(BlkActionState *common)
2006 BlockDirtyBitmapAdd *action;
2007 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2008 common, common);
2010 action = common->action->u.block_dirty_bitmap_add.data;
2011 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2012 * then the node reference and bitmap name must have been valid.
2014 if (state->prepared) {
2015 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2019 static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
2020 Error **errp)
2022 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2023 common, common);
2024 BlockDirtyBitmap *action;
2026 if (action_check_completion_mode(common, errp) < 0) {
2027 return;
2030 action = common->action->u.block_dirty_bitmap_clear.data;
2031 state->bitmap = block_dirty_bitmap_lookup(action->node,
2032 action->name,
2033 &state->bs,
2034 errp);
2035 if (!state->bitmap) {
2036 return;
2039 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) {
2040 return;
2043 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2046 static void block_dirty_bitmap_restore(BlkActionState *common)
2048 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2049 common, common);
2051 if (state->backup) {
2052 bdrv_restore_dirty_bitmap(state->bitmap, state->backup);
2056 static void block_dirty_bitmap_free_backup(BlkActionState *common)
2058 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2059 common, common);
2061 hbitmap_free(state->backup);
2064 static void block_dirty_bitmap_enable_prepare(BlkActionState *common,
2065 Error **errp)
2067 BlockDirtyBitmap *action;
2068 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2069 common, common);
2071 if (action_check_completion_mode(common, errp) < 0) {
2072 return;
2075 action = common->action->u.block_dirty_bitmap_enable.data;
2076 state->bitmap = block_dirty_bitmap_lookup(action->node,
2077 action->name,
2078 NULL,
2079 errp);
2080 if (!state->bitmap) {
2081 return;
2084 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
2085 return;
2088 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2089 bdrv_enable_dirty_bitmap(state->bitmap);
2092 static void block_dirty_bitmap_enable_abort(BlkActionState *common)
2094 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2095 common, common);
2097 if (!state->was_enabled) {
2098 bdrv_disable_dirty_bitmap(state->bitmap);
2102 static void block_dirty_bitmap_disable_prepare(BlkActionState *common,
2103 Error **errp)
2105 BlockDirtyBitmap *action;
2106 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2107 common, common);
2109 if (action_check_completion_mode(common, errp) < 0) {
2110 return;
2113 action = common->action->u.block_dirty_bitmap_disable.data;
2114 state->bitmap = block_dirty_bitmap_lookup(action->node,
2115 action->name,
2116 NULL,
2117 errp);
2118 if (!state->bitmap) {
2119 return;
2122 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
2123 return;
2126 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2127 bdrv_disable_dirty_bitmap(state->bitmap);
2130 static void block_dirty_bitmap_disable_abort(BlkActionState *common)
2132 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2133 common, common);
2135 if (state->was_enabled) {
2136 bdrv_enable_dirty_bitmap(state->bitmap);
2140 static void block_dirty_bitmap_merge_prepare(BlkActionState *common,
2141 Error **errp)
2143 BlockDirtyBitmapMerge *action;
2144 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2145 common, common);
2147 if (action_check_completion_mode(common, errp) < 0) {
2148 return;
2151 action = common->action->u.block_dirty_bitmap_merge.data;
2153 state->bitmap = block_dirty_bitmap_merge(action->node, action->target,
2154 action->bitmaps, &state->backup,
2155 errp);
2158 static void block_dirty_bitmap_remove_prepare(BlkActionState *common,
2159 Error **errp)
2161 BlockDirtyBitmap *action;
2162 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2163 common, common);
2165 if (action_check_completion_mode(common, errp) < 0) {
2166 return;
2169 action = common->action->u.block_dirty_bitmap_remove.data;
2171 state->bitmap = block_dirty_bitmap_remove(action->node, action->name,
2172 false, &state->bs, errp);
2173 if (state->bitmap) {
2174 bdrv_dirty_bitmap_skip_store(state->bitmap, true);
2175 bdrv_dirty_bitmap_set_busy(state->bitmap, true);
2179 static void block_dirty_bitmap_remove_abort(BlkActionState *common)
2181 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2182 common, common);
2184 if (state->bitmap) {
2185 bdrv_dirty_bitmap_skip_store(state->bitmap, false);
2186 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
2190 static void block_dirty_bitmap_remove_commit(BlkActionState *common)
2192 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2193 common, common);
2195 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
2196 bdrv_release_dirty_bitmap(state->bitmap);
2199 static void abort_prepare(BlkActionState *common, Error **errp)
2201 error_setg(errp, "Transaction aborted using Abort action");
2204 static void abort_commit(BlkActionState *common)
2206 g_assert_not_reached(); /* this action never succeeds */
2209 static const BlkActionOps actions[] = {
2210 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2211 .instance_size = sizeof(ExternalSnapshotState),
2212 .prepare = external_snapshot_prepare,
2213 .commit = external_snapshot_commit,
2214 .abort = external_snapshot_abort,
2215 .clean = external_snapshot_clean,
2217 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2218 .instance_size = sizeof(ExternalSnapshotState),
2219 .prepare = external_snapshot_prepare,
2220 .commit = external_snapshot_commit,
2221 .abort = external_snapshot_abort,
2222 .clean = external_snapshot_clean,
2224 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2225 .instance_size = sizeof(DriveBackupState),
2226 .prepare = drive_backup_prepare,
2227 .commit = drive_backup_commit,
2228 .abort = drive_backup_abort,
2229 .clean = drive_backup_clean,
2231 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2232 .instance_size = sizeof(BlockdevBackupState),
2233 .prepare = blockdev_backup_prepare,
2234 .commit = blockdev_backup_commit,
2235 .abort = blockdev_backup_abort,
2236 .clean = blockdev_backup_clean,
2238 [TRANSACTION_ACTION_KIND_ABORT] = {
2239 .instance_size = sizeof(BlkActionState),
2240 .prepare = abort_prepare,
2241 .commit = abort_commit,
2243 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2244 .instance_size = sizeof(InternalSnapshotState),
2245 .prepare = internal_snapshot_prepare,
2246 .abort = internal_snapshot_abort,
2247 .clean = internal_snapshot_clean,
2249 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2250 .instance_size = sizeof(BlockDirtyBitmapState),
2251 .prepare = block_dirty_bitmap_add_prepare,
2252 .abort = block_dirty_bitmap_add_abort,
2254 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2255 .instance_size = sizeof(BlockDirtyBitmapState),
2256 .prepare = block_dirty_bitmap_clear_prepare,
2257 .commit = block_dirty_bitmap_free_backup,
2258 .abort = block_dirty_bitmap_restore,
2260 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE] = {
2261 .instance_size = sizeof(BlockDirtyBitmapState),
2262 .prepare = block_dirty_bitmap_enable_prepare,
2263 .abort = block_dirty_bitmap_enable_abort,
2265 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE] = {
2266 .instance_size = sizeof(BlockDirtyBitmapState),
2267 .prepare = block_dirty_bitmap_disable_prepare,
2268 .abort = block_dirty_bitmap_disable_abort,
2270 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE] = {
2271 .instance_size = sizeof(BlockDirtyBitmapState),
2272 .prepare = block_dirty_bitmap_merge_prepare,
2273 .commit = block_dirty_bitmap_free_backup,
2274 .abort = block_dirty_bitmap_restore,
2276 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE] = {
2277 .instance_size = sizeof(BlockDirtyBitmapState),
2278 .prepare = block_dirty_bitmap_remove_prepare,
2279 .commit = block_dirty_bitmap_remove_commit,
2280 .abort = block_dirty_bitmap_remove_abort,
2282 /* Where are transactions for MIRROR, COMMIT and STREAM?
2283 * Although these blockjobs use transaction callbacks like the backup job,
2284 * these jobs do not necessarily adhere to transaction semantics.
2285 * These jobs may not fully undo all of their actions on abort, nor do they
2286 * necessarily work in transactions with more than one job in them.
2291 * Allocate a TransactionProperties structure if necessary, and fill
2292 * that structure with desired defaults if they are unset.
2294 static TransactionProperties *get_transaction_properties(
2295 TransactionProperties *props)
2297 if (!props) {
2298 props = g_new0(TransactionProperties, 1);
2301 if (!props->has_completion_mode) {
2302 props->has_completion_mode = true;
2303 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2306 return props;
2310 * 'Atomic' group operations. The operations are performed as a set, and if
2311 * any fail then we roll back all operations in the group.
2313 void qmp_transaction(TransactionActionList *dev_list,
2314 bool has_props,
2315 struct TransactionProperties *props,
2316 Error **errp)
2318 TransactionActionList *dev_entry = dev_list;
2319 JobTxn *block_job_txn = NULL;
2320 BlkActionState *state, *next;
2321 Error *local_err = NULL;
2323 QTAILQ_HEAD(, BlkActionState) snap_bdrv_states;
2324 QTAILQ_INIT(&snap_bdrv_states);
2326 /* Does this transaction get canceled as a group on failure?
2327 * If not, we don't really need to make a JobTxn.
2329 props = get_transaction_properties(props);
2330 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2331 block_job_txn = job_txn_new();
2334 /* drain all i/o before any operations */
2335 bdrv_drain_all();
2337 /* We don't do anything in this loop that commits us to the operations */
2338 while (NULL != dev_entry) {
2339 TransactionAction *dev_info = NULL;
2340 const BlkActionOps *ops;
2342 dev_info = dev_entry->value;
2343 dev_entry = dev_entry->next;
2345 assert(dev_info->type < ARRAY_SIZE(actions));
2347 ops = &actions[dev_info->type];
2348 assert(ops->instance_size > 0);
2350 state = g_malloc0(ops->instance_size);
2351 state->ops = ops;
2352 state->action = dev_info;
2353 state->block_job_txn = block_job_txn;
2354 state->txn_props = props;
2355 QTAILQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
2357 state->ops->prepare(state, &local_err);
2358 if (local_err) {
2359 error_propagate(errp, local_err);
2360 goto delete_and_fail;
2364 QTAILQ_FOREACH(state, &snap_bdrv_states, entry) {
2365 if (state->ops->commit) {
2366 state->ops->commit(state);
2370 /* success */
2371 goto exit;
2373 delete_and_fail:
2374 /* failure, and it is all-or-none; roll back all operations */
2375 QTAILQ_FOREACH_REVERSE(state, &snap_bdrv_states, entry) {
2376 if (state->ops->abort) {
2377 state->ops->abort(state);
2380 exit:
2381 QTAILQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2382 if (state->ops->clean) {
2383 state->ops->clean(state);
2385 g_free(state);
2387 if (!has_props) {
2388 qapi_free_TransactionProperties(props);
2390 job_txn_unref(block_job_txn);
2393 void qmp_block_passwd(bool has_device, const char *device,
2394 bool has_node_name, const char *node_name,
2395 const char *password, Error **errp)
2397 error_setg(errp,
2398 "Setting block passwords directly is no longer supported");
2401 BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
2402 const char *name,
2403 Error **errp)
2405 BdrvDirtyBitmap *bitmap;
2406 BlockDriverState *bs;
2407 BlockDirtyBitmapSha256 *ret = NULL;
2408 char *sha256;
2410 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
2411 if (!bitmap || !bs) {
2412 return NULL;
2415 sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp);
2416 if (sha256 == NULL) {
2417 return NULL;
2420 ret = g_new(BlockDirtyBitmapSha256, 1);
2421 ret->sha256 = sha256;
2423 return ret;
2426 void qmp_block_resize(bool has_device, const char *device,
2427 bool has_node_name, const char *node_name,
2428 int64_t size, Error **errp)
2430 Error *local_err = NULL;
2431 BlockBackend *blk = NULL;
2432 BlockDriverState *bs;
2433 AioContext *aio_context;
2435 bs = bdrv_lookup_bs(has_device ? device : NULL,
2436 has_node_name ? node_name : NULL,
2437 &local_err);
2438 if (local_err) {
2439 error_propagate(errp, local_err);
2440 return;
2443 aio_context = bdrv_get_aio_context(bs);
2444 aio_context_acquire(aio_context);
2446 if (size < 0) {
2447 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2448 goto out;
2451 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2452 error_setg(errp, QERR_DEVICE_IN_USE, device);
2453 goto out;
2456 blk = blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL, errp);
2457 if (!blk) {
2458 goto out;
2461 bdrv_drained_begin(bs);
2462 blk_truncate(blk, size, false, PREALLOC_MODE_OFF, 0, errp);
2463 bdrv_drained_end(bs);
2465 out:
2466 blk_unref(blk);
2467 aio_context_release(aio_context);
2470 void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
2471 bool has_base, const char *base,
2472 bool has_base_node, const char *base_node,
2473 bool has_backing_file, const char *backing_file,
2474 bool has_speed, int64_t speed,
2475 bool has_on_error, BlockdevOnError on_error,
2476 bool has_auto_finalize, bool auto_finalize,
2477 bool has_auto_dismiss, bool auto_dismiss,
2478 Error **errp)
2480 BlockDriverState *bs, *iter;
2481 BlockDriverState *base_bs = NULL;
2482 AioContext *aio_context;
2483 Error *local_err = NULL;
2484 const char *base_name = NULL;
2485 int job_flags = JOB_DEFAULT;
2487 if (!has_on_error) {
2488 on_error = BLOCKDEV_ON_ERROR_REPORT;
2491 bs = bdrv_lookup_bs(device, device, errp);
2492 if (!bs) {
2493 return;
2496 aio_context = bdrv_get_aio_context(bs);
2497 aio_context_acquire(aio_context);
2499 if (has_base && has_base_node) {
2500 error_setg(errp, "'base' and 'base-node' cannot be specified "
2501 "at the same time");
2502 goto out;
2505 if (has_base) {
2506 base_bs = bdrv_find_backing_image(bs, base);
2507 if (base_bs == NULL) {
2508 error_setg(errp, QERR_BASE_NOT_FOUND, base);
2509 goto out;
2511 assert(bdrv_get_aio_context(base_bs) == aio_context);
2512 base_name = base;
2515 if (has_base_node) {
2516 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2517 if (!base_bs) {
2518 goto out;
2520 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
2521 error_setg(errp, "Node '%s' is not a backing image of '%s'",
2522 base_node, device);
2523 goto out;
2525 assert(bdrv_get_aio_context(base_bs) == aio_context);
2526 bdrv_refresh_filename(base_bs);
2527 base_name = base_bs->filename;
2530 /* Check for op blockers in the whole chain between bs and base */
2531 for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) {
2532 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
2533 goto out;
2537 /* if we are streaming the entire chain, the result will have no backing
2538 * file, and specifying one is therefore an error */
2539 if (base_bs == NULL && has_backing_file) {
2540 error_setg(errp, "backing file specified, but streaming the "
2541 "entire chain");
2542 goto out;
2545 /* backing_file string overrides base bs filename */
2546 base_name = has_backing_file ? backing_file : base_name;
2548 if (has_auto_finalize && !auto_finalize) {
2549 job_flags |= JOB_MANUAL_FINALIZE;
2551 if (has_auto_dismiss && !auto_dismiss) {
2552 job_flags |= JOB_MANUAL_DISMISS;
2555 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
2556 job_flags, has_speed ? speed : 0, on_error, &local_err);
2557 if (local_err) {
2558 error_propagate(errp, local_err);
2559 goto out;
2562 trace_qmp_block_stream(bs);
2564 out:
2565 aio_context_release(aio_context);
2568 void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
2569 bool has_base_node, const char *base_node,
2570 bool has_base, const char *base,
2571 bool has_top_node, const char *top_node,
2572 bool has_top, const char *top,
2573 bool has_backing_file, const char *backing_file,
2574 bool has_speed, int64_t speed,
2575 bool has_on_error, BlockdevOnError on_error,
2576 bool has_filter_node_name, const char *filter_node_name,
2577 bool has_auto_finalize, bool auto_finalize,
2578 bool has_auto_dismiss, bool auto_dismiss,
2579 Error **errp)
2581 BlockDriverState *bs;
2582 BlockDriverState *iter;
2583 BlockDriverState *base_bs, *top_bs;
2584 AioContext *aio_context;
2585 Error *local_err = NULL;
2586 int job_flags = JOB_DEFAULT;
2588 if (!has_speed) {
2589 speed = 0;
2591 if (!has_on_error) {
2592 on_error = BLOCKDEV_ON_ERROR_REPORT;
2594 if (!has_filter_node_name) {
2595 filter_node_name = NULL;
2597 if (has_auto_finalize && !auto_finalize) {
2598 job_flags |= JOB_MANUAL_FINALIZE;
2600 if (has_auto_dismiss && !auto_dismiss) {
2601 job_flags |= JOB_MANUAL_DISMISS;
2604 /* Important Note:
2605 * libvirt relies on the DeviceNotFound error class in order to probe for
2606 * live commit feature versions; for this to work, we must make sure to
2607 * perform the device lookup before any generic errors that may occur in a
2608 * scenario in which all optional arguments are omitted. */
2609 bs = qmp_get_root_bs(device, &local_err);
2610 if (!bs) {
2611 bs = bdrv_lookup_bs(device, device, NULL);
2612 if (!bs) {
2613 error_free(local_err);
2614 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2615 "Device '%s' not found", device);
2616 } else {
2617 error_propagate(errp, local_err);
2619 return;
2622 aio_context = bdrv_get_aio_context(bs);
2623 aio_context_acquire(aio_context);
2625 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
2626 goto out;
2629 /* default top_bs is the active layer */
2630 top_bs = bs;
2632 if (has_top_node && has_top) {
2633 error_setg(errp, "'top-node' and 'top' are mutually exclusive");
2634 goto out;
2635 } else if (has_top_node) {
2636 top_bs = bdrv_lookup_bs(NULL, top_node, errp);
2637 if (top_bs == NULL) {
2638 goto out;
2640 if (!bdrv_chain_contains(bs, top_bs)) {
2641 error_setg(errp, "'%s' is not in this backing file chain",
2642 top_node);
2643 goto out;
2645 } else if (has_top && top) {
2646 /* This strcmp() is just a shortcut, there is no need to
2647 * refresh @bs's filename. If it mismatches,
2648 * bdrv_find_backing_image() will do the refresh and may still
2649 * return @bs. */
2650 if (strcmp(bs->filename, top) != 0) {
2651 top_bs = bdrv_find_backing_image(bs, top);
2655 if (top_bs == NULL) {
2656 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
2657 goto out;
2660 assert(bdrv_get_aio_context(top_bs) == aio_context);
2662 if (has_base_node && has_base) {
2663 error_setg(errp, "'base-node' and 'base' are mutually exclusive");
2664 goto out;
2665 } else if (has_base_node) {
2666 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2667 if (base_bs == NULL) {
2668 goto out;
2670 if (!bdrv_chain_contains(top_bs, base_bs)) {
2671 error_setg(errp, "'%s' is not in this backing file chain",
2672 base_node);
2673 goto out;
2675 } else if (has_base && base) {
2676 base_bs = bdrv_find_backing_image(top_bs, base);
2677 } else {
2678 base_bs = bdrv_find_base(top_bs);
2681 if (base_bs == NULL) {
2682 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
2683 goto out;
2686 assert(bdrv_get_aio_context(base_bs) == aio_context);
2688 for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) {
2689 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2690 goto out;
2694 /* Do not allow attempts to commit an image into itself */
2695 if (top_bs == base_bs) {
2696 error_setg(errp, "cannot commit an image into itself");
2697 goto out;
2700 if (top_bs == bs) {
2701 if (has_backing_file) {
2702 error_setg(errp, "'backing-file' specified,"
2703 " but 'top' is the active layer");
2704 goto out;
2706 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs,
2707 job_flags, speed, on_error,
2708 filter_node_name, NULL, NULL, false, &local_err);
2709 } else {
2710 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
2711 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2712 goto out;
2714 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, job_flags,
2715 speed, on_error, has_backing_file ? backing_file : NULL,
2716 filter_node_name, &local_err);
2718 if (local_err != NULL) {
2719 error_propagate(errp, local_err);
2720 goto out;
2723 out:
2724 aio_context_release(aio_context);
2727 /* Common QMP interface for drive-backup and blockdev-backup */
2728 static BlockJob *do_backup_common(BackupCommon *backup,
2729 BlockDriverState *bs,
2730 BlockDriverState *target_bs,
2731 AioContext *aio_context,
2732 JobTxn *txn, Error **errp)
2734 BlockJob *job = NULL;
2735 BdrvDirtyBitmap *bmap = NULL;
2736 int job_flags = JOB_DEFAULT;
2738 if (!backup->has_speed) {
2739 backup->speed = 0;
2741 if (!backup->has_on_source_error) {
2742 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2744 if (!backup->has_on_target_error) {
2745 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2747 if (!backup->has_job_id) {
2748 backup->job_id = NULL;
2750 if (!backup->has_auto_finalize) {
2751 backup->auto_finalize = true;
2753 if (!backup->has_auto_dismiss) {
2754 backup->auto_dismiss = true;
2756 if (!backup->has_compress) {
2757 backup->compress = false;
2760 if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) ||
2761 (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) {
2762 /* done before desugaring 'incremental' to print the right message */
2763 if (!backup->has_bitmap) {
2764 error_setg(errp, "must provide a valid bitmap name for "
2765 "'%s' sync mode", MirrorSyncMode_str(backup->sync));
2766 return NULL;
2770 if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) {
2771 if (backup->has_bitmap_mode &&
2772 backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) {
2773 error_setg(errp, "Bitmap sync mode must be '%s' "
2774 "when using sync mode '%s'",
2775 BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS),
2776 MirrorSyncMode_str(backup->sync));
2777 return NULL;
2779 backup->has_bitmap_mode = true;
2780 backup->sync = MIRROR_SYNC_MODE_BITMAP;
2781 backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS;
2784 if (backup->has_bitmap) {
2785 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
2786 if (!bmap) {
2787 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
2788 return NULL;
2790 if (!backup->has_bitmap_mode) {
2791 error_setg(errp, "Bitmap sync mode must be given "
2792 "when providing a bitmap");
2793 return NULL;
2795 if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) {
2796 return NULL;
2799 /* This does not produce a useful bitmap artifact: */
2800 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
2801 error_setg(errp, "sync mode '%s' does not produce meaningful bitmap"
2802 " outputs", MirrorSyncMode_str(backup->sync));
2803 return NULL;
2806 /* If the bitmap isn't used for input or output, this is useless: */
2807 if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER &&
2808 backup->sync != MIRROR_SYNC_MODE_BITMAP) {
2809 error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect"
2810 " when combined with sync mode '%s'",
2811 BitmapSyncMode_str(backup->bitmap_mode),
2812 MirrorSyncMode_str(backup->sync));
2813 return NULL;
2817 if (!backup->has_bitmap && backup->has_bitmap_mode) {
2818 error_setg(errp, "Cannot specify bitmap sync mode without a bitmap");
2819 return NULL;
2822 if (!backup->auto_finalize) {
2823 job_flags |= JOB_MANUAL_FINALIZE;
2825 if (!backup->auto_dismiss) {
2826 job_flags |= JOB_MANUAL_DISMISS;
2829 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
2830 backup->sync, bmap, backup->bitmap_mode,
2831 backup->compress,
2832 backup->filter_node_name,
2833 backup->on_source_error,
2834 backup->on_target_error,
2835 job_flags, NULL, NULL, txn, errp);
2836 return job;
2839 void qmp_drive_backup(DriveBackup *backup, Error **errp)
2841 TransactionAction action = {
2842 .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP,
2843 .u.drive_backup.data = backup,
2845 blockdev_do_action(&action, errp);
2848 BlockDeviceInfoList *qmp_query_named_block_nodes(bool has_flat,
2849 bool flat,
2850 Error **errp)
2852 bool return_flat = has_flat && flat;
2854 return bdrv_named_nodes_list(return_flat, errp);
2857 XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp)
2859 return bdrv_get_xdbg_block_graph(errp);
2862 void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp)
2864 TransactionAction action = {
2865 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP,
2866 .u.blockdev_backup.data = backup,
2868 blockdev_do_action(&action, errp);
2871 /* Parameter check and block job starting for drive mirroring.
2872 * Caller should hold @device and @target's aio context (must be the same).
2874 static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
2875 BlockDriverState *target,
2876 bool has_replaces, const char *replaces,
2877 enum MirrorSyncMode sync,
2878 BlockMirrorBackingMode backing_mode,
2879 bool zero_target,
2880 bool has_speed, int64_t speed,
2881 bool has_granularity, uint32_t granularity,
2882 bool has_buf_size, int64_t buf_size,
2883 bool has_on_source_error,
2884 BlockdevOnError on_source_error,
2885 bool has_on_target_error,
2886 BlockdevOnError on_target_error,
2887 bool has_unmap, bool unmap,
2888 bool has_filter_node_name,
2889 const char *filter_node_name,
2890 bool has_copy_mode, MirrorCopyMode copy_mode,
2891 bool has_auto_finalize, bool auto_finalize,
2892 bool has_auto_dismiss, bool auto_dismiss,
2893 Error **errp)
2895 int job_flags = JOB_DEFAULT;
2897 if (!has_speed) {
2898 speed = 0;
2900 if (!has_on_source_error) {
2901 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2903 if (!has_on_target_error) {
2904 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2906 if (!has_granularity) {
2907 granularity = 0;
2909 if (!has_buf_size) {
2910 buf_size = 0;
2912 if (!has_unmap) {
2913 unmap = true;
2915 if (!has_filter_node_name) {
2916 filter_node_name = NULL;
2918 if (!has_copy_mode) {
2919 copy_mode = MIRROR_COPY_MODE_BACKGROUND;
2921 if (has_auto_finalize && !auto_finalize) {
2922 job_flags |= JOB_MANUAL_FINALIZE;
2924 if (has_auto_dismiss && !auto_dismiss) {
2925 job_flags |= JOB_MANUAL_DISMISS;
2928 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
2929 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2930 "a value in range [512B, 64MB]");
2931 return;
2933 if (granularity & (granularity - 1)) {
2934 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2935 "power of 2");
2936 return;
2939 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
2940 return;
2942 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
2943 return;
2946 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
2947 sync = MIRROR_SYNC_MODE_FULL;
2950 if (has_replaces) {
2951 BlockDriverState *to_replace_bs;
2952 AioContext *replace_aio_context;
2953 int64_t bs_size, replace_size;
2955 bs_size = bdrv_getlength(bs);
2956 if (bs_size < 0) {
2957 error_setg_errno(errp, -bs_size, "Failed to query device's size");
2958 return;
2961 to_replace_bs = check_to_replace_node(bs, replaces, errp);
2962 if (!to_replace_bs) {
2963 return;
2966 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
2967 aio_context_acquire(replace_aio_context);
2968 replace_size = bdrv_getlength(to_replace_bs);
2969 aio_context_release(replace_aio_context);
2971 if (replace_size < 0) {
2972 error_setg_errno(errp, -replace_size,
2973 "Failed to query the replacement node's size");
2974 return;
2976 if (bs_size != replace_size) {
2977 error_setg(errp, "cannot replace image with a mirror image of "
2978 "different size");
2979 return;
2983 /* pass the node name to replace to mirror start since it's loose coupling
2984 * and will allow to check whether the node still exist at mirror completion
2986 mirror_start(job_id, bs, target,
2987 has_replaces ? replaces : NULL, job_flags,
2988 speed, granularity, buf_size, sync, backing_mode, zero_target,
2989 on_source_error, on_target_error, unmap, filter_node_name,
2990 copy_mode, errp);
2993 void qmp_drive_mirror(DriveMirror *arg, Error **errp)
2995 BlockDriverState *bs;
2996 BlockDriverState *source, *target_bs;
2997 AioContext *aio_context;
2998 AioContext *old_context;
2999 BlockMirrorBackingMode backing_mode;
3000 Error *local_err = NULL;
3001 QDict *options = NULL;
3002 int flags;
3003 int64_t size;
3004 const char *format = arg->format;
3005 bool zero_target;
3006 int ret;
3008 bs = qmp_get_root_bs(arg->device, errp);
3009 if (!bs) {
3010 return;
3013 /* Early check to avoid creating target */
3014 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3015 return;
3018 aio_context = bdrv_get_aio_context(bs);
3019 aio_context_acquire(aio_context);
3021 if (!arg->has_mode) {
3022 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3025 if (!arg->has_format) {
3026 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3027 ? NULL : bs->drv->format_name);
3030 flags = bs->open_flags | BDRV_O_RDWR;
3031 source = backing_bs(bs);
3032 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3033 arg->sync = MIRROR_SYNC_MODE_FULL;
3035 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
3036 source = bs;
3039 size = bdrv_getlength(bs);
3040 if (size < 0) {
3041 error_setg_errno(errp, -size, "bdrv_getlength failed");
3042 goto out;
3045 if (arg->has_replaces) {
3046 if (!arg->has_node_name) {
3047 error_setg(errp, "a node-name must be provided when replacing a"
3048 " named node of the graph");
3049 goto out;
3053 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
3054 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3055 } else {
3056 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3059 /* Don't open backing image in create() */
3060 flags |= BDRV_O_NO_BACKING;
3062 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3063 && arg->mode != NEW_IMAGE_MODE_EXISTING)
3065 /* create new image w/o backing file */
3066 assert(format);
3067 bdrv_img_create(arg->target, format,
3068 NULL, NULL, NULL, size, flags, false, &local_err);
3069 } else {
3070 switch (arg->mode) {
3071 case NEW_IMAGE_MODE_EXISTING:
3072 break;
3073 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3074 /* create new image with backing file */
3075 bdrv_refresh_filename(source);
3076 bdrv_img_create(arg->target, format,
3077 source->filename,
3078 source->drv->format_name,
3079 NULL, size, flags, false, &local_err);
3080 break;
3081 default:
3082 abort();
3086 if (local_err) {
3087 error_propagate(errp, local_err);
3088 goto out;
3091 options = qdict_new();
3092 if (arg->has_node_name) {
3093 qdict_put_str(options, "node-name", arg->node_name);
3095 if (format) {
3096 qdict_put_str(options, "driver", format);
3099 /* Mirroring takes care of copy-on-write using the source's backing
3100 * file.
3102 target_bs = bdrv_open(arg->target, NULL, options, flags, errp);
3103 if (!target_bs) {
3104 goto out;
3107 zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL &&
3108 (arg->mode == NEW_IMAGE_MODE_EXISTING ||
3109 !bdrv_has_zero_init(target_bs)));
3112 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3113 old_context = bdrv_get_aio_context(target_bs);
3114 aio_context_release(aio_context);
3115 aio_context_acquire(old_context);
3117 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
3118 if (ret < 0) {
3119 bdrv_unref(target_bs);
3120 aio_context_release(old_context);
3121 return;
3124 aio_context_release(old_context);
3125 aio_context_acquire(aio_context);
3127 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3128 arg->has_replaces, arg->replaces, arg->sync,
3129 backing_mode, zero_target,
3130 arg->has_speed, arg->speed,
3131 arg->has_granularity, arg->granularity,
3132 arg->has_buf_size, arg->buf_size,
3133 arg->has_on_source_error, arg->on_source_error,
3134 arg->has_on_target_error, arg->on_target_error,
3135 arg->has_unmap, arg->unmap,
3136 false, NULL,
3137 arg->has_copy_mode, arg->copy_mode,
3138 arg->has_auto_finalize, arg->auto_finalize,
3139 arg->has_auto_dismiss, arg->auto_dismiss,
3140 errp);
3141 bdrv_unref(target_bs);
3142 out:
3143 aio_context_release(aio_context);
3146 void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3147 const char *device, const char *target,
3148 bool has_replaces, const char *replaces,
3149 MirrorSyncMode sync,
3150 bool has_speed, int64_t speed,
3151 bool has_granularity, uint32_t granularity,
3152 bool has_buf_size, int64_t buf_size,
3153 bool has_on_source_error,
3154 BlockdevOnError on_source_error,
3155 bool has_on_target_error,
3156 BlockdevOnError on_target_error,
3157 bool has_filter_node_name,
3158 const char *filter_node_name,
3159 bool has_copy_mode, MirrorCopyMode copy_mode,
3160 bool has_auto_finalize, bool auto_finalize,
3161 bool has_auto_dismiss, bool auto_dismiss,
3162 Error **errp)
3164 BlockDriverState *bs;
3165 BlockDriverState *target_bs;
3166 AioContext *aio_context;
3167 AioContext *old_context;
3168 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
3169 bool zero_target;
3170 int ret;
3172 bs = qmp_get_root_bs(device, errp);
3173 if (!bs) {
3174 return;
3177 target_bs = bdrv_lookup_bs(target, target, errp);
3178 if (!target_bs) {
3179 return;
3182 zero_target = (sync == MIRROR_SYNC_MODE_FULL);
3184 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3185 old_context = bdrv_get_aio_context(target_bs);
3186 aio_context = bdrv_get_aio_context(bs);
3187 aio_context_acquire(old_context);
3189 ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
3191 aio_context_release(old_context);
3192 aio_context_acquire(aio_context);
3194 if (ret < 0) {
3195 goto out;
3198 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
3199 has_replaces, replaces, sync, backing_mode,
3200 zero_target, has_speed, speed,
3201 has_granularity, granularity,
3202 has_buf_size, buf_size,
3203 has_on_source_error, on_source_error,
3204 has_on_target_error, on_target_error,
3205 true, true,
3206 has_filter_node_name, filter_node_name,
3207 has_copy_mode, copy_mode,
3208 has_auto_finalize, auto_finalize,
3209 has_auto_dismiss, auto_dismiss,
3210 errp);
3211 out:
3212 aio_context_release(aio_context);
3215 /* Get a block job using its ID and acquire its AioContext */
3216 static BlockJob *find_block_job(const char *id, AioContext **aio_context,
3217 Error **errp)
3219 BlockJob *job;
3221 assert(id != NULL);
3223 *aio_context = NULL;
3225 job = block_job_get(id);
3227 if (!job) {
3228 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3229 "Block job '%s' not found", id);
3230 return NULL;
3233 *aio_context = blk_get_aio_context(job->blk);
3234 aio_context_acquire(*aio_context);
3236 return job;
3239 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
3241 AioContext *aio_context;
3242 BlockJob *job = find_block_job(device, &aio_context, errp);
3244 if (!job) {
3245 return;
3248 block_job_set_speed(job, speed, errp);
3249 aio_context_release(aio_context);
3252 void qmp_block_job_cancel(const char *device,
3253 bool has_force, bool force, Error **errp)
3255 AioContext *aio_context;
3256 BlockJob *job = find_block_job(device, &aio_context, errp);
3258 if (!job) {
3259 return;
3262 if (!has_force) {
3263 force = false;
3266 if (job_user_paused(&job->job) && !force) {
3267 error_setg(errp, "The block job for device '%s' is currently paused",
3268 device);
3269 goto out;
3272 trace_qmp_block_job_cancel(job);
3273 job_user_cancel(&job->job, force, errp);
3274 out:
3275 aio_context_release(aio_context);
3278 void qmp_block_job_pause(const char *device, Error **errp)
3280 AioContext *aio_context;
3281 BlockJob *job = find_block_job(device, &aio_context, errp);
3283 if (!job) {
3284 return;
3287 trace_qmp_block_job_pause(job);
3288 job_user_pause(&job->job, errp);
3289 aio_context_release(aio_context);
3292 void qmp_block_job_resume(const char *device, Error **errp)
3294 AioContext *aio_context;
3295 BlockJob *job = find_block_job(device, &aio_context, errp);
3297 if (!job) {
3298 return;
3301 trace_qmp_block_job_resume(job);
3302 job_user_resume(&job->job, errp);
3303 aio_context_release(aio_context);
3306 void qmp_block_job_complete(const char *device, Error **errp)
3308 AioContext *aio_context;
3309 BlockJob *job = find_block_job(device, &aio_context, errp);
3311 if (!job) {
3312 return;
3315 trace_qmp_block_job_complete(job);
3316 job_complete(&job->job, errp);
3317 aio_context_release(aio_context);
3320 void qmp_block_job_finalize(const char *id, Error **errp)
3322 AioContext *aio_context;
3323 BlockJob *job = find_block_job(id, &aio_context, errp);
3325 if (!job) {
3326 return;
3329 trace_qmp_block_job_finalize(job);
3330 job_ref(&job->job);
3331 job_finalize(&job->job, errp);
3334 * Job's context might have changed via job_finalize (and job_txn_apply
3335 * automatically acquires the new one), so make sure we release the correct
3336 * one.
3338 aio_context = blk_get_aio_context(job->blk);
3339 job_unref(&job->job);
3340 aio_context_release(aio_context);
3343 void qmp_block_job_dismiss(const char *id, Error **errp)
3345 AioContext *aio_context;
3346 BlockJob *bjob = find_block_job(id, &aio_context, errp);
3347 Job *job;
3349 if (!bjob) {
3350 return;
3353 trace_qmp_block_job_dismiss(bjob);
3354 job = &bjob->job;
3355 job_dismiss(&job, errp);
3356 aio_context_release(aio_context);
3359 void qmp_change_backing_file(const char *device,
3360 const char *image_node_name,
3361 const char *backing_file,
3362 Error **errp)
3364 BlockDriverState *bs = NULL;
3365 AioContext *aio_context;
3366 BlockDriverState *image_bs = NULL;
3367 Error *local_err = NULL;
3368 bool ro;
3369 int ret;
3371 bs = qmp_get_root_bs(device, errp);
3372 if (!bs) {
3373 return;
3376 aio_context = bdrv_get_aio_context(bs);
3377 aio_context_acquire(aio_context);
3379 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3380 if (local_err) {
3381 error_propagate(errp, local_err);
3382 goto out;
3385 if (!image_bs) {
3386 error_setg(errp, "image file not found");
3387 goto out;
3390 if (bdrv_find_base(image_bs) == image_bs) {
3391 error_setg(errp, "not allowing backing file change on an image "
3392 "without a backing file");
3393 goto out;
3396 /* even though we are not necessarily operating on bs, we need it to
3397 * determine if block ops are currently prohibited on the chain */
3398 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3399 goto out;
3402 /* final sanity check */
3403 if (!bdrv_chain_contains(bs, image_bs)) {
3404 error_setg(errp, "'%s' and image file are not in the same chain",
3405 device);
3406 goto out;
3409 /* if not r/w, reopen to make r/w */
3410 ro = bdrv_is_read_only(image_bs);
3412 if (ro) {
3413 if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) {
3414 goto out;
3418 ret = bdrv_change_backing_file(image_bs, backing_file,
3419 image_bs->drv ? image_bs->drv->format_name : "",
3420 false);
3422 if (ret < 0) {
3423 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3424 backing_file);
3425 /* don't exit here, so we can try to restore open flags if
3426 * appropriate */
3429 if (ro) {
3430 bdrv_reopen_set_read_only(image_bs, true, errp);
3433 out:
3434 aio_context_release(aio_context);
3437 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3439 BlockDriverState *bs;
3440 QObject *obj;
3441 Visitor *v = qobject_output_visitor_new(&obj);
3442 QDict *qdict;
3444 visit_type_BlockdevOptions(v, NULL, &options, &error_abort);
3445 visit_complete(v, &obj);
3446 qdict = qobject_to(QDict, obj);
3448 qdict_flatten(qdict);
3450 if (!qdict_get_try_str(qdict, "node-name")) {
3451 error_setg(errp, "'node-name' must be specified for the root node");
3452 goto fail;
3455 bs = bds_tree_init(qdict, errp);
3456 if (!bs) {
3457 goto fail;
3460 bdrv_set_monitor_owned(bs);
3462 fail:
3463 visit_free(v);
3466 void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp)
3468 BlockDriverState *bs;
3469 AioContext *ctx;
3470 QObject *obj;
3471 Visitor *v = qobject_output_visitor_new(&obj);
3472 BlockReopenQueue *queue;
3473 QDict *qdict;
3475 /* Check for the selected node name */
3476 if (!options->has_node_name) {
3477 error_setg(errp, "Node name not specified");
3478 goto fail;
3481 bs = bdrv_find_node(options->node_name);
3482 if (!bs) {
3483 error_setg(errp, "Cannot find node named '%s'", options->node_name);
3484 goto fail;
3487 /* Put all options in a QDict and flatten it */
3488 visit_type_BlockdevOptions(v, NULL, &options, &error_abort);
3489 visit_complete(v, &obj);
3490 qdict = qobject_to(QDict, obj);
3492 qdict_flatten(qdict);
3494 /* Perform the reopen operation */
3495 ctx = bdrv_get_aio_context(bs);
3496 aio_context_acquire(ctx);
3497 bdrv_subtree_drained_begin(bs);
3498 queue = bdrv_reopen_queue(NULL, bs, qdict, false);
3499 bdrv_reopen_multiple(queue, errp);
3500 bdrv_subtree_drained_end(bs);
3501 aio_context_release(ctx);
3503 fail:
3504 visit_free(v);
3507 void qmp_blockdev_del(const char *node_name, Error **errp)
3509 AioContext *aio_context;
3510 BlockDriverState *bs;
3512 bs = bdrv_find_node(node_name);
3513 if (!bs) {
3514 error_setg(errp, "Cannot find node %s", node_name);
3515 return;
3517 if (bdrv_has_blk(bs)) {
3518 error_setg(errp, "Node %s is in use", node_name);
3519 return;
3521 aio_context = bdrv_get_aio_context(bs);
3522 aio_context_acquire(aio_context);
3524 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3525 goto out;
3528 if (!QTAILQ_IN_USE(bs, monitor_list)) {
3529 error_setg(errp, "Node %s is not owned by the monitor",
3530 bs->node_name);
3531 goto out;
3534 if (bs->refcnt > 1) {
3535 error_setg(errp, "Block device %s is in use",
3536 bdrv_get_device_or_node_name(bs));
3537 goto out;
3540 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3541 bdrv_unref(bs);
3543 out:
3544 aio_context_release(aio_context);
3547 static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
3548 const char *child_name)
3550 BdrvChild *child;
3552 QLIST_FOREACH(child, &parent_bs->children, next) {
3553 if (strcmp(child->name, child_name) == 0) {
3554 return child;
3558 return NULL;
3561 void qmp_x_blockdev_change(const char *parent, bool has_child,
3562 const char *child, bool has_node,
3563 const char *node, Error **errp)
3565 BlockDriverState *parent_bs, *new_bs = NULL;
3566 BdrvChild *p_child;
3568 parent_bs = bdrv_lookup_bs(parent, parent, errp);
3569 if (!parent_bs) {
3570 return;
3573 if (has_child == has_node) {
3574 if (has_child) {
3575 error_setg(errp, "The parameters child and node are in conflict");
3576 } else {
3577 error_setg(errp, "Either child or node must be specified");
3579 return;
3582 if (has_child) {
3583 p_child = bdrv_find_child(parent_bs, child);
3584 if (!p_child) {
3585 error_setg(errp, "Node '%s' does not have child '%s'",
3586 parent, child);
3587 return;
3589 bdrv_del_child(parent_bs, p_child, errp);
3592 if (has_node) {
3593 new_bs = bdrv_find_node(node);
3594 if (!new_bs) {
3595 error_setg(errp, "Node '%s' not found", node);
3596 return;
3598 bdrv_add_child(parent_bs, new_bs, errp);
3602 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3604 BlockJobInfoList *head = NULL, **p_next = &head;
3605 BlockJob *job;
3607 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
3608 BlockJobInfoList *elem;
3609 AioContext *aio_context;
3611 if (block_job_is_internal(job)) {
3612 continue;
3614 elem = g_new0(BlockJobInfoList, 1);
3615 aio_context = blk_get_aio_context(job->blk);
3616 aio_context_acquire(aio_context);
3617 elem->value = block_job_query(job, errp);
3618 aio_context_release(aio_context);
3619 if (!elem->value) {
3620 g_free(elem);
3621 qapi_free_BlockJobInfoList(head);
3622 return NULL;
3624 *p_next = elem;
3625 p_next = &elem->next;
3628 return head;
3631 void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
3632 bool has_force, bool force, Error **errp)
3634 AioContext *old_context;
3635 AioContext *new_context;
3636 BlockDriverState *bs;
3638 bs = bdrv_find_node(node_name);
3639 if (!bs) {
3640 error_setg(errp, "Cannot find node %s", node_name);
3641 return;
3644 /* Protects against accidents. */
3645 if (!(has_force && force) && bdrv_has_blk(bs)) {
3646 error_setg(errp, "Node %s is associated with a BlockBackend and could "
3647 "be in use (use force=true to override this check)",
3648 node_name);
3649 return;
3652 if (iothread->type == QTYPE_QSTRING) {
3653 IOThread *obj = iothread_by_id(iothread->u.s);
3654 if (!obj) {
3655 error_setg(errp, "Cannot find iothread %s", iothread->u.s);
3656 return;
3659 new_context = iothread_get_aio_context(obj);
3660 } else {
3661 new_context = qemu_get_aio_context();
3664 old_context = bdrv_get_aio_context(bs);
3665 aio_context_acquire(old_context);
3667 bdrv_try_set_aio_context(bs, new_context, errp);
3669 aio_context_release(old_context);
3672 QemuOptsList qemu_common_drive_opts = {
3673 .name = "drive",
3674 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3675 .desc = {
3677 .name = "snapshot",
3678 .type = QEMU_OPT_BOOL,
3679 .help = "enable/disable snapshot mode",
3681 .name = "aio",
3682 .type = QEMU_OPT_STRING,
3683 .help = "host AIO implementation (threads, native, io_uring)",
3685 .name = BDRV_OPT_CACHE_WB,
3686 .type = QEMU_OPT_BOOL,
3687 .help = "Enable writeback mode",
3689 .name = "format",
3690 .type = QEMU_OPT_STRING,
3691 .help = "disk format (raw, qcow2, ...)",
3693 .name = "rerror",
3694 .type = QEMU_OPT_STRING,
3695 .help = "read error action",
3697 .name = "werror",
3698 .type = QEMU_OPT_STRING,
3699 .help = "write error action",
3701 .name = BDRV_OPT_READ_ONLY,
3702 .type = QEMU_OPT_BOOL,
3703 .help = "open drive file as read-only",
3706 THROTTLE_OPTS,
3709 .name = "throttling.group",
3710 .type = QEMU_OPT_STRING,
3711 .help = "name of the block throttling group",
3713 .name = "copy-on-read",
3714 .type = QEMU_OPT_BOOL,
3715 .help = "copy read data from backing file into image file",
3717 .name = "detect-zeroes",
3718 .type = QEMU_OPT_STRING,
3719 .help = "try to optimize zero writes (off, on, unmap)",
3721 .name = "stats-account-invalid",
3722 .type = QEMU_OPT_BOOL,
3723 .help = "whether to account for invalid I/O operations "
3724 "in the statistics",
3726 .name = "stats-account-failed",
3727 .type = QEMU_OPT_BOOL,
3728 .help = "whether to account for failed I/O operations "
3729 "in the statistics",
3731 { /* end of list */ }
3735 QemuOptsList qemu_drive_opts = {
3736 .name = "drive",
3737 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3738 .desc = {
3740 * no elements => accept any params
3741 * validation will happen later
3743 { /* end of list */ }