block: Add BlockJobTxn support to backup_run
[qemu/ar7.git] / blockdev.c
blob5ed4284fbff1dd654cb33bbbd43a7ba74b949c03
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, 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 * @entry: List membership for all Actions in this Transaction.
1368 * This structure must be arranged as first member in a subclassed type,
1369 * assuming that the compiler will also arrange it to the same offsets as the
1370 * base class.
1372 struct BlkActionState {
1373 TransactionAction *action;
1374 const BlkActionOps *ops;
1375 QSIMPLEQ_ENTRY(BlkActionState) entry;
1378 /* internal snapshot private data */
1379 typedef struct InternalSnapshotState {
1380 BlkActionState common;
1381 BlockDriverState *bs;
1382 AioContext *aio_context;
1383 QEMUSnapshotInfo sn;
1384 bool created;
1385 } InternalSnapshotState;
1387 static void internal_snapshot_prepare(BlkActionState *common,
1388 Error **errp)
1390 Error *local_err = NULL;
1391 const char *device;
1392 const char *name;
1393 BlockBackend *blk;
1394 BlockDriverState *bs;
1395 QEMUSnapshotInfo old_sn, *sn;
1396 bool ret;
1397 qemu_timeval tv;
1398 BlockdevSnapshotInternal *internal;
1399 InternalSnapshotState *state;
1400 int ret1;
1402 g_assert(common->action->type ==
1403 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1404 internal = common->action->u.blockdev_snapshot_internal_sync;
1405 state = DO_UPCAST(InternalSnapshotState, common, common);
1407 /* 1. parse input */
1408 device = internal->device;
1409 name = internal->name;
1411 /* 2. check for validation */
1412 blk = blk_by_name(device);
1413 if (!blk) {
1414 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1415 "Device '%s' not found", device);
1416 return;
1419 /* AioContext is released in .clean() */
1420 state->aio_context = blk_get_aio_context(blk);
1421 aio_context_acquire(state->aio_context);
1423 if (!blk_is_available(blk)) {
1424 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1425 return;
1427 bs = blk_bs(blk);
1429 state->bs = bs;
1430 bdrv_drained_begin(bs);
1432 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1433 return;
1436 if (bdrv_is_read_only(bs)) {
1437 error_setg(errp, "Device '%s' is read only", device);
1438 return;
1441 if (!bdrv_can_snapshot(bs)) {
1442 error_setg(errp, "Block format '%s' used by device '%s' "
1443 "does not support internal snapshots",
1444 bs->drv->format_name, device);
1445 return;
1448 if (!strlen(name)) {
1449 error_setg(errp, "Name is empty");
1450 return;
1453 /* check whether a snapshot with name exist */
1454 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1455 &local_err);
1456 if (local_err) {
1457 error_propagate(errp, local_err);
1458 return;
1459 } else if (ret) {
1460 error_setg(errp,
1461 "Snapshot with name '%s' already exists on device '%s'",
1462 name, device);
1463 return;
1466 /* 3. take the snapshot */
1467 sn = &state->sn;
1468 pstrcpy(sn->name, sizeof(sn->name), name);
1469 qemu_gettimeofday(&tv);
1470 sn->date_sec = tv.tv_sec;
1471 sn->date_nsec = tv.tv_usec * 1000;
1472 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1474 ret1 = bdrv_snapshot_create(bs, sn);
1475 if (ret1 < 0) {
1476 error_setg_errno(errp, -ret1,
1477 "Failed to create snapshot '%s' on device '%s'",
1478 name, device);
1479 return;
1482 /* 4. succeed, mark a snapshot is created */
1483 state->created = true;
1486 static void internal_snapshot_abort(BlkActionState *common)
1488 InternalSnapshotState *state =
1489 DO_UPCAST(InternalSnapshotState, common, common);
1490 BlockDriverState *bs = state->bs;
1491 QEMUSnapshotInfo *sn = &state->sn;
1492 Error *local_error = NULL;
1494 if (!state->created) {
1495 return;
1498 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1499 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1500 "device '%s' in abort: %s",
1501 sn->id_str,
1502 sn->name,
1503 bdrv_get_device_name(bs),
1504 error_get_pretty(local_error));
1505 error_free(local_error);
1509 static void internal_snapshot_clean(BlkActionState *common)
1511 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1512 common, common);
1514 if (state->aio_context) {
1515 if (state->bs) {
1516 bdrv_drained_end(state->bs);
1518 aio_context_release(state->aio_context);
1522 /* external snapshot private data */
1523 typedef struct ExternalSnapshotState {
1524 BlkActionState common;
1525 BlockDriverState *old_bs;
1526 BlockDriverState *new_bs;
1527 AioContext *aio_context;
1528 } ExternalSnapshotState;
1530 static void external_snapshot_prepare(BlkActionState *common,
1531 Error **errp)
1533 int flags = 0, ret;
1534 QDict *options = NULL;
1535 Error *local_err = NULL;
1536 /* Device and node name of the image to generate the snapshot from */
1537 const char *device;
1538 const char *node_name;
1539 /* Reference to the new image (for 'blockdev-snapshot') */
1540 const char *snapshot_ref;
1541 /* File name of the new image (for 'blockdev-snapshot-sync') */
1542 const char *new_image_file;
1543 ExternalSnapshotState *state =
1544 DO_UPCAST(ExternalSnapshotState, common, common);
1545 TransactionAction *action = common->action;
1547 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1548 * purpose but a different set of parameters */
1549 switch (action->type) {
1550 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1552 BlockdevSnapshot *s = action->u.blockdev_snapshot;
1553 device = s->node;
1554 node_name = s->node;
1555 new_image_file = NULL;
1556 snapshot_ref = s->overlay;
1558 break;
1559 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1561 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1562 device = s->has_device ? s->device : NULL;
1563 node_name = s->has_node_name ? s->node_name : NULL;
1564 new_image_file = s->snapshot_file;
1565 snapshot_ref = NULL;
1567 break;
1568 default:
1569 g_assert_not_reached();
1572 /* start processing */
1573 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1574 if (!state->old_bs) {
1575 return;
1578 /* Acquire AioContext now so any threads operating on old_bs stop */
1579 state->aio_context = bdrv_get_aio_context(state->old_bs);
1580 aio_context_acquire(state->aio_context);
1581 bdrv_drained_begin(state->old_bs);
1583 if (!bdrv_is_inserted(state->old_bs)) {
1584 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1585 return;
1588 if (bdrv_op_is_blocked(state->old_bs,
1589 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1590 return;
1593 if (!bdrv_is_read_only(state->old_bs)) {
1594 if (bdrv_flush(state->old_bs)) {
1595 error_setg(errp, QERR_IO_ERROR);
1596 return;
1600 if (!bdrv_is_first_non_filter(state->old_bs)) {
1601 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1602 return;
1605 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
1606 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1607 const char *format = s->has_format ? s->format : "qcow2";
1608 enum NewImageMode mode;
1609 const char *snapshot_node_name =
1610 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
1612 if (node_name && !snapshot_node_name) {
1613 error_setg(errp, "New snapshot node name missing");
1614 return;
1617 if (snapshot_node_name &&
1618 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1619 error_setg(errp, "New snapshot node name already in use");
1620 return;
1623 flags = state->old_bs->open_flags;
1625 /* create new image w/backing file */
1626 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1627 if (mode != NEW_IMAGE_MODE_EXISTING) {
1628 bdrv_img_create(new_image_file, format,
1629 state->old_bs->filename,
1630 state->old_bs->drv->format_name,
1631 NULL, -1, flags, &local_err, false);
1632 if (local_err) {
1633 error_propagate(errp, local_err);
1634 return;
1638 options = qdict_new();
1639 if (s->has_snapshot_node_name) {
1640 qdict_put(options, "node-name",
1641 qstring_from_str(snapshot_node_name));
1643 qdict_put(options, "driver", qstring_from_str(format));
1645 flags |= BDRV_O_NO_BACKING;
1648 assert(state->new_bs == NULL);
1649 ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options,
1650 flags, errp);
1651 /* We will manually add the backing_hd field to the bs later */
1652 if (ret != 0) {
1653 return;
1656 if (state->new_bs->blk != NULL) {
1657 error_setg(errp, "The snapshot is already in use by %s",
1658 blk_name(state->new_bs->blk));
1659 return;
1662 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1663 errp)) {
1664 return;
1667 if (state->new_bs->backing != NULL) {
1668 error_setg(errp, "The snapshot already has a backing image");
1669 return;
1672 if (!state->new_bs->drv->supports_backing) {
1673 error_setg(errp, "The snapshot does not support backing images");
1677 static void external_snapshot_commit(BlkActionState *common)
1679 ExternalSnapshotState *state =
1680 DO_UPCAST(ExternalSnapshotState, common, common);
1682 bdrv_set_aio_context(state->new_bs, state->aio_context);
1684 /* This removes our old bs and adds the new bs */
1685 bdrv_append(state->new_bs, state->old_bs);
1686 /* We don't need (or want) to use the transactional
1687 * bdrv_reopen_multiple() across all the entries at once, because we
1688 * don't want to abort all of them if one of them fails the reopen */
1689 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1690 NULL);
1693 static void external_snapshot_abort(BlkActionState *common)
1695 ExternalSnapshotState *state =
1696 DO_UPCAST(ExternalSnapshotState, common, common);
1697 if (state->new_bs) {
1698 bdrv_unref(state->new_bs);
1702 static void external_snapshot_clean(BlkActionState *common)
1704 ExternalSnapshotState *state =
1705 DO_UPCAST(ExternalSnapshotState, common, common);
1706 if (state->aio_context) {
1707 bdrv_drained_end(state->old_bs);
1708 aio_context_release(state->aio_context);
1712 typedef struct DriveBackupState {
1713 BlkActionState common;
1714 BlockDriverState *bs;
1715 AioContext *aio_context;
1716 BlockJob *job;
1717 } DriveBackupState;
1719 static void do_drive_backup(const char *device, const char *target,
1720 bool has_format, const char *format,
1721 enum MirrorSyncMode sync,
1722 bool has_mode, enum NewImageMode mode,
1723 bool has_speed, int64_t speed,
1724 bool has_bitmap, const char *bitmap,
1725 bool has_on_source_error,
1726 BlockdevOnError on_source_error,
1727 bool has_on_target_error,
1728 BlockdevOnError on_target_error,
1729 BlockJobTxn *txn, Error **errp);
1731 static void drive_backup_prepare(BlkActionState *common, Error **errp)
1733 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1734 BlockBackend *blk;
1735 DriveBackup *backup;
1736 Error *local_err = NULL;
1738 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1739 backup = common->action->u.drive_backup;
1741 blk = blk_by_name(backup->device);
1742 if (!blk) {
1743 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1744 "Device '%s' not found", backup->device);
1745 return;
1748 if (!blk_is_available(blk)) {
1749 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1750 return;
1753 /* AioContext is released in .clean() */
1754 state->aio_context = blk_get_aio_context(blk);
1755 aio_context_acquire(state->aio_context);
1756 bdrv_drained_begin(blk_bs(blk));
1757 state->bs = blk_bs(blk);
1759 do_drive_backup(backup->device, backup->target,
1760 backup->has_format, backup->format,
1761 backup->sync,
1762 backup->has_mode, backup->mode,
1763 backup->has_speed, backup->speed,
1764 backup->has_bitmap, backup->bitmap,
1765 backup->has_on_source_error, backup->on_source_error,
1766 backup->has_on_target_error, backup->on_target_error,
1767 NULL, &local_err);
1768 if (local_err) {
1769 error_propagate(errp, local_err);
1770 return;
1773 state->job = state->bs->job;
1776 static void drive_backup_abort(BlkActionState *common)
1778 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1779 BlockDriverState *bs = state->bs;
1781 /* Only cancel if it's the job we started */
1782 if (bs && bs->job && bs->job == state->job) {
1783 block_job_cancel_sync(bs->job);
1787 static void drive_backup_clean(BlkActionState *common)
1789 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1791 if (state->aio_context) {
1792 bdrv_drained_end(state->bs);
1793 aio_context_release(state->aio_context);
1797 typedef struct BlockdevBackupState {
1798 BlkActionState common;
1799 BlockDriverState *bs;
1800 BlockJob *job;
1801 AioContext *aio_context;
1802 } BlockdevBackupState;
1804 static void do_blockdev_backup(const char *device, const char *target,
1805 enum MirrorSyncMode sync,
1806 bool has_speed, int64_t speed,
1807 bool has_on_source_error,
1808 BlockdevOnError on_source_error,
1809 bool has_on_target_error,
1810 BlockdevOnError on_target_error,
1811 BlockJobTxn *txn, Error **errp);
1813 static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1815 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1816 BlockdevBackup *backup;
1817 BlockBackend *blk, *target;
1818 Error *local_err = NULL;
1820 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1821 backup = common->action->u.blockdev_backup;
1823 blk = blk_by_name(backup->device);
1824 if (!blk) {
1825 error_setg(errp, "Device '%s' not found", backup->device);
1826 return;
1829 if (!blk_is_available(blk)) {
1830 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1831 return;
1834 target = blk_by_name(backup->target);
1835 if (!target) {
1836 error_setg(errp, "Device '%s' not found", backup->target);
1837 return;
1840 /* AioContext is released in .clean() */
1841 state->aio_context = blk_get_aio_context(blk);
1842 if (state->aio_context != blk_get_aio_context(target)) {
1843 state->aio_context = NULL;
1844 error_setg(errp, "Backup between two IO threads is not implemented");
1845 return;
1847 aio_context_acquire(state->aio_context);
1848 state->bs = blk_bs(blk);
1849 bdrv_drained_begin(state->bs);
1851 do_blockdev_backup(backup->device, backup->target,
1852 backup->sync,
1853 backup->has_speed, backup->speed,
1854 backup->has_on_source_error, backup->on_source_error,
1855 backup->has_on_target_error, backup->on_target_error,
1856 NULL, &local_err);
1857 if (local_err) {
1858 error_propagate(errp, local_err);
1859 return;
1862 state->job = state->bs->job;
1865 static void blockdev_backup_abort(BlkActionState *common)
1867 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1868 BlockDriverState *bs = state->bs;
1870 /* Only cancel if it's the job we started */
1871 if (bs && bs->job && bs->job == state->job) {
1872 block_job_cancel_sync(bs->job);
1876 static void blockdev_backup_clean(BlkActionState *common)
1878 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1880 if (state->aio_context) {
1881 bdrv_drained_end(state->bs);
1882 aio_context_release(state->aio_context);
1886 typedef struct BlockDirtyBitmapState {
1887 BlkActionState common;
1888 BdrvDirtyBitmap *bitmap;
1889 BlockDriverState *bs;
1890 AioContext *aio_context;
1891 HBitmap *backup;
1892 bool prepared;
1893 } BlockDirtyBitmapState;
1895 static void block_dirty_bitmap_add_prepare(BlkActionState *common,
1896 Error **errp)
1898 Error *local_err = NULL;
1899 BlockDirtyBitmapAdd *action;
1900 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1901 common, common);
1903 action = common->action->u.block_dirty_bitmap_add;
1904 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1905 qmp_block_dirty_bitmap_add(action->node, action->name,
1906 action->has_granularity, action->granularity,
1907 &local_err);
1909 if (!local_err) {
1910 state->prepared = true;
1911 } else {
1912 error_propagate(errp, local_err);
1916 static void block_dirty_bitmap_add_abort(BlkActionState *common)
1918 BlockDirtyBitmapAdd *action;
1919 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1920 common, common);
1922 action = common->action->u.block_dirty_bitmap_add;
1923 /* Should not be able to fail: IF the bitmap was added via .prepare(),
1924 * then the node reference and bitmap name must have been valid.
1926 if (state->prepared) {
1927 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
1931 static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
1932 Error **errp)
1934 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1935 common, common);
1936 BlockDirtyBitmap *action;
1938 action = common->action->u.block_dirty_bitmap_clear;
1939 state->bitmap = block_dirty_bitmap_lookup(action->node,
1940 action->name,
1941 &state->bs,
1942 &state->aio_context,
1943 errp);
1944 if (!state->bitmap) {
1945 return;
1948 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
1949 error_setg(errp, "Cannot modify a frozen bitmap");
1950 return;
1951 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
1952 error_setg(errp, "Cannot clear a disabled bitmap");
1953 return;
1956 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
1957 /* AioContext is released in .clean() */
1960 static void block_dirty_bitmap_clear_abort(BlkActionState *common)
1962 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1963 common, common);
1965 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
1968 static void block_dirty_bitmap_clear_commit(BlkActionState *common)
1970 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1971 common, common);
1973 hbitmap_free(state->backup);
1976 static void block_dirty_bitmap_clear_clean(BlkActionState *common)
1978 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1979 common, common);
1981 if (state->aio_context) {
1982 aio_context_release(state->aio_context);
1986 static void abort_prepare(BlkActionState *common, Error **errp)
1988 error_setg(errp, "Transaction aborted using Abort action");
1991 static void abort_commit(BlkActionState *common)
1993 g_assert_not_reached(); /* this action never succeeds */
1996 static const BlkActionOps actions[] = {
1997 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
1998 .instance_size = sizeof(ExternalSnapshotState),
1999 .prepare = external_snapshot_prepare,
2000 .commit = external_snapshot_commit,
2001 .abort = external_snapshot_abort,
2003 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2004 .instance_size = sizeof(ExternalSnapshotState),
2005 .prepare = external_snapshot_prepare,
2006 .commit = external_snapshot_commit,
2007 .abort = external_snapshot_abort,
2008 .clean = external_snapshot_clean,
2010 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2011 .instance_size = sizeof(DriveBackupState),
2012 .prepare = drive_backup_prepare,
2013 .abort = drive_backup_abort,
2014 .clean = drive_backup_clean,
2016 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2017 .instance_size = sizeof(BlockdevBackupState),
2018 .prepare = blockdev_backup_prepare,
2019 .abort = blockdev_backup_abort,
2020 .clean = blockdev_backup_clean,
2022 [TRANSACTION_ACTION_KIND_ABORT] = {
2023 .instance_size = sizeof(BlkActionState),
2024 .prepare = abort_prepare,
2025 .commit = abort_commit,
2027 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2028 .instance_size = sizeof(InternalSnapshotState),
2029 .prepare = internal_snapshot_prepare,
2030 .abort = internal_snapshot_abort,
2031 .clean = internal_snapshot_clean,
2033 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2034 .instance_size = sizeof(BlockDirtyBitmapState),
2035 .prepare = block_dirty_bitmap_add_prepare,
2036 .abort = block_dirty_bitmap_add_abort,
2038 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2039 .instance_size = sizeof(BlockDirtyBitmapState),
2040 .prepare = block_dirty_bitmap_clear_prepare,
2041 .commit = block_dirty_bitmap_clear_commit,
2042 .abort = block_dirty_bitmap_clear_abort,
2043 .clean = block_dirty_bitmap_clear_clean,
2048 * 'Atomic' group operations. The operations are performed as a set, and if
2049 * any fail then we roll back all operations in the group.
2051 void qmp_transaction(TransactionActionList *dev_list, Error **errp)
2053 TransactionActionList *dev_entry = dev_list;
2054 BlkActionState *state, *next;
2055 Error *local_err = NULL;
2057 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
2058 QSIMPLEQ_INIT(&snap_bdrv_states);
2060 /* drain all i/o before any operations */
2061 bdrv_drain_all();
2063 /* We don't do anything in this loop that commits us to the operations */
2064 while (NULL != dev_entry) {
2065 TransactionAction *dev_info = NULL;
2066 const BlkActionOps *ops;
2068 dev_info = dev_entry->value;
2069 dev_entry = dev_entry->next;
2071 assert(dev_info->type < ARRAY_SIZE(actions));
2073 ops = &actions[dev_info->type];
2074 assert(ops->instance_size > 0);
2076 state = g_malloc0(ops->instance_size);
2077 state->ops = ops;
2078 state->action = dev_info;
2079 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
2081 state->ops->prepare(state, &local_err);
2082 if (local_err) {
2083 error_propagate(errp, local_err);
2084 goto delete_and_fail;
2088 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2089 if (state->ops->commit) {
2090 state->ops->commit(state);
2094 /* success */
2095 goto exit;
2097 delete_and_fail:
2098 /* failure, and it is all-or-none; roll back all operations */
2099 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2100 if (state->ops->abort) {
2101 state->ops->abort(state);
2104 exit:
2105 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2106 if (state->ops->clean) {
2107 state->ops->clean(state);
2109 g_free(state);
2113 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
2115 Error *local_err = NULL;
2117 qmp_blockdev_open_tray(device, has_force, force, &local_err);
2118 if (local_err) {
2119 error_propagate(errp, local_err);
2120 return;
2123 qmp_blockdev_remove_medium(device, errp);
2126 void qmp_block_passwd(bool has_device, const char *device,
2127 bool has_node_name, const char *node_name,
2128 const char *password, Error **errp)
2130 Error *local_err = NULL;
2131 BlockDriverState *bs;
2132 AioContext *aio_context;
2134 bs = bdrv_lookup_bs(has_device ? device : NULL,
2135 has_node_name ? node_name : NULL,
2136 &local_err);
2137 if (local_err) {
2138 error_propagate(errp, local_err);
2139 return;
2142 aio_context = bdrv_get_aio_context(bs);
2143 aio_context_acquire(aio_context);
2145 bdrv_add_key(bs, password, errp);
2147 aio_context_release(aio_context);
2150 void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
2151 Error **errp)
2153 BlockBackend *blk;
2154 bool locked;
2156 if (!has_force) {
2157 force = false;
2160 blk = blk_by_name(device);
2161 if (!blk) {
2162 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2163 "Device '%s' not found", device);
2164 return;
2167 if (!blk_dev_has_removable_media(blk)) {
2168 error_setg(errp, "Device '%s' is not removable", device);
2169 return;
2172 if (blk_dev_is_tray_open(blk)) {
2173 return;
2176 locked = blk_dev_is_medium_locked(blk);
2177 if (locked) {
2178 blk_dev_eject_request(blk, force);
2181 if (!locked || force) {
2182 blk_dev_change_media_cb(blk, false);
2186 void qmp_blockdev_close_tray(const char *device, Error **errp)
2188 BlockBackend *blk;
2190 blk = blk_by_name(device);
2191 if (!blk) {
2192 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2193 "Device '%s' not found", device);
2194 return;
2197 if (!blk_dev_has_removable_media(blk)) {
2198 error_setg(errp, "Device '%s' is not removable", device);
2199 return;
2202 if (!blk_dev_is_tray_open(blk)) {
2203 return;
2206 blk_dev_change_media_cb(blk, true);
2209 void qmp_blockdev_remove_medium(const char *device, Error **errp)
2211 BlockBackend *blk;
2212 BlockDriverState *bs;
2213 AioContext *aio_context;
2214 bool has_device;
2216 blk = blk_by_name(device);
2217 if (!blk) {
2218 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2219 "Device '%s' not found", device);
2220 return;
2223 /* For BBs without a device, we can exchange the BDS tree at will */
2224 has_device = blk_get_attached_dev(blk);
2226 if (has_device && !blk_dev_has_removable_media(blk)) {
2227 error_setg(errp, "Device '%s' is not removable", device);
2228 return;
2231 if (has_device && !blk_dev_is_tray_open(blk)) {
2232 error_setg(errp, "Tray of device '%s' is not open", device);
2233 return;
2236 bs = blk_bs(blk);
2237 if (!bs) {
2238 return;
2241 aio_context = bdrv_get_aio_context(bs);
2242 aio_context_acquire(aio_context);
2244 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2245 goto out;
2248 /* This follows the convention established by bdrv_make_anon() */
2249 if (bs->device_list.tqe_prev) {
2250 QTAILQ_REMOVE(&bdrv_states, bs, device_list);
2251 bs->device_list.tqe_prev = NULL;
2254 blk_remove_bs(blk);
2256 out:
2257 aio_context_release(aio_context);
2260 static void qmp_blockdev_insert_anon_medium(const char *device,
2261 BlockDriverState *bs, Error **errp)
2263 BlockBackend *blk;
2264 bool has_device;
2266 blk = blk_by_name(device);
2267 if (!blk) {
2268 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2269 "Device '%s' not found", device);
2270 return;
2273 /* For BBs without a device, we can exchange the BDS tree at will */
2274 has_device = blk_get_attached_dev(blk);
2276 if (has_device && !blk_dev_has_removable_media(blk)) {
2277 error_setg(errp, "Device '%s' is not removable", device);
2278 return;
2281 if (has_device && !blk_dev_is_tray_open(blk)) {
2282 error_setg(errp, "Tray of device '%s' is not open", device);
2283 return;
2286 if (blk_bs(blk)) {
2287 error_setg(errp, "There already is a medium in device '%s'", device);
2288 return;
2291 blk_insert_bs(blk, bs);
2293 QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
2296 void qmp_blockdev_insert_medium(const char *device, const char *node_name,
2297 Error **errp)
2299 BlockDriverState *bs;
2301 bs = bdrv_find_node(node_name);
2302 if (!bs) {
2303 error_setg(errp, "Node '%s' not found", node_name);
2304 return;
2307 if (bs->blk) {
2308 error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
2309 blk_name(bs->blk));
2310 return;
2313 qmp_blockdev_insert_anon_medium(device, bs, errp);
2316 void qmp_blockdev_change_medium(const char *device, const char *filename,
2317 bool has_format, const char *format,
2318 bool has_read_only,
2319 BlockdevChangeReadOnlyMode read_only,
2320 Error **errp)
2322 BlockBackend *blk;
2323 BlockDriverState *medium_bs = NULL;
2324 int bdrv_flags, ret;
2325 QDict *options = NULL;
2326 Error *err = NULL;
2328 blk = blk_by_name(device);
2329 if (!blk) {
2330 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2331 "Device '%s' not found", device);
2332 goto fail;
2335 if (blk_bs(blk)) {
2336 blk_update_root_state(blk);
2339 bdrv_flags = blk_get_open_flags_from_root_state(blk);
2341 if (!has_read_only) {
2342 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2345 switch (read_only) {
2346 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2347 break;
2349 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2350 bdrv_flags &= ~BDRV_O_RDWR;
2351 break;
2353 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2354 bdrv_flags |= BDRV_O_RDWR;
2355 break;
2357 default:
2358 abort();
2361 if (has_format) {
2362 options = qdict_new();
2363 qdict_put(options, "driver", qstring_from_str(format));
2366 assert(!medium_bs);
2367 ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2368 if (ret < 0) {
2369 goto fail;
2372 blk_apply_root_state(blk, medium_bs);
2374 bdrv_add_key(medium_bs, NULL, &err);
2375 if (err) {
2376 error_propagate(errp, err);
2377 goto fail;
2380 qmp_blockdev_open_tray(device, false, false, &err);
2381 if (err) {
2382 error_propagate(errp, err);
2383 goto fail;
2386 qmp_blockdev_remove_medium(device, &err);
2387 if (err) {
2388 error_propagate(errp, err);
2389 goto fail;
2392 qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2393 if (err) {
2394 error_propagate(errp, err);
2395 goto fail;
2398 qmp_blockdev_close_tray(device, errp);
2400 fail:
2401 /* If the medium has been inserted, the device has its own reference, so
2402 * ours must be relinquished; and if it has not been inserted successfully,
2403 * the reference must be relinquished anyway */
2404 bdrv_unref(medium_bs);
2407 /* throttling disk I/O limits */
2408 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
2409 int64_t bps_wr,
2410 int64_t iops,
2411 int64_t iops_rd,
2412 int64_t iops_wr,
2413 bool has_bps_max,
2414 int64_t bps_max,
2415 bool has_bps_rd_max,
2416 int64_t bps_rd_max,
2417 bool has_bps_wr_max,
2418 int64_t bps_wr_max,
2419 bool has_iops_max,
2420 int64_t iops_max,
2421 bool has_iops_rd_max,
2422 int64_t iops_rd_max,
2423 bool has_iops_wr_max,
2424 int64_t iops_wr_max,
2425 bool has_iops_size,
2426 int64_t iops_size,
2427 bool has_group,
2428 const char *group, Error **errp)
2430 ThrottleConfig cfg;
2431 BlockDriverState *bs;
2432 BlockBackend *blk;
2433 AioContext *aio_context;
2435 blk = blk_by_name(device);
2436 if (!blk) {
2437 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2438 "Device '%s' not found", device);
2439 return;
2442 aio_context = blk_get_aio_context(blk);
2443 aio_context_acquire(aio_context);
2445 bs = blk_bs(blk);
2446 if (!bs) {
2447 error_setg(errp, "Device '%s' has no medium", device);
2448 goto out;
2451 memset(&cfg, 0, sizeof(cfg));
2452 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2453 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
2454 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
2456 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2457 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
2458 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2460 if (has_bps_max) {
2461 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
2463 if (has_bps_rd_max) {
2464 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
2466 if (has_bps_wr_max) {
2467 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2469 if (has_iops_max) {
2470 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2472 if (has_iops_rd_max) {
2473 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2475 if (has_iops_wr_max) {
2476 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2479 if (has_iops_size) {
2480 cfg.op_size = iops_size;
2483 if (!check_throttle_config(&cfg, errp)) {
2484 goto out;
2487 if (throttle_enabled(&cfg)) {
2488 /* Enable I/O limits if they're not enabled yet, otherwise
2489 * just update the throttling group. */
2490 if (!bs->throttle_state) {
2491 bdrv_io_limits_enable(bs, has_group ? group : device);
2492 } else if (has_group) {
2493 bdrv_io_limits_update_group(bs, group);
2495 /* Set the new throttling configuration */
2496 bdrv_set_io_limits(bs, &cfg);
2497 } else if (bs->throttle_state) {
2498 /* If all throttling settings are set to 0, disable I/O limits */
2499 bdrv_io_limits_disable(bs);
2502 out:
2503 aio_context_release(aio_context);
2506 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2507 bool has_granularity, uint32_t granularity,
2508 Error **errp)
2510 AioContext *aio_context;
2511 BlockDriverState *bs;
2513 if (!name || name[0] == '\0') {
2514 error_setg(errp, "Bitmap name cannot be empty");
2515 return;
2518 bs = bdrv_lookup_bs(node, node, errp);
2519 if (!bs) {
2520 return;
2523 aio_context = bdrv_get_aio_context(bs);
2524 aio_context_acquire(aio_context);
2526 if (has_granularity) {
2527 if (granularity < 512 || !is_power_of_2(granularity)) {
2528 error_setg(errp, "Granularity must be power of 2 "
2529 "and at least 512");
2530 goto out;
2532 } else {
2533 /* Default to cluster size, if available: */
2534 granularity = bdrv_get_default_bitmap_granularity(bs);
2537 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2539 out:
2540 aio_context_release(aio_context);
2543 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2544 Error **errp)
2546 AioContext *aio_context;
2547 BlockDriverState *bs;
2548 BdrvDirtyBitmap *bitmap;
2550 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2551 if (!bitmap || !bs) {
2552 return;
2555 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2556 error_setg(errp,
2557 "Bitmap '%s' is currently frozen and cannot be removed",
2558 name);
2559 goto out;
2561 bdrv_dirty_bitmap_make_anon(bitmap);
2562 bdrv_release_dirty_bitmap(bs, bitmap);
2564 out:
2565 aio_context_release(aio_context);
2569 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2570 * immediately after a full backup operation.
2572 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2573 Error **errp)
2575 AioContext *aio_context;
2576 BdrvDirtyBitmap *bitmap;
2577 BlockDriverState *bs;
2579 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2580 if (!bitmap || !bs) {
2581 return;
2584 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2585 error_setg(errp,
2586 "Bitmap '%s' is currently frozen and cannot be modified",
2587 name);
2588 goto out;
2589 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2590 error_setg(errp,
2591 "Bitmap '%s' is currently disabled and cannot be cleared",
2592 name);
2593 goto out;
2596 bdrv_clear_dirty_bitmap(bitmap, NULL);
2598 out:
2599 aio_context_release(aio_context);
2602 void hmp_drive_del(Monitor *mon, const QDict *qdict)
2604 const char *id = qdict_get_str(qdict, "id");
2605 BlockBackend *blk;
2606 BlockDriverState *bs;
2607 AioContext *aio_context;
2608 Error *local_err = NULL;
2610 blk = blk_by_name(id);
2611 if (!blk) {
2612 error_report("Device '%s' not found", id);
2613 return;
2616 if (!blk_legacy_dinfo(blk)) {
2617 error_report("Deleting device added with blockdev-add"
2618 " is not supported");
2619 return;
2622 aio_context = blk_get_aio_context(blk);
2623 aio_context_acquire(aio_context);
2625 bs = blk_bs(blk);
2626 if (bs) {
2627 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2628 error_report_err(local_err);
2629 aio_context_release(aio_context);
2630 return;
2633 bdrv_close(bs);
2636 /* if we have a device attached to this BlockDriverState
2637 * then we need to make the drive anonymous until the device
2638 * can be removed. If this is a drive with no device backing
2639 * then we can just get rid of the block driver state right here.
2641 if (blk_get_attached_dev(blk)) {
2642 blk_hide_on_behalf_of_hmp_drive_del(blk);
2643 /* Further I/O must not pause the guest */
2644 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2645 BLOCKDEV_ON_ERROR_REPORT);
2646 } else {
2647 blk_unref(blk);
2650 aio_context_release(aio_context);
2653 void qmp_block_resize(bool has_device, const char *device,
2654 bool has_node_name, const char *node_name,
2655 int64_t size, Error **errp)
2657 Error *local_err = NULL;
2658 BlockDriverState *bs;
2659 AioContext *aio_context;
2660 int ret;
2662 bs = bdrv_lookup_bs(has_device ? device : NULL,
2663 has_node_name ? node_name : NULL,
2664 &local_err);
2665 if (local_err) {
2666 error_propagate(errp, local_err);
2667 return;
2670 aio_context = bdrv_get_aio_context(bs);
2671 aio_context_acquire(aio_context);
2673 if (!bdrv_is_first_non_filter(bs)) {
2674 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2675 goto out;
2678 if (size < 0) {
2679 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2680 goto out;
2683 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2684 error_setg(errp, QERR_DEVICE_IN_USE, device);
2685 goto out;
2688 /* complete all in-flight operations before resizing the device */
2689 bdrv_drain_all();
2691 ret = bdrv_truncate(bs, size);
2692 switch (ret) {
2693 case 0:
2694 break;
2695 case -ENOMEDIUM:
2696 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2697 break;
2698 case -ENOTSUP:
2699 error_setg(errp, QERR_UNSUPPORTED);
2700 break;
2701 case -EACCES:
2702 error_setg(errp, "Device '%s' is read only", device);
2703 break;
2704 case -EBUSY:
2705 error_setg(errp, QERR_DEVICE_IN_USE, device);
2706 break;
2707 default:
2708 error_setg_errno(errp, -ret, "Could not resize");
2709 break;
2712 out:
2713 aio_context_release(aio_context);
2716 static void block_job_cb(void *opaque, int ret)
2718 /* Note that this function may be executed from another AioContext besides
2719 * the QEMU main loop. If you need to access anything that assumes the
2720 * QEMU global mutex, use a BH or introduce a mutex.
2723 BlockDriverState *bs = opaque;
2724 const char *msg = NULL;
2726 trace_block_job_cb(bs, bs->job, ret);
2728 assert(bs->job);
2730 if (ret < 0) {
2731 msg = strerror(-ret);
2734 if (block_job_is_cancelled(bs->job)) {
2735 block_job_event_cancelled(bs->job);
2736 } else {
2737 block_job_event_completed(bs->job, msg);
2741 void qmp_block_stream(const char *device,
2742 bool has_base, const char *base,
2743 bool has_backing_file, const char *backing_file,
2744 bool has_speed, int64_t speed,
2745 bool has_on_error, BlockdevOnError on_error,
2746 Error **errp)
2748 BlockBackend *blk;
2749 BlockDriverState *bs;
2750 BlockDriverState *base_bs = NULL;
2751 AioContext *aio_context;
2752 Error *local_err = NULL;
2753 const char *base_name = NULL;
2755 if (!has_on_error) {
2756 on_error = BLOCKDEV_ON_ERROR_REPORT;
2759 blk = blk_by_name(device);
2760 if (!blk) {
2761 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2762 "Device '%s' not found", device);
2763 return;
2766 aio_context = blk_get_aio_context(blk);
2767 aio_context_acquire(aio_context);
2769 if (!blk_is_available(blk)) {
2770 error_setg(errp, "Device '%s' has no medium", device);
2771 goto out;
2773 bs = blk_bs(blk);
2775 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2776 goto out;
2779 if (has_base) {
2780 base_bs = bdrv_find_backing_image(bs, base);
2781 if (base_bs == NULL) {
2782 error_setg(errp, QERR_BASE_NOT_FOUND, base);
2783 goto out;
2785 assert(bdrv_get_aio_context(base_bs) == aio_context);
2786 base_name = base;
2789 /* if we are streaming the entire chain, the result will have no backing
2790 * file, and specifying one is therefore an error */
2791 if (base_bs == NULL && has_backing_file) {
2792 error_setg(errp, "backing file specified, but streaming the "
2793 "entire chain");
2794 goto out;
2797 /* backing_file string overrides base bs filename */
2798 base_name = has_backing_file ? backing_file : base_name;
2800 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
2801 on_error, block_job_cb, bs, &local_err);
2802 if (local_err) {
2803 error_propagate(errp, local_err);
2804 goto out;
2807 trace_qmp_block_stream(bs, bs->job);
2809 out:
2810 aio_context_release(aio_context);
2813 void qmp_block_commit(const char *device,
2814 bool has_base, const char *base,
2815 bool has_top, const char *top,
2816 bool has_backing_file, const char *backing_file,
2817 bool has_speed, int64_t speed,
2818 Error **errp)
2820 BlockBackend *blk;
2821 BlockDriverState *bs;
2822 BlockDriverState *base_bs, *top_bs;
2823 AioContext *aio_context;
2824 Error *local_err = NULL;
2825 /* This will be part of the QMP command, if/when the
2826 * BlockdevOnError change for blkmirror makes it in
2828 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
2830 if (!has_speed) {
2831 speed = 0;
2834 /* Important Note:
2835 * libvirt relies on the DeviceNotFound error class in order to probe for
2836 * live commit feature versions; for this to work, we must make sure to
2837 * perform the device lookup before any generic errors that may occur in a
2838 * scenario in which all optional arguments are omitted. */
2839 blk = blk_by_name(device);
2840 if (!blk) {
2841 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2842 "Device '%s' not found", device);
2843 return;
2846 aio_context = blk_get_aio_context(blk);
2847 aio_context_acquire(aio_context);
2849 if (!blk_is_available(blk)) {
2850 error_setg(errp, "Device '%s' has no medium", device);
2851 goto out;
2853 bs = blk_bs(blk);
2855 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
2856 goto out;
2859 /* default top_bs is the active layer */
2860 top_bs = bs;
2862 if (has_top && top) {
2863 if (strcmp(bs->filename, top) != 0) {
2864 top_bs = bdrv_find_backing_image(bs, top);
2868 if (top_bs == NULL) {
2869 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
2870 goto out;
2873 assert(bdrv_get_aio_context(top_bs) == aio_context);
2875 if (has_base && base) {
2876 base_bs = bdrv_find_backing_image(top_bs, base);
2877 } else {
2878 base_bs = bdrv_find_base(top_bs);
2881 if (base_bs == NULL) {
2882 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
2883 goto out;
2886 assert(bdrv_get_aio_context(base_bs) == aio_context);
2888 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2889 goto out;
2892 /* Do not allow attempts to commit an image into itself */
2893 if (top_bs == base_bs) {
2894 error_setg(errp, "cannot commit an image into itself");
2895 goto out;
2898 if (top_bs == bs) {
2899 if (has_backing_file) {
2900 error_setg(errp, "'backing-file' specified,"
2901 " but 'top' is the active layer");
2902 goto out;
2904 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
2905 bs, &local_err);
2906 } else {
2907 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
2908 has_backing_file ? backing_file : NULL, &local_err);
2910 if (local_err != NULL) {
2911 error_propagate(errp, local_err);
2912 goto out;
2915 out:
2916 aio_context_release(aio_context);
2919 static void do_drive_backup(const char *device, const char *target,
2920 bool has_format, const char *format,
2921 enum MirrorSyncMode sync,
2922 bool has_mode, enum NewImageMode mode,
2923 bool has_speed, int64_t speed,
2924 bool has_bitmap, const char *bitmap,
2925 bool has_on_source_error,
2926 BlockdevOnError on_source_error,
2927 bool has_on_target_error,
2928 BlockdevOnError on_target_error,
2929 BlockJobTxn *txn, Error **errp)
2931 BlockBackend *blk;
2932 BlockDriverState *bs;
2933 BlockDriverState *target_bs;
2934 BlockDriverState *source = NULL;
2935 BdrvDirtyBitmap *bmap = NULL;
2936 AioContext *aio_context;
2937 QDict *options = NULL;
2938 Error *local_err = NULL;
2939 int flags;
2940 int64_t size;
2941 int ret;
2943 if (!has_speed) {
2944 speed = 0;
2946 if (!has_on_source_error) {
2947 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2949 if (!has_on_target_error) {
2950 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2952 if (!has_mode) {
2953 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2956 blk = blk_by_name(device);
2957 if (!blk) {
2958 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2959 "Device '%s' not found", device);
2960 return;
2963 aio_context = blk_get_aio_context(blk);
2964 aio_context_acquire(aio_context);
2966 /* Although backup_run has this check too, we need to use bs->drv below, so
2967 * do an early check redundantly. */
2968 if (!blk_is_available(blk)) {
2969 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2970 goto out;
2972 bs = blk_bs(blk);
2974 if (!has_format) {
2975 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2978 /* Early check to avoid creating target */
2979 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
2980 goto out;
2983 flags = bs->open_flags | BDRV_O_RDWR;
2985 /* See if we have a backing HD we can use to create our new image
2986 * on top of. */
2987 if (sync == MIRROR_SYNC_MODE_TOP) {
2988 source = backing_bs(bs);
2989 if (!source) {
2990 sync = MIRROR_SYNC_MODE_FULL;
2993 if (sync == MIRROR_SYNC_MODE_NONE) {
2994 source = bs;
2997 size = bdrv_getlength(bs);
2998 if (size < 0) {
2999 error_setg_errno(errp, -size, "bdrv_getlength failed");
3000 goto out;
3003 if (mode != NEW_IMAGE_MODE_EXISTING) {
3004 assert(format);
3005 if (source) {
3006 bdrv_img_create(target, format, source->filename,
3007 source->drv->format_name, NULL,
3008 size, flags, &local_err, false);
3009 } else {
3010 bdrv_img_create(target, format, NULL, NULL, NULL,
3011 size, flags, &local_err, false);
3015 if (local_err) {
3016 error_propagate(errp, local_err);
3017 goto out;
3020 if (format) {
3021 options = qdict_new();
3022 qdict_put(options, "driver", qstring_from_str(format));
3025 target_bs = NULL;
3026 ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
3027 if (ret < 0) {
3028 error_propagate(errp, local_err);
3029 goto out;
3032 bdrv_set_aio_context(target_bs, aio_context);
3034 if (has_bitmap) {
3035 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3036 if (!bmap) {
3037 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
3038 goto out;
3042 backup_start(bs, target_bs, speed, sync, bmap,
3043 on_source_error, on_target_error,
3044 block_job_cb, bs, txn, &local_err);
3045 if (local_err != NULL) {
3046 bdrv_unref(target_bs);
3047 error_propagate(errp, local_err);
3048 goto out;
3051 out:
3052 aio_context_release(aio_context);
3055 void qmp_drive_backup(const char *device, const char *target,
3056 bool has_format, const char *format,
3057 enum MirrorSyncMode sync,
3058 bool has_mode, enum NewImageMode mode,
3059 bool has_speed, int64_t speed,
3060 bool has_bitmap, const char *bitmap,
3061 bool has_on_source_error, BlockdevOnError on_source_error,
3062 bool has_on_target_error, BlockdevOnError on_target_error,
3063 Error **errp)
3065 return do_drive_backup(device, target, has_format, format, sync,
3066 has_mode, mode, has_speed, speed,
3067 has_bitmap, bitmap,
3068 has_on_source_error, on_source_error,
3069 has_on_target_error, on_target_error,
3070 NULL, errp);
3073 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3075 return bdrv_named_nodes_list(errp);
3078 void do_blockdev_backup(const char *device, const char *target,
3079 enum MirrorSyncMode sync,
3080 bool has_speed, int64_t speed,
3081 bool has_on_source_error,
3082 BlockdevOnError on_source_error,
3083 bool has_on_target_error,
3084 BlockdevOnError on_target_error,
3085 BlockJobTxn *txn, Error **errp)
3087 BlockBackend *blk, *target_blk;
3088 BlockDriverState *bs;
3089 BlockDriverState *target_bs;
3090 Error *local_err = NULL;
3091 AioContext *aio_context;
3093 if (!has_speed) {
3094 speed = 0;
3096 if (!has_on_source_error) {
3097 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3099 if (!has_on_target_error) {
3100 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3103 blk = blk_by_name(device);
3104 if (!blk) {
3105 error_setg(errp, "Device '%s' not found", device);
3106 return;
3109 aio_context = blk_get_aio_context(blk);
3110 aio_context_acquire(aio_context);
3112 if (!blk_is_available(blk)) {
3113 error_setg(errp, "Device '%s' has no medium", device);
3114 goto out;
3116 bs = blk_bs(blk);
3118 target_blk = blk_by_name(target);
3119 if (!target_blk) {
3120 error_setg(errp, "Device '%s' not found", target);
3121 goto out;
3124 if (!blk_is_available(target_blk)) {
3125 error_setg(errp, "Device '%s' has no medium", target);
3126 goto out;
3128 target_bs = blk_bs(target_blk);
3130 bdrv_ref(target_bs);
3131 bdrv_set_aio_context(target_bs, aio_context);
3132 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
3133 on_target_error, block_job_cb, bs, txn, &local_err);
3134 if (local_err != NULL) {
3135 bdrv_unref(target_bs);
3136 error_propagate(errp, local_err);
3138 out:
3139 aio_context_release(aio_context);
3142 void qmp_blockdev_backup(const char *device, const char *target,
3143 enum MirrorSyncMode sync,
3144 bool has_speed, int64_t speed,
3145 bool has_on_source_error,
3146 BlockdevOnError on_source_error,
3147 bool has_on_target_error,
3148 BlockdevOnError on_target_error,
3149 Error **errp)
3151 do_blockdev_backup(device, target, sync, has_speed, speed,
3152 has_on_source_error, on_source_error,
3153 has_on_target_error, on_target_error,
3154 NULL, errp);
3157 void qmp_drive_mirror(const char *device, const char *target,
3158 bool has_format, const char *format,
3159 bool has_node_name, const char *node_name,
3160 bool has_replaces, const char *replaces,
3161 enum MirrorSyncMode sync,
3162 bool has_mode, enum NewImageMode mode,
3163 bool has_speed, int64_t speed,
3164 bool has_granularity, uint32_t granularity,
3165 bool has_buf_size, int64_t buf_size,
3166 bool has_on_source_error, BlockdevOnError on_source_error,
3167 bool has_on_target_error, BlockdevOnError on_target_error,
3168 bool has_unmap, bool unmap,
3169 Error **errp)
3171 BlockBackend *blk;
3172 BlockDriverState *bs;
3173 BlockDriverState *source, *target_bs;
3174 AioContext *aio_context;
3175 Error *local_err = NULL;
3176 QDict *options;
3177 int flags;
3178 int64_t size;
3179 int ret;
3181 if (!has_speed) {
3182 speed = 0;
3184 if (!has_on_source_error) {
3185 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3187 if (!has_on_target_error) {
3188 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3190 if (!has_mode) {
3191 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3193 if (!has_granularity) {
3194 granularity = 0;
3196 if (!has_buf_size) {
3197 buf_size = 0;
3199 if (!has_unmap) {
3200 unmap = true;
3203 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3204 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3205 "a value in range [512B, 64MB]");
3206 return;
3208 if (granularity & (granularity - 1)) {
3209 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3210 "power of 2");
3211 return;
3214 blk = blk_by_name(device);
3215 if (!blk) {
3216 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3217 "Device '%s' not found", device);
3218 return;
3221 aio_context = blk_get_aio_context(blk);
3222 aio_context_acquire(aio_context);
3224 if (!blk_is_available(blk)) {
3225 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3226 goto out;
3228 bs = blk_bs(blk);
3230 if (!has_format) {
3231 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3234 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
3235 goto out;
3238 flags = bs->open_flags | BDRV_O_RDWR;
3239 source = backing_bs(bs);
3240 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
3241 sync = MIRROR_SYNC_MODE_FULL;
3243 if (sync == MIRROR_SYNC_MODE_NONE) {
3244 source = bs;
3247 size = bdrv_getlength(bs);
3248 if (size < 0) {
3249 error_setg_errno(errp, -size, "bdrv_getlength failed");
3250 goto out;
3253 if (has_replaces) {
3254 BlockDriverState *to_replace_bs;
3255 AioContext *replace_aio_context;
3256 int64_t replace_size;
3258 if (!has_node_name) {
3259 error_setg(errp, "a node-name must be provided when replacing a"
3260 " named node of the graph");
3261 goto out;
3264 to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
3266 if (!to_replace_bs) {
3267 error_propagate(errp, local_err);
3268 goto out;
3271 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3272 aio_context_acquire(replace_aio_context);
3273 replace_size = bdrv_getlength(to_replace_bs);
3274 aio_context_release(replace_aio_context);
3276 if (size != replace_size) {
3277 error_setg(errp, "cannot replace image with a mirror image of "
3278 "different size");
3279 goto out;
3283 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
3284 && mode != NEW_IMAGE_MODE_EXISTING)
3286 /* create new image w/o backing file */
3287 assert(format);
3288 bdrv_img_create(target, format,
3289 NULL, NULL, NULL, size, flags, &local_err, false);
3290 } else {
3291 switch (mode) {
3292 case NEW_IMAGE_MODE_EXISTING:
3293 break;
3294 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3295 /* create new image with backing file */
3296 bdrv_img_create(target, format,
3297 source->filename,
3298 source->drv->format_name,
3299 NULL, size, flags, &local_err, false);
3300 break;
3301 default:
3302 abort();
3306 if (local_err) {
3307 error_propagate(errp, local_err);
3308 goto out;
3311 options = qdict_new();
3312 if (has_node_name) {
3313 qdict_put(options, "node-name", qstring_from_str(node_name));
3315 if (format) {
3316 qdict_put(options, "driver", qstring_from_str(format));
3319 /* Mirroring takes care of copy-on-write using the source's backing
3320 * file.
3322 target_bs = NULL;
3323 ret = bdrv_open(&target_bs, target, NULL, options,
3324 flags | BDRV_O_NO_BACKING, &local_err);
3325 if (ret < 0) {
3326 error_propagate(errp, local_err);
3327 goto out;
3330 bdrv_set_aio_context(target_bs, aio_context);
3332 /* pass the node name to replace to mirror start since it's loose coupling
3333 * and will allow to check whether the node still exist at mirror completion
3335 mirror_start(bs, target_bs,
3336 has_replaces ? replaces : NULL,
3337 speed, granularity, buf_size, sync,
3338 on_source_error, on_target_error,
3339 unmap,
3340 block_job_cb, bs, &local_err);
3341 if (local_err != NULL) {
3342 bdrv_unref(target_bs);
3343 error_propagate(errp, local_err);
3344 goto out;
3347 out:
3348 aio_context_release(aio_context);
3351 /* Get the block job for a given device name and acquire its AioContext */
3352 static BlockJob *find_block_job(const char *device, AioContext **aio_context,
3353 Error **errp)
3355 BlockBackend *blk;
3356 BlockDriverState *bs;
3358 *aio_context = NULL;
3360 blk = blk_by_name(device);
3361 if (!blk) {
3362 goto notfound;
3365 *aio_context = blk_get_aio_context(blk);
3366 aio_context_acquire(*aio_context);
3368 if (!blk_is_available(blk)) {
3369 goto notfound;
3371 bs = blk_bs(blk);
3373 if (!bs->job) {
3374 goto notfound;
3377 return bs->job;
3379 notfound:
3380 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3381 "No active block job on device '%s'", device);
3382 if (*aio_context) {
3383 aio_context_release(*aio_context);
3384 *aio_context = NULL;
3386 return NULL;
3389 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
3391 AioContext *aio_context;
3392 BlockJob *job = find_block_job(device, &aio_context, errp);
3394 if (!job) {
3395 return;
3398 block_job_set_speed(job, speed, errp);
3399 aio_context_release(aio_context);
3402 void qmp_block_job_cancel(const char *device,
3403 bool has_force, bool force, Error **errp)
3405 AioContext *aio_context;
3406 BlockJob *job = find_block_job(device, &aio_context, errp);
3408 if (!job) {
3409 return;
3412 if (!has_force) {
3413 force = false;
3416 if (job->user_paused && !force) {
3417 error_setg(errp, "The block job for device '%s' is currently paused",
3418 device);
3419 goto out;
3422 trace_qmp_block_job_cancel(job);
3423 block_job_cancel(job);
3424 out:
3425 aio_context_release(aio_context);
3428 void qmp_block_job_pause(const char *device, Error **errp)
3430 AioContext *aio_context;
3431 BlockJob *job = find_block_job(device, &aio_context, errp);
3433 if (!job || job->user_paused) {
3434 return;
3437 job->user_paused = true;
3438 trace_qmp_block_job_pause(job);
3439 block_job_pause(job);
3440 aio_context_release(aio_context);
3443 void qmp_block_job_resume(const char *device, Error **errp)
3445 AioContext *aio_context;
3446 BlockJob *job = find_block_job(device, &aio_context, errp);
3448 if (!job || !job->user_paused) {
3449 return;
3452 job->user_paused = false;
3453 trace_qmp_block_job_resume(job);
3454 block_job_resume(job);
3455 aio_context_release(aio_context);
3458 void qmp_block_job_complete(const char *device, Error **errp)
3460 AioContext *aio_context;
3461 BlockJob *job = find_block_job(device, &aio_context, errp);
3463 if (!job) {
3464 return;
3467 trace_qmp_block_job_complete(job);
3468 block_job_complete(job, errp);
3469 aio_context_release(aio_context);
3472 void qmp_change_backing_file(const char *device,
3473 const char *image_node_name,
3474 const char *backing_file,
3475 Error **errp)
3477 BlockBackend *blk;
3478 BlockDriverState *bs = NULL;
3479 AioContext *aio_context;
3480 BlockDriverState *image_bs = NULL;
3481 Error *local_err = NULL;
3482 bool ro;
3483 int open_flags;
3484 int ret;
3486 blk = blk_by_name(device);
3487 if (!blk) {
3488 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3489 "Device '%s' not found", device);
3490 return;
3493 aio_context = blk_get_aio_context(blk);
3494 aio_context_acquire(aio_context);
3496 if (!blk_is_available(blk)) {
3497 error_setg(errp, "Device '%s' has no medium", device);
3498 goto out;
3500 bs = blk_bs(blk);
3502 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3503 if (local_err) {
3504 error_propagate(errp, local_err);
3505 goto out;
3508 if (!image_bs) {
3509 error_setg(errp, "image file not found");
3510 goto out;
3513 if (bdrv_find_base(image_bs) == image_bs) {
3514 error_setg(errp, "not allowing backing file change on an image "
3515 "without a backing file");
3516 goto out;
3519 /* even though we are not necessarily operating on bs, we need it to
3520 * determine if block ops are currently prohibited on the chain */
3521 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3522 goto out;
3525 /* final sanity check */
3526 if (!bdrv_chain_contains(bs, image_bs)) {
3527 error_setg(errp, "'%s' and image file are not in the same chain",
3528 device);
3529 goto out;
3532 /* if not r/w, reopen to make r/w */
3533 open_flags = image_bs->open_flags;
3534 ro = bdrv_is_read_only(image_bs);
3536 if (ro) {
3537 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3538 if (local_err) {
3539 error_propagate(errp, local_err);
3540 goto out;
3544 ret = bdrv_change_backing_file(image_bs, backing_file,
3545 image_bs->drv ? image_bs->drv->format_name : "");
3547 if (ret < 0) {
3548 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3549 backing_file);
3550 /* don't exit here, so we can try to restore open flags if
3551 * appropriate */
3554 if (ro) {
3555 bdrv_reopen(image_bs, open_flags, &local_err);
3556 if (local_err) {
3557 error_propagate(errp, local_err); /* will preserve prior errp */
3561 out:
3562 aio_context_release(aio_context);
3565 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3567 QmpOutputVisitor *ov = qmp_output_visitor_new();
3568 BlockDriverState *bs;
3569 BlockBackend *blk = NULL;
3570 QObject *obj;
3571 QDict *qdict;
3572 Error *local_err = NULL;
3574 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3575 * cache.direct=false instead of silently switching to aio=threads, except
3576 * when called from drive_new().
3578 * For now, simply forbidding the combination for all drivers will do. */
3579 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3580 bool direct = options->has_cache &&
3581 options->cache->has_direct &&
3582 options->cache->direct;
3583 if (!direct) {
3584 error_setg(errp, "aio=native requires cache.direct=true");
3585 goto fail;
3589 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
3590 &options, NULL, &local_err);
3591 if (local_err) {
3592 error_propagate(errp, local_err);
3593 goto fail;
3596 obj = qmp_output_get_qobject(ov);
3597 qdict = qobject_to_qdict(obj);
3599 qdict_flatten(qdict);
3601 if (options->has_id) {
3602 blk = blockdev_init(NULL, qdict, &local_err);
3603 if (local_err) {
3604 error_propagate(errp, local_err);
3605 goto fail;
3608 bs = blk_bs(blk);
3609 } else {
3610 if (!qdict_get_try_str(qdict, "node-name")) {
3611 error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3612 "the root node");
3613 goto fail;
3616 bs = bds_tree_init(qdict, errp);
3617 if (!bs) {
3618 goto fail;
3622 if (bs && bdrv_key_required(bs)) {
3623 if (blk) {
3624 blk_unref(blk);
3625 } else {
3626 bdrv_unref(bs);
3628 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3629 goto fail;
3632 fail:
3633 qmp_output_visitor_cleanup(ov);
3636 void qmp_x_blockdev_del(bool has_id, const char *id,
3637 bool has_node_name, const char *node_name, Error **errp)
3639 AioContext *aio_context;
3640 BlockBackend *blk;
3641 BlockDriverState *bs;
3643 if (has_id && has_node_name) {
3644 error_setg(errp, "Only one of id and node-name must be specified");
3645 return;
3646 } else if (!has_id && !has_node_name) {
3647 error_setg(errp, "No block device specified");
3648 return;
3651 if (has_id) {
3652 blk = blk_by_name(id);
3653 if (!blk) {
3654 error_setg(errp, "Cannot find block backend %s", id);
3655 return;
3657 if (blk_get_refcnt(blk) > 1) {
3658 error_setg(errp, "Block backend %s is in use", id);
3659 return;
3661 bs = blk_bs(blk);
3662 aio_context = blk_get_aio_context(blk);
3663 } else {
3664 bs = bdrv_find_node(node_name);
3665 if (!bs) {
3666 error_setg(errp, "Cannot find node %s", node_name);
3667 return;
3669 blk = bs->blk;
3670 if (blk) {
3671 error_setg(errp, "Node %s is in use by %s",
3672 node_name, blk_name(blk));
3673 return;
3675 aio_context = bdrv_get_aio_context(bs);
3678 aio_context_acquire(aio_context);
3680 if (bs) {
3681 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3682 goto out;
3685 if (bs->refcnt > 1 || !QLIST_EMPTY(&bs->parents)) {
3686 error_setg(errp, "Block device %s is in use",
3687 bdrv_get_device_or_node_name(bs));
3688 goto out;
3692 if (blk) {
3693 blk_unref(blk);
3694 } else {
3695 bdrv_unref(bs);
3698 out:
3699 aio_context_release(aio_context);
3702 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3704 BlockJobInfoList *head = NULL, **p_next = &head;
3705 BlockDriverState *bs;
3707 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3708 AioContext *aio_context = bdrv_get_aio_context(bs);
3710 aio_context_acquire(aio_context);
3712 if (bs->job) {
3713 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3714 elem->value = block_job_query(bs->job);
3715 *p_next = elem;
3716 p_next = &elem->next;
3719 aio_context_release(aio_context);
3722 return head;
3725 QemuOptsList qemu_common_drive_opts = {
3726 .name = "drive",
3727 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3728 .desc = {
3730 .name = "snapshot",
3731 .type = QEMU_OPT_BOOL,
3732 .help = "enable/disable snapshot mode",
3734 .name = "discard",
3735 .type = QEMU_OPT_STRING,
3736 .help = "discard operation (ignore/off, unmap/on)",
3738 .name = BDRV_OPT_CACHE_WB,
3739 .type = QEMU_OPT_BOOL,
3740 .help = "enables writeback mode for any caches",
3742 .name = BDRV_OPT_CACHE_DIRECT,
3743 .type = QEMU_OPT_BOOL,
3744 .help = "enables use of O_DIRECT (bypass the host page cache)",
3746 .name = BDRV_OPT_CACHE_NO_FLUSH,
3747 .type = QEMU_OPT_BOOL,
3748 .help = "ignore any flush requests for the device",
3750 .name = "aio",
3751 .type = QEMU_OPT_STRING,
3752 .help = "host AIO implementation (threads, native)",
3754 .name = "format",
3755 .type = QEMU_OPT_STRING,
3756 .help = "disk format (raw, qcow2, ...)",
3758 .name = "rerror",
3759 .type = QEMU_OPT_STRING,
3760 .help = "read error action",
3762 .name = "werror",
3763 .type = QEMU_OPT_STRING,
3764 .help = "write error action",
3766 .name = "read-only",
3767 .type = QEMU_OPT_BOOL,
3768 .help = "open drive file as read-only",
3770 .name = "throttling.iops-total",
3771 .type = QEMU_OPT_NUMBER,
3772 .help = "limit total I/O operations per second",
3774 .name = "throttling.iops-read",
3775 .type = QEMU_OPT_NUMBER,
3776 .help = "limit read operations per second",
3778 .name = "throttling.iops-write",
3779 .type = QEMU_OPT_NUMBER,
3780 .help = "limit write operations per second",
3782 .name = "throttling.bps-total",
3783 .type = QEMU_OPT_NUMBER,
3784 .help = "limit total bytes per second",
3786 .name = "throttling.bps-read",
3787 .type = QEMU_OPT_NUMBER,
3788 .help = "limit read bytes per second",
3790 .name = "throttling.bps-write",
3791 .type = QEMU_OPT_NUMBER,
3792 .help = "limit write bytes per second",
3794 .name = "throttling.iops-total-max",
3795 .type = QEMU_OPT_NUMBER,
3796 .help = "I/O operations burst",
3798 .name = "throttling.iops-read-max",
3799 .type = QEMU_OPT_NUMBER,
3800 .help = "I/O operations read burst",
3802 .name = "throttling.iops-write-max",
3803 .type = QEMU_OPT_NUMBER,
3804 .help = "I/O operations write burst",
3806 .name = "throttling.bps-total-max",
3807 .type = QEMU_OPT_NUMBER,
3808 .help = "total bytes burst",
3810 .name = "throttling.bps-read-max",
3811 .type = QEMU_OPT_NUMBER,
3812 .help = "total bytes read burst",
3814 .name = "throttling.bps-write-max",
3815 .type = QEMU_OPT_NUMBER,
3816 .help = "total bytes write burst",
3818 .name = "throttling.iops-size",
3819 .type = QEMU_OPT_NUMBER,
3820 .help = "when limiting by iops max size of an I/O in bytes",
3822 .name = "throttling.group",
3823 .type = QEMU_OPT_STRING,
3824 .help = "name of the block throttling group",
3826 .name = "copy-on-read",
3827 .type = QEMU_OPT_BOOL,
3828 .help = "copy read data from backing file into image file",
3830 .name = "detect-zeroes",
3831 .type = QEMU_OPT_STRING,
3832 .help = "try to optimize zero writes (off, on, unmap)",
3834 { /* end of list */ }
3838 static QemuOptsList qemu_root_bds_opts = {
3839 .name = "root-bds",
3840 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3841 .desc = {
3843 .name = "discard",
3844 .type = QEMU_OPT_STRING,
3845 .help = "discard operation (ignore/off, unmap/on)",
3847 .name = "cache.writeback",
3848 .type = QEMU_OPT_BOOL,
3849 .help = "enables writeback mode for any caches",
3851 .name = "cache.direct",
3852 .type = QEMU_OPT_BOOL,
3853 .help = "enables use of O_DIRECT (bypass the host page cache)",
3855 .name = "cache.no-flush",
3856 .type = QEMU_OPT_BOOL,
3857 .help = "ignore any flush requests for the device",
3859 .name = "aio",
3860 .type = QEMU_OPT_STRING,
3861 .help = "host AIO implementation (threads, native)",
3863 .name = "read-only",
3864 .type = QEMU_OPT_BOOL,
3865 .help = "open drive file as read-only",
3867 .name = "copy-on-read",
3868 .type = QEMU_OPT_BOOL,
3869 .help = "copy read data from backing file into image file",
3871 .name = "detect-zeroes",
3872 .type = QEMU_OPT_STRING,
3873 .help = "try to optimize zero writes (off, on, unmap)",
3875 { /* end of list */ }
3879 QemuOptsList qemu_drive_opts = {
3880 .name = "drive",
3881 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3882 .desc = {
3884 * no elements => accept any params
3885 * validation will happen later
3887 { /* end of list */ }