configure: qemu-ga: report MSI install support in summary
[qemu.git] / blockdev.c
blob4125ff642ad08f588f9b9e0d3c0c537ffd2db8cf
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 if (throttle_max_is_missing_limit(cfg)) {
341 error_setg(errp, "bps_max/iops_max require corresponding"
342 " bps/iops values");
343 return false;
346 return true;
349 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
351 /* Takes the ownership of bs_opts */
352 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
353 Error **errp)
355 const char *buf;
356 int ro = 0;
357 int bdrv_flags = 0;
358 int on_read_error, on_write_error;
359 BlockBackend *blk;
360 BlockDriverState *bs;
361 ThrottleConfig cfg;
362 int snapshot = 0;
363 bool copy_on_read;
364 Error *error = NULL;
365 QemuOpts *opts;
366 const char *id;
367 bool has_driver_specific_opts;
368 BlockdevDetectZeroesOptions detect_zeroes;
369 const char *throttling_group;
371 /* Check common options by copying from bs_opts to opts, all other options
372 * stay in bs_opts for processing by bdrv_open(). */
373 id = qdict_get_try_str(bs_opts, "id");
374 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
375 if (error) {
376 error_propagate(errp, error);
377 goto err_no_opts;
380 qemu_opts_absorb_qdict(opts, bs_opts, &error);
381 if (error) {
382 error_propagate(errp, error);
383 goto early_err;
386 if (id) {
387 qdict_del(bs_opts, "id");
390 has_driver_specific_opts = !!qdict_size(bs_opts);
392 /* extract parameters */
393 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
394 ro = qemu_opt_get_bool(opts, "read-only", 0);
395 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
397 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
398 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
399 error_setg(errp, "invalid discard option");
400 goto early_err;
404 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) {
405 bdrv_flags |= BDRV_O_CACHE_WB;
407 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
408 bdrv_flags |= BDRV_O_NOCACHE;
410 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
411 bdrv_flags |= BDRV_O_NO_FLUSH;
414 #ifdef CONFIG_LINUX_AIO
415 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
416 if (!strcmp(buf, "native")) {
417 bdrv_flags |= BDRV_O_NATIVE_AIO;
418 } else if (!strcmp(buf, "threads")) {
419 /* this is the default */
420 } else {
421 error_setg(errp, "invalid aio option");
422 goto early_err;
425 #endif
427 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
428 if (is_help_option(buf)) {
429 error_printf("Supported formats:");
430 bdrv_iterate_format(bdrv_format_print, NULL);
431 error_printf("\n");
432 goto early_err;
435 if (qdict_haskey(bs_opts, "driver")) {
436 error_setg(errp, "Cannot specify both 'driver' and 'format'");
437 goto early_err;
439 qdict_put(bs_opts, "driver", qstring_from_str(buf));
442 /* disk I/O throttling */
443 memset(&cfg, 0, sizeof(cfg));
444 cfg.buckets[THROTTLE_BPS_TOTAL].avg =
445 qemu_opt_get_number(opts, "throttling.bps-total", 0);
446 cfg.buckets[THROTTLE_BPS_READ].avg =
447 qemu_opt_get_number(opts, "throttling.bps-read", 0);
448 cfg.buckets[THROTTLE_BPS_WRITE].avg =
449 qemu_opt_get_number(opts, "throttling.bps-write", 0);
450 cfg.buckets[THROTTLE_OPS_TOTAL].avg =
451 qemu_opt_get_number(opts, "throttling.iops-total", 0);
452 cfg.buckets[THROTTLE_OPS_READ].avg =
453 qemu_opt_get_number(opts, "throttling.iops-read", 0);
454 cfg.buckets[THROTTLE_OPS_WRITE].avg =
455 qemu_opt_get_number(opts, "throttling.iops-write", 0);
457 cfg.buckets[THROTTLE_BPS_TOTAL].max =
458 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
459 cfg.buckets[THROTTLE_BPS_READ].max =
460 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
461 cfg.buckets[THROTTLE_BPS_WRITE].max =
462 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
463 cfg.buckets[THROTTLE_OPS_TOTAL].max =
464 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
465 cfg.buckets[THROTTLE_OPS_READ].max =
466 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
467 cfg.buckets[THROTTLE_OPS_WRITE].max =
468 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
470 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
472 throttling_group = qemu_opt_get(opts, "throttling.group");
474 if (!check_throttle_config(&cfg, &error)) {
475 error_propagate(errp, error);
476 goto early_err;
479 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
480 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
481 on_write_error = parse_block_error_action(buf, 0, &error);
482 if (error) {
483 error_propagate(errp, error);
484 goto early_err;
488 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
489 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
490 on_read_error = parse_block_error_action(buf, 1, &error);
491 if (error) {
492 error_propagate(errp, error);
493 goto early_err;
497 detect_zeroes =
498 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
499 qemu_opt_get(opts, "detect-zeroes"),
500 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
501 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
502 &error);
503 if (error) {
504 error_propagate(errp, error);
505 goto early_err;
508 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
509 !(bdrv_flags & BDRV_O_UNMAP)) {
510 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
511 "without setting discard operation to unmap");
512 goto early_err;
515 /* init */
516 if ((!file || !*file) && !has_driver_specific_opts) {
517 blk = blk_new_with_bs(qemu_opts_id(opts), errp);
518 if (!blk) {
519 goto early_err;
522 bs = blk_bs(blk);
523 bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
524 bs->read_only = ro;
526 QDECREF(bs_opts);
527 } else {
528 if (file && !*file) {
529 file = NULL;
532 if (snapshot) {
533 /* always use cache=unsafe with snapshot */
534 bdrv_flags &= ~BDRV_O_CACHE_MASK;
535 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
538 if (copy_on_read) {
539 bdrv_flags |= BDRV_O_COPY_ON_READ;
542 if (runstate_check(RUN_STATE_INMIGRATE)) {
543 bdrv_flags |= BDRV_O_INCOMING;
546 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
548 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
549 errp);
550 if (!blk) {
551 goto err_no_bs_opts;
553 bs = blk_bs(blk);
556 bs->detect_zeroes = detect_zeroes;
558 bdrv_set_on_error(bs, on_read_error, on_write_error);
560 /* disk I/O throttling */
561 if (throttle_enabled(&cfg)) {
562 if (!throttling_group) {
563 throttling_group = blk_name(blk);
565 bdrv_io_limits_enable(bs, throttling_group);
566 bdrv_set_io_limits(bs, &cfg);
569 if (bdrv_key_required(bs)) {
570 autostart = 0;
573 err_no_bs_opts:
574 qemu_opts_del(opts);
575 return blk;
577 early_err:
578 qemu_opts_del(opts);
579 err_no_opts:
580 QDECREF(bs_opts);
581 return NULL;
584 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
585 Error **errp)
587 const char *value;
589 value = qemu_opt_get(opts, from);
590 if (value) {
591 if (qemu_opt_find(opts, to)) {
592 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
593 "same time", to, from);
594 return;
598 /* rename all items in opts */
599 while ((value = qemu_opt_get(opts, from))) {
600 qemu_opt_set(opts, to, value, &error_abort);
601 qemu_opt_unset(opts, from);
605 QemuOptsList qemu_legacy_drive_opts = {
606 .name = "drive",
607 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
608 .desc = {
610 .name = "bus",
611 .type = QEMU_OPT_NUMBER,
612 .help = "bus number",
614 .name = "unit",
615 .type = QEMU_OPT_NUMBER,
616 .help = "unit number (i.e. lun for scsi)",
618 .name = "index",
619 .type = QEMU_OPT_NUMBER,
620 .help = "index number",
622 .name = "media",
623 .type = QEMU_OPT_STRING,
624 .help = "media type (disk, cdrom)",
626 .name = "if",
627 .type = QEMU_OPT_STRING,
628 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
630 .name = "cyls",
631 .type = QEMU_OPT_NUMBER,
632 .help = "number of cylinders (ide disk geometry)",
634 .name = "heads",
635 .type = QEMU_OPT_NUMBER,
636 .help = "number of heads (ide disk geometry)",
638 .name = "secs",
639 .type = QEMU_OPT_NUMBER,
640 .help = "number of sectors (ide disk geometry)",
642 .name = "trans",
643 .type = QEMU_OPT_STRING,
644 .help = "chs translation (auto, lba, none)",
646 .name = "boot",
647 .type = QEMU_OPT_BOOL,
648 .help = "(deprecated, ignored)",
650 .name = "addr",
651 .type = QEMU_OPT_STRING,
652 .help = "pci address (virtio only)",
654 .name = "serial",
655 .type = QEMU_OPT_STRING,
656 .help = "disk serial number",
658 .name = "file",
659 .type = QEMU_OPT_STRING,
660 .help = "file name",
663 /* Options that are passed on, but have special semantics with -drive */
665 .name = "read-only",
666 .type = QEMU_OPT_BOOL,
667 .help = "open drive file as read-only",
669 .name = "rerror",
670 .type = QEMU_OPT_STRING,
671 .help = "read error action",
673 .name = "werror",
674 .type = QEMU_OPT_STRING,
675 .help = "write error action",
677 .name = "copy-on-read",
678 .type = QEMU_OPT_BOOL,
679 .help = "copy read data from backing file into image file",
682 { /* end of list */ }
686 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
688 const char *value;
689 BlockBackend *blk;
690 DriveInfo *dinfo = NULL;
691 QDict *bs_opts;
692 QemuOpts *legacy_opts;
693 DriveMediaType media = MEDIA_DISK;
694 BlockInterfaceType type;
695 int cyls, heads, secs, translation;
696 int max_devs, bus_id, unit_id, index;
697 const char *devaddr;
698 const char *werror, *rerror;
699 bool read_only = false;
700 bool copy_on_read;
701 const char *serial;
702 const char *filename;
703 Error *local_err = NULL;
704 int i;
706 /* Change legacy command line options into QMP ones */
707 static const struct {
708 const char *from;
709 const char *to;
710 } opt_renames[] = {
711 { "iops", "throttling.iops-total" },
712 { "iops_rd", "throttling.iops-read" },
713 { "iops_wr", "throttling.iops-write" },
715 { "bps", "throttling.bps-total" },
716 { "bps_rd", "throttling.bps-read" },
717 { "bps_wr", "throttling.bps-write" },
719 { "iops_max", "throttling.iops-total-max" },
720 { "iops_rd_max", "throttling.iops-read-max" },
721 { "iops_wr_max", "throttling.iops-write-max" },
723 { "bps_max", "throttling.bps-total-max" },
724 { "bps_rd_max", "throttling.bps-read-max" },
725 { "bps_wr_max", "throttling.bps-write-max" },
727 { "iops_size", "throttling.iops-size" },
729 { "group", "throttling.group" },
731 { "readonly", "read-only" },
734 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
735 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
736 &local_err);
737 if (local_err) {
738 error_report_err(local_err);
739 return NULL;
743 value = qemu_opt_get(all_opts, "cache");
744 if (value) {
745 int flags = 0;
747 if (bdrv_parse_cache_flags(value, &flags) != 0) {
748 error_report("invalid cache option");
749 return NULL;
752 /* Specific options take precedence */
753 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
754 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
755 !!(flags & BDRV_O_CACHE_WB), &error_abort);
757 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
758 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
759 !!(flags & BDRV_O_NOCACHE), &error_abort);
761 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
762 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
763 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
765 qemu_opt_unset(all_opts, "cache");
768 /* Get a QDict for processing the options */
769 bs_opts = qdict_new();
770 qemu_opts_to_qdict(all_opts, bs_opts);
772 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
773 &error_abort);
774 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
775 if (local_err) {
776 error_report_err(local_err);
777 goto fail;
780 /* Deprecated option boot=[on|off] */
781 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
782 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
783 "ignored. Future versions will reject this parameter. Please "
784 "update your scripts.\n");
787 /* Media type */
788 value = qemu_opt_get(legacy_opts, "media");
789 if (value) {
790 if (!strcmp(value, "disk")) {
791 media = MEDIA_DISK;
792 } else if (!strcmp(value, "cdrom")) {
793 media = MEDIA_CDROM;
794 read_only = true;
795 } else {
796 error_report("'%s' invalid media", value);
797 goto fail;
801 /* copy-on-read is disabled with a warning for read-only devices */
802 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
803 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
805 if (read_only && copy_on_read) {
806 error_report("warning: disabling copy-on-read on read-only drive");
807 copy_on_read = false;
810 qdict_put(bs_opts, "read-only",
811 qstring_from_str(read_only ? "on" : "off"));
812 qdict_put(bs_opts, "copy-on-read",
813 qstring_from_str(copy_on_read ? "on" :"off"));
815 /* Controller type */
816 value = qemu_opt_get(legacy_opts, "if");
817 if (value) {
818 for (type = 0;
819 type < IF_COUNT && strcmp(value, if_name[type]);
820 type++) {
822 if (type == IF_COUNT) {
823 error_report("unsupported bus type '%s'", value);
824 goto fail;
826 } else {
827 type = block_default_type;
830 /* Geometry */
831 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
832 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
833 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
835 if (cyls || heads || secs) {
836 if (cyls < 1) {
837 error_report("invalid physical cyls number");
838 goto fail;
840 if (heads < 1) {
841 error_report("invalid physical heads number");
842 goto fail;
844 if (secs < 1) {
845 error_report("invalid physical secs number");
846 goto fail;
850 translation = BIOS_ATA_TRANSLATION_AUTO;
851 value = qemu_opt_get(legacy_opts, "trans");
852 if (value != NULL) {
853 if (!cyls) {
854 error_report("'%s' trans must be used with cyls, heads and secs",
855 value);
856 goto fail;
858 if (!strcmp(value, "none")) {
859 translation = BIOS_ATA_TRANSLATION_NONE;
860 } else if (!strcmp(value, "lba")) {
861 translation = BIOS_ATA_TRANSLATION_LBA;
862 } else if (!strcmp(value, "large")) {
863 translation = BIOS_ATA_TRANSLATION_LARGE;
864 } else if (!strcmp(value, "rechs")) {
865 translation = BIOS_ATA_TRANSLATION_RECHS;
866 } else if (!strcmp(value, "auto")) {
867 translation = BIOS_ATA_TRANSLATION_AUTO;
868 } else {
869 error_report("'%s' invalid translation type", value);
870 goto fail;
874 if (media == MEDIA_CDROM) {
875 if (cyls || secs || heads) {
876 error_report("CHS can't be set with media=cdrom");
877 goto fail;
881 /* Device address specified by bus/unit or index.
882 * If none was specified, try to find the first free one. */
883 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
884 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
885 index = qemu_opt_get_number(legacy_opts, "index", -1);
887 max_devs = if_max_devs[type];
889 if (index != -1) {
890 if (bus_id != 0 || unit_id != -1) {
891 error_report("index cannot be used with bus and unit");
892 goto fail;
894 bus_id = drive_index_to_bus_id(type, index);
895 unit_id = drive_index_to_unit_id(type, index);
898 if (unit_id == -1) {
899 unit_id = 0;
900 while (drive_get(type, bus_id, unit_id) != NULL) {
901 unit_id++;
902 if (max_devs && unit_id >= max_devs) {
903 unit_id -= max_devs;
904 bus_id++;
909 if (max_devs && unit_id >= max_devs) {
910 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
911 goto fail;
914 if (drive_get(type, bus_id, unit_id) != NULL) {
915 error_report("drive with bus=%d, unit=%d (index=%d) exists",
916 bus_id, unit_id, index);
917 goto fail;
920 /* Serial number */
921 serial = qemu_opt_get(legacy_opts, "serial");
923 /* no id supplied -> create one */
924 if (qemu_opts_id(all_opts) == NULL) {
925 char *new_id;
926 const char *mediastr = "";
927 if (type == IF_IDE || type == IF_SCSI) {
928 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
930 if (max_devs) {
931 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
932 mediastr, unit_id);
933 } else {
934 new_id = g_strdup_printf("%s%s%i", if_name[type],
935 mediastr, unit_id);
937 qdict_put(bs_opts, "id", qstring_from_str(new_id));
938 g_free(new_id);
941 /* Add virtio block device */
942 devaddr = qemu_opt_get(legacy_opts, "addr");
943 if (devaddr && type != IF_VIRTIO) {
944 error_report("addr is not supported by this bus type");
945 goto fail;
948 if (type == IF_VIRTIO) {
949 QemuOpts *devopts;
950 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
951 &error_abort);
952 if (arch_type == QEMU_ARCH_S390X) {
953 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
954 } else {
955 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
957 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
958 &error_abort);
959 if (devaddr) {
960 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
964 filename = qemu_opt_get(legacy_opts, "file");
966 /* Check werror/rerror compatibility with if=... */
967 werror = qemu_opt_get(legacy_opts, "werror");
968 if (werror != NULL) {
969 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
970 type != IF_NONE) {
971 error_report("werror is not supported by this bus type");
972 goto fail;
974 qdict_put(bs_opts, "werror", qstring_from_str(werror));
977 rerror = qemu_opt_get(legacy_opts, "rerror");
978 if (rerror != NULL) {
979 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
980 type != IF_NONE) {
981 error_report("rerror is not supported by this bus type");
982 goto fail;
984 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
987 /* Actual block device init: Functionality shared with blockdev-add */
988 blk = blockdev_init(filename, bs_opts, &local_err);
989 bs_opts = NULL;
990 if (!blk) {
991 if (local_err) {
992 error_report_err(local_err);
994 goto fail;
995 } else {
996 assert(!local_err);
999 /* Create legacy DriveInfo */
1000 dinfo = g_malloc0(sizeof(*dinfo));
1001 dinfo->opts = all_opts;
1003 dinfo->cyls = cyls;
1004 dinfo->heads = heads;
1005 dinfo->secs = secs;
1006 dinfo->trans = translation;
1008 dinfo->type = type;
1009 dinfo->bus = bus_id;
1010 dinfo->unit = unit_id;
1011 dinfo->devaddr = devaddr;
1012 dinfo->serial = g_strdup(serial);
1014 blk_set_legacy_dinfo(blk, dinfo);
1016 switch(type) {
1017 case IF_IDE:
1018 case IF_SCSI:
1019 case IF_XEN:
1020 case IF_NONE:
1021 dinfo->media_cd = media == MEDIA_CDROM;
1022 break;
1023 default:
1024 break;
1027 fail:
1028 qemu_opts_del(legacy_opts);
1029 QDECREF(bs_opts);
1030 return dinfo;
1033 void hmp_commit(Monitor *mon, const QDict *qdict)
1035 const char *device = qdict_get_str(qdict, "device");
1036 BlockBackend *blk;
1037 int ret;
1039 if (!strcmp(device, "all")) {
1040 ret = bdrv_commit_all();
1041 } else {
1042 blk = blk_by_name(device);
1043 if (!blk) {
1044 monitor_printf(mon, "Device '%s' not found\n", device);
1045 return;
1047 ret = bdrv_commit(blk_bs(blk));
1049 if (ret < 0) {
1050 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1051 strerror(-ret));
1055 static void blockdev_do_action(int kind, void *data, Error **errp)
1057 TransactionAction action;
1058 TransactionActionList list;
1060 action.kind = kind;
1061 action.data = data;
1062 list.value = &action;
1063 list.next = NULL;
1064 qmp_transaction(&list, errp);
1067 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1068 bool has_node_name, const char *node_name,
1069 const char *snapshot_file,
1070 bool has_snapshot_node_name,
1071 const char *snapshot_node_name,
1072 bool has_format, const char *format,
1073 bool has_mode, NewImageMode mode, Error **errp)
1075 BlockdevSnapshot snapshot = {
1076 .has_device = has_device,
1077 .device = (char *) device,
1078 .has_node_name = has_node_name,
1079 .node_name = (char *) node_name,
1080 .snapshot_file = (char *) snapshot_file,
1081 .has_snapshot_node_name = has_snapshot_node_name,
1082 .snapshot_node_name = (char *) snapshot_node_name,
1083 .has_format = has_format,
1084 .format = (char *) format,
1085 .has_mode = has_mode,
1086 .mode = mode,
1088 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1089 &snapshot, errp);
1092 void qmp_blockdev_snapshot_internal_sync(const char *device,
1093 const char *name,
1094 Error **errp)
1096 BlockdevSnapshotInternal snapshot = {
1097 .device = (char *) device,
1098 .name = (char *) name
1101 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1102 &snapshot, errp);
1105 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1106 bool has_id,
1107 const char *id,
1108 bool has_name,
1109 const char *name,
1110 Error **errp)
1112 BlockDriverState *bs;
1113 BlockBackend *blk;
1114 AioContext *aio_context;
1115 QEMUSnapshotInfo sn;
1116 Error *local_err = NULL;
1117 SnapshotInfo *info = NULL;
1118 int ret;
1120 blk = blk_by_name(device);
1121 if (!blk) {
1122 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1123 "Device '%s' not found", device);
1124 return NULL;
1126 bs = blk_bs(blk);
1128 if (!has_id) {
1129 id = NULL;
1132 if (!has_name) {
1133 name = NULL;
1136 if (!id && !name) {
1137 error_setg(errp, "Name or id must be provided");
1138 return NULL;
1141 aio_context = bdrv_get_aio_context(bs);
1142 aio_context_acquire(aio_context);
1144 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1145 goto out_aio_context;
1148 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1149 if (local_err) {
1150 error_propagate(errp, local_err);
1151 goto out_aio_context;
1153 if (!ret) {
1154 error_setg(errp,
1155 "Snapshot with id '%s' and name '%s' does not exist on "
1156 "device '%s'",
1157 STR_OR_NULL(id), STR_OR_NULL(name), device);
1158 goto out_aio_context;
1161 bdrv_snapshot_delete(bs, id, name, &local_err);
1162 if (local_err) {
1163 error_propagate(errp, local_err);
1164 goto out_aio_context;
1167 aio_context_release(aio_context);
1169 info = g_new0(SnapshotInfo, 1);
1170 info->id = g_strdup(sn.id_str);
1171 info->name = g_strdup(sn.name);
1172 info->date_nsec = sn.date_nsec;
1173 info->date_sec = sn.date_sec;
1174 info->vm_state_size = sn.vm_state_size;
1175 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1176 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1178 return info;
1180 out_aio_context:
1181 aio_context_release(aio_context);
1182 return NULL;
1186 * block_dirty_bitmap_lookup:
1187 * Return a dirty bitmap (if present), after validating
1188 * the node reference and bitmap names.
1190 * @node: The name of the BDS node to search for bitmaps
1191 * @name: The name of the bitmap to search for
1192 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1193 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1194 * @errp: Output pointer for error information. Can be NULL.
1196 * @return: A bitmap object on success, or NULL on failure.
1198 static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1199 const char *name,
1200 BlockDriverState **pbs,
1201 AioContext **paio,
1202 Error **errp)
1204 BlockDriverState *bs;
1205 BdrvDirtyBitmap *bitmap;
1206 AioContext *aio_context;
1208 if (!node) {
1209 error_setg(errp, "Node cannot be NULL");
1210 return NULL;
1212 if (!name) {
1213 error_setg(errp, "Bitmap name cannot be NULL");
1214 return NULL;
1216 bs = bdrv_lookup_bs(node, node, NULL);
1217 if (!bs) {
1218 error_setg(errp, "Node '%s' not found", node);
1219 return NULL;
1222 aio_context = bdrv_get_aio_context(bs);
1223 aio_context_acquire(aio_context);
1225 bitmap = bdrv_find_dirty_bitmap(bs, name);
1226 if (!bitmap) {
1227 error_setg(errp, "Dirty bitmap '%s' not found", name);
1228 goto fail;
1231 if (pbs) {
1232 *pbs = bs;
1234 if (paio) {
1235 *paio = aio_context;
1236 } else {
1237 aio_context_release(aio_context);
1240 return bitmap;
1242 fail:
1243 aio_context_release(aio_context);
1244 return NULL;
1247 /* New and old BlockDriverState structs for atomic group operations */
1249 typedef struct BlkTransactionState BlkTransactionState;
1251 /* Only prepare() may fail. In a single transaction, only one of commit() or
1252 abort() will be called, clean() will always be called if it present. */
1253 typedef struct BdrvActionOps {
1254 /* Size of state struct, in bytes. */
1255 size_t instance_size;
1256 /* Prepare the work, must NOT be NULL. */
1257 void (*prepare)(BlkTransactionState *common, Error **errp);
1258 /* Commit the changes, can be NULL. */
1259 void (*commit)(BlkTransactionState *common);
1260 /* Abort the changes on fail, can be NULL. */
1261 void (*abort)(BlkTransactionState *common);
1262 /* Clean up resource in the end, can be NULL. */
1263 void (*clean)(BlkTransactionState *common);
1264 } BdrvActionOps;
1267 * This structure must be arranged as first member in child type, assuming
1268 * that compiler will also arrange it to the same address with parent instance.
1269 * Later it will be used in free().
1271 struct BlkTransactionState {
1272 TransactionAction *action;
1273 const BdrvActionOps *ops;
1274 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
1277 /* internal snapshot private data */
1278 typedef struct InternalSnapshotState {
1279 BlkTransactionState common;
1280 BlockDriverState *bs;
1281 AioContext *aio_context;
1282 QEMUSnapshotInfo sn;
1283 } InternalSnapshotState;
1285 static void internal_snapshot_prepare(BlkTransactionState *common,
1286 Error **errp)
1288 Error *local_err = NULL;
1289 const char *device;
1290 const char *name;
1291 BlockBackend *blk;
1292 BlockDriverState *bs;
1293 QEMUSnapshotInfo old_sn, *sn;
1294 bool ret;
1295 qemu_timeval tv;
1296 BlockdevSnapshotInternal *internal;
1297 InternalSnapshotState *state;
1298 int ret1;
1300 g_assert(common->action->kind ==
1301 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1302 internal = common->action->blockdev_snapshot_internal_sync;
1303 state = DO_UPCAST(InternalSnapshotState, common, common);
1305 /* 1. parse input */
1306 device = internal->device;
1307 name = internal->name;
1309 /* 2. check for validation */
1310 blk = blk_by_name(device);
1311 if (!blk) {
1312 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1313 "Device '%s' not found", device);
1314 return;
1316 bs = blk_bs(blk);
1318 /* AioContext is released in .clean() */
1319 state->aio_context = bdrv_get_aio_context(bs);
1320 aio_context_acquire(state->aio_context);
1322 if (!bdrv_is_inserted(bs)) {
1323 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1324 return;
1327 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1328 return;
1331 if (bdrv_is_read_only(bs)) {
1332 error_setg(errp, "Device '%s' is read only", device);
1333 return;
1336 if (!bdrv_can_snapshot(bs)) {
1337 error_setg(errp, "Block format '%s' used by device '%s' "
1338 "does not support internal snapshots",
1339 bs->drv->format_name, device);
1340 return;
1343 if (!strlen(name)) {
1344 error_setg(errp, "Name is empty");
1345 return;
1348 /* check whether a snapshot with name exist */
1349 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1350 &local_err);
1351 if (local_err) {
1352 error_propagate(errp, local_err);
1353 return;
1354 } else if (ret) {
1355 error_setg(errp,
1356 "Snapshot with name '%s' already exists on device '%s'",
1357 name, device);
1358 return;
1361 /* 3. take the snapshot */
1362 sn = &state->sn;
1363 pstrcpy(sn->name, sizeof(sn->name), name);
1364 qemu_gettimeofday(&tv);
1365 sn->date_sec = tv.tv_sec;
1366 sn->date_nsec = tv.tv_usec * 1000;
1367 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1369 ret1 = bdrv_snapshot_create(bs, sn);
1370 if (ret1 < 0) {
1371 error_setg_errno(errp, -ret1,
1372 "Failed to create snapshot '%s' on device '%s'",
1373 name, device);
1374 return;
1377 /* 4. succeed, mark a snapshot is created */
1378 state->bs = bs;
1381 static void internal_snapshot_abort(BlkTransactionState *common)
1383 InternalSnapshotState *state =
1384 DO_UPCAST(InternalSnapshotState, common, common);
1385 BlockDriverState *bs = state->bs;
1386 QEMUSnapshotInfo *sn = &state->sn;
1387 Error *local_error = NULL;
1389 if (!bs) {
1390 return;
1393 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1394 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1395 "device '%s' in abort: %s",
1396 sn->id_str,
1397 sn->name,
1398 bdrv_get_device_name(bs),
1399 error_get_pretty(local_error));
1400 error_free(local_error);
1404 static void internal_snapshot_clean(BlkTransactionState *common)
1406 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1407 common, common);
1409 if (state->aio_context) {
1410 aio_context_release(state->aio_context);
1414 /* external snapshot private data */
1415 typedef struct ExternalSnapshotState {
1416 BlkTransactionState common;
1417 BlockDriverState *old_bs;
1418 BlockDriverState *new_bs;
1419 AioContext *aio_context;
1420 } ExternalSnapshotState;
1422 static void external_snapshot_prepare(BlkTransactionState *common,
1423 Error **errp)
1425 BlockDriver *drv;
1426 int flags, ret;
1427 QDict *options = NULL;
1428 Error *local_err = NULL;
1429 bool has_device = false;
1430 const char *device;
1431 bool has_node_name = false;
1432 const char *node_name;
1433 bool has_snapshot_node_name = false;
1434 const char *snapshot_node_name;
1435 const char *new_image_file;
1436 const char *format = "qcow2";
1437 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1438 ExternalSnapshotState *state =
1439 DO_UPCAST(ExternalSnapshotState, common, common);
1440 TransactionAction *action = common->action;
1442 /* get parameters */
1443 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
1445 has_device = action->blockdev_snapshot_sync->has_device;
1446 device = action->blockdev_snapshot_sync->device;
1447 has_node_name = action->blockdev_snapshot_sync->has_node_name;
1448 node_name = action->blockdev_snapshot_sync->node_name;
1449 has_snapshot_node_name =
1450 action->blockdev_snapshot_sync->has_snapshot_node_name;
1451 snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name;
1453 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1454 if (action->blockdev_snapshot_sync->has_format) {
1455 format = action->blockdev_snapshot_sync->format;
1457 if (action->blockdev_snapshot_sync->has_mode) {
1458 mode = action->blockdev_snapshot_sync->mode;
1461 /* start processing */
1462 drv = bdrv_find_format(format);
1463 if (!drv) {
1464 error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
1465 return;
1468 state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
1469 has_node_name ? node_name : NULL,
1470 &local_err);
1471 if (local_err) {
1472 error_propagate(errp, local_err);
1473 return;
1476 if (has_node_name && !has_snapshot_node_name) {
1477 error_setg(errp, "New snapshot node name missing");
1478 return;
1481 if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) {
1482 error_setg(errp, "New snapshot node name already existing");
1483 return;
1486 /* Acquire AioContext now so any threads operating on old_bs stop */
1487 state->aio_context = bdrv_get_aio_context(state->old_bs);
1488 aio_context_acquire(state->aio_context);
1490 if (!bdrv_is_inserted(state->old_bs)) {
1491 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1492 return;
1495 if (bdrv_op_is_blocked(state->old_bs,
1496 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1497 return;
1500 if (!bdrv_is_read_only(state->old_bs)) {
1501 if (bdrv_flush(state->old_bs)) {
1502 error_setg(errp, QERR_IO_ERROR);
1503 return;
1507 if (!bdrv_is_first_non_filter(state->old_bs)) {
1508 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1509 return;
1512 flags = state->old_bs->open_flags;
1514 /* create new image w/backing file */
1515 if (mode != NEW_IMAGE_MODE_EXISTING) {
1516 bdrv_img_create(new_image_file, format,
1517 state->old_bs->filename,
1518 state->old_bs->drv->format_name,
1519 NULL, -1, flags, &local_err, false);
1520 if (local_err) {
1521 error_propagate(errp, local_err);
1522 return;
1526 if (has_snapshot_node_name) {
1527 options = qdict_new();
1528 qdict_put(options, "node-name",
1529 qstring_from_str(snapshot_node_name));
1532 /* TODO Inherit bs->options or only take explicit options with an
1533 * extended QMP command? */
1534 assert(state->new_bs == NULL);
1535 ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
1536 flags | BDRV_O_NO_BACKING, drv, &local_err);
1537 /* We will manually add the backing_hd field to the bs later */
1538 if (ret != 0) {
1539 error_propagate(errp, local_err);
1543 static void external_snapshot_commit(BlkTransactionState *common)
1545 ExternalSnapshotState *state =
1546 DO_UPCAST(ExternalSnapshotState, common, common);
1548 bdrv_set_aio_context(state->new_bs, state->aio_context);
1550 /* This removes our old bs and adds the new bs */
1551 bdrv_append(state->new_bs, state->old_bs);
1552 /* We don't need (or want) to use the transactional
1553 * bdrv_reopen_multiple() across all the entries at once, because we
1554 * don't want to abort all of them if one of them fails the reopen */
1555 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
1556 NULL);
1558 aio_context_release(state->aio_context);
1561 static void external_snapshot_abort(BlkTransactionState *common)
1563 ExternalSnapshotState *state =
1564 DO_UPCAST(ExternalSnapshotState, common, common);
1565 if (state->new_bs) {
1566 bdrv_unref(state->new_bs);
1568 if (state->aio_context) {
1569 aio_context_release(state->aio_context);
1573 typedef struct DriveBackupState {
1574 BlkTransactionState common;
1575 BlockDriverState *bs;
1576 AioContext *aio_context;
1577 BlockJob *job;
1578 } DriveBackupState;
1580 static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1582 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1583 BlockDriverState *bs;
1584 BlockBackend *blk;
1585 DriveBackup *backup;
1586 Error *local_err = NULL;
1588 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1589 backup = common->action->drive_backup;
1591 blk = blk_by_name(backup->device);
1592 if (!blk) {
1593 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1594 "Device '%s' not found", backup->device);
1595 return;
1597 bs = blk_bs(blk);
1599 /* AioContext is released in .clean() */
1600 state->aio_context = bdrv_get_aio_context(bs);
1601 aio_context_acquire(state->aio_context);
1603 qmp_drive_backup(backup->device, backup->target,
1604 backup->has_format, backup->format,
1605 backup->sync,
1606 backup->has_mode, backup->mode,
1607 backup->has_speed, backup->speed,
1608 backup->has_bitmap, backup->bitmap,
1609 backup->has_on_source_error, backup->on_source_error,
1610 backup->has_on_target_error, backup->on_target_error,
1611 &local_err);
1612 if (local_err) {
1613 error_propagate(errp, local_err);
1614 return;
1617 state->bs = bs;
1618 state->job = state->bs->job;
1621 static void drive_backup_abort(BlkTransactionState *common)
1623 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1624 BlockDriverState *bs = state->bs;
1626 /* Only cancel if it's the job we started */
1627 if (bs && bs->job && bs->job == state->job) {
1628 block_job_cancel_sync(bs->job);
1632 static void drive_backup_clean(BlkTransactionState *common)
1634 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1636 if (state->aio_context) {
1637 aio_context_release(state->aio_context);
1641 typedef struct BlockdevBackupState {
1642 BlkTransactionState common;
1643 BlockDriverState *bs;
1644 BlockJob *job;
1645 AioContext *aio_context;
1646 } BlockdevBackupState;
1648 static void blockdev_backup_prepare(BlkTransactionState *common, Error **errp)
1650 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1651 BlockdevBackup *backup;
1652 BlockDriverState *bs, *target;
1653 BlockBackend *blk;
1654 Error *local_err = NULL;
1656 assert(common->action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1657 backup = common->action->blockdev_backup;
1659 blk = blk_by_name(backup->device);
1660 if (!blk) {
1661 error_setg(errp, "Device '%s' not found", backup->device);
1662 return;
1664 bs = blk_bs(blk);
1666 blk = blk_by_name(backup->target);
1667 if (!blk) {
1668 error_setg(errp, "Device '%s' not found", backup->target);
1669 return;
1671 target = blk_bs(blk);
1673 /* AioContext is released in .clean() */
1674 state->aio_context = bdrv_get_aio_context(bs);
1675 if (state->aio_context != bdrv_get_aio_context(target)) {
1676 state->aio_context = NULL;
1677 error_setg(errp, "Backup between two IO threads is not implemented");
1678 return;
1680 aio_context_acquire(state->aio_context);
1682 qmp_blockdev_backup(backup->device, backup->target,
1683 backup->sync,
1684 backup->has_speed, backup->speed,
1685 backup->has_on_source_error, backup->on_source_error,
1686 backup->has_on_target_error, backup->on_target_error,
1687 &local_err);
1688 if (local_err) {
1689 error_propagate(errp, local_err);
1690 return;
1693 state->bs = bs;
1694 state->job = state->bs->job;
1697 static void blockdev_backup_abort(BlkTransactionState *common)
1699 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1700 BlockDriverState *bs = state->bs;
1702 /* Only cancel if it's the job we started */
1703 if (bs && bs->job && bs->job == state->job) {
1704 block_job_cancel_sync(bs->job);
1708 static void blockdev_backup_clean(BlkTransactionState *common)
1710 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1712 if (state->aio_context) {
1713 aio_context_release(state->aio_context);
1717 static void abort_prepare(BlkTransactionState *common, Error **errp)
1719 error_setg(errp, "Transaction aborted using Abort action");
1722 static void abort_commit(BlkTransactionState *common)
1724 g_assert_not_reached(); /* this action never succeeds */
1727 static const BdrvActionOps actions[] = {
1728 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1729 .instance_size = sizeof(ExternalSnapshotState),
1730 .prepare = external_snapshot_prepare,
1731 .commit = external_snapshot_commit,
1732 .abort = external_snapshot_abort,
1734 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1735 .instance_size = sizeof(DriveBackupState),
1736 .prepare = drive_backup_prepare,
1737 .abort = drive_backup_abort,
1738 .clean = drive_backup_clean,
1740 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
1741 .instance_size = sizeof(BlockdevBackupState),
1742 .prepare = blockdev_backup_prepare,
1743 .abort = blockdev_backup_abort,
1744 .clean = blockdev_backup_clean,
1746 [TRANSACTION_ACTION_KIND_ABORT] = {
1747 .instance_size = sizeof(BlkTransactionState),
1748 .prepare = abort_prepare,
1749 .commit = abort_commit,
1751 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1752 .instance_size = sizeof(InternalSnapshotState),
1753 .prepare = internal_snapshot_prepare,
1754 .abort = internal_snapshot_abort,
1755 .clean = internal_snapshot_clean,
1760 * 'Atomic' group operations. The operations are performed as a set, and if
1761 * any fail then we roll back all operations in the group.
1763 void qmp_transaction(TransactionActionList *dev_list, Error **errp)
1765 TransactionActionList *dev_entry = dev_list;
1766 BlkTransactionState *state, *next;
1767 Error *local_err = NULL;
1769 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
1770 QSIMPLEQ_INIT(&snap_bdrv_states);
1772 /* drain all i/o before any operations */
1773 bdrv_drain_all();
1775 /* We don't do anything in this loop that commits us to the operations */
1776 while (NULL != dev_entry) {
1777 TransactionAction *dev_info = NULL;
1778 const BdrvActionOps *ops;
1780 dev_info = dev_entry->value;
1781 dev_entry = dev_entry->next;
1783 assert(dev_info->kind < ARRAY_SIZE(actions));
1785 ops = &actions[dev_info->kind];
1786 assert(ops->instance_size > 0);
1788 state = g_malloc0(ops->instance_size);
1789 state->ops = ops;
1790 state->action = dev_info;
1791 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
1793 state->ops->prepare(state, &local_err);
1794 if (local_err) {
1795 error_propagate(errp, local_err);
1796 goto delete_and_fail;
1800 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1801 if (state->ops->commit) {
1802 state->ops->commit(state);
1806 /* success */
1807 goto exit;
1809 delete_and_fail:
1810 /* failure, and it is all-or-none; roll back all operations */
1811 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1812 if (state->ops->abort) {
1813 state->ops->abort(state);
1816 exit:
1817 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1818 if (state->ops->clean) {
1819 state->ops->clean(state);
1821 g_free(state);
1826 static void eject_device(BlockBackend *blk, int force, Error **errp)
1828 BlockDriverState *bs = blk_bs(blk);
1829 AioContext *aio_context;
1831 aio_context = bdrv_get_aio_context(bs);
1832 aio_context_acquire(aio_context);
1834 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
1835 goto out;
1837 if (!blk_dev_has_removable_media(blk)) {
1838 error_setg(errp, "Device '%s' is not removable",
1839 bdrv_get_device_name(bs));
1840 goto out;
1843 if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) {
1844 blk_dev_eject_request(blk, force);
1845 if (!force) {
1846 error_setg(errp, "Device '%s' is locked",
1847 bdrv_get_device_name(bs));
1848 goto out;
1852 bdrv_close(bs);
1854 out:
1855 aio_context_release(aio_context);
1858 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1860 BlockBackend *blk;
1862 blk = blk_by_name(device);
1863 if (!blk) {
1864 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1865 "Device '%s' not found", device);
1866 return;
1869 eject_device(blk, force, errp);
1872 void qmp_block_passwd(bool has_device, const char *device,
1873 bool has_node_name, const char *node_name,
1874 const char *password, Error **errp)
1876 Error *local_err = NULL;
1877 BlockDriverState *bs;
1878 AioContext *aio_context;
1880 bs = bdrv_lookup_bs(has_device ? device : NULL,
1881 has_node_name ? node_name : NULL,
1882 &local_err);
1883 if (local_err) {
1884 error_propagate(errp, local_err);
1885 return;
1888 aio_context = bdrv_get_aio_context(bs);
1889 aio_context_acquire(aio_context);
1891 bdrv_add_key(bs, password, errp);
1893 aio_context_release(aio_context);
1896 /* Assumes AioContext is held */
1897 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1898 int bdrv_flags, BlockDriver *drv,
1899 const char *password, Error **errp)
1901 Error *local_err = NULL;
1902 int ret;
1904 ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err);
1905 if (ret < 0) {
1906 error_propagate(errp, local_err);
1907 return;
1910 bdrv_add_key(bs, password, errp);
1913 void qmp_change_blockdev(const char *device, const char *filename,
1914 const char *format, Error **errp)
1916 BlockBackend *blk;
1917 BlockDriverState *bs;
1918 AioContext *aio_context;
1919 BlockDriver *drv = NULL;
1920 int bdrv_flags;
1921 Error *err = NULL;
1923 blk = blk_by_name(device);
1924 if (!blk) {
1925 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1926 "Device '%s' not found", device);
1927 return;
1929 bs = blk_bs(blk);
1931 aio_context = bdrv_get_aio_context(bs);
1932 aio_context_acquire(aio_context);
1934 if (format) {
1935 drv = bdrv_find_whitelisted_format(format, bs->read_only);
1936 if (!drv) {
1937 error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
1938 goto out;
1942 eject_device(blk, 0, &err);
1943 if (err) {
1944 error_propagate(errp, err);
1945 goto out;
1948 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1949 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1951 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1953 out:
1954 aio_context_release(aio_context);
1957 /* throttling disk I/O limits */
1958 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1959 int64_t bps_wr,
1960 int64_t iops,
1961 int64_t iops_rd,
1962 int64_t iops_wr,
1963 bool has_bps_max,
1964 int64_t bps_max,
1965 bool has_bps_rd_max,
1966 int64_t bps_rd_max,
1967 bool has_bps_wr_max,
1968 int64_t bps_wr_max,
1969 bool has_iops_max,
1970 int64_t iops_max,
1971 bool has_iops_rd_max,
1972 int64_t iops_rd_max,
1973 bool has_iops_wr_max,
1974 int64_t iops_wr_max,
1975 bool has_iops_size,
1976 int64_t iops_size,
1977 bool has_group,
1978 const char *group, Error **errp)
1980 ThrottleConfig cfg;
1981 BlockDriverState *bs;
1982 BlockBackend *blk;
1983 AioContext *aio_context;
1985 blk = blk_by_name(device);
1986 if (!blk) {
1987 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1988 "Device '%s' not found", device);
1989 return;
1991 bs = blk_bs(blk);
1993 memset(&cfg, 0, sizeof(cfg));
1994 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1995 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
1996 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
1998 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1999 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
2000 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2002 if (has_bps_max) {
2003 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
2005 if (has_bps_rd_max) {
2006 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
2008 if (has_bps_wr_max) {
2009 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2011 if (has_iops_max) {
2012 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2014 if (has_iops_rd_max) {
2015 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2017 if (has_iops_wr_max) {
2018 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2021 if (has_iops_size) {
2022 cfg.op_size = iops_size;
2025 if (!check_throttle_config(&cfg, errp)) {
2026 return;
2029 aio_context = bdrv_get_aio_context(bs);
2030 aio_context_acquire(aio_context);
2032 if (throttle_enabled(&cfg)) {
2033 /* Enable I/O limits if they're not enabled yet, otherwise
2034 * just update the throttling group. */
2035 if (!bs->io_limits_enabled) {
2036 bdrv_io_limits_enable(bs, has_group ? group : device);
2037 } else if (has_group) {
2038 bdrv_io_limits_update_group(bs, group);
2040 /* Set the new throttling configuration */
2041 bdrv_set_io_limits(bs, &cfg);
2042 } else if (bs->io_limits_enabled) {
2043 /* If all throttling settings are set to 0, disable I/O limits */
2044 bdrv_io_limits_disable(bs);
2047 aio_context_release(aio_context);
2050 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2051 bool has_granularity, uint32_t granularity,
2052 Error **errp)
2054 AioContext *aio_context;
2055 BlockDriverState *bs;
2057 if (!name || name[0] == '\0') {
2058 error_setg(errp, "Bitmap name cannot be empty");
2059 return;
2062 bs = bdrv_lookup_bs(node, node, errp);
2063 if (!bs) {
2064 return;
2067 aio_context = bdrv_get_aio_context(bs);
2068 aio_context_acquire(aio_context);
2070 if (has_granularity) {
2071 if (granularity < 512 || !is_power_of_2(granularity)) {
2072 error_setg(errp, "Granularity must be power of 2 "
2073 "and at least 512");
2074 goto out;
2076 } else {
2077 /* Default to cluster size, if available: */
2078 granularity = bdrv_get_default_bitmap_granularity(bs);
2081 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2083 out:
2084 aio_context_release(aio_context);
2087 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2088 Error **errp)
2090 AioContext *aio_context;
2091 BlockDriverState *bs;
2092 BdrvDirtyBitmap *bitmap;
2094 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2095 if (!bitmap || !bs) {
2096 return;
2099 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2100 error_setg(errp,
2101 "Bitmap '%s' is currently frozen and cannot be removed",
2102 name);
2103 goto out;
2105 bdrv_dirty_bitmap_make_anon(bitmap);
2106 bdrv_release_dirty_bitmap(bs, bitmap);
2108 out:
2109 aio_context_release(aio_context);
2113 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2114 * immediately after a full backup operation.
2116 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2117 Error **errp)
2119 AioContext *aio_context;
2120 BdrvDirtyBitmap *bitmap;
2121 BlockDriverState *bs;
2123 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2124 if (!bitmap || !bs) {
2125 return;
2128 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2129 error_setg(errp,
2130 "Bitmap '%s' is currently frozen and cannot be modified",
2131 name);
2132 goto out;
2133 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2134 error_setg(errp,
2135 "Bitmap '%s' is currently disabled and cannot be cleared",
2136 name);
2137 goto out;
2140 bdrv_clear_dirty_bitmap(bitmap);
2142 out:
2143 aio_context_release(aio_context);
2146 void hmp_drive_del(Monitor *mon, const QDict *qdict)
2148 const char *id = qdict_get_str(qdict, "id");
2149 BlockBackend *blk;
2150 BlockDriverState *bs;
2151 AioContext *aio_context;
2152 Error *local_err = NULL;
2154 blk = blk_by_name(id);
2155 if (!blk) {
2156 error_report("Device '%s' not found", id);
2157 return;
2159 bs = blk_bs(blk);
2161 if (!blk_legacy_dinfo(blk)) {
2162 error_report("Deleting device added with blockdev-add"
2163 " is not supported");
2164 return;
2167 aio_context = bdrv_get_aio_context(bs);
2168 aio_context_acquire(aio_context);
2170 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2171 error_report_err(local_err);
2172 aio_context_release(aio_context);
2173 return;
2176 bdrv_close(bs);
2178 /* if we have a device attached to this BlockDriverState
2179 * then we need to make the drive anonymous until the device
2180 * can be removed. If this is a drive with no device backing
2181 * then we can just get rid of the block driver state right here.
2183 if (blk_get_attached_dev(blk)) {
2184 blk_hide_on_behalf_of_hmp_drive_del(blk);
2185 /* Further I/O must not pause the guest */
2186 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
2187 BLOCKDEV_ON_ERROR_REPORT);
2188 } else {
2189 blk_unref(blk);
2192 aio_context_release(aio_context);
2195 void qmp_block_resize(bool has_device, const char *device,
2196 bool has_node_name, const char *node_name,
2197 int64_t size, Error **errp)
2199 Error *local_err = NULL;
2200 BlockDriverState *bs;
2201 AioContext *aio_context;
2202 int ret;
2204 bs = bdrv_lookup_bs(has_device ? device : NULL,
2205 has_node_name ? node_name : NULL,
2206 &local_err);
2207 if (local_err) {
2208 error_propagate(errp, local_err);
2209 return;
2212 aio_context = bdrv_get_aio_context(bs);
2213 aio_context_acquire(aio_context);
2215 if (!bdrv_is_first_non_filter(bs)) {
2216 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2217 goto out;
2220 if (size < 0) {
2221 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2222 goto out;
2225 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2226 error_setg(errp, QERR_DEVICE_IN_USE, device);
2227 goto out;
2230 /* complete all in-flight operations before resizing the device */
2231 bdrv_drain_all();
2233 ret = bdrv_truncate(bs, size);
2234 switch (ret) {
2235 case 0:
2236 break;
2237 case -ENOMEDIUM:
2238 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2239 break;
2240 case -ENOTSUP:
2241 error_setg(errp, QERR_UNSUPPORTED);
2242 break;
2243 case -EACCES:
2244 error_setg(errp, "Device '%s' is read only", device);
2245 break;
2246 case -EBUSY:
2247 error_setg(errp, QERR_DEVICE_IN_USE, device);
2248 break;
2249 default:
2250 error_setg_errno(errp, -ret, "Could not resize");
2251 break;
2254 out:
2255 aio_context_release(aio_context);
2258 static void block_job_cb(void *opaque, int ret)
2260 /* Note that this function may be executed from another AioContext besides
2261 * the QEMU main loop. If you need to access anything that assumes the
2262 * QEMU global mutex, use a BH or introduce a mutex.
2265 BlockDriverState *bs = opaque;
2266 const char *msg = NULL;
2268 trace_block_job_cb(bs, bs->job, ret);
2270 assert(bs->job);
2272 if (ret < 0) {
2273 msg = strerror(-ret);
2276 if (block_job_is_cancelled(bs->job)) {
2277 block_job_event_cancelled(bs->job);
2278 } else {
2279 block_job_event_completed(bs->job, msg);
2282 bdrv_put_ref_bh_schedule(bs);
2285 void qmp_block_stream(const char *device,
2286 bool has_base, const char *base,
2287 bool has_backing_file, const char *backing_file,
2288 bool has_speed, int64_t speed,
2289 bool has_on_error, BlockdevOnError on_error,
2290 Error **errp)
2292 BlockBackend *blk;
2293 BlockDriverState *bs;
2294 BlockDriverState *base_bs = NULL;
2295 AioContext *aio_context;
2296 Error *local_err = NULL;
2297 const char *base_name = NULL;
2299 if (!has_on_error) {
2300 on_error = BLOCKDEV_ON_ERROR_REPORT;
2303 blk = blk_by_name(device);
2304 if (!blk) {
2305 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2306 "Device '%s' not found", device);
2307 return;
2309 bs = blk_bs(blk);
2311 aio_context = bdrv_get_aio_context(bs);
2312 aio_context_acquire(aio_context);
2314 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2315 goto out;
2318 if (has_base) {
2319 base_bs = bdrv_find_backing_image(bs, base);
2320 if (base_bs == NULL) {
2321 error_setg(errp, QERR_BASE_NOT_FOUND, base);
2322 goto out;
2324 assert(bdrv_get_aio_context(base_bs) == aio_context);
2325 base_name = base;
2328 /* if we are streaming the entire chain, the result will have no backing
2329 * file, and specifying one is therefore an error */
2330 if (base_bs == NULL && has_backing_file) {
2331 error_setg(errp, "backing file specified, but streaming the "
2332 "entire chain");
2333 goto out;
2336 /* backing_file string overrides base bs filename */
2337 base_name = has_backing_file ? backing_file : base_name;
2339 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
2340 on_error, block_job_cb, bs, &local_err);
2341 if (local_err) {
2342 error_propagate(errp, local_err);
2343 goto out;
2346 trace_qmp_block_stream(bs, bs->job);
2348 out:
2349 aio_context_release(aio_context);
2352 void qmp_block_commit(const char *device,
2353 bool has_base, const char *base,
2354 bool has_top, const char *top,
2355 bool has_backing_file, const char *backing_file,
2356 bool has_speed, int64_t speed,
2357 Error **errp)
2359 BlockBackend *blk;
2360 BlockDriverState *bs;
2361 BlockDriverState *base_bs, *top_bs;
2362 AioContext *aio_context;
2363 Error *local_err = NULL;
2364 /* This will be part of the QMP command, if/when the
2365 * BlockdevOnError change for blkmirror makes it in
2367 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
2369 if (!has_speed) {
2370 speed = 0;
2373 /* Important Note:
2374 * libvirt relies on the DeviceNotFound error class in order to probe for
2375 * live commit feature versions; for this to work, we must make sure to
2376 * perform the device lookup before any generic errors that may occur in a
2377 * scenario in which all optional arguments are omitted. */
2378 blk = blk_by_name(device);
2379 if (!blk) {
2380 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2381 "Device '%s' not found", device);
2382 return;
2384 bs = blk_bs(blk);
2386 aio_context = bdrv_get_aio_context(bs);
2387 aio_context_acquire(aio_context);
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 void qmp_drive_mirror(const char *device, const char *target,
2649 bool has_format, const char *format,
2650 bool has_node_name, const char *node_name,
2651 bool has_replaces, const char *replaces,
2652 enum MirrorSyncMode sync,
2653 bool has_mode, enum NewImageMode mode,
2654 bool has_speed, int64_t speed,
2655 bool has_granularity, uint32_t granularity,
2656 bool has_buf_size, int64_t buf_size,
2657 bool has_on_source_error, BlockdevOnError on_source_error,
2658 bool has_on_target_error, BlockdevOnError on_target_error,
2659 bool has_unmap, bool unmap,
2660 Error **errp)
2662 BlockBackend *blk;
2663 BlockDriverState *bs;
2664 BlockDriverState *source, *target_bs;
2665 AioContext *aio_context;
2666 BlockDriver *drv = NULL;
2667 Error *local_err = NULL;
2668 QDict *options = NULL;
2669 int flags;
2670 int64_t size;
2671 int ret;
2673 if (!has_speed) {
2674 speed = 0;
2676 if (!has_on_source_error) {
2677 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2679 if (!has_on_target_error) {
2680 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2682 if (!has_mode) {
2683 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2685 if (!has_granularity) {
2686 granularity = 0;
2688 if (!has_buf_size) {
2689 buf_size = 0;
2691 if (!has_unmap) {
2692 unmap = true;
2695 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
2696 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2697 "a value in range [512B, 64MB]");
2698 return;
2700 if (granularity & (granularity - 1)) {
2701 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2702 "power of 2");
2703 return;
2706 blk = blk_by_name(device);
2707 if (!blk) {
2708 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2709 "Device '%s' not found", device);
2710 return;
2712 bs = blk_bs(blk);
2714 aio_context = bdrv_get_aio_context(bs);
2715 aio_context_acquire(aio_context);
2717 if (!bdrv_is_inserted(bs)) {
2718 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2719 goto out;
2722 if (!has_format) {
2723 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2725 if (format) {
2726 drv = bdrv_find_format(format);
2727 if (!drv) {
2728 error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
2729 goto out;
2733 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
2734 goto out;
2737 flags = bs->open_flags | BDRV_O_RDWR;
2738 source = bs->backing_hd;
2739 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
2740 sync = MIRROR_SYNC_MODE_FULL;
2742 if (sync == MIRROR_SYNC_MODE_NONE) {
2743 source = bs;
2746 size = bdrv_getlength(bs);
2747 if (size < 0) {
2748 error_setg_errno(errp, -size, "bdrv_getlength failed");
2749 goto out;
2752 if (has_replaces) {
2753 BlockDriverState *to_replace_bs;
2754 AioContext *replace_aio_context;
2755 int64_t replace_size;
2757 if (!has_node_name) {
2758 error_setg(errp, "a node-name must be provided when replacing a"
2759 " named node of the graph");
2760 goto out;
2763 to_replace_bs = check_to_replace_node(replaces, &local_err);
2765 if (!to_replace_bs) {
2766 error_propagate(errp, local_err);
2767 goto out;
2770 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
2771 aio_context_acquire(replace_aio_context);
2772 replace_size = bdrv_getlength(to_replace_bs);
2773 aio_context_release(replace_aio_context);
2775 if (size != replace_size) {
2776 error_setg(errp, "cannot replace image with a mirror image of "
2777 "different size");
2778 goto out;
2782 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
2783 && mode != NEW_IMAGE_MODE_EXISTING)
2785 /* create new image w/o backing file */
2786 assert(format && drv);
2787 bdrv_img_create(target, format,
2788 NULL, NULL, NULL, size, flags, &local_err, false);
2789 } else {
2790 switch (mode) {
2791 case NEW_IMAGE_MODE_EXISTING:
2792 break;
2793 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2794 /* create new image with backing file */
2795 bdrv_img_create(target, format,
2796 source->filename,
2797 source->drv->format_name,
2798 NULL, size, flags, &local_err, false);
2799 break;
2800 default:
2801 abort();
2805 if (local_err) {
2806 error_propagate(errp, local_err);
2807 goto out;
2810 if (has_node_name) {
2811 options = qdict_new();
2812 qdict_put(options, "node-name", qstring_from_str(node_name));
2815 /* Mirroring takes care of copy-on-write using the source's backing
2816 * file.
2818 target_bs = NULL;
2819 ret = bdrv_open(&target_bs, target, NULL, options,
2820 flags | BDRV_O_NO_BACKING, drv, &local_err);
2821 if (ret < 0) {
2822 error_propagate(errp, local_err);
2823 goto out;
2826 bdrv_set_aio_context(target_bs, aio_context);
2828 /* pass the node name to replace to mirror start since it's loose coupling
2829 * and will allow to check whether the node still exist at mirror completion
2831 mirror_start(bs, target_bs,
2832 has_replaces ? replaces : NULL,
2833 speed, granularity, buf_size, sync,
2834 on_source_error, on_target_error,
2835 unmap,
2836 block_job_cb, bs, &local_err);
2837 if (local_err != NULL) {
2838 bdrv_unref(target_bs);
2839 error_propagate(errp, local_err);
2840 goto out;
2843 out:
2844 aio_context_release(aio_context);
2847 /* Get the block job for a given device name and acquire its AioContext */
2848 static BlockJob *find_block_job(const char *device, AioContext **aio_context,
2849 Error **errp)
2851 BlockBackend *blk;
2852 BlockDriverState *bs;
2854 blk = blk_by_name(device);
2855 if (!blk) {
2856 goto notfound;
2858 bs = blk_bs(blk);
2860 *aio_context = bdrv_get_aio_context(bs);
2861 aio_context_acquire(*aio_context);
2863 if (!bs->job) {
2864 aio_context_release(*aio_context);
2865 goto notfound;
2868 return bs->job;
2870 notfound:
2871 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
2872 "No active block job on device '%s'", device);
2873 *aio_context = NULL;
2874 return NULL;
2877 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2879 AioContext *aio_context;
2880 BlockJob *job = find_block_job(device, &aio_context, errp);
2882 if (!job) {
2883 return;
2886 block_job_set_speed(job, speed, errp);
2887 aio_context_release(aio_context);
2890 void qmp_block_job_cancel(const char *device,
2891 bool has_force, bool force, Error **errp)
2893 AioContext *aio_context;
2894 BlockJob *job = find_block_job(device, &aio_context, errp);
2896 if (!job) {
2897 return;
2900 if (!has_force) {
2901 force = false;
2904 if (job->user_paused && !force) {
2905 error_setg(errp, "The block job for device '%s' is currently paused",
2906 device);
2907 goto out;
2910 trace_qmp_block_job_cancel(job);
2911 block_job_cancel(job);
2912 out:
2913 aio_context_release(aio_context);
2916 void qmp_block_job_pause(const char *device, Error **errp)
2918 AioContext *aio_context;
2919 BlockJob *job = find_block_job(device, &aio_context, errp);
2921 if (!job || job->user_paused) {
2922 return;
2925 job->user_paused = true;
2926 trace_qmp_block_job_pause(job);
2927 block_job_pause(job);
2928 aio_context_release(aio_context);
2931 void qmp_block_job_resume(const char *device, Error **errp)
2933 AioContext *aio_context;
2934 BlockJob *job = find_block_job(device, &aio_context, errp);
2936 if (!job || !job->user_paused) {
2937 return;
2940 job->user_paused = false;
2941 trace_qmp_block_job_resume(job);
2942 block_job_resume(job);
2943 aio_context_release(aio_context);
2946 void qmp_block_job_complete(const char *device, Error **errp)
2948 AioContext *aio_context;
2949 BlockJob *job = find_block_job(device, &aio_context, errp);
2951 if (!job) {
2952 return;
2955 trace_qmp_block_job_complete(job);
2956 block_job_complete(job, errp);
2957 aio_context_release(aio_context);
2960 void qmp_change_backing_file(const char *device,
2961 const char *image_node_name,
2962 const char *backing_file,
2963 Error **errp)
2965 BlockBackend *blk;
2966 BlockDriverState *bs = NULL;
2967 AioContext *aio_context;
2968 BlockDriverState *image_bs = NULL;
2969 Error *local_err = NULL;
2970 bool ro;
2971 int open_flags;
2972 int ret;
2974 blk = blk_by_name(device);
2975 if (!blk) {
2976 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2977 "Device '%s' not found", device);
2978 return;
2980 bs = blk_bs(blk);
2982 aio_context = bdrv_get_aio_context(bs);
2983 aio_context_acquire(aio_context);
2985 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
2986 if (local_err) {
2987 error_propagate(errp, local_err);
2988 goto out;
2991 if (!image_bs) {
2992 error_setg(errp, "image file not found");
2993 goto out;
2996 if (bdrv_find_base(image_bs) == image_bs) {
2997 error_setg(errp, "not allowing backing file change on an image "
2998 "without a backing file");
2999 goto out;
3002 /* even though we are not necessarily operating on bs, we need it to
3003 * determine if block ops are currently prohibited on the chain */
3004 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3005 goto out;
3008 /* final sanity check */
3009 if (!bdrv_chain_contains(bs, image_bs)) {
3010 error_setg(errp, "'%s' and image file are not in the same chain",
3011 device);
3012 goto out;
3015 /* if not r/w, reopen to make r/w */
3016 open_flags = image_bs->open_flags;
3017 ro = bdrv_is_read_only(image_bs);
3019 if (ro) {
3020 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3021 if (local_err) {
3022 error_propagate(errp, local_err);
3023 goto out;
3027 ret = bdrv_change_backing_file(image_bs, backing_file,
3028 image_bs->drv ? image_bs->drv->format_name : "");
3030 if (ret < 0) {
3031 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3032 backing_file);
3033 /* don't exit here, so we can try to restore open flags if
3034 * appropriate */
3037 if (ro) {
3038 bdrv_reopen(image_bs, open_flags, &local_err);
3039 if (local_err) {
3040 error_propagate(errp, local_err); /* will preserve prior errp */
3044 out:
3045 aio_context_release(aio_context);
3048 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3050 QmpOutputVisitor *ov = qmp_output_visitor_new();
3051 BlockBackend *blk;
3052 QObject *obj;
3053 QDict *qdict;
3054 Error *local_err = NULL;
3056 /* Require an ID in the top level */
3057 if (!options->has_id) {
3058 error_setg(errp, "Block device needs an ID");
3059 goto fail;
3062 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3063 * cache.direct=false instead of silently switching to aio=threads, except
3064 * when called from drive_new().
3066 * For now, simply forbidding the combination for all drivers will do. */
3067 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3068 bool direct = options->has_cache &&
3069 options->cache->has_direct &&
3070 options->cache->direct;
3071 if (!direct) {
3072 error_setg(errp, "aio=native requires cache.direct=true");
3073 goto fail;
3077 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
3078 &options, NULL, &local_err);
3079 if (local_err) {
3080 error_propagate(errp, local_err);
3081 goto fail;
3084 obj = qmp_output_get_qobject(ov);
3085 qdict = qobject_to_qdict(obj);
3087 qdict_flatten(qdict);
3089 blk = blockdev_init(NULL, qdict, &local_err);
3090 if (local_err) {
3091 error_propagate(errp, local_err);
3092 goto fail;
3095 if (bdrv_key_required(blk_bs(blk))) {
3096 blk_unref(blk);
3097 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3098 goto fail;
3101 fail:
3102 qmp_output_visitor_cleanup(ov);
3105 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3107 BlockJobInfoList *head = NULL, **p_next = &head;
3108 BlockDriverState *bs;
3110 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3111 AioContext *aio_context = bdrv_get_aio_context(bs);
3113 aio_context_acquire(aio_context);
3115 if (bs->job) {
3116 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3117 elem->value = block_job_query(bs->job);
3118 *p_next = elem;
3119 p_next = &elem->next;
3122 aio_context_release(aio_context);
3125 return head;
3128 QemuOptsList qemu_common_drive_opts = {
3129 .name = "drive",
3130 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3131 .desc = {
3133 .name = "snapshot",
3134 .type = QEMU_OPT_BOOL,
3135 .help = "enable/disable snapshot mode",
3137 .name = "discard",
3138 .type = QEMU_OPT_STRING,
3139 .help = "discard operation (ignore/off, unmap/on)",
3141 .name = BDRV_OPT_CACHE_WB,
3142 .type = QEMU_OPT_BOOL,
3143 .help = "enables writeback mode for any caches",
3145 .name = BDRV_OPT_CACHE_DIRECT,
3146 .type = QEMU_OPT_BOOL,
3147 .help = "enables use of O_DIRECT (bypass the host page cache)",
3149 .name = BDRV_OPT_CACHE_NO_FLUSH,
3150 .type = QEMU_OPT_BOOL,
3151 .help = "ignore any flush requests for the device",
3153 .name = "aio",
3154 .type = QEMU_OPT_STRING,
3155 .help = "host AIO implementation (threads, native)",
3157 .name = "format",
3158 .type = QEMU_OPT_STRING,
3159 .help = "disk format (raw, qcow2, ...)",
3161 .name = "rerror",
3162 .type = QEMU_OPT_STRING,
3163 .help = "read error action",
3165 .name = "werror",
3166 .type = QEMU_OPT_STRING,
3167 .help = "write error action",
3169 .name = "read-only",
3170 .type = QEMU_OPT_BOOL,
3171 .help = "open drive file as read-only",
3173 .name = "throttling.iops-total",
3174 .type = QEMU_OPT_NUMBER,
3175 .help = "limit total I/O operations per second",
3177 .name = "throttling.iops-read",
3178 .type = QEMU_OPT_NUMBER,
3179 .help = "limit read operations per second",
3181 .name = "throttling.iops-write",
3182 .type = QEMU_OPT_NUMBER,
3183 .help = "limit write operations per second",
3185 .name = "throttling.bps-total",
3186 .type = QEMU_OPT_NUMBER,
3187 .help = "limit total bytes per second",
3189 .name = "throttling.bps-read",
3190 .type = QEMU_OPT_NUMBER,
3191 .help = "limit read bytes per second",
3193 .name = "throttling.bps-write",
3194 .type = QEMU_OPT_NUMBER,
3195 .help = "limit write bytes per second",
3197 .name = "throttling.iops-total-max",
3198 .type = QEMU_OPT_NUMBER,
3199 .help = "I/O operations burst",
3201 .name = "throttling.iops-read-max",
3202 .type = QEMU_OPT_NUMBER,
3203 .help = "I/O operations read burst",
3205 .name = "throttling.iops-write-max",
3206 .type = QEMU_OPT_NUMBER,
3207 .help = "I/O operations write burst",
3209 .name = "throttling.bps-total-max",
3210 .type = QEMU_OPT_NUMBER,
3211 .help = "total bytes burst",
3213 .name = "throttling.bps-read-max",
3214 .type = QEMU_OPT_NUMBER,
3215 .help = "total bytes read burst",
3217 .name = "throttling.bps-write-max",
3218 .type = QEMU_OPT_NUMBER,
3219 .help = "total bytes write burst",
3221 .name = "throttling.iops-size",
3222 .type = QEMU_OPT_NUMBER,
3223 .help = "when limiting by iops max size of an I/O in bytes",
3225 .name = "throttling.group",
3226 .type = QEMU_OPT_STRING,
3227 .help = "name of the block throttling group",
3229 .name = "copy-on-read",
3230 .type = QEMU_OPT_BOOL,
3231 .help = "copy read data from backing file into image file",
3233 .name = "detect-zeroes",
3234 .type = QEMU_OPT_STRING,
3235 .help = "try to optimize zero writes (off, on, unmap)",
3237 { /* end of list */ }
3241 QemuOptsList qemu_drive_opts = {
3242 .name = "drive",
3243 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3244 .desc = {
3246 * no elements => accept any params
3247 * validation will happen later
3249 { /* end of list */ }