i.MX: Add code to emulate i.MX2 watchdog IP block
[qemu/ar7.git] / blockdev.c
blob8e977eef11f38a50acd736b9c9e37c4fade6cedb
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/throttle-groups.h"
39 #include "monitor/monitor.h"
40 #include "qemu/error-report.h"
41 #include "qemu/option.h"
42 #include "qemu/config-file.h"
43 #include "qapi/qmp/types.h"
44 #include "qapi-visit.h"
45 #include "qapi/qmp/qerror.h"
46 #include "qapi/qobject-output-visitor.h"
47 #include "sysemu/sysemu.h"
48 #include "sysemu/iothread.h"
49 #include "block/block_int.h"
50 #include "qmp-commands.h"
51 #include "block/trace.h"
52 #include "sysemu/arch_init.h"
53 #include "sysemu/qtest.h"
54 #include "qemu/cutils.h"
55 #include "qemu/help_option.h"
56 #include "qemu/throttle-options.h"
58 static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
59 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
61 static int do_open_tray(const char *blk_name, const char *qdev_id,
62 bool force, Error **errp);
63 static void blockdev_remove_medium(bool has_device, const char *device,
64 bool has_id, const char *id, Error **errp);
65 static void blockdev_insert_medium(bool has_device, const char *device,
66 bool has_id, const char *id,
67 const char *node_name, Error **errp);
69 static const char *const if_name[IF_COUNT] = {
70 [IF_NONE] = "none",
71 [IF_IDE] = "ide",
72 [IF_SCSI] = "scsi",
73 [IF_FLOPPY] = "floppy",
74 [IF_PFLASH] = "pflash",
75 [IF_MTD] = "mtd",
76 [IF_SD] = "sd",
77 [IF_VIRTIO] = "virtio",
78 [IF_XEN] = "xen",
81 static int if_max_devs[IF_COUNT] = {
83 * Do not change these numbers! They govern how drive option
84 * index maps to unit and bus. That mapping is ABI.
86 * All controllers used to implement if=T drives need to support
87 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
88 * Otherwise, some index values map to "impossible" bus, unit
89 * values.
91 * For instance, if you change [IF_SCSI] to 255, -drive
92 * if=scsi,index=12 no longer means bus=1,unit=5, but
93 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
94 * the drive can't be set up. Regression.
96 [IF_IDE] = 2,
97 [IF_SCSI] = 7,
101 * Boards may call this to offer board-by-board overrides
102 * of the default, global values.
104 void override_max_devs(BlockInterfaceType type, int max_devs)
106 BlockBackend *blk;
107 DriveInfo *dinfo;
109 if (max_devs <= 0) {
110 return;
113 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
114 dinfo = blk_legacy_dinfo(blk);
115 if (dinfo->type == type) {
116 fprintf(stderr, "Cannot override units-per-bus property of"
117 " the %s interface, because a drive of that type has"
118 " already been added.\n", if_name[type]);
119 g_assert_not_reached();
123 if_max_devs[type] = max_devs;
127 * We automatically delete the drive when a device using it gets
128 * unplugged. Questionable feature, but we can't just drop it.
129 * Device models call blockdev_mark_auto_del() to schedule the
130 * automatic deletion, and generic qdev code calls blockdev_auto_del()
131 * when deletion is actually safe.
133 void blockdev_mark_auto_del(BlockBackend *blk)
135 DriveInfo *dinfo = blk_legacy_dinfo(blk);
136 BlockDriverState *bs = blk_bs(blk);
137 AioContext *aio_context;
139 if (!dinfo) {
140 return;
143 if (bs) {
144 aio_context = bdrv_get_aio_context(bs);
145 aio_context_acquire(aio_context);
147 if (bs->job) {
148 block_job_cancel(bs->job);
151 aio_context_release(aio_context);
154 dinfo->auto_del = 1;
157 void blockdev_auto_del(BlockBackend *blk)
159 DriveInfo *dinfo = blk_legacy_dinfo(blk);
161 if (dinfo && dinfo->auto_del) {
162 monitor_remove_blk(blk);
163 blk_unref(blk);
168 * Returns the current mapping of how many units per bus
169 * a particular interface can support.
171 * A positive integer indicates n units per bus.
172 * 0 implies the mapping has not been established.
173 * -1 indicates an invalid BlockInterfaceType was given.
175 int drive_get_max_devs(BlockInterfaceType type)
177 if (type >= IF_IDE && type < IF_COUNT) {
178 return if_max_devs[type];
181 return -1;
184 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
186 int max_devs = if_max_devs[type];
187 return max_devs ? index / max_devs : 0;
190 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
192 int max_devs = if_max_devs[type];
193 return max_devs ? index % max_devs : index;
196 QemuOpts *drive_def(const char *optstr)
198 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
201 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
202 const char *optstr)
204 QemuOpts *opts;
206 opts = drive_def(optstr);
207 if (!opts) {
208 return NULL;
210 if (type != IF_DEFAULT) {
211 qemu_opt_set(opts, "if", if_name[type], &error_abort);
213 if (index >= 0) {
214 qemu_opt_set_number(opts, "index", index, &error_abort);
216 if (file)
217 qemu_opt_set(opts, "file", file, &error_abort);
218 return opts;
221 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
223 BlockBackend *blk;
224 DriveInfo *dinfo;
226 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
227 dinfo = blk_legacy_dinfo(blk);
228 if (dinfo && dinfo->type == type
229 && dinfo->bus == bus && dinfo->unit == unit) {
230 return dinfo;
234 return NULL;
237 void drive_check_orphaned(void)
239 BlockBackend *blk;
240 DriveInfo *dinfo;
241 Location loc;
242 bool orphans = false;
244 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
245 dinfo = blk_legacy_dinfo(blk);
246 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
247 dinfo->type != IF_NONE) {
248 loc_push_none(&loc);
249 qemu_opts_loc_restore(dinfo->opts);
250 error_report("machine type does not support"
251 " if=%s,bus=%d,unit=%d",
252 if_name[dinfo->type], dinfo->bus, dinfo->unit);
253 loc_pop(&loc);
254 orphans = true;
258 if (orphans) {
259 exit(1);
263 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
265 return drive_get(type,
266 drive_index_to_bus_id(type, index),
267 drive_index_to_unit_id(type, index));
270 int drive_get_max_bus(BlockInterfaceType type)
272 int max_bus;
273 BlockBackend *blk;
274 DriveInfo *dinfo;
276 max_bus = -1;
277 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
278 dinfo = blk_legacy_dinfo(blk);
279 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
280 max_bus = dinfo->bus;
283 return max_bus;
286 /* Get a block device. This should only be used for single-drive devices
287 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
288 appropriate bus. */
289 DriveInfo *drive_get_next(BlockInterfaceType type)
291 static int next_block_unit[IF_COUNT];
293 return drive_get(type, 0, next_block_unit[type]++);
296 static void bdrv_format_print(void *opaque, const char *name)
298 error_printf(" %s", name);
301 typedef struct {
302 QEMUBH *bh;
303 BlockDriverState *bs;
304 } BDRVPutRefBH;
306 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
308 if (!strcmp(buf, "ignore")) {
309 return BLOCKDEV_ON_ERROR_IGNORE;
310 } else if (!is_read && !strcmp(buf, "enospc")) {
311 return BLOCKDEV_ON_ERROR_ENOSPC;
312 } else if (!strcmp(buf, "stop")) {
313 return BLOCKDEV_ON_ERROR_STOP;
314 } else if (!strcmp(buf, "report")) {
315 return BLOCKDEV_ON_ERROR_REPORT;
316 } else {
317 error_setg(errp, "'%s' invalid %s error action",
318 buf, is_read ? "read" : "write");
319 return -1;
323 static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
324 Error **errp)
326 const QListEntry *entry;
327 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
328 switch (qobject_type(entry->value)) {
330 case QTYPE_QSTRING: {
331 unsigned long long length;
332 const char *str = qstring_get_str(qobject_to_qstring(entry->value));
333 if (parse_uint_full(str, &length, 10) == 0 &&
334 length > 0 && length <= UINT_MAX) {
335 block_acct_add_interval(stats, (unsigned) length);
336 } else {
337 error_setg(errp, "Invalid interval length: %s", str);
338 return false;
340 break;
343 case QTYPE_QNUM: {
344 int64_t length = qnum_get_int(qobject_to_qnum(entry->value));
346 if (length > 0 && length <= UINT_MAX) {
347 block_acct_add_interval(stats, (unsigned) length);
348 } else {
349 error_setg(errp, "Invalid interval length: %" PRId64, length);
350 return false;
352 break;
355 default:
356 error_setg(errp, "The specification of stats-intervals is invalid");
357 return false;
360 return true;
363 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
365 /* All parameters but @opts are optional and may be set to NULL. */
366 static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
367 const char **throttling_group, ThrottleConfig *throttle_cfg,
368 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
370 Error *local_error = NULL;
371 const char *aio;
373 if (bdrv_flags) {
374 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
375 *bdrv_flags |= BDRV_O_COPY_ON_READ;
378 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
379 if (!strcmp(aio, "native")) {
380 *bdrv_flags |= BDRV_O_NATIVE_AIO;
381 } else if (!strcmp(aio, "threads")) {
382 /* this is the default */
383 } else {
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 /* Takes the ownership of bs_opts */
458 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
459 Error **errp)
461 const char *buf;
462 int bdrv_flags = 0;
463 int on_read_error, on_write_error;
464 bool account_invalid, account_failed;
465 bool writethrough, read_only;
466 BlockBackend *blk;
467 BlockDriverState *bs;
468 ThrottleConfig cfg;
469 int snapshot = 0;
470 Error *error = NULL;
471 QemuOpts *opts;
472 QDict *interval_dict = NULL;
473 QList *interval_list = NULL;
474 const char *id;
475 BlockdevDetectZeroesOptions detect_zeroes =
476 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
477 const char *throttling_group = NULL;
479 /* Check common options by copying from bs_opts to opts, all other options
480 * stay in bs_opts for processing by bdrv_open(). */
481 id = qdict_get_try_str(bs_opts, "id");
482 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
483 if (error) {
484 error_propagate(errp, error);
485 goto err_no_opts;
488 qemu_opts_absorb_qdict(opts, bs_opts, &error);
489 if (error) {
490 error_propagate(errp, error);
491 goto early_err;
494 if (id) {
495 qdict_del(bs_opts, "id");
498 /* extract parameters */
499 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
501 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
502 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
504 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
506 id = qemu_opts_id(opts);
508 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
509 qdict_array_split(interval_dict, &interval_list);
511 if (qdict_size(interval_dict) != 0) {
512 error_setg(errp, "Invalid option stats-intervals.%s",
513 qdict_first(interval_dict)->key);
514 goto early_err;
517 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
518 &detect_zeroes, &error);
519 if (error) {
520 error_propagate(errp, error);
521 goto early_err;
524 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
525 if (is_help_option(buf)) {
526 error_printf("Supported formats:");
527 bdrv_iterate_format(bdrv_format_print, NULL);
528 error_printf("\n");
529 goto early_err;
532 if (qdict_haskey(bs_opts, "driver")) {
533 error_setg(errp, "Cannot specify both 'driver' and 'format'");
534 goto early_err;
536 qdict_put_str(bs_opts, "driver", buf);
539 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
540 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
541 on_write_error = parse_block_error_action(buf, 0, &error);
542 if (error) {
543 error_propagate(errp, error);
544 goto early_err;
548 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
549 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
550 on_read_error = parse_block_error_action(buf, 1, &error);
551 if (error) {
552 error_propagate(errp, error);
553 goto early_err;
557 if (snapshot) {
558 bdrv_flags |= BDRV_O_SNAPSHOT;
561 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
563 /* init */
564 if ((!file || !*file) && !qdict_size(bs_opts)) {
565 BlockBackendRootState *blk_rs;
567 blk = blk_new(0, BLK_PERM_ALL);
568 blk_rs = blk_get_root_state(blk);
569 blk_rs->open_flags = bdrv_flags;
570 blk_rs->read_only = read_only;
571 blk_rs->detect_zeroes = detect_zeroes;
573 QDECREF(bs_opts);
574 } else {
575 if (file && !*file) {
576 file = NULL;
579 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
580 * with other callers) rather than what we want as the real defaults.
581 * Apply the defaults here instead. */
582 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
583 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
584 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
585 read_only ? "on" : "off");
586 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
588 if (runstate_check(RUN_STATE_INMIGRATE)) {
589 bdrv_flags |= BDRV_O_INACTIVE;
592 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
593 if (!blk) {
594 goto err_no_bs_opts;
596 bs = blk_bs(blk);
598 bs->detect_zeroes = detect_zeroes;
600 block_acct_setup(blk_get_stats(blk), account_invalid, account_failed);
602 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
603 blk_unref(blk);
604 blk = NULL;
605 goto err_no_bs_opts;
609 /* disk I/O throttling */
610 if (throttle_enabled(&cfg)) {
611 if (!throttling_group) {
612 throttling_group = id;
614 blk_io_limits_enable(blk, throttling_group);
615 blk_set_io_limits(blk, &cfg);
618 blk_set_enable_write_cache(blk, !writethrough);
619 blk_set_on_error(blk, on_read_error, on_write_error);
621 if (!monitor_add_blk(blk, id, errp)) {
622 blk_unref(blk);
623 blk = NULL;
624 goto err_no_bs_opts;
627 err_no_bs_opts:
628 qemu_opts_del(opts);
629 QDECREF(interval_dict);
630 QDECREF(interval_list);
631 return blk;
633 early_err:
634 qemu_opts_del(opts);
635 QDECREF(interval_dict);
636 QDECREF(interval_list);
637 err_no_opts:
638 QDECREF(bs_opts);
639 return NULL;
642 /* Takes the ownership of bs_opts */
643 static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
645 int bdrv_flags = 0;
647 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
648 * with other callers) rather than what we want as the real defaults.
649 * Apply the defaults here instead. */
650 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
651 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
652 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
654 if (runstate_check(RUN_STATE_INMIGRATE)) {
655 bdrv_flags |= BDRV_O_INACTIVE;
658 return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
661 void blockdev_close_all_bdrv_states(void)
663 BlockDriverState *bs, *next_bs;
665 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
666 AioContext *ctx = bdrv_get_aio_context(bs);
668 aio_context_acquire(ctx);
669 bdrv_unref(bs);
670 aio_context_release(ctx);
674 /* Iterates over the list of monitor-owned BlockDriverStates */
675 BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
677 return bs ? QTAILQ_NEXT(bs, monitor_list)
678 : QTAILQ_FIRST(&monitor_bdrv_states);
681 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
682 Error **errp)
684 const char *value;
686 value = qemu_opt_get(opts, from);
687 if (value) {
688 if (qemu_opt_find(opts, to)) {
689 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
690 "same time", to, from);
691 return;
695 /* rename all items in opts */
696 while ((value = qemu_opt_get(opts, from))) {
697 qemu_opt_set(opts, to, value, &error_abort);
698 qemu_opt_unset(opts, from);
702 QemuOptsList qemu_legacy_drive_opts = {
703 .name = "drive",
704 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
705 .desc = {
707 .name = "bus",
708 .type = QEMU_OPT_NUMBER,
709 .help = "bus number",
711 .name = "unit",
712 .type = QEMU_OPT_NUMBER,
713 .help = "unit number (i.e. lun for scsi)",
715 .name = "index",
716 .type = QEMU_OPT_NUMBER,
717 .help = "index number",
719 .name = "media",
720 .type = QEMU_OPT_STRING,
721 .help = "media type (disk, cdrom)",
723 .name = "if",
724 .type = QEMU_OPT_STRING,
725 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
727 .name = "cyls",
728 .type = QEMU_OPT_NUMBER,
729 .help = "number of cylinders (ide disk geometry)",
731 .name = "heads",
732 .type = QEMU_OPT_NUMBER,
733 .help = "number of heads (ide disk geometry)",
735 .name = "secs",
736 .type = QEMU_OPT_NUMBER,
737 .help = "number of sectors (ide disk geometry)",
739 .name = "trans",
740 .type = QEMU_OPT_STRING,
741 .help = "chs translation (auto, lba, none)",
743 .name = "addr",
744 .type = QEMU_OPT_STRING,
745 .help = "pci address (virtio only)",
747 .name = "serial",
748 .type = QEMU_OPT_STRING,
749 .help = "disk serial number",
751 .name = "file",
752 .type = QEMU_OPT_STRING,
753 .help = "file name",
756 /* Options that are passed on, but have special semantics with -drive */
758 .name = BDRV_OPT_READ_ONLY,
759 .type = QEMU_OPT_BOOL,
760 .help = "open drive file as read-only",
762 .name = "rerror",
763 .type = QEMU_OPT_STRING,
764 .help = "read error action",
766 .name = "werror",
767 .type = QEMU_OPT_STRING,
768 .help = "write error action",
770 .name = "copy-on-read",
771 .type = QEMU_OPT_BOOL,
772 .help = "copy read data from backing file into image file",
775 { /* end of list */ }
779 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
781 const char *value;
782 BlockBackend *blk;
783 DriveInfo *dinfo = NULL;
784 QDict *bs_opts;
785 QemuOpts *legacy_opts;
786 DriveMediaType media = MEDIA_DISK;
787 BlockInterfaceType type;
788 int cyls, heads, secs, translation;
789 int max_devs, bus_id, unit_id, index;
790 const char *devaddr;
791 const char *werror, *rerror;
792 bool read_only = false;
793 bool copy_on_read;
794 const char *serial;
795 const char *filename;
796 Error *local_err = NULL;
797 int i;
798 const char *deprecated[] = {
799 "serial", "trans", "secs", "heads", "cyls", "addr"
802 /* Change legacy command line options into QMP ones */
803 static const struct {
804 const char *from;
805 const char *to;
806 } opt_renames[] = {
807 { "iops", "throttling.iops-total" },
808 { "iops_rd", "throttling.iops-read" },
809 { "iops_wr", "throttling.iops-write" },
811 { "bps", "throttling.bps-total" },
812 { "bps_rd", "throttling.bps-read" },
813 { "bps_wr", "throttling.bps-write" },
815 { "iops_max", "throttling.iops-total-max" },
816 { "iops_rd_max", "throttling.iops-read-max" },
817 { "iops_wr_max", "throttling.iops-write-max" },
819 { "bps_max", "throttling.bps-total-max" },
820 { "bps_rd_max", "throttling.bps-read-max" },
821 { "bps_wr_max", "throttling.bps-write-max" },
823 { "iops_size", "throttling.iops-size" },
825 { "group", "throttling.group" },
827 { "readonly", BDRV_OPT_READ_ONLY },
830 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
831 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
832 &local_err);
833 if (local_err) {
834 error_report_err(local_err);
835 return NULL;
839 value = qemu_opt_get(all_opts, "cache");
840 if (value) {
841 int flags = 0;
842 bool writethrough;
844 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
845 error_report("invalid cache option");
846 return NULL;
849 /* Specific options take precedence */
850 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
851 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
852 !writethrough, &error_abort);
854 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
855 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
856 !!(flags & BDRV_O_NOCACHE), &error_abort);
858 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
859 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
860 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
862 qemu_opt_unset(all_opts, "cache");
865 /* Get a QDict for processing the options */
866 bs_opts = qdict_new();
867 qemu_opts_to_qdict(all_opts, bs_opts);
869 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
870 &error_abort);
871 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
872 if (local_err) {
873 error_report_err(local_err);
874 goto fail;
877 /* Other deprecated options */
878 if (!qtest_enabled()) {
879 for (i = 0; i < ARRAY_SIZE(deprecated); i++) {
880 if (qemu_opt_get(legacy_opts, deprecated[i]) != NULL) {
881 error_report("'%s' is deprecated, please use the corresponding "
882 "option of '-device' instead", deprecated[i]);
887 /* Media type */
888 value = qemu_opt_get(legacy_opts, "media");
889 if (value) {
890 if (!strcmp(value, "disk")) {
891 media = MEDIA_DISK;
892 } else if (!strcmp(value, "cdrom")) {
893 media = MEDIA_CDROM;
894 read_only = true;
895 } else {
896 error_report("'%s' invalid media", value);
897 goto fail;
901 /* copy-on-read is disabled with a warning for read-only devices */
902 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
903 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
905 if (read_only && copy_on_read) {
906 warn_report("disabling copy-on-read on read-only drive");
907 copy_on_read = false;
910 qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off");
911 qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off");
913 /* Controller type */
914 value = qemu_opt_get(legacy_opts, "if");
915 if (value) {
916 for (type = 0;
917 type < IF_COUNT && strcmp(value, if_name[type]);
918 type++) {
920 if (type == IF_COUNT) {
921 error_report("unsupported bus type '%s'", value);
922 goto fail;
924 } else {
925 type = block_default_type;
928 /* Geometry */
929 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
930 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
931 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
933 if (cyls || heads || secs) {
934 if (cyls < 1) {
935 error_report("invalid physical cyls number");
936 goto fail;
938 if (heads < 1) {
939 error_report("invalid physical heads number");
940 goto fail;
942 if (secs < 1) {
943 error_report("invalid physical secs number");
944 goto fail;
948 translation = BIOS_ATA_TRANSLATION_AUTO;
949 value = qemu_opt_get(legacy_opts, "trans");
950 if (value != NULL) {
951 if (!cyls) {
952 error_report("'%s' trans must be used with cyls, heads and secs",
953 value);
954 goto fail;
956 if (!strcmp(value, "none")) {
957 translation = BIOS_ATA_TRANSLATION_NONE;
958 } else if (!strcmp(value, "lba")) {
959 translation = BIOS_ATA_TRANSLATION_LBA;
960 } else if (!strcmp(value, "large")) {
961 translation = BIOS_ATA_TRANSLATION_LARGE;
962 } else if (!strcmp(value, "rechs")) {
963 translation = BIOS_ATA_TRANSLATION_RECHS;
964 } else if (!strcmp(value, "auto")) {
965 translation = BIOS_ATA_TRANSLATION_AUTO;
966 } else {
967 error_report("'%s' invalid translation type", value);
968 goto fail;
972 if (media == MEDIA_CDROM) {
973 if (cyls || secs || heads) {
974 error_report("CHS can't be set with media=cdrom");
975 goto fail;
979 /* Device address specified by bus/unit or index.
980 * If none was specified, try to find the first free one. */
981 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
982 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
983 index = qemu_opt_get_number(legacy_opts, "index", -1);
985 max_devs = if_max_devs[type];
987 if (index != -1) {
988 if (bus_id != 0 || unit_id != -1) {
989 error_report("index cannot be used with bus and unit");
990 goto fail;
992 bus_id = drive_index_to_bus_id(type, index);
993 unit_id = drive_index_to_unit_id(type, index);
996 if (unit_id == -1) {
997 unit_id = 0;
998 while (drive_get(type, bus_id, unit_id) != NULL) {
999 unit_id++;
1000 if (max_devs && unit_id >= max_devs) {
1001 unit_id -= max_devs;
1002 bus_id++;
1007 if (max_devs && unit_id >= max_devs) {
1008 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1009 goto fail;
1012 if (drive_get(type, bus_id, unit_id) != NULL) {
1013 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1014 bus_id, unit_id, index);
1015 goto fail;
1018 /* Serial number */
1019 serial = qemu_opt_get(legacy_opts, "serial");
1021 /* no id supplied -> create one */
1022 if (qemu_opts_id(all_opts) == NULL) {
1023 char *new_id;
1024 const char *mediastr = "";
1025 if (type == IF_IDE || type == IF_SCSI) {
1026 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1028 if (max_devs) {
1029 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1030 mediastr, unit_id);
1031 } else {
1032 new_id = g_strdup_printf("%s%s%i", if_name[type],
1033 mediastr, unit_id);
1035 qdict_put_str(bs_opts, "id", new_id);
1036 g_free(new_id);
1039 /* Add virtio block device */
1040 devaddr = qemu_opt_get(legacy_opts, "addr");
1041 if (devaddr && type != IF_VIRTIO) {
1042 error_report("addr is not supported by this bus type");
1043 goto fail;
1046 if (type == IF_VIRTIO) {
1047 QemuOpts *devopts;
1048 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1049 &error_abort);
1050 if (arch_type == QEMU_ARCH_S390X) {
1051 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
1052 } else {
1053 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
1055 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1056 &error_abort);
1057 if (devaddr) {
1058 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
1062 filename = qemu_opt_get(legacy_opts, "file");
1064 /* Check werror/rerror compatibility with if=... */
1065 werror = qemu_opt_get(legacy_opts, "werror");
1066 if (werror != NULL) {
1067 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1068 type != IF_NONE) {
1069 error_report("werror is not supported by this bus type");
1070 goto fail;
1072 qdict_put_str(bs_opts, "werror", werror);
1075 rerror = qemu_opt_get(legacy_opts, "rerror");
1076 if (rerror != NULL) {
1077 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1078 type != IF_NONE) {
1079 error_report("rerror is not supported by this bus type");
1080 goto fail;
1082 qdict_put_str(bs_opts, "rerror", rerror);
1085 /* Actual block device init: Functionality shared with blockdev-add */
1086 blk = blockdev_init(filename, bs_opts, &local_err);
1087 bs_opts = NULL;
1088 if (!blk) {
1089 if (local_err) {
1090 error_report_err(local_err);
1092 goto fail;
1093 } else {
1094 assert(!local_err);
1097 /* Create legacy DriveInfo */
1098 dinfo = g_malloc0(sizeof(*dinfo));
1099 dinfo->opts = all_opts;
1101 dinfo->cyls = cyls;
1102 dinfo->heads = heads;
1103 dinfo->secs = secs;
1104 dinfo->trans = translation;
1106 dinfo->type = type;
1107 dinfo->bus = bus_id;
1108 dinfo->unit = unit_id;
1109 dinfo->devaddr = devaddr;
1110 dinfo->serial = g_strdup(serial);
1112 blk_set_legacy_dinfo(blk, dinfo);
1114 switch(type) {
1115 case IF_IDE:
1116 case IF_SCSI:
1117 case IF_XEN:
1118 case IF_NONE:
1119 dinfo->media_cd = media == MEDIA_CDROM;
1120 break;
1121 default:
1122 break;
1125 fail:
1126 qemu_opts_del(legacy_opts);
1127 QDECREF(bs_opts);
1128 return dinfo;
1131 static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1133 BlockDriverState *bs;
1135 bs = bdrv_lookup_bs(name, name, errp);
1136 if (bs == NULL) {
1137 return NULL;
1140 if (!bdrv_is_root_node(bs)) {
1141 error_setg(errp, "Need a root block node");
1142 return NULL;
1145 if (!bdrv_is_inserted(bs)) {
1146 error_setg(errp, "Device has no medium");
1147 return NULL;
1150 return bs;
1153 static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id,
1154 Error **errp)
1156 BlockBackend *blk;
1158 if (!blk_name == !qdev_id) {
1159 error_setg(errp, "Need exactly one of 'device' and 'id'");
1160 return NULL;
1163 if (qdev_id) {
1164 blk = blk_by_qdev_id(qdev_id, errp);
1165 } else {
1166 blk = blk_by_name(blk_name);
1167 if (blk == NULL) {
1168 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1169 "Device '%s' not found", blk_name);
1173 return blk;
1176 void hmp_commit(Monitor *mon, const QDict *qdict)
1178 const char *device = qdict_get_str(qdict, "device");
1179 BlockBackend *blk;
1180 int ret;
1182 if (!strcmp(device, "all")) {
1183 ret = blk_commit_all();
1184 } else {
1185 BlockDriverState *bs;
1186 AioContext *aio_context;
1188 blk = blk_by_name(device);
1189 if (!blk) {
1190 monitor_printf(mon, "Device '%s' not found\n", device);
1191 return;
1193 if (!blk_is_available(blk)) {
1194 monitor_printf(mon, "Device '%s' has no medium\n", device);
1195 return;
1198 bs = blk_bs(blk);
1199 aio_context = bdrv_get_aio_context(bs);
1200 aio_context_acquire(aio_context);
1202 ret = bdrv_commit(bs);
1204 aio_context_release(aio_context);
1206 if (ret < 0) {
1207 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1208 strerror(-ret));
1212 static void blockdev_do_action(TransactionAction *action, Error **errp)
1214 TransactionActionList list;
1216 list.value = action;
1217 list.next = NULL;
1218 qmp_transaction(&list, false, NULL, errp);
1221 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1222 bool has_node_name, const char *node_name,
1223 const char *snapshot_file,
1224 bool has_snapshot_node_name,
1225 const char *snapshot_node_name,
1226 bool has_format, const char *format,
1227 bool has_mode, NewImageMode mode, Error **errp)
1229 BlockdevSnapshotSync snapshot = {
1230 .has_device = has_device,
1231 .device = (char *) device,
1232 .has_node_name = has_node_name,
1233 .node_name = (char *) node_name,
1234 .snapshot_file = (char *) snapshot_file,
1235 .has_snapshot_node_name = has_snapshot_node_name,
1236 .snapshot_node_name = (char *) snapshot_node_name,
1237 .has_format = has_format,
1238 .format = (char *) format,
1239 .has_mode = has_mode,
1240 .mode = mode,
1242 TransactionAction action = {
1243 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1244 .u.blockdev_snapshot_sync.data = &snapshot,
1246 blockdev_do_action(&action, errp);
1249 void qmp_blockdev_snapshot(const char *node, const char *overlay,
1250 Error **errp)
1252 BlockdevSnapshot snapshot_data = {
1253 .node = (char *) node,
1254 .overlay = (char *) overlay
1256 TransactionAction action = {
1257 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
1258 .u.blockdev_snapshot.data = &snapshot_data,
1260 blockdev_do_action(&action, errp);
1263 void qmp_blockdev_snapshot_internal_sync(const char *device,
1264 const char *name,
1265 Error **errp)
1267 BlockdevSnapshotInternal snapshot = {
1268 .device = (char *) device,
1269 .name = (char *) name
1271 TransactionAction action = {
1272 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1273 .u.blockdev_snapshot_internal_sync.data = &snapshot,
1275 blockdev_do_action(&action, errp);
1278 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1279 bool has_id,
1280 const char *id,
1281 bool has_name,
1282 const char *name,
1283 Error **errp)
1285 BlockDriverState *bs;
1286 AioContext *aio_context;
1287 QEMUSnapshotInfo sn;
1288 Error *local_err = NULL;
1289 SnapshotInfo *info = NULL;
1290 int ret;
1292 bs = qmp_get_root_bs(device, errp);
1293 if (!bs) {
1294 return NULL;
1296 aio_context = bdrv_get_aio_context(bs);
1297 aio_context_acquire(aio_context);
1299 if (!has_id) {
1300 id = NULL;
1303 if (!has_name) {
1304 name = NULL;
1307 if (!id && !name) {
1308 error_setg(errp, "Name or id must be provided");
1309 goto out_aio_context;
1312 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1313 goto out_aio_context;
1316 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1317 if (local_err) {
1318 error_propagate(errp, local_err);
1319 goto out_aio_context;
1321 if (!ret) {
1322 error_setg(errp,
1323 "Snapshot with id '%s' and name '%s' does not exist on "
1324 "device '%s'",
1325 STR_OR_NULL(id), STR_OR_NULL(name), device);
1326 goto out_aio_context;
1329 bdrv_snapshot_delete(bs, id, name, &local_err);
1330 if (local_err) {
1331 error_propagate(errp, local_err);
1332 goto out_aio_context;
1335 aio_context_release(aio_context);
1337 info = g_new0(SnapshotInfo, 1);
1338 info->id = g_strdup(sn.id_str);
1339 info->name = g_strdup(sn.name);
1340 info->date_nsec = sn.date_nsec;
1341 info->date_sec = sn.date_sec;
1342 info->vm_state_size = sn.vm_state_size;
1343 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1344 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1346 return info;
1348 out_aio_context:
1349 aio_context_release(aio_context);
1350 return NULL;
1354 * block_dirty_bitmap_lookup:
1355 * Return a dirty bitmap (if present), after validating
1356 * the node reference and bitmap names.
1358 * @node: The name of the BDS node to search for bitmaps
1359 * @name: The name of the bitmap to search for
1360 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1361 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1362 * @errp: Output pointer for error information. Can be NULL.
1364 * @return: A bitmap object on success, or NULL on failure.
1366 static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1367 const char *name,
1368 BlockDriverState **pbs,
1369 Error **errp)
1371 BlockDriverState *bs;
1372 BdrvDirtyBitmap *bitmap;
1374 if (!node) {
1375 error_setg(errp, "Node cannot be NULL");
1376 return NULL;
1378 if (!name) {
1379 error_setg(errp, "Bitmap name cannot be NULL");
1380 return NULL;
1382 bs = bdrv_lookup_bs(node, node, NULL);
1383 if (!bs) {
1384 error_setg(errp, "Node '%s' not found", node);
1385 return NULL;
1388 bitmap = bdrv_find_dirty_bitmap(bs, name);
1389 if (!bitmap) {
1390 error_setg(errp, "Dirty bitmap '%s' not found", name);
1391 return NULL;
1394 if (pbs) {
1395 *pbs = bs;
1398 return bitmap;
1401 /* New and old BlockDriverState structs for atomic group operations */
1403 typedef struct BlkActionState BlkActionState;
1406 * BlkActionOps:
1407 * Table of operations that define an Action.
1409 * @instance_size: Size of state struct, in bytes.
1410 * @prepare: Prepare the work, must NOT be NULL.
1411 * @commit: Commit the changes, can be NULL.
1412 * @abort: Abort the changes on fail, can be NULL.
1413 * @clean: Clean up resources after all transaction actions have called
1414 * commit() or abort(). Can be NULL.
1416 * Only prepare() may fail. In a single transaction, only one of commit() or
1417 * abort() will be called. clean() will always be called if it is present.
1419 typedef struct BlkActionOps {
1420 size_t instance_size;
1421 void (*prepare)(BlkActionState *common, Error **errp);
1422 void (*commit)(BlkActionState *common);
1423 void (*abort)(BlkActionState *common);
1424 void (*clean)(BlkActionState *common);
1425 } BlkActionOps;
1428 * BlkActionState:
1429 * Describes one Action's state within a Transaction.
1431 * @action: QAPI-defined enum identifying which Action to perform.
1432 * @ops: Table of ActionOps this Action can perform.
1433 * @block_job_txn: Transaction which this action belongs to.
1434 * @entry: List membership for all Actions in this Transaction.
1436 * This structure must be arranged as first member in a subclassed type,
1437 * assuming that the compiler will also arrange it to the same offsets as the
1438 * base class.
1440 struct BlkActionState {
1441 TransactionAction *action;
1442 const BlkActionOps *ops;
1443 BlockJobTxn *block_job_txn;
1444 TransactionProperties *txn_props;
1445 QSIMPLEQ_ENTRY(BlkActionState) entry;
1448 /* internal snapshot private data */
1449 typedef struct InternalSnapshotState {
1450 BlkActionState common;
1451 BlockDriverState *bs;
1452 QEMUSnapshotInfo sn;
1453 bool created;
1454 } InternalSnapshotState;
1457 static int action_check_completion_mode(BlkActionState *s, Error **errp)
1459 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1460 error_setg(errp,
1461 "Action '%s' does not support Transaction property "
1462 "completion-mode = %s",
1463 TransactionActionKind_str(s->action->type),
1464 ActionCompletionMode_str(s->txn_props->completion_mode));
1465 return -1;
1467 return 0;
1470 static void internal_snapshot_prepare(BlkActionState *common,
1471 Error **errp)
1473 Error *local_err = NULL;
1474 const char *device;
1475 const char *name;
1476 BlockDriverState *bs;
1477 QEMUSnapshotInfo old_sn, *sn;
1478 bool ret;
1479 qemu_timeval tv;
1480 BlockdevSnapshotInternal *internal;
1481 InternalSnapshotState *state;
1482 AioContext *aio_context;
1483 int ret1;
1485 g_assert(common->action->type ==
1486 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1487 internal = common->action->u.blockdev_snapshot_internal_sync.data;
1488 state = DO_UPCAST(InternalSnapshotState, common, common);
1490 /* 1. parse input */
1491 device = internal->device;
1492 name = internal->name;
1494 /* 2. check for validation */
1495 if (action_check_completion_mode(common, errp) < 0) {
1496 return;
1499 bs = qmp_get_root_bs(device, errp);
1500 if (!bs) {
1501 return;
1504 aio_context = bdrv_get_aio_context(bs);
1505 aio_context_acquire(aio_context);
1507 state->bs = bs;
1509 /* Paired with .clean() */
1510 bdrv_drained_begin(bs);
1512 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1513 goto out;
1516 if (bdrv_is_read_only(bs)) {
1517 error_setg(errp, "Device '%s' is read only", device);
1518 goto out;
1521 if (!bdrv_can_snapshot(bs)) {
1522 error_setg(errp, "Block format '%s' used by device '%s' "
1523 "does not support internal snapshots",
1524 bs->drv->format_name, device);
1525 goto out;
1528 if (!strlen(name)) {
1529 error_setg(errp, "Name is empty");
1530 goto out;
1533 /* check whether a snapshot with name exist */
1534 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1535 &local_err);
1536 if (local_err) {
1537 error_propagate(errp, local_err);
1538 goto out;
1539 } else if (ret) {
1540 error_setg(errp,
1541 "Snapshot with name '%s' already exists on device '%s'",
1542 name, device);
1543 goto out;
1546 /* 3. take the snapshot */
1547 sn = &state->sn;
1548 pstrcpy(sn->name, sizeof(sn->name), name);
1549 qemu_gettimeofday(&tv);
1550 sn->date_sec = tv.tv_sec;
1551 sn->date_nsec = tv.tv_usec * 1000;
1552 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1554 ret1 = bdrv_snapshot_create(bs, sn);
1555 if (ret1 < 0) {
1556 error_setg_errno(errp, -ret1,
1557 "Failed to create snapshot '%s' on device '%s'",
1558 name, device);
1559 goto out;
1562 /* 4. succeed, mark a snapshot is created */
1563 state->created = true;
1565 out:
1566 aio_context_release(aio_context);
1569 static void internal_snapshot_abort(BlkActionState *common)
1571 InternalSnapshotState *state =
1572 DO_UPCAST(InternalSnapshotState, common, common);
1573 BlockDriverState *bs = state->bs;
1574 QEMUSnapshotInfo *sn = &state->sn;
1575 AioContext *aio_context;
1576 Error *local_error = NULL;
1578 if (!state->created) {
1579 return;
1582 aio_context = bdrv_get_aio_context(state->bs);
1583 aio_context_acquire(aio_context);
1585 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1586 error_reportf_err(local_error,
1587 "Failed to delete snapshot with id '%s' and "
1588 "name '%s' on device '%s' in abort: ",
1589 sn->id_str, sn->name,
1590 bdrv_get_device_name(bs));
1593 aio_context_release(aio_context);
1596 static void internal_snapshot_clean(BlkActionState *common)
1598 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1599 common, common);
1600 AioContext *aio_context;
1602 if (!state->bs) {
1603 return;
1606 aio_context = bdrv_get_aio_context(state->bs);
1607 aio_context_acquire(aio_context);
1609 bdrv_drained_end(state->bs);
1611 aio_context_release(aio_context);
1614 /* external snapshot private data */
1615 typedef struct ExternalSnapshotState {
1616 BlkActionState common;
1617 BlockDriverState *old_bs;
1618 BlockDriverState *new_bs;
1619 bool overlay_appended;
1620 } ExternalSnapshotState;
1622 static void external_snapshot_prepare(BlkActionState *common,
1623 Error **errp)
1625 int flags = 0;
1626 QDict *options = NULL;
1627 Error *local_err = NULL;
1628 /* Device and node name of the image to generate the snapshot from */
1629 const char *device;
1630 const char *node_name;
1631 /* Reference to the new image (for 'blockdev-snapshot') */
1632 const char *snapshot_ref;
1633 /* File name of the new image (for 'blockdev-snapshot-sync') */
1634 const char *new_image_file;
1635 ExternalSnapshotState *state =
1636 DO_UPCAST(ExternalSnapshotState, common, common);
1637 TransactionAction *action = common->action;
1638 AioContext *aio_context;
1640 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1641 * purpose but a different set of parameters */
1642 switch (action->type) {
1643 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1645 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
1646 device = s->node;
1647 node_name = s->node;
1648 new_image_file = NULL;
1649 snapshot_ref = s->overlay;
1651 break;
1652 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1654 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
1655 device = s->has_device ? s->device : NULL;
1656 node_name = s->has_node_name ? s->node_name : NULL;
1657 new_image_file = s->snapshot_file;
1658 snapshot_ref = NULL;
1660 break;
1661 default:
1662 g_assert_not_reached();
1665 /* start processing */
1666 if (action_check_completion_mode(common, errp) < 0) {
1667 return;
1670 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1671 if (!state->old_bs) {
1672 return;
1675 aio_context = bdrv_get_aio_context(state->old_bs);
1676 aio_context_acquire(aio_context);
1678 /* Paired with .clean() */
1679 bdrv_drained_begin(state->old_bs);
1681 if (!bdrv_is_inserted(state->old_bs)) {
1682 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1683 goto out;
1686 if (bdrv_op_is_blocked(state->old_bs,
1687 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1688 goto out;
1691 if (!bdrv_is_read_only(state->old_bs)) {
1692 if (bdrv_flush(state->old_bs)) {
1693 error_setg(errp, QERR_IO_ERROR);
1694 goto out;
1698 if (!bdrv_is_first_non_filter(state->old_bs)) {
1699 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1700 goto out;
1703 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
1704 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
1705 const char *format = s->has_format ? s->format : "qcow2";
1706 enum NewImageMode mode;
1707 const char *snapshot_node_name =
1708 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
1710 if (node_name && !snapshot_node_name) {
1711 error_setg(errp, "New snapshot node name missing");
1712 goto out;
1715 if (snapshot_node_name &&
1716 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1717 error_setg(errp, "New snapshot node name already in use");
1718 goto out;
1721 flags = state->old_bs->open_flags;
1722 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ);
1723 flags |= BDRV_O_NO_BACKING;
1725 /* create new image w/backing file */
1726 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1727 if (mode != NEW_IMAGE_MODE_EXISTING) {
1728 int64_t size = bdrv_getlength(state->old_bs);
1729 if (size < 0) {
1730 error_setg_errno(errp, -size, "bdrv_getlength failed");
1731 goto out;
1733 bdrv_img_create(new_image_file, format,
1734 state->old_bs->filename,
1735 state->old_bs->drv->format_name,
1736 NULL, size, flags, false, &local_err);
1737 if (local_err) {
1738 error_propagate(errp, local_err);
1739 goto out;
1743 options = qdict_new();
1744 if (s->has_snapshot_node_name) {
1745 qdict_put_str(options, "node-name", snapshot_node_name);
1747 qdict_put_str(options, "driver", format);
1750 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1751 errp);
1752 /* We will manually add the backing_hd field to the bs later */
1753 if (!state->new_bs) {
1754 goto out;
1757 if (bdrv_has_blk(state->new_bs)) {
1758 error_setg(errp, "The snapshot is already in use");
1759 goto out;
1762 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1763 errp)) {
1764 goto out;
1767 if (state->new_bs->backing != NULL) {
1768 error_setg(errp, "The snapshot already has a backing image");
1769 goto out;
1772 if (!state->new_bs->drv->supports_backing) {
1773 error_setg(errp, "The snapshot does not support backing images");
1774 goto out;
1777 bdrv_set_aio_context(state->new_bs, aio_context);
1779 /* This removes our old bs and adds the new bs. This is an operation that
1780 * can fail, so we need to do it in .prepare; undoing it for abort is
1781 * always possible. */
1782 bdrv_ref(state->new_bs);
1783 bdrv_append(state->new_bs, state->old_bs, &local_err);
1784 if (local_err) {
1785 error_propagate(errp, local_err);
1786 goto out;
1788 state->overlay_appended = true;
1790 out:
1791 aio_context_release(aio_context);
1794 static void external_snapshot_commit(BlkActionState *common)
1796 ExternalSnapshotState *state =
1797 DO_UPCAST(ExternalSnapshotState, common, common);
1798 AioContext *aio_context;
1800 aio_context = bdrv_get_aio_context(state->old_bs);
1801 aio_context_acquire(aio_context);
1803 /* We don't need (or want) to use the transactional
1804 * bdrv_reopen_multiple() across all the entries at once, because we
1805 * don't want to abort all of them if one of them fails the reopen */
1806 if (!atomic_read(&state->old_bs->copy_on_read)) {
1807 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1808 NULL);
1811 aio_context_release(aio_context);
1814 static void external_snapshot_abort(BlkActionState *common)
1816 ExternalSnapshotState *state =
1817 DO_UPCAST(ExternalSnapshotState, common, common);
1818 if (state->new_bs) {
1819 if (state->overlay_appended) {
1820 AioContext *aio_context;
1822 aio_context = bdrv_get_aio_context(state->old_bs);
1823 aio_context_acquire(aio_context);
1825 bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd()
1826 close state->old_bs; we need it */
1827 bdrv_set_backing_hd(state->new_bs, NULL, &error_abort);
1828 bdrv_replace_node(state->new_bs, state->old_bs, &error_abort);
1829 bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */
1831 aio_context_release(aio_context);
1836 static void external_snapshot_clean(BlkActionState *common)
1838 ExternalSnapshotState *state =
1839 DO_UPCAST(ExternalSnapshotState, common, common);
1840 AioContext *aio_context;
1842 if (!state->old_bs) {
1843 return;
1846 aio_context = bdrv_get_aio_context(state->old_bs);
1847 aio_context_acquire(aio_context);
1849 bdrv_drained_end(state->old_bs);
1850 bdrv_unref(state->new_bs);
1852 aio_context_release(aio_context);
1855 typedef struct DriveBackupState {
1856 BlkActionState common;
1857 BlockDriverState *bs;
1858 BlockJob *job;
1859 } DriveBackupState;
1861 static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
1862 Error **errp);
1864 static void drive_backup_prepare(BlkActionState *common, Error **errp)
1866 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1867 BlockDriverState *bs;
1868 DriveBackup *backup;
1869 AioContext *aio_context;
1870 Error *local_err = NULL;
1872 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1873 backup = common->action->u.drive_backup.data;
1875 bs = qmp_get_root_bs(backup->device, errp);
1876 if (!bs) {
1877 return;
1880 aio_context = bdrv_get_aio_context(bs);
1881 aio_context_acquire(aio_context);
1883 /* Paired with .clean() */
1884 bdrv_drained_begin(bs);
1886 state->bs = bs;
1888 state->job = do_drive_backup(backup, common->block_job_txn, &local_err);
1889 if (local_err) {
1890 error_propagate(errp, local_err);
1891 goto out;
1894 out:
1895 aio_context_release(aio_context);
1898 static void drive_backup_commit(BlkActionState *common)
1900 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1901 AioContext *aio_context;
1903 aio_context = bdrv_get_aio_context(state->bs);
1904 aio_context_acquire(aio_context);
1906 assert(state->job);
1907 block_job_start(state->job);
1909 aio_context_release(aio_context);
1912 static void drive_backup_abort(BlkActionState *common)
1914 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1916 if (state->job) {
1917 AioContext *aio_context;
1919 aio_context = bdrv_get_aio_context(state->bs);
1920 aio_context_acquire(aio_context);
1922 block_job_cancel_sync(state->job);
1924 aio_context_release(aio_context);
1928 static void drive_backup_clean(BlkActionState *common)
1930 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1931 AioContext *aio_context;
1933 if (!state->bs) {
1934 return;
1937 aio_context = bdrv_get_aio_context(state->bs);
1938 aio_context_acquire(aio_context);
1940 bdrv_drained_end(state->bs);
1942 aio_context_release(aio_context);
1945 typedef struct BlockdevBackupState {
1946 BlkActionState common;
1947 BlockDriverState *bs;
1948 BlockJob *job;
1949 } BlockdevBackupState;
1951 static BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
1952 Error **errp);
1954 static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1956 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1957 BlockdevBackup *backup;
1958 BlockDriverState *bs, *target;
1959 AioContext *aio_context;
1960 Error *local_err = NULL;
1962 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1963 backup = common->action->u.blockdev_backup.data;
1965 bs = qmp_get_root_bs(backup->device, errp);
1966 if (!bs) {
1967 return;
1970 target = bdrv_lookup_bs(backup->target, backup->target, errp);
1971 if (!target) {
1972 return;
1975 aio_context = bdrv_get_aio_context(bs);
1976 if (aio_context != bdrv_get_aio_context(target)) {
1977 error_setg(errp, "Backup between two IO threads is not implemented");
1978 return;
1980 aio_context_acquire(aio_context);
1981 state->bs = bs;
1983 /* Paired with .clean() */
1984 bdrv_drained_begin(state->bs);
1986 state->job = do_blockdev_backup(backup, common->block_job_txn, &local_err);
1987 if (local_err) {
1988 error_propagate(errp, local_err);
1989 goto out;
1992 out:
1993 aio_context_release(aio_context);
1996 static void blockdev_backup_commit(BlkActionState *common)
1998 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1999 AioContext *aio_context;
2001 aio_context = bdrv_get_aio_context(state->bs);
2002 aio_context_acquire(aio_context);
2004 assert(state->job);
2005 block_job_start(state->job);
2007 aio_context_release(aio_context);
2010 static void blockdev_backup_abort(BlkActionState *common)
2012 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
2014 if (state->job) {
2015 AioContext *aio_context;
2017 aio_context = bdrv_get_aio_context(state->bs);
2018 aio_context_acquire(aio_context);
2020 block_job_cancel_sync(state->job);
2022 aio_context_release(aio_context);
2026 static void blockdev_backup_clean(BlkActionState *common)
2028 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
2029 AioContext *aio_context;
2031 if (!state->bs) {
2032 return;
2035 aio_context = bdrv_get_aio_context(state->bs);
2036 aio_context_acquire(aio_context);
2038 bdrv_drained_end(state->bs);
2040 aio_context_release(aio_context);
2043 typedef struct BlockDirtyBitmapState {
2044 BlkActionState common;
2045 BdrvDirtyBitmap *bitmap;
2046 BlockDriverState *bs;
2047 HBitmap *backup;
2048 bool prepared;
2049 } BlockDirtyBitmapState;
2051 static void block_dirty_bitmap_add_prepare(BlkActionState *common,
2052 Error **errp)
2054 Error *local_err = NULL;
2055 BlockDirtyBitmapAdd *action;
2056 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2057 common, common);
2059 if (action_check_completion_mode(common, errp) < 0) {
2060 return;
2063 action = common->action->u.block_dirty_bitmap_add.data;
2064 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2065 qmp_block_dirty_bitmap_add(action->node, action->name,
2066 action->has_granularity, action->granularity,
2067 action->has_persistent, action->persistent,
2068 action->has_autoload, action->autoload,
2069 &local_err);
2071 if (!local_err) {
2072 state->prepared = true;
2073 } else {
2074 error_propagate(errp, local_err);
2078 static void block_dirty_bitmap_add_abort(BlkActionState *common)
2080 BlockDirtyBitmapAdd *action;
2081 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2082 common, common);
2084 action = common->action->u.block_dirty_bitmap_add.data;
2085 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2086 * then the node reference and bitmap name must have been valid.
2088 if (state->prepared) {
2089 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2093 static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
2094 Error **errp)
2096 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2097 common, common);
2098 BlockDirtyBitmap *action;
2100 if (action_check_completion_mode(common, errp) < 0) {
2101 return;
2104 action = common->action->u.block_dirty_bitmap_clear.data;
2105 state->bitmap = block_dirty_bitmap_lookup(action->node,
2106 action->name,
2107 &state->bs,
2108 errp);
2109 if (!state->bitmap) {
2110 return;
2113 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2114 error_setg(errp, "Cannot modify a frozen bitmap");
2115 return;
2116 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2117 error_setg(errp, "Cannot clear a disabled bitmap");
2118 return;
2119 } else if (bdrv_dirty_bitmap_readonly(state->bitmap)) {
2120 error_setg(errp, "Cannot clear a readonly bitmap");
2121 return;
2124 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2127 static void block_dirty_bitmap_clear_abort(BlkActionState *common)
2129 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2130 common, common);
2132 if (state->backup) {
2133 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2137 static void block_dirty_bitmap_clear_commit(BlkActionState *common)
2139 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2140 common, common);
2142 hbitmap_free(state->backup);
2145 static void abort_prepare(BlkActionState *common, Error **errp)
2147 error_setg(errp, "Transaction aborted using Abort action");
2150 static void abort_commit(BlkActionState *common)
2152 g_assert_not_reached(); /* this action never succeeds */
2155 static const BlkActionOps actions[] = {
2156 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2157 .instance_size = sizeof(ExternalSnapshotState),
2158 .prepare = external_snapshot_prepare,
2159 .commit = external_snapshot_commit,
2160 .abort = external_snapshot_abort,
2161 .clean = external_snapshot_clean,
2163 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2164 .instance_size = sizeof(ExternalSnapshotState),
2165 .prepare = external_snapshot_prepare,
2166 .commit = external_snapshot_commit,
2167 .abort = external_snapshot_abort,
2168 .clean = external_snapshot_clean,
2170 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2171 .instance_size = sizeof(DriveBackupState),
2172 .prepare = drive_backup_prepare,
2173 .commit = drive_backup_commit,
2174 .abort = drive_backup_abort,
2175 .clean = drive_backup_clean,
2177 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2178 .instance_size = sizeof(BlockdevBackupState),
2179 .prepare = blockdev_backup_prepare,
2180 .commit = blockdev_backup_commit,
2181 .abort = blockdev_backup_abort,
2182 .clean = blockdev_backup_clean,
2184 [TRANSACTION_ACTION_KIND_ABORT] = {
2185 .instance_size = sizeof(BlkActionState),
2186 .prepare = abort_prepare,
2187 .commit = abort_commit,
2189 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2190 .instance_size = sizeof(InternalSnapshotState),
2191 .prepare = internal_snapshot_prepare,
2192 .abort = internal_snapshot_abort,
2193 .clean = internal_snapshot_clean,
2195 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2196 .instance_size = sizeof(BlockDirtyBitmapState),
2197 .prepare = block_dirty_bitmap_add_prepare,
2198 .abort = block_dirty_bitmap_add_abort,
2200 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2201 .instance_size = sizeof(BlockDirtyBitmapState),
2202 .prepare = block_dirty_bitmap_clear_prepare,
2203 .commit = block_dirty_bitmap_clear_commit,
2204 .abort = block_dirty_bitmap_clear_abort,
2209 * Allocate a TransactionProperties structure if necessary, and fill
2210 * that structure with desired defaults if they are unset.
2212 static TransactionProperties *get_transaction_properties(
2213 TransactionProperties *props)
2215 if (!props) {
2216 props = g_new0(TransactionProperties, 1);
2219 if (!props->has_completion_mode) {
2220 props->has_completion_mode = true;
2221 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2224 return props;
2228 * 'Atomic' group operations. The operations are performed as a set, and if
2229 * any fail then we roll back all operations in the group.
2231 void qmp_transaction(TransactionActionList *dev_list,
2232 bool has_props,
2233 struct TransactionProperties *props,
2234 Error **errp)
2236 TransactionActionList *dev_entry = dev_list;
2237 BlockJobTxn *block_job_txn = NULL;
2238 BlkActionState *state, *next;
2239 Error *local_err = NULL;
2241 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
2242 QSIMPLEQ_INIT(&snap_bdrv_states);
2244 /* Does this transaction get canceled as a group on failure?
2245 * If not, we don't really need to make a BlockJobTxn.
2247 props = get_transaction_properties(props);
2248 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2249 block_job_txn = block_job_txn_new();
2252 /* drain all i/o before any operations */
2253 bdrv_drain_all();
2255 /* We don't do anything in this loop that commits us to the operations */
2256 while (NULL != dev_entry) {
2257 TransactionAction *dev_info = NULL;
2258 const BlkActionOps *ops;
2260 dev_info = dev_entry->value;
2261 dev_entry = dev_entry->next;
2263 assert(dev_info->type < ARRAY_SIZE(actions));
2265 ops = &actions[dev_info->type];
2266 assert(ops->instance_size > 0);
2268 state = g_malloc0(ops->instance_size);
2269 state->ops = ops;
2270 state->action = dev_info;
2271 state->block_job_txn = block_job_txn;
2272 state->txn_props = props;
2273 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
2275 state->ops->prepare(state, &local_err);
2276 if (local_err) {
2277 error_propagate(errp, local_err);
2278 goto delete_and_fail;
2282 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2283 if (state->ops->commit) {
2284 state->ops->commit(state);
2288 /* success */
2289 goto exit;
2291 delete_and_fail:
2292 /* failure, and it is all-or-none; roll back all operations */
2293 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2294 if (state->ops->abort) {
2295 state->ops->abort(state);
2298 exit:
2299 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2300 if (state->ops->clean) {
2301 state->ops->clean(state);
2303 g_free(state);
2305 if (!has_props) {
2306 qapi_free_TransactionProperties(props);
2308 block_job_txn_unref(block_job_txn);
2311 void qmp_eject(bool has_device, const char *device,
2312 bool has_id, const char *id,
2313 bool has_force, bool force, Error **errp)
2315 Error *local_err = NULL;
2316 int rc;
2318 if (!has_force) {
2319 force = false;
2322 rc = do_open_tray(has_device ? device : NULL,
2323 has_id ? id : NULL,
2324 force, &local_err);
2325 if (rc && rc != -ENOSYS) {
2326 error_propagate(errp, local_err);
2327 return;
2329 error_free(local_err);
2331 blockdev_remove_medium(has_device, device, has_id, id, errp);
2334 void qmp_block_passwd(bool has_device, const char *device,
2335 bool has_node_name, const char *node_name,
2336 const char *password, Error **errp)
2338 error_setg(errp,
2339 "Setting block passwords directly is no longer supported");
2343 * Attempt to open the tray of @device.
2344 * If @force, ignore its tray lock.
2345 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2346 * On error, store an error through @errp and return -errno.
2347 * If @device does not exist, return -ENODEV.
2348 * If it has no removable media, return -ENOTSUP.
2349 * If it has no tray, return -ENOSYS.
2350 * If the guest was asked to open the tray, return -EINPROGRESS.
2351 * Else, return 0.
2353 static int do_open_tray(const char *blk_name, const char *qdev_id,
2354 bool force, Error **errp)
2356 BlockBackend *blk;
2357 const char *device = qdev_id ?: blk_name;
2358 bool locked;
2360 blk = qmp_get_blk(blk_name, qdev_id, errp);
2361 if (!blk) {
2362 return -ENODEV;
2365 if (!blk_dev_has_removable_media(blk)) {
2366 error_setg(errp, "Device '%s' is not removable", device);
2367 return -ENOTSUP;
2370 if (!blk_dev_has_tray(blk)) {
2371 error_setg(errp, "Device '%s' does not have a tray", device);
2372 return -ENOSYS;
2375 if (blk_dev_is_tray_open(blk)) {
2376 return 0;
2379 locked = blk_dev_is_medium_locked(blk);
2380 if (locked) {
2381 blk_dev_eject_request(blk, force);
2384 if (!locked || force) {
2385 blk_dev_change_media_cb(blk, false, &error_abort);
2388 if (locked && !force) {
2389 error_setg(errp, "Device '%s' is locked and force was not specified, "
2390 "wait for tray to open and try again", device);
2391 return -EINPROGRESS;
2394 return 0;
2397 void qmp_blockdev_open_tray(bool has_device, const char *device,
2398 bool has_id, const char *id,
2399 bool has_force, bool force,
2400 Error **errp)
2402 Error *local_err = NULL;
2403 int rc;
2405 if (!has_force) {
2406 force = false;
2408 rc = do_open_tray(has_device ? device : NULL,
2409 has_id ? id : NULL,
2410 force, &local_err);
2411 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2412 error_propagate(errp, local_err);
2413 return;
2415 error_free(local_err);
2418 void qmp_blockdev_close_tray(bool has_device, const char *device,
2419 bool has_id, const char *id,
2420 Error **errp)
2422 BlockBackend *blk;
2423 Error *local_err = NULL;
2425 device = has_device ? device : NULL;
2426 id = has_id ? id : NULL;
2428 blk = qmp_get_blk(device, id, errp);
2429 if (!blk) {
2430 return;
2433 if (!blk_dev_has_removable_media(blk)) {
2434 error_setg(errp, "Device '%s' is not removable", device ?: id);
2435 return;
2438 if (!blk_dev_has_tray(blk)) {
2439 /* Ignore this command on tray-less devices */
2440 return;
2443 if (!blk_dev_is_tray_open(blk)) {
2444 return;
2447 blk_dev_change_media_cb(blk, true, &local_err);
2448 if (local_err) {
2449 error_propagate(errp, local_err);
2450 return;
2454 static void blockdev_remove_medium(bool has_device, const char *device,
2455 bool has_id, const char *id, Error **errp)
2457 BlockBackend *blk;
2458 BlockDriverState *bs;
2459 AioContext *aio_context;
2460 bool has_attached_device;
2462 device = has_device ? device : NULL;
2463 id = has_id ? id : NULL;
2465 blk = qmp_get_blk(device, id, errp);
2466 if (!blk) {
2467 return;
2470 /* For BBs without a device, we can exchange the BDS tree at will */
2471 has_attached_device = blk_get_attached_dev(blk);
2473 if (has_attached_device && !blk_dev_has_removable_media(blk)) {
2474 error_setg(errp, "Device '%s' is not removable", device ?: id);
2475 return;
2478 if (has_attached_device && blk_dev_has_tray(blk) &&
2479 !blk_dev_is_tray_open(blk))
2481 error_setg(errp, "Tray of device '%s' is not open", device ?: id);
2482 return;
2485 bs = blk_bs(blk);
2486 if (!bs) {
2487 return;
2490 aio_context = bdrv_get_aio_context(bs);
2491 aio_context_acquire(aio_context);
2493 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2494 goto out;
2497 blk_remove_bs(blk);
2499 if (!blk_dev_has_tray(blk)) {
2500 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2501 * called at all); therefore, the medium needs to be ejected here.
2502 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2503 * value passed here (i.e. false). */
2504 blk_dev_change_media_cb(blk, false, &error_abort);
2507 out:
2508 aio_context_release(aio_context);
2511 void qmp_blockdev_remove_medium(const char *id, Error **errp)
2513 blockdev_remove_medium(false, NULL, true, id, errp);
2516 static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,
2517 BlockDriverState *bs, Error **errp)
2519 Error *local_err = NULL;
2520 bool has_device;
2521 int ret;
2523 /* For BBs without a device, we can exchange the BDS tree at will */
2524 has_device = blk_get_attached_dev(blk);
2526 if (has_device && !blk_dev_has_removable_media(blk)) {
2527 error_setg(errp, "Device is not removable");
2528 return;
2531 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2532 error_setg(errp, "Tray of the device is not open");
2533 return;
2536 if (blk_bs(blk)) {
2537 error_setg(errp, "There already is a medium in the device");
2538 return;
2541 ret = blk_insert_bs(blk, bs, errp);
2542 if (ret < 0) {
2543 return;
2546 if (!blk_dev_has_tray(blk)) {
2547 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2548 * called at all); therefore, the medium needs to be pushed into the
2549 * slot here.
2550 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2551 * value passed here (i.e. true). */
2552 blk_dev_change_media_cb(blk, true, &local_err);
2553 if (local_err) {
2554 error_propagate(errp, local_err);
2555 blk_remove_bs(blk);
2556 return;
2561 static void blockdev_insert_medium(bool has_device, const char *device,
2562 bool has_id, const char *id,
2563 const char *node_name, Error **errp)
2565 BlockBackend *blk;
2566 BlockDriverState *bs;
2568 blk = qmp_get_blk(has_device ? device : NULL,
2569 has_id ? id : NULL,
2570 errp);
2571 if (!blk) {
2572 return;
2575 bs = bdrv_find_node(node_name);
2576 if (!bs) {
2577 error_setg(errp, "Node '%s' not found", node_name);
2578 return;
2581 if (bdrv_has_blk(bs)) {
2582 error_setg(errp, "Node '%s' is already in use", node_name);
2583 return;
2586 qmp_blockdev_insert_anon_medium(blk, bs, errp);
2589 void qmp_blockdev_insert_medium(const char *id, const char *node_name,
2590 Error **errp)
2592 blockdev_insert_medium(false, NULL, true, id, node_name, errp);
2595 void qmp_blockdev_change_medium(bool has_device, const char *device,
2596 bool has_id, const char *id,
2597 const char *filename,
2598 bool has_format, const char *format,
2599 bool has_read_only,
2600 BlockdevChangeReadOnlyMode read_only,
2601 Error **errp)
2603 BlockBackend *blk;
2604 BlockDriverState *medium_bs = NULL;
2605 int bdrv_flags;
2606 bool detect_zeroes;
2607 int rc;
2608 QDict *options = NULL;
2609 Error *err = NULL;
2611 blk = qmp_get_blk(has_device ? device : NULL,
2612 has_id ? id : NULL,
2613 errp);
2614 if (!blk) {
2615 goto fail;
2618 if (blk_bs(blk)) {
2619 blk_update_root_state(blk);
2622 bdrv_flags = blk_get_open_flags_from_root_state(blk);
2623 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2624 BDRV_O_PROTOCOL);
2626 if (!has_read_only) {
2627 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2630 switch (read_only) {
2631 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2632 break;
2634 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2635 bdrv_flags &= ~BDRV_O_RDWR;
2636 break;
2638 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2639 bdrv_flags |= BDRV_O_RDWR;
2640 break;
2642 default:
2643 abort();
2646 options = qdict_new();
2647 detect_zeroes = blk_get_detect_zeroes_from_root_state(blk);
2648 qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off");
2650 if (has_format) {
2651 qdict_put_str(options, "driver", format);
2654 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
2655 if (!medium_bs) {
2656 goto fail;
2659 rc = do_open_tray(has_device ? device : NULL,
2660 has_id ? id : NULL,
2661 false, &err);
2662 if (rc && rc != -ENOSYS) {
2663 error_propagate(errp, err);
2664 goto fail;
2666 error_free(err);
2667 err = NULL;
2669 blockdev_remove_medium(has_device, device, has_id, id, &err);
2670 if (err) {
2671 error_propagate(errp, err);
2672 goto fail;
2675 qmp_blockdev_insert_anon_medium(blk, medium_bs, &err);
2676 if (err) {
2677 error_propagate(errp, err);
2678 goto fail;
2681 qmp_blockdev_close_tray(has_device, device, has_id, id, errp);
2683 fail:
2684 /* If the medium has been inserted, the device has its own reference, so
2685 * ours must be relinquished; and if it has not been inserted successfully,
2686 * the reference must be relinquished anyway */
2687 bdrv_unref(medium_bs);
2690 /* throttling disk I/O limits */
2691 void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
2693 ThrottleConfig cfg;
2694 BlockDriverState *bs;
2695 BlockBackend *blk;
2696 AioContext *aio_context;
2698 blk = qmp_get_blk(arg->has_device ? arg->device : NULL,
2699 arg->has_id ? arg->id : NULL,
2700 errp);
2701 if (!blk) {
2702 return;
2705 aio_context = blk_get_aio_context(blk);
2706 aio_context_acquire(aio_context);
2708 bs = blk_bs(blk);
2709 if (!bs) {
2710 error_setg(errp, "Device has no medium");
2711 goto out;
2714 throttle_config_init(&cfg);
2715 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
2716 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
2717 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
2719 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
2720 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
2721 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
2723 if (arg->has_bps_max) {
2724 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
2726 if (arg->has_bps_rd_max) {
2727 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
2729 if (arg->has_bps_wr_max) {
2730 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
2732 if (arg->has_iops_max) {
2733 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
2735 if (arg->has_iops_rd_max) {
2736 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
2738 if (arg->has_iops_wr_max) {
2739 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
2742 if (arg->has_bps_max_length) {
2743 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
2745 if (arg->has_bps_rd_max_length) {
2746 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
2748 if (arg->has_bps_wr_max_length) {
2749 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
2751 if (arg->has_iops_max_length) {
2752 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
2754 if (arg->has_iops_rd_max_length) {
2755 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
2757 if (arg->has_iops_wr_max_length) {
2758 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
2761 if (arg->has_iops_size) {
2762 cfg.op_size = arg->iops_size;
2765 if (!throttle_is_valid(&cfg, errp)) {
2766 goto out;
2769 if (throttle_enabled(&cfg)) {
2770 /* Enable I/O limits if they're not enabled yet, otherwise
2771 * just update the throttling group. */
2772 if (!blk_get_public(blk)->throttle_group_member.throttle_state) {
2773 blk_io_limits_enable(blk,
2774 arg->has_group ? arg->group :
2775 arg->has_device ? arg->device :
2776 arg->id);
2777 } else if (arg->has_group) {
2778 blk_io_limits_update_group(blk, arg->group);
2780 /* Set the new throttling configuration */
2781 blk_set_io_limits(blk, &cfg);
2782 } else if (blk_get_public(blk)->throttle_group_member.throttle_state) {
2783 /* If all throttling settings are set to 0, disable I/O limits */
2784 blk_io_limits_disable(blk);
2787 out:
2788 aio_context_release(aio_context);
2791 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2792 bool has_granularity, uint32_t granularity,
2793 bool has_persistent, bool persistent,
2794 bool has_autoload, bool autoload,
2795 Error **errp)
2797 BlockDriverState *bs;
2798 BdrvDirtyBitmap *bitmap;
2800 if (!name || name[0] == '\0') {
2801 error_setg(errp, "Bitmap name cannot be empty");
2802 return;
2805 bs = bdrv_lookup_bs(node, node, errp);
2806 if (!bs) {
2807 return;
2810 if (has_granularity) {
2811 if (granularity < 512 || !is_power_of_2(granularity)) {
2812 error_setg(errp, "Granularity must be power of 2 "
2813 "and at least 512");
2814 return;
2816 } else {
2817 /* Default to cluster size, if available: */
2818 granularity = bdrv_get_default_bitmap_granularity(bs);
2821 if (!has_persistent) {
2822 persistent = false;
2824 if (!has_autoload) {
2825 autoload = false;
2828 if (has_autoload && !persistent) {
2829 error_setg(errp, "Autoload flag must be used only for persistent "
2830 "bitmaps");
2831 return;
2834 if (persistent &&
2835 !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp))
2837 return;
2840 bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2841 if (bitmap == NULL) {
2842 return;
2845 bdrv_dirty_bitmap_set_persistance(bitmap, persistent);
2846 bdrv_dirty_bitmap_set_autoload(bitmap, autoload);
2849 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2850 Error **errp)
2852 BlockDriverState *bs;
2853 BdrvDirtyBitmap *bitmap;
2854 Error *local_err = NULL;
2856 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
2857 if (!bitmap || !bs) {
2858 return;
2861 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2862 error_setg(errp,
2863 "Bitmap '%s' is currently frozen and cannot be removed",
2864 name);
2865 return;
2868 if (bdrv_dirty_bitmap_get_persistance(bitmap)) {
2869 bdrv_remove_persistent_dirty_bitmap(bs, name, &local_err);
2870 if (local_err != NULL) {
2871 error_propagate(errp, local_err);
2872 return;
2876 bdrv_dirty_bitmap_make_anon(bitmap);
2877 bdrv_release_dirty_bitmap(bs, bitmap);
2881 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2882 * immediately after a full backup operation.
2884 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2885 Error **errp)
2887 BdrvDirtyBitmap *bitmap;
2888 BlockDriverState *bs;
2890 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
2891 if (!bitmap || !bs) {
2892 return;
2895 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2896 error_setg(errp,
2897 "Bitmap '%s' is currently frozen and cannot be modified",
2898 name);
2899 return;
2900 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2901 error_setg(errp,
2902 "Bitmap '%s' is currently disabled and cannot be cleared",
2903 name);
2904 return;
2905 } else if (bdrv_dirty_bitmap_readonly(bitmap)) {
2906 error_setg(errp, "Bitmap '%s' is readonly and cannot be cleared", name);
2907 return;
2910 bdrv_clear_dirty_bitmap(bitmap, NULL);
2913 BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
2914 const char *name,
2915 Error **errp)
2917 BdrvDirtyBitmap *bitmap;
2918 BlockDriverState *bs;
2919 BlockDirtyBitmapSha256 *ret = NULL;
2920 char *sha256;
2922 bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
2923 if (!bitmap || !bs) {
2924 return NULL;
2927 sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp);
2928 if (sha256 == NULL) {
2929 return NULL;
2932 ret = g_new(BlockDirtyBitmapSha256, 1);
2933 ret->sha256 = sha256;
2935 return ret;
2938 void hmp_drive_del(Monitor *mon, const QDict *qdict)
2940 const char *id = qdict_get_str(qdict, "id");
2941 BlockBackend *blk;
2942 BlockDriverState *bs;
2943 AioContext *aio_context;
2944 Error *local_err = NULL;
2946 bs = bdrv_find_node(id);
2947 if (bs) {
2948 qmp_blockdev_del(id, &local_err);
2949 if (local_err) {
2950 error_report_err(local_err);
2952 return;
2955 blk = blk_by_name(id);
2956 if (!blk) {
2957 error_report("Device '%s' not found", id);
2958 return;
2961 if (!blk_legacy_dinfo(blk)) {
2962 error_report("Deleting device added with blockdev-add"
2963 " is not supported");
2964 return;
2967 aio_context = blk_get_aio_context(blk);
2968 aio_context_acquire(aio_context);
2970 bs = blk_bs(blk);
2971 if (bs) {
2972 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2973 error_report_err(local_err);
2974 aio_context_release(aio_context);
2975 return;
2978 blk_remove_bs(blk);
2981 /* Make the BlockBackend and the attached BlockDriverState anonymous */
2982 monitor_remove_blk(blk);
2984 /* If this BlockBackend has a device attached to it, its refcount will be
2985 * decremented when the device is removed; otherwise we have to do so here.
2987 if (blk_get_attached_dev(blk)) {
2988 /* Further I/O must not pause the guest */
2989 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2990 BLOCKDEV_ON_ERROR_REPORT);
2991 } else {
2992 blk_unref(blk);
2995 aio_context_release(aio_context);
2998 void qmp_block_resize(bool has_device, const char *device,
2999 bool has_node_name, const char *node_name,
3000 int64_t size, Error **errp)
3002 Error *local_err = NULL;
3003 BlockBackend *blk = NULL;
3004 BlockDriverState *bs;
3005 AioContext *aio_context;
3006 int ret;
3008 bs = bdrv_lookup_bs(has_device ? device : NULL,
3009 has_node_name ? node_name : NULL,
3010 &local_err);
3011 if (local_err) {
3012 error_propagate(errp, local_err);
3013 return;
3016 aio_context = bdrv_get_aio_context(bs);
3017 aio_context_acquire(aio_context);
3019 if (!bdrv_is_first_non_filter(bs)) {
3020 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
3021 goto out;
3024 if (size < 0) {
3025 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
3026 goto out;
3029 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
3030 error_setg(errp, QERR_DEVICE_IN_USE, device);
3031 goto out;
3034 blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL);
3035 ret = blk_insert_bs(blk, bs, errp);
3036 if (ret < 0) {
3037 goto out;
3040 bdrv_drained_begin(bs);
3041 ret = blk_truncate(blk, size, PREALLOC_MODE_OFF, errp);
3042 bdrv_drained_end(bs);
3044 out:
3045 blk_unref(blk);
3046 aio_context_release(aio_context);
3049 void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
3050 bool has_base, const char *base,
3051 bool has_base_node, const char *base_node,
3052 bool has_backing_file, const char *backing_file,
3053 bool has_speed, int64_t speed,
3054 bool has_on_error, BlockdevOnError on_error,
3055 Error **errp)
3057 BlockDriverState *bs, *iter;
3058 BlockDriverState *base_bs = NULL;
3059 AioContext *aio_context;
3060 Error *local_err = NULL;
3061 const char *base_name = NULL;
3063 if (!has_on_error) {
3064 on_error = BLOCKDEV_ON_ERROR_REPORT;
3067 bs = bdrv_lookup_bs(device, device, errp);
3068 if (!bs) {
3069 return;
3072 aio_context = bdrv_get_aio_context(bs);
3073 aio_context_acquire(aio_context);
3075 if (has_base && has_base_node) {
3076 error_setg(errp, "'base' and 'base-node' cannot be specified "
3077 "at the same time");
3078 goto out;
3081 if (has_base) {
3082 base_bs = bdrv_find_backing_image(bs, base);
3083 if (base_bs == NULL) {
3084 error_setg(errp, QERR_BASE_NOT_FOUND, base);
3085 goto out;
3087 assert(bdrv_get_aio_context(base_bs) == aio_context);
3088 base_name = base;
3091 if (has_base_node) {
3092 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
3093 if (!base_bs) {
3094 goto out;
3096 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
3097 error_setg(errp, "Node '%s' is not a backing image of '%s'",
3098 base_node, device);
3099 goto out;
3101 assert(bdrv_get_aio_context(base_bs) == aio_context);
3102 base_name = base_bs->filename;
3105 /* Check for op blockers in the whole chain between bs and base */
3106 for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) {
3107 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
3108 goto out;
3112 /* if we are streaming the entire chain, the result will have no backing
3113 * file, and specifying one is therefore an error */
3114 if (base_bs == NULL && has_backing_file) {
3115 error_setg(errp, "backing file specified, but streaming the "
3116 "entire chain");
3117 goto out;
3120 /* backing_file string overrides base bs filename */
3121 base_name = has_backing_file ? backing_file : base_name;
3123 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
3124 has_speed ? speed : 0, on_error, &local_err);
3125 if (local_err) {
3126 error_propagate(errp, local_err);
3127 goto out;
3130 trace_qmp_block_stream(bs, bs->job);
3132 out:
3133 aio_context_release(aio_context);
3136 void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
3137 bool has_base, const char *base,
3138 bool has_top, const char *top,
3139 bool has_backing_file, const char *backing_file,
3140 bool has_speed, int64_t speed,
3141 bool has_filter_node_name, const char *filter_node_name,
3142 Error **errp)
3144 BlockDriverState *bs;
3145 BlockDriverState *iter;
3146 BlockDriverState *base_bs, *top_bs;
3147 AioContext *aio_context;
3148 Error *local_err = NULL;
3149 /* This will be part of the QMP command, if/when the
3150 * BlockdevOnError change for blkmirror makes it in
3152 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
3154 if (!has_speed) {
3155 speed = 0;
3157 if (!has_filter_node_name) {
3158 filter_node_name = NULL;
3161 /* Important Note:
3162 * libvirt relies on the DeviceNotFound error class in order to probe for
3163 * live commit feature versions; for this to work, we must make sure to
3164 * perform the device lookup before any generic errors that may occur in a
3165 * scenario in which all optional arguments are omitted. */
3166 bs = qmp_get_root_bs(device, &local_err);
3167 if (!bs) {
3168 bs = bdrv_lookup_bs(device, device, NULL);
3169 if (!bs) {
3170 error_free(local_err);
3171 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3172 "Device '%s' not found", device);
3173 } else {
3174 error_propagate(errp, local_err);
3176 return;
3179 aio_context = bdrv_get_aio_context(bs);
3180 aio_context_acquire(aio_context);
3182 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
3183 goto out;
3186 /* default top_bs is the active layer */
3187 top_bs = bs;
3189 if (has_top && top) {
3190 if (strcmp(bs->filename, top) != 0) {
3191 top_bs = bdrv_find_backing_image(bs, top);
3195 if (top_bs == NULL) {
3196 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
3197 goto out;
3200 assert(bdrv_get_aio_context(top_bs) == aio_context);
3202 if (has_base && base) {
3203 base_bs = bdrv_find_backing_image(top_bs, base);
3204 } else {
3205 base_bs = bdrv_find_base(top_bs);
3208 if (base_bs == NULL) {
3209 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
3210 goto out;
3213 assert(bdrv_get_aio_context(base_bs) == aio_context);
3215 for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) {
3216 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3217 goto out;
3221 /* Do not allow attempts to commit an image into itself */
3222 if (top_bs == base_bs) {
3223 error_setg(errp, "cannot commit an image into itself");
3224 goto out;
3227 if (top_bs == bs) {
3228 if (has_backing_file) {
3229 error_setg(errp, "'backing-file' specified,"
3230 " but 'top' is the active layer");
3231 goto out;
3233 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs,
3234 BLOCK_JOB_DEFAULT, speed, on_error,
3235 filter_node_name, NULL, NULL, false, &local_err);
3236 } else {
3237 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
3238 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3239 goto out;
3241 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed,
3242 on_error, has_backing_file ? backing_file : NULL,
3243 filter_node_name, &local_err);
3245 if (local_err != NULL) {
3246 error_propagate(errp, local_err);
3247 goto out;
3250 out:
3251 aio_context_release(aio_context);
3254 static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
3255 Error **errp)
3257 BlockDriverState *bs;
3258 BlockDriverState *target_bs;
3259 BlockDriverState *source = NULL;
3260 BlockJob *job = NULL;
3261 BdrvDirtyBitmap *bmap = NULL;
3262 AioContext *aio_context;
3263 QDict *options = NULL;
3264 Error *local_err = NULL;
3265 int flags;
3266 int64_t size;
3267 bool set_backing_hd = false;
3269 if (!backup->has_speed) {
3270 backup->speed = 0;
3272 if (!backup->has_on_source_error) {
3273 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3275 if (!backup->has_on_target_error) {
3276 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3278 if (!backup->has_mode) {
3279 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3281 if (!backup->has_job_id) {
3282 backup->job_id = NULL;
3284 if (!backup->has_compress) {
3285 backup->compress = false;
3288 bs = qmp_get_root_bs(backup->device, errp);
3289 if (!bs) {
3290 return NULL;
3293 aio_context = bdrv_get_aio_context(bs);
3294 aio_context_acquire(aio_context);
3296 if (!backup->has_format) {
3297 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
3298 NULL : (char*) bs->drv->format_name;
3301 /* Early check to avoid creating target */
3302 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
3303 goto out;
3306 flags = bs->open_flags | BDRV_O_RDWR;
3308 /* See if we have a backing HD we can use to create our new image
3309 * on top of. */
3310 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
3311 source = backing_bs(bs);
3312 if (!source) {
3313 backup->sync = MIRROR_SYNC_MODE_FULL;
3316 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
3317 source = bs;
3318 flags |= BDRV_O_NO_BACKING;
3319 set_backing_hd = true;
3322 size = bdrv_getlength(bs);
3323 if (size < 0) {
3324 error_setg_errno(errp, -size, "bdrv_getlength failed");
3325 goto out;
3328 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
3329 assert(backup->format);
3330 if (source) {
3331 bdrv_img_create(backup->target, backup->format, source->filename,
3332 source->drv->format_name, NULL,
3333 size, flags, false, &local_err);
3334 } else {
3335 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
3336 size, flags, false, &local_err);
3340 if (local_err) {
3341 error_propagate(errp, local_err);
3342 goto out;
3345 if (backup->format) {
3346 if (!options) {
3347 options = qdict_new();
3349 qdict_put_str(options, "driver", backup->format);
3352 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
3353 if (!target_bs) {
3354 goto out;
3357 bdrv_set_aio_context(target_bs, aio_context);
3359 if (set_backing_hd) {
3360 bdrv_set_backing_hd(target_bs, source, &local_err);
3361 if (local_err) {
3362 bdrv_unref(target_bs);
3363 goto out;
3367 if (backup->has_bitmap) {
3368 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
3369 if (!bmap) {
3370 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
3371 bdrv_unref(target_bs);
3372 goto out;
3376 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
3377 backup->sync, bmap, backup->compress,
3378 backup->on_source_error, backup->on_target_error,
3379 BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err);
3380 bdrv_unref(target_bs);
3381 if (local_err != NULL) {
3382 error_propagate(errp, local_err);
3383 goto out;
3386 out:
3387 aio_context_release(aio_context);
3388 return job;
3391 void qmp_drive_backup(DriveBackup *arg, Error **errp)
3394 BlockJob *job;
3395 job = do_drive_backup(arg, NULL, errp);
3396 if (job) {
3397 block_job_start(job);
3401 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3403 return bdrv_named_nodes_list(errp);
3406 BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
3407 Error **errp)
3409 BlockDriverState *bs;
3410 BlockDriverState *target_bs;
3411 Error *local_err = NULL;
3412 AioContext *aio_context;
3413 BlockJob *job = NULL;
3415 if (!backup->has_speed) {
3416 backup->speed = 0;
3418 if (!backup->has_on_source_error) {
3419 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3421 if (!backup->has_on_target_error) {
3422 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3424 if (!backup->has_job_id) {
3425 backup->job_id = NULL;
3427 if (!backup->has_compress) {
3428 backup->compress = false;
3431 bs = qmp_get_root_bs(backup->device, errp);
3432 if (!bs) {
3433 return NULL;
3436 aio_context = bdrv_get_aio_context(bs);
3437 aio_context_acquire(aio_context);
3439 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
3440 if (!target_bs) {
3441 goto out;
3444 if (bdrv_get_aio_context(target_bs) != aio_context) {
3445 if (!bdrv_has_blk(target_bs)) {
3446 /* The target BDS is not attached, we can safely move it to another
3447 * AioContext. */
3448 bdrv_set_aio_context(target_bs, aio_context);
3449 } else {
3450 error_setg(errp, "Target is attached to a different thread from "
3451 "source.");
3452 goto out;
3455 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
3456 backup->sync, NULL, backup->compress,
3457 backup->on_source_error, backup->on_target_error,
3458 BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err);
3459 if (local_err != NULL) {
3460 error_propagate(errp, local_err);
3462 out:
3463 aio_context_release(aio_context);
3464 return job;
3467 void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp)
3469 BlockJob *job;
3470 job = do_blockdev_backup(arg, NULL, errp);
3471 if (job) {
3472 block_job_start(job);
3476 /* Parameter check and block job starting for drive mirroring.
3477 * Caller should hold @device and @target's aio context (must be the same).
3479 static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
3480 BlockDriverState *target,
3481 bool has_replaces, const char *replaces,
3482 enum MirrorSyncMode sync,
3483 BlockMirrorBackingMode backing_mode,
3484 bool has_speed, int64_t speed,
3485 bool has_granularity, uint32_t granularity,
3486 bool has_buf_size, int64_t buf_size,
3487 bool has_on_source_error,
3488 BlockdevOnError on_source_error,
3489 bool has_on_target_error,
3490 BlockdevOnError on_target_error,
3491 bool has_unmap, bool unmap,
3492 bool has_filter_node_name,
3493 const char *filter_node_name,
3494 Error **errp)
3497 if (!has_speed) {
3498 speed = 0;
3500 if (!has_on_source_error) {
3501 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3503 if (!has_on_target_error) {
3504 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3506 if (!has_granularity) {
3507 granularity = 0;
3509 if (!has_buf_size) {
3510 buf_size = 0;
3512 if (!has_unmap) {
3513 unmap = true;
3515 if (!has_filter_node_name) {
3516 filter_node_name = NULL;
3519 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3520 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3521 "a value in range [512B, 64MB]");
3522 return;
3524 if (granularity & (granularity - 1)) {
3525 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3526 "power of 2");
3527 return;
3530 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3531 return;
3533 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3534 return;
3537 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3538 sync = MIRROR_SYNC_MODE_FULL;
3541 /* pass the node name to replace to mirror start since it's loose coupling
3542 * and will allow to check whether the node still exist at mirror completion
3544 mirror_start(job_id, bs, target,
3545 has_replaces ? replaces : NULL,
3546 speed, granularity, buf_size, sync, backing_mode,
3547 on_source_error, on_target_error, unmap, filter_node_name,
3548 errp);
3551 void qmp_drive_mirror(DriveMirror *arg, Error **errp)
3553 BlockDriverState *bs;
3554 BlockDriverState *source, *target_bs;
3555 AioContext *aio_context;
3556 BlockMirrorBackingMode backing_mode;
3557 Error *local_err = NULL;
3558 QDict *options = NULL;
3559 int flags;
3560 int64_t size;
3561 const char *format = arg->format;
3563 bs = qmp_get_root_bs(arg->device, errp);
3564 if (!bs) {
3565 return;
3568 aio_context = bdrv_get_aio_context(bs);
3569 aio_context_acquire(aio_context);
3571 if (!arg->has_mode) {
3572 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3575 if (!arg->has_format) {
3576 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3577 ? NULL : bs->drv->format_name);
3580 flags = bs->open_flags | BDRV_O_RDWR;
3581 source = backing_bs(bs);
3582 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3583 arg->sync = MIRROR_SYNC_MODE_FULL;
3585 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
3586 source = bs;
3589 size = bdrv_getlength(bs);
3590 if (size < 0) {
3591 error_setg_errno(errp, -size, "bdrv_getlength failed");
3592 goto out;
3595 if (arg->has_replaces) {
3596 BlockDriverState *to_replace_bs;
3597 AioContext *replace_aio_context;
3598 int64_t replace_size;
3600 if (!arg->has_node_name) {
3601 error_setg(errp, "a node-name must be provided when replacing a"
3602 " named node of the graph");
3603 goto out;
3606 to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err);
3608 if (!to_replace_bs) {
3609 error_propagate(errp, local_err);
3610 goto out;
3613 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3614 aio_context_acquire(replace_aio_context);
3615 replace_size = bdrv_getlength(to_replace_bs);
3616 aio_context_release(replace_aio_context);
3618 if (size != replace_size) {
3619 error_setg(errp, "cannot replace image with a mirror image of "
3620 "different size");
3621 goto out;
3625 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
3626 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3627 } else {
3628 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3631 /* Don't open backing image in create() */
3632 flags |= BDRV_O_NO_BACKING;
3634 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3635 && arg->mode != NEW_IMAGE_MODE_EXISTING)
3637 /* create new image w/o backing file */
3638 assert(format);
3639 bdrv_img_create(arg->target, format,
3640 NULL, NULL, NULL, size, flags, false, &local_err);
3641 } else {
3642 switch (arg->mode) {
3643 case NEW_IMAGE_MODE_EXISTING:
3644 break;
3645 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3646 /* create new image with backing file */
3647 bdrv_img_create(arg->target, format,
3648 source->filename,
3649 source->drv->format_name,
3650 NULL, size, flags, false, &local_err);
3651 break;
3652 default:
3653 abort();
3657 if (local_err) {
3658 error_propagate(errp, local_err);
3659 goto out;
3662 options = qdict_new();
3663 if (arg->has_node_name) {
3664 qdict_put_str(options, "node-name", arg->node_name);
3666 if (format) {
3667 qdict_put_str(options, "driver", format);
3670 /* Mirroring takes care of copy-on-write using the source's backing
3671 * file.
3673 target_bs = bdrv_open(arg->target, NULL, options, flags, errp);
3674 if (!target_bs) {
3675 goto out;
3678 bdrv_set_aio_context(target_bs, aio_context);
3680 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3681 arg->has_replaces, arg->replaces, arg->sync,
3682 backing_mode, arg->has_speed, arg->speed,
3683 arg->has_granularity, arg->granularity,
3684 arg->has_buf_size, arg->buf_size,
3685 arg->has_on_source_error, arg->on_source_error,
3686 arg->has_on_target_error, arg->on_target_error,
3687 arg->has_unmap, arg->unmap,
3688 false, NULL,
3689 &local_err);
3690 bdrv_unref(target_bs);
3691 error_propagate(errp, local_err);
3692 out:
3693 aio_context_release(aio_context);
3696 void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3697 const char *device, const char *target,
3698 bool has_replaces, const char *replaces,
3699 MirrorSyncMode sync,
3700 bool has_speed, int64_t speed,
3701 bool has_granularity, uint32_t granularity,
3702 bool has_buf_size, int64_t buf_size,
3703 bool has_on_source_error,
3704 BlockdevOnError on_source_error,
3705 bool has_on_target_error,
3706 BlockdevOnError on_target_error,
3707 bool has_filter_node_name,
3708 const char *filter_node_name,
3709 Error **errp)
3711 BlockDriverState *bs;
3712 BlockDriverState *target_bs;
3713 AioContext *aio_context;
3714 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
3715 Error *local_err = NULL;
3717 bs = qmp_get_root_bs(device, errp);
3718 if (!bs) {
3719 return;
3722 target_bs = bdrv_lookup_bs(target, target, errp);
3723 if (!target_bs) {
3724 return;
3727 aio_context = bdrv_get_aio_context(bs);
3728 aio_context_acquire(aio_context);
3730 bdrv_set_aio_context(target_bs, aio_context);
3732 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
3733 has_replaces, replaces, sync, backing_mode,
3734 has_speed, speed,
3735 has_granularity, granularity,
3736 has_buf_size, buf_size,
3737 has_on_source_error, on_source_error,
3738 has_on_target_error, on_target_error,
3739 true, true,
3740 has_filter_node_name, filter_node_name,
3741 &local_err);
3742 error_propagate(errp, local_err);
3744 aio_context_release(aio_context);
3747 /* Get a block job using its ID and acquire its AioContext */
3748 static BlockJob *find_block_job(const char *id, AioContext **aio_context,
3749 Error **errp)
3751 BlockJob *job;
3753 assert(id != NULL);
3755 *aio_context = NULL;
3757 job = block_job_get(id);
3759 if (!job) {
3760 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3761 "Block job '%s' not found", id);
3762 return NULL;
3765 *aio_context = blk_get_aio_context(job->blk);
3766 aio_context_acquire(*aio_context);
3768 return job;
3771 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
3773 AioContext *aio_context;
3774 BlockJob *job = find_block_job(device, &aio_context, errp);
3776 if (!job) {
3777 return;
3780 block_job_set_speed(job, speed, errp);
3781 aio_context_release(aio_context);
3784 void qmp_block_job_cancel(const char *device,
3785 bool has_force, bool force, Error **errp)
3787 AioContext *aio_context;
3788 BlockJob *job = find_block_job(device, &aio_context, errp);
3790 if (!job) {
3791 return;
3794 if (!has_force) {
3795 force = false;
3798 if (block_job_user_paused(job) && !force) {
3799 error_setg(errp, "The block job for device '%s' is currently paused",
3800 device);
3801 goto out;
3804 trace_qmp_block_job_cancel(job);
3805 block_job_cancel(job);
3806 out:
3807 aio_context_release(aio_context);
3810 void qmp_block_job_pause(const char *device, Error **errp)
3812 AioContext *aio_context;
3813 BlockJob *job = find_block_job(device, &aio_context, errp);
3815 if (!job || block_job_user_paused(job)) {
3816 return;
3819 trace_qmp_block_job_pause(job);
3820 block_job_user_pause(job);
3821 aio_context_release(aio_context);
3824 void qmp_block_job_resume(const char *device, Error **errp)
3826 AioContext *aio_context;
3827 BlockJob *job = find_block_job(device, &aio_context, errp);
3829 if (!job || !block_job_user_paused(job)) {
3830 return;
3833 trace_qmp_block_job_resume(job);
3834 block_job_user_resume(job);
3835 aio_context_release(aio_context);
3838 void qmp_block_job_complete(const char *device, Error **errp)
3840 AioContext *aio_context;
3841 BlockJob *job = find_block_job(device, &aio_context, errp);
3843 if (!job) {
3844 return;
3847 trace_qmp_block_job_complete(job);
3848 block_job_complete(job, errp);
3849 aio_context_release(aio_context);
3852 void qmp_change_backing_file(const char *device,
3853 const char *image_node_name,
3854 const char *backing_file,
3855 Error **errp)
3857 BlockDriverState *bs = NULL;
3858 AioContext *aio_context;
3859 BlockDriverState *image_bs = NULL;
3860 Error *local_err = NULL;
3861 bool ro;
3862 int open_flags;
3863 int ret;
3865 bs = qmp_get_root_bs(device, errp);
3866 if (!bs) {
3867 return;
3870 aio_context = bdrv_get_aio_context(bs);
3871 aio_context_acquire(aio_context);
3873 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3874 if (local_err) {
3875 error_propagate(errp, local_err);
3876 goto out;
3879 if (!image_bs) {
3880 error_setg(errp, "image file not found");
3881 goto out;
3884 if (bdrv_find_base(image_bs) == image_bs) {
3885 error_setg(errp, "not allowing backing file change on an image "
3886 "without a backing file");
3887 goto out;
3890 /* even though we are not necessarily operating on bs, we need it to
3891 * determine if block ops are currently prohibited on the chain */
3892 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3893 goto out;
3896 /* final sanity check */
3897 if (!bdrv_chain_contains(bs, image_bs)) {
3898 error_setg(errp, "'%s' and image file are not in the same chain",
3899 device);
3900 goto out;
3903 /* if not r/w, reopen to make r/w */
3904 open_flags = image_bs->open_flags;
3905 ro = bdrv_is_read_only(image_bs);
3907 if (ro) {
3908 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3909 if (local_err) {
3910 error_propagate(errp, local_err);
3911 goto out;
3915 ret = bdrv_change_backing_file(image_bs, backing_file,
3916 image_bs->drv ? image_bs->drv->format_name : "");
3918 if (ret < 0) {
3919 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3920 backing_file);
3921 /* don't exit here, so we can try to restore open flags if
3922 * appropriate */
3925 if (ro) {
3926 bdrv_reopen(image_bs, open_flags, &local_err);
3927 error_propagate(errp, local_err);
3930 out:
3931 aio_context_release(aio_context);
3934 void hmp_drive_add_node(Monitor *mon, const char *optstr)
3936 QemuOpts *opts;
3937 QDict *qdict;
3938 Error *local_err = NULL;
3940 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
3941 if (!opts) {
3942 return;
3945 qdict = qemu_opts_to_qdict(opts, NULL);
3947 if (!qdict_get_try_str(qdict, "node-name")) {
3948 QDECREF(qdict);
3949 error_report("'node-name' needs to be specified");
3950 goto out;
3953 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
3954 if (!bs) {
3955 error_report_err(local_err);
3956 goto out;
3959 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3961 out:
3962 qemu_opts_del(opts);
3965 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3967 BlockDriverState *bs;
3968 QObject *obj;
3969 Visitor *v = qobject_output_visitor_new(&obj);
3970 QDict *qdict;
3971 const QDictEntry *ent;
3972 Error *local_err = NULL;
3974 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
3975 if (local_err) {
3976 error_propagate(errp, local_err);
3977 goto fail;
3980 visit_complete(v, &obj);
3981 qdict = qobject_to_qdict(obj);
3983 qdict_flatten(qdict);
3986 * Rewrite "backing": null to "backing": ""
3987 * TODO Rewrite "" to null instead, and perhaps not even here
3989 for (ent = qdict_first(qdict); ent; ent = qdict_next(qdict, ent)) {
3990 char *dot = strrchr(ent->key, '.');
3992 if (!strcmp(dot ? dot + 1 : ent->key, "backing")
3993 && qobject_type(ent->value) == QTYPE_QNULL) {
3994 qdict_put(qdict, ent->key, qstring_new());
3998 if (!qdict_get_try_str(qdict, "node-name")) {
3999 error_setg(errp, "'node-name' must be specified for the root node");
4000 goto fail;
4003 bs = bds_tree_init(qdict, errp);
4004 if (!bs) {
4005 goto fail;
4008 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
4010 fail:
4011 visit_free(v);
4014 void qmp_blockdev_del(const char *node_name, Error **errp)
4016 AioContext *aio_context;
4017 BlockDriverState *bs;
4019 bs = bdrv_find_node(node_name);
4020 if (!bs) {
4021 error_setg(errp, "Cannot find node %s", node_name);
4022 return;
4024 if (bdrv_has_blk(bs)) {
4025 error_setg(errp, "Node %s is in use", node_name);
4026 return;
4028 aio_context = bdrv_get_aio_context(bs);
4029 aio_context_acquire(aio_context);
4031 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
4032 goto out;
4035 if (!bs->monitor_list.tqe_prev) {
4036 error_setg(errp, "Node %s is not owned by the monitor",
4037 bs->node_name);
4038 goto out;
4041 if (bs->refcnt > 1) {
4042 error_setg(errp, "Block device %s is in use",
4043 bdrv_get_device_or_node_name(bs));
4044 goto out;
4047 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
4048 bdrv_unref(bs);
4050 out:
4051 aio_context_release(aio_context);
4054 static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
4055 const char *child_name)
4057 BdrvChild *child;
4059 QLIST_FOREACH(child, &parent_bs->children, next) {
4060 if (strcmp(child->name, child_name) == 0) {
4061 return child;
4065 return NULL;
4068 void qmp_x_blockdev_change(const char *parent, bool has_child,
4069 const char *child, bool has_node,
4070 const char *node, Error **errp)
4072 BlockDriverState *parent_bs, *new_bs = NULL;
4073 BdrvChild *p_child;
4075 parent_bs = bdrv_lookup_bs(parent, parent, errp);
4076 if (!parent_bs) {
4077 return;
4080 if (has_child == has_node) {
4081 if (has_child) {
4082 error_setg(errp, "The parameters child and node are in conflict");
4083 } else {
4084 error_setg(errp, "Either child or node must be specified");
4086 return;
4089 if (has_child) {
4090 p_child = bdrv_find_child(parent_bs, child);
4091 if (!p_child) {
4092 error_setg(errp, "Node '%s' does not have child '%s'",
4093 parent, child);
4094 return;
4096 bdrv_del_child(parent_bs, p_child, errp);
4099 if (has_node) {
4100 new_bs = bdrv_find_node(node);
4101 if (!new_bs) {
4102 error_setg(errp, "Node '%s' not found", node);
4103 return;
4105 bdrv_add_child(parent_bs, new_bs, errp);
4109 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
4111 BlockJobInfoList *head = NULL, **p_next = &head;
4112 BlockJob *job;
4114 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
4115 BlockJobInfoList *elem;
4116 AioContext *aio_context;
4118 if (block_job_is_internal(job)) {
4119 continue;
4121 elem = g_new0(BlockJobInfoList, 1);
4122 aio_context = blk_get_aio_context(job->blk);
4123 aio_context_acquire(aio_context);
4124 elem->value = block_job_query(job, errp);
4125 aio_context_release(aio_context);
4126 if (!elem->value) {
4127 g_free(elem);
4128 qapi_free_BlockJobInfoList(head);
4129 return NULL;
4131 *p_next = elem;
4132 p_next = &elem->next;
4135 return head;
4138 void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
4139 bool has_force, bool force, Error **errp)
4141 AioContext *old_context;
4142 AioContext *new_context;
4143 BlockDriverState *bs;
4145 bs = bdrv_find_node(node_name);
4146 if (!bs) {
4147 error_setg(errp, "Cannot find node %s", node_name);
4148 return;
4151 /* Protects against accidents. */
4152 if (!(has_force && force) && bdrv_has_blk(bs)) {
4153 error_setg(errp, "Node %s is associated with a BlockBackend and could "
4154 "be in use (use force=true to override this check)",
4155 node_name);
4156 return;
4159 if (iothread->type == QTYPE_QSTRING) {
4160 IOThread *obj = iothread_by_id(iothread->u.s);
4161 if (!obj) {
4162 error_setg(errp, "Cannot find iothread %s", iothread->u.s);
4163 return;
4166 new_context = iothread_get_aio_context(obj);
4167 } else {
4168 new_context = qemu_get_aio_context();
4171 old_context = bdrv_get_aio_context(bs);
4172 aio_context_acquire(old_context);
4174 bdrv_set_aio_context(bs, new_context);
4176 aio_context_release(old_context);
4179 QemuOptsList qemu_common_drive_opts = {
4180 .name = "drive",
4181 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4182 .desc = {
4184 .name = "snapshot",
4185 .type = QEMU_OPT_BOOL,
4186 .help = "enable/disable snapshot mode",
4188 .name = "aio",
4189 .type = QEMU_OPT_STRING,
4190 .help = "host AIO implementation (threads, native)",
4192 .name = BDRV_OPT_CACHE_WB,
4193 .type = QEMU_OPT_BOOL,
4194 .help = "Enable writeback mode",
4196 .name = "format",
4197 .type = QEMU_OPT_STRING,
4198 .help = "disk format (raw, qcow2, ...)",
4200 .name = "rerror",
4201 .type = QEMU_OPT_STRING,
4202 .help = "read error action",
4204 .name = "werror",
4205 .type = QEMU_OPT_STRING,
4206 .help = "write error action",
4208 .name = BDRV_OPT_READ_ONLY,
4209 .type = QEMU_OPT_BOOL,
4210 .help = "open drive file as read-only",
4213 THROTTLE_OPTS,
4216 .name = "throttling.group",
4217 .type = QEMU_OPT_STRING,
4218 .help = "name of the block throttling group",
4220 .name = "copy-on-read",
4221 .type = QEMU_OPT_BOOL,
4222 .help = "copy read data from backing file into image file",
4224 .name = "detect-zeroes",
4225 .type = QEMU_OPT_STRING,
4226 .help = "try to optimize zero writes (off, on, unmap)",
4228 .name = "stats-account-invalid",
4229 .type = QEMU_OPT_BOOL,
4230 .help = "whether to account for invalid I/O operations "
4231 "in the statistics",
4233 .name = "stats-account-failed",
4234 .type = QEMU_OPT_BOOL,
4235 .help = "whether to account for failed I/O operations "
4236 "in the statistics",
4238 { /* end of list */ }
4242 QemuOptsList qemu_drive_opts = {
4243 .name = "drive",
4244 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4245 .desc = {
4247 * no elements => accept any params
4248 * validation will happen later
4250 { /* end of list */ }