qga/qapi-schema: Reformat doc comments to conform to current conventions
[qemu/ar7.git] / blockdev.c
blobd7b5c18f0af513a23b734deb02ebcb53b1742979
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/dirty-bitmap.h"
39 #include "block/qdict.h"
40 #include "block/throttle-groups.h"
41 #include "monitor/monitor.h"
42 #include "qemu/error-report.h"
43 #include "qemu/option.h"
44 #include "qemu/qemu-print.h"
45 #include "qemu/config-file.h"
46 #include "qapi/qapi-commands-block.h"
47 #include "qapi/qapi-commands-transaction.h"
48 #include "qapi/qapi-visit-block-core.h"
49 #include "qapi/qmp/qdict.h"
50 #include "qapi/qmp/qnum.h"
51 #include "qapi/qmp/qstring.h"
52 #include "qapi/error.h"
53 #include "qapi/qmp/qerror.h"
54 #include "qapi/qmp/qlist.h"
55 #include "qapi/qobject-output-visitor.h"
56 #include "sysemu/sysemu.h"
57 #include "sysemu/iothread.h"
58 #include "block/block_int.h"
59 #include "block/trace.h"
60 #include "sysemu/runstate.h"
61 #include "sysemu/replay.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 /* Protected by BQL */
68 QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
69 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
71 void bdrv_set_monitor_owned(BlockDriverState *bs)
73 GLOBAL_STATE_CODE();
74 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
77 static const char *const if_name[IF_COUNT] = {
78 [IF_NONE] = "none",
79 [IF_IDE] = "ide",
80 [IF_SCSI] = "scsi",
81 [IF_FLOPPY] = "floppy",
82 [IF_PFLASH] = "pflash",
83 [IF_MTD] = "mtd",
84 [IF_SD] = "sd",
85 [IF_VIRTIO] = "virtio",
86 [IF_XEN] = "xen",
89 static int if_max_devs[IF_COUNT] = {
91 * Do not change these numbers! They govern how drive option
92 * index maps to unit and bus. That mapping is ABI.
94 * All controllers used to implement if=T drives need to support
95 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
96 * Otherwise, some index values map to "impossible" bus, unit
97 * values.
99 * For instance, if you change [IF_SCSI] to 255, -drive
100 * if=scsi,index=12 no longer means bus=1,unit=5, but
101 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
102 * the drive can't be set up. Regression.
104 [IF_IDE] = 2,
105 [IF_SCSI] = 7,
109 * Boards may call this to offer board-by-board overrides
110 * of the default, global values.
112 void override_max_devs(BlockInterfaceType type, int max_devs)
114 BlockBackend *blk;
115 DriveInfo *dinfo;
117 GLOBAL_STATE_CODE();
119 if (max_devs <= 0) {
120 return;
123 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
124 dinfo = blk_legacy_dinfo(blk);
125 if (dinfo->type == type) {
126 fprintf(stderr, "Cannot override units-per-bus property of"
127 " the %s interface, because a drive of that type has"
128 " already been added.\n", if_name[type]);
129 g_assert_not_reached();
133 if_max_devs[type] = max_devs;
137 * We automatically delete the drive when a device using it gets
138 * unplugged. Questionable feature, but we can't just drop it.
139 * Device models call blockdev_mark_auto_del() to schedule the
140 * automatic deletion, and generic qdev code calls blockdev_auto_del()
141 * when deletion is actually safe.
143 void blockdev_mark_auto_del(BlockBackend *blk)
145 DriveInfo *dinfo = blk_legacy_dinfo(blk);
146 BlockJob *job;
148 GLOBAL_STATE_CODE();
150 if (!dinfo) {
151 return;
154 JOB_LOCK_GUARD();
156 for (job = block_job_next_locked(NULL); job;
157 job = block_job_next_locked(job)) {
158 if (block_job_has_bdrv(job, blk_bs(blk))) {
159 job_cancel_locked(&job->job, false);
163 dinfo->auto_del = 1;
166 void blockdev_auto_del(BlockBackend *blk)
168 DriveInfo *dinfo = blk_legacy_dinfo(blk);
169 GLOBAL_STATE_CODE();
171 if (dinfo && dinfo->auto_del) {
172 monitor_remove_blk(blk);
173 blk_unref(blk);
177 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
179 int max_devs = if_max_devs[type];
180 return max_devs ? index / max_devs : 0;
183 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
185 int max_devs = if_max_devs[type];
186 return max_devs ? index % max_devs : index;
189 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
190 const char *optstr)
192 QemuOpts *opts;
194 GLOBAL_STATE_CODE();
196 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
197 if (!opts) {
198 return NULL;
200 if (type != IF_DEFAULT) {
201 qemu_opt_set(opts, "if", if_name[type], &error_abort);
203 if (index >= 0) {
204 qemu_opt_set_number(opts, "index", index, &error_abort);
206 if (file)
207 qemu_opt_set(opts, "file", file, &error_abort);
208 return opts;
211 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
213 BlockBackend *blk;
214 DriveInfo *dinfo;
216 GLOBAL_STATE_CODE();
218 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
219 dinfo = blk_legacy_dinfo(blk);
220 if (dinfo && dinfo->type == type
221 && dinfo->bus == bus && dinfo->unit == unit) {
222 return dinfo;
226 return NULL;
230 * Check board claimed all -drive that are meant to be claimed.
231 * Fatal error if any remain unclaimed.
233 void drive_check_orphaned(void)
235 BlockBackend *blk;
236 DriveInfo *dinfo;
237 Location loc;
238 bool orphans = false;
240 GLOBAL_STATE_CODE();
242 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
243 dinfo = blk_legacy_dinfo(blk);
245 * Ignore default drives, because we create certain default
246 * drives unconditionally, then leave them unclaimed. Not the
247 * users fault.
248 * Ignore IF_VIRTIO, because it gets desugared into -device,
249 * so we can leave failing to -device.
250 * Ignore IF_NONE, because leaving unclaimed IF_NONE remains
251 * available for device_add is a feature.
253 if (dinfo->is_default || dinfo->type == IF_VIRTIO
254 || dinfo->type == IF_NONE) {
255 continue;
257 if (!blk_get_attached_dev(blk)) {
258 loc_push_none(&loc);
259 qemu_opts_loc_restore(dinfo->opts);
260 error_report("machine type does not support"
261 " if=%s,bus=%d,unit=%d",
262 if_name[dinfo->type], dinfo->bus, dinfo->unit);
263 loc_pop(&loc);
264 orphans = true;
268 if (orphans) {
269 exit(1);
273 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
275 GLOBAL_STATE_CODE();
276 return drive_get(type,
277 drive_index_to_bus_id(type, index),
278 drive_index_to_unit_id(type, index));
281 int drive_get_max_bus(BlockInterfaceType type)
283 int max_bus;
284 BlockBackend *blk;
285 DriveInfo *dinfo;
287 GLOBAL_STATE_CODE();
289 max_bus = -1;
290 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
291 dinfo = blk_legacy_dinfo(blk);
292 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
293 max_bus = dinfo->bus;
296 return max_bus;
299 static void bdrv_format_print(void *opaque, const char *name)
301 qemu_printf(" %s", name);
304 typedef struct {
305 QEMUBH *bh;
306 BlockDriverState *bs;
307 } BDRVPutRefBH;
309 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
311 if (!strcmp(buf, "ignore")) {
312 return BLOCKDEV_ON_ERROR_IGNORE;
313 } else if (!is_read && !strcmp(buf, "enospc")) {
314 return BLOCKDEV_ON_ERROR_ENOSPC;
315 } else if (!strcmp(buf, "stop")) {
316 return BLOCKDEV_ON_ERROR_STOP;
317 } else if (!strcmp(buf, "report")) {
318 return BLOCKDEV_ON_ERROR_REPORT;
319 } else {
320 error_setg(errp, "'%s' invalid %s error action",
321 buf, is_read ? "read" : "write");
322 return -1;
326 static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
327 Error **errp)
329 const QListEntry *entry;
330 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
331 switch (qobject_type(entry->value)) {
333 case QTYPE_QSTRING: {
334 unsigned long long length;
335 const char *str = qstring_get_str(qobject_to(QString,
336 entry->value));
337 if (parse_uint_full(str, &length, 10) == 0 &&
338 length > 0 && length <= UINT_MAX) {
339 block_acct_add_interval(stats, (unsigned) length);
340 } else {
341 error_setg(errp, "Invalid interval length: %s", str);
342 return false;
344 break;
347 case QTYPE_QNUM: {
348 int64_t length = qnum_get_int(qobject_to(QNum, entry->value));
350 if (length > 0 && length <= UINT_MAX) {
351 block_acct_add_interval(stats, (unsigned) length);
352 } else {
353 error_setg(errp, "Invalid interval length: %" PRId64, length);
354 return false;
356 break;
359 default:
360 error_setg(errp, "The specification of stats-intervals is invalid");
361 return false;
364 return true;
367 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
369 /* All parameters but @opts are optional and may be set to NULL. */
370 static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
371 const char **throttling_group, ThrottleConfig *throttle_cfg,
372 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
374 Error *local_error = NULL;
375 const char *aio;
377 if (bdrv_flags) {
378 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
379 *bdrv_flags |= BDRV_O_COPY_ON_READ;
382 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
383 if (bdrv_parse_aio(aio, bdrv_flags) < 0) {
384 error_setg(errp, "invalid aio option");
385 return;
390 /* disk I/O throttling */
391 if (throttling_group) {
392 *throttling_group = qemu_opt_get(opts, "throttling.group");
395 if (throttle_cfg) {
396 throttle_config_init(throttle_cfg);
397 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
398 qemu_opt_get_number(opts, "throttling.bps-total", 0);
399 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
400 qemu_opt_get_number(opts, "throttling.bps-read", 0);
401 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
402 qemu_opt_get_number(opts, "throttling.bps-write", 0);
403 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
404 qemu_opt_get_number(opts, "throttling.iops-total", 0);
405 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
406 qemu_opt_get_number(opts, "throttling.iops-read", 0);
407 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
408 qemu_opt_get_number(opts, "throttling.iops-write", 0);
410 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
411 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
412 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
413 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
414 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
415 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
416 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
417 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
418 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
419 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
420 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
421 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
423 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
424 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
425 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
426 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
427 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
428 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
429 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
430 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
431 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
432 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
433 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
434 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
436 throttle_cfg->op_size =
437 qemu_opt_get_number(opts, "throttling.iops-size", 0);
439 if (!throttle_is_valid(throttle_cfg, errp)) {
440 return;
444 if (detect_zeroes) {
445 *detect_zeroes =
446 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
447 qemu_opt_get(opts, "detect-zeroes"),
448 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
449 &local_error);
450 if (local_error) {
451 error_propagate(errp, local_error);
452 return;
457 static OnOffAuto account_get_opt(QemuOpts *opts, const char *name)
459 if (!qemu_opt_find(opts, name)) {
460 return ON_OFF_AUTO_AUTO;
462 if (qemu_opt_get_bool(opts, name, true)) {
463 return ON_OFF_AUTO_ON;
465 return ON_OFF_AUTO_OFF;
468 /* Takes the ownership of bs_opts */
469 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
470 Error **errp)
472 const char *buf;
473 int bdrv_flags = 0;
474 int on_read_error, on_write_error;
475 OnOffAuto account_invalid, account_failed;
476 bool writethrough, read_only;
477 BlockBackend *blk;
478 BlockDriverState *bs;
479 ThrottleConfig cfg;
480 int snapshot = 0;
481 Error *error = NULL;
482 QemuOpts *opts;
483 QDict *interval_dict = NULL;
484 QList *interval_list = NULL;
485 const char *id;
486 BlockdevDetectZeroesOptions detect_zeroes =
487 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
488 const char *throttling_group = NULL;
490 /* Check common options by copying from bs_opts to opts, all other options
491 * stay in bs_opts for processing by bdrv_open(). */
492 id = qdict_get_try_str(bs_opts, "id");
493 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, errp);
494 if (!opts) {
495 goto err_no_opts;
498 if (!qemu_opts_absorb_qdict(opts, bs_opts, errp)) {
499 goto early_err;
502 if (id) {
503 qdict_del(bs_opts, "id");
506 /* extract parameters */
507 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
509 account_invalid = account_get_opt(opts, "stats-account-invalid");
510 account_failed = account_get_opt(opts, "stats-account-failed");
512 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
514 id = qemu_opts_id(opts);
516 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
517 qdict_array_split(interval_dict, &interval_list);
519 if (qdict_size(interval_dict) != 0) {
520 error_setg(errp, "Invalid option stats-intervals.%s",
521 qdict_first(interval_dict)->key);
522 goto early_err;
525 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
526 &detect_zeroes, &error);
527 if (error) {
528 error_propagate(errp, error);
529 goto early_err;
532 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
533 if (is_help_option(buf)) {
534 qemu_printf("Supported formats:");
535 bdrv_iterate_format(bdrv_format_print, NULL, false);
536 qemu_printf("\nSupported formats (read-only):");
537 bdrv_iterate_format(bdrv_format_print, NULL, true);
538 qemu_printf("\n");
539 goto early_err;
542 if (qdict_haskey(bs_opts, "driver")) {
543 error_setg(errp, "Cannot specify both 'driver' and 'format'");
544 goto early_err;
546 qdict_put_str(bs_opts, "driver", buf);
549 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
550 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
551 on_write_error = parse_block_error_action(buf, 0, &error);
552 if (error) {
553 error_propagate(errp, error);
554 goto early_err;
558 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
559 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
560 on_read_error = parse_block_error_action(buf, 1, &error);
561 if (error) {
562 error_propagate(errp, error);
563 goto early_err;
567 if (snapshot) {
568 bdrv_flags |= BDRV_O_SNAPSHOT;
571 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
573 /* init */
574 if ((!file || !*file) && !qdict_size(bs_opts)) {
575 BlockBackendRootState *blk_rs;
577 blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
578 blk_rs = blk_get_root_state(blk);
579 blk_rs->open_flags = bdrv_flags | (read_only ? 0 : BDRV_O_RDWR);
580 blk_rs->detect_zeroes = detect_zeroes;
582 qobject_unref(bs_opts);
583 } else {
584 if (file && !*file) {
585 file = NULL;
588 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
589 * with other callers) rather than what we want as the real defaults.
590 * Apply the defaults here instead. */
591 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
592 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
593 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
594 read_only ? "on" : "off");
595 qdict_set_default_str(bs_opts, BDRV_OPT_AUTO_READ_ONLY, "on");
596 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
598 if (runstate_check(RUN_STATE_INMIGRATE)) {
599 bdrv_flags |= BDRV_O_INACTIVE;
602 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
603 if (!blk) {
604 goto err_no_bs_opts;
606 bs = blk_bs(blk);
608 bs->detect_zeroes = detect_zeroes;
610 block_acct_setup(blk_get_stats(blk), account_invalid, account_failed);
612 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
613 blk_unref(blk);
614 blk = NULL;
615 goto err_no_bs_opts;
619 /* disk I/O throttling */
620 if (throttle_enabled(&cfg)) {
621 if (!throttling_group) {
622 throttling_group = id;
624 blk_io_limits_enable(blk, throttling_group);
625 blk_set_io_limits(blk, &cfg);
628 blk_set_enable_write_cache(blk, !writethrough);
629 blk_set_on_error(blk, on_read_error, on_write_error);
631 if (!monitor_add_blk(blk, id, errp)) {
632 blk_unref(blk);
633 blk = NULL;
634 goto err_no_bs_opts;
637 err_no_bs_opts:
638 qemu_opts_del(opts);
639 qobject_unref(interval_dict);
640 qobject_unref(interval_list);
641 return blk;
643 early_err:
644 qemu_opts_del(opts);
645 qobject_unref(interval_dict);
646 qobject_unref(interval_list);
647 err_no_opts:
648 qobject_unref(bs_opts);
649 return NULL;
652 /* Takes the ownership of bs_opts */
653 BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
655 int bdrv_flags = 0;
657 GLOBAL_STATE_CODE();
658 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
659 * with other callers) rather than what we want as the real defaults.
660 * Apply the defaults here instead. */
661 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
662 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
663 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
665 if (runstate_check(RUN_STATE_INMIGRATE)) {
666 bdrv_flags |= BDRV_O_INACTIVE;
669 return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
672 void blockdev_close_all_bdrv_states(void)
674 BlockDriverState *bs, *next_bs;
676 GLOBAL_STATE_CODE();
677 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
678 AioContext *ctx = bdrv_get_aio_context(bs);
680 aio_context_acquire(ctx);
681 bdrv_unref(bs);
682 aio_context_release(ctx);
686 /* Iterates over the list of monitor-owned BlockDriverStates */
687 BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
689 GLOBAL_STATE_CODE();
690 return bs ? QTAILQ_NEXT(bs, monitor_list)
691 : QTAILQ_FIRST(&monitor_bdrv_states);
694 static bool qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
695 Error **errp)
697 const char *value;
699 value = qemu_opt_get(opts, from);
700 if (value) {
701 if (qemu_opt_find(opts, to)) {
702 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
703 "same time", to, from);
704 return false;
708 /* rename all items in opts */
709 while ((value = qemu_opt_get(opts, from))) {
710 qemu_opt_set(opts, to, value, &error_abort);
711 qemu_opt_unset(opts, from);
713 return true;
716 QemuOptsList qemu_legacy_drive_opts = {
717 .name = "drive",
718 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
719 .desc = {
721 .name = "bus",
722 .type = QEMU_OPT_NUMBER,
723 .help = "bus number",
725 .name = "unit",
726 .type = QEMU_OPT_NUMBER,
727 .help = "unit number (i.e. lun for scsi)",
729 .name = "index",
730 .type = QEMU_OPT_NUMBER,
731 .help = "index number",
733 .name = "media",
734 .type = QEMU_OPT_STRING,
735 .help = "media type (disk, cdrom)",
737 .name = "if",
738 .type = QEMU_OPT_STRING,
739 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
741 .name = "file",
742 .type = QEMU_OPT_STRING,
743 .help = "file name",
746 /* Options that are passed on, but have special semantics with -drive */
748 .name = BDRV_OPT_READ_ONLY,
749 .type = QEMU_OPT_BOOL,
750 .help = "open drive file as read-only",
752 .name = "rerror",
753 .type = QEMU_OPT_STRING,
754 .help = "read error action",
756 .name = "werror",
757 .type = QEMU_OPT_STRING,
758 .help = "write error action",
760 .name = "copy-on-read",
761 .type = QEMU_OPT_BOOL,
762 .help = "copy read data from backing file into image file",
765 { /* end of list */ }
769 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type,
770 Error **errp)
772 const char *value;
773 BlockBackend *blk;
774 DriveInfo *dinfo = NULL;
775 QDict *bs_opts;
776 QemuOpts *legacy_opts;
777 DriveMediaType media = MEDIA_DISK;
778 BlockInterfaceType type;
779 int max_devs, bus_id, unit_id, index;
780 const char *werror, *rerror;
781 bool read_only = false;
782 bool copy_on_read;
783 const char *filename;
784 int i;
786 GLOBAL_STATE_CODE();
788 /* Change legacy command line options into QMP ones */
789 static const struct {
790 const char *from;
791 const char *to;
792 } opt_renames[] = {
793 { "iops", "throttling.iops-total" },
794 { "iops_rd", "throttling.iops-read" },
795 { "iops_wr", "throttling.iops-write" },
797 { "bps", "throttling.bps-total" },
798 { "bps_rd", "throttling.bps-read" },
799 { "bps_wr", "throttling.bps-write" },
801 { "iops_max", "throttling.iops-total-max" },
802 { "iops_rd_max", "throttling.iops-read-max" },
803 { "iops_wr_max", "throttling.iops-write-max" },
805 { "bps_max", "throttling.bps-total-max" },
806 { "bps_rd_max", "throttling.bps-read-max" },
807 { "bps_wr_max", "throttling.bps-write-max" },
809 { "iops_size", "throttling.iops-size" },
811 { "group", "throttling.group" },
813 { "readonly", BDRV_OPT_READ_ONLY },
816 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
817 if (!qemu_opt_rename(all_opts, opt_renames[i].from,
818 opt_renames[i].to, errp)) {
819 return NULL;
823 value = qemu_opt_get(all_opts, "cache");
824 if (value) {
825 int flags = 0;
826 bool writethrough;
828 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
829 error_setg(errp, "invalid cache option");
830 return NULL;
833 /* Specific options take precedence */
834 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
835 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
836 !writethrough, &error_abort);
838 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
839 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
840 !!(flags & BDRV_O_NOCACHE), &error_abort);
842 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
843 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
844 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
846 qemu_opt_unset(all_opts, "cache");
849 /* Get a QDict for processing the options */
850 bs_opts = qdict_new();
851 qemu_opts_to_qdict(all_opts, bs_opts);
853 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
854 &error_abort);
855 if (!qemu_opts_absorb_qdict(legacy_opts, bs_opts, errp)) {
856 goto fail;
859 /* Media type */
860 value = qemu_opt_get(legacy_opts, "media");
861 if (value) {
862 if (!strcmp(value, "disk")) {
863 media = MEDIA_DISK;
864 } else if (!strcmp(value, "cdrom")) {
865 media = MEDIA_CDROM;
866 read_only = true;
867 } else {
868 error_setg(errp, "'%s' invalid media", value);
869 goto fail;
873 /* copy-on-read is disabled with a warning for read-only devices */
874 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
875 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
877 if (read_only && copy_on_read) {
878 warn_report("disabling copy-on-read on read-only drive");
879 copy_on_read = false;
882 qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off");
883 qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off");
885 /* Controller type */
886 value = qemu_opt_get(legacy_opts, "if");
887 if (value) {
888 for (type = 0;
889 type < IF_COUNT && strcmp(value, if_name[type]);
890 type++) {
892 if (type == IF_COUNT) {
893 error_setg(errp, "unsupported bus type '%s'", value);
894 goto fail;
896 } else {
897 type = block_default_type;
900 /* Device address specified by bus/unit or index.
901 * If none was specified, try to find the first free one. */
902 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
903 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
904 index = qemu_opt_get_number(legacy_opts, "index", -1);
906 max_devs = if_max_devs[type];
908 if (index != -1) {
909 if (bus_id != 0 || unit_id != -1) {
910 error_setg(errp, "index cannot be used with bus and unit");
911 goto fail;
913 bus_id = drive_index_to_bus_id(type, index);
914 unit_id = drive_index_to_unit_id(type, index);
917 if (unit_id == -1) {
918 unit_id = 0;
919 while (drive_get(type, bus_id, unit_id) != NULL) {
920 unit_id++;
921 if (max_devs && unit_id >= max_devs) {
922 unit_id -= max_devs;
923 bus_id++;
928 if (max_devs && unit_id >= max_devs) {
929 error_setg(errp, "unit %d too big (max is %d)", unit_id, max_devs - 1);
930 goto fail;
933 if (drive_get(type, bus_id, unit_id) != NULL) {
934 error_setg(errp, "drive with bus=%d, unit=%d (index=%d) exists",
935 bus_id, unit_id, index);
936 goto fail;
939 /* no id supplied -> create one */
940 if (qemu_opts_id(all_opts) == NULL) {
941 char *new_id;
942 const char *mediastr = "";
943 if (type == IF_IDE || type == IF_SCSI) {
944 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
946 if (max_devs) {
947 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
948 mediastr, unit_id);
949 } else {
950 new_id = g_strdup_printf("%s%s%i", if_name[type],
951 mediastr, unit_id);
953 qdict_put_str(bs_opts, "id", new_id);
954 g_free(new_id);
957 /* Add virtio block device */
958 if (type == IF_VIRTIO) {
959 QemuOpts *devopts;
960 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
961 &error_abort);
962 qemu_opt_set(devopts, "driver", "virtio-blk", &error_abort);
963 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
964 &error_abort);
967 filename = qemu_opt_get(legacy_opts, "file");
969 /* Check werror/rerror compatibility with if=... */
970 werror = qemu_opt_get(legacy_opts, "werror");
971 if (werror != NULL) {
972 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
973 type != IF_NONE) {
974 error_setg(errp, "werror is not supported by this bus type");
975 goto fail;
977 qdict_put_str(bs_opts, "werror", werror);
980 rerror = qemu_opt_get(legacy_opts, "rerror");
981 if (rerror != NULL) {
982 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
983 type != IF_NONE) {
984 error_setg(errp, "rerror is not supported by this bus type");
985 goto fail;
987 qdict_put_str(bs_opts, "rerror", rerror);
990 /* Actual block device init: Functionality shared with blockdev-add */
991 blk = blockdev_init(filename, bs_opts, errp);
992 bs_opts = NULL;
993 if (!blk) {
994 goto fail;
997 /* Create legacy DriveInfo */
998 dinfo = g_malloc0(sizeof(*dinfo));
999 dinfo->opts = all_opts;
1001 dinfo->type = type;
1002 dinfo->bus = bus_id;
1003 dinfo->unit = unit_id;
1005 blk_set_legacy_dinfo(blk, dinfo);
1007 switch(type) {
1008 case IF_IDE:
1009 case IF_SCSI:
1010 case IF_XEN:
1011 case IF_NONE:
1012 dinfo->media_cd = media == MEDIA_CDROM;
1013 break;
1014 default:
1015 break;
1018 fail:
1019 qemu_opts_del(legacy_opts);
1020 qobject_unref(bs_opts);
1021 return dinfo;
1024 static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1026 BlockDriverState *bs;
1027 AioContext *aio_context;
1029 bs = bdrv_lookup_bs(name, name, errp);
1030 if (bs == NULL) {
1031 return NULL;
1034 if (!bdrv_is_root_node(bs)) {
1035 error_setg(errp, "Need a root block node");
1036 return NULL;
1039 aio_context = bdrv_get_aio_context(bs);
1040 aio_context_acquire(aio_context);
1042 if (!bdrv_is_inserted(bs)) {
1043 error_setg(errp, "Device has no medium");
1044 bs = NULL;
1047 aio_context_release(aio_context);
1049 return bs;
1052 static void blockdev_do_action(TransactionAction *action, Error **errp)
1054 TransactionActionList list;
1056 list.value = action;
1057 list.next = NULL;
1058 qmp_transaction(&list, NULL, errp);
1061 void qmp_blockdev_snapshot_sync(const char *device, const char *node_name,
1062 const char *snapshot_file,
1063 const char *snapshot_node_name,
1064 const char *format,
1065 bool has_mode, NewImageMode mode, Error **errp)
1067 BlockdevSnapshotSync snapshot = {
1068 .device = (char *) device,
1069 .node_name = (char *) node_name,
1070 .snapshot_file = (char *) snapshot_file,
1071 .snapshot_node_name = (char *) snapshot_node_name,
1072 .format = (char *) format,
1073 .has_mode = has_mode,
1074 .mode = mode,
1076 TransactionAction action = {
1077 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1078 .u.blockdev_snapshot_sync.data = &snapshot,
1080 blockdev_do_action(&action, errp);
1083 void qmp_blockdev_snapshot(const char *node, const char *overlay,
1084 Error **errp)
1086 BlockdevSnapshot snapshot_data = {
1087 .node = (char *) node,
1088 .overlay = (char *) overlay
1090 TransactionAction action = {
1091 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
1092 .u.blockdev_snapshot.data = &snapshot_data,
1094 blockdev_do_action(&action, errp);
1097 void qmp_blockdev_snapshot_internal_sync(const char *device,
1098 const char *name,
1099 Error **errp)
1101 BlockdevSnapshotInternal snapshot = {
1102 .device = (char *) device,
1103 .name = (char *) name
1105 TransactionAction action = {
1106 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1107 .u.blockdev_snapshot_internal_sync.data = &snapshot,
1109 blockdev_do_action(&action, errp);
1112 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1113 const char *id,
1114 const char *name,
1115 Error **errp)
1117 BlockDriverState *bs;
1118 AioContext *aio_context;
1119 QEMUSnapshotInfo sn;
1120 Error *local_err = NULL;
1121 SnapshotInfo *info = NULL;
1122 int ret;
1124 bs = qmp_get_root_bs(device, errp);
1125 if (!bs) {
1126 return NULL;
1128 aio_context = bdrv_get_aio_context(bs);
1129 aio_context_acquire(aio_context);
1131 if (!id && !name) {
1132 error_setg(errp, "Name or id must be provided");
1133 goto out_aio_context;
1136 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1137 goto out_aio_context;
1140 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1141 if (local_err) {
1142 error_propagate(errp, local_err);
1143 goto out_aio_context;
1145 if (!ret) {
1146 error_setg(errp,
1147 "Snapshot with id '%s' and name '%s' does not exist on "
1148 "device '%s'",
1149 STR_OR_NULL(id), STR_OR_NULL(name), device);
1150 goto out_aio_context;
1153 bdrv_snapshot_delete(bs, id, name, &local_err);
1154 if (local_err) {
1155 error_propagate(errp, local_err);
1156 goto out_aio_context;
1159 aio_context_release(aio_context);
1161 info = g_new0(SnapshotInfo, 1);
1162 info->id = g_strdup(sn.id_str);
1163 info->name = g_strdup(sn.name);
1164 info->date_nsec = sn.date_nsec;
1165 info->date_sec = sn.date_sec;
1166 info->vm_state_size = sn.vm_state_size;
1167 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1168 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1169 if (sn.icount != -1ULL) {
1170 info->icount = sn.icount;
1171 info->has_icount = true;
1174 return info;
1176 out_aio_context:
1177 aio_context_release(aio_context);
1178 return NULL;
1181 /* New and old BlockDriverState structs for atomic group operations */
1183 typedef struct BlkActionState BlkActionState;
1186 * BlkActionOps:
1187 * Table of operations that define an Action.
1189 * @instance_size: Size of state struct, in bytes.
1190 * @prepare: Prepare the work, must NOT be NULL.
1191 * @commit: Commit the changes, can be NULL.
1192 * @abort: Abort the changes on fail, can be NULL.
1193 * @clean: Clean up resources after all transaction actions have called
1194 * commit() or abort(). Can be NULL.
1196 * Only prepare() may fail. In a single transaction, only one of commit() or
1197 * abort() will be called. clean() will always be called if it is present.
1199 * Always run under BQL.
1201 typedef struct BlkActionOps {
1202 size_t instance_size;
1203 void (*prepare)(BlkActionState *common, Error **errp);
1204 void (*commit)(BlkActionState *common);
1205 void (*abort)(BlkActionState *common);
1206 void (*clean)(BlkActionState *common);
1207 } BlkActionOps;
1210 * BlkActionState:
1211 * Describes one Action's state within a Transaction.
1213 * @action: QAPI-defined enum identifying which Action to perform.
1214 * @ops: Table of ActionOps this Action can perform.
1215 * @block_job_txn: Transaction which this action belongs to.
1216 * @entry: List membership for all Actions in this Transaction.
1218 * This structure must be arranged as first member in a subclassed type,
1219 * assuming that the compiler will also arrange it to the same offsets as the
1220 * base class.
1222 struct BlkActionState {
1223 TransactionAction *action;
1224 const BlkActionOps *ops;
1225 JobTxn *block_job_txn;
1226 TransactionProperties *txn_props;
1227 QTAILQ_ENTRY(BlkActionState) entry;
1230 /* internal snapshot private data */
1231 typedef struct InternalSnapshotState {
1232 BlkActionState common;
1233 BlockDriverState *bs;
1234 QEMUSnapshotInfo sn;
1235 bool created;
1236 } InternalSnapshotState;
1239 static int action_check_completion_mode(BlkActionState *s, Error **errp)
1241 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1242 error_setg(errp,
1243 "Action '%s' does not support Transaction property "
1244 "completion-mode = %s",
1245 TransactionActionKind_str(s->action->type),
1246 ActionCompletionMode_str(s->txn_props->completion_mode));
1247 return -1;
1249 return 0;
1252 static void internal_snapshot_prepare(BlkActionState *common,
1253 Error **errp)
1255 Error *local_err = NULL;
1256 const char *device;
1257 const char *name;
1258 BlockDriverState *bs;
1259 QEMUSnapshotInfo old_sn, *sn;
1260 bool ret;
1261 int64_t rt;
1262 BlockdevSnapshotInternal *internal;
1263 InternalSnapshotState *state;
1264 AioContext *aio_context;
1265 int ret1;
1267 g_assert(common->action->type ==
1268 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1269 internal = common->action->u.blockdev_snapshot_internal_sync.data;
1270 state = DO_UPCAST(InternalSnapshotState, common, common);
1272 /* 1. parse input */
1273 device = internal->device;
1274 name = internal->name;
1276 /* 2. check for validation */
1277 if (action_check_completion_mode(common, errp) < 0) {
1278 return;
1281 bs = qmp_get_root_bs(device, errp);
1282 if (!bs) {
1283 return;
1286 aio_context = bdrv_get_aio_context(bs);
1287 aio_context_acquire(aio_context);
1289 state->bs = bs;
1291 /* Paired with .clean() */
1292 bdrv_drained_begin(bs);
1294 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1295 goto out;
1298 if (bdrv_is_read_only(bs)) {
1299 error_setg(errp, "Device '%s' is read only", device);
1300 goto out;
1303 if (!bdrv_can_snapshot(bs)) {
1304 error_setg(errp, "Block format '%s' used by device '%s' "
1305 "does not support internal snapshots",
1306 bs->drv->format_name, device);
1307 goto out;
1310 if (!strlen(name)) {
1311 error_setg(errp, "Name is empty");
1312 goto out;
1315 /* check whether a snapshot with name exist */
1316 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1317 &local_err);
1318 if (local_err) {
1319 error_propagate(errp, local_err);
1320 goto out;
1321 } else if (ret) {
1322 error_setg(errp,
1323 "Snapshot with name '%s' already exists on device '%s'",
1324 name, device);
1325 goto out;
1328 /* 3. take the snapshot */
1329 sn = &state->sn;
1330 pstrcpy(sn->name, sizeof(sn->name), name);
1331 rt = g_get_real_time();
1332 sn->date_sec = rt / G_USEC_PER_SEC;
1333 sn->date_nsec = (rt % G_USEC_PER_SEC) * 1000;
1334 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1335 if (replay_mode != REPLAY_MODE_NONE) {
1336 sn->icount = replay_get_current_icount();
1337 } else {
1338 sn->icount = -1ULL;
1341 ret1 = bdrv_snapshot_create(bs, sn);
1342 if (ret1 < 0) {
1343 error_setg_errno(errp, -ret1,
1344 "Failed to create snapshot '%s' on device '%s'",
1345 name, device);
1346 goto out;
1349 /* 4. succeed, mark a snapshot is created */
1350 state->created = true;
1352 out:
1353 aio_context_release(aio_context);
1356 static void internal_snapshot_abort(BlkActionState *common)
1358 InternalSnapshotState *state =
1359 DO_UPCAST(InternalSnapshotState, common, common);
1360 BlockDriverState *bs = state->bs;
1361 QEMUSnapshotInfo *sn = &state->sn;
1362 AioContext *aio_context;
1363 Error *local_error = NULL;
1365 if (!state->created) {
1366 return;
1369 aio_context = bdrv_get_aio_context(state->bs);
1370 aio_context_acquire(aio_context);
1372 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1373 error_reportf_err(local_error,
1374 "Failed to delete snapshot with id '%s' and "
1375 "name '%s' on device '%s' in abort: ",
1376 sn->id_str, sn->name,
1377 bdrv_get_device_name(bs));
1380 aio_context_release(aio_context);
1383 static void internal_snapshot_clean(BlkActionState *common)
1385 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1386 common, common);
1387 AioContext *aio_context;
1389 if (!state->bs) {
1390 return;
1393 aio_context = bdrv_get_aio_context(state->bs);
1394 aio_context_acquire(aio_context);
1396 bdrv_drained_end(state->bs);
1398 aio_context_release(aio_context);
1401 /* external snapshot private data */
1402 typedef struct ExternalSnapshotState {
1403 BlkActionState common;
1404 BlockDriverState *old_bs;
1405 BlockDriverState *new_bs;
1406 bool overlay_appended;
1407 } ExternalSnapshotState;
1409 static void external_snapshot_prepare(BlkActionState *common,
1410 Error **errp)
1412 int ret;
1413 int flags = 0;
1414 QDict *options = NULL;
1415 Error *local_err = NULL;
1416 /* Device and node name of the image to generate the snapshot from */
1417 const char *device;
1418 const char *node_name;
1419 /* Reference to the new image (for 'blockdev-snapshot') */
1420 const char *snapshot_ref;
1421 /* File name of the new image (for 'blockdev-snapshot-sync') */
1422 const char *new_image_file;
1423 ExternalSnapshotState *state =
1424 DO_UPCAST(ExternalSnapshotState, common, common);
1425 TransactionAction *action = common->action;
1426 AioContext *aio_context;
1427 uint64_t perm, shared;
1429 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1430 * purpose but a different set of parameters */
1431 switch (action->type) {
1432 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1434 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
1435 device = s->node;
1436 node_name = s->node;
1437 new_image_file = NULL;
1438 snapshot_ref = s->overlay;
1440 break;
1441 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1443 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
1444 device = s->device;
1445 node_name = s->node_name;
1446 new_image_file = s->snapshot_file;
1447 snapshot_ref = NULL;
1449 break;
1450 default:
1451 g_assert_not_reached();
1454 /* start processing */
1455 if (action_check_completion_mode(common, errp) < 0) {
1456 return;
1459 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1460 if (!state->old_bs) {
1461 return;
1464 aio_context = bdrv_get_aio_context(state->old_bs);
1465 aio_context_acquire(aio_context);
1467 /* Paired with .clean() */
1468 bdrv_drained_begin(state->old_bs);
1470 if (!bdrv_is_inserted(state->old_bs)) {
1471 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1472 goto out;
1475 if (bdrv_op_is_blocked(state->old_bs,
1476 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1477 goto out;
1480 if (!bdrv_is_read_only(state->old_bs)) {
1481 if (bdrv_flush(state->old_bs)) {
1482 error_setg(errp, QERR_IO_ERROR);
1483 goto out;
1487 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
1488 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
1489 const char *format = s->format ?: "qcow2";
1490 enum NewImageMode mode;
1491 const char *snapshot_node_name = s->snapshot_node_name;
1493 if (node_name && !snapshot_node_name) {
1494 error_setg(errp, "New overlay node-name missing");
1495 goto out;
1498 if (snapshot_node_name &&
1499 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1500 error_setg(errp, "New overlay node-name already in use");
1501 goto out;
1504 flags = state->old_bs->open_flags;
1505 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ);
1506 flags |= BDRV_O_NO_BACKING;
1508 /* create new image w/backing file */
1509 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1510 if (mode != NEW_IMAGE_MODE_EXISTING) {
1511 int64_t size = bdrv_getlength(state->old_bs);
1512 if (size < 0) {
1513 error_setg_errno(errp, -size, "bdrv_getlength failed");
1514 goto out;
1516 bdrv_refresh_filename(state->old_bs);
1518 aio_context_release(aio_context);
1519 bdrv_img_create(new_image_file, format,
1520 state->old_bs->filename,
1521 state->old_bs->drv->format_name,
1522 NULL, size, flags, false, &local_err);
1523 aio_context_acquire(aio_context);
1525 if (local_err) {
1526 error_propagate(errp, local_err);
1527 goto out;
1531 options = qdict_new();
1532 if (snapshot_node_name) {
1533 qdict_put_str(options, "node-name", snapshot_node_name);
1535 qdict_put_str(options, "driver", format);
1538 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1539 errp);
1540 /* We will manually add the backing_hd field to the bs later */
1541 if (!state->new_bs) {
1542 goto out;
1546 * Allow attaching a backing file to an overlay that's already in use only
1547 * if the parents don't assume that they are already seeing a valid image.
1548 * (Specifically, allow it as a mirror target, which is write-only access.)
1550 bdrv_get_cumulative_perm(state->new_bs, &perm, &shared);
1551 if (perm & BLK_PERM_CONSISTENT_READ) {
1552 error_setg(errp, "The overlay is already in use");
1553 goto out;
1556 if (state->new_bs->drv->is_filter) {
1557 error_setg(errp, "Filters cannot be used as overlays");
1558 goto out;
1561 if (bdrv_cow_child(state->new_bs)) {
1562 error_setg(errp, "The overlay already has a backing image");
1563 goto out;
1566 if (!state->new_bs->drv->supports_backing) {
1567 error_setg(errp, "The overlay does not support backing images");
1568 goto out;
1571 ret = bdrv_append(state->new_bs, state->old_bs, errp);
1572 if (ret < 0) {
1573 goto out;
1575 state->overlay_appended = true;
1577 out:
1578 aio_context_release(aio_context);
1581 static void external_snapshot_commit(BlkActionState *common)
1583 ExternalSnapshotState *state =
1584 DO_UPCAST(ExternalSnapshotState, common, common);
1585 AioContext *aio_context;
1587 aio_context = bdrv_get_aio_context(state->old_bs);
1588 aio_context_acquire(aio_context);
1590 /* We don't need (or want) to use the transactional
1591 * bdrv_reopen_multiple() across all the entries at once, because we
1592 * don't want to abort all of them if one of them fails the reopen */
1593 if (!qatomic_read(&state->old_bs->copy_on_read)) {
1594 bdrv_reopen_set_read_only(state->old_bs, true, NULL);
1597 aio_context_release(aio_context);
1600 static void external_snapshot_abort(BlkActionState *common)
1602 ExternalSnapshotState *state =
1603 DO_UPCAST(ExternalSnapshotState, common, common);
1604 if (state->new_bs) {
1605 if (state->overlay_appended) {
1606 AioContext *aio_context;
1607 AioContext *tmp_context;
1608 int ret;
1610 aio_context = bdrv_get_aio_context(state->old_bs);
1611 aio_context_acquire(aio_context);
1613 bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd()
1614 close state->old_bs; we need it */
1615 bdrv_set_backing_hd(state->new_bs, NULL, &error_abort);
1618 * The call to bdrv_set_backing_hd() above returns state->old_bs to
1619 * the main AioContext. As we're still going to be using it, return
1620 * it to the AioContext it was before.
1622 tmp_context = bdrv_get_aio_context(state->old_bs);
1623 if (aio_context != tmp_context) {
1624 aio_context_release(aio_context);
1625 aio_context_acquire(tmp_context);
1627 ret = bdrv_try_change_aio_context(state->old_bs,
1628 aio_context, NULL, NULL);
1629 assert(ret == 0);
1631 aio_context_release(tmp_context);
1632 aio_context_acquire(aio_context);
1635 bdrv_replace_node(state->new_bs, state->old_bs, &error_abort);
1636 bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */
1638 aio_context_release(aio_context);
1643 static void external_snapshot_clean(BlkActionState *common)
1645 ExternalSnapshotState *state =
1646 DO_UPCAST(ExternalSnapshotState, common, common);
1647 AioContext *aio_context;
1649 if (!state->old_bs) {
1650 return;
1653 aio_context = bdrv_get_aio_context(state->old_bs);
1654 aio_context_acquire(aio_context);
1656 bdrv_drained_end(state->old_bs);
1657 bdrv_unref(state->new_bs);
1659 aio_context_release(aio_context);
1662 typedef struct DriveBackupState {
1663 BlkActionState common;
1664 BlockDriverState *bs;
1665 BlockJob *job;
1666 } DriveBackupState;
1668 static BlockJob *do_backup_common(BackupCommon *backup,
1669 BlockDriverState *bs,
1670 BlockDriverState *target_bs,
1671 AioContext *aio_context,
1672 JobTxn *txn, Error **errp);
1674 static void drive_backup_prepare(BlkActionState *common, Error **errp)
1676 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1677 DriveBackup *backup;
1678 BlockDriverState *bs;
1679 BlockDriverState *target_bs;
1680 BlockDriverState *source = NULL;
1681 AioContext *aio_context;
1682 AioContext *old_context;
1683 const char *format;
1684 QDict *options;
1685 Error *local_err = NULL;
1686 int flags;
1687 int64_t size;
1688 bool set_backing_hd = false;
1689 int ret;
1691 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1692 backup = common->action->u.drive_backup.data;
1694 if (!backup->has_mode) {
1695 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1698 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
1699 if (!bs) {
1700 return;
1703 if (!bs->drv) {
1704 error_setg(errp, "Device has no medium");
1705 return;
1708 aio_context = bdrv_get_aio_context(bs);
1709 aio_context_acquire(aio_context);
1711 state->bs = bs;
1712 /* Paired with .clean() */
1713 bdrv_drained_begin(bs);
1715 format = backup->format;
1716 if (!format && backup->mode != NEW_IMAGE_MODE_EXISTING) {
1717 format = bs->drv->format_name;
1720 /* Early check to avoid creating target */
1721 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
1722 goto out;
1725 flags = bs->open_flags | BDRV_O_RDWR;
1728 * See if we have a backing HD we can use to create our new image
1729 * on top of.
1731 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
1733 * Backup will not replace the source by the target, so none
1734 * of the filters skipped here will be removed (in contrast to
1735 * mirror). Therefore, we can skip all of them when looking
1736 * for the first COW relationship.
1738 source = bdrv_cow_bs(bdrv_skip_filters(bs));
1739 if (!source) {
1740 backup->sync = MIRROR_SYNC_MODE_FULL;
1743 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
1744 source = bs;
1745 flags |= BDRV_O_NO_BACKING;
1746 set_backing_hd = true;
1749 size = bdrv_getlength(bs);
1750 if (size < 0) {
1751 error_setg_errno(errp, -size, "bdrv_getlength failed");
1752 goto out;
1755 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
1756 assert(format);
1757 if (source) {
1758 /* Implicit filters should not appear in the filename */
1759 BlockDriverState *explicit_backing =
1760 bdrv_skip_implicit_filters(source);
1762 bdrv_refresh_filename(explicit_backing);
1763 bdrv_img_create(backup->target, format,
1764 explicit_backing->filename,
1765 explicit_backing->drv->format_name, NULL,
1766 size, flags, false, &local_err);
1767 } else {
1768 bdrv_img_create(backup->target, format, NULL, NULL, NULL,
1769 size, flags, false, &local_err);
1773 if (local_err) {
1774 error_propagate(errp, local_err);
1775 goto out;
1778 options = qdict_new();
1779 qdict_put_str(options, "discard", "unmap");
1780 qdict_put_str(options, "detect-zeroes", "unmap");
1781 if (format) {
1782 qdict_put_str(options, "driver", format);
1785 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
1786 if (!target_bs) {
1787 goto out;
1790 /* Honor bdrv_try_change_aio_context() context acquisition requirements. */
1791 old_context = bdrv_get_aio_context(target_bs);
1792 aio_context_release(aio_context);
1793 aio_context_acquire(old_context);
1795 ret = bdrv_try_change_aio_context(target_bs, aio_context, NULL, errp);
1796 if (ret < 0) {
1797 bdrv_unref(target_bs);
1798 aio_context_release(old_context);
1799 return;
1802 aio_context_release(old_context);
1803 aio_context_acquire(aio_context);
1805 if (set_backing_hd) {
1806 if (bdrv_set_backing_hd(target_bs, source, errp) < 0) {
1807 goto unref;
1811 state->job = do_backup_common(qapi_DriveBackup_base(backup),
1812 bs, target_bs, aio_context,
1813 common->block_job_txn, errp);
1815 unref:
1816 bdrv_unref(target_bs);
1817 out:
1818 aio_context_release(aio_context);
1821 static void drive_backup_commit(BlkActionState *common)
1823 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1824 AioContext *aio_context;
1826 aio_context = bdrv_get_aio_context(state->bs);
1827 aio_context_acquire(aio_context);
1829 assert(state->job);
1830 job_start(&state->job->job);
1832 aio_context_release(aio_context);
1835 static void drive_backup_abort(BlkActionState *common)
1837 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1839 if (state->job) {
1840 job_cancel_sync(&state->job->job, true);
1844 static void drive_backup_clean(BlkActionState *common)
1846 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1847 AioContext *aio_context;
1849 if (!state->bs) {
1850 return;
1853 aio_context = bdrv_get_aio_context(state->bs);
1854 aio_context_acquire(aio_context);
1856 bdrv_drained_end(state->bs);
1858 aio_context_release(aio_context);
1861 typedef struct BlockdevBackupState {
1862 BlkActionState common;
1863 BlockDriverState *bs;
1864 BlockJob *job;
1865 } BlockdevBackupState;
1867 static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1869 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1870 BlockdevBackup *backup;
1871 BlockDriverState *bs;
1872 BlockDriverState *target_bs;
1873 AioContext *aio_context;
1874 AioContext *old_context;
1875 int ret;
1877 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1878 backup = common->action->u.blockdev_backup.data;
1880 bs = bdrv_lookup_bs(backup->device, backup->device, errp);
1881 if (!bs) {
1882 return;
1885 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
1886 if (!target_bs) {
1887 return;
1890 /* Honor bdrv_try_change_aio_context() context acquisition requirements. */
1891 aio_context = bdrv_get_aio_context(bs);
1892 old_context = bdrv_get_aio_context(target_bs);
1893 aio_context_acquire(old_context);
1895 ret = bdrv_try_change_aio_context(target_bs, aio_context, NULL, errp);
1896 if (ret < 0) {
1897 aio_context_release(old_context);
1898 return;
1901 aio_context_release(old_context);
1902 aio_context_acquire(aio_context);
1903 state->bs = bs;
1905 /* Paired with .clean() */
1906 bdrv_drained_begin(state->bs);
1908 state->job = do_backup_common(qapi_BlockdevBackup_base(backup),
1909 bs, target_bs, aio_context,
1910 common->block_job_txn, errp);
1912 aio_context_release(aio_context);
1915 static void blockdev_backup_commit(BlkActionState *common)
1917 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1918 AioContext *aio_context;
1920 aio_context = bdrv_get_aio_context(state->bs);
1921 aio_context_acquire(aio_context);
1923 assert(state->job);
1924 job_start(&state->job->job);
1926 aio_context_release(aio_context);
1929 static void blockdev_backup_abort(BlkActionState *common)
1931 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1933 if (state->job) {
1934 job_cancel_sync(&state->job->job, true);
1938 static void blockdev_backup_clean(BlkActionState *common)
1940 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1941 AioContext *aio_context;
1943 if (!state->bs) {
1944 return;
1947 aio_context = bdrv_get_aio_context(state->bs);
1948 aio_context_acquire(aio_context);
1950 bdrv_drained_end(state->bs);
1952 aio_context_release(aio_context);
1955 typedef struct BlockDirtyBitmapState {
1956 BlkActionState common;
1957 BdrvDirtyBitmap *bitmap;
1958 BlockDriverState *bs;
1959 HBitmap *backup;
1960 bool prepared;
1961 bool was_enabled;
1962 } BlockDirtyBitmapState;
1964 static void block_dirty_bitmap_add_prepare(BlkActionState *common,
1965 Error **errp)
1967 Error *local_err = NULL;
1968 BlockDirtyBitmapAdd *action;
1969 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1970 common, common);
1972 if (action_check_completion_mode(common, errp) < 0) {
1973 return;
1976 action = common->action->u.block_dirty_bitmap_add.data;
1977 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1978 qmp_block_dirty_bitmap_add(action->node, action->name,
1979 action->has_granularity, action->granularity,
1980 action->has_persistent, action->persistent,
1981 action->has_disabled, action->disabled,
1982 &local_err);
1984 if (!local_err) {
1985 state->prepared = true;
1986 } else {
1987 error_propagate(errp, local_err);
1991 static void block_dirty_bitmap_add_abort(BlkActionState *common)
1993 BlockDirtyBitmapAdd *action;
1994 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1995 common, common);
1997 action = common->action->u.block_dirty_bitmap_add.data;
1998 /* Should not be able to fail: IF the bitmap was added via .prepare(),
1999 * then the node reference and bitmap name must have been valid.
2001 if (state->prepared) {
2002 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2006 static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
2007 Error **errp)
2009 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2010 common, common);
2011 BlockDirtyBitmap *action;
2013 if (action_check_completion_mode(common, errp) < 0) {
2014 return;
2017 action = common->action->u.block_dirty_bitmap_clear.data;
2018 state->bitmap = block_dirty_bitmap_lookup(action->node,
2019 action->name,
2020 &state->bs,
2021 errp);
2022 if (!state->bitmap) {
2023 return;
2026 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) {
2027 return;
2030 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2033 static void block_dirty_bitmap_restore(BlkActionState *common)
2035 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2036 common, common);
2038 if (state->backup) {
2039 bdrv_restore_dirty_bitmap(state->bitmap, state->backup);
2043 static void block_dirty_bitmap_free_backup(BlkActionState *common)
2045 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2046 common, common);
2048 hbitmap_free(state->backup);
2051 static void block_dirty_bitmap_enable_prepare(BlkActionState *common,
2052 Error **errp)
2054 BlockDirtyBitmap *action;
2055 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2056 common, common);
2058 if (action_check_completion_mode(common, errp) < 0) {
2059 return;
2062 action = common->action->u.block_dirty_bitmap_enable.data;
2063 state->bitmap = block_dirty_bitmap_lookup(action->node,
2064 action->name,
2065 NULL,
2066 errp);
2067 if (!state->bitmap) {
2068 return;
2071 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
2072 return;
2075 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2076 bdrv_enable_dirty_bitmap(state->bitmap);
2079 static void block_dirty_bitmap_enable_abort(BlkActionState *common)
2081 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2082 common, common);
2084 if (!state->was_enabled) {
2085 bdrv_disable_dirty_bitmap(state->bitmap);
2089 static void block_dirty_bitmap_disable_prepare(BlkActionState *common,
2090 Error **errp)
2092 BlockDirtyBitmap *action;
2093 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2094 common, common);
2096 if (action_check_completion_mode(common, errp) < 0) {
2097 return;
2100 action = common->action->u.block_dirty_bitmap_disable.data;
2101 state->bitmap = block_dirty_bitmap_lookup(action->node,
2102 action->name,
2103 NULL,
2104 errp);
2105 if (!state->bitmap) {
2106 return;
2109 if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
2110 return;
2113 state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
2114 bdrv_disable_dirty_bitmap(state->bitmap);
2117 static void block_dirty_bitmap_disable_abort(BlkActionState *common)
2119 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2120 common, common);
2122 if (state->was_enabled) {
2123 bdrv_enable_dirty_bitmap(state->bitmap);
2127 static void block_dirty_bitmap_merge_prepare(BlkActionState *common,
2128 Error **errp)
2130 BlockDirtyBitmapMerge *action;
2131 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2132 common, common);
2134 if (action_check_completion_mode(common, errp) < 0) {
2135 return;
2138 action = common->action->u.block_dirty_bitmap_merge.data;
2140 state->bitmap = block_dirty_bitmap_merge(action->node, action->target,
2141 action->bitmaps, &state->backup,
2142 errp);
2145 static void block_dirty_bitmap_remove_prepare(BlkActionState *common,
2146 Error **errp)
2148 BlockDirtyBitmap *action;
2149 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2150 common, common);
2152 if (action_check_completion_mode(common, errp) < 0) {
2153 return;
2156 action = common->action->u.block_dirty_bitmap_remove.data;
2158 state->bitmap = block_dirty_bitmap_remove(action->node, action->name,
2159 false, &state->bs, errp);
2160 if (state->bitmap) {
2161 bdrv_dirty_bitmap_skip_store(state->bitmap, true);
2162 bdrv_dirty_bitmap_set_busy(state->bitmap, true);
2166 static void block_dirty_bitmap_remove_abort(BlkActionState *common)
2168 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2169 common, common);
2171 if (state->bitmap) {
2172 bdrv_dirty_bitmap_skip_store(state->bitmap, false);
2173 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
2177 static void block_dirty_bitmap_remove_commit(BlkActionState *common)
2179 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2180 common, common);
2182 bdrv_dirty_bitmap_set_busy(state->bitmap, false);
2183 bdrv_release_dirty_bitmap(state->bitmap);
2186 static void abort_prepare(BlkActionState *common, Error **errp)
2188 error_setg(errp, "Transaction aborted using Abort action");
2191 static void abort_commit(BlkActionState *common)
2193 g_assert_not_reached(); /* this action never succeeds */
2196 static const BlkActionOps actions[] = {
2197 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2198 .instance_size = sizeof(ExternalSnapshotState),
2199 .prepare = external_snapshot_prepare,
2200 .commit = external_snapshot_commit,
2201 .abort = external_snapshot_abort,
2202 .clean = external_snapshot_clean,
2204 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2205 .instance_size = sizeof(ExternalSnapshotState),
2206 .prepare = external_snapshot_prepare,
2207 .commit = external_snapshot_commit,
2208 .abort = external_snapshot_abort,
2209 .clean = external_snapshot_clean,
2211 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2212 .instance_size = sizeof(DriveBackupState),
2213 .prepare = drive_backup_prepare,
2214 .commit = drive_backup_commit,
2215 .abort = drive_backup_abort,
2216 .clean = drive_backup_clean,
2218 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2219 .instance_size = sizeof(BlockdevBackupState),
2220 .prepare = blockdev_backup_prepare,
2221 .commit = blockdev_backup_commit,
2222 .abort = blockdev_backup_abort,
2223 .clean = blockdev_backup_clean,
2225 [TRANSACTION_ACTION_KIND_ABORT] = {
2226 .instance_size = sizeof(BlkActionState),
2227 .prepare = abort_prepare,
2228 .commit = abort_commit,
2230 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2231 .instance_size = sizeof(InternalSnapshotState),
2232 .prepare = internal_snapshot_prepare,
2233 .abort = internal_snapshot_abort,
2234 .clean = internal_snapshot_clean,
2236 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2237 .instance_size = sizeof(BlockDirtyBitmapState),
2238 .prepare = block_dirty_bitmap_add_prepare,
2239 .abort = block_dirty_bitmap_add_abort,
2241 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2242 .instance_size = sizeof(BlockDirtyBitmapState),
2243 .prepare = block_dirty_bitmap_clear_prepare,
2244 .commit = block_dirty_bitmap_free_backup,
2245 .abort = block_dirty_bitmap_restore,
2247 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE] = {
2248 .instance_size = sizeof(BlockDirtyBitmapState),
2249 .prepare = block_dirty_bitmap_enable_prepare,
2250 .abort = block_dirty_bitmap_enable_abort,
2252 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE] = {
2253 .instance_size = sizeof(BlockDirtyBitmapState),
2254 .prepare = block_dirty_bitmap_disable_prepare,
2255 .abort = block_dirty_bitmap_disable_abort,
2257 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE] = {
2258 .instance_size = sizeof(BlockDirtyBitmapState),
2259 .prepare = block_dirty_bitmap_merge_prepare,
2260 .commit = block_dirty_bitmap_free_backup,
2261 .abort = block_dirty_bitmap_restore,
2263 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE] = {
2264 .instance_size = sizeof(BlockDirtyBitmapState),
2265 .prepare = block_dirty_bitmap_remove_prepare,
2266 .commit = block_dirty_bitmap_remove_commit,
2267 .abort = block_dirty_bitmap_remove_abort,
2269 /* Where are transactions for MIRROR, COMMIT and STREAM?
2270 * Although these blockjobs use transaction callbacks like the backup job,
2271 * these jobs do not necessarily adhere to transaction semantics.
2272 * These jobs may not fully undo all of their actions on abort, nor do they
2273 * necessarily work in transactions with more than one job in them.
2278 * Allocate a TransactionProperties structure if necessary, and fill
2279 * that structure with desired defaults if they are unset.
2281 static TransactionProperties *get_transaction_properties(
2282 TransactionProperties *props)
2284 if (!props) {
2285 props = g_new0(TransactionProperties, 1);
2288 if (!props->has_completion_mode) {
2289 props->has_completion_mode = true;
2290 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2293 return props;
2297 * 'Atomic' group operations. The operations are performed as a set, and if
2298 * any fail then we roll back all operations in the group.
2300 * Always run under BQL.
2302 void qmp_transaction(TransactionActionList *dev_list,
2303 struct TransactionProperties *props,
2304 Error **errp)
2306 TransactionActionList *dev_entry = dev_list;
2307 bool has_props = !!props;
2308 JobTxn *block_job_txn = NULL;
2309 BlkActionState *state, *next;
2310 Error *local_err = NULL;
2312 GLOBAL_STATE_CODE();
2314 QTAILQ_HEAD(, BlkActionState) snap_bdrv_states;
2315 QTAILQ_INIT(&snap_bdrv_states);
2317 /* Does this transaction get canceled as a group on failure?
2318 * If not, we don't really need to make a JobTxn.
2320 props = get_transaction_properties(props);
2321 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2322 block_job_txn = job_txn_new();
2325 /* drain all i/o before any operations */
2326 bdrv_drain_all();
2328 /* We don't do anything in this loop that commits us to the operations */
2329 while (NULL != dev_entry) {
2330 TransactionAction *dev_info = NULL;
2331 const BlkActionOps *ops;
2333 dev_info = dev_entry->value;
2334 dev_entry = dev_entry->next;
2336 assert(dev_info->type < ARRAY_SIZE(actions));
2338 ops = &actions[dev_info->type];
2339 assert(ops->instance_size > 0);
2341 state = g_malloc0(ops->instance_size);
2342 state->ops = ops;
2343 state->action = dev_info;
2344 state->block_job_txn = block_job_txn;
2345 state->txn_props = props;
2346 QTAILQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
2348 state->ops->prepare(state, &local_err);
2349 if (local_err) {
2350 error_propagate(errp, local_err);
2351 goto delete_and_fail;
2355 QTAILQ_FOREACH(state, &snap_bdrv_states, entry) {
2356 if (state->ops->commit) {
2357 state->ops->commit(state);
2361 /* success */
2362 goto exit;
2364 delete_and_fail:
2365 /* failure, and it is all-or-none; roll back all operations */
2366 QTAILQ_FOREACH_REVERSE(state, &snap_bdrv_states, entry) {
2367 if (state->ops->abort) {
2368 state->ops->abort(state);
2371 exit:
2372 QTAILQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2373 if (state->ops->clean) {
2374 state->ops->clean(state);
2376 g_free(state);
2378 if (!has_props) {
2379 qapi_free_TransactionProperties(props);
2381 job_txn_unref(block_job_txn);
2384 BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
2385 const char *name,
2386 Error **errp)
2388 BdrvDirtyBitmap *bitmap;
2389 BlockDriverState *bs;
2390 BlockDirtyBitmapSha256 *ret = NULL;
2391 char *sha256;
2393 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
2394 if (!bitmap || !bs) {
2395 return NULL;
2398 sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp);
2399 if (sha256 == NULL) {
2400 return NULL;
2403 ret = g_new(BlockDirtyBitmapSha256, 1);
2404 ret->sha256 = sha256;
2406 return ret;
2409 void coroutine_fn qmp_block_resize(const char *device, const char *node_name,
2410 int64_t size, Error **errp)
2412 Error *local_err = NULL;
2413 BlockBackend *blk;
2414 BlockDriverState *bs;
2415 AioContext *old_ctx;
2417 bs = bdrv_lookup_bs(device, node_name, &local_err);
2418 if (local_err) {
2419 error_propagate(errp, local_err);
2420 return;
2423 if (size < 0) {
2424 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2425 return;
2428 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2429 error_setg(errp, QERR_DEVICE_IN_USE, device);
2430 return;
2433 blk = blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL, errp);
2434 if (!blk) {
2435 return;
2438 bdrv_co_lock(bs);
2439 bdrv_drained_begin(bs);
2440 bdrv_co_unlock(bs);
2442 old_ctx = bdrv_co_enter(bs);
2443 blk_co_truncate(blk, size, false, PREALLOC_MODE_OFF, 0, errp);
2444 bdrv_co_leave(bs, old_ctx);
2446 bdrv_co_lock(bs);
2447 bdrv_drained_end(bs);
2448 blk_unref(blk);
2449 bdrv_co_unlock(bs);
2452 void qmp_block_stream(const char *job_id, const char *device,
2453 const char *base,
2454 const char *base_node,
2455 const char *backing_file,
2456 const char *bottom,
2457 bool has_speed, int64_t speed,
2458 bool has_on_error, BlockdevOnError on_error,
2459 const char *filter_node_name,
2460 bool has_auto_finalize, bool auto_finalize,
2461 bool has_auto_dismiss, bool auto_dismiss,
2462 Error **errp)
2464 BlockDriverState *bs, *iter, *iter_end;
2465 BlockDriverState *base_bs = NULL;
2466 BlockDriverState *bottom_bs = NULL;
2467 AioContext *aio_context;
2468 Error *local_err = NULL;
2469 int job_flags = JOB_DEFAULT;
2471 if (base && base_node) {
2472 error_setg(errp, "'base' and 'base-node' cannot be specified "
2473 "at the same time");
2474 return;
2477 if (base && bottom) {
2478 error_setg(errp, "'base' and 'bottom' cannot be specified "
2479 "at the same time");
2480 return;
2483 if (bottom && base_node) {
2484 error_setg(errp, "'bottom' and 'base-node' cannot be specified "
2485 "at the same time");
2486 return;
2489 if (!has_on_error) {
2490 on_error = BLOCKDEV_ON_ERROR_REPORT;
2493 bs = bdrv_lookup_bs(device, device, errp);
2494 if (!bs) {
2495 return;
2498 aio_context = bdrv_get_aio_context(bs);
2499 aio_context_acquire(aio_context);
2501 if (base) {
2502 base_bs = bdrv_find_backing_image(bs, base);
2503 if (base_bs == NULL) {
2504 error_setg(errp, "Can't find '%s' in the backing chain", base);
2505 goto out;
2507 assert(bdrv_get_aio_context(base_bs) == aio_context);
2510 if (base_node) {
2511 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2512 if (!base_bs) {
2513 goto out;
2515 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
2516 error_setg(errp, "Node '%s' is not a backing image of '%s'",
2517 base_node, device);
2518 goto out;
2520 assert(bdrv_get_aio_context(base_bs) == aio_context);
2521 bdrv_refresh_filename(base_bs);
2524 if (bottom) {
2525 bottom_bs = bdrv_lookup_bs(NULL, bottom, errp);
2526 if (!bottom_bs) {
2527 goto out;
2529 if (!bottom_bs->drv) {
2530 error_setg(errp, "Node '%s' is not open", bottom);
2531 goto out;
2533 if (bottom_bs->drv->is_filter) {
2534 error_setg(errp, "Node '%s' is a filter, use a non-filter node "
2535 "as 'bottom'", bottom);
2536 goto out;
2538 if (!bdrv_chain_contains(bs, bottom_bs)) {
2539 error_setg(errp, "Node '%s' is not in a chain starting from '%s'",
2540 bottom, device);
2541 goto out;
2543 assert(bdrv_get_aio_context(bottom_bs) == aio_context);
2547 * Check for op blockers in the whole chain between bs and base (or bottom)
2549 iter_end = bottom ? bdrv_filter_or_cow_bs(bottom_bs) : base_bs;
2550 for (iter = bs; iter && iter != iter_end;
2551 iter = bdrv_filter_or_cow_bs(iter))
2553 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
2554 goto out;
2558 /* if we are streaming the entire chain, the result will have no backing
2559 * file, and specifying one is therefore an error */
2560 if (!base_bs && backing_file) {
2561 error_setg(errp, "backing file specified, but streaming the "
2562 "entire chain");
2563 goto out;
2566 if (has_auto_finalize && !auto_finalize) {
2567 job_flags |= JOB_MANUAL_FINALIZE;
2569 if (has_auto_dismiss && !auto_dismiss) {
2570 job_flags |= JOB_MANUAL_DISMISS;
2573 stream_start(job_id, bs, base_bs, backing_file,
2574 bottom_bs, job_flags, has_speed ? speed : 0, on_error,
2575 filter_node_name, &local_err);
2576 if (local_err) {
2577 error_propagate(errp, local_err);
2578 goto out;
2581 trace_qmp_block_stream(bs);
2583 out:
2584 aio_context_release(aio_context);
2587 void qmp_block_commit(const char *job_id, const char *device,
2588 const char *base_node,
2589 const char *base,
2590 const char *top_node,
2591 const char *top,
2592 const char *backing_file,
2593 bool has_speed, int64_t speed,
2594 bool has_on_error, BlockdevOnError on_error,
2595 const char *filter_node_name,
2596 bool has_auto_finalize, bool auto_finalize,
2597 bool has_auto_dismiss, bool auto_dismiss,
2598 Error **errp)
2600 BlockDriverState *bs;
2601 BlockDriverState *iter;
2602 BlockDriverState *base_bs, *top_bs;
2603 AioContext *aio_context;
2604 Error *local_err = NULL;
2605 int job_flags = JOB_DEFAULT;
2606 uint64_t top_perm, top_shared;
2608 if (!has_speed) {
2609 speed = 0;
2611 if (!has_on_error) {
2612 on_error = BLOCKDEV_ON_ERROR_REPORT;
2614 if (has_auto_finalize && !auto_finalize) {
2615 job_flags |= JOB_MANUAL_FINALIZE;
2617 if (has_auto_dismiss && !auto_dismiss) {
2618 job_flags |= JOB_MANUAL_DISMISS;
2621 /* Important Note:
2622 * libvirt relies on the DeviceNotFound error class in order to probe for
2623 * live commit feature versions; for this to work, we must make sure to
2624 * perform the device lookup before any generic errors that may occur in a
2625 * scenario in which all optional arguments are omitted. */
2626 bs = qmp_get_root_bs(device, &local_err);
2627 if (!bs) {
2628 bs = bdrv_lookup_bs(device, device, NULL);
2629 if (!bs) {
2630 error_free(local_err);
2631 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2632 "Device '%s' not found", device);
2633 } else {
2634 error_propagate(errp, local_err);
2636 return;
2639 aio_context = bdrv_get_aio_context(bs);
2640 aio_context_acquire(aio_context);
2642 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
2643 goto out;
2646 /* default top_bs is the active layer */
2647 top_bs = bs;
2649 if (top_node && top) {
2650 error_setg(errp, "'top-node' and 'top' are mutually exclusive");
2651 goto out;
2652 } else if (top_node) {
2653 top_bs = bdrv_lookup_bs(NULL, top_node, errp);
2654 if (top_bs == NULL) {
2655 goto out;
2657 if (!bdrv_chain_contains(bs, top_bs)) {
2658 error_setg(errp, "'%s' is not in this backing file chain",
2659 top_node);
2660 goto out;
2662 } else if (top) {
2663 /* This strcmp() is just a shortcut, there is no need to
2664 * refresh @bs's filename. If it mismatches,
2665 * bdrv_find_backing_image() will do the refresh and may still
2666 * return @bs. */
2667 if (strcmp(bs->filename, top) != 0) {
2668 top_bs = bdrv_find_backing_image(bs, top);
2672 if (top_bs == NULL) {
2673 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
2674 goto out;
2677 assert(bdrv_get_aio_context(top_bs) == aio_context);
2679 if (base_node && base) {
2680 error_setg(errp, "'base-node' and 'base' are mutually exclusive");
2681 goto out;
2682 } else if (base_node) {
2683 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2684 if (base_bs == NULL) {
2685 goto out;
2687 if (!bdrv_chain_contains(top_bs, base_bs)) {
2688 error_setg(errp, "'%s' is not in this backing file chain",
2689 base_node);
2690 goto out;
2692 } else if (base) {
2693 base_bs = bdrv_find_backing_image(top_bs, base);
2694 if (base_bs == NULL) {
2695 error_setg(errp, "Can't find '%s' in the backing chain", base);
2696 goto out;
2698 } else {
2699 base_bs = bdrv_find_base(top_bs);
2700 if (base_bs == NULL) {
2701 error_setg(errp, "There is no backimg image");
2702 goto out;
2706 assert(bdrv_get_aio_context(base_bs) == aio_context);
2708 for (iter = top_bs; iter != bdrv_filter_or_cow_bs(base_bs);
2709 iter = bdrv_filter_or_cow_bs(iter))
2711 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2712 goto out;
2716 /* Do not allow attempts to commit an image into itself */
2717 if (top_bs == base_bs) {
2718 error_setg(errp, "cannot commit an image into itself");
2719 goto out;
2723 * Active commit is required if and only if someone has taken a
2724 * WRITE permission on the top node. Historically, we have always
2725 * used active commit for top nodes, so continue that practice
2726 * lest we possibly break clients that rely on this behavior, e.g.
2727 * to later attach this node to a writing parent.
2728 * (Active commit is never really wrong.)
2730 bdrv_get_cumulative_perm(top_bs, &top_perm, &top_shared);
2731 if (top_perm & BLK_PERM_WRITE ||
2732 bdrv_skip_filters(top_bs) == bdrv_skip_filters(bs))
2734 if (backing_file) {
2735 if (bdrv_skip_filters(top_bs) == bdrv_skip_filters(bs)) {
2736 error_setg(errp, "'backing-file' specified,"
2737 " but 'top' is the active layer");
2738 } else {
2739 error_setg(errp, "'backing-file' specified, but 'top' has a "
2740 "writer on it");
2742 goto out;
2744 if (!job_id) {
2746 * Emulate here what block_job_create() does, because it
2747 * is possible that @bs != @top_bs (the block job should
2748 * be named after @bs, even if @top_bs is the actual
2749 * source)
2751 job_id = bdrv_get_device_name(bs);
2753 commit_active_start(job_id, top_bs, base_bs, job_flags, speed, on_error,
2754 filter_node_name, NULL, NULL, false, &local_err);
2755 } else {
2756 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
2757 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2758 goto out;
2760 commit_start(job_id, bs, base_bs, top_bs, job_flags,
2761 speed, on_error, backing_file,
2762 filter_node_name, &local_err);
2764 if (local_err != NULL) {
2765 error_propagate(errp, local_err);
2766 goto out;
2769 out:
2770 aio_context_release(aio_context);
2773 /* Common QMP interface for drive-backup and blockdev-backup */
2774 static BlockJob *do_backup_common(BackupCommon *backup,
2775 BlockDriverState *bs,
2776 BlockDriverState *target_bs,
2777 AioContext *aio_context,
2778 JobTxn *txn, Error **errp)
2780 BlockJob *job = NULL;
2781 BdrvDirtyBitmap *bmap = NULL;
2782 BackupPerf perf = { .max_workers = 64 };
2783 int job_flags = JOB_DEFAULT;
2785 if (!backup->has_speed) {
2786 backup->speed = 0;
2788 if (!backup->has_on_source_error) {
2789 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2791 if (!backup->has_on_target_error) {
2792 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2794 if (!backup->has_auto_finalize) {
2795 backup->auto_finalize = true;
2797 if (!backup->has_auto_dismiss) {
2798 backup->auto_dismiss = true;
2800 if (!backup->has_compress) {
2801 backup->compress = false;
2804 if (backup->x_perf) {
2805 if (backup->x_perf->has_use_copy_range) {
2806 perf.use_copy_range = backup->x_perf->use_copy_range;
2808 if (backup->x_perf->has_max_workers) {
2809 perf.max_workers = backup->x_perf->max_workers;
2811 if (backup->x_perf->has_max_chunk) {
2812 perf.max_chunk = backup->x_perf->max_chunk;
2816 if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) ||
2817 (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) {
2818 /* done before desugaring 'incremental' to print the right message */
2819 if (!backup->bitmap) {
2820 error_setg(errp, "must provide a valid bitmap name for "
2821 "'%s' sync mode", MirrorSyncMode_str(backup->sync));
2822 return NULL;
2826 if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) {
2827 if (backup->has_bitmap_mode &&
2828 backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) {
2829 error_setg(errp, "Bitmap sync mode must be '%s' "
2830 "when using sync mode '%s'",
2831 BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS),
2832 MirrorSyncMode_str(backup->sync));
2833 return NULL;
2835 backup->has_bitmap_mode = true;
2836 backup->sync = MIRROR_SYNC_MODE_BITMAP;
2837 backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS;
2840 if (backup->bitmap) {
2841 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
2842 if (!bmap) {
2843 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
2844 return NULL;
2846 if (!backup->has_bitmap_mode) {
2847 error_setg(errp, "Bitmap sync mode must be given "
2848 "when providing a bitmap");
2849 return NULL;
2851 if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) {
2852 return NULL;
2855 /* This does not produce a useful bitmap artifact: */
2856 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
2857 error_setg(errp, "sync mode '%s' does not produce meaningful bitmap"
2858 " outputs", MirrorSyncMode_str(backup->sync));
2859 return NULL;
2862 /* If the bitmap isn't used for input or output, this is useless: */
2863 if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER &&
2864 backup->sync != MIRROR_SYNC_MODE_BITMAP) {
2865 error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect"
2866 " when combined with sync mode '%s'",
2867 BitmapSyncMode_str(backup->bitmap_mode),
2868 MirrorSyncMode_str(backup->sync));
2869 return NULL;
2873 if (!backup->bitmap && backup->has_bitmap_mode) {
2874 error_setg(errp, "Cannot specify bitmap sync mode without a bitmap");
2875 return NULL;
2878 if (!backup->auto_finalize) {
2879 job_flags |= JOB_MANUAL_FINALIZE;
2881 if (!backup->auto_dismiss) {
2882 job_flags |= JOB_MANUAL_DISMISS;
2885 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
2886 backup->sync, bmap, backup->bitmap_mode,
2887 backup->compress,
2888 backup->filter_node_name,
2889 &perf,
2890 backup->on_source_error,
2891 backup->on_target_error,
2892 job_flags, NULL, NULL, txn, errp);
2893 return job;
2896 void qmp_drive_backup(DriveBackup *backup, Error **errp)
2898 TransactionAction action = {
2899 .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP,
2900 .u.drive_backup.data = backup,
2902 blockdev_do_action(&action, errp);
2905 BlockDeviceInfoList *qmp_query_named_block_nodes(bool has_flat,
2906 bool flat,
2907 Error **errp)
2909 bool return_flat = has_flat && flat;
2911 return bdrv_named_nodes_list(return_flat, errp);
2914 XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp)
2916 return bdrv_get_xdbg_block_graph(errp);
2919 void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp)
2921 TransactionAction action = {
2922 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP,
2923 .u.blockdev_backup.data = backup,
2925 blockdev_do_action(&action, errp);
2928 /* Parameter check and block job starting for drive mirroring.
2929 * Caller should hold @device and @target's aio context (must be the same).
2931 static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
2932 BlockDriverState *target,
2933 const char *replaces,
2934 enum MirrorSyncMode sync,
2935 BlockMirrorBackingMode backing_mode,
2936 bool zero_target,
2937 bool has_speed, int64_t speed,
2938 bool has_granularity, uint32_t granularity,
2939 bool has_buf_size, int64_t buf_size,
2940 bool has_on_source_error,
2941 BlockdevOnError on_source_error,
2942 bool has_on_target_error,
2943 BlockdevOnError on_target_error,
2944 bool has_unmap, bool unmap,
2945 const char *filter_node_name,
2946 bool has_copy_mode, MirrorCopyMode copy_mode,
2947 bool has_auto_finalize, bool auto_finalize,
2948 bool has_auto_dismiss, bool auto_dismiss,
2949 Error **errp)
2951 BlockDriverState *unfiltered_bs;
2952 int job_flags = JOB_DEFAULT;
2954 if (!has_speed) {
2955 speed = 0;
2957 if (!has_on_source_error) {
2958 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2960 if (!has_on_target_error) {
2961 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2963 if (!has_granularity) {
2964 granularity = 0;
2966 if (!has_buf_size) {
2967 buf_size = 0;
2969 if (!has_unmap) {
2970 unmap = true;
2972 if (!has_copy_mode) {
2973 copy_mode = MIRROR_COPY_MODE_BACKGROUND;
2975 if (has_auto_finalize && !auto_finalize) {
2976 job_flags |= JOB_MANUAL_FINALIZE;
2978 if (has_auto_dismiss && !auto_dismiss) {
2979 job_flags |= JOB_MANUAL_DISMISS;
2982 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
2983 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2984 "a value in range [512B, 64MB]");
2985 return;
2987 if (granularity & (granularity - 1)) {
2988 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2989 "a power of 2");
2990 return;
2993 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
2994 return;
2996 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
2997 return;
3000 if (!bdrv_backing_chain_next(bs) && sync == MIRROR_SYNC_MODE_TOP) {
3001 sync = MIRROR_SYNC_MODE_FULL;
3004 if (!replaces) {
3005 /* We want to mirror from @bs, but keep implicit filters on top */
3006 unfiltered_bs = bdrv_skip_implicit_filters(bs);
3007 if (unfiltered_bs != bs) {
3008 replaces = unfiltered_bs->node_name;
3012 if (replaces) {
3013 BlockDriverState *to_replace_bs;
3014 AioContext *replace_aio_context;
3015 int64_t bs_size, replace_size;
3017 bs_size = bdrv_getlength(bs);
3018 if (bs_size < 0) {
3019 error_setg_errno(errp, -bs_size, "Failed to query device's size");
3020 return;
3023 to_replace_bs = check_to_replace_node(bs, replaces, errp);
3024 if (!to_replace_bs) {
3025 return;
3028 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3029 aio_context_acquire(replace_aio_context);
3030 replace_size = bdrv_getlength(to_replace_bs);
3031 aio_context_release(replace_aio_context);
3033 if (replace_size < 0) {
3034 error_setg_errno(errp, -replace_size,
3035 "Failed to query the replacement node's size");
3036 return;
3038 if (bs_size != replace_size) {
3039 error_setg(errp, "cannot replace image with a mirror image of "
3040 "different size");
3041 return;
3045 /* pass the node name to replace to mirror start since it's loose coupling
3046 * and will allow to check whether the node still exist at mirror completion
3048 mirror_start(job_id, bs, target,
3049 replaces, job_flags,
3050 speed, granularity, buf_size, sync, backing_mode, zero_target,
3051 on_source_error, on_target_error, unmap, filter_node_name,
3052 copy_mode, errp);
3055 void qmp_drive_mirror(DriveMirror *arg, Error **errp)
3057 BlockDriverState *bs;
3058 BlockDriverState *target_backing_bs, *target_bs;
3059 AioContext *aio_context;
3060 AioContext *old_context;
3061 BlockMirrorBackingMode backing_mode;
3062 Error *local_err = NULL;
3063 QDict *options = NULL;
3064 int flags;
3065 int64_t size;
3066 const char *format = arg->format;
3067 bool zero_target;
3068 int ret;
3070 bs = qmp_get_root_bs(arg->device, errp);
3071 if (!bs) {
3072 return;
3075 /* Early check to avoid creating target */
3076 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3077 return;
3080 aio_context = bdrv_get_aio_context(bs);
3081 aio_context_acquire(aio_context);
3083 if (!arg->has_mode) {
3084 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3087 if (!arg->format) {
3088 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3089 ? NULL : bs->drv->format_name);
3092 flags = bs->open_flags | BDRV_O_RDWR;
3093 target_backing_bs = bdrv_cow_bs(bdrv_skip_filters(bs));
3094 if (!target_backing_bs && arg->sync == MIRROR_SYNC_MODE_TOP) {
3095 arg->sync = MIRROR_SYNC_MODE_FULL;
3097 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
3098 target_backing_bs = bs;
3101 size = bdrv_getlength(bs);
3102 if (size < 0) {
3103 error_setg_errno(errp, -size, "bdrv_getlength failed");
3104 goto out;
3107 if (arg->replaces) {
3108 if (!arg->node_name) {
3109 error_setg(errp, "a node-name must be provided when replacing a"
3110 " named node of the graph");
3111 goto out;
3115 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
3116 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3117 } else {
3118 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3121 /* Don't open backing image in create() */
3122 flags |= BDRV_O_NO_BACKING;
3124 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !target_backing_bs)
3125 && arg->mode != NEW_IMAGE_MODE_EXISTING)
3127 /* create new image w/o backing file */
3128 assert(format);
3129 bdrv_img_create(arg->target, format,
3130 NULL, NULL, NULL, size, flags, false, &local_err);
3131 } else {
3132 /* Implicit filters should not appear in the filename */
3133 BlockDriverState *explicit_backing =
3134 bdrv_skip_implicit_filters(target_backing_bs);
3136 switch (arg->mode) {
3137 case NEW_IMAGE_MODE_EXISTING:
3138 break;
3139 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3140 /* create new image with backing file */
3141 bdrv_refresh_filename(explicit_backing);
3142 bdrv_img_create(arg->target, format,
3143 explicit_backing->filename,
3144 explicit_backing->drv->format_name,
3145 NULL, size, flags, false, &local_err);
3146 break;
3147 default:
3148 abort();
3152 if (local_err) {
3153 error_propagate(errp, local_err);
3154 goto out;
3157 options = qdict_new();
3158 if (arg->node_name) {
3159 qdict_put_str(options, "node-name", arg->node_name);
3161 if (format) {
3162 qdict_put_str(options, "driver", format);
3165 /* Mirroring takes care of copy-on-write using the source's backing
3166 * file.
3168 target_bs = bdrv_open(arg->target, NULL, options, flags, errp);
3169 if (!target_bs) {
3170 goto out;
3173 zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL &&
3174 (arg->mode == NEW_IMAGE_MODE_EXISTING ||
3175 !bdrv_has_zero_init(target_bs)));
3178 /* Honor bdrv_try_change_aio_context() context acquisition requirements. */
3179 old_context = bdrv_get_aio_context(target_bs);
3180 aio_context_release(aio_context);
3181 aio_context_acquire(old_context);
3183 ret = bdrv_try_change_aio_context(target_bs, aio_context, NULL, errp);
3184 if (ret < 0) {
3185 bdrv_unref(target_bs);
3186 aio_context_release(old_context);
3187 return;
3190 aio_context_release(old_context);
3191 aio_context_acquire(aio_context);
3193 blockdev_mirror_common(arg->job_id, bs, target_bs,
3194 arg->replaces, arg->sync,
3195 backing_mode, zero_target,
3196 arg->has_speed, arg->speed,
3197 arg->has_granularity, arg->granularity,
3198 arg->has_buf_size, arg->buf_size,
3199 arg->has_on_source_error, arg->on_source_error,
3200 arg->has_on_target_error, arg->on_target_error,
3201 arg->has_unmap, arg->unmap,
3202 NULL,
3203 arg->has_copy_mode, arg->copy_mode,
3204 arg->has_auto_finalize, arg->auto_finalize,
3205 arg->has_auto_dismiss, arg->auto_dismiss,
3206 errp);
3207 bdrv_unref(target_bs);
3208 out:
3209 aio_context_release(aio_context);
3212 void qmp_blockdev_mirror(const char *job_id,
3213 const char *device, const char *target,
3214 const char *replaces,
3215 MirrorSyncMode sync,
3216 bool has_speed, int64_t speed,
3217 bool has_granularity, uint32_t granularity,
3218 bool has_buf_size, int64_t buf_size,
3219 bool has_on_source_error,
3220 BlockdevOnError on_source_error,
3221 bool has_on_target_error,
3222 BlockdevOnError on_target_error,
3223 const char *filter_node_name,
3224 bool has_copy_mode, MirrorCopyMode copy_mode,
3225 bool has_auto_finalize, bool auto_finalize,
3226 bool has_auto_dismiss, bool auto_dismiss,
3227 Error **errp)
3229 BlockDriverState *bs;
3230 BlockDriverState *target_bs;
3231 AioContext *aio_context;
3232 AioContext *old_context;
3233 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
3234 bool zero_target;
3235 int ret;
3237 bs = qmp_get_root_bs(device, errp);
3238 if (!bs) {
3239 return;
3242 target_bs = bdrv_lookup_bs(target, target, errp);
3243 if (!target_bs) {
3244 return;
3247 zero_target = (sync == MIRROR_SYNC_MODE_FULL);
3249 /* Honor bdrv_try_change_aio_context() context acquisition requirements. */
3250 old_context = bdrv_get_aio_context(target_bs);
3251 aio_context = bdrv_get_aio_context(bs);
3252 aio_context_acquire(old_context);
3254 ret = bdrv_try_change_aio_context(target_bs, aio_context, NULL, errp);
3256 aio_context_release(old_context);
3257 aio_context_acquire(aio_context);
3259 if (ret < 0) {
3260 goto out;
3263 blockdev_mirror_common(job_id, bs, target_bs,
3264 replaces, sync, backing_mode,
3265 zero_target, has_speed, speed,
3266 has_granularity, granularity,
3267 has_buf_size, buf_size,
3268 has_on_source_error, on_source_error,
3269 has_on_target_error, on_target_error,
3270 true, true, filter_node_name,
3271 has_copy_mode, copy_mode,
3272 has_auto_finalize, auto_finalize,
3273 has_auto_dismiss, auto_dismiss,
3274 errp);
3275 out:
3276 aio_context_release(aio_context);
3280 * Get a block job using its ID. Called with job_mutex held.
3282 static BlockJob *find_block_job_locked(const char *id, Error **errp)
3284 BlockJob *job;
3286 assert(id != NULL);
3288 job = block_job_get_locked(id);
3290 if (!job) {
3291 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3292 "Block job '%s' not found", id);
3293 return NULL;
3296 return job;
3299 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
3301 BlockJob *job;
3303 JOB_LOCK_GUARD();
3304 job = find_block_job_locked(device, errp);
3306 if (!job) {
3307 return;
3310 block_job_set_speed_locked(job, speed, errp);
3313 void qmp_block_job_cancel(const char *device,
3314 bool has_force, bool force, Error **errp)
3316 BlockJob *job;
3318 JOB_LOCK_GUARD();
3319 job = find_block_job_locked(device, errp);
3321 if (!job) {
3322 return;
3325 if (!has_force) {
3326 force = false;
3329 if (job_user_paused_locked(&job->job) && !force) {
3330 error_setg(errp, "The block job for device '%s' is currently paused",
3331 device);
3332 return;
3335 trace_qmp_block_job_cancel(job);
3336 job_user_cancel_locked(&job->job, force, errp);
3339 void qmp_block_job_pause(const char *device, Error **errp)
3341 BlockJob *job;
3343 JOB_LOCK_GUARD();
3344 job = find_block_job_locked(device, errp);
3346 if (!job) {
3347 return;
3350 trace_qmp_block_job_pause(job);
3351 job_user_pause_locked(&job->job, errp);
3354 void qmp_block_job_resume(const char *device, Error **errp)
3356 BlockJob *job;
3358 JOB_LOCK_GUARD();
3359 job = find_block_job_locked(device, errp);
3361 if (!job) {
3362 return;
3365 trace_qmp_block_job_resume(job);
3366 job_user_resume_locked(&job->job, errp);
3369 void qmp_block_job_complete(const char *device, Error **errp)
3371 BlockJob *job;
3373 JOB_LOCK_GUARD();
3374 job = find_block_job_locked(device, errp);
3376 if (!job) {
3377 return;
3380 trace_qmp_block_job_complete(job);
3381 job_complete_locked(&job->job, errp);
3384 void qmp_block_job_finalize(const char *id, Error **errp)
3386 BlockJob *job;
3388 JOB_LOCK_GUARD();
3389 job = find_block_job_locked(id, errp);
3391 if (!job) {
3392 return;
3395 trace_qmp_block_job_finalize(job);
3396 job_ref_locked(&job->job);
3397 job_finalize_locked(&job->job, errp);
3399 job_unref_locked(&job->job);
3402 void qmp_block_job_dismiss(const char *id, Error **errp)
3404 BlockJob *bjob;
3405 Job *job;
3407 JOB_LOCK_GUARD();
3408 bjob = find_block_job_locked(id, errp);
3410 if (!bjob) {
3411 return;
3414 trace_qmp_block_job_dismiss(bjob);
3415 job = &bjob->job;
3416 job_dismiss_locked(&job, errp);
3419 void qmp_change_backing_file(const char *device,
3420 const char *image_node_name,
3421 const char *backing_file,
3422 Error **errp)
3424 BlockDriverState *bs = NULL;
3425 AioContext *aio_context;
3426 BlockDriverState *image_bs = NULL;
3427 Error *local_err = NULL;
3428 bool ro;
3429 int ret;
3431 bs = qmp_get_root_bs(device, errp);
3432 if (!bs) {
3433 return;
3436 aio_context = bdrv_get_aio_context(bs);
3437 aio_context_acquire(aio_context);
3439 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3440 if (local_err) {
3441 error_propagate(errp, local_err);
3442 goto out;
3445 if (!image_bs) {
3446 error_setg(errp, "image file not found");
3447 goto out;
3450 if (bdrv_find_base(image_bs) == image_bs) {
3451 error_setg(errp, "not allowing backing file change on an image "
3452 "without a backing file");
3453 goto out;
3456 /* even though we are not necessarily operating on bs, we need it to
3457 * determine if block ops are currently prohibited on the chain */
3458 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3459 goto out;
3462 /* final sanity check */
3463 if (!bdrv_chain_contains(bs, image_bs)) {
3464 error_setg(errp, "'%s' and image file are not in the same chain",
3465 device);
3466 goto out;
3469 /* if not r/w, reopen to make r/w */
3470 ro = bdrv_is_read_only(image_bs);
3472 if (ro) {
3473 if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) {
3474 goto out;
3478 ret = bdrv_change_backing_file(image_bs, backing_file,
3479 image_bs->drv ? image_bs->drv->format_name : "",
3480 false);
3482 if (ret < 0) {
3483 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3484 backing_file);
3485 /* don't exit here, so we can try to restore open flags if
3486 * appropriate */
3489 if (ro) {
3490 bdrv_reopen_set_read_only(image_bs, true, errp);
3493 out:
3494 aio_context_release(aio_context);
3497 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3499 BlockDriverState *bs;
3500 QObject *obj;
3501 Visitor *v = qobject_output_visitor_new(&obj);
3502 QDict *qdict;
3504 visit_type_BlockdevOptions(v, NULL, &options, &error_abort);
3505 visit_complete(v, &obj);
3506 qdict = qobject_to(QDict, obj);
3508 qdict_flatten(qdict);
3510 if (!qdict_get_try_str(qdict, "node-name")) {
3511 error_setg(errp, "'node-name' must be specified for the root node");
3512 goto fail;
3515 bs = bds_tree_init(qdict, errp);
3516 if (!bs) {
3517 goto fail;
3520 bdrv_set_monitor_owned(bs);
3522 fail:
3523 visit_free(v);
3526 void qmp_blockdev_reopen(BlockdevOptionsList *reopen_list, Error **errp)
3528 BlockReopenQueue *queue = NULL;
3530 /* Add each one of the BDS that we want to reopen to the queue */
3531 for (; reopen_list != NULL; reopen_list = reopen_list->next) {
3532 BlockdevOptions *options = reopen_list->value;
3533 BlockDriverState *bs;
3534 AioContext *ctx;
3535 QObject *obj;
3536 Visitor *v;
3537 QDict *qdict;
3539 /* Check for the selected node name */
3540 if (!options->node_name) {
3541 error_setg(errp, "node-name not specified");
3542 goto fail;
3545 bs = bdrv_find_node(options->node_name);
3546 if (!bs) {
3547 error_setg(errp, "Failed to find node with node-name='%s'",
3548 options->node_name);
3549 goto fail;
3552 /* Put all options in a QDict and flatten it */
3553 v = qobject_output_visitor_new(&obj);
3554 visit_type_BlockdevOptions(v, NULL, &options, &error_abort);
3555 visit_complete(v, &obj);
3556 visit_free(v);
3558 qdict = qobject_to(QDict, obj);
3560 qdict_flatten(qdict);
3562 ctx = bdrv_get_aio_context(bs);
3563 aio_context_acquire(ctx);
3565 queue = bdrv_reopen_queue(queue, bs, qdict, false);
3567 aio_context_release(ctx);
3570 /* Perform the reopen operation */
3571 bdrv_reopen_multiple(queue, errp);
3572 queue = NULL;
3574 fail:
3575 bdrv_reopen_queue_free(queue);
3578 void qmp_blockdev_del(const char *node_name, Error **errp)
3580 AioContext *aio_context;
3581 BlockDriverState *bs;
3583 GLOBAL_STATE_CODE();
3585 bs = bdrv_find_node(node_name);
3586 if (!bs) {
3587 error_setg(errp, "Failed to find node with node-name='%s'", node_name);
3588 return;
3590 if (bdrv_has_blk(bs)) {
3591 error_setg(errp, "Node %s is in use", node_name);
3592 return;
3594 aio_context = bdrv_get_aio_context(bs);
3595 aio_context_acquire(aio_context);
3597 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3598 goto out;
3601 if (!QTAILQ_IN_USE(bs, monitor_list)) {
3602 error_setg(errp, "Node %s is not owned by the monitor",
3603 bs->node_name);
3604 goto out;
3607 if (bs->refcnt > 1) {
3608 error_setg(errp, "Block device %s is in use",
3609 bdrv_get_device_or_node_name(bs));
3610 goto out;
3613 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3614 bdrv_unref(bs);
3616 out:
3617 aio_context_release(aio_context);
3620 static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
3621 const char *child_name)
3623 BdrvChild *child;
3625 QLIST_FOREACH(child, &parent_bs->children, next) {
3626 if (strcmp(child->name, child_name) == 0) {
3627 return child;
3631 return NULL;
3634 void qmp_x_blockdev_change(const char *parent, const char *child,
3635 const char *node, Error **errp)
3637 BlockDriverState *parent_bs, *new_bs = NULL;
3638 BdrvChild *p_child;
3640 parent_bs = bdrv_lookup_bs(parent, parent, errp);
3641 if (!parent_bs) {
3642 return;
3645 if (!child == !node) {
3646 if (child) {
3647 error_setg(errp, "The parameters child and node are in conflict");
3648 } else {
3649 error_setg(errp, "Either child or node must be specified");
3651 return;
3654 if (child) {
3655 p_child = bdrv_find_child(parent_bs, child);
3656 if (!p_child) {
3657 error_setg(errp, "Node '%s' does not have child '%s'",
3658 parent, child);
3659 return;
3661 bdrv_del_child(parent_bs, p_child, errp);
3664 if (node) {
3665 new_bs = bdrv_find_node(node);
3666 if (!new_bs) {
3667 error_setg(errp, "Node '%s' not found", node);
3668 return;
3670 bdrv_add_child(parent_bs, new_bs, errp);
3674 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3676 BlockJobInfoList *head = NULL, **tail = &head;
3677 BlockJob *job;
3679 JOB_LOCK_GUARD();
3681 for (job = block_job_next_locked(NULL); job;
3682 job = block_job_next_locked(job)) {
3683 BlockJobInfo *value;
3685 if (block_job_is_internal(job)) {
3686 continue;
3688 value = block_job_query_locked(job, errp);
3689 if (!value) {
3690 qapi_free_BlockJobInfoList(head);
3691 return NULL;
3693 QAPI_LIST_APPEND(tail, value);
3696 return head;
3699 void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
3700 bool has_force, bool force, Error **errp)
3702 AioContext *old_context;
3703 AioContext *new_context;
3704 BlockDriverState *bs;
3706 bs = bdrv_find_node(node_name);
3707 if (!bs) {
3708 error_setg(errp, "Failed to find node with node-name='%s'", node_name);
3709 return;
3712 /* Protects against accidents. */
3713 if (!(has_force && force) && bdrv_has_blk(bs)) {
3714 error_setg(errp, "Node %s is associated with a BlockBackend and could "
3715 "be in use (use force=true to override this check)",
3716 node_name);
3717 return;
3720 if (iothread->type == QTYPE_QSTRING) {
3721 IOThread *obj = iothread_by_id(iothread->u.s);
3722 if (!obj) {
3723 error_setg(errp, "Cannot find iothread %s", iothread->u.s);
3724 return;
3727 new_context = iothread_get_aio_context(obj);
3728 } else {
3729 new_context = qemu_get_aio_context();
3732 old_context = bdrv_get_aio_context(bs);
3733 aio_context_acquire(old_context);
3735 bdrv_try_change_aio_context(bs, new_context, NULL, errp);
3737 aio_context_release(old_context);
3740 QemuOptsList qemu_common_drive_opts = {
3741 .name = "drive",
3742 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3743 .desc = {
3745 .name = "snapshot",
3746 .type = QEMU_OPT_BOOL,
3747 .help = "enable/disable snapshot mode",
3749 .name = "aio",
3750 .type = QEMU_OPT_STRING,
3751 .help = "host AIO implementation (threads, native, io_uring)",
3753 .name = BDRV_OPT_CACHE_WB,
3754 .type = QEMU_OPT_BOOL,
3755 .help = "Enable writeback mode",
3757 .name = "format",
3758 .type = QEMU_OPT_STRING,
3759 .help = "disk format (raw, qcow2, ...)",
3761 .name = "rerror",
3762 .type = QEMU_OPT_STRING,
3763 .help = "read error action",
3765 .name = "werror",
3766 .type = QEMU_OPT_STRING,
3767 .help = "write error action",
3769 .name = BDRV_OPT_READ_ONLY,
3770 .type = QEMU_OPT_BOOL,
3771 .help = "open drive file as read-only",
3774 THROTTLE_OPTS,
3777 .name = "throttling.group",
3778 .type = QEMU_OPT_STRING,
3779 .help = "name of the block throttling group",
3781 .name = "copy-on-read",
3782 .type = QEMU_OPT_BOOL,
3783 .help = "copy read data from backing file into image file",
3785 .name = "detect-zeroes",
3786 .type = QEMU_OPT_STRING,
3787 .help = "try to optimize zero writes (off, on, unmap)",
3789 .name = "stats-account-invalid",
3790 .type = QEMU_OPT_BOOL,
3791 .help = "whether to account for invalid I/O operations "
3792 "in the statistics",
3794 .name = "stats-account-failed",
3795 .type = QEMU_OPT_BOOL,
3796 .help = "whether to account for failed I/O operations "
3797 "in the statistics",
3799 { /* end of list */ }
3803 QemuOptsList qemu_drive_opts = {
3804 .name = "drive",
3805 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3806 .desc = {
3808 * no elements => accept any params
3809 * validation will happen later
3811 { /* end of list */ }