block: define 'clock_type' for the accounting code
[qemu.git] / blockdev.c
blob977ffd47ff2a187a0a45d87c0edc5a192afc901d
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 if (bs) {
128 aio_context = bdrv_get_aio_context(bs);
129 aio_context_acquire(aio_context);
131 if (bs->job) {
132 block_job_cancel(bs->job);
135 aio_context_release(aio_context);
138 dinfo->auto_del = 1;
141 void blockdev_auto_del(BlockBackend *blk)
143 DriveInfo *dinfo = blk_legacy_dinfo(blk);
145 if (dinfo && dinfo->auto_del) {
146 blk_unref(blk);
151 * Returns the current mapping of how many units per bus
152 * a particular interface can support.
154 * A positive integer indicates n units per bus.
155 * 0 implies the mapping has not been established.
156 * -1 indicates an invalid BlockInterfaceType was given.
158 int drive_get_max_devs(BlockInterfaceType type)
160 if (type >= IF_IDE && type < IF_COUNT) {
161 return if_max_devs[type];
164 return -1;
167 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
169 int max_devs = if_max_devs[type];
170 return max_devs ? index / max_devs : 0;
173 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
175 int max_devs = if_max_devs[type];
176 return max_devs ? index % max_devs : index;
179 QemuOpts *drive_def(const char *optstr)
181 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
184 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
185 const char *optstr)
187 QemuOpts *opts;
189 opts = drive_def(optstr);
190 if (!opts) {
191 return NULL;
193 if (type != IF_DEFAULT) {
194 qemu_opt_set(opts, "if", if_name[type], &error_abort);
196 if (index >= 0) {
197 qemu_opt_set_number(opts, "index", index, &error_abort);
199 if (file)
200 qemu_opt_set(opts, "file", file, &error_abort);
201 return opts;
204 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
206 BlockBackend *blk;
207 DriveInfo *dinfo;
209 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
210 dinfo = blk_legacy_dinfo(blk);
211 if (dinfo && dinfo->type == type
212 && dinfo->bus == bus && dinfo->unit == unit) {
213 return dinfo;
217 return NULL;
220 bool drive_check_orphaned(void)
222 BlockBackend *blk;
223 DriveInfo *dinfo;
224 bool rs = false;
226 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
227 dinfo = blk_legacy_dinfo(blk);
228 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
229 /* Unless this is a default drive, this may be an oversight. */
230 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
231 dinfo->type != IF_NONE) {
232 fprintf(stderr, "Warning: Orphaned drive without device: "
233 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
234 blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
235 if_name[dinfo->type], dinfo->bus, dinfo->unit);
236 rs = true;
240 return rs;
243 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
245 return drive_get(type,
246 drive_index_to_bus_id(type, index),
247 drive_index_to_unit_id(type, index));
250 int drive_get_max_bus(BlockInterfaceType type)
252 int max_bus;
253 BlockBackend *blk;
254 DriveInfo *dinfo;
256 max_bus = -1;
257 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
258 dinfo = blk_legacy_dinfo(blk);
259 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
260 max_bus = dinfo->bus;
263 return max_bus;
266 /* Get a block device. This should only be used for single-drive devices
267 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
268 appropriate bus. */
269 DriveInfo *drive_get_next(BlockInterfaceType type)
271 static int next_block_unit[IF_COUNT];
273 return drive_get(type, 0, next_block_unit[type]++);
276 static void bdrv_format_print(void *opaque, const char *name)
278 error_printf(" %s", name);
281 typedef struct {
282 QEMUBH *bh;
283 BlockDriverState *bs;
284 } BDRVPutRefBH;
286 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
288 if (!strcmp(buf, "ignore")) {
289 return BLOCKDEV_ON_ERROR_IGNORE;
290 } else if (!is_read && !strcmp(buf, "enospc")) {
291 return BLOCKDEV_ON_ERROR_ENOSPC;
292 } else if (!strcmp(buf, "stop")) {
293 return BLOCKDEV_ON_ERROR_STOP;
294 } else if (!strcmp(buf, "report")) {
295 return BLOCKDEV_ON_ERROR_REPORT;
296 } else {
297 error_setg(errp, "'%s' invalid %s error action",
298 buf, is_read ? "read" : "write");
299 return -1;
303 static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
305 if (throttle_conflicting(cfg)) {
306 error_setg(errp, "bps/iops/max total values and read/write values"
307 " cannot be used at the same time");
308 return false;
311 if (!throttle_is_valid(cfg)) {
312 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
313 return false;
316 if (throttle_max_is_missing_limit(cfg)) {
317 error_setg(errp, "bps_max/iops_max require corresponding"
318 " bps/iops values");
319 return false;
322 return true;
325 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
327 /* All parameters but @opts are optional and may be set to NULL. */
328 static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
329 const char **throttling_group, ThrottleConfig *throttle_cfg,
330 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
332 const char *discard;
333 Error *local_error = NULL;
334 const char *aio;
336 if (bdrv_flags) {
337 if (!qemu_opt_get_bool(opts, "read-only", false)) {
338 *bdrv_flags |= BDRV_O_RDWR;
340 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
341 *bdrv_flags |= BDRV_O_COPY_ON_READ;
344 if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
345 if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
346 error_setg(errp, "Invalid discard option");
347 return;
351 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) {
352 *bdrv_flags |= BDRV_O_CACHE_WB;
354 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
355 *bdrv_flags |= BDRV_O_NOCACHE;
357 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
358 *bdrv_flags |= BDRV_O_NO_FLUSH;
361 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
362 if (!strcmp(aio, "native")) {
363 *bdrv_flags |= BDRV_O_NATIVE_AIO;
364 } else if (!strcmp(aio, "threads")) {
365 /* this is the default */
366 } else {
367 error_setg(errp, "invalid aio option");
368 return;
373 /* disk I/O throttling */
374 if (throttling_group) {
375 *throttling_group = qemu_opt_get(opts, "throttling.group");
378 if (throttle_cfg) {
379 memset(throttle_cfg, 0, sizeof(*throttle_cfg));
380 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
381 qemu_opt_get_number(opts, "throttling.bps-total", 0);
382 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
383 qemu_opt_get_number(opts, "throttling.bps-read", 0);
384 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
385 qemu_opt_get_number(opts, "throttling.bps-write", 0);
386 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
387 qemu_opt_get_number(opts, "throttling.iops-total", 0);
388 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
389 qemu_opt_get_number(opts, "throttling.iops-read", 0);
390 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
391 qemu_opt_get_number(opts, "throttling.iops-write", 0);
393 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
394 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
395 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
396 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
397 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
398 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
399 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
400 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
401 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
402 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
403 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
404 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
406 throttle_cfg->op_size =
407 qemu_opt_get_number(opts, "throttling.iops-size", 0);
409 if (!check_throttle_config(throttle_cfg, errp)) {
410 return;
414 if (detect_zeroes) {
415 *detect_zeroes =
416 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
417 qemu_opt_get(opts, "detect-zeroes"),
418 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
419 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
420 &local_error);
421 if (local_error) {
422 error_propagate(errp, local_error);
423 return;
426 if (bdrv_flags &&
427 *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
428 !(*bdrv_flags & BDRV_O_UNMAP))
430 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
431 "without setting discard operation to unmap");
432 return;
437 /* Takes the ownership of bs_opts */
438 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
439 Error **errp)
441 const char *buf;
442 int bdrv_flags = 0;
443 int on_read_error, on_write_error;
444 BlockBackend *blk;
445 BlockDriverState *bs;
446 ThrottleConfig cfg;
447 int snapshot = 0;
448 Error *error = NULL;
449 QemuOpts *opts;
450 const char *id;
451 bool has_driver_specific_opts;
452 BlockdevDetectZeroesOptions detect_zeroes =
453 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
454 const char *throttling_group = NULL;
456 /* Check common options by copying from bs_opts to opts, all other options
457 * stay in bs_opts for processing by bdrv_open(). */
458 id = qdict_get_try_str(bs_opts, "id");
459 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
460 if (error) {
461 error_propagate(errp, error);
462 goto err_no_opts;
465 qemu_opts_absorb_qdict(opts, bs_opts, &error);
466 if (error) {
467 error_propagate(errp, error);
468 goto early_err;
471 if (id) {
472 qdict_del(bs_opts, "id");
475 has_driver_specific_opts = !!qdict_size(bs_opts);
477 /* extract parameters */
478 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
480 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
481 &detect_zeroes, &error);
482 if (error) {
483 error_propagate(errp, error);
484 goto early_err;
487 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
488 if (is_help_option(buf)) {
489 error_printf("Supported formats:");
490 bdrv_iterate_format(bdrv_format_print, NULL);
491 error_printf("\n");
492 goto early_err;
495 if (qdict_haskey(bs_opts, "driver")) {
496 error_setg(errp, "Cannot specify both 'driver' and 'format'");
497 goto early_err;
499 qdict_put(bs_opts, "driver", qstring_from_str(buf));
502 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
503 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
504 on_write_error = parse_block_error_action(buf, 0, &error);
505 if (error) {
506 error_propagate(errp, error);
507 goto early_err;
511 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
512 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
513 on_read_error = parse_block_error_action(buf, 1, &error);
514 if (error) {
515 error_propagate(errp, error);
516 goto early_err;
520 if (snapshot) {
521 /* always use cache=unsafe with snapshot */
522 bdrv_flags &= ~BDRV_O_CACHE_MASK;
523 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
526 /* init */
527 if ((!file || !*file) && !has_driver_specific_opts) {
528 BlockBackendRootState *blk_rs;
530 blk = blk_new(qemu_opts_id(opts), errp);
531 if (!blk) {
532 goto early_err;
535 blk_rs = blk_get_root_state(blk);
536 blk_rs->open_flags = bdrv_flags;
537 blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR);
538 blk_rs->detect_zeroes = detect_zeroes;
540 if (throttle_enabled(&cfg)) {
541 if (!throttling_group) {
542 throttling_group = blk_name(blk);
544 blk_rs->throttle_group = g_strdup(throttling_group);
545 blk_rs->throttle_state = throttle_group_incref(throttling_group);
546 blk_rs->throttle_state->cfg = cfg;
549 QDECREF(bs_opts);
550 } else {
551 if (file && !*file) {
552 file = NULL;
555 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
556 errp);
557 if (!blk) {
558 goto err_no_bs_opts;
560 bs = blk_bs(blk);
562 bs->detect_zeroes = detect_zeroes;
564 /* disk I/O throttling */
565 if (throttle_enabled(&cfg)) {
566 if (!throttling_group) {
567 throttling_group = blk_name(blk);
569 bdrv_io_limits_enable(bs, throttling_group);
570 bdrv_set_io_limits(bs, &cfg);
573 if (bdrv_key_required(bs)) {
574 autostart = 0;
578 blk_set_on_error(blk, on_read_error, on_write_error);
580 err_no_bs_opts:
581 qemu_opts_del(opts);
582 return blk;
584 early_err:
585 qemu_opts_del(opts);
586 err_no_opts:
587 QDECREF(bs_opts);
588 return NULL;
591 static QemuOptsList qemu_root_bds_opts;
593 /* Takes the ownership of bs_opts */
594 static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
596 BlockDriverState *bs;
597 QemuOpts *opts;
598 Error *local_error = NULL;
599 BlockdevDetectZeroesOptions detect_zeroes;
600 int ret;
601 int bdrv_flags = 0;
603 opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
604 if (!opts) {
605 goto fail;
608 qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
609 if (local_error) {
610 error_propagate(errp, local_error);
611 goto fail;
614 extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
615 &detect_zeroes, &local_error);
616 if (local_error) {
617 error_propagate(errp, local_error);
618 goto fail;
621 bs = NULL;
622 ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp);
623 if (ret < 0) {
624 goto fail_no_bs_opts;
627 bs->detect_zeroes = detect_zeroes;
629 fail_no_bs_opts:
630 qemu_opts_del(opts);
631 return bs;
633 fail:
634 qemu_opts_del(opts);
635 QDECREF(bs_opts);
636 return NULL;
639 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
640 Error **errp)
642 const char *value;
644 value = qemu_opt_get(opts, from);
645 if (value) {
646 if (qemu_opt_find(opts, to)) {
647 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
648 "same time", to, from);
649 return;
653 /* rename all items in opts */
654 while ((value = qemu_opt_get(opts, from))) {
655 qemu_opt_set(opts, to, value, &error_abort);
656 qemu_opt_unset(opts, from);
660 QemuOptsList qemu_legacy_drive_opts = {
661 .name = "drive",
662 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
663 .desc = {
665 .name = "bus",
666 .type = QEMU_OPT_NUMBER,
667 .help = "bus number",
669 .name = "unit",
670 .type = QEMU_OPT_NUMBER,
671 .help = "unit number (i.e. lun for scsi)",
673 .name = "index",
674 .type = QEMU_OPT_NUMBER,
675 .help = "index number",
677 .name = "media",
678 .type = QEMU_OPT_STRING,
679 .help = "media type (disk, cdrom)",
681 .name = "if",
682 .type = QEMU_OPT_STRING,
683 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
685 .name = "cyls",
686 .type = QEMU_OPT_NUMBER,
687 .help = "number of cylinders (ide disk geometry)",
689 .name = "heads",
690 .type = QEMU_OPT_NUMBER,
691 .help = "number of heads (ide disk geometry)",
693 .name = "secs",
694 .type = QEMU_OPT_NUMBER,
695 .help = "number of sectors (ide disk geometry)",
697 .name = "trans",
698 .type = QEMU_OPT_STRING,
699 .help = "chs translation (auto, lba, none)",
701 .name = "boot",
702 .type = QEMU_OPT_BOOL,
703 .help = "(deprecated, ignored)",
705 .name = "addr",
706 .type = QEMU_OPT_STRING,
707 .help = "pci address (virtio only)",
709 .name = "serial",
710 .type = QEMU_OPT_STRING,
711 .help = "disk serial number",
713 .name = "file",
714 .type = QEMU_OPT_STRING,
715 .help = "file name",
718 /* Options that are passed on, but have special semantics with -drive */
720 .name = "read-only",
721 .type = QEMU_OPT_BOOL,
722 .help = "open drive file as read-only",
724 .name = "rerror",
725 .type = QEMU_OPT_STRING,
726 .help = "read error action",
728 .name = "werror",
729 .type = QEMU_OPT_STRING,
730 .help = "write error action",
732 .name = "copy-on-read",
733 .type = QEMU_OPT_BOOL,
734 .help = "copy read data from backing file into image file",
737 { /* end of list */ }
741 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
743 const char *value;
744 BlockBackend *blk;
745 DriveInfo *dinfo = NULL;
746 QDict *bs_opts;
747 QemuOpts *legacy_opts;
748 DriveMediaType media = MEDIA_DISK;
749 BlockInterfaceType type;
750 int cyls, heads, secs, translation;
751 int max_devs, bus_id, unit_id, index;
752 const char *devaddr;
753 const char *werror, *rerror;
754 bool read_only = false;
755 bool copy_on_read;
756 const char *serial;
757 const char *filename;
758 Error *local_err = NULL;
759 int i;
761 /* Change legacy command line options into QMP ones */
762 static const struct {
763 const char *from;
764 const char *to;
765 } opt_renames[] = {
766 { "iops", "throttling.iops-total" },
767 { "iops_rd", "throttling.iops-read" },
768 { "iops_wr", "throttling.iops-write" },
770 { "bps", "throttling.bps-total" },
771 { "bps_rd", "throttling.bps-read" },
772 { "bps_wr", "throttling.bps-write" },
774 { "iops_max", "throttling.iops-total-max" },
775 { "iops_rd_max", "throttling.iops-read-max" },
776 { "iops_wr_max", "throttling.iops-write-max" },
778 { "bps_max", "throttling.bps-total-max" },
779 { "bps_rd_max", "throttling.bps-read-max" },
780 { "bps_wr_max", "throttling.bps-write-max" },
782 { "iops_size", "throttling.iops-size" },
784 { "group", "throttling.group" },
786 { "readonly", "read-only" },
789 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
790 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
791 &local_err);
792 if (local_err) {
793 error_report_err(local_err);
794 return NULL;
798 value = qemu_opt_get(all_opts, "cache");
799 if (value) {
800 int flags = 0;
802 if (bdrv_parse_cache_flags(value, &flags) != 0) {
803 error_report("invalid cache option");
804 return NULL;
807 /* Specific options take precedence */
808 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
809 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
810 !!(flags & BDRV_O_CACHE_WB), &error_abort);
812 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
813 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
814 !!(flags & BDRV_O_NOCACHE), &error_abort);
816 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
817 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
818 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
820 qemu_opt_unset(all_opts, "cache");
823 /* Get a QDict for processing the options */
824 bs_opts = qdict_new();
825 qemu_opts_to_qdict(all_opts, bs_opts);
827 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
828 &error_abort);
829 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
830 if (local_err) {
831 error_report_err(local_err);
832 goto fail;
835 /* Deprecated option boot=[on|off] */
836 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
837 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
838 "ignored. Future versions will reject this parameter. Please "
839 "update your scripts.\n");
842 /* Media type */
843 value = qemu_opt_get(legacy_opts, "media");
844 if (value) {
845 if (!strcmp(value, "disk")) {
846 media = MEDIA_DISK;
847 } else if (!strcmp(value, "cdrom")) {
848 media = MEDIA_CDROM;
849 read_only = true;
850 } else {
851 error_report("'%s' invalid media", value);
852 goto fail;
856 /* copy-on-read is disabled with a warning for read-only devices */
857 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
858 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
860 if (read_only && copy_on_read) {
861 error_report("warning: disabling copy-on-read on read-only drive");
862 copy_on_read = false;
865 qdict_put(bs_opts, "read-only",
866 qstring_from_str(read_only ? "on" : "off"));
867 qdict_put(bs_opts, "copy-on-read",
868 qstring_from_str(copy_on_read ? "on" :"off"));
870 /* Controller type */
871 value = qemu_opt_get(legacy_opts, "if");
872 if (value) {
873 for (type = 0;
874 type < IF_COUNT && strcmp(value, if_name[type]);
875 type++) {
877 if (type == IF_COUNT) {
878 error_report("unsupported bus type '%s'", value);
879 goto fail;
881 } else {
882 type = block_default_type;
885 /* Geometry */
886 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
887 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
888 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
890 if (cyls || heads || secs) {
891 if (cyls < 1) {
892 error_report("invalid physical cyls number");
893 goto fail;
895 if (heads < 1) {
896 error_report("invalid physical heads number");
897 goto fail;
899 if (secs < 1) {
900 error_report("invalid physical secs number");
901 goto fail;
905 translation = BIOS_ATA_TRANSLATION_AUTO;
906 value = qemu_opt_get(legacy_opts, "trans");
907 if (value != NULL) {
908 if (!cyls) {
909 error_report("'%s' trans must be used with cyls, heads and secs",
910 value);
911 goto fail;
913 if (!strcmp(value, "none")) {
914 translation = BIOS_ATA_TRANSLATION_NONE;
915 } else if (!strcmp(value, "lba")) {
916 translation = BIOS_ATA_TRANSLATION_LBA;
917 } else if (!strcmp(value, "large")) {
918 translation = BIOS_ATA_TRANSLATION_LARGE;
919 } else if (!strcmp(value, "rechs")) {
920 translation = BIOS_ATA_TRANSLATION_RECHS;
921 } else if (!strcmp(value, "auto")) {
922 translation = BIOS_ATA_TRANSLATION_AUTO;
923 } else {
924 error_report("'%s' invalid translation type", value);
925 goto fail;
929 if (media == MEDIA_CDROM) {
930 if (cyls || secs || heads) {
931 error_report("CHS can't be set with media=cdrom");
932 goto fail;
936 /* Device address specified by bus/unit or index.
937 * If none was specified, try to find the first free one. */
938 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
939 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
940 index = qemu_opt_get_number(legacy_opts, "index", -1);
942 max_devs = if_max_devs[type];
944 if (index != -1) {
945 if (bus_id != 0 || unit_id != -1) {
946 error_report("index cannot be used with bus and unit");
947 goto fail;
949 bus_id = drive_index_to_bus_id(type, index);
950 unit_id = drive_index_to_unit_id(type, index);
953 if (unit_id == -1) {
954 unit_id = 0;
955 while (drive_get(type, bus_id, unit_id) != NULL) {
956 unit_id++;
957 if (max_devs && unit_id >= max_devs) {
958 unit_id -= max_devs;
959 bus_id++;
964 if (max_devs && unit_id >= max_devs) {
965 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
966 goto fail;
969 if (drive_get(type, bus_id, unit_id) != NULL) {
970 error_report("drive with bus=%d, unit=%d (index=%d) exists",
971 bus_id, unit_id, index);
972 goto fail;
975 /* Serial number */
976 serial = qemu_opt_get(legacy_opts, "serial");
978 /* no id supplied -> create one */
979 if (qemu_opts_id(all_opts) == NULL) {
980 char *new_id;
981 const char *mediastr = "";
982 if (type == IF_IDE || type == IF_SCSI) {
983 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
985 if (max_devs) {
986 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
987 mediastr, unit_id);
988 } else {
989 new_id = g_strdup_printf("%s%s%i", if_name[type],
990 mediastr, unit_id);
992 qdict_put(bs_opts, "id", qstring_from_str(new_id));
993 g_free(new_id);
996 /* Add virtio block device */
997 devaddr = qemu_opt_get(legacy_opts, "addr");
998 if (devaddr && type != IF_VIRTIO) {
999 error_report("addr is not supported by this bus type");
1000 goto fail;
1003 if (type == IF_VIRTIO) {
1004 QemuOpts *devopts;
1005 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1006 &error_abort);
1007 if (arch_type == QEMU_ARCH_S390X) {
1008 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
1009 } else {
1010 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
1012 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1013 &error_abort);
1014 if (devaddr) {
1015 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
1019 filename = qemu_opt_get(legacy_opts, "file");
1021 /* Check werror/rerror compatibility with if=... */
1022 werror = qemu_opt_get(legacy_opts, "werror");
1023 if (werror != NULL) {
1024 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1025 type != IF_NONE) {
1026 error_report("werror is not supported by this bus type");
1027 goto fail;
1029 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1032 rerror = qemu_opt_get(legacy_opts, "rerror");
1033 if (rerror != NULL) {
1034 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1035 type != IF_NONE) {
1036 error_report("rerror is not supported by this bus type");
1037 goto fail;
1039 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1042 /* Actual block device init: Functionality shared with blockdev-add */
1043 blk = blockdev_init(filename, bs_opts, &local_err);
1044 bs_opts = NULL;
1045 if (!blk) {
1046 if (local_err) {
1047 error_report_err(local_err);
1049 goto fail;
1050 } else {
1051 assert(!local_err);
1054 /* Create legacy DriveInfo */
1055 dinfo = g_malloc0(sizeof(*dinfo));
1056 dinfo->opts = all_opts;
1058 dinfo->cyls = cyls;
1059 dinfo->heads = heads;
1060 dinfo->secs = secs;
1061 dinfo->trans = translation;
1063 dinfo->type = type;
1064 dinfo->bus = bus_id;
1065 dinfo->unit = unit_id;
1066 dinfo->devaddr = devaddr;
1067 dinfo->serial = g_strdup(serial);
1069 blk_set_legacy_dinfo(blk, dinfo);
1071 switch(type) {
1072 case IF_IDE:
1073 case IF_SCSI:
1074 case IF_XEN:
1075 case IF_NONE:
1076 dinfo->media_cd = media == MEDIA_CDROM;
1077 break;
1078 default:
1079 break;
1082 fail:
1083 qemu_opts_del(legacy_opts);
1084 QDECREF(bs_opts);
1085 return dinfo;
1088 void hmp_commit(Monitor *mon, const QDict *qdict)
1090 const char *device = qdict_get_str(qdict, "device");
1091 BlockBackend *blk;
1092 int ret;
1094 if (!strcmp(device, "all")) {
1095 ret = bdrv_commit_all();
1096 } else {
1097 BlockDriverState *bs;
1098 AioContext *aio_context;
1100 blk = blk_by_name(device);
1101 if (!blk) {
1102 monitor_printf(mon, "Device '%s' not found\n", device);
1103 return;
1105 if (!blk_is_available(blk)) {
1106 monitor_printf(mon, "Device '%s' has no medium\n", device);
1107 return;
1110 bs = blk_bs(blk);
1111 aio_context = bdrv_get_aio_context(bs);
1112 aio_context_acquire(aio_context);
1114 ret = bdrv_commit(bs);
1116 aio_context_release(aio_context);
1118 if (ret < 0) {
1119 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1120 strerror(-ret));
1124 static void blockdev_do_action(TransactionActionKind type, void *data,
1125 Error **errp)
1127 TransactionAction action;
1128 TransactionActionList list;
1130 action.type = type;
1131 action.u.data = data;
1132 list.value = &action;
1133 list.next = NULL;
1134 qmp_transaction(&list, false, NULL, errp);
1137 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1138 bool has_node_name, const char *node_name,
1139 const char *snapshot_file,
1140 bool has_snapshot_node_name,
1141 const char *snapshot_node_name,
1142 bool has_format, const char *format,
1143 bool has_mode, NewImageMode mode, Error **errp)
1145 BlockdevSnapshotSync snapshot = {
1146 .has_device = has_device,
1147 .device = (char *) device,
1148 .has_node_name = has_node_name,
1149 .node_name = (char *) node_name,
1150 .snapshot_file = (char *) snapshot_file,
1151 .has_snapshot_node_name = has_snapshot_node_name,
1152 .snapshot_node_name = (char *) snapshot_node_name,
1153 .has_format = has_format,
1154 .format = (char *) format,
1155 .has_mode = has_mode,
1156 .mode = mode,
1158 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1159 &snapshot, errp);
1162 void qmp_blockdev_snapshot(const char *node, const char *overlay,
1163 Error **errp)
1165 BlockdevSnapshot snapshot_data = {
1166 .node = (char *) node,
1167 .overlay = (char *) overlay
1170 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
1171 &snapshot_data, errp);
1174 void qmp_blockdev_snapshot_internal_sync(const char *device,
1175 const char *name,
1176 Error **errp)
1178 BlockdevSnapshotInternal snapshot = {
1179 .device = (char *) device,
1180 .name = (char *) name
1183 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1184 &snapshot, errp);
1187 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1188 bool has_id,
1189 const char *id,
1190 bool has_name,
1191 const char *name,
1192 Error **errp)
1194 BlockDriverState *bs;
1195 BlockBackend *blk;
1196 AioContext *aio_context;
1197 QEMUSnapshotInfo sn;
1198 Error *local_err = NULL;
1199 SnapshotInfo *info = NULL;
1200 int ret;
1202 blk = blk_by_name(device);
1203 if (!blk) {
1204 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1205 "Device '%s' not found", device);
1206 return NULL;
1209 aio_context = blk_get_aio_context(blk);
1210 aio_context_acquire(aio_context);
1212 if (!has_id) {
1213 id = NULL;
1216 if (!has_name) {
1217 name = NULL;
1220 if (!id && !name) {
1221 error_setg(errp, "Name or id must be provided");
1222 goto out_aio_context;
1225 if (!blk_is_available(blk)) {
1226 error_setg(errp, "Device '%s' has no medium", device);
1227 goto out_aio_context;
1229 bs = blk_bs(blk);
1231 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1232 goto out_aio_context;
1235 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1236 if (local_err) {
1237 error_propagate(errp, local_err);
1238 goto out_aio_context;
1240 if (!ret) {
1241 error_setg(errp,
1242 "Snapshot with id '%s' and name '%s' does not exist on "
1243 "device '%s'",
1244 STR_OR_NULL(id), STR_OR_NULL(name), device);
1245 goto out_aio_context;
1248 bdrv_snapshot_delete(bs, id, name, &local_err);
1249 if (local_err) {
1250 error_propagate(errp, local_err);
1251 goto out_aio_context;
1254 aio_context_release(aio_context);
1256 info = g_new0(SnapshotInfo, 1);
1257 info->id = g_strdup(sn.id_str);
1258 info->name = g_strdup(sn.name);
1259 info->date_nsec = sn.date_nsec;
1260 info->date_sec = sn.date_sec;
1261 info->vm_state_size = sn.vm_state_size;
1262 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1263 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1265 return info;
1267 out_aio_context:
1268 aio_context_release(aio_context);
1269 return NULL;
1273 * block_dirty_bitmap_lookup:
1274 * Return a dirty bitmap (if present), after validating
1275 * the node reference and bitmap names.
1277 * @node: The name of the BDS node to search for bitmaps
1278 * @name: The name of the bitmap to search for
1279 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1280 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1281 * @errp: Output pointer for error information. Can be NULL.
1283 * @return: A bitmap object on success, or NULL on failure.
1285 static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1286 const char *name,
1287 BlockDriverState **pbs,
1288 AioContext **paio,
1289 Error **errp)
1291 BlockDriverState *bs;
1292 BdrvDirtyBitmap *bitmap;
1293 AioContext *aio_context;
1295 if (!node) {
1296 error_setg(errp, "Node cannot be NULL");
1297 return NULL;
1299 if (!name) {
1300 error_setg(errp, "Bitmap name cannot be NULL");
1301 return NULL;
1303 bs = bdrv_lookup_bs(node, node, NULL);
1304 if (!bs) {
1305 error_setg(errp, "Node '%s' not found", node);
1306 return NULL;
1309 aio_context = bdrv_get_aio_context(bs);
1310 aio_context_acquire(aio_context);
1312 bitmap = bdrv_find_dirty_bitmap(bs, name);
1313 if (!bitmap) {
1314 error_setg(errp, "Dirty bitmap '%s' not found", name);
1315 goto fail;
1318 if (pbs) {
1319 *pbs = bs;
1321 if (paio) {
1322 *paio = aio_context;
1323 } else {
1324 aio_context_release(aio_context);
1327 return bitmap;
1329 fail:
1330 aio_context_release(aio_context);
1331 return NULL;
1334 /* New and old BlockDriverState structs for atomic group operations */
1336 typedef struct BlkActionState BlkActionState;
1339 * BlkActionOps:
1340 * Table of operations that define an Action.
1342 * @instance_size: Size of state struct, in bytes.
1343 * @prepare: Prepare the work, must NOT be NULL.
1344 * @commit: Commit the changes, can be NULL.
1345 * @abort: Abort the changes on fail, can be NULL.
1346 * @clean: Clean up resources after all transaction actions have called
1347 * commit() or abort(). Can be NULL.
1349 * Only prepare() may fail. In a single transaction, only one of commit() or
1350 * abort() will be called. clean() will always be called if it is present.
1352 typedef struct BlkActionOps {
1353 size_t instance_size;
1354 void (*prepare)(BlkActionState *common, Error **errp);
1355 void (*commit)(BlkActionState *common);
1356 void (*abort)(BlkActionState *common);
1357 void (*clean)(BlkActionState *common);
1358 } BlkActionOps;
1361 * BlkActionState:
1362 * Describes one Action's state within a Transaction.
1364 * @action: QAPI-defined enum identifying which Action to perform.
1365 * @ops: Table of ActionOps this Action can perform.
1366 * @block_job_txn: Transaction which this action belongs to.
1367 * @entry: List membership for all Actions in this Transaction.
1369 * This structure must be arranged as first member in a subclassed type,
1370 * assuming that the compiler will also arrange it to the same offsets as the
1371 * base class.
1373 struct BlkActionState {
1374 TransactionAction *action;
1375 const BlkActionOps *ops;
1376 BlockJobTxn *block_job_txn;
1377 TransactionProperties *txn_props;
1378 QSIMPLEQ_ENTRY(BlkActionState) entry;
1381 /* internal snapshot private data */
1382 typedef struct InternalSnapshotState {
1383 BlkActionState common;
1384 BlockDriverState *bs;
1385 AioContext *aio_context;
1386 QEMUSnapshotInfo sn;
1387 bool created;
1388 } InternalSnapshotState;
1391 static int action_check_completion_mode(BlkActionState *s, Error **errp)
1393 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1394 error_setg(errp,
1395 "Action '%s' does not support Transaction property "
1396 "completion-mode = %s",
1397 TransactionActionKind_lookup[s->action->type],
1398 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1399 return -1;
1401 return 0;
1404 static void internal_snapshot_prepare(BlkActionState *common,
1405 Error **errp)
1407 Error *local_err = NULL;
1408 const char *device;
1409 const char *name;
1410 BlockBackend *blk;
1411 BlockDriverState *bs;
1412 QEMUSnapshotInfo old_sn, *sn;
1413 bool ret;
1414 qemu_timeval tv;
1415 BlockdevSnapshotInternal *internal;
1416 InternalSnapshotState *state;
1417 int ret1;
1419 g_assert(common->action->type ==
1420 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1421 internal = common->action->u.blockdev_snapshot_internal_sync;
1422 state = DO_UPCAST(InternalSnapshotState, common, common);
1424 /* 1. parse input */
1425 device = internal->device;
1426 name = internal->name;
1428 /* 2. check for validation */
1429 if (action_check_completion_mode(common, errp) < 0) {
1430 return;
1433 blk = blk_by_name(device);
1434 if (!blk) {
1435 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1436 "Device '%s' not found", device);
1437 return;
1440 /* AioContext is released in .clean() */
1441 state->aio_context = blk_get_aio_context(blk);
1442 aio_context_acquire(state->aio_context);
1444 if (!blk_is_available(blk)) {
1445 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1446 return;
1448 bs = blk_bs(blk);
1450 state->bs = bs;
1451 bdrv_drained_begin(bs);
1453 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1454 return;
1457 if (bdrv_is_read_only(bs)) {
1458 error_setg(errp, "Device '%s' is read only", device);
1459 return;
1462 if (!bdrv_can_snapshot(bs)) {
1463 error_setg(errp, "Block format '%s' used by device '%s' "
1464 "does not support internal snapshots",
1465 bs->drv->format_name, device);
1466 return;
1469 if (!strlen(name)) {
1470 error_setg(errp, "Name is empty");
1471 return;
1474 /* check whether a snapshot with name exist */
1475 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1476 &local_err);
1477 if (local_err) {
1478 error_propagate(errp, local_err);
1479 return;
1480 } else if (ret) {
1481 error_setg(errp,
1482 "Snapshot with name '%s' already exists on device '%s'",
1483 name, device);
1484 return;
1487 /* 3. take the snapshot */
1488 sn = &state->sn;
1489 pstrcpy(sn->name, sizeof(sn->name), name);
1490 qemu_gettimeofday(&tv);
1491 sn->date_sec = tv.tv_sec;
1492 sn->date_nsec = tv.tv_usec * 1000;
1493 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1495 ret1 = bdrv_snapshot_create(bs, sn);
1496 if (ret1 < 0) {
1497 error_setg_errno(errp, -ret1,
1498 "Failed to create snapshot '%s' on device '%s'",
1499 name, device);
1500 return;
1503 /* 4. succeed, mark a snapshot is created */
1504 state->created = true;
1507 static void internal_snapshot_abort(BlkActionState *common)
1509 InternalSnapshotState *state =
1510 DO_UPCAST(InternalSnapshotState, common, common);
1511 BlockDriverState *bs = state->bs;
1512 QEMUSnapshotInfo *sn = &state->sn;
1513 Error *local_error = NULL;
1515 if (!state->created) {
1516 return;
1519 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1520 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1521 "device '%s' in abort: %s",
1522 sn->id_str,
1523 sn->name,
1524 bdrv_get_device_name(bs),
1525 error_get_pretty(local_error));
1526 error_free(local_error);
1530 static void internal_snapshot_clean(BlkActionState *common)
1532 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1533 common, common);
1535 if (state->aio_context) {
1536 if (state->bs) {
1537 bdrv_drained_end(state->bs);
1539 aio_context_release(state->aio_context);
1543 /* external snapshot private data */
1544 typedef struct ExternalSnapshotState {
1545 BlkActionState common;
1546 BlockDriverState *old_bs;
1547 BlockDriverState *new_bs;
1548 AioContext *aio_context;
1549 } ExternalSnapshotState;
1551 static void external_snapshot_prepare(BlkActionState *common,
1552 Error **errp)
1554 int flags = 0, ret;
1555 QDict *options = NULL;
1556 Error *local_err = NULL;
1557 /* Device and node name of the image to generate the snapshot from */
1558 const char *device;
1559 const char *node_name;
1560 /* Reference to the new image (for 'blockdev-snapshot') */
1561 const char *snapshot_ref;
1562 /* File name of the new image (for 'blockdev-snapshot-sync') */
1563 const char *new_image_file;
1564 ExternalSnapshotState *state =
1565 DO_UPCAST(ExternalSnapshotState, common, common);
1566 TransactionAction *action = common->action;
1568 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1569 * purpose but a different set of parameters */
1570 switch (action->type) {
1571 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1573 BlockdevSnapshot *s = action->u.blockdev_snapshot;
1574 device = s->node;
1575 node_name = s->node;
1576 new_image_file = NULL;
1577 snapshot_ref = s->overlay;
1579 break;
1580 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1582 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1583 device = s->has_device ? s->device : NULL;
1584 node_name = s->has_node_name ? s->node_name : NULL;
1585 new_image_file = s->snapshot_file;
1586 snapshot_ref = NULL;
1588 break;
1589 default:
1590 g_assert_not_reached();
1593 /* start processing */
1594 if (action_check_completion_mode(common, errp) < 0) {
1595 return;
1598 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1599 if (!state->old_bs) {
1600 return;
1603 /* Acquire AioContext now so any threads operating on old_bs stop */
1604 state->aio_context = bdrv_get_aio_context(state->old_bs);
1605 aio_context_acquire(state->aio_context);
1606 bdrv_drained_begin(state->old_bs);
1608 if (!bdrv_is_inserted(state->old_bs)) {
1609 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1610 return;
1613 if (bdrv_op_is_blocked(state->old_bs,
1614 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1615 return;
1618 if (!bdrv_is_read_only(state->old_bs)) {
1619 if (bdrv_flush(state->old_bs)) {
1620 error_setg(errp, QERR_IO_ERROR);
1621 return;
1625 if (!bdrv_is_first_non_filter(state->old_bs)) {
1626 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1627 return;
1630 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
1631 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1632 const char *format = s->has_format ? s->format : "qcow2";
1633 enum NewImageMode mode;
1634 const char *snapshot_node_name =
1635 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
1637 if (node_name && !snapshot_node_name) {
1638 error_setg(errp, "New snapshot node name missing");
1639 return;
1642 if (snapshot_node_name &&
1643 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1644 error_setg(errp, "New snapshot node name already in use");
1645 return;
1648 flags = state->old_bs->open_flags;
1650 /* create new image w/backing file */
1651 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1652 if (mode != NEW_IMAGE_MODE_EXISTING) {
1653 bdrv_img_create(new_image_file, format,
1654 state->old_bs->filename,
1655 state->old_bs->drv->format_name,
1656 NULL, -1, flags, &local_err, false);
1657 if (local_err) {
1658 error_propagate(errp, local_err);
1659 return;
1663 options = qdict_new();
1664 if (s->has_snapshot_node_name) {
1665 qdict_put(options, "node-name",
1666 qstring_from_str(snapshot_node_name));
1668 qdict_put(options, "driver", qstring_from_str(format));
1670 flags |= BDRV_O_NO_BACKING;
1673 assert(state->new_bs == NULL);
1674 ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options,
1675 flags, errp);
1676 /* We will manually add the backing_hd field to the bs later */
1677 if (ret != 0) {
1678 return;
1681 if (state->new_bs->blk != NULL) {
1682 error_setg(errp, "The snapshot is already in use by %s",
1683 blk_name(state->new_bs->blk));
1684 return;
1687 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1688 errp)) {
1689 return;
1692 if (state->new_bs->backing != NULL) {
1693 error_setg(errp, "The snapshot already has a backing image");
1694 return;
1697 if (!state->new_bs->drv->supports_backing) {
1698 error_setg(errp, "The snapshot does not support backing images");
1702 static void external_snapshot_commit(BlkActionState *common)
1704 ExternalSnapshotState *state =
1705 DO_UPCAST(ExternalSnapshotState, common, common);
1707 bdrv_set_aio_context(state->new_bs, state->aio_context);
1709 /* This removes our old bs and adds the new bs */
1710 bdrv_append(state->new_bs, state->old_bs);
1711 /* We don't need (or want) to use the transactional
1712 * bdrv_reopen_multiple() across all the entries at once, because we
1713 * don't want to abort all of them if one of them fails the reopen */
1714 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1715 NULL);
1718 static void external_snapshot_abort(BlkActionState *common)
1720 ExternalSnapshotState *state =
1721 DO_UPCAST(ExternalSnapshotState, common, common);
1722 if (state->new_bs) {
1723 bdrv_unref(state->new_bs);
1727 static void external_snapshot_clean(BlkActionState *common)
1729 ExternalSnapshotState *state =
1730 DO_UPCAST(ExternalSnapshotState, common, common);
1731 if (state->aio_context) {
1732 bdrv_drained_end(state->old_bs);
1733 aio_context_release(state->aio_context);
1737 typedef struct DriveBackupState {
1738 BlkActionState common;
1739 BlockDriverState *bs;
1740 AioContext *aio_context;
1741 BlockJob *job;
1742 } DriveBackupState;
1744 static void do_drive_backup(const char *device, const char *target,
1745 bool has_format, const char *format,
1746 enum MirrorSyncMode sync,
1747 bool has_mode, enum NewImageMode mode,
1748 bool has_speed, int64_t speed,
1749 bool has_bitmap, const char *bitmap,
1750 bool has_on_source_error,
1751 BlockdevOnError on_source_error,
1752 bool has_on_target_error,
1753 BlockdevOnError on_target_error,
1754 BlockJobTxn *txn, Error **errp);
1756 static void drive_backup_prepare(BlkActionState *common, Error **errp)
1758 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1759 BlockBackend *blk;
1760 DriveBackup *backup;
1761 Error *local_err = NULL;
1763 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1764 backup = common->action->u.drive_backup;
1766 blk = blk_by_name(backup->device);
1767 if (!blk) {
1768 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1769 "Device '%s' not found", backup->device);
1770 return;
1773 if (!blk_is_available(blk)) {
1774 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1775 return;
1778 /* AioContext is released in .clean() */
1779 state->aio_context = blk_get_aio_context(blk);
1780 aio_context_acquire(state->aio_context);
1781 bdrv_drained_begin(blk_bs(blk));
1782 state->bs = blk_bs(blk);
1784 do_drive_backup(backup->device, backup->target,
1785 backup->has_format, backup->format,
1786 backup->sync,
1787 backup->has_mode, backup->mode,
1788 backup->has_speed, backup->speed,
1789 backup->has_bitmap, backup->bitmap,
1790 backup->has_on_source_error, backup->on_source_error,
1791 backup->has_on_target_error, backup->on_target_error,
1792 common->block_job_txn, &local_err);
1793 if (local_err) {
1794 error_propagate(errp, local_err);
1795 return;
1798 state->job = state->bs->job;
1801 static void drive_backup_abort(BlkActionState *common)
1803 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1804 BlockDriverState *bs = state->bs;
1806 /* Only cancel if it's the job we started */
1807 if (bs && bs->job && bs->job == state->job) {
1808 block_job_cancel_sync(bs->job);
1812 static void drive_backup_clean(BlkActionState *common)
1814 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1816 if (state->aio_context) {
1817 bdrv_drained_end(state->bs);
1818 aio_context_release(state->aio_context);
1822 typedef struct BlockdevBackupState {
1823 BlkActionState common;
1824 BlockDriverState *bs;
1825 BlockJob *job;
1826 AioContext *aio_context;
1827 } BlockdevBackupState;
1829 static void do_blockdev_backup(const char *device, const char *target,
1830 enum MirrorSyncMode sync,
1831 bool has_speed, int64_t speed,
1832 bool has_on_source_error,
1833 BlockdevOnError on_source_error,
1834 bool has_on_target_error,
1835 BlockdevOnError on_target_error,
1836 BlockJobTxn *txn, Error **errp);
1838 static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1840 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1841 BlockdevBackup *backup;
1842 BlockBackend *blk, *target;
1843 Error *local_err = NULL;
1845 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1846 backup = common->action->u.blockdev_backup;
1848 blk = blk_by_name(backup->device);
1849 if (!blk) {
1850 error_setg(errp, "Device '%s' not found", backup->device);
1851 return;
1854 if (!blk_is_available(blk)) {
1855 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1856 return;
1859 target = blk_by_name(backup->target);
1860 if (!target) {
1861 error_setg(errp, "Device '%s' not found", backup->target);
1862 return;
1865 /* AioContext is released in .clean() */
1866 state->aio_context = blk_get_aio_context(blk);
1867 if (state->aio_context != blk_get_aio_context(target)) {
1868 state->aio_context = NULL;
1869 error_setg(errp, "Backup between two IO threads is not implemented");
1870 return;
1872 aio_context_acquire(state->aio_context);
1873 state->bs = blk_bs(blk);
1874 bdrv_drained_begin(state->bs);
1876 do_blockdev_backup(backup->device, backup->target,
1877 backup->sync,
1878 backup->has_speed, backup->speed,
1879 backup->has_on_source_error, backup->on_source_error,
1880 backup->has_on_target_error, backup->on_target_error,
1881 common->block_job_txn, &local_err);
1882 if (local_err) {
1883 error_propagate(errp, local_err);
1884 return;
1887 state->job = state->bs->job;
1890 static void blockdev_backup_abort(BlkActionState *common)
1892 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1893 BlockDriverState *bs = state->bs;
1895 /* Only cancel if it's the job we started */
1896 if (bs && bs->job && bs->job == state->job) {
1897 block_job_cancel_sync(bs->job);
1901 static void blockdev_backup_clean(BlkActionState *common)
1903 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1905 if (state->aio_context) {
1906 bdrv_drained_end(state->bs);
1907 aio_context_release(state->aio_context);
1911 typedef struct BlockDirtyBitmapState {
1912 BlkActionState common;
1913 BdrvDirtyBitmap *bitmap;
1914 BlockDriverState *bs;
1915 AioContext *aio_context;
1916 HBitmap *backup;
1917 bool prepared;
1918 } BlockDirtyBitmapState;
1920 static void block_dirty_bitmap_add_prepare(BlkActionState *common,
1921 Error **errp)
1923 Error *local_err = NULL;
1924 BlockDirtyBitmapAdd *action;
1925 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1926 common, common);
1928 if (action_check_completion_mode(common, errp) < 0) {
1929 return;
1932 action = common->action->u.block_dirty_bitmap_add;
1933 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1934 qmp_block_dirty_bitmap_add(action->node, action->name,
1935 action->has_granularity, action->granularity,
1936 &local_err);
1938 if (!local_err) {
1939 state->prepared = true;
1940 } else {
1941 error_propagate(errp, local_err);
1945 static void block_dirty_bitmap_add_abort(BlkActionState *common)
1947 BlockDirtyBitmapAdd *action;
1948 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1949 common, common);
1951 action = common->action->u.block_dirty_bitmap_add;
1952 /* Should not be able to fail: IF the bitmap was added via .prepare(),
1953 * then the node reference and bitmap name must have been valid.
1955 if (state->prepared) {
1956 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
1960 static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
1961 Error **errp)
1963 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1964 common, common);
1965 BlockDirtyBitmap *action;
1967 if (action_check_completion_mode(common, errp) < 0) {
1968 return;
1971 action = common->action->u.block_dirty_bitmap_clear;
1972 state->bitmap = block_dirty_bitmap_lookup(action->node,
1973 action->name,
1974 &state->bs,
1975 &state->aio_context,
1976 errp);
1977 if (!state->bitmap) {
1978 return;
1981 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
1982 error_setg(errp, "Cannot modify a frozen bitmap");
1983 return;
1984 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
1985 error_setg(errp, "Cannot clear a disabled bitmap");
1986 return;
1989 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
1990 /* AioContext is released in .clean() */
1993 static void block_dirty_bitmap_clear_abort(BlkActionState *common)
1995 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1996 common, common);
1998 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2001 static void block_dirty_bitmap_clear_commit(BlkActionState *common)
2003 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2004 common, common);
2006 hbitmap_free(state->backup);
2009 static void block_dirty_bitmap_clear_clean(BlkActionState *common)
2011 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2012 common, common);
2014 if (state->aio_context) {
2015 aio_context_release(state->aio_context);
2019 static void abort_prepare(BlkActionState *common, Error **errp)
2021 error_setg(errp, "Transaction aborted using Abort action");
2024 static void abort_commit(BlkActionState *common)
2026 g_assert_not_reached(); /* this action never succeeds */
2029 static const BlkActionOps actions[] = {
2030 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2031 .instance_size = sizeof(ExternalSnapshotState),
2032 .prepare = external_snapshot_prepare,
2033 .commit = external_snapshot_commit,
2034 .abort = external_snapshot_abort,
2036 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2037 .instance_size = sizeof(ExternalSnapshotState),
2038 .prepare = external_snapshot_prepare,
2039 .commit = external_snapshot_commit,
2040 .abort = external_snapshot_abort,
2041 .clean = external_snapshot_clean,
2043 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2044 .instance_size = sizeof(DriveBackupState),
2045 .prepare = drive_backup_prepare,
2046 .abort = drive_backup_abort,
2047 .clean = drive_backup_clean,
2049 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2050 .instance_size = sizeof(BlockdevBackupState),
2051 .prepare = blockdev_backup_prepare,
2052 .abort = blockdev_backup_abort,
2053 .clean = blockdev_backup_clean,
2055 [TRANSACTION_ACTION_KIND_ABORT] = {
2056 .instance_size = sizeof(BlkActionState),
2057 .prepare = abort_prepare,
2058 .commit = abort_commit,
2060 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2061 .instance_size = sizeof(InternalSnapshotState),
2062 .prepare = internal_snapshot_prepare,
2063 .abort = internal_snapshot_abort,
2064 .clean = internal_snapshot_clean,
2066 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2067 .instance_size = sizeof(BlockDirtyBitmapState),
2068 .prepare = block_dirty_bitmap_add_prepare,
2069 .abort = block_dirty_bitmap_add_abort,
2071 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2072 .instance_size = sizeof(BlockDirtyBitmapState),
2073 .prepare = block_dirty_bitmap_clear_prepare,
2074 .commit = block_dirty_bitmap_clear_commit,
2075 .abort = block_dirty_bitmap_clear_abort,
2076 .clean = block_dirty_bitmap_clear_clean,
2081 * Allocate a TransactionProperties structure if necessary, and fill
2082 * that structure with desired defaults if they are unset.
2084 static TransactionProperties *get_transaction_properties(
2085 TransactionProperties *props)
2087 if (!props) {
2088 props = g_new0(TransactionProperties, 1);
2091 if (!props->has_completion_mode) {
2092 props->has_completion_mode = true;
2093 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2096 return props;
2100 * 'Atomic' group operations. The operations are performed as a set, and if
2101 * any fail then we roll back all operations in the group.
2103 void qmp_transaction(TransactionActionList *dev_list,
2104 bool has_props,
2105 struct TransactionProperties *props,
2106 Error **errp)
2108 TransactionActionList *dev_entry = dev_list;
2109 BlockJobTxn *block_job_txn = NULL;
2110 BlkActionState *state, *next;
2111 Error *local_err = NULL;
2113 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
2114 QSIMPLEQ_INIT(&snap_bdrv_states);
2116 /* Does this transaction get canceled as a group on failure?
2117 * If not, we don't really need to make a BlockJobTxn.
2119 props = get_transaction_properties(props);
2120 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2121 block_job_txn = block_job_txn_new();
2124 /* drain all i/o before any operations */
2125 bdrv_drain_all();
2127 /* We don't do anything in this loop that commits us to the operations */
2128 while (NULL != dev_entry) {
2129 TransactionAction *dev_info = NULL;
2130 const BlkActionOps *ops;
2132 dev_info = dev_entry->value;
2133 dev_entry = dev_entry->next;
2135 assert(dev_info->type < ARRAY_SIZE(actions));
2137 ops = &actions[dev_info->type];
2138 assert(ops->instance_size > 0);
2140 state = g_malloc0(ops->instance_size);
2141 state->ops = ops;
2142 state->action = dev_info;
2143 state->block_job_txn = block_job_txn;
2144 state->txn_props = props;
2145 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
2147 state->ops->prepare(state, &local_err);
2148 if (local_err) {
2149 error_propagate(errp, local_err);
2150 goto delete_and_fail;
2154 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2155 if (state->ops->commit) {
2156 state->ops->commit(state);
2160 /* success */
2161 goto exit;
2163 delete_and_fail:
2164 /* failure, and it is all-or-none; roll back all operations */
2165 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2166 if (state->ops->abort) {
2167 state->ops->abort(state);
2170 exit:
2171 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2172 if (state->ops->clean) {
2173 state->ops->clean(state);
2175 g_free(state);
2177 if (!has_props) {
2178 qapi_free_TransactionProperties(props);
2180 block_job_txn_unref(block_job_txn);
2183 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
2185 Error *local_err = NULL;
2187 qmp_blockdev_open_tray(device, has_force, force, &local_err);
2188 if (local_err) {
2189 error_propagate(errp, local_err);
2190 return;
2193 qmp_blockdev_remove_medium(device, errp);
2196 void qmp_block_passwd(bool has_device, const char *device,
2197 bool has_node_name, const char *node_name,
2198 const char *password, Error **errp)
2200 Error *local_err = NULL;
2201 BlockDriverState *bs;
2202 AioContext *aio_context;
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 bdrv_add_key(bs, password, errp);
2217 aio_context_release(aio_context);
2220 void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
2221 Error **errp)
2223 BlockBackend *blk;
2224 bool locked;
2226 if (!has_force) {
2227 force = false;
2230 blk = blk_by_name(device);
2231 if (!blk) {
2232 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2233 "Device '%s' not found", device);
2234 return;
2237 if (!blk_dev_has_removable_media(blk)) {
2238 error_setg(errp, "Device '%s' is not removable", device);
2239 return;
2242 if (blk_dev_is_tray_open(blk)) {
2243 return;
2246 locked = blk_dev_is_medium_locked(blk);
2247 if (locked) {
2248 blk_dev_eject_request(blk, force);
2251 if (!locked || force) {
2252 blk_dev_change_media_cb(blk, false);
2256 void qmp_blockdev_close_tray(const char *device, Error **errp)
2258 BlockBackend *blk;
2260 blk = blk_by_name(device);
2261 if (!blk) {
2262 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2263 "Device '%s' not found", device);
2264 return;
2267 if (!blk_dev_has_removable_media(blk)) {
2268 error_setg(errp, "Device '%s' is not removable", device);
2269 return;
2272 if (!blk_dev_is_tray_open(blk)) {
2273 return;
2276 blk_dev_change_media_cb(blk, true);
2279 void qmp_blockdev_remove_medium(const char *device, Error **errp)
2281 BlockBackend *blk;
2282 BlockDriverState *bs;
2283 AioContext *aio_context;
2284 bool has_device;
2286 blk = blk_by_name(device);
2287 if (!blk) {
2288 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2289 "Device '%s' not found", device);
2290 return;
2293 /* For BBs without a device, we can exchange the BDS tree at will */
2294 has_device = blk_get_attached_dev(blk);
2296 if (has_device && !blk_dev_has_removable_media(blk)) {
2297 error_setg(errp, "Device '%s' is not removable", device);
2298 return;
2301 if (has_device && !blk_dev_is_tray_open(blk)) {
2302 error_setg(errp, "Tray of device '%s' is not open", device);
2303 return;
2306 bs = blk_bs(blk);
2307 if (!bs) {
2308 return;
2311 aio_context = bdrv_get_aio_context(bs);
2312 aio_context_acquire(aio_context);
2314 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2315 goto out;
2318 /* This follows the convention established by bdrv_make_anon() */
2319 if (bs->device_list.tqe_prev) {
2320 QTAILQ_REMOVE(&bdrv_states, bs, device_list);
2321 bs->device_list.tqe_prev = NULL;
2324 blk_remove_bs(blk);
2326 out:
2327 aio_context_release(aio_context);
2330 static void qmp_blockdev_insert_anon_medium(const char *device,
2331 BlockDriverState *bs, Error **errp)
2333 BlockBackend *blk;
2334 bool has_device;
2336 blk = blk_by_name(device);
2337 if (!blk) {
2338 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2339 "Device '%s' not found", device);
2340 return;
2343 /* For BBs without a device, we can exchange the BDS tree at will */
2344 has_device = blk_get_attached_dev(blk);
2346 if (has_device && !blk_dev_has_removable_media(blk)) {
2347 error_setg(errp, "Device '%s' is not removable", device);
2348 return;
2351 if (has_device && !blk_dev_is_tray_open(blk)) {
2352 error_setg(errp, "Tray of device '%s' is not open", device);
2353 return;
2356 if (blk_bs(blk)) {
2357 error_setg(errp, "There already is a medium in device '%s'", device);
2358 return;
2361 blk_insert_bs(blk, bs);
2363 QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
2366 void qmp_blockdev_insert_medium(const char *device, const char *node_name,
2367 Error **errp)
2369 BlockDriverState *bs;
2371 bs = bdrv_find_node(node_name);
2372 if (!bs) {
2373 error_setg(errp, "Node '%s' not found", node_name);
2374 return;
2377 if (bs->blk) {
2378 error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
2379 blk_name(bs->blk));
2380 return;
2383 qmp_blockdev_insert_anon_medium(device, bs, errp);
2386 void qmp_blockdev_change_medium(const char *device, const char *filename,
2387 bool has_format, const char *format,
2388 bool has_read_only,
2389 BlockdevChangeReadOnlyMode read_only,
2390 Error **errp)
2392 BlockBackend *blk;
2393 BlockDriverState *medium_bs = NULL;
2394 int bdrv_flags, ret;
2395 QDict *options = NULL;
2396 Error *err = NULL;
2398 blk = blk_by_name(device);
2399 if (!blk) {
2400 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2401 "Device '%s' not found", device);
2402 goto fail;
2405 if (blk_bs(blk)) {
2406 blk_update_root_state(blk);
2409 bdrv_flags = blk_get_open_flags_from_root_state(blk);
2411 if (!has_read_only) {
2412 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2415 switch (read_only) {
2416 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2417 break;
2419 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2420 bdrv_flags &= ~BDRV_O_RDWR;
2421 break;
2423 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2424 bdrv_flags |= BDRV_O_RDWR;
2425 break;
2427 default:
2428 abort();
2431 if (has_format) {
2432 options = qdict_new();
2433 qdict_put(options, "driver", qstring_from_str(format));
2436 assert(!medium_bs);
2437 ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2438 if (ret < 0) {
2439 goto fail;
2442 blk_apply_root_state(blk, medium_bs);
2444 bdrv_add_key(medium_bs, NULL, &err);
2445 if (err) {
2446 error_propagate(errp, err);
2447 goto fail;
2450 qmp_blockdev_open_tray(device, false, false, &err);
2451 if (err) {
2452 error_propagate(errp, err);
2453 goto fail;
2456 qmp_blockdev_remove_medium(device, &err);
2457 if (err) {
2458 error_propagate(errp, err);
2459 goto fail;
2462 qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2463 if (err) {
2464 error_propagate(errp, err);
2465 goto fail;
2468 qmp_blockdev_close_tray(device, errp);
2470 fail:
2471 /* If the medium has been inserted, the device has its own reference, so
2472 * ours must be relinquished; and if it has not been inserted successfully,
2473 * the reference must be relinquished anyway */
2474 bdrv_unref(medium_bs);
2477 /* throttling disk I/O limits */
2478 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
2479 int64_t bps_wr,
2480 int64_t iops,
2481 int64_t iops_rd,
2482 int64_t iops_wr,
2483 bool has_bps_max,
2484 int64_t bps_max,
2485 bool has_bps_rd_max,
2486 int64_t bps_rd_max,
2487 bool has_bps_wr_max,
2488 int64_t bps_wr_max,
2489 bool has_iops_max,
2490 int64_t iops_max,
2491 bool has_iops_rd_max,
2492 int64_t iops_rd_max,
2493 bool has_iops_wr_max,
2494 int64_t iops_wr_max,
2495 bool has_iops_size,
2496 int64_t iops_size,
2497 bool has_group,
2498 const char *group, Error **errp)
2500 ThrottleConfig cfg;
2501 BlockDriverState *bs;
2502 BlockBackend *blk;
2503 AioContext *aio_context;
2505 blk = blk_by_name(device);
2506 if (!blk) {
2507 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2508 "Device '%s' not found", device);
2509 return;
2512 aio_context = blk_get_aio_context(blk);
2513 aio_context_acquire(aio_context);
2515 bs = blk_bs(blk);
2516 if (!bs) {
2517 error_setg(errp, "Device '%s' has no medium", device);
2518 goto out;
2521 memset(&cfg, 0, sizeof(cfg));
2522 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2523 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
2524 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
2526 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2527 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
2528 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2530 if (has_bps_max) {
2531 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
2533 if (has_bps_rd_max) {
2534 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
2536 if (has_bps_wr_max) {
2537 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2539 if (has_iops_max) {
2540 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2542 if (has_iops_rd_max) {
2543 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2545 if (has_iops_wr_max) {
2546 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2549 if (has_iops_size) {
2550 cfg.op_size = iops_size;
2553 if (!check_throttle_config(&cfg, errp)) {
2554 goto out;
2557 if (throttle_enabled(&cfg)) {
2558 /* Enable I/O limits if they're not enabled yet, otherwise
2559 * just update the throttling group. */
2560 if (!bs->throttle_state) {
2561 bdrv_io_limits_enable(bs, has_group ? group : device);
2562 } else if (has_group) {
2563 bdrv_io_limits_update_group(bs, group);
2565 /* Set the new throttling configuration */
2566 bdrv_set_io_limits(bs, &cfg);
2567 } else if (bs->throttle_state) {
2568 /* If all throttling settings are set to 0, disable I/O limits */
2569 bdrv_io_limits_disable(bs);
2572 out:
2573 aio_context_release(aio_context);
2576 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2577 bool has_granularity, uint32_t granularity,
2578 Error **errp)
2580 AioContext *aio_context;
2581 BlockDriverState *bs;
2583 if (!name || name[0] == '\0') {
2584 error_setg(errp, "Bitmap name cannot be empty");
2585 return;
2588 bs = bdrv_lookup_bs(node, node, errp);
2589 if (!bs) {
2590 return;
2593 aio_context = bdrv_get_aio_context(bs);
2594 aio_context_acquire(aio_context);
2596 if (has_granularity) {
2597 if (granularity < 512 || !is_power_of_2(granularity)) {
2598 error_setg(errp, "Granularity must be power of 2 "
2599 "and at least 512");
2600 goto out;
2602 } else {
2603 /* Default to cluster size, if available: */
2604 granularity = bdrv_get_default_bitmap_granularity(bs);
2607 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2609 out:
2610 aio_context_release(aio_context);
2613 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2614 Error **errp)
2616 AioContext *aio_context;
2617 BlockDriverState *bs;
2618 BdrvDirtyBitmap *bitmap;
2620 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2621 if (!bitmap || !bs) {
2622 return;
2625 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2626 error_setg(errp,
2627 "Bitmap '%s' is currently frozen and cannot be removed",
2628 name);
2629 goto out;
2631 bdrv_dirty_bitmap_make_anon(bitmap);
2632 bdrv_release_dirty_bitmap(bs, bitmap);
2634 out:
2635 aio_context_release(aio_context);
2639 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2640 * immediately after a full backup operation.
2642 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2643 Error **errp)
2645 AioContext *aio_context;
2646 BdrvDirtyBitmap *bitmap;
2647 BlockDriverState *bs;
2649 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2650 if (!bitmap || !bs) {
2651 return;
2654 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2655 error_setg(errp,
2656 "Bitmap '%s' is currently frozen and cannot be modified",
2657 name);
2658 goto out;
2659 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2660 error_setg(errp,
2661 "Bitmap '%s' is currently disabled and cannot be cleared",
2662 name);
2663 goto out;
2666 bdrv_clear_dirty_bitmap(bitmap, NULL);
2668 out:
2669 aio_context_release(aio_context);
2672 void hmp_drive_del(Monitor *mon, const QDict *qdict)
2674 const char *id = qdict_get_str(qdict, "id");
2675 BlockBackend *blk;
2676 BlockDriverState *bs;
2677 AioContext *aio_context;
2678 Error *local_err = NULL;
2680 blk = blk_by_name(id);
2681 if (!blk) {
2682 error_report("Device '%s' not found", id);
2683 return;
2686 if (!blk_legacy_dinfo(blk)) {
2687 error_report("Deleting device added with blockdev-add"
2688 " is not supported");
2689 return;
2692 aio_context = blk_get_aio_context(blk);
2693 aio_context_acquire(aio_context);
2695 bs = blk_bs(blk);
2696 if (bs) {
2697 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2698 error_report_err(local_err);
2699 aio_context_release(aio_context);
2700 return;
2703 bdrv_close(bs);
2706 /* if we have a device attached to this BlockDriverState
2707 * then we need to make the drive anonymous until the device
2708 * can be removed. If this is a drive with no device backing
2709 * then we can just get rid of the block driver state right here.
2711 if (blk_get_attached_dev(blk)) {
2712 blk_hide_on_behalf_of_hmp_drive_del(blk);
2713 /* Further I/O must not pause the guest */
2714 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2715 BLOCKDEV_ON_ERROR_REPORT);
2716 } else {
2717 blk_unref(blk);
2720 aio_context_release(aio_context);
2723 void qmp_block_resize(bool has_device, const char *device,
2724 bool has_node_name, const char *node_name,
2725 int64_t size, Error **errp)
2727 Error *local_err = NULL;
2728 BlockDriverState *bs;
2729 AioContext *aio_context;
2730 int ret;
2732 bs = bdrv_lookup_bs(has_device ? device : NULL,
2733 has_node_name ? node_name : NULL,
2734 &local_err);
2735 if (local_err) {
2736 error_propagate(errp, local_err);
2737 return;
2740 aio_context = bdrv_get_aio_context(bs);
2741 aio_context_acquire(aio_context);
2743 if (!bdrv_is_first_non_filter(bs)) {
2744 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2745 goto out;
2748 if (size < 0) {
2749 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2750 goto out;
2753 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2754 error_setg(errp, QERR_DEVICE_IN_USE, device);
2755 goto out;
2758 /* complete all in-flight operations before resizing the device */
2759 bdrv_drain_all();
2761 ret = bdrv_truncate(bs, size);
2762 switch (ret) {
2763 case 0:
2764 break;
2765 case -ENOMEDIUM:
2766 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2767 break;
2768 case -ENOTSUP:
2769 error_setg(errp, QERR_UNSUPPORTED);
2770 break;
2771 case -EACCES:
2772 error_setg(errp, "Device '%s' is read only", device);
2773 break;
2774 case -EBUSY:
2775 error_setg(errp, QERR_DEVICE_IN_USE, device);
2776 break;
2777 default:
2778 error_setg_errno(errp, -ret, "Could not resize");
2779 break;
2782 out:
2783 aio_context_release(aio_context);
2786 static void block_job_cb(void *opaque, int ret)
2788 /* Note that this function may be executed from another AioContext besides
2789 * the QEMU main loop. If you need to access anything that assumes the
2790 * QEMU global mutex, use a BH or introduce a mutex.
2793 BlockDriverState *bs = opaque;
2794 const char *msg = NULL;
2796 trace_block_job_cb(bs, bs->job, ret);
2798 assert(bs->job);
2800 if (ret < 0) {
2801 msg = strerror(-ret);
2804 if (block_job_is_cancelled(bs->job)) {
2805 block_job_event_cancelled(bs->job);
2806 } else {
2807 block_job_event_completed(bs->job, msg);
2811 void qmp_block_stream(const char *device,
2812 bool has_base, const char *base,
2813 bool has_backing_file, const char *backing_file,
2814 bool has_speed, int64_t speed,
2815 bool has_on_error, BlockdevOnError on_error,
2816 Error **errp)
2818 BlockBackend *blk;
2819 BlockDriverState *bs;
2820 BlockDriverState *base_bs = NULL;
2821 AioContext *aio_context;
2822 Error *local_err = NULL;
2823 const char *base_name = NULL;
2825 if (!has_on_error) {
2826 on_error = BLOCKDEV_ON_ERROR_REPORT;
2829 blk = blk_by_name(device);
2830 if (!blk) {
2831 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2832 "Device '%s' not found", device);
2833 return;
2836 aio_context = blk_get_aio_context(blk);
2837 aio_context_acquire(aio_context);
2839 if (!blk_is_available(blk)) {
2840 error_setg(errp, "Device '%s' has no medium", device);
2841 goto out;
2843 bs = blk_bs(blk);
2845 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2846 goto out;
2849 if (has_base) {
2850 base_bs = bdrv_find_backing_image(bs, base);
2851 if (base_bs == NULL) {
2852 error_setg(errp, QERR_BASE_NOT_FOUND, base);
2853 goto out;
2855 assert(bdrv_get_aio_context(base_bs) == aio_context);
2856 base_name = base;
2859 /* if we are streaming the entire chain, the result will have no backing
2860 * file, and specifying one is therefore an error */
2861 if (base_bs == NULL && has_backing_file) {
2862 error_setg(errp, "backing file specified, but streaming the "
2863 "entire chain");
2864 goto out;
2867 /* backing_file string overrides base bs filename */
2868 base_name = has_backing_file ? backing_file : base_name;
2870 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
2871 on_error, block_job_cb, bs, &local_err);
2872 if (local_err) {
2873 error_propagate(errp, local_err);
2874 goto out;
2877 trace_qmp_block_stream(bs, bs->job);
2879 out:
2880 aio_context_release(aio_context);
2883 void qmp_block_commit(const char *device,
2884 bool has_base, const char *base,
2885 bool has_top, const char *top,
2886 bool has_backing_file, const char *backing_file,
2887 bool has_speed, int64_t speed,
2888 Error **errp)
2890 BlockBackend *blk;
2891 BlockDriverState *bs;
2892 BlockDriverState *base_bs, *top_bs;
2893 AioContext *aio_context;
2894 Error *local_err = NULL;
2895 /* This will be part of the QMP command, if/when the
2896 * BlockdevOnError change for blkmirror makes it in
2898 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
2900 if (!has_speed) {
2901 speed = 0;
2904 /* Important Note:
2905 * libvirt relies on the DeviceNotFound error class in order to probe for
2906 * live commit feature versions; for this to work, we must make sure to
2907 * perform the device lookup before any generic errors that may occur in a
2908 * scenario in which all optional arguments are omitted. */
2909 blk = blk_by_name(device);
2910 if (!blk) {
2911 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2912 "Device '%s' not found", device);
2913 return;
2916 aio_context = blk_get_aio_context(blk);
2917 aio_context_acquire(aio_context);
2919 if (!blk_is_available(blk)) {
2920 error_setg(errp, "Device '%s' has no medium", device);
2921 goto out;
2923 bs = blk_bs(blk);
2925 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
2926 goto out;
2929 /* default top_bs is the active layer */
2930 top_bs = bs;
2932 if (has_top && top) {
2933 if (strcmp(bs->filename, top) != 0) {
2934 top_bs = bdrv_find_backing_image(bs, top);
2938 if (top_bs == NULL) {
2939 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
2940 goto out;
2943 assert(bdrv_get_aio_context(top_bs) == aio_context);
2945 if (has_base && base) {
2946 base_bs = bdrv_find_backing_image(top_bs, base);
2947 } else {
2948 base_bs = bdrv_find_base(top_bs);
2951 if (base_bs == NULL) {
2952 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
2953 goto out;
2956 assert(bdrv_get_aio_context(base_bs) == aio_context);
2958 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2959 goto out;
2962 /* Do not allow attempts to commit an image into itself */
2963 if (top_bs == base_bs) {
2964 error_setg(errp, "cannot commit an image into itself");
2965 goto out;
2968 if (top_bs == bs) {
2969 if (has_backing_file) {
2970 error_setg(errp, "'backing-file' specified,"
2971 " but 'top' is the active layer");
2972 goto out;
2974 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
2975 bs, &local_err);
2976 } else {
2977 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
2978 has_backing_file ? backing_file : NULL, &local_err);
2980 if (local_err != NULL) {
2981 error_propagate(errp, local_err);
2982 goto out;
2985 out:
2986 aio_context_release(aio_context);
2989 static void do_drive_backup(const char *device, const char *target,
2990 bool has_format, const char *format,
2991 enum MirrorSyncMode sync,
2992 bool has_mode, enum NewImageMode mode,
2993 bool has_speed, int64_t speed,
2994 bool has_bitmap, const char *bitmap,
2995 bool has_on_source_error,
2996 BlockdevOnError on_source_error,
2997 bool has_on_target_error,
2998 BlockdevOnError on_target_error,
2999 BlockJobTxn *txn, Error **errp)
3001 BlockBackend *blk;
3002 BlockDriverState *bs;
3003 BlockDriverState *target_bs;
3004 BlockDriverState *source = NULL;
3005 BdrvDirtyBitmap *bmap = NULL;
3006 AioContext *aio_context;
3007 QDict *options = NULL;
3008 Error *local_err = NULL;
3009 int flags;
3010 int64_t size;
3011 int ret;
3013 if (!has_speed) {
3014 speed = 0;
3016 if (!has_on_source_error) {
3017 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3019 if (!has_on_target_error) {
3020 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3022 if (!has_mode) {
3023 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3026 blk = blk_by_name(device);
3027 if (!blk) {
3028 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3029 "Device '%s' not found", device);
3030 return;
3033 aio_context = blk_get_aio_context(blk);
3034 aio_context_acquire(aio_context);
3036 /* Although backup_run has this check too, we need to use bs->drv below, so
3037 * do an early check redundantly. */
3038 if (!blk_is_available(blk)) {
3039 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3040 goto out;
3042 bs = blk_bs(blk);
3044 if (!has_format) {
3045 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3048 /* Early check to avoid creating target */
3049 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
3050 goto out;
3053 flags = bs->open_flags | BDRV_O_RDWR;
3055 /* See if we have a backing HD we can use to create our new image
3056 * on top of. */
3057 if (sync == MIRROR_SYNC_MODE_TOP) {
3058 source = backing_bs(bs);
3059 if (!source) {
3060 sync = MIRROR_SYNC_MODE_FULL;
3063 if (sync == MIRROR_SYNC_MODE_NONE) {
3064 source = bs;
3067 size = bdrv_getlength(bs);
3068 if (size < 0) {
3069 error_setg_errno(errp, -size, "bdrv_getlength failed");
3070 goto out;
3073 if (mode != NEW_IMAGE_MODE_EXISTING) {
3074 assert(format);
3075 if (source) {
3076 bdrv_img_create(target, format, source->filename,
3077 source->drv->format_name, NULL,
3078 size, flags, &local_err, false);
3079 } else {
3080 bdrv_img_create(target, format, NULL, NULL, NULL,
3081 size, flags, &local_err, false);
3085 if (local_err) {
3086 error_propagate(errp, local_err);
3087 goto out;
3090 if (format) {
3091 options = qdict_new();
3092 qdict_put(options, "driver", qstring_from_str(format));
3095 target_bs = NULL;
3096 ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
3097 if (ret < 0) {
3098 error_propagate(errp, local_err);
3099 goto out;
3102 bdrv_set_aio_context(target_bs, aio_context);
3104 if (has_bitmap) {
3105 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3106 if (!bmap) {
3107 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
3108 goto out;
3112 backup_start(bs, target_bs, speed, sync, bmap,
3113 on_source_error, on_target_error,
3114 block_job_cb, bs, txn, &local_err);
3115 if (local_err != NULL) {
3116 bdrv_unref(target_bs);
3117 error_propagate(errp, local_err);
3118 goto out;
3121 out:
3122 aio_context_release(aio_context);
3125 void qmp_drive_backup(const char *device, const char *target,
3126 bool has_format, const char *format,
3127 enum MirrorSyncMode sync,
3128 bool has_mode, enum NewImageMode mode,
3129 bool has_speed, int64_t speed,
3130 bool has_bitmap, const char *bitmap,
3131 bool has_on_source_error, BlockdevOnError on_source_error,
3132 bool has_on_target_error, BlockdevOnError on_target_error,
3133 Error **errp)
3135 return do_drive_backup(device, target, has_format, format, sync,
3136 has_mode, mode, has_speed, speed,
3137 has_bitmap, bitmap,
3138 has_on_source_error, on_source_error,
3139 has_on_target_error, on_target_error,
3140 NULL, errp);
3143 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3145 return bdrv_named_nodes_list(errp);
3148 void do_blockdev_backup(const char *device, const char *target,
3149 enum MirrorSyncMode sync,
3150 bool has_speed, int64_t speed,
3151 bool has_on_source_error,
3152 BlockdevOnError on_source_error,
3153 bool has_on_target_error,
3154 BlockdevOnError on_target_error,
3155 BlockJobTxn *txn, Error **errp)
3157 BlockBackend *blk, *target_blk;
3158 BlockDriverState *bs;
3159 BlockDriverState *target_bs;
3160 Error *local_err = NULL;
3161 AioContext *aio_context;
3163 if (!has_speed) {
3164 speed = 0;
3166 if (!has_on_source_error) {
3167 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3169 if (!has_on_target_error) {
3170 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3173 blk = blk_by_name(device);
3174 if (!blk) {
3175 error_setg(errp, "Device '%s' not found", device);
3176 return;
3179 aio_context = blk_get_aio_context(blk);
3180 aio_context_acquire(aio_context);
3182 if (!blk_is_available(blk)) {
3183 error_setg(errp, "Device '%s' has no medium", device);
3184 goto out;
3186 bs = blk_bs(blk);
3188 target_blk = blk_by_name(target);
3189 if (!target_blk) {
3190 error_setg(errp, "Device '%s' not found", target);
3191 goto out;
3194 if (!blk_is_available(target_blk)) {
3195 error_setg(errp, "Device '%s' has no medium", target);
3196 goto out;
3198 target_bs = blk_bs(target_blk);
3200 bdrv_ref(target_bs);
3201 bdrv_set_aio_context(target_bs, aio_context);
3202 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
3203 on_target_error, block_job_cb, bs, txn, &local_err);
3204 if (local_err != NULL) {
3205 bdrv_unref(target_bs);
3206 error_propagate(errp, local_err);
3208 out:
3209 aio_context_release(aio_context);
3212 void qmp_blockdev_backup(const char *device, const char *target,
3213 enum MirrorSyncMode sync,
3214 bool has_speed, int64_t speed,
3215 bool has_on_source_error,
3216 BlockdevOnError on_source_error,
3217 bool has_on_target_error,
3218 BlockdevOnError on_target_error,
3219 Error **errp)
3221 do_blockdev_backup(device, target, sync, has_speed, speed,
3222 has_on_source_error, on_source_error,
3223 has_on_target_error, on_target_error,
3224 NULL, errp);
3227 void qmp_drive_mirror(const char *device, const char *target,
3228 bool has_format, const char *format,
3229 bool has_node_name, const char *node_name,
3230 bool has_replaces, const char *replaces,
3231 enum MirrorSyncMode sync,
3232 bool has_mode, enum NewImageMode mode,
3233 bool has_speed, int64_t speed,
3234 bool has_granularity, uint32_t granularity,
3235 bool has_buf_size, int64_t buf_size,
3236 bool has_on_source_error, BlockdevOnError on_source_error,
3237 bool has_on_target_error, BlockdevOnError on_target_error,
3238 bool has_unmap, bool unmap,
3239 Error **errp)
3241 BlockBackend *blk;
3242 BlockDriverState *bs;
3243 BlockDriverState *source, *target_bs;
3244 AioContext *aio_context;
3245 Error *local_err = NULL;
3246 QDict *options;
3247 int flags;
3248 int64_t size;
3249 int ret;
3251 if (!has_speed) {
3252 speed = 0;
3254 if (!has_on_source_error) {
3255 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3257 if (!has_on_target_error) {
3258 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3260 if (!has_mode) {
3261 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3263 if (!has_granularity) {
3264 granularity = 0;
3266 if (!has_buf_size) {
3267 buf_size = 0;
3269 if (!has_unmap) {
3270 unmap = true;
3273 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3274 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3275 "a value in range [512B, 64MB]");
3276 return;
3278 if (granularity & (granularity - 1)) {
3279 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3280 "power of 2");
3281 return;
3284 blk = blk_by_name(device);
3285 if (!blk) {
3286 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3287 "Device '%s' not found", device);
3288 return;
3291 aio_context = blk_get_aio_context(blk);
3292 aio_context_acquire(aio_context);
3294 if (!blk_is_available(blk)) {
3295 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3296 goto out;
3298 bs = blk_bs(blk);
3300 if (!has_format) {
3301 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3304 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
3305 goto out;
3308 flags = bs->open_flags | BDRV_O_RDWR;
3309 source = backing_bs(bs);
3310 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
3311 sync = MIRROR_SYNC_MODE_FULL;
3313 if (sync == MIRROR_SYNC_MODE_NONE) {
3314 source = bs;
3317 size = bdrv_getlength(bs);
3318 if (size < 0) {
3319 error_setg_errno(errp, -size, "bdrv_getlength failed");
3320 goto out;
3323 if (has_replaces) {
3324 BlockDriverState *to_replace_bs;
3325 AioContext *replace_aio_context;
3326 int64_t replace_size;
3328 if (!has_node_name) {
3329 error_setg(errp, "a node-name must be provided when replacing a"
3330 " named node of the graph");
3331 goto out;
3334 to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
3336 if (!to_replace_bs) {
3337 error_propagate(errp, local_err);
3338 goto out;
3341 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3342 aio_context_acquire(replace_aio_context);
3343 replace_size = bdrv_getlength(to_replace_bs);
3344 aio_context_release(replace_aio_context);
3346 if (size != replace_size) {
3347 error_setg(errp, "cannot replace image with a mirror image of "
3348 "different size");
3349 goto out;
3353 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
3354 && mode != NEW_IMAGE_MODE_EXISTING)
3356 /* create new image w/o backing file */
3357 assert(format);
3358 bdrv_img_create(target, format,
3359 NULL, NULL, NULL, size, flags, &local_err, false);
3360 } else {
3361 switch (mode) {
3362 case NEW_IMAGE_MODE_EXISTING:
3363 break;
3364 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3365 /* create new image with backing file */
3366 bdrv_img_create(target, format,
3367 source->filename,
3368 source->drv->format_name,
3369 NULL, size, flags, &local_err, false);
3370 break;
3371 default:
3372 abort();
3376 if (local_err) {
3377 error_propagate(errp, local_err);
3378 goto out;
3381 options = qdict_new();
3382 if (has_node_name) {
3383 qdict_put(options, "node-name", qstring_from_str(node_name));
3385 if (format) {
3386 qdict_put(options, "driver", qstring_from_str(format));
3389 /* Mirroring takes care of copy-on-write using the source's backing
3390 * file.
3392 target_bs = NULL;
3393 ret = bdrv_open(&target_bs, target, NULL, options,
3394 flags | BDRV_O_NO_BACKING, &local_err);
3395 if (ret < 0) {
3396 error_propagate(errp, local_err);
3397 goto out;
3400 bdrv_set_aio_context(target_bs, aio_context);
3402 /* pass the node name to replace to mirror start since it's loose coupling
3403 * and will allow to check whether the node still exist at mirror completion
3405 mirror_start(bs, target_bs,
3406 has_replaces ? replaces : NULL,
3407 speed, granularity, buf_size, sync,
3408 on_source_error, on_target_error,
3409 unmap,
3410 block_job_cb, bs, &local_err);
3411 if (local_err != NULL) {
3412 bdrv_unref(target_bs);
3413 error_propagate(errp, local_err);
3414 goto out;
3417 out:
3418 aio_context_release(aio_context);
3421 /* Get the block job for a given device name and acquire its AioContext */
3422 static BlockJob *find_block_job(const char *device, AioContext **aio_context,
3423 Error **errp)
3425 BlockBackend *blk;
3426 BlockDriverState *bs;
3428 *aio_context = NULL;
3430 blk = blk_by_name(device);
3431 if (!blk) {
3432 goto notfound;
3435 *aio_context = blk_get_aio_context(blk);
3436 aio_context_acquire(*aio_context);
3438 if (!blk_is_available(blk)) {
3439 goto notfound;
3441 bs = blk_bs(blk);
3443 if (!bs->job) {
3444 goto notfound;
3447 return bs->job;
3449 notfound:
3450 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3451 "No active block job on device '%s'", device);
3452 if (*aio_context) {
3453 aio_context_release(*aio_context);
3454 *aio_context = NULL;
3456 return NULL;
3459 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
3461 AioContext *aio_context;
3462 BlockJob *job = find_block_job(device, &aio_context, errp);
3464 if (!job) {
3465 return;
3468 block_job_set_speed(job, speed, errp);
3469 aio_context_release(aio_context);
3472 void qmp_block_job_cancel(const char *device,
3473 bool has_force, bool force, Error **errp)
3475 AioContext *aio_context;
3476 BlockJob *job = find_block_job(device, &aio_context, errp);
3478 if (!job) {
3479 return;
3482 if (!has_force) {
3483 force = false;
3486 if (job->user_paused && !force) {
3487 error_setg(errp, "The block job for device '%s' is currently paused",
3488 device);
3489 goto out;
3492 trace_qmp_block_job_cancel(job);
3493 block_job_cancel(job);
3494 out:
3495 aio_context_release(aio_context);
3498 void qmp_block_job_pause(const char *device, Error **errp)
3500 AioContext *aio_context;
3501 BlockJob *job = find_block_job(device, &aio_context, errp);
3503 if (!job || job->user_paused) {
3504 return;
3507 job->user_paused = true;
3508 trace_qmp_block_job_pause(job);
3509 block_job_pause(job);
3510 aio_context_release(aio_context);
3513 void qmp_block_job_resume(const char *device, Error **errp)
3515 AioContext *aio_context;
3516 BlockJob *job = find_block_job(device, &aio_context, errp);
3518 if (!job || !job->user_paused) {
3519 return;
3522 job->user_paused = false;
3523 trace_qmp_block_job_resume(job);
3524 block_job_resume(job);
3525 aio_context_release(aio_context);
3528 void qmp_block_job_complete(const char *device, Error **errp)
3530 AioContext *aio_context;
3531 BlockJob *job = find_block_job(device, &aio_context, errp);
3533 if (!job) {
3534 return;
3537 trace_qmp_block_job_complete(job);
3538 block_job_complete(job, errp);
3539 aio_context_release(aio_context);
3542 void qmp_change_backing_file(const char *device,
3543 const char *image_node_name,
3544 const char *backing_file,
3545 Error **errp)
3547 BlockBackend *blk;
3548 BlockDriverState *bs = NULL;
3549 AioContext *aio_context;
3550 BlockDriverState *image_bs = NULL;
3551 Error *local_err = NULL;
3552 bool ro;
3553 int open_flags;
3554 int ret;
3556 blk = blk_by_name(device);
3557 if (!blk) {
3558 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3559 "Device '%s' not found", device);
3560 return;
3563 aio_context = blk_get_aio_context(blk);
3564 aio_context_acquire(aio_context);
3566 if (!blk_is_available(blk)) {
3567 error_setg(errp, "Device '%s' has no medium", device);
3568 goto out;
3570 bs = blk_bs(blk);
3572 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3573 if (local_err) {
3574 error_propagate(errp, local_err);
3575 goto out;
3578 if (!image_bs) {
3579 error_setg(errp, "image file not found");
3580 goto out;
3583 if (bdrv_find_base(image_bs) == image_bs) {
3584 error_setg(errp, "not allowing backing file change on an image "
3585 "without a backing file");
3586 goto out;
3589 /* even though we are not necessarily operating on bs, we need it to
3590 * determine if block ops are currently prohibited on the chain */
3591 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3592 goto out;
3595 /* final sanity check */
3596 if (!bdrv_chain_contains(bs, image_bs)) {
3597 error_setg(errp, "'%s' and image file are not in the same chain",
3598 device);
3599 goto out;
3602 /* if not r/w, reopen to make r/w */
3603 open_flags = image_bs->open_flags;
3604 ro = bdrv_is_read_only(image_bs);
3606 if (ro) {
3607 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3608 if (local_err) {
3609 error_propagate(errp, local_err);
3610 goto out;
3614 ret = bdrv_change_backing_file(image_bs, backing_file,
3615 image_bs->drv ? image_bs->drv->format_name : "");
3617 if (ret < 0) {
3618 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3619 backing_file);
3620 /* don't exit here, so we can try to restore open flags if
3621 * appropriate */
3624 if (ro) {
3625 bdrv_reopen(image_bs, open_flags, &local_err);
3626 if (local_err) {
3627 error_propagate(errp, local_err); /* will preserve prior errp */
3631 out:
3632 aio_context_release(aio_context);
3635 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3637 QmpOutputVisitor *ov = qmp_output_visitor_new();
3638 BlockDriverState *bs;
3639 BlockBackend *blk = NULL;
3640 QObject *obj;
3641 QDict *qdict;
3642 Error *local_err = NULL;
3644 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3645 * cache.direct=false instead of silently switching to aio=threads, except
3646 * when called from drive_new().
3648 * For now, simply forbidding the combination for all drivers will do. */
3649 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3650 bool direct = options->has_cache &&
3651 options->cache->has_direct &&
3652 options->cache->direct;
3653 if (!direct) {
3654 error_setg(errp, "aio=native requires cache.direct=true");
3655 goto fail;
3659 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
3660 &options, NULL, &local_err);
3661 if (local_err) {
3662 error_propagate(errp, local_err);
3663 goto fail;
3666 obj = qmp_output_get_qobject(ov);
3667 qdict = qobject_to_qdict(obj);
3669 qdict_flatten(qdict);
3671 if (options->has_id) {
3672 blk = blockdev_init(NULL, qdict, &local_err);
3673 if (local_err) {
3674 error_propagate(errp, local_err);
3675 goto fail;
3678 bs = blk_bs(blk);
3679 } else {
3680 if (!qdict_get_try_str(qdict, "node-name")) {
3681 error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3682 "the root node");
3683 goto fail;
3686 bs = bds_tree_init(qdict, errp);
3687 if (!bs) {
3688 goto fail;
3692 if (bs && bdrv_key_required(bs)) {
3693 if (blk) {
3694 blk_unref(blk);
3695 } else {
3696 bdrv_unref(bs);
3698 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3699 goto fail;
3702 fail:
3703 qmp_output_visitor_cleanup(ov);
3706 void qmp_x_blockdev_del(bool has_id, const char *id,
3707 bool has_node_name, const char *node_name, Error **errp)
3709 AioContext *aio_context;
3710 BlockBackend *blk;
3711 BlockDriverState *bs;
3713 if (has_id && has_node_name) {
3714 error_setg(errp, "Only one of id and node-name must be specified");
3715 return;
3716 } else if (!has_id && !has_node_name) {
3717 error_setg(errp, "No block device specified");
3718 return;
3721 if (has_id) {
3722 blk = blk_by_name(id);
3723 if (!blk) {
3724 error_setg(errp, "Cannot find block backend %s", id);
3725 return;
3727 if (blk_get_refcnt(blk) > 1) {
3728 error_setg(errp, "Block backend %s is in use", id);
3729 return;
3731 bs = blk_bs(blk);
3732 aio_context = blk_get_aio_context(blk);
3733 } else {
3734 bs = bdrv_find_node(node_name);
3735 if (!bs) {
3736 error_setg(errp, "Cannot find node %s", node_name);
3737 return;
3739 blk = bs->blk;
3740 if (blk) {
3741 error_setg(errp, "Node %s is in use by %s",
3742 node_name, blk_name(blk));
3743 return;
3745 aio_context = bdrv_get_aio_context(bs);
3748 aio_context_acquire(aio_context);
3750 if (bs) {
3751 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3752 goto out;
3755 if (bs->refcnt > 1 || !QLIST_EMPTY(&bs->parents)) {
3756 error_setg(errp, "Block device %s is in use",
3757 bdrv_get_device_or_node_name(bs));
3758 goto out;
3762 if (blk) {
3763 blk_unref(blk);
3764 } else {
3765 bdrv_unref(bs);
3768 out:
3769 aio_context_release(aio_context);
3772 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3774 BlockJobInfoList *head = NULL, **p_next = &head;
3775 BlockDriverState *bs;
3777 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3778 AioContext *aio_context = bdrv_get_aio_context(bs);
3780 aio_context_acquire(aio_context);
3782 if (bs->job) {
3783 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3784 elem->value = block_job_query(bs->job);
3785 *p_next = elem;
3786 p_next = &elem->next;
3789 aio_context_release(aio_context);
3792 return head;
3795 QemuOptsList qemu_common_drive_opts = {
3796 .name = "drive",
3797 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3798 .desc = {
3800 .name = "snapshot",
3801 .type = QEMU_OPT_BOOL,
3802 .help = "enable/disable snapshot mode",
3804 .name = "discard",
3805 .type = QEMU_OPT_STRING,
3806 .help = "discard operation (ignore/off, unmap/on)",
3808 .name = BDRV_OPT_CACHE_WB,
3809 .type = QEMU_OPT_BOOL,
3810 .help = "enables writeback mode for any caches",
3812 .name = BDRV_OPT_CACHE_DIRECT,
3813 .type = QEMU_OPT_BOOL,
3814 .help = "enables use of O_DIRECT (bypass the host page cache)",
3816 .name = BDRV_OPT_CACHE_NO_FLUSH,
3817 .type = QEMU_OPT_BOOL,
3818 .help = "ignore any flush requests for the device",
3820 .name = "aio",
3821 .type = QEMU_OPT_STRING,
3822 .help = "host AIO implementation (threads, native)",
3824 .name = "format",
3825 .type = QEMU_OPT_STRING,
3826 .help = "disk format (raw, qcow2, ...)",
3828 .name = "rerror",
3829 .type = QEMU_OPT_STRING,
3830 .help = "read error action",
3832 .name = "werror",
3833 .type = QEMU_OPT_STRING,
3834 .help = "write error action",
3836 .name = "read-only",
3837 .type = QEMU_OPT_BOOL,
3838 .help = "open drive file as read-only",
3840 .name = "throttling.iops-total",
3841 .type = QEMU_OPT_NUMBER,
3842 .help = "limit total I/O operations per second",
3844 .name = "throttling.iops-read",
3845 .type = QEMU_OPT_NUMBER,
3846 .help = "limit read operations per second",
3848 .name = "throttling.iops-write",
3849 .type = QEMU_OPT_NUMBER,
3850 .help = "limit write operations per second",
3852 .name = "throttling.bps-total",
3853 .type = QEMU_OPT_NUMBER,
3854 .help = "limit total bytes per second",
3856 .name = "throttling.bps-read",
3857 .type = QEMU_OPT_NUMBER,
3858 .help = "limit read bytes per second",
3860 .name = "throttling.bps-write",
3861 .type = QEMU_OPT_NUMBER,
3862 .help = "limit write bytes per second",
3864 .name = "throttling.iops-total-max",
3865 .type = QEMU_OPT_NUMBER,
3866 .help = "I/O operations burst",
3868 .name = "throttling.iops-read-max",
3869 .type = QEMU_OPT_NUMBER,
3870 .help = "I/O operations read burst",
3872 .name = "throttling.iops-write-max",
3873 .type = QEMU_OPT_NUMBER,
3874 .help = "I/O operations write burst",
3876 .name = "throttling.bps-total-max",
3877 .type = QEMU_OPT_NUMBER,
3878 .help = "total bytes burst",
3880 .name = "throttling.bps-read-max",
3881 .type = QEMU_OPT_NUMBER,
3882 .help = "total bytes read burst",
3884 .name = "throttling.bps-write-max",
3885 .type = QEMU_OPT_NUMBER,
3886 .help = "total bytes write burst",
3888 .name = "throttling.iops-size",
3889 .type = QEMU_OPT_NUMBER,
3890 .help = "when limiting by iops max size of an I/O in bytes",
3892 .name = "throttling.group",
3893 .type = QEMU_OPT_STRING,
3894 .help = "name of the block throttling group",
3896 .name = "copy-on-read",
3897 .type = QEMU_OPT_BOOL,
3898 .help = "copy read data from backing file into image file",
3900 .name = "detect-zeroes",
3901 .type = QEMU_OPT_STRING,
3902 .help = "try to optimize zero writes (off, on, unmap)",
3904 { /* end of list */ }
3908 static QemuOptsList qemu_root_bds_opts = {
3909 .name = "root-bds",
3910 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3911 .desc = {
3913 .name = "discard",
3914 .type = QEMU_OPT_STRING,
3915 .help = "discard operation (ignore/off, unmap/on)",
3917 .name = "cache.writeback",
3918 .type = QEMU_OPT_BOOL,
3919 .help = "enables writeback mode for any caches",
3921 .name = "cache.direct",
3922 .type = QEMU_OPT_BOOL,
3923 .help = "enables use of O_DIRECT (bypass the host page cache)",
3925 .name = "cache.no-flush",
3926 .type = QEMU_OPT_BOOL,
3927 .help = "ignore any flush requests for the device",
3929 .name = "aio",
3930 .type = QEMU_OPT_STRING,
3931 .help = "host AIO implementation (threads, native)",
3933 .name = "read-only",
3934 .type = QEMU_OPT_BOOL,
3935 .help = "open drive file as read-only",
3937 .name = "copy-on-read",
3938 .type = QEMU_OPT_BOOL,
3939 .help = "copy read data from backing file into image file",
3941 .name = "detect-zeroes",
3942 .type = QEMU_OPT_STRING,
3943 .help = "try to optimize zero writes (off, on, unmap)",
3945 { /* end of list */ }
3949 QemuOptsList qemu_drive_opts = {
3950 .name = "drive",
3951 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3952 .desc = {
3954 * no elements => accept any params
3955 * validation will happen later
3957 { /* end of list */ }