Merge remote-tracking branch 'remotes/bkoppelmann/tags/pull-tricore-20150629' into...
[qemu/ar7.git] / blockdev.c
blobb35467646eeb4e1cde394f18be99e2bb33d35033
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 "sysemu/block-backend.h"
34 #include "sysemu/blockdev.h"
35 #include "hw/block/block.h"
36 #include "block/blockjob.h"
37 #include "block/throttle-groups.h"
38 #include "monitor/monitor.h"
39 #include "qemu/error-report.h"
40 #include "qemu/option.h"
41 #include "qemu/config-file.h"
42 #include "qapi/qmp/types.h"
43 #include "qapi-visit.h"
44 #include "qapi/qmp/qerror.h"
45 #include "qapi/qmp-output-visitor.h"
46 #include "qapi/util.h"
47 #include "sysemu/sysemu.h"
48 #include "block/block_int.h"
49 #include "qmp-commands.h"
50 #include "trace.h"
51 #include "sysemu/arch_init.h"
53 static const char *const if_name[IF_COUNT] = {
54 [IF_NONE] = "none",
55 [IF_IDE] = "ide",
56 [IF_SCSI] = "scsi",
57 [IF_FLOPPY] = "floppy",
58 [IF_PFLASH] = "pflash",
59 [IF_MTD] = "mtd",
60 [IF_SD] = "sd",
61 [IF_VIRTIO] = "virtio",
62 [IF_XEN] = "xen",
65 static int if_max_devs[IF_COUNT] = {
67 * Do not change these numbers! They govern how drive option
68 * index maps to unit and bus. That mapping is ABI.
70 * All controllers used to imlement if=T drives need to support
71 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
72 * Otherwise, some index values map to "impossible" bus, unit
73 * values.
75 * For instance, if you change [IF_SCSI] to 255, -drive
76 * if=scsi,index=12 no longer means bus=1,unit=5, but
77 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
78 * the drive can't be set up. Regression.
80 [IF_IDE] = 2,
81 [IF_SCSI] = 7,
84 /**
85 * Boards may call this to offer board-by-board overrides
86 * of the default, global values.
88 void override_max_devs(BlockInterfaceType type, int max_devs)
90 BlockBackend *blk;
91 DriveInfo *dinfo;
93 if (max_devs <= 0) {
94 return;
97 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
98 dinfo = blk_legacy_dinfo(blk);
99 if (dinfo->type == type) {
100 fprintf(stderr, "Cannot override units-per-bus property of"
101 " the %s interface, because a drive of that type has"
102 " already been added.\n", if_name[type]);
103 g_assert_not_reached();
107 if_max_devs[type] = max_devs;
111 * We automatically delete the drive when a device using it gets
112 * unplugged. Questionable feature, but we can't just drop it.
113 * Device models call blockdev_mark_auto_del() to schedule the
114 * automatic deletion, and generic qdev code calls blockdev_auto_del()
115 * when deletion is actually safe.
117 void blockdev_mark_auto_del(BlockBackend *blk)
119 DriveInfo *dinfo = blk_legacy_dinfo(blk);
120 BlockDriverState *bs = blk_bs(blk);
121 AioContext *aio_context;
123 if (!dinfo) {
124 return;
127 aio_context = bdrv_get_aio_context(bs);
128 aio_context_acquire(aio_context);
130 if (bs->job) {
131 block_job_cancel(bs->job);
134 aio_context_release(aio_context);
136 dinfo->auto_del = 1;
139 void blockdev_auto_del(BlockBackend *blk)
141 DriveInfo *dinfo = blk_legacy_dinfo(blk);
143 if (dinfo && dinfo->auto_del) {
144 blk_unref(blk);
149 * Returns the current mapping of how many units per bus
150 * a particular interface can support.
152 * A positive integer indicates n units per bus.
153 * 0 implies the mapping has not been established.
154 * -1 indicates an invalid BlockInterfaceType was given.
156 int drive_get_max_devs(BlockInterfaceType type)
158 if (type >= IF_IDE && type < IF_COUNT) {
159 return if_max_devs[type];
162 return -1;
165 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
167 int max_devs = if_max_devs[type];
168 return max_devs ? index / max_devs : 0;
171 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
173 int max_devs = if_max_devs[type];
174 return max_devs ? index % max_devs : index;
177 QemuOpts *drive_def(const char *optstr)
179 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
182 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
183 const char *optstr)
185 QemuOpts *opts;
187 opts = drive_def(optstr);
188 if (!opts) {
189 return NULL;
191 if (type != IF_DEFAULT) {
192 qemu_opt_set(opts, "if", if_name[type], &error_abort);
194 if (index >= 0) {
195 qemu_opt_set_number(opts, "index", index, &error_abort);
197 if (file)
198 qemu_opt_set(opts, "file", file, &error_abort);
199 return opts;
202 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
204 BlockBackend *blk;
205 DriveInfo *dinfo;
207 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
208 dinfo = blk_legacy_dinfo(blk);
209 if (dinfo && dinfo->type == type
210 && dinfo->bus == bus && dinfo->unit == unit) {
211 return dinfo;
215 return NULL;
218 bool drive_check_orphaned(void)
220 BlockBackend *blk;
221 DriveInfo *dinfo;
222 bool rs = false;
224 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
225 dinfo = blk_legacy_dinfo(blk);
226 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
227 /* Unless this is a default drive, this may be an oversight. */
228 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
229 dinfo->type != IF_NONE) {
230 fprintf(stderr, "Warning: Orphaned drive without device: "
231 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
232 blk_name(blk), blk_bs(blk)->filename, if_name[dinfo->type],
233 dinfo->bus, dinfo->unit);
234 rs = true;
238 return rs;
241 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
243 return drive_get(type,
244 drive_index_to_bus_id(type, index),
245 drive_index_to_unit_id(type, index));
248 int drive_get_max_bus(BlockInterfaceType type)
250 int max_bus;
251 BlockBackend *blk;
252 DriveInfo *dinfo;
254 max_bus = -1;
255 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
256 dinfo = blk_legacy_dinfo(blk);
257 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
258 max_bus = dinfo->bus;
261 return max_bus;
264 /* Get a block device. This should only be used for single-drive devices
265 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
266 appropriate bus. */
267 DriveInfo *drive_get_next(BlockInterfaceType type)
269 static int next_block_unit[IF_COUNT];
271 return drive_get(type, 0, next_block_unit[type]++);
274 static void bdrv_format_print(void *opaque, const char *name)
276 error_printf(" %s", name);
279 typedef struct {
280 QEMUBH *bh;
281 BlockDriverState *bs;
282 } BDRVPutRefBH;
284 static void bdrv_put_ref_bh(void *opaque)
286 BDRVPutRefBH *s = opaque;
288 bdrv_unref(s->bs);
289 qemu_bh_delete(s->bh);
290 g_free(s);
294 * Release a BDS reference in a BH
296 * It is not safe to use bdrv_unref() from a callback function when the callers
297 * still need the BlockDriverState. In such cases we schedule a BH to release
298 * the reference.
300 static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
302 BDRVPutRefBH *s;
304 s = g_new(BDRVPutRefBH, 1);
305 s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
306 s->bs = bs;
307 qemu_bh_schedule(s->bh);
310 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
312 if (!strcmp(buf, "ignore")) {
313 return BLOCKDEV_ON_ERROR_IGNORE;
314 } else if (!is_read && !strcmp(buf, "enospc")) {
315 return BLOCKDEV_ON_ERROR_ENOSPC;
316 } else if (!strcmp(buf, "stop")) {
317 return BLOCKDEV_ON_ERROR_STOP;
318 } else if (!strcmp(buf, "report")) {
319 return BLOCKDEV_ON_ERROR_REPORT;
320 } else {
321 error_setg(errp, "'%s' invalid %s error action",
322 buf, is_read ? "read" : "write");
323 return -1;
327 static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
329 if (throttle_conflicting(cfg)) {
330 error_setg(errp, "bps/iops/max total values and read/write values"
331 " cannot be used at the same time");
332 return false;
335 if (!throttle_is_valid(cfg)) {
336 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
337 return false;
340 return true;
343 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
345 /* Takes the ownership of bs_opts */
346 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
347 Error **errp)
349 const char *buf;
350 int ro = 0;
351 int bdrv_flags = 0;
352 int on_read_error, on_write_error;
353 BlockBackend *blk;
354 BlockDriverState *bs;
355 ThrottleConfig cfg;
356 int snapshot = 0;
357 bool copy_on_read;
358 Error *error = NULL;
359 QemuOpts *opts;
360 const char *id;
361 bool has_driver_specific_opts;
362 BlockdevDetectZeroesOptions detect_zeroes;
363 const char *throttling_group;
365 /* Check common options by copying from bs_opts to opts, all other options
366 * stay in bs_opts for processing by bdrv_open(). */
367 id = qdict_get_try_str(bs_opts, "id");
368 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
369 if (error) {
370 error_propagate(errp, error);
371 goto err_no_opts;
374 qemu_opts_absorb_qdict(opts, bs_opts, &error);
375 if (error) {
376 error_propagate(errp, error);
377 goto early_err;
380 if (id) {
381 qdict_del(bs_opts, "id");
384 has_driver_specific_opts = !!qdict_size(bs_opts);
386 /* extract parameters */
387 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
388 ro = qemu_opt_get_bool(opts, "read-only", 0);
389 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
391 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
392 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
393 error_setg(errp, "invalid discard option");
394 goto early_err;
398 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) {
399 bdrv_flags |= BDRV_O_CACHE_WB;
401 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
402 bdrv_flags |= BDRV_O_NOCACHE;
404 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
405 bdrv_flags |= BDRV_O_NO_FLUSH;
408 #ifdef CONFIG_LINUX_AIO
409 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
410 if (!strcmp(buf, "native")) {
411 bdrv_flags |= BDRV_O_NATIVE_AIO;
412 } else if (!strcmp(buf, "threads")) {
413 /* this is the default */
414 } else {
415 error_setg(errp, "invalid aio option");
416 goto early_err;
419 #endif
421 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
422 if (is_help_option(buf)) {
423 error_printf("Supported formats:");
424 bdrv_iterate_format(bdrv_format_print, NULL);
425 error_printf("\n");
426 goto early_err;
429 if (qdict_haskey(bs_opts, "driver")) {
430 error_setg(errp, "Cannot specify both 'driver' and 'format'");
431 goto early_err;
433 qdict_put(bs_opts, "driver", qstring_from_str(buf));
436 /* disk I/O throttling */
437 memset(&cfg, 0, sizeof(cfg));
438 cfg.buckets[THROTTLE_BPS_TOTAL].avg =
439 qemu_opt_get_number(opts, "throttling.bps-total", 0);
440 cfg.buckets[THROTTLE_BPS_READ].avg =
441 qemu_opt_get_number(opts, "throttling.bps-read", 0);
442 cfg.buckets[THROTTLE_BPS_WRITE].avg =
443 qemu_opt_get_number(opts, "throttling.bps-write", 0);
444 cfg.buckets[THROTTLE_OPS_TOTAL].avg =
445 qemu_opt_get_number(opts, "throttling.iops-total", 0);
446 cfg.buckets[THROTTLE_OPS_READ].avg =
447 qemu_opt_get_number(opts, "throttling.iops-read", 0);
448 cfg.buckets[THROTTLE_OPS_WRITE].avg =
449 qemu_opt_get_number(opts, "throttling.iops-write", 0);
451 cfg.buckets[THROTTLE_BPS_TOTAL].max =
452 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
453 cfg.buckets[THROTTLE_BPS_READ].max =
454 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
455 cfg.buckets[THROTTLE_BPS_WRITE].max =
456 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
457 cfg.buckets[THROTTLE_OPS_TOTAL].max =
458 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
459 cfg.buckets[THROTTLE_OPS_READ].max =
460 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
461 cfg.buckets[THROTTLE_OPS_WRITE].max =
462 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
464 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
466 throttling_group = qemu_opt_get(opts, "throttling.group");
468 if (!check_throttle_config(&cfg, &error)) {
469 error_propagate(errp, error);
470 goto early_err;
473 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
474 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
475 on_write_error = parse_block_error_action(buf, 0, &error);
476 if (error) {
477 error_propagate(errp, error);
478 goto early_err;
482 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
483 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
484 on_read_error = parse_block_error_action(buf, 1, &error);
485 if (error) {
486 error_propagate(errp, error);
487 goto early_err;
491 detect_zeroes =
492 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
493 qemu_opt_get(opts, "detect-zeroes"),
494 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
495 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
496 &error);
497 if (error) {
498 error_propagate(errp, error);
499 goto early_err;
502 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
503 !(bdrv_flags & BDRV_O_UNMAP)) {
504 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
505 "without setting discard operation to unmap");
506 goto early_err;
509 /* init */
510 if ((!file || !*file) && !has_driver_specific_opts) {
511 blk = blk_new_with_bs(qemu_opts_id(opts), errp);
512 if (!blk) {
513 goto early_err;
516 bs = blk_bs(blk);
517 bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
518 bs->read_only = ro;
520 QDECREF(bs_opts);
521 } else {
522 if (file && !*file) {
523 file = NULL;
526 if (snapshot) {
527 /* always use cache=unsafe with snapshot */
528 bdrv_flags &= ~BDRV_O_CACHE_MASK;
529 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
532 if (copy_on_read) {
533 bdrv_flags |= BDRV_O_COPY_ON_READ;
536 if (runstate_check(RUN_STATE_INMIGRATE)) {
537 bdrv_flags |= BDRV_O_INCOMING;
540 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
542 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
543 errp);
544 if (!blk) {
545 goto err_no_bs_opts;
547 bs = blk_bs(blk);
550 bs->detect_zeroes = detect_zeroes;
552 bdrv_set_on_error(bs, on_read_error, on_write_error);
554 /* disk I/O throttling */
555 if (throttle_enabled(&cfg)) {
556 if (!throttling_group) {
557 throttling_group = blk_name(blk);
559 bdrv_io_limits_enable(bs, throttling_group);
560 bdrv_set_io_limits(bs, &cfg);
563 if (bdrv_key_required(bs)) {
564 autostart = 0;
567 err_no_bs_opts:
568 qemu_opts_del(opts);
569 return blk;
571 early_err:
572 qemu_opts_del(opts);
573 err_no_opts:
574 QDECREF(bs_opts);
575 return NULL;
578 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
579 Error **errp)
581 const char *value;
583 value = qemu_opt_get(opts, from);
584 if (value) {
585 if (qemu_opt_find(opts, to)) {
586 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
587 "same time", to, from);
588 return;
592 /* rename all items in opts */
593 while ((value = qemu_opt_get(opts, from))) {
594 qemu_opt_set(opts, to, value, &error_abort);
595 qemu_opt_unset(opts, from);
599 QemuOptsList qemu_legacy_drive_opts = {
600 .name = "drive",
601 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
602 .desc = {
604 .name = "bus",
605 .type = QEMU_OPT_NUMBER,
606 .help = "bus number",
608 .name = "unit",
609 .type = QEMU_OPT_NUMBER,
610 .help = "unit number (i.e. lun for scsi)",
612 .name = "index",
613 .type = QEMU_OPT_NUMBER,
614 .help = "index number",
616 .name = "media",
617 .type = QEMU_OPT_STRING,
618 .help = "media type (disk, cdrom)",
620 .name = "if",
621 .type = QEMU_OPT_STRING,
622 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
624 .name = "cyls",
625 .type = QEMU_OPT_NUMBER,
626 .help = "number of cylinders (ide disk geometry)",
628 .name = "heads",
629 .type = QEMU_OPT_NUMBER,
630 .help = "number of heads (ide disk geometry)",
632 .name = "secs",
633 .type = QEMU_OPT_NUMBER,
634 .help = "number of sectors (ide disk geometry)",
636 .name = "trans",
637 .type = QEMU_OPT_STRING,
638 .help = "chs translation (auto, lba, none)",
640 .name = "boot",
641 .type = QEMU_OPT_BOOL,
642 .help = "(deprecated, ignored)",
644 .name = "addr",
645 .type = QEMU_OPT_STRING,
646 .help = "pci address (virtio only)",
648 .name = "serial",
649 .type = QEMU_OPT_STRING,
650 .help = "disk serial number",
652 .name = "file",
653 .type = QEMU_OPT_STRING,
654 .help = "file name",
657 /* Options that are passed on, but have special semantics with -drive */
659 .name = "read-only",
660 .type = QEMU_OPT_BOOL,
661 .help = "open drive file as read-only",
663 .name = "rerror",
664 .type = QEMU_OPT_STRING,
665 .help = "read error action",
667 .name = "werror",
668 .type = QEMU_OPT_STRING,
669 .help = "write error action",
671 .name = "copy-on-read",
672 .type = QEMU_OPT_BOOL,
673 .help = "copy read data from backing file into image file",
676 { /* end of list */ }
680 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
682 const char *value;
683 BlockBackend *blk;
684 DriveInfo *dinfo = NULL;
685 QDict *bs_opts;
686 QemuOpts *legacy_opts;
687 DriveMediaType media = MEDIA_DISK;
688 BlockInterfaceType type;
689 int cyls, heads, secs, translation;
690 int max_devs, bus_id, unit_id, index;
691 const char *devaddr;
692 const char *werror, *rerror;
693 bool read_only = false;
694 bool copy_on_read;
695 const char *serial;
696 const char *filename;
697 Error *local_err = NULL;
698 int i;
700 /* Change legacy command line options into QMP ones */
701 static const struct {
702 const char *from;
703 const char *to;
704 } opt_renames[] = {
705 { "iops", "throttling.iops-total" },
706 { "iops_rd", "throttling.iops-read" },
707 { "iops_wr", "throttling.iops-write" },
709 { "bps", "throttling.bps-total" },
710 { "bps_rd", "throttling.bps-read" },
711 { "bps_wr", "throttling.bps-write" },
713 { "iops_max", "throttling.iops-total-max" },
714 { "iops_rd_max", "throttling.iops-read-max" },
715 { "iops_wr_max", "throttling.iops-write-max" },
717 { "bps_max", "throttling.bps-total-max" },
718 { "bps_rd_max", "throttling.bps-read-max" },
719 { "bps_wr_max", "throttling.bps-write-max" },
721 { "iops_size", "throttling.iops-size" },
723 { "group", "throttling.group" },
725 { "readonly", "read-only" },
728 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
729 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
730 &local_err);
731 if (local_err) {
732 error_report_err(local_err);
733 return NULL;
737 value = qemu_opt_get(all_opts, "cache");
738 if (value) {
739 int flags = 0;
741 if (bdrv_parse_cache_flags(value, &flags) != 0) {
742 error_report("invalid cache option");
743 return NULL;
746 /* Specific options take precedence */
747 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
748 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
749 !!(flags & BDRV_O_CACHE_WB), &error_abort);
751 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
752 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
753 !!(flags & BDRV_O_NOCACHE), &error_abort);
755 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
756 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
757 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
759 qemu_opt_unset(all_opts, "cache");
762 /* Get a QDict for processing the options */
763 bs_opts = qdict_new();
764 qemu_opts_to_qdict(all_opts, bs_opts);
766 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
767 &error_abort);
768 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
769 if (local_err) {
770 error_report_err(local_err);
771 goto fail;
774 /* Deprecated option boot=[on|off] */
775 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
776 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
777 "ignored. Future versions will reject this parameter. Please "
778 "update your scripts.\n");
781 /* Media type */
782 value = qemu_opt_get(legacy_opts, "media");
783 if (value) {
784 if (!strcmp(value, "disk")) {
785 media = MEDIA_DISK;
786 } else if (!strcmp(value, "cdrom")) {
787 media = MEDIA_CDROM;
788 read_only = true;
789 } else {
790 error_report("'%s' invalid media", value);
791 goto fail;
795 /* copy-on-read is disabled with a warning for read-only devices */
796 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
797 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
799 if (read_only && copy_on_read) {
800 error_report("warning: disabling copy-on-read on read-only drive");
801 copy_on_read = false;
804 qdict_put(bs_opts, "read-only",
805 qstring_from_str(read_only ? "on" : "off"));
806 qdict_put(bs_opts, "copy-on-read",
807 qstring_from_str(copy_on_read ? "on" :"off"));
809 /* Controller type */
810 value = qemu_opt_get(legacy_opts, "if");
811 if (value) {
812 for (type = 0;
813 type < IF_COUNT && strcmp(value, if_name[type]);
814 type++) {
816 if (type == IF_COUNT) {
817 error_report("unsupported bus type '%s'", value);
818 goto fail;
820 } else {
821 type = block_default_type;
824 /* Geometry */
825 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
826 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
827 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
829 if (cyls || heads || secs) {
830 if (cyls < 1) {
831 error_report("invalid physical cyls number");
832 goto fail;
834 if (heads < 1) {
835 error_report("invalid physical heads number");
836 goto fail;
838 if (secs < 1) {
839 error_report("invalid physical secs number");
840 goto fail;
844 translation = BIOS_ATA_TRANSLATION_AUTO;
845 value = qemu_opt_get(legacy_opts, "trans");
846 if (value != NULL) {
847 if (!cyls) {
848 error_report("'%s' trans must be used with cyls, heads and secs",
849 value);
850 goto fail;
852 if (!strcmp(value, "none")) {
853 translation = BIOS_ATA_TRANSLATION_NONE;
854 } else if (!strcmp(value, "lba")) {
855 translation = BIOS_ATA_TRANSLATION_LBA;
856 } else if (!strcmp(value, "large")) {
857 translation = BIOS_ATA_TRANSLATION_LARGE;
858 } else if (!strcmp(value, "rechs")) {
859 translation = BIOS_ATA_TRANSLATION_RECHS;
860 } else if (!strcmp(value, "auto")) {
861 translation = BIOS_ATA_TRANSLATION_AUTO;
862 } else {
863 error_report("'%s' invalid translation type", value);
864 goto fail;
868 if (media == MEDIA_CDROM) {
869 if (cyls || secs || heads) {
870 error_report("CHS can't be set with media=cdrom");
871 goto fail;
875 /* Device address specified by bus/unit or index.
876 * If none was specified, try to find the first free one. */
877 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
878 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
879 index = qemu_opt_get_number(legacy_opts, "index", -1);
881 max_devs = if_max_devs[type];
883 if (index != -1) {
884 if (bus_id != 0 || unit_id != -1) {
885 error_report("index cannot be used with bus and unit");
886 goto fail;
888 bus_id = drive_index_to_bus_id(type, index);
889 unit_id = drive_index_to_unit_id(type, index);
892 if (unit_id == -1) {
893 unit_id = 0;
894 while (drive_get(type, bus_id, unit_id) != NULL) {
895 unit_id++;
896 if (max_devs && unit_id >= max_devs) {
897 unit_id -= max_devs;
898 bus_id++;
903 if (max_devs && unit_id >= max_devs) {
904 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
905 goto fail;
908 if (drive_get(type, bus_id, unit_id) != NULL) {
909 error_report("drive with bus=%d, unit=%d (index=%d) exists",
910 bus_id, unit_id, index);
911 goto fail;
914 /* Serial number */
915 serial = qemu_opt_get(legacy_opts, "serial");
917 /* no id supplied -> create one */
918 if (qemu_opts_id(all_opts) == NULL) {
919 char *new_id;
920 const char *mediastr = "";
921 if (type == IF_IDE || type == IF_SCSI) {
922 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
924 if (max_devs) {
925 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
926 mediastr, unit_id);
927 } else {
928 new_id = g_strdup_printf("%s%s%i", if_name[type],
929 mediastr, unit_id);
931 qdict_put(bs_opts, "id", qstring_from_str(new_id));
932 g_free(new_id);
935 /* Add virtio block device */
936 devaddr = qemu_opt_get(legacy_opts, "addr");
937 if (devaddr && type != IF_VIRTIO) {
938 error_report("addr is not supported by this bus type");
939 goto fail;
942 if (type == IF_VIRTIO) {
943 QemuOpts *devopts;
944 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
945 &error_abort);
946 if (arch_type == QEMU_ARCH_S390X) {
947 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
948 } else {
949 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
951 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
952 &error_abort);
953 if (devaddr) {
954 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
958 filename = qemu_opt_get(legacy_opts, "file");
960 /* Check werror/rerror compatibility with if=... */
961 werror = qemu_opt_get(legacy_opts, "werror");
962 if (werror != NULL) {
963 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
964 type != IF_NONE) {
965 error_report("werror is not supported by this bus type");
966 goto fail;
968 qdict_put(bs_opts, "werror", qstring_from_str(werror));
971 rerror = qemu_opt_get(legacy_opts, "rerror");
972 if (rerror != NULL) {
973 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
974 type != IF_NONE) {
975 error_report("rerror is not supported by this bus type");
976 goto fail;
978 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
981 /* Actual block device init: Functionality shared with blockdev-add */
982 blk = blockdev_init(filename, bs_opts, &local_err);
983 bs_opts = NULL;
984 if (!blk) {
985 if (local_err) {
986 error_report_err(local_err);
988 goto fail;
989 } else {
990 assert(!local_err);
993 /* Create legacy DriveInfo */
994 dinfo = g_malloc0(sizeof(*dinfo));
995 dinfo->opts = all_opts;
997 dinfo->cyls = cyls;
998 dinfo->heads = heads;
999 dinfo->secs = secs;
1000 dinfo->trans = translation;
1002 dinfo->type = type;
1003 dinfo->bus = bus_id;
1004 dinfo->unit = unit_id;
1005 dinfo->devaddr = devaddr;
1006 dinfo->serial = g_strdup(serial);
1008 blk_set_legacy_dinfo(blk, dinfo);
1010 switch(type) {
1011 case IF_IDE:
1012 case IF_SCSI:
1013 case IF_XEN:
1014 case IF_NONE:
1015 dinfo->media_cd = media == MEDIA_CDROM;
1016 break;
1017 default:
1018 break;
1021 fail:
1022 qemu_opts_del(legacy_opts);
1023 QDECREF(bs_opts);
1024 return dinfo;
1027 void hmp_commit(Monitor *mon, const QDict *qdict)
1029 const char *device = qdict_get_str(qdict, "device");
1030 BlockBackend *blk;
1031 int ret;
1033 if (!strcmp(device, "all")) {
1034 ret = bdrv_commit_all();
1035 } else {
1036 blk = blk_by_name(device);
1037 if (!blk) {
1038 monitor_printf(mon, "Device '%s' not found\n", device);
1039 return;
1041 ret = bdrv_commit(blk_bs(blk));
1043 if (ret < 0) {
1044 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1045 strerror(-ret));
1049 static void blockdev_do_action(int kind, void *data, Error **errp)
1051 TransactionAction action;
1052 TransactionActionList list;
1054 action.kind = kind;
1055 action.data = data;
1056 list.value = &action;
1057 list.next = NULL;
1058 qmp_transaction(&list, errp);
1061 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1062 bool has_node_name, const char *node_name,
1063 const char *snapshot_file,
1064 bool has_snapshot_node_name,
1065 const char *snapshot_node_name,
1066 bool has_format, const char *format,
1067 bool has_mode, NewImageMode mode, Error **errp)
1069 BlockdevSnapshot snapshot = {
1070 .has_device = has_device,
1071 .device = (char *) device,
1072 .has_node_name = has_node_name,
1073 .node_name = (char *) node_name,
1074 .snapshot_file = (char *) snapshot_file,
1075 .has_snapshot_node_name = has_snapshot_node_name,
1076 .snapshot_node_name = (char *) snapshot_node_name,
1077 .has_format = has_format,
1078 .format = (char *) format,
1079 .has_mode = has_mode,
1080 .mode = mode,
1082 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1083 &snapshot, errp);
1086 void qmp_blockdev_snapshot_internal_sync(const char *device,
1087 const char *name,
1088 Error **errp)
1090 BlockdevSnapshotInternal snapshot = {
1091 .device = (char *) device,
1092 .name = (char *) name
1095 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1096 &snapshot, errp);
1099 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1100 bool has_id,
1101 const char *id,
1102 bool has_name,
1103 const char *name,
1104 Error **errp)
1106 BlockDriverState *bs;
1107 BlockBackend *blk;
1108 AioContext *aio_context;
1109 QEMUSnapshotInfo sn;
1110 Error *local_err = NULL;
1111 SnapshotInfo *info = NULL;
1112 int ret;
1114 blk = blk_by_name(device);
1115 if (!blk) {
1116 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1117 "Device '%s' not found", device);
1118 return NULL;
1120 bs = blk_bs(blk);
1122 if (!has_id) {
1123 id = NULL;
1126 if (!has_name) {
1127 name = NULL;
1130 if (!id && !name) {
1131 error_setg(errp, "Name or id must be provided");
1132 return NULL;
1135 aio_context = bdrv_get_aio_context(bs);
1136 aio_context_acquire(aio_context);
1138 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1139 goto out_aio_context;
1142 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1143 if (local_err) {
1144 error_propagate(errp, local_err);
1145 goto out_aio_context;
1147 if (!ret) {
1148 error_setg(errp,
1149 "Snapshot with id '%s' and name '%s' does not exist on "
1150 "device '%s'",
1151 STR_OR_NULL(id), STR_OR_NULL(name), device);
1152 goto out_aio_context;
1155 bdrv_snapshot_delete(bs, id, name, &local_err);
1156 if (local_err) {
1157 error_propagate(errp, local_err);
1158 goto out_aio_context;
1161 aio_context_release(aio_context);
1163 info = g_new0(SnapshotInfo, 1);
1164 info->id = g_strdup(sn.id_str);
1165 info->name = g_strdup(sn.name);
1166 info->date_nsec = sn.date_nsec;
1167 info->date_sec = sn.date_sec;
1168 info->vm_state_size = sn.vm_state_size;
1169 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1170 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1172 return info;
1174 out_aio_context:
1175 aio_context_release(aio_context);
1176 return NULL;
1180 * block_dirty_bitmap_lookup:
1181 * Return a dirty bitmap (if present), after validating
1182 * the node reference and bitmap names.
1184 * @node: The name of the BDS node to search for bitmaps
1185 * @name: The name of the bitmap to search for
1186 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1187 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1188 * @errp: Output pointer for error information. Can be NULL.
1190 * @return: A bitmap object on success, or NULL on failure.
1192 static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1193 const char *name,
1194 BlockDriverState **pbs,
1195 AioContext **paio,
1196 Error **errp)
1198 BlockDriverState *bs;
1199 BdrvDirtyBitmap *bitmap;
1200 AioContext *aio_context;
1202 if (!node) {
1203 error_setg(errp, "Node cannot be NULL");
1204 return NULL;
1206 if (!name) {
1207 error_setg(errp, "Bitmap name cannot be NULL");
1208 return NULL;
1210 bs = bdrv_lookup_bs(node, node, NULL);
1211 if (!bs) {
1212 error_setg(errp, "Node '%s' not found", node);
1213 return NULL;
1216 aio_context = bdrv_get_aio_context(bs);
1217 aio_context_acquire(aio_context);
1219 bitmap = bdrv_find_dirty_bitmap(bs, name);
1220 if (!bitmap) {
1221 error_setg(errp, "Dirty bitmap '%s' not found", name);
1222 goto fail;
1225 if (pbs) {
1226 *pbs = bs;
1228 if (paio) {
1229 *paio = aio_context;
1230 } else {
1231 aio_context_release(aio_context);
1234 return bitmap;
1236 fail:
1237 aio_context_release(aio_context);
1238 return NULL;
1241 /* New and old BlockDriverState structs for atomic group operations */
1243 typedef struct BlkTransactionState BlkTransactionState;
1245 /* Only prepare() may fail. In a single transaction, only one of commit() or
1246 abort() will be called, clean() will always be called if it present. */
1247 typedef struct BdrvActionOps {
1248 /* Size of state struct, in bytes. */
1249 size_t instance_size;
1250 /* Prepare the work, must NOT be NULL. */
1251 void (*prepare)(BlkTransactionState *common, Error **errp);
1252 /* Commit the changes, can be NULL. */
1253 void (*commit)(BlkTransactionState *common);
1254 /* Abort the changes on fail, can be NULL. */
1255 void (*abort)(BlkTransactionState *common);
1256 /* Clean up resource in the end, can be NULL. */
1257 void (*clean)(BlkTransactionState *common);
1258 } BdrvActionOps;
1261 * This structure must be arranged as first member in child type, assuming
1262 * that compiler will also arrange it to the same address with parent instance.
1263 * Later it will be used in free().
1265 struct BlkTransactionState {
1266 TransactionAction *action;
1267 const BdrvActionOps *ops;
1268 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
1271 /* internal snapshot private data */
1272 typedef struct InternalSnapshotState {
1273 BlkTransactionState common;
1274 BlockDriverState *bs;
1275 AioContext *aio_context;
1276 QEMUSnapshotInfo sn;
1277 } InternalSnapshotState;
1279 static void internal_snapshot_prepare(BlkTransactionState *common,
1280 Error **errp)
1282 Error *local_err = NULL;
1283 const char *device;
1284 const char *name;
1285 BlockBackend *blk;
1286 BlockDriverState *bs;
1287 QEMUSnapshotInfo old_sn, *sn;
1288 bool ret;
1289 qemu_timeval tv;
1290 BlockdevSnapshotInternal *internal;
1291 InternalSnapshotState *state;
1292 int ret1;
1294 g_assert(common->action->kind ==
1295 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1296 internal = common->action->blockdev_snapshot_internal_sync;
1297 state = DO_UPCAST(InternalSnapshotState, common, common);
1299 /* 1. parse input */
1300 device = internal->device;
1301 name = internal->name;
1303 /* 2. check for validation */
1304 blk = blk_by_name(device);
1305 if (!blk) {
1306 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1307 "Device '%s' not found", device);
1308 return;
1310 bs = blk_bs(blk);
1312 /* AioContext is released in .clean() */
1313 state->aio_context = bdrv_get_aio_context(bs);
1314 aio_context_acquire(state->aio_context);
1316 if (!bdrv_is_inserted(bs)) {
1317 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1318 return;
1321 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1322 return;
1325 if (bdrv_is_read_only(bs)) {
1326 error_setg(errp, "Device '%s' is read only", device);
1327 return;
1330 if (!bdrv_can_snapshot(bs)) {
1331 error_setg(errp, "Block format '%s' used by device '%s' "
1332 "does not support internal snapshots",
1333 bs->drv->format_name, device);
1334 return;
1337 if (!strlen(name)) {
1338 error_setg(errp, "Name is empty");
1339 return;
1342 /* check whether a snapshot with name exist */
1343 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1344 &local_err);
1345 if (local_err) {
1346 error_propagate(errp, local_err);
1347 return;
1348 } else if (ret) {
1349 error_setg(errp,
1350 "Snapshot with name '%s' already exists on device '%s'",
1351 name, device);
1352 return;
1355 /* 3. take the snapshot */
1356 sn = &state->sn;
1357 pstrcpy(sn->name, sizeof(sn->name), name);
1358 qemu_gettimeofday(&tv);
1359 sn->date_sec = tv.tv_sec;
1360 sn->date_nsec = tv.tv_usec * 1000;
1361 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1363 ret1 = bdrv_snapshot_create(bs, sn);
1364 if (ret1 < 0) {
1365 error_setg_errno(errp, -ret1,
1366 "Failed to create snapshot '%s' on device '%s'",
1367 name, device);
1368 return;
1371 /* 4. succeed, mark a snapshot is created */
1372 state->bs = bs;
1375 static void internal_snapshot_abort(BlkTransactionState *common)
1377 InternalSnapshotState *state =
1378 DO_UPCAST(InternalSnapshotState, common, common);
1379 BlockDriverState *bs = state->bs;
1380 QEMUSnapshotInfo *sn = &state->sn;
1381 Error *local_error = NULL;
1383 if (!bs) {
1384 return;
1387 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1388 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1389 "device '%s' in abort: %s",
1390 sn->id_str,
1391 sn->name,
1392 bdrv_get_device_name(bs),
1393 error_get_pretty(local_error));
1394 error_free(local_error);
1398 static void internal_snapshot_clean(BlkTransactionState *common)
1400 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1401 common, common);
1403 if (state->aio_context) {
1404 aio_context_release(state->aio_context);
1408 /* external snapshot private data */
1409 typedef struct ExternalSnapshotState {
1410 BlkTransactionState common;
1411 BlockDriverState *old_bs;
1412 BlockDriverState *new_bs;
1413 AioContext *aio_context;
1414 } ExternalSnapshotState;
1416 static void external_snapshot_prepare(BlkTransactionState *common,
1417 Error **errp)
1419 BlockDriver *drv;
1420 int flags, ret;
1421 QDict *options = NULL;
1422 Error *local_err = NULL;
1423 bool has_device = false;
1424 const char *device;
1425 bool has_node_name = false;
1426 const char *node_name;
1427 bool has_snapshot_node_name = false;
1428 const char *snapshot_node_name;
1429 const char *new_image_file;
1430 const char *format = "qcow2";
1431 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1432 ExternalSnapshotState *state =
1433 DO_UPCAST(ExternalSnapshotState, common, common);
1434 TransactionAction *action = common->action;
1436 /* get parameters */
1437 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
1439 has_device = action->blockdev_snapshot_sync->has_device;
1440 device = action->blockdev_snapshot_sync->device;
1441 has_node_name = action->blockdev_snapshot_sync->has_node_name;
1442 node_name = action->blockdev_snapshot_sync->node_name;
1443 has_snapshot_node_name =
1444 action->blockdev_snapshot_sync->has_snapshot_node_name;
1445 snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name;
1447 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1448 if (action->blockdev_snapshot_sync->has_format) {
1449 format = action->blockdev_snapshot_sync->format;
1451 if (action->blockdev_snapshot_sync->has_mode) {
1452 mode = action->blockdev_snapshot_sync->mode;
1455 /* start processing */
1456 drv = bdrv_find_format(format);
1457 if (!drv) {
1458 error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
1459 return;
1462 state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
1463 has_node_name ? node_name : NULL,
1464 &local_err);
1465 if (local_err) {
1466 error_propagate(errp, local_err);
1467 return;
1470 if (has_node_name && !has_snapshot_node_name) {
1471 error_setg(errp, "New snapshot node name missing");
1472 return;
1475 if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) {
1476 error_setg(errp, "New snapshot node name already existing");
1477 return;
1480 /* Acquire AioContext now so any threads operating on old_bs stop */
1481 state->aio_context = bdrv_get_aio_context(state->old_bs);
1482 aio_context_acquire(state->aio_context);
1484 if (!bdrv_is_inserted(state->old_bs)) {
1485 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1486 return;
1489 if (bdrv_op_is_blocked(state->old_bs,
1490 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1491 return;
1494 if (!bdrv_is_read_only(state->old_bs)) {
1495 if (bdrv_flush(state->old_bs)) {
1496 error_setg(errp, QERR_IO_ERROR);
1497 return;
1501 if (!bdrv_is_first_non_filter(state->old_bs)) {
1502 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1503 return;
1506 flags = state->old_bs->open_flags;
1508 /* create new image w/backing file */
1509 if (mode != NEW_IMAGE_MODE_EXISTING) {
1510 bdrv_img_create(new_image_file, format,
1511 state->old_bs->filename,
1512 state->old_bs->drv->format_name,
1513 NULL, -1, flags, &local_err, false);
1514 if (local_err) {
1515 error_propagate(errp, local_err);
1516 return;
1520 if (has_snapshot_node_name) {
1521 options = qdict_new();
1522 qdict_put(options, "node-name",
1523 qstring_from_str(snapshot_node_name));
1526 /* TODO Inherit bs->options or only take explicit options with an
1527 * extended QMP command? */
1528 assert(state->new_bs == NULL);
1529 ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
1530 flags | BDRV_O_NO_BACKING, drv, &local_err);
1531 /* We will manually add the backing_hd field to the bs later */
1532 if (ret != 0) {
1533 error_propagate(errp, local_err);
1537 static void external_snapshot_commit(BlkTransactionState *common)
1539 ExternalSnapshotState *state =
1540 DO_UPCAST(ExternalSnapshotState, common, common);
1542 bdrv_set_aio_context(state->new_bs, state->aio_context);
1544 /* This removes our old bs and adds the new bs */
1545 bdrv_append(state->new_bs, state->old_bs);
1546 /* We don't need (or want) to use the transactional
1547 * bdrv_reopen_multiple() across all the entries at once, because we
1548 * don't want to abort all of them if one of them fails the reopen */
1549 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
1550 NULL);
1552 aio_context_release(state->aio_context);
1555 static void external_snapshot_abort(BlkTransactionState *common)
1557 ExternalSnapshotState *state =
1558 DO_UPCAST(ExternalSnapshotState, common, common);
1559 if (state->new_bs) {
1560 bdrv_unref(state->new_bs);
1562 if (state->aio_context) {
1563 aio_context_release(state->aio_context);
1567 typedef struct DriveBackupState {
1568 BlkTransactionState common;
1569 BlockDriverState *bs;
1570 AioContext *aio_context;
1571 BlockJob *job;
1572 } DriveBackupState;
1574 static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1576 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1577 BlockDriverState *bs;
1578 BlockBackend *blk;
1579 DriveBackup *backup;
1580 Error *local_err = NULL;
1582 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1583 backup = common->action->drive_backup;
1585 blk = blk_by_name(backup->device);
1586 if (!blk) {
1587 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1588 "Device '%s' not found", backup->device);
1589 return;
1591 bs = blk_bs(blk);
1593 /* AioContext is released in .clean() */
1594 state->aio_context = bdrv_get_aio_context(bs);
1595 aio_context_acquire(state->aio_context);
1597 qmp_drive_backup(backup->device, backup->target,
1598 backup->has_format, backup->format,
1599 backup->sync,
1600 backup->has_mode, backup->mode,
1601 backup->has_speed, backup->speed,
1602 backup->has_bitmap, backup->bitmap,
1603 backup->has_on_source_error, backup->on_source_error,
1604 backup->has_on_target_error, backup->on_target_error,
1605 &local_err);
1606 if (local_err) {
1607 error_propagate(errp, local_err);
1608 return;
1611 state->bs = bs;
1612 state->job = state->bs->job;
1615 static void drive_backup_abort(BlkTransactionState *common)
1617 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1618 BlockDriverState *bs = state->bs;
1620 /* Only cancel if it's the job we started */
1621 if (bs && bs->job && bs->job == state->job) {
1622 block_job_cancel_sync(bs->job);
1626 static void drive_backup_clean(BlkTransactionState *common)
1628 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1630 if (state->aio_context) {
1631 aio_context_release(state->aio_context);
1635 typedef struct BlockdevBackupState {
1636 BlkTransactionState common;
1637 BlockDriverState *bs;
1638 BlockJob *job;
1639 AioContext *aio_context;
1640 } BlockdevBackupState;
1642 static void blockdev_backup_prepare(BlkTransactionState *common, Error **errp)
1644 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1645 BlockdevBackup *backup;
1646 BlockDriverState *bs, *target;
1647 BlockBackend *blk;
1648 Error *local_err = NULL;
1650 assert(common->action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1651 backup = common->action->blockdev_backup;
1653 blk = blk_by_name(backup->device);
1654 if (!blk) {
1655 error_setg(errp, "Device '%s' not found", backup->device);
1656 return;
1658 bs = blk_bs(blk);
1660 blk = blk_by_name(backup->target);
1661 if (!blk) {
1662 error_setg(errp, "Device '%s' not found", backup->target);
1663 return;
1665 target = blk_bs(blk);
1667 /* AioContext is released in .clean() */
1668 state->aio_context = bdrv_get_aio_context(bs);
1669 if (state->aio_context != bdrv_get_aio_context(target)) {
1670 state->aio_context = NULL;
1671 error_setg(errp, "Backup between two IO threads is not implemented");
1672 return;
1674 aio_context_acquire(state->aio_context);
1676 qmp_blockdev_backup(backup->device, backup->target,
1677 backup->sync,
1678 backup->has_speed, backup->speed,
1679 backup->has_on_source_error, backup->on_source_error,
1680 backup->has_on_target_error, backup->on_target_error,
1681 &local_err);
1682 if (local_err) {
1683 error_propagate(errp, local_err);
1684 return;
1687 state->bs = bs;
1688 state->job = state->bs->job;
1691 static void blockdev_backup_abort(BlkTransactionState *common)
1693 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1694 BlockDriverState *bs = state->bs;
1696 /* Only cancel if it's the job we started */
1697 if (bs && bs->job && bs->job == state->job) {
1698 block_job_cancel_sync(bs->job);
1702 static void blockdev_backup_clean(BlkTransactionState *common)
1704 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1706 if (state->aio_context) {
1707 aio_context_release(state->aio_context);
1711 static void abort_prepare(BlkTransactionState *common, Error **errp)
1713 error_setg(errp, "Transaction aborted using Abort action");
1716 static void abort_commit(BlkTransactionState *common)
1718 g_assert_not_reached(); /* this action never succeeds */
1721 static const BdrvActionOps actions[] = {
1722 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1723 .instance_size = sizeof(ExternalSnapshotState),
1724 .prepare = external_snapshot_prepare,
1725 .commit = external_snapshot_commit,
1726 .abort = external_snapshot_abort,
1728 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1729 .instance_size = sizeof(DriveBackupState),
1730 .prepare = drive_backup_prepare,
1731 .abort = drive_backup_abort,
1732 .clean = drive_backup_clean,
1734 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
1735 .instance_size = sizeof(BlockdevBackupState),
1736 .prepare = blockdev_backup_prepare,
1737 .abort = blockdev_backup_abort,
1738 .clean = blockdev_backup_clean,
1740 [TRANSACTION_ACTION_KIND_ABORT] = {
1741 .instance_size = sizeof(BlkTransactionState),
1742 .prepare = abort_prepare,
1743 .commit = abort_commit,
1745 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1746 .instance_size = sizeof(InternalSnapshotState),
1747 .prepare = internal_snapshot_prepare,
1748 .abort = internal_snapshot_abort,
1749 .clean = internal_snapshot_clean,
1754 * 'Atomic' group operations. The operations are performed as a set, and if
1755 * any fail then we roll back all operations in the group.
1757 void qmp_transaction(TransactionActionList *dev_list, Error **errp)
1759 TransactionActionList *dev_entry = dev_list;
1760 BlkTransactionState *state, *next;
1761 Error *local_err = NULL;
1763 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
1764 QSIMPLEQ_INIT(&snap_bdrv_states);
1766 /* drain all i/o before any operations */
1767 bdrv_drain_all();
1769 /* We don't do anything in this loop that commits us to the operations */
1770 while (NULL != dev_entry) {
1771 TransactionAction *dev_info = NULL;
1772 const BdrvActionOps *ops;
1774 dev_info = dev_entry->value;
1775 dev_entry = dev_entry->next;
1777 assert(dev_info->kind < ARRAY_SIZE(actions));
1779 ops = &actions[dev_info->kind];
1780 assert(ops->instance_size > 0);
1782 state = g_malloc0(ops->instance_size);
1783 state->ops = ops;
1784 state->action = dev_info;
1785 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
1787 state->ops->prepare(state, &local_err);
1788 if (local_err) {
1789 error_propagate(errp, local_err);
1790 goto delete_and_fail;
1794 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1795 if (state->ops->commit) {
1796 state->ops->commit(state);
1800 /* success */
1801 goto exit;
1803 delete_and_fail:
1804 /* failure, and it is all-or-none; roll back all operations */
1805 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1806 if (state->ops->abort) {
1807 state->ops->abort(state);
1810 exit:
1811 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1812 if (state->ops->clean) {
1813 state->ops->clean(state);
1815 g_free(state);
1820 static void eject_device(BlockBackend *blk, int force, Error **errp)
1822 BlockDriverState *bs = blk_bs(blk);
1823 AioContext *aio_context;
1825 aio_context = bdrv_get_aio_context(bs);
1826 aio_context_acquire(aio_context);
1828 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
1829 goto out;
1831 if (!blk_dev_has_removable_media(blk)) {
1832 error_setg(errp, "Device '%s' is not removable",
1833 bdrv_get_device_name(bs));
1834 goto out;
1837 if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) {
1838 blk_dev_eject_request(blk, force);
1839 if (!force) {
1840 error_setg(errp, "Device '%s' is locked",
1841 bdrv_get_device_name(bs));
1842 goto out;
1846 bdrv_close(bs);
1848 out:
1849 aio_context_release(aio_context);
1852 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1854 BlockBackend *blk;
1856 blk = blk_by_name(device);
1857 if (!blk) {
1858 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1859 "Device '%s' not found", device);
1860 return;
1863 eject_device(blk, force, errp);
1866 void qmp_block_passwd(bool has_device, const char *device,
1867 bool has_node_name, const char *node_name,
1868 const char *password, Error **errp)
1870 Error *local_err = NULL;
1871 BlockDriverState *bs;
1872 AioContext *aio_context;
1874 bs = bdrv_lookup_bs(has_device ? device : NULL,
1875 has_node_name ? node_name : NULL,
1876 &local_err);
1877 if (local_err) {
1878 error_propagate(errp, local_err);
1879 return;
1882 aio_context = bdrv_get_aio_context(bs);
1883 aio_context_acquire(aio_context);
1885 bdrv_add_key(bs, password, errp);
1887 aio_context_release(aio_context);
1890 /* Assumes AioContext is held */
1891 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1892 int bdrv_flags, BlockDriver *drv,
1893 const char *password, Error **errp)
1895 Error *local_err = NULL;
1896 int ret;
1898 ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err);
1899 if (ret < 0) {
1900 error_propagate(errp, local_err);
1901 return;
1904 bdrv_add_key(bs, password, errp);
1907 void qmp_change_blockdev(const char *device, const char *filename,
1908 const char *format, Error **errp)
1910 BlockBackend *blk;
1911 BlockDriverState *bs;
1912 AioContext *aio_context;
1913 BlockDriver *drv = NULL;
1914 int bdrv_flags;
1915 Error *err = NULL;
1917 blk = blk_by_name(device);
1918 if (!blk) {
1919 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1920 "Device '%s' not found", device);
1921 return;
1923 bs = blk_bs(blk);
1925 aio_context = bdrv_get_aio_context(bs);
1926 aio_context_acquire(aio_context);
1928 if (format) {
1929 drv = bdrv_find_whitelisted_format(format, bs->read_only);
1930 if (!drv) {
1931 error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
1932 goto out;
1936 eject_device(blk, 0, &err);
1937 if (err) {
1938 error_propagate(errp, err);
1939 goto out;
1942 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1943 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1945 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1947 out:
1948 aio_context_release(aio_context);
1951 /* throttling disk I/O limits */
1952 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1953 int64_t bps_wr,
1954 int64_t iops,
1955 int64_t iops_rd,
1956 int64_t iops_wr,
1957 bool has_bps_max,
1958 int64_t bps_max,
1959 bool has_bps_rd_max,
1960 int64_t bps_rd_max,
1961 bool has_bps_wr_max,
1962 int64_t bps_wr_max,
1963 bool has_iops_max,
1964 int64_t iops_max,
1965 bool has_iops_rd_max,
1966 int64_t iops_rd_max,
1967 bool has_iops_wr_max,
1968 int64_t iops_wr_max,
1969 bool has_iops_size,
1970 int64_t iops_size,
1971 bool has_group,
1972 const char *group, Error **errp)
1974 ThrottleConfig cfg;
1975 BlockDriverState *bs;
1976 BlockBackend *blk;
1977 AioContext *aio_context;
1979 blk = blk_by_name(device);
1980 if (!blk) {
1981 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1982 "Device '%s' not found", device);
1983 return;
1985 bs = blk_bs(blk);
1987 memset(&cfg, 0, sizeof(cfg));
1988 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1989 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
1990 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
1992 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1993 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
1994 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
1996 if (has_bps_max) {
1997 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
1999 if (has_bps_rd_max) {
2000 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
2002 if (has_bps_wr_max) {
2003 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2005 if (has_iops_max) {
2006 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2008 if (has_iops_rd_max) {
2009 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2011 if (has_iops_wr_max) {
2012 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2015 if (has_iops_size) {
2016 cfg.op_size = iops_size;
2019 if (!check_throttle_config(&cfg, errp)) {
2020 return;
2023 aio_context = bdrv_get_aio_context(bs);
2024 aio_context_acquire(aio_context);
2026 if (throttle_enabled(&cfg)) {
2027 /* Enable I/O limits if they're not enabled yet, otherwise
2028 * just update the throttling group. */
2029 if (!bs->io_limits_enabled) {
2030 bdrv_io_limits_enable(bs, has_group ? group : device);
2031 } else if (has_group) {
2032 bdrv_io_limits_update_group(bs, group);
2034 /* Set the new throttling configuration */
2035 bdrv_set_io_limits(bs, &cfg);
2036 } else if (bs->io_limits_enabled) {
2037 /* If all throttling settings are set to 0, disable I/O limits */
2038 bdrv_io_limits_disable(bs);
2041 aio_context_release(aio_context);
2044 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2045 bool has_granularity, uint32_t granularity,
2046 Error **errp)
2048 AioContext *aio_context;
2049 BlockDriverState *bs;
2051 if (!name || name[0] == '\0') {
2052 error_setg(errp, "Bitmap name cannot be empty");
2053 return;
2056 bs = bdrv_lookup_bs(node, node, errp);
2057 if (!bs) {
2058 return;
2061 aio_context = bdrv_get_aio_context(bs);
2062 aio_context_acquire(aio_context);
2064 if (has_granularity) {
2065 if (granularity < 512 || !is_power_of_2(granularity)) {
2066 error_setg(errp, "Granularity must be power of 2 "
2067 "and at least 512");
2068 goto out;
2070 } else {
2071 /* Default to cluster size, if available: */
2072 granularity = bdrv_get_default_bitmap_granularity(bs);
2075 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2077 out:
2078 aio_context_release(aio_context);
2081 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2082 Error **errp)
2084 AioContext *aio_context;
2085 BlockDriverState *bs;
2086 BdrvDirtyBitmap *bitmap;
2088 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2089 if (!bitmap || !bs) {
2090 return;
2093 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2094 error_setg(errp,
2095 "Bitmap '%s' is currently frozen and cannot be removed",
2096 name);
2097 goto out;
2099 bdrv_dirty_bitmap_make_anon(bitmap);
2100 bdrv_release_dirty_bitmap(bs, bitmap);
2102 out:
2103 aio_context_release(aio_context);
2107 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2108 * immediately after a full backup operation.
2110 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2111 Error **errp)
2113 AioContext *aio_context;
2114 BdrvDirtyBitmap *bitmap;
2115 BlockDriverState *bs;
2117 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2118 if (!bitmap || !bs) {
2119 return;
2122 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2123 error_setg(errp,
2124 "Bitmap '%s' is currently frozen and cannot be modified",
2125 name);
2126 goto out;
2127 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2128 error_setg(errp,
2129 "Bitmap '%s' is currently disabled and cannot be cleared",
2130 name);
2131 goto out;
2134 bdrv_clear_dirty_bitmap(bitmap);
2136 out:
2137 aio_context_release(aio_context);
2140 void hmp_drive_del(Monitor *mon, const QDict *qdict)
2142 const char *id = qdict_get_str(qdict, "id");
2143 BlockBackend *blk;
2144 BlockDriverState *bs;
2145 AioContext *aio_context;
2146 Error *local_err = NULL;
2148 blk = blk_by_name(id);
2149 if (!blk) {
2150 error_report("Device '%s' not found", id);
2151 return;
2153 bs = blk_bs(blk);
2155 if (!blk_legacy_dinfo(blk)) {
2156 error_report("Deleting device added with blockdev-add"
2157 " is not supported");
2158 return;
2161 aio_context = bdrv_get_aio_context(bs);
2162 aio_context_acquire(aio_context);
2164 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2165 error_report_err(local_err);
2166 aio_context_release(aio_context);
2167 return;
2170 /* quiesce block driver; prevent further io */
2171 bdrv_drain_all();
2172 bdrv_flush(bs);
2173 bdrv_close(bs);
2175 /* if we have a device attached to this BlockDriverState
2176 * then we need to make the drive anonymous until the device
2177 * can be removed. If this is a drive with no device backing
2178 * then we can just get rid of the block driver state right here.
2180 if (blk_get_attached_dev(blk)) {
2181 blk_hide_on_behalf_of_hmp_drive_del(blk);
2182 /* Further I/O must not pause the guest */
2183 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
2184 BLOCKDEV_ON_ERROR_REPORT);
2185 } else {
2186 blk_unref(blk);
2189 aio_context_release(aio_context);
2192 void qmp_block_resize(bool has_device, const char *device,
2193 bool has_node_name, const char *node_name,
2194 int64_t size, Error **errp)
2196 Error *local_err = NULL;
2197 BlockDriverState *bs;
2198 AioContext *aio_context;
2199 int ret;
2201 bs = bdrv_lookup_bs(has_device ? device : NULL,
2202 has_node_name ? node_name : NULL,
2203 &local_err);
2204 if (local_err) {
2205 error_propagate(errp, local_err);
2206 return;
2209 aio_context = bdrv_get_aio_context(bs);
2210 aio_context_acquire(aio_context);
2212 if (!bdrv_is_first_non_filter(bs)) {
2213 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2214 goto out;
2217 if (size < 0) {
2218 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2219 goto out;
2222 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2223 error_setg(errp, QERR_DEVICE_IN_USE, device);
2224 goto out;
2227 /* complete all in-flight operations before resizing the device */
2228 bdrv_drain_all();
2230 ret = bdrv_truncate(bs, size);
2231 switch (ret) {
2232 case 0:
2233 break;
2234 case -ENOMEDIUM:
2235 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2236 break;
2237 case -ENOTSUP:
2238 error_setg(errp, QERR_UNSUPPORTED);
2239 break;
2240 case -EACCES:
2241 error_setg(errp, "Device '%s' is read only", device);
2242 break;
2243 case -EBUSY:
2244 error_setg(errp, QERR_DEVICE_IN_USE, device);
2245 break;
2246 default:
2247 error_setg_errno(errp, -ret, "Could not resize");
2248 break;
2251 out:
2252 aio_context_release(aio_context);
2255 static void block_job_cb(void *opaque, int ret)
2257 /* Note that this function may be executed from another AioContext besides
2258 * the QEMU main loop. If you need to access anything that assumes the
2259 * QEMU global mutex, use a BH or introduce a mutex.
2262 BlockDriverState *bs = opaque;
2263 const char *msg = NULL;
2265 trace_block_job_cb(bs, bs->job, ret);
2267 assert(bs->job);
2269 if (ret < 0) {
2270 msg = strerror(-ret);
2273 if (block_job_is_cancelled(bs->job)) {
2274 block_job_event_cancelled(bs->job);
2275 } else {
2276 block_job_event_completed(bs->job, msg);
2279 bdrv_put_ref_bh_schedule(bs);
2282 void qmp_block_stream(const char *device,
2283 bool has_base, const char *base,
2284 bool has_backing_file, const char *backing_file,
2285 bool has_speed, int64_t speed,
2286 bool has_on_error, BlockdevOnError on_error,
2287 Error **errp)
2289 BlockBackend *blk;
2290 BlockDriverState *bs;
2291 BlockDriverState *base_bs = NULL;
2292 AioContext *aio_context;
2293 Error *local_err = NULL;
2294 const char *base_name = NULL;
2296 if (!has_on_error) {
2297 on_error = BLOCKDEV_ON_ERROR_REPORT;
2300 blk = blk_by_name(device);
2301 if (!blk) {
2302 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2303 "Device '%s' not found", device);
2304 return;
2306 bs = blk_bs(blk);
2308 aio_context = bdrv_get_aio_context(bs);
2309 aio_context_acquire(aio_context);
2311 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2312 goto out;
2315 if (has_base) {
2316 base_bs = bdrv_find_backing_image(bs, base);
2317 if (base_bs == NULL) {
2318 error_setg(errp, QERR_BASE_NOT_FOUND, base);
2319 goto out;
2321 assert(bdrv_get_aio_context(base_bs) == aio_context);
2322 base_name = base;
2325 /* if we are streaming the entire chain, the result will have no backing
2326 * file, and specifying one is therefore an error */
2327 if (base_bs == NULL && has_backing_file) {
2328 error_setg(errp, "backing file specified, but streaming the "
2329 "entire chain");
2330 goto out;
2333 /* backing_file string overrides base bs filename */
2334 base_name = has_backing_file ? backing_file : base_name;
2336 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
2337 on_error, block_job_cb, bs, &local_err);
2338 if (local_err) {
2339 error_propagate(errp, local_err);
2340 goto out;
2343 trace_qmp_block_stream(bs, bs->job);
2345 out:
2346 aio_context_release(aio_context);
2349 void qmp_block_commit(const char *device,
2350 bool has_base, const char *base,
2351 bool has_top, const char *top,
2352 bool has_backing_file, const char *backing_file,
2353 bool has_speed, int64_t speed,
2354 Error **errp)
2356 BlockBackend *blk;
2357 BlockDriverState *bs;
2358 BlockDriverState *base_bs, *top_bs;
2359 AioContext *aio_context;
2360 Error *local_err = NULL;
2361 /* This will be part of the QMP command, if/when the
2362 * BlockdevOnError change for blkmirror makes it in
2364 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
2366 if (!has_speed) {
2367 speed = 0;
2370 /* Important Note:
2371 * libvirt relies on the DeviceNotFound error class in order to probe for
2372 * live commit feature versions; for this to work, we must make sure to
2373 * perform the device lookup before any generic errors that may occur in a
2374 * scenario in which all optional arguments are omitted. */
2375 blk = blk_by_name(device);
2376 if (!blk) {
2377 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2378 "Device '%s' not found", device);
2379 return;
2381 bs = blk_bs(blk);
2383 aio_context = bdrv_get_aio_context(bs);
2384 aio_context_acquire(aio_context);
2386 /* drain all i/o before commits */
2387 bdrv_drain_all();
2389 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
2390 goto out;
2393 /* default top_bs is the active layer */
2394 top_bs = bs;
2396 if (has_top && top) {
2397 if (strcmp(bs->filename, top) != 0) {
2398 top_bs = bdrv_find_backing_image(bs, top);
2402 if (top_bs == NULL) {
2403 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
2404 goto out;
2407 assert(bdrv_get_aio_context(top_bs) == aio_context);
2409 if (has_base && base) {
2410 base_bs = bdrv_find_backing_image(top_bs, base);
2411 } else {
2412 base_bs = bdrv_find_base(top_bs);
2415 if (base_bs == NULL) {
2416 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
2417 goto out;
2420 assert(bdrv_get_aio_context(base_bs) == aio_context);
2422 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2423 goto out;
2426 /* Do not allow attempts to commit an image into itself */
2427 if (top_bs == base_bs) {
2428 error_setg(errp, "cannot commit an image into itself");
2429 goto out;
2432 if (top_bs == bs) {
2433 if (has_backing_file) {
2434 error_setg(errp, "'backing-file' specified,"
2435 " but 'top' is the active layer");
2436 goto out;
2438 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
2439 bs, &local_err);
2440 } else {
2441 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
2442 has_backing_file ? backing_file : NULL, &local_err);
2444 if (local_err != NULL) {
2445 error_propagate(errp, local_err);
2446 goto out;
2449 out:
2450 aio_context_release(aio_context);
2453 void qmp_drive_backup(const char *device, const char *target,
2454 bool has_format, const char *format,
2455 enum MirrorSyncMode sync,
2456 bool has_mode, enum NewImageMode mode,
2457 bool has_speed, int64_t speed,
2458 bool has_bitmap, const char *bitmap,
2459 bool has_on_source_error, BlockdevOnError on_source_error,
2460 bool has_on_target_error, BlockdevOnError on_target_error,
2461 Error **errp)
2463 BlockBackend *blk;
2464 BlockDriverState *bs;
2465 BlockDriverState *target_bs;
2466 BlockDriverState *source = NULL;
2467 BdrvDirtyBitmap *bmap = NULL;
2468 AioContext *aio_context;
2469 BlockDriver *drv = NULL;
2470 Error *local_err = NULL;
2471 int flags;
2472 int64_t size;
2473 int ret;
2475 if (!has_speed) {
2476 speed = 0;
2478 if (!has_on_source_error) {
2479 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2481 if (!has_on_target_error) {
2482 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2484 if (!has_mode) {
2485 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2488 blk = blk_by_name(device);
2489 if (!blk) {
2490 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2491 "Device '%s' not found", device);
2492 return;
2494 bs = blk_bs(blk);
2496 aio_context = bdrv_get_aio_context(bs);
2497 aio_context_acquire(aio_context);
2499 /* Although backup_run has this check too, we need to use bs->drv below, so
2500 * do an early check redundantly. */
2501 if (!bdrv_is_inserted(bs)) {
2502 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2503 goto out;
2506 if (!has_format) {
2507 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2509 if (format) {
2510 drv = bdrv_find_format(format);
2511 if (!drv) {
2512 error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
2513 goto out;
2517 /* Early check to avoid creating target */
2518 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
2519 goto out;
2522 flags = bs->open_flags | BDRV_O_RDWR;
2524 /* See if we have a backing HD we can use to create our new image
2525 * on top of. */
2526 if (sync == MIRROR_SYNC_MODE_TOP) {
2527 source = bs->backing_hd;
2528 if (!source) {
2529 sync = MIRROR_SYNC_MODE_FULL;
2532 if (sync == MIRROR_SYNC_MODE_NONE) {
2533 source = bs;
2536 size = bdrv_getlength(bs);
2537 if (size < 0) {
2538 error_setg_errno(errp, -size, "bdrv_getlength failed");
2539 goto out;
2542 if (mode != NEW_IMAGE_MODE_EXISTING) {
2543 assert(format && drv);
2544 if (source) {
2545 bdrv_img_create(target, format, source->filename,
2546 source->drv->format_name, NULL,
2547 size, flags, &local_err, false);
2548 } else {
2549 bdrv_img_create(target, format, NULL, NULL, NULL,
2550 size, flags, &local_err, false);
2554 if (local_err) {
2555 error_propagate(errp, local_err);
2556 goto out;
2559 target_bs = NULL;
2560 ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err);
2561 if (ret < 0) {
2562 error_propagate(errp, local_err);
2563 goto out;
2566 bdrv_set_aio_context(target_bs, aio_context);
2568 if (has_bitmap) {
2569 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
2570 if (!bmap) {
2571 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
2572 goto out;
2576 backup_start(bs, target_bs, speed, sync, bmap,
2577 on_source_error, on_target_error,
2578 block_job_cb, bs, &local_err);
2579 if (local_err != NULL) {
2580 bdrv_unref(target_bs);
2581 error_propagate(errp, local_err);
2582 goto out;
2585 out:
2586 aio_context_release(aio_context);
2589 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
2591 return bdrv_named_nodes_list(errp);
2594 void qmp_blockdev_backup(const char *device, const char *target,
2595 enum MirrorSyncMode sync,
2596 bool has_speed, int64_t speed,
2597 bool has_on_source_error,
2598 BlockdevOnError on_source_error,
2599 bool has_on_target_error,
2600 BlockdevOnError on_target_error,
2601 Error **errp)
2603 BlockBackend *blk;
2604 BlockDriverState *bs;
2605 BlockDriverState *target_bs;
2606 Error *local_err = NULL;
2607 AioContext *aio_context;
2609 if (!has_speed) {
2610 speed = 0;
2612 if (!has_on_source_error) {
2613 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2615 if (!has_on_target_error) {
2616 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2619 blk = blk_by_name(device);
2620 if (!blk) {
2621 error_setg(errp, "Device '%s' not found", device);
2622 return;
2624 bs = blk_bs(blk);
2626 aio_context = bdrv_get_aio_context(bs);
2627 aio_context_acquire(aio_context);
2629 blk = blk_by_name(target);
2630 if (!blk) {
2631 error_setg(errp, "Device '%s' not found", target);
2632 goto out;
2634 target_bs = blk_bs(blk);
2636 bdrv_ref(target_bs);
2637 bdrv_set_aio_context(target_bs, aio_context);
2638 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
2639 on_target_error, block_job_cb, bs, &local_err);
2640 if (local_err != NULL) {
2641 bdrv_unref(target_bs);
2642 error_propagate(errp, local_err);
2644 out:
2645 aio_context_release(aio_context);
2648 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2650 void qmp_drive_mirror(const char *device, const char *target,
2651 bool has_format, const char *format,
2652 bool has_node_name, const char *node_name,
2653 bool has_replaces, const char *replaces,
2654 enum MirrorSyncMode sync,
2655 bool has_mode, enum NewImageMode mode,
2656 bool has_speed, int64_t speed,
2657 bool has_granularity, uint32_t granularity,
2658 bool has_buf_size, int64_t buf_size,
2659 bool has_on_source_error, BlockdevOnError on_source_error,
2660 bool has_on_target_error, BlockdevOnError on_target_error,
2661 Error **errp)
2663 BlockBackend *blk;
2664 BlockDriverState *bs;
2665 BlockDriverState *source, *target_bs;
2666 AioContext *aio_context;
2667 BlockDriver *drv = NULL;
2668 Error *local_err = NULL;
2669 QDict *options = NULL;
2670 int flags;
2671 int64_t size;
2672 int ret;
2674 if (!has_speed) {
2675 speed = 0;
2677 if (!has_on_source_error) {
2678 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2680 if (!has_on_target_error) {
2681 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2683 if (!has_mode) {
2684 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2686 if (!has_granularity) {
2687 granularity = 0;
2689 if (!has_buf_size) {
2690 buf_size = DEFAULT_MIRROR_BUF_SIZE;
2693 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
2694 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2695 "a value in range [512B, 64MB]");
2696 return;
2698 if (granularity & (granularity - 1)) {
2699 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2700 "power of 2");
2701 return;
2704 blk = blk_by_name(device);
2705 if (!blk) {
2706 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2707 "Device '%s' not found", device);
2708 return;
2710 bs = blk_bs(blk);
2712 aio_context = bdrv_get_aio_context(bs);
2713 aio_context_acquire(aio_context);
2715 if (!bdrv_is_inserted(bs)) {
2716 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2717 goto out;
2720 if (!has_format) {
2721 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2723 if (format) {
2724 drv = bdrv_find_format(format);
2725 if (!drv) {
2726 error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
2727 goto out;
2731 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
2732 goto out;
2735 flags = bs->open_flags | BDRV_O_RDWR;
2736 source = bs->backing_hd;
2737 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
2738 sync = MIRROR_SYNC_MODE_FULL;
2740 if (sync == MIRROR_SYNC_MODE_NONE) {
2741 source = bs;
2744 size = bdrv_getlength(bs);
2745 if (size < 0) {
2746 error_setg_errno(errp, -size, "bdrv_getlength failed");
2747 goto out;
2750 if (has_replaces) {
2751 BlockDriverState *to_replace_bs;
2752 AioContext *replace_aio_context;
2753 int64_t replace_size;
2755 if (!has_node_name) {
2756 error_setg(errp, "a node-name must be provided when replacing a"
2757 " named node of the graph");
2758 goto out;
2761 to_replace_bs = check_to_replace_node(replaces, &local_err);
2763 if (!to_replace_bs) {
2764 error_propagate(errp, local_err);
2765 goto out;
2768 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
2769 aio_context_acquire(replace_aio_context);
2770 replace_size = bdrv_getlength(to_replace_bs);
2771 aio_context_release(replace_aio_context);
2773 if (size != replace_size) {
2774 error_setg(errp, "cannot replace image with a mirror image of "
2775 "different size");
2776 goto out;
2780 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
2781 && mode != NEW_IMAGE_MODE_EXISTING)
2783 /* create new image w/o backing file */
2784 assert(format && drv);
2785 bdrv_img_create(target, format,
2786 NULL, NULL, NULL, size, flags, &local_err, false);
2787 } else {
2788 switch (mode) {
2789 case NEW_IMAGE_MODE_EXISTING:
2790 break;
2791 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2792 /* create new image with backing file */
2793 bdrv_img_create(target, format,
2794 source->filename,
2795 source->drv->format_name,
2796 NULL, size, flags, &local_err, false);
2797 break;
2798 default:
2799 abort();
2803 if (local_err) {
2804 error_propagate(errp, local_err);
2805 goto out;
2808 if (has_node_name) {
2809 options = qdict_new();
2810 qdict_put(options, "node-name", qstring_from_str(node_name));
2813 /* Mirroring takes care of copy-on-write using the source's backing
2814 * file.
2816 target_bs = NULL;
2817 ret = bdrv_open(&target_bs, target, NULL, options,
2818 flags | BDRV_O_NO_BACKING, drv, &local_err);
2819 if (ret < 0) {
2820 error_propagate(errp, local_err);
2821 goto out;
2824 bdrv_set_aio_context(target_bs, aio_context);
2826 /* pass the node name to replace to mirror start since it's loose coupling
2827 * and will allow to check whether the node still exist at mirror completion
2829 mirror_start(bs, target_bs,
2830 has_replaces ? replaces : NULL,
2831 speed, granularity, buf_size, sync,
2832 on_source_error, on_target_error,
2833 block_job_cb, bs, &local_err);
2834 if (local_err != NULL) {
2835 bdrv_unref(target_bs);
2836 error_propagate(errp, local_err);
2837 goto out;
2840 out:
2841 aio_context_release(aio_context);
2844 /* Get the block job for a given device name and acquire its AioContext */
2845 static BlockJob *find_block_job(const char *device, AioContext **aio_context,
2846 Error **errp)
2848 BlockBackend *blk;
2849 BlockDriverState *bs;
2851 blk = blk_by_name(device);
2852 if (!blk) {
2853 goto notfound;
2855 bs = blk_bs(blk);
2857 *aio_context = bdrv_get_aio_context(bs);
2858 aio_context_acquire(*aio_context);
2860 if (!bs->job) {
2861 aio_context_release(*aio_context);
2862 goto notfound;
2865 return bs->job;
2867 notfound:
2868 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
2869 "No active block job on device '%s'", device);
2870 *aio_context = NULL;
2871 return NULL;
2874 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2876 AioContext *aio_context;
2877 BlockJob *job = find_block_job(device, &aio_context, errp);
2879 if (!job) {
2880 return;
2883 block_job_set_speed(job, speed, errp);
2884 aio_context_release(aio_context);
2887 void qmp_block_job_cancel(const char *device,
2888 bool has_force, bool force, Error **errp)
2890 AioContext *aio_context;
2891 BlockJob *job = find_block_job(device, &aio_context, errp);
2893 if (!job) {
2894 return;
2897 if (!has_force) {
2898 force = false;
2901 if (job->user_paused && !force) {
2902 error_setg(errp, "The block job for device '%s' is currently paused",
2903 device);
2904 goto out;
2907 trace_qmp_block_job_cancel(job);
2908 block_job_cancel(job);
2909 out:
2910 aio_context_release(aio_context);
2913 void qmp_block_job_pause(const char *device, Error **errp)
2915 AioContext *aio_context;
2916 BlockJob *job = find_block_job(device, &aio_context, errp);
2918 if (!job || job->user_paused) {
2919 return;
2922 job->user_paused = true;
2923 trace_qmp_block_job_pause(job);
2924 block_job_pause(job);
2925 aio_context_release(aio_context);
2928 void qmp_block_job_resume(const char *device, Error **errp)
2930 AioContext *aio_context;
2931 BlockJob *job = find_block_job(device, &aio_context, errp);
2933 if (!job || !job->user_paused) {
2934 return;
2937 job->user_paused = false;
2938 trace_qmp_block_job_resume(job);
2939 block_job_resume(job);
2940 aio_context_release(aio_context);
2943 void qmp_block_job_complete(const char *device, Error **errp)
2945 AioContext *aio_context;
2946 BlockJob *job = find_block_job(device, &aio_context, errp);
2948 if (!job) {
2949 return;
2952 trace_qmp_block_job_complete(job);
2953 block_job_complete(job, errp);
2954 aio_context_release(aio_context);
2957 void qmp_change_backing_file(const char *device,
2958 const char *image_node_name,
2959 const char *backing_file,
2960 Error **errp)
2962 BlockBackend *blk;
2963 BlockDriverState *bs = NULL;
2964 AioContext *aio_context;
2965 BlockDriverState *image_bs = NULL;
2966 Error *local_err = NULL;
2967 bool ro;
2968 int open_flags;
2969 int ret;
2971 blk = blk_by_name(device);
2972 if (!blk) {
2973 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2974 "Device '%s' not found", device);
2975 return;
2977 bs = blk_bs(blk);
2979 aio_context = bdrv_get_aio_context(bs);
2980 aio_context_acquire(aio_context);
2982 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
2983 if (local_err) {
2984 error_propagate(errp, local_err);
2985 goto out;
2988 if (!image_bs) {
2989 error_setg(errp, "image file not found");
2990 goto out;
2993 if (bdrv_find_base(image_bs) == image_bs) {
2994 error_setg(errp, "not allowing backing file change on an image "
2995 "without a backing file");
2996 goto out;
2999 /* even though we are not necessarily operating on bs, we need it to
3000 * determine if block ops are currently prohibited on the chain */
3001 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3002 goto out;
3005 /* final sanity check */
3006 if (!bdrv_chain_contains(bs, image_bs)) {
3007 error_setg(errp, "'%s' and image file are not in the same chain",
3008 device);
3009 goto out;
3012 /* if not r/w, reopen to make r/w */
3013 open_flags = image_bs->open_flags;
3014 ro = bdrv_is_read_only(image_bs);
3016 if (ro) {
3017 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3018 if (local_err) {
3019 error_propagate(errp, local_err);
3020 goto out;
3024 ret = bdrv_change_backing_file(image_bs, backing_file,
3025 image_bs->drv ? image_bs->drv->format_name : "");
3027 if (ret < 0) {
3028 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3029 backing_file);
3030 /* don't exit here, so we can try to restore open flags if
3031 * appropriate */
3034 if (ro) {
3035 bdrv_reopen(image_bs, open_flags, &local_err);
3036 if (local_err) {
3037 error_propagate(errp, local_err); /* will preserve prior errp */
3041 out:
3042 aio_context_release(aio_context);
3045 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3047 QmpOutputVisitor *ov = qmp_output_visitor_new();
3048 BlockBackend *blk;
3049 QObject *obj;
3050 QDict *qdict;
3051 Error *local_err = NULL;
3053 /* Require an ID in the top level */
3054 if (!options->has_id) {
3055 error_setg(errp, "Block device needs an ID");
3056 goto fail;
3059 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3060 * cache.direct=false instead of silently switching to aio=threads, except
3061 * when called from drive_new().
3063 * For now, simply forbidding the combination for all drivers will do. */
3064 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3065 bool direct = options->has_cache &&
3066 options->cache->has_direct &&
3067 options->cache->direct;
3068 if (!direct) {
3069 error_setg(errp, "aio=native requires cache.direct=true");
3070 goto fail;
3074 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
3075 &options, NULL, &local_err);
3076 if (local_err) {
3077 error_propagate(errp, local_err);
3078 goto fail;
3081 obj = qmp_output_get_qobject(ov);
3082 qdict = qobject_to_qdict(obj);
3084 qdict_flatten(qdict);
3086 blk = blockdev_init(NULL, qdict, &local_err);
3087 if (local_err) {
3088 error_propagate(errp, local_err);
3089 goto fail;
3092 if (bdrv_key_required(blk_bs(blk))) {
3093 blk_unref(blk);
3094 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3095 goto fail;
3098 fail:
3099 qmp_output_visitor_cleanup(ov);
3102 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3104 BlockJobInfoList *head = NULL, **p_next = &head;
3105 BlockDriverState *bs;
3107 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3108 AioContext *aio_context = bdrv_get_aio_context(bs);
3110 aio_context_acquire(aio_context);
3112 if (bs->job) {
3113 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3114 elem->value = block_job_query(bs->job);
3115 *p_next = elem;
3116 p_next = &elem->next;
3119 aio_context_release(aio_context);
3122 return head;
3125 QemuOptsList qemu_common_drive_opts = {
3126 .name = "drive",
3127 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3128 .desc = {
3130 .name = "snapshot",
3131 .type = QEMU_OPT_BOOL,
3132 .help = "enable/disable snapshot mode",
3134 .name = "discard",
3135 .type = QEMU_OPT_STRING,
3136 .help = "discard operation (ignore/off, unmap/on)",
3138 .name = BDRV_OPT_CACHE_WB,
3139 .type = QEMU_OPT_BOOL,
3140 .help = "enables writeback mode for any caches",
3142 .name = BDRV_OPT_CACHE_DIRECT,
3143 .type = QEMU_OPT_BOOL,
3144 .help = "enables use of O_DIRECT (bypass the host page cache)",
3146 .name = BDRV_OPT_CACHE_NO_FLUSH,
3147 .type = QEMU_OPT_BOOL,
3148 .help = "ignore any flush requests for the device",
3150 .name = "aio",
3151 .type = QEMU_OPT_STRING,
3152 .help = "host AIO implementation (threads, native)",
3154 .name = "format",
3155 .type = QEMU_OPT_STRING,
3156 .help = "disk format (raw, qcow2, ...)",
3158 .name = "rerror",
3159 .type = QEMU_OPT_STRING,
3160 .help = "read error action",
3162 .name = "werror",
3163 .type = QEMU_OPT_STRING,
3164 .help = "write error action",
3166 .name = "read-only",
3167 .type = QEMU_OPT_BOOL,
3168 .help = "open drive file as read-only",
3170 .name = "throttling.iops-total",
3171 .type = QEMU_OPT_NUMBER,
3172 .help = "limit total I/O operations per second",
3174 .name = "throttling.iops-read",
3175 .type = QEMU_OPT_NUMBER,
3176 .help = "limit read operations per second",
3178 .name = "throttling.iops-write",
3179 .type = QEMU_OPT_NUMBER,
3180 .help = "limit write operations per second",
3182 .name = "throttling.bps-total",
3183 .type = QEMU_OPT_NUMBER,
3184 .help = "limit total bytes per second",
3186 .name = "throttling.bps-read",
3187 .type = QEMU_OPT_NUMBER,
3188 .help = "limit read bytes per second",
3190 .name = "throttling.bps-write",
3191 .type = QEMU_OPT_NUMBER,
3192 .help = "limit write bytes per second",
3194 .name = "throttling.iops-total-max",
3195 .type = QEMU_OPT_NUMBER,
3196 .help = "I/O operations burst",
3198 .name = "throttling.iops-read-max",
3199 .type = QEMU_OPT_NUMBER,
3200 .help = "I/O operations read burst",
3202 .name = "throttling.iops-write-max",
3203 .type = QEMU_OPT_NUMBER,
3204 .help = "I/O operations write burst",
3206 .name = "throttling.bps-total-max",
3207 .type = QEMU_OPT_NUMBER,
3208 .help = "total bytes burst",
3210 .name = "throttling.bps-read-max",
3211 .type = QEMU_OPT_NUMBER,
3212 .help = "total bytes read burst",
3214 .name = "throttling.bps-write-max",
3215 .type = QEMU_OPT_NUMBER,
3216 .help = "total bytes write burst",
3218 .name = "throttling.iops-size",
3219 .type = QEMU_OPT_NUMBER,
3220 .help = "when limiting by iops max size of an I/O in bytes",
3222 .name = "throttling.group",
3223 .type = QEMU_OPT_STRING,
3224 .help = "name of the block throttling group",
3226 .name = "copy-on-read",
3227 .type = QEMU_OPT_BOOL,
3228 .help = "copy read data from backing file into image file",
3230 .name = "detect-zeroes",
3231 .type = QEMU_OPT_STRING,
3232 .help = "try to optimize zero writes (off, on, unmap)",
3234 { /* end of list */ }
3238 QemuOptsList qemu_drive_opts = {
3239 .name = "drive",
3240 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3241 .desc = {
3243 * no elements => accept any params
3244 * validation will happen later
3246 { /* end of list */ }