Merge remote-tracking branch 'remotes/stefanha/tags/tracing-pull-request' into staging
[qemu/cris-port.git] / blockdev.c
blobd4bc43594020ace2c3887d1b10d949e52d34450b
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);
2516 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2517 BDRV_O_PROTOCOL);
2519 if (!has_read_only) {
2520 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2523 switch (read_only) {
2524 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2525 break;
2527 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2528 bdrv_flags &= ~BDRV_O_RDWR;
2529 break;
2531 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2532 bdrv_flags |= BDRV_O_RDWR;
2533 break;
2535 default:
2536 abort();
2539 if (has_format) {
2540 options = qdict_new();
2541 qdict_put(options, "driver", qstring_from_str(format));
2544 assert(!medium_bs);
2545 ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2546 if (ret < 0) {
2547 goto fail;
2550 blk_apply_root_state(blk, medium_bs);
2552 bdrv_add_key(medium_bs, NULL, &err);
2553 if (err) {
2554 error_propagate(errp, err);
2555 goto fail;
2558 qmp_blockdev_open_tray(device, false, false, &err);
2559 if (err) {
2560 error_propagate(errp, err);
2561 goto fail;
2564 qmp_x_blockdev_remove_medium(device, &err);
2565 if (err) {
2566 error_propagate(errp, err);
2567 goto fail;
2570 qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2571 if (err) {
2572 error_propagate(errp, err);
2573 goto fail;
2576 qmp_blockdev_close_tray(device, errp);
2578 fail:
2579 /* If the medium has been inserted, the device has its own reference, so
2580 * ours must be relinquished; and if it has not been inserted successfully,
2581 * the reference must be relinquished anyway */
2582 bdrv_unref(medium_bs);
2585 /* throttling disk I/O limits */
2586 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
2587 int64_t bps_wr,
2588 int64_t iops,
2589 int64_t iops_rd,
2590 int64_t iops_wr,
2591 bool has_bps_max,
2592 int64_t bps_max,
2593 bool has_bps_rd_max,
2594 int64_t bps_rd_max,
2595 bool has_bps_wr_max,
2596 int64_t bps_wr_max,
2597 bool has_iops_max,
2598 int64_t iops_max,
2599 bool has_iops_rd_max,
2600 int64_t iops_rd_max,
2601 bool has_iops_wr_max,
2602 int64_t iops_wr_max,
2603 bool has_bps_max_length,
2604 int64_t bps_max_length,
2605 bool has_bps_rd_max_length,
2606 int64_t bps_rd_max_length,
2607 bool has_bps_wr_max_length,
2608 int64_t bps_wr_max_length,
2609 bool has_iops_max_length,
2610 int64_t iops_max_length,
2611 bool has_iops_rd_max_length,
2612 int64_t iops_rd_max_length,
2613 bool has_iops_wr_max_length,
2614 int64_t iops_wr_max_length,
2615 bool has_iops_size,
2616 int64_t iops_size,
2617 bool has_group,
2618 const char *group, Error **errp)
2620 ThrottleConfig cfg;
2621 BlockDriverState *bs;
2622 BlockBackend *blk;
2623 AioContext *aio_context;
2625 blk = blk_by_name(device);
2626 if (!blk) {
2627 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2628 "Device '%s' not found", device);
2629 return;
2632 aio_context = blk_get_aio_context(blk);
2633 aio_context_acquire(aio_context);
2635 bs = blk_bs(blk);
2636 if (!bs) {
2637 error_setg(errp, "Device '%s' has no medium", device);
2638 goto out;
2641 throttle_config_init(&cfg);
2642 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2643 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
2644 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
2646 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2647 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
2648 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2650 if (has_bps_max) {
2651 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
2653 if (has_bps_rd_max) {
2654 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
2656 if (has_bps_wr_max) {
2657 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2659 if (has_iops_max) {
2660 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2662 if (has_iops_rd_max) {
2663 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2665 if (has_iops_wr_max) {
2666 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2669 if (has_bps_max_length) {
2670 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = bps_max_length;
2672 if (has_bps_rd_max_length) {
2673 cfg.buckets[THROTTLE_BPS_READ].burst_length = bps_rd_max_length;
2675 if (has_bps_wr_max_length) {
2676 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = bps_wr_max_length;
2678 if (has_iops_max_length) {
2679 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = iops_max_length;
2681 if (has_iops_rd_max_length) {
2682 cfg.buckets[THROTTLE_OPS_READ].burst_length = iops_rd_max_length;
2684 if (has_iops_wr_max_length) {
2685 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = iops_wr_max_length;
2688 if (has_iops_size) {
2689 cfg.op_size = iops_size;
2692 if (!throttle_is_valid(&cfg, errp)) {
2693 goto out;
2696 if (throttle_enabled(&cfg)) {
2697 /* Enable I/O limits if they're not enabled yet, otherwise
2698 * just update the throttling group. */
2699 if (!bs->throttle_state) {
2700 bdrv_io_limits_enable(bs, has_group ? group : device);
2701 } else if (has_group) {
2702 bdrv_io_limits_update_group(bs, group);
2704 /* Set the new throttling configuration */
2705 bdrv_set_io_limits(bs, &cfg);
2706 } else if (bs->throttle_state) {
2707 /* If all throttling settings are set to 0, disable I/O limits */
2708 bdrv_io_limits_disable(bs);
2711 out:
2712 aio_context_release(aio_context);
2715 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2716 bool has_granularity, uint32_t granularity,
2717 Error **errp)
2719 AioContext *aio_context;
2720 BlockDriverState *bs;
2722 if (!name || name[0] == '\0') {
2723 error_setg(errp, "Bitmap name cannot be empty");
2724 return;
2727 bs = bdrv_lookup_bs(node, node, errp);
2728 if (!bs) {
2729 return;
2732 aio_context = bdrv_get_aio_context(bs);
2733 aio_context_acquire(aio_context);
2735 if (has_granularity) {
2736 if (granularity < 512 || !is_power_of_2(granularity)) {
2737 error_setg(errp, "Granularity must be power of 2 "
2738 "and at least 512");
2739 goto out;
2741 } else {
2742 /* Default to cluster size, if available: */
2743 granularity = bdrv_get_default_bitmap_granularity(bs);
2746 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2748 out:
2749 aio_context_release(aio_context);
2752 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2753 Error **errp)
2755 AioContext *aio_context;
2756 BlockDriverState *bs;
2757 BdrvDirtyBitmap *bitmap;
2759 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2760 if (!bitmap || !bs) {
2761 return;
2764 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2765 error_setg(errp,
2766 "Bitmap '%s' is currently frozen and cannot be removed",
2767 name);
2768 goto out;
2770 bdrv_dirty_bitmap_make_anon(bitmap);
2771 bdrv_release_dirty_bitmap(bs, bitmap);
2773 out:
2774 aio_context_release(aio_context);
2778 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2779 * immediately after a full backup operation.
2781 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2782 Error **errp)
2784 AioContext *aio_context;
2785 BdrvDirtyBitmap *bitmap;
2786 BlockDriverState *bs;
2788 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2789 if (!bitmap || !bs) {
2790 return;
2793 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2794 error_setg(errp,
2795 "Bitmap '%s' is currently frozen and cannot be modified",
2796 name);
2797 goto out;
2798 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2799 error_setg(errp,
2800 "Bitmap '%s' is currently disabled and cannot be cleared",
2801 name);
2802 goto out;
2805 bdrv_clear_dirty_bitmap(bitmap, NULL);
2807 out:
2808 aio_context_release(aio_context);
2811 void hmp_drive_del(Monitor *mon, const QDict *qdict)
2813 const char *id = qdict_get_str(qdict, "id");
2814 BlockBackend *blk;
2815 BlockDriverState *bs;
2816 AioContext *aio_context;
2817 Error *local_err = NULL;
2819 blk = blk_by_name(id);
2820 if (!blk) {
2821 error_report("Device '%s' not found", id);
2822 return;
2825 if (!blk_legacy_dinfo(blk)) {
2826 error_report("Deleting device added with blockdev-add"
2827 " is not supported");
2828 return;
2831 aio_context = blk_get_aio_context(blk);
2832 aio_context_acquire(aio_context);
2834 bs = blk_bs(blk);
2835 if (bs) {
2836 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2837 error_report_err(local_err);
2838 aio_context_release(aio_context);
2839 return;
2842 blk_remove_bs(blk);
2845 /* if we have a device attached to this BlockDriverState
2846 * then we need to make the drive anonymous until the device
2847 * can be removed. If this is a drive with no device backing
2848 * then we can just get rid of the block driver state right here.
2850 if (blk_get_attached_dev(blk)) {
2851 blk_hide_on_behalf_of_hmp_drive_del(blk);
2852 /* Further I/O must not pause the guest */
2853 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2854 BLOCKDEV_ON_ERROR_REPORT);
2855 } else {
2856 blk_unref(blk);
2859 aio_context_release(aio_context);
2862 void qmp_block_resize(bool has_device, const char *device,
2863 bool has_node_name, const char *node_name,
2864 int64_t size, Error **errp)
2866 Error *local_err = NULL;
2867 BlockDriverState *bs;
2868 AioContext *aio_context;
2869 int ret;
2871 bs = bdrv_lookup_bs(has_device ? device : NULL,
2872 has_node_name ? node_name : NULL,
2873 &local_err);
2874 if (local_err) {
2875 error_propagate(errp, local_err);
2876 return;
2879 aio_context = bdrv_get_aio_context(bs);
2880 aio_context_acquire(aio_context);
2882 if (!bdrv_is_first_non_filter(bs)) {
2883 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2884 goto out;
2887 if (size < 0) {
2888 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2889 goto out;
2892 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2893 error_setg(errp, QERR_DEVICE_IN_USE, device);
2894 goto out;
2897 /* complete all in-flight operations before resizing the device */
2898 bdrv_drain_all();
2900 ret = bdrv_truncate(bs, size);
2901 switch (ret) {
2902 case 0:
2903 break;
2904 case -ENOMEDIUM:
2905 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2906 break;
2907 case -ENOTSUP:
2908 error_setg(errp, QERR_UNSUPPORTED);
2909 break;
2910 case -EACCES:
2911 error_setg(errp, "Device '%s' is read only", device);
2912 break;
2913 case -EBUSY:
2914 error_setg(errp, QERR_DEVICE_IN_USE, device);
2915 break;
2916 default:
2917 error_setg_errno(errp, -ret, "Could not resize");
2918 break;
2921 out:
2922 aio_context_release(aio_context);
2925 static void block_job_cb(void *opaque, int ret)
2927 /* Note that this function may be executed from another AioContext besides
2928 * the QEMU main loop. If you need to access anything that assumes the
2929 * QEMU global mutex, use a BH or introduce a mutex.
2932 BlockDriverState *bs = opaque;
2933 const char *msg = NULL;
2935 trace_block_job_cb(bs, bs->job, ret);
2937 assert(bs->job);
2939 if (ret < 0) {
2940 msg = strerror(-ret);
2943 if (block_job_is_cancelled(bs->job)) {
2944 block_job_event_cancelled(bs->job);
2945 } else {
2946 block_job_event_completed(bs->job, msg);
2950 void qmp_block_stream(const char *device,
2951 bool has_base, const char *base,
2952 bool has_backing_file, const char *backing_file,
2953 bool has_speed, int64_t speed,
2954 bool has_on_error, BlockdevOnError on_error,
2955 Error **errp)
2957 BlockBackend *blk;
2958 BlockDriverState *bs;
2959 BlockDriverState *base_bs = NULL;
2960 AioContext *aio_context;
2961 Error *local_err = NULL;
2962 const char *base_name = NULL;
2964 if (!has_on_error) {
2965 on_error = BLOCKDEV_ON_ERROR_REPORT;
2968 blk = blk_by_name(device);
2969 if (!blk) {
2970 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2971 "Device '%s' not found", device);
2972 return;
2975 aio_context = blk_get_aio_context(blk);
2976 aio_context_acquire(aio_context);
2978 if (!blk_is_available(blk)) {
2979 error_setg(errp, "Device '%s' has no medium", device);
2980 goto out;
2982 bs = blk_bs(blk);
2984 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2985 goto out;
2988 if (has_base) {
2989 base_bs = bdrv_find_backing_image(bs, base);
2990 if (base_bs == NULL) {
2991 error_setg(errp, QERR_BASE_NOT_FOUND, base);
2992 goto out;
2994 assert(bdrv_get_aio_context(base_bs) == aio_context);
2995 base_name = base;
2998 /* if we are streaming the entire chain, the result will have no backing
2999 * file, and specifying one is therefore an error */
3000 if (base_bs == NULL && has_backing_file) {
3001 error_setg(errp, "backing file specified, but streaming the "
3002 "entire chain");
3003 goto out;
3006 /* backing_file string overrides base bs filename */
3007 base_name = has_backing_file ? backing_file : base_name;
3009 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
3010 on_error, block_job_cb, bs, &local_err);
3011 if (local_err) {
3012 error_propagate(errp, local_err);
3013 goto out;
3016 trace_qmp_block_stream(bs, bs->job);
3018 out:
3019 aio_context_release(aio_context);
3022 void qmp_block_commit(const char *device,
3023 bool has_base, const char *base,
3024 bool has_top, const char *top,
3025 bool has_backing_file, const char *backing_file,
3026 bool has_speed, int64_t speed,
3027 Error **errp)
3029 BlockBackend *blk;
3030 BlockDriverState *bs;
3031 BlockDriverState *base_bs, *top_bs;
3032 AioContext *aio_context;
3033 Error *local_err = NULL;
3034 /* This will be part of the QMP command, if/when the
3035 * BlockdevOnError change for blkmirror makes it in
3037 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
3039 if (!has_speed) {
3040 speed = 0;
3043 /* Important Note:
3044 * libvirt relies on the DeviceNotFound error class in order to probe for
3045 * live commit feature versions; for this to work, we must make sure to
3046 * perform the device lookup before any generic errors that may occur in a
3047 * scenario in which all optional arguments are omitted. */
3048 blk = blk_by_name(device);
3049 if (!blk) {
3050 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3051 "Device '%s' not found", device);
3052 return;
3055 aio_context = blk_get_aio_context(blk);
3056 aio_context_acquire(aio_context);
3058 if (!blk_is_available(blk)) {
3059 error_setg(errp, "Device '%s' has no medium", device);
3060 goto out;
3062 bs = blk_bs(blk);
3064 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
3065 goto out;
3068 /* default top_bs is the active layer */
3069 top_bs = bs;
3071 if (has_top && top) {
3072 if (strcmp(bs->filename, top) != 0) {
3073 top_bs = bdrv_find_backing_image(bs, top);
3077 if (top_bs == NULL) {
3078 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
3079 goto out;
3082 assert(bdrv_get_aio_context(top_bs) == aio_context);
3084 if (has_base && base) {
3085 base_bs = bdrv_find_backing_image(top_bs, base);
3086 } else {
3087 base_bs = bdrv_find_base(top_bs);
3090 if (base_bs == NULL) {
3091 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
3092 goto out;
3095 assert(bdrv_get_aio_context(base_bs) == aio_context);
3097 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3098 goto out;
3101 /* Do not allow attempts to commit an image into itself */
3102 if (top_bs == base_bs) {
3103 error_setg(errp, "cannot commit an image into itself");
3104 goto out;
3107 if (top_bs == bs) {
3108 if (has_backing_file) {
3109 error_setg(errp, "'backing-file' specified,"
3110 " but 'top' is the active layer");
3111 goto out;
3113 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
3114 bs, &local_err);
3115 } else {
3116 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
3117 has_backing_file ? backing_file : NULL, &local_err);
3119 if (local_err != NULL) {
3120 error_propagate(errp, local_err);
3121 goto out;
3124 out:
3125 aio_context_release(aio_context);
3128 static void do_drive_backup(const char *device, const char *target,
3129 bool has_format, const char *format,
3130 enum MirrorSyncMode sync,
3131 bool has_mode, enum NewImageMode mode,
3132 bool has_speed, int64_t speed,
3133 bool has_bitmap, const char *bitmap,
3134 bool has_on_source_error,
3135 BlockdevOnError on_source_error,
3136 bool has_on_target_error,
3137 BlockdevOnError on_target_error,
3138 BlockJobTxn *txn, Error **errp)
3140 BlockBackend *blk;
3141 BlockDriverState *bs;
3142 BlockDriverState *target_bs;
3143 BlockDriverState *source = NULL;
3144 BdrvDirtyBitmap *bmap = NULL;
3145 AioContext *aio_context;
3146 QDict *options = NULL;
3147 Error *local_err = NULL;
3148 int flags;
3149 int64_t size;
3150 int ret;
3152 if (!has_speed) {
3153 speed = 0;
3155 if (!has_on_source_error) {
3156 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3158 if (!has_on_target_error) {
3159 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3161 if (!has_mode) {
3162 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3165 blk = blk_by_name(device);
3166 if (!blk) {
3167 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3168 "Device '%s' not found", device);
3169 return;
3172 aio_context = blk_get_aio_context(blk);
3173 aio_context_acquire(aio_context);
3175 /* Although backup_run has this check too, we need to use bs->drv below, so
3176 * do an early check redundantly. */
3177 if (!blk_is_available(blk)) {
3178 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3179 goto out;
3181 bs = blk_bs(blk);
3183 if (!has_format) {
3184 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3187 /* Early check to avoid creating target */
3188 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
3189 goto out;
3192 flags = bs->open_flags | BDRV_O_RDWR;
3194 /* See if we have a backing HD we can use to create our new image
3195 * on top of. */
3196 if (sync == MIRROR_SYNC_MODE_TOP) {
3197 source = backing_bs(bs);
3198 if (!source) {
3199 sync = MIRROR_SYNC_MODE_FULL;
3202 if (sync == MIRROR_SYNC_MODE_NONE) {
3203 source = bs;
3206 size = bdrv_getlength(bs);
3207 if (size < 0) {
3208 error_setg_errno(errp, -size, "bdrv_getlength failed");
3209 goto out;
3212 if (mode != NEW_IMAGE_MODE_EXISTING) {
3213 assert(format);
3214 if (source) {
3215 bdrv_img_create(target, format, source->filename,
3216 source->drv->format_name, NULL,
3217 size, flags, &local_err, false);
3218 } else {
3219 bdrv_img_create(target, format, NULL, NULL, NULL,
3220 size, flags, &local_err, false);
3224 if (local_err) {
3225 error_propagate(errp, local_err);
3226 goto out;
3229 if (format) {
3230 options = qdict_new();
3231 qdict_put(options, "driver", qstring_from_str(format));
3234 target_bs = NULL;
3235 ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
3236 if (ret < 0) {
3237 error_propagate(errp, local_err);
3238 goto out;
3241 bdrv_set_aio_context(target_bs, aio_context);
3243 if (has_bitmap) {
3244 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3245 if (!bmap) {
3246 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
3247 bdrv_unref(target_bs);
3248 goto out;
3252 backup_start(bs, target_bs, speed, sync, bmap,
3253 on_source_error, on_target_error,
3254 block_job_cb, bs, txn, &local_err);
3255 if (local_err != NULL) {
3256 bdrv_unref(target_bs);
3257 error_propagate(errp, local_err);
3258 goto out;
3261 out:
3262 aio_context_release(aio_context);
3265 void qmp_drive_backup(const char *device, const char *target,
3266 bool has_format, const char *format,
3267 enum MirrorSyncMode sync,
3268 bool has_mode, enum NewImageMode mode,
3269 bool has_speed, int64_t speed,
3270 bool has_bitmap, const char *bitmap,
3271 bool has_on_source_error, BlockdevOnError on_source_error,
3272 bool has_on_target_error, BlockdevOnError on_target_error,
3273 Error **errp)
3275 return do_drive_backup(device, target, has_format, format, sync,
3276 has_mode, mode, has_speed, speed,
3277 has_bitmap, bitmap,
3278 has_on_source_error, on_source_error,
3279 has_on_target_error, on_target_error,
3280 NULL, errp);
3283 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3285 return bdrv_named_nodes_list(errp);
3288 void do_blockdev_backup(const char *device, const char *target,
3289 enum MirrorSyncMode sync,
3290 bool has_speed, int64_t speed,
3291 bool has_on_source_error,
3292 BlockdevOnError on_source_error,
3293 bool has_on_target_error,
3294 BlockdevOnError on_target_error,
3295 BlockJobTxn *txn, Error **errp)
3297 BlockBackend *blk, *target_blk;
3298 BlockDriverState *bs;
3299 BlockDriverState *target_bs;
3300 Error *local_err = NULL;
3301 AioContext *aio_context;
3303 if (!has_speed) {
3304 speed = 0;
3306 if (!has_on_source_error) {
3307 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3309 if (!has_on_target_error) {
3310 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3313 blk = blk_by_name(device);
3314 if (!blk) {
3315 error_setg(errp, "Device '%s' not found", device);
3316 return;
3319 aio_context = blk_get_aio_context(blk);
3320 aio_context_acquire(aio_context);
3322 if (!blk_is_available(blk)) {
3323 error_setg(errp, "Device '%s' has no medium", device);
3324 goto out;
3326 bs = blk_bs(blk);
3328 target_blk = blk_by_name(target);
3329 if (!target_blk) {
3330 error_setg(errp, "Device '%s' not found", target);
3331 goto out;
3334 if (!blk_is_available(target_blk)) {
3335 error_setg(errp, "Device '%s' has no medium", target);
3336 goto out;
3338 target_bs = blk_bs(target_blk);
3340 bdrv_ref(target_bs);
3341 bdrv_set_aio_context(target_bs, aio_context);
3342 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
3343 on_target_error, block_job_cb, bs, txn, &local_err);
3344 if (local_err != NULL) {
3345 bdrv_unref(target_bs);
3346 error_propagate(errp, local_err);
3348 out:
3349 aio_context_release(aio_context);
3352 void qmp_blockdev_backup(const char *device, const char *target,
3353 enum MirrorSyncMode sync,
3354 bool has_speed, int64_t speed,
3355 bool has_on_source_error,
3356 BlockdevOnError on_source_error,
3357 bool has_on_target_error,
3358 BlockdevOnError on_target_error,
3359 Error **errp)
3361 do_blockdev_backup(device, target, sync, has_speed, speed,
3362 has_on_source_error, on_source_error,
3363 has_on_target_error, on_target_error,
3364 NULL, errp);
3367 /* Parameter check and block job starting for drive mirroring.
3368 * Caller should hold @device and @target's aio context (must be the same).
3370 static void blockdev_mirror_common(BlockDriverState *bs,
3371 BlockDriverState *target,
3372 bool has_replaces, const char *replaces,
3373 enum MirrorSyncMode sync,
3374 bool has_speed, int64_t speed,
3375 bool has_granularity, uint32_t granularity,
3376 bool has_buf_size, int64_t buf_size,
3377 bool has_on_source_error,
3378 BlockdevOnError on_source_error,
3379 bool has_on_target_error,
3380 BlockdevOnError on_target_error,
3381 bool has_unmap, bool unmap,
3382 Error **errp)
3385 if (!has_speed) {
3386 speed = 0;
3388 if (!has_on_source_error) {
3389 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3391 if (!has_on_target_error) {
3392 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3394 if (!has_granularity) {
3395 granularity = 0;
3397 if (!has_buf_size) {
3398 buf_size = 0;
3400 if (!has_unmap) {
3401 unmap = true;
3404 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3405 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3406 "a value in range [512B, 64MB]");
3407 return;
3409 if (granularity & (granularity - 1)) {
3410 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3411 "power of 2");
3412 return;
3415 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3416 return;
3418 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3419 return;
3421 if (target->blk) {
3422 error_setg(errp, "Cannot mirror to an attached block device");
3423 return;
3426 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3427 sync = MIRROR_SYNC_MODE_FULL;
3430 /* pass the node name to replace to mirror start since it's loose coupling
3431 * and will allow to check whether the node still exist at mirror completion
3433 mirror_start(bs, target,
3434 has_replaces ? replaces : NULL,
3435 speed, granularity, buf_size, sync,
3436 on_source_error, on_target_error, unmap,
3437 block_job_cb, bs, errp);
3440 void qmp_drive_mirror(const char *device, const char *target,
3441 bool has_format, const char *format,
3442 bool has_node_name, const char *node_name,
3443 bool has_replaces, const char *replaces,
3444 enum MirrorSyncMode sync,
3445 bool has_mode, enum NewImageMode mode,
3446 bool has_speed, int64_t speed,
3447 bool has_granularity, uint32_t granularity,
3448 bool has_buf_size, int64_t buf_size,
3449 bool has_on_source_error, BlockdevOnError on_source_error,
3450 bool has_on_target_error, BlockdevOnError on_target_error,
3451 bool has_unmap, bool unmap,
3452 Error **errp)
3454 BlockDriverState *bs;
3455 BlockBackend *blk;
3456 BlockDriverState *source, *target_bs;
3457 AioContext *aio_context;
3458 Error *local_err = NULL;
3459 QDict *options = NULL;
3460 int flags;
3461 int64_t size;
3462 int ret;
3464 blk = blk_by_name(device);
3465 if (!blk) {
3466 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3467 "Device '%s' not found", device);
3468 return;
3471 aio_context = blk_get_aio_context(blk);
3472 aio_context_acquire(aio_context);
3474 if (!blk_is_available(blk)) {
3475 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3476 goto out;
3478 bs = blk_bs(blk);
3479 if (!has_mode) {
3480 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3483 if (!has_format) {
3484 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3487 flags = bs->open_flags | BDRV_O_RDWR;
3488 source = backing_bs(bs);
3489 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
3490 sync = MIRROR_SYNC_MODE_FULL;
3492 if (sync == MIRROR_SYNC_MODE_NONE) {
3493 source = bs;
3496 size = bdrv_getlength(bs);
3497 if (size < 0) {
3498 error_setg_errno(errp, -size, "bdrv_getlength failed");
3499 goto out;
3502 if (has_replaces) {
3503 BlockDriverState *to_replace_bs;
3504 AioContext *replace_aio_context;
3505 int64_t replace_size;
3507 if (!has_node_name) {
3508 error_setg(errp, "a node-name must be provided when replacing a"
3509 " named node of the graph");
3510 goto out;
3513 to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
3515 if (!to_replace_bs) {
3516 error_propagate(errp, local_err);
3517 goto out;
3520 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3521 aio_context_acquire(replace_aio_context);
3522 replace_size = bdrv_getlength(to_replace_bs);
3523 aio_context_release(replace_aio_context);
3525 if (size != replace_size) {
3526 error_setg(errp, "cannot replace image with a mirror image of "
3527 "different size");
3528 goto out;
3532 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
3533 && mode != NEW_IMAGE_MODE_EXISTING)
3535 /* create new image w/o backing file */
3536 assert(format);
3537 bdrv_img_create(target, format,
3538 NULL, NULL, NULL, size, flags, &local_err, false);
3539 } else {
3540 switch (mode) {
3541 case NEW_IMAGE_MODE_EXISTING:
3542 break;
3543 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3544 /* create new image with backing file */
3545 bdrv_img_create(target, format,
3546 source->filename,
3547 source->drv->format_name,
3548 NULL, size, flags, &local_err, false);
3549 break;
3550 default:
3551 abort();
3555 if (local_err) {
3556 error_propagate(errp, local_err);
3557 goto out;
3560 options = qdict_new();
3561 if (has_node_name) {
3562 qdict_put(options, "node-name", qstring_from_str(node_name));
3564 if (format) {
3565 qdict_put(options, "driver", qstring_from_str(format));
3568 /* Mirroring takes care of copy-on-write using the source's backing
3569 * file.
3571 target_bs = NULL;
3572 ret = bdrv_open(&target_bs, target, NULL, options,
3573 flags | BDRV_O_NO_BACKING, &local_err);
3574 if (ret < 0) {
3575 error_propagate(errp, local_err);
3576 goto out;
3579 bdrv_set_aio_context(target_bs, aio_context);
3581 blockdev_mirror_common(bs, target_bs,
3582 has_replaces, replaces, sync,
3583 has_speed, speed,
3584 has_granularity, granularity,
3585 has_buf_size, buf_size,
3586 has_on_source_error, on_source_error,
3587 has_on_target_error, on_target_error,
3588 has_unmap, unmap,
3589 &local_err);
3590 if (local_err) {
3591 error_propagate(errp, local_err);
3592 bdrv_unref(target_bs);
3594 out:
3595 aio_context_release(aio_context);
3598 void qmp_blockdev_mirror(const char *device, const char *target,
3599 bool has_replaces, const char *replaces,
3600 MirrorSyncMode sync,
3601 bool has_speed, int64_t speed,
3602 bool has_granularity, uint32_t granularity,
3603 bool has_buf_size, int64_t buf_size,
3604 bool has_on_source_error,
3605 BlockdevOnError on_source_error,
3606 bool has_on_target_error,
3607 BlockdevOnError on_target_error,
3608 Error **errp)
3610 BlockDriverState *bs;
3611 BlockBackend *blk;
3612 BlockDriverState *target_bs;
3613 AioContext *aio_context;
3614 Error *local_err = NULL;
3616 blk = blk_by_name(device);
3617 if (!blk) {
3618 error_setg(errp, "Device '%s' not found", device);
3619 return;
3621 bs = blk_bs(blk);
3623 if (!bs) {
3624 error_setg(errp, "Device '%s' has no media", device);
3625 return;
3628 target_bs = bdrv_lookup_bs(target, target, errp);
3629 if (!target_bs) {
3630 return;
3633 aio_context = bdrv_get_aio_context(bs);
3634 aio_context_acquire(aio_context);
3636 bdrv_ref(target_bs);
3637 bdrv_set_aio_context(target_bs, aio_context);
3639 blockdev_mirror_common(bs, target_bs,
3640 has_replaces, replaces, sync,
3641 has_speed, speed,
3642 has_granularity, granularity,
3643 has_buf_size, buf_size,
3644 has_on_source_error, on_source_error,
3645 has_on_target_error, on_target_error,
3646 true, true,
3647 &local_err);
3648 if (local_err) {
3649 error_propagate(errp, local_err);
3650 bdrv_unref(target_bs);
3653 aio_context_release(aio_context);
3656 /* Get the block job for a given device name and acquire its AioContext */
3657 static BlockJob *find_block_job(const char *device, AioContext **aio_context,
3658 Error **errp)
3660 BlockBackend *blk;
3661 BlockDriverState *bs;
3663 *aio_context = NULL;
3665 blk = blk_by_name(device);
3666 if (!blk) {
3667 goto notfound;
3670 *aio_context = blk_get_aio_context(blk);
3671 aio_context_acquire(*aio_context);
3673 if (!blk_is_available(blk)) {
3674 goto notfound;
3676 bs = blk_bs(blk);
3678 if (!bs->job) {
3679 goto notfound;
3682 return bs->job;
3684 notfound:
3685 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3686 "No active block job on device '%s'", device);
3687 if (*aio_context) {
3688 aio_context_release(*aio_context);
3689 *aio_context = NULL;
3691 return NULL;
3694 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
3696 AioContext *aio_context;
3697 BlockJob *job = find_block_job(device, &aio_context, errp);
3699 if (!job) {
3700 return;
3703 block_job_set_speed(job, speed, errp);
3704 aio_context_release(aio_context);
3707 void qmp_block_job_cancel(const char *device,
3708 bool has_force, bool force, Error **errp)
3710 AioContext *aio_context;
3711 BlockJob *job = find_block_job(device, &aio_context, errp);
3713 if (!job) {
3714 return;
3717 if (!has_force) {
3718 force = false;
3721 if (job->user_paused && !force) {
3722 error_setg(errp, "The block job for device '%s' is currently paused",
3723 device);
3724 goto out;
3727 trace_qmp_block_job_cancel(job);
3728 block_job_cancel(job);
3729 out:
3730 aio_context_release(aio_context);
3733 void qmp_block_job_pause(const char *device, Error **errp)
3735 AioContext *aio_context;
3736 BlockJob *job = find_block_job(device, &aio_context, errp);
3738 if (!job || job->user_paused) {
3739 return;
3742 job->user_paused = true;
3743 trace_qmp_block_job_pause(job);
3744 block_job_pause(job);
3745 aio_context_release(aio_context);
3748 void qmp_block_job_resume(const char *device, Error **errp)
3750 AioContext *aio_context;
3751 BlockJob *job = find_block_job(device, &aio_context, errp);
3753 if (!job || !job->user_paused) {
3754 return;
3757 job->user_paused = false;
3758 trace_qmp_block_job_resume(job);
3759 block_job_resume(job);
3760 aio_context_release(aio_context);
3763 void qmp_block_job_complete(const char *device, Error **errp)
3765 AioContext *aio_context;
3766 BlockJob *job = find_block_job(device, &aio_context, errp);
3768 if (!job) {
3769 return;
3772 trace_qmp_block_job_complete(job);
3773 block_job_complete(job, errp);
3774 aio_context_release(aio_context);
3777 void qmp_change_backing_file(const char *device,
3778 const char *image_node_name,
3779 const char *backing_file,
3780 Error **errp)
3782 BlockBackend *blk;
3783 BlockDriverState *bs = NULL;
3784 AioContext *aio_context;
3785 BlockDriverState *image_bs = NULL;
3786 Error *local_err = NULL;
3787 bool ro;
3788 int open_flags;
3789 int ret;
3791 blk = blk_by_name(device);
3792 if (!blk) {
3793 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3794 "Device '%s' not found", device);
3795 return;
3798 aio_context = blk_get_aio_context(blk);
3799 aio_context_acquire(aio_context);
3801 if (!blk_is_available(blk)) {
3802 error_setg(errp, "Device '%s' has no medium", device);
3803 goto out;
3805 bs = blk_bs(blk);
3807 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3808 if (local_err) {
3809 error_propagate(errp, local_err);
3810 goto out;
3813 if (!image_bs) {
3814 error_setg(errp, "image file not found");
3815 goto out;
3818 if (bdrv_find_base(image_bs) == image_bs) {
3819 error_setg(errp, "not allowing backing file change on an image "
3820 "without a backing file");
3821 goto out;
3824 /* even though we are not necessarily operating on bs, we need it to
3825 * determine if block ops are currently prohibited on the chain */
3826 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3827 goto out;
3830 /* final sanity check */
3831 if (!bdrv_chain_contains(bs, image_bs)) {
3832 error_setg(errp, "'%s' and image file are not in the same chain",
3833 device);
3834 goto out;
3837 /* if not r/w, reopen to make r/w */
3838 open_flags = image_bs->open_flags;
3839 ro = bdrv_is_read_only(image_bs);
3841 if (ro) {
3842 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3843 if (local_err) {
3844 error_propagate(errp, local_err);
3845 goto out;
3849 ret = bdrv_change_backing_file(image_bs, backing_file,
3850 image_bs->drv ? image_bs->drv->format_name : "");
3852 if (ret < 0) {
3853 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3854 backing_file);
3855 /* don't exit here, so we can try to restore open flags if
3856 * appropriate */
3859 if (ro) {
3860 bdrv_reopen(image_bs, open_flags, &local_err);
3861 if (local_err) {
3862 error_propagate(errp, local_err); /* will preserve prior errp */
3866 out:
3867 aio_context_release(aio_context);
3870 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3872 QmpOutputVisitor *ov = qmp_output_visitor_new();
3873 BlockDriverState *bs;
3874 BlockBackend *blk = NULL;
3875 QObject *obj;
3876 QDict *qdict;
3877 Error *local_err = NULL;
3879 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3880 * cache.direct=false instead of silently switching to aio=threads, except
3881 * when called from drive_new().
3883 * For now, simply forbidding the combination for all drivers will do. */
3884 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3885 bool direct = options->has_cache &&
3886 options->cache->has_direct &&
3887 options->cache->direct;
3888 if (!direct) {
3889 error_setg(errp, "aio=native requires cache.direct=true");
3890 goto fail;
3894 visit_type_BlockdevOptions(qmp_output_get_visitor(ov), NULL, &options,
3895 &local_err);
3896 if (local_err) {
3897 error_propagate(errp, local_err);
3898 goto fail;
3901 obj = qmp_output_get_qobject(ov);
3902 qdict = qobject_to_qdict(obj);
3904 qdict_flatten(qdict);
3906 if (options->has_id) {
3907 blk = blockdev_init(NULL, qdict, &local_err);
3908 if (local_err) {
3909 error_propagate(errp, local_err);
3910 goto fail;
3913 bs = blk_bs(blk);
3914 } else {
3915 if (!qdict_get_try_str(qdict, "node-name")) {
3916 error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3917 "the root node");
3918 goto fail;
3921 bs = bds_tree_init(qdict, errp);
3922 if (!bs) {
3923 goto fail;
3926 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3929 if (bs && bdrv_key_required(bs)) {
3930 if (blk) {
3931 blk_unref(blk);
3932 } else {
3933 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3934 bdrv_unref(bs);
3936 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3937 goto fail;
3940 fail:
3941 qmp_output_visitor_cleanup(ov);
3944 void qmp_x_blockdev_del(bool has_id, const char *id,
3945 bool has_node_name, const char *node_name, Error **errp)
3947 AioContext *aio_context;
3948 BlockBackend *blk;
3949 BlockDriverState *bs;
3951 if (has_id && has_node_name) {
3952 error_setg(errp, "Only one of id and node-name must be specified");
3953 return;
3954 } else if (!has_id && !has_node_name) {
3955 error_setg(errp, "No block device specified");
3956 return;
3959 if (has_id) {
3960 blk = blk_by_name(id);
3961 if (!blk) {
3962 error_setg(errp, "Cannot find block backend %s", id);
3963 return;
3965 if (blk_get_refcnt(blk) > 1) {
3966 error_setg(errp, "Block backend %s is in use", id);
3967 return;
3969 bs = blk_bs(blk);
3970 aio_context = blk_get_aio_context(blk);
3971 } else {
3972 bs = bdrv_find_node(node_name);
3973 if (!bs) {
3974 error_setg(errp, "Cannot find node %s", node_name);
3975 return;
3977 blk = bs->blk;
3978 if (blk) {
3979 error_setg(errp, "Node %s is in use by %s",
3980 node_name, blk_name(blk));
3981 return;
3983 aio_context = bdrv_get_aio_context(bs);
3986 aio_context_acquire(aio_context);
3988 if (bs) {
3989 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3990 goto out;
3993 if (!blk && !bs->monitor_list.tqe_prev) {
3994 error_setg(errp, "Node %s is not owned by the monitor",
3995 bs->node_name);
3996 goto out;
3999 if (bs->refcnt > 1) {
4000 error_setg(errp, "Block device %s is in use",
4001 bdrv_get_device_or_node_name(bs));
4002 goto out;
4006 if (blk) {
4007 blk_unref(blk);
4008 } else {
4009 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
4010 bdrv_unref(bs);
4013 out:
4014 aio_context_release(aio_context);
4017 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
4019 BlockJobInfoList *head = NULL, **p_next = &head;
4020 BlockDriverState *bs;
4022 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4023 AioContext *aio_context = bdrv_get_aio_context(bs);
4025 aio_context_acquire(aio_context);
4027 if (bs->job) {
4028 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
4029 elem->value = block_job_query(bs->job);
4030 *p_next = elem;
4031 p_next = &elem->next;
4034 aio_context_release(aio_context);
4037 return head;
4040 QemuOptsList qemu_common_drive_opts = {
4041 .name = "drive",
4042 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4043 .desc = {
4045 .name = "snapshot",
4046 .type = QEMU_OPT_BOOL,
4047 .help = "enable/disable snapshot mode",
4049 .name = "discard",
4050 .type = QEMU_OPT_STRING,
4051 .help = "discard operation (ignore/off, unmap/on)",
4053 .name = "aio",
4054 .type = QEMU_OPT_STRING,
4055 .help = "host AIO implementation (threads, native)",
4057 .name = "format",
4058 .type = QEMU_OPT_STRING,
4059 .help = "disk format (raw, qcow2, ...)",
4061 .name = "rerror",
4062 .type = QEMU_OPT_STRING,
4063 .help = "read error action",
4065 .name = "werror",
4066 .type = QEMU_OPT_STRING,
4067 .help = "write error action",
4069 .name = "read-only",
4070 .type = QEMU_OPT_BOOL,
4071 .help = "open drive file as read-only",
4073 .name = "throttling.iops-total",
4074 .type = QEMU_OPT_NUMBER,
4075 .help = "limit total I/O operations per second",
4077 .name = "throttling.iops-read",
4078 .type = QEMU_OPT_NUMBER,
4079 .help = "limit read operations per second",
4081 .name = "throttling.iops-write",
4082 .type = QEMU_OPT_NUMBER,
4083 .help = "limit write operations per second",
4085 .name = "throttling.bps-total",
4086 .type = QEMU_OPT_NUMBER,
4087 .help = "limit total bytes per second",
4089 .name = "throttling.bps-read",
4090 .type = QEMU_OPT_NUMBER,
4091 .help = "limit read bytes per second",
4093 .name = "throttling.bps-write",
4094 .type = QEMU_OPT_NUMBER,
4095 .help = "limit write bytes per second",
4097 .name = "throttling.iops-total-max",
4098 .type = QEMU_OPT_NUMBER,
4099 .help = "I/O operations burst",
4101 .name = "throttling.iops-read-max",
4102 .type = QEMU_OPT_NUMBER,
4103 .help = "I/O operations read burst",
4105 .name = "throttling.iops-write-max",
4106 .type = QEMU_OPT_NUMBER,
4107 .help = "I/O operations write burst",
4109 .name = "throttling.bps-total-max",
4110 .type = QEMU_OPT_NUMBER,
4111 .help = "total bytes burst",
4113 .name = "throttling.bps-read-max",
4114 .type = QEMU_OPT_NUMBER,
4115 .help = "total bytes read burst",
4117 .name = "throttling.bps-write-max",
4118 .type = QEMU_OPT_NUMBER,
4119 .help = "total bytes write burst",
4121 .name = "throttling.iops-total-max-length",
4122 .type = QEMU_OPT_NUMBER,
4123 .help = "length of the iops-total-max burst period, in seconds",
4125 .name = "throttling.iops-read-max-length",
4126 .type = QEMU_OPT_NUMBER,
4127 .help = "length of the iops-read-max burst period, in seconds",
4129 .name = "throttling.iops-write-max-length",
4130 .type = QEMU_OPT_NUMBER,
4131 .help = "length of the iops-write-max burst period, in seconds",
4133 .name = "throttling.bps-total-max-length",
4134 .type = QEMU_OPT_NUMBER,
4135 .help = "length of the bps-total-max burst period, in seconds",
4137 .name = "throttling.bps-read-max-length",
4138 .type = QEMU_OPT_NUMBER,
4139 .help = "length of the bps-read-max burst period, in seconds",
4141 .name = "throttling.bps-write-max-length",
4142 .type = QEMU_OPT_NUMBER,
4143 .help = "length of the bps-write-max burst period, in seconds",
4145 .name = "throttling.iops-size",
4146 .type = QEMU_OPT_NUMBER,
4147 .help = "when limiting by iops max size of an I/O in bytes",
4149 .name = "throttling.group",
4150 .type = QEMU_OPT_STRING,
4151 .help = "name of the block throttling group",
4153 .name = "copy-on-read",
4154 .type = QEMU_OPT_BOOL,
4155 .help = "copy read data from backing file into image file",
4157 .name = "detect-zeroes",
4158 .type = QEMU_OPT_STRING,
4159 .help = "try to optimize zero writes (off, on, unmap)",
4161 .name = "stats-account-invalid",
4162 .type = QEMU_OPT_BOOL,
4163 .help = "whether to account for invalid I/O operations "
4164 "in the statistics",
4166 .name = "stats-account-failed",
4167 .type = QEMU_OPT_BOOL,
4168 .help = "whether to account for failed I/O operations "
4169 "in the statistics",
4171 { /* end of list */ }
4175 static QemuOptsList qemu_root_bds_opts = {
4176 .name = "root-bds",
4177 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4178 .desc = {
4180 .name = "discard",
4181 .type = QEMU_OPT_STRING,
4182 .help = "discard operation (ignore/off, unmap/on)",
4184 .name = "aio",
4185 .type = QEMU_OPT_STRING,
4186 .help = "host AIO implementation (threads, native)",
4188 .name = "read-only",
4189 .type = QEMU_OPT_BOOL,
4190 .help = "open drive file as read-only",
4192 .name = "copy-on-read",
4193 .type = QEMU_OPT_BOOL,
4194 .help = "copy read data from backing file into image file",
4196 .name = "detect-zeroes",
4197 .type = QEMU_OPT_STRING,
4198 .help = "try to optimize zero writes (off, on, unmap)",
4200 { /* end of list */ }
4204 QemuOptsList qemu_drive_opts = {
4205 .name = "drive",
4206 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4207 .desc = {
4209 * no elements => accept any params
4210 * validation will happen later
4212 { /* end of list */ }