docs: Document the throttling infrastructure
[qemu/ar7.git] / blockdev.c
blob5c02a4289c435d5eaf290108c435602d7a7ca902
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/qmp-output-visitor.h"
47 #include "qapi/util.h"
48 #include "sysemu/sysemu.h"
49 #include "block/block_int.h"
50 #include "qmp-commands.h"
51 #include "trace.h"
52 #include "sysemu/arch_init.h"
54 static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
55 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
57 static const char *const if_name[IF_COUNT] = {
58 [IF_NONE] = "none",
59 [IF_IDE] = "ide",
60 [IF_SCSI] = "scsi",
61 [IF_FLOPPY] = "floppy",
62 [IF_PFLASH] = "pflash",
63 [IF_MTD] = "mtd",
64 [IF_SD] = "sd",
65 [IF_VIRTIO] = "virtio",
66 [IF_XEN] = "xen",
69 static int if_max_devs[IF_COUNT] = {
71 * Do not change these numbers! They govern how drive option
72 * index maps to unit and bus. That mapping is ABI.
74 * All controllers used to imlement if=T drives need to support
75 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
76 * Otherwise, some index values map to "impossible" bus, unit
77 * values.
79 * For instance, if you change [IF_SCSI] to 255, -drive
80 * if=scsi,index=12 no longer means bus=1,unit=5, but
81 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
82 * the drive can't be set up. Regression.
84 [IF_IDE] = 2,
85 [IF_SCSI] = 7,
88 /**
89 * Boards may call this to offer board-by-board overrides
90 * of the default, global values.
92 void override_max_devs(BlockInterfaceType type, int max_devs)
94 BlockBackend *blk;
95 DriveInfo *dinfo;
97 if (max_devs <= 0) {
98 return;
101 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
102 dinfo = blk_legacy_dinfo(blk);
103 if (dinfo->type == type) {
104 fprintf(stderr, "Cannot override units-per-bus property of"
105 " the %s interface, because a drive of that type has"
106 " already been added.\n", if_name[type]);
107 g_assert_not_reached();
111 if_max_devs[type] = max_devs;
115 * We automatically delete the drive when a device using it gets
116 * unplugged. Questionable feature, but we can't just drop it.
117 * Device models call blockdev_mark_auto_del() to schedule the
118 * automatic deletion, and generic qdev code calls blockdev_auto_del()
119 * when deletion is actually safe.
121 void blockdev_mark_auto_del(BlockBackend *blk)
123 DriveInfo *dinfo = blk_legacy_dinfo(blk);
124 BlockDriverState *bs = blk_bs(blk);
125 AioContext *aio_context;
127 if (!dinfo) {
128 return;
131 if (bs) {
132 aio_context = bdrv_get_aio_context(bs);
133 aio_context_acquire(aio_context);
135 if (bs->job) {
136 block_job_cancel(bs->job);
139 aio_context_release(aio_context);
142 dinfo->auto_del = 1;
145 void blockdev_auto_del(BlockBackend *blk)
147 DriveInfo *dinfo = blk_legacy_dinfo(blk);
149 if (dinfo && dinfo->auto_del) {
150 blk_unref(blk);
155 * Returns the current mapping of how many units per bus
156 * a particular interface can support.
158 * A positive integer indicates n units per bus.
159 * 0 implies the mapping has not been established.
160 * -1 indicates an invalid BlockInterfaceType was given.
162 int drive_get_max_devs(BlockInterfaceType type)
164 if (type >= IF_IDE && type < IF_COUNT) {
165 return if_max_devs[type];
168 return -1;
171 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
173 int max_devs = if_max_devs[type];
174 return max_devs ? index / max_devs : 0;
177 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
179 int max_devs = if_max_devs[type];
180 return max_devs ? index % max_devs : index;
183 QemuOpts *drive_def(const char *optstr)
185 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
188 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
189 const char *optstr)
191 QemuOpts *opts;
193 opts = drive_def(optstr);
194 if (!opts) {
195 return NULL;
197 if (type != IF_DEFAULT) {
198 qemu_opt_set(opts, "if", if_name[type], &error_abort);
200 if (index >= 0) {
201 qemu_opt_set_number(opts, "index", index, &error_abort);
203 if (file)
204 qemu_opt_set(opts, "file", file, &error_abort);
205 return opts;
208 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
210 BlockBackend *blk;
211 DriveInfo *dinfo;
213 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
214 dinfo = blk_legacy_dinfo(blk);
215 if (dinfo && dinfo->type == type
216 && dinfo->bus == bus && dinfo->unit == unit) {
217 return dinfo;
221 return NULL;
224 bool drive_check_orphaned(void)
226 BlockBackend *blk;
227 DriveInfo *dinfo;
228 bool rs = false;
230 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
231 dinfo = blk_legacy_dinfo(blk);
232 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
233 /* Unless this is a default drive, this may be an oversight. */
234 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
235 dinfo->type != IF_NONE) {
236 fprintf(stderr, "Warning: Orphaned drive without device: "
237 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
238 blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
239 if_name[dinfo->type], dinfo->bus, dinfo->unit);
240 rs = true;
244 return rs;
247 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
249 return drive_get(type,
250 drive_index_to_bus_id(type, index),
251 drive_index_to_unit_id(type, index));
254 int drive_get_max_bus(BlockInterfaceType type)
256 int max_bus;
257 BlockBackend *blk;
258 DriveInfo *dinfo;
260 max_bus = -1;
261 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
262 dinfo = blk_legacy_dinfo(blk);
263 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
264 max_bus = dinfo->bus;
267 return max_bus;
270 /* Get a block device. This should only be used for single-drive devices
271 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
272 appropriate bus. */
273 DriveInfo *drive_get_next(BlockInterfaceType type)
275 static int next_block_unit[IF_COUNT];
277 return drive_get(type, 0, next_block_unit[type]++);
280 static void bdrv_format_print(void *opaque, const char *name)
282 error_printf(" %s", name);
285 typedef struct {
286 QEMUBH *bh;
287 BlockDriverState *bs;
288 } BDRVPutRefBH;
290 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
292 if (!strcmp(buf, "ignore")) {
293 return BLOCKDEV_ON_ERROR_IGNORE;
294 } else if (!is_read && !strcmp(buf, "enospc")) {
295 return BLOCKDEV_ON_ERROR_ENOSPC;
296 } else if (!strcmp(buf, "stop")) {
297 return BLOCKDEV_ON_ERROR_STOP;
298 } else if (!strcmp(buf, "report")) {
299 return BLOCKDEV_ON_ERROR_REPORT;
300 } else {
301 error_setg(errp, "'%s' invalid %s error action",
302 buf, is_read ? "read" : "write");
303 return -1;
307 static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
308 Error **errp)
310 const QListEntry *entry;
311 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
312 switch (qobject_type(entry->value)) {
314 case QTYPE_QSTRING: {
315 unsigned long long length;
316 const char *str = qstring_get_str(qobject_to_qstring(entry->value));
317 if (parse_uint_full(str, &length, 10) == 0 &&
318 length > 0 && length <= UINT_MAX) {
319 block_acct_add_interval(stats, (unsigned) length);
320 } else {
321 error_setg(errp, "Invalid interval length: %s", str);
322 return false;
324 break;
327 case QTYPE_QINT: {
328 int64_t length = qint_get_int(qobject_to_qint(entry->value));
329 if (length > 0 && length <= UINT_MAX) {
330 block_acct_add_interval(stats, (unsigned) length);
331 } else {
332 error_setg(errp, "Invalid interval length: %" PRId64, length);
333 return false;
335 break;
338 default:
339 error_setg(errp, "The specification of stats-intervals is invalid");
340 return false;
343 return true;
346 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
348 /* All parameters but @opts are optional and may be set to NULL. */
349 static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
350 const char **throttling_group, ThrottleConfig *throttle_cfg,
351 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
353 const char *discard;
354 Error *local_error = NULL;
355 const char *aio;
357 if (bdrv_flags) {
358 if (!qemu_opt_get_bool(opts, "read-only", false)) {
359 *bdrv_flags |= BDRV_O_RDWR;
361 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
362 *bdrv_flags |= BDRV_O_COPY_ON_READ;
365 if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
366 if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
367 error_setg(errp, "Invalid discard option");
368 return;
372 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
373 if (!strcmp(aio, "native")) {
374 *bdrv_flags |= BDRV_O_NATIVE_AIO;
375 } else if (!strcmp(aio, "threads")) {
376 /* this is the default */
377 } else {
378 error_setg(errp, "invalid aio option");
379 return;
384 /* disk I/O throttling */
385 if (throttling_group) {
386 *throttling_group = qemu_opt_get(opts, "throttling.group");
389 if (throttle_cfg) {
390 throttle_config_init(throttle_cfg);
391 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
392 qemu_opt_get_number(opts, "throttling.bps-total", 0);
393 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
394 qemu_opt_get_number(opts, "throttling.bps-read", 0);
395 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
396 qemu_opt_get_number(opts, "throttling.bps-write", 0);
397 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
398 qemu_opt_get_number(opts, "throttling.iops-total", 0);
399 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
400 qemu_opt_get_number(opts, "throttling.iops-read", 0);
401 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
402 qemu_opt_get_number(opts, "throttling.iops-write", 0);
404 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
405 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
406 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
407 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
408 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
409 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
410 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
411 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
412 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
413 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
414 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
415 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
417 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
418 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
419 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
420 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
421 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
422 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
423 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
424 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
425 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
426 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
427 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
428 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
430 throttle_cfg->op_size =
431 qemu_opt_get_number(opts, "throttling.iops-size", 0);
433 if (!throttle_is_valid(throttle_cfg, errp)) {
434 return;
438 if (detect_zeroes) {
439 *detect_zeroes =
440 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
441 qemu_opt_get(opts, "detect-zeroes"),
442 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
443 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
444 &local_error);
445 if (local_error) {
446 error_propagate(errp, local_error);
447 return;
450 if (bdrv_flags &&
451 *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
452 !(*bdrv_flags & BDRV_O_UNMAP))
454 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
455 "without setting discard operation to unmap");
456 return;
461 /* Takes the ownership of bs_opts */
462 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
463 Error **errp)
465 const char *buf;
466 int bdrv_flags = 0;
467 int on_read_error, on_write_error;
468 bool account_invalid, account_failed;
469 BlockBackend *blk;
470 BlockDriverState *bs;
471 ThrottleConfig cfg;
472 int snapshot = 0;
473 Error *error = NULL;
474 QemuOpts *opts;
475 QDict *interval_dict = NULL;
476 QList *interval_list = NULL;
477 const char *id;
478 BlockdevDetectZeroesOptions detect_zeroes =
479 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
480 const char *throttling_group = NULL;
482 /* Check common options by copying from bs_opts to opts, all other options
483 * stay in bs_opts for processing by bdrv_open(). */
484 id = qdict_get_try_str(bs_opts, "id");
485 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
486 if (error) {
487 error_propagate(errp, error);
488 goto err_no_opts;
491 qemu_opts_absorb_qdict(opts, bs_opts, &error);
492 if (error) {
493 error_propagate(errp, error);
494 goto early_err;
497 if (id) {
498 qdict_del(bs_opts, "id");
501 /* extract parameters */
502 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
504 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
505 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
507 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
508 qdict_array_split(interval_dict, &interval_list);
510 if (qdict_size(interval_dict) != 0) {
511 error_setg(errp, "Invalid option stats-intervals.%s",
512 qdict_first(interval_dict)->key);
513 goto early_err;
516 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
517 &detect_zeroes, &error);
518 if (error) {
519 error_propagate(errp, error);
520 goto early_err;
523 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
524 if (is_help_option(buf)) {
525 error_printf("Supported formats:");
526 bdrv_iterate_format(bdrv_format_print, NULL);
527 error_printf("\n");
528 goto early_err;
531 if (qdict_haskey(bs_opts, "driver")) {
532 error_setg(errp, "Cannot specify both 'driver' and 'format'");
533 goto early_err;
535 qdict_put(bs_opts, "driver", qstring_from_str(buf));
538 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
539 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
540 on_write_error = parse_block_error_action(buf, 0, &error);
541 if (error) {
542 error_propagate(errp, error);
543 goto early_err;
547 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
548 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
549 on_read_error = parse_block_error_action(buf, 1, &error);
550 if (error) {
551 error_propagate(errp, error);
552 goto early_err;
556 if (snapshot) {
557 bdrv_flags |= BDRV_O_SNAPSHOT;
560 /* init */
561 if ((!file || !*file) && !qdict_size(bs_opts)) {
562 BlockBackendRootState *blk_rs;
564 blk = blk_new(qemu_opts_id(opts), errp);
565 if (!blk) {
566 goto early_err;
569 blk_rs = blk_get_root_state(blk);
570 blk_rs->open_flags = bdrv_flags;
571 blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR);
572 blk_rs->detect_zeroes = detect_zeroes;
574 if (throttle_enabled(&cfg)) {
575 if (!throttling_group) {
576 throttling_group = blk_name(blk);
578 blk_rs->throttle_group = g_strdup(throttling_group);
579 blk_rs->throttle_state = throttle_group_incref(throttling_group);
580 blk_rs->throttle_state->cfg = cfg;
583 QDECREF(bs_opts);
584 } else {
585 if (file && !*file) {
586 file = NULL;
589 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
590 * with other callers) rather than what we want as the real defaults.
591 * Apply the defaults here instead. */
592 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_WB, "on");
593 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
594 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
596 if (snapshot) {
597 /* always use cache=unsafe with snapshot */
598 qdict_put(bs_opts, BDRV_OPT_CACHE_WB, qstring_from_str("on"));
599 qdict_put(bs_opts, BDRV_OPT_CACHE_DIRECT, qstring_from_str("off"));
600 qdict_put(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, qstring_from_str("on"));
603 if (runstate_check(RUN_STATE_INMIGRATE)) {
604 bdrv_flags |= BDRV_O_INACTIVE;
607 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
608 errp);
609 if (!blk) {
610 goto err_no_bs_opts;
612 bs = blk_bs(blk);
614 bs->detect_zeroes = detect_zeroes;
616 /* disk I/O throttling */
617 if (throttle_enabled(&cfg)) {
618 if (!throttling_group) {
619 throttling_group = blk_name(blk);
621 bdrv_io_limits_enable(bs, throttling_group);
622 bdrv_set_io_limits(bs, &cfg);
625 if (bdrv_key_required(bs)) {
626 autostart = 0;
629 block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
631 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
632 blk_unref(blk);
633 blk = NULL;
634 goto err_no_bs_opts;
638 blk_set_on_error(blk, on_read_error, on_write_error);
640 err_no_bs_opts:
641 qemu_opts_del(opts);
642 QDECREF(interval_dict);
643 QDECREF(interval_list);
644 return blk;
646 early_err:
647 qemu_opts_del(opts);
648 QDECREF(interval_dict);
649 QDECREF(interval_list);
650 err_no_opts:
651 QDECREF(bs_opts);
652 return NULL;
655 static QemuOptsList qemu_root_bds_opts;
657 /* Takes the ownership of bs_opts */
658 static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
660 BlockDriverState *bs;
661 QemuOpts *opts;
662 Error *local_error = NULL;
663 BlockdevDetectZeroesOptions detect_zeroes;
664 int ret;
665 int bdrv_flags = 0;
667 opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
668 if (!opts) {
669 goto fail;
672 qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
673 if (local_error) {
674 error_propagate(errp, local_error);
675 goto fail;
678 extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
679 &detect_zeroes, &local_error);
680 if (local_error) {
681 error_propagate(errp, local_error);
682 goto fail;
685 if (runstate_check(RUN_STATE_INMIGRATE)) {
686 bdrv_flags |= BDRV_O_INACTIVE;
689 bs = NULL;
690 ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp);
691 if (ret < 0) {
692 goto fail_no_bs_opts;
695 bs->detect_zeroes = detect_zeroes;
697 fail_no_bs_opts:
698 qemu_opts_del(opts);
699 return bs;
701 fail:
702 qemu_opts_del(opts);
703 QDECREF(bs_opts);
704 return NULL;
707 void blockdev_close_all_bdrv_states(void)
709 BlockDriverState *bs, *next_bs;
711 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
712 AioContext *ctx = bdrv_get_aio_context(bs);
714 aio_context_acquire(ctx);
715 bdrv_unref(bs);
716 aio_context_release(ctx);
720 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
721 Error **errp)
723 const char *value;
725 value = qemu_opt_get(opts, from);
726 if (value) {
727 if (qemu_opt_find(opts, to)) {
728 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
729 "same time", to, from);
730 return;
734 /* rename all items in opts */
735 while ((value = qemu_opt_get(opts, from))) {
736 qemu_opt_set(opts, to, value, &error_abort);
737 qemu_opt_unset(opts, from);
741 QemuOptsList qemu_legacy_drive_opts = {
742 .name = "drive",
743 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
744 .desc = {
746 .name = "bus",
747 .type = QEMU_OPT_NUMBER,
748 .help = "bus number",
750 .name = "unit",
751 .type = QEMU_OPT_NUMBER,
752 .help = "unit number (i.e. lun for scsi)",
754 .name = "index",
755 .type = QEMU_OPT_NUMBER,
756 .help = "index number",
758 .name = "media",
759 .type = QEMU_OPT_STRING,
760 .help = "media type (disk, cdrom)",
762 .name = "if",
763 .type = QEMU_OPT_STRING,
764 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
766 .name = "cyls",
767 .type = QEMU_OPT_NUMBER,
768 .help = "number of cylinders (ide disk geometry)",
770 .name = "heads",
771 .type = QEMU_OPT_NUMBER,
772 .help = "number of heads (ide disk geometry)",
774 .name = "secs",
775 .type = QEMU_OPT_NUMBER,
776 .help = "number of sectors (ide disk geometry)",
778 .name = "trans",
779 .type = QEMU_OPT_STRING,
780 .help = "chs translation (auto, lba, none)",
782 .name = "boot",
783 .type = QEMU_OPT_BOOL,
784 .help = "(deprecated, ignored)",
786 .name = "addr",
787 .type = QEMU_OPT_STRING,
788 .help = "pci address (virtio only)",
790 .name = "serial",
791 .type = QEMU_OPT_STRING,
792 .help = "disk serial number",
794 .name = "file",
795 .type = QEMU_OPT_STRING,
796 .help = "file name",
799 /* Options that are passed on, but have special semantics with -drive */
801 .name = "read-only",
802 .type = QEMU_OPT_BOOL,
803 .help = "open drive file as read-only",
805 .name = "rerror",
806 .type = QEMU_OPT_STRING,
807 .help = "read error action",
809 .name = "werror",
810 .type = QEMU_OPT_STRING,
811 .help = "write error action",
813 .name = "copy-on-read",
814 .type = QEMU_OPT_BOOL,
815 .help = "copy read data from backing file into image file",
818 { /* end of list */ }
822 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
824 const char *value;
825 BlockBackend *blk;
826 DriveInfo *dinfo = NULL;
827 QDict *bs_opts;
828 QemuOpts *legacy_opts;
829 DriveMediaType media = MEDIA_DISK;
830 BlockInterfaceType type;
831 int cyls, heads, secs, translation;
832 int max_devs, bus_id, unit_id, index;
833 const char *devaddr;
834 const char *werror, *rerror;
835 bool read_only = false;
836 bool copy_on_read;
837 const char *serial;
838 const char *filename;
839 Error *local_err = NULL;
840 int i;
842 /* Change legacy command line options into QMP ones */
843 static const struct {
844 const char *from;
845 const char *to;
846 } opt_renames[] = {
847 { "iops", "throttling.iops-total" },
848 { "iops_rd", "throttling.iops-read" },
849 { "iops_wr", "throttling.iops-write" },
851 { "bps", "throttling.bps-total" },
852 { "bps_rd", "throttling.bps-read" },
853 { "bps_wr", "throttling.bps-write" },
855 { "iops_max", "throttling.iops-total-max" },
856 { "iops_rd_max", "throttling.iops-read-max" },
857 { "iops_wr_max", "throttling.iops-write-max" },
859 { "bps_max", "throttling.bps-total-max" },
860 { "bps_rd_max", "throttling.bps-read-max" },
861 { "bps_wr_max", "throttling.bps-write-max" },
863 { "iops_size", "throttling.iops-size" },
865 { "group", "throttling.group" },
867 { "readonly", "read-only" },
870 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
871 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
872 &local_err);
873 if (local_err) {
874 error_report_err(local_err);
875 return NULL;
879 value = qemu_opt_get(all_opts, "cache");
880 if (value) {
881 int flags = 0;
883 if (bdrv_parse_cache_flags(value, &flags) != 0) {
884 error_report("invalid cache option");
885 return NULL;
888 /* Specific options take precedence */
889 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
890 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
891 !!(flags & BDRV_O_CACHE_WB), &error_abort);
893 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
894 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
895 !!(flags & BDRV_O_NOCACHE), &error_abort);
897 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
898 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
899 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
901 qemu_opt_unset(all_opts, "cache");
904 /* Get a QDict for processing the options */
905 bs_opts = qdict_new();
906 qemu_opts_to_qdict(all_opts, bs_opts);
908 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
909 &error_abort);
910 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
911 if (local_err) {
912 error_report_err(local_err);
913 goto fail;
916 /* Deprecated option boot=[on|off] */
917 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
918 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
919 "ignored. Future versions will reject this parameter. Please "
920 "update your scripts.\n");
923 /* Media type */
924 value = qemu_opt_get(legacy_opts, "media");
925 if (value) {
926 if (!strcmp(value, "disk")) {
927 media = MEDIA_DISK;
928 } else if (!strcmp(value, "cdrom")) {
929 media = MEDIA_CDROM;
930 read_only = true;
931 } else {
932 error_report("'%s' invalid media", value);
933 goto fail;
937 /* copy-on-read is disabled with a warning for read-only devices */
938 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
939 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
941 if (read_only && copy_on_read) {
942 error_report("warning: disabling copy-on-read on read-only drive");
943 copy_on_read = false;
946 qdict_put(bs_opts, "read-only",
947 qstring_from_str(read_only ? "on" : "off"));
948 qdict_put(bs_opts, "copy-on-read",
949 qstring_from_str(copy_on_read ? "on" :"off"));
951 /* Controller type */
952 value = qemu_opt_get(legacy_opts, "if");
953 if (value) {
954 for (type = 0;
955 type < IF_COUNT && strcmp(value, if_name[type]);
956 type++) {
958 if (type == IF_COUNT) {
959 error_report("unsupported bus type '%s'", value);
960 goto fail;
962 } else {
963 type = block_default_type;
966 /* Geometry */
967 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
968 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
969 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
971 if (cyls || heads || secs) {
972 if (cyls < 1) {
973 error_report("invalid physical cyls number");
974 goto fail;
976 if (heads < 1) {
977 error_report("invalid physical heads number");
978 goto fail;
980 if (secs < 1) {
981 error_report("invalid physical secs number");
982 goto fail;
986 translation = BIOS_ATA_TRANSLATION_AUTO;
987 value = qemu_opt_get(legacy_opts, "trans");
988 if (value != NULL) {
989 if (!cyls) {
990 error_report("'%s' trans must be used with cyls, heads and secs",
991 value);
992 goto fail;
994 if (!strcmp(value, "none")) {
995 translation = BIOS_ATA_TRANSLATION_NONE;
996 } else if (!strcmp(value, "lba")) {
997 translation = BIOS_ATA_TRANSLATION_LBA;
998 } else if (!strcmp(value, "large")) {
999 translation = BIOS_ATA_TRANSLATION_LARGE;
1000 } else if (!strcmp(value, "rechs")) {
1001 translation = BIOS_ATA_TRANSLATION_RECHS;
1002 } else if (!strcmp(value, "auto")) {
1003 translation = BIOS_ATA_TRANSLATION_AUTO;
1004 } else {
1005 error_report("'%s' invalid translation type", value);
1006 goto fail;
1010 if (media == MEDIA_CDROM) {
1011 if (cyls || secs || heads) {
1012 error_report("CHS can't be set with media=cdrom");
1013 goto fail;
1017 /* Device address specified by bus/unit or index.
1018 * If none was specified, try to find the first free one. */
1019 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
1020 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
1021 index = qemu_opt_get_number(legacy_opts, "index", -1);
1023 max_devs = if_max_devs[type];
1025 if (index != -1) {
1026 if (bus_id != 0 || unit_id != -1) {
1027 error_report("index cannot be used with bus and unit");
1028 goto fail;
1030 bus_id = drive_index_to_bus_id(type, index);
1031 unit_id = drive_index_to_unit_id(type, index);
1034 if (unit_id == -1) {
1035 unit_id = 0;
1036 while (drive_get(type, bus_id, unit_id) != NULL) {
1037 unit_id++;
1038 if (max_devs && unit_id >= max_devs) {
1039 unit_id -= max_devs;
1040 bus_id++;
1045 if (max_devs && unit_id >= max_devs) {
1046 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1047 goto fail;
1050 if (drive_get(type, bus_id, unit_id) != NULL) {
1051 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1052 bus_id, unit_id, index);
1053 goto fail;
1056 /* Serial number */
1057 serial = qemu_opt_get(legacy_opts, "serial");
1059 /* no id supplied -> create one */
1060 if (qemu_opts_id(all_opts) == NULL) {
1061 char *new_id;
1062 const char *mediastr = "";
1063 if (type == IF_IDE || type == IF_SCSI) {
1064 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1066 if (max_devs) {
1067 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1068 mediastr, unit_id);
1069 } else {
1070 new_id = g_strdup_printf("%s%s%i", if_name[type],
1071 mediastr, unit_id);
1073 qdict_put(bs_opts, "id", qstring_from_str(new_id));
1074 g_free(new_id);
1077 /* Add virtio block device */
1078 devaddr = qemu_opt_get(legacy_opts, "addr");
1079 if (devaddr && type != IF_VIRTIO) {
1080 error_report("addr is not supported by this bus type");
1081 goto fail;
1084 if (type == IF_VIRTIO) {
1085 QemuOpts *devopts;
1086 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1087 &error_abort);
1088 if (arch_type == QEMU_ARCH_S390X) {
1089 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
1090 } else {
1091 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
1093 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1094 &error_abort);
1095 if (devaddr) {
1096 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
1100 filename = qemu_opt_get(legacy_opts, "file");
1102 /* Check werror/rerror compatibility with if=... */
1103 werror = qemu_opt_get(legacy_opts, "werror");
1104 if (werror != NULL) {
1105 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1106 type != IF_NONE) {
1107 error_report("werror is not supported by this bus type");
1108 goto fail;
1110 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1113 rerror = qemu_opt_get(legacy_opts, "rerror");
1114 if (rerror != NULL) {
1115 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1116 type != IF_NONE) {
1117 error_report("rerror is not supported by this bus type");
1118 goto fail;
1120 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1123 /* Actual block device init: Functionality shared with blockdev-add */
1124 blk = blockdev_init(filename, bs_opts, &local_err);
1125 bs_opts = NULL;
1126 if (!blk) {
1127 if (local_err) {
1128 error_report_err(local_err);
1130 goto fail;
1131 } else {
1132 assert(!local_err);
1135 /* Create legacy DriveInfo */
1136 dinfo = g_malloc0(sizeof(*dinfo));
1137 dinfo->opts = all_opts;
1139 dinfo->cyls = cyls;
1140 dinfo->heads = heads;
1141 dinfo->secs = secs;
1142 dinfo->trans = translation;
1144 dinfo->type = type;
1145 dinfo->bus = bus_id;
1146 dinfo->unit = unit_id;
1147 dinfo->devaddr = devaddr;
1148 dinfo->serial = g_strdup(serial);
1150 blk_set_legacy_dinfo(blk, dinfo);
1152 switch(type) {
1153 case IF_IDE:
1154 case IF_SCSI:
1155 case IF_XEN:
1156 case IF_NONE:
1157 dinfo->media_cd = media == MEDIA_CDROM;
1158 break;
1159 default:
1160 break;
1163 fail:
1164 qemu_opts_del(legacy_opts);
1165 QDECREF(bs_opts);
1166 return dinfo;
1169 void hmp_commit(Monitor *mon, const QDict *qdict)
1171 const char *device = qdict_get_str(qdict, "device");
1172 BlockBackend *blk;
1173 int ret;
1175 if (!strcmp(device, "all")) {
1176 ret = bdrv_commit_all();
1177 } else {
1178 BlockDriverState *bs;
1179 AioContext *aio_context;
1181 blk = blk_by_name(device);
1182 if (!blk) {
1183 monitor_printf(mon, "Device '%s' not found\n", device);
1184 return;
1186 if (!blk_is_available(blk)) {
1187 monitor_printf(mon, "Device '%s' has no medium\n", device);
1188 return;
1191 bs = blk_bs(blk);
1192 aio_context = bdrv_get_aio_context(bs);
1193 aio_context_acquire(aio_context);
1195 ret = bdrv_commit(bs);
1197 aio_context_release(aio_context);
1199 if (ret < 0) {
1200 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1201 strerror(-ret));
1205 static void blockdev_do_action(TransactionActionKind type, void *data,
1206 Error **errp)
1208 TransactionAction action;
1209 TransactionActionList list;
1211 action.type = type;
1212 action.u.data = data;
1213 list.value = &action;
1214 list.next = NULL;
1215 qmp_transaction(&list, false, NULL, errp);
1218 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1219 bool has_node_name, const char *node_name,
1220 const char *snapshot_file,
1221 bool has_snapshot_node_name,
1222 const char *snapshot_node_name,
1223 bool has_format, const char *format,
1224 bool has_mode, NewImageMode mode, Error **errp)
1226 BlockdevSnapshotSync snapshot = {
1227 .has_device = has_device,
1228 .device = (char *) device,
1229 .has_node_name = has_node_name,
1230 .node_name = (char *) node_name,
1231 .snapshot_file = (char *) snapshot_file,
1232 .has_snapshot_node_name = has_snapshot_node_name,
1233 .snapshot_node_name = (char *) snapshot_node_name,
1234 .has_format = has_format,
1235 .format = (char *) format,
1236 .has_mode = has_mode,
1237 .mode = mode,
1239 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1240 &snapshot, errp);
1243 void qmp_blockdev_snapshot(const char *node, const char *overlay,
1244 Error **errp)
1246 BlockdevSnapshot snapshot_data = {
1247 .node = (char *) node,
1248 .overlay = (char *) overlay
1251 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
1252 &snapshot_data, errp);
1255 void qmp_blockdev_snapshot_internal_sync(const char *device,
1256 const char *name,
1257 Error **errp)
1259 BlockdevSnapshotInternal snapshot = {
1260 .device = (char *) device,
1261 .name = (char *) name
1264 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1265 &snapshot, errp);
1268 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1269 bool has_id,
1270 const char *id,
1271 bool has_name,
1272 const char *name,
1273 Error **errp)
1275 BlockDriverState *bs;
1276 BlockBackend *blk;
1277 AioContext *aio_context;
1278 QEMUSnapshotInfo sn;
1279 Error *local_err = NULL;
1280 SnapshotInfo *info = NULL;
1281 int ret;
1283 blk = blk_by_name(device);
1284 if (!blk) {
1285 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1286 "Device '%s' not found", device);
1287 return NULL;
1290 aio_context = blk_get_aio_context(blk);
1291 aio_context_acquire(aio_context);
1293 if (!has_id) {
1294 id = NULL;
1297 if (!has_name) {
1298 name = NULL;
1301 if (!id && !name) {
1302 error_setg(errp, "Name or id must be provided");
1303 goto out_aio_context;
1306 if (!blk_is_available(blk)) {
1307 error_setg(errp, "Device '%s' has no medium", device);
1308 goto out_aio_context;
1310 bs = blk_bs(blk);
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 AioContext **paio,
1370 Error **errp)
1372 BlockDriverState *bs;
1373 BdrvDirtyBitmap *bitmap;
1374 AioContext *aio_context;
1376 if (!node) {
1377 error_setg(errp, "Node cannot be NULL");
1378 return NULL;
1380 if (!name) {
1381 error_setg(errp, "Bitmap name cannot be NULL");
1382 return NULL;
1384 bs = bdrv_lookup_bs(node, node, NULL);
1385 if (!bs) {
1386 error_setg(errp, "Node '%s' not found", node);
1387 return NULL;
1390 aio_context = bdrv_get_aio_context(bs);
1391 aio_context_acquire(aio_context);
1393 bitmap = bdrv_find_dirty_bitmap(bs, name);
1394 if (!bitmap) {
1395 error_setg(errp, "Dirty bitmap '%s' not found", name);
1396 goto fail;
1399 if (pbs) {
1400 *pbs = bs;
1402 if (paio) {
1403 *paio = aio_context;
1404 } else {
1405 aio_context_release(aio_context);
1408 return bitmap;
1410 fail:
1411 aio_context_release(aio_context);
1412 return NULL;
1415 /* New and old BlockDriverState structs for atomic group operations */
1417 typedef struct BlkActionState BlkActionState;
1420 * BlkActionOps:
1421 * Table of operations that define an Action.
1423 * @instance_size: Size of state struct, in bytes.
1424 * @prepare: Prepare the work, must NOT be NULL.
1425 * @commit: Commit the changes, can be NULL.
1426 * @abort: Abort the changes on fail, can be NULL.
1427 * @clean: Clean up resources after all transaction actions have called
1428 * commit() or abort(). Can be NULL.
1430 * Only prepare() may fail. In a single transaction, only one of commit() or
1431 * abort() will be called. clean() will always be called if it is present.
1433 typedef struct BlkActionOps {
1434 size_t instance_size;
1435 void (*prepare)(BlkActionState *common, Error **errp);
1436 void (*commit)(BlkActionState *common);
1437 void (*abort)(BlkActionState *common);
1438 void (*clean)(BlkActionState *common);
1439 } BlkActionOps;
1442 * BlkActionState:
1443 * Describes one Action's state within a Transaction.
1445 * @action: QAPI-defined enum identifying which Action to perform.
1446 * @ops: Table of ActionOps this Action can perform.
1447 * @block_job_txn: Transaction which this action belongs to.
1448 * @entry: List membership for all Actions in this Transaction.
1450 * This structure must be arranged as first member in a subclassed type,
1451 * assuming that the compiler will also arrange it to the same offsets as the
1452 * base class.
1454 struct BlkActionState {
1455 TransactionAction *action;
1456 const BlkActionOps *ops;
1457 BlockJobTxn *block_job_txn;
1458 TransactionProperties *txn_props;
1459 QSIMPLEQ_ENTRY(BlkActionState) entry;
1462 /* internal snapshot private data */
1463 typedef struct InternalSnapshotState {
1464 BlkActionState common;
1465 BlockDriverState *bs;
1466 AioContext *aio_context;
1467 QEMUSnapshotInfo sn;
1468 bool created;
1469 } InternalSnapshotState;
1472 static int action_check_completion_mode(BlkActionState *s, Error **errp)
1474 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1475 error_setg(errp,
1476 "Action '%s' does not support Transaction property "
1477 "completion-mode = %s",
1478 TransactionActionKind_lookup[s->action->type],
1479 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1480 return -1;
1482 return 0;
1485 static void internal_snapshot_prepare(BlkActionState *common,
1486 Error **errp)
1488 Error *local_err = NULL;
1489 const char *device;
1490 const char *name;
1491 BlockBackend *blk;
1492 BlockDriverState *bs;
1493 QEMUSnapshotInfo old_sn, *sn;
1494 bool ret;
1495 qemu_timeval tv;
1496 BlockdevSnapshotInternal *internal;
1497 InternalSnapshotState *state;
1498 int ret1;
1500 g_assert(common->action->type ==
1501 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1502 internal = common->action->u.blockdev_snapshot_internal_sync;
1503 state = DO_UPCAST(InternalSnapshotState, common, common);
1505 /* 1. parse input */
1506 device = internal->device;
1507 name = internal->name;
1509 /* 2. check for validation */
1510 if (action_check_completion_mode(common, errp) < 0) {
1511 return;
1514 blk = blk_by_name(device);
1515 if (!blk) {
1516 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1517 "Device '%s' not found", device);
1518 return;
1521 /* AioContext is released in .clean() */
1522 state->aio_context = blk_get_aio_context(blk);
1523 aio_context_acquire(state->aio_context);
1525 if (!blk_is_available(blk)) {
1526 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1527 return;
1529 bs = blk_bs(blk);
1531 state->bs = bs;
1532 bdrv_drained_begin(bs);
1534 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1535 return;
1538 if (bdrv_is_read_only(bs)) {
1539 error_setg(errp, "Device '%s' is read only", device);
1540 return;
1543 if (!bdrv_can_snapshot(bs)) {
1544 error_setg(errp, "Block format '%s' used by device '%s' "
1545 "does not support internal snapshots",
1546 bs->drv->format_name, device);
1547 return;
1550 if (!strlen(name)) {
1551 error_setg(errp, "Name is empty");
1552 return;
1555 /* check whether a snapshot with name exist */
1556 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1557 &local_err);
1558 if (local_err) {
1559 error_propagate(errp, local_err);
1560 return;
1561 } else if (ret) {
1562 error_setg(errp,
1563 "Snapshot with name '%s' already exists on device '%s'",
1564 name, device);
1565 return;
1568 /* 3. take the snapshot */
1569 sn = &state->sn;
1570 pstrcpy(sn->name, sizeof(sn->name), name);
1571 qemu_gettimeofday(&tv);
1572 sn->date_sec = tv.tv_sec;
1573 sn->date_nsec = tv.tv_usec * 1000;
1574 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1576 ret1 = bdrv_snapshot_create(bs, sn);
1577 if (ret1 < 0) {
1578 error_setg_errno(errp, -ret1,
1579 "Failed to create snapshot '%s' on device '%s'",
1580 name, device);
1581 return;
1584 /* 4. succeed, mark a snapshot is created */
1585 state->created = true;
1588 static void internal_snapshot_abort(BlkActionState *common)
1590 InternalSnapshotState *state =
1591 DO_UPCAST(InternalSnapshotState, common, common);
1592 BlockDriverState *bs = state->bs;
1593 QEMUSnapshotInfo *sn = &state->sn;
1594 Error *local_error = NULL;
1596 if (!state->created) {
1597 return;
1600 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1601 error_reportf_err(local_error,
1602 "Failed to delete snapshot with id '%s' and "
1603 "name '%s' on device '%s' in abort: ",
1604 sn->id_str, sn->name,
1605 bdrv_get_device_name(bs));
1609 static void internal_snapshot_clean(BlkActionState *common)
1611 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1612 common, common);
1614 if (state->aio_context) {
1615 if (state->bs) {
1616 bdrv_drained_end(state->bs);
1618 aio_context_release(state->aio_context);
1622 /* external snapshot private data */
1623 typedef struct ExternalSnapshotState {
1624 BlkActionState common;
1625 BlockDriverState *old_bs;
1626 BlockDriverState *new_bs;
1627 AioContext *aio_context;
1628 } ExternalSnapshotState;
1630 static void external_snapshot_prepare(BlkActionState *common,
1631 Error **errp)
1633 int flags = 0, ret;
1634 QDict *options = NULL;
1635 Error *local_err = NULL;
1636 /* Device and node name of the image to generate the snapshot from */
1637 const char *device;
1638 const char *node_name;
1639 /* Reference to the new image (for 'blockdev-snapshot') */
1640 const char *snapshot_ref;
1641 /* File name of the new image (for 'blockdev-snapshot-sync') */
1642 const char *new_image_file;
1643 ExternalSnapshotState *state =
1644 DO_UPCAST(ExternalSnapshotState, common, common);
1645 TransactionAction *action = common->action;
1647 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1648 * purpose but a different set of parameters */
1649 switch (action->type) {
1650 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1652 BlockdevSnapshot *s = action->u.blockdev_snapshot;
1653 device = s->node;
1654 node_name = s->node;
1655 new_image_file = NULL;
1656 snapshot_ref = s->overlay;
1658 break;
1659 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1661 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1662 device = s->has_device ? s->device : NULL;
1663 node_name = s->has_node_name ? s->node_name : NULL;
1664 new_image_file = s->snapshot_file;
1665 snapshot_ref = NULL;
1667 break;
1668 default:
1669 g_assert_not_reached();
1672 /* start processing */
1673 if (action_check_completion_mode(common, errp) < 0) {
1674 return;
1677 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1678 if (!state->old_bs) {
1679 return;
1682 /* Acquire AioContext now so any threads operating on old_bs stop */
1683 state->aio_context = bdrv_get_aio_context(state->old_bs);
1684 aio_context_acquire(state->aio_context);
1685 bdrv_drained_begin(state->old_bs);
1687 if (!bdrv_is_inserted(state->old_bs)) {
1688 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1689 return;
1692 if (bdrv_op_is_blocked(state->old_bs,
1693 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1694 return;
1697 if (!bdrv_is_read_only(state->old_bs)) {
1698 if (bdrv_flush(state->old_bs)) {
1699 error_setg(errp, QERR_IO_ERROR);
1700 return;
1704 if (!bdrv_is_first_non_filter(state->old_bs)) {
1705 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1706 return;
1709 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
1710 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1711 const char *format = s->has_format ? s->format : "qcow2";
1712 enum NewImageMode mode;
1713 const char *snapshot_node_name =
1714 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
1716 if (node_name && !snapshot_node_name) {
1717 error_setg(errp, "New snapshot node name missing");
1718 return;
1721 if (snapshot_node_name &&
1722 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1723 error_setg(errp, "New snapshot node name already in use");
1724 return;
1727 flags = state->old_bs->open_flags;
1729 /* create new image w/backing file */
1730 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1731 if (mode != NEW_IMAGE_MODE_EXISTING) {
1732 bdrv_img_create(new_image_file, format,
1733 state->old_bs->filename,
1734 state->old_bs->drv->format_name,
1735 NULL, -1, flags, &local_err, false);
1736 if (local_err) {
1737 error_propagate(errp, local_err);
1738 return;
1742 options = qdict_new();
1743 if (s->has_snapshot_node_name) {
1744 qdict_put(options, "node-name",
1745 qstring_from_str(snapshot_node_name));
1747 qdict_put(options, "driver", qstring_from_str(format));
1749 flags |= BDRV_O_NO_BACKING;
1752 assert(state->new_bs == NULL);
1753 ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options,
1754 flags, errp);
1755 /* We will manually add the backing_hd field to the bs later */
1756 if (ret != 0) {
1757 return;
1760 if (state->new_bs->blk != NULL) {
1761 error_setg(errp, "The snapshot is already in use by %s",
1762 blk_name(state->new_bs->blk));
1763 return;
1766 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1767 errp)) {
1768 return;
1771 if (state->new_bs->backing != NULL) {
1772 error_setg(errp, "The snapshot already has a backing image");
1773 return;
1776 if (!state->new_bs->drv->supports_backing) {
1777 error_setg(errp, "The snapshot does not support backing images");
1781 static void external_snapshot_commit(BlkActionState *common)
1783 ExternalSnapshotState *state =
1784 DO_UPCAST(ExternalSnapshotState, common, common);
1786 bdrv_set_aio_context(state->new_bs, state->aio_context);
1788 /* This removes our old bs and adds the new bs */
1789 bdrv_append(state->new_bs, state->old_bs);
1790 /* We don't need (or want) to use the transactional
1791 * bdrv_reopen_multiple() across all the entries at once, because we
1792 * don't want to abort all of them if one of them fails the reopen */
1793 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1794 NULL);
1797 static void external_snapshot_abort(BlkActionState *common)
1799 ExternalSnapshotState *state =
1800 DO_UPCAST(ExternalSnapshotState, common, common);
1801 if (state->new_bs) {
1802 bdrv_unref(state->new_bs);
1806 static void external_snapshot_clean(BlkActionState *common)
1808 ExternalSnapshotState *state =
1809 DO_UPCAST(ExternalSnapshotState, common, common);
1810 if (state->aio_context) {
1811 bdrv_drained_end(state->old_bs);
1812 aio_context_release(state->aio_context);
1816 typedef struct DriveBackupState {
1817 BlkActionState common;
1818 BlockDriverState *bs;
1819 AioContext *aio_context;
1820 BlockJob *job;
1821 } DriveBackupState;
1823 static void do_drive_backup(const char *device, const char *target,
1824 bool has_format, const char *format,
1825 enum MirrorSyncMode sync,
1826 bool has_mode, enum NewImageMode mode,
1827 bool has_speed, int64_t speed,
1828 bool has_bitmap, const char *bitmap,
1829 bool has_on_source_error,
1830 BlockdevOnError on_source_error,
1831 bool has_on_target_error,
1832 BlockdevOnError on_target_error,
1833 BlockJobTxn *txn, Error **errp);
1835 static void drive_backup_prepare(BlkActionState *common, Error **errp)
1837 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1838 BlockBackend *blk;
1839 DriveBackup *backup;
1840 Error *local_err = NULL;
1842 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1843 backup = common->action->u.drive_backup;
1845 blk = blk_by_name(backup->device);
1846 if (!blk) {
1847 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1848 "Device '%s' not found", backup->device);
1849 return;
1852 if (!blk_is_available(blk)) {
1853 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1854 return;
1857 /* AioContext is released in .clean() */
1858 state->aio_context = blk_get_aio_context(blk);
1859 aio_context_acquire(state->aio_context);
1860 bdrv_drained_begin(blk_bs(blk));
1861 state->bs = blk_bs(blk);
1863 do_drive_backup(backup->device, backup->target,
1864 backup->has_format, backup->format,
1865 backup->sync,
1866 backup->has_mode, backup->mode,
1867 backup->has_speed, backup->speed,
1868 backup->has_bitmap, backup->bitmap,
1869 backup->has_on_source_error, backup->on_source_error,
1870 backup->has_on_target_error, backup->on_target_error,
1871 common->block_job_txn, &local_err);
1872 if (local_err) {
1873 error_propagate(errp, local_err);
1874 return;
1877 state->job = state->bs->job;
1880 static void drive_backup_abort(BlkActionState *common)
1882 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1883 BlockDriverState *bs = state->bs;
1885 /* Only cancel if it's the job we started */
1886 if (bs && bs->job && bs->job == state->job) {
1887 block_job_cancel_sync(bs->job);
1891 static void drive_backup_clean(BlkActionState *common)
1893 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1895 if (state->aio_context) {
1896 bdrv_drained_end(state->bs);
1897 aio_context_release(state->aio_context);
1901 typedef struct BlockdevBackupState {
1902 BlkActionState common;
1903 BlockDriverState *bs;
1904 BlockJob *job;
1905 AioContext *aio_context;
1906 } BlockdevBackupState;
1908 static void do_blockdev_backup(const char *device, const char *target,
1909 enum MirrorSyncMode sync,
1910 bool has_speed, int64_t speed,
1911 bool has_on_source_error,
1912 BlockdevOnError on_source_error,
1913 bool has_on_target_error,
1914 BlockdevOnError on_target_error,
1915 BlockJobTxn *txn, Error **errp);
1917 static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1919 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1920 BlockdevBackup *backup;
1921 BlockBackend *blk, *target;
1922 Error *local_err = NULL;
1924 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1925 backup = common->action->u.blockdev_backup;
1927 blk = blk_by_name(backup->device);
1928 if (!blk) {
1929 error_setg(errp, "Device '%s' not found", backup->device);
1930 return;
1933 if (!blk_is_available(blk)) {
1934 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1935 return;
1938 target = blk_by_name(backup->target);
1939 if (!target) {
1940 error_setg(errp, "Device '%s' not found", backup->target);
1941 return;
1944 /* AioContext is released in .clean() */
1945 state->aio_context = blk_get_aio_context(blk);
1946 if (state->aio_context != blk_get_aio_context(target)) {
1947 state->aio_context = NULL;
1948 error_setg(errp, "Backup between two IO threads is not implemented");
1949 return;
1951 aio_context_acquire(state->aio_context);
1952 state->bs = blk_bs(blk);
1953 bdrv_drained_begin(state->bs);
1955 do_blockdev_backup(backup->device, backup->target,
1956 backup->sync,
1957 backup->has_speed, backup->speed,
1958 backup->has_on_source_error, backup->on_source_error,
1959 backup->has_on_target_error, backup->on_target_error,
1960 common->block_job_txn, &local_err);
1961 if (local_err) {
1962 error_propagate(errp, local_err);
1963 return;
1966 state->job = state->bs->job;
1969 static void blockdev_backup_abort(BlkActionState *common)
1971 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1972 BlockDriverState *bs = state->bs;
1974 /* Only cancel if it's the job we started */
1975 if (bs && bs->job && bs->job == state->job) {
1976 block_job_cancel_sync(bs->job);
1980 static void blockdev_backup_clean(BlkActionState *common)
1982 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1984 if (state->aio_context) {
1985 bdrv_drained_end(state->bs);
1986 aio_context_release(state->aio_context);
1990 typedef struct BlockDirtyBitmapState {
1991 BlkActionState common;
1992 BdrvDirtyBitmap *bitmap;
1993 BlockDriverState *bs;
1994 AioContext *aio_context;
1995 HBitmap *backup;
1996 bool prepared;
1997 } BlockDirtyBitmapState;
1999 static void block_dirty_bitmap_add_prepare(BlkActionState *common,
2000 Error **errp)
2002 Error *local_err = NULL;
2003 BlockDirtyBitmapAdd *action;
2004 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2005 common, common);
2007 if (action_check_completion_mode(common, errp) < 0) {
2008 return;
2011 action = common->action->u.block_dirty_bitmap_add;
2012 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2013 qmp_block_dirty_bitmap_add(action->node, action->name,
2014 action->has_granularity, action->granularity,
2015 &local_err);
2017 if (!local_err) {
2018 state->prepared = true;
2019 } else {
2020 error_propagate(errp, local_err);
2024 static void block_dirty_bitmap_add_abort(BlkActionState *common)
2026 BlockDirtyBitmapAdd *action;
2027 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2028 common, common);
2030 action = common->action->u.block_dirty_bitmap_add;
2031 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2032 * then the node reference and bitmap name must have been valid.
2034 if (state->prepared) {
2035 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2039 static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
2040 Error **errp)
2042 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2043 common, common);
2044 BlockDirtyBitmap *action;
2046 if (action_check_completion_mode(common, errp) < 0) {
2047 return;
2050 action = common->action->u.block_dirty_bitmap_clear;
2051 state->bitmap = block_dirty_bitmap_lookup(action->node,
2052 action->name,
2053 &state->bs,
2054 &state->aio_context,
2055 errp);
2056 if (!state->bitmap) {
2057 return;
2060 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2061 error_setg(errp, "Cannot modify a frozen bitmap");
2062 return;
2063 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2064 error_setg(errp, "Cannot clear a disabled bitmap");
2065 return;
2068 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2069 /* AioContext is released in .clean() */
2072 static void block_dirty_bitmap_clear_abort(BlkActionState *common)
2074 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2075 common, common);
2077 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2080 static void block_dirty_bitmap_clear_commit(BlkActionState *common)
2082 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2083 common, common);
2085 hbitmap_free(state->backup);
2088 static void block_dirty_bitmap_clear_clean(BlkActionState *common)
2090 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2091 common, common);
2093 if (state->aio_context) {
2094 aio_context_release(state->aio_context);
2098 static void abort_prepare(BlkActionState *common, Error **errp)
2100 error_setg(errp, "Transaction aborted using Abort action");
2103 static void abort_commit(BlkActionState *common)
2105 g_assert_not_reached(); /* this action never succeeds */
2108 static const BlkActionOps actions[] = {
2109 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2110 .instance_size = sizeof(ExternalSnapshotState),
2111 .prepare = external_snapshot_prepare,
2112 .commit = external_snapshot_commit,
2113 .abort = external_snapshot_abort,
2114 .clean = external_snapshot_clean,
2116 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2117 .instance_size = sizeof(ExternalSnapshotState),
2118 .prepare = external_snapshot_prepare,
2119 .commit = external_snapshot_commit,
2120 .abort = external_snapshot_abort,
2121 .clean = external_snapshot_clean,
2123 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2124 .instance_size = sizeof(DriveBackupState),
2125 .prepare = drive_backup_prepare,
2126 .abort = drive_backup_abort,
2127 .clean = drive_backup_clean,
2129 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2130 .instance_size = sizeof(BlockdevBackupState),
2131 .prepare = blockdev_backup_prepare,
2132 .abort = blockdev_backup_abort,
2133 .clean = blockdev_backup_clean,
2135 [TRANSACTION_ACTION_KIND_ABORT] = {
2136 .instance_size = sizeof(BlkActionState),
2137 .prepare = abort_prepare,
2138 .commit = abort_commit,
2140 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2141 .instance_size = sizeof(InternalSnapshotState),
2142 .prepare = internal_snapshot_prepare,
2143 .abort = internal_snapshot_abort,
2144 .clean = internal_snapshot_clean,
2146 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2147 .instance_size = sizeof(BlockDirtyBitmapState),
2148 .prepare = block_dirty_bitmap_add_prepare,
2149 .abort = block_dirty_bitmap_add_abort,
2151 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2152 .instance_size = sizeof(BlockDirtyBitmapState),
2153 .prepare = block_dirty_bitmap_clear_prepare,
2154 .commit = block_dirty_bitmap_clear_commit,
2155 .abort = block_dirty_bitmap_clear_abort,
2156 .clean = block_dirty_bitmap_clear_clean,
2161 * Allocate a TransactionProperties structure if necessary, and fill
2162 * that structure with desired defaults if they are unset.
2164 static TransactionProperties *get_transaction_properties(
2165 TransactionProperties *props)
2167 if (!props) {
2168 props = g_new0(TransactionProperties, 1);
2171 if (!props->has_completion_mode) {
2172 props->has_completion_mode = true;
2173 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2176 return props;
2180 * 'Atomic' group operations. The operations are performed as a set, and if
2181 * any fail then we roll back all operations in the group.
2183 void qmp_transaction(TransactionActionList *dev_list,
2184 bool has_props,
2185 struct TransactionProperties *props,
2186 Error **errp)
2188 TransactionActionList *dev_entry = dev_list;
2189 BlockJobTxn *block_job_txn = NULL;
2190 BlkActionState *state, *next;
2191 Error *local_err = NULL;
2193 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
2194 QSIMPLEQ_INIT(&snap_bdrv_states);
2196 /* Does this transaction get canceled as a group on failure?
2197 * If not, we don't really need to make a BlockJobTxn.
2199 props = get_transaction_properties(props);
2200 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2201 block_job_txn = block_job_txn_new();
2204 /* drain all i/o before any operations */
2205 bdrv_drain_all();
2207 /* We don't do anything in this loop that commits us to the operations */
2208 while (NULL != dev_entry) {
2209 TransactionAction *dev_info = NULL;
2210 const BlkActionOps *ops;
2212 dev_info = dev_entry->value;
2213 dev_entry = dev_entry->next;
2215 assert(dev_info->type < ARRAY_SIZE(actions));
2217 ops = &actions[dev_info->type];
2218 assert(ops->instance_size > 0);
2220 state = g_malloc0(ops->instance_size);
2221 state->ops = ops;
2222 state->action = dev_info;
2223 state->block_job_txn = block_job_txn;
2224 state->txn_props = props;
2225 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
2227 state->ops->prepare(state, &local_err);
2228 if (local_err) {
2229 error_propagate(errp, local_err);
2230 goto delete_and_fail;
2234 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2235 if (state->ops->commit) {
2236 state->ops->commit(state);
2240 /* success */
2241 goto exit;
2243 delete_and_fail:
2244 /* failure, and it is all-or-none; roll back all operations */
2245 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2246 if (state->ops->abort) {
2247 state->ops->abort(state);
2250 exit:
2251 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2252 if (state->ops->clean) {
2253 state->ops->clean(state);
2255 g_free(state);
2257 if (!has_props) {
2258 qapi_free_TransactionProperties(props);
2260 block_job_txn_unref(block_job_txn);
2263 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
2265 Error *local_err = NULL;
2267 qmp_blockdev_open_tray(device, has_force, force, &local_err);
2268 if (local_err) {
2269 error_propagate(errp, local_err);
2270 return;
2273 qmp_x_blockdev_remove_medium(device, errp);
2276 void qmp_block_passwd(bool has_device, const char *device,
2277 bool has_node_name, const char *node_name,
2278 const char *password, Error **errp)
2280 Error *local_err = NULL;
2281 BlockDriverState *bs;
2282 AioContext *aio_context;
2284 bs = bdrv_lookup_bs(has_device ? device : NULL,
2285 has_node_name ? node_name : NULL,
2286 &local_err);
2287 if (local_err) {
2288 error_propagate(errp, local_err);
2289 return;
2292 aio_context = bdrv_get_aio_context(bs);
2293 aio_context_acquire(aio_context);
2295 bdrv_add_key(bs, password, errp);
2297 aio_context_release(aio_context);
2300 void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
2301 Error **errp)
2303 BlockBackend *blk;
2304 bool locked;
2306 if (!has_force) {
2307 force = false;
2310 blk = blk_by_name(device);
2311 if (!blk) {
2312 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2313 "Device '%s' not found", device);
2314 return;
2317 if (!blk_dev_has_removable_media(blk)) {
2318 error_setg(errp, "Device '%s' is not removable", device);
2319 return;
2322 if (!blk_dev_has_tray(blk)) {
2323 /* Ignore this command on tray-less devices */
2324 return;
2327 if (blk_dev_is_tray_open(blk)) {
2328 return;
2331 locked = blk_dev_is_medium_locked(blk);
2332 if (locked) {
2333 blk_dev_eject_request(blk, force);
2336 if (!locked || force) {
2337 blk_dev_change_media_cb(blk, false);
2341 void qmp_blockdev_close_tray(const char *device, Error **errp)
2343 BlockBackend *blk;
2345 blk = blk_by_name(device);
2346 if (!blk) {
2347 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2348 "Device '%s' not found", device);
2349 return;
2352 if (!blk_dev_has_removable_media(blk)) {
2353 error_setg(errp, "Device '%s' is not removable", device);
2354 return;
2357 if (!blk_dev_has_tray(blk)) {
2358 /* Ignore this command on tray-less devices */
2359 return;
2362 if (!blk_dev_is_tray_open(blk)) {
2363 return;
2366 blk_dev_change_media_cb(blk, true);
2369 void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
2371 BlockBackend *blk;
2372 BlockDriverState *bs;
2373 AioContext *aio_context;
2374 bool has_device;
2376 blk = blk_by_name(device);
2377 if (!blk) {
2378 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2379 "Device '%s' not found", device);
2380 return;
2383 /* For BBs without a device, we can exchange the BDS tree at will */
2384 has_device = blk_get_attached_dev(blk);
2386 if (has_device && !blk_dev_has_removable_media(blk)) {
2387 error_setg(errp, "Device '%s' is not removable", device);
2388 return;
2391 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2392 error_setg(errp, "Tray of device '%s' is not open", device);
2393 return;
2396 bs = blk_bs(blk);
2397 if (!bs) {
2398 return;
2401 aio_context = bdrv_get_aio_context(bs);
2402 aio_context_acquire(aio_context);
2404 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2405 goto out;
2408 /* This follows the convention established by bdrv_make_anon() */
2409 if (bs->device_list.tqe_prev) {
2410 bdrv_device_remove(bs);
2413 blk_remove_bs(blk);
2415 if (!blk_dev_has_tray(blk)) {
2416 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2417 * called at all); therefore, the medium needs to be ejected here.
2418 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2419 * value passed here (i.e. false). */
2420 blk_dev_change_media_cb(blk, false);
2423 out:
2424 aio_context_release(aio_context);
2427 static void qmp_blockdev_insert_anon_medium(const char *device,
2428 BlockDriverState *bs, Error **errp)
2430 BlockBackend *blk;
2431 bool has_device;
2433 blk = blk_by_name(device);
2434 if (!blk) {
2435 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2436 "Device '%s' not found", device);
2437 return;
2440 /* For BBs without a device, we can exchange the BDS tree at will */
2441 has_device = blk_get_attached_dev(blk);
2443 if (has_device && !blk_dev_has_removable_media(blk)) {
2444 error_setg(errp, "Device '%s' is not removable", device);
2445 return;
2448 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2449 error_setg(errp, "Tray of device '%s' is not open", device);
2450 return;
2453 if (blk_bs(blk)) {
2454 error_setg(errp, "There already is a medium in device '%s'", device);
2455 return;
2458 blk_insert_bs(blk, bs);
2460 QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
2462 if (!blk_dev_has_tray(blk)) {
2463 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2464 * called at all); therefore, the medium needs to be pushed into the
2465 * slot here.
2466 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2467 * value passed here (i.e. true). */
2468 blk_dev_change_media_cb(blk, true);
2472 void qmp_x_blockdev_insert_medium(const char *device, const char *node_name,
2473 Error **errp)
2475 BlockDriverState *bs;
2477 bs = bdrv_find_node(node_name);
2478 if (!bs) {
2479 error_setg(errp, "Node '%s' not found", node_name);
2480 return;
2483 if (bs->blk) {
2484 error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
2485 blk_name(bs->blk));
2486 return;
2489 qmp_blockdev_insert_anon_medium(device, bs, errp);
2492 void qmp_blockdev_change_medium(const char *device, const char *filename,
2493 bool has_format, const char *format,
2494 bool has_read_only,
2495 BlockdevChangeReadOnlyMode read_only,
2496 Error **errp)
2498 BlockBackend *blk;
2499 BlockDriverState *medium_bs = NULL;
2500 int bdrv_flags, ret;
2501 QDict *options = NULL;
2502 Error *err = NULL;
2504 blk = blk_by_name(device);
2505 if (!blk) {
2506 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2507 "Device '%s' not found", device);
2508 goto fail;
2511 if (blk_bs(blk)) {
2512 blk_update_root_state(blk);
2515 bdrv_flags = blk_get_open_flags_from_root_state(blk);
2517 if (!has_read_only) {
2518 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2521 switch (read_only) {
2522 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2523 break;
2525 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2526 bdrv_flags &= ~BDRV_O_RDWR;
2527 break;
2529 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2530 bdrv_flags |= BDRV_O_RDWR;
2531 break;
2533 default:
2534 abort();
2537 if (has_format) {
2538 options = qdict_new();
2539 qdict_put(options, "driver", qstring_from_str(format));
2542 assert(!medium_bs);
2543 ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2544 if (ret < 0) {
2545 goto fail;
2548 blk_apply_root_state(blk, medium_bs);
2550 bdrv_add_key(medium_bs, NULL, &err);
2551 if (err) {
2552 error_propagate(errp, err);
2553 goto fail;
2556 qmp_blockdev_open_tray(device, false, false, &err);
2557 if (err) {
2558 error_propagate(errp, err);
2559 goto fail;
2562 qmp_x_blockdev_remove_medium(device, &err);
2563 if (err) {
2564 error_propagate(errp, err);
2565 goto fail;
2568 qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2569 if (err) {
2570 error_propagate(errp, err);
2571 goto fail;
2574 qmp_blockdev_close_tray(device, errp);
2576 fail:
2577 /* If the medium has been inserted, the device has its own reference, so
2578 * ours must be relinquished; and if it has not been inserted successfully,
2579 * the reference must be relinquished anyway */
2580 bdrv_unref(medium_bs);
2583 /* throttling disk I/O limits */
2584 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
2585 int64_t bps_wr,
2586 int64_t iops,
2587 int64_t iops_rd,
2588 int64_t iops_wr,
2589 bool has_bps_max,
2590 int64_t bps_max,
2591 bool has_bps_rd_max,
2592 int64_t bps_rd_max,
2593 bool has_bps_wr_max,
2594 int64_t bps_wr_max,
2595 bool has_iops_max,
2596 int64_t iops_max,
2597 bool has_iops_rd_max,
2598 int64_t iops_rd_max,
2599 bool has_iops_wr_max,
2600 int64_t iops_wr_max,
2601 bool has_bps_max_length,
2602 int64_t bps_max_length,
2603 bool has_bps_rd_max_length,
2604 int64_t bps_rd_max_length,
2605 bool has_bps_wr_max_length,
2606 int64_t bps_wr_max_length,
2607 bool has_iops_max_length,
2608 int64_t iops_max_length,
2609 bool has_iops_rd_max_length,
2610 int64_t iops_rd_max_length,
2611 bool has_iops_wr_max_length,
2612 int64_t iops_wr_max_length,
2613 bool has_iops_size,
2614 int64_t iops_size,
2615 bool has_group,
2616 const char *group, Error **errp)
2618 ThrottleConfig cfg;
2619 BlockDriverState *bs;
2620 BlockBackend *blk;
2621 AioContext *aio_context;
2623 blk = blk_by_name(device);
2624 if (!blk) {
2625 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2626 "Device '%s' not found", device);
2627 return;
2630 aio_context = blk_get_aio_context(blk);
2631 aio_context_acquire(aio_context);
2633 bs = blk_bs(blk);
2634 if (!bs) {
2635 error_setg(errp, "Device '%s' has no medium", device);
2636 goto out;
2639 throttle_config_init(&cfg);
2640 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2641 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
2642 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
2644 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2645 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
2646 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2648 if (has_bps_max) {
2649 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
2651 if (has_bps_rd_max) {
2652 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
2654 if (has_bps_wr_max) {
2655 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2657 if (has_iops_max) {
2658 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2660 if (has_iops_rd_max) {
2661 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2663 if (has_iops_wr_max) {
2664 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2667 if (has_bps_max_length) {
2668 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = bps_max_length;
2670 if (has_bps_rd_max_length) {
2671 cfg.buckets[THROTTLE_BPS_READ].burst_length = bps_rd_max_length;
2673 if (has_bps_wr_max_length) {
2674 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = bps_wr_max_length;
2676 if (has_iops_max_length) {
2677 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = iops_max_length;
2679 if (has_iops_rd_max_length) {
2680 cfg.buckets[THROTTLE_OPS_READ].burst_length = iops_rd_max_length;
2682 if (has_iops_wr_max_length) {
2683 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = iops_wr_max_length;
2686 if (has_iops_size) {
2687 cfg.op_size = iops_size;
2690 if (!throttle_is_valid(&cfg, errp)) {
2691 goto out;
2694 if (throttle_enabled(&cfg)) {
2695 /* Enable I/O limits if they're not enabled yet, otherwise
2696 * just update the throttling group. */
2697 if (!bs->throttle_state) {
2698 bdrv_io_limits_enable(bs, has_group ? group : device);
2699 } else if (has_group) {
2700 bdrv_io_limits_update_group(bs, group);
2702 /* Set the new throttling configuration */
2703 bdrv_set_io_limits(bs, &cfg);
2704 } else if (bs->throttle_state) {
2705 /* If all throttling settings are set to 0, disable I/O limits */
2706 bdrv_io_limits_disable(bs);
2709 out:
2710 aio_context_release(aio_context);
2713 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2714 bool has_granularity, uint32_t granularity,
2715 Error **errp)
2717 AioContext *aio_context;
2718 BlockDriverState *bs;
2720 if (!name || name[0] == '\0') {
2721 error_setg(errp, "Bitmap name cannot be empty");
2722 return;
2725 bs = bdrv_lookup_bs(node, node, errp);
2726 if (!bs) {
2727 return;
2730 aio_context = bdrv_get_aio_context(bs);
2731 aio_context_acquire(aio_context);
2733 if (has_granularity) {
2734 if (granularity < 512 || !is_power_of_2(granularity)) {
2735 error_setg(errp, "Granularity must be power of 2 "
2736 "and at least 512");
2737 goto out;
2739 } else {
2740 /* Default to cluster size, if available: */
2741 granularity = bdrv_get_default_bitmap_granularity(bs);
2744 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2746 out:
2747 aio_context_release(aio_context);
2750 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2751 Error **errp)
2753 AioContext *aio_context;
2754 BlockDriverState *bs;
2755 BdrvDirtyBitmap *bitmap;
2757 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2758 if (!bitmap || !bs) {
2759 return;
2762 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2763 error_setg(errp,
2764 "Bitmap '%s' is currently frozen and cannot be removed",
2765 name);
2766 goto out;
2768 bdrv_dirty_bitmap_make_anon(bitmap);
2769 bdrv_release_dirty_bitmap(bs, bitmap);
2771 out:
2772 aio_context_release(aio_context);
2776 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2777 * immediately after a full backup operation.
2779 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2780 Error **errp)
2782 AioContext *aio_context;
2783 BdrvDirtyBitmap *bitmap;
2784 BlockDriverState *bs;
2786 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2787 if (!bitmap || !bs) {
2788 return;
2791 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2792 error_setg(errp,
2793 "Bitmap '%s' is currently frozen and cannot be modified",
2794 name);
2795 goto out;
2796 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2797 error_setg(errp,
2798 "Bitmap '%s' is currently disabled and cannot be cleared",
2799 name);
2800 goto out;
2803 bdrv_clear_dirty_bitmap(bitmap, NULL);
2805 out:
2806 aio_context_release(aio_context);
2809 void hmp_drive_del(Monitor *mon, const QDict *qdict)
2811 const char *id = qdict_get_str(qdict, "id");
2812 BlockBackend *blk;
2813 BlockDriverState *bs;
2814 AioContext *aio_context;
2815 Error *local_err = NULL;
2817 blk = blk_by_name(id);
2818 if (!blk) {
2819 error_report("Device '%s' not found", id);
2820 return;
2823 if (!blk_legacy_dinfo(blk)) {
2824 error_report("Deleting device added with blockdev-add"
2825 " is not supported");
2826 return;
2829 aio_context = blk_get_aio_context(blk);
2830 aio_context_acquire(aio_context);
2832 bs = blk_bs(blk);
2833 if (bs) {
2834 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2835 error_report_err(local_err);
2836 aio_context_release(aio_context);
2837 return;
2840 blk_remove_bs(blk);
2843 /* if we have a device attached to this BlockDriverState
2844 * then we need to make the drive anonymous until the device
2845 * can be removed. If this is a drive with no device backing
2846 * then we can just get rid of the block driver state right here.
2848 if (blk_get_attached_dev(blk)) {
2849 blk_hide_on_behalf_of_hmp_drive_del(blk);
2850 /* Further I/O must not pause the guest */
2851 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2852 BLOCKDEV_ON_ERROR_REPORT);
2853 } else {
2854 blk_unref(blk);
2857 aio_context_release(aio_context);
2860 void qmp_block_resize(bool has_device, const char *device,
2861 bool has_node_name, const char *node_name,
2862 int64_t size, Error **errp)
2864 Error *local_err = NULL;
2865 BlockDriverState *bs;
2866 AioContext *aio_context;
2867 int ret;
2869 bs = bdrv_lookup_bs(has_device ? device : NULL,
2870 has_node_name ? node_name : NULL,
2871 &local_err);
2872 if (local_err) {
2873 error_propagate(errp, local_err);
2874 return;
2877 aio_context = bdrv_get_aio_context(bs);
2878 aio_context_acquire(aio_context);
2880 if (!bdrv_is_first_non_filter(bs)) {
2881 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2882 goto out;
2885 if (size < 0) {
2886 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2887 goto out;
2890 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2891 error_setg(errp, QERR_DEVICE_IN_USE, device);
2892 goto out;
2895 /* complete all in-flight operations before resizing the device */
2896 bdrv_drain_all();
2898 ret = bdrv_truncate(bs, size);
2899 switch (ret) {
2900 case 0:
2901 break;
2902 case -ENOMEDIUM:
2903 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2904 break;
2905 case -ENOTSUP:
2906 error_setg(errp, QERR_UNSUPPORTED);
2907 break;
2908 case -EACCES:
2909 error_setg(errp, "Device '%s' is read only", device);
2910 break;
2911 case -EBUSY:
2912 error_setg(errp, QERR_DEVICE_IN_USE, device);
2913 break;
2914 default:
2915 error_setg_errno(errp, -ret, "Could not resize");
2916 break;
2919 out:
2920 aio_context_release(aio_context);
2923 static void block_job_cb(void *opaque, int ret)
2925 /* Note that this function may be executed from another AioContext besides
2926 * the QEMU main loop. If you need to access anything that assumes the
2927 * QEMU global mutex, use a BH or introduce a mutex.
2930 BlockDriverState *bs = opaque;
2931 const char *msg = NULL;
2933 trace_block_job_cb(bs, bs->job, ret);
2935 assert(bs->job);
2937 if (ret < 0) {
2938 msg = strerror(-ret);
2941 if (block_job_is_cancelled(bs->job)) {
2942 block_job_event_cancelled(bs->job);
2943 } else {
2944 block_job_event_completed(bs->job, msg);
2948 void qmp_block_stream(const char *device,
2949 bool has_base, const char *base,
2950 bool has_backing_file, const char *backing_file,
2951 bool has_speed, int64_t speed,
2952 bool has_on_error, BlockdevOnError on_error,
2953 Error **errp)
2955 BlockBackend *blk;
2956 BlockDriverState *bs;
2957 BlockDriverState *base_bs = NULL;
2958 AioContext *aio_context;
2959 Error *local_err = NULL;
2960 const char *base_name = NULL;
2962 if (!has_on_error) {
2963 on_error = BLOCKDEV_ON_ERROR_REPORT;
2966 blk = blk_by_name(device);
2967 if (!blk) {
2968 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2969 "Device '%s' not found", device);
2970 return;
2973 aio_context = blk_get_aio_context(blk);
2974 aio_context_acquire(aio_context);
2976 if (!blk_is_available(blk)) {
2977 error_setg(errp, "Device '%s' has no medium", device);
2978 goto out;
2980 bs = blk_bs(blk);
2982 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2983 goto out;
2986 if (has_base) {
2987 base_bs = bdrv_find_backing_image(bs, base);
2988 if (base_bs == NULL) {
2989 error_setg(errp, QERR_BASE_NOT_FOUND, base);
2990 goto out;
2992 assert(bdrv_get_aio_context(base_bs) == aio_context);
2993 base_name = base;
2996 /* if we are streaming the entire chain, the result will have no backing
2997 * file, and specifying one is therefore an error */
2998 if (base_bs == NULL && has_backing_file) {
2999 error_setg(errp, "backing file specified, but streaming the "
3000 "entire chain");
3001 goto out;
3004 /* backing_file string overrides base bs filename */
3005 base_name = has_backing_file ? backing_file : base_name;
3007 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
3008 on_error, block_job_cb, bs, &local_err);
3009 if (local_err) {
3010 error_propagate(errp, local_err);
3011 goto out;
3014 trace_qmp_block_stream(bs, bs->job);
3016 out:
3017 aio_context_release(aio_context);
3020 void qmp_block_commit(const char *device,
3021 bool has_base, const char *base,
3022 bool has_top, const char *top,
3023 bool has_backing_file, const char *backing_file,
3024 bool has_speed, int64_t speed,
3025 Error **errp)
3027 BlockBackend *blk;
3028 BlockDriverState *bs;
3029 BlockDriverState *base_bs, *top_bs;
3030 AioContext *aio_context;
3031 Error *local_err = NULL;
3032 /* This will be part of the QMP command, if/when the
3033 * BlockdevOnError change for blkmirror makes it in
3035 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
3037 if (!has_speed) {
3038 speed = 0;
3041 /* Important Note:
3042 * libvirt relies on the DeviceNotFound error class in order to probe for
3043 * live commit feature versions; for this to work, we must make sure to
3044 * perform the device lookup before any generic errors that may occur in a
3045 * scenario in which all optional arguments are omitted. */
3046 blk = blk_by_name(device);
3047 if (!blk) {
3048 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3049 "Device '%s' not found", device);
3050 return;
3053 aio_context = blk_get_aio_context(blk);
3054 aio_context_acquire(aio_context);
3056 if (!blk_is_available(blk)) {
3057 error_setg(errp, "Device '%s' has no medium", device);
3058 goto out;
3060 bs = blk_bs(blk);
3062 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
3063 goto out;
3066 /* default top_bs is the active layer */
3067 top_bs = bs;
3069 if (has_top && top) {
3070 if (strcmp(bs->filename, top) != 0) {
3071 top_bs = bdrv_find_backing_image(bs, top);
3075 if (top_bs == NULL) {
3076 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
3077 goto out;
3080 assert(bdrv_get_aio_context(top_bs) == aio_context);
3082 if (has_base && base) {
3083 base_bs = bdrv_find_backing_image(top_bs, base);
3084 } else {
3085 base_bs = bdrv_find_base(top_bs);
3088 if (base_bs == NULL) {
3089 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
3090 goto out;
3093 assert(bdrv_get_aio_context(base_bs) == aio_context);
3095 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3096 goto out;
3099 /* Do not allow attempts to commit an image into itself */
3100 if (top_bs == base_bs) {
3101 error_setg(errp, "cannot commit an image into itself");
3102 goto out;
3105 if (top_bs == bs) {
3106 if (has_backing_file) {
3107 error_setg(errp, "'backing-file' specified,"
3108 " but 'top' is the active layer");
3109 goto out;
3111 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
3112 bs, &local_err);
3113 } else {
3114 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
3115 has_backing_file ? backing_file : NULL, &local_err);
3117 if (local_err != NULL) {
3118 error_propagate(errp, local_err);
3119 goto out;
3122 out:
3123 aio_context_release(aio_context);
3126 static void do_drive_backup(const char *device, const char *target,
3127 bool has_format, const char *format,
3128 enum MirrorSyncMode sync,
3129 bool has_mode, enum NewImageMode mode,
3130 bool has_speed, int64_t speed,
3131 bool has_bitmap, const char *bitmap,
3132 bool has_on_source_error,
3133 BlockdevOnError on_source_error,
3134 bool has_on_target_error,
3135 BlockdevOnError on_target_error,
3136 BlockJobTxn *txn, Error **errp)
3138 BlockBackend *blk;
3139 BlockDriverState *bs;
3140 BlockDriverState *target_bs;
3141 BlockDriverState *source = NULL;
3142 BdrvDirtyBitmap *bmap = NULL;
3143 AioContext *aio_context;
3144 QDict *options = NULL;
3145 Error *local_err = NULL;
3146 int flags;
3147 int64_t size;
3148 int ret;
3150 if (!has_speed) {
3151 speed = 0;
3153 if (!has_on_source_error) {
3154 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3156 if (!has_on_target_error) {
3157 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3159 if (!has_mode) {
3160 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3163 blk = blk_by_name(device);
3164 if (!blk) {
3165 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3166 "Device '%s' not found", device);
3167 return;
3170 aio_context = blk_get_aio_context(blk);
3171 aio_context_acquire(aio_context);
3173 /* Although backup_run has this check too, we need to use bs->drv below, so
3174 * do an early check redundantly. */
3175 if (!blk_is_available(blk)) {
3176 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3177 goto out;
3179 bs = blk_bs(blk);
3181 if (!has_format) {
3182 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3185 /* Early check to avoid creating target */
3186 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
3187 goto out;
3190 flags = bs->open_flags | BDRV_O_RDWR;
3192 /* See if we have a backing HD we can use to create our new image
3193 * on top of. */
3194 if (sync == MIRROR_SYNC_MODE_TOP) {
3195 source = backing_bs(bs);
3196 if (!source) {
3197 sync = MIRROR_SYNC_MODE_FULL;
3200 if (sync == MIRROR_SYNC_MODE_NONE) {
3201 source = bs;
3204 size = bdrv_getlength(bs);
3205 if (size < 0) {
3206 error_setg_errno(errp, -size, "bdrv_getlength failed");
3207 goto out;
3210 if (mode != NEW_IMAGE_MODE_EXISTING) {
3211 assert(format);
3212 if (source) {
3213 bdrv_img_create(target, format, source->filename,
3214 source->drv->format_name, NULL,
3215 size, flags, &local_err, false);
3216 } else {
3217 bdrv_img_create(target, format, NULL, NULL, NULL,
3218 size, flags, &local_err, false);
3222 if (local_err) {
3223 error_propagate(errp, local_err);
3224 goto out;
3227 if (format) {
3228 options = qdict_new();
3229 qdict_put(options, "driver", qstring_from_str(format));
3232 target_bs = NULL;
3233 ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
3234 if (ret < 0) {
3235 error_propagate(errp, local_err);
3236 goto out;
3239 bdrv_set_aio_context(target_bs, aio_context);
3241 if (has_bitmap) {
3242 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3243 if (!bmap) {
3244 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
3245 bdrv_unref(target_bs);
3246 goto out;
3250 backup_start(bs, target_bs, speed, sync, bmap,
3251 on_source_error, on_target_error,
3252 block_job_cb, bs, txn, &local_err);
3253 if (local_err != NULL) {
3254 bdrv_unref(target_bs);
3255 error_propagate(errp, local_err);
3256 goto out;
3259 out:
3260 aio_context_release(aio_context);
3263 void qmp_drive_backup(const char *device, const char *target,
3264 bool has_format, const char *format,
3265 enum MirrorSyncMode sync,
3266 bool has_mode, enum NewImageMode mode,
3267 bool has_speed, int64_t speed,
3268 bool has_bitmap, const char *bitmap,
3269 bool has_on_source_error, BlockdevOnError on_source_error,
3270 bool has_on_target_error, BlockdevOnError on_target_error,
3271 Error **errp)
3273 return do_drive_backup(device, target, has_format, format, sync,
3274 has_mode, mode, has_speed, speed,
3275 has_bitmap, bitmap,
3276 has_on_source_error, on_source_error,
3277 has_on_target_error, on_target_error,
3278 NULL, errp);
3281 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3283 return bdrv_named_nodes_list(errp);
3286 void do_blockdev_backup(const char *device, const char *target,
3287 enum MirrorSyncMode sync,
3288 bool has_speed, int64_t speed,
3289 bool has_on_source_error,
3290 BlockdevOnError on_source_error,
3291 bool has_on_target_error,
3292 BlockdevOnError on_target_error,
3293 BlockJobTxn *txn, Error **errp)
3295 BlockBackend *blk, *target_blk;
3296 BlockDriverState *bs;
3297 BlockDriverState *target_bs;
3298 Error *local_err = NULL;
3299 AioContext *aio_context;
3301 if (!has_speed) {
3302 speed = 0;
3304 if (!has_on_source_error) {
3305 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3307 if (!has_on_target_error) {
3308 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3311 blk = blk_by_name(device);
3312 if (!blk) {
3313 error_setg(errp, "Device '%s' not found", device);
3314 return;
3317 aio_context = blk_get_aio_context(blk);
3318 aio_context_acquire(aio_context);
3320 if (!blk_is_available(blk)) {
3321 error_setg(errp, "Device '%s' has no medium", device);
3322 goto out;
3324 bs = blk_bs(blk);
3326 target_blk = blk_by_name(target);
3327 if (!target_blk) {
3328 error_setg(errp, "Device '%s' not found", target);
3329 goto out;
3332 if (!blk_is_available(target_blk)) {
3333 error_setg(errp, "Device '%s' has no medium", target);
3334 goto out;
3336 target_bs = blk_bs(target_blk);
3338 bdrv_ref(target_bs);
3339 bdrv_set_aio_context(target_bs, aio_context);
3340 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
3341 on_target_error, block_job_cb, bs, txn, &local_err);
3342 if (local_err != NULL) {
3343 bdrv_unref(target_bs);
3344 error_propagate(errp, local_err);
3346 out:
3347 aio_context_release(aio_context);
3350 void qmp_blockdev_backup(const char *device, const char *target,
3351 enum MirrorSyncMode sync,
3352 bool has_speed, int64_t speed,
3353 bool has_on_source_error,
3354 BlockdevOnError on_source_error,
3355 bool has_on_target_error,
3356 BlockdevOnError on_target_error,
3357 Error **errp)
3359 do_blockdev_backup(device, target, sync, has_speed, speed,
3360 has_on_source_error, on_source_error,
3361 has_on_target_error, on_target_error,
3362 NULL, errp);
3365 /* Parameter check and block job starting for drive mirroring.
3366 * Caller should hold @device and @target's aio context (must be the same).
3368 static void blockdev_mirror_common(BlockDriverState *bs,
3369 BlockDriverState *target,
3370 bool has_replaces, const char *replaces,
3371 enum MirrorSyncMode sync,
3372 bool has_speed, int64_t speed,
3373 bool has_granularity, uint32_t granularity,
3374 bool has_buf_size, int64_t buf_size,
3375 bool has_on_source_error,
3376 BlockdevOnError on_source_error,
3377 bool has_on_target_error,
3378 BlockdevOnError on_target_error,
3379 bool has_unmap, bool unmap,
3380 Error **errp)
3383 if (!has_speed) {
3384 speed = 0;
3386 if (!has_on_source_error) {
3387 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3389 if (!has_on_target_error) {
3390 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3392 if (!has_granularity) {
3393 granularity = 0;
3395 if (!has_buf_size) {
3396 buf_size = 0;
3398 if (!has_unmap) {
3399 unmap = true;
3402 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3403 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3404 "a value in range [512B, 64MB]");
3405 return;
3407 if (granularity & (granularity - 1)) {
3408 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3409 "power of 2");
3410 return;
3413 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3414 return;
3416 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3417 return;
3419 if (target->blk) {
3420 error_setg(errp, "Cannot mirror to an attached block device");
3421 return;
3424 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3425 sync = MIRROR_SYNC_MODE_FULL;
3428 /* pass the node name to replace to mirror start since it's loose coupling
3429 * and will allow to check whether the node still exist at mirror completion
3431 mirror_start(bs, target,
3432 has_replaces ? replaces : NULL,
3433 speed, granularity, buf_size, sync,
3434 on_source_error, on_target_error, unmap,
3435 block_job_cb, bs, errp);
3438 void qmp_drive_mirror(const char *device, const char *target,
3439 bool has_format, const char *format,
3440 bool has_node_name, const char *node_name,
3441 bool has_replaces, const char *replaces,
3442 enum MirrorSyncMode sync,
3443 bool has_mode, enum NewImageMode mode,
3444 bool has_speed, int64_t speed,
3445 bool has_granularity, uint32_t granularity,
3446 bool has_buf_size, int64_t buf_size,
3447 bool has_on_source_error, BlockdevOnError on_source_error,
3448 bool has_on_target_error, BlockdevOnError on_target_error,
3449 bool has_unmap, bool unmap,
3450 Error **errp)
3452 BlockDriverState *bs;
3453 BlockBackend *blk;
3454 BlockDriverState *source, *target_bs;
3455 AioContext *aio_context;
3456 Error *local_err = NULL;
3457 QDict *options = NULL;
3458 int flags;
3459 int64_t size;
3460 int ret;
3462 blk = blk_by_name(device);
3463 if (!blk) {
3464 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3465 "Device '%s' not found", device);
3466 return;
3469 aio_context = blk_get_aio_context(blk);
3470 aio_context_acquire(aio_context);
3472 if (!blk_is_available(blk)) {
3473 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3474 goto out;
3476 bs = blk_bs(blk);
3477 if (!has_mode) {
3478 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3481 if (!has_format) {
3482 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3485 flags = bs->open_flags | BDRV_O_RDWR;
3486 source = backing_bs(bs);
3487 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
3488 sync = MIRROR_SYNC_MODE_FULL;
3490 if (sync == MIRROR_SYNC_MODE_NONE) {
3491 source = bs;
3494 size = bdrv_getlength(bs);
3495 if (size < 0) {
3496 error_setg_errno(errp, -size, "bdrv_getlength failed");
3497 goto out;
3500 if (has_replaces) {
3501 BlockDriverState *to_replace_bs;
3502 AioContext *replace_aio_context;
3503 int64_t replace_size;
3505 if (!has_node_name) {
3506 error_setg(errp, "a node-name must be provided when replacing a"
3507 " named node of the graph");
3508 goto out;
3511 to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
3513 if (!to_replace_bs) {
3514 error_propagate(errp, local_err);
3515 goto out;
3518 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3519 aio_context_acquire(replace_aio_context);
3520 replace_size = bdrv_getlength(to_replace_bs);
3521 aio_context_release(replace_aio_context);
3523 if (size != replace_size) {
3524 error_setg(errp, "cannot replace image with a mirror image of "
3525 "different size");
3526 goto out;
3530 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
3531 && mode != NEW_IMAGE_MODE_EXISTING)
3533 /* create new image w/o backing file */
3534 assert(format);
3535 bdrv_img_create(target, format,
3536 NULL, NULL, NULL, size, flags, &local_err, false);
3537 } else {
3538 switch (mode) {
3539 case NEW_IMAGE_MODE_EXISTING:
3540 break;
3541 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3542 /* create new image with backing file */
3543 bdrv_img_create(target, format,
3544 source->filename,
3545 source->drv->format_name,
3546 NULL, size, flags, &local_err, false);
3547 break;
3548 default:
3549 abort();
3553 if (local_err) {
3554 error_propagate(errp, local_err);
3555 goto out;
3558 options = qdict_new();
3559 if (has_node_name) {
3560 qdict_put(options, "node-name", qstring_from_str(node_name));
3562 if (format) {
3563 qdict_put(options, "driver", qstring_from_str(format));
3566 /* Mirroring takes care of copy-on-write using the source's backing
3567 * file.
3569 target_bs = NULL;
3570 ret = bdrv_open(&target_bs, target, NULL, options,
3571 flags | BDRV_O_NO_BACKING, &local_err);
3572 if (ret < 0) {
3573 error_propagate(errp, local_err);
3574 goto out;
3577 bdrv_set_aio_context(target_bs, aio_context);
3579 blockdev_mirror_common(bs, target_bs,
3580 has_replaces, replaces, sync,
3581 has_speed, speed,
3582 has_granularity, granularity,
3583 has_buf_size, buf_size,
3584 has_on_source_error, on_source_error,
3585 has_on_target_error, on_target_error,
3586 has_unmap, unmap,
3587 &local_err);
3588 if (local_err) {
3589 error_propagate(errp, local_err);
3590 bdrv_unref(target_bs);
3592 out:
3593 aio_context_release(aio_context);
3596 void qmp_blockdev_mirror(const char *device, const char *target,
3597 bool has_replaces, const char *replaces,
3598 MirrorSyncMode sync,
3599 bool has_speed, int64_t speed,
3600 bool has_granularity, uint32_t granularity,
3601 bool has_buf_size, int64_t buf_size,
3602 bool has_on_source_error,
3603 BlockdevOnError on_source_error,
3604 bool has_on_target_error,
3605 BlockdevOnError on_target_error,
3606 Error **errp)
3608 BlockDriverState *bs;
3609 BlockBackend *blk;
3610 BlockDriverState *target_bs;
3611 AioContext *aio_context;
3612 Error *local_err = NULL;
3614 blk = blk_by_name(device);
3615 if (!blk) {
3616 error_setg(errp, "Device '%s' not found", device);
3617 return;
3619 bs = blk_bs(blk);
3621 if (!bs) {
3622 error_setg(errp, "Device '%s' has no media", device);
3623 return;
3626 target_bs = bdrv_lookup_bs(target, target, errp);
3627 if (!target_bs) {
3628 return;
3631 aio_context = bdrv_get_aio_context(bs);
3632 aio_context_acquire(aio_context);
3634 bdrv_ref(target_bs);
3635 bdrv_set_aio_context(target_bs, aio_context);
3637 blockdev_mirror_common(bs, target_bs,
3638 has_replaces, replaces, sync,
3639 has_speed, speed,
3640 has_granularity, granularity,
3641 has_buf_size, buf_size,
3642 has_on_source_error, on_source_error,
3643 has_on_target_error, on_target_error,
3644 true, true,
3645 &local_err);
3646 if (local_err) {
3647 error_propagate(errp, local_err);
3648 bdrv_unref(target_bs);
3651 aio_context_release(aio_context);
3654 /* Get the block job for a given device name and acquire its AioContext */
3655 static BlockJob *find_block_job(const char *device, AioContext **aio_context,
3656 Error **errp)
3658 BlockBackend *blk;
3659 BlockDriverState *bs;
3661 *aio_context = NULL;
3663 blk = blk_by_name(device);
3664 if (!blk) {
3665 goto notfound;
3668 *aio_context = blk_get_aio_context(blk);
3669 aio_context_acquire(*aio_context);
3671 if (!blk_is_available(blk)) {
3672 goto notfound;
3674 bs = blk_bs(blk);
3676 if (!bs->job) {
3677 goto notfound;
3680 return bs->job;
3682 notfound:
3683 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3684 "No active block job on device '%s'", device);
3685 if (*aio_context) {
3686 aio_context_release(*aio_context);
3687 *aio_context = NULL;
3689 return NULL;
3692 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
3694 AioContext *aio_context;
3695 BlockJob *job = find_block_job(device, &aio_context, errp);
3697 if (!job) {
3698 return;
3701 block_job_set_speed(job, speed, errp);
3702 aio_context_release(aio_context);
3705 void qmp_block_job_cancel(const char *device,
3706 bool has_force, bool force, Error **errp)
3708 AioContext *aio_context;
3709 BlockJob *job = find_block_job(device, &aio_context, errp);
3711 if (!job) {
3712 return;
3715 if (!has_force) {
3716 force = false;
3719 if (job->user_paused && !force) {
3720 error_setg(errp, "The block job for device '%s' is currently paused",
3721 device);
3722 goto out;
3725 trace_qmp_block_job_cancel(job);
3726 block_job_cancel(job);
3727 out:
3728 aio_context_release(aio_context);
3731 void qmp_block_job_pause(const char *device, Error **errp)
3733 AioContext *aio_context;
3734 BlockJob *job = find_block_job(device, &aio_context, errp);
3736 if (!job || job->user_paused) {
3737 return;
3740 job->user_paused = true;
3741 trace_qmp_block_job_pause(job);
3742 block_job_pause(job);
3743 aio_context_release(aio_context);
3746 void qmp_block_job_resume(const char *device, Error **errp)
3748 AioContext *aio_context;
3749 BlockJob *job = find_block_job(device, &aio_context, errp);
3751 if (!job || !job->user_paused) {
3752 return;
3755 job->user_paused = false;
3756 trace_qmp_block_job_resume(job);
3757 block_job_resume(job);
3758 aio_context_release(aio_context);
3761 void qmp_block_job_complete(const char *device, Error **errp)
3763 AioContext *aio_context;
3764 BlockJob *job = find_block_job(device, &aio_context, errp);
3766 if (!job) {
3767 return;
3770 trace_qmp_block_job_complete(job);
3771 block_job_complete(job, errp);
3772 aio_context_release(aio_context);
3775 void qmp_change_backing_file(const char *device,
3776 const char *image_node_name,
3777 const char *backing_file,
3778 Error **errp)
3780 BlockBackend *blk;
3781 BlockDriverState *bs = NULL;
3782 AioContext *aio_context;
3783 BlockDriverState *image_bs = NULL;
3784 Error *local_err = NULL;
3785 bool ro;
3786 int open_flags;
3787 int ret;
3789 blk = blk_by_name(device);
3790 if (!blk) {
3791 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3792 "Device '%s' not found", device);
3793 return;
3796 aio_context = blk_get_aio_context(blk);
3797 aio_context_acquire(aio_context);
3799 if (!blk_is_available(blk)) {
3800 error_setg(errp, "Device '%s' has no medium", device);
3801 goto out;
3803 bs = blk_bs(blk);
3805 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3806 if (local_err) {
3807 error_propagate(errp, local_err);
3808 goto out;
3811 if (!image_bs) {
3812 error_setg(errp, "image file not found");
3813 goto out;
3816 if (bdrv_find_base(image_bs) == image_bs) {
3817 error_setg(errp, "not allowing backing file change on an image "
3818 "without a backing file");
3819 goto out;
3822 /* even though we are not necessarily operating on bs, we need it to
3823 * determine if block ops are currently prohibited on the chain */
3824 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3825 goto out;
3828 /* final sanity check */
3829 if (!bdrv_chain_contains(bs, image_bs)) {
3830 error_setg(errp, "'%s' and image file are not in the same chain",
3831 device);
3832 goto out;
3835 /* if not r/w, reopen to make r/w */
3836 open_flags = image_bs->open_flags;
3837 ro = bdrv_is_read_only(image_bs);
3839 if (ro) {
3840 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3841 if (local_err) {
3842 error_propagate(errp, local_err);
3843 goto out;
3847 ret = bdrv_change_backing_file(image_bs, backing_file,
3848 image_bs->drv ? image_bs->drv->format_name : "");
3850 if (ret < 0) {
3851 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3852 backing_file);
3853 /* don't exit here, so we can try to restore open flags if
3854 * appropriate */
3857 if (ro) {
3858 bdrv_reopen(image_bs, open_flags, &local_err);
3859 if (local_err) {
3860 error_propagate(errp, local_err); /* will preserve prior errp */
3864 out:
3865 aio_context_release(aio_context);
3868 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3870 QmpOutputVisitor *ov = qmp_output_visitor_new();
3871 BlockDriverState *bs;
3872 BlockBackend *blk = NULL;
3873 QObject *obj;
3874 QDict *qdict;
3875 Error *local_err = NULL;
3877 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3878 * cache.direct=false instead of silently switching to aio=threads, except
3879 * when called from drive_new().
3881 * For now, simply forbidding the combination for all drivers will do. */
3882 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3883 bool direct = options->has_cache &&
3884 options->cache->has_direct &&
3885 options->cache->direct;
3886 if (!direct) {
3887 error_setg(errp, "aio=native requires cache.direct=true");
3888 goto fail;
3892 visit_type_BlockdevOptions(qmp_output_get_visitor(ov), NULL, &options,
3893 &local_err);
3894 if (local_err) {
3895 error_propagate(errp, local_err);
3896 goto fail;
3899 obj = qmp_output_get_qobject(ov);
3900 qdict = qobject_to_qdict(obj);
3902 qdict_flatten(qdict);
3904 if (options->has_id) {
3905 blk = blockdev_init(NULL, qdict, &local_err);
3906 if (local_err) {
3907 error_propagate(errp, local_err);
3908 goto fail;
3911 bs = blk_bs(blk);
3912 } else {
3913 if (!qdict_get_try_str(qdict, "node-name")) {
3914 error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3915 "the root node");
3916 goto fail;
3919 bs = bds_tree_init(qdict, errp);
3920 if (!bs) {
3921 goto fail;
3924 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3927 if (bs && bdrv_key_required(bs)) {
3928 if (blk) {
3929 blk_unref(blk);
3930 } else {
3931 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3932 bdrv_unref(bs);
3934 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3935 goto fail;
3938 fail:
3939 qmp_output_visitor_cleanup(ov);
3942 void qmp_x_blockdev_del(bool has_id, const char *id,
3943 bool has_node_name, const char *node_name, Error **errp)
3945 AioContext *aio_context;
3946 BlockBackend *blk;
3947 BlockDriverState *bs;
3949 if (has_id && has_node_name) {
3950 error_setg(errp, "Only one of id and node-name must be specified");
3951 return;
3952 } else if (!has_id && !has_node_name) {
3953 error_setg(errp, "No block device specified");
3954 return;
3957 if (has_id) {
3958 blk = blk_by_name(id);
3959 if (!blk) {
3960 error_setg(errp, "Cannot find block backend %s", id);
3961 return;
3963 if (blk_get_refcnt(blk) > 1) {
3964 error_setg(errp, "Block backend %s is in use", id);
3965 return;
3967 bs = blk_bs(blk);
3968 aio_context = blk_get_aio_context(blk);
3969 } else {
3970 bs = bdrv_find_node(node_name);
3971 if (!bs) {
3972 error_setg(errp, "Cannot find node %s", node_name);
3973 return;
3975 blk = bs->blk;
3976 if (blk) {
3977 error_setg(errp, "Node %s is in use by %s",
3978 node_name, blk_name(blk));
3979 return;
3981 aio_context = bdrv_get_aio_context(bs);
3984 aio_context_acquire(aio_context);
3986 if (bs) {
3987 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3988 goto out;
3991 if (!blk && !bs->monitor_list.tqe_prev) {
3992 error_setg(errp, "Node %s is not owned by the monitor",
3993 bs->node_name);
3994 goto out;
3997 if (bs->refcnt > 1) {
3998 error_setg(errp, "Block device %s is in use",
3999 bdrv_get_device_or_node_name(bs));
4000 goto out;
4004 if (blk) {
4005 blk_unref(blk);
4006 } else {
4007 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
4008 bdrv_unref(bs);
4011 out:
4012 aio_context_release(aio_context);
4015 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
4017 BlockJobInfoList *head = NULL, **p_next = &head;
4018 BlockDriverState *bs;
4020 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4021 AioContext *aio_context = bdrv_get_aio_context(bs);
4023 aio_context_acquire(aio_context);
4025 if (bs->job) {
4026 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
4027 elem->value = block_job_query(bs->job);
4028 *p_next = elem;
4029 p_next = &elem->next;
4032 aio_context_release(aio_context);
4035 return head;
4038 QemuOptsList qemu_common_drive_opts = {
4039 .name = "drive",
4040 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4041 .desc = {
4043 .name = "snapshot",
4044 .type = QEMU_OPT_BOOL,
4045 .help = "enable/disable snapshot mode",
4047 .name = "discard",
4048 .type = QEMU_OPT_STRING,
4049 .help = "discard operation (ignore/off, unmap/on)",
4051 .name = "aio",
4052 .type = QEMU_OPT_STRING,
4053 .help = "host AIO implementation (threads, native)",
4055 .name = "format",
4056 .type = QEMU_OPT_STRING,
4057 .help = "disk format (raw, qcow2, ...)",
4059 .name = "rerror",
4060 .type = QEMU_OPT_STRING,
4061 .help = "read error action",
4063 .name = "werror",
4064 .type = QEMU_OPT_STRING,
4065 .help = "write error action",
4067 .name = "read-only",
4068 .type = QEMU_OPT_BOOL,
4069 .help = "open drive file as read-only",
4071 .name = "throttling.iops-total",
4072 .type = QEMU_OPT_NUMBER,
4073 .help = "limit total I/O operations per second",
4075 .name = "throttling.iops-read",
4076 .type = QEMU_OPT_NUMBER,
4077 .help = "limit read operations per second",
4079 .name = "throttling.iops-write",
4080 .type = QEMU_OPT_NUMBER,
4081 .help = "limit write operations per second",
4083 .name = "throttling.bps-total",
4084 .type = QEMU_OPT_NUMBER,
4085 .help = "limit total bytes per second",
4087 .name = "throttling.bps-read",
4088 .type = QEMU_OPT_NUMBER,
4089 .help = "limit read bytes per second",
4091 .name = "throttling.bps-write",
4092 .type = QEMU_OPT_NUMBER,
4093 .help = "limit write bytes per second",
4095 .name = "throttling.iops-total-max",
4096 .type = QEMU_OPT_NUMBER,
4097 .help = "I/O operations burst",
4099 .name = "throttling.iops-read-max",
4100 .type = QEMU_OPT_NUMBER,
4101 .help = "I/O operations read burst",
4103 .name = "throttling.iops-write-max",
4104 .type = QEMU_OPT_NUMBER,
4105 .help = "I/O operations write burst",
4107 .name = "throttling.bps-total-max",
4108 .type = QEMU_OPT_NUMBER,
4109 .help = "total bytes burst",
4111 .name = "throttling.bps-read-max",
4112 .type = QEMU_OPT_NUMBER,
4113 .help = "total bytes read burst",
4115 .name = "throttling.bps-write-max",
4116 .type = QEMU_OPT_NUMBER,
4117 .help = "total bytes write burst",
4119 .name = "throttling.iops-total-max-length",
4120 .type = QEMU_OPT_NUMBER,
4121 .help = "length of the iops-total-max burst period, in seconds",
4123 .name = "throttling.iops-read-max-length",
4124 .type = QEMU_OPT_NUMBER,
4125 .help = "length of the iops-read-max burst period, in seconds",
4127 .name = "throttling.iops-write-max-length",
4128 .type = QEMU_OPT_NUMBER,
4129 .help = "length of the iops-write-max burst period, in seconds",
4131 .name = "throttling.bps-total-max-length",
4132 .type = QEMU_OPT_NUMBER,
4133 .help = "length of the bps-total-max burst period, in seconds",
4135 .name = "throttling.bps-read-max-length",
4136 .type = QEMU_OPT_NUMBER,
4137 .help = "length of the bps-read-max burst period, in seconds",
4139 .name = "throttling.bps-write-max-length",
4140 .type = QEMU_OPT_NUMBER,
4141 .help = "length of the bps-write-max burst period, in seconds",
4143 .name = "throttling.iops-size",
4144 .type = QEMU_OPT_NUMBER,
4145 .help = "when limiting by iops max size of an I/O in bytes",
4147 .name = "throttling.group",
4148 .type = QEMU_OPT_STRING,
4149 .help = "name of the block throttling group",
4151 .name = "copy-on-read",
4152 .type = QEMU_OPT_BOOL,
4153 .help = "copy read data from backing file into image file",
4155 .name = "detect-zeroes",
4156 .type = QEMU_OPT_STRING,
4157 .help = "try to optimize zero writes (off, on, unmap)",
4159 .name = "stats-account-invalid",
4160 .type = QEMU_OPT_BOOL,
4161 .help = "whether to account for invalid I/O operations "
4162 "in the statistics",
4164 .name = "stats-account-failed",
4165 .type = QEMU_OPT_BOOL,
4166 .help = "whether to account for failed I/O operations "
4167 "in the statistics",
4169 { /* end of list */ }
4173 static QemuOptsList qemu_root_bds_opts = {
4174 .name = "root-bds",
4175 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4176 .desc = {
4178 .name = "discard",
4179 .type = QEMU_OPT_STRING,
4180 .help = "discard operation (ignore/off, unmap/on)",
4182 .name = "aio",
4183 .type = QEMU_OPT_STRING,
4184 .help = "host AIO implementation (threads, native)",
4186 .name = "read-only",
4187 .type = QEMU_OPT_BOOL,
4188 .help = "open drive file as read-only",
4190 .name = "copy-on-read",
4191 .type = QEMU_OPT_BOOL,
4192 .help = "copy read data from backing file into image file",
4194 .name = "detect-zeroes",
4195 .type = QEMU_OPT_STRING,
4196 .help = "try to optimize zero writes (off, on, unmap)",
4198 { /* end of list */ }
4202 QemuOptsList qemu_drive_opts = {
4203 .name = "drive",
4204 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4205 .desc = {
4207 * no elements => accept any params
4208 * validation will happen later
4210 { /* end of list */ }