disas: QOMify alpha specific disas setup
[qemu/cris-port.git] / blockdev.c
blob8141b6b3da5dd287b2efcaccc71d184fadd748da
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 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
415 if (!strcmp(buf, "native")) {
416 bdrv_flags |= BDRV_O_NATIVE_AIO;
417 } else if (!strcmp(buf, "threads")) {
418 /* this is the default */
419 } else {
420 error_setg(errp, "invalid aio option");
421 goto early_err;
425 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
426 if (is_help_option(buf)) {
427 error_printf("Supported formats:");
428 bdrv_iterate_format(bdrv_format_print, NULL);
429 error_printf("\n");
430 goto early_err;
433 if (qdict_haskey(bs_opts, "driver")) {
434 error_setg(errp, "Cannot specify both 'driver' and 'format'");
435 goto early_err;
437 qdict_put(bs_opts, "driver", qstring_from_str(buf));
440 /* disk I/O throttling */
441 memset(&cfg, 0, sizeof(cfg));
442 cfg.buckets[THROTTLE_BPS_TOTAL].avg =
443 qemu_opt_get_number(opts, "throttling.bps-total", 0);
444 cfg.buckets[THROTTLE_BPS_READ].avg =
445 qemu_opt_get_number(opts, "throttling.bps-read", 0);
446 cfg.buckets[THROTTLE_BPS_WRITE].avg =
447 qemu_opt_get_number(opts, "throttling.bps-write", 0);
448 cfg.buckets[THROTTLE_OPS_TOTAL].avg =
449 qemu_opt_get_number(opts, "throttling.iops-total", 0);
450 cfg.buckets[THROTTLE_OPS_READ].avg =
451 qemu_opt_get_number(opts, "throttling.iops-read", 0);
452 cfg.buckets[THROTTLE_OPS_WRITE].avg =
453 qemu_opt_get_number(opts, "throttling.iops-write", 0);
455 cfg.buckets[THROTTLE_BPS_TOTAL].max =
456 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
457 cfg.buckets[THROTTLE_BPS_READ].max =
458 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
459 cfg.buckets[THROTTLE_BPS_WRITE].max =
460 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
461 cfg.buckets[THROTTLE_OPS_TOTAL].max =
462 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
463 cfg.buckets[THROTTLE_OPS_READ].max =
464 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
465 cfg.buckets[THROTTLE_OPS_WRITE].max =
466 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
468 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
470 throttling_group = qemu_opt_get(opts, "throttling.group");
472 if (!check_throttle_config(&cfg, &error)) {
473 error_propagate(errp, error);
474 goto early_err;
477 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
478 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
479 on_write_error = parse_block_error_action(buf, 0, &error);
480 if (error) {
481 error_propagate(errp, error);
482 goto early_err;
486 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
487 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
488 on_read_error = parse_block_error_action(buf, 1, &error);
489 if (error) {
490 error_propagate(errp, error);
491 goto early_err;
495 detect_zeroes =
496 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
497 qemu_opt_get(opts, "detect-zeroes"),
498 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
499 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
500 &error);
501 if (error) {
502 error_propagate(errp, error);
503 goto early_err;
506 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
507 !(bdrv_flags & BDRV_O_UNMAP)) {
508 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
509 "without setting discard operation to unmap");
510 goto early_err;
513 /* init */
514 if ((!file || !*file) && !has_driver_specific_opts) {
515 blk = blk_new_with_bs(qemu_opts_id(opts), errp);
516 if (!blk) {
517 goto early_err;
520 bs = blk_bs(blk);
521 bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
522 bs->read_only = ro;
524 QDECREF(bs_opts);
525 } else {
526 if (file && !*file) {
527 file = NULL;
530 if (snapshot) {
531 /* always use cache=unsafe with snapshot */
532 bdrv_flags &= ~BDRV_O_CACHE_MASK;
533 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
536 if (copy_on_read) {
537 bdrv_flags |= BDRV_O_COPY_ON_READ;
540 if (runstate_check(RUN_STATE_INMIGRATE)) {
541 bdrv_flags |= BDRV_O_INCOMING;
544 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
546 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
547 errp);
548 if (!blk) {
549 goto err_no_bs_opts;
551 bs = blk_bs(blk);
554 bs->detect_zeroes = detect_zeroes;
556 bdrv_set_on_error(bs, on_read_error, on_write_error);
558 /* disk I/O throttling */
559 if (throttle_enabled(&cfg)) {
560 if (!throttling_group) {
561 throttling_group = blk_name(blk);
563 bdrv_io_limits_enable(bs, throttling_group);
564 bdrv_set_io_limits(bs, &cfg);
567 if (bdrv_key_required(bs)) {
568 autostart = 0;
571 err_no_bs_opts:
572 qemu_opts_del(opts);
573 return blk;
575 early_err:
576 qemu_opts_del(opts);
577 err_no_opts:
578 QDECREF(bs_opts);
579 return NULL;
582 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
583 Error **errp)
585 const char *value;
587 value = qemu_opt_get(opts, from);
588 if (value) {
589 if (qemu_opt_find(opts, to)) {
590 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
591 "same time", to, from);
592 return;
596 /* rename all items in opts */
597 while ((value = qemu_opt_get(opts, from))) {
598 qemu_opt_set(opts, to, value, &error_abort);
599 qemu_opt_unset(opts, from);
603 QemuOptsList qemu_legacy_drive_opts = {
604 .name = "drive",
605 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
606 .desc = {
608 .name = "bus",
609 .type = QEMU_OPT_NUMBER,
610 .help = "bus number",
612 .name = "unit",
613 .type = QEMU_OPT_NUMBER,
614 .help = "unit number (i.e. lun for scsi)",
616 .name = "index",
617 .type = QEMU_OPT_NUMBER,
618 .help = "index number",
620 .name = "media",
621 .type = QEMU_OPT_STRING,
622 .help = "media type (disk, cdrom)",
624 .name = "if",
625 .type = QEMU_OPT_STRING,
626 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
628 .name = "cyls",
629 .type = QEMU_OPT_NUMBER,
630 .help = "number of cylinders (ide disk geometry)",
632 .name = "heads",
633 .type = QEMU_OPT_NUMBER,
634 .help = "number of heads (ide disk geometry)",
636 .name = "secs",
637 .type = QEMU_OPT_NUMBER,
638 .help = "number of sectors (ide disk geometry)",
640 .name = "trans",
641 .type = QEMU_OPT_STRING,
642 .help = "chs translation (auto, lba, none)",
644 .name = "boot",
645 .type = QEMU_OPT_BOOL,
646 .help = "(deprecated, ignored)",
648 .name = "addr",
649 .type = QEMU_OPT_STRING,
650 .help = "pci address (virtio only)",
652 .name = "serial",
653 .type = QEMU_OPT_STRING,
654 .help = "disk serial number",
656 .name = "file",
657 .type = QEMU_OPT_STRING,
658 .help = "file name",
661 /* Options that are passed on, but have special semantics with -drive */
663 .name = "read-only",
664 .type = QEMU_OPT_BOOL,
665 .help = "open drive file as read-only",
667 .name = "rerror",
668 .type = QEMU_OPT_STRING,
669 .help = "read error action",
671 .name = "werror",
672 .type = QEMU_OPT_STRING,
673 .help = "write error action",
675 .name = "copy-on-read",
676 .type = QEMU_OPT_BOOL,
677 .help = "copy read data from backing file into image file",
680 { /* end of list */ }
684 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
686 const char *value;
687 BlockBackend *blk;
688 DriveInfo *dinfo = NULL;
689 QDict *bs_opts;
690 QemuOpts *legacy_opts;
691 DriveMediaType media = MEDIA_DISK;
692 BlockInterfaceType type;
693 int cyls, heads, secs, translation;
694 int max_devs, bus_id, unit_id, index;
695 const char *devaddr;
696 const char *werror, *rerror;
697 bool read_only = false;
698 bool copy_on_read;
699 const char *serial;
700 const char *filename;
701 Error *local_err = NULL;
702 int i;
704 /* Change legacy command line options into QMP ones */
705 static const struct {
706 const char *from;
707 const char *to;
708 } opt_renames[] = {
709 { "iops", "throttling.iops-total" },
710 { "iops_rd", "throttling.iops-read" },
711 { "iops_wr", "throttling.iops-write" },
713 { "bps", "throttling.bps-total" },
714 { "bps_rd", "throttling.bps-read" },
715 { "bps_wr", "throttling.bps-write" },
717 { "iops_max", "throttling.iops-total-max" },
718 { "iops_rd_max", "throttling.iops-read-max" },
719 { "iops_wr_max", "throttling.iops-write-max" },
721 { "bps_max", "throttling.bps-total-max" },
722 { "bps_rd_max", "throttling.bps-read-max" },
723 { "bps_wr_max", "throttling.bps-write-max" },
725 { "iops_size", "throttling.iops-size" },
727 { "group", "throttling.group" },
729 { "readonly", "read-only" },
732 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
733 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
734 &local_err);
735 if (local_err) {
736 error_report_err(local_err);
737 return NULL;
741 value = qemu_opt_get(all_opts, "cache");
742 if (value) {
743 int flags = 0;
745 if (bdrv_parse_cache_flags(value, &flags) != 0) {
746 error_report("invalid cache option");
747 return NULL;
750 /* Specific options take precedence */
751 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
752 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
753 !!(flags & BDRV_O_CACHE_WB), &error_abort);
755 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
756 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
757 !!(flags & BDRV_O_NOCACHE), &error_abort);
759 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
760 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
761 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
763 qemu_opt_unset(all_opts, "cache");
766 /* Get a QDict for processing the options */
767 bs_opts = qdict_new();
768 qemu_opts_to_qdict(all_opts, bs_opts);
770 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
771 &error_abort);
772 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
773 if (local_err) {
774 error_report_err(local_err);
775 goto fail;
778 /* Deprecated option boot=[on|off] */
779 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
780 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
781 "ignored. Future versions will reject this parameter. Please "
782 "update your scripts.\n");
785 /* Media type */
786 value = qemu_opt_get(legacy_opts, "media");
787 if (value) {
788 if (!strcmp(value, "disk")) {
789 media = MEDIA_DISK;
790 } else if (!strcmp(value, "cdrom")) {
791 media = MEDIA_CDROM;
792 read_only = true;
793 } else {
794 error_report("'%s' invalid media", value);
795 goto fail;
799 /* copy-on-read is disabled with a warning for read-only devices */
800 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
801 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
803 if (read_only && copy_on_read) {
804 error_report("warning: disabling copy-on-read on read-only drive");
805 copy_on_read = false;
808 qdict_put(bs_opts, "read-only",
809 qstring_from_str(read_only ? "on" : "off"));
810 qdict_put(bs_opts, "copy-on-read",
811 qstring_from_str(copy_on_read ? "on" :"off"));
813 /* Controller type */
814 value = qemu_opt_get(legacy_opts, "if");
815 if (value) {
816 for (type = 0;
817 type < IF_COUNT && strcmp(value, if_name[type]);
818 type++) {
820 if (type == IF_COUNT) {
821 error_report("unsupported bus type '%s'", value);
822 goto fail;
824 } else {
825 type = block_default_type;
828 /* Geometry */
829 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
830 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
831 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
833 if (cyls || heads || secs) {
834 if (cyls < 1) {
835 error_report("invalid physical cyls number");
836 goto fail;
838 if (heads < 1) {
839 error_report("invalid physical heads number");
840 goto fail;
842 if (secs < 1) {
843 error_report("invalid physical secs number");
844 goto fail;
848 translation = BIOS_ATA_TRANSLATION_AUTO;
849 value = qemu_opt_get(legacy_opts, "trans");
850 if (value != NULL) {
851 if (!cyls) {
852 error_report("'%s' trans must be used with cyls, heads and secs",
853 value);
854 goto fail;
856 if (!strcmp(value, "none")) {
857 translation = BIOS_ATA_TRANSLATION_NONE;
858 } else if (!strcmp(value, "lba")) {
859 translation = BIOS_ATA_TRANSLATION_LBA;
860 } else if (!strcmp(value, "large")) {
861 translation = BIOS_ATA_TRANSLATION_LARGE;
862 } else if (!strcmp(value, "rechs")) {
863 translation = BIOS_ATA_TRANSLATION_RECHS;
864 } else if (!strcmp(value, "auto")) {
865 translation = BIOS_ATA_TRANSLATION_AUTO;
866 } else {
867 error_report("'%s' invalid translation type", value);
868 goto fail;
872 if (media == MEDIA_CDROM) {
873 if (cyls || secs || heads) {
874 error_report("CHS can't be set with media=cdrom");
875 goto fail;
879 /* Device address specified by bus/unit or index.
880 * If none was specified, try to find the first free one. */
881 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
882 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
883 index = qemu_opt_get_number(legacy_opts, "index", -1);
885 max_devs = if_max_devs[type];
887 if (index != -1) {
888 if (bus_id != 0 || unit_id != -1) {
889 error_report("index cannot be used with bus and unit");
890 goto fail;
892 bus_id = drive_index_to_bus_id(type, index);
893 unit_id = drive_index_to_unit_id(type, index);
896 if (unit_id == -1) {
897 unit_id = 0;
898 while (drive_get(type, bus_id, unit_id) != NULL) {
899 unit_id++;
900 if (max_devs && unit_id >= max_devs) {
901 unit_id -= max_devs;
902 bus_id++;
907 if (max_devs && unit_id >= max_devs) {
908 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
909 goto fail;
912 if (drive_get(type, bus_id, unit_id) != NULL) {
913 error_report("drive with bus=%d, unit=%d (index=%d) exists",
914 bus_id, unit_id, index);
915 goto fail;
918 /* Serial number */
919 serial = qemu_opt_get(legacy_opts, "serial");
921 /* no id supplied -> create one */
922 if (qemu_opts_id(all_opts) == NULL) {
923 char *new_id;
924 const char *mediastr = "";
925 if (type == IF_IDE || type == IF_SCSI) {
926 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
928 if (max_devs) {
929 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
930 mediastr, unit_id);
931 } else {
932 new_id = g_strdup_printf("%s%s%i", if_name[type],
933 mediastr, unit_id);
935 qdict_put(bs_opts, "id", qstring_from_str(new_id));
936 g_free(new_id);
939 /* Add virtio block device */
940 devaddr = qemu_opt_get(legacy_opts, "addr");
941 if (devaddr && type != IF_VIRTIO) {
942 error_report("addr is not supported by this bus type");
943 goto fail;
946 if (type == IF_VIRTIO) {
947 QemuOpts *devopts;
948 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
949 &error_abort);
950 if (arch_type == QEMU_ARCH_S390X) {
951 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
952 } else {
953 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
955 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
956 &error_abort);
957 if (devaddr) {
958 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
962 filename = qemu_opt_get(legacy_opts, "file");
964 /* Check werror/rerror compatibility with if=... */
965 werror = qemu_opt_get(legacy_opts, "werror");
966 if (werror != NULL) {
967 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
968 type != IF_NONE) {
969 error_report("werror is not supported by this bus type");
970 goto fail;
972 qdict_put(bs_opts, "werror", qstring_from_str(werror));
975 rerror = qemu_opt_get(legacy_opts, "rerror");
976 if (rerror != NULL) {
977 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
978 type != IF_NONE) {
979 error_report("rerror is not supported by this bus type");
980 goto fail;
982 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
985 /* Actual block device init: Functionality shared with blockdev-add */
986 blk = blockdev_init(filename, bs_opts, &local_err);
987 bs_opts = NULL;
988 if (!blk) {
989 if (local_err) {
990 error_report_err(local_err);
992 goto fail;
993 } else {
994 assert(!local_err);
997 /* Create legacy DriveInfo */
998 dinfo = g_malloc0(sizeof(*dinfo));
999 dinfo->opts = all_opts;
1001 dinfo->cyls = cyls;
1002 dinfo->heads = heads;
1003 dinfo->secs = secs;
1004 dinfo->trans = translation;
1006 dinfo->type = type;
1007 dinfo->bus = bus_id;
1008 dinfo->unit = unit_id;
1009 dinfo->devaddr = devaddr;
1010 dinfo->serial = g_strdup(serial);
1012 blk_set_legacy_dinfo(blk, dinfo);
1014 switch(type) {
1015 case IF_IDE:
1016 case IF_SCSI:
1017 case IF_XEN:
1018 case IF_NONE:
1019 dinfo->media_cd = media == MEDIA_CDROM;
1020 break;
1021 default:
1022 break;
1025 fail:
1026 qemu_opts_del(legacy_opts);
1027 QDECREF(bs_opts);
1028 return dinfo;
1031 void hmp_commit(Monitor *mon, const QDict *qdict)
1033 const char *device = qdict_get_str(qdict, "device");
1034 BlockBackend *blk;
1035 int ret;
1037 if (!strcmp(device, "all")) {
1038 ret = bdrv_commit_all();
1039 } else {
1040 blk = blk_by_name(device);
1041 if (!blk) {
1042 monitor_printf(mon, "Device '%s' not found\n", device);
1043 return;
1045 ret = bdrv_commit(blk_bs(blk));
1047 if (ret < 0) {
1048 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1049 strerror(-ret));
1053 static void blockdev_do_action(int kind, void *data, Error **errp)
1055 TransactionAction action;
1056 TransactionActionList list;
1058 action.kind = kind;
1059 action.data = data;
1060 list.value = &action;
1061 list.next = NULL;
1062 qmp_transaction(&list, errp);
1065 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1066 bool has_node_name, const char *node_name,
1067 const char *snapshot_file,
1068 bool has_snapshot_node_name,
1069 const char *snapshot_node_name,
1070 bool has_format, const char *format,
1071 bool has_mode, NewImageMode mode, Error **errp)
1073 BlockdevSnapshot snapshot = {
1074 .has_device = has_device,
1075 .device = (char *) device,
1076 .has_node_name = has_node_name,
1077 .node_name = (char *) node_name,
1078 .snapshot_file = (char *) snapshot_file,
1079 .has_snapshot_node_name = has_snapshot_node_name,
1080 .snapshot_node_name = (char *) snapshot_node_name,
1081 .has_format = has_format,
1082 .format = (char *) format,
1083 .has_mode = has_mode,
1084 .mode = mode,
1086 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1087 &snapshot, errp);
1090 void qmp_blockdev_snapshot_internal_sync(const char *device,
1091 const char *name,
1092 Error **errp)
1094 BlockdevSnapshotInternal snapshot = {
1095 .device = (char *) device,
1096 .name = (char *) name
1099 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1100 &snapshot, errp);
1103 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1104 bool has_id,
1105 const char *id,
1106 bool has_name,
1107 const char *name,
1108 Error **errp)
1110 BlockDriverState *bs;
1111 BlockBackend *blk;
1112 AioContext *aio_context;
1113 QEMUSnapshotInfo sn;
1114 Error *local_err = NULL;
1115 SnapshotInfo *info = NULL;
1116 int ret;
1118 blk = blk_by_name(device);
1119 if (!blk) {
1120 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1121 "Device '%s' not found", device);
1122 return NULL;
1124 bs = blk_bs(blk);
1126 if (!has_id) {
1127 id = NULL;
1130 if (!has_name) {
1131 name = NULL;
1134 if (!id && !name) {
1135 error_setg(errp, "Name or id must be provided");
1136 return NULL;
1139 aio_context = bdrv_get_aio_context(bs);
1140 aio_context_acquire(aio_context);
1142 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1143 goto out_aio_context;
1146 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1147 if (local_err) {
1148 error_propagate(errp, local_err);
1149 goto out_aio_context;
1151 if (!ret) {
1152 error_setg(errp,
1153 "Snapshot with id '%s' and name '%s' does not exist on "
1154 "device '%s'",
1155 STR_OR_NULL(id), STR_OR_NULL(name), device);
1156 goto out_aio_context;
1159 bdrv_snapshot_delete(bs, id, name, &local_err);
1160 if (local_err) {
1161 error_propagate(errp, local_err);
1162 goto out_aio_context;
1165 aio_context_release(aio_context);
1167 info = g_new0(SnapshotInfo, 1);
1168 info->id = g_strdup(sn.id_str);
1169 info->name = g_strdup(sn.name);
1170 info->date_nsec = sn.date_nsec;
1171 info->date_sec = sn.date_sec;
1172 info->vm_state_size = sn.vm_state_size;
1173 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1174 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1176 return info;
1178 out_aio_context:
1179 aio_context_release(aio_context);
1180 return NULL;
1184 * block_dirty_bitmap_lookup:
1185 * Return a dirty bitmap (if present), after validating
1186 * the node reference and bitmap names.
1188 * @node: The name of the BDS node to search for bitmaps
1189 * @name: The name of the bitmap to search for
1190 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1191 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1192 * @errp: Output pointer for error information. Can be NULL.
1194 * @return: A bitmap object on success, or NULL on failure.
1196 static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1197 const char *name,
1198 BlockDriverState **pbs,
1199 AioContext **paio,
1200 Error **errp)
1202 BlockDriverState *bs;
1203 BdrvDirtyBitmap *bitmap;
1204 AioContext *aio_context;
1206 if (!node) {
1207 error_setg(errp, "Node cannot be NULL");
1208 return NULL;
1210 if (!name) {
1211 error_setg(errp, "Bitmap name cannot be NULL");
1212 return NULL;
1214 bs = bdrv_lookup_bs(node, node, NULL);
1215 if (!bs) {
1216 error_setg(errp, "Node '%s' not found", node);
1217 return NULL;
1220 aio_context = bdrv_get_aio_context(bs);
1221 aio_context_acquire(aio_context);
1223 bitmap = bdrv_find_dirty_bitmap(bs, name);
1224 if (!bitmap) {
1225 error_setg(errp, "Dirty bitmap '%s' not found", name);
1226 goto fail;
1229 if (pbs) {
1230 *pbs = bs;
1232 if (paio) {
1233 *paio = aio_context;
1234 } else {
1235 aio_context_release(aio_context);
1238 return bitmap;
1240 fail:
1241 aio_context_release(aio_context);
1242 return NULL;
1245 /* New and old BlockDriverState structs for atomic group operations */
1247 typedef struct BlkTransactionState BlkTransactionState;
1249 /* Only prepare() may fail. In a single transaction, only one of commit() or
1250 abort() will be called, clean() will always be called if it present. */
1251 typedef struct BdrvActionOps {
1252 /* Size of state struct, in bytes. */
1253 size_t instance_size;
1254 /* Prepare the work, must NOT be NULL. */
1255 void (*prepare)(BlkTransactionState *common, Error **errp);
1256 /* Commit the changes, can be NULL. */
1257 void (*commit)(BlkTransactionState *common);
1258 /* Abort the changes on fail, can be NULL. */
1259 void (*abort)(BlkTransactionState *common);
1260 /* Clean up resource in the end, can be NULL. */
1261 void (*clean)(BlkTransactionState *common);
1262 } BdrvActionOps;
1265 * This structure must be arranged as first member in child type, assuming
1266 * that compiler will also arrange it to the same address with parent instance.
1267 * Later it will be used in free().
1269 struct BlkTransactionState {
1270 TransactionAction *action;
1271 const BdrvActionOps *ops;
1272 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
1275 /* internal snapshot private data */
1276 typedef struct InternalSnapshotState {
1277 BlkTransactionState common;
1278 BlockDriverState *bs;
1279 AioContext *aio_context;
1280 QEMUSnapshotInfo sn;
1281 } InternalSnapshotState;
1283 static void internal_snapshot_prepare(BlkTransactionState *common,
1284 Error **errp)
1286 Error *local_err = NULL;
1287 const char *device;
1288 const char *name;
1289 BlockBackend *blk;
1290 BlockDriverState *bs;
1291 QEMUSnapshotInfo old_sn, *sn;
1292 bool ret;
1293 qemu_timeval tv;
1294 BlockdevSnapshotInternal *internal;
1295 InternalSnapshotState *state;
1296 int ret1;
1298 g_assert(common->action->kind ==
1299 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1300 internal = common->action->blockdev_snapshot_internal_sync;
1301 state = DO_UPCAST(InternalSnapshotState, common, common);
1303 /* 1. parse input */
1304 device = internal->device;
1305 name = internal->name;
1307 /* 2. check for validation */
1308 blk = blk_by_name(device);
1309 if (!blk) {
1310 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1311 "Device '%s' not found", device);
1312 return;
1314 bs = blk_bs(blk);
1316 /* AioContext is released in .clean() */
1317 state->aio_context = bdrv_get_aio_context(bs);
1318 aio_context_acquire(state->aio_context);
1320 if (!bdrv_is_inserted(bs)) {
1321 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1322 return;
1325 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1326 return;
1329 if (bdrv_is_read_only(bs)) {
1330 error_setg(errp, "Device '%s' is read only", device);
1331 return;
1334 if (!bdrv_can_snapshot(bs)) {
1335 error_setg(errp, "Block format '%s' used by device '%s' "
1336 "does not support internal snapshots",
1337 bs->drv->format_name, device);
1338 return;
1341 if (!strlen(name)) {
1342 error_setg(errp, "Name is empty");
1343 return;
1346 /* check whether a snapshot with name exist */
1347 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1348 &local_err);
1349 if (local_err) {
1350 error_propagate(errp, local_err);
1351 return;
1352 } else if (ret) {
1353 error_setg(errp,
1354 "Snapshot with name '%s' already exists on device '%s'",
1355 name, device);
1356 return;
1359 /* 3. take the snapshot */
1360 sn = &state->sn;
1361 pstrcpy(sn->name, sizeof(sn->name), name);
1362 qemu_gettimeofday(&tv);
1363 sn->date_sec = tv.tv_sec;
1364 sn->date_nsec = tv.tv_usec * 1000;
1365 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1367 ret1 = bdrv_snapshot_create(bs, sn);
1368 if (ret1 < 0) {
1369 error_setg_errno(errp, -ret1,
1370 "Failed to create snapshot '%s' on device '%s'",
1371 name, device);
1372 return;
1375 /* 4. succeed, mark a snapshot is created */
1376 state->bs = bs;
1379 static void internal_snapshot_abort(BlkTransactionState *common)
1381 InternalSnapshotState *state =
1382 DO_UPCAST(InternalSnapshotState, common, common);
1383 BlockDriverState *bs = state->bs;
1384 QEMUSnapshotInfo *sn = &state->sn;
1385 Error *local_error = NULL;
1387 if (!bs) {
1388 return;
1391 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1392 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1393 "device '%s' in abort: %s",
1394 sn->id_str,
1395 sn->name,
1396 bdrv_get_device_name(bs),
1397 error_get_pretty(local_error));
1398 error_free(local_error);
1402 static void internal_snapshot_clean(BlkTransactionState *common)
1404 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1405 common, common);
1407 if (state->aio_context) {
1408 aio_context_release(state->aio_context);
1412 /* external snapshot private data */
1413 typedef struct ExternalSnapshotState {
1414 BlkTransactionState common;
1415 BlockDriverState *old_bs;
1416 BlockDriverState *new_bs;
1417 AioContext *aio_context;
1418 } ExternalSnapshotState;
1420 static void external_snapshot_prepare(BlkTransactionState *common,
1421 Error **errp)
1423 int flags, ret;
1424 QDict *options;
1425 Error *local_err = NULL;
1426 bool has_device = false;
1427 const char *device;
1428 bool has_node_name = false;
1429 const char *node_name;
1430 bool has_snapshot_node_name = false;
1431 const char *snapshot_node_name;
1432 const char *new_image_file;
1433 const char *format = "qcow2";
1434 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1435 ExternalSnapshotState *state =
1436 DO_UPCAST(ExternalSnapshotState, common, common);
1437 TransactionAction *action = common->action;
1439 /* get parameters */
1440 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
1442 has_device = action->blockdev_snapshot_sync->has_device;
1443 device = action->blockdev_snapshot_sync->device;
1444 has_node_name = action->blockdev_snapshot_sync->has_node_name;
1445 node_name = action->blockdev_snapshot_sync->node_name;
1446 has_snapshot_node_name =
1447 action->blockdev_snapshot_sync->has_snapshot_node_name;
1448 snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name;
1450 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1451 if (action->blockdev_snapshot_sync->has_format) {
1452 format = action->blockdev_snapshot_sync->format;
1454 if (action->blockdev_snapshot_sync->has_mode) {
1455 mode = action->blockdev_snapshot_sync->mode;
1458 /* start processing */
1459 state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
1460 has_node_name ? node_name : NULL,
1461 &local_err);
1462 if (local_err) {
1463 error_propagate(errp, local_err);
1464 return;
1467 if (has_node_name && !has_snapshot_node_name) {
1468 error_setg(errp, "New snapshot node name missing");
1469 return;
1472 if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) {
1473 error_setg(errp, "New snapshot node name already existing");
1474 return;
1477 /* Acquire AioContext now so any threads operating on old_bs stop */
1478 state->aio_context = bdrv_get_aio_context(state->old_bs);
1479 aio_context_acquire(state->aio_context);
1481 if (!bdrv_is_inserted(state->old_bs)) {
1482 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1483 return;
1486 if (bdrv_op_is_blocked(state->old_bs,
1487 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1488 return;
1491 if (!bdrv_is_read_only(state->old_bs)) {
1492 if (bdrv_flush(state->old_bs)) {
1493 error_setg(errp, QERR_IO_ERROR);
1494 return;
1498 if (!bdrv_is_first_non_filter(state->old_bs)) {
1499 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1500 return;
1503 flags = state->old_bs->open_flags;
1505 /* create new image w/backing file */
1506 if (mode != NEW_IMAGE_MODE_EXISTING) {
1507 bdrv_img_create(new_image_file, format,
1508 state->old_bs->filename,
1509 state->old_bs->drv->format_name,
1510 NULL, -1, flags, &local_err, false);
1511 if (local_err) {
1512 error_propagate(errp, local_err);
1513 return;
1517 options = qdict_new();
1518 if (has_snapshot_node_name) {
1519 qdict_put(options, "node-name",
1520 qstring_from_str(snapshot_node_name));
1522 qdict_put(options, "driver", qstring_from_str(format));
1524 /* TODO Inherit bs->options or only take explicit options with an
1525 * extended QMP command? */
1526 assert(state->new_bs == NULL);
1527 ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
1528 flags | BDRV_O_NO_BACKING, &local_err);
1529 /* We will manually add the backing_hd field to the bs later */
1530 if (ret != 0) {
1531 error_propagate(errp, local_err);
1535 static void external_snapshot_commit(BlkTransactionState *common)
1537 ExternalSnapshotState *state =
1538 DO_UPCAST(ExternalSnapshotState, common, common);
1540 bdrv_set_aio_context(state->new_bs, state->aio_context);
1542 /* This removes our old bs and adds the new bs */
1543 bdrv_append(state->new_bs, state->old_bs);
1544 /* We don't need (or want) to use the transactional
1545 * bdrv_reopen_multiple() across all the entries at once, because we
1546 * don't want to abort all of them if one of them fails the reopen */
1547 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1548 NULL);
1550 aio_context_release(state->aio_context);
1553 static void external_snapshot_abort(BlkTransactionState *common)
1555 ExternalSnapshotState *state =
1556 DO_UPCAST(ExternalSnapshotState, common, common);
1557 if (state->new_bs) {
1558 bdrv_unref(state->new_bs);
1560 if (state->aio_context) {
1561 aio_context_release(state->aio_context);
1565 typedef struct DriveBackupState {
1566 BlkTransactionState common;
1567 BlockDriverState *bs;
1568 AioContext *aio_context;
1569 BlockJob *job;
1570 } DriveBackupState;
1572 static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1574 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1575 BlockDriverState *bs;
1576 BlockBackend *blk;
1577 DriveBackup *backup;
1578 Error *local_err = NULL;
1580 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1581 backup = common->action->drive_backup;
1583 blk = blk_by_name(backup->device);
1584 if (!blk) {
1585 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1586 "Device '%s' not found", backup->device);
1587 return;
1589 bs = blk_bs(blk);
1591 /* AioContext is released in .clean() */
1592 state->aio_context = bdrv_get_aio_context(bs);
1593 aio_context_acquire(state->aio_context);
1595 qmp_drive_backup(backup->device, backup->target,
1596 backup->has_format, backup->format,
1597 backup->sync,
1598 backup->has_mode, backup->mode,
1599 backup->has_speed, backup->speed,
1600 backup->has_bitmap, backup->bitmap,
1601 backup->has_on_source_error, backup->on_source_error,
1602 backup->has_on_target_error, backup->on_target_error,
1603 &local_err);
1604 if (local_err) {
1605 error_propagate(errp, local_err);
1606 return;
1609 state->bs = bs;
1610 state->job = state->bs->job;
1613 static void drive_backup_abort(BlkTransactionState *common)
1615 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1616 BlockDriverState *bs = state->bs;
1618 /* Only cancel if it's the job we started */
1619 if (bs && bs->job && bs->job == state->job) {
1620 block_job_cancel_sync(bs->job);
1624 static void drive_backup_clean(BlkTransactionState *common)
1626 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1628 if (state->aio_context) {
1629 aio_context_release(state->aio_context);
1633 typedef struct BlockdevBackupState {
1634 BlkTransactionState common;
1635 BlockDriverState *bs;
1636 BlockJob *job;
1637 AioContext *aio_context;
1638 } BlockdevBackupState;
1640 static void blockdev_backup_prepare(BlkTransactionState *common, Error **errp)
1642 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1643 BlockdevBackup *backup;
1644 BlockDriverState *bs, *target;
1645 BlockBackend *blk;
1646 Error *local_err = NULL;
1648 assert(common->action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1649 backup = common->action->blockdev_backup;
1651 blk = blk_by_name(backup->device);
1652 if (!blk) {
1653 error_setg(errp, "Device '%s' not found", backup->device);
1654 return;
1656 bs = blk_bs(blk);
1658 blk = blk_by_name(backup->target);
1659 if (!blk) {
1660 error_setg(errp, "Device '%s' not found", backup->target);
1661 return;
1663 target = blk_bs(blk);
1665 /* AioContext is released in .clean() */
1666 state->aio_context = bdrv_get_aio_context(bs);
1667 if (state->aio_context != bdrv_get_aio_context(target)) {
1668 state->aio_context = NULL;
1669 error_setg(errp, "Backup between two IO threads is not implemented");
1670 return;
1672 aio_context_acquire(state->aio_context);
1674 qmp_blockdev_backup(backup->device, backup->target,
1675 backup->sync,
1676 backup->has_speed, backup->speed,
1677 backup->has_on_source_error, backup->on_source_error,
1678 backup->has_on_target_error, backup->on_target_error,
1679 &local_err);
1680 if (local_err) {
1681 error_propagate(errp, local_err);
1682 return;
1685 state->bs = bs;
1686 state->job = state->bs->job;
1689 static void blockdev_backup_abort(BlkTransactionState *common)
1691 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1692 BlockDriverState *bs = state->bs;
1694 /* Only cancel if it's the job we started */
1695 if (bs && bs->job && bs->job == state->job) {
1696 block_job_cancel_sync(bs->job);
1700 static void blockdev_backup_clean(BlkTransactionState *common)
1702 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1704 if (state->aio_context) {
1705 aio_context_release(state->aio_context);
1709 static void abort_prepare(BlkTransactionState *common, Error **errp)
1711 error_setg(errp, "Transaction aborted using Abort action");
1714 static void abort_commit(BlkTransactionState *common)
1716 g_assert_not_reached(); /* this action never succeeds */
1719 static const BdrvActionOps actions[] = {
1720 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1721 .instance_size = sizeof(ExternalSnapshotState),
1722 .prepare = external_snapshot_prepare,
1723 .commit = external_snapshot_commit,
1724 .abort = external_snapshot_abort,
1726 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1727 .instance_size = sizeof(DriveBackupState),
1728 .prepare = drive_backup_prepare,
1729 .abort = drive_backup_abort,
1730 .clean = drive_backup_clean,
1732 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
1733 .instance_size = sizeof(BlockdevBackupState),
1734 .prepare = blockdev_backup_prepare,
1735 .abort = blockdev_backup_abort,
1736 .clean = blockdev_backup_clean,
1738 [TRANSACTION_ACTION_KIND_ABORT] = {
1739 .instance_size = sizeof(BlkTransactionState),
1740 .prepare = abort_prepare,
1741 .commit = abort_commit,
1743 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1744 .instance_size = sizeof(InternalSnapshotState),
1745 .prepare = internal_snapshot_prepare,
1746 .abort = internal_snapshot_abort,
1747 .clean = internal_snapshot_clean,
1752 * 'Atomic' group operations. The operations are performed as a set, and if
1753 * any fail then we roll back all operations in the group.
1755 void qmp_transaction(TransactionActionList *dev_list, Error **errp)
1757 TransactionActionList *dev_entry = dev_list;
1758 BlkTransactionState *state, *next;
1759 Error *local_err = NULL;
1761 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
1762 QSIMPLEQ_INIT(&snap_bdrv_states);
1764 /* drain all i/o before any operations */
1765 bdrv_drain_all();
1767 /* We don't do anything in this loop that commits us to the operations */
1768 while (NULL != dev_entry) {
1769 TransactionAction *dev_info = NULL;
1770 const BdrvActionOps *ops;
1772 dev_info = dev_entry->value;
1773 dev_entry = dev_entry->next;
1775 assert(dev_info->kind < ARRAY_SIZE(actions));
1777 ops = &actions[dev_info->kind];
1778 assert(ops->instance_size > 0);
1780 state = g_malloc0(ops->instance_size);
1781 state->ops = ops;
1782 state->action = dev_info;
1783 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
1785 state->ops->prepare(state, &local_err);
1786 if (local_err) {
1787 error_propagate(errp, local_err);
1788 goto delete_and_fail;
1792 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1793 if (state->ops->commit) {
1794 state->ops->commit(state);
1798 /* success */
1799 goto exit;
1801 delete_and_fail:
1802 /* failure, and it is all-or-none; roll back all operations */
1803 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1804 if (state->ops->abort) {
1805 state->ops->abort(state);
1808 exit:
1809 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1810 if (state->ops->clean) {
1811 state->ops->clean(state);
1813 g_free(state);
1818 static void eject_device(BlockBackend *blk, int force, Error **errp)
1820 BlockDriverState *bs = blk_bs(blk);
1821 AioContext *aio_context;
1823 aio_context = bdrv_get_aio_context(bs);
1824 aio_context_acquire(aio_context);
1826 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
1827 goto out;
1829 if (!blk_dev_has_removable_media(blk)) {
1830 error_setg(errp, "Device '%s' is not removable",
1831 bdrv_get_device_name(bs));
1832 goto out;
1835 if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) {
1836 blk_dev_eject_request(blk, force);
1837 if (!force) {
1838 error_setg(errp, "Device '%s' is locked",
1839 bdrv_get_device_name(bs));
1840 goto out;
1844 bdrv_close(bs);
1846 out:
1847 aio_context_release(aio_context);
1850 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1852 BlockBackend *blk;
1854 blk = blk_by_name(device);
1855 if (!blk) {
1856 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1857 "Device '%s' not found", device);
1858 return;
1861 eject_device(blk, force, errp);
1864 void qmp_block_passwd(bool has_device, const char *device,
1865 bool has_node_name, const char *node_name,
1866 const char *password, Error **errp)
1868 Error *local_err = NULL;
1869 BlockDriverState *bs;
1870 AioContext *aio_context;
1872 bs = bdrv_lookup_bs(has_device ? device : NULL,
1873 has_node_name ? node_name : NULL,
1874 &local_err);
1875 if (local_err) {
1876 error_propagate(errp, local_err);
1877 return;
1880 aio_context = bdrv_get_aio_context(bs);
1881 aio_context_acquire(aio_context);
1883 bdrv_add_key(bs, password, errp);
1885 aio_context_release(aio_context);
1888 /* Assumes AioContext is held */
1889 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1890 int bdrv_flags, const char *format,
1891 const char *password, Error **errp)
1893 Error *local_err = NULL;
1894 QDict *options = NULL;
1895 int ret;
1897 if (format) {
1898 options = qdict_new();
1899 qdict_put(options, "driver", qstring_from_str(format));
1902 ret = bdrv_open(&bs, filename, NULL, options, bdrv_flags, &local_err);
1903 if (ret < 0) {
1904 error_propagate(errp, local_err);
1905 return;
1908 bdrv_add_key(bs, password, errp);
1911 void qmp_change_blockdev(const char *device, const char *filename,
1912 const char *format, Error **errp)
1914 BlockBackend *blk;
1915 BlockDriverState *bs;
1916 AioContext *aio_context;
1917 int bdrv_flags;
1918 Error *err = NULL;
1920 blk = blk_by_name(device);
1921 if (!blk) {
1922 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1923 "Device '%s' not found", device);
1924 return;
1926 bs = blk_bs(blk);
1928 aio_context = bdrv_get_aio_context(bs);
1929 aio_context_acquire(aio_context);
1931 eject_device(blk, 0, &err);
1932 if (err) {
1933 error_propagate(errp, err);
1934 goto out;
1937 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1938 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1940 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, format, NULL, errp);
1942 out:
1943 aio_context_release(aio_context);
1946 /* throttling disk I/O limits */
1947 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1948 int64_t bps_wr,
1949 int64_t iops,
1950 int64_t iops_rd,
1951 int64_t iops_wr,
1952 bool has_bps_max,
1953 int64_t bps_max,
1954 bool has_bps_rd_max,
1955 int64_t bps_rd_max,
1956 bool has_bps_wr_max,
1957 int64_t bps_wr_max,
1958 bool has_iops_max,
1959 int64_t iops_max,
1960 bool has_iops_rd_max,
1961 int64_t iops_rd_max,
1962 bool has_iops_wr_max,
1963 int64_t iops_wr_max,
1964 bool has_iops_size,
1965 int64_t iops_size,
1966 bool has_group,
1967 const char *group, Error **errp)
1969 ThrottleConfig cfg;
1970 BlockDriverState *bs;
1971 BlockBackend *blk;
1972 AioContext *aio_context;
1974 blk = blk_by_name(device);
1975 if (!blk) {
1976 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1977 "Device '%s' not found", device);
1978 return;
1980 bs = blk_bs(blk);
1982 memset(&cfg, 0, sizeof(cfg));
1983 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1984 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
1985 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
1987 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1988 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
1989 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
1991 if (has_bps_max) {
1992 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
1994 if (has_bps_rd_max) {
1995 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
1997 if (has_bps_wr_max) {
1998 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2000 if (has_iops_max) {
2001 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2003 if (has_iops_rd_max) {
2004 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2006 if (has_iops_wr_max) {
2007 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2010 if (has_iops_size) {
2011 cfg.op_size = iops_size;
2014 if (!check_throttle_config(&cfg, errp)) {
2015 return;
2018 aio_context = bdrv_get_aio_context(bs);
2019 aio_context_acquire(aio_context);
2021 if (throttle_enabled(&cfg)) {
2022 /* Enable I/O limits if they're not enabled yet, otherwise
2023 * just update the throttling group. */
2024 if (!bs->io_limits_enabled) {
2025 bdrv_io_limits_enable(bs, has_group ? group : device);
2026 } else if (has_group) {
2027 bdrv_io_limits_update_group(bs, group);
2029 /* Set the new throttling configuration */
2030 bdrv_set_io_limits(bs, &cfg);
2031 } else if (bs->io_limits_enabled) {
2032 /* If all throttling settings are set to 0, disable I/O limits */
2033 bdrv_io_limits_disable(bs);
2036 aio_context_release(aio_context);
2039 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2040 bool has_granularity, uint32_t granularity,
2041 Error **errp)
2043 AioContext *aio_context;
2044 BlockDriverState *bs;
2046 if (!name || name[0] == '\0') {
2047 error_setg(errp, "Bitmap name cannot be empty");
2048 return;
2051 bs = bdrv_lookup_bs(node, node, errp);
2052 if (!bs) {
2053 return;
2056 aio_context = bdrv_get_aio_context(bs);
2057 aio_context_acquire(aio_context);
2059 if (has_granularity) {
2060 if (granularity < 512 || !is_power_of_2(granularity)) {
2061 error_setg(errp, "Granularity must be power of 2 "
2062 "and at least 512");
2063 goto out;
2065 } else {
2066 /* Default to cluster size, if available: */
2067 granularity = bdrv_get_default_bitmap_granularity(bs);
2070 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2072 out:
2073 aio_context_release(aio_context);
2076 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2077 Error **errp)
2079 AioContext *aio_context;
2080 BlockDriverState *bs;
2081 BdrvDirtyBitmap *bitmap;
2083 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2084 if (!bitmap || !bs) {
2085 return;
2088 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2089 error_setg(errp,
2090 "Bitmap '%s' is currently frozen and cannot be removed",
2091 name);
2092 goto out;
2094 bdrv_dirty_bitmap_make_anon(bitmap);
2095 bdrv_release_dirty_bitmap(bs, bitmap);
2097 out:
2098 aio_context_release(aio_context);
2102 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2103 * immediately after a full backup operation.
2105 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2106 Error **errp)
2108 AioContext *aio_context;
2109 BdrvDirtyBitmap *bitmap;
2110 BlockDriverState *bs;
2112 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2113 if (!bitmap || !bs) {
2114 return;
2117 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2118 error_setg(errp,
2119 "Bitmap '%s' is currently frozen and cannot be modified",
2120 name);
2121 goto out;
2122 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2123 error_setg(errp,
2124 "Bitmap '%s' is currently disabled and cannot be cleared",
2125 name);
2126 goto out;
2129 bdrv_clear_dirty_bitmap(bitmap);
2131 out:
2132 aio_context_release(aio_context);
2135 void hmp_drive_del(Monitor *mon, const QDict *qdict)
2137 const char *id = qdict_get_str(qdict, "id");
2138 BlockBackend *blk;
2139 BlockDriverState *bs;
2140 AioContext *aio_context;
2141 Error *local_err = NULL;
2143 blk = blk_by_name(id);
2144 if (!blk) {
2145 error_report("Device '%s' not found", id);
2146 return;
2148 bs = blk_bs(blk);
2150 if (!blk_legacy_dinfo(blk)) {
2151 error_report("Deleting device added with blockdev-add"
2152 " is not supported");
2153 return;
2156 aio_context = bdrv_get_aio_context(bs);
2157 aio_context_acquire(aio_context);
2159 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2160 error_report_err(local_err);
2161 aio_context_release(aio_context);
2162 return;
2165 bdrv_close(bs);
2167 /* if we have a device attached to this BlockDriverState
2168 * then we need to make the drive anonymous until the device
2169 * can be removed. If this is a drive with no device backing
2170 * then we can just get rid of the block driver state right here.
2172 if (blk_get_attached_dev(blk)) {
2173 blk_hide_on_behalf_of_hmp_drive_del(blk);
2174 /* Further I/O must not pause the guest */
2175 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
2176 BLOCKDEV_ON_ERROR_REPORT);
2177 } else {
2178 blk_unref(blk);
2181 aio_context_release(aio_context);
2184 void qmp_block_resize(bool has_device, const char *device,
2185 bool has_node_name, const char *node_name,
2186 int64_t size, Error **errp)
2188 Error *local_err = NULL;
2189 BlockDriverState *bs;
2190 AioContext *aio_context;
2191 int ret;
2193 bs = bdrv_lookup_bs(has_device ? device : NULL,
2194 has_node_name ? node_name : NULL,
2195 &local_err);
2196 if (local_err) {
2197 error_propagate(errp, local_err);
2198 return;
2201 aio_context = bdrv_get_aio_context(bs);
2202 aio_context_acquire(aio_context);
2204 if (!bdrv_is_first_non_filter(bs)) {
2205 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2206 goto out;
2209 if (size < 0) {
2210 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2211 goto out;
2214 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2215 error_setg(errp, QERR_DEVICE_IN_USE, device);
2216 goto out;
2219 /* complete all in-flight operations before resizing the device */
2220 bdrv_drain_all();
2222 ret = bdrv_truncate(bs, size);
2223 switch (ret) {
2224 case 0:
2225 break;
2226 case -ENOMEDIUM:
2227 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2228 break;
2229 case -ENOTSUP:
2230 error_setg(errp, QERR_UNSUPPORTED);
2231 break;
2232 case -EACCES:
2233 error_setg(errp, "Device '%s' is read only", device);
2234 break;
2235 case -EBUSY:
2236 error_setg(errp, QERR_DEVICE_IN_USE, device);
2237 break;
2238 default:
2239 error_setg_errno(errp, -ret, "Could not resize");
2240 break;
2243 out:
2244 aio_context_release(aio_context);
2247 static void block_job_cb(void *opaque, int ret)
2249 /* Note that this function may be executed from another AioContext besides
2250 * the QEMU main loop. If you need to access anything that assumes the
2251 * QEMU global mutex, use a BH or introduce a mutex.
2254 BlockDriverState *bs = opaque;
2255 const char *msg = NULL;
2257 trace_block_job_cb(bs, bs->job, ret);
2259 assert(bs->job);
2261 if (ret < 0) {
2262 msg = strerror(-ret);
2265 if (block_job_is_cancelled(bs->job)) {
2266 block_job_event_cancelled(bs->job);
2267 } else {
2268 block_job_event_completed(bs->job, msg);
2271 bdrv_put_ref_bh_schedule(bs);
2274 void qmp_block_stream(const char *device,
2275 bool has_base, const char *base,
2276 bool has_backing_file, const char *backing_file,
2277 bool has_speed, int64_t speed,
2278 bool has_on_error, BlockdevOnError on_error,
2279 Error **errp)
2281 BlockBackend *blk;
2282 BlockDriverState *bs;
2283 BlockDriverState *base_bs = NULL;
2284 AioContext *aio_context;
2285 Error *local_err = NULL;
2286 const char *base_name = NULL;
2288 if (!has_on_error) {
2289 on_error = BLOCKDEV_ON_ERROR_REPORT;
2292 blk = blk_by_name(device);
2293 if (!blk) {
2294 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2295 "Device '%s' not found", device);
2296 return;
2298 bs = blk_bs(blk);
2300 aio_context = bdrv_get_aio_context(bs);
2301 aio_context_acquire(aio_context);
2303 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2304 goto out;
2307 if (has_base) {
2308 base_bs = bdrv_find_backing_image(bs, base);
2309 if (base_bs == NULL) {
2310 error_setg(errp, QERR_BASE_NOT_FOUND, base);
2311 goto out;
2313 assert(bdrv_get_aio_context(base_bs) == aio_context);
2314 base_name = base;
2317 /* if we are streaming the entire chain, the result will have no backing
2318 * file, and specifying one is therefore an error */
2319 if (base_bs == NULL && has_backing_file) {
2320 error_setg(errp, "backing file specified, but streaming the "
2321 "entire chain");
2322 goto out;
2325 /* backing_file string overrides base bs filename */
2326 base_name = has_backing_file ? backing_file : base_name;
2328 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
2329 on_error, block_job_cb, bs, &local_err);
2330 if (local_err) {
2331 error_propagate(errp, local_err);
2332 goto out;
2335 trace_qmp_block_stream(bs, bs->job);
2337 out:
2338 aio_context_release(aio_context);
2341 void qmp_block_commit(const char *device,
2342 bool has_base, const char *base,
2343 bool has_top, const char *top,
2344 bool has_backing_file, const char *backing_file,
2345 bool has_speed, int64_t speed,
2346 Error **errp)
2348 BlockBackend *blk;
2349 BlockDriverState *bs;
2350 BlockDriverState *base_bs, *top_bs;
2351 AioContext *aio_context;
2352 Error *local_err = NULL;
2353 /* This will be part of the QMP command, if/when the
2354 * BlockdevOnError change for blkmirror makes it in
2356 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
2358 if (!has_speed) {
2359 speed = 0;
2362 /* Important Note:
2363 * libvirt relies on the DeviceNotFound error class in order to probe for
2364 * live commit feature versions; for this to work, we must make sure to
2365 * perform the device lookup before any generic errors that may occur in a
2366 * scenario in which all optional arguments are omitted. */
2367 blk = blk_by_name(device);
2368 if (!blk) {
2369 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2370 "Device '%s' not found", device);
2371 return;
2373 bs = blk_bs(blk);
2375 aio_context = bdrv_get_aio_context(bs);
2376 aio_context_acquire(aio_context);
2378 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
2379 goto out;
2382 /* default top_bs is the active layer */
2383 top_bs = bs;
2385 if (has_top && top) {
2386 if (strcmp(bs->filename, top) != 0) {
2387 top_bs = bdrv_find_backing_image(bs, top);
2391 if (top_bs == NULL) {
2392 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
2393 goto out;
2396 assert(bdrv_get_aio_context(top_bs) == aio_context);
2398 if (has_base && base) {
2399 base_bs = bdrv_find_backing_image(top_bs, base);
2400 } else {
2401 base_bs = bdrv_find_base(top_bs);
2404 if (base_bs == NULL) {
2405 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
2406 goto out;
2409 assert(bdrv_get_aio_context(base_bs) == aio_context);
2411 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2412 goto out;
2415 /* Do not allow attempts to commit an image into itself */
2416 if (top_bs == base_bs) {
2417 error_setg(errp, "cannot commit an image into itself");
2418 goto out;
2421 if (top_bs == bs) {
2422 if (has_backing_file) {
2423 error_setg(errp, "'backing-file' specified,"
2424 " but 'top' is the active layer");
2425 goto out;
2427 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
2428 bs, &local_err);
2429 } else {
2430 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
2431 has_backing_file ? backing_file : NULL, &local_err);
2433 if (local_err != NULL) {
2434 error_propagate(errp, local_err);
2435 goto out;
2438 out:
2439 aio_context_release(aio_context);
2442 void qmp_drive_backup(const char *device, const char *target,
2443 bool has_format, const char *format,
2444 enum MirrorSyncMode sync,
2445 bool has_mode, enum NewImageMode mode,
2446 bool has_speed, int64_t speed,
2447 bool has_bitmap, const char *bitmap,
2448 bool has_on_source_error, BlockdevOnError on_source_error,
2449 bool has_on_target_error, BlockdevOnError on_target_error,
2450 Error **errp)
2452 BlockBackend *blk;
2453 BlockDriverState *bs;
2454 BlockDriverState *target_bs;
2455 BlockDriverState *source = NULL;
2456 BdrvDirtyBitmap *bmap = NULL;
2457 AioContext *aio_context;
2458 QDict *options = NULL;
2459 Error *local_err = NULL;
2460 int flags;
2461 int64_t size;
2462 int ret;
2464 if (!has_speed) {
2465 speed = 0;
2467 if (!has_on_source_error) {
2468 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2470 if (!has_on_target_error) {
2471 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2473 if (!has_mode) {
2474 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2477 blk = blk_by_name(device);
2478 if (!blk) {
2479 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2480 "Device '%s' not found", device);
2481 return;
2483 bs = blk_bs(blk);
2485 aio_context = bdrv_get_aio_context(bs);
2486 aio_context_acquire(aio_context);
2488 /* Although backup_run has this check too, we need to use bs->drv below, so
2489 * do an early check redundantly. */
2490 if (!bdrv_is_inserted(bs)) {
2491 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2492 goto out;
2495 if (!has_format) {
2496 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2499 /* Early check to avoid creating target */
2500 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
2501 goto out;
2504 flags = bs->open_flags | BDRV_O_RDWR;
2506 /* See if we have a backing HD we can use to create our new image
2507 * on top of. */
2508 if (sync == MIRROR_SYNC_MODE_TOP) {
2509 source = backing_bs(bs);
2510 if (!source) {
2511 sync = MIRROR_SYNC_MODE_FULL;
2514 if (sync == MIRROR_SYNC_MODE_NONE) {
2515 source = bs;
2518 size = bdrv_getlength(bs);
2519 if (size < 0) {
2520 error_setg_errno(errp, -size, "bdrv_getlength failed");
2521 goto out;
2524 if (mode != NEW_IMAGE_MODE_EXISTING) {
2525 assert(format);
2526 if (source) {
2527 bdrv_img_create(target, format, source->filename,
2528 source->drv->format_name, NULL,
2529 size, flags, &local_err, false);
2530 } else {
2531 bdrv_img_create(target, format, NULL, NULL, NULL,
2532 size, flags, &local_err, false);
2536 if (local_err) {
2537 error_propagate(errp, local_err);
2538 goto out;
2541 if (format) {
2542 options = qdict_new();
2543 qdict_put(options, "driver", qstring_from_str(format));
2546 target_bs = NULL;
2547 ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
2548 if (ret < 0) {
2549 error_propagate(errp, local_err);
2550 goto out;
2553 bdrv_set_aio_context(target_bs, aio_context);
2555 if (has_bitmap) {
2556 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
2557 if (!bmap) {
2558 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
2559 goto out;
2563 backup_start(bs, target_bs, speed, sync, bmap,
2564 on_source_error, on_target_error,
2565 block_job_cb, bs, &local_err);
2566 if (local_err != NULL) {
2567 bdrv_unref(target_bs);
2568 error_propagate(errp, local_err);
2569 goto out;
2572 out:
2573 aio_context_release(aio_context);
2576 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
2578 return bdrv_named_nodes_list(errp);
2581 void qmp_blockdev_backup(const char *device, const char *target,
2582 enum MirrorSyncMode sync,
2583 bool has_speed, int64_t speed,
2584 bool has_on_source_error,
2585 BlockdevOnError on_source_error,
2586 bool has_on_target_error,
2587 BlockdevOnError on_target_error,
2588 Error **errp)
2590 BlockBackend *blk;
2591 BlockDriverState *bs;
2592 BlockDriverState *target_bs;
2593 Error *local_err = NULL;
2594 AioContext *aio_context;
2596 if (!has_speed) {
2597 speed = 0;
2599 if (!has_on_source_error) {
2600 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2602 if (!has_on_target_error) {
2603 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2606 blk = blk_by_name(device);
2607 if (!blk) {
2608 error_setg(errp, "Device '%s' not found", device);
2609 return;
2611 bs = blk_bs(blk);
2613 aio_context = bdrv_get_aio_context(bs);
2614 aio_context_acquire(aio_context);
2616 blk = blk_by_name(target);
2617 if (!blk) {
2618 error_setg(errp, "Device '%s' not found", target);
2619 goto out;
2621 target_bs = blk_bs(blk);
2623 bdrv_ref(target_bs);
2624 bdrv_set_aio_context(target_bs, aio_context);
2625 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
2626 on_target_error, block_job_cb, bs, &local_err);
2627 if (local_err != NULL) {
2628 bdrv_unref(target_bs);
2629 error_propagate(errp, local_err);
2631 out:
2632 aio_context_release(aio_context);
2635 void qmp_drive_mirror(const char *device, const char *target,
2636 bool has_format, const char *format,
2637 bool has_node_name, const char *node_name,
2638 bool has_replaces, const char *replaces,
2639 enum MirrorSyncMode sync,
2640 bool has_mode, enum NewImageMode mode,
2641 bool has_speed, int64_t speed,
2642 bool has_granularity, uint32_t granularity,
2643 bool has_buf_size, int64_t buf_size,
2644 bool has_on_source_error, BlockdevOnError on_source_error,
2645 bool has_on_target_error, BlockdevOnError on_target_error,
2646 bool has_unmap, bool unmap,
2647 Error **errp)
2649 BlockBackend *blk;
2650 BlockDriverState *bs;
2651 BlockDriverState *source, *target_bs;
2652 AioContext *aio_context;
2653 Error *local_err = NULL;
2654 QDict *options;
2655 int flags;
2656 int64_t size;
2657 int ret;
2659 if (!has_speed) {
2660 speed = 0;
2662 if (!has_on_source_error) {
2663 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2665 if (!has_on_target_error) {
2666 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2668 if (!has_mode) {
2669 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2671 if (!has_granularity) {
2672 granularity = 0;
2674 if (!has_buf_size) {
2675 buf_size = 0;
2677 if (!has_unmap) {
2678 unmap = true;
2681 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
2682 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2683 "a value in range [512B, 64MB]");
2684 return;
2686 if (granularity & (granularity - 1)) {
2687 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2688 "power of 2");
2689 return;
2692 blk = blk_by_name(device);
2693 if (!blk) {
2694 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2695 "Device '%s' not found", device);
2696 return;
2698 bs = blk_bs(blk);
2700 aio_context = bdrv_get_aio_context(bs);
2701 aio_context_acquire(aio_context);
2703 if (!bdrv_is_inserted(bs)) {
2704 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2705 goto out;
2708 if (!has_format) {
2709 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2712 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
2713 goto out;
2716 flags = bs->open_flags | BDRV_O_RDWR;
2717 source = backing_bs(bs);
2718 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
2719 sync = MIRROR_SYNC_MODE_FULL;
2721 if (sync == MIRROR_SYNC_MODE_NONE) {
2722 source = bs;
2725 size = bdrv_getlength(bs);
2726 if (size < 0) {
2727 error_setg_errno(errp, -size, "bdrv_getlength failed");
2728 goto out;
2731 if (has_replaces) {
2732 BlockDriverState *to_replace_bs;
2733 AioContext *replace_aio_context;
2734 int64_t replace_size;
2736 if (!has_node_name) {
2737 error_setg(errp, "a node-name must be provided when replacing a"
2738 " named node of the graph");
2739 goto out;
2742 to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
2744 if (!to_replace_bs) {
2745 error_propagate(errp, local_err);
2746 goto out;
2749 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
2750 aio_context_acquire(replace_aio_context);
2751 replace_size = bdrv_getlength(to_replace_bs);
2752 aio_context_release(replace_aio_context);
2754 if (size != replace_size) {
2755 error_setg(errp, "cannot replace image with a mirror image of "
2756 "different size");
2757 goto out;
2761 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
2762 && mode != NEW_IMAGE_MODE_EXISTING)
2764 /* create new image w/o backing file */
2765 assert(format);
2766 bdrv_img_create(target, format,
2767 NULL, NULL, NULL, size, flags, &local_err, false);
2768 } else {
2769 switch (mode) {
2770 case NEW_IMAGE_MODE_EXISTING:
2771 break;
2772 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2773 /* create new image with backing file */
2774 bdrv_img_create(target, format,
2775 source->filename,
2776 source->drv->format_name,
2777 NULL, size, flags, &local_err, false);
2778 break;
2779 default:
2780 abort();
2784 if (local_err) {
2785 error_propagate(errp, local_err);
2786 goto out;
2789 options = qdict_new();
2790 if (has_node_name) {
2791 qdict_put(options, "node-name", qstring_from_str(node_name));
2793 if (format) {
2794 qdict_put(options, "driver", qstring_from_str(format));
2797 /* Mirroring takes care of copy-on-write using the source's backing
2798 * file.
2800 target_bs = NULL;
2801 ret = bdrv_open(&target_bs, target, NULL, options,
2802 flags | BDRV_O_NO_BACKING, &local_err);
2803 if (ret < 0) {
2804 error_propagate(errp, local_err);
2805 goto out;
2808 bdrv_set_aio_context(target_bs, aio_context);
2810 /* pass the node name to replace to mirror start since it's loose coupling
2811 * and will allow to check whether the node still exist at mirror completion
2813 mirror_start(bs, target_bs,
2814 has_replaces ? replaces : NULL,
2815 speed, granularity, buf_size, sync,
2816 on_source_error, on_target_error,
2817 unmap,
2818 block_job_cb, bs, &local_err);
2819 if (local_err != NULL) {
2820 bdrv_unref(target_bs);
2821 error_propagate(errp, local_err);
2822 goto out;
2825 out:
2826 aio_context_release(aio_context);
2829 /* Get the block job for a given device name and acquire its AioContext */
2830 static BlockJob *find_block_job(const char *device, AioContext **aio_context,
2831 Error **errp)
2833 BlockBackend *blk;
2834 BlockDriverState *bs;
2836 blk = blk_by_name(device);
2837 if (!blk) {
2838 goto notfound;
2840 bs = blk_bs(blk);
2842 *aio_context = bdrv_get_aio_context(bs);
2843 aio_context_acquire(*aio_context);
2845 if (!bs->job) {
2846 aio_context_release(*aio_context);
2847 goto notfound;
2850 return bs->job;
2852 notfound:
2853 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
2854 "No active block job on device '%s'", device);
2855 *aio_context = NULL;
2856 return NULL;
2859 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2861 AioContext *aio_context;
2862 BlockJob *job = find_block_job(device, &aio_context, errp);
2864 if (!job) {
2865 return;
2868 block_job_set_speed(job, speed, errp);
2869 aio_context_release(aio_context);
2872 void qmp_block_job_cancel(const char *device,
2873 bool has_force, bool force, Error **errp)
2875 AioContext *aio_context;
2876 BlockJob *job = find_block_job(device, &aio_context, errp);
2878 if (!job) {
2879 return;
2882 if (!has_force) {
2883 force = false;
2886 if (job->user_paused && !force) {
2887 error_setg(errp, "The block job for device '%s' is currently paused",
2888 device);
2889 goto out;
2892 trace_qmp_block_job_cancel(job);
2893 block_job_cancel(job);
2894 out:
2895 aio_context_release(aio_context);
2898 void qmp_block_job_pause(const char *device, Error **errp)
2900 AioContext *aio_context;
2901 BlockJob *job = find_block_job(device, &aio_context, errp);
2903 if (!job || job->user_paused) {
2904 return;
2907 job->user_paused = true;
2908 trace_qmp_block_job_pause(job);
2909 block_job_pause(job);
2910 aio_context_release(aio_context);
2913 void qmp_block_job_resume(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 = false;
2923 trace_qmp_block_job_resume(job);
2924 block_job_resume(job);
2925 aio_context_release(aio_context);
2928 void qmp_block_job_complete(const char *device, Error **errp)
2930 AioContext *aio_context;
2931 BlockJob *job = find_block_job(device, &aio_context, errp);
2933 if (!job) {
2934 return;
2937 trace_qmp_block_job_complete(job);
2938 block_job_complete(job, errp);
2939 aio_context_release(aio_context);
2942 void qmp_change_backing_file(const char *device,
2943 const char *image_node_name,
2944 const char *backing_file,
2945 Error **errp)
2947 BlockBackend *blk;
2948 BlockDriverState *bs = NULL;
2949 AioContext *aio_context;
2950 BlockDriverState *image_bs = NULL;
2951 Error *local_err = NULL;
2952 bool ro;
2953 int open_flags;
2954 int ret;
2956 blk = blk_by_name(device);
2957 if (!blk) {
2958 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2959 "Device '%s' not found", device);
2960 return;
2962 bs = blk_bs(blk);
2964 aio_context = bdrv_get_aio_context(bs);
2965 aio_context_acquire(aio_context);
2967 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
2968 if (local_err) {
2969 error_propagate(errp, local_err);
2970 goto out;
2973 if (!image_bs) {
2974 error_setg(errp, "image file not found");
2975 goto out;
2978 if (bdrv_find_base(image_bs) == image_bs) {
2979 error_setg(errp, "not allowing backing file change on an image "
2980 "without a backing file");
2981 goto out;
2984 /* even though we are not necessarily operating on bs, we need it to
2985 * determine if block ops are currently prohibited on the chain */
2986 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
2987 goto out;
2990 /* final sanity check */
2991 if (!bdrv_chain_contains(bs, image_bs)) {
2992 error_setg(errp, "'%s' and image file are not in the same chain",
2993 device);
2994 goto out;
2997 /* if not r/w, reopen to make r/w */
2998 open_flags = image_bs->open_flags;
2999 ro = bdrv_is_read_only(image_bs);
3001 if (ro) {
3002 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3003 if (local_err) {
3004 error_propagate(errp, local_err);
3005 goto out;
3009 ret = bdrv_change_backing_file(image_bs, backing_file,
3010 image_bs->drv ? image_bs->drv->format_name : "");
3012 if (ret < 0) {
3013 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3014 backing_file);
3015 /* don't exit here, so we can try to restore open flags if
3016 * appropriate */
3019 if (ro) {
3020 bdrv_reopen(image_bs, open_flags, &local_err);
3021 if (local_err) {
3022 error_propagate(errp, local_err); /* will preserve prior errp */
3026 out:
3027 aio_context_release(aio_context);
3030 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3032 QmpOutputVisitor *ov = qmp_output_visitor_new();
3033 BlockBackend *blk;
3034 QObject *obj;
3035 QDict *qdict;
3036 Error *local_err = NULL;
3038 /* Require an ID in the top level */
3039 if (!options->has_id) {
3040 error_setg(errp, "Block device needs an ID");
3041 goto fail;
3044 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3045 * cache.direct=false instead of silently switching to aio=threads, except
3046 * when called from drive_new().
3048 * For now, simply forbidding the combination for all drivers will do. */
3049 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3050 bool direct = options->has_cache &&
3051 options->cache->has_direct &&
3052 options->cache->direct;
3053 if (!direct) {
3054 error_setg(errp, "aio=native requires cache.direct=true");
3055 goto fail;
3059 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
3060 &options, NULL, &local_err);
3061 if (local_err) {
3062 error_propagate(errp, local_err);
3063 goto fail;
3066 obj = qmp_output_get_qobject(ov);
3067 qdict = qobject_to_qdict(obj);
3069 qdict_flatten(qdict);
3071 blk = blockdev_init(NULL, qdict, &local_err);
3072 if (local_err) {
3073 error_propagate(errp, local_err);
3074 goto fail;
3077 if (bdrv_key_required(blk_bs(blk))) {
3078 blk_unref(blk);
3079 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3080 goto fail;
3083 fail:
3084 qmp_output_visitor_cleanup(ov);
3087 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3089 BlockJobInfoList *head = NULL, **p_next = &head;
3090 BlockDriverState *bs;
3092 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3093 AioContext *aio_context = bdrv_get_aio_context(bs);
3095 aio_context_acquire(aio_context);
3097 if (bs->job) {
3098 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3099 elem->value = block_job_query(bs->job);
3100 *p_next = elem;
3101 p_next = &elem->next;
3104 aio_context_release(aio_context);
3107 return head;
3110 QemuOptsList qemu_common_drive_opts = {
3111 .name = "drive",
3112 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3113 .desc = {
3115 .name = "snapshot",
3116 .type = QEMU_OPT_BOOL,
3117 .help = "enable/disable snapshot mode",
3119 .name = "discard",
3120 .type = QEMU_OPT_STRING,
3121 .help = "discard operation (ignore/off, unmap/on)",
3123 .name = BDRV_OPT_CACHE_WB,
3124 .type = QEMU_OPT_BOOL,
3125 .help = "enables writeback mode for any caches",
3127 .name = BDRV_OPT_CACHE_DIRECT,
3128 .type = QEMU_OPT_BOOL,
3129 .help = "enables use of O_DIRECT (bypass the host page cache)",
3131 .name = BDRV_OPT_CACHE_NO_FLUSH,
3132 .type = QEMU_OPT_BOOL,
3133 .help = "ignore any flush requests for the device",
3135 .name = "aio",
3136 .type = QEMU_OPT_STRING,
3137 .help = "host AIO implementation (threads, native)",
3139 .name = "format",
3140 .type = QEMU_OPT_STRING,
3141 .help = "disk format (raw, qcow2, ...)",
3143 .name = "rerror",
3144 .type = QEMU_OPT_STRING,
3145 .help = "read error action",
3147 .name = "werror",
3148 .type = QEMU_OPT_STRING,
3149 .help = "write error action",
3151 .name = "read-only",
3152 .type = QEMU_OPT_BOOL,
3153 .help = "open drive file as read-only",
3155 .name = "throttling.iops-total",
3156 .type = QEMU_OPT_NUMBER,
3157 .help = "limit total I/O operations per second",
3159 .name = "throttling.iops-read",
3160 .type = QEMU_OPT_NUMBER,
3161 .help = "limit read operations per second",
3163 .name = "throttling.iops-write",
3164 .type = QEMU_OPT_NUMBER,
3165 .help = "limit write operations per second",
3167 .name = "throttling.bps-total",
3168 .type = QEMU_OPT_NUMBER,
3169 .help = "limit total bytes per second",
3171 .name = "throttling.bps-read",
3172 .type = QEMU_OPT_NUMBER,
3173 .help = "limit read bytes per second",
3175 .name = "throttling.bps-write",
3176 .type = QEMU_OPT_NUMBER,
3177 .help = "limit write bytes per second",
3179 .name = "throttling.iops-total-max",
3180 .type = QEMU_OPT_NUMBER,
3181 .help = "I/O operations burst",
3183 .name = "throttling.iops-read-max",
3184 .type = QEMU_OPT_NUMBER,
3185 .help = "I/O operations read burst",
3187 .name = "throttling.iops-write-max",
3188 .type = QEMU_OPT_NUMBER,
3189 .help = "I/O operations write burst",
3191 .name = "throttling.bps-total-max",
3192 .type = QEMU_OPT_NUMBER,
3193 .help = "total bytes burst",
3195 .name = "throttling.bps-read-max",
3196 .type = QEMU_OPT_NUMBER,
3197 .help = "total bytes read burst",
3199 .name = "throttling.bps-write-max",
3200 .type = QEMU_OPT_NUMBER,
3201 .help = "total bytes write burst",
3203 .name = "throttling.iops-size",
3204 .type = QEMU_OPT_NUMBER,
3205 .help = "when limiting by iops max size of an I/O in bytes",
3207 .name = "throttling.group",
3208 .type = QEMU_OPT_STRING,
3209 .help = "name of the block throttling group",
3211 .name = "copy-on-read",
3212 .type = QEMU_OPT_BOOL,
3213 .help = "copy read data from backing file into image file",
3215 .name = "detect-zeroes",
3216 .type = QEMU_OPT_STRING,
3217 .help = "try to optimize zero writes (off, on, unmap)",
3219 { /* end of list */ }
3223 QemuOptsList qemu_drive_opts = {
3224 .name = "drive",
3225 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3226 .desc = {
3228 * no elements => accept any params
3229 * validation will happen later
3231 { /* end of list */ }